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.

Fields§

§w: f64

Scalar part.

§x: f64

i coefficient.

§y: f64

j coefficient.

§z: f64

k coefficient.

Implementations§

Source§

impl Quaternion

Source

pub const IDENTITY: Self

The identity rotation.

Source

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

From components.

Source

pub fn from_axis_angle(axis: Direction, angle: f64) -> Self

The rotation of angle radians about axis, right-handed.

Source

pub fn between( from: Direction, to: Direction, tol: Tolerances, ) -> OgeomResult<Self>

The shortest rotation taking from to to.

§Errors

OgeomError::Construction if the two are antiparallel: infinitely many shortest rotations exist and picking one arbitrarily would make the result depend on unobservable rounding.

Source

pub fn from_matrix(m: &Matrix3, tol: Tolerances) -> OgeomResult<Self>

From a rotation matrix.

Uses Shepperd’s method: pick the largest of the four possible divisors so the division is never by something near zero. The naive w-first formulation loses precision for rotations near π, where w → 0.

§Errors

OgeomError::Construction if m is not orthonormal with determinant +1.

Source

pub fn square_norm(self) -> f64

Squared norm.

Source

pub fn norm(self) -> f64

Norm.

Source

pub fn normalized(self, tol: Tolerances) -> OgeomResult<Self>

This quaternion scaled to unit norm.

§Errors

OgeomError::Construction if the norm is below tol.confusion() or any component is non-finite.

Source

pub fn is_finite(self) -> bool

Whether every component is finite.

Source

pub const fn conjugate(self) -> Self

The conjugate. For a unit quaternion this is the inverse rotation.

Source

pub fn inverse(self, tol: Tolerances) -> OgeomResult<Self>

The multiplicative inverse.

§Errors

OgeomError::Numeric if the norm is degenerate.

Source

pub fn rotate(self, v: Vector) -> Vector

Apply this rotation to a vector. Assumes unit norm.

Evaluated as v + 2w(u × v) + 2(u × (u × v)) with u the vector part, which costs fewer operations than building the matrix and avoids the intermediate rounding of a full quaternion sandwich product.

Source

pub fn to_matrix(self) -> Matrix3

The equivalent rotation matrix. Assumes unit norm.

Source

pub fn to_axis_angle(self, tol: Tolerances) -> OgeomResult<(Direction, f64)>

The rotation axis and angle. Assumes unit norm; angle is in [0, π].

§Errors

OgeomError::Construction if the rotation is the identity, where the axis is undefined.

Source

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

Dot product, as 4-vectors.

Source

pub fn slerp(self, other: Self, t: f64, tol: Tolerances) -> OgeomResult<Self>

Spherical linear interpolation, t = 0 giving self.

Takes the shorter of the two arcs, and falls back to normalized linear interpolation when the two are nearly coincident, where the sin in the denominator of the spherical form goes to zero.

§Errors

OgeomError::Construction if either operand cannot be normalized.

Source

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

The rotation angle to other, in [0, π]. Assumes unit norms.

Trait Implementations§

Source§

impl Add for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Quaternion

Source§

fn clone(&self) -> Quaternion

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 Mul for Quaternion

Source§

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

Hamilton product. (a * b) rotates by b first, then by a.

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

impl Mul<f64> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

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

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

Performs the unary - operation. Read more
Source§

impl PartialEq for Quaternion

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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: Self) -> Self

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
§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.