VTK  9.3.1
vtkBoundingBox.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
19 #ifndef vtkBoundingBox_h
20 #define vtkBoundingBox_h
21 #include "vtkCommonDataModelModule.h" // For export macro
22 #include "vtkSystemIncludes.h"
23 #include <atomic> // For threaded bounding box computation
24 
25 VTK_ABI_NAMESPACE_BEGIN
26 class vtkPoints;
27 
28 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
29 {
30 public:
32 
37  vtkBoundingBox(const double bounds[6]);
38  vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
40 
44  vtkBoundingBox(const vtkBoundingBox& bbox);
45 
49  vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
50 
52 
55  bool operator==(const vtkBoundingBox& bbox) const;
56  bool operator!=(const vtkBoundingBox& bbox) const;
58 
60 
64  void SetBounds(const double bounds[6]);
65  void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
67 
69 
76  static void ComputeBounds(vtkPoints* pts, double bounds[6]);
77  static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
78  static void ComputeBounds(
79  vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
80  static void ComputeBounds(
81  vtkPoints* pts, const long long* ptIds, long long numPointIds, double bounds[6]);
82  static void ComputeBounds(vtkPoints* pts, const long* ptIds, long numPointIds, double bounds[6]);
83  static void ComputeBounds(vtkPoints* pts, const int* ptIds, int numPointIds, double bounds[6]);
85  {
86  double bds[6];
88  this->MinPnt[0] = bds[0];
89  this->MinPnt[1] = bds[2];
90  this->MinPnt[2] = bds[4];
91  this->MaxPnt[0] = bds[1];
92  this->MaxPnt[1] = bds[3];
93  this->MaxPnt[2] = bds[5];
94  }
95  void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
96  {
97  double bds[6];
98  vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
99  this->MinPnt[0] = bds[0];
100  this->MinPnt[1] = bds[2];
101  this->MinPnt[2] = bds[4];
102  this->MaxPnt[0] = bds[1];
103  this->MaxPnt[1] = bds[3];
104  this->MaxPnt[2] = bds[5];
105  }
107 
109 
113  static void ComputeLocalBounds(
114  vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
116 
118 
122  void SetMinPoint(double x, double y, double z);
123  void SetMinPoint(double p[3]);
125 
127 
131  void SetMaxPoint(double x, double y, double z);
132  void SetMaxPoint(double p[3]);
134 
136 
140  int IsValid() const;
141  static int IsValid(const double bounds[6]);
143 
145 
149  void AddPoint(double p[3]);
150  void AddPoint(double px, double py, double pz);
152 
157  void AddBox(const vtkBoundingBox& bbox);
158 
163  void AddBounds(const double bounds[6]);
164 
168  bool IsSubsetOf(const vtkBoundingBox& bbox) const;
169 
175  int IntersectBox(const vtkBoundingBox& bbox);
176 
180  int Intersects(const vtkBoundingBox& bbox) const;
181 
187  bool IntersectPlane(double origin[3], double normal[3]);
188 
193  bool IntersectsSphere(double center[3], double squaredRadius) const;
194 
199  bool IntersectsLine(const double p1[3], const double p2[3]) const;
200 
204  int ComputeInnerDimension() const;
205 
210  int Contains(const vtkBoundingBox& bbox) const;
211 
228  static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
229  double xInt[3], int& plane);
230 
232 
235  void GetBounds(double bounds[6]) const;
236  void GetBounds(
237  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
239 
243  double GetBound(int i) const;
244 
246 
249  const double* GetMinPoint() const VTK_SIZEHINT(3);
250  void GetMinPoint(double& x, double& y, double& z) const;
251  void GetMinPoint(double x[3]) const;
253 
255 
258  const double* GetMaxPoint() const VTK_SIZEHINT(3);
259  void GetMaxPoint(double& x, double& y, double& z) const;
260  void GetMaxPoint(double x[3]) const;
262 
267  void GetCorner(int corner, double p[3]) const;
268 
270 
273  vtkTypeBool ContainsPoint(const double p[3]) const;
274  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
275  template <class PointT>
276  bool ContainsPoint(const PointT& p) const;
278 
282  void GetCenter(double center[3]) const;
283 
287  void GetLengths(double lengths[3]) const;
288 
292  double GetLength(int i) const;
293 
297  double GetMaxLength() const;
298 
300 
304  double GetDiagonalLength2() const;
305  double GetDiagonalLength() const;
307 
309 
320  void Inflate(double delta);
321  void Inflate(double deltaX, double deltaY, double deltaZ);
322  void Inflate();
323  void InflateSlice(double delta);
325 
327 
333  void Scale(double s[3]);
334  void Scale(double sx, double sy, double sz);
336 
338 
343  void ScaleAboutCenter(double s);
344  void ScaleAboutCenter(double s[3]);
345  void ScaleAboutCenter(double sx, double sy, double sz);
347 
358  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
359 
364  static void ClampDivisions(vtkIdType targetBins, int divs[3]);
365 
369  void Reset();
370 
371 protected:
372  double MinPnt[3], MaxPnt[3];
373 };
374 
375 inline void vtkBoundingBox::Reset()
376 {
377  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
378  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
379 }
380 
382  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
383 {
384  xMin = this->MinPnt[0];
385  xMax = this->MaxPnt[0];
386  yMin = this->MinPnt[1];
387  yMax = this->MaxPnt[1];
388  zMin = this->MinPnt[2];
389  zMax = this->MaxPnt[2];
390 }
391 
392 inline double vtkBoundingBox::GetBound(int i) const
393 {
394  // If i is odd then when are returning a part of the max bounds
395  // else part of the min bounds is requested. The exact component
396  // needed is i /2 (or i right shifted by 1
397  return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
398 }
399 
400 inline const double* vtkBoundingBox::GetMinPoint() const
401 {
402  return this->MinPnt;
403 }
404 
405 inline void vtkBoundingBox::GetMinPoint(double x[3]) const
406 {
407  x[0] = this->MinPnt[0];
408  x[1] = this->MinPnt[1];
409  x[2] = this->MinPnt[2];
410 }
411 
412 inline const double* vtkBoundingBox::GetMaxPoint() const
413 {
414  return this->MaxPnt;
415 }
416 
417 inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
418 {
419  x[0] = this->MaxPnt[0];
420  x[1] = this->MaxPnt[1];
421  x[2] = this->MaxPnt[2];
422 }
423 
424 inline int vtkBoundingBox::IsValid() const
425 {
426  return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
427  (this->MinPnt[2] <= this->MaxPnt[2]));
428 }
429 
430 inline int vtkBoundingBox::IsValid(const double bounds[6])
431 {
432  return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
433 }
434 
435 inline double vtkBoundingBox::GetLength(int i) const
436 {
437  return this->MaxPnt[i] - this->MinPnt[i];
438 }
439 
440 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
441 {
442  lengths[0] = this->GetLength(0);
443  lengths[1] = this->GetLength(1);
444  lengths[2] = this->GetLength(2);
445 }
446 
447 inline void vtkBoundingBox::GetCenter(double center[3]) const
448 {
449  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
450  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
451  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
452 }
453 
454 inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
455 {
456  const double* bboxMaxPnt = bbox.GetMaxPoint();
457  const double* bboxMinPnt = bbox.GetMinPoint();
458  return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
459  this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
460  this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
461 }
462 
463 inline void vtkBoundingBox::SetBounds(const double bounds[6])
464 {
465  this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
466 }
467 
468 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
469 {
470  this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
471 }
472 
474 {
475  this->Reset();
476 }
477 
478 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
479 {
480  this->Reset();
481  this->SetBounds(bounds);
482 }
483 
485  double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
486 {
487  this->Reset();
488  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
489 }
490 
492 {
493  this->MinPnt[0] = bbox.MinPnt[0];
494  this->MinPnt[1] = bbox.MinPnt[1];
495  this->MinPnt[2] = bbox.MinPnt[2];
496 
497  this->MaxPnt[0] = bbox.MaxPnt[0];
498  this->MaxPnt[1] = bbox.MaxPnt[1];
499  this->MaxPnt[2] = bbox.MaxPnt[2];
500 }
501 
503 {
504  this->MinPnt[0] = bbox.MinPnt[0];
505  this->MinPnt[1] = bbox.MinPnt[1];
506  this->MinPnt[2] = bbox.MinPnt[2];
507 
508  this->MaxPnt[0] = bbox.MaxPnt[0];
509  this->MaxPnt[1] = bbox.MaxPnt[1];
510  this->MaxPnt[2] = bbox.MaxPnt[2];
511  return *this;
512 }
513 
514 inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
515 {
516  return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
517  (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
518  (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
519 }
520 
521 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
522 {
523  return !((*this) == bbox);
524 }
525 
526 inline void vtkBoundingBox::SetMinPoint(double p[3])
527 {
528  this->SetMinPoint(p[0], p[1], p[2]);
529 }
530 
531 inline void vtkBoundingBox::SetMaxPoint(double p[3])
532 {
533  this->SetMaxPoint(p[0], p[1], p[2]);
534 }
535 
536 inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
537 {
538  x = this->MinPnt[0];
539  y = this->MinPnt[1];
540  z = this->MinPnt[2];
541 }
542 
543 inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
544 {
545  x = this->MaxPnt[0];
546  y = this->MaxPnt[1];
547  z = this->MaxPnt[2];
548 }
549 
550 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
551 {
552  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
553  {
554  return 0;
555  }
556  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
557  {
558  return 0;
559  }
560  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
561  {
562  return 0;
563  }
564  return 1;
565 }
566 
567 inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
568 {
569  return this->ContainsPoint(p[0], p[1], p[2]);
570 }
571 
572 template <class PointT>
573 inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
574 {
575  return this->ContainsPoint(p[0], p[1], p[2]);
576 }
577 
578 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
579 {
580  if ((corner < 0) || (corner > 7))
581  {
582  p[0] = VTK_DOUBLE_MAX;
583  p[1] = VTK_DOUBLE_MAX;
584  p[2] = VTK_DOUBLE_MAX;
585  return; // out of bounds
586  }
587 
588  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
589  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
590  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
591 
592  const double* pts[2] = { this->MinPnt, this->MaxPnt };
593  p[0] = pts[ix][0];
594  p[1] = pts[iy][1];
595  p[2] = pts[iz][2];
596 }
597 
598 VTK_ABI_NAMESPACE_END
599 #endif
600 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
void GetBounds(T a, double bds[6])
const double * GetMinPoint() const
Get the minimum point of the bounding box.
#define VTK_DOUBLE_MAX
Definition: vtkType.h:154
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 ...
void ComputeBounds(vtkPoints *pts)
Compute the bounding box from an array of vtkPoints.
bool operator!=(const vtkBoundingBox &bbox) const
Equality operator.
static void ComputeBounds(vtkPoints *pts, double bounds[6])
Compute the bounding box from an array of vtkPoints.
int vtkIdType
Definition: vtkType.h:315
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
void GetCorner(int corner, double p[3]) const
Get the ith corner of the bounding box.
int vtkTypeBool
Definition: vtkABI.h:64
bool operator==(const vtkBoundingBox &bbox) const
Equality operator.
void GetCenter(double center[3]) const
Get the center of the bounding box.
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...
double GetBound(int i) const
Return the ith bounds of the box (defined by VTK style).
void GetLengths(double lengths[3]) const
Get the length of each side of the box.
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
bool VTKCOMMONCORE_EXPORT operator!=(const std::string &a, const vtkStringToken &b)
vtkTypeBool ContainsPoint(const double p[3]) const
Returns 1 if the point is contained in the box else 0.
#define VTK_SIZEHINT(...)
bool IsSubsetOf(const vtkBoundingBox &bbox) const
Returns true if this instance is entirely contained by bbox.
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:153
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)...
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]
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
represent and manipulate 3D points
Definition: vtkPoints.h:28
Fast, simple class for representing and operating on 3D bounds.
double MinPnt[3]