00001 // Magic Software, Inc. 00002 // http://www.magic-software.com 00003 // Copyright (c) 2000-2003. All Rights Reserved 00004 // 00005 // Source code from Magic Software is supplied under the terms of a license 00006 // agreement and may not be copied or disclosed except in accordance with the 00007 // terms of that agreement. The various license agreements may be found at 00008 // the Magic Software web site. This file is subject to the license 00009 // 00010 // FREE SOURCE CODE 00011 // http://www.magic-software.com/License/free.pdf 00012 00013 #ifndef MGCMATH_H 00014 #define MGCMATH_H 00015 00016 #include "MagicFMLibType.h" 00017 #include "MgcRTLib.h" 00018 #define MGC_USE_DOUBLE 00019 00020 namespace Mgc { 00021 00022 #ifdef MGC_USE_DOUBLE 00023 typedef double Real; 00024 #else 00025 typedef float Real; 00026 #endif 00027 00028 class MAGICFM Math 00029 { 00030 public: 00031 // Return -1 if the input is negative, 0 if the input is zero, and +1 00032 // if the input is positive. 00033 static int Sign (int iValue); 00034 static Real Sign (Real fValue); 00035 00036 // Just computes fValue*fValue. 00037 static Real Sqr (Real fValue); 00038 00039 // Generate a random number in [0,1). The random number generator may 00040 // be seeded by a first call to UnitRandom with a positive seed. 00041 static Real UnitRandom (Real fSeed = 0.0f); 00042 00043 // Generate a random number in [-1,1). The random number generator may 00044 // be seeded by a first call to SymmetricRandom with a positive seed. 00045 static Real SymmetricRandom (Real fSeed = 0.0f); 00046 00047 // Generate a random number in [min,max). The random number generator may 00048 // be seeded by a first call to IntervalRandom with a positive seed. 00049 static Real IntervalRandom (Real fMin, Real fMax, Real fSeed = 0.0f); 00050 00051 // Fast evaluation of sin(angle) by polynomial approximations. The angle 00052 // must be in [0,pi/2]. The maximum absolute error is about 1.7e-04 for 00053 // FastSin0 and about 2.3e-09 for FastSin1. The speed up is about 2 for 00054 // FastSin0 and about 1.5 for FastSin1. 00055 static Real FastSin0 (Real fAngle); 00056 static Real FastSin1 (Real fAngle); 00057 00058 // Fast evaluation of cos(angle) by polynomial approximations. The angle 00059 // must be in [0,pi/2]. The maximum absolute error is about 1.2e-03 for 00060 // FastCos0 and about 2.3e-09 for FastCos1. The speed up is about 2 for 00061 // FastCos0 and about 1.5 for FastCos1. 00062 static Real FastCos0 (Real fAngle); 00063 static Real FastCos1 (Real fAngle); 00064 00065 // Fast evaluation of tan(angle) by polynomial approximations. The angle 00066 // must be in [0,pi/4]. The maximum absolute error is about 8.1e-04 for 00067 // FastTan0 and about 1.9e-08 for FastTan1. The speed up is about 2.5 for 00068 // FastTan0 and about 1.75 for FastTan1. 00069 static Real FastTan0 (Real fAngle); 00070 static Real FastTan1 (Real fAngle); 00071 00072 // Fast evaluation of asin(value) by a sqrt times a polynomial. The value 00073 // must be in [0,1]. The maximum absolute error is about 6.8e-05 and the 00074 // speed up is about 2.5. 00075 static Real FastInvSin (Real fValue); 00076 00077 // Fast evaluation of acos(value) by a sqrt times a polynomial. The value 00078 // must be in [0,1]. The maximum absolute error is about 6.8e-05 and the 00079 // speed up is about 2.5. 00080 static Real FastInvCos (Real fValue); 00081 00082 // Fast evaluation of atan(value) by polynomial approximations. The value 00083 // must be in [-1,1]. The maximum absolute error is about 1.2e-05 for 00084 // FastInvTan0 and about 1.43-08 for FastInvTan1. The speed up is about 00085 // 2.2 for FastInvTan0 and about 1.5 for FastInvTan1. 00086 static Real FastInvTan0 (Real fValue); 00087 static Real FastInvTan1 (Real fValue); 00088 00089 // Wrappers for acos and asin, but the input value is clamped to [-1,1] 00090 // to avoid silent returns of NaN. 00091 static Real ACos (Real fValue); 00092 static Real ASin (Real fValue); 00093 00094 // Wrapper for 1/sqrt to allow a fast implementation to replace it at a 00095 // later date. 00096 static Real InvSqrt (Real fValue); 00097 00098 // Wrappers to hide 'double foo(double)' versus 'float foof(float)'. 00099 static Real ATan (Real fValue); 00100 static Real ATan2 (Real fY, Real fX); 00101 static Real Ceil (Real fValue); 00102 static Real Cos (Real fValue); 00103 static Real Exp (Real fValue); 00104 static Real FAbs (Real fValue); 00105 static Real Floor (Real fValue); 00106 static Real Log (Real fValue); 00107 static Real Pow (Real kBase, Real kExponent); 00108 static Real Sin (Real fValue); 00109 static Real Sqrt (Real fValue); 00110 static Real Tan (Real fValue); 00111 00112 // common constants 00113 static const Real MAX_REAL; 00114 static const Real PI; 00115 static const Real TWO_PI; 00116 static const Real HALF_PI; 00117 static const Real INV_TWO_PI; 00118 static const Real DEG_TO_RAD; 00119 static const Real RAD_TO_DEG; 00120 }; 00121 00122 #include "MgcMath.inl" 00123 00124 } // namespace Mgc 00125 00126 #endif 00127 00128 00129