ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Mathematics.h
Go to the documentation of this file.
1/**
2 * @file Mathematics.h
3 * @brief Provides a collection of mathematical utility functions and constants.
4 *
5 * This header defines the Mathematics class, containing static methods and constants
6 * useful for a variety of common mathematical operations, including interpolation,
7 * range mapping, and basic trigonometric utilities.
8 *
9 * @date 22/12/2024
10 * @version 1.0
11 * @author Coela Can't
12 */
13
14#pragma once
15
16#include <cmath>
17#include <math.h>
18#include <WString.h>
19
20/**
21 * @class Mathematics
22 * @brief A static utility class for mathematical operations commonly used
23 * throughout embedded or real-time applications.
24 *
25 * This class provides:
26 * - Constants (e.g., \c EPSILON, \c MPI, \c FLTMAX).
27 * - Methods for numeric checks (NaN, Infinity, etc.).
28 * - Interpolation methods (Lerp, Cosine, Bounce, Cubic).
29 * - Mapping and constraint utilities for data normalization.
30 * - Basic arithmetic wrappers (Pow, Sqrt, etc.).
31 */
33public:
34 /**
35 * @brief A small constant used for floating-point comparisons.
36 */
37 static const float EPSILON;
38
39 /**
40 * @brief Mathematical constant \f$\pi\f$ (3.14159265358979323846...).
41 */
42 static const float MPI;
43
44 /**
45 * @brief The value of \f$\pi \div 180.0\f$, useful for converting degrees to radians.
46 */
47 static const float MPID180;
48
49 /**
50 * @brief The value of \f$180.0 \div \pi\f$, useful for converting radians to degrees.
51 */
52 static const float M180DPI;
53
54 /**
55 * @brief Maximum float value (shortcut to a large number).
56 */
57 static const float FLTMAX;
58
59 /**
60 * @brief Minimum float value (shortcut to a very small or near-zero number).
61 */
62 static const float FLTMIN;
63
64 /**
65 * @brief Converts a floating-point value to a String, removing trailing decimals if not needed.
66 * @param value The float to convert.
67 * @return A \c String without unnecessary trailing zeros.
68 */
69 static String DoubleToCleanString(float value);
70
71 /**
72 * @brief Checks if a floating-point value is NaN (Not a Number).
73 * @param value The float value to check.
74 * @return \c true if \p value is NaN, otherwise \c false.
75 */
76 static bool IsNaN(float value);
77
78 /**
79 * @brief Checks if a floating-point value is infinite.
80 * @param value The float value to check.
81 * @return \c true if \p value is infinite, otherwise \c false.
82 */
83 static bool IsInfinite(float value);
84
85 /**
86 * @brief Checks if a floating-point value is finite.
87 * @param value The float value to check.
88 * @return \c true if finite, otherwise \c false.
89 */
90 static bool IsFinite(float value);
91
92 /**
93 * @brief Checks if two values are close within a specified epsilon.
94 * @param v1 The first value.
95 * @param v2 The second value.
96 * @param epsilon The tolerance for comparison.
97 * @return \c true if \p v1 and \p v2 differ by less than \p epsilon.
98 */
99 static bool IsClose(float v1, float v2, float epsilon);
100
101 /**
102 * @brief Determines the sign of a floating-point value.
103 * @param value The float to check.
104 * @return \c +1 if \p value > 0, \c -1 if \p value < 0, and \c 0 if \p value == 0.
105 */
106 static int Sign(float value);
107
108 /**
109 * @brief Raises a value to a given exponent.
110 * @param value The base value.
111 * @param exponent The exponent to raise by.
112 * @return \p value raised to \p exponent.
113 */
114 static float Pow(float value, float exponent);
115
116 /**
117 * @brief Computes the square root of a value.
118 * @param value The float to take the square root of.
119 * @return The square root of \p value.
120 */
121 static float Sqrt(float value);
122
123 /**
124 * @brief Returns the fractional part of a floating-point value.
125 *
126 * For example, Fract(3.75) = 0.75.
127 *
128 * @param value The float to evaluate.
129 * @return The fractional component of \p value.
130 */
131 static float Fract(float value);
132
133 /**
134 * @brief Applies a cosine-based interpolation between two values.
135 * @param beg The start value.
136 * @param fin The end value.
137 * @param ratio A normalized ratio (0 to 1).
138 * @return The interpolated value.
139 *
140 * @see CosineTransition
141 */
142 static float CosineInterpolation(float beg, float fin, float ratio);
143
144 /**
145 * @brief A convenience alias for \c CosineInterpolation.
146 * @param beg The start value.
147 * @param fin The end value.
148 * @param ratio A normalized ratio (0 to 1).
149 * @return The interpolated value.
150 *
151 * @see CosineInterpolation
152 */
153 static float CosineTransition(float beg, float fin, float ratio);
154
155 /**
156 * @brief A 'bounce'-like interpolation between two values.
157 * @param beg The start value.
158 * @param fin The end value.
159 * @param ratio A normalized ratio (0 to 1).
160 * @return The interpolated value, with a "bounce" effect near the end.
161 */
162 static float BounceInterpolation(float beg, float fin, float ratio);
163
164 /**
165 * @brief Floors a floating-point value (rounds down).
166 * @param f The float to floor.
167 * @return The greatest integer less than or equal to \p f.
168 */
169 static float FFloor(float f);
170
171 /**
172 * @brief Returns the absolute value of a floating-point number.
173 * @param f The float to evaluate.
174 * @return \c f if \c f >= 0, otherwise -\c f.
175 */
176 static float FAbs(float f);
177
178 /**
179 * @brief Computes the square root of a value (internal wrapper for \c std::sqrt).
180 * @param f The float to take the square root of.
181 * @return The square root of \p f.
182 */
183 static float FSqrt(float f);
184
185 /**
186 * @brief Hermite interpolation function for smooth transitions.
187 * @param t A normalized value (0 to 1).
188 * @return The Hermite-interpolated value.
189 */
190 static float HermiteInterpolation(float t);
191
192 /**
193 * @brief Quintic interpolation function for smooth transitions.
194 * @param t A normalized value (0 to 1).
195 * @return The quintic-interpolated value.
196 */
197 static float QuinticInterpolation(float t);
198
199 /**
200 * @brief Linear interpolation between two values.
201 * @param a The start value.
202 * @param b The end value.
203 * @param t A normalized ratio (0 to 1).
204 * @return (1 - t)*a + t*b
205 */
206 static float Lerp(float a, float b, float t);
207
208 /**
209 * @brief Cubic interpolation across four points (a, b, c, d).
210 * @param a First point.
211 * @param b Second point (start).
212 * @param c Third point (end).
213 * @param d Fourth point.
214 * @param t A normalized ratio (0 to 1).
215 * @return The cubic interpolation result.
216 */
217 static float CubicLerp(float a, float b, float c, float d, float t);
218
219 /**
220 * @brief Performs a bilinear interpolation on a 2D grid.
221 *
222 * This is often used for texture sampling or heightmap sampling.
223 *
224 * @param scaleX Normalized X interpolation factor (0 to 1).
225 * @param scaleY Normalized Y interpolation factor (0 to 1).
226 * @param x1 Left X coordinate.
227 * @param y1 Bottom Y coordinate.
228 * @param x2 Right X coordinate.
229 * @param y2 Top Y coordinate.
230 * @param v11 Value at (x1, y1).
231 * @param v12 Value at (x1, y2).
232 * @param v21 Value at (x2, y1).
233 * @param v22 Value at (x2, y2).
234 * @return The interpolated value.
235 */
236 static float BilinearInterpolation(float scaleX, float scaleY, float x1, float y1, float x2, float y2, float v11, float v12, float v21, float v22);
237
238 /**
239 * @brief Repeats a value between 0 and 1, then mirrors back and forth (like a ping-pong).
240 *
241 * For example, PingPong(t) toggles between [0..1..0..1...] as \p t increases.
242 *
243 * @param t The time or input value.
244 * @return A float between 0 and 1, repeating in a ping-pong pattern.
245 */
246 static float PingPong(float t);
247
248 /**
249 * @brief Rounds \p value up to the nearest multiple of \p multiple.
250 * @param value The integer to round up.
251 * @param multiple The multiple to round to.
252 * @return \p value rounded up to the nearest multiple of \p multiple.
253 */
254 static int RoundUpWindow(int value, int multiple);
255
256 /**
257 * @brief Constrains a value between minimum and maximum.
258 * @tparam T A numeric type (e.g., int, float).
259 * @param value The value to constrain.
260 * @param minimum The lower bound.
261 * @param maximum The upper bound.
262 * @return The constrained value within [minimum, maximum].
263 */
264 template<typename T>
265 static T Constrain(T value, T minimum, T maximum);
266
267 /**
268 * @brief Converts degrees to radians.
269 * @tparam T A numeric type.
270 * @param degrees The angle in degrees.
271 * @return The angle in radians.
272 */
273 template<typename T>
275
276 /**
277 * @brief Converts radians to degrees.
278 * @tparam T A numeric type.
279 * @param radians The angle in radians.
280 * @return The angle in degrees.
281 */
282 template<typename T>
284
285 /**
286 * @brief Maps a value from one range to another.
287 * @tparam T A numeric type.
288 * @param value The value to map.
289 * @param inLow The lower bound of the input range.
290 * @param inMax The upper bound of the input range.
291 * @param outMin The lower bound of the output range.
292 * @param outMax The upper bound of the output range.
293 * @return The mapped value within [outMin, outMax].
294 */
295 template<typename T>
297
298 /**
299 * @brief Returns the maximum of two values.
300 * @tparam T A numeric type.
301 * @param value1 The first value.
302 * @param value2 The second value.
303 * @return \c value1 if \c value1 >= \c value2, otherwise \c value2.
304 */
305 template <typename T>
306 static T Max(T value1, T value2);
307
308 /**
309 * @brief Returns the minimum of two values.
310 * @tparam T A numeric type.
311 * @param value1 The first value.
312 * @param value2 The second value.
313 * @return \c value1 if \c value1 <= \c value2, otherwise \c value2.
314 */
315 template <typename T>
316 static T Min(T value1, T value2);
317
318 /**
319 * @brief Returns the minimum of three values.
320 * @tparam T A numeric type.
321 * @param v1 The first value.
322 * @param v2 The second value.
323 * @param v3 The third value.
324 * @return The smallest of \p v1, \p v2, and \p v3.
325 */
326 template <typename T>
327 static T Min(T v1, T v2, T v3);
328
329 /**
330 * @brief Returns the maximum of three values.
331 * @tparam T A numeric type.
332 * @param v1 The first value.
333 * @param v2 The second value.
334 * @param v3 The third value.
335 * @return The largest of \p v1, \p v2, and \p v3.
336 */
337 template <typename T>
338 static T Max(T v1, T v2, T v3);
339
340 /**
341 * @brief Combines Constrain and Map in one step:
342 * first maps \p value from [inLow, inMax] to [outMin, outMax],
343 * then constrains it to stay within [outMin, outMax].
344 * @tparam T A numeric type.
345 * @param value The value to map/constrain.
346 * @param inLow The lower bound of the input range.
347 * @param inMax The upper bound of the input range.
348 * @param outMin The lower bound of the output range.
349 * @param outMax The upper bound of the output range.
350 * @return The mapped value, constrained between \p outMin and \p outMax.
351 */
352 template<typename T>
354
355};
356
357// Pull in template implementations.
358#include "Mathematics.tpp"
Implements a generic Kalman Filter for 1D data.
A static utility class for mathematical operations commonly used throughout embedded or real-time app...
Definition Mathematics.h:32
static T RadiansToDegrees(T radians)
Converts radians to degrees.
static int Sign(float value)
Determines the sign of a floating-point value.
static float BilinearInterpolation(float scaleX, float scaleY, float x1, float y1, float x2, float y2, float v11, float v12, float v21, float v22)
Performs a bilinear interpolation on a 2D grid.
static T Max(T v1, T v2, T v3)
Returns the maximum of three values.
static T Min(T v1, T v2, T v3)
Returns the minimum of three values.
static float Sqrt(float value)
Computes the square root of a value.
static const float FLTMIN
Minimum float value (shortcut to a very small or near-zero number).
Definition Mathematics.h:62
static const float MPI
Mathematical constant (3.14159265358979323846...).
Definition Mathematics.h:42
static float HermiteInterpolation(float t)
Hermite interpolation function for smooth transitions.
static T Min(T value1, T value2)
Returns the minimum of two values.
static float Lerp(float a, float b, float t)
Linear interpolation between two values.
static T ConstrainMap(T value, T inLow, T inMax, T outMin, T outMax)
Combines Constrain and Map in one step: first maps value from [inLow, inMax] to [outMin,...
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static String DoubleToCleanString(float value)
Converts a floating-point value to a String, removing trailing decimals if not needed.
static T Max(T value1, T value2)
Returns the maximum of two values.
static const float FLTMAX
Maximum float value (shortcut to a large number).
Definition Mathematics.h:57
static float CosineTransition(float beg, float fin, float ratio)
A convenience alias for CosineInterpolation.
static bool IsFinite(float value)
Checks if a floating-point value is finite.
static T Map(T value, T inLow, T inMax, T outMin, T outMax)
Maps a value from one range to another.
static float QuinticInterpolation(float t)
Quintic interpolation function for smooth transitions.
static float Fract(float value)
Returns the fractional part of a floating-point value.
static int RoundUpWindow(int value, int multiple)
Rounds value up to the nearest multiple of multiple.
static float Pow(float value, float exponent)
Raises a value to a given exponent.
static float CubicLerp(float a, float b, float c, float d, float t)
Cubic interpolation across four points (a, b, c, d).
static float FSqrt(float f)
Computes the square root of a value (internal wrapper for std::sqrt).
static T DegreesToRadians(T degrees)
Converts degrees to radians.
static float FAbs(float f)
Returns the absolute value of a floating-point number.
static const float MPID180
The value of , useful for converting degrees to radians.
Definition Mathematics.h:47
static bool IsInfinite(float value)
Checks if a floating-point value is infinite.
static const float EPSILON
A small constant used for floating-point comparisons.
Definition Mathematics.h:37
static T Constrain(T value, T minimum, T maximum)
Constrains a value between minimum and maximum.
static const float M180DPI
The value of , useful for converting radians to degrees.
Definition Mathematics.h:52
static float CosineInterpolation(float beg, float fin, float ratio)
Applies a cosine-based interpolation between two values.
static float BounceInterpolation(float beg, float fin, float ratio)
A 'bounce'-like interpolation between two values.
static float FFloor(float f)
Floors a floating-point value (rounds down).
static bool IsNaN(float value)
Checks if a floating-point value is NaN (Not a Number).
static float PingPong(float t)
Repeats a value between 0 and 1, then mirrors back and forth (like a ping-pong).