Struct micromath::Quaternion

source ·
pub struct Quaternion(/* private fields */);
Available on crate feature quaternion only.
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>) -> Selfwhere 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) -> Selfwhere 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>) -> F32x3where 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) -> Selfwhere 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

§

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 copy 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 Quaternionwhere 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

§

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 Quaternionwhere C: Component + Into<f32>,

Available on crate feature vector only.
§

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

§

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

§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub for Quaternion

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.