Struct vek::quaternion::repr_c::Quaternion
[−]
[src]
#[repr(C)]pub struct Quaternion<T> { pub x: T, pub y: T, pub z: T, pub w: T, }
Quaternion
s are a convenient representation for rotations in 3D spaces.
IMPORTANT: Quaternions are only valid as rotations as long as they are normalized (i.e their magnitude is 1). Most operations assume this, instead of normalizing inputs behind your back, so be careful.
They essentially consist of a vector part (x
, y
, z
), and scalar part (w
).
For unit quaternions, the vector part is the unit axis of rotation scaled by the sine of
the half-angle of the rotation, and the scalar part is the cosine of the half-angle.
Fields
x: T
y: T
z: T
w: T
Methods
impl<T> Quaternion<T>
[src]
fn from_xyzw(x: T, y: T, z: T, w: T) -> Self
[src]
Creates a new Quaternion
with x
, y
, z
and w
elements in order.
You are responsible for ensuring that the resulting Quaternion
is normalized.
fn from_scalar_and_vec3<V: Into<Vec3<T>>>(pair: (T, V)) -> Self
[src]
Creates a new Quaternion
from a scalar-and-vector pair.
You are responsible for ensuring that the resulting Quaternion
is normalized.
fn into_scalar_and_vec3(self) -> (T, Vec3<T>)
[src]
Converts this Quaternion
into a scalar-and-vector pair by destructuring.
Not to be confused with into_angle_axis()
.
fn zero() -> Self where
T: Zero,
[src]
T: Zero,
Creates a new Quaternion
with all elements set to zero.
Be careful: since it has a magnitude equal to zero, it is not valid to use for most operations.
fn identity() -> Self where
T: Zero + One,
[src]
T: Zero + One,
Creates the identity Quaternion
.
use std::f32::consts::PI; let id = Quaternion::<f32>::identity(); assert_eq!(id, Default::default()); assert_relative_eq!(id, id.conjugate()); assert_relative_eq!(id, id.inverse()); let q = Quaternion::rotation_y(PI); assert_relative_eq!(id * q, q); assert_relative_eq!(q * id, q);
fn conjugate(self) -> Self where
T: Neg<Output = T>,
[src]
T: Neg<Output = T>,
Gets this Quaternion
's conjugate (copy with negated vector part).
On normalized Quaternion
s, the conjugate also happens to be the inverse.
use std::f32::consts::PI; let p = Quaternion::rotation_x(PI); let q = Quaternion::rotation_z(PI); assert_relative_eq!((p*q).conjugate(), q.conjugate() * p.conjugate()); // Rotation quaternions are normalized, so their conjugate is also their inverse. assert_relative_eq!(q.conjugate(), q.inverse());
fn inverse(self) -> Self where
T: Neg<Output = T> + Copy + Sum + Mul<Output = T> + Div<Output = T>,
[src]
T: Neg<Output = T> + Copy + Sum + Mul<Output = T> + Div<Output = T>,
Gets this Quaternion
's inverse, i.e the one that reverses its effect.
On normalized Quaternion
s, the inverse happens to be the conjugate.
use std::f32::consts::PI; let rot = Quaternion::rotation_y(PI); let inv = rot.inverse(); assert_relative_eq!(rot*inv, Quaternion::identity()); assert_relative_eq!(inv*rot, Quaternion::identity()); let p = Quaternion::rotation_x(PI); let q = Quaternion::rotation_z(PI); assert_relative_eq!((p*q).inverse(), q.inverse() * p.inverse());
fn dot(self, q: Self) -> T where
T: Copy + Sum + Mul<Output = T>,
[src]
T: Copy + Sum + Mul<Output = T>,
Gets the dot product between two quaternions.
fn normalized(self) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
Gets a normalized copy of this quaternion.
fn magnitude_squared(self) -> T where
T: Float + Sum,
[src]
T: Float + Sum,
Gets this quaternion's magnitude, squared.
fn magnitude(self) -> T where
T: Float + Sum,
[src]
T: Float + Sum,
Gets this quaternion's magnitude.
fn rotation_from_to_3d<V: Into<Vec3<T>>>(from: V, to: V) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
Creates a Quaternion
that would rotate a from
direction to to
.
let (from, to) = (Vec4::<f32>::unit_x(), Vec4::<f32>::unit_z()); let q = Quaternion::<f32>::rotation_from_to_3d(from, to); assert_relative_eq!(q * from, to); let (from, to) = (Vec4::<f32>::unit_x(), -Vec4::<f32>::unit_x()); let q = Quaternion::<f32>::rotation_from_to_3d(from, to); assert_relative_eq!(q * from, to);
fn rotation_3d<V: Into<Vec3<T>>>(angle_radians: T, axis: V) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
Creates a Quaternion
from an angle and axis.
fn rotation_x(angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotation_y(angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotation_z(angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotated_3d<V: Into<Vec3<T>>>(self, angle_radians: T, axis: V) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotated_x(self, angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotated_y(self, angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotated_z(self, angle_radians: T) -> Self where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotate_3d<V: Into<Vec3<T>>>(&mut self, angle_radians: T, axis: V) where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotate_x(&mut self, angle_radians: T) where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotate_y(&mut self, angle_radians: T) where
T: Float + Sum,
[src]
T: Float + Sum,
fn rotate_z(&mut self, angle_radians: T) where
T: Float + Sum,
[src]
T: Float + Sum,
fn into_angle_axis(self) -> (T, Vec3<T>) where
T: Float,
[src]
T: Float,
Convert this quaternion to angle-axis representation, assuming the quaternion is normalized.
use std::f32::consts::PI; let q = Quaternion::rotation_x(PI/2.); let (angle, axis) = q.into_angle_axis(); assert_relative_eq!(angle, PI/2.); assert_relative_eq!(axis, Vec3::unit_x()); let q = Quaternion::rotation_3d(PI*4./5., Vec3::new(1., 2., 3.)); let (angle, axis) = q.into_angle_axis(); assert_relative_eq!(angle, PI*4./5.); assert_relative_eq!(axis, Vec3::new(1., 2., 3.));
fn into_vec4(self) -> Vec4<T>
[src]
fn from_vec4(v: Vec4<T>) -> Self
[src]
fn into_vec3(self) -> Vec3<T>
[src]
impl<T> Quaternion<T> where
T: Copy + One + Mul<Output = T> + Sub<Output = T> + MulAdd<T, T, Output = T>,
[src]
T: Copy + One + Mul<Output = T> + Sub<Output = T> + MulAdd<T, T, Output = T>,
fn lerp_precise_unnormalized(from: Self, to: Self, factor: T) -> Self where
T: Clamp + Zero,
[src]
T: Clamp + Zero,
Performs linear interpolation without normalizing the result, using an implementation that supposedly yields a more precise result.
This is probably not what you're looking for.
For an implementation that normalizes the result (which is more commonly wanted), see the Lerp
implementation.
fn lerp_unclamped_precise_unnormalized(from: Self, to: Self, factor: T) -> Self
[src]
Performs linear interpolation without normalizing the result and without
implicitly constraining factor
to be between 0 and 1,
using an implementation that supposedly yields a more precise result.
This is probably not what you're looking for.
For an implementation that normalizes the result (which is more commonly wanted), see the Lerp
implementation.
impl<T> Quaternion<T> where
T: Copy + Sub<Output = T> + MulAdd<T, T, Output = T>,
[src]
T: Copy + Sub<Output = T> + MulAdd<T, T, Output = T>,
fn lerp_unnormalized(from: Self, to: Self, factor: T) -> Self where
T: Clamp + Zero + One,
[src]
T: Clamp + Zero + One,
Performs linear interpolation without normalizing the result.
This is probably not what you're looking for.
For an implementation that normalizes the result (which is more commonly wanted), see the Lerp
implementation.
fn lerp_unclamped_unnormalized(from: Self, to: Self, factor: T) -> Self
[src]
Performs linear interpolation without normalizing the result and without
implicitly constraining factor
to be between 0 and 1.
This is probably not what you're looking for.
For an implementation that normalizes the result (which is more commonly wanted), see the Lerp
implementation.
impl<T> Quaternion<T> where
T: Lerp<T, Output = T> + Sum + Float,
[src]
T: Lerp<T, Output = T> + Sum + Float,
fn slerp_unclamped(from: Self, to: Self, factor: T) -> Self
[src]
Performs spherical linear interpolation without implictly constraining factor
to
be between 0 and 1.
use std::f32::consts::PI; let from = Quaternion::rotation_z(0_f32); let to = Quaternion::rotation_z(PI*9./10.); let angles = 32; for i in 0..angles { let factor = (i as f32) / (angles as f32); let expected = Quaternion::rotation_z(factor * PI*9./10.); let slerp = Quaternion::slerp(from, to, factor); assert_relative_eq!(slerp, expected); }
fn slerp(from: Self, to: Self, factor: T) -> Self where
T: Clamp,
[src]
T: Clamp,
Perform spherical linear interpolation, constraining factor
to
be between 0 and 1.
Trait Implementations
impl<T: Debug> Debug for Quaternion<T>
[src]
impl<T: Clone> Clone for Quaternion<T>
[src]
fn clone(&self) -> Quaternion<T>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T: Copy> Copy for Quaternion<T>
[src]
impl<T: Hash> Hash for Quaternion<T>
[src]
fn hash<__HT: Hasher>(&self, __arg_0: &mut __HT)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T: Eq> Eq for Quaternion<T>
[src]
impl<T: PartialEq> PartialEq for Quaternion<T>
[src]
fn eq(&self, __arg_0: &Quaternion<T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Quaternion<T>) -> bool
[src]
This method tests for !=
.
impl<T: Zero + One> Default for Quaternion<T>
[src]
The default value for a Quaternion
is the identity.
assert_eq!(Quaternion::<f32>::identity(), Quaternion::default());
impl<T, Factor> Lerp<Factor> for Quaternion<T> where
T: Lerp<Factor, Output = T> + Sum + Float,
Factor: Copy,
[src]
T: Lerp<Factor, Output = T> + Sum + Float,
Factor: Copy,
The Lerp
implementation for Quaternion
is the "Normalized LERP".
type Output = Self
The resulting type after performing the LERP operation.
fn lerp_unclamped_precise(from: Self, to: Self, factor: Factor) -> Self
[src]
Returns the linear interpolation of from
to to
with factor
unconstrained, using a possibly slower but more precise operation. Read more
fn lerp_unclamped(from: Self, to: Self, factor: Factor) -> Self
[src]
Returns the linear interpolation of from
to to
with factor
unconstrained, using the supposedly fastest but less precise implementation. Read more
fn lerp(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Alias to lerp_unclamped
which constrains factor
to be between 0 and 1 (inclusive). Read more
fn lerp_precise(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Alias to lerp_unclamped_precise
which constrains factor
to be between 0 and 1 (inclusive). Read more
impl<'a, T, Factor> Lerp<Factor> for &'a Quaternion<T> where
T: Lerp<Factor, Output = T> + Sum + Float,
Factor: Copy,
[src]
T: Lerp<Factor, Output = T> + Sum + Float,
Factor: Copy,
The Lerp
implementation for Quaternion
is the "Normalized LERP".
type Output = Quaternion<T>
The resulting type after performing the LERP operation.
fn lerp_unclamped_precise(from: Self, to: Self, factor: Factor) -> Quaternion<T>
[src]
Returns the linear interpolation of from
to to
with factor
unconstrained, using a possibly slower but more precise operation. Read more
fn lerp_unclamped(from: Self, to: Self, factor: Factor) -> Quaternion<T>
[src]
Returns the linear interpolation of from
to to
with factor
unconstrained, using the supposedly fastest but less precise implementation. Read more
fn lerp(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Alias to lerp_unclamped
which constrains factor
to be between 0 and 1 (inclusive). Read more
fn lerp_precise(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Alias to lerp_unclamped_precise
which constrains factor
to be between 0 and 1 (inclusive). Read more
impl<T, Factor> Slerp<Factor> for Quaternion<T> where
T: Lerp<T, Output = T> + Sum + Float,
Factor: Into<T>,
[src]
T: Lerp<T, Output = T> + Sum + Float,
Factor: Into<T>,
type Output = Self
The resulting type after performing the SLERP operation.
fn slerp_unclamped(from: Self, to: Self, factor: Factor) -> Self
[src]
Performs spherical linear interpolation without implictly constraining factor
to be between 0 and 1. Read more
fn slerp(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Performs spherical linear interpolation, constraining factor
to be between 0 and 1. Read more
impl<'a, T, Factor> Slerp<Factor> for &'a Quaternion<T> where
T: Lerp<T, Output = T> + Sum + Float,
Factor: Into<T>,
[src]
T: Lerp<T, Output = T> + Sum + Float,
Factor: Into<T>,
type Output = Quaternion<T>
The resulting type after performing the SLERP operation.
fn slerp_unclamped(from: Self, to: Self, factor: Factor) -> Quaternion<T>
[src]
Performs spherical linear interpolation without implictly constraining factor
to be between 0 and 1. Read more
fn slerp(from: Self, to: Self, factor: Factor) -> Self::Output where
Factor: Clamp + Zero + One,
[src]
Factor: Clamp + Zero + One,
Performs spherical linear interpolation, constraining factor
to be between 0 and 1. Read more
impl<T> Neg for Quaternion<T> where
T: Neg<Output = T>,
[src]
T: Neg<Output = T>,
type Output = Self
The resulting type after applying the -
operator.
fn neg(self) -> Self::Output
[src]
Performs the unary -
operation.
impl<T> Div<T> for Quaternion<T> where
T: Copy + Div<Output = T>,
[src]
T: Copy + Div<Output = T>,
type Output = Self
The resulting type after applying the /
operator.
fn div(self, rhs: T) -> Self::Output
[src]
Performs the /
operation.
impl<T> Add for Quaternion<T> where
T: Add<Output = T>,
[src]
T: Add<Output = T>,
type Output = Self
The resulting type after applying the +
operator.
fn add(self, rhs: Self) -> Self::Output
[src]
Performs the +
operation.
impl<T> Sub for Quaternion<T> where
T: Sub<Output = T>,
[src]
T: Sub<Output = T>,
type Output = Self
The resulting type after applying the -
operator.
fn sub(self, rhs: Self) -> Self::Output
[src]
Performs the -
operation.
impl<T> Mul for Quaternion<T> where
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + Sum,
[src]
T: Copy + Mul<Output = T> + Sub<Output = T> + Zero + Sum,
The Mul
implementation for Quaternion
s is concatenation, a.k.a Grassman product.
use std::f32::consts::PI; let v = Vec4::unit_x(); let p = Quaternion::rotation_y(PI/2.); let q = Quaternion::rotation_z(PI/2.); assert_relative_eq!((p*q)*v, p*(q*v)); assert_relative_eq!(p*q*v, Vec4::unit_y()); assert_relative_eq!(q*p*v, -Vec4::unit_z());
type Output = Self
The resulting type after applying the *
operator.
fn mul(self, rhs: Self) -> Self::Output
[src]
Performs the *
operation.
impl<T> Mul<T> for Quaternion<T> where
T: Mul<Output = T> + Copy,
[src]
T: Mul<Output = T> + Copy,
type Output = Self
The resulting type after applying the *
operator.
fn mul(self, rhs: T) -> Self::Output
[src]
Performs the *
operation.
impl<T: ApproxEq> ApproxEq for Quaternion<T> where
T::Epsilon: Copy,
[src]
T::Epsilon: Copy,
type Epsilon = T::Epsilon
Used for specifying relative comparisons.
fn default_epsilon() -> T::Epsilon
[src]
The default tolerance to use when testing values that are close together. Read more
fn default_max_relative() -> T::Epsilon
[src]
The default relative tolerance for testing values that are far-apart. Read more
fn default_max_ulps() -> u32
[src]
The default ULPs to tolerate when testing values that are far-apart. Read more
fn relative_eq(
&self,
other: &Self,
epsilon: T::Epsilon,
max_relative: T::Epsilon
) -> bool
[src]
&self,
other: &Self,
epsilon: T::Epsilon,
max_relative: T::Epsilon
) -> bool
A test for equality that uses a relative comparison if the values are far apart.
fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool
[src]
A test for equality that uses units in the last place (ULP) if the values are far apart.
fn relative_ne(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
[src]
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
The inverse of ApproxEq::relative_eq
.
fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool
[src]
The inverse of ApproxEq::ulps_eq
.
impl<T: Float + Sum> Mul<Vec4<T>> for Quaternion<T>
[src]
3D vectors can be rotated by being premultiplied by a quaternion, assuming the
quaternion is normalized.
On Vec4
s, the w
element is preserved, so you can safely rotate
points and directions.
use std::f32::consts::PI; let v = Vec4::unit_x(); let q = Quaternion::<f32>::identity(); assert_relative_eq!(q * v, v); let q = Quaternion::rotation_z(PI); assert_relative_eq!(q * v, -v); let q = Quaternion::rotation_z(PI * 0.5); assert_relative_eq!(q * v, Vec4::unit_y()); let q = Quaternion::rotation_z(PI * 1.5); assert_relative_eq!(q * v, -Vec4::unit_y()); let angles = 32; for i in 0..angles { let theta = PI * 2. * (i as f32) / (angles as f32); // See what rotating unit vectors do for most angles between 0 and 2*PI. // It's helpful to picture this as a right-handed coordinate system. let v = Vec4::unit_y(); let q = Quaternion::rotation_x(theta); assert_relative_eq!(q * v, Vec4::new(0., theta.cos(), theta.sin(), 0.)); let v = Vec4::unit_z(); let q = Quaternion::rotation_y(theta); assert_relative_eq!(q * v, Vec4::new(theta.sin(), 0., theta.cos(), 0.)); let v = Vec4::unit_x(); let q = Quaternion::rotation_z(theta); assert_relative_eq!(q * v, Vec4::new(theta.cos(), theta.sin(), 0., 0.)); }
type Output = Vec4<T>
The resulting type after applying the *
operator.
fn mul(self, rhs: Vec4<T>) -> Self::Output
[src]
Performs the *
operation.
impl<T: Float + Sum> Mul<Vec3<T>> for Quaternion<T>
[src]
3D vectors can be rotated by being premultiplied by a quaternion, assuming the quaternion is normalized.
type Output = Vec3<T>
The resulting type after applying the *
operator.
fn mul(self, rhs: Vec3<T>) -> Self::Output
[src]
Performs the *
operation.
impl<T> From<Vec4<T>> for Quaternion<T>
[src]
A Quaternion
can be created directly from a Vec4
's x
, y
, z
and w
elements.
You are responsible for ensuring that the resulting Quaternion
is normalized.