VTK  9.3.20240328
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 
54 VTK_ABI_NAMESPACE_BEGIN
55 class vtkPoints;
56 
57 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
58 {
59 public:
61 
66  vtkBoundingBox(const double bounds[6]);
67  vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
69 
73  vtkBoundingBox(const vtkBoundingBox& bbox);
74 
78  vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
79 
81 
84  bool operator==(const vtkBoundingBox& bbox) const;
85  bool operator!=(const vtkBoundingBox& bbox) const;
87 
89 
93  void SetBounds(const double bounds[6]);
94  void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
96 
98 
105  static void ComputeBounds(vtkPoints* pts, double bounds[6]);
106  static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
107  static void ComputeBounds(
108  vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
109  static void ComputeBounds(
110  vtkPoints* pts, const long long* ptIds, long long numPointIds, double bounds[6]);
111  static void ComputeBounds(vtkPoints* pts, const long* ptIds, long numPointIds, double bounds[6]);
112  static void ComputeBounds(vtkPoints* pts, const int* ptIds, int numPointIds, double bounds[6]);
114  {
115  double bds[6];
117  this->MinPnt[0] = bds[0];
118  this->MinPnt[1] = bds[2];
119  this->MinPnt[2] = bds[4];
120  this->MaxPnt[0] = bds[1];
121  this->MaxPnt[1] = bds[3];
122  this->MaxPnt[2] = bds[5];
123  }
124  void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
125  {
126  double bds[6];
127  vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
128  this->MinPnt[0] = bds[0];
129  this->MinPnt[1] = bds[2];
130  this->MinPnt[2] = bds[4];
131  this->MaxPnt[0] = bds[1];
132  this->MaxPnt[1] = bds[3];
133  this->MaxPnt[2] = bds[5];
134  }
136 
138 
142  static void ComputeLocalBounds(
143  vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
145 
147 
151  void SetMinPoint(double x, double y, double z);
152  void SetMinPoint(double p[3]);
154 
156 
160  void SetMaxPoint(double x, double y, double z);
161  void SetMaxPoint(double p[3]);
163 
165 
169  int IsValid() const;
170  static int IsValid(const double bounds[6]);
172 
174 
178  void AddPoint(double p[3]);
179  void AddPoint(double px, double py, double pz);
181 
186  void AddBox(const vtkBoundingBox& bbox);
187 
192  void AddBounds(const double bounds[6]);
193 
197  bool IsSubsetOf(const vtkBoundingBox& bbox) const;
198 
204  int IntersectBox(const vtkBoundingBox& bbox);
205 
209  int Intersects(const vtkBoundingBox& bbox) const;
210 
216  bool IntersectPlane(double origin[3], double normal[3]);
217 
222  bool IntersectsSphere(double center[3], double squaredRadius) const;
223 
228  bool IntersectsLine(const double p1[3], const double p2[3]) const;
229 
234 
239  int Contains(const vtkBoundingBox& bbox) const;
240 
257  static bool ContainsLine(const double x[3], const double s[3], const double lineEnd[3], double& t,
258  double xInt[3], int& plane);
259 
261 
264  void GetBounds(double bounds[6]) const;
265  void GetBounds(
266  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
268 
272  double GetBound(int i) const;
273 
275 
278  const double* GetMinPoint() const VTK_SIZEHINT(3);
279  void GetMinPoint(double& x, double& y, double& z) const;
280  void GetMinPoint(double x[3]) const;
282 
284 
287  const double* GetMaxPoint() const VTK_SIZEHINT(3);
288  void GetMaxPoint(double& x, double& y, double& z) const;
289  void GetMaxPoint(double x[3]) const;
291 
296  void GetCorner(int corner, double p[3]) const;
297 
299 
302  vtkTypeBool ContainsPoint(const double p[3]) const;
303  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
304  template <class PointT>
305  bool ContainsPoint(const PointT& p) const;
307 
311  void GetCenter(double center[3]) const;
312 
316  void GetLengths(double lengths[3]) const;
317 
321  double GetLength(int i) const;
322 
326  double GetMaxLength() const;
327 
329 
333  double GetDiagonalLength2() const;
334  double GetDiagonalLength() const;
336 
338 
349  void Inflate(double delta);
350  void Inflate(double deltaX, double deltaY, double deltaZ);
351  void Inflate();
352  void InflateSlice(double delta);
354 
356 
362  void Scale(double s[3]);
363  void Scale(double sx, double sy, double sz);
365 
367 
372  void ScaleAboutCenter(double s);
373  void ScaleAboutCenter(double s[3]);
374  void ScaleAboutCenter(double sx, double sy, double sz);
376 
387  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
388 
393  static void ClampDivisions(vtkIdType targetBins, int divs[3]);
394 
398  void Reset();
399 
400 protected:
401  double MinPnt[3], MaxPnt[3];
402 };
403 
404 inline void vtkBoundingBox::Reset()
405 {
406  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
407  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
408 }
409 
411  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
412 {
413  xMin = this->MinPnt[0];
414  xMax = this->MaxPnt[0];
415  yMin = this->MinPnt[1];
416  yMax = this->MaxPnt[1];
417  zMin = this->MinPnt[2];
418  zMax = this->MaxPnt[2];
419 }
420 
421 inline double vtkBoundingBox::GetBound(int i) const
422 {
423  // If i is odd then when are returning a part of the max bounds
424  // else part of the min bounds is requested. The exact component
425  // needed is i /2 (or i right shifted by 1
426  return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
427 }
428 
429 inline const double* vtkBoundingBox::GetMinPoint() const
430 {
431  return this->MinPnt;
432 }
433 
434 inline void vtkBoundingBox::GetMinPoint(double x[3]) const
435 {
436  x[0] = this->MinPnt[0];
437  x[1] = this->MinPnt[1];
438  x[2] = this->MinPnt[2];
439 }
440 
441 inline const double* vtkBoundingBox::GetMaxPoint() const
442 {
443  return this->MaxPnt;
444 }
445 
446 inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
447 {
448  x[0] = this->MaxPnt[0];
449  x[1] = this->MaxPnt[1];
450  x[2] = this->MaxPnt[2];
451 }
452 
453 inline int vtkBoundingBox::IsValid() const
454 {
455  return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
456  (this->MinPnt[2] <= this->MaxPnt[2]));
457 }
458 
459 inline int vtkBoundingBox::IsValid(const double bounds[6])
460 {
461  return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
462 }
463 
464 inline double vtkBoundingBox::GetLength(int i) const
465 {
466  return this->MaxPnt[i] - this->MinPnt[i];
467 }
468 
469 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
470 {
471  lengths[0] = this->GetLength(0);
472  lengths[1] = this->GetLength(1);
473  lengths[2] = this->GetLength(2);
474 }
475 
476 inline void vtkBoundingBox::GetCenter(double center[3]) const
477 {
478  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
479  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
480  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
481 }
482 
483 inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
484 {
485  const double* bboxMaxPnt = bbox.GetMaxPoint();
486  const double* bboxMinPnt = bbox.GetMinPoint();
487  return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
488  this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
489  this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
490 }
491 
492 inline void vtkBoundingBox::SetBounds(const double bounds[6])
493 {
494  this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
495 }
496 
497 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
498 {
499  this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
500 }
501 
503 {
504  this->Reset();
505 }
506 
507 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
508 {
509  this->Reset();
510  this->SetBounds(bounds);
511 }
512 
514  double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
515 {
516  this->Reset();
517  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
518 }
519 
521 {
522  this->MinPnt[0] = bbox.MinPnt[0];
523  this->MinPnt[1] = bbox.MinPnt[1];
524  this->MinPnt[2] = bbox.MinPnt[2];
525 
526  this->MaxPnt[0] = bbox.MaxPnt[0];
527  this->MaxPnt[1] = bbox.MaxPnt[1];
528  this->MaxPnt[2] = bbox.MaxPnt[2];
529 }
530 
532 {
533  this->MinPnt[0] = bbox.MinPnt[0];
534  this->MinPnt[1] = bbox.MinPnt[1];
535  this->MinPnt[2] = bbox.MinPnt[2];
536 
537  this->MaxPnt[0] = bbox.MaxPnt[0];
538  this->MaxPnt[1] = bbox.MaxPnt[1];
539  this->MaxPnt[2] = bbox.MaxPnt[2];
540  return *this;
541 }
542 
543 inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
544 {
545  return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
546  (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
547  (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
548 }
549 
550 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
551 {
552  return !((*this) == bbox);
553 }
554 
555 inline void vtkBoundingBox::SetMinPoint(double p[3])
556 {
557  this->SetMinPoint(p[0], p[1], p[2]);
558 }
559 
560 inline void vtkBoundingBox::SetMaxPoint(double p[3])
561 {
562  this->SetMaxPoint(p[0], p[1], p[2]);
563 }
564 
565 inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
566 {
567  x = this->MinPnt[0];
568  y = this->MinPnt[1];
569  z = this->MinPnt[2];
570 }
571 
572 inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
573 {
574  x = this->MaxPnt[0];
575  y = this->MaxPnt[1];
576  z = this->MaxPnt[2];
577 }
578 
579 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
580 {
581  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
582  {
583  return 0;
584  }
585  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
586  {
587  return 0;
588  }
589  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
590  {
591  return 0;
592  }
593  return 1;
594 }
595 
596 inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
597 {
598  return this->ContainsPoint(p[0], p[1], p[2]);
599 }
600 
601 template <class PointT>
602 inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
603 {
604  return this->ContainsPoint(p[0], p[1], p[2]);
605 }
606 
607 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
608 {
609  if ((corner < 0) || (corner > 7))
610  {
611  p[0] = VTK_DOUBLE_MAX;
612  p[1] = VTK_DOUBLE_MAX;
613  p[2] = VTK_DOUBLE_MAX;
614  return; // out of bounds
615  }
616 
617  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
618  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
619  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
620 
621  const double* pts[2] = { this->MinPnt, this->MaxPnt };
622  p[0] = pts[ix][0];
623  p[1] = pts[iy][1];
624  p[2] = pts[iz][2];
625 }
626 
627 VTK_ABI_NAMESPACE_END
628 #endif
629 // 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...
double MinPnt[3]
double MaxPnt[3]
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:138
void GetBounds(T a, double bds[6])
@ points
Definition: vtkX3D.h:446
@ center
Definition: vtkX3D.h:230
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(...)