VTK  9.2.6
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 =========================================================================*/
31 #ifndef vtkBoundingBox_h
32 #define vtkBoundingBox_h
33 #include "vtkCommonDataModelModule.h" // For export macro
34 #include "vtkSystemIncludes.h"
35 #include <atomic> // For threaded bounding box computation
36 
37 class vtkPoints;
38 
39 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
40 {
41 public:
43 
48  vtkBoundingBox(const double bounds[6]);
49  vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
51 
55  vtkBoundingBox(const vtkBoundingBox& bbox);
56 
60  vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
61 
63 
66  bool operator==(const vtkBoundingBox& bbox) const;
67  bool operator!=(const vtkBoundingBox& bbox) const;
69 
71 
75  void SetBounds(const double bounds[6]);
76  void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
78 
80 
87  static void ComputeBounds(vtkPoints* pts, double bounds[6]);
88  static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
89  static void ComputeBounds(
90  vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
91  void ComputeBounds(vtkPoints* pts) { this->ComputeBounds(pts, (unsigned char*)nullptr); }
92  void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
93  {
94  double bds[6];
95  vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
96  this->MinPnt[0] = bds[0];
97  this->MinPnt[1] = bds[2];
98  this->MinPnt[2] = bds[4];
99  this->MaxPnt[0] = bds[1];
100  this->MaxPnt[1] = bds[3];
101  this->MaxPnt[2] = bds[5];
102  }
104 
106 
110  static void ComputeLocalBounds(
111  vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
113 
115 
119  void SetMinPoint(double x, double y, double z);
120  void SetMinPoint(double p[3]);
122 
124 
128  void SetMaxPoint(double x, double y, double z);
129  void SetMaxPoint(double p[3]);
131 
133 
137  int IsValid() const;
138  static int IsValid(const double bounds[6]);
140 
142 
146  void AddPoint(double p[3]);
147  void AddPoint(double px, double py, double pz);
149 
154  void AddBox(const vtkBoundingBox& bbox);
155 
160  void AddBounds(const double bounds[]);
161 
165  bool IsSubsetOf(const vtkBoundingBox& bbox) const;
166 
172  int IntersectBox(const vtkBoundingBox& bbox);
173 
177  int Intersects(const vtkBoundingBox& bbox) const;
178 
184  bool IntersectPlane(double origin[3], double normal[3]);
185 
190  bool IntersectsSphere(double center[3], double squaredRadius) const;
191 
196  bool IntersectsLine(const double p1[3], const double p2[3]) const;
197 
201  int ComputeInnerDimension() const;
202 
207  int Contains(const vtkBoundingBox& bbox) const;
208 
210 
213  void GetBounds(double bounds[6]) const;
214  void GetBounds(
215  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
217 
221  double GetBound(int i) const;
222 
224 
227  const double* GetMinPoint() const VTK_SIZEHINT(3);
228  void GetMinPoint(double& x, double& y, double& z) const;
229  void GetMinPoint(double x[3]) const;
231 
233 
236  const double* GetMaxPoint() const VTK_SIZEHINT(3);
237  void GetMaxPoint(double& x, double& y, double& z) const;
238  void GetMaxPoint(double x[3]) const;
240 
245  void GetCorner(int corner, double p[3]) const;
246 
248 
251  vtkTypeBool ContainsPoint(const double p[3]) const;
252  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
253  template <class PointT>
254  bool ContainsPoint(const PointT& p) const;
256 
260  void GetCenter(double center[3]) const;
261 
265  void GetLengths(double lengths[3]) const;
266 
270  double GetLength(int i) const;
271 
275  double GetMaxLength() const;
276 
281  double GetDiagonalLength() const;
282 
284 
295  void Inflate(double delta);
296  void Inflate(double deltaX, double deltaY, double deltaZ);
297  void Inflate();
298  void InflateSlice(double delta);
300 
302 
308  void Scale(double s[3]);
309  void Scale(double sx, double sy, double sz);
311 
313 
318  void ScaleAboutCenter(double s);
319  void ScaleAboutCenter(double s[3]);
320  void ScaleAboutCenter(double sx, double sy, double sz);
322 
333  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
334 
339  static void ClampDivisions(vtkIdType targetBins, int divs[3]);
340 
344  void Reset();
345 
346 protected:
347  double MinPnt[3], MaxPnt[3];
348 };
349 
350 inline void vtkBoundingBox::Reset()
351 {
352  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
353  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
354 }
355 
357  double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
358 {
359  xMin = this->MinPnt[0];
360  xMax = this->MaxPnt[0];
361  yMin = this->MinPnt[1];
362  yMax = this->MaxPnt[1];
363  zMin = this->MinPnt[2];
364  zMax = this->MaxPnt[2];
365 }
366 
367 inline double vtkBoundingBox::GetBound(int i) const
368 {
369  // If i is odd then when are returning a part of the max bounds
370  // else part of the min bounds is requested. The exact component
371  // needed is i /2 (or i right shifted by 1
372  return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
373 }
374 
375 inline const double* vtkBoundingBox::GetMinPoint() const
376 {
377  return this->MinPnt;
378 }
379 
380 inline void vtkBoundingBox::GetMinPoint(double x[3]) const
381 {
382  x[0] = this->MinPnt[0];
383  x[1] = this->MinPnt[1];
384  x[2] = this->MinPnt[2];
385 }
386 
387 inline const double* vtkBoundingBox::GetMaxPoint() const
388 {
389  return this->MaxPnt;
390 }
391 
392 inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
393 {
394  x[0] = this->MaxPnt[0];
395  x[1] = this->MaxPnt[1];
396  x[2] = this->MaxPnt[2];
397 }
398 
399 inline int vtkBoundingBox::IsValid() const
400 {
401  return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
402  (this->MinPnt[2] <= this->MaxPnt[2]));
403 }
404 
405 inline int vtkBoundingBox::IsValid(const double bounds[6])
406 {
407  return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
408 }
409 
410 inline double vtkBoundingBox::GetLength(int i) const
411 {
412  return this->MaxPnt[i] - this->MinPnt[i];
413 }
414 
415 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
416 {
417  lengths[0] = this->GetLength(0);
418  lengths[1] = this->GetLength(1);
419  lengths[2] = this->GetLength(2);
420 }
421 
422 inline void vtkBoundingBox::GetCenter(double center[3]) const
423 {
424  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
425  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
426  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
427 }
428 
429 inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
430 {
431  const double* bboxMaxPnt = bbox.GetMaxPoint();
432  const double* bboxMinPnt = bbox.GetMinPoint();
433  return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
434  this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
435  this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
436 }
437 
438 inline void vtkBoundingBox::SetBounds(const double bounds[6])
439 {
440  this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
441 }
442 
443 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
444 {
445  this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
446 }
447 
449 {
450  this->Reset();
451 }
452 
453 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
454 {
455  this->Reset();
456  this->SetBounds(bounds);
457 }
458 
460  double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
461 {
462  this->Reset();
463  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
464 }
465 
467 {
468  this->MinPnt[0] = bbox.MinPnt[0];
469  this->MinPnt[1] = bbox.MinPnt[1];
470  this->MinPnt[2] = bbox.MinPnt[2];
471 
472  this->MaxPnt[0] = bbox.MaxPnt[0];
473  this->MaxPnt[1] = bbox.MaxPnt[1];
474  this->MaxPnt[2] = bbox.MaxPnt[2];
475 }
476 
478 {
479  this->MinPnt[0] = bbox.MinPnt[0];
480  this->MinPnt[1] = bbox.MinPnt[1];
481  this->MinPnt[2] = bbox.MinPnt[2];
482 
483  this->MaxPnt[0] = bbox.MaxPnt[0];
484  this->MaxPnt[1] = bbox.MaxPnt[1];
485  this->MaxPnt[2] = bbox.MaxPnt[2];
486  return *this;
487 }
488 
489 inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
490 {
491  return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
492  (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
493  (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
494 }
495 
496 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
497 {
498  return !((*this) == bbox);
499 }
500 
501 inline void vtkBoundingBox::SetMinPoint(double p[3])
502 {
503  this->SetMinPoint(p[0], p[1], p[2]);
504 }
505 
506 inline void vtkBoundingBox::SetMaxPoint(double p[3])
507 {
508  this->SetMaxPoint(p[0], p[1], p[2]);
509 }
510 
511 inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
512 {
513  x = this->MinPnt[0];
514  y = this->MinPnt[1];
515  z = this->MinPnt[2];
516 }
517 
518 inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
519 {
520  x = this->MaxPnt[0];
521  y = this->MaxPnt[1];
522  z = this->MaxPnt[2];
523 }
524 
525 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
526 {
527  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
528  {
529  return 0;
530  }
531  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
532  {
533  return 0;
534  }
535  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
536  {
537  return 0;
538  }
539  return 1;
540 }
541 
542 inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
543 {
544  return this->ContainsPoint(p[0], p[1], p[2]);
545 }
546 
547 template <class PointT>
548 inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
549 {
550  return this->ContainsPoint(p[0], p[1], p[2]);
551 }
552 
553 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
554 {
555  if ((corner < 0) || (corner > 7))
556  {
557  p[0] = VTK_DOUBLE_MAX;
558  p[1] = VTK_DOUBLE_MAX;
559  p[2] = VTK_DOUBLE_MAX;
560  return; // out of bounds
561  }
562 
563  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
564  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
565  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
566 
567  const double* pts[2] = { this->MinPnt, this->MaxPnt };
568  p[0] = pts[ix][0];
569  p[1] = pts[iy][1];
570  p[2] = pts[iz][2];
571 }
572 
573 #endif
574 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
void GetBounds(T a, double bds[6])
const double * GetMinPoint() const
Get the minimum point of the bounding box.
#define VTK_DOUBLE_MAX
Definition: vtkType.h:165
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 ComputeBounds(vtkPoints *pts)
Compute the bounding box from an array of vtkPoints.
bool operator!=(const vtkBoundingBox &bbox) const
Equality operator.
bool VTKCOMMONDATAMODEL_EXPORT operator!=(vtkEdgeBase e1, vtkEdgeBase e2)
static void ComputeBounds(vtkPoints *pts, double bounds[6])
Compute the bounding box from an array of vtkPoints.
int vtkIdType
Definition: vtkType.h:332
void GetCorner(int corner, double p[3]) const
Get the ith corner of the bounding box.
int vtkTypeBool
Definition: vtkABI.h:69
bool operator==(const vtkBoundingBox &bbox) const
Equality operator.
void GetCenter(double center[3]) const
Get the center of the bounding 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...
bool VTKCOMMONDATAMODEL_EXPORT operator==(vtkEdgeBase e1, vtkEdgeBase e2)
double GetBound(int i) const
Return the ith bounds of the box (defined by VTK style).
void GetLengths(double lengths[3]) const
Get the length of each side of the box.
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
vtkTypeBool ContainsPoint(const double p[3]) const
Returns 1 if the point is contained in the box else 0.
#define VTK_SIZEHINT(...)
bool IsSubsetOf(const vtkBoundingBox &bbox) const
Returns true if this instance is entirely contained by bbox.
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...
#define VTK_DOUBLE_MIN
Definition: vtkType.h:164
double GetLength(int i) const
Return the length of the bounding box in the ith direction.
void SetBounds(const double bounds[6])
Set the bounds explicitly of the box (using the VTK convention for representing a bounding box)...
vtkBoundingBox()
Construct a bounding box with the min point set to VTK_DOUBLE_MAX and the max point set to VTK_DOUBLE...
double MaxPnt[3]
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
represent and manipulate 3D points
Definition: vtkPoints.h:39
Fast, simple class for representing and operating on 3D bounds.
double MinPnt[3]