MorphoGraphX
BoundingBox.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef BOUNDINGBOX_HPP
12 #define BOUNDINGBOX_HPP
13 
14 #include <Config.hpp>
15 
16 #include <Vector.hpp>
17 
18 #include <iostream>
19 #include <limits>
20 
21 namespace mgx {
22 namespace util {
23 
24 template <size_t N, typename T> class BoundingBox {
25 public:
26  typedef Vector<N, T> Point;
27  typedef std::numeric_limits<T> limits;
28 
29  CU_HOST_DEVICE
30  BoundingBox() {
31  reset();
32  }
33 
34  CU_HOST_DEVICE
35  BoundingBox(const Point& vmin, const Point& vmax)
36  {
37  pts[0] = vmin;
38  pts[1] = vmax;
39  }
40 
41  CU_HOST_DEVICE
42  BoundingBox(const BoundingBox& copy)
43  {
44  pts[0] = copy.pts[0];
45  pts[1] = copy.pts[1];
46  }
47 
48  CU_HOST_DEVICE
49  BoundingBox(const Point& p)
50  {
51  pts[0] = pts[1] = p;
52  if(limits::is_integer)
53  pts[1] += T(1); // What does this do?
54  }
55 
56  CU_HOST_DEVICE
57  void reset()
58  {
59  T v = limits::max();
60  pts[0] = Point(v, v, v);
61  if(limits::is_signed)
62  pts[1] = -pts[0];
63  else
64  pts[1] = Point(0, 0, 0);
65  }
66 
67  CU_HOST_DEVICE
68  bool empty() const
69  {
70  for(size_t i = 0; i < N; ++i)
71  if(pts[0][i] >= pts[1][i])
72  return true;
73  return false;
74  }
75 
76  CU_HOST_DEVICE
77  operator bool() const {
78  return not empty();
79  }
80 
81  CU_HOST_DEVICE
82  bool operator==(const BoundingBox& other) const {
83  return pts[0] == other.pts[0] and pts[1] == other.pts[1];
84  }
85 
86  CU_HOST_DEVICE
87  bool operator!=(const BoundingBox& other) const {
88  return pts[0] != other.pts[0] or pts[1] != other.pts[1];
89  }
90 
91  CU_HOST_DEVICE
92  Point size() const {
93  return pts[1] - pts[0];
94  }
95 
99  CU_HOST_DEVICE
101  {
102  for(size_t i = 0; i < N; ++i) {
103  if(pts[0][i] < other.pts[0][i])
104  pts[0][i] = other.pts[0][i];
105  if(pts[1][i] > other.pts[1][i])
106  pts[1][i] = other.pts[1][i];
107  }
108  return *this;
109  }
110 
111  CU_HOST_DEVICE
112  BoundingBox operator&(const BoundingBox& other) const
113  {
114  BoundingBox copy(*this);
115  return copy &= other;
116  }
117 
118  CU_HOST_DEVICE
119  BoundingBox& operator*=(const BoundingBox& other) {
120  return (*this) &= (other);
121  }
122 
123  CU_HOST_DEVICE
124  BoundingBox operator*(const BoundingBox& other) const
125  {
126  BoundingBox copy(*this);
127  return copy &= other;
128  }
129 
133  CU_HOST_DEVICE
135  {
136  for(size_t i = 0; i < N; ++i) {
137  if(pts[0][i] > other.pts[0][i])
138  pts[0][i] = other.pts[0][i];
139  if(pts[1][i] < other.pts[1][i])
140  pts[1][i] = other.pts[1][i];
141  }
142  return *this;
143  }
144 
145  CU_HOST_DEVICE
146  BoundingBox operator|(const BoundingBox& other) const
147  {
148  BoundingBox copy(*this);
149  return copy |= other;
150  }
151 
152  CU_HOST_DEVICE
153  BoundingBox& operator+=(const BoundingBox& other) {
154  return (*this) |= (other);
155  }
156 
157  CU_HOST_DEVICE
158  BoundingBox operator+(const BoundingBox& other) const
159  {
160  BoundingBox copy(*this);
161  return copy |= other;
162  }
163 
167  CU_HOST_DEVICE
168  BoundingBox& operator|=(const Point& p)
169  {
170  for(size_t i = 0; i < N; ++i) {
171  if(pts[0][i] > p[i])
172  pts[0][i] = p[i];
173  if(limits::is_integer) {
174  if(pts[1][i] < p[i] + 1)
175  pts[1][i] = p[i] + 1;
176  } else {
177  if(pts[1][i] < p[i])
178  pts[1][i] = p[i];
179  }
180  }
181  return *this;
182  }
183 
184  CU_HOST_DEVICE
185  BoundingBox operator|(const Point& p) const
186  {
187  BoundingBox copy(*this);
188  return copy |= p;
189  }
190 
191  CU_HOST_DEVICE
192  friend BoundingBox operator&(const Point& p, const BoundingBox& b)
193  {
194  BoundingBox copy(b);
195  return copy |= p;
196  }
197 
198  CU_HOST_DEVICE
199  const Point& operator[](int i) const
200  {
201  if(i == 0)
202  return pts[0];
203  return pts[1];
204  }
205 
206  CU_HOST_DEVICE
207  Point& operator[](int i)
208  {
209  if(i == 0)
210  return pts[0];
211  return pts[1];
212  }
213 
217  CU_HOST_DEVICE
218  bool contains(const Point& p) const
219  {
220  return (p.x() >= pts[0].x() and p.y() >= pts[0].y() and p.z() >= pts[0].z() and p.x() < pts[1].x()
221  and p.y() < pts[1].y() and p.z() < pts[1].z());
222  }
223 
224  CU_HOST_DEVICE
225  Point* data() {
226  return &pts[0];
227  }
228 
229  CU_HOST_DEVICE
230  Point pmin() {
231  return pts[0];
232  }
233 
234  CU_HOST_DEVICE
235  Point pmin() const {
236  return pts[0];
237  }
238 
239  CU_HOST_DEVICE
240  Point pmax() {
241  return pts[1];
242  }
243 
244  CU_HOST_DEVICE
245  Point pmax() const {
246  return pts[1];
247  }
248 
249  Point pts[2];
250 
251  CU_HOST_DEVICE
252  friend std::ostream& operator<<(std::ostream& s, const BoundingBox& bbox)
253  {
254  s << bbox.pts[0] << " " << bbox.pts[1];
255  return s;
256  }
257 
258  CU_HOST_DEVICE
259  friend std::istream& operator>>(std::istream& s, BoundingBox& bbox)
260  {
261  s >> bbox.pts[0] >> bbox.pts[1];
262  return s;
263  }
264 
265 #ifndef COMPILE_CUDA
266  friend QTextStream& operator<<(QTextStream& s, const BoundingBox& bbox)
267  {
268  s << bbox.pts[0] << " " << bbox.pts[1];
269  return s;
270  }
271 
272  friend QTextStream& operator>>(QTextStream& s, BoundingBox& bbox)
273  {
274  s >> bbox.pts[0] >> bbox.pts[1];
275  return s;
276  }
277 #endif
278 };
279 } // namespace util
280 } // namespace mgx
281 #endif // BOUNDINGBOX_HPP
Definition: BoundingBox.hpp:24
CU_HOST_DEVICE BoundingBox & operator|=(const Point &p)
Adding a point.
Definition: BoundingBox.hpp:168
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:661
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:670
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:679
CU_HOST_DEVICE bool contains(const Point &p) const
Check if a point is in the BoundingBox.
Definition: BoundingBox.hpp:218
CU_HOST_DEVICE BoundingBox & operator&=(const BoundingBox &other)
Bounding box intersection.
Definition: BoundingBox.hpp:100
CU_HOST_DEVICE BoundingBox & operator|=(const BoundingBox &other)
Bounding box union.
Definition: BoundingBox.hpp:134
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...