#[repr(C)]
pub struct Quaternion<T> { pub x: T, pub y: T, pub z: T, pub w: T, }
Expand description

Quaternions 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: Ty: Tz: Tw: T

Implementations

Creates a new quaternion with x, y, z and w elements in order.

You are responsible for ensuring that the resulting quaternion is normalized.

Creates a new quaternion from a scalar-and-vector pair.

You are responsible for ensuring that the resulting quaternion is normalized.

Converts this quaternion into a scalar-and-vector pair by destructuring.

Not to be confused with into_angle_axis().

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.

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

Gets this quaternion’s conjugate (copy with negated vector part).

On normalized quaternions, 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());

Gets this quaternion’s inverse, i.e the one that reverses its effect.

On normalized quaternions, 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());

Gets the dot product between two quaternions.

Gets a normalized copy of this quaternion.

Gets this quaternion’s magnitude, squared.

Gets this quaternion’s magnitude.

Creates a quaternion that would rotate a from direction to to.


let (from, to) = (Vec4::<f32>::unit_x(), Vec4::<f32>::unit_y());
let q = Quaternion::<f32>::rotation_from_to_3d(from, to);
assert_relative_eq!(q * from, to);
assert_relative_eq!(q * Vec4::unit_y(), -Vec4::unit_x());

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

Creates a quaternion from an angle and axis. The axis is not required to be normalized.

Creates a quaternion from an angle for a rotation around the X axis.

Creates a quaternion from an angle for a rotation around the Y axis.

Creates a quaternion from an angle for a rotation around the Y axis.

Returns this quaternion rotated around the given axis with given angle. The axis is not required to be normalized.

Returns this quaternion rotated around the X axis with given angle.

Returns this quaternion rotated around the Y axis with given angle.

Returns this quaternion rotated around the Z axis with given angle.

Rotates this quaternion around the given axis with given angle. The axis is not required to be normalized.

Rotates this quaternion around the X axis with given angle.

Rotates this quaternion around the Y axis with given angle.

Rotates this quaternion around the Z axis with given angle.

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 angle = PI*4./5.;
let axis = Vec3::new(1_f32, 2., 3.);
let q = Quaternion::rotation_3d(angle, axis);
let (a, v) = q.into_angle_axis();
assert_relative_eq!(a, angle);
assert_relative_eq!(v, axis.normalized());

Converts this quaternion to a Vec4 by destructuring.

Creates a quaternion from a Vec4 by destructuring. You are responsible for ensuring that the resulting quaternion is normalized.

Converts this quaternion to a Vec3 by destructuring, dropping the w element.

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.

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.

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.

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.

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

Perform spherical linear interpolation, constraining factor to be between 0 and 1.

Trait Implementations

Used for specifying relative comparisons.

The default tolerance to use when testing values that are close together. Read more

A test for equality that uses the absolute difference to compute the approximate equality of two numbers. Read more

The inverse of AbsDiffEq::abs_diff_eq.

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The default value for a quaternion is the identity.

assert_eq!(Quaternion::<f32>::identity(), Quaternion::default());

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

Deserialize this value from the given Serde deserializer. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Rotation matrices can be obtained from quaternions. This implementation only works properly if the quaternion is normalized.

use std::f32::consts::PI;

let angles = 32;
for i in 0..angles {
    let theta = PI * 2. * (i as f32) / (angles as f32);

    assert_relative_eq!(Mat4::rotation_x(theta), Mat4::from(Quaternion::rotation_x(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat4::rotation_y(theta), Mat4::from(Quaternion::rotation_y(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat4::rotation_z(theta), Mat4::from(Quaternion::rotation_z(theta)), epsilon = 0.000001);

    assert_relative_eq!(Mat4::rotation_x(theta), Mat4::rotation_3d(theta, Vec4::unit_x()));
    assert_relative_eq!(Mat4::rotation_y(theta), Mat4::rotation_3d(theta, Vec4::unit_y()));
    assert_relative_eq!(Mat4::rotation_z(theta), Mat4::rotation_3d(theta, Vec4::unit_z()));

    // 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 m = Mat4::rotation_x(theta);
    assert_relative_eq!(m * v, Vec4::new(0., theta.cos(), theta.sin(), 0.));

    let v = Vec4::unit_z();
    let m = Mat4::rotation_y(theta);
    assert_relative_eq!(m * v, Vec4::new(theta.sin(), 0., theta.cos(), 0.));

    let v = Vec4::unit_x();
    let m = Mat4::rotation_z(theta);
    assert_relative_eq!(m * v, Vec4::new(theta.cos(), theta.sin(), 0., 0.));
}

Performs the conversion.

Performs the conversion.

Rotation matrices can be obtained from quaternions. This implementation only works properly if the quaternion is normalized.

use std::f32::consts::PI;

let angles = 32;
for i in 0..angles {
    let theta = PI * 2. * (i as f32) / (angles as f32);

    assert_relative_eq!(Mat4::rotation_x(theta), Mat4::from(Quaternion::rotation_x(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat4::rotation_y(theta), Mat4::from(Quaternion::rotation_y(theta)), epsilon = 0.000001);
    assert_relative_eq!(Mat4::rotation_z(theta), Mat4::from(Quaternion::rotation_z(theta)), epsilon = 0.000001);

    assert_relative_eq!(Mat4::rotation_x(theta), Mat4::rotation_3d(theta, Vec4::unit_x()));
    assert_relative_eq!(Mat4::rotation_y(theta), Mat4::rotation_3d(theta, Vec4::unit_y()));
    assert_relative_eq!(Mat4::rotation_z(theta), Mat4::rotation_3d(theta, Vec4::unit_z()));

    // 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 m = Mat4::rotation_x(theta);
    assert_relative_eq!(m * v, Vec4::new(0., theta.cos(), theta.sin(), 0.));

    let v = Vec4::unit_z();
    let m = Mat4::rotation_y(theta);
    assert_relative_eq!(m * v, Vec4::new(theta.sin(), 0., theta.cos(), 0.));

    let v = Vec4::unit_x();
    let m = Mat4::rotation_z(theta);
    assert_relative_eq!(m * v, Vec4::new(theta.cos(), theta.sin(), 0., 0.));
}

Performs the conversion.

Performs the conversion.

A Vec4 can be created directly from a quaternion’s x, y, z and w elements.

Performs the conversion.

A Vec3 can be created directly from a quaternion’s x, y and z elements.

Performs the conversion.

A Vec4 can be created directly from a quaternion’s x, y, z and w elements.

Performs the conversion.

A Vec3 can be created directly from a quaternion’s x, y and z elements.

Performs the conversion.

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.

Performs the conversion.

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.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The Lerp implementation for quaternion is the “Normalized LERP”.

The resulting type after performing the LERP operation.

Returns the linear interpolation of from to to with factor unconstrained, using a possibly slower but more precise operation. Read more

Returns the linear interpolation of from to to with factor unconstrained, using the supposedly fastest but less precise implementation. Read more

Version of lerp_unclamped() that used a single RangeInclusive parameter instead of two values.

Version of lerp_unclamped_precise() that used a single RangeInclusive parameter instead of two values.

Alias to lerp_unclamped which constrains factor to be between 0 and 1 (inclusive). Read more

Version of lerp() that used a single RangeInclusive parameter instead of two values.

Alias to lerp_unclamped_precise which constrains factor to be between 0 and 1 (inclusive). Read more

Version of lerp_precise() that used a single RangeInclusive parameter instead of two values.

The Lerp implementation for quaternion is the “Normalized LERP”.

The resulting type after performing the LERP operation.

Returns the linear interpolation of from to to with factor unconstrained, using a possibly slower but more precise operation. Read more

Returns the linear interpolation of from to to with factor unconstrained, using the supposedly fastest but less precise implementation. Read more

Version of lerp_unclamped() that used a single RangeInclusive parameter instead of two values.

Version of lerp_unclamped_precise() that used a single RangeInclusive parameter instead of two values.

Alias to lerp_unclamped which constrains factor to be between 0 and 1 (inclusive). Read more

Version of lerp() that used a single RangeInclusive parameter instead of two values.

Alias to lerp_unclamped_precise which constrains factor to be between 0 and 1 (inclusive). Read more

Version of lerp_precise() that used a single RangeInclusive parameter instead of two values.

The Mul implementation for quaternions 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());

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

3D vectors can be rotated by being premultiplied by a quaternion, assuming the quaternion is normalized.

The resulting type after applying the * operator.

Performs the * operation. Read more

3D vectors can be rotated by being premultiplied by a quaternion, assuming the quaternion is normalized.

The resulting type after applying the * operator.

Performs the * operation. Read more

3D vectors can be rotated by being premultiplied by a quaternion, assuming the quaternion is normalized. On Vec4s, 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.));
}

The resulting type after applying the * operator.

Performs the * operation. Read more

3D vectors can be rotated by being premultiplied by a quaternion, assuming the quaternion is normalized. On Vec4s, 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.));
}

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The default relative tolerance for testing values that are far-apart. Read more

A test for equality that uses a relative comparison if the values are far apart.

The inverse of RelativeEq::relative_eq.

Serialize this value into the given Serde serializer. Read more

The resulting type after performing the SLERP operation.

Performs spherical linear interpolation without implictly constraining factor to be between 0 and 1. Read more

Performs spherical linear interpolation, constraining factor to be between 0 and 1. Read more

The resulting type after performing the SLERP operation.

Performs spherical linear interpolation without implictly constraining factor to be between 0 and 1. Read more

Performs spherical linear interpolation, constraining factor to be between 0 and 1. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The default ULPs to tolerate when testing values that are far-apart. Read more

A test for equality that uses units in the last place (ULP) if the values are far apart.

The inverse of UlpsEq::ulps_eq.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.