00001 // Wild Magic Source Code 00002 // David Eberly 00003 // http://www.geometrictools.com 00004 // Copyright (c) 1998-2008 00005 // 00006 // This library is free software; you can redistribute it and/or modify it 00007 // under the terms of the GNU Lesser General Public License as published by 00008 // the Free Software Foundation; either version 2.1 of the License, or (at 00009 // your option) any later version. The license is available for reading at 00010 // either of the locations: 00011 // http://www.gnu.org/copyleft/lgpl.html 00012 // http://www.geometrictools.com/License/WildMagicLicense.pdf 00013 // 00014 // Version: 4.0.3 (2007/05/13) 00015 00016 #ifndef WM4MATH_H 00017 #define WM4MATH_H 00018 00019 #include "Wm4FoundationLIB.h" 00020 #include "Wm4System.h" 00021 00022 namespace Wm4 00023 { 00024 00025 template <class Real> 00026 class Math 00027 { 00028 public: 00029 // Wrappers to hide implementations of functions. The ACos and ASin 00030 // functions clamp the input argument to [-1,1] to avoid NaN issues 00031 // when the input is slightly larger than 1 or slightly smaller than -1. 00032 // Other functions have the potential for using a fast and approximate 00033 // algorithm rather than calling the standard math library functions. 00034 static Real ACos (Real fValue); 00035 static Real ASin (Real fValue); 00036 static Real ATan (Real fValue); 00037 static Real ATan2 (Real fY, Real fX); 00038 static Real Ceil (Real fValue); 00039 static Real Cos (Real fValue); 00040 static Real Exp (Real fValue); 00041 static Real FAbs (Real fValue); 00042 static Real Floor (Real fValue); 00043 static Real FMod (Real fX, Real fY); 00044 static Real InvSqrt (Real fValue); 00045 static Real Log (Real fValue); 00046 static Real Log2 (Real fValue); 00047 static Real Log10 (Real fValue); 00048 static Real Pow (Real fBase, Real fExponent); 00049 static Real Sin (Real fValue); 00050 static Real Sqr (Real fValue); 00051 static Real Sqrt (Real fValue); 00052 static Real Tan (Real fValue); 00053 00054 // Return -1 if the input is negative, 0 if the input is zero, and +1 00055 // if the input is positive. 00056 static int Sign (int iValue); 00057 static Real Sign (Real fValue); 00058 00059 // Generate a random number in [0,1). The random number generator may 00060 // be seeded by a first call to UnitRandom with a positive seed. 00061 static Real UnitRandom (unsigned int uiSeed = 0); 00062 00063 // Generate a random number in [-1,1). The random number generator may 00064 // be seeded by a first call to SymmetricRandom with a positive seed. 00065 static Real SymmetricRandom (unsigned int uiSeed = 0); 00066 00067 // Generate a random number in [min,max). The random number generator may 00068 // be seeded by a first call to IntervalRandom with a positive seed. 00069 static Real IntervalRandom (Real fMin, Real fMax, 00070 unsigned int uiSeed = 0); 00071 00072 // Fast evaluation of trigonometric and inverse trigonometric functions 00073 // using polynomial approximations. The speed ups were measured on an 00074 // AMD 2800 (2.08 GHz) processor using Visual Studion .NET 2003 with a 00075 // release build. 00076 00077 // The input must be in [0,pi/2]. 00078 // max error sin0 = 1.7e-04, speed up = 4.0 00079 // max error sin1 = 1.9e-08, speed up = 2.8 00080 static Real FastSin0 (Real fAngle); 00081 static Real FastSin1 (Real fAngle); 00082 00083 // The input must be in [0,pi/2] 00084 // max error cos0 = 1.2e-03, speed up = 4.5 00085 // max error cos1 = 6.5e-09, speed up = 2.8 00086 static Real FastCos0 (Real fAngle); 00087 static Real FastCos1 (Real fAngle); 00088 00089 // The input must be in [0,pi/4]. 00090 // max error tan0 = 8.1e-04, speed up = 5.6 00091 // max error tan1 = 1.9e-08, speed up = 3.4 00092 static Real FastTan0 (Real fAngle); 00093 static Real FastTan1 (Real fAngle); 00094 00095 // The input must be in [0,1]. 00096 // max error invsin0 = 6.8e-05, speed up = 7.5 00097 // max error invsin1 = 1.4e-07, speed up = 5.5 00098 static Real FastInvSin0 (Real fValue); 00099 static Real FastInvSin1 (Real fValue); 00100 00101 // The input must be in [0,1]. 00102 // max error invcos0 = 6.8e-05, speed up = 7.5 00103 // max error invcos1 = 1.4e-07, speed up = 5.7 00104 static Real FastInvCos0 (Real fValue); 00105 static Real FastInvCos1 (Real fValue); 00106 00107 // The input must be in [-1,1]. 00108 // max error invtan0 = 1.2e-05, speed up = 2.8 00109 // max error invtan1 = 2.3e-08, speed up = 1.8 00110 static Real FastInvTan0 (Real fValue); 00111 static Real FastInvTan1 (Real fValue); 00112 00113 // A fast approximation to 1/sqrt. 00114 static Real FastInvSqrt (Real fValue); 00115 00116 // Fast approximations to exp(-x). The input x must be in [0,infinity). 00117 // max error negexp0 = 0.00024, speed up = 25.4 00118 // max error negexp1 = 0.000024, speed up = 25.4 00119 // max error negexp2 = 0.0000024, speed up = 20.5 00120 // max error negexp3 = 0.00000025, speed up = 17.3 00121 static Real FastNegExp0 (Real fValue); 00122 static Real FastNegExp1 (Real fValue); 00123 static Real FastNegExp2 (Real fValue); 00124 static Real FastNegExp3 (Real fValue); 00125 00126 // common constants 00127 WM4_FOUNDATION_ITEM static const Real EPSILON; 00128 WM4_FOUNDATION_ITEM static const Real ZERO_TOLERANCE; 00129 WM4_FOUNDATION_ITEM static const Real MAX_REAL; 00130 WM4_FOUNDATION_ITEM static const Real PI; 00131 WM4_FOUNDATION_ITEM static const Real TWO_PI; 00132 WM4_FOUNDATION_ITEM static const Real HALF_PI; 00133 WM4_FOUNDATION_ITEM static const Real INV_PI; 00134 WM4_FOUNDATION_ITEM static const Real INV_TWO_PI; 00135 WM4_FOUNDATION_ITEM static const Real DEG_TO_RAD; 00136 WM4_FOUNDATION_ITEM static const Real RAD_TO_DEG; 00137 WM4_FOUNDATION_ITEM static const Real LN_2; 00138 WM4_FOUNDATION_ITEM static const Real LN_10; 00139 WM4_FOUNDATION_ITEM static const Real INV_LN_2; 00140 WM4_FOUNDATION_ITEM static const Real INV_LN_10; 00141 }; 00142 00143 #include "Wm4Math.inl" 00144 #include "Wm4MathMCR.h" 00145 00146 typedef Math<float> Mathf; 00147 typedef Math<double> Mathd; 00148 00149 #ifndef WM4_USING_VC70 00150 template<> float Math<float>::FastInvSqrt (float fValue); 00151 template<> double Math<double>::FastInvSqrt (double dValue); 00152 #endif 00153 00154 } 00155 00156 #endif