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 
00104 
00105   // Desciption:
00106   // Intersect this box with the half space defined by plane. 
00107    //Returns true if there is intersection---which implies that the box has been modified
00108   // Returns false otherwise
00109   bool IntersectPlane(double origin[3],double normal[3]);
00110 
00111 
00114   int Contains(const vtkBoundingBox &bbox) const;
00115 
00117 
00118   void GetBounds(double bounds[6]) const;
00119   void GetBounds(double &xMin, double &xMax,
00120                  double &yMin, double &yMax,
00121                  double &zMin, double &zMax) const;
00123     
00125   double GetBound(int i) const;
00126     
00128 
00129   const double *GetMinPoint() const;
00130   void GetMinPoint(double &x, double &y, double &z) const;
00132 
00134 
00135   const double *GetMaxPoint() const;
00136   void GetMaxPoint(double &x, double &y, double &z) const;
00138 
00140 
00141   int ContainsPoint(double p[3]) const;
00142   int ContainsPoint(double px, double py, double pz) const;
00144 
00146   void GetCenter(double center[3]) const;
00147 
00149   void GetLengths(double lengths[3]) const;
00150 
00152   double GetLength(int i) const;
00153 
00155   double GetMaxLength() const;
00156 
00158   double GetDiagonalLength() const;
00159 
00162   void Inflate(double delta);
00163 
00165 
00167   int IsValid() const;
00168   static int IsValid(double bounds[6]);
00170   
00172   void Reset();
00173 
00175 
00179   void Scale(double s[3]);
00180   void Scale(double sx,
00181              double sy,
00182              double sz);
00184 
00185 protected:
00186   double MinPnt[3], MaxPnt[3];
00187 };
00188 
00189 inline void vtkBoundingBox::Reset()
00190 {
00191   this->MinPnt[0] = this->MinPnt[1] = this->MinPnt[2] = VTK_DOUBLE_MAX;    
00192   this->MaxPnt[0] = this->MaxPnt[1] = this->MaxPnt[2] = VTK_DOUBLE_MIN;
00193 }
00194 
00195 inline void vtkBoundingBox::GetBounds(double &xMin, double &xMax,
00196                                       double &yMin, double &yMax,
00197                                       double &zMin, double &zMax) const
00198 {
00199   xMin = this->MinPnt[0];
00200   xMax = this->MaxPnt[0];
00201   yMin = this->MinPnt[1];
00202   yMax = this->MaxPnt[1];
00203   zMin = this->MinPnt[2];
00204   zMax = this->MaxPnt[2];
00205 }
00206 
00207 inline double vtkBoundingBox::GetBound(int i) const
00208 {
00209   // If i is odd then when are returning a part of the max bounds
00210   // else part of the min bounds is requested.  The exact component
00211   // needed is i /2 (or i right shifted by 1
00212   return ((i & 0x1) ? this->MaxPnt[i>>1] : this->MinPnt[i>>1]);
00213 }
00214 
00215 inline const double *vtkBoundingBox::GetMinPoint() const
00216 {
00217   return this->MinPnt;
00218 }
00219 
00220 inline const double *vtkBoundingBox::GetMaxPoint() const
00221 {
00222   return this->MaxPnt;
00223 }
00224 
00225 inline int vtkBoundingBox::IsValid() const
00226 {
00227   return ((this->MinPnt[0] <= this->MaxPnt[0]) && 
00228           (this->MinPnt[1] <= this->MaxPnt[1]) && 
00229           (this->MinPnt[2] <= this->MaxPnt[2]));
00230 } 
00231 
00232 inline int vtkBoundingBox::IsValid(double bounds[6])
00233 {
00234   return (bounds[0] <= bounds[1] &&
00235     bounds[2] <= bounds[3] &&
00236     bounds[4] <= bounds[5]);
00237 }
00238 
00239 inline double vtkBoundingBox::GetLength(int i) const
00240 {
00241   return this->MaxPnt[i] - this->MinPnt[i];
00242 }
00243 
00244 inline void vtkBoundingBox::GetLengths(double lengths[3]) const
00245 {
00246   lengths[0] = this->GetLength(0);
00247   lengths[1] = this->GetLength(1);
00248   lengths[2] = this->GetLength(2);
00249 }
00250 
00251 inline void vtkBoundingBox::GetCenter(double center[3]) const
00252 {
00253   center[0] = 0.5 * (this->MaxPnt[0] + this->MinPnt[0]);
00254   center[1] = 0.5 * (this->MaxPnt[1] + this->MinPnt[1]);
00255   center[2] = 0.5 * (this->MaxPnt[2] + this->MinPnt[2]);
00256 }
00257 
00258 inline void vtkBoundingBox::SetBounds(double bounds[6])
00259 {
00260   this->SetBounds(bounds[0], bounds[1], bounds[2],
00261                   bounds[3], bounds[4], bounds[5]);
00262 }
00263 
00264 inline void vtkBoundingBox::GetBounds(double bounds[6]) const
00265 {
00266   this->GetBounds(bounds[0], bounds[1], bounds[2],
00267                   bounds[3], bounds[4], bounds[5]);
00268 }
00269 
00270 inline vtkBoundingBox::vtkBoundingBox()
00271 {
00272   this->Reset();
00273 }
00274 
00275 inline vtkBoundingBox::vtkBoundingBox(double bounds[6])
00276 {
00277   this->Reset();
00278   this->SetBounds(bounds);
00279 }
00280 
00281 inline vtkBoundingBox::vtkBoundingBox(double xMin, double xMax,
00282                                       double yMin, double yMax,
00283                                       double zMin, double zMax)
00284 {
00285   this->Reset();
00286   this->SetBounds(xMin, xMax, yMin, yMax, zMin, zMax);
00287 }
00288 
00289 inline vtkBoundingBox::vtkBoundingBox(const vtkBoundingBox &bbox)
00290 {
00291   this->MinPnt[0] = bbox.MinPnt[0];
00292   this->MinPnt[1] = bbox.MinPnt[1];
00293   this->MinPnt[2] = bbox.MinPnt[2];
00294 
00295   this->MaxPnt[0] = bbox.MaxPnt[0];
00296   this->MaxPnt[1] = bbox.MaxPnt[1];
00297   this->MaxPnt[2] = bbox.MaxPnt[2];
00298 }
00299 
00300 inline vtkBoundingBox &vtkBoundingBox::operator=(const vtkBoundingBox &bbox)
00301 {
00302   this->MinPnt[0] = bbox.MinPnt[0];
00303   this->MinPnt[1] = bbox.MinPnt[1];
00304   this->MinPnt[2] = bbox.MinPnt[2];
00305 
00306   this->MaxPnt[0] = bbox.MaxPnt[0];
00307   this->MaxPnt[1] = bbox.MaxPnt[1];
00308   this->MaxPnt[2] = bbox.MaxPnt[2];
00309   return *this;
00310 }
00311 
00312 inline int vtkBoundingBox::operator==(const vtkBoundingBox &bbox)const
00313 {
00314   return ((this->MinPnt[0] == bbox.MinPnt[0]) &&
00315           (this->MinPnt[1] == bbox.MinPnt[1]) &&
00316           (this->MinPnt[2] == bbox.MinPnt[2]) &&
00317           (this->MaxPnt[0] == bbox.MaxPnt[0]) &&
00318           (this->MaxPnt[1] == bbox.MaxPnt[1]) &&
00319           (this->MaxPnt[2] == bbox.MaxPnt[2]));
00320 }
00321 
00322 inline int vtkBoundingBox::operator!=(const vtkBoundingBox &bbox)const
00323 {
00324   return !((*this) == bbox);
00325 }
00326 
00327 inline void vtkBoundingBox::SetMinPoint(double p[3])
00328 {
00329   this->SetMinPoint(p[0], p[1], p[2]);
00330 }
00331 
00332 inline void vtkBoundingBox::SetMaxPoint(double p[3])
00333 {
00334   this->SetMaxPoint(p[0], p[1], p[2]);
00335 }
00336 
00337 inline void vtkBoundingBox::GetMinPoint(double &x, double &y, double &z) const
00338 {
00339   x = this->MinPnt[0];
00340   y = this->MinPnt[1];
00341   z = this->MinPnt[2];
00342 }
00343 
00344 inline void vtkBoundingBox::GetMaxPoint(double &x, double &y, double &z) const
00345 {
00346   x = this->MaxPnt[0];
00347   y = this->MaxPnt[1];
00348   z = this->MaxPnt[2];
00349 }
00350 
00351 inline int vtkBoundingBox::ContainsPoint(double px, double py, 
00352                                          double pz) const
00353 {
00354   if ((px < this->MinPnt[0]) || (px > this->MaxPnt[0]))
00355     {
00356     return 0;
00357     }
00358   if ((py < this->MinPnt[1]) || (py > this->MaxPnt[1]))
00359     {
00360     return 0;
00361     }
00362   if ((pz < this->MinPnt[2]) || (pz > this->MaxPnt[2]))
00363     {
00364     return 0;
00365     }
00366   return 1;
00367 }
00368 
00369 inline int vtkBoundingBox::ContainsPoint(double p[3]) const
00370 {
00371   return this->ContainsPoint(p[0], p[1], p[2]);
00372 }
00373 
00374 #endif