RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <concepts>
5#include <limits>
6#include <numbers>
7#include <type_traits>
8
9namespace RST::Maths {
10
13 template <std::floating_point T>
14 [[nodiscard]] constexpr T toRad(T degrees) noexcept {
15 return degrees * (std::numbers::pi_v<T> / T(180));
16 }
17
18 template <std::floating_point T>
19 [[nodiscard]] constexpr T toDeg(T radians) noexcept {
20 return radians * (T(180) / std::numbers::pi_v<T>);
21 }
22
23 template <typename T>
24 [[nodiscard]] constexpr T abs(T value) noexcept {
25 return (value < T(0)) ? -value : value;
26 }
27
28 template <typename T>
29 [[nodiscard]] constexpr T clamp(T value, T min, T max) noexcept {
30 return (value < min) ? min : ((value > max) ? max : value);
31 }
32
34 template <typename T>
35 [[nodiscard]] constexpr int sign(T value) noexcept {
36 return (T(0) < value) - (value < T(0));
37 }
38
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));
43 }
44
46 template <std::floating_point T>
47 [[nodiscard]] constexpr T inverseLerp(T start, T end, T value) noexcept {
48 return start == end ? T(0) : (value - start) / (end - start);
49 }
50
52 template <typename T>
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);
55 }
56
58 template <std::floating_point T>
59 [[nodiscard]] constexpr T smoothstep(T edge0, T edge1, T x) noexcept {
60 const T t = clamp(inverseLerp(edge0, edge1, x), T(0), T(1));
61 return t * t * (T(3) - T(2) * t);
62 }
63
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);
70 } else {
71 const T offset = static_cast<T>((value - min) % range);
72 return static_cast<T>((offset < 0 ? offset + range : offset) + min);
73 }
74 }
75
77 template <typename T>
78 [[nodiscard]] constexpr bool isNearlyEqual(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept {
79 if (a == b) {
80 return true;
81 }
82 const T largest = abs(a) > abs(b) ? abs(a) : abs(b);
83 if constexpr (std::numeric_limits<T>::has_infinity) {
84 if (largest > std::numeric_limits<T>::max()) {
85 return false;
86 }
87 }
88 return abs(a - b) <= epsilon * (largest > T(1) ? largest : T(1));
89 }
91
92}
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