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 =========================================================================*/
27 #ifndef vtkBoundingBox_h
28 #define vtkBoundingBox_h
29 #include "vtkCommonDataModelModule.h" // For export macro
30 #include "vtkSystemIncludes.h"
31 
32 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
33 {
34 public:
36 
41  vtkBoundingBox(const double bounds[6]);
42  vtkBoundingBox(double xMin, double xMax,
43  double yMin, double yMax,
44  double zMin, double zMax);
46 
50  vtkBoundingBox(const vtkBoundingBox &bbox);
51 
55  vtkBoundingBox &operator=(const vtkBoundingBox &bbox);
56 
58 
61  bool operator==(const vtkBoundingBox &bbox)const;
62  bool operator!=(const vtkBoundingBox &bbox)const;
64 
66 
70  void SetBounds(const double bounds[6]);
71  void SetBounds(double xMin, double xMax,
72  double yMin, double yMax,
73  double zMin, double zMax);
75 
77 
81  void SetMinPoint(double x, double y, double z);
82  void SetMinPoint(double p[3]);
84 
86 
90  void SetMaxPoint(double x, double y, double z);
91  void SetMaxPoint(double p[3]);
93 
95 
99  int IsValid() const;
100  static int IsValid(const double bounds[6]);
102 
104 
108  void AddPoint(double p[3]);
109  void AddPoint(double px, double py, double pz);
111 
116  void AddBox(const vtkBoundingBox &bbox);
117 
122  void AddBounds(const double bounds[]);
123 
129  int IntersectBox(const vtkBoundingBox &bbox);
130 
134  int Intersects(const vtkBoundingBox &bbox) const;
135 
141  bool IntersectPlane(double origin[3],double normal[3]);
142 
147  int Contains(const vtkBoundingBox &bbox) const;
148 
150 
153  void GetBounds(double bounds[6]) const;
154  void GetBounds(double &xMin, double &xMax,
155  double &yMin, double &yMax,
156  double &zMin, double &zMax) const;
158 
162  double GetBound(int i) const;
163 
165 
168  const double *GetMinPoint() const VTK_SIZEHINT(3);
169  void GetMinPoint(double &x, double &y, double &z) const;
170  void GetMinPoint(double x[3]);
172 
174 
177  const double *GetMaxPoint() const VTK_SIZEHINT(3);
178  void GetMaxPoint(double &x, double &y, double &z) const;
179  void GetMaxPoint(double x[3]);
181 
186  void GetCorner(int corner, double p[3]) const;
187 
189 
192  int ContainsPoint(double p[3]) const;
193  int ContainsPoint(double px, double py, double pz) const;
195 
199  void GetCenter(double center[3]) const;
200 
204  void GetLengths(double lengths[3]) const;
205 
209  double GetLength(int i) const;
210 
214  double GetMaxLength() const;
215 
220  double GetDiagonalLength() const;
221 
223 
231  void Inflate(double delta);
232  void Inflate(double deltaX, double deltaY, double deltaZ);
233  void Inflate();
235 
237 
243  void Scale(double s[3]);
244  void Scale(double sx, double sy, double sz);
246 
248 
253  void ScaleAboutCenter(double s);
254  void ScaleAboutCenter(double s[3]);
255  void ScaleAboutCenter(double sx, double sy, double sz);
257 
268  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
269 
273  void Reset();
274 
275 protected:
276  double MinPnt[3], MaxPnt[3];
277 };
278 
280 {
281  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
282  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
283 }
284 
285 inline void vtkBoundingBox::GetBounds(double &xMin, double &xMax,
286  double &yMin, double &yMax,
287  double &zMin, double &zMax) const
288 {
289  xMin = this->MinPnt[0];
290  xMax = this->MaxPnt[0];
291  yMin = this->MinPnt[1];
292  yMax = this->MaxPnt[1];
293  zMin = this->MinPnt[2];
294  zMax = this->MaxPnt[2];
295 }
296 
297 inline double vtkBoundingBox::GetBound(int i) const
298 {
299  // If i is odd then when are returning a part of the max bounds
300  // else part of the min bounds is requested. The exact component
301  // needed is i /2 (or i right shifted by 1
302  return ((i & 0x1) ? this->MaxPnt[i>>1] : this->MinPnt[i>>1]);
303 }
304 
305 inline const double *vtkBoundingBox::GetMinPoint() const
306 {
307  return this->MinPnt;
308 }
309 
310 inline void vtkBoundingBox::GetMinPoint(double x[3])
311 {
312  x[0] = this->MinPnt[0];
313  x[1] = this->MinPnt[1];
314  x[2] = this->MinPnt[2];
315 }
316 
317 inline const double *vtkBoundingBox::GetMaxPoint() const
318 {
319  return this->MaxPnt;
320 }
321 
322 inline void vtkBoundingBox::GetMaxPoint(double x[3])
323 {
324  x[0] = this->MaxPnt[0];
325  x[1] = this->MaxPnt[1];
326  x[2] = this->MaxPnt[2];
327 }
328 
329 inline int vtkBoundingBox::IsValid() const
330 {
331  return ((this->MinPnt[0] <= this->MaxPnt[0]) &&
332  (this->MinPnt[1] <= this->MaxPnt[1]) &&
333  (this->MinPnt[2] <= this->MaxPnt[2]));
334 }
335 
336 inline int vtkBoundingBox::IsValid(const double bounds[6])
337 {
338  return (bounds[0] <= bounds[1] &&
339  bounds[2] <= bounds[3] &&
340  bounds[4] <= bounds[5]);
341 }
342 
343 inline double vtkBoundingBox::GetLength(int i) const
344 {
345  return this->MaxPnt[i] - this->MinPnt[i];
346 }
347 
348 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
349 {
350  lengths[0] = this->GetLength(0);
351  lengths[1] = this->GetLength(1);
352  lengths[2] = this->GetLength(2);
353 }
354 
355 inline void vtkBoundingBox::GetCenter(double center[3]) const
356 {
357  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
358  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
359  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
360 }
361 
362 inline void vtkBoundingBox::SetBounds(const double bounds[6])
363 {
364  this->SetBounds(bounds[0], bounds[1], bounds[2],
365  bounds[3], bounds[4], bounds[5]);
366 }
367 
368 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
369 {
370  this->GetBounds(bounds[0], bounds[1], bounds[2],
371  bounds[3], bounds[4], bounds[5]);
372 }
373 
375 {
376  this->Reset();
377 }
378 
379 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
380 {
381  this->Reset();
382  this->SetBounds(bounds);
383 }
384 
385 inline vtkBoundingBox::vtkBoundingBox(double xMin, double xMax,
386  double yMin, double yMax,
387  double zMin, double zMax)
388 {
389  this->Reset();
390  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
391 }
392 
394 {
395  this->MinPnt[0] = bbox.MinPnt[0];
396  this->MinPnt[1] = bbox.MinPnt[1];
397  this->MinPnt[2] = bbox.MinPnt[2];
398 
399  this->MaxPnt[0] = bbox.MaxPnt[0];
400  this->MaxPnt[1] = bbox.MaxPnt[1];
401  this->MaxPnt[2] = bbox.MaxPnt[2];
402 }
403 
405 {
406  this->MinPnt[0] = bbox.MinPnt[0];
407  this->MinPnt[1] = bbox.MinPnt[1];
408  this->MinPnt[2] = bbox.MinPnt[2];
409 
410  this->MaxPnt[0] = bbox.MaxPnt[0];
411  this->MaxPnt[1] = bbox.MaxPnt[1];
412  this->MaxPnt[2] = bbox.MaxPnt[2];
413  return *this;
414 }
415 
416 inline bool vtkBoundingBox::operator==(const vtkBoundingBox &bbox)const
417 {
418  return ((this->MinPnt[0] == bbox.MinPnt[0]) &&
419  (this->MinPnt[1] == bbox.MinPnt[1]) &&
420  (this->MinPnt[2] == bbox.MinPnt[2]) &&
421  (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
422  (this->MaxPnt[1] == bbox.MaxPnt[1]) &&
423  (this->MaxPnt[2] == bbox.MaxPnt[2]));
424 }
425 
426 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox &bbox)const
427 {
428  return !((*this) == bbox);
429 }
430 
431 inline void vtkBoundingBox::SetMinPoint(double p[3])
432 {
433  this->SetMinPoint(p[0], p[1], p[2]);
434 }
435 
436 inline void vtkBoundingBox::SetMaxPoint(double p[3])
437 {
438  this->SetMaxPoint(p[0], p[1], p[2]);
439 }
440 
441 inline void vtkBoundingBox::GetMinPoint(double &x, double &y, double &z) const
442 {
443  x = this->MinPnt[0];
444  y = this->MinPnt[1];
445  z = this->MinPnt[2];
446 }
447 
448 inline void vtkBoundingBox::GetMaxPoint(double &x, double &y, double &z) const
449 {
450  x = this->MaxPnt[0];
451  y = this->MaxPnt[1];
452  z = this->MaxPnt[2];
453 }
454 
455 inline int vtkBoundingBox::ContainsPoint(double px, double py,
456  double pz) const
457 {
458  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
459  {
460  return 0;
461  }
462  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
463  {
464  return 0;
465  }
466  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
467  {
468  return 0;
469  }
470  return 1;
471 }
472 
473 inline int vtkBoundingBox::ContainsPoint(double p[3]) const
474 {
475  return this->ContainsPoint(p[0], p[1], p[2]);
476 }
477 
478 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
479 {
480  if ((corner < 0) || (corner > 7))
481  {
482  p[0] = VTK_DOUBLE_MAX;
483  p[1] = VTK_DOUBLE_MAX;
484  p[2] = VTK_DOUBLE_MAX;
485  return; // out of bounds
486  }
487 
488  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
489  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
490  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
491 
492  const double* pts[2] = { this->MinPnt, this->MaxPnt };
493  p[0] = pts[ix][0];
494  p[1] = pts[iy][1];
495  p[2] = pts[iz][2];
496 }
497 
498 #endif
499 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
double GetBound(int i) const
Return the ith bounds of the box (defined by VTK style).
void GetCenter(double center[3]) const
Get the center of the bounding box.
#define VTK_DOUBLE_MAX
Definition: vtkType.h:169
void SetMaxPoint(double x, double y, double z)
Set the maximum point of the bounding box - if the max point is less than the min point then the min ...
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
void Reset()
Returns the box to its initialized state.
int ContainsPoint(double p[3]) const
Returns 1 if the point is contained in the box else 0.
int vtkIdType
Definition: vtkType.h:347
int IsValid() const
Returns 1 if the bounds have been set and 0 if the box is in its initialized state which is an invert...
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
void GetCorner(int corner, double p[3]) const
Get the ith corner of the bounding box.
#define VTK_SIZEHINT(...)
void SetMinPoint(double x, double y, double z)
Set the minimum point of the bounding box - if the min point is greater than the max point then the m...
#define VTK_DOUBLE_MIN
Definition: vtkType.h:168
const double * GetMinPoint() const
Get the minimum point of the bounding box.
bool operator!=(const vtkBoundingBox &bbox) const
Equality operator.
double GetLength(int i) const
Return the length of the bounding box in the ith direction.
void SetBounds(const double bounds[6])
Set the bounds explicitly of the box (using the VTK convention for representing a bounding box).
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
vtkBoundingBox()
Construct a bounding box with the min point set to VTK_DOUBLE_MAX and the max point set to VTK_DOUBLE...
double MaxPnt[3]
bool operator==(const vtkBoundingBox &bbox) const
Equality operator.
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
void GetLengths(double lengths[3]) const
Get the length of each sode of the box.
Fast Simple Class for dealing with 3D bounds.
double MinPnt[3]