| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #ifndef BASE_MATH_H |
| 4 | #define BASE_MATH_H |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <concepts> |
| 9 | #include <cstdlib> |
| 10 | |
| 11 | template<typename T> |
| 12 | concept Numeric = std::integral<T> || std::floating_point<T>; |
| 13 | |
| 14 | constexpr float pi = 3.1415926535897932384626433f; |
| 15 | |
| 16 | constexpr int round_to_int(float f) |
| 17 | { |
| 18 | return f > 0 ? (int)(f + 0.5f) : (int)(f - 0.5f); |
| 19 | } |
| 20 | |
| 21 | constexpr int round_truncate(float f) |
| 22 | { |
| 23 | return (int)f; |
| 24 | } |
| 25 | |
| 26 | template<typename T, typename TB> |
| 27 | constexpr T mix(const T a, const T b, TB amount) |
| 28 | { |
| 29 | return a + (b - a) * amount; |
| 30 | } |
| 31 | |
| 32 | template<typename T, typename TB> |
| 33 | constexpr T bezier(const T p0, const T p1, const T p2, const T p3, TB amount) |
| 34 | { |
| 35 | // De-Casteljau Algorithm |
| 36 | const T c10 = mix(p0, p1, amount); |
| 37 | const T c11 = mix(p1, p2, amount); |
| 38 | const T c12 = mix(p2, p3, amount); |
| 39 | |
| 40 | const T c20 = mix(c10, c11, amount); |
| 41 | const T c21 = mix(c11, c12, amount); |
| 42 | |
| 43 | return mix(c20, c21, amount); // c30 |
| 44 | } |
| 45 | |
| 46 | template<typename T, typename TB> |
| 47 | constexpr T mix_polynomial(const TB time[], const T data[], int samples, TB amount, T init) |
| 48 | { |
| 49 | T result = init; |
| 50 | for(int i = 0; i < samples; i++) |
| 51 | { |
| 52 | T term = data[i]; |
| 53 | for(int j = 0; j < samples; j++) |
| 54 | if(j != i) |
| 55 | term = term * (amount - time[j]) / TB(time[i] - time[j]); |
| 56 | result += term; |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | inline float random_float() |
| 62 | { |
| 63 | return rand() / (float)(RAND_MAX); |
| 64 | } |
| 65 | |
| 66 | inline float random_float(float min, float max) |
| 67 | { |
| 68 | return min + random_float() * (max - min); |
| 69 | } |
| 70 | |
| 71 | inline float random_float(float max) |
| 72 | { |
| 73 | return random_float(min: 0.0f, max); |
| 74 | } |
| 75 | |
| 76 | inline float random_angle() |
| 77 | { |
| 78 | return 2.0f * pi * (rand() / std::nextafter(x: (float)RAND_MAX, y: std::numeric_limits<float>::max())); |
| 79 | } |
| 80 | |
| 81 | constexpr int fxpscale = 1 << 10; |
| 82 | |
| 83 | // float to fixed |
| 84 | constexpr int f2fx(float v) |
| 85 | { |
| 86 | return round_to_int(f: v * fxpscale); |
| 87 | } |
| 88 | constexpr float fx2f(int v) |
| 89 | { |
| 90 | return v / (float)fxpscale; |
| 91 | } |
| 92 | |
| 93 | // int to fixed |
| 94 | constexpr int i2fx(int v) |
| 95 | { |
| 96 | return v * fxpscale; |
| 97 | } |
| 98 | constexpr int fx2i(int v) |
| 99 | { |
| 100 | return v / fxpscale; |
| 101 | } |
| 102 | |
| 103 | class fxp |
| 104 | { |
| 105 | int value; |
| 106 | |
| 107 | public: |
| 108 | constexpr void set(int v) |
| 109 | { |
| 110 | value = v; |
| 111 | } |
| 112 | constexpr int get() const |
| 113 | { |
| 114 | return value; |
| 115 | } |
| 116 | constexpr fxp &operator=(int v) |
| 117 | { |
| 118 | value = i2fx(v); |
| 119 | return *this; |
| 120 | } |
| 121 | constexpr fxp &operator=(float v) |
| 122 | { |
| 123 | value = f2fx(v); |
| 124 | return *this; |
| 125 | } |
| 126 | constexpr operator int() const |
| 127 | { |
| 128 | return fx2i(v: value); |
| 129 | } |
| 130 | constexpr operator float() const |
| 131 | { |
| 132 | return fx2f(v: value); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | template<Numeric T> |
| 137 | constexpr T minimum(T a, T b) |
| 138 | { |
| 139 | return std::min(a, b); |
| 140 | } |
| 141 | template<Numeric T> |
| 142 | constexpr T minimum(T a, T b, T c) |
| 143 | { |
| 144 | return std::min(std::min(a, b), c); |
| 145 | } |
| 146 | template<Numeric T> |
| 147 | constexpr T maximum(T a, T b) |
| 148 | { |
| 149 | return std::max(a, b); |
| 150 | } |
| 151 | template<Numeric T> |
| 152 | constexpr T maximum(T a, T b, T c) |
| 153 | { |
| 154 | return std::max(std::max(a, b), c); |
| 155 | } |
| 156 | template<typename T> |
| 157 | constexpr T absolute(T a) |
| 158 | { |
| 159 | return a < T(0) ? -a : a; |
| 160 | } |
| 161 | |
| 162 | template<Numeric T> |
| 163 | constexpr bool in_range(T a, T lower, T upper) |
| 164 | { |
| 165 | return lower <= a && a <= upper; |
| 166 | } |
| 167 | template<Numeric T> |
| 168 | constexpr bool in_range(T a, T upper) |
| 169 | { |
| 170 | return in_range(a, 0, upper); |
| 171 | } |
| 172 | |
| 173 | #endif // BASE_MATH_H |
| 174 | |