# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

aabbox.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_AABBOX_H
00027 #define NL_AABBOX_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/vector.h"
00031 #include "nel/misc/plane.h"
00032 #include "nel/misc/common.h"
00033 #include "nel/misc/stream.h"
00034 
00035 
00036 namespace NLMISC
00037 {
00038 
00039 class CMatrix;
00040 class CBSphere;
00041 
00042 using   NLMISC::CVector;
00043 using   NLMISC::CPlane;
00044 
00045 
00046 // ***************************************************************************
00054 class CAABBox
00055 {
00056 protected:
00058         CVector                 Center;
00060         CVector                 HalfSize;
00061 
00062 public:
00063 
00065         CAABBox() : Center(0,0,0), HalfSize(0,0,0) {}
00066 
00067 
00069         // @{
00070         void                    setCenter(const CVector &center) {Center= center;}
00071         void                    setHalfSize(const CVector &hs) {HalfSize= hs;}
00073         void                    setSize(const CVector &s) {HalfSize= s/2;}
00075         void                    setMinMax(const CVector &bmin, const CVector &bmax)
00076         {
00077                 Center= (bmin+bmax)/2;
00078                 HalfSize= bmax-Center;
00079         }
00084         void                    extend(const CVector &v);
00086 
00087 
00089         // @{
00090         CVector                 getMin() const {return Center-HalfSize;}
00091         CVector                 getMax() const {return Center+HalfSize;}
00092         void                    getMin(CVector &ret) const {ret= Center-HalfSize;}
00093         void                    getMax(CVector &ret) const {ret= Center+HalfSize;}
00094         const CVector   &getCenter() const {return Center;}
00095         const CVector   &getHalfSize() const {return HalfSize;}
00097         CVector                 getSize() const {return HalfSize*2;}
00098         void                    getSize(CVector &ret) const {ret= HalfSize*2;}
00100         float                   getRadius() const {return HalfSize.norm();}
00101         // @}
00102 
00104         // @{
00106         bool                    clipFront(const CPlane &p) const;
00108         bool                    clipBack(const CPlane &p) const;
00110         bool                    include(const CVector &a) const;
00112         bool                    include(const CAABBox &box) const;
00114         bool                    intersect(const CAABBox &box) const;
00116         bool                    intersect(const CVector &a, const CVector &b, const CVector &c) const;
00118         bool                    intersect(const CBSphere &s) const;
00119         // @}
00120 
00122         // @{
00124         void                    makePyramid(CPlane      planes[6]) const;
00125 
00131         static CAABBox  computeAABBoxUnion(const CAABBox &b1, const CAABBox &b2);
00132 
00138         void                    computeIntersection(const CAABBox &b1, const CAABBox &b2);
00139 
00140 
00144         static CAABBox transformAABBox(const CMatrix &mat, const CAABBox &box);
00145 
00146         // @}
00147 
00148         void                    serial(NLMISC::IStream &f);
00149 };
00150 
00151 
00152 // ***************************************************************************
00159 class   CAABBoxExt : private CAABBox
00160 {
00161 protected:
00162         float                   RadiusMin, RadiusMax;
00163 
00164         void                    updateRadius()
00165         {
00166                 // The bounding sphere.
00167                 RadiusMax= CAABBox::getRadius();
00168                 // The including sphere.
00169                 RadiusMin= NLMISC::minof((float)fabs(HalfSize.x), (float)fabs(HalfSize.y), (float)fabs(HalfSize.z));
00170         }
00171 
00172 public:
00174         CAABBoxExt() {RadiusMin= RadiusMax=0;}
00176         CAABBoxExt(const CAABBox &o) {RadiusMin= RadiusMax=0; *this=o;}
00177 
00178 
00180         // @{
00181         void                    setCenter(const CVector &center) {Center= center;}
00182         void                    setHalfSize(const CVector &hs) {HalfSize= hs; updateRadius();}
00183         void                    setSize(const CVector &s) {HalfSize= s/2;  updateRadius();}
00185         void                    setMinMax(const CVector &bmin, const CVector &bmax)
00186         {
00187                 Center= (bmin+bmax)/2;
00188                 HalfSize= bmax-Center;
00189                 updateRadius();
00190         }
00191         CAABBoxExt              &operator=(const CAABBox &o) {Center= o.getCenter(); HalfSize= o.getHalfSize(); updateRadius(); return (*this);}
00193 
00194 
00196         // @{
00197         CVector                 getMin() const {return CAABBox::getMin();}
00198         CVector                 getMax() const {return CAABBox::getMax();}
00199         const CVector   &getCenter() const {return Center;}
00200         const CVector   &getHalfSize() const {return HalfSize;}
00202         CVector                 getSize() const {return HalfSize*2;}
00204         float                   getRadius() const {return RadiusMax;}
00206         CAABBox                 getAABBox() const { CAABBox box; box.setCenter(getCenter()); box.setHalfSize(getHalfSize()); return box; }
00207         // @}
00208 
00210         // @{
00212         bool                    clipFront(const CPlane &p) const;
00214         bool                    clipBack(const CPlane &p) const;
00216         bool                    intersect(const CAABBoxExt &box) const
00217                 {return CAABBox::intersect(box);}
00219         bool                    intersect(const CVector &a, const CVector &b, const CVector &c) const
00220                 {return CAABBox::intersect(a,b,c);}
00221         // @}
00222 
00223         void                    serial(NLMISC::IStream &f);
00224 };
00225 
00226 
00227 } // NLMISC
00228 
00229 
00230 #endif // NL_AABBOX_H
00231 
00232 /* End of aabbox.h */