VTK
dox/Common/vtkBoundingBox.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003 Program:   Visualization Toolkit
00004 Module:    vtkBoundingBox.h
00005 
00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007 All rights reserved.
00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010 This software is distributed WITHOUT ANY WARRANTY; without even
00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012 PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00029 #ifndef __vtkBoundingBox_h
00030 #define __vtkBoundingBox_h
00031 #include "vtkSystemIncludes.h"
00032 
00033 class VTK_COMMON_EXPORT vtkBoundingBox 
00034 {
00035 public:
00037 
00039   vtkBoundingBox();
00040   vtkBoundingBox(double bounds[6]);
00041   vtkBoundingBox(double xMin, double xMax,
00042                  double yMin, double yMax,
00043                  double zMin, double zMax);
00045   
00047   vtkBoundingBox(const vtkBoundingBox &bbox);
00048 
00050   vtkBoundingBox &operator=(const vtkBoundingBox &bbox);
00051 
00053 
00054   int operator==(const vtkBoundingBox &bbox)const;
00055   int operator!=(const vtkBoundingBox &bbox)const;
00057 
00059 
00061   void SetBounds(double bounds[6]);
00062   void SetBounds(double xMin, double xMax,
00063                  double yMin, double yMax,
00064                  double zMin, double zMax);
00066 
00068 
00070   void SetMinPoint(double x, double y, double z);
00071   void SetMinPoint(double p[3]);
00073 
00075 
00077   void SetMaxPoint(double x, double y, double z);
00078   void SetMaxPoint(double p[3]);
00080 
00082 
00084   void AddPoint(double p[3]);
00085   void AddPoint(double px, double py, double pz);
00087   
00089   void AddBox(const vtkBoundingBox &bbox);
00090   
00093   void AddBounds(double bounds[6]);
00094   
00095   // Desciption:
00096   // Intersect this box with bbox. The method returns 1 if
00097   // both boxes are valid and they do have overlap else it will return 0.
00098   // If 0 is returned the box has not been modified
00099   int IntersectBox(const vtkBoundingBox &bbox);
00100   
00102   int Intersects(const vtkBoundingBox &bbox) const;
00103 
00106   int Contains(const vtkBoundingBox &bbox) const;
00107 
00109 
00110   void GetBounds(double bounds[6]) const;
00111   void GetBounds(double &xMin, double &xMax,
00112                  double &yMin, double &yMax,
00113                  double &zMin, double &zMax) const;
00115     
00117   double GetBound(int i) const;
00118     
00120 
00121   const double *GetMinPoint() const;
00122   void GetMinPoint(double &x, double &y, double &z) const;
00124 
00126 
00127   const double *GetMaxPoint() const;
00128   void GetMaxPoint(double &x, double &y, double &z) const;
00130 
00132 
00133   int ContainsPoint(double p[3]) const;
00134   int ContainsPoint(double px, double py, double pz) const;
00136 
00138   void GetCenter(double center[3]) const;
00139 
00141   void GetLengths(double lengths[3]) const;
00142 
00144   double GetLength(int i) const;
00145 
00147   double GetMaxLength() const;
00148 
00150   double GetDiagonalLength() const;
00151 
00154   void Inflate(double delta);
00155 
00157 
00159   int IsValid() const;
00160   static int IsValid(double bounds[6]);
00162   
00164   void Reset();
00165 
00167 
00171   void Scale(double s[3]);
00172   void Scale(double sx,
00173              double sy,
00174              double sz);
00176 
00177 protected:
00178   double MinPnt[3], MaxPnt[3];
00179 };
00180 
00181 inline void vtkBoundingBox::Reset()
00182 {
00183   this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;    
00184   this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
00185 }
00186 
00187 inline void vtkBoundingBox::GetBounds(double &xMin, double &xMax,
00188                                       double &yMin, double &yMax,
00189                                       double &zMin, double &zMax) const
00190 {
00191   xMin = this->MinPnt[0];
00192   xMax = this->MaxPnt[0];
00193   yMin = this->MinPnt[1];
00194   yMax = this->MaxPnt[1];
00195   zMin = this->MinPnt[2];
00196   zMax = this->MaxPnt[2];
00197 }
00198 
00199 inline double vtkBoundingBox::GetBound(int i) const
00200 {
00201   // If i is odd then when are returning a part of the max bounds
00202   // else part of the min bounds is requested.  The exact component
00203   // needed is i /2 (or i right shifted by 1
00204   return ((i & 0x1) ? this->MaxPnt[i>>1] : this->MinPnt[i>>1]);
00205 }
00206 
00207 inline const double *vtkBoundingBox::GetMinPoint() const
00208 {
00209   return this->MinPnt;
00210 }
00211 
00212 inline const double *vtkBoundingBox::GetMaxPoint() const
00213 {
00214   return this->MaxPnt;
00215 }
00216 
00217 inline int vtkBoundingBox::IsValid() const
00218 {
00219   return ((this->MinPnt[0] <= this->MaxPnt[0]) && 
00220           (this->MinPnt[1] <= this->MaxPnt[1]) && 
00221           (this->MinPnt[2] <= this->MaxPnt[2]));
00222 } 
00223 
00224 inline int vtkBoundingBox::IsValid(double bounds[6])
00225 {
00226   return (bounds[0] <= bounds[1] &&
00227     bounds[2] <= bounds[3] &&
00228     bounds[4] <= bounds[5]);
00229 }
00230 
00231 inline double vtkBoundingBox::GetLength(int i) const
00232 {
00233   return this->MaxPnt[i] - this->MinPnt[i];
00234 }
00235 
00236 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
00237 {
00238   lengths[0] = this->GetLength(0);
00239   lengths[1] = this->GetLength(1);
00240   lengths[2] = this->GetLength(2);
00241 }
00242 
00243 inline void vtkBoundingBox::GetCenter(double center[3]) const
00244 {
00245   center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
00246   center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
00247   center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
00248 }
00249 
00250 inline void vtkBoundingBox::SetBounds(double bounds[6])
00251 {
00252   this->SetBounds(bounds[0], bounds[1], bounds[2],
00253                   bounds[3], bounds[4], bounds[5]);
00254 }
00255 
00256 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
00257 {
00258   this->GetBounds(bounds[0], bounds[1], bounds[2],
00259                   bounds[3], bounds[4], bounds[5]);
00260 }
00261 
00262 inline vtkBoundingBox::vtkBoundingBox()
00263 {
00264   this->Reset();
00265 }
00266 
00267 inline vtkBoundingBox::vtkBoundingBox(double bounds[6])
00268 {
00269   this->Reset();
00270   this->SetBounds(bounds);
00271 }
00272 
00273 inline vtkBoundingBox::vtkBoundingBox(double xMin, double xMax,
00274                                       double yMin, double yMax,
00275                                       double zMin, double zMax)
00276 {
00277   this->Reset();
00278   this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
00279 }
00280 
00281 inline vtkBoundingBox::vtkBoundingBox(const vtkBoundingBox &bbox)
00282 {
00283   this->MinPnt[0] = bbox.MinPnt[0];
00284   this->MinPnt[1] = bbox.MinPnt[1];
00285   this->MinPnt[2] = bbox.MinPnt[2];
00286 
00287   this->MaxPnt[0] = bbox.MaxPnt[0];
00288   this->MaxPnt[1] = bbox.MaxPnt[1];
00289   this->MaxPnt[2] = bbox.MaxPnt[2];
00290 }
00291 
00292 inline vtkBoundingBox &vtkBoundingBox::operator=(const vtkBoundingBox &bbox)
00293 {
00294   this->MinPnt[0] = bbox.MinPnt[0];
00295   this->MinPnt[1] = bbox.MinPnt[1];
00296   this->MinPnt[2] = bbox.MinPnt[2];
00297 
00298   this->MaxPnt[0] = bbox.MaxPnt[0];
00299   this->MaxPnt[1] = bbox.MaxPnt[1];
00300   this->MaxPnt[2] = bbox.MaxPnt[2];
00301   return *this;
00302 }
00303 
00304 inline int vtkBoundingBox::operator==(const vtkBoundingBox &bbox)const
00305 {
00306   return ((this->MinPnt[0] == bbox.MinPnt[0]) &&
00307           (this->MinPnt[1] == bbox.MinPnt[1]) &&
00308           (this->MinPnt[2] == bbox.MinPnt[2]) &&
00309           (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
00310           (this->MaxPnt[1] == bbox.MaxPnt[1]) &&
00311           (this->MaxPnt[2] == bbox.MaxPnt[2]));
00312 }
00313 
00314 inline int vtkBoundingBox::operator!=(const vtkBoundingBox &bbox)const
00315 {
00316   return !((*this) == bbox);
00317 }
00318 
00319 inline void vtkBoundingBox::SetMinPoint(double p[3])
00320 {
00321   this->SetMinPoint(p[0], p[1], p[2]);
00322 }
00323 
00324 inline void vtkBoundingBox::SetMaxPoint(double p[3])
00325 {
00326   this->SetMaxPoint(p[0], p[1], p[2]);
00327 }
00328 
00329 inline void vtkBoundingBox::GetMinPoint(double &x, double &y, double &z) const
00330 {
00331   x = this->MinPnt[0];
00332   y = this->MinPnt[1];
00333   z = this->MinPnt[2];
00334 }
00335 
00336 inline void vtkBoundingBox::GetMaxPoint(double &x, double &y, double &z) const
00337 {
00338   x = this->MaxPnt[0];
00339   y = this->MaxPnt[1];
00340   z = this->MaxPnt[2];
00341 }
00342 
00343 inline int vtkBoundingBox::ContainsPoint(double px, double py, 
00344                                          double pz) const
00345 {
00346   if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
00347     {
00348     return 0;
00349     }
00350   if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
00351     {
00352     return 0;
00353     }
00354   if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
00355     {
00356     return 0;
00357     }
00358   return 1;
00359 }
00360 
00361 inline int vtkBoundingBox::ContainsPoint(double p[3]) const
00362 {
00363   return this->ContainsPoint(p[0], p[1], p[2]);
00364 }
00365 
00366 #endif