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=========================================================================*/
27
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
34class vtkPoints;
35
36class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
37{
38public:
40
45 vtkBoundingBox(const double bounds[6]);
46 vtkBoundingBox(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
48
52 vtkBoundingBox(const vtkBoundingBox& bbox);
53
58
60
63 bool operator==(const vtkBoundingBox& bbox) const;
64 bool operator!=(const vtkBoundingBox& bbox) const;
66
68
72 void SetBounds(const double bounds[6]);
73 void SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax);
75
77
84 static void ComputeBounds(vtkPoints* pts, double bounds[6]);
85 static void ComputeBounds(vtkPoints* pts, const unsigned char* ptUses, double bounds[6]);
86 static void ComputeBounds(
87 vtkPoints* pts, const std::atomic<unsigned char>* ptUses, double bounds[6]);
88 void ComputeBounds(vtkPoints* pts) { this->ComputeBounds(pts, (unsigned char*)nullptr); }
89 void ComputeBounds(vtkPoints* pts, unsigned char* ptUses)
90 {
91 double bds[6];
92 vtkBoundingBox::ComputeBounds(pts, ptUses, bds);
93 this->MinPnt[0] = bds[0];
94 this->MinPnt[1] = bds[2];
95 this->MinPnt[2] = bds[4];
96 this->MaxPnt[0] = bds[1];
97 this->MaxPnt[1] = bds[3];
98 this->MaxPnt[2] = bds[5];
99 }
100
101
103
108 vtkPoints* points, double u[3], double v[3], double w[3], double outputBounds[6]);
110
112
116 void SetMinPoint(double x, double y, double z);
117 void SetMinPoint(double p[3]);
119
121
125 void SetMaxPoint(double x, double y, double z);
126 void SetMaxPoint(double p[3]);
128
130
134 int IsValid() const;
135 static int IsValid(const double bounds[6]);
137
139
143 void AddPoint(double p[3]);
144 void AddPoint(double px, double py, double pz);
146
151 void AddBox(const vtkBoundingBox& bbox);
152
157 void AddBounds(const double bounds[]);
158
162 bool IsSubsetOf(const vtkBoundingBox& bbox) const;
163
169 int IntersectBox(const vtkBoundingBox& bbox);
170
174 int Intersects(const vtkBoundingBox& bbox) const;
175
181 bool IntersectPlane(double origin[3], double normal[3]);
182
187 bool IntersectsSphere(double center[3], double squaredRadius) const;
188
193 bool IntersectsLine(const double p1[3], const double p2[3]) const;
194
199
204 int Contains(const vtkBoundingBox& bbox) const;
205
207
210 void GetBounds(double bounds[6]) const;
211 void GetBounds(
212 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const;
214
218 double GetBound(int i) const;
219
221
224 const double* GetMinPoint() const VTK_SIZEHINT(3);
225 void GetMinPoint(double& x, double& y, double& z) const;
226 void GetMinPoint(double x[3]) const;
228
230
233 const double* GetMaxPoint() const VTK_SIZEHINT(3);
234 void GetMaxPoint(double& x, double& y, double& z) const;
235 void GetMaxPoint(double x[3]) const;
237
242 void GetCorner(int corner, double p[3]) const;
243
245
248 vtkTypeBool ContainsPoint(const double p[3]) const;
249 vtkTypeBool ContainsPoint(double px, double py, double pz) const;
250 template <class PointT>
251 bool ContainsPoint(const PointT& p) const;
253
257 void GetCenter(double center[3]) const;
258
262 void GetLengths(double lengths[3]) const;
263
267 double GetLength(int i) const;
268
272 double GetMaxLength() const;
273
278 double GetDiagonalLength() const;
279
281
292 void Inflate(double delta);
293 void Inflate(double deltaX, double deltaY, double deltaZ);
294 void Inflate();
295 void InflateSlice(double delta);
297
299
305 void Scale(double s[3]);
306 void Scale(double sx, double sy, double sz);
308
310
315 void ScaleAboutCenter(double s);
316 void ScaleAboutCenter(double s[3]);
317 void ScaleAboutCenter(double sx, double sy, double sz);
319
330 vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
331
336 static void ClampDivisions(vtkIdType targetBins, int divs[3]);
337
341 void Reset();
342
343protected:
344 double MinPnt[3], MaxPnt[3];
345};
346
347inline void vtkBoundingBox::Reset()
348{
349 this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
350 this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
351}
352
354 double& xMin, double& xMax, double& yMin, double& yMax, double& zMin, double& zMax) const
355{
356 xMin = this->MinPnt[0];
357 xMax = this->MaxPnt[0];
358 yMin = this->MinPnt[1];
359 yMax = this->MaxPnt[1];
360 zMin = this->MinPnt[2];
361 zMax = this->MaxPnt[2];
362}
363
364inline double vtkBoundingBox::GetBound(int i) const
365{
366 // If i is odd then when are returning a part of the max bounds
367 // else part of the min bounds is requested. The exact component
368 // needed is i /2 (or i right shifted by 1
369 return ((i & 0x1) ? this->MaxPnt[i >> 1] : this->MinPnt[i >> 1]);
370}
371
372inline const double* vtkBoundingBox::GetMinPoint() const
373{
374 return this->MinPnt;
375}
376
377inline void vtkBoundingBox::GetMinPoint(double x[3]) const
378{
379 x[0] = this->MinPnt[0];
380 x[1] = this->MinPnt[1];
381 x[2] = this->MinPnt[2];
382}
383
384inline const double* vtkBoundingBox::GetMaxPoint() const
385{
386 return this->MaxPnt;
387}
388
389inline void vtkBoundingBox::GetMaxPoint(double x[3]) const
390{
391 x[0] = this->MaxPnt[0];
392 x[1] = this->MaxPnt[1];
393 x[2] = this->MaxPnt[2];
394}
395
396inline int vtkBoundingBox::IsValid() const
397{
398 return ((this->MinPnt[0] <= this->MaxPnt[0]) && (this->MinPnt[1] <= this->MaxPnt[1]) &&
399 (this->MinPnt[2] <= this->MaxPnt[2]));
400}
401
402inline int vtkBoundingBox::IsValid(const double bounds[6])
403{
404 return (bounds[0] <= bounds[1] && bounds[2] <= bounds[3] && bounds[4] <= bounds[5]);
405}
406
407inline double vtkBoundingBox::GetLength(int i) const
408{
409 return this->MaxPnt[i] - this->MinPnt[i];
410}
411
412inline void vtkBoundingBox::GetLengths(double lengths[3]) const
413{
414 lengths[0] = this->GetLength(0);
415 lengths[1] = this->GetLength(1);
416 lengths[2] = this->GetLength(2);
417}
418
419inline void vtkBoundingBox::GetCenter(double center[3]) const
420{
421 center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
422 center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
423 center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
424}
425
426inline bool vtkBoundingBox::IsSubsetOf(const vtkBoundingBox& bbox) const
427{
428 const double* bboxMaxPnt = bbox.GetMaxPoint();
429 const double* bboxMinPnt = bbox.GetMinPoint();
430 return this->MaxPnt[0] < bboxMaxPnt[0] && this->MinPnt[0] > bboxMinPnt[0] &&
431 this->MaxPnt[1] < bboxMaxPnt[1] && this->MinPnt[1] > bboxMinPnt[1] &&
432 this->MaxPnt[2] < bboxMaxPnt[2] && this->MinPnt[2] > bboxMinPnt[2];
433}
434
435inline void vtkBoundingBox::SetBounds(const double bounds[6])
436{
437 this->SetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
438}
439
440inline void vtkBoundingBox::GetBounds(double bounds[6]) const
441{
442 this->GetBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
443}
444
446{
447 this->Reset();
448}
449
450inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
451{
452 this->Reset();
453 this->SetBounds(bounds);
454}
455
457 double xMin, double xMax, double yMin, double yMax, double zMin, double zMax)
458{
459 this->Reset();
460 this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
461}
462
464{
465 this->MinPnt[0] = bbox.MinPnt[0];
466 this->MinPnt[1] = bbox.MinPnt[1];
467 this->MinPnt[2] = bbox.MinPnt[2];
468
469 this->MaxPnt[0] = bbox.MaxPnt[0];
470 this->MaxPnt[1] = bbox.MaxPnt[1];
471 this->MaxPnt[2] = bbox.MaxPnt[2];
472}
473
475{
476 this->MinPnt[0] = bbox.MinPnt[0];
477 this->MinPnt[1] = bbox.MinPnt[1];
478 this->MinPnt[2] = bbox.MinPnt[2];
479
480 this->MaxPnt[0] = bbox.MaxPnt[0];
481 this->MaxPnt[1] = bbox.MaxPnt[1];
482 this->MaxPnt[2] = bbox.MaxPnt[2];
483 return *this;
484}
485
486inline bool vtkBoundingBox::operator==(const vtkBoundingBox& bbox) const
487{
488 return ((this->MinPnt[0] == bbox.MinPnt[0]) && (this->MinPnt[1] == bbox.MinPnt[1]) &&
489 (this->MinPnt[2] == bbox.MinPnt[2]) && (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
490 (this->MaxPnt[1] == bbox.MaxPnt[1]) && (this->MaxPnt[2] == bbox.MaxPnt[2]));
491}
492
493inline bool vtkBoundingBox::operator!=(const vtkBoundingBox& bbox) const
494{
495 return !((*this) == bbox);
496}
497
498inline void vtkBoundingBox::SetMinPoint(double p[3])
499{
500 this->SetMinPoint(p[0], p[1], p[2]);
501}
502
503inline void vtkBoundingBox::SetMaxPoint(double p[3])
504{
505 this->SetMaxPoint(p[0], p[1], p[2]);
506}
507
508inline void vtkBoundingBox::GetMinPoint(double& x, double& y, double& z) const
509{
510 x = this->MinPnt[0];
511 y = this->MinPnt[1];
512 z = this->MinPnt[2];
513}
514
515inline void vtkBoundingBox::GetMaxPoint(double& x, double& y, double& z) const
516{
517 x = this->MaxPnt[0];
518 y = this->MaxPnt[1];
519 z = this->MaxPnt[2];
520}
521
522inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py, double pz) const
523{
524 if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
525 {
526 return 0;
527 }
528 if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
529 {
530 return 0;
531 }
532 if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
533 {
534 return 0;
535 }
536 return 1;
537}
538
539inline vtkTypeBool vtkBoundingBox::ContainsPoint(const double p[3]) const
540{
541 return this->ContainsPoint(p[0], p[1], p[2]);
542}
543
544template <class PointT>
545inline bool vtkBoundingBox::ContainsPoint(const PointT& p) const
546{
547 return this->ContainsPoint(p[0], p[1], p[2]);
548}
549
550inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
551{
552 if ((corner < 0) || (corner > 7))
553 {
554 p[0] = VTK_DOUBLE_MAX;
555 p[1] = VTK_DOUBLE_MAX;
556 p[2] = VTK_DOUBLE_MAX;
557 return; // out of bounds
558 }
559
560 int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
561 int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
562 int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
563
564 const double* pts[2] = { this->MinPnt, this->MaxPnt };
565 p[0] = pts[ix][0];
566 p[1] = pts[iy][1];
567 p[2] = pts[iz][2];
568}
569
570#endif
571// VTK-HeaderTest-Exclude: vtkBoundingBox.h
void ScaleAboutCenter(double s)
Scale each dimension of the box by some given factor, with the origin of the bounding box the center ...
void Scale(double s[3])
Scale each dimension of the box by some given factor.
vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const
Compute the number of divisions in the x-y-z directions given a psoitive, target number of total bins...
int IntersectBox(const vtkBoundingBox &bbox)
Intersect this box with bbox.
const double * GetMinPoint() const
Get the minimum point of the bounding box.
double GetDiagonalLength() const
Return the length of the diagonal.
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.
double GetMaxLength() const
Return the maximum length of the box.
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 ...
void Reset()
Returns the box to its initialized state.
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.
static void ClampDivisions(vtkIdType targetBins, int divs[3])
Clamp the number of divisions to be less than or equal to a target number of bins,...
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 Inflate(double delta)
Expand the bounding box.
void ComputeBounds(vtkPoints *pts, unsigned char *ptUses)
Compute the bounding box from an array of vtkPoints.
void InflateSlice(double delta)
Expand the bounding box.
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:34
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(...)