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>

source

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.]);
source

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.]);
source

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]);
source

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>

source

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);
source

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);
source

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);
source

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>

source

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>

source

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

Calculates the dot product of 2 Quaternions.

§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);
source

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);
source

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]);
source

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.]);
source

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>

source

pub fn len(&self) -> T

Calculates the length of this Quaternion.

§Examples
assert_eq!(<Quaternion>::from_slice(&[2., 5., 14., 8.]).len(), 17.);
source

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.]);
source

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.]);
source

pub fn lerp(&self, rhs: Self, t: T) -> Self

Linear interpolates between 2 unit Quaternions.

§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>

source

pub fn slerp(&self, rhs: Self, t: T) -> Self

Shperical linear interpolates between 2 unit Quaternions.

§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> Add for Quaternion<T>

§

type Output = Quaternion<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Copy + NumAssign> AddAssign for Quaternion<T>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<T: Copy + NumAssign> AsMut<[T]> for Quaternion<T>

source§

fn as_mut(&mut self) -> &mut [T]

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T: Copy + NumAssign> AsRef<[T]> for Quaternion<T>

source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: Clone + Copy + NumAssign> Clone for Quaternion<T>

source§

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

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + Copy + NumAssign> Debug for Quaternion<T>

source§

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

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

impl<T: Copy + NumAssign> Default for Quaternion<T>

source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for Quaternion<T>
where T: Deserialize<'de> + Copy + NumAssign,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: Copy + NumAssign> Div<T> for Quaternion<T>

§

type Output = Quaternion<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
source§

impl<T: Copy + NumAssign> DivAssign<T> for Quaternion<T>

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<T: Copy + FloatEq<T> + NumAssign> FloatEq<T> for Quaternion<T>

source§

fn float_eq(&self, rhs: Self, epsilon: T) -> bool

Checks if self equals to RHS within an epsilon. Read more
source§

impl<T: Copy + NumAssign> From<&[T]> for Quaternion<T>

source§

fn from(data: &[T]) -> Self

Converts to this type from the input type.
source§

impl<T: Copy + NumAssign> From<[T; 4]> for Quaternion<T>

source§

fn from(data: [T; 4]) -> Self

Converts to this type from the input type.
source§

impl<T: Copy + NumAssign> From<Matrix<T, 3, 1>> for Quaternion<T>

source§

fn from(v: Vec3<T>) -> Self

Creates a Quaternion from a Vec3 using w = 0.

§Examples
let q = Quaternion::from(Vec3::<i32>::from_slice(&[2, 3, 4]));
assert_eq!(*q.as_ref(), [2, 3, 4, 0]);
source§

impl<T: Copy + NumAssign> From<Quaternion<T>> for [T; 4]

source§

fn from(q: Quaternion<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Copy + NumAssign> From<Quaternion<T>> for Vec3<T>

source§

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>

source§

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> Index<usize> for Quaternion<T>

§

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: Copy + NumAssign> IndexMut<usize> for Quaternion<T>

source§

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

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

impl<T: Copy + NumAssign> Mul<T> for Quaternion<T>

§

type Output = Quaternion<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Copy + NumAssign> Mul for Quaternion<T>

§

type Output = Quaternion<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Copy + NumAssign> MulAssign<T> for Quaternion<T>

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<T: Copy + NumAssign> MulAssign for Quaternion<T>

source§

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: Copy + NumAssign> One for Quaternion<T>

source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
source§

impl<T: PartialEq + Copy + NumAssign> PartialEq for Quaternion<T>

source§

fn eq(&self, other: &Quaternion<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for Quaternion<T>
where T: Serialize + Copy + NumAssign,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Copy + NumAssign> Sub for Quaternion<T>

§

type Output = Quaternion<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Copy + NumAssign> SubAssign for Quaternion<T>

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl<T: Copy + NumAssign> Zero for Quaternion<T>

source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
source§

impl<T: Copy + Copy + NumAssign> Copy for Quaternion<T>

source§

impl<T: Copy + NumAssign> StructuralPartialEq for Quaternion<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Quaternion<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> 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,