#pragma once #ifndef Convention_Runtime_Math_hpp #define Convention_Runtime_Math_hpp #include "Config.hpp" namespace Convention { namespace Math { // Mathematical constants template struct Constants { static constexpr T PI = static_cast(3.14159265358979323846); static constexpr T E = static_cast(2.71828182845904523536); static constexpr T SQRT2 = static_cast(1.41421356237309504880); static constexpr T SQRT3 = static_cast(1.73205080756887729353); static constexpr T LN2 = static_cast(0.69314718055994530942); static constexpr T LN10 = static_cast(2.30258509299404568402); }; // Basic math utilities template constexpr T Abs(const T& value) { return value < T{} ? -value : value; } template constexpr T Min(const T& a, const T& b) { return a < b ? a : b; } template constexpr T Max(const T& a, const T& b) { return a > b ? a : b; } template constexpr T Clamp(const T& value, const T& min, const T& max) { return Min(Max(value, min), max); } template constexpr T Sign(const T& value) { return value > T{} ? T{1} : (value < T{} ? T{-1} : T{0}); } // Power and root functions template T Pow(const T& base, const T& exponent) { return std::pow(base, exponent); } template T Sqrt(const T& value) { return std::sqrt(value); } template T Cbrt(const T& value) { return std::cbrt(value); } // Trigonometric functions template T Sin(const T& radians) { return std::sin(radians); } template T Cos(const T& radians) { return std::cos(radians); } template T Tan(const T& radians) { return std::tan(radians); } template T Asin(const T& value) { return std::asin(value); } template T Acos(const T& value) { return std::acos(value); } template T Atan(const T& value) { return std::atan(value); } template T Atan2(const T& y, const T& x) { return std::atan2(y, x); } // Degree/Radian conversion template constexpr T DegToRad(const T& degrees) { return degrees * Constants::PI / static_cast(180); } template constexpr T RadToDeg(const T& radians) { return radians * static_cast(180) / Constants::PI; } // Logarithmic functions template T Log(const T& value) { return std::log(value); } template T Log10(const T& value) { return std::log10(value); } template T Log2(const T& value) { return std::log2(value); } template T Exp(const T& value) { return std::exp(value); } // Floating point utilities template bool IsNaN(const T& value) { return std::isnan(value); } template bool IsInfinite(const T& value) { return std::isinf(value); } template bool IsFinite(const T& value) { return std::isfinite(value); } template bool Equal(const T& a, const T& b, const T& epsilon = std::numeric_limits::epsilon()) { return Abs(a - b) <= epsilon; } // Interpolation template constexpr T Lerp(const T& a, const T& b, const T& t) { return a + t * (b - a); } template constexpr T InverseLerp(const T& a, const T& b, const T& value) { return (value - a) / (b - a); } // Rounding functions template T Floor(const T& value) { return std::floor(value); } template T Ceil(const T& value) { return std::ceil(value); } template T Round(const T& value) { return std::round(value); } template T Trunc(const T& value) { return std::trunc(value); } // Modulo template T Mod(const T& a, const T& b) { return std::fmod(a, b); } // Random number generation utilities class Random { private: std::mt19937 generator; public: Random() : generator(std::random_device{}()) {} explicit Random(uint32_t seed) : generator(seed) {} // Generate random integer in range [min, max] template T Range(T min, T max) { static_assert(std::is_integral_v, "T must be an integral type"); std::uniform_int_distribution dist(min, max); return dist(generator); } // Generate random float in range [min, max) template T Range(T min, T max) { static_assert(std::is_floating_point_v, "T must be a floating point type"); std::uniform_real_distribution dist(min, max); return dist(generator); } // Generate random float in range [0, 1) template T Value() { static_assert(std::is_floating_point_v, "T must be a floating point type"); return Range(T{0}, T{1}); } // Generate random boolean bool Bool() { return Range(0, 1) == 1; } }; // Global random instance inline Random& GetGlobalRandom() { static Random instance; return instance; } // Convenience functions using global random template T RandomRange(T min, T max) { return GetGlobalRandom().Range(min, max); } template T RandomValue() { return GetGlobalRandom().Value(); } inline bool RandomBool() { return GetGlobalRandom().Bool(); } } } #endif // Convention_Runtime_Math_hpp