diff --git a/include/Kyoto/Math/CMath.hpp b/include/Kyoto/Math/CMath.hpp index 4c5cc734..3c4ef5cb 100644 --- a/include/Kyoto/Math/CMath.hpp +++ b/include/Kyoto/Math/CMath.hpp @@ -5,20 +5,19 @@ #include "math.h" +#include + #define M_PIF 3.14159265358979323846f #define M_2PIF 6.28318530718f class CMath { public: - static float FastCosR(float v); - static float FastSinR(float v); - static float FastArcCosR(float v); static inline float FastFmod(float x, float y) { int v = static_cast< int >(x * (1.f / y)); return x - v * y; } template < typename T > - static const T& Clamp(const T& min, const T& val, const T& max); // TODO: weak + static const T& Clamp(const T& min, const T& val, const T& max); static float SqrtF(float v); static inline float Limit(float v, float h) { return fabs(v) > h ? h * Sign(v) : v; } static inline float Sign(float v) { return FastFSel(v, 1.f, -1.f); } @@ -31,9 +30,7 @@ public: return out; } #else - static inline float FastFSel(float v, float h, float l) { - return v >= 0.f ? h : l; - } + static inline float FastFSel(float v, float h, float l) { return v >= 0.f ? h : l; } #endif static inline float AbsF(float v) { return fabs(v); } static inline double AbsD(double v) { return fabs(v); } @@ -44,12 +41,15 @@ public: static const T& Min(const T& a, const T& b); template < typename T > static const T& Max(const T& a, const T& b); - // InvSqrtF__5CMathFf global - // FastArcCosR__5CMathFf global - // SlowCosineR__5CMathFf global - // SlowSineR__5CMathFf global - // FastCosR__5CMathFf global - // GetBezierPoint__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3ff global + static float InvSqrtF(float x); + static float FastArcCosR(float x); + static float SlowCosineR(float x); + static float SlowSineR(float x); + static float SlowTangentR(float x); + static float FastSinR(float x); + static float FastCosR(float x); + static CVector3f GetBezierPoint(const CVector3f&, const CVector3f&, const CVector3f&, + const CVector3f&, float); static float ClampRadians(float rad) { float value = FastFmod(rad, M_2PIF); if (value < 0.f) { @@ -60,8 +60,12 @@ public: // ModF__5CMathFff weak static float Deg2Rad(float deg) { return Deg2Rev(deg) * M_2PIF; } static float Deg2Rev(float deg) { return deg * (1.f / 360.f); } + static float ArcSineR(float v); static float ArcCosineR(float v); - // FloorF__5CMathFf global + static float ArcTangentR(float v); + static float PowF(float x, float y); + static const float FloorF(float x); + static float CeilingF(float x); // BaryToWorld__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3f global // GetCatmullRomSplinePoint__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3ff global // FastSqrtF__5CMathFf weak @@ -77,8 +81,18 @@ public: static float Rad2Rev(float rad) { return rad * (1.f / M_2PIF); } // CeilingF__5CMathFf global // ArcTangentR__5CMathFf global - // Swap__5CMathFRfRf weak + template < typename T > + static void Swap(T& a, T& b) { + T tmp = a; + a = b; + b = tmp; + } + static int FloorPowerOfTwo(int v); }; +template < typename T > +const T& CMath::Clamp(const T& min, const T& val, const T& max) { + return min > val ? min : max < val ? max : val; +} #endif // _CMATH diff --git a/libc/math.h b/libc/math.h index 5913aadd..d9ea1aa2 100644 --- a/libc/math.h +++ b/libc/math.h @@ -73,6 +73,7 @@ double cos(double x); double atan(double x); double atan2(double y, double x); double tan(double x); +double ceil(double x); _MATH_INLINE float fabsf(float x) { return (float)fabs((double)x); } _MATH_INLINE float sinf(float x) { return (float)sin((double)x); } @@ -80,8 +81,11 @@ _MATH_INLINE float cosf(float x) { return (float)cos((double)x); } _MATH_INLINE float atan2f(float y, float x) { return (float)atan2((double)y, (double)x); } _MATH_INLINE float fmodf(float x, float m) { return (float)fmod((double)x, (double)m); } float tanf(float x); +double asin(double x); double acos(double x); float acosf(float x); +double log(double x); +double exp(double x); double ldexp(double x, int exp); diff --git a/src/Kyoto/Math/RMathUtils.cpp b/src/Kyoto/Math/RMathUtils.cpp index 5d785c83..3aac4d49 100644 --- a/src/Kyoto/Math/RMathUtils.cpp +++ b/src/Kyoto/Math/RMathUtils.cpp @@ -3,3 +3,38 @@ float CMath::SqrtF(float x) { return 0.f; } double CMath::SqrtD(double x) { return 0.0; } + +float CMath::InvSqrtF(float x) { return 0.f; } + +float CMath::ArcSineR(float v) { return asin(v); } + +float CMath::ArcCosineR(float v) { return acos(v); } + +float CMath::ArcTangentR(float v) { return atan(v); } + +float CMath::PowF(float x, float y) { return exp(log(x) * y); } + +float CMath::SlowSineR(float x) { return sin(x); } + +float CMath::SlowCosineR(float x) { return cos(x); } + +float CMath::SlowTangentR(float x) { return tan(x); } + +const float CMath::FloorF(float x) { return floor(x); } + +float CMath::CeilingF(float x) { + float tmp = FloorF(x); + if (tmp == x) { + return x; + } + return tmp + 1.f; +} + +CVector3f CMath::GetBezierPoint(const CVector3f& a, const CVector3f& b, const CVector3f& c, + const CVector3f& d, float t) { + const float omt = 1.f - t; + return ((a * omt + b * t) * omt + (b * omt + c * t) * t) * omt + + ((b * omt + c * t) * omt + (c * omt + d * t) * t) * t; +} + +float CMath::FastSinR(float x) {}