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