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