RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RST::Maths Namespace Reference

Math helpers: constants, interpolation, random numbers, 2D and 3D vectors and matrices. More...

Classes

class  Matrix
 Fixed-size matrix of Rows x Cols elements, stored in row-major order. More...
 
struct  Vector2D
 2D vector; arithmetic operators work component-wise, with a vector or a scalar. More...
 
struct  Vector3D
 3D vector; arithmetic operators work component-wise, with a vector or a scalar. More...
 

Typedefs

template<typename T >
using Matrix4 = Matrix< T, 4, 4 >
 
template<typename T >
using Matrix3 = Matrix< T, 3, 3 >
 
using Matrix4f = Matrix< float, 4, 4 >
 
using Matrix3f = Matrix< float, 3, 3 >
 
using Matrix4d = Matrix< double, 4, 4 >
 
using Matrix3d = Matrix< double, 3, 3 >
 
using Vector4f = Matrix< float, 4, 1 >
 Column vector, to multiply with a Matrix4f.
 
using Vector3f = Matrix< float, 3, 1 >
 Column vector, to multiply with a Matrix3f.
 
using Vec2f = Vector2D< float >
 
using Vec2d = Vector2D< double >
 
using Vec2i = Vector2D< int >
 
using Vec2u = Vector2D< unsigned int >
 
using Vec3f = Vector3D< float >
 
using Vec3d = Vector3D< double >
 
using Vec3i = Vector3D< int >
 
using Vec3u = Vector3D< unsigned int >
 

Functions

template<typename T , size_t Rows, size_t Cols>
std::ostream & operator<< (std::ostream &os, const Matrix< T, Rows, Cols > &mat)
 
Random numbers
std::mt19937 & randomEngine ()
 Per-thread engine behind random() and randomChoice(); call seed() on it for reproducible results.
 
template<typename T >
T random (const T &min, const T &max)
 Uniform random number in [min, max] for integers, or [min, max) for floating-point types.
 
template<typename T >
T randomChoice (const std::vector< T > &list)
 Random element of list, or T{} if it is empty.
 
Numeric helpers
template<std::floating_point T>
constexpr T toRad (T degrees) noexcept
 
template<std::floating_point T>
constexpr T toDeg (T radians) noexcept
 
template<typename T >
constexpr T abs (T value) noexcept
 
template<typename T >
constexpr T clamp (T value, T min, T max) noexcept
 
template<typename T >
constexpr int sign (T value) noexcept
 Returns -1, 0 or 1 depending on the sign of value.
 
template<typename T , typename U >
constexpr T lerp (T start, T end, U t) noexcept
 Linear interpolation from start to end, with t clamped to [0, 1].
 
template<std::floating_point T>
constexpr T inverseLerp (T start, T end, T value) noexcept
 Inverse of lerp(): where value lies between start and end, or 0 if they are equal.
 
template<typename T >
constexpr T remap (T value, T inMin, T inMax, T outMin, T outMax) noexcept
 Maps value from [inMin, inMax] to [outMin, outMax], without clamping.
 
template<std::floating_point T>
constexpr T smoothstep (T edge0, T edge1, T x) noexcept
 Smooth Hermite interpolation from 0 at edge0 to 1 at edge1.
 
template<typename T >
requires std::is_signed_v<T>
T wrap (T value, T min, T max) noexcept
 Wraps value into [min, max), such as an angle into [0, 360).
 
template<typename T >
constexpr bool isNearlyEqual (T a, T b, T epsilon=std::numeric_limits< T >::epsilon()) noexcept
 True if a and b differ by at most epsilon, scaled by their magnitude when it exceeds 1.
 
Vector2D operators
template<typename T >
constexpr Vector2D< T > operator+ (Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector2D< T > operator- (Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector2D< T > operator* (Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector2D< T > operator/ (Vector2D< T > lhs, const Vector2D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector2D< T > operator* (Vector2D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector2D< T > operator* (std::type_identity_t< T > s, Vector2D< T > v) noexcept
 
template<typename T >
constexpr Vector2D< T > operator/ (Vector2D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector2D< T > operator+ (Vector2D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector2D< T > operator- (Vector2D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
std::ostream & operator<< (std::ostream &os, const Vector2D< T > &vec)
 
Vector3D operators
template<typename T >
constexpr Vector3D< T > operator+ (Vector3D< T > lhs, const Vector3D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector3D< T > operator- (Vector3D< T > lhs, const Vector3D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector3D< T > operator* (Vector3D< T > lhs, const Vector3D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector3D< T > operator/ (Vector3D< T > lhs, const Vector3D< T > &rhs) noexcept
 
template<typename T >
constexpr Vector3D< T > operator* (Vector3D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector3D< T > operator* (std::type_identity_t< T > s, Vector3D< T > v) noexcept
 
template<typename T >
constexpr Vector3D< T > operator/ (Vector3D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector3D< T > operator+ (Vector3D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
constexpr Vector3D< T > operator- (Vector3D< T > v, std::type_identity_t< T > s) noexcept
 
template<typename T >
std::ostream & operator<< (std::ostream &os, const Vector3D< T > &vec)
 

Variables

Constants

Multiply by DegToRad or RadToDeg to convert an angle.

constexpr double Pi = std::numbers::pi
 
constexpr double TwoPi = 2.0 * std::numbers::pi
 
constexpr double HalfPi = std::numbers::pi / 2.0
 
constexpr double E = std::numbers::e
 
constexpr double Sqrt2 = std::numbers::sqrt2
 
constexpr double DegToRad = std::numbers::pi / 180.0
 
constexpr double RadToDeg = 180.0 / std::numbers::pi
 

Detailed Description

Math helpers: constants, interpolation, random numbers, 2D and 3D vectors and matrices.

Typedef Documentation

◆ Matrix4

template<typename T >
using RST::Maths::Matrix4 = typedef Matrix<T, 4, 4>

◆ Matrix3

template<typename T >
using RST::Maths::Matrix3 = typedef Matrix<T, 3, 3>

◆ Matrix4f

using RST::Maths::Matrix4f = typedef Matrix<float, 4, 4>

◆ Matrix3f

using RST::Maths::Matrix3f = typedef Matrix<float, 3, 3>

◆ Matrix4d

using RST::Maths::Matrix4d = typedef Matrix<double, 4, 4>

◆ Matrix3d

using RST::Maths::Matrix3d = typedef Matrix<double, 3, 3>

◆ Vector4f

using RST::Maths::Vector4f = typedef Matrix<float, 4, 1>

Column vector, to multiply with a Matrix4f.

◆ Vector3f

using RST::Maths::Vector3f = typedef Matrix<float, 3, 1>

Column vector, to multiply with a Matrix3f.

◆ Vec2f

using RST::Maths::Vec2f = typedef Vector2D<float>

◆ Vec2d

using RST::Maths::Vec2d = typedef Vector2D<double>

◆ Vec2i

using RST::Maths::Vec2i = typedef Vector2D<int>

◆ Vec2u

using RST::Maths::Vec2u = typedef Vector2D<unsigned int>

◆ Vec3f

using RST::Maths::Vec3f = typedef Vector3D<float>

◆ Vec3d

using RST::Maths::Vec3d = typedef Vector3D<double>

◆ Vec3i

using RST::Maths::Vec3i = typedef Vector3D<int>

◆ Vec3u

using RST::Maths::Vec3u = typedef Vector3D<unsigned int>

Function Documentation

◆ operator<<() [1/3]

template<typename T , size_t Rows, size_t Cols>
std::ostream & RST::Maths::operator<< ( std::ostream &  os,
const Matrix< T, Rows, Cols > &  mat 
)

◆ randomEngine()

std::mt19937 & RST::Maths::randomEngine ( )
inline

Per-thread engine behind random() and randomChoice(); call seed() on it for reproducible results.

◆ random()

template<typename T >
T RST::Maths::random ( const T &  min,
const T &  max 
)

Uniform random number in [min, max] for integers, or [min, max) for floating-point types.

◆ randomChoice()

template<typename T >
T RST::Maths::randomChoice ( const std::vector< T > &  list)

Random element of list, or T{} if it is empty.

◆ toRad()

template<std::floating_point T>
constexpr T RST::Maths::toRad ( T  degrees)
constexprnoexcept

◆ toDeg()

template<std::floating_point T>
constexpr T RST::Maths::toDeg ( T  radians)
constexprnoexcept

◆ abs()

template<typename T >
constexpr T RST::Maths::abs ( T  value)
constexprnoexcept

◆ clamp()

template<typename T >
constexpr T RST::Maths::clamp ( T  value,
T  min,
T  max 
)
constexprnoexcept

◆ sign()

template<typename T >
constexpr int RST::Maths::sign ( T  value)
constexprnoexcept

Returns -1, 0 or 1 depending on the sign of value.

◆ lerp()

template<typename T , typename U >
constexpr T RST::Maths::lerp ( T  start,
T  end,
U  t 
)
constexprnoexcept

Linear interpolation from start to end, with t clamped to [0, 1].

◆ inverseLerp()

template<std::floating_point T>
constexpr T RST::Maths::inverseLerp ( T  start,
T  end,
T  value 
)
constexprnoexcept

Inverse of lerp(): where value lies between start and end, or 0 if they are equal.

◆ remap()

template<typename T >
constexpr T RST::Maths::remap ( T  value,
T  inMin,
T  inMax,
T  outMin,
T  outMax 
)
constexprnoexcept

Maps value from [inMin, inMax] to [outMin, outMax], without clamping.

◆ smoothstep()

template<std::floating_point T>
constexpr T RST::Maths::smoothstep ( T  edge0,
T  edge1,
T  x 
)
constexprnoexcept

Smooth Hermite interpolation from 0 at edge0 to 1 at edge1.

◆ wrap()

template<typename T >
requires std::is_signed_v<T>
T RST::Maths::wrap ( T  value,
T  min,
T  max 
)
noexcept

Wraps value into [min, max), such as an angle into [0, 360).

◆ isNearlyEqual()

template<typename T >
constexpr bool RST::Maths::isNearlyEqual ( T  a,
T  b,
T  epsilon = std::numeric_limits<T>::epsilon() 
)
constexprnoexcept

True if a and b differ by at most epsilon, scaled by their magnitude when it exceeds 1.

◆ operator+() [1/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator+ ( Vector2D< T >  lhs,
const Vector2D< T > &  rhs 
)
constexprnoexcept

◆ operator-() [1/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator- ( Vector2D< T >  lhs,
const Vector2D< T > &  rhs 
)
constexprnoexcept

◆ operator*() [1/6]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator* ( Vector2D< T >  lhs,
const Vector2D< T > &  rhs 
)
constexprnoexcept

◆ operator/() [1/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator/ ( Vector2D< T >  lhs,
const Vector2D< T > &  rhs 
)
constexprnoexcept

◆ operator*() [2/6]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator* ( Vector2D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator*() [3/6]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator* ( std::type_identity_t< T >  s,
Vector2D< T >  v 
)
constexprnoexcept

◆ operator/() [2/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator/ ( Vector2D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator+() [2/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator+ ( Vector2D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator-() [2/4]

template<typename T >
constexpr Vector2D< T > RST::Maths::operator- ( Vector2D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator<<() [2/3]

template<typename T >
std::ostream & RST::Maths::operator<< ( std::ostream &  os,
const Vector2D< T > &  vec 
)

◆ operator+() [3/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator+ ( Vector3D< T >  lhs,
const Vector3D< T > &  rhs 
)
constexprnoexcept

◆ operator-() [3/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator- ( Vector3D< T >  lhs,
const Vector3D< T > &  rhs 
)
constexprnoexcept

◆ operator*() [4/6]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator* ( Vector3D< T >  lhs,
const Vector3D< T > &  rhs 
)
constexprnoexcept

◆ operator/() [3/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator/ ( Vector3D< T >  lhs,
const Vector3D< T > &  rhs 
)
constexprnoexcept

◆ operator*() [5/6]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator* ( Vector3D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator*() [6/6]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator* ( std::type_identity_t< T >  s,
Vector3D< T >  v 
)
constexprnoexcept

◆ operator/() [4/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator/ ( Vector3D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator+() [4/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator+ ( Vector3D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator-() [4/4]

template<typename T >
constexpr Vector3D< T > RST::Maths::operator- ( Vector3D< T >  v,
std::type_identity_t< T >  s 
)
constexprnoexcept

◆ operator<<() [3/3]

template<typename T >
std::ostream & RST::Maths::operator<< ( std::ostream &  os,
const Vector3D< T > &  vec 
)

Variable Documentation

◆ Pi

constexpr double RST::Maths::Pi = std::numbers::pi
inlineconstexpr

◆ TwoPi

constexpr double RST::Maths::TwoPi = 2.0 * std::numbers::pi
inlineconstexpr

◆ HalfPi

constexpr double RST::Maths::HalfPi = std::numbers::pi / 2.0
inlineconstexpr

◆ E

constexpr double RST::Maths::E = std::numbers::e
inlineconstexpr

◆ Sqrt2

constexpr double RST::Maths::Sqrt2 = std::numbers::sqrt2
inlineconstexpr

◆ DegToRad

constexpr double RST::Maths::DegToRad = std::numbers::pi / 180.0
inlineconstexpr

◆ RadToDeg

constexpr double RST::Maths::RadToDeg = 180.0 / std::numbers::pi
inlineconstexpr