TDME2 1.9.121
Math.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdlib>
5
6#include <tdme/tdme.h>
8
9#if defined(_WIN32) && defined(_MSC_VER)
10 #define NOMINMAX
11 #undef max
12 #undef min
13#endif
14
15/**
16 * Standard math functions
17 * @author Andreas Drewke
18 * @version $Id$
19 */
21{
22public:
23 static constexpr float E { 2.718281828459045f };
24 static constexpr float PI { 3.141592653589793f };
25 static constexpr float EPSILON { 0.0000001f };
26 static constexpr float DEG2RAD { 0.017453294f };
27 static constexpr float g { 9.80665f };
28
29 /**
30 * Clamps a int value to min or max value
31 * @param value value
32 * @param min min value
33 * @param max max value
34 * @return clamped value
35 */
36 inline static int clamp(int value, int min, int max) {
37 if (value < min) return min;
38 if (value > max) return max;
39 return value;
40 }
41
42 /**
43 * Clamps a 64 bit int value to min or max value
44 * @param value value
45 * @param min min value
46 * @param max max value
47 * @return clamped value
48 */
49 inline static int64_t clamp(int64_t value, int64_t min, int64_t max) {
50 if (value < min) return min;
51 if (value > max) return max;
52 return value;
53 }
54
55 /**
56 * Clamps a float value to min or max value
57 * @param value value
58 * @param min min value
59 * @param max max value
60 * @return clamped value
61 */
62 inline static float clamp(float value, float min, float max) {
63 if (value < min) return min;
64 if (value > max) return max;
65 return value;
66 }
67
68 /**
69 * Returns sign of value
70 * @param value value
71 * @return -1 if value is negative or +1 if positive
72 */
73 inline static float sign(float value) {
74 return value / Math::abs(value);
75 }
76
77 /**
78 * Do the square product
79 * @param value value
80 * @return
81 */
82 inline static float square(float value) {
83 return value * value;
84 }
85
86 /**
87 * Returns absolute value
88 * @param value value
89 * @return absolute value
90 */
91 inline static int32_t abs(int32_t value) {
92 return std::abs(value);
93 }
94
95 /**
96 * Returns absolute value
97 * @param value value
98 * @return absolute value
99 */
100 inline static int64_t abs(int64_t value) {
101 return std::abs(value);
102 }
103
104 /**
105 * Returns absolute value
106 * @param value value
107 * @return absolute value
108 */
109 inline static float abs(float value) {
110 return std::fabs(value);
111 }
112
113 /**
114 * Returns the arc cosine of a value
115 * @param value value
116 * @return arc cosine
117 */
118 inline static float acos(float value) {
119 return std::acos(value);
120 }
121
122 /**
123 * Returns the arc sine of a value
124 * @param value value
125 * @return arc sine
126 */
127 inline static float asin(float value) {
128 return std::asin(value);
129 }
130
131 /**
132 * Returns the arc tangent of a value
133 * @param value value
134 * @return arc tangent
135 */
136 inline static float atan(float value) {
137 return std::atan(value);
138 }
139
140 /**
141 * Returns the angle from the conversion of rectangular coordinates to polar coordinates.
142 * @param y y
143 * @param x x
144 * @return angle
145 *
146 */
147 inline static float atan2(float y, float x) {
148 return std::atan2(y, x);
149 }
150
151 /**
152 * Returns the higher integer value of given value
153 * @param value value
154 * @return higher integer
155 */
156 inline static float ceil(float value) {
157 return std::ceil(value);
158 }
159
160 /**
161 * Returns the cosine of an angle
162 * @param value value
163 * @return cosine
164 */
165 inline static float cos(float value) {
166 return std::cos(value);
167 }
168
169 /**
170 * Returns the lower integer value of given value
171 * @param value value
172 * @return lower integer
173 */
174 inline static float floor(float value) {
175 return std::floor(value);
176 }
177
178 /**
179 * Returns the higher value of given values
180 * @param value1 value 1
181 * @param value2 value 2
182 * @return higher value
183 */
184 inline static int32_t max(int32_t value1, int32_t value2) {
185 return value1 > value2?value1:value2;
186 }
187
188 /**
189 * Returns the higher value of given values
190 * @param value1 value 1
191 * @param value2 value 2
192 * @return higher value
193 */
194 inline static int64_t max(int64_t value1, int64_t value2) {
195 return value1 > value2?value1:value2;
196 }
197
198 /**
199 * Returns the higher value of given values
200 * @param value1 value 1
201 * @param value2 value 2
202 * @return higher value
203 */
204 inline static float max(float value1, float value2) {
205 return value1 > value2?value1:value2;
206 }
207
208 /**
209 * Returns the lesser value of given values
210 * @param value1 value 1
211 * @param value2 value 2
212 * @return lesser value
213 */
214 inline static int32_t min(int32_t value1, int32_t value2) {
215 return value1 < value2?value1:value2;
216 }
217
218 /**
219 * Returns the lesser value of given values
220 * @param value1 value 1
221 * @param value2 value 2
222 * @return lesser value
223 */
224 inline static int32_t min(int64_t value1, int64_t value2) {
225 return value1 < value2?value1:value2;
226 }
227
228 /**
229 * Returns the lesser value of given values
230 * @param value1 value 1
231 * @param value2 value 2
232 * @return lesser value
233 */
234 inline static float min(float value1, float value2) {
235 return value1 < value2?value1:value2;
236 }
237
238 /**
239 * Returns the rounded value of given float value
240 * @param value value
241 * @return rounded value
242 */
243 inline static float round(float value) {
244 return std::round(value);
245 }
246
247 /**
248 * Returns the value of the value 1 raised to the power of value2
249 * @param value1 value 1
250 * @param value2 value 2
251 * @return pow
252 */
253 inline static int32_t pow(int32_t value1, int32_t value2) {
254 return std::pow(value1, value2);
255 }
256
257 /**
258 * Returns the value of the value 1 raised to the power of value2
259 * @param value1 value 1
260 * @param value2 value 2
261 * @return pow
262 */
263 inline static float pow(float value1, float value2) {
264 return std::pow(value1, value2);
265 }
266
267 /**
268 * Returns a random value between 0.0 .. 1.0
269 * @return random value
270 */
271 inline static float random() {
272 return (float)rand() / (float)RAND_MAX;
273 }
274
275 /**
276 * Returns the sine of an angle
277 * @param value value
278 * @return sine
279 */
280 inline static float sin(float value) {
281 return std::sin(value);
282 }
283
284 /**
285 * Returns the square of given value
286 * @param value value
287 * @return squaere
288 */
289 inline static float sqrt(float value) {
290 return std::sqrt(value);
291 }
292
293 /**
294 * Returns the tangent of an angle
295 * @param value value
296 * @return tangent
297 */
298 inline static float tan(float value) {
299 return std::tan(value);
300 }
301
302 /**
303 * Returns e raised to the given power
304 * @param value value
305 * @return e
306 */
307 inline static float exp(float value) {
308 return std::exp(value);
309 }
310
311 /**
312 * Returns the natural (base e) logarithm of value
313 * @param value value
314 * @return log
315 */
316 inline static float log(float value) {
317 return std::log(value);
318 }
319
320 /**
321 * Returns modulo of value, so that return value is -range < value < range
322 * @param value value
323 * @param range range
324 * @return modulo of value
325 */
326 inline static float mod(float value, float range) {
327 return std::fmod(value, range);
328 }
329
330 /**
331 * Returns absolute modulo of value, so that return value is 0.0f <= value < range
332 * @param value value
333 * @param range range
334 * @return modulo of value
335 */
336 inline static float absmod(float value, float range) {
337 while (value < 0.0f) value+= range;
338 return std::fmod(value, range);
339 }
340
341};
Standard math functions.
Definition: Math.h:21
static int32_t max(int32_t value1, int32_t value2)
Returns the higher value of given values.
Definition: Math.h:184
static float sqrt(float value)
Returns the square of given value.
Definition: Math.h:289
static float log(float value)
Returns the natural (base e) logarithm of value.
Definition: Math.h:316
static float max(float value1, float value2)
Returns the higher value of given values.
Definition: Math.h:204
static float round(float value)
Returns the rounded value of given float value.
Definition: Math.h:243
static int clamp(int value, int min, int max)
Clamps a int value to min or max value.
Definition: Math.h:36
static float tan(float value)
Returns the tangent of an angle.
Definition: Math.h:298
static int64_t max(int64_t value1, int64_t value2)
Returns the higher value of given values.
Definition: Math.h:194
static float square(float value)
Do the square product.
Definition: Math.h:82
static float acos(float value)
Returns the arc cosine of a value.
Definition: Math.h:118
static float cos(float value)
Returns the cosine of an angle.
Definition: Math.h:165
static float min(float value1, float value2)
Returns the lesser value of given values.
Definition: Math.h:234
static float clamp(float value, float min, float max)
Clamps a float value to min or max value.
Definition: Math.h:62
static int64_t abs(int64_t value)
Returns absolute value.
Definition: Math.h:100
static float floor(float value)
Returns the lower integer value of given value.
Definition: Math.h:174
static float asin(float value)
Returns the arc sine of a value.
Definition: Math.h:127
static constexpr float EPSILON
Definition: Math.h:25
static int32_t pow(int32_t value1, int32_t value2)
Returns the value of the value 1 raised to the power of value2.
Definition: Math.h:253
static float abs(float value)
Returns absolute value.
Definition: Math.h:109
static float sign(float value)
Returns sign of value.
Definition: Math.h:73
static constexpr float E
Definition: Math.h:23
static float random()
Returns a random value between 0.0 .
Definition: Math.h:271
static int64_t clamp(int64_t value, int64_t min, int64_t max)
Clamps a 64 bit int value to min or max value.
Definition: Math.h:49
static float exp(float value)
Returns e raised to the given power.
Definition: Math.h:307
static int32_t abs(int32_t value)
Returns absolute value.
Definition: Math.h:91
static int32_t min(int64_t value1, int64_t value2)
Returns the lesser value of given values.
Definition: Math.h:224
static float atan(float value)
Returns the arc tangent of a value.
Definition: Math.h:136
static float absmod(float value, float range)
Returns absolute modulo of value, so that return value is 0.0f <= value < range.
Definition: Math.h:336
static float atan2(float y, float x)
Returns the angle from the conversion of rectangular coordinates to polar coordinates.
Definition: Math.h:147
static int32_t min(int32_t value1, int32_t value2)
Returns the lesser value of given values.
Definition: Math.h:214
static constexpr float PI
Definition: Math.h:24
static float pow(float value1, float value2)
Returns the value of the value 1 raised to the power of value2.
Definition: Math.h:263
static float mod(float value, float range)
Returns modulo of value, so that return value is -range < value < range.
Definition: Math.h:326
static float ceil(float value)
Returns the higher integer value of given value.
Definition: Math.h:156
static constexpr float DEG2RAD
Definition: Math.h:26
static constexpr float g
Definition: Math.h:27
static float sin(float value)
Returns the sine of an angle.
Definition: Math.h:280