#[repr(C)]pub struct Quaternion {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}Expand description
Represents a quaternion for efficient 3D rotations.
Quaternions are a four-dimensional complex number system that can represent rotations in 3D space. They are generally more efficient and numerically stable than rotation matrices, avoiding issues like gimbal lock.
A quaternion is stored as (x, y, z, w), where [x, y, z] is the “vector” part
and w is the “scalar” part. For representing rotations, it should be a “unit
quaternion” where x² + y² + z² + w² = 1.
Fields§
§x: f32The x component of the vector part.
y: f32The y component of the vector part.
z: f32The z component of the vector part.
w: f32The scalar (real) part.
Implementations§
Source§impl Quaternion
impl Quaternion
Sourcepub const IDENTITY: Quaternion
pub const IDENTITY: Quaternion
The identity quaternion, representing no rotation.
Sourcepub fn new(x: f32, y: f32, z: f32, w: f32) -> Quaternion
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Quaternion
Creates a new quaternion from its raw components.
Note: This does not guarantee a unit quaternion. For creating rotations,
prefer using from_axis_angle or other rotation-specific constructors.
Sourcepub fn from_axis_angle(axis: Vec3, angle_radians: f32) -> Quaternion
pub fn from_axis_angle(axis: Vec3, angle_radians: f32) -> Quaternion
Creates a quaternion representing a rotation around a given axis by a given angle.
§Arguments
axis: The axis of rotation. It is recommended to pass a normalized vector.angle_radians: The angle of rotation in radians.
Sourcepub fn from_rotation_matrix(m: &Mat4) -> Quaternion
pub fn from_rotation_matrix(m: &Mat4) -> Quaternion
Creates a quaternion from a 4x4 rotation matrix.
This method only considers the upper 3x3 part of the matrix for the conversion.
Sourcepub fn magnitude_squared(&self) -> f32
pub fn magnitude_squared(&self) -> f32
Calculates the squared length (magnitude) of the quaternion.
Sourcepub fn normalize(&self) -> Quaternion
pub fn normalize(&self) -> Quaternion
Returns a normalized version of the quaternion with a length of 1. If the quaternion has a near-zero magnitude, it returns the identity quaternion.
Sourcepub fn conjugate(&self) -> Quaternion
pub fn conjugate(&self) -> Quaternion
Computes the conjugate of the quaternion, which negates the vector part.
Sourcepub fn inverse(&self) -> Quaternion
pub fn inverse(&self) -> Quaternion
Computes the inverse of the quaternion. For a unit quaternion, the inverse is equal to its conjugate.
Sourcepub fn dot(&self, other: Quaternion) -> f32
pub fn dot(&self, other: Quaternion) -> f32
Computes the dot product of two quaternions.
Sourcepub fn rotate_vec3(&self, v: Vec3) -> Vec3
pub fn rotate_vec3(&self, v: Vec3) -> Vec3
Rotates a 3D vector by this quaternion.
Sourcepub fn slerp(start: Quaternion, end: Quaternion, t: f32) -> Quaternion
pub fn slerp(start: Quaternion, end: Quaternion, t: f32) -> Quaternion
Performs a Spherical Linear Interpolation (Slerp) between two quaternions.
Slerp provides a smooth, constant-speed interpolation between two rotations, following the shortest path on the surface of a 4D sphere.
t- The interpolation factor, clamped to the[0.0, 1.0]range.
Trait Implementations§
Source§impl Add for Quaternion
impl Add for Quaternion
Source§fn add(self, rhs: Quaternion) -> <Quaternion as Add>::Output
fn add(self, rhs: Quaternion) -> <Quaternion as Add>::Output
Adds two quaternions component-wise. Note: This is not a standard rotation operation.
Source§type Output = Quaternion
type Output = Quaternion
+ operator.Source§impl<'__de, __Context> BorrowDecode<'__de, __Context> for Quaternion
impl<'__de, __Context> BorrowDecode<'__de, __Context> for Quaternion
Source§fn borrow_decode<__D>(decoder: &mut __D) -> Result<Quaternion, DecodeError>where
__D: BorrowDecoder<'__de, Context = __Context>,
fn borrow_decode<__D>(decoder: &mut __D) -> Result<Quaternion, DecodeError>where
__D: BorrowDecoder<'__de, Context = __Context>,
Source§impl Clone for Quaternion
impl Clone for Quaternion
Source§fn clone(&self) -> Quaternion
fn clone(&self) -> Quaternion
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Quaternion
impl Debug for Quaternion
Source§impl<__Context> Decode<__Context> for Quaternion
impl<__Context> Decode<__Context> for Quaternion
Source§fn decode<__D>(decoder: &mut __D) -> Result<Quaternion, DecodeError>where
__D: Decoder<Context = __Context>,
fn decode<__D>(decoder: &mut __D) -> Result<Quaternion, DecodeError>where
__D: Decoder<Context = __Context>,
Source§impl Default for Quaternion
impl Default for Quaternion
Source§fn default() -> Quaternion
fn default() -> Quaternion
Returns the identity quaternion, representing no rotation.
Source§impl<'de> Deserialize<'de> for Quaternion
impl<'de> Deserialize<'de> for Quaternion
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Quaternion, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Quaternion, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Encode for Quaternion
impl Encode for Quaternion
Source§impl Mul<Vec3> for Quaternion
impl Mul<Vec3> for Quaternion
Source§impl Mul<f32> for Quaternion
impl Mul<f32> for Quaternion
Source§impl Mul for Quaternion
impl Mul for Quaternion
Source§fn mul(self, rhs: Quaternion) -> <Quaternion as Mul>::Output
fn mul(self, rhs: Quaternion) -> <Quaternion as Mul>::Output
Combines two rotations using the Hamilton product. Note that quaternion multiplication is not commutative.
Source§type Output = Quaternion
type Output = Quaternion
* operator.Source§impl MulAssign for Quaternion
impl MulAssign for Quaternion
Source§fn mul_assign(&mut self, rhs: Quaternion)
fn mul_assign(&mut self, rhs: Quaternion)
Combines this rotation with another.
Source§impl Neg for Quaternion
impl Neg for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
- operator.Source§impl PartialEq for Quaternion
impl PartialEq for Quaternion
Source§impl Serialize for Quaternion
impl Serialize for Quaternion
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl Sub for Quaternion
impl Sub for Quaternion
Source§fn sub(self, rhs: Quaternion) -> <Quaternion as Sub>::Output
fn sub(self, rhs: Quaternion) -> <Quaternion as Sub>::Output
Subtracts two quaternions component-wise.
Source§type Output = Quaternion
type Output = Quaternion
- operator.impl Copy for Quaternion
impl StructuralPartialEq for Quaternion
Auto Trait Implementations§
impl Freeze for Quaternion
impl RefUnwindSafe for Quaternion
impl Send for Quaternion
impl Sync for Quaternion
impl Unpin for Quaternion
impl UnwindSafe for Quaternion
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.