KeosMath.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #include "KeosMath.h"
00023 
00024 namespace Keos
00025 {
00026   //=======================================================================
00027   // CMath implementation
00028   //=======================================================================
00029 
00030   const Real CMath::POS_INFINITY = std::numeric_limits<Real>::infinity();
00031   const Real CMath::NEG_INFINITY = -std::numeric_limits<Real>::infinity();
00032   const Real CMath::PI = Real( 4.0 * atan( 1.0 ) );
00033   const Real CMath::TWO_PI = Real( 2.0 * PI );
00034   const Real CMath::HALF_PI = Real( 0.5 * PI );
00035   const Real CMath::fDeg2Rad = PI / Real(180.0);
00036   const Real CMath::fRad2Deg = Real(180.0) / PI;
00037 
00038 
00039   //-----------------------------------------------------------------------
00040   CMath::CMath( )
00041   {}
00042 
00043   //-----------------------------------------------------------------------
00044   CMath::~CMath()
00045   {}
00046 
00047   //-----------------------------------------------------------------------
00048   uint CMath::NearestPowerOfTwo(uint nValue)
00049   {
00050     uint nTemp = nValue;
00051     uint nPowerOfTwo = 0;
00052 
00053     while (nTemp > 1)
00054     {
00055       nTemp >>= 1;
00056       ++nPowerOfTwo;
00057     }
00058 
00059     uint nRetval = 1 << nPowerOfTwo;
00060 
00061     return nRetval == nValue ? nRetval : nRetval << 1;
00062   }
00063 
00064   //-----------------------------------------------------------------------
00065   Real CMath::UnitRandom ()
00066   {
00067     return float(rand()) / float(RAND_MAX);
00068   }
00069 
00070   //-----------------------------------------------------------------------
00071   Real CMath::RangeRandom (Real fLow, Real fHigh)
00072   {
00073     return (fHigh -fLow)*UnitRandom() + fLow;
00074   }
00075 
00076   //-----------------------------------------------------------------------
00077   Real CMath::SymmetricRandom ()
00078   {
00079     return 2.0f * UnitRandom() - 1.0f;
00080   }
00081 
00082   //-----------------------------------------------------------------------
00083   bool CMath::RealEqual( Real a, Real b, Real tolerance )
00084   {
00085     if (fabs(b - a) <= tolerance)
00086       return true;
00087     else
00088       return false;
00089   }
00090 
00091   //-----------------------------------------------------------------------
00092   TVector4F CMath::calculateFaceNormal(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3)
00093   {
00094     TVector3F normal = calculateBasicFaceNormal(v1, v2, v3);
00095     // Now set up the w (distance of tri from origin
00096     return TVector4F(normal.x, normal.y, normal.z, -VectorDot(normal, v1));
00097   }
00098 
00099   //-----------------------------------------------------------------------
00100   TVector3F CMath::calculateBasicFaceNormal(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3)
00101   {
00102     TVector3F normal = VectorCross((v2 - v1), (v3 - v1));
00103     normal.Normalize();
00104     return normal;
00105   }
00106 
00107   //-----------------------------------------------------------------------
00108   TVector4F CMath::calculateFaceNormalWithoutNormalize(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3)
00109   {
00110     TVector3F normal = calculateBasicFaceNormalWithoutNormalize(v1, v2, v3);
00111     // Now set up the w (distance of tri from origin)
00112     return TVector4F(normal.x, normal.y, normal.z, -VectorDot(normal, v1));
00113   }
00114 
00115   //-----------------------------------------------------------------------
00116   TVector3F CMath::calculateBasicFaceNormalWithoutNormalize(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3)
00117   {
00118     TVector3F normal = VectorCross((v2 - v1), (v3 - v1));
00119     return normal;
00120   }
00121 
00122 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1