mal-math
C++20 mathematics library.
All Classes Namespaces Files Functions Variables Typedefs Concepts
mal_math::qua< T > Class Template Reference

Represents a quaternion with primitive type T. More...

#include "quaternion.hpp"

Collaboration diagram for mal_math::qua< T >:
[legend]

Public Types

using type = T
 Alias for the primitive data type used in the quaternion.
 

Public Member Functions

constexpr qua ()
 Default constructor initializes the quaternion to its default state (1, 0, 0, 0).
 
constexpr qua (T w, T x, T y, T z)
 Constructs a quaternion from its four components.
 
constexpr void reset ()
 Resets the quaternion to its default state (1, 0, 0, 0).
 
constexpr qua (T radians, vec< 3, T > axis)
 Constructs a quaternion from an angle and rotation axis.
 
constexpr mat< 3, 3, T > as_mat3 () const noexcept
 Converts the quaternion to a 3x3 rotation matrix.
 
constexpr mat< 4, 4, T > as_mat4 () const noexcept
 Converts the quaternion to a 4x4 rotation matrix.
 
constexpr vec< 3, T > const & axis () const noexcept
 Retrieves the axis of rotation from the quaternion.
 
constexpr T radians () const noexcept
 Retrieves the angle of rotation from the quaternion.
 
constexpr qua< T > const & operator+ () const noexcept
 Unary plus operator.
 
constexpr qua< T > operator- () const noexcept
 Unary minus operator. Inverts the sign of each component.
 
template<Primitive U>
constexpr qua< T > & operator*= (U const value) noexcept
 Multiplies the quaternion by a scalar value.
 
template<Primitive U>
constexpr qua< T > & operator/= (U const value) noexcept
 Divides the components of the quaternion by a scalar value.
 
template<Primitive U>
constexpr qua< T > & operator+= (qua< U > const &other) noexcept
 Adds another quaternion to this quaternion component-wise.
 
template<Primitive U>
constexpr qua< T > & operator-= (qua< U > const &other) noexcept
 Subtracts another quaternion from this quaternion component-wise.
 
template<Primitive U>
constexpr qua< T > & operator*= (qua< U > const &other) noexcept
 Multiplies this quaternion with another quaternion.
 
template<Primitive U>
constexpr qua< T > & operator/= (qua< U > const &other) noexcept
 Divides this quaternion by another quaternion.
 
constexpr T & operator[] (size_t i)
 Accesses a component of the quaternion by index.
 
constexpr T const & operator[] (size_t i) const
 Accesses a component of the quaternion by index (const version).
 

Public Attributes

union { 
 
   struct { 
 
      union { 
 
         T   x 
 
         T   r 
 
         T   s 
 
      }  
 
      union { 
 
         T   y 
 
         T   g 
 
         T   t 
 
      }  
 
      union { 
 
         T   z 
 
         T   b 
 
         T   p 
 
      }  
 
      union { 
 
         T   w 
 
         T   a 
 
         T   q 
 
      }  
 
   }  
 
   std::array< T, size >   data 
 
};  
 Unions allowing various ways to access quaternion components.
 

Static Public Attributes

static constexpr size_t size = 4
 Represents the number of components in the quaternion.
 

Detailed Description

template<Primitive T>
class mal_math::qua< T >

Represents a quaternion with primitive type T.

Template Parameters
TData type of the quaternion components (should be a primitive type).

Definition at line 23 of file quaternion.hpp.

Member Typedef Documentation

◆ type

template<Primitive T>
using mal_math::qua< T >::type = T

Alias for the primitive data type used in the quaternion.

Definition at line 26 of file quaternion.hpp.

Constructor & Destructor Documentation

◆ qua() [1/3]

template<Primitive T>
mal_math::qua< T >::qua ( )
inlineconstexpr

Default constructor initializes the quaternion to its default state (1, 0, 0, 0).

Definition at line 30 of file quaternion.hpp.

◆ qua() [2/3]

template<Primitive T>
mal_math::qua< T >::qua ( T w,
T x,
T y,
T z )
explicitconstexpr

Constructs a quaternion from its four components.

Definition at line 7 of file quaternion.inl.

◆ qua() [3/3]

template<Primitive T>
mal_math::qua< T >::qua ( T radians,
vec< 3, T > axis )
explicitconstexpr

Constructs a quaternion from an angle and rotation axis.

Parameters
radiansAngle of rotation in radians.
axisA 3D vector representing the axis of rotation.

Definition at line 22 of file quaternion.inl.

Here is the call graph for this function:

Member Function Documentation

◆ as_mat3()

template<Primitive T>
mat< 3, 3, T > mal_math::qua< T >::as_mat3 ( ) const
nodiscardconstexprnoexcept

Converts the quaternion to a 3x3 rotation matrix.

Returns
A 3x3 matrix representing the rotation defined by this quaternion.

Definition at line 36 of file quaternion.inl.

◆ as_mat4()

template<Primitive T>
mat< 4, 4, T > mal_math::qua< T >::as_mat4 ( ) const
nodiscardconstexprnoexcept

Converts the quaternion to a 4x4 rotation matrix.

Returns
A 4x4 matrix representing the rotation defined by this quaternion.

Definition at line 64 of file quaternion.inl.

◆ axis()

template<Primitive T>
vec< 3, T > const & mal_math::qua< T >::axis ( ) const
nodiscardconstexprnoexcept

Retrieves the axis of rotation from the quaternion.

Returns
A 3D vector representing the axis of rotation.

Definition at line 69 of file quaternion.inl.

◆ operator*=() [1/2]

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator*= ( qua< U > const & other)
constexprnoexcept

Multiplies this quaternion with another quaternion.

Template Parameters
UPrimitive type of the other quaternion.
Parameters
otherThe other quaternion to multiply with this one.
Returns
Reference to the modified quaternion.

Definition at line 132 of file quaternion.inl.

◆ operator*=() [2/2]

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator*= ( U const value)
constexprnoexcept

Multiplies the quaternion by a scalar value.

Template Parameters
UPrimitive type of the scalar.
Parameters
valueScalar value to multiply the quaternion with.
Returns
Reference to the modified quaternion.

Definition at line 92 of file quaternion.inl.

◆ operator+()

template<Primitive T>
qua< T > const & mal_math::qua< T >::operator+ ( ) const
nodiscardconstexprnoexcept

Unary plus operator.

Returns
A reference to the quaternion.

Definition at line 80 of file quaternion.inl.

◆ operator+=()

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator+= ( qua< U > const & other)
constexprnoexcept

Adds another quaternion to this quaternion component-wise.

Template Parameters
UPrimitive type of the other quaternion.
Parameters
otherThe other quaternion to be added to this one.
Returns
Reference to the modified quaternion.

Definition at line 112 of file quaternion.inl.

◆ operator-()

template<Primitive T>
qua< T > mal_math::qua< T >::operator- ( ) const
nodiscardconstexprnoexcept

Unary minus operator. Inverts the sign of each component.

Returns
A new quaternion with inverted components.

Definition at line 85 of file quaternion.inl.

◆ operator-=()

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator-= ( qua< U > const & other)
constexprnoexcept

Subtracts another quaternion from this quaternion component-wise.

Template Parameters
UPrimitive type of the other quaternion.
Parameters
otherThe other quaternion to be subtracted from this one.
Returns
Reference to the modified quaternion.

Definition at line 122 of file quaternion.inl.

◆ operator/=() [1/2]

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator/= ( qua< U > const & other)
constexprnoexcept

Divides this quaternion by another quaternion.

Note
Ensure that the divisor quaternion isn't near zero to avoid inaccuracies or division by zero errors.
Template Parameters
UPrimitive type of the other quaternion.
Parameters
otherThe divisor quaternion.
Returns
Reference to the modified quaternion.

Definition at line 145 of file quaternion.inl.

◆ operator/=() [2/2]

template<Primitive T>
template<Primitive U>
qua< T > & mal_math::qua< T >::operator/= ( U const value)
constexprnoexcept

Divides the components of the quaternion by a scalar value.

Template Parameters
UPrimitive type of the scalar.
Parameters
valueScalar value to divide the quaternion components by.
Returns
Reference to the modified quaternion.

Definition at line 102 of file quaternion.inl.

◆ operator[]() [1/2]

template<Primitive T>
T & mal_math::qua< T >::operator[] ( size_t i)
nodiscardconstexpr

Accesses a component of the quaternion by index.

Parameters
iIndex of the component (0-based).
Returns
Reference to the i-th component of the quaternion.

Definition at line 155 of file quaternion.inl.

◆ operator[]() [2/2]

template<Primitive T>
T const & mal_math::qua< T >::operator[] ( size_t i) const
nodiscardconstexpr

Accesses a component of the quaternion by index (const version).

Parameters
iIndex of the component (0-based).
Returns
Const reference to the i-th component of the quaternion.

Definition at line 161 of file quaternion.inl.

◆ radians()

template<Primitive T>
T mal_math::qua< T >::radians ( ) const
nodiscardconstexprnoexcept

Retrieves the angle of rotation from the quaternion.

Returns
Angle of rotation in radians.

Definition at line 74 of file quaternion.inl.

◆ reset()

template<Primitive T>
void mal_math::qua< T >::reset ( )
constexpr

Resets the quaternion to its default state (1, 0, 0, 0).

Definition at line 15 of file quaternion.inl.

Member Data Documentation

◆ [union]

union { ... } mal_math::qua< T >

Unions allowing various ways to access quaternion components.

◆ a

template<Primitive T>
T mal_math::qua< T >::a

Definition at line 160 of file quaternion.hpp.

◆ b

template<Primitive T>
T mal_math::qua< T >::b

Definition at line 156 of file quaternion.hpp.

◆ data

template<Primitive T>
std::array<T, size> mal_math::qua< T >::data

Definition at line 163 of file quaternion.hpp.

◆ g

template<Primitive T>
T mal_math::qua< T >::g

Definition at line 152 of file quaternion.hpp.

◆ p

template<Primitive T>
T mal_math::qua< T >::p

Definition at line 156 of file quaternion.hpp.

◆ q

template<Primitive T>
T mal_math::qua< T >::q

Definition at line 160 of file quaternion.hpp.

◆ r

template<Primitive T>
T mal_math::qua< T >::r

Definition at line 148 of file quaternion.hpp.

◆ s

template<Primitive T>
T mal_math::qua< T >::s

Definition at line 148 of file quaternion.hpp.

◆ size

template<Primitive T>
size_t mal_math::qua< T >::size = 4
staticconstexpr

Represents the number of components in the quaternion.

Definition at line 28 of file quaternion.hpp.

◆ t

template<Primitive T>
T mal_math::qua< T >::t

Definition at line 152 of file quaternion.hpp.

◆ w

template<Primitive T>
T mal_math::qua< T >::w

Definition at line 160 of file quaternion.hpp.

◆ x

template<Primitive T>
T mal_math::qua< T >::x

Definition at line 148 of file quaternion.hpp.

◆ y

template<Primitive T>
T mal_math::qua< T >::y

Definition at line 152 of file quaternion.hpp.

◆ z

template<Primitive T>
T mal_math::qua< T >::z

Definition at line 156 of file quaternion.hpp.


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