bit of calc organization

This commit is contained in:
Noel Berry 2022-10-03 00:30:27 -07:00
parent dcd3e11b16
commit 93a231db4d

View File

@ -18,52 +18,92 @@ namespace Blah
constexpr f32 UP = PI / -2; constexpr f32 UP = PI / -2;
constexpr f32 DOWN = PI / 2; constexpr f32 DOWN = PI / 2;
template<class T> constexpr f32 mod(f32 x, f32 m) { return x - (int)(x / m) * m; }
T sign(T x) { return static_cast<T>(x == 0 ? 0 : (x < 0 ? -1 : 1)); }
template<class T>
T abs(T x) { return x < 0 ? -x : x; }
template<class T, class TMin, class TMax>
T clamp(T value, TMin min, TMax max) { return value < min ? static_cast<T>(min) : (value > max ? static_cast<T>(max) : value); }
template<class T>
T min(T a, T b) { return (T)(a < b ? a : b); }
template<class T, typename ... Args>
T min(const T& a, const T& b, const Args&... args) { return Calc::min(a, Calc::min(b, args...)); }
template<class T>
T max(T a, T b) { return (T)(a > b ? a : b); }
template<class T, typename ... Args>
T max(const T& a, const T& b, const Args&... args) { return Calc::max(a, Calc::max(b, args...)); }
inline f32 round(f32 x) { return ::roundf(x); }
inline f32 floor(f32 x) { return ::floorf(x); }
inline f32 ceiling(f32 x) { return ::ceilf(x); }
inline f32 sin(f32 x) { return ::sinf(x); } inline f32 sin(f32 x) { return ::sinf(x); }
inline f32 cos(f32 x) { return ::cosf(x); } inline f32 cos(f32 x) { return ::cosf(x); }
inline f32 tan(f32 x) { return ::tanf(x); } inline f32 tan(f32 x) { return ::tanf(x); }
inline f32 atan2(f32 y, f32 x) { return ::atan2f(y, x); } inline f32 atan2(f32 y, f32 x) { return ::atan2f(y, x); }
inline f32 round(f32 x) { return ::roundf(x); }
inline f32 floor(f32 x) { return ::floorf(x); }
inline f32 ceiling(f32 x) { return ::ceilf(x); }
inline f32 pow(f32 x, f32 n) { return ::powf(x, n); } inline f32 pow(f32 x, f32 n) { return ::powf(x, n); }
inline f32 sqrt(f32 x) { return ::sqrtf(x); } inline f32 sqrt(f32 x) { return ::sqrtf(x); }
inline f32 mod(f32 x, f32 m) { return x - (int)(x / m) * m; }
inline f32 approach(f32 t, f32 target, f32 delta) { return t < target ? Calc::min(t + delta, target) : Calc::max(t - delta, target); } template<class T>
inline f32 map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max) { return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min); } constexpr T sign(T x)
inline f32 clamped_map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max) { return map(Calc::clamp(t, old_min, old_max), old_min, old_max, new_min, new_max); }
inline f32 lerp(f32 a, f32 b, f32 t) { return a + (b - a) * t; }
inline f32 angle_diff(f32 radians_a, f32 radians_b) { return mod((radians_b - radians_a) + PI, TAU) - PI; }
inline f32 angle_lerp(f32 radians_a, f32 radians_b, f32 p)
{ {
const auto shortest_angle = mod(mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI; return static_cast<T>(x == 0 ? 0 : (x < 0 ? -1 : 1));
return radians_a + mod(shortest_angle * p, TAU);
} }
template<class T> template<class T>
inline void swap_endian(T* value) constexpr T abs(T x)
{
return x < 0 ? -x : x;
}
template<class T, class TMin, class TMax>
constexpr T clamp(T value, TMin min, TMax max)
{
return value < min ? static_cast<T>(min) : (value > max ? static_cast<T>(max) : value);
}
template<class T>
constexpr T min(T a, T b)
{
return (T)(a < b ? a : b);
}
template<class T, typename ... Args>
constexpr T min(const T& a, const T& b, const Args&... args)
{
return Calc::min(a, Calc::min(b, args...));
}
template<class T>
constexpr T max(T a, T b)
{
return (T)(a > b ? a : b);
}
template<class T, typename ... Args>
constexpr T max(const T& a, const T& b, const Args&... args)
{
return Calc::max(a, Calc::max(b, args...));
}
constexpr f32 approach(f32 t, f32 target, f32 delta)
{
return t < target ? Calc::min(t + delta, target) : Calc::max(t - delta, target);
}
constexpr f32 map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max)
{
return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min);
}
constexpr f32 clamped_map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max)
{
return map(Calc::clamp(t, old_min, old_max), old_min, old_max, new_min, new_max);
}
constexpr f32 lerp(f32 a, f32 b, f32 t)
{
return a + (b - a) * t;
}
constexpr f32 angle_diff(f32 radians_a, f32 radians_b)
{
return Calc::mod((radians_b - radians_a) + PI, TAU) - PI;
}
constexpr f32 angle_lerp(f32 radians_a, f32 radians_b, f32 p)
{
const auto shortest_angle = Calc::mod(Calc::mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI;
return radians_a + Calc::mod(shortest_angle * p, TAU);
}
template<class T>
constexpr void swap_endian(T* value)
{ {
for (int i = 0; i < sizeof(T) / 2; i++) for (int i = 0; i < sizeof(T) / 2; i++)
{ {
@ -74,8 +114,15 @@ namespace Blah
} }
} }
inline bool is_big_endian() { return (*((short*)"AB") == 0x4243); } inline bool is_big_endian()
inline bool is_little_endian() { return (*((short*)"AB") != 0x4243); } {
return (*((short*)"AB") == 0x4243);
}
inline bool is_little_endian()
{
return (*((short*)"AB") != 0x4243);
}
inline bool is_endian(const Endian& endian) inline bool is_endian(const Endian& endian)
{ {