DDNet documentation
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
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 <cmath>
7#include <concepts>
8#include <cstdlib>
9#include <limits>
10
11template<typename T>
12concept Numeric = std::integral<T> || std::floating_point<T>;
13
14constexpr float pi = 3.1415926535897932384626433f;
15constexpr float normalized_golden_angle = 137.50776f / 360.0f;
16
17// NaN and floats outside the int range convert to INT_MIN, a plain cast would be undefined
18constexpr int round_truncate(float f)
19{
20 constexpr float imin = (float)std::numeric_limits<int>::min();
21 return f >= imin && f < -imin ? (int)f : std::numeric_limits<int>::min();
22}
23
24constexpr int round_to_int(float f)
25{
26 return f > 0 ? round_truncate(f + 0.5f) : round_truncate(f - 0.5f);
27}
28
29template<typename T, typename TB>
30constexpr T mix(const T a, const T b, TB amount)
31{
32 return a + (b - a) * amount;
33}
34
35template<typename T, typename TB>
36constexpr T bezier(const T p0, const T p1, const T p2, const T p3, TB amount)
37{
38 // De-Casteljau Algorithm
39 const T c10 = mix(p0, p1, amount);
40 const T c11 = mix(p1, p2, amount);
41 const T c12 = mix(p2, p3, amount);
42
43 const T c20 = mix(c10, c11, amount);
44 const T c21 = mix(c11, c12, amount);
45
46 return mix(c20, c21, amount); // c30
47}
48
49template<typename T, typename TB>
50constexpr T mix_polynomial(const TB time[], const T data[], int samples, TB amount, T init)
51{
52 T result = init;
53 for(int i = 0; i < samples; i++)
54 {
55 T term = data[i];
56 for(int j = 0; j < samples; j++)
57 if(j != i)
58 term = term * (amount - time[j]) / TB(time[i] - time[j]);
59 result += term;
60 }
61 return result;
62}
63
64inline float random_float()
65{
66 return rand() / (float)(RAND_MAX);
67}
68
69inline float random_float(float min, float max)
70{
71 return min + random_float() * (max - min);
72}
73
74inline float random_float(float max)
75{
76 return random_float(0.0f, max);
77}
78
79inline float random_angle()
80{
81 return 2.0f * pi * (rand() / std::nextafter((float)RAND_MAX, std::numeric_limits<float>::max()));
82}
83
84constexpr int fxpscale = 1 << 10;
85
86// float to fixed
87constexpr int f2fx(float v)
88{
89 return round_to_int(v * fxpscale);
90}
91constexpr float fx2f(int v)
92{
93 return v / (float)fxpscale;
94}
95
96// int to fixed
97constexpr int i2fx(int v)
98{
99 return v * fxpscale;
100}
101constexpr int fx2i(int v)
102{
103 return v / fxpscale;
104}
105
106class fxp
107{
108 int value;
109
110public:
111 constexpr void set(int v)
112 {
113 value = v;
114 }
115 constexpr int get() const
116 {
117 return value;
118 }
119 constexpr fxp &operator=(int v)
120 {
121 value = i2fx(v);
122 return *this;
123 }
124 constexpr fxp &operator=(float v)
125 {
126 value = f2fx(v);
127 return *this;
128 }
129 constexpr operator int() const
130 {
131 return fx2i(value);
132 }
133 constexpr operator float() const
134 {
135 return fx2f(value);
136 }
137};
138
139template<typename T>
140constexpr T absolute(T a)
141{
142 return a < T(0) ? -a : a;
143}
144
145template<Numeric T>
146constexpr bool in_range(T a, T lower, T upper)
147{
148 return lower <= a && a <= upper;
149}
150template<Numeric T>
151constexpr bool in_range(T a, T upper)
152{
153 return in_range(a, 0, upper);
154}
155
156#endif // BASE_MATH_H
Definition math.h:107
int value
Definition math.h:108
constexpr int get() const
Definition math.h:115
constexpr void set(int v)
Definition math.h:111
constexpr fxp & operator=(float v)
Definition math.h:124
constexpr fxp & operator=(int v)
Definition math.h:119
Definition math.h:12
constexpr float pi
Definition math.h:14
constexpr bool in_range(T a, T lower, T upper)
Definition math.h:146
float random_float()
Definition math.h:64
constexpr int fx2i(int v)
Definition math.h:101
constexpr int fxpscale
Definition math.h:84
constexpr float normalized_golden_angle
Definition math.h:15
constexpr T mix_polynomial(const TB time[], const T data[], int samples, TB amount, T init)
Definition math.h:50
constexpr float fx2f(int v)
Definition math.h:91
float random_angle()
Definition math.h:79
constexpr T mix(const T a, const T b, TB amount)
Definition math.h:30
constexpr int i2fx(int v)
Definition math.h:97
constexpr int round_to_int(float f)
Definition math.h:24
constexpr int f2fx(float v)
Definition math.h:87
constexpr int round_truncate(float f)
Definition math.h:18
constexpr T absolute(T a)
Definition math.h:140
constexpr T bezier(const T p0, const T p1, const T p2, const T p3, TB amount)
Definition math.h:36