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