SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BBox.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Core/GeometryPrimitives/BBox.h
30 
31 #ifndef CORE_GEOMETRY_BBOX_H
32 #define CORE_GEOMETRY_BBOX_H 1
33 
38 
39 /// @todo replace asserts in this code with warnings or other reporting
40 /// mechanism that doesn't abort the program
41 
42 namespace SCIRun {
43 namespace Core {
44 namespace Geometry {
45 
46 class BBox {
47  public:
48  enum { INSIDE, INTERSECT, OUTSIDE };
49 
50  BBox() : is_valid_(false) {}
51 
52  BBox(const BBox& copy)
53  : cmin_(copy.cmin_), cmax_(copy.cmax_), is_valid_(copy.is_valid_) {}
54 
55  BBox& operator=(const BBox& copy)
56  {
57  is_valid_ = copy.is_valid_;
58  cmin_ = copy.cmin_;
59  cmax_ = copy.cmax_;
60  return *this;
61  }
62 
63  BBox(const BBox& b1, const BBox& b2)
64  : cmin_(b1.cmin_), cmax_(b1.cmax_), is_valid_(true)
65  {
66  extend(b2.cmin_);
67  extend(b2.cmax_);
68  }
69 
70 
71  BBox(const Point& p1, const Point& p2)
72  : cmin_(p1), cmax_(p1), is_valid_(true)
73  {
74  extend(p2);
75  }
76 
77  BBox(const Point& p1, const Point& p2, const Point& p3)
78  : cmin_(p1), cmax_(p1), is_valid_(true)
79  {
80  extend(p2);
81  extend(p3);
82  }
83 
84  explicit BBox(const std::vector<Point>& points) :
85  is_valid_(false)
86  {
87  for (size_t j=0; j<points.size(); j++)
88  {
89  extend(points[j]);
90  }
91  }
92 
93  inline bool valid() const { return is_valid_; }
94  inline void set_valid(bool v) { is_valid_ = v; }
95  inline void reset() { is_valid_ = false; }
96 
97  /// Expand the bounding box to include point p
98  inline BBox& extend(const Point& p)
99  {
100  if(is_valid_)
101  {
102  cmin_=Min(p, cmin_);
103  cmax_=Max(p, cmax_);
104  }
105  else
106  {
107  cmin_=p;
108  cmax_=p;
109  is_valid_ = true;
110  }
111  return *this;
112  }
113 
114  /// Extend the bounding box on all sides by a margin
115  /// For example to expand it by a certain epsilon to make
116  /// sure that a lookup will be inside the bounding box
117  inline void extend(double val)
118  {
119  if (is_valid_)
120  {
121  cmin_.x(cmin_.x()-val);
122  cmin_.y(cmin_.y()-val);
123  cmin_.z(cmin_.z()-val);
124  cmax_.x(cmax_.x()+val);
125  cmax_.y(cmax_.y()+val);
126  cmax_.z(cmax_.z()+val);
127  }
128  }
129 
130  /// Expand the bounding box to include a sphere of radius radius
131  /// and centered at point p
132  inline void extend(const Point& p, double radius)
133  {
134  Vector r(radius,radius,radius);
135  if(is_valid_)
136  {
137  cmin_=Min(p-r, cmin_);
138  cmax_=Max(p+r, cmax_);
139  }
140  else
141  {
142  cmin_=p-r;
143  cmax_=p+r;
144  is_valid_ = true;
145  }
146  }
147 
148  /// Expand the bounding box to include bounding box b
149  inline void extend(const BBox& b)
150  {
151  if(b.valid())
152  {
153  extend(b.min());
154  extend(b.max());
155  }
156  }
157 
158  /// Expand the bounding box to include a disk centered at cen,
159  /// with normal normal, and radius r.
160  SCISHARE void extend_disk(const Point& cen, const Vector& normal, double r);
161 
162  inline Point center() const
163  {
164  /// @todo: C assert: assert(is_valid_);
165  Vector d = diagonal();
166  return cmin_ + (d * 0.5);
167  }
168 
169  inline double longest_edge() const
170  {
171  /// @todo: C assert: assert(is_valid_);
172  Vector diagonal(cmax_-cmin_);
173  return Max(diagonal.x(), diagonal.y(), diagonal.z());
174  }
175 
176  inline double shortest_edge() const
177  {
178  /// @todo: C assert: assert(is_valid_);
179  Vector diagonal(cmax_-cmin_);
180  return Min(diagonal.x(), diagonal.y(), diagonal.z());
181  }
182 
183  /// Check whether two BBoxes are similar
184  SCISHARE bool is_similar_to(const BBox &b, double diff=0.5) const;
185 
186  /// Move the bounding box
187  SCISHARE void translate(const Vector &v);
188 
189  /// Scale the bounding box by s, centered around o
190  SCISHARE void scale(double s, const Vector &o);
191 
192  inline Point min() const
193  { return cmin_; }
194 
195  inline Point max() const
196  { return cmax_; }
197 
198  inline Vector diagonal() const
199  {
200  //TODO: needs invariant check, or refactoring.
201  ASSERT(is_valid_);
202  return cmax_-cmin_;
203  }
204 
205  inline bool inside(const Point &p) const
206  {
207  return (is_valid_ && p.x() >= cmin_.x() &&
208  p.y() >= cmin_.y() && p.z() >= cmin_.z() &&
209  p.x() <= cmax_.x() && p.y() <= cmax_.y() &&
210  p.z() <= cmax_.z());
211  }
212 
213  inline int intersect(const BBox& b) const
214  {
215  if ((cmax_.x() < b.cmin_.x()) || (cmin_.x() > b.cmax_.x()) ||
216  (cmax_.y() < b.cmin_.y()) || (cmin_.y() > b.cmax_.y()) ||
217  (cmax_.z() < b.cmin_.z()) || (cmin_.z() > b.cmax_.z()))
218  {
219  return OUTSIDE;
220  }
221 
222  if ((cmin_.x() <= b.cmin_.x()) && (cmax_.x() >= b.cmax_.x()) &&
223  (cmin_.y() <= b.cmin_.y()) && (cmax_.y() >= b.cmax_.y()) &&
224  (cmin_.z() <= b.cmin_.z()) && (cmax_.z() >= b.cmax_.z()))
225  {
226  return INSIDE;
227  }
228 
229  return INTERSECT;
230  }
231 
232  inline double x_length() { return (cmax_.x() - cmin_.x()); }
233  inline double y_length() { return (cmax_.y() - cmin_.y()); }
234  inline double z_length() { return (cmax_.z() - cmin_.z()); }
235 
236  /// bbox's that share a face overlap
237  SCISHARE bool overlaps(const BBox& bb) const;
238  /// bbox's that share a face do not overlap_inside
239  SCISHARE bool overlaps_inside(const BBox& bb) const;
240 
241  /// returns true if the ray hit the bbox and returns the hit point
242  /// in hitNear
243  SCISHARE bool intersect(const Point& e, const Vector& v, Point& hitNear);
244 
245  friend std::ostream& operator<<(std::ostream& out, const BBox& b);
246 
247  private:
248  Point cmin_;
249  Point cmax_;
250  bool is_valid_;
251 };
252 
253 SCISHARE void Pio( Piostream &, BBox& );
254 
255 }}}
256 
257 #endif
Point Min(const Point &p1, const Point &p2)
Definition: Point.h:194
BBox(const Point &p1, const Point &p2, const Point &p3)
Definition: BBox.h:77
int intersect(const BBox &b) const
Definition: BBox.h:213
void extend(const Point &p, double radius)
Definition: BBox.h:132
Point max() const
Definition: BBox.h:195
friend std::ostream & operator<<(std::ostream &out, const BBox &b)
Definition: Persistent.h:89
BBox(const std::vector< Point > &points)
Definition: BBox.h:84
void extend(double val)
Definition: BBox.h:117
void y(const double)
Definition: Point.h:135
BBox & extend(const Point &p)
Expand the bounding box to include point p.
Definition: BBox.h:98
Definition: Point.h:49
#define SCISHARE
Definition: share.h:39
Definition: BBox.h:46
Utility for specifying data invariants (Assertions)
BBox(const Point &p1, const Point &p2)
Definition: BBox.h:71
SCISHARE bool is_similar_to(const BBox &b, double diff=0.5) const
Check whether two BBoxes are similar.
Definition: BBox.cc:52
#define ASSERT(condition)
Definition: Assert.h:110
void extend(const BBox &b)
Expand the bounding box to include bounding box b.
Definition: BBox.h:149
Definition: Vector.h:63
double y_length()
Definition: BBox.h:233
void z(const double)
Definition: Point.h:145
bool valid() const
Definition: BBox.h:93
void x(const double)
Definition: Point.h:125
Vector diagonal() const
Definition: BBox.h:198
BBox(const BBox &copy)
Definition: BBox.h:52
Point min() const
Definition: BBox.h:192
SCISHARE bool overlaps(const BBox &bb) const
bbox&#39;s that share a face overlap
Definition: BBox.cc:88
Point center() const
Definition: BBox.h:162
void x(double)
Definition: Vector.h:175
SCISHARE void extend_disk(const Point &cen, const Vector &normal, double r)
Definition: BBox.cc:37
bool inside(const Point &p) const
Definition: BBox.h:205
v
Definition: readAllFields.py:42
void y(double)
Definition: Vector.h:185
Point Max(const Point &p1, const Point &p2)
Definition: Point.h:202
BBox & operator=(const BBox &copy)
Definition: BBox.h:55
SCISHARE void translate(const Vector &v)
Move the bounding box.
Definition: BBox.cc:70
SCISHARE void scale(double s, const Vector &o)
Scale the bounding box by s, centered around o.
Definition: BBox.cc:77
double z_length()
Definition: BBox.h:234
void reset()
Definition: BBox.h:95
double x_length()
Definition: BBox.h:232
double shortest_edge() const
Definition: BBox.h:176
void set_valid(bool v)
Definition: BBox.h:94
double longest_edge() const
Definition: BBox.h:169
BBox(const BBox &b1, const BBox &b2)
Definition: BBox.h:63
void z(double)
Definition: Vector.h:195
BBox()
Definition: BBox.h:50
SCISHARE void Pio(Piostream &, BBox &)
Definition: BBox.cc:134
SCISHARE bool overlaps_inside(const BBox &bb) const
bbox&#39;s that share a face do not overlap_inside
Definition: BBox.cc:101