ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Mathematics Class Reference

A static utility class for mathematical operations commonly used throughout embedded or real-time applications. More...

#include <Mathematics.h>

Static Public Member Functions

static String DoubleToCleanString (float value)
 Converts a floating-point value to a String, removing trailing decimals if not needed.
 
static bool IsNaN (float value)
 Checks if a floating-point value is NaN (Not a Number).
 
static bool IsInfinite (float value)
 Checks if a floating-point value is infinite.
 
static bool IsFinite (float value)
 Checks if a floating-point value is finite.
 
static bool IsClose (float v1, float v2, float epsilon)
 Checks if two values are close within a specified epsilon.
 
static int Sign (float value)
 Determines the sign of a floating-point value.
 
static float Pow (float value, float exponent)
 Raises a value to a given exponent.
 
static float Sqrt (float value)
 Computes the square root of a value.
 
static float Fract (float value)
 Returns the fractional part of a floating-point value.
 
static float CosineInterpolation (float beg, float fin, float ratio)
 Applies a cosine-based interpolation between two values.
 
static float CosineTransition (float beg, float fin, float ratio)
 A convenience alias for CosineInterpolation.
 
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 float FAbs (float f)
 Returns the absolute value of a floating-point number.
 
static float FSqrt (float f)
 Computes the square root of a value (internal wrapper for std::sqrt).
 
static float HermiteInterpolation (float t)
 Hermite interpolation function for smooth transitions.
 
static float QuinticInterpolation (float t)
 Quintic interpolation function for smooth transitions.
 
static float Lerp (float a, float b, float t)
 Linear interpolation between two values.
 
static float CubicLerp (float a, float b, float c, float d, float t)
 Cubic interpolation across four points (a, b, c, d).
 
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 PingPong (float t)
 Repeats a value between 0 and 1, then mirrors back and forth (like a ping-pong).
 
static int RoundUpWindow (int value, int multiple)
 Rounds value up to the nearest multiple of multiple.
 
template<typename T >
static T Constrain (T value, T minimum, T maximum)
 Constrains a value between minimum and maximum.
 
template<typename T >
static T DegreesToRadians (T degrees)
 Converts degrees to radians.
 
template<typename T >
static T RadiansToDegrees (T radians)
 Converts radians to degrees.
 
template<typename T >
static T Map (T value, T inLow, T inMax, T outMin, T outMax)
 Maps a value from one range to another.
 
template<typename T >
static T Max (T value1, T value2)
 Returns the maximum of two values.
 
template<typename T >
static T Min (T value1, T value2)
 Returns the minimum of two values.
 
template<typename T >
static T Min (T v1, T v2, T v3)
 Returns the minimum of three values.
 
template<typename T >
static T Max (T v1, T v2, T v3)
 Returns the maximum of three values.
 
template<typename T >
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, outMax], then constrains it to stay within [outMin, outMax].
 

Static Public Attributes

static const float EPSILON = 0.001f
 A small constant used for floating-point comparisons.
 
static const float MPI = 3.14159265358979323846f
 Mathematical constant \(\pi\) (3.14159265358979323846...).
 
static const float MPID180 = 0.01745329251994329576f
 The value of \(\pi \div 180.0\), useful for converting degrees to radians.
 
static const float M180DPI = 57.29577951308232087684f
 The value of \(180.0 \div \pi\), useful for converting radians to degrees.
 
static const float FLTMAX = __FLT_MAX__
 Maximum float value (shortcut to a large number).
 
static const float FLTMIN = __FLT_MIN__
 Minimum float value (shortcut to a very small or near-zero number).
 

Detailed Description

A static utility class for mathematical operations commonly used throughout embedded or real-time applications.

This class provides:

  • Constants (e.g., EPSILON, MPI, FLTMAX).
  • Methods for numeric checks (NaN, Infinity, etc.).
  • Interpolation methods (Lerp, Cosine, Bounce, Cubic).
  • Mapping and constraint utilities for data normalization.
  • Basic arithmetic wrappers (Pow, Sqrt, etc.).

Definition at line 32 of file Mathematics.h.

Member Function Documentation

◆ BilinearInterpolation()

float BilinearInterpolation ( float  scaleX,
float  scaleY,
float  x1,
float  y1,
float  x2,
float  y2,
float  v11,
float  v12,
float  v21,
float  v22 
)
static

Performs a bilinear interpolation on a 2D grid.

This is often used for texture sampling or heightmap sampling.

Parameters
scaleXNormalized X interpolation factor (0 to 1).
scaleYNormalized Y interpolation factor (0 to 1).
x1Left X coordinate.
y1Bottom Y coordinate.
x2Right X coordinate.
y2Top Y coordinate.
v11Value at (x1, y1).
v12Value at (x1, y2).
v21Value at (x2, y1).
v22Value at (x2, y2).
Returns
The interpolated value.

Definition at line 100 of file Mathematics.cpp.

Referenced by VectorField2D::GetVectorAtPosition().

◆ BounceInterpolation()

float BounceInterpolation ( float  beg,
float  fin,
float  ratio 
)
static

A 'bounce'-like interpolation between two values.

Parameters
begThe start value.
finThe end value.
ratioA normalized ratio (0 to 1).
Returns
The interpolated value, with a "bounce" effect near the end.

Definition at line 59 of file Mathematics.cpp.

References Map().

◆ Constrain()

◆ ConstrainMap()

template<typename T >
static T ConstrainMap ( T  value,
T  inLow,
T  inMax,
T  outMin,
T  outMax 
)
static

Combines Constrain and Map in one step: first maps value from [inLow, inMax] to [outMin, outMax], then constrains it to stay within [outMin, outMax].

Template Parameters
TA numeric type.
Parameters
valueThe value to map/constrain.
inLowThe lower bound of the input range.
inMaxThe upper bound of the input range.
outMinThe lower bound of the output range.
outMaxThe upper bound of the output range.
Returns
The mapped value, constrained between outMin and outMax.

Referenced by HeadsUpDisplay::EnableBitFaceRender().

◆ CosineInterpolation()

float CosineInterpolation ( float  beg,
float  fin,
float  ratio 
)
static

Applies a cosine-based interpolation between two values.

Parameters
begThe start value.
finThe end value.
ratioA normalized ratio (0 to 1).
Returns
The interpolated value.
See also
CosineTransition

Definition at line 46 of file Mathematics.cpp.

References MPI.

Referenced by TriangleGroupDeformer::CosineInterpolationDeformer(), ObjectDeformer::CosineInterpolationDeformer(), CosineTransition(), Crash::FadeIn(), Crash::FadeOut(), AudioReactiveGradient::GetRGB(), Oscilloscope::GetRGB(), SpectrumAnalyzer::GetRGB(), and Boot::Update().

◆ CosineTransition()

float CosineTransition ( float  beg,
float  fin,
float  ratio 
)
static

A convenience alias for CosineInterpolation.

Parameters
begThe start value.
finThe end value.
ratioA normalized ratio (0 to 1).
Returns
The interpolated value.
See also
CosineInterpolation

Definition at line 52 of file Mathematics.cpp.

References CosineInterpolation().

◆ CubicLerp()

float CubicLerp ( float  a,
float  b,
float  c,
float  d,
float  t 
)
static

Cubic interpolation across four points (a, b, c, d).

Parameters
aFirst point.
bSecond point (start).
cThird point (end).
dFourth point.
tA normalized ratio (0 to 1).
Returns
The cubic interpolation result.

Definition at line 91 of file Mathematics.cpp.

◆ DegreesToRadians()

template<typename T >
static T DegreesToRadians ( T  degrees)
static

Converts degrees to radians.

Template Parameters
TA numeric type.
Parameters
degreesThe angle in degrees.
Returns
The angle in radians.

Referenced by Rotation::AxisAngleToQuaternion(), Rotation::EulerAnglesToQuaternion(), RotationMatrix::RotateX(), RotationMatrix::RotateY(), and RotationMatrix::RotateZ().

◆ DoubleToCleanString()

String DoubleToCleanString ( float  value)
static

Converts a floating-point value to a String, removing trailing decimals if not needed.

Parameters
valueThe float to convert.
Returns
A String without unnecessary trailing zeros.

Definition at line 10 of file Mathematics.cpp.

Referenced by AxisAngle::ToString(), DirectionAngle::ToString(), Quaternion::ToString(), Vector2D::ToString(), Vector3D::ToString(), and YawPitchRoll::ToString().

◆ FAbs()

float FAbs ( float  f)
static

Returns the absolute value of a floating-point number.

Parameters
fThe float to evaluate.
Returns
f if f >= 0, otherwise -f.

Definition at line 71 of file Mathematics.cpp.

◆ FFloor()

float FFloor ( float  f)
static

Floors a floating-point value (rounds down).

Parameters
fThe float to floor.
Returns
The greatest integer less than or equal to f.

Definition at line 67 of file Mathematics.cpp.

◆ Fract()

float Fract ( float  value)
static

Returns the fractional part of a floating-point value.

For example, Fract(3.75) = 0.75.

Parameters
valueThe float to evaluate.
Returns
The fractional component of value.

Definition at line 42 of file Mathematics.cpp.

Referenced by SpiralMaterial::GetRGB().

◆ FSqrt()

float FSqrt ( float  f)
static

Computes the square root of a value (internal wrapper for std::sqrt).

Parameters
fThe float to take the square root of.
Returns
The square root of f.

Definition at line 75 of file Mathematics.cpp.

◆ HermiteInterpolation()

float HermiteInterpolation ( float  t)
static

Hermite interpolation function for smooth transitions.

Parameters
tA normalized value (0 to 1).
Returns
The Hermite-interpolated value.

Definition at line 79 of file Mathematics.cpp.

◆ IsClose()

bool IsClose ( float  v1,
float  v2,
float  epsilon 
)
static

Checks if two values are close within a specified epsilon.

Parameters
v1The first value.
v2The second value.
epsilonThe tolerance for comparison.
Returns
true if v1 and v2 differ by less than epsilon.

Definition at line 26 of file Mathematics.cpp.

Referenced by DampedSpring::Calculate(), Quaternion::Divide(), Vector2D::Divide(), RampFilter::Filter(), ObjectAlign::GetPlaneOrientation(), AudioReactiveGradient::GetRGB(), Oscilloscope::GetRGB(), SpectrumAnalyzer::GetRGB(), VectorField2D::GetVectorAtPosition(), Quaternion::MultiplicativeInverse(), Quaternion::Multiply(), Vector2D::Multiply(), Vector2D::Normal(), Vector3D::Normal(), Vector2D::UnitCircle(), and Vector3D::UnitSphere().

◆ IsFinite()

bool IsFinite ( float  value)
static

Checks if a floating-point value is finite.

Parameters
valueThe float value to check.
Returns
true if finite, otherwise false.

Definition at line 22 of file Mathematics.cpp.

Referenced by Quaternion::IsInfinite().

◆ IsInfinite()

bool IsInfinite ( float  value)
static

Checks if a floating-point value is infinite.

Parameters
valueThe float value to check.
Returns
true if value is infinite, otherwise false.

Definition at line 18 of file Mathematics.cpp.

Referenced by Quaternion::IsFinite().

◆ IsNaN()

bool IsNaN ( float  value)
static

Checks if a floating-point value is NaN (Not a Number).

Parameters
valueThe float value to check.
Returns
true if value is NaN, otherwise false.

Definition at line 14 of file Mathematics.cpp.

Referenced by Quaternion::IsNaN().

◆ Lerp()

float Lerp ( float  a,
float  b,
float  t 
)
static

Linear interpolation between two values.

Parameters
aThe start value.
bThe end value.
tA normalized ratio (0 to 1).
Returns
(1 - t)*a + t*b

Definition at line 87 of file Mathematics.cpp.

◆ Map()

◆ Max() [1/2]

template<typename T >
static T Max ( T  v1,
T  v2,
T  v3 
)
static

Returns the maximum of three values.

Template Parameters
TA numeric type.
Parameters
v1The first value.
v2The second value.
v3The third value.
Returns
The largest of v1, v2, and v3.

◆ Max() [2/2]

template<typename T >
static T Max ( T  value1,
T  value2 
)
static

Returns the maximum of two values.

Template Parameters
TA numeric type.
Parameters
value1The first value.
value2The second value.
Returns
value1 if value1 >= value2, otherwise value2.

Referenced by Vector3D::AverageHighestTwoComponents(), Triangle2D::DidIntersectSAT(), ObjectAlign::GetPlaneOrientation(), and Vector3D::Max().

◆ Min() [1/2]

template<typename T >
static T Min ( T  v1,
T  v2,
T  v3 
)
static

Returns the minimum of three values.

Template Parameters
TA numeric type.
Parameters
v1The first value.
v2The second value.
v3The third value.
Returns
The smallest of v1, v2, and v3.

◆ Min() [2/2]

template<typename T >
static T Min ( T  value1,
T  value2 
)
static

Returns the minimum of two values.

Template Parameters
TA numeric type.
Parameters
value1The first value.
value2The second value.
Returns
value1 if value1 <= value2, otherwise value2.

Referenced by ObjectAlign::AlignObjects(), Triangle2D::DidIntersectSAT(), ObjectAlign::GetObjectPlanarityRatio(), ObjectAlign::GetTransform(), and Vector3D::Min().

◆ PingPong()

float PingPong ( float  t)
static

Repeats a value between 0 and 1, then mirrors back and forth (like a ping-pong).

For example, PingPong(t) toggles between [0..1..0..1...] as t increases.

Parameters
tThe time or input value.
Returns
A float between 0 and 1, repeating in a ping-pong pattern.

Definition at line 114 of file Mathematics.cpp.

◆ Pow()

float Pow ( float  value,
float  exponent 
)
static

Raises a value to a given exponent.

Parameters
valueThe base value.
exponentThe exponent to raise by.
Returns
value raised to exponent.

Definition at line 34 of file Mathematics.cpp.

Referenced by Quaternion::Power(), and Quaternion::Power().

◆ QuinticInterpolation()

float QuinticInterpolation ( float  t)
static

Quintic interpolation function for smooth transitions.

Parameters
tA normalized value (0 to 1).
Returns
The quintic-interpolated value.

Definition at line 83 of file Mathematics.cpp.

◆ RadiansToDegrees()

template<typename T >
static T RadiansToDegrees ( T  radians)
static

Converts radians to degrees.

Template Parameters
TA numeric type.
Parameters
radiansThe angle in radians.
Returns
The angle in degrees.

Referenced by Rotation::GetAxisAngle(), Rotation::GetDirectionAngle(), Rotation::GetEulerAngles(), and Rotation::GetYawPitchRoll().

◆ RoundUpWindow()

int RoundUpWindow ( int  value,
int  multiple 
)
static

Rounds value up to the nearest multiple of multiple.

Parameters
valueThe integer to round up.
multipleThe multiple to round to.
Returns
value rounded up to the nearest multiple of multiple.

Definition at line 119 of file Mathematics.cpp.

Referenced by TriangleGroupDeformer::CosineInterpolationDeformer(), and ObjectDeformer::CosineInterpolationDeformer().

◆ Sign()

int Sign ( float  value)
static

Determines the sign of a floating-point value.

Parameters
valueThe float to check.
Returns
+1 if value > 0, -1 if value < 0, and 0 if value == 0.

Definition at line 30 of file Mathematics.cpp.

◆ Sqrt()

float Sqrt ( float  value)
static

Computes the square root of a value.

Parameters
valueThe float to take the square root of.
Returns
The square root of value.

Definition at line 38 of file Mathematics.cpp.

Referenced by Quaternion::Magnitude(), Vector2D::Magnitude(), Vector3D::Magnitude(), and Quaternion::Normal().

Member Data Documentation

◆ EPSILON

◆ FLTMAX

const float FLTMAX = __FLT_MAX__
static

Maximum float value (shortcut to a large number).

Definition at line 57 of file Mathematics.h.

◆ FLTMIN

const float FLTMIN = __FLT_MIN__
static

Minimum float value (shortcut to a very small or near-zero number).

Definition at line 62 of file Mathematics.h.

◆ M180DPI

const float M180DPI = 57.29577951308232087684f
static

The value of \(180.0 \div \pi\), useful for converting radians to degrees.

Definition at line 52 of file Mathematics.h.

Referenced by Vector2D::RadiansToDegrees(), and Vector3D::RadiansToDegrees().

◆ MPI

◆ MPID180

const float MPID180 = 0.01745329251994329576f
static

The value of \(\pi \div 180.0\), useful for converting degrees to radians.

Definition at line 47 of file Mathematics.h.

Referenced by Vector2D::DegreesToRadians(), Vector3D::DegreesToRadians(), Ellipse::IsInShape(), Rectangle::IsInShape(), and Vector2D::Rotate().


The documentation for this class was generated from the following files: