Skip to main content

Quaternion

Struct Quaternion 

Source
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: f64

Implementations§

Source§

impl Quaternion

Source

pub const fn new(w: f64, x: f64, y: f64, z: f64) -> Self

Source

pub const fn identity() -> Self

The multiplicative identity, i.e. no rotation.

Source

pub const fn from_vector(v: [f64; 3]) -> Self

A pure quaternion (zero scalar part) holding a vector.

Source

pub const fn vector_part(&self) -> [f64; 3]

The vector (imaginary) part.

Source

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).

Source

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.

Source

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.

Source

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).

Source

pub fn to_rotation_matrix(&self) -> [[f64; 3]; 3]

The 3×3 rotation matrix (row-major) of the normalised quaternion.

Source

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.

Source

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.

Source

pub fn norm_squared(&self) -> f64

Source

pub fn norm(&self) -> f64

Source

pub fn normalized(&self) -> Self

Scaled to unit length; the identity if the norm is zero.

Source

pub fn conjugate(&self) -> Self

Source

pub fn inverse(&self) -> Option<Self>

The multiplicative inverse q* / |q|²; None for the zero quaternion.

Source

pub fn dot(&self, other: &Self) -> f64

Source

pub fn rotate_vector(&self, v: [f64; 3]) -> [f64; 3]

Rotates v by this (normalised) quaternion: q v q*.

Source

pub fn rotate_free_vector(&self, v: &FreeVector3D) -> FreeVector3D

Rotates a FreeVector3D (computed in f64, stored back as f32).

Source

pub fn rotate_point(&self, p: &CoordinateVector3D) -> CoordinateVector3D

Rotates a point about the origin.

Source

pub fn exp(&self) -> Self

The quaternion exponential.

Source

pub fn ln(&self) -> Self

The principal logarithm (zero quaternion has no logarithm: returns -∞ scalar).

Source

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.

Source

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.

Source

pub fn nlerp(&self, other: &Self, t: f64) -> Self

Normalised linear interpolation: cheaper than Quaternion::slerp, not constant-speed.

Source

pub fn angle_to(&self, other: &Self) -> f64

The angle in radians (0 to π) of the rotation taking self to other.

Source

pub fn approx_eq(&self, other: &Self, tol: f64) -> bool

Whether the two quaternions are within tol componentwise (not modulo sign).

Trait Implementations§

Source§

impl Add for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the + operator.
Source§

fn add(self, o: Quaternion) -> Quaternion

Performs the + operation. Read more
Source§

impl Clone for Quaternion

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Quaternion

Source§

impl Debug for Quaternion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Quaternion

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Div<f64> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the / operator.
Source§

fn div(self, s: f64) -> Quaternion

Performs the / operation. Read more
Source§

impl From<FreeVector3D> for Quaternion

Source§

fn from(v: FreeVector3D) -> Self

The pure quaternion of a vector.

Source§

impl From<Quaternion> for Rotation3

Source§

fn from(q: Quaternion) -> Self

Converts to this type from the input type.
Source§

impl Mul for Quaternion

The Hamilton product: a * b applies b first, then a, when used as rotations.

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, o: Quaternion) -> Quaternion

Performs the * operation. Read more
Source§

impl Mul<f64> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, s: f64) -> Quaternion

Performs the * operation. Read more
Source§

impl Neg for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

fn neg(self) -> Quaternion

Performs the unary - operation. Read more
Source§

impl PartialEq for Quaternion

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Quaternion

Source§

impl Sub for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

fn sub(self, o: Quaternion) -> Quaternion

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSerialize for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.