# 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.cpp

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 #include "nel/3d/aabbox.h"
00027 #include "nel/3d/polygon.h"
00028 
00029 
00030 namespace NL3D {
00031 
00032 
00033 // ***************************************************************************
00034 bool    CAABBox::clipFront(const CPlane &p) const
00035 {
00036         CVector         hswap;
00037 
00038         // The bbox is front of the plane if only one of his vertex is in front.
00039         if(p*(Center + HalfSize) > 0)   return true;
00040         if(p*(Center - HalfSize) > 0)   return true;
00041         hswap.set(-HalfSize.x, HalfSize.y, HalfSize.z);
00042         if(p*(Center + hswap) > 0)      return true;
00043         if(p*(Center - hswap) > 0)      return true;
00044         hswap.set(HalfSize.x, -HalfSize.y, HalfSize.z);
00045         if(p*(Center + hswap) > 0)      return true;
00046         if(p*(Center - hswap) > 0)      return true;
00047         hswap.set(HalfSize.x, HalfSize.y, -HalfSize.z);
00048         if(p*(Center + hswap) > 0)      return true;
00049         if(p*(Center - hswap) > 0)      return true;
00050 
00051         return false;
00052 }
00053 // ***************************************************************************
00054 bool    CAABBox::clipBack(const CPlane &p) const
00055 {
00056         CVector         hswap;
00057 
00058         // The bbox is back of the plane if only one of his vertex is in back.
00059         if(p*(Center + HalfSize) < 0)   return true;
00060         if(p*(Center - HalfSize) < 0)   return true;
00061         hswap.set(-HalfSize.x, HalfSize.y, HalfSize.z);
00062         if(p*(Center + hswap) < 0)      return true;
00063         if(p*(Center - hswap) < 0)      return true;
00064         hswap.set(HalfSize.x, -HalfSize.y, HalfSize.z);
00065         if(p*(Center + hswap) < 0)      return true;
00066         if(p*(Center - hswap) < 0)      return true;
00067         hswap.set(HalfSize.x, HalfSize.y, -HalfSize.z);
00068         if(p*(Center + hswap) < 0)      return true;
00069         if(p*(Center - hswap) < 0)      return true;
00070 
00071         return false;
00072 }
00073 
00074 
00075 // ***************************************************************************
00076 bool                    CAABBox::include(const CVector &a) const
00077 {
00078         if(Center.x+HalfSize.x<a.x)     return false;
00079         if(Center.x-HalfSize.x>a.x)     return false;
00080         if(Center.y+HalfSize.y<a.y)     return false;
00081         if(Center.y-HalfSize.y>a.y)     return false;
00082         if(Center.z+HalfSize.z<a.z)     return false;
00083         if(Center.z-HalfSize.z>a.z)     return false;
00084         return true;
00085 }
00086 
00087 
00088 // ***************************************************************************
00089 bool                    CAABBox::intersect(const CVector &a, const CVector &b, const CVector &c) const
00090 {
00091         // Trivial test.
00092         if(include(a) || include(b) || include(c))
00093                 return true;
00094         // Else, must test if the polygon intersect the pyamid.
00095         CPlane          planes[6];
00096         makePyramid(planes);
00097         CPolygon        poly(a,b,c);
00098         poly.clip(planes, 6);
00099         if(poly.getNumVertices()==0)
00100                 return false;
00101         return true;
00102 }
00103 
00104 
00105 // ***************************************************************************
00106 void                    CAABBox::makePyramid(CPlane     planes[6]) const
00107 {
00108         planes[0].make(CVector(-1,0,0), Center-HalfSize);
00109         planes[1].make(CVector(+1,0,0), Center+HalfSize);
00110         planes[2].make(CVector(0,-1,0), Center-HalfSize);
00111         planes[3].make(CVector(0,+1,0), Center+HalfSize);
00112         planes[4].make(CVector(0,0,-1), Center-HalfSize);
00113         planes[5].make(CVector(0,0,+1), Center+HalfSize);
00114 }
00115 
00116 
00117 // ***************************************************************************
00118 void                    CAABBox::serial(NLMISC::IStream &f)
00119 {
00120         uint    ver= f.serialVersion(0);
00121         f.serial(Center);
00122         f.serial(HalfSize);
00123 }
00124 
00125 
00126 // ***************************************************************************
00127 void    CAABBox::extend(const CVector &v)
00128 {
00129         CVector         bmin= getMin(), bmax= getMax();
00130 
00131         bmin.minof(bmin, v);
00132         bmax.maxof(bmax, v);
00133         setMinMax(bmin, bmax);
00134 }
00135 
00136 
00137 // ***************************************************************************
00138 bool    CAABBoxExt::clipFront(const CPlane &p) const
00139 {
00140         // Assume normalized planes.
00141 
00142         // if( SpherMax OUT )   return false.
00143         float   d= p*Center;
00144         if(d<-RadiusMax)
00145                 return false;
00146         // if( SphereMin IN )   return true;
00147         if(d>-RadiusMin)
00148                 return true;
00149 
00150         // else, standard clip box.
00151         return CAABBox::clipFront(p);
00152 }
00153 
00154 
00155 // ***************************************************************************
00156 bool    CAABBoxExt::clipBack(const CPlane &p) const
00157 {
00158         // Assume normalized planes.
00159 
00160         // if( SpherMax OUT )   return false.
00161         float   d= p*Center;
00162         if(d>RadiusMax)
00163                 return false;
00164         // if( SphereMin IN )   return true;
00165         if(d<RadiusMin)
00166                 return true;
00167 
00168         // else, standard clip box.
00169         return CAABBox::clipBack(p);
00170 }
00171 
00172 
00173 // ***************************************************************************
00174 void                    CAABBoxExt::serial(NLMISC::IStream &f)
00175 {
00176         CAABBox::serial(f);
00177         if(f.isReading())
00178                 updateRadius();
00179 }
00180 
00181 
00182 } // NL3D