00001 #ifndef __MATH_H__
00002 #define __MATH_H__
00003
00004 #include <stdlib.h>
00005 #include <limits>
00006 #include <math.h>
00007 #include "Vector3.h"
00008 #include "Matrix3.h"
00009
00010 class Ray;
00011
00012 class Math
00013 {
00014 public:
00015 static const float MAX_FLOAT;
00016 static const float EPSILON_FLOAT;
00017 static const float PI;
00018 static const float HALF_PI;
00019 static const float DOUBLE_PI;
00020 static const float DEG_TO_RAD;
00021 static const float RAD_TO_DEG;
00022 static const float EULER;
00023 static const float SQUARE_ROOT_TWO;
00024
00025 static const float BAUCHSCHE_FAKE_CONST;
00026
00028 static int NextPowerOf2Value(int val);
00030 static bool IsPowerOf2(int val);
00031
00033 static float InterpolateAngle(float angle1,float angle2, float t);
00034
00036 static Vector3 InterpolatePosition(const Vector3& a, const Vector3& b, const Vector3& tXYZ);
00037
00039 static float InterpolateCoord(float a, float b, float t);
00040
00042 static float GetAngleY(const Vector3& direction);
00043
00045 static float GetAngleDifference(float angle1,float angle2);
00046
00048 static Vector3 GetDirectionFromAngleY(float angle);
00049
00051 template<class T>
00052 static T InterpolateCatmullRom(float t, const T& p1, const T& p2, const T& p3, const T& p4);
00053
00055 template<class T>
00056 static T InterpolateCatmullRomDerivation(float t, const T& p1, const T& p2, const T& p3, const T& p4);
00057
00059 template<class T>
00060 static T InterpolateCatmullRom(float t, const T& p1, const T& p2, const T& p3, const T& p4, T& derivation);
00061
00063 template<class T>
00064 static T InterpolateCosine(T start, T end, float ratio);
00065
00067 template<class T>
00068 static T Lerp(T start, T end, float ratio);
00069
00071 template<class T>
00072 static T InterpolateCubic(const T preStart, const T start, const T end, const T postEnd, const float ratio);
00073
00077 template<class T>
00078 static T InterpolateHermite(const T preStart, const T start, const T end, const T postEnd, const float ratio, const float tension = 0, const float bias = 0);
00079
00081 static void GetBarycentric(const Vector3& p, const Vector3& a, const Vector3& b, const Vector3& c, float& b0, float& b1, float& b2);
00082
00083
00084 static float Trunc(float val);
00086 static float Frac(float val);
00087
00089 static float Clamp(float val, float min, float max);
00090
00092 static int Clamp(int val, int min, int max);
00093
00095 static float Wrap(float val, float lower, float upper);
00096
00098 static bool IsSmaller(float x, float y) { return x < (y-EPSILON_FLOAT); }
00100 static bool IsBigger(float x, float y) { return x > (y+EPSILON_FLOAT); }
00101
00103 static bool IsEqual(float x, float y) { return !IsSmaller(x,y) && !IsBigger(x,y); }
00105 static bool IsNotEqual(float x, float y) { return IsSmaller(x,y) || IsBigger(x,y); }
00106
00108 static float Sin(float degree) { return sinf(degree); }
00110 static float ASin(float degree) { return asinf(degree); }
00112 static float Cos(float degree) { return cosf(degree); }
00114 static float ACos(float degree) { return acosf(degree); }
00116 static float Tan(float degree) { return tanf(degree); }
00118 static float ATan(float degree) { return atanf(degree); }
00120 static float ATan2(float x, float y) { return atan2f(x, y); }
00122 static float Cot(float degree) { return 1.0f/Tan(degree); }
00124 static float Sqrt(float value) { return sqrtf(value); }
00126 static float Log(float value);
00127
00129 static float InvSqrt(float value);
00131 static float Abs(float value) { return abs(value); }
00133 static int Abs(int value) { return abs(value); }
00135 static float Pow(float base, float exponent) { return powf(base, exponent); }
00137 static int Pow(int base, int exponent) { return (int)pow(base, exponent); }
00138
00140 static int Max(int x, int y) { return((x)>(y)?(x):(y)); }
00142 static float Max(float x, float y) { return((x)>(y)?(x):(y)); }
00144 static int Min(int x, int y) { return((x)<(y)?(x):(y)); }
00146 static float Min(float x, float y) { return((x)<(y)?(x):(y)); }
00148 static float FMod(float x, float y) { return fmodf(x, y); }
00149
00151 static bool IsReal(float f) { if (!(f==f) || (f == std::numeric_limits<float>::infinity()) || (f == -std::numeric_limits<float>::infinity())) return false; else return true; }
00152
00154
00155 static float CopySign(float x, float sign) { return ((sign>=0.0f) ? (Math::Abs(x)) : (-Math::Abs(x))); }
00156
00157 static float Round(float value, int digits = 0);
00158
00159 static float RandomFloat(const float fMin, const float fMax) {return fMin + (fMax - fMin) * ((float)(rand() % 10001) / 10000.0f);}
00160
00161 static Matrix3 MatrixFromVectorRotation(const Vector3& vector);
00162 static void AxisAngleFromVectorRotation(const Vector3& v1, const Vector3& target, Vector3& axis, float& angle);
00163
00164 static void DirectionToPitchHeading(const Vector3& dir, float& pitch, float& heading);
00165
00166
00167 static float ShiftAngle(float angle);
00168
00169 static float GetYAngleBetweenDirections(const Vector3& dir1, const Vector3& dir2);
00170 };
00171
00172 #define RAD2DEG(a) (((a)*180.0f)/(Math::PI))
00173 #define DEG2RAD(a) (((a)*Math::PI)/180.0f)
00174
00175 #define DEG1 DEG2RAD(1.0f)
00176 #define DEG45 DEG2RAD(45.0f)
00177 #define DEG90 DEG2RAD(90.0f)
00178 #define DEG180 DEG2RAD(180.0f)
00179 #define DEG270 DEG2RAD(270.0f)
00180 #define DEG360 DEG2RAD(360.0f)
00181
00182 #endif //__MATH_H__