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 asFloat,NumAssign,Copy, etc.
Fields§
§x: T§y: T§z: T§w: TImplementations§
Source§impl<T> Quat<T>
impl<T> Quat<T>
Sourcepub fn from_euler(roll: T, pitch: T, yaw: T) -> Self
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);Sourcepub fn from_vec4(v: &Vec4<T>) -> Self
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: AVec4instance 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));Sourcepub fn dot(&self, other: &Self) -> T
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 quaternionsSourcepub fn length_squared(&self) -> T
pub fn length_squared(&self) -> T
Sourcepub fn normalize(&self) -> Option<Self>
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)));Sourcepub fn conjugate(&self) -> Self
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));Sourcepub fn inverse(&self) -> Option<Self>
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)));Sourcepub fn multiply(&self, other: &Self) -> Self
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 multiplicationSourcepub fn slerp(start: &Self, end: &Self, t: T) -> Self
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 quaternionTrait Implementations§
Source§impl<T> AddAssign for Quat<T>
impl<T> AddAssign for Quat<T>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreSource§impl<T> AddAssign<T> for Quat<T>
impl<T> AddAssign<T> for Quat<T>
Source§fn add_assign(&mut self, scalar: T)
fn add_assign(&mut self, scalar: T)
+= operation. Read moreimpl<T: Copy> Copy for Quat<T>
Source§impl<T> DivAssign for Quat<T>
impl<T> DivAssign for Quat<T>
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
/= operation. Read moreSource§impl<T> DivAssign<T> for Quat<T>
impl<T> DivAssign<T> for Quat<T>
Source§fn div_assign(&mut self, scalar: T)
fn div_assign(&mut self, scalar: T)
/= operation. Read moreSource§impl<T> MulAssign for Quat<T>
impl<T> MulAssign for Quat<T>
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
*= operation. Read moreSource§impl<T> MulAssign<T> for Quat<T>
impl<T> MulAssign<T> for Quat<T>
Source§fn mul_assign(&mut self, scalar: T)
fn mul_assign(&mut self, scalar: T)
*= operation. Read more