# 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  

bsphere.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 "stdmisc.h"
00027 
00028 #include "nel/misc/bsphere.h"
00029 
00030 using namespace NLMISC;
00031 using namespace std;
00032 
00033 
00034 namespace NLMISC {
00035 
00036 
00037 bool    CBSphere::clipFront(const CPlane &p) const
00038 {
00039         // assume normalized planes.
00040 
00041         // if( SpherMax OUT )   return false.
00042         float   d= p*Center;
00043         if(d<-Radius)
00044                 return false;
00045 
00046         return true;
00047 }
00048 
00049 
00050 bool    CBSphere::clipBack(const CPlane &p) const
00051 {
00052         // assume normalized planes.
00053 
00054         // if( SpherMax OUT )   return false.
00055         float   d= p*Center;
00056         if(d>Radius)
00057                 return false;
00058 
00059         return true;
00060 }
00061 
00062 
00063 bool    CBSphere::include(const CVector &p) const
00064 {
00065         float   r2= (p-Center).sqrnorm();
00066         return (r2<=sqr(Radius));
00067 }
00068 
00069 bool    CBSphere::include(const CBSphere &s) const
00070 {
00071         // if smaller than s, how could we include it???
00072         if(Radius<=s.Radius)
00073                 return false;
00074         float   r2= (s.Center-Center).sqrnorm();
00075         // Because of prec test, Radius-s.Radius>0.
00076         return  r2<=sqr(Radius-s.Radius);
00077 }
00078 
00079 bool    CBSphere::intersect(const CBSphere &s) const
00080 {
00081         float   r2= (s.Center-Center).sqrnorm();
00082 
00083         return r2<=sqr(Radius+s.Radius);
00084 
00085 }
00086 
00087 
00088 void    CBSphere::applyTransform(const CMatrix &mat, CBSphere &res)
00089 {
00090         res.Center= mat*Center;
00091 
00092         if(!mat.hasScalePart())
00093                 res.Radius= Radius;
00094         else
00095         {
00096                 if(mat.hasScaleUniform())
00097                         res.Radius= Radius*mat.getScaleUniform();
00098                 else
00099                 {
00100                         // must compute max of 3 axis.
00101                         float   m, mx;
00102                         CVector i,j,k;
00103                         i= mat.getI();
00104                         j= mat.getJ();
00105                         k= mat.getK();
00106                         // take the max of the 3 axis.
00107                         m= i.sqrnorm();
00108                         mx= m;
00109                         m= j.sqrnorm();
00110                         mx= max(m, mx);
00111                         m= k.sqrnorm();
00112                         mx= max(m, mx);
00113 
00114                         // result.
00115                         res.Radius= Radius * (float)sqrt(mx);
00116                 }
00117         }
00118 }
00119 
00120 
00121 } // NLMISC