Struct munum::Quaternion
source · pub struct Quaternion<T: Copy + NumAssign = f32>(/* private fields */);
Expand description
A quaternion in (x, y, z, w) order, where q = w + xi + yj + zk.
Implementations§
source§impl<T: Copy + NumAssign> Quaternion<T>
impl<T: Copy + NumAssign> Quaternion<T>
sourcepub fn new(x: T, y: T, z: T, w: T) -> Self
pub fn new(x: T, y: T, z: T, w: T) -> Self
Creates a new quaternion.
§Examples
let q = <Quaternion>::new(1., 2., 3., 4.);
assert_eq!(*q.as_ref(), [1., 2., 3., 4.]);
sourcepub fn from_array(arr: [T; 4]) -> Self
pub fn from_array(arr: [T; 4]) -> Self
Creates a quaternion from raw array.
§Examples
let q = <Quaternion>::from_array([1., 2., 3., 4.]);
assert_eq!(*q.as_ref(), [1., 2., 3., 4.]);
sourcepub fn identity() -> Self
pub fn identity() -> Self
Creates an identity quaternion.
§Examples
let q = <Quaternion>::identity();
assert_eq!(*q.as_ref(), [0.0, 0.0, 0.0, 1.0]);
sourcepub fn from_slice(data: &[T]) -> Self
pub fn from_slice(data: &[T]) -> Self
Creates a quaternion from slice.
§Examples
let q = <Quaternion>::from_slice(&[1., 2., 3., 4.]);
assert_eq!(*q.as_ref(), [1., 2., 3., 4.]);
source§impl<T: Copy + FloatOps + NumAssign> Quaternion<T>
impl<T: Copy + FloatOps + NumAssign> Quaternion<T>
sourcepub fn from_axis_angle(axis: Vec3<T>, angle: T) -> Self
pub fn from_axis_angle(axis: Vec3<T>, angle: T) -> Self
Creates a quaternion from a unit axis vector and rotation angle in couterclockwise direction.
§Examples
let q = <Quaternion>::from_axis_angle(<Vec3>::from_slice(&[3., 5., 7.]), PI/3.);
let expected = <Quaternion>::from_slice(&[3. * (PI/6.).sin(), 5. * (PI/6.).sin(), 7. * (PI/6.).sin(), (PI/6.).cos()]);
assert_float_eq!(q, expected, 0.00001);
sourcepub fn from_angle_x(angle: T) -> Self
pub fn from_angle_x(angle: T) -> Self
Creates a quaternion from a rotation angle around x-axis in couterclockwise direction.
§Examples
let q = <Quaternion>::from_angle_x(PI/3.);
let expected = <Quaternion>::from_slice(&[(PI/6.).sin(), 0., 0., (PI/6.).cos()]);
assert_float_eq!(q, expected, 0.00001);
sourcepub fn from_angle_y(angle: T) -> Self
pub fn from_angle_y(angle: T) -> Self
Creates a quaternion from a rotation angle around x-axis in couterclockwise direction.
§Examples
let q = <Quaternion>::from_angle_y(PI/3.);
let expected = <Quaternion>::from_slice(&[0., (PI/6.).sin(), 0., (PI/6.).cos()]);
assert_float_eq!(q, expected, 0.00001);
sourcepub fn from_angle_z(angle: T) -> Self
pub fn from_angle_z(angle: T) -> Self
Creates a quaternion from a rotation angle around x-axis in couterclockwise direction.
§Examples
let q = <Quaternion>::from_angle_z(PI/3.);
let expected = <Quaternion>::from_slice(&[0., 0., (PI/6.).sin(), (PI/6.).cos()]);
assert_float_eq!(q, expected, 0.00001);
source§impl<T: Copy + FloatOps + FloatEq<T> + NumAssign + NumCast + Signed> Quaternion<T>
impl<T: Copy + FloatOps + FloatEq<T> + NumAssign + NumCast + Signed> Quaternion<T>
sourcepub fn from_unit_vecs(from: Vec3<T>, to: Vec3<T>) -> Self
pub fn from_unit_vecs(from: Vec3<T>, to: Vec3<T>) -> Self
Creates a quaternion that represents the shortest arc rotation between 2 unit vectors.
§Examples
let q = <Quaternion>::from_unit_vecs(<Vec3>::from_slice(&[0., 0., 1.]), <Vec3>::from_slice(&[1., 0., 0.]));
let expected = <Quaternion>::from_slice(&[0., (PI/4.).sin(), 0., (PI/4.).cos()]);
assert_float_eq!(q, expected, 0.00001);
source§impl<T: Copy + NumAssign> Quaternion<T>
impl<T: Copy + NumAssign> Quaternion<T>
sourcepub fn dot(&self, rhs: Self) -> T
pub fn dot(&self, rhs: Self) -> T
Calculates the dot product of 2 Quaternion
s.
§Examples
let (v1, v2) = (Quaternion::<i32>::from_slice(&[29, 31, 37, 41]), Quaternion::<i32>::from_slice(&[43, 47, 53, 59]));
assert_eq!(v1.dot(v2), 7084);
sourcepub fn sqr_len(&self) -> T
pub fn sqr_len(&self) -> T
Calculates the square length of a 2 Quaternion
.
§Examples
assert_eq!(Quaternion::<i32>::from_slice(&[2, 5, 14, 8]).sqr_len(), 289);
sourcepub fn conj(&mut self)
pub fn conj(&mut self)
Transforms this Quaternion
into its conjugate.
§Examples
let mut q = Quaternion::<i32>::from_slice(&[2, 5, 14, 8]);
q.conj();
assert_eq!(*q.as_ref(), [-2, -5, -14, 8]);
sourcepub fn invert(&mut self) -> bool
pub fn invert(&mut self) -> bool
Inverts this Quaternion
.
§Examples
let mut q = <Quaternion>::from_slice(&[2., 5., 14., 8.]);
assert!(q.invert());
assert_eq!(*q.as_ref(), [-2. / 289., -5. / 289., -14. / 289., 8. / 289.]);
sourcepub fn rotate_vec3(self, v: Vec3<T>) -> Vec3<T>
pub fn rotate_vec3(self, v: Vec3<T>) -> Vec3<T>
Returns the result from rotating given Vec3
by this Quaternion
, using the formula v’ = q * v * q^-1.
See: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Using_quaternion_as_rotations
§Examples
let q = <Quaternion>::from_slice(&[0., 1./2_f32.sqrt(), 0., 1./2_f32.sqrt()]);
let v = <Vec3>::from_slice(&[1. ,1., 1.]);
assert_eq!(*q.rotate_vec3(v).as_ref(), [1., 1., -1.]);
source§impl<T: Copy + FloatOps + NumAssign + Signed> Quaternion<T>
impl<T: Copy + FloatOps + NumAssign + Signed> Quaternion<T>
sourcepub fn len(&self) -> T
pub fn len(&self) -> T
Calculates the length of this Quaternion
.
§Examples
assert_eq!(<Quaternion>::from_slice(&[2., 5., 14., 8.]).len(), 17.);
sourcepub fn normalize(&mut self) -> bool
pub fn normalize(&mut self) -> bool
Normizalizes this Quaternion
.
§Examples
let mut q = <Quaternion>::from_slice(&[2., 5., 14., 8.]);
assert!(q.normalize());
assert_float_eq!(q.as_ref(), &[2./17., 5./17., 14./17., 8./17.]);
sourcepub fn normalized(&self) -> Option<Self>
pub fn normalized(&self) -> Option<Self>
Returns a normalized version of this vector.
§Examples
let q = <Quaternion>::from_slice(&[2., 5., 14., 8.]).normalized().unwrap();
assert_float_eq!(q.as_ref(), &[2./17., 5./17., 14./17., 8./17.]);
sourcepub fn lerp(&self, rhs: Self, t: T) -> Self
pub fn lerp(&self, rhs: Self, t: T) -> Self
Linear interpolates between 2 unit Quaternion
s.
§Examples
let (q1, q2) = (<Quaternion>::from_slice(&[(PI/4.).sin(), 0., 0., (PI/4.).cos()]), <Quaternion>::from_slice(&[(PI/4.).sin(), 0., 0., -(PI/4.).cos()]));
assert_eq!(*q1.lerp(q2, 0.5).as_ref(), [1., 0., 0., 0.]);
source§impl<T: Copy + FloatOps + NumAssign + NumCast + Signed> Quaternion<T>
impl<T: Copy + FloatOps + NumAssign + NumCast + Signed> Quaternion<T>
sourcepub fn slerp(&self, rhs: Self, t: T) -> Self
pub fn slerp(&self, rhs: Self, t: T) -> Self
Shperical linear interpolates between 2 unit Quaternion
s.
§Examples
let (q1, q2) = (<Quaternion>::from_slice(&[(PI/6.).sin(), 0., 0., (PI/6.).cos()]), <Quaternion>::from_slice(&[-(PI/6.).cos(), 0., 0., -(PI/6.).sin()]));
assert_float_eq!(q1.slerp(q2, 0.5).as_ref(), &[(PI/4.).sin(), 0., 0., (PI/4.).cos()]);
Trait Implementations§
source§impl<T: Copy + NumAssign> AddAssign for Quaternion<T>
impl<T: Copy + NumAssign> AddAssign for Quaternion<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<T: Clone + Copy + NumAssign> Clone for Quaternion<T>
impl<T: Clone + Copy + NumAssign> Clone for Quaternion<T>
source§fn clone(&self) -> Quaternion<T>
fn clone(&self) -> Quaternion<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'de, T> Deserialize<'de> for Quaternion<T>
impl<'de, T> Deserialize<'de> for Quaternion<T>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<T: Copy + NumAssign> DivAssign<T> for Quaternion<T>
impl<T: Copy + NumAssign> DivAssign<T> for Quaternion<T>
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moresource§impl<T: Copy + NumAssign> From<Quaternion<T>> for [T; 4]
impl<T: Copy + NumAssign> From<Quaternion<T>> for [T; 4]
source§fn from(q: Quaternion<T>) -> Self
fn from(q: Quaternion<T>) -> Self
source§impl<T: Copy + NumAssign> From<Quaternion<T>> for Vec3<T>
impl<T: Copy + NumAssign> From<Quaternion<T>> for Vec3<T>
source§fn from(q: Quaternion<T>) -> Self
fn from(q: Quaternion<T>) -> Self
Creates a Vec3
from a Quaternion
by dropping the w
component.
§Examples
let v = Vec3::from(Quaternion::<i32>::from_slice(&[2, 3, 4, 5]));
assert_eq!(*v.as_ref(), [2, 3, 4]);
source§impl<T: Copy + NumAssign> From<Quaternion<T>> for Mat3<T>
impl<T: Copy + NumAssign> From<Quaternion<T>> for Mat3<T>
source§fn from(q: Quaternion<T>) -> Self
fn from(q: Quaternion<T>) -> Self
Converts a Quaternion
into a Mat3
§Examples
let ONE_OVER_SQRT3: f32 = 1. / 3_f32.sqrt();
let m = <Mat3>::from(<Quaternion>::from_slice(&[ONE_OVER_SQRT3, ONE_OVER_SQRT3, ONE_OVER_SQRT3, 0.]));
assert_float_eq!(m.as_ref(), &[-1./3., 2./3., 2./3., 2./3., -1./3., 2./3., 2./3., 2./3., -1./3.]);
source§impl<T: Copy + NumAssign> MulAssign<T> for Quaternion<T>
impl<T: Copy + NumAssign> MulAssign<T> for Quaternion<T>
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moresource§impl<T: Copy + NumAssign> MulAssign for Quaternion<T>
impl<T: Copy + NumAssign> MulAssign for Quaternion<T>
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Calculate the Hamilton product of 2
§Examples
let mut q = Quaternion::<i32>::from_slice(&[2, 3, 5, -7]);
q *= Quaternion::<i32>::from_slice(&[11, 13, -17, 19]);
assert_eq!(*q.as_ref(), [-155, 55, 207, -109]);
source§impl<T: PartialEq + Copy + NumAssign> PartialEq for Quaternion<T>
impl<T: PartialEq + Copy + NumAssign> PartialEq for Quaternion<T>
source§fn eq(&self, other: &Quaternion<T>) -> bool
fn eq(&self, other: &Quaternion<T>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T> Serialize for Quaternion<T>
impl<T> Serialize for Quaternion<T>
source§impl<T: Copy + NumAssign> SubAssign for Quaternion<T>
impl<T: Copy + NumAssign> SubAssign for Quaternion<T>
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more