Skip to main content

Quaternion

Struct Quaternion 

Source
pub struct Quaternion {
    pub w: f32,
    pub x: f32,
    pub y: f32,
    pub z: f32,
}
Expand description

A quaternion using Hamilton (not JPL) transformation conventions. The most common operations usedful for representing orientations and rotations are defined, including for operations with Vec3.

Fields§

§w: f32§x: f32§y: f32§z: f32

Implementations§

Source§

impl Quaternion

Source

pub const fn new_identity() -> Self

Source

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

Source

pub fn from_slice(slice: &[f32]) -> Result<Self, BufError>

Construct from the first 4 values in a slice: &[w, x, y, z].

Source

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

Convert to a len-4 array: [w, x, y, z].

Source

pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self

Create a rotation quaternion from an axis and angle.

Source

pub fn from_unit_vecs(v0: Vec3, v1: Vec3) -> Self

Create the quaternion that creates the shortest (great circle) rotation from vec0 to vec1.

Source

pub fn from_euler(euler: &EulerAngle) -> Self

Convert Euler angles to a quaternion. https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

Source

pub fn to_euler(&self) -> EulerAngle

Convert this quaternion to Tait-Bryon (Euler) angles. We use this reference: http://marc-b-reynolds.github.io/math/2017/04/18/TaitEuler.html. Most sources online provide us with results that do not behave how we expect.

Source

pub fn axis(&self) -> Vec3

Extract the axis of rotation.

Source

pub fn angle(&self) -> f32

Extract the axis of rotation.

Source

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

Convert an attitude to rotations around individual axes. Assumes X is left, Z is Z_VEC, and Y is Y_VEC.

Source

pub fn to_vec(self) -> Vec3

Convert to a 3D vector, discarding w.

Source

pub fn slerp(&self, end: Quaternion, amount: f32) -> Quaternion

Performs Spherical Linear Interpolation between this and another quaternion. A high amount will make the result more towards end. An amount of 0 will result in this quaternion. Ref: https://github.com/bitshifter/glam-rs/blob/main/src/$f/scalar/quat.rs#L567 Alternative implementation to examine: https://github.com/MartinWeigel/Quaternion/blob/master/Quaternion.c

Source

pub fn to_matrix(&self) -> Mat4

Converts a Quaternion to a rotation matrix

Source

pub fn to_matrix3(&self) -> Mat3

Converts a Quaternion to a rotation matrix

Source§

impl Quaternion

Source

pub fn magnitude(&self) -> f32

Returns the magnitude.

Source

pub fn normalize(&mut self)

Normalize, modifying in place.

Source

pub fn to_normalized(self) -> Self

Returns the normalized version of the vector.

Source

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

Used by slerp.

Source

pub fn inverse(self) -> Self

Source

pub fn rotate_vec(self, vec: Vec3) -> Vec3

Rotate a vector using this quaternion. Note that our multiplication Q * v operation is effectively quaternion multiplication, with a quaternion created by a vec with w=0. Uses the X_VEC hand rule.

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::Output

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

Available on crate feature std only.
Source§

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

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

impl Div<f32> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Quaternion

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl From<Quaternion> for Quaternion

Source§

fn from(other: Quaternion) -> Self

Converts to this type from the input type.
Source§

impl From<Quaternion> for Quaternion

Source§

fn from(other: Quaternion) -> Self

Converts to this type from the input type.
Source§

impl Mul<Vec3> for Quaternion

Source§

fn mul(self, rhs: Vec3) -> Self::Output

Returns the multiplication of a Quaternion with a vector. This is a normal Quaternion multiplication where the vector is treated as a Quaternion with a W element value of zero. The Quaternion is post- multiplied by the vector.

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

impl Mul<f32> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

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, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Quaternion

Source§

fn mul_assign(&mut self, rhs: 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::Output

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.