pub struct Quaternion {
pub w: f64,
pub x: f64,
pub y: f64,
pub z: f64,
}Expand description
A quaternion representing a rotation in 3D space.
With the optional serde feature, this type implements Serialize and
Deserialize (the docs.rs listing cannot banner derive-generated impls).
Fields§
§w: f64The scalar (real) part of the quaternion.
x: f64The x component of the vector part.
y: f64The y component of the vector part.
z: f64The z component of the vector part.
Implementations§
Source§impl Quaternion
impl Quaternion
Sourcepub const fn from_wxyz(w: f64, x: f64, y: f64, z: f64) -> Quaternion
pub const fn from_wxyz(w: f64, x: f64, y: f64, z: f64) -> Quaternion
Creates a quaternion from its w, x, y, and z components.
The scalar part w comes first — the name spells the order out,
because the other common convention puts it last and a silently
swapped w is a valid quaternion describing a different rotation.
No normalization is performed; rotations are expected to be unit
quaternions, so call Quaternion::normalize if the components do not
already form one.
§Examples
use transforms::geometry::Quaternion;
let q = Quaternion::from_wxyz(1.0, 0.0, 0.0, 0.0);
assert_eq!(q, Quaternion::identity());Sourcepub const fn identity() -> Quaternion
pub const fn identity() -> Quaternion
Creates an identity quaternion representing no rotation.
Returns a quaternion with w=1 and x=y=z=0, which represents the identity rotation (i.e., no rotation at all).
§Examples
use transforms::geometry::Quaternion;
let q = Quaternion::identity();
assert_eq!(q.w, 1.0);
assert_eq!(q.x, 0.0);
assert_eq!(q.y, 0.0);
assert_eq!(q.z, 0.0);Sourcepub fn conjugate(self) -> Quaternion
pub fn conjugate(self) -> Quaternion
Returns the conjugate of the quaternion.
§Examples
use transforms::geometry::Quaternion;
let q = Quaternion::from_wxyz(1.0, 2.0, 3.0, 4.0);
assert_eq!(q.conjugate(), Quaternion::from_wxyz(1.0, -2.0, -3.0, -4.0));Sourcepub fn normalize(self) -> Result<Quaternion, QuaternionError>
pub fn normalize(self) -> Result<Quaternion, QuaternionError>
Normalizes the quaternion to unit length.
The intermediate norm — the square root of the sum of squares — sets
both limits. A component beyond roughly 1.3e154, the square root of
f64::MAX, squares to infinity (reported as NonFinite), and a norm
below f64::EPSILON (about 2.2e-16) counts as zero (reported as
ZeroLengthNormalization). That threshold is exclusive: a norm of
exactly f64::EPSILON still normalizes.
§Errors
Returns QuaternionError::ZeroLengthNormalization if the norm is
below f64::EPSILON, and QuaternionError::NonFinite if any
component is NaN or the sum of squares overflows.
§Examples
use transforms::{errors::QuaternionError, geometry::Quaternion};
let q = Quaternion::from_wxyz(1.0, 2.0, 3.0, 4.0);
let normalized = q.normalize().unwrap();
assert!((normalized.norm() - 1.0).abs() < f64::EPSILON);
// The zero threshold is `f64::EPSILON` on the norm, not a
// rotation-scale epsilon: this one is far below rotation scale and
// still normalizes.
let at_threshold = Quaternion::from_wxyz(f64::EPSILON, 0.0, 0.0, 0.0);
assert_eq!(at_threshold.normalize().unwrap(), Quaternion::identity());
let below_threshold = Quaternion::from_wxyz(f64::EPSILON / 2.0, 0.0, 0.0, 0.0);
assert!(matches!(
below_threshold.normalize(),
Err(QuaternionError::ZeroLengthNormalization)
));
let zero_q = Quaternion::from_wxyz(0.0, 0.0, 0.0, 0.0);
assert!(matches!(
zero_q.normalize(),
Err(QuaternionError::ZeroLengthNormalization)
));Sourcepub fn norm(self) -> f64
pub fn norm(self) -> f64
Computes the norm (magnitude) of the quaternion.
§Examples
use transforms::geometry::Quaternion;
let q = Quaternion::from_wxyz(1.0, 1.0, 1.0, 1.0);
assert_eq!(q.norm(), 2.0);Sourcepub fn rotate_vector(self, v: Vector3) -> Vector3
pub fn rotate_vector(self, v: Vector3) -> Vector3
Rotates a vector by the quaternion.
The vector is treated as a pure quaternion with a real part of zero.
§Examples
use transforms::geometry::{Quaternion, Vector3};
let q = Quaternion::from_wxyz(
(core::f64::consts::PI / 4.0).cos(),
0.0,
0.0,
(core::f64::consts::PI / 4.0).sin(),
);
let v = Vector3::new(1.0, 0.0, 0.0);
assert_relative_eq!(q.rotate_vector(v), Vector3::new(0.0, 1.0, 0.0));Sourcepub fn slerp(self, other: Quaternion, t: f64) -> Quaternion
pub fn slerp(self, other: Quaternion, t: f64) -> Quaternion
Performs spherical linear interpolation (slerp) between two quaternions.
Interpolates between self and other by the factor t, which is
clamped to [0.0, 1.0] — there is no extrapolation, matching the
crate-wide policy. Infinite factors saturate to the corresponding
endpoint; a NaN factor yields a NaN result.
The trigonometry runs through libm whether or not std is enabled,
so the same operands yield bit-identical results in both feature
modes.
§Examples
use transforms::geometry::Quaternion;
let q1 = Quaternion::identity();
let q2 = Quaternion::from_wxyz(0.0, 1.0, 0.0, 0.0);
let result = q1.slerp(q2, 0.5);
let expected = Quaternion::from_wxyz((0.5_f64).sqrt(), (0.5_f64).sqrt(), 0.0, 0.0);
assert_relative_eq!(result.w, expected.w, epsilon = f64::EPSILON);
assert_relative_eq!(result.x, expected.x, epsilon = f64::EPSILON);
assert_relative_eq!(result.y, expected.y, epsilon = f64::EPSILON);
assert_relative_eq!(result.z, expected.z, epsilon = f64::EPSILON);Trait Implementations§
Source§impl AbsDiffEq for Quaternion
impl AbsDiffEq for Quaternion
Source§fn default_epsilon() -> <Quaternion as AbsDiffEq>::Epsilon
fn default_epsilon() -> <Quaternion as AbsDiffEq>::Epsilon
Source§fn abs_diff_eq(
&self,
other: &Quaternion,
epsilon: <Quaternion as AbsDiffEq>::Epsilon,
) -> bool
fn abs_diff_eq( &self, other: &Quaternion, epsilon: <Quaternion as AbsDiffEq>::Epsilon, ) -> bool
Source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq.Source§impl Clone for Quaternion
impl Clone for Quaternion
Source§fn clone(&self) -> Quaternion
fn clone(&self) -> Quaternion
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for Quaternion
Source§impl Debug for Quaternion
impl Debug for Quaternion
Source§impl Mul for Quaternion
impl Mul for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
* operator.Source§fn mul(self, other: Quaternion) -> Quaternion
fn mul(self, other: Quaternion) -> Quaternion
* operation. Read moreSource§impl PartialEq for Quaternion
impl PartialEq for Quaternion
Source§impl RelativeEq for Quaternion
impl RelativeEq for Quaternion
Source§fn default_max_relative() -> <Quaternion as AbsDiffEq>::Epsilon
fn default_max_relative() -> <Quaternion as AbsDiffEq>::Epsilon
Source§fn relative_eq(
&self,
other: &Quaternion,
epsilon: <Quaternion as AbsDiffEq>::Epsilon,
max_relative: <Quaternion as AbsDiffEq>::Epsilon,
) -> bool
fn relative_eq( &self, other: &Quaternion, epsilon: <Quaternion as AbsDiffEq>::Epsilon, max_relative: <Quaternion as AbsDiffEq>::Epsilon, ) -> bool
Source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq.