VTK  9.1.0
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 =========================================================================*/
57 #ifndef vtkBoundingBox_h
58 #define vtkBoundingBox_h
59 #include "vtkCommonDataModelModule.h" // For export macro
60 #include "vtkSystemIncludes.h"
61 #include <atomic> // For threaded bounding box computation
62 
63 class vtkPoints;
64 
65 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
66 {
67 public:
69 
74  vtkBoundingBox(const double bounds[6]);
75  vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
77 
81  vtkBoundingBox(const vtkBoundingBox& bbox);
82 
86  vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
87 
89 
92  bool operator==(const vtkBoundingBox& bbox) const;
93  bool operator!=(const vtkBoundingBox& bbox) const;
95 
97 
101  void SetBounds(const double bounds[6]);
102  void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
104 
106 
113  static void ComputeBounds(vtkPoints* pts, double bounds[6]);
114  static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
115  static void ComputeBounds(
116  vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
117  void ComputeBounds(vtkPoints* pts) { this->ComputeBounds(pts, (unsigned char*)nullptr); }
118  void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
119  {
120  double bds[6];
121  vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
122  this->MinPnt[0] = bds[0];
123  this->MinPnt[1] = bds[2];
124  this->MinPnt[2] = bds[4];
125  this->MaxPnt[0] = bds[1];
126  this->MaxPnt[1] = bds[3];
127  this->MaxPnt[2] = bds[5];
128  }
130 
132 
136  static void ComputeLocalBounds(
137  vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
139 
141 
145  void SetMinPoint(double x, double y, double z);
146  void SetMinPoint(double p[3]);
148 
150 
154  void SetMaxPoint(double x, double y, double z);
155  void SetMaxPoint(double p[3]);
157 
159 
163  int IsValid() const;
164  static int IsValid(const double bounds[6]);
166 
168 
172  void AddPoint(double p[3]);
173  void AddPoint(double px, double py, double pz);
175 
180  void AddBox(const vtkBoundingBox& bbox);
181 
186  void AddBounds(const double bounds[]);
187 
191  bool IsSubsetOf(const vtkBoundingBox& bbox) const;
192 
198  int IntersectBox(const vtkBoundingBox& bbox);
199 
203  int Intersects(const vtkBoundingBox& bbox) const;
204 
210  bool IntersectPlane(double origin[3], double normal[3]);
211 
216  bool IntersectsSphere(double center[3], double squaredRadius) const;
217 
222  bool IntersectsLine(const double p1[3], const double p2[3]) const;
223 
228 
233  int Contains(const vtkBoundingBox& bbox) const;
234 
236 
239  void GetBounds(double bounds[6]) const;
240  void GetBounds(
241  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
243 
247  double GetBound(int i) const;
248 
250 
253  const double* GetMinPoint() const VTK_SIZEHINT(3);
254  void GetMinPoint(double& x, double& y, double& z) const;
255  void GetMinPoint(double x[3]) const;
257 
259 
262  const double* GetMaxPoint() const VTK_SIZEHINT(3);
263  void GetMaxPoint(double& x, double& y, double& z) const;
264  void GetMaxPoint(double x[3]) const;
266 
271  void GetCorner(int corner, double p[3]) const;
272 
274 
277  vtkTypeBool ContainsPoint(const double p[3]) const;
278  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
279  template <class PointT>
280  bool ContainsPoint(const PointT& p) const;
282 
286  void GetCenter(double center[3]) const;
287 
291  void GetLengths(double lengths[3]) const;
292 
296  double GetLength(int i) const;
297 
301  double GetMaxLength() const;
302 
307  double GetDiagonalLength() const;
308 
310 
318  void Inflate(double delta);
319  void Inflate(double deltaX, double deltaY, double deltaZ);
320  void Inflate();
322 
324 
330  void Scale(double s[3]);
331  void Scale(double sx, double sy, double sz);
333 
335 
340  void ScaleAboutCenter(double s);
341  void ScaleAboutCenter(double s[3]);
342  void ScaleAboutCenter(double sx, double sy, double sz);
344 
355  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
356 
361  static void ClampDivisions(vtkIdType targetBins, int divs[3]);
362 
366  void Reset();
367 
368 protected:
369  double MinPnt[3], MaxPnt[3];
370 };
371 
372 inline void vtkBoundingBox::Reset()
373 {
374  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
375  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
376 }
377 
379  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
380 {
381  xMin = this->MinPnt[0];
382  xMax = this->MaxPnt[0];
383  yMin = this->MinPnt[1];
384  yMax = this->MaxPnt[1];
385  zMin = this->MinPnt[2];
386  zMax = this->MaxPnt[2];
387 }
388 
389 inline double vtkBoundingBox::GetBound(int i) const
390 {
391  // If i is odd then when are returning a part of the max bounds
392  // else part of the min bounds is requested. The exact component
393  // needed is i /2 (or i right shifted by 1
394  return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
395 }
396 
397 inline const double* vtkBoundingBox::GetMinPoint() const
398 {
399  return this->MinPnt;
400 }
401 
402 inline void vtkBoundingBox::GetMinPoint(double x[3]) const
403 {
404  x[0] = this->MinPnt[0];
405  x[1] = this->MinPnt[1];
406  x[2] = this->MinPnt[2];
407 }
408 
409 inline const double* vtkBoundingBox::GetMaxPoint() const
410 {
411  return this->MaxPnt;
412 }
413 
414 inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
415 {
416  x[0] = this->MaxPnt[0];
417  x[1] = this->MaxPnt[1];
418  x[2] = this->MaxPnt[2];
419 }
420 
421 inline int vtkBoundingBox::IsValid() const
422 {
423  return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
424  (this->MinPnt[2] <= this->MaxPnt[2]));
425 }
426 
427 inline int vtkBoundingBox::IsValid(const double bounds[6])
428 {
429  return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
430 }
431 
432 inline double vtkBoundingBox::GetLength(int i) const
433 {
434  return this->MaxPnt[i] - this->MinPnt[i];
435 }
436 
437 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
438 {
439  lengths[0] = this->GetLength(0);
440  lengths[1] = this->GetLength(1);
441  lengths[2] = this->GetLength(2);
442 }
443 
444 inline void vtkBoundingBox::GetCenter(double center[3]) const
445 {
446  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
447  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
448  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
449 }
450 
451 inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
452 {
453  const double* bboxMaxPnt = bbox.GetMaxPoint();
454  const double* bboxMinPnt = bbox.GetMinPoint();
455  return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
456  this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
457  this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
458 }
459 
460 inline void vtkBoundingBox::SetBounds(const double bounds[6])
461 {
462  this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
463 }
464 
465 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
466 {
467  this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
468 }
469 
471 {
472  this->Reset();
473 }
474 
475 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
476 {
477  this->Reset();
478  this->SetBounds(bounds);
479 }
480 
482  double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
483 {
484  this->Reset();
485  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
486 }
487 
489 {
490  this->MinPnt[0] = bbox.MinPnt[0];
491  this->MinPnt[1] = bbox.MinPnt[1];
492  this->MinPnt[2] = bbox.MinPnt[2];
493 
494  this->MaxPnt[0] = bbox.MaxPnt[0];
495  this->MaxPnt[1] = bbox.MaxPnt[1];
496  this->MaxPnt[2] = bbox.MaxPnt[2];
497 }
498 
500 {
501  this->MinPnt[0] = bbox.MinPnt[0];
502  this->MinPnt[1] = bbox.MinPnt[1];
503  this->MinPnt[2] = bbox.MinPnt[2];
504 
505  this->MaxPnt[0] = bbox.MaxPnt[0];
506  this->MaxPnt[1] = bbox.MaxPnt[1];
507  this->MaxPnt[2] = bbox.MaxPnt[2];
508  return *this;
509 }
510 
511 inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
512 {
513  return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
514  (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
515  (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
516 }
517 
518 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
519 {
520  return !((*this) == bbox);
521 }
522 
523 inline void vtkBoundingBox::SetMinPoint(double p[3])
524 {
525  this->SetMinPoint(p[0], p[1], p[2]);
526 }
527 
528 inline void vtkBoundingBox::SetMaxPoint(double p[3])
529 {
530  this->SetMaxPoint(p[0], p[1], p[2]);
531 }
532 
533 inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
534 {
535  x = this->MinPnt[0];
536  y = this->MinPnt[1];
537  z = this->MinPnt[2];
538 }
539 
540 inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
541 {
542  x = this->MaxPnt[0];
543  y = this->MaxPnt[1];
544  z = this->MaxPnt[2];
545 }
546 
547 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
548 {
549  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
550  {
551  return 0;
552  }
553  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
554  {
555  return 0;
556  }
557  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
558  {
559  return 0;
560  }
561  return 1;
562 }
563 
564 inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
565 {
566  return this->ContainsPoint(p[0], p[1], p[2]);
567 }
568 
569 template <class PointT>
570 inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
571 {
572  return this->ContainsPoint(p[0], p[1], p[2]);
573 }
574 
575 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
576 {
577  if ((corner < 0) || (corner > 7))
578  {
579  p[0] = VTK_DOUBLE_MAX;
580  p[1] = VTK_DOUBLE_MAX;
581  p[2] = VTK_DOUBLE_MAX;
582  return; // out of bounds
583  }
584 
585  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
586  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
587  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
588 
589  const double* pts[2] = { this->MinPnt, this->MaxPnt };
590  p[0] = pts[ix][0];
591  p[1] = pts[iy][1];
592  p[2] = pts[iz][2];
593 }
594 
595 #endif
596 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
vtkPoints
represent and manipulate 3D points
Definition: vtkPoints.h:143
vtkBoundingBox::operator=
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
Definition: vtkBoundingBox.h:499
vtkBoundingBox::GetMaxPoint
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
Definition: vtkBoundingBox.h:409
vtkBoundingBox::ComputeBounds
static void ComputeBounds(vtkPoints *pts, double bounds[6])
Compute the bounding box from an array of vtkPoints.
vtkBoundingBox::ComputeInnerDimension
int ComputeInnerDimension() const
Returns the inner dimension of the bounding box.
vtkBoundingBox::ComputeLocalBounds
static void ComputeLocalBounds(vtkPoints *points, double u[3], double v[3], double w[3], double outputBounds[6])
Compute local bounds.
vtkBoundingBox::MaxPnt
double MaxPnt[3]
Definition: vtkBoundingBox.h:369
vtkBoundingBox::IntersectBox
int IntersectBox(const vtkBoundingBox &bbox)
Intersect this box with bbox.
vtkBoundingBox::MinPnt
double MinPnt[3]
Definition: vtkBoundingBox.h:369
vtkIdType
int vtkIdType
Definition: vtkType.h:332
vtkBoundingBox::IntersectsSphere
bool IntersectsSphere(double center[3], double squaredRadius) const
Intersect this box with a sphere.
vtkBoundingBox::IsValid
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...
Definition: vtkBoundingBox.h:421
vtkBoundingBox::ContainsPoint
vtkTypeBool ContainsPoint(const double p[3]) const
Returns 1 if the point is contained in the box else 0.
Definition: vtkBoundingBox.h:564
vtkX3D::center
@ center
Definition: vtkX3D.h:236
vtkBoundingBox::SetMinPoint
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...
vtkBoundingBox::GetLengths
void GetLengths(double lengths[3]) const
Get the length of each side of the box.
Definition: vtkBoundingBox.h:437
vtkBoundingBox
Fast, simple class for representing and operating on 3D bounds.
Definition: vtkBoundingBox.h:66
vtkBoundingBox::SetMaxPoint
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 ...
vtkX3D::points
@ points
Definition: vtkX3D.h:452
vtkBoundingBox::SetBounds
void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
Set the bounds explicitly of the box (using the VTK convention for representing a bounding box).
VTK_SIZEHINT
#define VTK_SIZEHINT(...)
Definition: vtkWrappingHints.h:48
vtkBoundingBox::AddBounds
void AddBounds(const double bounds[])
Adjust the bounding box so it contains the specified bounds (defined by the VTK representation (xmin,...
vtkBoundingBox::SetBounds
void SetBounds(const double bounds[6])
Set the bounds explicitly of the box (using the VTK convention for representing a bounding box).
Definition: vtkBoundingBox.h:460
vtkBoundingBox::GetBounds
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
Definition: vtkBoundingBox.h:465
vtkBoundingBox::operator!=
bool operator!=(const vtkBoundingBox &bbox) const
Equality operator.
Definition: vtkBoundingBox.h:518
vtkBoundingBox::ComputeBounds
static void ComputeBounds(vtkPoints *pts, const unsigned char *ptUses, double bounds[6])
Compute the bounding box from an array of vtkPoints.
vtkBoundingBox::GetMinPoint
const double * GetMinPoint() const
Get the minimum point of the bounding box.
Definition: vtkBoundingBox.h:397
vtkBlockSortHelper::GetBounds
void GetBounds(T a, double bds[6])
Definition: vtkBlockSortHelper.h:37
vtkBoundingBox::ComputeBounds
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
Definition: vtkBoundingBox.h:118
vtkBoundingBox::operator==
bool operator==(const vtkBoundingBox &bbox) const
Equality operator.
Definition: vtkBoundingBox.h:511
vtkBoundingBox::GetCenter
void GetCenter(double center[3]) const
Get the center of the bounding box.
Definition: vtkBoundingBox.h:444
vtkBoundingBox::Contains
int Contains(const vtkBoundingBox &bbox) const
Returns 1 if the min and max points of bbox are contained within the bounds of the specified box,...
vtkBoundingBox::IsSubsetOf
bool IsSubsetOf(const vtkBoundingBox &bbox) const
Returns true if this instance is entirely contained by bbox.
Definition: vtkBoundingBox.h:451
VTK_DOUBLE_MIN
#define VTK_DOUBLE_MIN
Definition: vtkType.h:164
vtkBoundingBox::GetLength
double GetLength(int i) const
Return the length of the bounding box in the ith direction.
Definition: vtkBoundingBox.h:432
operator==
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
vtkBoundingBox::AddPoint
void AddPoint(double p[3])
Change bounding box so it includes the point p.
vtkBoundingBox::AddPoint
void AddPoint(double px, double py, double pz)
Change bounding box so it includes the point p.
vtkBoundingBox::vtkBoundingBox
vtkBoundingBox()
Construct a bounding box with the min point set to VTK_DOUBLE_MAX and the max point set to VTK_DOUBLE...
Definition: vtkBoundingBox.h:470
vtkBoundingBox::ComputeBounds
static void ComputeBounds(vtkPoints *pts, const std::atomic< unsigned char > *ptUses, double bounds[6])
Compute the bounding box from an array of vtkPoints.
vtkBoundingBox::GetCorner
void GetCorner(int corner, double p[3]) const
Get the ith corner of the bounding box.
Definition: vtkBoundingBox.h:575
vtkBoundingBox::ComputeBounds
void ComputeBounds(vtkPoints *pts)
Compute the bounding box from an array of vtkPoints.
Definition: vtkBoundingBox.h:117
vtkBoundingBox::Intersects
int Intersects(const vtkBoundingBox &bbox) const
Returns 1 if the boxes intersect else returns 0.
vtkBoundingBox::IntersectPlane
bool IntersectPlane(double origin[3], double normal[3])
Intersect this box with the half space defined by plane.
operator!=
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTK_DOUBLE_MAX
#define VTK_DOUBLE_MAX
Definition: vtkType.h:165
vtkBoundingBox::GetBound
double GetBound(int i) const
Return the ith bounds of the box (defined by VTK style).
Definition: vtkBoundingBox.h:389
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkBoundingBox::AddBox
void AddBox(const vtkBoundingBox &bbox)
Change the bounding box to be the union of itself and the specified bbox.
vtkSystemIncludes.h
vtkBoundingBox::IntersectsLine
bool IntersectsLine(const double p1[3], const double p2[3]) const
Returns true if any part of segment [p1,p2] lies inside the bounding box, as well as on its boundarie...