DDraceNetwork 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 <algorithm>
7#include <cmath>
8#include <concepts>
9#include <cstdlib>
10
11template<typename T>
12concept Numeric = std::integral<T> || std::floating_point<T>;
13
14constexpr float pi = 3.1415926535897932384626433f;
15
16constexpr int round_to_int(float f)
17{
18 return f > 0 ? (int)(f + 0.5f) : (int)(f - 0.5f);
19}
20
21constexpr int round_truncate(float f)
22{
23 return (int)f;
24}
25
26template<typename T, typename TB>
27constexpr T mix(const T a, const T b, TB amount)
28{
29 return a + (b - a) * amount;
30}
31
32template<typename T, typename TB>
33constexpr 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
46template<typename T, typename TB>
47constexpr 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
61inline float random_float()
62{
63 return rand() / (float)(RAND_MAX);
64}
65
66inline float random_float(float min, float max)
67{
68 return min + random_float() * (max - min);
69}
70
71inline float random_float(float max)
72{
73 return random_float(0.0f, max);
74}
75
76inline float random_angle()
77{
78 return 2.0f * pi * (rand() / std::nextafter((float)RAND_MAX, std::numeric_limits<float>::max()));
79}
80
81constexpr int fxpscale = 1 << 10;
82
83// float to fixed
84constexpr int f2fx(float v)
85{
86 return round_to_int(v * fxpscale);
87}
88constexpr float fx2f(int v)
89{
90 return v / (float)fxpscale;
91}
92
93// int to fixed
94constexpr int i2fx(int v)
95{
96 return v * fxpscale;
97}
98constexpr int fx2i(int v)
99{
100 return v / fxpscale;
101}
102
103class fxp
104{
105 int value;
106
107public:
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(value);
129 }
130 constexpr operator float() const
131 {
132 return fx2f(value);
133 }
134};
135
136template<Numeric T>
137constexpr T minimum(T a, T b)
138{
139 return std::min(a, b);
140}
141template<Numeric T>
142constexpr T minimum(T a, T b, T c)
143{
144 return std::min(std::min(a, b), c);
145}
146template<Numeric T>
147constexpr T maximum(T a, T b)
148{
149 return std::max(a, b);
150}
151template<Numeric T>
152constexpr T maximum(T a, T b, T c)
153{
154 return std::max(std::max(a, b), c);
155}
156template<typename T>
157constexpr T absolute(T a)
158{
159 return a < T(0) ? -a : a;
160}
161
162template<Numeric T>
163constexpr bool in_range(T a, T lower, T upper)
164{
165 return lower <= a && a <= upper;
166}
167template<Numeric T>
168constexpr bool in_range(T a, T upper)
169{
170 return in_range(a, 0, upper);
171}
172
173#endif // BASE_MATH_H
Definition math.h:104
int value
Definition math.h:105
constexpr int get() const
Definition math.h:112
constexpr void set(int v)
Definition math.h:108
constexpr fxp & operator=(float v)
Definition math.h:121
constexpr fxp & operator=(int v)
Definition math.h:116
Definition math.h:12
constexpr float pi
Definition math.h:14
constexpr T minimum(T a, T b)
Definition math.h:137
constexpr bool in_range(T a, T lower, T upper)
Definition math.h:163
float random_float()
Definition math.h:61
constexpr int fx2i(int v)
Definition math.h:98
constexpr int fxpscale
Definition math.h:81
constexpr T mix_polynomial(const TB time[], const T data[], int samples, TB amount, T init)
Definition math.h:47
constexpr float fx2f(int v)
Definition math.h:88
float random_angle()
Definition math.h:76
constexpr T mix(const T a, const T b, TB amount)
Definition math.h:27
constexpr int i2fx(int v)
Definition math.h:94
constexpr int round_to_int(float f)
Definition math.h:16
constexpr T maximum(T a, T b)
Definition math.h:147
constexpr int f2fx(float v)
Definition math.h:84
constexpr int round_truncate(float f)
Definition math.h:21
constexpr T absolute(T a)
Definition math.h:157
constexpr T bezier(const T p0, const T p1, const T p2, const T p3, TB amount)
Definition math.h:33