VTK  9.2.6
vtkBoundingBox.h
Go to the documentation of this file.
1/*=========================================================================
2
3Program: Visualization Toolkit
4Module: vtkBoundingBox.h
5
6Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7All rights reserved.
8See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10This software is distributed WITHOUT ANY WARRANTY; without even
11the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
40#ifndef vtkBoundingBox_h
41#define vtkBoundingBox_h
42#include "vtkCommonDataModelModule.h" // For export macro
43#include "vtkSystemIncludes.h"
44#include <atomic> // For threaded bounding box computation
45
46class vtkPoints;
47
48class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
49{
50public:
52
57 vtkBoundingBox(const double bounds[6]);
58 vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
60
64 vtkBoundingBox(const vtkBoundingBox& bbox);
65
69 vtkBoundingBox& operator=(const vtkBoundingBox& bbox);
70
72
75 bool operator==(const vtkBoundingBox& bbox) const;
76 bool operator!=(const vtkBoundingBox& bbox) const;
78
80
84 void SetBounds(const double bounds[6]);
85 void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
87
89
96 static void ComputeBounds(vtkPoints* pts, double bounds[6]);
97 static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
98 static void ComputeBounds(
99 vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
100 void ComputeBounds(vtkPoints* pts) { this->ComputeBounds(pts, (unsigned char*)nullptr); }
101 void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
102 {
103 double bds[6];
104 vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
105 this->MinPnt[0] = bds[0];
106 this->MinPnt[1] = bds[2];
107 this->MinPnt[2] = bds[4];
108 this->MaxPnt[0] = bds[1];
109 this->MaxPnt[1] = bds[3];
110 this->MaxPnt[2] = bds[5];
111 }
113
115
120 vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
122
124
128 void SetMinPoint(double x, double y, double z);
129 void SetMinPoint(double p[3]);
131
133
137 void SetMaxPoint(double x, double y, double z);
138 void SetMaxPoint(double p[3]);
140
142
146 int IsValid() const;
147 static int IsValid(const double bounds[6]);
149
151
155 void AddPoint(double p[3]);
156 void AddPoint(double px, double py, double pz);
158
163 void AddBox(const vtkBoundingBox& bbox);
164
169 void AddBounds(const double bounds[]);
170
174 bool IsSubsetOf(const vtkBoundingBox& bbox) const;
175
181 int IntersectBox(const vtkBoundingBox& bbox);
182
186 int Intersects(const vtkBoundingBox& bbox) const;
187
193 bool IntersectPlane(double origin[3], double normal[3]);
194
199 bool IntersectsSphere(double center[3], double squaredRadius) const;
200
205 bool IntersectsLine(const double p1[3], const double p2[3]) const;
206
211
216 int Contains(const vtkBoundingBox& bbox) const;
217
219
222 void GetBounds(double bounds[6]) const;
223 void GetBounds(
224 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
226
230 double GetBound(int i) const;
231
233
236 const double* GetMinPoint() const VTK_SIZEHINT(3);
237 void GetMinPoint(double& x, double& y, double& z) const;
238 void GetMinPoint(double x[3]) const;
240
242
245 const double* GetMaxPoint() const VTK_SIZEHINT(3);
246 void GetMaxPoint(double& x, double& y, double& z) const;
247 void GetMaxPoint(double x[3]) const;
249
254 void GetCorner(int corner, double p[3]) const;
255
257
260 vtkTypeBool ContainsPoint(const double p[3]) const;
261 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
262 template <class PointT>
263 bool ContainsPoint(const PointT& p) const;
265
269 void GetCenter(double center[3]) const;
270
274 void GetLengths(double lengths[3]) const;
275
279 double GetLength(int i) const;
280
284 double GetMaxLength() const;
285
290 double GetDiagonalLength() const;
291
293
304 void Inflate(double delta);
305 void Inflate(double deltaX, double deltaY, double deltaZ);
306 void Inflate();
307 void InflateSlice(double delta);
309
311
317 void Scale(double s[3]);
318 void Scale(double sx, double sy, double sz);
320
322
327 void ScaleAboutCenter(double s);
328 void ScaleAboutCenter(double s[3]);
329 void ScaleAboutCenter(double sx, double sy, double sz);
331
342 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
343
348 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
349
353 void Reset();
354
355protected:
356 double MinPnt[3], MaxPnt[3];
357};
358
359inline void vtkBoundingBox::Reset()
360{
361 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
362 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
363}
364
366 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
367{
368 xMin = this->MinPnt[0];
369 xMax = this->MaxPnt[0];
370 yMin = this->MinPnt[1];
371 yMax = this->MaxPnt[1];
372 zMin = this->MinPnt[2];
373 zMax = this->MaxPnt[2];
374}
375
376inline double vtkBoundingBox::GetBound(int i) const
377{
378 // If i is odd then when are returning a part of the max bounds
379 // else part of the min bounds is requested. The exact component
380 // needed is i /2 (or i right shifted by 1
381 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
382}
383
384inline const double* vtkBoundingBox::GetMinPoint() const
385{
386 return this->MinPnt;
387}
388
389inline void vtkBoundingBox::GetMinPoint(double x[3]) const
390{
391 x[0] = this->MinPnt[0];
392 x[1] = this->MinPnt[1];
393 x[2] = this->MinPnt[2];
394}
395
396inline const double* vtkBoundingBox::GetMaxPoint() const
397{
398 return this->MaxPnt;
399}
400
401inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
402{
403 x[0] = this->MaxPnt[0];
404 x[1] = this->MaxPnt[1];
405 x[2] = this->MaxPnt[2];
406}
407
408inline int vtkBoundingBox::IsValid() const
409{
410 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
411 (this->MinPnt[2] <= this->MaxPnt[2]));
412}
413
414inline int vtkBoundingBox::IsValid(const double bounds[6])
415{
416 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
417}
418
419inline double vtkBoundingBox::GetLength(int i) const
420{
421 return this->MaxPnt[i] - this->MinPnt[i];
422}
423
424inline void vtkBoundingBox::GetLengths(double lengths[3]) const
425{
426 lengths[0] = this->GetLength(0);
427 lengths[1] = this->GetLength(1);
428 lengths[2] = this->GetLength(2);
429}
430
431inline void vtkBoundingBox::GetCenter(double center[3]) const
432{
433 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
434 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
435 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
436}
437
438inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
439{
440 const double* bboxMaxPnt = bbox.GetMaxPoint();
441 const double* bboxMinPnt = bbox.GetMinPoint();
442 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
443 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
444 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
445}
446
447inline void vtkBoundingBox::SetBounds(const double bounds[6])
448{
449 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
450}
451
452inline void vtkBoundingBox::GetBounds(double bounds[6]) const
453{
454 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
455}
456
458{
459 this->Reset();
460}
461
462inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
463{
464 this->Reset();
465 this->SetBounds(bounds);
466}
467
469 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
470{
471 this->Reset();
472 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
473}
474
476{
477 this->MinPnt[0] = bbox.MinPnt[0];
478 this->MinPnt[1] = bbox.MinPnt[1];
479 this->MinPnt[2] = bbox.MinPnt[2];
480
481 this->MaxPnt[0] = bbox.MaxPnt[0];
482 this->MaxPnt[1] = bbox.MaxPnt[1];
483 this->MaxPnt[2] = bbox.MaxPnt[2];
484}
485
487{
488 this->MinPnt[0] = bbox.MinPnt[0];
489 this->MinPnt[1] = bbox.MinPnt[1];
490 this->MinPnt[2] = bbox.MinPnt[2];
491
492 this->MaxPnt[0] = bbox.MaxPnt[0];
493 this->MaxPnt[1] = bbox.MaxPnt[1];
494 this->MaxPnt[2] = bbox.MaxPnt[2];
495 return *this;
496}
497
498inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
499{
500 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
501 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
502 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
503}
504
505inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
506{
507 return !((*this) == bbox);
508}
509
510inline void vtkBoundingBox::SetMinPoint(double p[3])
511{
512 this->SetMinPoint(p[0], p[1], p[2]);
513}
514
515inline void vtkBoundingBox::SetMaxPoint(double p[3])
516{
517 this->SetMaxPoint(p[0], p[1], p[2]);
518}
519
520inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
521{
522 x = this->MinPnt[0];
523 y = this->MinPnt[1];
524 z = this->MinPnt[2];
525}
526
527inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
528{
529 x = this->MaxPnt[0];
530 y = this->MaxPnt[1];
531 z = this->MaxPnt[2];
532}
533
534inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
535{
536 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
537 {
538 return 0;
539 }
540 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
541 {
542 return 0;
543 }
544 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
545 {
546 return 0;
547 }
548 return 1;
549}
550
551inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
552{
553 return this->ContainsPoint(p[0], p[1], p[2]);
554}
555
556template <class PointT>
557inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
558{
559 return this->ContainsPoint(p[0], p[1], p[2]);
560}
561
562inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
563{
564 if ((corner < 0) || (corner > 7))
565 {
566 p[0] = VTK_DOUBLE_MAX;
567 p[1] = VTK_DOUBLE_MAX;
568 p[2] = VTK_DOUBLE_MAX;
569 return; // out of bounds
570 }
571
572 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
573 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
574 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
575
576 const double* pts[2] = { this->MinPnt, this->MaxPnt };
577 p[0] = pts[ix][0];
578 p[1] = pts[iy][1];
579 p[2] = pts[iz][2];
580}
581
582#endif
583// 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...
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:49
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(...)