VTK
vtkBoundingBox.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: Visualization Toolkit
4 Module: vtkBoundingBox.h
5 
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
29 #ifndef __vtkBoundingBox_h
30 #define __vtkBoundingBox_h
31 #include "vtkSystemIncludes.h"
32 
34 {
35 public:
37 
40  vtkBoundingBox(double bounds[6]);
41  vtkBoundingBox(double xMin, double xMax,
42  double yMin, double yMax,
43  double zMin, double zMax);
45 
47  vtkBoundingBox(const vtkBoundingBox &bbox);
48 
50  vtkBoundingBox &operator=(const vtkBoundingBox &bbox);
51 
53 
54  int operator==(const vtkBoundingBox &bbox)const;
55  int operator!=(const vtkBoundingBox &bbox)const;
57 
59 
61  void SetBounds(double bounds[6]);
62  void SetBounds(double xMin, double xMax,
63  double yMin, double yMax,
64  double zMin, double zMax);
66 
68 
70  void SetMinPoint(double x, double y, double z);
71  void SetMinPoint(double p[3]);
73 
75 
77  void SetMaxPoint(double x, double y, double z);
78  void SetMaxPoint(double p[3]);
80 
82 
84  void AddPoint(double p[3]);
85  void AddPoint(double px, double py, double pz);
87 
89  void AddBox(const vtkBoundingBox &bbox);
90 
93  void AddBounds(double bounds[6]);
94 
95  // Desciption:
96  // Intersect this box with bbox. The method returns 1 if
97  // both boxes are valid and they do have overlap else it will return 0.
98  // If 0 is returned the box has not been modified
99  int IntersectBox(const vtkBoundingBox &bbox);
100 
102  int Intersects(const vtkBoundingBox &bbox) const;
103 
104 
105  // Desciption:
106  // Intersect this box with the half space defined by plane.
107  //Returns true if there is intersection---which implies that the box has been modified
108  // Returns false otherwise
109  bool IntersectPlane(double origin[3],double normal[3]);
110 
111 
114  int Contains(const vtkBoundingBox &bbox) const;
115 
117 
118  void GetBounds(double bounds[6]) const;
119  void GetBounds(double &xMin, double &xMax,
120  double &yMin, double &yMax,
121  double &zMin, double &zMax) const;
123 
125  double GetBound(int i) const;
126 
128 
129  const double *GetMinPoint() const;
130  void GetMinPoint(double &x, double &y, double &z) const;
132 
134 
135  const double *GetMaxPoint() const;
136  void GetMaxPoint(double &x, double &y, double &z) const;
138 
140 
141  int ContainsPoint(double p[3]) const;
142  int ContainsPoint(double px, double py, double pz) const;
144 
146  void GetCenter(double center[3]) const;
147 
149  void GetLengths(double lengths[3]) const;
150 
152  double GetLength(int i) const;
153 
155  double GetMaxLength() const;
156 
158  double GetDiagonalLength() const;
159 
162  void Inflate(double delta);
163 
165 
167  int IsValid() const;
168  static int IsValid(double bounds[6]);
170 
172  void Reset();
173 
175 
179  void Scale(double s[3]);
180  void Scale(double sx,
181  double sy,
182  double sz);
184 
185 protected:
186  double MinPnt[3], MaxPnt[3];
187 };
188 
190 {
191  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
192  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
193 }
194 
195 inline void vtkBoundingBox::GetBounds(double &xMin, double &xMax,
196  double &yMin, double &yMax,
197  double &zMin, double &zMax) const
198 {
199  xMin = this->MinPnt[0];
200  xMax = this->MaxPnt[0];
201  yMin = this->MinPnt[1];
202  yMax = this->MaxPnt[1];
203  zMin = this->MinPnt[2];
204  zMax = this->MaxPnt[2];
205 }
206 
207 inline double vtkBoundingBox::GetBound(int i) const
208 {
209  // If i is odd then when are returning a part of the max bounds
210  // else part of the min bounds is requested. The exact component
211  // needed is i /2 (or i right shifted by 1
212  return ((i & 0x1) ? this->MaxPnt[i>>1] : this->MinPnt[i>>1]);
213 }
214 
215 inline const double *vtkBoundingBox::GetMinPoint() const
216 {
217  return this->MinPnt;
218 }
219 
220 inline const double *vtkBoundingBox::GetMaxPoint() const
221 {
222  return this->MaxPnt;
223 }
224 
225 inline int vtkBoundingBox::IsValid() const
226 {
227  return ((this->MinPnt[0] <= this->MaxPnt[0]) &&
228  (this->MinPnt[1] <= this->MaxPnt[1]) &&
229  (this->MinPnt[2] <= this->MaxPnt[2]));
230 }
231 
232 inline int vtkBoundingBox::IsValid(double bounds[6])
233 {
234  return (bounds[0] <= bounds[1] &&
235  bounds[2] <= bounds[3] &&
236  bounds[4] <= bounds[5]);
237 }
238 
239 inline double vtkBoundingBox::GetLength(int i) const
240 {
241  return this->MaxPnt[i] - this->MinPnt[i];
242 }
243 
244 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
245 {
246  lengths[0] = this->GetLength(0);
247  lengths[1] = this->GetLength(1);
248  lengths[2] = this->GetLength(2);
249 }
250 
251 inline void vtkBoundingBox::GetCenter(double center[3]) const
252 {
253  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
254  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
255  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
256 }
257 
258 inline void vtkBoundingBox::SetBounds(double bounds[6])
259 {
260  this->SetBounds(bounds[0], bounds[1], bounds[2],
261  bounds[3], bounds[4], bounds[5]);
262 }
263 
264 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
265 {
266  this->GetBounds(bounds[0], bounds[1], bounds[2],
267  bounds[3], bounds[4], bounds[5]);
268 }
269 
271 {
272  this->Reset();
273 }
274 
275 inline vtkBoundingBox::vtkBoundingBox(double bounds[6])
276 {
277  this->Reset();
278  this->SetBounds(bounds);
279 }
280 
281 inline vtkBoundingBox::vtkBoundingBox(double xMin, double xMax,
282  double yMin, double yMax,
283  double zMin, double zMax)
284 {
285  this->Reset();
286  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
287 }
288 
290 {
291  this->MinPnt[0] = bbox.MinPnt[0];
292  this->MinPnt[1] = bbox.MinPnt[1];
293  this->MinPnt[2] = bbox.MinPnt[2];
294 
295  this->MaxPnt[0] = bbox.MaxPnt[0];
296  this->MaxPnt[1] = bbox.MaxPnt[1];
297  this->MaxPnt[2] = bbox.MaxPnt[2];
298 }
299 
301 {
302  this->MinPnt[0] = bbox.MinPnt[0];
303  this->MinPnt[1] = bbox.MinPnt[1];
304  this->MinPnt[2] = bbox.MinPnt[2];
305 
306  this->MaxPnt[0] = bbox.MaxPnt[0];
307  this->MaxPnt[1] = bbox.MaxPnt[1];
308  this->MaxPnt[2] = bbox.MaxPnt[2];
309  return *this;
310 }
311 
312 inline int vtkBoundingBox::operator==(const vtkBoundingBox &bbox)const
313 {
314  return ((this->MinPnt[0] == bbox.MinPnt[0]) &&
315  (this->MinPnt[1] == bbox.MinPnt[1]) &&
316  (this->MinPnt[2] == bbox.MinPnt[2]) &&
317  (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
318  (this->MaxPnt[1] == bbox.MaxPnt[1]) &&
319  (this->MaxPnt[2] == bbox.MaxPnt[2]));
320 }
321 
322 inline int vtkBoundingBox::operator!=(const vtkBoundingBox &bbox)const
323 {
324  return !((*this) == bbox);
325 }
326 
327 inline void vtkBoundingBox::SetMinPoint(double p[3])
328 {
329  this->SetMinPoint(p[0], p[1], p[2]);
330 }
331 
332 inline void vtkBoundingBox::SetMaxPoint(double p[3])
333 {
334  this->SetMaxPoint(p[0], p[1], p[2]);
335 }
336 
337 inline void vtkBoundingBox::GetMinPoint(double &x, double &y, double &z) const
338 {
339  x = this->MinPnt[0];
340  y = this->MinPnt[1];
341  z = this->MinPnt[2];
342 }
343 
344 inline void vtkBoundingBox::GetMaxPoint(double &x, double &y, double &z) const
345 {
346  x = this->MaxPnt[0];
347  y = this->MaxPnt[1];
348  z = this->MaxPnt[2];
349 }
350 
351 inline int vtkBoundingBox::ContainsPoint(double px, double py,
352  double pz) const
353 {
354  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
355  {
356  return 0;
357  }
358  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
359  {
360  return 0;
361  }
362  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
363  {
364  return 0;
365  }
366  return 1;
367 }
368 
369 inline int vtkBoundingBox::ContainsPoint(double p[3]) const
370 {
371  return this->ContainsPoint(p[0], p[1], p[2]);
372 }
373 
374 #endif