Struct Quaternion
#[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.
§Examples
use khora_core::math::{Quaternion, Vec3};
use std::f32::consts::FRAC_PI_2;
// A quarter-turn about the Y axis maps +Z onto +X.
let yaw = Quaternion::from_axis_angle(Vec3::Y, FRAC_PI_2);
let rotated = yaw * Vec3::Z;
assert!((rotated - Vec3::X).length() < 1e-5);
// Composing rotations is quaternion multiplication; the identity is a no-op.
let combined = yaw * Quaternion::IDENTITY;
assert!((combined * Vec3::Z - Vec3::X).length() < 1e-5);
// `slerp` blends along the shortest arc; halfway is a 45° turn.
let half = Quaternion::slerp(Quaternion::IDENTITY, yaw, 0.5);
let expected = Quaternion::from_axis_angle(Vec3::Y, FRAC_PI_2 / 2.0);
assert!((half * Vec3::Z - expected * Vec3::Z).length() < 1e-5);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§
§impl Quaternion
impl Quaternion
pub const IDENTITY: Quaternion
pub const IDENTITY: Quaternion
The identity quaternion, representing no rotation.
pub 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.
pub 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.
pub 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.
pub fn magnitude_squared(&self) -> f32
pub fn magnitude_squared(&self) -> f32
Calculates the squared length (magnitude) of the quaternion.
pub 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.
pub fn conjugate(&self) -> Quaternion
pub fn conjugate(&self) -> Quaternion
Computes the conjugate of the quaternion, which negates the vector part.
pub 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.
pub fn dot(&self, other: Quaternion) -> f32
pub fn dot(&self, other: Quaternion) -> f32
Computes the dot product of two quaternions.
pub fn rotate_vec3(&self, v: Vec3) -> Vec3
pub fn rotate_vec3(&self, v: Vec3) -> Vec3
Rotates a 3D vector by this quaternion.
pub 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.
pub fn to_euler_xyz(&self) -> (f32, f32, f32)
pub fn to_euler_xyz(&self) -> (f32, f32, f32)
Converts this quaternion to Euler angles in XYZ (pitch, yaw, roll) order.
Returns (x, y, z) angles in radians.
x= pitch (rotation around X axis)y= yaw (rotation around Y axis)z= roll (rotation around Z axis)
pub fn from_euler_xyz(x: f32, y: f32, z: f32) -> Quaternion
pub fn from_euler_xyz(x: f32, y: f32, z: f32) -> Quaternion
Creates a quaternion from Euler angles in XYZ order.
§Arguments
x— pitch in radians (rotation around X axis)y— yaw in radians (rotation around Y axis)z— roll in radians (rotation around Z axis)
Trait Implementations§
§impl Add for Quaternion
impl Add for Quaternion
§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.
§type Output = Quaternion
type Output = Quaternion
+ operator.§impl<'__de, __Context> BorrowDecode<'__de, __Context> for Quaternion
impl<'__de, __Context> BorrowDecode<'__de, __Context> for Quaternion
§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>,
§impl Clone for Quaternion
impl Clone for Quaternion
§fn clone(&self) -> Quaternion
fn clone(&self) -> Quaternion
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for Quaternion
impl Debug for Quaternion
§impl<__Context> Decode<__Context> for Quaternion
impl<__Context> Decode<__Context> for Quaternion
§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>,
§impl Default for Quaternion
impl Default for Quaternion
§fn default() -> Quaternion
fn default() -> Quaternion
Returns the identity quaternion, representing no rotation.
§impl<'de> Deserialize<'de> for Quaternion
impl<'de> Deserialize<'de> for Quaternion
§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>,
§impl Encode for Quaternion
impl Encode for Quaternion
§impl Mul<Vec3> for Quaternion
impl Mul<Vec3> for Quaternion
§impl Mul<f32> for Quaternion
impl Mul<f32> for Quaternion
§fn mul(self, scalar: f32) -> <Quaternion as Mul<f32>>::Output
fn mul(self, scalar: f32) -> <Quaternion as Mul<f32>>::Output
Scales all components of the quaternion by a scalar.
§type Output = Quaternion
type Output = Quaternion
* operator.§impl Mul for Quaternion
impl Mul for Quaternion
§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.
§type Output = Quaternion
type Output = Quaternion
* operator.§impl MulAssign for Quaternion
impl MulAssign for Quaternion
§fn mul_assign(&mut self, rhs: Quaternion)
fn mul_assign(&mut self, rhs: Quaternion)
Combines this rotation with another.
§impl Neg for Quaternion
impl Neg for Quaternion
§fn neg(self) -> <Quaternion as Neg>::Output
fn neg(self) -> <Quaternion as Neg>::Output
Negates all components of the quaternion.
§type Output = Quaternion
type Output = Quaternion
- operator.§impl PartialEq for Quaternion
impl PartialEq for Quaternion
§impl Serialize for Quaternion
impl Serialize for Quaternion
§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,
§impl Sub for Quaternion
impl Sub for Quaternion
§fn sub(self, rhs: Quaternion) -> <Quaternion as Sub>::Output
fn sub(self, rhs: Quaternion) -> <Quaternion as Sub>::Output
Subtracts two quaternions component-wise.
§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 UnsafeUnpin for Quaternion
impl UnwindSafe for Quaternion
Blanket Implementations§
§impl<T> AppLifecycle for Twhere
T: ?Sized,
impl<T> AppLifecycle for Twhere
T: ?Sized,
§fn on_start(&mut self, ctx: &mut dyn AppContext)
fn on_start(&mut self, ctx: &mut dyn AppContext)
update. Apps install their theme / fonts here.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<F, T, S> SimdInto<T, S> for Fwhere
T: SimdFrom<F, S>,
S: Simd,
impl<F, T, S> SimdInto<T, S> for Fwhere
T: SimdFrom<F, S>,
S: Simd,
§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.