VTK  9.6.20260614
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
239 static bool IntersectsSphere(
240 const double min[3], const double max[3], const double center[3], double r2)
241 {
242 double d2 = 0.0;
243 for (int i = 0; i < 3; ++i)
244 {
245 if (center[i] < min[i])
246 {
247 d2 += (center[i] - min[i]) * (center[i] - min[i]);
248 }
249 else if (center[i] > max[i])
250 {
251 d2 += (center[i] - max[i]) * (center[i] - max[i]);
252 }
253 }
254 return (d2 <= r2);
255 }
256
261 bool IntersectsSphere(double center[3], double radius) const;
262
267 bool IntersectsSphere2(double center[3], double radius2) const
268 {
269 return vtkBoundingBox::IntersectsSphere(this->MinPnt, this->MaxPnt, center, radius2);
270 }
271
278 static bool InsideSphere(
279 const double min[3], const double max[3], const double center[3], double r2)
280 {
281 double dmax = 0.0;
282 for (int i = 0; i < 3; ++i)
283 {
284 double a = (center[i] - min[i]) * (center[i] - min[i]);
285 double b = (center[i] - max[i]) * (center[i] - max[i]);
286 dmax += std::max(a, b);
287 }
288 return dmax <= r2;
289 }
290
295 bool InsideSphere(double center[3], double radius2) const
296 {
297 return vtkBoundingBox::InsideSphere(this->MinPnt, this->MaxPnt, center, radius2);
298 }
299
304 bool IntersectsLine(const double p1[3], const double p2[3]) const;
305
310
315 int Contains(const vtkBoundingBox& bbox) const;
316
333 static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
334 double xInt[3], int& plane);
335
337
340 void GetBounds(double bounds[6]) const;
341 void GetBounds(
342 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
344
348 double GetBound(int i) const;
349
351
354 const double* GetMinPoint() const VTK_SIZEHINT(3);
355 void GetMinPoint(double& x, double& y, double& z) const;
356 void GetMinPoint(double x[3]) const;
358
360
363 const double* GetMaxPoint() const VTK_SIZEHINT(3);
364 void GetMaxPoint(double& x, double& y, double& z) const;
365 void GetMaxPoint(double x[3]) const;
367
372 void GetCorner(int corner, double p[3]) const;
373
375
378 vtkTypeBool ContainsPoint(const double p[3]) const;
379 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
380 template <class PointT>
381 bool ContainsPoint(const PointT& p) const;
383
387 void GetCenter(double center[3]) const;
388
392 void GetLengths(double lengths[3]) const;
393
397 double GetLength(int i) const;
398
402 double GetMaxLength() const;
403
405
409 double GetDiagonalLength2() const;
410 double GetDiagonalLength() const;
412
414
425 void Inflate(double delta);
426 void Inflate(double deltaX, double deltaY, double deltaZ);
427 void Inflate();
428 void InflateSlice(double delta);
430
432
438 void Scale(double s[3]);
439 void Scale(double sx, double sy, double sz);
441
443
448 void ScaleAboutCenter(double s);
449 void ScaleAboutCenter(double s[3]);
450 void ScaleAboutCenter(double sx, double sy, double sz);
452
463 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
464
469 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
470
474 void Reset();
475
480 void ClampPoint(double point[3]);
481
488 void GetDistance(double point[3], double distance[3]);
489
494 void Translate(double motion[3]);
495
496protected:
497 double MinPnt[3], MaxPnt[3];
498};
499
500inline void vtkBoundingBox::Reset()
501{
502 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
503 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
504}
505
507 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
508{
509 xMin = this->MinPnt[0];
510 xMax = this->MaxPnt[0];
511 yMin = this->MinPnt[1];
512 yMax = this->MaxPnt[1];
513 zMin = this->MinPnt[2];
514 zMax = this->MaxPnt[2];
515}
516
517inline double vtkBoundingBox::GetBound(int i) const
518{
519 // If i is odd then when are returning a part of the max bounds
520 // else part of the min bounds is requested. The exact component
521 // needed is i /2 (or i right shifted by 1
522 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
523}
524
525inline const double* vtkBoundingBox::GetMinPoint() const
526{
527 return this->MinPnt;
528}
529
530inline void vtkBoundingBox::GetMinPoint(double x[3]) const
531{
532 x[0] = this->MinPnt[0];
533 x[1] = this->MinPnt[1];
534 x[2] = this->MinPnt[2];
535}
536
537inline const double* vtkBoundingBox::GetMaxPoint() const
538{
539 return this->MaxPnt;
540}
541
542inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
543{
544 x[0] = this->MaxPnt[0];
545 x[1] = this->MaxPnt[1];
546 x[2] = this->MaxPnt[2];
547}
548
549inline int vtkBoundingBox::IsValid() const
550{
551 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
552 (this->MinPnt[2] <= this->MaxPnt[2]));
553}
554
555inline int vtkBoundingBox::IsValid(const double bounds[6])
556{
557 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
558}
559
560inline double vtkBoundingBox::GetLength(int i) const
561{
562 return this->MaxPnt[i] - this->MinPnt[i];
563}
564
565inline void vtkBoundingBox::GetLengths(double lengths[3]) const
566{
567 lengths[0] = this->GetLength(0);
568 lengths[1] = this->GetLength(1);
569 lengths[2] = this->GetLength(2);
570}
571
572inline void vtkBoundingBox::GetCenter(double center[3]) const
573{
574 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
575 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
576 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
577}
578
579inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
580{
581 const double* bboxMaxPnt = bbox.GetMaxPoint();
582 const double* bboxMinPnt = bbox.GetMinPoint();
583 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
584 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
585 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
586}
587
588inline void vtkBoundingBox::SetBounds(const double bounds[6])
589{
590 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
591}
592
593inline void vtkBoundingBox::GetBounds(double bounds[6]) const
594{
595 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
596}
597
599{
600 this->Reset();
601}
602
603inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
604{
605 this->Reset();
606 this->SetBounds(bounds);
607}
608
610 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
611{
612 this->Reset();
613 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
614}
615
617{
618 this->MinPnt[0] = bbox.MinPnt[0];
619 this->MinPnt[1] = bbox.MinPnt[1];
620 this->MinPnt[2] = bbox.MinPnt[2];
621
622 this->MaxPnt[0] = bbox.MaxPnt[0];
623 this->MaxPnt[1] = bbox.MaxPnt[1];
624 this->MaxPnt[2] = bbox.MaxPnt[2];
625}
626
627inline vtkBoundingBox::vtkBoundingBox(const double min[3], const double max[3])
628{
629 this->MinPnt[0] = min[0];
630 this->MinPnt[1] = min[1];
631 this->MinPnt[2] = min[2];
632
633 this->MaxPnt[0] = max[0];
634 this->MaxPnt[1] = max[1];
635 this->MaxPnt[2] = max[2];
636}
637
638inline vtkBoundingBox::vtkBoundingBox(double center[3], double delta)
639{
640 this->Reset();
641 this->AddPoint(center);
642 this->Inflate(delta);
643}
644
646{
647 this->MinPnt[0] = bbox.MinPnt[0];
648 this->MinPnt[1] = bbox.MinPnt[1];
649 this->MinPnt[2] = bbox.MinPnt[2];
650
651 this->MaxPnt[0] = bbox.MaxPnt[0];
652 this->MaxPnt[1] = bbox.MaxPnt[1];
653 this->MaxPnt[2] = bbox.MaxPnt[2];
654 return *this;
655}
656
657inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
658{
659 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
660 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
661 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
662}
663
664inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
665{
666 return !((*this) == bbox);
667}
668
669inline void vtkBoundingBox::SetMinPoint(double p[3])
670{
671 this->SetMinPoint(p[0], p[1], p[2]);
672}
673
674inline void vtkBoundingBox::SetMaxPoint(double p[3])
675{
676 this->SetMaxPoint(p[0], p[1], p[2]);
677}
678
679inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
680{
681 x = this->MinPnt[0];
682 y = this->MinPnt[1];
683 z = this->MinPnt[2];
684}
685
686inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
687{
688 x = this->MaxPnt[0];
689 y = this->MaxPnt[1];
690 z = this->MaxPnt[2];
691}
692
693inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
694{
695 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
696 {
697 return 0;
698 }
699 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
700 {
701 return 0;
702 }
703 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
704 {
705 return 0;
706 }
707 return 1;
708}
709
710inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
711{
712 return this->ContainsPoint(p[0], p[1], p[2]);
713}
714
715template <class PointT>
716inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
717{
718 return this->ContainsPoint(p[0], p[1], p[2]);
719}
720
721inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
722{
723 if ((corner < 0) || (corner > 7))
724 {
725 p[0] = VTK_DOUBLE_MAX;
726 p[1] = VTK_DOUBLE_MAX;
727 p[2] = VTK_DOUBLE_MAX;
728 return; // out of bounds
729 }
730
731 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
732 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
733 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
734
735 const double* pts[2] = { this->MinPnt, this->MaxPnt };
736 p[0] = pts[ix][0];
737 p[1] = pts[iy][1];
738 p[2] = pts[iz][2];
739}
740
741VTK_ABI_NAMESPACE_END
742#endif
743// 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)
Determine if a box is fully inside a sphere.
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
represent and manipulate 3D points
Definition vtkPoints.h:140
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:363
#define VTK_DOUBLE_MIN
Definition vtkType.h:201
#define VTK_DOUBLE_MAX
Definition vtkType.h:202
#define VTK_SIZEHINT(...)
#define max(a, b)