NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches

Provides various math utility functions. More...

Functions

bool nnl::utl::math::IsNan (float value)
 Checks if a floating-point value is not a number.
template<typename TContainer>
bool nnl::utl::math::IsNan (const TContainer &value)
 Checks if a container has any floating-point values that are not numbers.
bool nnl::utl::math::IsFinite (float value)
 Checks if a floating-point value is finite.
template<typename TContainer>
bool nnl::utl::math::IsFinite (const TContainer &container)
 Checks if all floating-point values in a container are finite.
glm::quat nnl::utl::math::EulerToQuat (glm::vec3 euler)
 Converts Euler angles in degrees (Pitch X, Yaw Y, Roll Z) to a quaternion.
glm::vec3 nnl::utl::math::QuatToEuler (glm::quat quat)
 Converts quaternion to Euler angles (Pitch X, Yaw Y, Roll Z)
std::tuple< glm::vec3, glm::quat, glm::vec3 > nnl::utl::math::Decompose (const glm::mat4 &matrix)
 Decomposes a transformation matrix into scale, rotation, and translation.
glm::mat4 nnl::utl::math::Compose (glm::vec3 scale, glm::quat rotation, glm::vec3 translation)
 Composes a transformation matrix from scale, rotation, and translation.
glm::mat4 nnl::utl::math::InverseSafe (const glm::mat4 &m)
 Computes the inverse of a matrix safely.
glm::mat3 nnl::utl::math::InverseSafe (const glm::mat3 &m)
 Computes the inverse of a matrix safely.
glm::mat4 nnl::utl::math::Inverse (const glm::mat4 &m)
 Computes the inverse of a matrix.
glm::mat3 nnl::utl::math::Inverse (const glm::mat3 &m)
 Computes the inverse of a matrix.
glm::vec3 nnl::utl::math::NormalizeSafe (const glm::vec3 &vec)
 Normalizes a vector safely.
template<typename T>
constexpr T nnl::utl::math::Sqr (T value)
 Calculates the square of a value.
template<typename T>
bool nnl::utl::math::IsPow2 (T n)
 Checks whether a number can be expressed as an integer power of 2.
u32 nnl::utl::math::RoundUpPow2 (u32 v)
 Rounds up the number to the next power of 2.
u32 nnl::utl::math::RoundDownPow2 (u32 v)
 Rounds down to the previous power of 2.
template<typename T>
nnl::utl::math::RoundNum (T number, std::size_t multiple)
 Rounds number up to the nearest multiple.
template<typename T, std::size_t num_int = 0>
constexpr T nnl::utl::math::FloatToFixed (f32 value)
 Converts a float value to a fixed point value.
template<typename T, std::size_t num_int = 0>
constexpr f32 nnl::utl::math::FixedToFloat (T value)
 Converts a fixed-point value to a float value.
bool nnl::utl::math::IsApproxEqual (float a, float b, float range=NNL_EPSILON)
 Checks if floating-point numbers are approximately equal.
template<typename TContainer>
bool nnl::utl::math::IsApproxEqual (TContainer a, TContainer b, float range=NNL_EPSILON)
 Checks if two vectors or matrices are approximately equal.

Detailed Description

Provides various math utility functions.

Function Documentation

◆ Compose()

glm::mat4 nnl::utl::math::Compose ( glm::vec3 scale,
glm::quat rotation,
glm::vec3 translation )

Composes a transformation matrix from scale, rotation, and translation.

Parameters
scale
rotation
translation
Returns
A glm::mat4

◆ Decompose()

std::tuple< glm::vec3, glm::quat, glm::vec3 > nnl::utl::math::Decompose ( const glm::mat4 & matrix)

Decomposes a transformation matrix into scale, rotation, and translation.

Parameters
matrixThe 4x4 transformation matrix to decompose
Returns
Tuple containing scale (glm::vec3), rotation (glm::quat), and translation (glm::vec3)
Note
If any scale component is 0, the rotation is set to the identity

◆ EulerToQuat()

glm::quat nnl::utl::math::EulerToQuat ( glm::vec3 euler)

Converts Euler angles in degrees (Pitch X, Yaw Y, Roll Z) to a quaternion.

Parameters
eulerThe angles to convert
Returns
glm::quat

◆ FixedToFloat()

template<typename T, std::size_t num_int = 0>
f32 nnl::utl::math::FixedToFloat ( T value)
constexpr

Converts a fixed-point value to a float value.

Template Parameters
TIntegral type
num_intNum of bits for the integer part
Parameters
valueInteger value to convert
Returns
A float value

◆ FloatToFixed()

template<typename T, std::size_t num_int = 0>
T nnl::utl::math::FloatToFixed ( f32 value)
constexpr

Converts a float value to a fixed point value.

Template Parameters
TIntegral type
num_intNum of bits for the integer part
Parameters
valueFloat value to convert
Returns
A fixed point value

◆ Inverse() [1/2]

glm::mat3 nnl::utl::math::Inverse ( const glm::mat3 & m)

Computes the inverse of a matrix.

This function takes a matrix and returns its inverse. If the matrix is not invertible, the result is undefined.

Parameters
mThe matrix to be inverted.
Returns
A matrix representing the inverse.

◆ Inverse() [2/2]

glm::mat4 nnl::utl::math::Inverse ( const glm::mat4 & m)

Computes the inverse of a matrix.

This function takes a matrix and returns its inverse. If the matrix is not invertible, the result is undefined.

Parameters
mThe matrix to be inverted.
Returns
A matrix representing the inverse.

◆ InverseSafe() [1/2]

glm::mat3 nnl::utl::math::InverseSafe ( const glm::mat3 & m)

Computes the inverse of a matrix safely.

This function takes a matrix and returns its inverse. If the matrix is not invertible, a small value is added to its diagonal elements. If the adjusted matrix is still not invertible, the function will return the identity matrix.

Parameters
mThe matrix to be inverted.
Returns
A matrix representing the inverse or an identity matrix.

◆ InverseSafe() [2/2]

glm::mat4 nnl::utl::math::InverseSafe ( const glm::mat4 & m)

Computes the inverse of a matrix safely.

This function takes a matrix and returns its inverse. If the matrix is not invertible, a small value is added to its diagonal elements. If the adjusted matrix is still not invertible, the function will return the identity matrix.

Parameters
mThe matrix to be inverted.
Returns
A matrix representing the inverse or an identity matrix.

◆ IsApproxEqual() [1/2]

bool nnl::utl::math::IsApproxEqual ( float a,
float b,
float range = NNL_EPSILON )
inline

Checks if floating-point numbers are approximately equal.

Parameters
aFirst float
bSecond float
rangeTolerance range
Returns
true if |a - b| <= range

◆ IsApproxEqual() [2/2]

template<typename TContainer>
bool nnl::utl::math::IsApproxEqual ( TContainer a,
TContainer b,
float range = NNL_EPSILON )

Checks if two vectors or matrices are approximately equal.

Template Parameters
TContainer type
Parameters
aFirst value
bSecond value
rangeTolerance range per element
Returns
true if all elements are approximately equal

◆ IsFinite() [1/2]

template<typename TContainer>
bool nnl::utl::math::IsFinite ( const TContainer & container)

Checks if all floating-point values in a container are finite.

Template Parameters
TContainerContainer type
Parameters
containerThe container to check.
Returns
true if all values are finite.

◆ IsFinite() [2/2]

bool nnl::utl::math::IsFinite ( float value)
inline

Checks if a floating-point value is finite.

Parameters
valueThe value to check.
Returns
true if the input value is finite.

◆ IsNan() [1/2]

template<typename TContainer>
bool nnl::utl::math::IsNan ( const TContainer & value)

Checks if a container has any floating-point values that are not numbers.

Template Parameters
TContainerContainer type
Parameters
valueThe container to check.
Returns
true if the input container has any NaN values.

◆ IsNan() [2/2]

bool nnl::utl::math::IsNan ( float value)
inline

Checks if a floating-point value is not a number.

Parameters
valueThe value to check.
Returns
true if the input value is NaN.

◆ IsPow2()

template<typename T>
bool nnl::utl::math::IsPow2 ( T n)

Checks whether a number can be expressed as an integer power of 2.

Template Parameters
TIntegral type
Parameters
nThe number to check
Returns
true if n is a power of 2, false otherwise

◆ NormalizeSafe()

glm::vec3 nnl::utl::math::NormalizeSafe ( const glm::vec3 & vec)

Normalizes a vector safely.

Returns a unit vector pointing in the same direction as the input. If the input vector has a length of zero (or is near-zero), it returns a default vector.

Parameters
vecThe vector to be normalized.
Returns
The normalized unit vector, or a default vector.

◆ QuatToEuler()

glm::vec3 nnl::utl::math::QuatToEuler ( glm::quat quat)

Converts quaternion to Euler angles (Pitch X, Yaw Y, Roll Z)

Parameters
quatThe quaternion to convert
Returns
glm::vec3 containing Euler angles in degrees
Note
Conversion from Euler -> Quaternion -> Euler may not yield the same set of angles since there are multiple Euler combinations that represent the same rotation.

◆ RoundDownPow2()

u32 nnl::utl::math::RoundDownPow2 ( u32 v)
inline

Rounds down to the previous power of 2.

Parameters
v32-bit unsigned integer to round
Returns
Previous power of 2 or the number itself

◆ RoundNum()

template<typename T>
T nnl::utl::math::RoundNum ( T number,
std::size_t multiple )

Rounds number up to the nearest multiple.

Template Parameters
TIntegral type
Parameters
numberNumber to round
multipleRounding multiple
Returns
Rounded number

◆ RoundUpPow2()

u32 nnl::utl::math::RoundUpPow2 ( u32 v)
inline

Rounds up the number to the next power of 2.

Parameters
v32-bit unsigned integer to round
Returns
Next power of 2 or the number itself

◆ Sqr()

template<typename T>
T nnl::utl::math::Sqr ( T value)
inlineconstexpr

Calculates the square of a value.

Template Parameters
TNumeric type
Parameters
valueThe value to square
Returns
value * value