From 93a231db4d6a71900c4bd6e05ce3f985b1941ab4 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Mon, 3 Oct 2022 00:30:27 -0700 Subject: [PATCH] bit of calc organization --- include/blah_calc.h | 121 ++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 37 deletions(-) diff --git a/include/blah_calc.h b/include/blah_calc.h index 807f1ec..58ff3da 100644 --- a/include/blah_calc.h +++ b/include/blah_calc.h @@ -18,52 +18,92 @@ namespace Blah constexpr f32 UP = PI / -2; constexpr f32 DOWN = PI / 2; - template - T sign(T x) { return static_cast(x == 0 ? 0 : (x < 0 ? -1 : 1)); } - - template - T abs(T x) { return x < 0 ? -x : x; } - - template - T clamp(T value, TMin min, TMax max) { return value < min ? static_cast(min) : (value > max ? static_cast(max) : value); } - - template - T min(T a, T b) { return (T)(a < b ? a : b); } - - template - T min(const T& a, const T& b, const Args&... args) { return Calc::min(a, Calc::min(b, args...)); } - - template - T max(T a, T b) { return (T)(a > b ? a : b); } - - template - 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); } + constexpr f32 mod(f32 x, f32 m) { return x - (int)(x / m) * m; } inline f32 sin(f32 x) { return ::sinf(x); } inline f32 cos(f32 x) { return ::cosf(x); } inline f32 tan(f32 x) { return ::tanf(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 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); } - 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); } - 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) + template + constexpr T sign(T x) { - const auto shortest_angle = mod(mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI; - return radians_a + mod(shortest_angle * p, TAU); + return static_cast(x == 0 ? 0 : (x < 0 ? -1 : 1)); } template - inline void swap_endian(T* value) + constexpr T abs(T x) + { + return x < 0 ? -x : x; + } + + template + constexpr T clamp(T value, TMin min, TMax max) + { + return value < min ? static_cast(min) : (value > max ? static_cast(max) : value); + } + + template + constexpr T min(T a, T b) + { + return (T)(a < b ? a : b); + } + + template + constexpr T min(const T& a, const T& b, const Args&... args) + { + return Calc::min(a, Calc::min(b, args...)); + } + + template + constexpr T max(T a, T b) + { + return (T)(a > b ? a : b); + } + + template + 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 + constexpr void swap_endian(T* value) { 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_little_endian() { return (*((short*)"AB") != 0x4243); } + inline bool is_big_endian() + { + return (*((short*)"AB") == 0x4243); + } + + inline bool is_little_endian() + { + return (*((short*)"AB") != 0x4243); + } inline bool is_endian(const Endian& endian) {