VTK
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 =========================================================================*/
30 #ifndef vtkBoundingBox_h
31 #define vtkBoundingBox_h
32 #include "vtkCommonDataModelModule.h" // For export macro
33 #include "vtkSystemIncludes.h"
34 
35 class VTKCOMMONDATAMODEL_EXPORT vtkBoundingBox
36 {
37 public:
39 
44  vtkBoundingBox(const double bounds[6]);
45  vtkBoundingBox(double xMin, double xMax,
46  double yMin, double yMax,
47  double zMin, double zMax);
49 
53  vtkBoundingBox(const vtkBoundingBox &bbox);
54 
58  vtkBoundingBox &operator=(const vtkBoundingBox &bbox);
59 
61 
64  bool operator==(const vtkBoundingBox &bbox)const;
65  bool operator!=(const vtkBoundingBox &bbox)const;
67 
69 
73  void SetBounds(const double bounds[6]);
74  void SetBounds(double xMin, double xMax,
75  double yMin, double yMax,
76  double zMin, double zMax);
78 
80 
84  void SetMinPoint(double x, double y, double z);
85  void SetMinPoint(double p[3]);
87 
89 
93  void SetMaxPoint(double x, double y, double z);
94  void SetMaxPoint(double p[3]);
96 
98 
102  int IsValid() const;
103  static int IsValid(const double bounds[6]);
105 
107 
111  void AddPoint(double p[3]);
112  void AddPoint(double px, double py, double pz);
114 
119  void AddBox(const vtkBoundingBox &bbox);
120 
125  void AddBounds(const double bounds[]);
126 
132  int IntersectBox(const vtkBoundingBox &bbox);
133 
137  int Intersects(const vtkBoundingBox &bbox) const;
138 
144  bool IntersectPlane(double origin[3],double normal[3]);
145 
150  int Contains(const vtkBoundingBox &bbox) const;
151 
153 
156  void GetBounds(double bounds[6]) const;
157  void GetBounds(double &xMin, double &xMax,
158  double &yMin, double &yMax,
159  double &zMin, double &zMax) const;
161 
165  double GetBound(int i) const;
166 
168 
171  const double *GetMinPoint() const VTK_SIZEHINT(3);
172  void GetMinPoint(double &x, double &y, double &z) const;
173  void GetMinPoint(double x[3]);
175 
177 
180  const double *GetMaxPoint() const VTK_SIZEHINT(3);
181  void GetMaxPoint(double &x, double &y, double &z) const;
182  void GetMaxPoint(double x[3]);
184 
189  void GetCorner(int corner, double p[3]) const;
190 
192 
195  vtkTypeBool ContainsPoint(double p[3]) const;
196  vtkTypeBool ContainsPoint(double px, double py, double pz) const;
198 
202  void GetCenter(double center[3]) const;
203 
207  void GetLengths(double lengths[3]) const;
208 
212  double GetLength(int i) const;
213 
217  double GetMaxLength() const;
218 
223  double GetDiagonalLength() const;
224 
226 
234  void Inflate(double delta);
235  void Inflate(double deltaX, double deltaY, double deltaZ);
236  void Inflate();
238 
240 
246  void Scale(double s[3]);
247  void Scale(double sx, double sy, double sz);
249 
251 
256  void ScaleAboutCenter(double s);
257  void ScaleAboutCenter(double s[3]);
258  void ScaleAboutCenter(double sx, double sy, double sz);
260 
271  vtkIdType ComputeDivisions(vtkIdType totalBins, double bounds[6], int divs[3]) const;
272 
276  void Reset();
277 
278 protected:
279  double MinPnt[3], MaxPnt[3];
280 };
281 
283 {
284  this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;
285  this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
286 }
287 
288 inline void vtkBoundingBox::GetBounds(double &xMin, double &xMax,
289  double &yMin, double &yMax,
290  double &zMin, double &zMax) const
291 {
292  xMin = this->MinPnt[0];
293  xMax = this->MaxPnt[0];
294  yMin = this->MinPnt[1];
295  yMax = this->MaxPnt[1];
296  zMin = this->MinPnt[2];
297  zMax = this->MaxPnt[2];
298 }
299 
300 inline double vtkBoundingBox::GetBound(int i) const
301 {
302  // If i is odd then when are returning a part of the max bounds
303  // else part of the min bounds is requested. The exact component
304  // needed is i /2 (or i right shifted by 1
305  return ((i & 0x1) ? this->MaxPnt[i>>1] : this->MinPnt[i>>1]);
306 }
307 
308 inline const double *vtkBoundingBox::GetMinPoint() const
309 {
310  return this->MinPnt;
311 }
312 
313 inline void vtkBoundingBox::GetMinPoint(double x[3])
314 {
315  x[0] = this->MinPnt[0];
316  x[1] = this->MinPnt[1];
317  x[2] = this->MinPnt[2];
318 }
319 
320 inline const double *vtkBoundingBox::GetMaxPoint() const
321 {
322  return this->MaxPnt;
323 }
324 
325 inline void vtkBoundingBox::GetMaxPoint(double x[3])
326 {
327  x[0] = this->MaxPnt[0];
328  x[1] = this->MaxPnt[1];
329  x[2] = this->MaxPnt[2];
330 }
331 
332 inline int vtkBoundingBox::IsValid() const
333 {
334  return ((this->MinPnt[0] <= this->MaxPnt[0]) &&
335  (this->MinPnt[1] <= this->MaxPnt[1]) &&
336  (this->MinPnt[2] <= this->MaxPnt[2]));
337 }
338 
339 inline int vtkBoundingBox::IsValid(const double bounds[6])
340 {
341  return (bounds[0] <= bounds[1] &&
342  bounds[2] <= bounds[3] &&
343  bounds[4] <= bounds[5]);
344 }
345 
346 inline double vtkBoundingBox::GetLength(int i) const
347 {
348  return this->MaxPnt[i] - this->MinPnt[i];
349 }
350 
351 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
352 {
353  lengths[0] = this->GetLength(0);
354  lengths[1] = this->GetLength(1);
355  lengths[2] = this->GetLength(2);
356 }
357 
358 inline void vtkBoundingBox::GetCenter(double center[3]) const
359 {
360  center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
361  center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
362  center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
363 }
364 
365 inline void vtkBoundingBox::SetBounds(const double bounds[6])
366 {
367  this->SetBounds(bounds[0], bounds[1], bounds[2],
368  bounds[3], bounds[4], bounds[5]);
369 }
370 
371 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
372 {
373  this->GetBounds(bounds[0], bounds[1], bounds[2],
374  bounds[3], bounds[4], bounds[5]);
375 }
376 
378 {
379  this->Reset();
380 }
381 
382 inline vtkBoundingBox::vtkBoundingBox(const double bounds[6])
383 {
384  this->Reset();
385  this->SetBounds(bounds);
386 }
387 
388 inline vtkBoundingBox::vtkBoundingBox(double xMin, double xMax,
389  double yMin, double yMax,
390  double zMin, double zMax)
391 {
392  this->Reset();
393  this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
394 }
395 
397 {
398  this->MinPnt[0] = bbox.MinPnt[0];
399  this->MinPnt[1] = bbox.MinPnt[1];
400  this->MinPnt[2] = bbox.MinPnt[2];
401 
402  this->MaxPnt[0] = bbox.MaxPnt[0];
403  this->MaxPnt[1] = bbox.MaxPnt[1];
404  this->MaxPnt[2] = bbox.MaxPnt[2];
405 }
406 
408 {
409  this->MinPnt[0] = bbox.MinPnt[0];
410  this->MinPnt[1] = bbox.MinPnt[1];
411  this->MinPnt[2] = bbox.MinPnt[2];
412 
413  this->MaxPnt[0] = bbox.MaxPnt[0];
414  this->MaxPnt[1] = bbox.MaxPnt[1];
415  this->MaxPnt[2] = bbox.MaxPnt[2];
416  return *this;
417 }
418 
419 inline bool vtkBoundingBox::operator==(const vtkBoundingBox &bbox)const
420 {
421  return ((this->MinPnt[0] == bbox.MinPnt[0]) &&
422  (this->MinPnt[1] == bbox.MinPnt[1]) &&
423  (this->MinPnt[2] == bbox.MinPnt[2]) &&
424  (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
425  (this->MaxPnt[1] == bbox.MaxPnt[1]) &&
426  (this->MaxPnt[2] == bbox.MaxPnt[2]));
427 }
428 
429 inline bool vtkBoundingBox::operator!=(const vtkBoundingBox &bbox)const
430 {
431  return !((*this) == bbox);
432 }
433 
434 inline void vtkBoundingBox::SetMinPoint(double p[3])
435 {
436  this->SetMinPoint(p[0], p[1], p[2]);
437 }
438 
439 inline void vtkBoundingBox::SetMaxPoint(double p[3])
440 {
441  this->SetMaxPoint(p[0], p[1], p[2]);
442 }
443 
444 inline void vtkBoundingBox::GetMinPoint(double &x, double &y, double &z) const
445 {
446  x = this->MinPnt[0];
447  y = this->MinPnt[1];
448  z = this->MinPnt[2];
449 }
450 
451 inline void vtkBoundingBox::GetMaxPoint(double &x, double &y, double &z) const
452 {
453  x = this->MaxPnt[0];
454  y = this->MaxPnt[1];
455  z = this->MaxPnt[2];
456 }
457 
458 inline vtkTypeBool vtkBoundingBox::ContainsPoint(double px, double py,
459  double pz) const
460 {
461  if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
462  {
463  return 0;
464  }
465  if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
466  {
467  return 0;
468  }
469  if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
470  {
471  return 0;
472  }
473  return 1;
474 }
475 
477 {
478  return this->ContainsPoint(p[0], p[1], p[2]);
479 }
480 
481 inline void vtkBoundingBox::GetCorner(int corner, double p[3]) const
482 {
483  if ((corner < 0) || (corner > 7))
484  {
485  p[0] = VTK_DOUBLE_MAX;
486  p[1] = VTK_DOUBLE_MAX;
487  p[2] = VTK_DOUBLE_MAX;
488  return; // out of bounds
489  }
490 
491  int ix = (corner & 1) ? 1 : 0; // 0,1,0,1,0,1,0,1
492  int iy = ((corner >> 1) & 1) ? 1 : 0; // 0,0,1,1,0,0,1,1
493  int iz = (corner >> 2) ? 1 : 0; // 0,0,0,0,1,1,1,1
494 
495  const double* pts[2] = { this->MinPnt, this->MaxPnt };
496  p[0] = pts[ix][0];
497  p[1] = pts[iy][1];
498  p[2] = pts[iz][2];
499 }
500 
501 #endif
502 // VTK-HeaderTest-Exclude: vtkBoundingBox.h
void GetBounds(double bounds[6]) const
Get the bounds of the box (defined by VTK style).
const double * GetMinPoint() const
Get the minimum point of the bounding box.
#define VTK_DOUBLE_MAX
Definition: vtkType.h:169
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 operator!=(const vtkBoundingBox &bbox) const
Equality operator.
void Reset()
Returns the box to its initialized state.
int vtkIdType
Definition: vtkType.h:347
vtkTypeBool ContainsPoint(double p[3]) const
Returns 1 if the point is contained in the box else 0.
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...
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 sode of the box.
vtkBoundingBox & operator=(const vtkBoundingBox &bbox)
Assignment Operator.
#define VTK_SIZEHINT(...)
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:168
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]
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
const double * GetMaxPoint() const
Get the maximum point of the bounding box.
Fast Simple Class for dealing with 3D bounds.
double MinPnt[3]