00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KeosMath.h"
00023
00024 namespace Keos
00025 {
00026
00027
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
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
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 }