|
Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
|
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. | |
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).
| T | Component 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.
|
inlineconstexpr |
Default constructor - identity quaternion (1 + 0i + 0j + 0k)
Definition at line 49 of file Quaternion.hpp.
Construct from scalar and vector components.
| 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.
Construct from scalar and vector parts.
| w_ | Scalar part |
| v_ | Vector part (x, y, z) |
Definition at line 69 of file Quaternion.hpp.
Construct from vector part only (scalar part = 0)
This creates a pure quaternion (0 + v), useful for representing vectors in quaternion form.
| v_ | Vector part |
Definition at line 80 of file Quaternion.hpp.
|
inline |
Convert all components to a different unit of the same dimension.
| UnitT | The target unit type |
Definition at line 351 of file Quaternion.hpp.
|
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.
|
inlineconstexpr |
Dot product of two quaternions.
| U | Component type of the other quaternion |
| other | The other quaternion |
Definition at line 408 of file Quaternion.hpp.
|
inlinestatic |
Create a unit quaternion from axis and angle.
| axis | Unit vector axis of rotation (must be normalized) |
| theta | Rotation angle in radians (dimensionless) |
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.
|
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).
| yaw | Rotation around Z axis (radians, dimensionless) |
| pitch | Rotation around Y axis (radians, dimensionless) |
| roll | Rotation around X axis (radians, dimensionless) |
Definition at line 112 of file Quaternion.hpp.
|
inline |
Inverse: q^(-1) = conjugate / |q|^2.
For unit quaternions, inverse equals conjugate.
Definition at line 166 of file Quaternion.hpp.
|
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.
|
inline |
Magnitude (sqrt of LengthSquared)
Definition at line 148 of file Quaternion.hpp.
|
inline |
|
inline |
Normalized quaternion (unit quaternion)
Definition at line 178 of file Quaternion.hpp.
|
inlineconstexpr |
Inequality comparison.
Definition at line 340 of file Quaternion.hpp.
|
inlineconstexpr |
Quaternion multiplication.
The product of two quaternions represents the composition of their rotations.
Definition at line 223 of file Quaternion.hpp.
|
inlineconstexpr |
Scalar multiplication.
| scalar | The scalar multiplier (must be a Quantity, typically Scalar) |
Definition at line 267 of file Quaternion.hpp.
|
inlineconstexpr |
Scalar multiplication assignment.
Definition at line 314 of file Quaternion.hpp.
|
inlineconstexpr |
Quaternion addition.
Definition at line 235 of file Quaternion.hpp.
|
inlineconstexpr |
Addition assignment.
Definition at line 294 of file Quaternion.hpp.
|
inlineconstexpr |
Unary negation.
Definition at line 256 of file Quaternion.hpp.
|
inlineconstexpr |
Quaternion subtraction.
Definition at line 246 of file Quaternion.hpp.
|
inlineconstexpr |
Subtraction assignment.
Definition at line 304 of file Quaternion.hpp.
|
inlineconstexpr |
Scalar division.
| scalar | The scalar divisor (must be a Quantity, typically Scalar) |
Definition at line 283 of file Quaternion.hpp.
|
inlineconstexpr |
Scalar division assignment.
Definition at line 324 of file Quaternion.hpp.
|
inlineconstexpr |
Equality comparison.
Definition at line 334 of file Quaternion.hpp.
|
inlineconstexpr |
Index-based access (non-const)
| i | Index: 0 = w, 1 = x, 2 = y, 3 = z |
Definition at line 418 of file Quaternion.hpp.
|
inlineconstexpr |
Index-based access (const)
| i | Index: 0 = w, 1 = x, 2 = y, 3 = z |
Definition at line 428 of file Quaternion.hpp.
Rotate a vector by this quaternion.
The rotation is performed as: q * (0 + v) * q^(-1)
| v | The vector to rotate |
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.
|
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.
| U | Component type of the second quaternion |
| q1 | Start quaternion |
| q2 | End quaternion |
| t | Interpolation parameter [0, 1] (dimensionless scalar) |
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.
Extract the scalar part.
Definition at line 134 of file Quaternion.hpp.
|
inlineconstexpr |
Extract the vector part as a Vector3.
Definition at line 129 of file Quaternion.hpp.
|
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.
| T Phyth::Quaternion< T >::w |
Scalar (real) part
Definition at line 38 of file Quaternion.hpp.
| T Phyth::Quaternion< T >::x |
X component of the vector (imaginary) part
Definition at line 40 of file Quaternion.hpp.
| T Phyth::Quaternion< T >::y |
Y component of the vector (imaginary) part
Definition at line 42 of file Quaternion.hpp.
| T Phyth::Quaternion< T >::z |
Z component of the vector (imaginary) part
Definition at line 44 of file Quaternion.hpp.