00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "mathutils.h"
00022
00023 #include <cmath>
00024 #include <stdint.h>
00025 #include <string.h>
00026 #include <float.h>
00027
00028 #define MATH_UTILS_MAX_ANGLE 360
00029
00030 float sinList[MATH_UTILS_MAX_ANGLE];
00031 float cosList[MATH_UTILS_MAX_ANGLE];
00032 float tanList[MATH_UTILS_MAX_ANGLE];
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 float utils::math::fastInvSqrt(float x)
00045 {
00046 typedef char float_must_be_32_bits[(sizeof(float) == 4) * 2 - 1];
00047 float xhalf = 0.5f * x;
00048 uint32_t i;
00049 memcpy(&i, &x, 4);
00050 i = 0x5f375a86 - (i >> 1);
00051 memcpy(&x, &i, 4);
00052 x = x * (1.5f-xhalf * x * x);
00053 return x;
00054 }
00055
00056 float utils::math::fastSqrt(float x)
00057 {
00058 return x * utils::math::fastInvSqrt(x);
00059 }
00060
00061 void utils::math::init()
00062 {
00063
00064 const float radianAngleRatio = M_PI_2 / 90.0f;
00065
00066 for (int i = 0; i < MATH_UTILS_MAX_ANGLE; i++)
00067 {
00068 sinList[i] = sin(radianAngleRatio * (float) i);
00069 cosList[i] = cos(radianAngleRatio * (float) i);
00070
00071 if (i == 90)
00072 {
00073 tanList[i] = FLT_MAX;
00074 continue;
00075 }
00076 if (i == 270)
00077 {
00078 tanList[i] = -FLT_MAX;
00079 continue;
00080 }
00081 tanList[i] = tan(radianAngleRatio * (float) i);
00082 }
00083 }
00084
00085 float utils::math::cachedSin(int angle)
00086 {
00087 return sinList[angle];
00088 }
00089
00090 float utils::math::cachedCos(int angle)
00091 {
00092 return cosList[angle];
00093 }
00094
00095 float utils::math::cachedTan(int angle)
00096 {
00097 return tanList[angle];
00098 }