00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_math.hpp"
00012
00013 #include <stdlib.h>
00014
00015 namespace Xrb
00016 {
00017
00018 Float Math::CanonicalAngle (Float const angle)
00019 {
00020 Float retval = angle;
00021
00022 if (retval < -180.0f)
00023 retval -= 360.0f * static_cast<Sint32>((retval - 180.0f) / 360.0f);
00024 else if (retval > 180.0f)
00025 retval -= 360.0f * static_cast<Sint32>((retval + 180.0f) / 360.0f);
00026
00027 return retval;
00028 }
00029
00030 Float Math::PowInt (Float const base, Uint32 const exponent)
00031 {
00032 Float retval = 1.0f;
00033 Uint32 bit = 1;
00034 Float accumulator = base;
00035 while (bit <= exponent)
00036 {
00037 if ((bit & exponent) != 0)
00038 retval *= accumulator;
00039 accumulator *= accumulator;
00040 bit <<= 1;
00041 }
00042 return retval;
00043 }
00044
00045 Float Math::RandomFloat (Float const lower_bound, Float const upper_bound)
00046 {
00047 ASSERT1(lower_bound <= upper_bound);
00048 static Float const s_rand_max = static_cast<Float>(RAND_MAX);
00049 return
00050 (upper_bound - lower_bound) *
00051 static_cast<Float>(rand()) /
00052 s_rand_max
00053 +
00054 lower_bound;
00055 }
00056
00057 Uint16 Math::RandomUint16 (Uint16 const lower_bound, Uint16 const upper_bound)
00058 {
00059
00060 #if !defined(WIN32)
00061 ASSERT1(RAND_MAX >= 65536);
00062 #endif
00063 ASSERT1(lower_bound <= upper_bound);
00064 Uint16 range = upper_bound + 1 - lower_bound;
00065 if (range == 0)
00066 return static_cast<Uint16>(rand());
00067 else
00068 return static_cast<Uint16>(rand()) % range + lower_bound;
00069 }
00070
00071 Uint32 Math::HighestBitIndex (Uint32 const x)
00072 {
00073 static Uint32 const s_highest_bit_index_table[0x10] =
00074 {
00075 0, 0, 1, 1,
00076 2, 2, 2, 2,
00077 3, 3, 3, 3,
00078 3, 3, 3, 3
00079 };
00080 if ((x & 0xFFFF0000) != 0)
00081 if ((x & 0xFF000000) != 0)
00082 if ((x & 0xF0000000) != 0)
00083 return 28 + s_highest_bit_index_table[x >> 28];
00084 else
00085 return 24 + s_highest_bit_index_table[x >> 24];
00086 else
00087 if ((x & 0x00F00000) != 0)
00088 return 20 + s_highest_bit_index_table[x >> 20];
00089 else
00090 return 16 + s_highest_bit_index_table[x >> 16];
00091 else
00092 if ((x & 0x0000FF00) != 0)
00093 if ((x & 0x0000F000) != 0)
00094 return 12 + s_highest_bit_index_table[x >> 12];
00095 else
00096 return 8 + s_highest_bit_index_table[x >> 8];
00097 else
00098 if ((x & 0x000000F0) != 0)
00099 return 4 + s_highest_bit_index_table[x >> 4];
00100 else
00101 return s_highest_bit_index_table[x];
00102 }
00103
00104 Float Math::FastSin (Float const angle)
00105 {
00106 ASSERT0(false && "Not implemented yet");
00107 return Sin(angle);
00108 }
00109
00110 Float Math::FastAsin (Float const value)
00111 {
00112 ASSERT0(false && "Not implemented yet");
00113 return Asin(value);
00114 }
00115
00116 Float Math::FastCos (Float const angle)
00117 {
00118 ASSERT0(false && "Not implemented yet");
00119 return Cos(angle);
00120 }
00121
00122 Float Math::FastAcos (Float const value)
00123 {
00124 ASSERT0(false && "Not implemented yet");
00125 return Acos(value);
00126 }
00127
00128 Float Math::FastTan (Float const angle)
00129 {
00130 ASSERT0(false && "Not implemented yet");
00131 return Tan(angle);
00132 }
00133
00134 Float Math::FastAtan (Float const value)
00135 {
00136 ASSERT0(false && "Not implemented yet");
00137 return Atan(value);
00138 }
00139
00140 Float Math::FastAtan (FloatVector2 const &vector)
00141 {
00142 ASSERT0(false && "Not implemented yet");
00143 return Atan(vector);
00144 }
00145
00146 Float Math::FastAtan2 (Float const y, Float const x)
00147 {
00148 ASSERT0(false && "Not implemented yet");
00149 return Atan2(y, x);
00150 }
00151
00152 FloatVector2 Math::FastUnitVector (Float angle)
00153 {
00154 ASSERT0(false && "Not implemented yet");
00155 return UnitVector(angle);
00156 }
00157
00158 Float Math::FastSqrt (Float const x)
00159 {
00160 ASSERT0(false && "Not implemented yet");
00161 return Sqrt(x);
00162 }
00163
00164 Float Math::FastPow (Float const base, Float const exponent)
00165 {
00166 ASSERT0(false && "Not implemented yet");
00167 return Pow(base, exponent);
00168 }
00169
00170 }
00171