VTK  9.1.20220528
vtkBoundingBox.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: Visualization Toolkit
4 Module: vtkBoundingBox.h
5 
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
60 #ifndef vtkBoundingBox_h
61 #define vtkBoundingBox_h
62 #include "vtkCommonDataModelModule.h" // For export macro
63 #include "vtkSystemIncludes.h"
64 #include <atomic> // For threaded bounding box computation
65 
66 class vtkPoints;
67 
68 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
69 {
70 public:
72 
77  vtkBoundingBox(const double bounds[6]);
78  vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
80 
84  vtkBoundingBox(const vtkBoundingBox& bbox);
85 
89  vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
90 
92 
95  bool operator==(const vtkBoundingBox& bbox) const;
96  bool operator!=(const vtkBoundingBox& bbox) const;
98 
100 
104  void SetBounds(const double bounds[6]);
105  void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
107 
109 
116  static void ComputeBounds(vtkPoints* pts, double bounds[6]);
117  static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
118  static void ComputeBounds(
119  vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
120  void ComputeBounds(vtkPoints* pts) { this->ComputeBounds(pts, (unsigned char*)nullptr); }
121  void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
122  {
123  double bds[6];
124  vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
125  this->MinPnt[0] = bds[0];
126  this->MinPnt[1] = bds[2];
127  this->MinPnt[2] = bds[4];
128  this->MaxPnt[0] = bds[1];
129  this->MaxPnt[1] = bds[3];
130  this->MaxPnt[2] = bds[5];
131  }
133 
135 
139  static void ComputeLocalBounds(
140  vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
142 
144 
148  void SetMinPoint(double x, double y, double z);
149  void SetMinPoint(double p[3]);
151 
153 
157  void SetMaxPoint(double x, double y, double z);
158  void SetMaxPoint(double p[3]);
160 
162 
166  int IsValid() const;
167  static int IsValid(const double bounds[6]);
169 
171 
175  void AddPoint(double p[3]);
176  void AddPoint(double px, double py, double pz);
178 
183  void AddBox(const vtkBoundingBox& bbox);
184 
189  void AddBounds(const double bounds[]);
190 
194  bool IsSubsetOf(const vtkBoundingBox& bbox) const;
195 
201  int IntersectBox(const vtkBoundingBox& bbox);
202 
206  int Intersects(const vtkBoundingBox& bbox) const;
207 
213  bool IntersectPlane(double origin[3], double normal[3]);
214 
219  bool IntersectsSphere(double center[3], double squaredRadius) const;
220 
225  bool IntersectsLine(const double p1[3], const double p2[3]) const;
226 
231 
236  int Contains(const vtkBoundingBox& bbox) const;
237 
239 
242  void GetBounds(double bounds[6]) const;
243  void GetBounds(
244  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
246 
250  double GetBound(int i) const;
251 
253 
256  const double* GetMinPoint() const VTK_SIZEHINT(3);
257  void GetMinPoint(double& x, double& y, double& z) const;
258  void GetMinPoint(double x[3]) const;
260 
262 
265  const double* GetMaxPoint() const VTK_SIZEHINT(3);
266  void GetMaxPoint(double& x, double& y, double& z) const;
267  void GetMaxPoint(double x[3]) const;
269 
274  void GetCorner(int corner, double p[3]) const;
275 
277 
280  vtkTypeBool ContainsPoint(const double p[3]) const;
281  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
282  template <class PointT>
283  bool ContainsPoint(const PointT& p) const;
285 
289  void GetCenter(double center[3]) const;
290 
294  void GetLengths(double lengths[3]) const;
295 
299  double GetLength(int i) const;
300 
304  double GetMaxLength() const;
305 
310  double GetDiagonalLength() const;
311 
313 
324  void Inflate(double delta);
325  void Inflate(double deltaX, double deltaY, double deltaZ);
326  void Inflate();
327  void InflateSlice(double delta);
329 
331 
337  void Scale(double s[3]);
338  void Scale(double sx, double sy, double sz);
340 
342 
347  void ScaleAboutCenter(double s);
348  void ScaleAboutCenter(double s[3]);
349  void ScaleAboutCenter(double sx, double sy, double sz);
351 
362  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
363 
368  static void ClampDivisions(vtkIdType targetBins, int divs[3]);
369 
373  void Reset();
374 
375 protected:
376  double MinPnt[3], MaxPnt[3];
377 };
378 
379 inline void vtkBoundingBox::Reset()
380 {
381  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
382  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
383 }
384 
386  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
387 {
388  xMin = this->MinPnt[0];
389  xMax = this->MaxPnt[0];
390  yMin = this->MinPnt[1];
391  yMax = this->MaxPnt[1];
392  zMin = this->MinPnt[2];
393  zMax = this->MaxPnt[2];
394 }
395 
396 inline double vtkBoundingBox::GetBound(int i) const
397 {
398  // If i is odd then when are returning a part of the max bounds
399  // else part of the min bounds is requested. The exact component
400  // needed is i /2 (or i right shifted by 1
401  return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
402 }
403 
404 inline const double* vtkBoundingBox::GetMinPoint() const
405 {
406  return this->MinPnt;
407 }
408 
409 inline void vtkBoundingBox::GetMinPoint(double x[3]) const
410 {
411  x[0] = this->MinPnt[0];
412  x[1] = this->MinPnt[1];
413  x[2] = this->MinPnt[2];
414 }
415 
416 inline const double* vtkBoundingBox::GetMaxPoint() const
417 {
418  return this->MaxPnt;
419 }
420 
421 inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
422 {
423  x[0] = this->MaxPnt[0];
424  x[1] = this->MaxPnt[1];
425  x[2] = this->MaxPnt[2];
426 }
427 
428 inline int vtkBoundingBox::IsValid() const
429 {
430  return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
431  (this->MinPnt[2] <= this->MaxPnt[2]));
432 }
433 
434 inline int vtkBoundingBox::IsValid(const double bounds[6])
435 {
436  return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
437 }
438 
439 inline double vtkBoundingBox::GetLength(int i) const
440 {
441  return this->MaxPnt[i] - this->MinPnt[i];
442 }
443 
444 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
445 {
446  lengths[0] = this->GetLength(0);
447  lengths[1] = this->GetLength(1);
448  lengths[2] = this->GetLength(2);
449 }
450 
451 inline void vtkBoundingBox::GetCenter(double center[3]) const
452 {
453  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
454  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
455  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
456 }
457 
458 inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
459 {
460  const double* bboxMaxPnt = bbox.GetMaxPoint();
461  const double* bboxMinPnt = bbox.GetMinPoint();
462  return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
463  this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
464  this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
465 }
466 
467 inline void vtkBoundingBox::SetBounds(const double bounds[6])
468 {
469  this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
470 }
471 
472 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
473 {
474  this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
475 }
476 
478 {
479  this->Reset();
480 }
481 
482 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
483 {
484  this->Reset();
485  this->SetBounds(bounds);
486 }
487 
489  double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
490 {
491  this->Reset();
492  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
493 }
494 
496 {
497  this->MinPnt[0] = bbox.MinPnt[0];
498  this->MinPnt[1] = bbox.MinPnt[1];
499  this->MinPnt[2] = bbox.MinPnt[2];
500 
501  this->MaxPnt[0] = bbox.MaxPnt[0];
502  this->MaxPnt[1] = bbox.MaxPnt[1];
503  this->MaxPnt[2] = bbox.MaxPnt[2];
504 }
505 
507 {
508  this->MinPnt[0] = bbox.MinPnt[0];
509  this->MinPnt[1] = bbox.MinPnt[1];
510  this->MinPnt[2] = bbox.MinPnt[2];
511 
512  this->MaxPnt[0] = bbox.MaxPnt[0];
513  this->MaxPnt[1] = bbox.MaxPnt[1];
514  this->MaxPnt[2] = bbox.MaxPnt[2];
515  return *this;
516 }
517 
518 inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
519 {
520  return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
521  (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
522  (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
523 }
524 
525 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
526 {
527  return !((*this) == bbox);
528 }
529 
530 inline void vtkBoundingBox::SetMinPoint(double p[3])
531 {
532  this->SetMinPoint(p[0], p[1], p[2]);
533 }
534 
535 inline void vtkBoundingBox::SetMaxPoint(double p[3])
536 {
537  this->SetMaxPoint(p[0], p[1], p[2]);
538 }
539 
540 inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
541 {
542  x = this->MinPnt[0];
543  y = this->MinPnt[1];
544  z = this->MinPnt[2];
545 }
546 
547 inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
548 {
549  x = this->MaxPnt[0];
550  y = this->MaxPnt[1];
551  z = this->MaxPnt[2];
552 }
553 
554 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
555 {
556  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
557  {
558  return 0;
559  }
560  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
561  {
562  return 0;
563  }
564  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
565  {
566  return 0;
567  }
568  return 1;
569 }
570 
571 inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
572 {
573  return this->ContainsPoint(p[0], p[1], p[2]);
574 }
575 
576 template <class PointT>
577 inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
578 {
579  return this->ContainsPoint(p[0], p[1], p[2]);
580 }
581 
582 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
583 {
584  if ((corner < 0) || (corner > 7))
585  {
586  p[0] = VTK_DOUBLE_MAX;
587  p[1] = VTK_DOUBLE_MAX;
588  p[2] = VTK_DOUBLE_MAX;
589  return; // out of bounds
590  }
591 
592  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
593  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
594  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
595 
596  const double* pts[2] = { this->MinPnt, this->MaxPnt };
597  p[0] = pts[ix][0];
598  p[1] = pts[iy][1];
599  p[2] = pts[iz][2];
600 }
601 
602 #endif
603 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
Fast, simple class for representing and operating on 3D bounds.
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.
void AddBounds(const double bounds[])
Adjust the bounding box so it contains the specified bounds (defined by the VTK representation (xmin,...
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 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]
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:149
void GetBounds(T a, double bds[6])
@ points
Definition: vtkX3D.h:452
@ center
Definition: vtkX3D.h:236
int vtkTypeBool
Definition: vtkABI.h:69
bool VTKCOMMONDATAMODEL_EXPORT operator==(vtkEdgeBase e1, vtkEdgeBase e2)
bool VTKCOMMONDATAMODEL_EXPORT operator!=(vtkEdgeBase e1, vtkEdgeBase e2)
int vtkIdType
Definition: vtkType.h:332
#define VTK_DOUBLE_MIN
Definition: vtkType.h:164
#define VTK_DOUBLE_MAX
Definition: vtkType.h:165
#define VTK_SIZEHINT(...)