ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Mathematics.cpp
Go to the documentation of this file.
1#include "Mathematics.h"
2
3const float Mathematics::EPSILON = 0.001f;
4const float Mathematics::MPI = 3.14159265358979323846f;
5const float Mathematics::MPID180 = 0.01745329251994329576f;
6const float Mathematics::M180DPI = 57.29577951308232087684f;
9
13
15 return value != value;
16}
17
19 return std::isinf(value);
20}
21
23 return std::isfinite(value);
24}
25
26bool Mathematics::IsClose(float v1, float v2, float epsilon) {
27 return std::fabs(v1 - v2) < epsilon;
28}
29
31 return (0 < value) - (value < 0);
32}
33
34float Mathematics::Pow(float value, float exponent) {
35 return powf(value, exponent);
36}
37
39 return sqrtf(value);
40}
41
43 return value - std::floor(value);
44}
45
46float Mathematics::CosineInterpolation(float beg, float fin, float ratio) {
47 float mu2 = (1.0f - cosf(ratio * MPI)) / 2.0f;
48
49 return (beg * (1.0f - mu2) + fin * mu2);
50}
51
52float Mathematics::CosineTransition(float beg, float fin, float ratio) {
53 float mid = (beg - fin) / 2.0f;
54
55 if (ratio < mid) return CosineInterpolation(beg, fin, 1.0f - ratio * 2.0f);
56 else return CosineInterpolation(fin, beg, (ratio - 0.5f) * 2.0f);
57}
58
59float Mathematics::BounceInterpolation(float beg, float fin, float ratio) {
60 float baseLog = log10f(10.0f * ratio + 1.0f);
61 float baseSine = sinf(16.0f * ratio) * powf((2.0f * ratio - 2.0f), 2.0f) / 4.0f / 4.0f;
62 float bounce = baseLog + baseSine;
63
64 return Map(ratio, 0.0f, bounce, beg, fin);
65}
66
67float Mathematics::FFloor(float f) {
68 int fi = (int)f; return f < fi ? fi - 1 : fi;
69}
70
71float Mathematics::FAbs(float f) {
72 return f < 0 ? -f : f;
73}
74
75float Mathematics::FSqrt(float f) {
76 return sqrtf(f);
77}
78
80 return t * t * (3 - 2 * t);
81}
82
84 return t * t * t * (t * (t * 6 - 15) + 10);
85}
86
87float Mathematics::Lerp(float a, float b, float t) {
88 return a + t * (b - a);
89}
90
91float Mathematics::CubicLerp(float a, float b, float c, float d, float t){
92 float p = (d - c) - (a - b);
93 float q = (a - b) - p;
94 float r = c - a;
95 float s = b;
96
97 return p * t * t * t + q * t * t + r * t + s;
98}
99
100float Mathematics::BilinearInterpolation(float scaleX, float scaleY, float x1, float y1, float x2, float y2, float v11, float v12, float v21, float v22) {
101 // Calculate the interpolation weights
102 float s1 = x2 - scaleX;
103 float s2 = scaleX - x1;
104 float t1 = y2 - scaleY;
105 float t2 = scaleY - y1;
106
107 // Perform bilinear interpolation
108 float interpolatedValue = (s1 * t1 * v11 + s2 * t1 * v21 + s1 * t2 * v12 + s2 * t2 * v22) / ((x2 - x1) * (y2 - y1));
109
110 return interpolatedValue;
111}
112
113
115 t -= (int)(t * 0.5f) * 2;
116 return t < 1 ? t : 2 - t;
117}
118
120 if (multiple == 0)
121 return value;
122
123 int remainder = abs(value) % multiple;
124 if (remainder == 0)
125 return value;
126
127 if (value < 0)
128 return -(std::abs(value) - remainder);
129 else
130 return value + multiple - remainder;
131}
Provides a collection of mathematical utility functions and constants.
Implements a generic Kalman Filter for 1D data.
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 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 float Lerp(float a, float b, float t)
Linear interpolation between two values.
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 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 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 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).