VTK  9.4.20250206
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
48#ifndef vtkBoundingBox_h
49#define vtkBoundingBox_h
50#include "vtkCommonDataModelModule.h" // For export macro
51#include "vtkSystemIncludes.h"
52#include <atomic> // For threaded bounding box computation
53
54VTK_ABI_NAMESPACE_BEGIN
55class vtkPoints;
56
57class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
58{
59public:
61
69 vtkBoundingBox(const double bounds[6]);
73 vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
77 vtkBoundingBox(double center[3], double delta);
79
83 vtkBoundingBox(const vtkBoundingBox& bbox);
84
88 vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
89
91
94 bool operator==(const vtkBoundingBox& bbox) const;
95 bool operator!=(const vtkBoundingBox& bbox) const;
97
99
103 void SetBounds(const double bounds[6]);
104 void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
106
108
115 static void ComputeBounds(vtkPoints* pts, double bounds[6]);
116 static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
117 static void ComputeBounds(
118 vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
119 static void ComputeBounds(
120 vtkPoints* pts, const long long* ptIds, long long numPointIds, double bounds[6]);
121 static void ComputeBounds(vtkPoints* pts, const long* ptIds, long numPointIds, double bounds[6]);
122 static void ComputeBounds(vtkPoints* pts, const int* ptIds, int numPointIds, double bounds[6]);
124 {
125 double bds[6];
127 this->MinPnt[0] = bds[0];
128 this->MinPnt[1] = bds[2];
129 this->MinPnt[2] = bds[4];
130 this->MaxPnt[0] = bds[1];
131 this->MaxPnt[1] = bds[3];
132 this->MaxPnt[2] = bds[5];
133 }
134 void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
135 {
136 double bds[6];
137 vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
138 this->MinPnt[0] = bds[0];
139 this->MinPnt[1] = bds[2];
140 this->MinPnt[2] = bds[4];
141 this->MaxPnt[0] = bds[1];
142 this->MaxPnt[1] = bds[3];
143 this->MaxPnt[2] = bds[5];
144 }
146
148
153 vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
155
157
161 void SetMinPoint(double x, double y, double z);
162 void SetMinPoint(double p[3]);
164
166
170 void SetMaxPoint(double x, double y, double z);
171 void SetMaxPoint(double p[3]);
173
175
179 int IsValid() const;
180 static int IsValid(const double bounds[6]);
182
184
188 void AddPoint(double p[3]);
189 void AddPoint(double px, double py, double pz);
191
196 void AddBox(const vtkBoundingBox& bbox);
197
202 void AddBounds(const double bounds[6]);
203
207 bool IsSubsetOf(const vtkBoundingBox& bbox) const;
208
214 int IntersectBox(const vtkBoundingBox& bbox);
215
219 int Intersects(const vtkBoundingBox& bbox) const;
220
226 bool IntersectPlane(double origin[3], double normal[3]);
227
232 bool IntersectsSphere(double center[3], double squaredRadius) const;
233
238 bool IntersectsLine(const double p1[3], const double p2[3]) const;
239
244
249 int Contains(const vtkBoundingBox& bbox) const;
250
267 static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
268 double xInt[3], int& plane);
269
271
274 void GetBounds(double bounds[6]) const;
275 void GetBounds(
276 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
278
282 double GetBound(int i) const;
283
285
288 const double* GetMinPoint() const VTK_SIZEHINT(3);
289 void GetMinPoint(double& x, double& y, double& z) const;
290 void GetMinPoint(double x[3]) const;
292
294
297 const double* GetMaxPoint() const VTK_SIZEHINT(3);
298 void GetMaxPoint(double& x, double& y, double& z) const;
299 void GetMaxPoint(double x[3]) const;
301
306 void GetCorner(int corner, double p[3]) const;
307
309
312 vtkTypeBool ContainsPoint(const double p[3]) const;
313 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
314 template <class PointT>
315 bool ContainsPoint(const PointT& p) const;
317
321 void GetCenter(double center[3]) const;
322
326 void GetLengths(double lengths[3]) const;
327
331 double GetLength(int i) const;
332
336 double GetMaxLength() const;
337
339
343 double GetDiagonalLength2() const;
344 double GetDiagonalLength() const;
346
348
359 void Inflate(double delta);
360 void Inflate(double deltaX, double deltaY, double deltaZ);
361 void Inflate();
362 void InflateSlice(double delta);
364
366
372 void Scale(double s[3]);
373 void Scale(double sx, double sy, double sz);
375
377
382 void ScaleAboutCenter(double s);
383 void ScaleAboutCenter(double s[3]);
384 void ScaleAboutCenter(double sx, double sy, double sz);
386
397 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
398
403 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
404
408 void Reset();
409
414 void ClampPoint(double point[3]);
415
422 void GetDistance(double point[3], double distance[3]);
423
428 void Translate(double motion[3]);
429
430protected:
431 double MinPnt[3], MaxPnt[3];
432};
433
434inline void vtkBoundingBox::Reset()
435{
436 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
437 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
438}
439
441 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
442{
443 xMin = this->MinPnt[0];
444 xMax = this->MaxPnt[0];
445 yMin = this->MinPnt[1];
446 yMax = this->MaxPnt[1];
447 zMin = this->MinPnt[2];
448 zMax = this->MaxPnt[2];
449}
450
451inline double vtkBoundingBox::GetBound(int i) const
452{
453 // If i is odd then when are returning a part of the max bounds
454 // else part of the min bounds is requested. The exact component
455 // needed is i /2 (or i right shifted by 1
456 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
457}
458
459inline const double* vtkBoundingBox::GetMinPoint() const
460{
461 return this->MinPnt;
462}
463
464inline void vtkBoundingBox::GetMinPoint(double x[3]) const
465{
466 x[0] = this->MinPnt[0];
467 x[1] = this->MinPnt[1];
468 x[2] = this->MinPnt[2];
469}
470
471inline const double* vtkBoundingBox::GetMaxPoint() const
472{
473 return this->MaxPnt;
474}
475
476inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
477{
478 x[0] = this->MaxPnt[0];
479 x[1] = this->MaxPnt[1];
480 x[2] = this->MaxPnt[2];
481}
482
483inline int vtkBoundingBox::IsValid() const
484{
485 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
486 (this->MinPnt[2] <= this->MaxPnt[2]));
487}
488
489inline int vtkBoundingBox::IsValid(const double bounds[6])
490{
491 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
492}
493
494inline double vtkBoundingBox::GetLength(int i) const
495{
496 return this->MaxPnt[i] - this->MinPnt[i];
497}
498
499inline void vtkBoundingBox::GetLengths(double lengths[3]) const
500{
501 lengths[0] = this->GetLength(0);
502 lengths[1] = this->GetLength(1);
503 lengths[2] = this->GetLength(2);
504}
505
506inline void vtkBoundingBox::GetCenter(double center[3]) const
507{
508 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
509 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
510 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
511}
512
513inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
514{
515 const double* bboxMaxPnt = bbox.GetMaxPoint();
516 const double* bboxMinPnt = bbox.GetMinPoint();
517 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
518 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
519 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
520}
521
522inline void vtkBoundingBox::SetBounds(const double bounds[6])
523{
524 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
525}
526
527inline void vtkBoundingBox::GetBounds(double bounds[6]) const
528{
529 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
530}
531
533{
534 this->Reset();
535}
536
537inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
538{
539 this->Reset();
540 this->SetBounds(bounds);
541}
542
544 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
545{
546 this->Reset();
547 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
548}
549
551{
552 this->MinPnt[0] = bbox.MinPnt[0];
553 this->MinPnt[1] = bbox.MinPnt[1];
554 this->MinPnt[2] = bbox.MinPnt[2];
555
556 this->MaxPnt[0] = bbox.MaxPnt[0];
557 this->MaxPnt[1] = bbox.MaxPnt[1];
558 this->MaxPnt[2] = bbox.MaxPnt[2];
559}
560
561inline vtkBoundingBox::vtkBoundingBox(double center[3], double delta)
562{
563 this->Reset();
564 this->AddPoint(center);
565 this->Inflate(delta);
566}
567
569{
570 this->MinPnt[0] = bbox.MinPnt[0];
571 this->MinPnt[1] = bbox.MinPnt[1];
572 this->MinPnt[2] = bbox.MinPnt[2];
573
574 this->MaxPnt[0] = bbox.MaxPnt[0];
575 this->MaxPnt[1] = bbox.MaxPnt[1];
576 this->MaxPnt[2] = bbox.MaxPnt[2];
577 return *this;
578}
579
580inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
581{
582 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
583 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
584 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
585}
586
587inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
588{
589 return !((*this) == bbox);
590}
591
592inline void vtkBoundingBox::SetMinPoint(double p[3])
593{
594 this->SetMinPoint(p[0], p[1], p[2]);
595}
596
597inline void vtkBoundingBox::SetMaxPoint(double p[3])
598{
599 this->SetMaxPoint(p[0], p[1], p[2]);
600}
601
602inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
603{
604 x = this->MinPnt[0];
605 y = this->MinPnt[1];
606 z = this->MinPnt[2];
607}
608
609inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
610{
611 x = this->MaxPnt[0];
612 y = this->MaxPnt[1];
613 z = this->MaxPnt[2];
614}
615
616inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
617{
618 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
619 {
620 return 0;
621 }
622 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
623 {
624 return 0;
625 }
626 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
627 {
628 return 0;
629 }
630 return 1;
631}
632
633inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
634{
635 return this->ContainsPoint(p[0], p[1], p[2]);
636}
637
638template <class PointT>
639inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
640{
641 return this->ContainsPoint(p[0], p[1], p[2]);
642}
643
644inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
645{
646 if ((corner < 0) || (corner > 7))
647 {
648 p[0] = VTK_DOUBLE_MAX;
649 p[1] = VTK_DOUBLE_MAX;
650 p[2] = VTK_DOUBLE_MAX;
651 return; // out of bounds
652 }
653
654 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
655 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
656 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
657
658 const double* pts[2] = { this->MinPnt, this->MaxPnt };
659 p[0] = pts[ix][0];
660 p[1] = pts[iy][1];
661 p[2] = pts[iz][2];
662}
663
664VTK_ABI_NAMESPACE_END
665#endif
666// VTK-HeaderTest-Exclude: vtkBoundingBox.h
Fast, simple class for representing and operating on 3D bounds.
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...
static void ComputeBounds(vtkPoints *pts, const long long *ptIds, long long numPointIds, double bounds[6])
Compute the bounding box from an array of vtkPoints.
void AddBounds(const double bounds[6])
Adjust the bounding box so it contains the specified bounds (defined by the VTK representation (xmin,...
int IntersectBox(const vtkBoundingBox &bbox)
Intersect this box with bbox.
const double * GetMinPoint() const
Get the minimum point of the bounding box.
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.
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.
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.
bool IntersectsSphere(double center[3], double squaredRadius) const
Intersect this box with a sphere.
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 ...
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 ComputeBounds(vtkPoints *pts, const long *ptIds, long numPointIds, double bounds[6])
Compute the bounding box from an array of vtkPoints.
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.
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.
vtkBoundingBox()
Construct a bounding box with the min point set to VTK_DOUBLE_MAX and the max point set to VTK_DOUBLE...
static void ComputeBounds(vtkPoints *pts, const int *ptIds, int numPointIds, double bounds[6])
Compute the bounding box from an array of vtkPoints.
void GetLengths(double lengths[3]) const
Get the length of each side of the box.
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
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 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 & 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:315
#define VTK_DOUBLE_MIN
Definition vtkType.h:153
#define VTK_DOUBLE_MAX
Definition vtkType.h:154
#define VTK_SIZEHINT(...)