/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include #include #include "math/nmath.h" using namespace GS; //------------------------------------------------------------------------------ Math::rOrder Math::ReverserRotationOrder(rOrder r) { switch (r) { case rOrder_ZYX: return rOrder_XYZ; case rOrder_YZX: return rOrder_XZY; case rOrder_ZXY: return rOrder_YXZ; case rOrder_XZY: return rOrder_YZX; case rOrder_YXZ: return rOrder_ZXY; case rOrder_XYZ: return rOrder_ZYX; default: return rOrder_Default; } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ float Math::Sqrt(float v) { return sqrtf(v); } float Math::TestEqual(float a, float b, float e) { return Types::Abs(b - a) < e ? true : false; } bool Math::EqualZero(float v, float e) { return (v < -e) || (v > e) ? false : true; } float Math::Pow(float v, float e) { return pow(v, e); } float Math::Ceil(float v) { return (v < 0) ? (float)((int)v) : (float)((int)(v + 1)); } float Math::Floor(float v) { return (v < 0) ? (float)((int)(v - 1)) : (float)((int)v); } float Math::Mod(float v) { double integral; return (float)modf(v, &integral); } float Math::RangeAdjust(float v, float old_min, float old_max, float new_min, float new_max) { return Types::Clamp((v - old_min) / (old_max - old_min) * (new_max - new_min) + new_min, new_min, new_max); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ bool Math::IsFinite(float v) { return (v <= FLT_MAX && v >= -FLT_MAX); } //------------------------------------------------------------------------------ #define __USE_LUT_BASED_TRIG__ 0 #if (__USE_LUT_BASED_TRIG__ == 0) void Math::Init() {} //------------------------------------------------------------------------------ float Math::Quantize(float v, float q) { return Floor(v / q) * q; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ float Math::Sin(float v) { return sin(v); } float Math::ASin(float v) { return asin(Types::Clamp(v, -1.f, 1.f)); } float Math::Cos(float v) { return cos(v); } float Math::ACos(float v) { return acos(Types::Clamp(v, -1.f, 1.f)); } float Math::Tan(float v) { return tan(v); } float Math::ATan(float v) { return atan(v); } //------------------------------------------------------------------------------ #else #include "container/narray.h" // keep as power of 2 #define __LUT_PRECISION 64 static nArray lCos, lSin, lTan, lACos, lASin, lAtan; //------------------------------------------------------------------------------ void Math::Init() { lCos.Allocate(__LUT_PRECISION); lSin.Allocate(__LUT_PRECISION); lTan.Allocate(__LUT_PRECISION); lACos.Allocate(__LUT_PRECISION); lASin.Allocate(__LUT_PRECISION); for (int v = 0; v < __LUT_PRECISION; ++v) { const float deg = ((float)v / __LUT_PRECISION) * (Pi * 2.f); lSin[v] = sin(deg); lCos[v] = cos(deg); lTan[v] = tan(deg); const float inv = ((float)v / __LUT_PRECISION) * 2.f - 1.f; lASin[v] = asin(inv); lACos[v] = acos(inv); } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ float Math::Sin(float v) { return lSin[int(v * (__LUT_PRECISION / (Pi * 2.f))) & (__LUT_PRECISION - 1)]; } float Math::ASin(float v) { return lASin[int((v + 1.f) * (__LUT_PRECISION / 2)) & (__LUT_PRECISION - 1)]; } float Math::Cos(float v) { return lCos[int(v * (__LUT_PRECISION / (Pi * 2.f))) & (__LUT_PRECISION - 1)]; } float Math::ACos(float v) { return lACos[int((v + 1.f) * (__LUT_PRECISION / 2)) & (__LUT_PRECISION - 1)]; } float Math::Tan(float v) { return lTan[int(v * (__LUT_PRECISION / (Pi * 2.f))) & (__LUT_PRECISION - 1)]; } float Math::ATan(float v) { return atan(v); } //------------------------------------------------------------------------------ #endif