13 template <std::
floating_po
int T>
14 [[nodiscard]]
constexpr T
toRad(T degrees)
noexcept {
15 return degrees * (std::numbers::pi_v<T> / T(180));
18 template <std::
floating_po
int T>
19 [[nodiscard]]
constexpr T
toDeg(T radians)
noexcept {
20 return radians * (T(180) / std::numbers::pi_v<T>);
24 [[nodiscard]]
constexpr T
abs(T value)
noexcept {
25 return (value < T(0)) ? -value : value;
29 [[nodiscard]]
constexpr T
clamp(T value, T min, T max)
noexcept {
30 return (value < min) ? min : ((value > max) ? max : value);
35 [[nodiscard]]
constexpr int sign(T value)
noexcept {
36 return (T(0) < value) - (value < T(0));
40 template <
typename T,
typename U>
41 [[nodiscard]]
constexpr T
lerp(T start, T end, U t)
noexcept {
42 return start + (end - start) *
clamp(t, U(0), U(1));
46 template <std::
floating_po
int T>
47 [[nodiscard]]
constexpr T
inverseLerp(T start, T end, T value)
noexcept {
48 return start == end ? T(0) : (value - start) / (end - start);
53 [[nodiscard]]
constexpr T
remap(T value, T inMin, T inMax, T outMin, T outMax)
noexcept {
54 return outMin + (value - inMin) * (outMax - outMin) / (inMax - inMin);
58 template <std::
floating_po
int T>
59 [[nodiscard]]
constexpr T
smoothstep(T edge0, T edge1, T x)
noexcept {
61 return t * t * (T(3) - T(2) * t);
65 template <
typename T>
requires std::is_signed_v<T>
66 [[nodiscard]] T
wrap(T value, T min, T max)
noexcept {
67 const T range = max - min;
68 if constexpr (std::is_floating_point_v<T>) {
69 return value - range * std::floor((value - min) / range);
71 const T offset =
static_cast<T
>((value - min) % range);
72 return static_cast<T
>((offset < 0 ? offset + range : offset) + min);
78 [[nodiscard]]
constexpr bool isNearlyEqual(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept {
83 if constexpr (std::numeric_limits<T>::has_infinity) {
84 if (largest > std::numeric_limits<T>::max()) {
88 return abs(a - b) <= epsilon * (largest > T(1) ? largest : T(1));
Math helpers: constants, interpolation, random numbers, 2D and 3D vectors and matrices.
Definition Constants.hpp:5
constexpr bool isNearlyEqual(T a, T b, T epsilon=std::numeric_limits< T >::epsilon()) noexcept
True if a and b differ by at most epsilon, scaled by their magnitude when it exceeds 1.
Definition Utils.hpp:78
constexpr T abs(T value) noexcept
Definition Utils.hpp:24
constexpr T toRad(T degrees) noexcept
Definition Utils.hpp:14
constexpr T remap(T value, T inMin, T inMax, T outMin, T outMax) noexcept
Maps value from [inMin, inMax] to [outMin, outMax], without clamping.
Definition Utils.hpp:53
constexpr int sign(T value) noexcept
Returns -1, 0 or 1 depending on the sign of value.
Definition Utils.hpp:35
constexpr T clamp(T value, T min, T max) noexcept
Definition Utils.hpp:29
T wrap(T value, T min, T max) noexcept
Wraps value into [min, max), such as an angle into [0, 360).
Definition Utils.hpp:66
constexpr T smoothstep(T edge0, T edge1, T x) noexcept
Smooth Hermite interpolation from 0 at edge0 to 1 at edge1.
Definition Utils.hpp:59
constexpr T lerp(T start, T end, U t) noexcept
Linear interpolation from start to end, with t clamped to [0, 1].
Definition Utils.hpp:41
constexpr T toDeg(T radians) noexcept
Definition Utils.hpp:19
constexpr T inverseLerp(T start, T end, T value) noexcept
Inverse of lerp(): where value lies between start and end, or 0 if they are equal.
Definition Utils.hpp:47