AliceVision
Photogrammetric Computer Vision Framework
Voxel.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2017 AliceVision contributors.
3 // This Source Code Form is subject to the terms of the Mozilla Public License,
4 // v. 2.0. If a copy of the MPL was not distributed with this file,
5 // You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #pragma once
8 
9 #include <cmath>
10 #include <iostream>
11 
12 namespace aliceVision {
13 
14 struct Voxel
15 {
16  union
17  {
18  struct
19  {
20  int x, y, z;
21  };
22  int m[3];
23  };
24 
25  Voxel()
26  {
27  x = 0;
28  y = 0;
29  z = 0;
30  }
31 
32  Voxel(const int _x, const int _y, const int _z)
33  {
34  x = _x;
35  y = _y;
36  z = _z;
37  }
38 
39  int& operator[](const int index) { return m[index]; }
40 
41  Voxel& operator=(const Voxel& param)
42  {
43  x = param.x;
44  y = param.y;
45  z = param.z;
46  return *this;
47  }
48 
49  Voxel operator-(const Voxel& _p) const
50  {
51  Voxel p;
52  p.x = x - _p.x;
53  p.y = y - _p.y;
54  p.z = z - _p.z;
55  return p;
56  }
57 
58  Voxel operator+(const Voxel& _p) const
59  {
60  Voxel p;
61  p.x = x + _p.x;
62  p.y = y + _p.y;
63  p.z = z + _p.z;
64  return p;
65  }
66 
67  Voxel operator+(int _p) const
68  {
69  Voxel p;
70  p.x = x + _p;
71  p.y = y + _p;
72  p.z = z + _p;
73  return p;
74  }
75 
76  Voxel operator*(const Voxel& _p) const
77  {
78  Voxel p;
79  p.x = x * _p.x;
80  p.y = y * _p.y;
81  p.z = z * _p.z;
82  return p;
83  }
84 
85  Voxel operator*(int d) const
86  {
87  Voxel p;
88  p.x = x * d;
89  p.y = y * d;
90  p.z = z * d;
91  return p;
92  }
93 
94  Voxel operator/(int d) const
95  {
96  if (d == 0)
97  return Voxel(0, 0, 0);
98 
99  Voxel p;
100  p.x = static_cast<int>(std::floor(static_cast<float>(x) / static_cast<float>(d) + 0.5f));
101  p.y = static_cast<int>(std::floor(static_cast<float>(y) / static_cast<float>(d) + 0.5f));
102  p.z = static_cast<int>(std::floor(static_cast<float>(z) / static_cast<float>(d) + 0.5f));
103  return p;
104  }
105 
106  float size() const
107  {
108  float d = static_cast<float>(x * x + y * y + z * z);
109  if (d == 0.0f)
110  return 0.0f;
111  return std::sqrt(d);
112  }
113 
114  bool operator==(const Voxel& param) const { return (x == param.x) && (y == param.y) && (z == param.z); }
115 
116  bool operator!=(const Voxel& param) const { return (x != param.x) || (y != param.y) || (z != param.z); }
117 };
118 
119 inline std::ostream& operator<<(std::ostream& out, const Voxel& v)
120 {
121  out << v.x << "," << v.y << "," << v.z;
122  return out;
123 }
124 
125 } // namespace aliceVision
aliceVision::Voxel
Definition: Voxel.hpp:14
aliceVision
Definition: checkerDetector.cpp:32