DDNet documentation
Toggle main menu visibility
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
11
template
<
typename
T>
12
concept
Numeric
= std::integral<T> || std::floating_point<T>;
13
14
constexpr
float
pi
= 3.1415926535897932384626433f;
15
constexpr
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
18
constexpr
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
24
constexpr
int
round_to_int
(
float
f)
25
{
26
return
f > 0 ?
round_truncate
(f + 0.5f) :
round_truncate
(f - 0.5f);
27
}
28
29
template
<
typename
T,
typename
TB>
30
constexpr
T
mix
(
const
T a,
const
T b, TB amount)
31
{
32
return
a + (b - a) * amount;
33
}
34
35
template
<
typename
T,
typename
TB>
36
constexpr
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
49
template
<
typename
T,
typename
TB>
50
constexpr
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
64
inline
float
random_float
()
65
{
66
return
rand() / (float)(RAND_MAX);
67
}
68
69
inline
float
random_float
(
float
min,
float
max)
70
{
71
return
min +
random_float
() * (max - min);
72
}
73
74
inline
float
random_float
(
float
max)
75
{
76
return
random_float
(0.0f, max);
77
}
78
79
inline
float
random_angle
()
80
{
81
return
2.0f *
pi
* (rand() / std::nextafter((
float
)RAND_MAX, std::numeric_limits<float>::max()));
82
}
83
84
constexpr
int
fxpscale
= 1 << 10;
85
86
// float to fixed
87
constexpr
int
f2fx
(
float
v)
88
{
89
return
round_to_int
(v *
fxpscale
);
90
}
91
constexpr
float
fx2f
(
int
v)
92
{
93
return
v / (float)
fxpscale
;
94
}
95
96
// int to fixed
97
constexpr
int
i2fx
(
int
v)
98
{
99
return
v *
fxpscale
;
100
}
101
constexpr
int
fx2i
(
int
v)
102
{
103
return
v /
fxpscale
;
104
}
105
106
class
fxp
107
{
108
int
value
;
109
110
public
:
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
139
template
<
typename
T>
140
constexpr
T
absolute
(T a)
141
{
142
return
a < T(0) ? -a : a;
143
}
144
145
template
<Numeric T>
146
constexpr
bool
in_range
(T a, T lower, T upper)
147
{
148
return
lower <= a && a <= upper;
149
}
150
template
<Numeric T>
151
constexpr
bool
in_range
(T a, T upper)
152
{
153
return
in_range
(a, 0, upper);
154
}
155
156
#endif
// BASE_MATH_H
fxp
Definition
math.h:107
fxp::value
int value
Definition
math.h:108
fxp::get
constexpr int get() const
Definition
math.h:115
fxp::set
constexpr void set(int v)
Definition
math.h:111
fxp::operator=
constexpr fxp & operator=(float v)
Definition
math.h:124
fxp::operator=
constexpr fxp & operator=(int v)
Definition
math.h:119
Numeric
Definition
math.h:12
pi
constexpr float pi
Definition
math.h:14
in_range
constexpr bool in_range(T a, T lower, T upper)
Definition
math.h:146
random_float
float random_float()
Definition
math.h:64
fx2i
constexpr int fx2i(int v)
Definition
math.h:101
fxpscale
constexpr int fxpscale
Definition
math.h:84
normalized_golden_angle
constexpr float normalized_golden_angle
Definition
math.h:15
mix_polynomial
constexpr T mix_polynomial(const TB time[], const T data[], int samples, TB amount, T init)
Definition
math.h:50
fx2f
constexpr float fx2f(int v)
Definition
math.h:91
random_angle
float random_angle()
Definition
math.h:79
mix
constexpr T mix(const T a, const T b, TB amount)
Definition
math.h:30
i2fx
constexpr int i2fx(int v)
Definition
math.h:97
round_to_int
constexpr int round_to_int(float f)
Definition
math.h:24
f2fx
constexpr int f2fx(float v)
Definition
math.h:87
round_truncate
constexpr int round_truncate(float f)
Definition
math.h:18
absolute
constexpr T absolute(T a)
Definition
math.h:140
bezier
constexpr T bezier(const T p0, const T p1, const T p2, const T p3, TB amount)
Definition
math.h:36
src
base
math.h
Generated by
1.18.0