Struct Quaternion

Source
pub struct Quaternion(/* private fields */);
Expand description

Quaternions are a number system that extends the complex numbers which can be used for efficiently computing spatial rotations.

They’re computed as the quotient of two directed lines in a three-dimensional space, or equivalently as the quotient of two vectors.

For given real numbers a, b, c, and d, they take the form:

a + bi + cj + dk

where i, j, and k are the fundamental quaternion units:

i² = j² = k² = i*j*k = -1

Quaternion multiplication is non-commutative:

x1ijk
11ijk
ii-1k-j
jj-k-1i
kkj-i-1

Implementations§

Source§

impl Quaternion

Source

pub const IDENTITY: Self

Identity quaternion.

Source

pub const fn new(a: f32, b: f32, c: f32, d: f32) -> Self

Create a new quaternion.

Source

pub fn from_two_vectors<C>(u: Vector3d<C>, v: Vector3d<C>) -> Self
where C: Component + Into<f32>,

Get the quaternion that represents the smallest rotation between two vectors.

Source

pub fn conj(self) -> Self

Returns the conjugate of this quaternion.

Source

pub fn dot(self, rhs: Self) -> f32

Returns the dot product of this quaternion.

Source

pub fn inv(self) -> Self

Compute the inverse of this quaternion.

Panics if Quaternion::norm is zero.

Source

pub fn magnitude(self) -> f32

Compute the magnitude (a.k.a length) of this quaternion.

Source

pub fn norm(self) -> f32

Returns the norm of this quaternion, i.e. a²+b²+c²+d².

https://www.mathworks.com/help/aeroblks/quaternionnorm.html

Source

pub fn axis_angle<C>(v: Vector3d<C>, theta: C) -> Self
where C: Component + Into<f32>,

Available on crate feature vector only.

Compute a quaternion for the given axis vector and angle.

Source

pub fn rotate<C>(self, v: Vector3d<C>) -> F32x3
where C: Component + Into<f32>,

Available on crate feature vector only.

Rotate a 3D vector using this quaternion, assumes the quaternion is of unit length.

Source

pub fn scale<S>(self, scalar: S) -> Self
where S: Into<f32>,

Scale by a scalar.

Source

pub fn normalize(self) -> Self

Normalize the quaternion.

Source

pub fn to_euler(&self) -> (f32, f32, f32)

Get the (roll, pitch, yaw) Euler angles, assumes the quaternion is normalized.

Source

pub fn to_array(&self) -> [f32; 4]

Convert this quaternion into an array.

Source

pub fn w(&self) -> f32

Access the w / a real component

Source

pub fn x(&self) -> f32

Access the x / b / i imaginary component

Source

pub fn y(&self) -> f32

Access the y / c / j imaginary component

Source

pub fn z(&self) -> f32

Access the z / d / k imaginary component

Trait Implementations§

Source§

impl Add for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Quaternion

Source§

fn add_assign(&mut self, rhs: 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 · Source§

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

Performs copy-assignment from source. Read more
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 From<(f32, f32, f32, f32)> for Quaternion

Source§

fn from(q: (f32, f32, f32, f32)) -> Quaternion

Converts to this type from the input type.
Source§

impl From<Quaternion> for (f32, f32, f32, f32)

Source§

fn from(q: Quaternion) -> (f32, f32, f32, f32)

Converts to this type from the input type.
Source§

impl<C> From<Vector3d<C>> for Quaternion
where C: Component + Into<f32>,

Available on crate feature vector only.
Source§

fn from(v: Vector3d<C>) -> Quaternion

Converts to this type from the input type.
Source§

impl Mul<Quaternion> for f32

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<C> Mul<Vector3d<C>> for Quaternion
where C: Component + Into<f32>,

Available on crate feature vector only.
Source§

type Output = Vector3d<f32>

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vector3d<C>) -> F32x3

Performs the * operation. Read more
Source§

impl Mul<f32> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, k: f32) -> Self

Performs the * operation. Read more
Source§

impl Mul for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Quaternion

Source§

fn mul_assign(&mut self, k: f32)

Performs the *= 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 · 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 Sub for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Quaternion

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for Quaternion

Source§

impl StructuralPartialEq for Quaternion

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