MorphoGraphX  2.0-1-227
BoundingBox.hpp
Go to the documentation of this file.
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 {
23  template <size_t N, typename T> class BoundingBox {
24  public:
26  typedef std::numeric_limits<T> limits;
27 
29  BoundingBox() { reset(); }
30 
32  BoundingBox(const Point& vmin, const Point& vmax)
33  {
34  pts[0] = vmin;
35  pts[1] = vmax;
36  }
37 
39  BoundingBox(const BoundingBox& copy)
40  {
41  pts[0] = copy.pts[0];
42  pts[1] = copy.pts[1];
43  }
44 
46  BoundingBox(const Point& p)
47  {
48  pts[0] = pts[1] = p;
49  if(limits::is_integer)
50  pts[1] += T(1); // What does this do?
51  }
52 
54  void reset()
55  {
56  T v = limits::max();
57  pts[0] = Point(v, v, v);
58  if(limits::is_signed)
59  pts[1] = -pts[0];
60  else
61  pts[1] = Point(0, 0, 0);
62  }
63 
65  bool empty() const
66  {
67  for(size_t i = 0; i < N; ++i)
68  if(pts[0][i] >= pts[1][i])
69  return true;
70  return false;
71  }
72 
74  operator bool() const { return not empty(); }
75 
77  bool operator==(const BoundingBox& other) const
78  {
79  return pts[0] == other.pts[0] and pts[1] == other.pts[1];
80  }
81 
83  bool operator!=(const BoundingBox& other) const
84  {
85  return pts[0] != other.pts[0] or pts[1] != other.pts[1];
86  }
87 
89  Point size() const {
90  return pts[1] - pts[0];
91  }
92 
98  {
99  for(size_t i = 0; i < N; ++i) {
100  if(pts[0][i] < other.pts[0][i])
101  pts[0][i] = other.pts[0][i];
102  if(pts[1][i] > other.pts[1][i])
103  pts[1][i] = other.pts[1][i];
104  }
105  return *this;
106  }
107 
109  BoundingBox operator&(const BoundingBox& other) const
110  {
111  BoundingBox copy(*this);
112  return copy &= other;
113  }
114 
117  {
118  return (*this) &= (other);
119  }
120 
122  BoundingBox operator*(const BoundingBox& other) const
123  {
124  BoundingBox copy(*this);
125  return copy &= other;
126  }
127 
133  {
134  for(size_t i = 0; i < N; ++i) {
135  if(pts[0][i] > other.pts[0][i])
136  pts[0][i] = other.pts[0][i];
137  if(pts[1][i] < other.pts[1][i])
138  pts[1][i] = other.pts[1][i];
139  }
140  return *this;
141  }
142 
144  BoundingBox operator|(const BoundingBox& other) const
145  {
146  BoundingBox copy(*this);
147  return copy |= other;
148  }
149 
152  {
153  return (*this) |= (other);
154  }
155 
157  BoundingBox operator+(const BoundingBox& other) const
158  {
159  BoundingBox copy(*this);
160  return copy |= other;
161  }
162 
168  {
169  for(size_t i = 0; i < N; ++i) {
170  if(pts[0][i] > p[i])
171  pts[0][i] = p[i];
172  if(limits::is_integer) {
173  if(pts[1][i] < p[i] + 1)
174  pts[1][i] = p[i] + 1;
175  } else {
176  if(pts[1][i] < p[i])
177  pts[1][i] = p[i];
178  }
179  }
180  return *this;
181  }
182 
184  BoundingBox operator|(const Point& p) const
185  {
186  BoundingBox copy(*this);
187  return copy |= p;
188  }
189 
191  friend BoundingBox operator&(const Point& p, const BoundingBox& b)
192  {
193  BoundingBox copy(b);
194  return copy |= p;
195  }
196 
198  const Point& operator[](int i) const
199  {
200  if(i == 0)
201  return pts[0];
202  return pts[1];
203  }
204 
207  {
208  if(i == 0)
209  return pts[0];
210  return pts[1];
211  }
212 
217  bool contains(const Point& p) const
218  {
219  return (p.x() >= pts[0].x() and p.y() >= pts[0].y() and p.z() >= pts[0].z()
220  and p.x() < pts[1].x() and p.y() < pts[1].y() and p.z() < pts[1].z());
221  }
222 
224  Point* data() {
225  return &pts[0];
226  }
227 
230  return pts[0];
231  }
232 
234  Point pmin() const {
235  return pts[0];
236  }
237 
240  return pts[1];
241  }
242 
244  Point pmax() const {
245  return pts[1];
246  }
247 
248  Point pts[2];
249 
251  friend std::ostream& operator<<(std::ostream& s, const BoundingBox& bbox)
252  {
253  s << bbox.pts[0] << " " << bbox.pts[1];
254  return s;
255  }
256 
258  friend std::istream& operator>>(std::istream& s, BoundingBox& bbox)
259  {
260  s >> bbox.pts[0] >> bbox.pts[1];
261  return s;
262  }
263 
264  #ifndef COMPILE_CUDA
265  friend QTextStream& operator<<(QTextStream& s, const BoundingBox& bbox)
266  {
267  s << bbox.pts[0] << " " << bbox.pts[1];
268  return s;
269  }
270 
271  friend QTextStream& operator>>(QTextStream& s, BoundingBox& bbox)
272  {
273  s >> bbox.pts[0] >> bbox.pts[1];
274  return s;
275  }
276  #endif
277  };
278 }
279 #endif // BOUNDINGBOX_HPP
mgx::BoundingBox::operator*
CU_HOST_DEVICE BoundingBox operator*(const BoundingBox &other) const
Definition: BoundingBox.hpp:122
Vector.hpp
mgx::BoundingBox::operator<<
CU_HOST_DEVICE friend std::ostream & operator<<(std::ostream &s, const BoundingBox &bbox)
Definition: BoundingBox.hpp:251
mgx::BoundingBox::operator==
CU_HOST_DEVICE bool operator==(const BoundingBox &other) const
Definition: BoundingBox.hpp:77
mgx::BoundingBox::operator|
CU_HOST_DEVICE BoundingBox operator|(const BoundingBox &other) const
Definition: BoundingBox.hpp:144
mgx::BoundingBox::data
CU_HOST_DEVICE Point * data()
Definition: BoundingBox.hpp:224
mgx::BoundingBox::empty
CU_HOST_DEVICE bool empty() const
Definition: BoundingBox.hpp:65
mgx::limits::max
__device__ __host__ static T max()
mgx::BoundingBox::operator>>
friend QTextStream & operator>>(QTextStream &s, BoundingBox &bbox)
Definition: BoundingBox.hpp:271
mgx::BoundingBox::operator>>
CU_HOST_DEVICE friend std::istream & operator>>(std::istream &s, BoundingBox &bbox)
Definition: BoundingBox.hpp:258
mgx::Vector::z
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:739
mgx::BoundingBox::operator[]
CU_HOST_DEVICE Point & operator[](int i)
Definition: BoundingBox.hpp:206
mgx::BoundingBox::pmax
CU_HOST_DEVICE Point pmax() const
Definition: BoundingBox.hpp:244
mgx::BoundingBox::reset
CU_HOST_DEVICE void reset()
Definition: BoundingBox.hpp:54
mgx::BoundingBox::operator|
CU_HOST_DEVICE BoundingBox operator|(const Point &p) const
Definition: BoundingBox.hpp:184
mgx::BoundingBox::operator[]
const CU_HOST_DEVICE Point & operator[](int i) const
Definition: BoundingBox.hpp:198
mgx::BoundingBox::BoundingBox
CU_HOST_DEVICE BoundingBox()
Definition: BoundingBox.hpp:29
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::BoundingBox::pmin
CU_HOST_DEVICE Point pmin()
Definition: BoundingBox.hpp:229
mgx::BoundingBox::operator+
CU_HOST_DEVICE BoundingBox operator+(const BoundingBox &other) const
Definition: BoundingBox.hpp:157
mgx::BoundingBox::BoundingBox
CU_HOST_DEVICE BoundingBox(const BoundingBox &copy)
Definition: BoundingBox.hpp:39
mgx::BoundingBox::operator&=
CU_HOST_DEVICE BoundingBox & operator&=(const BoundingBox &other)
Bounding box intersection.
Definition: BoundingBox.hpp:97
mgx::BoundingBox::operator!=
CU_HOST_DEVICE bool operator!=(const BoundingBox &other) const
Definition: BoundingBox.hpp:83
mgx::BoundingBox::operator&
CU_HOST_DEVICE BoundingBox operator&(const BoundingBox &other) const
Definition: BoundingBox.hpp:109
mgx::BoundingBox::contains
CU_HOST_DEVICE bool contains(const Point &p) const
Check if a point is in the BoundingBox.
Definition: BoundingBox.hpp:217
mgx::Vector::y
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:730
mgx::BoundingBox::BoundingBox
CU_HOST_DEVICE BoundingBox(const Point &p)
Definition: BoundingBox.hpp:46
mgx::BoundingBox::pmin
CU_HOST_DEVICE Point pmin() const
Definition: BoundingBox.hpp:234
mgx::BoundingBox::Point
Vector< N, T > Point
Definition: BoundingBox.hpp:25
mgx::BoundingBox::operator*=
CU_HOST_DEVICE BoundingBox & operator*=(const BoundingBox &other)
Definition: BoundingBox.hpp:116
mgx::BoundingBox::operator|=
CU_HOST_DEVICE BoundingBox & operator|=(const BoundingBox &other)
Bounding box union.
Definition: BoundingBox.hpp:132
mgx::BoundingBox::limits
std::numeric_limits< T > limits
Definition: BoundingBox.hpp:26
mgx::BoundingBox::operator|=
CU_HOST_DEVICE BoundingBox & operator|=(const Point &p)
Adding a point.
Definition: BoundingBox.hpp:167
mgx::BoundingBox::operator<<
friend QTextStream & operator<<(QTextStream &s, const BoundingBox &bbox)
Definition: BoundingBox.hpp:265
mgx::BoundingBox::operator+=
CU_HOST_DEVICE BoundingBox & operator+=(const BoundingBox &other)
Definition: BoundingBox.hpp:151
mgx::Vector< N, T >
mgx::BoundingBox::size
CU_HOST_DEVICE Point size() const
Definition: BoundingBox.hpp:89
mgx::BoundingBox::pmax
CU_HOST_DEVICE Point pmax()
Definition: BoundingBox.hpp:239
CU_HOST_DEVICE
#define CU_HOST_DEVICE
Definition: CudaGlobal.hpp:22
mgx::BoundingBox::pts
Point pts[2]
Definition: BoundingBox.hpp:248
mgx::BoundingBox::operator&
CU_HOST_DEVICE friend BoundingBox operator&(const Point &p, const BoundingBox &b)
Definition: BoundingBox.hpp:191
mgx::Vector::x
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:721
mgx::BoundingBox::BoundingBox
CU_HOST_DEVICE BoundingBox(const Point &vmin, const Point &vmax)
Definition: BoundingBox.hpp:32
mgx::BoundingBox
Definition: BoundingBox.hpp:23