VTK  9.3.0
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
28#ifndef vtkBoundingBox_h
29#define vtkBoundingBox_h
30#include "vtkCommonDataModelModule.h" // For export macro
31#include "vtkSystemIncludes.h"
32#include <atomic> // For threaded bounding box computation
33
34VTK_ABI_NAMESPACE_BEGIN
35class vtkPoints;
36
37class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
38{
39public:
41
46 vtkBoundingBox(const double bounds[6]);
47 vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
49
53 vtkBoundingBox(const vtkBoundingBox& bbox);
54
58 vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
59
61
64 bool operator==(const vtkBoundingBox& bbox) const;
65 bool operator!=(const vtkBoundingBox& bbox) const;
67
69
73 void SetBounds(const double bounds[6]);
74 void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
76
78
85 static void ComputeBounds(vtkPoints* pts, double bounds[6]);
86 static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
87 static void ComputeBounds(
88 vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
89 static void ComputeBounds(
90 vtkPoints* pts, const long long* ptIds, long long numPointIds, double bounds[6]);
91 static void ComputeBounds(vtkPoints* pts, const long* ptIds, long numPointIds, double bounds[6]);
92 static void ComputeBounds(vtkPoints* pts, const int* ptIds, int numPointIds, double bounds[6]);
94 {
95 double bds[6];
97 this->MinPnt[0] = bds[0];
98 this->MinPnt[1] = bds[2];
99 this->MinPnt[2] = bds[4];
100 this->MaxPnt[0] = bds[1];
101 this->MaxPnt[1] = bds[3];
102 this->MaxPnt[2] = bds[5];
103 }
104 void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
105 {
106 double bds[6];
107 vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
108 this->MinPnt[0] = bds[0];
109 this->MinPnt[1] = bds[2];
110 this->MinPnt[2] = bds[4];
111 this->MaxPnt[0] = bds[1];
112 this->MaxPnt[1] = bds[3];
113 this->MaxPnt[2] = bds[5];
114 }
116
118
123 vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
125
127
131 void SetMinPoint(double x, double y, double z);
132 void SetMinPoint(double p[3]);
134
136
140 void SetMaxPoint(double x, double y, double z);
141 void SetMaxPoint(double p[3]);
143
145
149 int IsValid() const;
150 static int IsValid(const double bounds[6]);
152
154
158 void AddPoint(double p[3]);
159 void AddPoint(double px, double py, double pz);
161
166 void AddBox(const vtkBoundingBox& bbox);
167
172 void AddBounds(const double bounds[6]);
173
177 bool IsSubsetOf(const vtkBoundingBox& bbox) const;
178
184 int IntersectBox(const vtkBoundingBox& bbox);
185
189 int Intersects(const vtkBoundingBox& bbox) const;
190
196 bool IntersectPlane(double origin[3], double normal[3]);
197
202 bool IntersectsSphere(double center[3], double squaredRadius) const;
203
208 bool IntersectsLine(const double p1[3], const double p2[3]) const;
209
214
219 int Contains(const vtkBoundingBox& bbox) const;
220
237 static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
238 double xInt[3], int& plane);
239
241
244 void GetBounds(double bounds[6]) const;
245 void GetBounds(
246 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
248
252 double GetBound(int i) const;
253
255
258 const double* GetMinPoint() const VTK_SIZEHINT(3);
259 void GetMinPoint(double& x, double& y, double& z) const;
260 void GetMinPoint(double x[3]) const;
262
264
267 const double* GetMaxPoint() const VTK_SIZEHINT(3);
268 void GetMaxPoint(double& x, double& y, double& z) const;
269 void GetMaxPoint(double x[3]) const;
271
276 void GetCorner(int corner, double p[3]) const;
277
279
282 vtkTypeBool ContainsPoint(const double p[3]) const;
283 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
284 template <class PointT>
285 bool ContainsPoint(const PointT& p) const;
287
291 void GetCenter(double center[3]) const;
292
296 void GetLengths(double lengths[3]) const;
297
301 double GetLength(int i) const;
302
306 double GetMaxLength() const;
307
309
313 double GetDiagonalLength2() const;
314 double GetDiagonalLength() const;
316
318
329 void Inflate(double delta);
330 void Inflate(double deltaX, double deltaY, double deltaZ);
331 void Inflate();
332 void InflateSlice(double delta);
334
336
342 void Scale(double s[3]);
343 void Scale(double sx, double sy, double sz);
345
347
352 void ScaleAboutCenter(double s);
353 void ScaleAboutCenter(double s[3]);
354 void ScaleAboutCenter(double sx, double sy, double sz);
356
367 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
368
373 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
374
378 void Reset();
379
380protected:
381 double MinPnt[3], MaxPnt[3];
382};
383
384inline void vtkBoundingBox::Reset()
385{
386 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
387 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
388}
389
391 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
392{
393 xMin = this->MinPnt[0];
394 xMax = this->MaxPnt[0];
395 yMin = this->MinPnt[1];
396 yMax = this->MaxPnt[1];
397 zMin = this->MinPnt[2];
398 zMax = this->MaxPnt[2];
399}
400
401inline double vtkBoundingBox::GetBound(int i) const
402{
403 // If i is odd then when are returning a part of the max bounds
404 // else part of the min bounds is requested. The exact component
405 // needed is i /2 (or i right shifted by 1
406 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
407}
408
409inline const double* vtkBoundingBox::GetMinPoint() const
410{
411 return this->MinPnt;
412}
413
414inline void vtkBoundingBox::GetMinPoint(double x[3]) const
415{
416 x[0] = this->MinPnt[0];
417 x[1] = this->MinPnt[1];
418 x[2] = this->MinPnt[2];
419}
420
421inline const double* vtkBoundingBox::GetMaxPoint() const
422{
423 return this->MaxPnt;
424}
425
426inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
427{
428 x[0] = this->MaxPnt[0];
429 x[1] = this->MaxPnt[1];
430 x[2] = this->MaxPnt[2];
431}
432
433inline int vtkBoundingBox::IsValid() const
434{
435 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
436 (this->MinPnt[2] <= this->MaxPnt[2]));
437}
438
439inline int vtkBoundingBox::IsValid(const double bounds[6])
440{
441 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
442}
443
444inline double vtkBoundingBox::GetLength(int i) const
445{
446 return this->MaxPnt[i] - this->MinPnt[i];
447}
448
449inline void vtkBoundingBox::GetLengths(double lengths[3]) const
450{
451 lengths[0] = this->GetLength(0);
452 lengths[1] = this->GetLength(1);
453 lengths[2] = this->GetLength(2);
454}
455
456inline void vtkBoundingBox::GetCenter(double center[3]) const
457{
458 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
459 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
460 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
461}
462
463inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
464{
465 const double* bboxMaxPnt = bbox.GetMaxPoint();
466 const double* bboxMinPnt = bbox.GetMinPoint();
467 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
468 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
469 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
470}
471
472inline void vtkBoundingBox::SetBounds(const double bounds[6])
473{
474 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
475}
476
477inline void vtkBoundingBox::GetBounds(double bounds[6]) const
478{
479 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
480}
481
483{
484 this->Reset();
485}
486
487inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
488{
489 this->Reset();
490 this->SetBounds(bounds);
491}
492
494 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
495{
496 this->Reset();
497 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
498}
499
501{
502 this->MinPnt[0] = bbox.MinPnt[0];
503 this->MinPnt[1] = bbox.MinPnt[1];
504 this->MinPnt[2] = bbox.MinPnt[2];
505
506 this->MaxPnt[0] = bbox.MaxPnt[0];
507 this->MaxPnt[1] = bbox.MaxPnt[1];
508 this->MaxPnt[2] = bbox.MaxPnt[2];
509}
510
512{
513 this->MinPnt[0] = bbox.MinPnt[0];
514 this->MinPnt[1] = bbox.MinPnt[1];
515 this->MinPnt[2] = bbox.MinPnt[2];
516
517 this->MaxPnt[0] = bbox.MaxPnt[0];
518 this->MaxPnt[1] = bbox.MaxPnt[1];
519 this->MaxPnt[2] = bbox.MaxPnt[2];
520 return *this;
521}
522
523inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
524{
525 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
526 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
527 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
528}
529
530inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
531{
532 return !((*this) == bbox);
533}
534
535inline void vtkBoundingBox::SetMinPoint(double p[3])
536{
537 this->SetMinPoint(p[0], p[1], p[2]);
538}
539
540inline void vtkBoundingBox::SetMaxPoint(double p[3])
541{
542 this->SetMaxPoint(p[0], p[1], p[2]);
543}
544
545inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
546{
547 x = this->MinPnt[0];
548 y = this->MinPnt[1];
549 z = this->MinPnt[2];
550}
551
552inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
553{
554 x = this->MaxPnt[0];
555 y = this->MaxPnt[1];
556 z = this->MaxPnt[2];
557}
558
559inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
560{
561 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
562 {
563 return 0;
564 }
565 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
566 {
567 return 0;
568 }
569 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
570 {
571 return 0;
572 }
573 return 1;
574}
575
576inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
577{
578 return this->ContainsPoint(p[0], p[1], p[2]);
579}
580
581template <class PointT>
582inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
583{
584 return this->ContainsPoint(p[0], p[1], p[2]);
585}
586
587inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
588{
589 if ((corner < 0) || (corner > 7))
590 {
591 p[0] = VTK_DOUBLE_MAX;
592 p[1] = VTK_DOUBLE_MAX;
593 p[2] = VTK_DOUBLE_MAX;
594 return; // out of bounds
595 }
596
597 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
598 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
599 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
600
601 const double* pts[2] = { this->MinPnt, this->MaxPnt };
602 p[0] = pts[ix][0];
603 p[1] = pts[iy][1];
604 p[2] = pts[iz][2];
605}
606
607VTK_ABI_NAMESPACE_END
608#endif
609// 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:38
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(...)