KeosMath.h

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 #ifndef KEOS_MATH_H
00023 #define KEOS_MATH_H
00024 
00025 #include "KeosPrerequisites.h"
00026 
00027 namespace Keos
00028 {
00029   class CDegree;
00030 
00031   //-----------------------------------------------------------------------
00037   class CRadian
00038   {
00039   protected:
00040 
00042     Real m_Rad;
00043 
00044   public:
00045     CRadian ( Real r = 0 ) : m_Rad(r)
00046     {}
00047     CRadian ( const CDegree& d );
00048     const CRadian& operator = ( const Real& f )
00049     {
00050       m_Rad = f;
00051       return *this;
00052     }
00053     const CRadian& operator = ( const CRadian& r )
00054     {
00055       m_Rad = r.m_Rad;
00056       return *this;
00057     }
00058     const CRadian& operator = ( const CDegree& d );
00059 
00060     Real valueDegrees() const;
00061     Real valueRadians() const
00062     {
00063       return m_Rad;
00064     }
00065 
00066     CRadian operator + ( const CRadian& r ) const
00067     {
00068       return CRadian ( m_Rad + r.m_Rad );
00069     }
00070     CRadian operator + ( const CDegree& d ) const;
00071     CRadian& operator += ( const CRadian& r )
00072     {
00073       m_Rad += r.m_Rad;
00074       return *this;
00075     }
00076     CRadian& operator += ( const CDegree& d );
00077     CRadian operator - ()
00078     {
00079       return CRadian(-m_Rad);
00080     }
00081     CRadian operator - ( const CRadian& r ) const
00082     {
00083       return CRadian ( m_Rad - r.m_Rad );
00084     }
00085     CRadian operator - ( const CDegree& d ) const;
00086     CRadian& operator -= ( const CRadian& r )
00087     {
00088       m_Rad -= r.m_Rad;
00089       return *this;
00090     }
00091     CRadian& operator -= ( const CDegree& d );
00092     CRadian operator * ( Real f ) const
00093     {
00094       return CRadian ( m_Rad * f );
00095     }
00096     CRadian operator * ( const CRadian& f ) const
00097     {
00098       return CRadian ( m_Rad * f.m_Rad );
00099     }
00100     CRadian& operator *= ( Real f )
00101     {
00102       m_Rad *= f;
00103       return *this;
00104     }
00105     CRadian operator / ( Real f ) const
00106     {
00107       return CRadian ( m_Rad / f );
00108     }
00109     CRadian& operator /= ( Real f )
00110     {
00111       m_Rad /= f;
00112       return *this;
00113     }
00114 
00115     bool operator <  ( const CRadian& r ) const
00116     {
00117       return m_Rad <  r.m_Rad;
00118     }
00119     bool operator <= ( const CRadian& r ) const
00120     {
00121       return m_Rad <= r.m_Rad;
00122     }
00123     bool operator == ( const CRadian& r ) const
00124     {
00125       return m_Rad == r.m_Rad;
00126     }
00127     bool operator != ( const CRadian& r ) const
00128     {
00129       return m_Rad != r.m_Rad;
00130     }
00131     bool operator >= ( const CRadian& r ) const
00132     {
00133       return m_Rad >= r.m_Rad;
00134     }
00135     bool operator >  ( const CRadian& r ) const
00136     {
00137       return m_Rad >  r.m_Rad;
00138     }
00139   };
00140 
00141   //-----------------------------------------------------------------------
00147   class CDegree
00148   {
00149   protected:
00151     Real m_Deg;
00152 
00153   public:
00154     explicit CDegree ( Real d = 0 ) : m_Deg(d)
00155     {}
00156     CDegree ( const CRadian& r ) : m_Deg(r.valueDegrees())
00157     {}
00158     const CDegree& operator = ( const Real& f )
00159     {
00160       m_Deg = f;
00161       return *this;
00162     }
00163     const CDegree& operator = ( const CDegree& d )
00164     {
00165       m_Deg = d.m_Deg;
00166       return *this;
00167     }
00168     const CDegree& operator = ( const CRadian& r )
00169     {
00170       m_Deg = r.valueDegrees();
00171       return *this;
00172     }
00173 
00174     Real valueDegrees() const
00175     {
00176       return m_Deg;
00177     }
00178     Real valueRadians() const;
00179 
00180     CDegree operator + ( const CDegree& d ) const
00181     {
00182       return CDegree ( m_Deg + d.m_Deg );
00183     }
00184     CDegree operator + ( const CRadian& r ) const
00185     {
00186       return CDegree ( m_Deg + r.valueDegrees() );
00187     }
00188     CDegree& operator += ( const CDegree& d )
00189     {
00190       m_Deg += d.m_Deg;
00191       return *this;
00192     }
00193     CDegree& operator += ( const CRadian& r )
00194     {
00195       m_Deg += r.valueDegrees();
00196       return *this;
00197     }
00198     CDegree operator - ()
00199     {
00200       return CDegree(-m_Deg);
00201     }
00202     CDegree operator - ( const CDegree& d ) const
00203     {
00204       return CDegree ( m_Deg - d.m_Deg );
00205     }
00206     CDegree operator - ( const CRadian& r ) const
00207     {
00208       return CDegree ( m_Deg - r.valueDegrees() );
00209     }
00210     CDegree& operator -= ( const CDegree& d )
00211     {
00212       m_Deg -= d.m_Deg;
00213       return *this;
00214     }
00215     CDegree& operator -= ( const CRadian& r )
00216     {
00217       m_Deg -= r.valueDegrees();
00218       return *this;
00219     }
00220     CDegree operator * ( Real f ) const
00221     {
00222       return CDegree ( m_Deg * f );
00223     }
00224     CDegree operator * ( const CDegree& f ) const
00225     {
00226       return CDegree ( m_Deg * f.m_Deg );
00227     }
00228     CDegree& operator *= ( Real f )
00229     {
00230       m_Deg *= f;
00231       return *this;
00232     }
00233     CDegree operator / ( Real f ) const
00234     {
00235       return CDegree ( m_Deg / f );
00236     }
00237     CDegree& operator /= ( Real f )
00238     {
00239       m_Deg /= f;
00240       return *this;
00241     }
00242 
00243     bool operator <  ( const CDegree& d ) const
00244     {
00245       return m_Deg <  d.m_Deg;
00246     }
00247     bool operator <= ( const CDegree& d ) const
00248     {
00249       return m_Deg <= d.m_Deg;
00250     }
00251     bool operator == ( const CDegree& d ) const
00252     {
00253       return m_Deg == d.m_Deg;
00254     }
00255     bool operator != ( const CDegree& d ) const
00256     {
00257       return m_Deg != d.m_Deg;
00258     }
00259     bool operator >= ( const CDegree& d ) const
00260     {
00261       return m_Deg >= d.m_Deg;
00262     }
00263     bool operator >  ( const CDegree& d ) const
00264     {
00265       return m_Deg >  d.m_Deg;
00266     }
00267   };
00268 
00269 
00270   //-----------------------------------------------------------------------
00274   class KEOS_EXPORT CMath
00275   {
00276   public:
00277 
00280     CMath();
00281 
00284     ~CMath();
00285 
00290     static uint NearestPowerOfTwo(uint nValue);
00291 
00294     static Real UnitRandom ();
00295 
00298     static Real RangeRandom (Real fLow, Real fHigh);
00299 
00302     static Real SymmetricRandom ();
00303 
00306     static inline Real DegreesToRadians(Real degrees)
00307     {
00308       return degrees * fDeg2Rad;
00309     }
00310 
00313     static inline Real RadiansToDegrees(Real radians)
00314     {
00315       return radians * fRad2Deg;
00316     }
00317 
00320     static bool RealEqual(Real a, Real b,
00321                           Real tolerance = std::numeric_limits<Real>::epsilon());
00322 
00325     static TVector4F calculateFaceNormal(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3);
00326 
00329     static TVector3F calculateBasicFaceNormal(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3);
00330 
00333     static TVector4F calculateFaceNormalWithoutNormalize(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3);
00334 
00337     static TVector3F calculateBasicFaceNormalWithoutNormalize(const TVector3F& v1, const TVector3F& v2, const TVector3F& v3);
00338 
00339     static const Real POS_INFINITY;
00340     static const Real NEG_INFINITY;
00341     static const Real PI;
00342     static const Real TWO_PI;
00343     static const Real HALF_PI;
00344     static const Real fDeg2Rad;
00345     static const Real fRad2Deg;
00346   };
00347 
00348 
00349   //=======================================================================
00350   // Inline functions
00351   //=======================================================================
00352 
00353   //-----------------------------------------------------------------------
00354   inline CRadian::CRadian ( const CDegree& d ) : m_Rad(d.valueRadians())
00355   {}
00356 
00357   //-----------------------------------------------------------------------
00358   inline const CRadian& CRadian::operator = ( const CDegree& d )
00359   {
00360     m_Rad = d.valueRadians();
00361     return *this;
00362   }
00363 
00364   //-----------------------------------------------------------------------
00365   inline CRadian CRadian::operator + ( const CDegree& d ) const
00366   {
00367     return CRadian ( m_Rad + d.valueRadians() );
00368   }
00369 
00370   //-----------------------------------------------------------------------
00371   inline CRadian& CRadian::operator += ( const CDegree& d )
00372   {
00373     m_Rad += d.valueRadians();
00374     return *this;
00375   }
00376 
00377   //-----------------------------------------------------------------------
00378   inline CRadian CRadian::operator - ( const CDegree& d ) const
00379   {
00380     return CRadian ( m_Rad - d.valueRadians() );
00381   }
00382 
00383   //-----------------------------------------------------------------------
00384   inline CRadian& CRadian::operator -= ( const CDegree& d )
00385   {
00386     m_Rad -= d.valueRadians();
00387     return *this;
00388   }
00389 
00390   //-----------------------------------------------------------------------
00391   inline Real CRadian::valueDegrees() const
00392   {
00393     return CMath::RadiansToDegrees ( m_Rad );
00394   }
00395 
00396   //-----------------------------------------------------------------------
00397   inline Real CDegree::valueRadians() const
00398   {
00399     return CMath::DegreesToRadians ( m_Deg );
00400   }
00401 
00402   //-----------------------------------------------------------------------
00403   inline CRadian operator * ( Real a, const CRadian& b )
00404   {
00405     return CRadian ( a * b.valueRadians() );
00406   }
00407 
00408   //-----------------------------------------------------------------------
00409   inline CRadian operator / ( Real a, const CRadian& b )
00410   {
00411     return CRadian ( a / b.valueRadians() );
00412   }
00413 
00414   //-----------------------------------------------------------------------
00415   inline CDegree operator * ( Real a, const CDegree& b )
00416   {
00417     return CDegree ( a * b.valueDegrees() );
00418   }
00419 
00420   //-----------------------------------------------------------------------
00421   inline CDegree operator / ( Real a, const CDegree& b )
00422   {
00423     return CDegree ( a / b.valueDegrees() );
00424   }
00425 
00426 } // namespace Keos
00427 
00428 #endif // KEOS_MATH_H

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