aabbox.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2000 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_AABBOX_H
00025 #define NL_AABBOX_H
00026 
00027 #include "types_nl.h"
00028 #include "vector.h"
00029 #include "plane.h"
00030 #include "common.h"
00031 #include "stream.h"
00032 #include "bsphere.h"
00033 
00034 
00035 namespace NLMISC
00036 {
00037 
00038 class CMatrix;
00039 
00040 // ***************************************************************************
00048 class CAABBox
00049 {
00050 protected:
00052     CVector         Center;
00054     CVector         HalfSize;
00055 
00056 public:
00057 
00059     CAABBox() : Center(0,0,0), HalfSize(0,0,0) {}
00060     /* ***********************************************
00061      *  WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
00062      *  It can be loaded/called through CAsyncFileManager for instance
00063      * ***********************************************/
00064 
00065 
00067     // @{
00068     void            setCenter(const CVector &center) {Center= center;}
00069     void            setHalfSize(const CVector &hs) {HalfSize= hs;}
00071     void            setSize(const CVector &s) {HalfSize= s/2;}
00073     void            setMinMax(const CVector &bmin, const CVector &bmax)
00074     {
00075         Center= (bmin+bmax)/2;
00076         HalfSize= bmax-Center;
00077     }
00082     void            extend(const CVector &v);
00084 
00085 
00087     // @{
00088     CVector         getMin() const {return Center-HalfSize;}
00089     CVector         getMax() const {return Center+HalfSize;}
00090     void            getMin(CVector &ret) const {ret= Center-HalfSize;}
00091     void            getMax(CVector &ret) const {ret= Center+HalfSize;}
00092     const CVector   &getCenter() const {return Center;}
00093     const CVector   &getHalfSize() const {return HalfSize;}
00095     CVector         getSize() const {return HalfSize*2;}
00096     void            getSize(CVector &ret) const {ret= HalfSize*2;}
00098     float           getRadius() const {return HalfSize.norm();}
00099     // @}
00100 
00102     // @{
00104     bool            clipFront(const CPlane &p) const;
00106     bool            clipBack(const CPlane &p) const;
00108     bool            include(const CVector &a) const;
00110     bool            include(const CAABBox &box) const;
00112     bool            intersect(const CAABBox &box) const;
00114     bool            intersect(const CVector &a, const CVector &b, const CVector &c) const;
00116     bool            intersect(const CBSphere &s) const;
00118     bool            intersect(const CVector &a, const CVector &b) const;
00120     bool            clipSegment(CVector &a, CVector &b) const;
00121     // @}
00122 
00124     // @{
00126     void            makePyramid(CPlane  planes[6]) const;
00127 
00133     static CAABBox  computeAABBoxUnion(const CAABBox &b1, const CAABBox &b2);
00134 
00140     void            computeIntersection(const CAABBox &b1, const CAABBox &b2);
00141 
00142 
00146     static CAABBox transformAABBox(const CMatrix &mat, const CAABBox &box);
00147 
00148     // @}
00149 
00150     void            serial(NLMISC::IStream &f);
00151 };
00152 
00153 
00154 // ***************************************************************************
00161 class   CAABBoxExt : private CAABBox
00162 {
00163 protected:
00164     float           RadiusMin, RadiusMax;
00165 
00166     void            updateRadius()
00167     {
00168         // The bounding sphere.
00169         RadiusMax= CAABBox::getRadius();
00170         // The including sphere.
00171         RadiusMin= NLMISC::minof((float)fabs(HalfSize.x), (float)fabs(HalfSize.y), (float)fabs(HalfSize.z));
00172     }
00173 
00174 public:
00176     CAABBoxExt() {RadiusMin= RadiusMax=0;}
00178     CAABBoxExt(const CAABBox &o) {RadiusMin= RadiusMax=0; *this=o;}
00179     /* ***********************************************
00180      *  WARNING: This Class/Method must be thread-safe (ctor/dtor/serial): no static access for instance
00181      *  It can be loaded/called through CAsyncFileManager for instance
00182      * ***********************************************/
00183 
00184 
00186     // @{
00187     void            setCenter(const CVector &center) {Center= center;}
00188     void            setHalfSize(const CVector &hs) {HalfSize= hs; updateRadius();}
00189     void            setSize(const CVector &s) {HalfSize= s/2;  updateRadius();}
00191     void            setMinMax(const CVector &bmin, const CVector &bmax)
00192     {
00193         Center= (bmin+bmax)/2;
00194         HalfSize= bmax-Center;
00195         updateRadius();
00196     }
00197     CAABBoxExt      &operator=(const CAABBox &o) {Center= o.getCenter(); HalfSize= o.getHalfSize(); updateRadius(); return (*this);}
00199 
00200 
00202     // @{
00203     CVector         getMin() const {return CAABBox::getMin();}
00204     CVector         getMax() const {return CAABBox::getMax();}
00205     const CVector   &getCenter() const {return Center;}
00206     const CVector   &getHalfSize() const {return HalfSize;}
00208     CVector         getSize() const {return HalfSize*2;}
00210     float           getRadius() const {return RadiusMax;}
00212     CAABBox         getAABBox() const { CAABBox box; box.setCenter(getCenter()); box.setHalfSize(getHalfSize()); return box; }
00213     // @}
00214 
00216     // @{
00218     bool            clipFront(const CPlane &p) const;
00220     bool            clipBack(const CPlane &p) const;
00222     bool            intersect(const CAABBoxExt &box) const
00223         {return CAABBox::intersect(box);}
00225     bool            intersect(const CVector &a, const CVector &b, const CVector &c) const
00226         {return CAABBox::intersect(a,b,c);}
00227     // @}
00228 
00229     void            serial(NLMISC::IStream &f);
00230 };
00231 
00232 
00233 } // NLMISC
00234 
00235 
00236 #endif // NL_AABBOX_H
00237 
00238 /* End of aabbox.h */

Generated on Thu Jan 7 08:26:22 2010 for NeL by  doxygen 1.6.1