#pragma once // // Created by josh on 12/25/2023. // #include #include #include #include // TODO: Move to separate math lib, find duplicates! template void Swap(T &a, T &b) { T temp = std::move(a); a = std::move(b); b = std::move(temp); } namespace J3ML::SizedIntegralTypes { using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using s8 = int8_t; using s16 = int16_t; using s32 = int32_t; using s64 = int64_t; } namespace J3ML::SizedFloatTypes { using f32 = float; using f64 = double; using f128 = long double; } using namespace J3ML::SizedIntegralTypes; using namespace J3ML::SizedFloatTypes; //On windows there is no shorthand for pi???? - Redacted. #ifdef _WIN32 #define M_PI 3.14159265358979323846 #endif namespace J3ML::Math { bool EqualAbs(float a, float b, float epsilon = 1e-3f); float RecipFast(float x); // Coming soon: Units Namespace // For Dimensional Analysis /* namespace Units { struct Unit {}; struct Meters : public Unit { }; struct ImperialInches : public Unit {}; struct Time : public Unit {}; struct Grams : public Unit {}; struct MetersPerSecond : public Unit {}; template struct Quantity { public: float Value; }; struct Mass : public Quantity {}; struct Length : public Quantity { }; struct Velocity : public Quantity{ }; class MetrixPrefix { public: std::string Prefix; std::string Symbol; int Power; float InverseMultiply(float input) const { return std::pow(input, -Power); } float Multiply(float input) const { return std::pow(input, Power); } }; namespace Prefixes { static constexpr MetrixPrefix Tera {"tera", "T", 12}; static constexpr MetrixPrefix Giga {"giga", "G", 9}; static constexpr MetrixPrefix Mega {"mega", "M", 6}; static constexpr MetrixPrefix Kilo {"kilo", "k", 3}; static constexpr MetrixPrefix Hecto {"hecto", "h", 2}; static constexpr MetrixPrefix Deca {"deca", "da", 1}; static constexpr MetrixPrefix None {"", "", 0}; static constexpr MetrixPrefix Deci {"", "", 0}; static constexpr MetrixPrefix Centi {"", "", 0}; static constexpr MetrixPrefix Milli {"", "", 0}; static constexpr MetrixPrefix Micro {"", "", 0}; static constexpr MetrixPrefix Nano {"", "", 0}; static constexpr MetrixPrefix Pico {"", "", 0}; } Length operator ""_meters(long double value) { return {(float)value}; } Length operator ""_m(long double value) { return {(float)value}; } constexpr Length operator ""_kilometers(long double value) { return Length {(float)value}; } Length operator ""_km(long double value) { return {(float)value}; } Length operator ""_centimeters(long double value) { return {(float)value}; } Length operator ""_cm(long double value) { return {(float)value}; } Length operator ""_millimeters(long double value) { return {(float)value}; } Length operator ""_mm(long double value) { return {(float)value}; } Velocity operator ""_mps(long double value) { return {(float)value}; } Velocity operator ""_meters_per_second(long double value) { return {(float)value}; } Velocity operator ""_kmps(long double value) { return {(float)value}; } }*/ #pragma region Constants static const float Pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f; static const float RecipSqrt2Pi = 0.3989422804014326779399460599343818684758586311649346576659258296706579258993018385012523339073069364f; static const float GoldenRatio = 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911375f; #pragma endregion #pragma region Math Functions inline float Radians(float degrees); inline float Degrees(float radians); struct NumberRange { float LowerBound; float UpperBound; }; float NormalizeToRange(float input, float fromLower, float fromUpper, float toLower, float toUpper); float NormalizeToRange(float input, const NumberRange& from, const NumberRange& to); // auto rotation_normalized = NormalizeToRange(inp, {0, 360}, {-1, 1}); inline float Lerp(float a, float b, float t); /// Linearly interpolates from a to b, under the modulus mod. /// This function takes evaluates a and b in the range [0, mod] and takes the shorter path to reach from a to b. inline float LerpMod(float a, float b, float mod, float t); /// Computes the lerp factor a and b have to be Lerp()ed to get x. inline float InverseLerp(float a, float b, float x); /// See http://msdn.microsoft.com/en-us/library/bb509665(v=VS.85).aspx inline float Step(float y, float x); /// See http://msdn.microsoft.com/en-us/library/bb509658(v=vs.85).aspx inline float Ramp(float min, float max, float x); inline float PingPongMod(float x, float mod); inline float Sqrt(float x); inline float FastSqrt(float x); /// Returns 1/Sqrt(x). (The reciprocal of the square root of x) inline float RSqrt(float x); inline float FastRSqrt(float x); #pragma endregion namespace BitTwiddling { /// Parses a string of form "011101010" to a u32 u32 BinaryStringToValue(const char* s); /// Returns the number of 1's set in the given value. inline int CountBitsSet(u32 value); } namespace Interp { inline float SmoothStart(float t); } struct Rotation { public: Rotation(); Rotation(float value); float valueInRadians; float ValueInRadians() const; float ValueInDegrees() const; Rotation operator+(const Rotation& rhs); }; Rotation operator ""_rad(long double rads); Rotation operator ""_radians(long double rads); Rotation operator ""_deg(long double rads); Rotation operator ""_degrees(long double rads); }