Radium Engine  1.5.20
Loading...
Searching...
No Matches
Ra::Core::Math Namespace Reference

This namespace contains usefull math functions. More...

Functions

void print (const MatrixN &matrix)
 
template<typename Vector >
Vector floor (const Vector &v)
 Component-wise floor() function on a floating-point vector.
 
template<typename Vector >
Vector ceil (const Vector &v)
 Component-wise ceil() function on a floating-point vector.
 
template<typename Vector >
Vector trunc (const Vector &v)
 Component-wise trunc() function on a floating-point vector.
 
template<typename Derived , typename DerivedA , typename DerivedB >
Derived::PlainMatrix clamp (const Eigen::MatrixBase< Derived > &v, const Eigen::MatrixBase< DerivedA > &min, const Eigen::MatrixBase< DerivedB > &max)
 Component-wise clamp() function on a floating-point vector.
 
template<typename Derived >
Derived::PlainMatrix clamp (const Eigen::MatrixBase< Derived > &v, const Scalar &min, const Scalar &max)
 Component-wise clamp() function on a floating-point vector.
 
template<typename S >
bool checkInvalidNumbers (Eigen::Ref< Eigen::Quaternion< S > > q, const bool FAIL_ON_ASSERT=false)
 Call std::isfinite on quaternion entries.
 
template<typename Matrix_ >
bool checkInvalidNumbers (Eigen::Ref< const Matrix_ > matrix, const bool FAIL_ON_ASSERT=false)
 
void getOrthogonalVectors (const Vector3 &fx, Eigen::Ref< Vector3 > fy, Eigen::Ref< Vector3 > fz)
 
template<typename Vector_ >
Scalar angle (const Vector_ &v1, const Vector_ &v2)
 
template<typename Vector_ >
Vector_ slerp (const Vector_ &v1, const Vector_ &v2, Scalar t)
 
Vector3 projectOnPlane (const Vector3 &planePos, const Vector3 &planeNormal, const Vector3 &point)
 
template<typename Vector_ >
Scalar cotan (const Vector_ &v1, const Vector_ &v2)
 
template<typename Vector_ >
Scalar cos (const Vector_ &v1, const Vector_ &v2)
 
template<typename Vector_ >
Scalar getNormAndNormalize (Vector_ &v)
 
template<typename Vector_ >
Scalar getNormAndNormalizeSafe (Vector_ &v)
 
template<typename Scalar >
Eigen::ParametrizedLine< Scalar, 3 > transformRay (const Eigen::Transform< Scalar, 3, Eigen::Affine > &t, const Eigen::ParametrizedLine< Scalar, 3 > &r)
 
Matrix4 lookAt (const Vector3 &position, const Vector3 &target, const Vector3 &up)
 
Matrix4 perspective (Scalar fovy, Scalar aspect, Scalar near, Scalar zfar)
 
Matrix4 orthographic (Scalar left, Scalar right, Scalar bottom, Scalar top, Scalar near, Scalar zfar)
 
Quaternion scale (const Quaternion &q, const Scalar k)
 Returns the quaternion q multipled by a scalar factor of k.
 
Quaternion add (const Quaternion &q1, const Quaternion &q2)
 Returns the sum of two quaternions.
 
Quaternion addQlerp (const Quaternion &q1, const Quaternion &q2)
 
void getSwingTwist (const Quaternion &in, Quaternion &swingOut, Quaternion &twistOut)
 
template<typename Vector_ >
Vector_ floor (const Vector_ &v)
 
template<typename Vector_ >
Vector_ ceil (const Vector_ &v)
 
template<typename Vector_ >
Vector_ trunc (const Vector_ &v)
 
template<typename DerivedA , typename DerivedB >
bool allClose (const Eigen::DenseBase< DerivedA > &a, const Eigen::DenseBase< DerivedB > &b, const typename DerivedA::RealScalar &rtol=Eigen::NumTraits< typename DerivedA::RealScalar >::dummy_precision(), const typename DerivedA::RealScalar &atol=Eigen::NumTraits< typename DerivedA::RealScalar >::epsilon())
 Returns True if two arrays are element-wise equal within a tolerance.
 
constexpr Scalar toRadians (Scalar a)
 Useful functions.
 
constexpr Scalar toDegrees (Scalar a)
 Converts an angle from radians to degrees.
 
template<class T >
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type areApproxEqual (T x, T y, T espilonBoostFactor=T(10))
 Compare two numbers such that |x-y| < espilon*epsilonBoostFactor.
 
template<typename Vector_Or_Scalar >
bool checkRange (const Vector_Or_Scalar &v, const Scalar &min, const Scalar &max)
 
template<typename T >
ipow (const T &x, uint exp)
 Run-time exponent version.
 
template<uint N, typename T >
constexpr T ipow (const T &x)
 Compile-time exponent version.
 
template<typename T >
constexpr int signum (T x, std::true_type)
 
template<typename T >
constexpr int sign (const T &val)
 Returns the sign of any numeric type as { -1, 0, 1}.
 
template<typename T >
constexpr T signNZ (const T &val)
 
template<typename T >
constexpr T saturate (T v)
 Clamps the value between 0 and 1.
 
template<typename T >
constexpr T lerp (const T &a, const T &b, Scalar t)
 Returns the linear interpolation between a and b.
 
template<typename T >
smoothstep (T edge0, T edge1, T x)
 As define by https://registry.khronos.org/OpenGL-Refpages/gl4/html/smoothstep.xhtml.
 
template<typename T , template< typename, int... > typename M, int... p>
M< T, p... > smoothstep (T edge0, T edge1, M< T, p... > v)
 
Linear Interpolation
template<typename T >
linearInterpolate (const T &v0, const T &v1, const Scalar t)
 
template<>
Core::Quaternion linearInterpolate< Core::Quaternion > (const Core::Quaternion &q0, const Core::Quaternion &q1, const Scalar t)
 
template<>
Core::Transform linearInterpolate< Core::Transform > (const Core::Transform &T0, const Core::Transform &T1, const Scalar t)
 

Variables

constexpr Scalar Sqrt2 = Scalar( 1.41421356237309504880 )
 Mathematical constants casted to Scalar. Values taken from math.h.
 
constexpr Scalar e = Scalar( 2.7182818284590452354 )
 
constexpr Scalar Pi = Scalar( 3.14159265358979323846 )
 
constexpr Scalar InvPi = Scalar( 0.31830988618379067154 )
 
constexpr Scalar PiDiv2 = Scalar( 1.57079632679489661923 )
 
constexpr Scalar PiDiv3 = Scalar( 1.04719755119659774615 )
 
constexpr Scalar PiDiv4 = Scalar( 0.78539816339744830962 )
 
constexpr Scalar PiDiv6 = Scalar( 0.52359877559829887307 )
 
constexpr Scalar PiMul2 = Scalar( 2 * Pi )
 
constexpr Scalar toRad = Scalar( Pi / Scalar( 180.0 ) )
 
constexpr Scalar toDeg = Scalar( Scalar( 180.0 ) * InvPi )
 
constexpr Scalar machineEps = std::numeric_limits<Scalar>::epsilon()
 

Detailed Description

This namespace contains usefull math functions.

Function Documentation

◆ add()

Quaternion Ra::Core::Math::add ( const Quaternion & q1,
const Quaternion & q2 )
inline

Returns the sum of two quaternions.

Definition at line 245 of file LinearAlgebra.hpp.

◆ addQlerp()

Quaternion Ra::Core::Math::addQlerp ( const Quaternion & q1,
const Quaternion & q2 )
inline

Returns the sum of two quaternions, resolving antipodality by flipping the sign of q2 if q1.q2 is negative. This operation is usually denoted as a circled + sign, forming the basis of the QLERP algorithm. See "Spherical Blend Skinning" (Kavan & Zara 2005) for more details.

Definition at line 249 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ allClose()

template<typename DerivedA , typename DerivedB >
bool Ra::Core::Math::allClose ( const Eigen::DenseBase< DerivedA > & a,
const Eigen::DenseBase< DerivedB > & b,
const typename DerivedA::RealScalar & rtol = Eigen::NumTraits<typename DerivedA::RealScalar>::dummy_precision(),
const typename DerivedA::RealScalar & atol = Eigen::NumTraits<typename DerivedA::RealScalar>::epsilon() )

Returns True if two arrays are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

Parameters:

Parameters
a,bInput Matrix to compare
rtolThe relative tolerance parameter (see Notes).
atolThe absolute tolerance parameter (see Notes).
See also
https://stackoverflow.com/questions/15051367/how-to-compare-vectors-approximately-in-eigen, https://numpy.org/doc/stable/reference/generated/numpy.allclose.html

Definition at line 392 of file LinearAlgebra.hpp.

◆ angle()

template<typename Vector_ >
Scalar Ra::Core::Math::angle ( const Vector_ & v1,
const Vector_ & v2 )
inline

Get the angle between two vectors. Works for types where the cross product is defined (i.e. 2D and 3D vectors).

Definition at line 206 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ areApproxEqual()

template<class T >
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type Ra::Core::Math::areApproxEqual ( T x,
T y,
T espilonBoostFactor = T( 10 ) )
inline

Compare two numbers such that |x-y| < espilon*epsilonBoostFactor.

Definition at line 42 of file Math.hpp.

+ Here is the call graph for this function:

◆ ceil()

template<typename Vector_ >
Vector_ Ra::Core::Math::ceil ( const Vector_ & v)
inline

Definition at line 179 of file LinearAlgebra.hpp.

◆ checkInvalidNumbers() [1/2]

template<typename Matrix_ >
bool Ra::Core::Math::checkInvalidNumbers ( Eigen::Ref< const Matrix_ > matrix,
const bool FAIL_ON_ASSERT = false )
inline

Call std::isfinite on matrix entry. Dense version

Definition at line 362 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ checkInvalidNumbers() [2/2]

template<typename S >
bool Ra::Core::Math::checkInvalidNumbers ( Eigen::Ref< Eigen::Quaternion< S > > q,
const bool FAIL_ON_ASSERT = false )
inline

Call std::isfinite on quaternion entries.

Definition at line 59 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ checkRange()

template<typename Vector_Or_Scalar >
bool Ra::Core::Math::checkRange ( const Vector_Or_Scalar & v,
const Scalar & min,
const Scalar & max )
inline

Scalar or component-wise range check. Note: a definition of clamp must be available:

  • for vectors, relies on the component-wise clamp defined in Core/Math/LinearAlgebra
  • for scalars, use std version (call using std::clamp; beforehand)

Definition at line 53 of file Math.hpp.

+ Here is the call graph for this function:

◆ clamp() [1/2]

template<typename Derived , typename DerivedA , typename DerivedB >
Derived::PlainMatrix Ra::Core::Math::clamp ( const Eigen::MatrixBase< Derived > & v,
const Eigen::MatrixBase< DerivedA > & min,
const Eigen::MatrixBase< DerivedB > & max )
inline

Component-wise clamp() function on a floating-point vector.

Definition at line 193 of file LinearAlgebra.hpp.

◆ clamp() [2/2]

template<typename Derived >
Derived::PlainMatrix Ra::Core::Math::clamp ( const Eigen::MatrixBase< Derived > & v,
const Scalar & min,
const Scalar & max )
inline

Component-wise clamp() function on a floating-point vector.

Definition at line 201 of file LinearAlgebra.hpp.

◆ cos()

template<typename Vector_ >
Scalar Ra::Core::Math::cos ( const Vector_ & v1,
const Vector_ & v2 )
inline

Get the cosine of the angle between two vectors.

Todo
use dot instead

Definition at line 216 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ cotan()

template<typename Vector_ >
Scalar Ra::Core::Math::cotan ( const Vector_ & v1,
const Vector_ & v2 )
inline

Get the cotangent of the angle between two vectors. Works for vector types where dot and cross product is defined (2D or 3D vectors).

Definition at line 211 of file LinearAlgebra.hpp.

◆ floor()

template<typename Vector_ >
Vector_ Ra::Core::Math::floor ( const Vector_ & v)
inline

Definition at line 172 of file LinearAlgebra.hpp.

◆ getNormAndNormalize()

template<typename Vector_ >
Scalar Ra::Core::Math::getNormAndNormalize ( Vector_ & v)
inline

Normalize a vector and returns its norm before normalization. If the vector's norm is 0, the vector's components will be overwritten by NaNs

Definition at line 221 of file LinearAlgebra.hpp.

◆ getNormAndNormalizeSafe()

template<typename Vector_ >
Scalar Ra::Core::Math::getNormAndNormalizeSafe ( Vector_ & v)
inline

Normalize a vector and returns its norm before normalization. If the vector's norm is 0, the vector remains null

Definition at line 228 of file LinearAlgebra.hpp.

◆ getOrthogonalVectors()

void Ra::Core::Math::getOrthogonalVectors ( const Vector3 & fx,
Eigen::Ref< Vector3 > fy,
Eigen::Ref< Vector3 > fz )
inline

Get two vectors orthogonal to a given vector.

Warning
fx must be normalized (this is not checked in the function)

Definition at line 259 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ getSwingTwist()

void Ra::Core::Math::getSwingTwist ( const Quaternion & in,
Quaternion & swingOut,
Quaternion & twistOut )
inline

Decompose a given rotation Qin into a swing rotation and a twist rotation. Qswing is a rotation whose axis lies in the XY plane and Qtwist is a rotation about axis Z. such as Qin = Qswing * Qtwist If the rotation is already around axis z, Qswing will be set to identity and Qtwist equal to Qin

Definition at line 285 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ ipow() [1/2]

template<uint N, typename T >
T Ra::Core::Math::ipow ( const T & x)
inlineconstexpr

Compile-time exponent version.

Definition at line 95 of file Math.hpp.

◆ ipow() [2/2]

template<typename T >
T Ra::Core::Math::ipow ( const T & x,
uint exp )
inline

Run-time exponent version.

Integer power functions. Work for all numeric types which support multiplication and for which T(1) is a valid expression. x^0 always return T(1) and x^1 always return x (even when x is 0).

Definition at line 64 of file Math.hpp.

+ Here is the call graph for this function:

◆ lerp()

template<typename T >
T Ra::Core::Math::lerp ( const T & a,
const T & b,
Scalar t )
inlineconstexpr

Returns the linear interpolation between a and b.

Definition at line 126 of file Math.hpp.

◆ linearInterpolate()

template<typename T >
T Ra::Core::Math::linearInterpolate ( const T & v0,
const T & v1,
const Scalar t )
inline
Returns
in result the linear interpolation between v0 and v1, according to t.
Note
Not designed for interpolation on integer or boolean types.

Definition at line 18 of file Interpolation.hpp.

◆ linearInterpolate< Core::Quaternion >()

template<>
Core::Quaternion Ra::Core::Math::linearInterpolate< Core::Quaternion > ( const Core::Quaternion & q0,
const Core::Quaternion & q1,
const Scalar t )
inline
Returns
in result the spherical linear interpolation between q0 and q1, according to t.

Definition at line 27 of file Interpolation.hpp.

◆ linearInterpolate< Core::Transform >()

template<>
Core::Transform Ra::Core::Math::linearInterpolate< Core::Transform > ( const Core::Transform & T0,
const Core::Transform & T1,
const Scalar t )
inline
Returns
in result the linear interpolation between q0 and q1, according to t. To do so, the rotational parts are interpolated through spherical linear interpolation while the translational parts are interpolated through linear interpolation.

Definition at line 40 of file Interpolation.hpp.

◆ lookAt()

Matrix4 Ra::Core::Math::lookAt ( const Vector3 & position,
const Vector3 & target,
const Vector3 & up )
inline

Definition at line 316 of file LinearAlgebra.hpp.

◆ orthographic()

Matrix4 Ra::Core::Math::orthographic ( Scalar left,
Scalar right,
Scalar bottom,
Scalar top,
Scalar near,
Scalar zfar )
inline

Definition at line 346 of file LinearAlgebra.hpp.

◆ perspective()

Matrix4 Ra::Core::Math::perspective ( Scalar fovy,
Scalar aspect,
Scalar near,
Scalar zfar )
inline

Definition at line 330 of file LinearAlgebra.hpp.

◆ print()

void Ra::Core::Math::print ( const MatrixN & matrix)
inline

Definition at line 157 of file LinearAlgebra.hpp.

◆ projectOnPlane()

Vector3 Ra::Core::Math::projectOnPlane ( const Vector3 & planePos,
const Vector3 & planeNormal,
const Vector3 & point )
inline
Returns
the projection of point on the plane define by plane and planeNormal

Definition at line 270 of file LinearAlgebra.hpp.

◆ saturate()

template<typename T >
T Ra::Core::Math::saturate ( T v)
inlineconstexpr

Clamps the value between 0 and 1.

Definition at line 120 of file Math.hpp.

◆ scale()

Quaternion Ra::Core::Math::scale ( const Quaternion & q,
const Scalar k )
inline

Returns the quaternion q multipled by a scalar factor of k.

Definition at line 254 of file LinearAlgebra.hpp.

◆ sign()

template<typename T >
int Ra::Core::Math::sign ( const T & val)
inlineconstexpr

Returns the sign of any numeric type as { -1, 0, 1}.

Definition at line 106 of file Math.hpp.

◆ signNZ()

template<typename T >
T Ra::Core::Math::signNZ ( const T & val)
inlineconstexpr

Returns the sign of any numeric type as { -1, 1} Note: signNZ(0) returns 1 for any integral type but signNZ(-0.f) will return -1

Definition at line 114 of file Math.hpp.

+ Here is the call graph for this function:

◆ signum()

template<typename T >
int Ra::Core::Math::signum ( T x,
std::true_type  )
inlineconstexpr

Definition at line 100 of file Math.hpp.

◆ slerp()

template<typename Vector_ >
Vector_ Ra::Core::Math::slerp ( const Vector_ & v1,
const Vector_ & v2,
Scalar t )
inline

Get the spherical linear interpolation between two unit non-colinear vectors. works for types where the cross-product is defined (i.e. 2D and 3D vectors).

Definition at line 275 of file LinearAlgebra.hpp.

+ Here is the call graph for this function:

◆ smoothstep() [1/2]

template<typename T , template< typename, int... > typename M, int... p>
M< T, p... > Ra::Core::Math::smoothstep ( T edge0,
T edge1,
M< T, p... > v )

Definition at line 139 of file Math.hpp.

◆ smoothstep() [2/2]

template<typename T >
T Ra::Core::Math::smoothstep ( T edge0,
T edge1,
T x )

As define by https://registry.khronos.org/OpenGL-Refpages/gl4/html/smoothstep.xhtml.

Definition at line 132 of file Math.hpp.

+ Here is the call graph for this function:

◆ toDegrees()

Scalar Ra::Core::Math::toDegrees ( Scalar a)
inlineconstexpr

Converts an angle from radians to degrees.

Definition at line 35 of file Math.hpp.

◆ toRadians()

Scalar Ra::Core::Math::toRadians ( Scalar a)
inlineconstexpr

Useful functions.

Converts an angle from degrees to radians.

Definition at line 30 of file Math.hpp.

◆ transformRay()

template<typename Scalar >
Eigen::ParametrizedLine< Scalar, 3 > Ra::Core::Math::transformRay ( const Eigen::Transform< Scalar, 3, Eigen::Affine > & t,
const Eigen::ParametrizedLine< Scalar, 3 > & r )
inline

Transform a ray, direction is only transformed by linear part of the transformation, while origin is fully transformed corresponds to t*r

Parameters
t: transform matrix
r: ray to transform
Returns
transoformed ray, origine is translated while direction is only linarly transformed.

Definition at line 236 of file LinearAlgebra.hpp.

◆ trunc()

template<typename Vector_ >
Vector_ Ra::Core::Math::trunc ( const Vector_ & v)
inline

Definition at line 186 of file LinearAlgebra.hpp.

Variable Documentation

◆ e

Scalar Ra::Core::Math::e = Scalar( 2.7182818284590452354 )
constexpr

Definition at line 14 of file Math.hpp.

◆ InvPi

Scalar Ra::Core::Math::InvPi = Scalar( 0.31830988618379067154 )
constexpr

Definition at line 16 of file Math.hpp.

◆ machineEps

Scalar Ra::Core::Math::machineEps = std::numeric_limits<Scalar>::epsilon()
constexpr

Definition at line 25 of file Math.hpp.

◆ Pi

Scalar Ra::Core::Math::Pi = Scalar( 3.14159265358979323846 )
constexpr

Definition at line 15 of file Math.hpp.

◆ PiDiv2

Scalar Ra::Core::Math::PiDiv2 = Scalar( 1.57079632679489661923 )
constexpr

Definition at line 17 of file Math.hpp.

◆ PiDiv3

Scalar Ra::Core::Math::PiDiv3 = Scalar( 1.04719755119659774615 )
constexpr

Definition at line 18 of file Math.hpp.

◆ PiDiv4

Scalar Ra::Core::Math::PiDiv4 = Scalar( 0.78539816339744830962 )
constexpr

Definition at line 19 of file Math.hpp.

◆ PiDiv6

Scalar Ra::Core::Math::PiDiv6 = Scalar( 0.52359877559829887307 )
constexpr

Definition at line 20 of file Math.hpp.

◆ PiMul2

Scalar Ra::Core::Math::PiMul2 = Scalar( 2 * Pi )
constexpr

Definition at line 21 of file Math.hpp.

◆ Sqrt2

Scalar Ra::Core::Math::Sqrt2 = Scalar( 1.41421356237309504880 )
constexpr

Mathematical constants casted to Scalar. Values taken from math.h.

Definition at line 13 of file Math.hpp.

◆ toDeg

Scalar Ra::Core::Math::toDeg = Scalar( Scalar( 180.0 ) * InvPi )
constexpr

Definition at line 23 of file Math.hpp.

◆ toRad

Scalar Ra::Core::Math::toRad = Scalar( Pi / Scalar( 180.0 ) )
constexpr

Definition at line 22 of file Math.hpp.