Skip to main content

Quat

Struct Quat 

Source
pub struct Quat<T> {
    pub x: T,
    pub y: T,
    pub z: T,
    pub w: T,
}
Expand description

Represents a quaternion with generic numeric components.

Quat is a generic struct representing a quaternion with components x, y, z, and w. It provides methods for quaternion operations such as multiplication, inversion, and normalization.

§Type Parameters

  • T: The numeric type of the quaternion components. This type must implement traits such as Float, NumAssign, Copy, etc.

Fields§

§x: T§y: T§z: T§w: T

Implementations§

Source§

impl<T> Quat<T>
where T: NumAssign + Float + Copy,

Source

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

Creates a new Quat instance with the given components.

§Parameters
  • x, y, z, w: The components of the quaternion.
§Returns

Returns a new Quat with the specified components.

§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(q, Quat::new(1.0, 2.0, 3.0, 4.0));
Source

pub fn zero() -> Self

Returns the zero quaternion (0, 0, 0, 0).

§Returns

Returns a new Quat where all components are zero.

§Examples
let q = Quat::zero();
assert_eq!(q, Quat::new(0.0, 0.0, 0.0, 0.0));
Source

pub fn one() -> Self

Returns the quaternion (1, 1, 1, 1).

§Returns

Returns a new Quat where all components are one.

§Examples
let q = Quat::one();
assert_eq!(q, Quat::new(1.0, 1.0, 1.0, 1.0));
Source

pub fn from_euler(roll: T, pitch: T, yaw: T) -> Self

Creates a quaternion from Euler angles (roll, pitch, yaw).

This method converts Euler angles into a quaternion representation. It assumes the order of rotations is Z-Y-X (yaw-pitch-roll).

§Parameters
  • roll: Rotation around the X axis.
  • pitch: Rotation around the Y axis.
  • yaw: Rotation around the Z axis.
§Returns

Returns a new Quat representing the rotation.

§Examples
let q = Quat::from_euler(0.0, std::f64::consts::PI / 2.0, 0.0);
Source

pub fn from_vec4(v: &Vec4<T>) -> Self

Creates a quaternion from a Vec4 by using its components as the quaternion’s (x, y, z, w) respectively.

§Parameters
  • v: A Vec4 instance representing the quaternion components.
§Returns

Returns a new Quat with the components taken from v.

§Examples
let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let q = Quat::from_vec4(&v);
assert_eq!(q, Quat::new(1.0, 2.0, 3.0, 4.0));
Source

pub fn to_vec4(&self) -> Vec4<T>

Converts this quaternion to a Vec4.

§Returns

Returns a Vec4 with the quaternion’s components (x, y, z, w).

§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let v = q.to_vec4();
assert_eq!(v, Vec4::new(1.0, 2.0, 3.0, 4.0));
Source

pub fn to_mat4(&self) -> Mat4<T>

Converts this quaternion to a 4x4 transformation matrix.

§Returns

Returns a Mat4 representing the rotation described by the quaternion.

§Examples
let q = Quat::new(0.0, 0.0, 0.0, 1.0);
let mat = q.to_mat4();
Source

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

Computes the dot product of this quaternion and another quaternion.

§Parameters
  • other: The other quaternion to compute the dot product with.
§Returns

Returns the dot product as a T.

§Examples
let q1 = Quat::new(1.0, 0.0, 0.0, 0.0);
let q2 = Quat::new(0.0, 1.0, 0.0, 0.0);
let dot_product = q1.dot(&q2);
assert_eq!(dot_product, 0.0); // Dot product of orthogonal quaternions
Source

pub fn length_squared(&self) -> T

Computes the squared length of the quaternion.

§Returns

Returns the squared length as a T.

§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let length_sq = q.length_squared();
assert_eq!(length_sq, 30.0); // Squared length of the quaternion
Source

pub fn length(&self) -> T

Computes the length (magnitude) of the quaternion.

§Returns

Returns the length as a T.

§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let length = q.length();
assert_eq!(length, 5.4772256); // Length of the quaternion
Source

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

Normalizes the quaternion to make its length equal to 1.

If the quaternion is zero-length, normalization is not possible, and None is returned to indicate this failure.

§Returns
  • Some(Self): The normalized quaternion if the length is non-zero.
  • None: If the quaternion is zero-length.
§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let normalized = q.normalize();
assert_eq!(normalized, Some(Quat::new(0.1825742, 0.36514837, 0.5477225, 0.7302967)));
Source

pub fn conjugate(&self) -> Self

Computes the conjugate of the quaternion.

The conjugate of a quaternion is obtained by negating the x, y, and z components while keeping the w component unchanged.

§Returns

Returns the conjugate of this quaternion.

§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let conjugate = q.conjugate();
assert_eq!(conjugate, Quat::new(-1.0, -2.0, -3.0, 4.0));
Source

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

Computes the inverse of the quaternion.

The inverse of a quaternion is computed using its conjugate and length squared. If the length squared is zero, the inverse is not defined and None is returned.

§Returns
  • Some(Self): The inverse of the quaternion if the length squared is non-zero.
  • None: If the length squared is zero.
§Examples
let q = Quat::new(1.0, 2.0, 3.0, 4.0);
let inverse = q.inverse();
assert_eq!(inverse, Some(Quat::new(-0.03333333, -0.06666667, -0.1, 0.13333334)));
Source

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

Multiplies this quaternion by another quaternion.

This method performs quaternion multiplication, which is used to combine rotations.

§Parameters
  • other: The quaternion to multiply with.
§Returns

Returns the resulting quaternion after multiplication.

§Examples
let q1 = Quat::new(0.0, 0.0, 0.0, 1.0);
let q2 = Quat::new(0.0, 1.0, 0.0, 0.0);
let result = q1.multiply(&q2);
assert_eq!(result, Quat::new(-1.0, 0.0, 0.0, 0.0)); // Result of quaternion multiplication
Source

pub fn slerp(start: &Self, end: &Self, t: T) -> Self

Performs spherical linear interpolation (slerp) between two quaternions.

This method computes a quaternion that represents a rotation interpolated between start and end quaternions by a factor t. t ranges from 0.0 to 1.0, where 0.0 results in start and 1.0 results in end.

§Parameters
  • start: The starting quaternion.
  • end: The ending quaternion.
  • t: The interpolation factor, between 0.0 and 1.0.
§Returns

Returns the interpolated quaternion.

§Examples
let q1 = Quat::new(1.0, 0.0, 0.0, 0.0);
let q2 = Quat::new(0.0, 1.0, 0.0, 0.0);
let interpolated = Quat::slerp(&q1, &q2, 0.5);
assert_eq!(interpolated, Quat::new(0.5, 0.5, 0.0, 0.5)); // Interpolated quaternion

Trait Implementations§

Source§

impl<T> Add for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T> Add<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the + operator.
Source§

fn add(self, scalar: T) -> Self

Performs the + operation. Read more
Source§

impl<T> AddAssign for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<T> AddAssign<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn add_assign(&mut self, scalar: T)

Performs the += operation. Read more
Source§

impl<T: Clone> Clone for Quat<T>

Source§

fn clone(&self) -> Quat<T>

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<T: Copy> Copy for Quat<T>

Source§

impl<T: Debug> Debug for Quat<T>

Source§

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

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

impl<T: Default> Default for Quat<T>

Source§

fn default() -> Quat<T>

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

impl<T> Display for Quat<T>
where T: Display,

Source§

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

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

impl<T> Div for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T> Div<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the / operator.
Source§

fn div(self, scalar: T) -> Self

Performs the / operation. Read more
Source§

impl<T> DivAssign for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl<T> DivAssign<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn div_assign(&mut self, scalar: T)

Performs the /= operation. Read more
Source§

impl<T> Index<usize> for Quat<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Quat<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> Mul for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T> Mul<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Self

Performs the * operation. Read more
Source§

impl<T> MulAssign for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl<T> MulAssign<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn mul_assign(&mut self, scalar: T)

Performs the *= operation. Read more
Source§

impl<T: PartialEq> PartialEq for Quat<T>

Source§

fn eq(&self, other: &Quat<T>) -> 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<T> StructuralPartialEq for Quat<T>

Source§

impl<T> Sub for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T> Sub<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

type Output = Quat<T>

The resulting type after applying the - operator.
Source§

fn sub(self, scalar: T) -> Self

Performs the - operation. Read more
Source§

impl<T> SubAssign for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<T> SubAssign<T> for Quat<T>
where T: NumAssign + Float + Copy,

Source§

fn sub_assign(&mut self, scalar: T)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Quat<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Quat<T>
where T: RefUnwindSafe,

§

impl<T> Send for Quat<T>
where T: Send,

§

impl<T> Sync for Quat<T>
where T: Sync,

§

impl<T> Unpin for Quat<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Quat<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Quat<T>
where T: UnwindSafe,

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.