VTK  9.5.20251214
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
47
48#ifndef vtkBoundingBox_h
49#define vtkBoundingBox_h
50#include "vtkCommonDataModelModule.h" // For export macro
51#include "vtkSystemIncludes.h"
52
53#include <algorithm> // For std::min, std::max
54#include <atomic> // For threaded bounding box computation
55
56VTK_ABI_NAMESPACE_BEGIN
57class vtkPoints;
58
59class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
60{
61public:
63
71 vtkBoundingBox(const double bounds[6]);
75 vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
79 vtkBoundingBox(const double min[3], const double max[3]);
83 vtkBoundingBox(double center[3], double delta);
85
89 vtkBoundingBox(const vtkBoundingBox& bbox);
90
95
97
100 bool operator==(const vtkBoundingBox& bbox) const;
101 bool operator!=(const vtkBoundingBox& bbox) const;
103
105
109 void SetBounds(const double bounds[6]);
110 void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
112
114
121 static void ComputeBounds(vtkPoints* pts, double bounds[6]);
122 static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
123 static void ComputeBounds(
124 vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
125 template <typename TIter>
126 static void ComputeBounds(vtkPoints* pts, TIter ptIds, vtkIdType numPointIds, double bounds[6]);
128 {
129 double bds[6];
131 this->MinPnt[0] = bds[0];
132 this->MinPnt[1] = bds[2];
133 this->MinPnt[2] = bds[4];
134 this->MaxPnt[0] = bds[1];
135 this->MaxPnt[1] = bds[3];
136 this->MaxPnt[2] = bds[5];
137 }
138 void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
139 {
140 double bds[6];
141 vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
142 this->MinPnt[0] = bds[0];
143 this->MinPnt[1] = bds[2];
144 this->MinPnt[2] = bds[4];
145 this->MaxPnt[0] = bds[1];
146 this->MaxPnt[1] = bds[3];
147 this->MaxPnt[2] = bds[5];
148 }
149
150
152
157 vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
159
161
165 void SetMinPoint(double x, double y, double z);
166 void SetMinPoint(double p[3]);
168
170
174 void SetMaxPoint(double x, double y, double z);
175 void SetMaxPoint(double p[3]);
177
179
183 int IsValid() const;
184 static int IsValid(const double bounds[6]);
186
188
192 void AddPoint(double p[3]);
193 void AddPoint(double px, double py, double pz);
195
200 void AddBox(const vtkBoundingBox& bbox);
201
206 void AddBounds(const double bounds[6]);
207
211 bool IsSubsetOf(const vtkBoundingBox& bbox) const;
212
218 int IntersectBox(const vtkBoundingBox& bbox);
219
223 int Intersects(const vtkBoundingBox& bbox) const;
224
230 bool IntersectPlane(double origin[3], double normal[3]);
231
237 static bool IntersectsSphere(
238 const double min[3], const double max[3], const double center[3], double r2)
239 {
240 double d2 = 0.0;
241 for (int i = 0; i < 3; ++i)
242 {
243 if (center[i] < min[i])
244 {
245 d2 += (center[i] - min[i]) * (center[i] - min[i]);
246 }
247 else if (center[i] > max[i])
248 {
249 d2 += (center[i] - max[i]) * (center[i] - max[i]);
250 }
251 }
252 return (d2 <= r2);
253 }
254
259 bool IntersectsSphere(double center[3], double radius) const;
260
265 bool IntersectsSphere2(double center[3], double radius2) const
266 {
267 return vtkBoundingBox::IntersectsSphere(this->MinPnt, this->MaxPnt, center, radius2);
268 }
269
275 static bool InsideSphere(
276 const double min[3], const double max[3], const double center[3], double r2)
277 {
278 double dmin = 0.0, dmax = 0.0;
279 for (int i = 0; i < 3; ++i)
280 {
281 double a = (center[i] - min[i]) * (center[i] - min[i]);
282 double b = (center[i] - max[i]) * (center[i] - max[i]);
283 dmax += std::max(a, b);
284 if (min[i] <= center[i] && center[i] <= max[i])
285 {
286 dmin += std::min(a, b);
287 }
288 }
289 return (!(dmin <= r2 && r2 <= dmax));
290 }
291
296 bool InsideSphere(double center[3], double radius2) const
297 {
298 return vtkBoundingBox::InsideSphere(this->MinPnt, this->MaxPnt, center, radius2);
299 }
300
305 bool IntersectsLine(const double p1[3], const double p2[3]) const;
306
311
316 int Contains(const vtkBoundingBox& bbox) const;
317
334 static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
335 double xInt[3], int& plane);
336
338
341 void GetBounds(double bounds[6]) const;
342 void GetBounds(
343 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
345
349 double GetBound(int i) const;
350
352
355 const double* GetMinPoint() const VTK_SIZEHINT(3);
356 void GetMinPoint(double& x, double& y, double& z) const;
357 void GetMinPoint(double x[3]) const;
359
361
364 const double* GetMaxPoint() const VTK_SIZEHINT(3);
365 void GetMaxPoint(double& x, double& y, double& z) const;
366 void GetMaxPoint(double x[3]) const;
368
373 void GetCorner(int corner, double p[3]) const;
374
376
379 vtkTypeBool ContainsPoint(const double p[3]) const;
380 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
381 template <class PointT>
382 bool ContainsPoint(const PointT& p) const;
384
388 void GetCenter(double center[3]) const;
389
393 void GetLengths(double lengths[3]) const;
394
398 double GetLength(int i) const;
399
403 double GetMaxLength() const;
404
406
410 double GetDiagonalLength2() const;
411 double GetDiagonalLength() const;
413
415
426 void Inflate(double delta);
427 void Inflate(double deltaX, double deltaY, double deltaZ);
428 void Inflate();
429 void InflateSlice(double delta);
431
433
439 void Scale(double s[3]);
440 void Scale(double sx, double sy, double sz);
442
444
449 void ScaleAboutCenter(double s);
450 void ScaleAboutCenter(double s[3]);
451 void ScaleAboutCenter(double sx, double sy, double sz);
453
464 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
465
470 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
471
475 void Reset();
476
481 void ClampPoint(double point[3]);
482
489 void GetDistance(double point[3], double distance[3]);
490
495 void Translate(double motion[3]);
496
497protected:
498 double MinPnt[3], MaxPnt[3];
499};
500
501inline void vtkBoundingBox::Reset()
502{
503 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
504 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
505}
506
508 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
509{
510 xMin = this->MinPnt[0];
511 xMax = this->MaxPnt[0];
512 yMin = this->MinPnt[1];
513 yMax = this->MaxPnt[1];
514 zMin = this->MinPnt[2];
515 zMax = this->MaxPnt[2];
516}
517
518inline double vtkBoundingBox::GetBound(int i) const
519{
520 // If i is odd then when are returning a part of the max bounds
521 // else part of the min bounds is requested. The exact component
522 // needed is i /2 (or i right shifted by 1
523 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
524}
525
526inline const double* vtkBoundingBox::GetMinPoint() const
527{
528 return this->MinPnt;
529}
530
531inline void vtkBoundingBox::GetMinPoint(double x[3]) const
532{
533 x[0] = this->MinPnt[0];
534 x[1] = this->MinPnt[1];
535 x[2] = this->MinPnt[2];
536}
537
538inline const double* vtkBoundingBox::GetMaxPoint() const
539{
540 return this->MaxPnt;
541}
542
543inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
544{
545 x[0] = this->MaxPnt[0];
546 x[1] = this->MaxPnt[1];
547 x[2] = this->MaxPnt[2];
548}
549
550inline int vtkBoundingBox::IsValid() const
551{
552 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
553 (this->MinPnt[2] <= this->MaxPnt[2]));
554}
555
556inline int vtkBoundingBox::IsValid(const double bounds[6])
557{
558 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
559}
560
561inline double vtkBoundingBox::GetLength(int i) const
562{
563 return this->MaxPnt[i] - this->MinPnt[i];
564}
565
566inline void vtkBoundingBox::GetLengths(double lengths[3]) const
567{
568 lengths[0] = this->GetLength(0);
569 lengths[1] = this->GetLength(1);
570 lengths[2] = this->GetLength(2);
571}
572
573inline void vtkBoundingBox::GetCenter(double center[3]) const
574{
575 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
576 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
577 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
578}
579
580inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
581{
582 const double* bboxMaxPnt = bbox.GetMaxPoint();
583 const double* bboxMinPnt = bbox.GetMinPoint();
584 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
585 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
586 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
587}
588
589inline void vtkBoundingBox::SetBounds(const double bounds[6])
590{
591 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
592}
593
594inline void vtkBoundingBox::GetBounds(double bounds[6]) const
595{
596 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
597}
598
600{
601 this->Reset();
602}
603
604inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
605{
606 this->Reset();
607 this->SetBounds(bounds);
608}
609
611 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
612{
613 this->Reset();
614 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
615}
616
618{
619 this->MinPnt[0] = bbox.MinPnt[0];
620 this->MinPnt[1] = bbox.MinPnt[1];
621 this->MinPnt[2] = bbox.MinPnt[2];
622
623 this->MaxPnt[0] = bbox.MaxPnt[0];
624 this->MaxPnt[1] = bbox.MaxPnt[1];
625 this->MaxPnt[2] = bbox.MaxPnt[2];
626}
627
628inline vtkBoundingBox::vtkBoundingBox(const double min[3], const double max[3])
629{
630 this->MinPnt[0] = min[0];
631 this->MinPnt[1] = min[1];
632 this->MinPnt[2] = min[2];
633
634 this->MaxPnt[0] = max[0];
635 this->MaxPnt[1] = max[1];
636 this->MaxPnt[2] = max[2];
637}
638
639inline vtkBoundingBox::vtkBoundingBox(double center[3], double delta)
640{
641 this->Reset();
642 this->AddPoint(center);
643 this->Inflate(delta);
644}
645
647{
648 this->MinPnt[0] = bbox.MinPnt[0];
649 this->MinPnt[1] = bbox.MinPnt[1];
650 this->MinPnt[2] = bbox.MinPnt[2];
651
652 this->MaxPnt[0] = bbox.MaxPnt[0];
653 this->MaxPnt[1] = bbox.MaxPnt[1];
654 this->MaxPnt[2] = bbox.MaxPnt[2];
655 return *this;
656}
657
658inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
659{
660 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
661 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
662 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
663}
664
665inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
666{
667 return !((*this) == bbox);
668}
669
670inline void vtkBoundingBox::SetMinPoint(double p[3])
671{
672 this->SetMinPoint(p[0], p[1], p[2]);
673}
674
675inline void vtkBoundingBox::SetMaxPoint(double p[3])
676{
677 this->SetMaxPoint(p[0], p[1], p[2]);
678}
679
680inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
681{
682 x = this->MinPnt[0];
683 y = this->MinPnt[1];
684 z = this->MinPnt[2];
685}
686
687inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
688{
689 x = this->MaxPnt[0];
690 y = this->MaxPnt[1];
691 z = this->MaxPnt[2];
692}
693
694inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
695{
696 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
697 {
698 return 0;
699 }
700 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
701 {
702 return 0;
703 }
704 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
705 {
706 return 0;
707 }
708 return 1;
709}
710
711inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
712{
713 return this->ContainsPoint(p[0], p[1], p[2]);
714}
715
716template <class PointT>
717inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
718{
719 return this->ContainsPoint(p[0], p[1], p[2]);
720}
721
722inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
723{
724 if ((corner < 0) || (corner > 7))
725 {
726 p[0] = VTK_DOUBLE_MAX;
727 p[1] = VTK_DOUBLE_MAX;
728 p[2] = VTK_DOUBLE_MAX;
729 return; // out of bounds
730 }
731
732 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
733 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
734 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
735
736 const double* pts[2] = { this->MinPnt, this->MaxPnt };
737 p[0] = pts[ix][0];
738 p[1] = pts[iy][1];
739 p[2] = pts[iz][2];
740}
741
742VTK_ABI_NAMESPACE_END
743#endif
744// VTK-HeaderTest-Exclude: vtkBoundingBox.h
RealT r2
Definition PyrC2Basis.h:20
void ScaleAboutCenter(double s)
Scale each dimension of the box by some given factor, with the origin of the bounding box the center ...
static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double &t, double xInt[3], int &plane)
A specialized, performant method to compute the containment of a finite line emanating from the cente...
double GetDiagonalLength2() const
Return the length of the diagonal.
void Scale(double s[3])
Scale each dimension of the box by some given factor.
void ClampPoint(double point[3])
Clamp point so it is contained inside box.
void AddBounds(const double bounds[6])
Adjust the bounding box so it contains the specified bounds (defined by the VTK representation (xmin,...
vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const
Compute the number of divisions in the x-y-z directions given a psoitive, target number of total bins...
static void ComputeBounds(vtkPoints *pts, TIter ptIds, vtkIdType numPointIds, double bounds[6])
Compute the bounding box from an array of vtkPoints.
int IntersectBox(const vtkBoundingBox &bbox)
Intersect this box with bbox.
const double * GetMinPoint() const
Get the minimum point of the bounding box.
double GetDiagonalLength() const
Return the length of the diagonal.
bool InsideSphere(double center[3], double radius2) const
Determine if this bounding box is completely contained by a sphere.
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).
void AddBox(const vtkBoundingBox &bbox)
Change the bounding box to be the union of itself and the specified bbox.
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,...
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...
int Intersects(const vtkBoundingBox &bbox) const
Returns 1 if the boxes intersect else returns 0.
double GetMaxLength() const
Return the maximum length of the box.
bool operator!=(const vtkBoundingBox &bbox) const
Equality operator.
void AddPoint(double px, double py, double pz)
Change bounding box so it includes the point p.
void GetDistance(double point[3], double distance[3])
For each axis, get the minimum distance to put the point inside the box.
int ComputeInnerDimension() const
Returns the inner dimension of the bounding box.
void GetCorner(int corner, double p[3]) const
Get the ith corner of the bounding box.
void ComputeBounds(vtkPoints *pts)
Compute the bounding box from an array of vtkPoints.
bool IsSubsetOf(const vtkBoundingBox &bbox) const
Returns true if this instance is entirely contained by bbox.
static void ComputeBounds(vtkPoints *pts, double bounds[6])
Compute the bounding box from an array of vtkPoints.
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 Reset()
Returns the box to its initialized state.
bool IntersectPlane(double origin[3], double normal[3])
Intersect this box with the half space defined by plane.
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...
static void ComputeLocalBounds(vtkPoints *points, double u[3], double v[3], double w[3], double outputBounds[6])
Compute local bounds.
void GetCenter(double center[3]) const
Get the center of the bounding box.
void AddPoint(double p[3])
Change bounding box so it includes the point p.
bool IntersectsSphere2(double center[3], double radius2) const
Intersect this box with a sphere.
double GetLength(int i) const
Return the length of the bounding box in the ith direction.
bool operator==(const vtkBoundingBox &bbox) const
Equality operator.
vtkTypeBool ContainsPoint(const double p[3]) const
Returns 1 if the point is contained in the box else 0.
static void ClampDivisions(vtkIdType targetBins, int divs[3])
Clamp the number of divisions to be less than or equal to a target number of bins,...
vtkBoundingBox()
Construct a bounding box with the min point set to VTK_DOUBLE_MAX and the max point set to VTK_DOUBLE...
bool IntersectsSphere(double center[3], double radius) const
Intersect this box with a sphere.
void GetLengths(double lengths[3]) const
Get the length of each side of the box.
static bool IntersectsSphere(const double min[3], const double max[3], const double center[3], double r2)
Performant method to intersect box with a sphere.
void Inflate(double delta)
Expand the bounding box.
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
void InflateSlice(double delta)
Expand the bounding box.
static void ComputeBounds(vtkPoints *pts, const unsigned char *ptUses, double bounds[6])
Compute the bounding box from an array of vtkPoints.
void SetBounds(const double bounds[6])
Set the bounds explicitly of the box (using the VTK convention for representing a bounding box).
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
double GetBound(int i) const
Return the ith bounds of the box (defined by VTK style).
static void ComputeBounds(vtkPoints *pts, const std::atomic< unsigned char > *ptUses, double bounds[6])
Compute the bounding box from an array of vtkPoints.
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
void Translate(double motion[3])
Translate box from motion.
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...
static bool InsideSphere(const double min[3], const double max[3], const double center[3], double r2)
Performant method to determine if box if fully inside a sphere.
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
represent and manipulate 3D points
Definition vtkPoints.h:139
int vtkTypeBool
Definition vtkABI.h:64
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator!=(const std::string &a, const vtkStringToken &b)
int vtkIdType
Definition vtkType.h:367
#define VTK_DOUBLE_MIN
Definition vtkType.h:205
#define VTK_DOUBLE_MAX
Definition vtkType.h:206
#define VTK_SIZEHINT(...)
#define max(a, b)