pub struct Quaternion {
pub w: f64,
pub x: f64,
pub y: f64,
pub z: f64,
}Expand description
A quaternion w + xi + yj + zk (scalar part first).
Fields§
§w: f64§x: f64§y: f64§z: f64Implementations§
Source§impl Quaternion
impl Quaternion
pub const fn new(w: f64, x: f64, y: f64, z: f64) -> Self
Sourcepub const fn from_vector(v: [f64; 3]) -> Self
pub const fn from_vector(v: [f64; 3]) -> Self
A pure quaternion (zero scalar part) holding a vector.
Sourcepub const fn vector_part(&self) -> [f64; 3]
pub const fn vector_part(&self) -> [f64; 3]
The vector (imaginary) part.
Sourcepub fn from_axis_angle(axis: [f64; 3], angle: f64) -> Self
pub fn from_axis_angle(axis: [f64; 3], angle: f64) -> Self
Rotation by angle radians about axis (which need not be normalised; a zero axis gives the
identity).
Sourcepub fn to_axis_angle(&self) -> ([f64; 3], f64)
pub fn to_axis_angle(&self) -> ([f64; 3], f64)
The axis and angle in [0, π] of a (unit) rotation quaternion; the axis is [1, 0, 0] for
the identity, where it is arbitrary.
Sourcepub fn from_euler(roll: f64, pitch: f64, yaw: f64) -> Self
pub fn from_euler(roll: f64, pitch: f64, yaw: f64) -> Self
Rotation from intrinsic Z-Y-X (yaw, pitch, roll) Euler angles in radians: first yaw about Z, then pitch about the new Y, then roll about the new X.
Sourcepub fn to_euler(&self) -> (f64, f64, f64)
pub fn to_euler(&self) -> (f64, f64, f64)
(roll, pitch, yaw) for the Z-Y-X convention of Quaternion::from_euler; pitch is in
[-π/2, π/2] (at the ±90° gimbal lock yaw is taken as zero).
Sourcepub fn to_rotation_matrix(&self) -> [[f64; 3]; 3]
pub fn to_rotation_matrix(&self) -> [[f64; 3]; 3]
The 3×3 rotation matrix (row-major) of the normalised quaternion.
Sourcepub fn from_rotation_matrix(m: &[[f64; 3]; 3]) -> Self
pub fn from_rotation_matrix(m: &[[f64; 3]; 3]) -> Self
The quaternion (with w >= 0) of a proper rotation matrix, by Shepperd’s method, which is
numerically stable for every rotation.
Sourcepub fn from_two_vectors(from: [f64; 3], to: [f64; 3]) -> Self
pub fn from_two_vectors(from: [f64; 3], to: [f64; 3]) -> Self
The shortest rotation taking direction from onto direction to (neither need be unit).
Returns the identity if either is zero.
pub fn norm_squared(&self) -> f64
pub fn norm(&self) -> f64
Sourcepub fn normalized(&self) -> Self
pub fn normalized(&self) -> Self
Scaled to unit length; the identity if the norm is zero.
pub fn conjugate(&self) -> Self
Sourcepub fn inverse(&self) -> Option<Self>
pub fn inverse(&self) -> Option<Self>
The multiplicative inverse q* / |q|²; None for the zero quaternion.
pub fn dot(&self, other: &Self) -> f64
Sourcepub fn rotate_vector(&self, v: [f64; 3]) -> [f64; 3]
pub fn rotate_vector(&self, v: [f64; 3]) -> [f64; 3]
Rotates v by this (normalised) quaternion: q v q*.
Sourcepub fn rotate_free_vector(&self, v: &FreeVector3D) -> FreeVector3D
pub fn rotate_free_vector(&self, v: &FreeVector3D) -> FreeVector3D
Rotates a FreeVector3D (computed in f64, stored back as f32).
Sourcepub fn rotate_point(&self, p: &CoordinateVector3D) -> CoordinateVector3D
pub fn rotate_point(&self, p: &CoordinateVector3D) -> CoordinateVector3D
Rotates a point about the origin.
Sourcepub fn ln(&self) -> Self
pub fn ln(&self) -> Self
The principal logarithm (zero quaternion has no logarithm: returns -∞ scalar).
Sourcepub fn powf(&self, t: f64) -> Self
pub fn powf(&self, t: f64) -> Self
Real power q^t through exp(t ln q); for a unit rotation this scales the angle by t.
Sourcepub fn slerp(&self, other: &Self, t: f64) -> Self
pub fn slerp(&self, other: &Self, t: f64) -> Self
Spherical linear interpolation along the shortest arc from self (t = 0) to other (t = 1),
for unit quaternions. Constant angular velocity.
Sourcepub fn nlerp(&self, other: &Self, t: f64) -> Self
pub fn nlerp(&self, other: &Self, t: f64) -> Self
Normalised linear interpolation: cheaper than Quaternion::slerp, not constant-speed.
Trait Implementations§
Source§impl Add for Quaternion
impl Add for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
+ operator.Source§fn add(self, o: Quaternion) -> Quaternion
fn add(self, o: Quaternion) -> Quaternion
+ operation. Read moreSource§impl Clone for Quaternion
impl Clone for Quaternion
impl Copy for Quaternion
Source§impl Debug for Quaternion
impl Debug for Quaternion
Source§impl Default for Quaternion
impl Default for Quaternion
Source§impl Div<f64> for Quaternion
impl Div<f64> for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
/ operator.Source§impl From<FreeVector3D> for Quaternion
impl From<FreeVector3D> for Quaternion
Source§fn from(v: FreeVector3D) -> Self
fn from(v: FreeVector3D) -> Self
The pure quaternion of a vector.
Source§impl From<Quaternion> for Rotation3
impl From<Quaternion> for Rotation3
Source§fn from(q: Quaternion) -> Self
fn from(q: Quaternion) -> Self
Source§impl Mul for Quaternion
The Hamilton product: a * b applies b first, then a, when used as rotations.
impl Mul for Quaternion
The Hamilton product: a * b applies b first, then a, when used as rotations.
Source§type Output = Quaternion
type Output = Quaternion
* operator.Source§fn mul(self, o: Quaternion) -> Quaternion
fn mul(self, o: Quaternion) -> Quaternion
* operation. Read moreSource§impl Mul<f64> for Quaternion
impl Mul<f64> for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
* operator.Source§impl Neg for Quaternion
impl Neg for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
- operator.Source§fn neg(self) -> Quaternion
fn neg(self) -> Quaternion
- operation. Read moreSource§impl PartialEq for Quaternion
impl PartialEq for Quaternion
impl StructuralPartialEq for Quaternion
Source§impl Sub for Quaternion
impl Sub for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
- operator.Source§fn sub(self, o: Quaternion) -> Quaternion
fn sub(self, o: Quaternion) -> Quaternion
- operation. Read more