Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
Phyth::Quaternion< T > Struct Template Reference

Quaternion with Quantity components. More...

#include <Quaternion.hpp>

Public Member Functions

constexpr Quaternion ()
 Default constructor - identity quaternion (1 + 0i + 0j + 0k)
 
constexpr Quaternion (T w_, T x_, T y_, T z_)
 Construct from scalar and vector components.
 
constexpr Quaternion (T w_, const Vector3< T > &v_)
 Construct from scalar and vector parts.
 
constexpr Quaternion (const Vector3< T > &v_)
 Construct from vector part only (scalar part = 0)
 
constexpr Vector3< T > ToVector () const
 Extract the vector part as a Vector3.
 
constexpr T ToQuantity () const
 Extract the scalar part.
 
auto LengthSquared () const
 Squared magnitude.
 
auto Length () const
 Magnitude (sqrt of LengthSquared)
 
constexpr Quaternion Conjugated () const
 Conjugate: (w + xi + yj + zk) -> (w - xi - yj - zk)
 
auto Inversed () const
 Inverse: q^(-1) = conjugate / |q|^2.
 
auto Normalized () const
 Normalized quaternion (unit quaternion)
 
bool IsUnit () const
 Check if the quaternion is a unit quaternion.
 
auto Rotated (const Vector3< T > &v) const
 Rotate a vector by this quaternion.
 
template<typename U >
constexpr auto operator* (const Quaternion< U > &other) const
 Quaternion multiplication.
 
template<typename U >
constexpr auto operator+ (const Quaternion< U > &other) const
 Quaternion addition.
 
template<typename U >
constexpr auto operator- (const Quaternion< U > &other) const
 Quaternion subtraction.
 
constexpr auto operator- () const
 Unary negation.
 
template<typename U >
constexpr auto operator* (Quantity< U > scalar) const
 Scalar multiplication.
 
template<typename U >
constexpr auto operator/ (U scalar) const
 Scalar division.
 
template<typename U >
constexpr Quaternion & operator+= (const Quaternion< U > &other)
 Addition assignment.
 
template<typename U >
constexpr Quaternion & operator-= (const Quaternion< U > &other)
 Subtraction assignment.
 
template<typename U >
constexpr Quaternion & operator*= (U scalar)
 Scalar multiplication assignment.
 
template<typename U >
constexpr Quaternion & operator/= (U scalar)
 Scalar division assignment.
 
template<typename U >
constexpr bool operator== (const Quaternion< U > &other) const
 Equality comparison.
 
template<typename U >
constexpr bool operator!= (const Quaternion< U > &other) const
 Inequality comparison.
 
template<typename UnitT >
Quaternion< Quantity< UnitT > > as () const
 Convert all components to a different unit of the same dimension.
 
template<typename U >
constexpr auto Dot (const Quaternion< U > &other) const
 Dot product of two quaternions.
 
constexpr T & operator[] (int i)
 Index-based access (non-const)
 
constexpr const T & operator[] (int i) const
 Index-based access (const)
 

Static Public Member Functions

static Quaternion FromAxisAngle (const Vector3< T > &axis, const Scalar theta)
 Create a unit quaternion from axis and angle.
 
static Quaternion FromEuler (const Scalar yaw, const Scalar pitch, const Scalar roll)
 Create a unit quaternion from Euler angles (ZYX convention)
 
template<typename U >
static auto Slerp (const Quaternion &q1, const Quaternion< U > &q2, const Scalar t)
 Spherical linear interpolation between two quaternions.
 

Public Attributes

T w
 
T x
 
T y
 
T z
 

Friends

std::ostream & operator<< (std::ostream &os, const Quaternion &q)
 Stream insertion operator for Quaternion.
 

Detailed Description

template<typename T>
struct Phyth::Quaternion< T >

Quaternion with Quantity components.

A quaternion represents rotations in 3D space. It consists of a scalar part (w) and a vector part (x, y, z).

This implementation uses Quantity types for all components, ensuring that quaternion operations are dimensionally consistent. For rotation quaternions, all components should be dimensionless (Scalar type).

Template Parameters
TComponent type (must be a Quantity instantiation, typically Scalar)

Example: using Rotation = Quaternion<Scalar>;

// Rotate a vector by 90 degrees around Z axis auto q = Rotation::FromAxisAngle(Vector3<Scalar>(0, 0, 1), 90.0_deg); auto v = Vector3<Quantity<Meter>>(1.0_m, 0_m, 0_m); auto rotated = q.Rotated(v); // (0_m, 1_m, 0_m)

Definition at line 33 of file Quaternion.hpp.

Constructor & Destructor Documentation

◆ Quaternion() [1/4]

template<typename T >
constexpr Phyth::Quaternion< T >::Quaternion ( )
inlineconstexpr

Default constructor - identity quaternion (1 + 0i + 0j + 0k)

Definition at line 49 of file Quaternion.hpp.

◆ Quaternion() [2/4]

template<typename T >
constexpr Phyth::Quaternion< T >::Quaternion ( T  w_,
T  x_,
T  y_,
T  z_ 
)
inlineconstexpr

Construct from scalar and vector components.

Parameters
w_Scalar part
x_X component of vector part
y_Y component of vector part
z_Z component of vector part

Definition at line 60 of file Quaternion.hpp.

◆ Quaternion() [3/4]

template<typename T >
constexpr Phyth::Quaternion< T >::Quaternion ( T  w_,
const Vector3< T > &  v_ 
)
inlineconstexpr

Construct from scalar and vector parts.

Parameters
w_Scalar part
v_Vector part (x, y, z)

Definition at line 69 of file Quaternion.hpp.

◆ Quaternion() [4/4]

template<typename T >
constexpr Phyth::Quaternion< T >::Quaternion ( const Vector3< T > &  v_)
inlineexplicitconstexpr

Construct from vector part only (scalar part = 0)

This creates a pure quaternion (0 + v), useful for representing vectors in quaternion form.

Parameters
v_Vector part

Definition at line 80 of file Quaternion.hpp.

Member Function Documentation

◆ as()

template<typename T >
template<typename UnitT >
Quaternion< Quantity< UnitT > > Phyth::Quaternion< T >::as ( ) const
inline

Convert all components to a different unit of the same dimension.

Template Parameters
UnitTThe target unit type
Returns
Quaternion with each component converted to UnitT

Definition at line 351 of file Quaternion.hpp.

◆ Conjugated()

template<typename T >
constexpr Quaternion Phyth::Quaternion< T >::Conjugated ( ) const
inlineconstexpr

Conjugate: (w + xi + yj + zk) -> (w - xi - yj - zk)

For unit quaternions, the conjugate is also the inverse.

Definition at line 157 of file Quaternion.hpp.

◆ Dot()

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::Dot ( const Quaternion< U > &  other) const
inlineconstexpr

Dot product of two quaternions.

Template Parameters
UComponent type of the other quaternion
Parameters
otherThe other quaternion
Returns
The dot product as a Quantity

Definition at line 408 of file Quaternion.hpp.

◆ FromAxisAngle()

template<typename T >
static Quaternion Phyth::Quaternion< T >::FromAxisAngle ( const Vector3< T > &  axis,
const Scalar  theta 
)
inlinestatic

Create a unit quaternion from axis and angle.

Parameters
axisUnit vector axis of rotation (must be normalized)
thetaRotation angle in radians (dimensionless)
Returns
Unit quaternion representing the rotation

Example: auto axis = Vector3<Scalar>(0, 0, 1); auto q = Quaternion<Scalar>::FromAxisAngle(axis, 90.0_deg);

Definition at line 94 of file Quaternion.hpp.

◆ FromEuler()

template<typename T >
static Quaternion Phyth::Quaternion< T >::FromEuler ( const Scalar  yaw,
const Scalar  pitch,
const Scalar  roll 
)
inlinestatic

Create a unit quaternion from Euler angles (ZYX convention)

Converts yaw-pitch-roll Euler angles to a quaternion. The rotation order is: first roll (X), then pitch (Y), then yaw (Z).

Parameters
yawRotation around Z axis (radians, dimensionless)
pitchRotation around Y axis (radians, dimensionless)
rollRotation around X axis (radians, dimensionless)
Returns
Unit quaternion representing the combined rotation

Definition at line 112 of file Quaternion.hpp.

◆ Inversed()

template<typename T >
auto Phyth::Quaternion< T >::Inversed ( ) const
inline

Inverse: q^(-1) = conjugate / |q|^2.

For unit quaternions, inverse equals conjugate.

Definition at line 166 of file Quaternion.hpp.

◆ IsUnit()

template<typename T >
bool Phyth::Quaternion< T >::IsUnit ( ) const
inline

Check if the quaternion is a unit quaternion.

Returns true if |w^2 + x^2 + y^2 + z^2 - 1| < Config::tolerance

Definition at line 193 of file Quaternion.hpp.

◆ Length()

template<typename T >
auto Phyth::Quaternion< T >::Length ( ) const
inline

Magnitude (sqrt of LengthSquared)

Definition at line 148 of file Quaternion.hpp.

◆ LengthSquared()

template<typename T >
auto Phyth::Quaternion< T >::LengthSquared ( ) const
inline

Squared magnitude.

Returns w^2 + x^2 + y^2 + z^2

Definition at line 143 of file Quaternion.hpp.

◆ Normalized()

template<typename T >
auto Phyth::Quaternion< T >::Normalized ( ) const
inline

Normalized quaternion (unit quaternion)

Definition at line 178 of file Quaternion.hpp.

◆ operator!=()

template<typename T >
template<typename U >
constexpr bool Phyth::Quaternion< T >::operator!= ( const Quaternion< U > &  other) const
inlineconstexpr

Inequality comparison.

Definition at line 340 of file Quaternion.hpp.

◆ operator*() [1/2]

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::operator* ( const Quaternion< U > &  other) const
inlineconstexpr

Quaternion multiplication.

The product of two quaternions represents the composition of their rotations.

Definition at line 223 of file Quaternion.hpp.

◆ operator*() [2/2]

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::operator* ( Quantity< U >  scalar) const
inlineconstexpr

Scalar multiplication.

Parameters
scalarThe scalar multiplier (must be a Quantity, typically Scalar)
Returns
Quaternion with all components multiplied by scalar

Definition at line 267 of file Quaternion.hpp.

◆ operator*=()

template<typename T >
template<typename U >
constexpr Quaternion & Phyth::Quaternion< T >::operator*= ( U  scalar)
inlineconstexpr

Scalar multiplication assignment.

Definition at line 314 of file Quaternion.hpp.

◆ operator+()

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::operator+ ( const Quaternion< U > &  other) const
inlineconstexpr

Quaternion addition.

Definition at line 235 of file Quaternion.hpp.

◆ operator+=()

template<typename T >
template<typename U >
constexpr Quaternion & Phyth::Quaternion< T >::operator+= ( const Quaternion< U > &  other)
inlineconstexpr

Addition assignment.

Definition at line 294 of file Quaternion.hpp.

◆ operator-() [1/2]

template<typename T >
constexpr auto Phyth::Quaternion< T >::operator- ( ) const
inlineconstexpr

Unary negation.

Definition at line 256 of file Quaternion.hpp.

◆ operator-() [2/2]

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::operator- ( const Quaternion< U > &  other) const
inlineconstexpr

Quaternion subtraction.

Definition at line 246 of file Quaternion.hpp.

◆ operator-=()

template<typename T >
template<typename U >
constexpr Quaternion & Phyth::Quaternion< T >::operator-= ( const Quaternion< U > &  other)
inlineconstexpr

Subtraction assignment.

Definition at line 304 of file Quaternion.hpp.

◆ operator/()

template<typename T >
template<typename U >
constexpr auto Phyth::Quaternion< T >::operator/ ( U  scalar) const
inlineconstexpr

Scalar division.

Parameters
scalarThe scalar divisor (must be a Quantity, typically Scalar)
Returns
Quaternion with all components divided by scalar

Definition at line 283 of file Quaternion.hpp.

◆ operator/=()

template<typename T >
template<typename U >
constexpr Quaternion & Phyth::Quaternion< T >::operator/= ( U  scalar)
inlineconstexpr

Scalar division assignment.

Definition at line 324 of file Quaternion.hpp.

◆ operator==()

template<typename T >
template<typename U >
constexpr bool Phyth::Quaternion< T >::operator== ( const Quaternion< U > &  other) const
inlineconstexpr

Equality comparison.

Definition at line 334 of file Quaternion.hpp.

◆ operator[]() [1/2]

template<typename T >
constexpr T & Phyth::Quaternion< T >::operator[] ( int  i)
inlineconstexpr

Index-based access (non-const)

Parameters
iIndex: 0 = w, 1 = x, 2 = y, 3 = z
Returns
Reference to the component

Definition at line 418 of file Quaternion.hpp.

◆ operator[]() [2/2]

template<typename T >
constexpr const T & Phyth::Quaternion< T >::operator[] ( int  i) const
inlineconstexpr

Index-based access (const)

Parameters
iIndex: 0 = w, 1 = x, 2 = y, 3 = z
Returns
Const reference to the component

Definition at line 428 of file Quaternion.hpp.

◆ Rotated()

template<typename T >
auto Phyth::Quaternion< T >::Rotated ( const Vector3< T > &  v) const
inline

Rotate a vector by this quaternion.

The rotation is performed as: q * (0 + v) * q^(-1)

Parameters
vThe vector to rotate
Returns
The rotated vector

Example: auto q = Quaternion<Scalar>::FromAxisAngle(axis, 90.0_deg); auto v = Vector3<Quantity<Meter>>(1.0_m, 0_m, 0_m); auto rotated = q.Rotated(v);

Definition at line 210 of file Quaternion.hpp.

◆ Slerp()

template<typename T >
template<typename U >
static auto Phyth::Quaternion< T >::Slerp ( const Quaternion< T > &  q1,
const Quaternion< U > &  q2,
const Scalar  t 
)
inlinestatic

Spherical linear interpolation between two quaternions.

Interpolates along the shortest path on the unit sphere. Handles negative dot products to maintain the shortest path.

Template Parameters
UComponent type of the second quaternion
Parameters
q1Start quaternion
q2End quaternion
tInterpolation parameter [0, 1] (dimensionless scalar)
Returns
Interpolated quaternion (unit quaternion)

Example: auto q1 = Quaternion<Scalar>(); // identity auto q2 = Quaternion<Scalar>::FromAxisAngle(axis, 90.0_deg); auto q_mid = Quaternion<Scalar>::Slerp(q1, q2, 0.5); // 45-degree rotation

Definition at line 378 of file Quaternion.hpp.

◆ ToQuantity()

template<typename T >
constexpr T Phyth::Quaternion< T >::ToQuantity ( ) const
inlineconstexpr

Extract the scalar part.

Definition at line 134 of file Quaternion.hpp.

◆ ToVector()

template<typename T >
constexpr Vector3< T > Phyth::Quaternion< T >::ToVector ( ) const
inlineconstexpr

Extract the vector part as a Vector3.

Definition at line 129 of file Quaternion.hpp.

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
std::ostream & operator<< ( std::ostream &  os,
const Quaternion< T > &  q 
)
friend

Stream insertion operator for Quaternion.

Output format: "(w + xi + yj + zk)" where each component uses Quantity's stream formatting.

Definition at line 438 of file Quaternion.hpp.

Member Data Documentation

◆ w

template<typename T >
T Phyth::Quaternion< T >::w

Scalar (real) part

Definition at line 38 of file Quaternion.hpp.

◆ x

template<typename T >
T Phyth::Quaternion< T >::x

X component of the vector (imaginary) part

Definition at line 40 of file Quaternion.hpp.

◆ y

template<typename T >
T Phyth::Quaternion< T >::y

Y component of the vector (imaginary) part

Definition at line 42 of file Quaternion.hpp.

◆ z

template<typename T >
T Phyth::Quaternion< T >::z

Z component of the vector (imaginary) part

Definition at line 44 of file Quaternion.hpp.


The documentation for this struct was generated from the following file: