00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_MATH_HPP_)
00012 #define _XRB_MATH_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #if defined(WIN32)
00017
00018 #include <float.h>
00019 #endif // defined(WIN32)
00020 #include <math.h>
00021 #include <stdlib.h>
00022
00023 #include "xrb_vector.hpp"
00024
00025 namespace Xrb
00026 {
00027
00036 namespace Math
00037 {
00038
00041 inline Float Radians (Float const angle)
00042 {
00043 static Float const conversion_factor = static_cast<Float>(M_PI) / 180.0f;
00044 return angle * conversion_factor;
00045 }
00048 inline Float Degrees (Float const radians)
00049 {
00050 static Float const conversion_factor = 180.0f / static_cast<Float>(M_PI);
00051 return radians * conversion_factor;
00052 }
00056 Float CanonicalAngle (Float angle);
00057
00060 inline Float Sin (Float const angle)
00061 {
00062 return sinf(Radians(angle));
00063 }
00066 inline Float Asin (Float const value)
00067 {
00068 return Degrees(asinf(value));
00069 }
00072 inline Float Cos (Float const angle)
00073 {
00074 return cosf(Radians(angle));
00075 }
00078 inline Float Acos (Float const value)
00079 {
00080 return Degrees(acosf(value));
00081 }
00084 inline Float Tan (Float const angle)
00085 {
00086 return tanf(Radians(angle));
00087 }
00090 inline Float Atan (Float const value)
00091 {
00092 return Degrees(atanf(value));
00093 }
00097 inline Float Atan (FloatVector2 const &vector)
00098 {
00099 return Degrees(atan2f(vector[Dim::Y], vector[Dim::X]));
00100 }
00104 inline Float Atan2 (Float const y, Float const x)
00105 {
00106 return Degrees(atan2f(y, x));
00107 }
00111 inline FloatVector2 UnitVector (Float const angle)
00112 {
00113 Float angle_in_radians = Radians(angle);
00114 return FloatVector2(cosf(angle_in_radians), sinf(angle_in_radians));
00115 }
00118 inline Float Sqrt (Float const x)
00119 {
00120 return sqrtf(x);
00121 }
00124 inline Float Pow (Float const base, Float const exponent)
00125 {
00126 return powf(base, exponent);
00127 }
00130 Float PowInt (Float base, Uint32 exponent);
00133 Float RandomFloat (Float lower_bound, Float upper_bound);
00138 Uint16 RandomUint16 (Uint16 lower_bound = 0, Uint16 upper_bound = 65535);
00141 inline Float RandomAngle ()
00142 {
00143 return RandomFloat(static_cast<Float>(0), static_cast<Float>(360));
00144 }
00147 inline Float ExpBaseE (Float const exponent)
00148 {
00149 return expf(exponent);
00150 }
00153 inline Float ExpBase10 (Float const exponent)
00154 {
00155 return powf(static_cast<Float>(10), exponent);
00156 }
00159 inline Float LogBaseE (Float const x)
00160 {
00161 return logf(x);
00162 }
00165 inline Float LogBase10 (Float const x)
00166 {
00167 return log10f(x);
00168 }
00171 inline Float Nan ()
00172 {
00173
00174
00175 #if defined(WORDS_BIGENDIAN)
00176 static Uint8 const s_nan_bytes[8] = { 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
00177 static Float const s_nan = *reinterpret_cast<Float const *>(s_nan_bytes);
00178 #else // !defined(WORDS_BIGENDIAN)
00179 static Uint8 const s_nan_bytes[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F };
00180 static Float const s_nan = *reinterpret_cast<Float const *>(s_nan_bytes + 8 - sizeof(Float));
00181 #endif // !defined(WORDS_BIGENDIAN)
00182
00183 return s_nan;
00184 }
00188 inline bool IsFinite (Float const x)
00189 {
00190 #if defined(WIN32)
00191 return _finite(static_cast<double>(x)) != 0;
00192 #else // !defined(WIN32)
00193 return isfinite(x);
00194 #endif
00195 }
00201 Uint32 HighestBitIndex (Uint32 x);
00206 template <Uint32 fractional_bits, typename IntType>
00207 inline IntType FixedPointRound (IntType x)
00208 {
00209 if (x < 0)
00210 x -= (1<<(fractional_bits-1));
00211 else
00212 x += (1<<(fractional_bits-1));
00213 return x;
00214 }
00215
00218 Float FastSin (Float angle);
00221 Float FastAsin (Float value);
00224 Float FastCos (Float angle);
00227 Float FastAcos (Float value);
00230 Float FastTan (Float angle);
00234 Float FastAtan (Float value);
00238 Float FastAtan (FloatVector2 const &vector);
00243 Float FastAtan2 (Float y, Float x);
00247 FloatVector2 FastUnitVector (Float angle);
00250 Float FastSqrt (Float x);
00254 Float FastPow (Float base, Float exponent);
00255
00259 inline Float Floor (Float const x)
00260 {
00261 return floorf(x);
00262 }
00266 inline Float Ceiling (Float const x)
00267 {
00268 return ceilf(x);
00269 }
00273 inline Float Round (Float const x)
00274 {
00275 return floorf(x + 0.5f);
00276 }
00277 }
00278
00279 }
00280
00281 #endif // !defined(_XRB_MATH_HPP_)
00282