box3.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: box3.h,v 1.46 2005/12/23 17:01:48 amoll Exp $
00005 //
00006 
00007 #ifndef BALL_MATHS_BOX3_H
00008 #define BALL_MATHS_BOX3_H
00009 
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013 
00014 #ifndef BALL_MATHS_VECTOR3_H
00015 # include <BALL/MATHS/vector3.h>
00016 #endif
00017 
00018 namespace BALL 
00019 {
00025 
00031   template <typename T>
00032   class TBox3
00033   {
00034     public:
00035 
00036     BALL_CREATE(TBox3<T>)
00037 
00038     
00041 
00044     TBox3();
00045 
00049     TBox3(const TBox3& box);
00050 
00053     TBox3(const TVector3<T>& point, 
00054           const TVector3<T>& right_vector  = TVector3<T>((T) 0, (T)1, (T)0),
00055           const TVector3<T>& height_vector = TVector3<T>((T)-1, (T)0, (T)0),
00056           const T& depth = 1);
00057 
00060     virtual ~TBox3()
00061     {
00062     }
00063 
00067     virtual void clear();
00068 
00070 
00072 
00076     void set(const TBox3& box);
00077 
00082     const TBox3& operator = (const TBox3& box);
00083 
00087     void swap(TBox3& box);
00088 
00090 
00092     
00094     void setWidth(T width);
00095 
00097     T getWidth() const { return width_;}
00098 
00100     void setHeight(T height) ;
00101     
00103     T getHeight() const { return height_;}
00104 
00106     void setDepth(T depth)  { depth_ = depth;}
00107 
00109     T getDepth() const { return depth_;}
00110 
00112     const TVector3<T>& getPoint() const { return point_;}
00113 
00115     void setPoint(const TVector3<T>& point)  { point_ = point;}
00116 
00118     const TVector3<T>& getRightVector() const { return right_vector_;}
00119 
00121     void setRightVector(const TVector3<T>& v);
00122 
00124     const TVector3<T>& getHeightVector() const { return height_vector_;}
00125     
00127     void setHeightVector(const TVector3<T>& v);
00128 
00132     T getSurface() const;
00133 
00137     T getVolume() const;
00138 
00140     TVector3<T> getDiagonalVector() const;
00141 
00143 
00145 
00149     bool operator == (const TBox3& box) const;
00150 
00154     bool operator != (const TBox3& box) const;
00155 
00156     /*  Test if a given point is a member of the box.
00157         Optional it can be testet, if the point lies on the surface.
00158         @param point the point to be tested
00159         @param on_surface true to test the surface (default = false)
00160         @return bool, <b>true</b> or <b>false</b>
00161     */
00162     //bool has(const TVector3<T>& point, bool on_surface = false) const;
00163 
00164     /*  Test if two boxes intersect.
00165         @param box the box to be tested
00166         @return bool, <b>true</b> if the two boxes are intersecting, <b>false</b> otherwise
00167     */
00168     //bool isIntersecting(const TBox3& box) const;
00169   
00171 
00173 
00178     bool isValid() const;
00179 
00186     void dump(std::ostream& s = std::cout, Size depth = 0) const;
00188 
00189     protected:
00190 
00191     //_ lower left point of the box.
00192     TVector3<T> point_;
00193     TVector3<T> right_vector_;
00194     TVector3<T> height_vector_;
00195 
00196     T width_;
00197     T height_;
00198     T depth_;
00199   };
00201 
00202 
00203   template <typename T>
00204   TBox3<T>::TBox3(const TVector3<T>& point, const TVector3<T>& right_vector,const TVector3<T>& height_vector,const T& depth)
00205     : point_(point),
00206       right_vector_(right_vector),
00207       height_vector_(height_vector),
00208       width_(right_vector.getLength()),
00209       height_(height_vector.getLength()),
00210       depth_(depth)
00211   {}
00212 
00213   template <typename T>
00214   TBox3<T>::TBox3()
00215     : point_((T)0, (T)0, (T)0),
00216       right_vector_((T)0, (T)1, (T)0),
00217       height_vector_((T)-1, (T)0, (T)0),
00218       width_((T)1),
00219       height_((T)1),
00220       depth_((T)1)
00221   {
00222   }
00223 
00224   template <typename T>
00225   TBox3<T>::TBox3(const TBox3<T>& box)
00226     : point_(box.point_),
00227       right_vector_(box.right_vector_),
00228       height_vector_(box.height_vector_),
00229       width_(box.width_),
00230       height_(box.height_),
00231       depth_(box.depth_)
00232   {
00233   }
00234 
00235   template <typename T>
00236   void TBox3<T>::set(const TBox3<T>& box)
00237   {
00238     point_ = box.point_;
00239     right_vector_ = box.right_vector_;
00240     height_vector_ = box.height_vector_;
00241     width_ = box.width_;
00242     height_ = box.height_;
00243     depth_ = box.depth_;
00244   }
00245 
00246   template <typename T>
00247   BALL_INLINE
00248   const TBox3<T>& TBox3<T>::operator = (const TBox3<T> &box)
00249   {
00250     set(box);
00251     return *this;
00252   }
00253 
00254   template <typename T>
00255   void TBox3<T>::swap(TBox3<T>& box)
00256   {
00257     point_.swap(box.point_);
00258     right_vector_.swap(box.right_vector_);
00259     height_vector_.swap(box.height_vector_);
00260 
00261     std::swap(width_, box.width_);
00262     std::swap(height_, box.height_);
00263     std::swap(depth_, box.depth_);
00264   }
00265 
00266   template <typename T>
00267   void TBox3<T>::clear()
00268   {
00269     point_.clear();
00270     right_vector_ = TVector3<T>((T)0, (T)1, (T)0);
00271     height_vector_ = TVector3<T>((T)-1, (T)0, (T)0);
00272     width_ = (T) 1;
00273     height_ = (T) 1;
00274     depth_  = (T) 1;
00275   }
00276   
00277 
00278   template <typename T>
00279   BALL_INLINE
00280   void TBox3<T>::setWidth(T width) 
00281   {
00282     right_vector_.normalize();
00283     right_vector_ *= width;
00284     width_ = width;
00285   }
00286 
00287   template <typename T>
00288   BALL_INLINE
00289   void TBox3<T>::setHeight(T height)
00290   {
00291     height_vector_.normalize();
00292     height_vector_ *= height;
00293     height_ = height;
00294   }
00295 
00296   template <typename T>
00297   BALL_INLINE
00298   void TBox3<T>::setRightVector(const TVector3<T>& v)
00299   {
00300     right_vector_ = v;
00301     width_ = right_vector_.getLength();
00302   }
00303 
00304   template <typename T>
00305   BALL_INLINE
00306   void TBox3<T>::setHeightVector(const TVector3<T>& v)
00307   {
00308     height_vector_ = v;
00309     height_ = height_vector_.getLength();
00310   }
00311 
00312   template <typename T>
00313   BALL_INLINE 
00314   T TBox3<T>::getSurface() const
00315   {
00316     return ((width_  * height_ + width_  * depth_  + height_ * depth_) * 2);
00317   }
00318 
00319   template <typename T>
00320   TVector3<T> TBox3<T>::getDiagonalVector() const
00321   {
00322     TVector3<T> v = right_vector_ % height_vector_;
00323     v.normalize();
00324     v *= depth_;
00325     return (v + right_vector_ + height_vector_);
00326   }
00327 
00328   template <typename T>
00329   BALL_INLINE 
00330   T TBox3<T>::getVolume() const
00331   {
00332     return (width_ * height_ * depth_);
00333   }
00334 
00335   template <typename T>
00336   bool TBox3<T>::operator == (const TBox3<T>& box) const
00337   {
00338     return (point_ == box.point_ && right_vector_ == box.right_vector_ && height_vector_ == box.height_vector_ &&width_ == box.width_ && height_ == box.height_ && depth_ == box.depth_);
00339   }
00340 
00341   template <typename T>
00342   BALL_INLINE 
00343   bool TBox3<T>::operator != (const TBox3<T> &box) const
00344   {
00345     return !(*this == box);
00346   }
00347 
00348   template <typename T>
00349   BALL_INLINE 
00350   bool TBox3<T>::isValid() const
00351   {
00352     return (point_.isValid() && 
00353             right_vector_.isValid() &&
00354             height_vector_.isValid() &&
00355             !right_vector_.isZero() &&
00356             !height_vector_.isZero() && 
00357             right_vector_.isOrthogonalTo(height_vector_));
00358             
00359   }
00360 
00361   template <typename T>
00362   void TBox3<T>::dump(std::ostream& s, Size depth) const
00363   {
00364     BALL_DUMP_STREAM_PREFIX(s);
00365     
00366     BALL_DUMP_HEADER(s, this, this);
00367     
00368     BALL_DUMP_DEPTH(s, depth);
00369     s << "point: " << point_ << std::endl;
00370     
00371     BALL_DUMP_DEPTH(s, depth);
00372     s << "right_vector: " << right_vector_ << std::endl;
00373 
00374     BALL_DUMP_DEPTH(s, depth);
00375     s << "height_vector: " << height_vector_ << std::endl;
00376 
00377     BALL_DUMP_DEPTH(s, depth);
00378     s << "width: " << width_ << std::endl;
00379     
00380     BALL_DUMP_DEPTH(s, depth);
00381     s << "height: " << height_ << std::endl;
00382     
00383     BALL_DUMP_DEPTH(s, depth);
00384     s << "depth: " << depth_ << std::endl;
00385     
00386     BALL_DUMP_STREAM_SUFFIX(s);
00387   }
00388 
00396   template <typename T>
00397   std::istream& operator >> (std::istream& s, TBox3<T>& box)
00398   {
00399     TVector3<T> point, right, height;
00400     T depth;
00401     s >> point >> right >> height >> depth;
00402     box.setPoint(point);
00403     box.setRightVector(right);
00404     box.setHeightVector(height);
00405     box.setDepth(depth);
00406     return s;
00407   }
00408 
00416   template <typename T>
00417   std::ostream& operator << (std::ostream& s, const TBox3<T>& box)
00418   {
00419     return s << box.getPoint() << " " << box.getRightVector() << " " << box.getHeightVector() << " " << box.getDepth();
00420   }
00422 
00426   typedef TBox3<float> Box3;
00427 
00428 } // namespace BALL
00429 
00430 #endif // BALL_MATHS_BOX3_H