MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BoundingBox.hpp
1 #ifndef BOUNDINGBOX_HPP
2 #define BOUNDINGBOX_HPP
3 
4 #include <Config.hpp>
5 
6 #include <Vector.hpp>
7 
8 #include <iostream>
9 #include <limits>
10 
11 namespace mgx {
12 namespace util {
13 
14 template <size_t N, typename T> class BoundingBox {
15 public:
16  typedef Vector<N, T> Point;
17  typedef std::numeric_limits<T> limits;
18 
19  CU_HOST_DEVICE
20  BoundingBox() {
21  reset();
22  }
23 
24  CU_HOST_DEVICE
25  BoundingBox(const Point& vmin, const Point& vmax)
26  {
27  pts[0] = vmin;
28  pts[1] = vmax;
29  }
30 
31  CU_HOST_DEVICE
32  BoundingBox(const BoundingBox& copy)
33  {
34  pts[0] = copy.pts[0];
35  pts[1] = copy.pts[1];
36  }
37 
38  CU_HOST_DEVICE
39  BoundingBox(const Point& p)
40  {
41  pts[0] = pts[1] = p;
42  if(limits::is_integer)
43  pts[1] += T(1); // What does this do?
44  }
45 
46  CU_HOST_DEVICE
47  void reset()
48  {
49  T v = limits::max();
50  pts[0] = Point(v, v, v);
51  if(limits::is_signed)
52  pts[1] = -pts[0];
53  else
54  pts[1] = Point(0, 0, 0);
55  }
56 
57  CU_HOST_DEVICE
58  bool empty() const
59  {
60  for(size_t i = 0; i < N; ++i)
61  if(pts[0][i] >= pts[1][i])
62  return true;
63  return false;
64  }
65 
66  CU_HOST_DEVICE
67  operator bool() const {
68  return not empty();
69  }
70 
71  CU_HOST_DEVICE
72  bool operator==(const BoundingBox& other) const {
73  return pts[0] == other.pts[0] and pts[1] == other.pts[1];
74  }
75 
76  CU_HOST_DEVICE
77  bool operator!=(const BoundingBox& other) const {
78  return pts[0] != other.pts[0] or pts[1] != other.pts[1];
79  }
80 
81  CU_HOST_DEVICE
82  Point size() const {
83  return pts[1] - pts[0];
84  }
85 
89  CU_HOST_DEVICE
91  {
92  for(size_t i = 0; i < N; ++i) {
93  if(pts[0][i] < other.pts[0][i])
94  pts[0][i] = other.pts[0][i];
95  if(pts[1][i] > other.pts[1][i])
96  pts[1][i] = other.pts[1][i];
97  }
98  return *this;
99  }
100 
101  CU_HOST_DEVICE
102  BoundingBox operator&(const BoundingBox& other) const
103  {
104  BoundingBox copy(*this);
105  return copy &= other;
106  }
107 
108  CU_HOST_DEVICE
109  BoundingBox& operator*=(const BoundingBox& other) {
110  return (*this) &= (other);
111  }
112 
113  CU_HOST_DEVICE
114  BoundingBox operator*(const BoundingBox& other) const
115  {
116  BoundingBox copy(*this);
117  return copy &= other;
118  }
119 
123  CU_HOST_DEVICE
125  {
126  for(size_t i = 0; i < N; ++i) {
127  if(pts[0][i] > other.pts[0][i])
128  pts[0][i] = other.pts[0][i];
129  if(pts[1][i] < other.pts[1][i])
130  pts[1][i] = other.pts[1][i];
131  }
132  return *this;
133  }
134 
135  CU_HOST_DEVICE
136  BoundingBox operator|(const BoundingBox& other) const
137  {
138  BoundingBox copy(*this);
139  return copy |= other;
140  }
141 
142  CU_HOST_DEVICE
143  BoundingBox& operator+=(const BoundingBox& other) {
144  return (*this) |= (other);
145  }
146 
147  CU_HOST_DEVICE
148  BoundingBox operator+(const BoundingBox& other) const
149  {
150  BoundingBox copy(*this);
151  return copy |= other;
152  }
153 
157  CU_HOST_DEVICE
159  {
160  for(size_t i = 0; i < N; ++i) {
161  if(pts[0][i] > p[i])
162  pts[0][i] = p[i];
163  if(limits::is_integer) {
164  if(pts[1][i] < p[i] + 1)
165  pts[1][i] = p[i] + 1;
166  } else {
167  if(pts[1][i] < p[i])
168  pts[1][i] = p[i];
169  }
170  }
171  return *this;
172  }
173 
174  CU_HOST_DEVICE
175  BoundingBox operator|(const Point& p) const
176  {
177  BoundingBox copy(*this);
178  return copy |= p;
179  }
180 
181  CU_HOST_DEVICE
182  friend BoundingBox operator&(const Point& p, const BoundingBox& b)
183  {
184  BoundingBox copy(b);
185  return copy |= p;
186  }
187 
188  CU_HOST_DEVICE
189  const Point& operator[](int i) const
190  {
191  if(i == 0)
192  return pts[0];
193  return pts[1];
194  }
195 
196  CU_HOST_DEVICE
197  Point& operator[](int i)
198  {
199  if(i == 0)
200  return pts[0];
201  return pts[1];
202  }
203 
207  CU_HOST_DEVICE
208  bool contains(const Point& p) const
209  {
210  return (p.x() >= pts[0].x() and p.y() >= pts[0].y() and p.z() >= pts[0].z() and p.x() < pts[1].x()
211  and p.y() < pts[1].y() and p.z() < pts[1].z());
212  }
213 
214  CU_HOST_DEVICE
215  Point* data() {
216  return &pts[0];
217  }
218 
219  CU_HOST_DEVICE
220  Point pmin() {
221  return pts[0];
222  }
223 
224  CU_HOST_DEVICE
225  Point pmin() const {
226  return pts[0];
227  }
228 
229  CU_HOST_DEVICE
230  Point pmax() {
231  return pts[1];
232  }
233 
234  CU_HOST_DEVICE
235  Point pmax() const {
236  return pts[1];
237  }
238 
239  Point pts[2];
240 
241  CU_HOST_DEVICE
242  friend std::ostream& operator<<(std::ostream& s, const BoundingBox& bbox)
243  {
244  s << bbox.pts[0] << " " << bbox.pts[1];
245  return s;
246  }
247 
248  CU_HOST_DEVICE
249  friend std::istream& operator>>(std::istream& s, BoundingBox& bbox)
250  {
251  s >> bbox.pts[0] >> bbox.pts[1];
252  return s;
253  }
254 
255 #ifndef COMPILE_CUDA
256  friend QTextStream& operator<<(QTextStream& s, const BoundingBox& bbox)
257  {
258  s << bbox.pts[0] << " " << bbox.pts[1];
259  return s;
260  }
261 
262  friend QTextStream& operator>>(QTextStream& s, BoundingBox& bbox)
263  {
264  s >> bbox.pts[0] >> bbox.pts[1];
265  return s;
266  }
267 #endif
268 };
269 } // namespace util
270 } // namespace mgx
271 #endif // BOUNDINGBOX_HPP
Definition: BoundingBox.hpp:14
CU_HOST_DEVICE BoundingBox & operator|=(const Point &p)
Adding a point.
Definition: BoundingBox.hpp:158
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:651
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:660
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:669
CU_HOST_DEVICE bool contains(const Point &p) const
Check if a point is in the BoundingBox.
Definition: BoundingBox.hpp:208
CU_HOST_DEVICE BoundingBox & operator&=(const BoundingBox &other)
Bounding box intersection.
Definition: BoundingBox.hpp:90
CU_HOST_DEVICE BoundingBox & operator|=(const BoundingBox &other)
Bounding box union.
Definition: BoundingBox.hpp:124
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can't...