Quaternion types and operations. More...
Classes | |
| struct | Crux::Quat< T > |
| Templated quaternion representation. More... | |
Functions | |
| template<typename T> | |
| constexpr T | Crux::QuatDot (const Quat< T > &quatA, const Quat< T > &quatB) noexcept |
| Compute the dot product of two quaternions. | |
| template<typename T> | |
| constexpr T | Crux::QuatLengthSq (const Quat< T > &quat) noexcept |
| Compute squared length (norm) of a quaternion. | |
| template<typename T> | |
| T | Crux::QuatLength (const Quat< T > &quat) noexcept |
| Compute Euclidean length (norm) of a quaternion. | |
| template<typename T> | |
| constexpr T | Crux::QuatLengthCT (const Quat< T > &quat) noexcept |
| Compute Euclidean length (norm) of a quaternion (compile-time version). | |
| template<typename T> | |
| constexpr Quat< T > | Crux::QuatConjugate (const Quat< T > &quat) noexcept |
| Compute the conjugate of a quaternion. | |
| template<typename T> | |
| constexpr Quat< T > | Crux::QuatInverse (const Quat< T > &quat) noexcept |
| Compute the multiplicative inverse of a quaternion. | |
| template<typename T> | |
| T | Crux::QuatNormalizeInplace (Quat< T > &quat) noexcept |
| Normalize quaternion in-place. | |
| template<typename T> | |
| Quat< T > | Crux::QuatNormalize (Quat< T > quat) noexcept |
| Return normalized copy of quaternion. | |
| template<typename T> | |
| constexpr Quat< T > | Crux::QuatMul (const Quat< T > &quatA, const Quat< T > &quatB) noexcept |
| Hamilton product of two quaternions (composition of rotations). | |
| template<typename T> | |
| constexpr Vector< T, 3 > | Crux::QuatRotate (const Quat< T > &quat, const Vector< T, 3 > &vec) noexcept |
| Rotate a 3D vector by a quaternion (optimized form). | |
| template<typename T> | |
| Quat< T > | Crux::QuatFromAxisAngle (const Vector< T, 3 > &axis, T angleRadians) noexcept |
| Construct quaternion from axis-angle representation. | |
| template<typename T> | |
| void | Crux::QuatToAxisAngle (const Quat< T > &quat, Vector< T, 3 > &outAxis, T &outAngle) noexcept |
| Decompose quaternion into axis-angle representation. | |
| template<typename T> | |
| Quat< T > | Crux::QuatFromEuler (T roll, T pitch, T yaw) noexcept |
| Construct quaternion from Euler angles (roll, pitch, yaw). | |
| template<typename T> | |
| void | Crux::QuatToEuler (const Quat< T > &quat, T &outRoll, T &outPitch, T &outYaw) noexcept |
| Convert quaternion to Euler angles (roll, pitch, yaw). | |
| template<typename T> | |
| Quat< T > | Crux::QuatLerp (const Quat< T > &quatA, const Quat< T > &quatB, T t) noexcept |
| Linear interpolation (LERP) between two quaternions. | |
| template<typename T> | |
| Quat< T > | Crux::QuatNlerp (const Quat< T > &quatA, const Quat< T > &quatB, T t) noexcept |
| Normalized linear interpolation (NLERP) between two quaternions. | |
| template<typename T> | |
| Quat< T > | Crux::QuatSlerp (const Quat< T > &quatA, const Quat< T > &quatB, T t) noexcept |
| Spherical linear interpolation (SLERP) between two quaternions. | |
| template<typename T> | |
| Matrix< T, 3, 3 > | Crux::QuatToMat3 (const Quat< T > &quatIn) noexcept |
| Convert quaternion to 3x3 rotation matrix (row-major). | |
| template<typename T> | |
| Matrix< T, 4, 4 > | Crux::ToMat4 (const Quat< T > &quat) noexcept |
| Convert quaternion to 4x4 rotation matrix. | |
| template<typename T> | |
| Quat< T > | Crux::QuatFromMat3 (const Matrix< T, 3, 3 > &mat) noexcept |
| Convert a 3x3 rotation matrix to a quaternion. | |
| template<typename T> | |
| Quat< T > | Crux::operator* (const Quat< T > &a, const Quat< T > &b) noexcept |
| Quaternion multiplication operator (Hamilton product). | |
| template<typename T> | |
| Quat< T > | Crux::operator* (T s, const Quat< T > &q) noexcept |
| Scalar * quaternion multiplication (commutative for scalar). | |
Quaternion types and operations.
Contains quaternion definitions, transformations, and related algorithms.
|
inlinenoexcept |
Quaternion multiplication operator (Hamilton product).
| T | Scalar type. |
| a | Left quaternion operand. |
| b | Right quaternion operand. |
Equivalent to QuatMul(a, b). See QuatMul for detailed documentation.
Scalar * quaternion multiplication (commutative for scalar).
| T | Scalar type. |
| s | Scalar value. |
| q | Quaternion. |
Scales all components of the quaternion by the scalar:
\[s \cdot q = (sx, sy, sz, sw) \]
This operation is commutative: \(s \cdot q = q \cdot s\).
Compute the conjugate of a quaternion.
| T | Scalar type. |
| quat | Input quaternion. |
For quaternion \(q = (x, y, z, w)\) with vector part \(\vec{v} = (x, y, z)\) and scalar part \(w\), the conjugate is defined as:
\[q^* = (-\vec{v}, w) = (-x, -y, -z, w) \]
The conjugate represents the inverse rotation of a unit quaternion: \(q \otimes q^* = q^* \otimes q = (0, 0, 0, 1)\) for unit quaternions.
|
constexprnoexcept |
Compute the dot product of two quaternions.
| T | Scalar type. |
| quatA | First quaternion. |
| quatB | Second quaternion. |
For quaternions \(a = (x_a, y_a, z_a, w_a)\) and \(b = (x_b, y_b, z_b, w_b)\), the dot product is defined as:
\[a \cdot b = x_a x_b + y_a y_b + z_a z_b + w_a w_b \]
This operation is commutative and is used to measure the angular similarity between two quaternions.
|
inlinenoexcept |
Construct quaternion from axis-angle representation.
| T | Scalar type. |
| axis | Rotation axis (should be normalized). |
| angleRadians | Rotation angle in radians. |
Constructs a quaternion from axis-angle form using:
\[q = \left(\vec{a} \sin\frac{\theta}{2}, \cos\frac{\theta}{2}\right) \]
where \(\vec{a}\) is the rotation axis and \(\theta\) is the rotation angle.
This representation is unique up to sign (both \(q\) and \(-q\) represent the same rotation).
|
inlinenoexcept |
Construct quaternion from Euler angles (roll, pitch, yaw).
| T | Scalar type. |
| roll | Rotation about X-axis in radians. |
| pitch | Rotation about Y-axis in radians. |
| yaw | Rotation about Z-axis in radians. |
Constructs a quaternion from Euler angles using the ZYX convention:
\[q = q_z(\text{yaw}) \otimes q_y(\text{pitch}) \otimes q_x(\text{roll}) \]
where individual axis quaternions are:
\[\begin{align*} q_x(\alpha) &= \left(\sin\frac{\alpha}{2}, 0, 0, \cos\frac{\alpha}{2}\right) \\ q_y(\beta) &= \left(0, \sin\frac{\beta}{2}, 0, \cos\frac{\beta}{2}\right) \\ q_z(\gamma) &= \left(0, 0, \sin\frac{\gamma}{2}, \cos\frac{\gamma}{2}\right) \end{align*} \]
The implementation uses the expanded half-angle formula:
\[\begin{align*} w &= c_r c_p c_y + s_r s_p s_y \\ x &= s_r c_p c_y - c_r s_p s_y \\ y &= c_r s_p c_y + s_r c_p s_y \\ z &= c_r c_p s_y - s_r s_p c_y \end{align*} \]
where \(s_r = \sin(\text{roll}/2)\), \(c_r = \cos(\text{roll}/2)\), etc.
Convert a 3x3 rotation matrix to a quaternion.
| T | Scalar type. |
| mat | Input 3x3 rotation matrix (row-major). |
Converts an orthonormal rotation matrix to a quaternion using a numerically stable algorithm that avoids division by small numbers:
Given a rotation matrix:
\[R = \begin{pmatrix} m_{00} & m_{01} & m_{02} \\ m_{10} & m_{11} & m_{12} \\ m_{20} & m_{21} & m_{22} \end{pmatrix} \]
The algorithm selects the largest diagonal element to maximize numerical stability:
The remaining components are computed using matrix relationships:
\[\begin{align*} x &= \frac{m_{21} - m_{12}}{4w} \quad \text{(when w is largest)} \\ y &= \frac{m_{02} - m_{20}}{4w} \\ z &= \frac{m_{10} - m_{01}}{4w} \end{align*} \]
Compute the multiplicative inverse of a quaternion.
| T | Scalar type. |
| quat | Input quaternion. |
For quaternion \(q\), the multiplicative inverse is defined as:
\[q^{-1} = \frac{q^*}{\|q\|^2} \]
where \(q^*\) is the conjugate and \(\|q\|^2\) is the squared length.
The inverse satisfies: \(q \otimes q^{-1} = q^{-1} \otimes q = (0, 0, 0, 1)\).
|
inlinenoexcept |
Compute Euclidean length (norm) of a quaternion.
| T | Scalar type. |
| quat | Quaternion to measure. |
For quaternion \(q = (x, y, z, w)\), the length is defined as:
\[\|q\| = \sqrt{x^2 + y^2 + z^2 + w^2} = \sqrt{q \cdot q} \]
|
constexprnoexcept |
Compute Euclidean length (norm) of a quaternion (compile-time version).
| T | Scalar type. |
| quat | Quaternion to measure. |
For quaternion \(q = (x, y, z, w)\), the length is defined as:
\[\|q\| = \sqrt{x^2 + y^2 + z^2 + w^2} = \sqrt{q \cdot q} \]
|
constexprnoexcept |
Compute squared length (norm) of a quaternion.
| T | Scalar type. |
| quat | Quaternion to measure. |
For quaternion \(q = (x, y, z, w)\), the squared length is defined as:
\[\|q\|^2 = q \cdot q = x^2 + y^2 + z^2 + w^2 \]
This operation is more efficient than computing the actual length since it avoids the square root computation.
|
inlinenoexcept |
Linear interpolation (LERP) between two quaternions.
| T | Scalar type. |
| quatA | Start quaternion. |
| quatB | End quaternion. |
| t | Interpolation parameter in [0,1]. |
Performs component-wise linear interpolation:
\[q(t) = (1-t)a + tb = a + t(b - a) \]
Component-wise this yields:
\[\begin{align*} x(t) &= x_a + t(x_b - x_a) \\ y(t) &= y_a + t(y_b - y_a) \\ z(t) &= z_a + t(z_b - z_a) \\ w(t) &= w_a + t(w_b - w_a) \end{align*} \]
|
constexprnoexcept |
Hamilton product of two quaternions (composition of rotations).
| T | Scalar type. |
| quatA | Left quaternion operand. |
| quatB | Right quaternion operand. |
For scalar-last quaternions \(q = (x, y, z, w)\) with vector part \(\vec{v}=(x, y, z)\) and scalar \(w\), the Hamilton product is defined as:
\[q = a \otimes b = \Bigl( \vec{v}_a \times \vec{v}_b + w_a \vec{v}_b + w_b \vec{v}_a,\; w_a w_b - \vec{v}_a \cdot \vec{v}_b \Bigr) \]
where:
This operation is non-commutative: the order of operands affects the resulting rotation.
|
inlinenoexcept |
Normalized linear interpolation (NLERP) between two quaternions.
| T | Scalar type. |
| quatA | Start quaternion. |
| quatB | End quaternion. |
| t | Interpolation parameter in [0,1]. |
NLERP performs linear interpolation followed by normalization:
\[\text{nlerp}(a, b, t) = \frac{(1-t)a + tb}{\|(1-t)a + tb\|} = \frac{a + t(b - a)}{\|a + t(b - a)\|} \]
This provides a faster alternative to SLERP with the following trade-offs:
The implementation automatically handles the sign ambiguity by choosing the shorter arc: if \(a \cdot b < 0\), it negates \(b\) to ensure the interpolation takes the shorter path on the quaternion hypersphere.
For small rotation angles, NLERP is nearly indistinguishable from SLERP but much more efficient. The velocity variation becomes noticeable only for large rotation angles (> 90 degrees).
Return normalized copy of quaternion.
| T | Scalar type. |
| quat | Quaternion to normalize (copied). |
Returns a unit-length quaternion:
\[\hat{q} = \frac{q}{\|q\|} \]
|
inlinenoexcept |
Normalize quaternion in-place.
| T | Scalar type. |
| quat | Quaternion to normalize (modified in-place). |
Normalizes the quaternion to unit length:
\[\hat{q} = \frac{q}{\|q\|} = \frac{(x, y, z, w)}{\sqrt{x^2 + y^2 + z^2 + w^2}} \]
A unit quaternion satisfies \(\|\hat{q}\| = 1\) and represents a pure rotation without scaling.
|
constexprnoexcept |
Rotate a 3D vector by a quaternion (optimized form).
| T | Scalar type. |
| quat | Quaternion describing rotation. Ideally quat is unit-length. |
| vec | Vector to be rotated. |
Applies quaternion rotation to a 3D vector using the optimized formula:
\[\vec{v}' = \vec{v} + 2w(\vec{q} \times \vec{v}) + 2(\vec{q} \times (\vec{q} \times \vec{v})) \]
where \(\vec{q} = (x, y, z)\) is the vector part and \(w\) is the scalar part.
This is mathematically equivalent to the sandwich product \(q \otimes (0, \vec{v}) \otimes q^{-1}\) but more efficient as it avoids constructing temporary quaternions.
Implementation uses the rearranged form:
\[\begin{align*} \vec{t} &= 2(\vec{q} \times \vec{v}) \\ \vec{v}' &= \vec{v} + w\vec{t} + \vec{q} \times \vec{t} \end{align*} \]
|
inlinenoexcept |
Spherical linear interpolation (SLERP) between two quaternions.
| T | Scalar type. |
| quatA | Start quaternion. |
| quatB | End quaternion. |
| t | Interpolation parameter in [0,1]. |
SLERP provides constant-angular-velocity interpolation along the shortest great-circle arc on the 4D unit hypersphere:
\[\text{slerp}(a, b, t) = \frac{\sin((1-t)\theta)}{\sin\theta}a + \frac{\sin(t\theta)}{\sin\theta}b \]
where \(\theta = \arccos(a \cdot b)\) is the angle between quaternions.
The implementation handles several special cases:
The interpolated quaternion is:
\[q(t) = \frac{w_a \cdot a + w_b \cdot b}{\|w_a \cdot a + w_b \cdot b\|} \]
where:
\[\begin{align*} w_a &= \frac{\sin((1-t)\theta)}{\sin\theta} \\ w_b &= \frac{\sin(t\theta)}{\sin\theta} \end{align*} \]
|
inlinenoexcept |
Decompose quaternion into axis-angle representation.
| T | Scalar type. |
| quat | Input quaternion (not necessarily normalized). |
| outAxis | Output axis (normalized when angle != 0). |
| outAngle | Output angle in radians. |
Extracts the axis-angle representation from a quaternion:
\[\begin{align*} \theta &= 2\arccos(w) \\ \vec{a} &= \frac{(x, y, z)}{\sin(\theta/2)} = \frac{(x, y, z)}{\sqrt{1 - w^2}} \end{align*} \]
If the rotation angle is approximately zero (identity rotation), the axis is set to \((1, 0, 0)\) arbitrarily since all axes represent the same identity rotation.
|
inlinenoexcept |
Convert quaternion to Euler angles (roll, pitch, yaw).
| T | Scalar type. |
| quat | Input quaternion. |
| outRoll | Output roll angle (X axis) in radians. |
| outPitch | Output pitch angle (Y axis) in radians. |
| outYaw | Output yaw angle (Z axis) in radians. |
Extracts Euler angles from a quaternion using the ZYX convention:
\[\begin{align*} \text{roll} &= \arctan2(2(wx + yz), 1 - 2(x^2 + y^2)) \\ \text{pitch} &= \arcsin(2(wy - zx)) \\ \text{yaw} &= \arctan2(2(wz + xy), 1 - 2(y^2 + z^2)) \end{align*} \]
The conversion handles gimbal lock by clamping the pitch sine value:
Convert quaternion to 3x3 rotation matrix (row-major).
| T | Scalar type. |
| quatIn | Input quaternion. |
Converts a unit quaternion \(q = (x, y, z, w)\) to a 3×3 rotation matrix:
\[R = \begin{pmatrix} 1 - 2(y^2 + z^2) & 2(xy - wz) & 2(xz + wy) \\ 2(xy + wz) & 1 - 2(x^2 + z^2) & 2(yz - wx) \\ 2(xz - wy) & 2(yz + wx) & 1 - 2(x^2 + y^2) \end{pmatrix} \]
This matrix represents the same rotation as the quaternion and can be used to transform vectors: \(\vec{v}' = R\vec{v}\).
The conversion is derived from the quaternion rotation formula and produces an orthonormal matrix (i.e., \(R^T R = I\) and \(\det(R) = 1\)) when the input quaternion is normalized.
Convert quaternion to 4x4 rotation matrix.
| T | Scalar type. |
| quat | Input quaternion. |
Expands the 3x3 quaternion rotation matrix into homogeneous coordinates suitable for transform composition.