Radium Engine  1.5.0
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)
 
constexpr Scalar toRadians (Scalar a)
 Useful functions. More...
 
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. More...
 
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

◆ 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:

◆ 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.

◆ checkInvalidNumbers()

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.

◆ 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:

◆ 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.

◆ 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.

◆ 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.

◆ ipow()

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.

◆ 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.

◆ 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.

◆ signNZ()

template<typename T >
constexpr 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.

◆ 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:

◆ toRadians()

constexpr 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.