2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _CMATH
|
|
|
|
#define _CMATH
|
2022-09-05 04:00:04 +00:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-09-29 05:30:20 +00:00
|
|
|
#include "math.h"
|
|
|
|
|
2023-10-18 00:54:00 +00:00
|
|
|
#include <Kyoto/Math/CVector3f.hpp>
|
|
|
|
|
2022-09-21 05:18:07 +00:00
|
|
|
#define M_PIF 3.14159265358979323846f
|
2022-10-08 04:53:28 +00:00
|
|
|
#define M_2PIF 6.28318530718f
|
2022-09-21 05:18:07 +00:00
|
|
|
|
2022-09-05 04:00:04 +00:00
|
|
|
class CMath {
|
|
|
|
public:
|
2022-10-09 05:37:23 +00:00
|
|
|
static inline float FastFmod(float x, float y) {
|
2022-09-13 04:26:54 +00:00
|
|
|
int v = static_cast< int >(x * (1.f / y));
|
2022-09-05 04:00:04 +00:00
|
|
|
return x - v * y;
|
|
|
|
}
|
2022-09-13 04:26:54 +00:00
|
|
|
template < typename T >
|
2023-10-18 00:54:00 +00:00
|
|
|
static const T& Clamp(const T& min, const T& val, const T& max);
|
2022-10-09 05:37:23 +00:00
|
|
|
static float SqrtF(float v);
|
2024-09-30 06:02:23 +00:00
|
|
|
static inline float Limit(float v, float h) { if (fabs(v) > h) return h * Sign(v); return v; }
|
2022-10-09 05:37:23 +00:00
|
|
|
static inline float Sign(float v) { return FastFSel(v, 1.f, -1.f); }
|
2022-10-21 01:32:04 +00:00
|
|
|
#ifdef __MWERKS__
|
2022-10-09 05:37:23 +00:00
|
|
|
static inline float FastFSel(register float v, register float h, register float l) {
|
|
|
|
register float out;
|
2022-09-29 05:30:20 +00:00
|
|
|
asm {
|
|
|
|
fsel out, v, h, l
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2022-10-21 01:32:04 +00:00
|
|
|
#else
|
2023-10-18 00:54:00 +00:00
|
|
|
static inline float FastFSel(float v, float h, float l) { return v >= 0.f ? h : l; }
|
2022-10-21 01:32:04 +00:00
|
|
|
#endif
|
2022-10-09 05:37:23 +00:00
|
|
|
static inline float AbsF(float v) { return fabs(v); }
|
|
|
|
static inline double AbsD(double v) { return fabs(v); }
|
2022-10-05 00:16:03 +00:00
|
|
|
static inline int AbsI(int v) { return abs(v); }
|
2022-09-29 05:30:20 +00:00
|
|
|
// WrapPi__5CMathFf weak
|
|
|
|
// WrapTwoPi__5CMathFf weak
|
2022-10-03 04:49:11 +00:00
|
|
|
template < typename T >
|
|
|
|
static const T& Min(const T& a, const T& b);
|
|
|
|
template < typename T >
|
|
|
|
static const T& Max(const T& a, const T& b);
|
2023-10-18 00:54:00 +00:00
|
|
|
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);
|
2022-10-09 05:37:23 +00:00
|
|
|
static float ClampRadians(float rad) {
|
|
|
|
float value = FastFmod(rad, M_2PIF);
|
2022-10-05 23:06:15 +00:00
|
|
|
if (value < 0.f) {
|
2022-10-08 04:53:28 +00:00
|
|
|
value += M_2PIF;
|
2022-10-05 23:06:15 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2022-09-29 05:30:20 +00:00
|
|
|
// ModF__5CMathFff weak
|
2022-10-09 05:37:23 +00:00
|
|
|
static float Deg2Rad(float deg) { return Deg2Rev(deg) * M_2PIF; }
|
|
|
|
static float Deg2Rev(float deg) { return deg * (1.f / 360.f); }
|
2023-10-18 00:54:00 +00:00
|
|
|
static float ArcSineR(float v);
|
2022-10-09 05:37:23 +00:00
|
|
|
static float ArcCosineR(float v);
|
2023-10-18 00:54:00 +00:00
|
|
|
static float ArcTangentR(float v);
|
|
|
|
static float PowF(float x, float y);
|
|
|
|
static const float FloorF(float x);
|
|
|
|
static float CeilingF(float x);
|
2023-10-18 04:23:34 +00:00
|
|
|
static CVector3f GetCatmullRomSplinePoint(const CVector3f& a, const CVector3f& b,
|
|
|
|
const CVector3f& c, const CVector3f& d, float t);
|
|
|
|
static float GetCatmullRomSplinePoint(float a, float b, float c, float d, float t);
|
|
|
|
static CVector3f GetBezierPoint(const CVector3f&, const CVector3f&, const CVector3f&,
|
|
|
|
const CVector3f&, float);
|
|
|
|
static CVector3f BaryToWorld(const CVector3f& p0, const CVector3f& p1, const CVector3f& p2,
|
|
|
|
const CVector3f& bary);
|
2022-09-29 05:30:20 +00:00
|
|
|
// GetCatmullRomSplinePoint__5CMathFRC9CVector3fRC9CVector3fRC9CVector3fRC9CVector3ff global
|
2024-09-30 06:02:23 +00:00
|
|
|
#ifdef __MWERKS__
|
|
|
|
static inline float FastSqrtF(register float x) {
|
|
|
|
if (x == 0.f) {
|
|
|
|
return 0.f;
|
|
|
|
}
|
|
|
|
return x * __frsqrte(x);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline float FastSqrtF(float x) { return sqrtf(x); }
|
|
|
|
#endif
|
2022-10-04 19:57:04 +00:00
|
|
|
static double SqrtD(double x);
|
2022-09-29 05:30:20 +00:00
|
|
|
// IsEpsilon__5CMathFfff global
|
|
|
|
// FastMin__5CMathFff weak
|
|
|
|
// FastMax__5CMathFff weak
|
|
|
|
// PowF__5CMathFff global
|
|
|
|
// Rev2Deg__5CMathFf weak
|
|
|
|
// GetCatmullRomSplinePoint__5CMathFfffff global
|
|
|
|
// SlowTangentR__5CMathFf global
|
2022-10-09 05:37:23 +00:00
|
|
|
static float Rad2Deg(float rad) { return rad * (180.f / M_PIF); }
|
|
|
|
static float Rad2Rev(float rad) { return rad * (1.f / M_2PIF); }
|
2022-09-29 05:30:20 +00:00
|
|
|
// CeilingF__5CMathFf global
|
|
|
|
// ArcTangentR__5CMathFf global
|
2023-10-18 00:54:00 +00:00
|
|
|
template < typename T >
|
|
|
|
static void Swap(T& a, T& b) {
|
|
|
|
T tmp = a;
|
|
|
|
a = b;
|
|
|
|
b = tmp;
|
|
|
|
}
|
|
|
|
|
2023-10-17 18:26:21 +00:00
|
|
|
static int FloorPowerOfTwo(int v);
|
2022-09-05 04:00:04 +00:00
|
|
|
};
|
|
|
|
|
2023-10-18 00:54:00 +00:00
|
|
|
template < typename T >
|
|
|
|
const T& CMath::Clamp(const T& min, const T& val, const T& max) {
|
|
|
|
return min > val ? min : max < val ? max : val;
|
|
|
|
}
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _CMATH
|