packages

spatial::Quat

On this page

QuatSource

pub struct Quat<T = f32>

A rotation in 3D as a unit quaternion x i + y j + z k + w. Quat alone is Quat<f32>; DQuat is the f64 form. #[repr(C)] with x, y, z, w in that order, as glTF and most shaders store one. a * b rotates by b first, then a, as matrices do; q * v rotates the vector v.

nabi
use spatial::{Quat, Vec3};

let q = Quat::from_rotation_z(f32::PI / 2.0);
assert((q * Vec3::X).approx_eq(Vec3::Y, 1e-6));
  • pub x: T§

    Source
  • pub y: T§

    Source
  • pub z: T§

    Source
  • pub w: T§

    Source
  • pub const fn from_xyzw(x: T, y: T, z: T, w: T) -> Quat<T>§

    Source

    The quaternion of its components, as given: not normalized.

    nabi
    assert(spatial::DQuat::from_xyzw(0.0, 0.0, 0.0, 1.0) == spatial::DQuat::IDENTITY);
    
  • pub fn identity() -> Quat<T>§

    Source

    No rotation.

    nabi
    assert(spatial::Quat::<f32>::identity() == spatial::Quat::IDENTITY);
    
  • pub const fn from_array(a: [T; 4]) -> Quat<T>§

    Source

    From [x, y, z, w].

    nabi
    assert(spatial::DQuat::from_array([0.0, 0.0, 0.0, 1.0]) == spatial::DQuat::IDENTITY);
    
  • pub const fn to_array(self) -> [T; 4]§

    Source

    [x, y, z, w].

    nabi
    assert(spatial::Quat::IDENTITY.to_array() == [0.0, 0.0, 0.0, 1.0]);
    
  • pub const fn to_vec4(self) -> Vec4<T>§

    Source

    (x, y, z, w) as a Vec4.

    nabi
    assert(spatial::Quat::IDENTITY.to_vec4() == spatial::Vec4::W);
    
  • pub fn from_axis_angle(axis: Vec3<T>, angle: T) -> Quat<T>§

    Source

    The rotation by angle radians about the unit vector axis, counterclockwise when the axis points at the viewer.

    nabi
    use spatial::{Quat, Vec3};
    
    let q = Quat::from_axis_angle(Vec3::Y, f32::PI);
    assert((q * Vec3::X).approx_eq(-Vec3::X, 1e-6));
    
  • pub fn from_rotation_x(angle: T) -> Quat<T>§

    Source

    The rotation by angle radians about +x.

    nabi
    use spatial::{Quat, Vec3};
    
    assert((Quat::from_rotation_x(f32::PI / 2.0) * Vec3::Y).approx_eq(Vec3::Z, 1e-6));
    
  • pub fn from_rotation_y(angle: T) -> Quat<T>§

    Source

    The rotation by angle radians about +y.

    nabi
    use spatial::{Quat, Vec3};
    
    assert((Quat::from_rotation_y(f32::PI / 2.0) * Vec3::Z).approx_eq(Vec3::X, 1e-6));
    
  • pub fn from_rotation_z(angle: T) -> Quat<T>§

    Source

    The rotation by angle radians about +z.

    nabi
    use spatial::{Quat, Vec3};
    
    assert((Quat::from_rotation_z(f32::PI / 2.0) * Vec3::X).approx_eq(Vec3::Y, 1e-6));
    
  • pub fn from_euler(order: EulerOrder, a: T, b: T, c: T) -> Quat<T>§

    Source

    The rotation of Euler angles a, b, c in radians about the axes order names, in that order: see EulerOrder.

    nabi
    use spatial::{EulerOrder, Quat};
    
    let q = Quat::from_euler(EulerOrder::YXZ, 0.5, 0.0, 0.0);
    assert(q.approx_eq(Quat::from_rotation_y(0.5), 1e-6));
    
  • pub fn to_euler(self, order: EulerOrder) -> (T, T, T)§

    Source

    The Euler angles (a, b, c) of this rotation in order, so that Quat::from_euler(order, a, b, c) gives it back. b is in -pi/2..=pi/2, a and c in -pi..=pi; at b = +-pi/2 (gimbal lock) c is 0.

    nabi
    use spatial::{EulerOrder, Quat};
    
    let q = Quat::<f64>::from_euler(EulerOrder::ZYX, 0.1, 0.2, 0.3);
    let (a, b, c) = q.to_euler(EulerOrder::ZYX);
    assert((a - 0.1).abs() < 1e-12 && (b - 0.2).abs() < 1e-12 && (c - 0.3).abs() < 1e-12);
    
  • pub fn from_mat3(m: Mat3<T>) -> Quat<T>§

    Source

    The rotation part of a rotation matrix (orthonormal columns, no scale).

    nabi
    use spatial::{Mat3, Quat};
    
    let q = Quat::from_rotation_x(0.7);
    assert(Quat::from_mat3(Mat3::from_quat(q)).approx_eq(q, 1e-6));
    
  • pub fn from_mat4(m: Mat4<T>) -> Quat<T>§

    Source

    The rotation part of a rotation-and-translation matrix.

    nabi
    use spatial::{Mat4, Quat};
    
    let q = Quat::from_rotation_y(0.3);
    assert(Quat::from_mat4(Mat4::from_quat(q)).approx_eq(q, 1e-6));
    
  • pub fn from_rotation_arc(from: Vec3<T>, to: Vec3<T>) -> Quat<T>§

    Source

    The shortest rotation that turns the unit vector from into the unit vector to. Opposite vectors turn by pi about some axis at right angles to them.

    nabi
    use spatial::{Quat, Vec3};
    
    let q = Quat::from_rotation_arc(Vec3::X, Vec3::Z);
    assert((q * Vec3::X).approx_eq(Vec3::Z, 1e-6));
    
  • pub fn to_axis_angle(self) -> (Vec3<T>, T)§

    Source

    The unit axis and the angle in radians, 0..=pi for a normalized quaternion with w >= 0; no rotation gives (+x, 0).

    nabi
    use spatial::{Quat, Vec3};
    
    let (axis, angle) = Quat::from_axis_angle(Vec3::Z, 0.5).to_axis_angle();
    assert(axis.approx_eq(Vec3::Z, 1e-6) && (angle - 0.5).abs() < 1e-6);
    
  • pub fn conjugate(self) -> Quat<T>§

    Source

    (-x, -y, -z, w): the inverse of a unit quaternion.

    nabi
    let q = spatial::DQuat::from_rotation_x(0.4);
    assert((q * q.conjugate()).approx_eq(spatial::DQuat::IDENTITY, 1e-12));
    
  • pub fn inverse(self) -> Quat<T>§

    Source

    The inverse rotation. For a unit quaternion this is conjugate; any other is divided by its squared length too.

    nabi
    let q = spatial::DQuat::from_rotation_y(1.0) * 2.0;
    assert((q * q.inverse()).approx_eq(spatial::DQuat::IDENTITY, 1e-12));
    
  • pub fn dot(self, o: Quat<T>) -> T§

    Source

    The four-dimensional dot product; +-1 for the same rotation.

    nabi
    assert(spatial::Quat::IDENTITY.dot(spatial::Quat::IDENTITY) == 1.0);
    
  • pub fn length_squared(self) -> T§

    Source

    The squared length.

    nabi
    assert(spatial::Quat::IDENTITY.length_squared() == 1.0);
    
  • pub fn length(self) -> T§

    Source

    The length; 1 for a rotation.

    nabi
    assert((spatial::Quat::from_rotation_z(2.0).length() - 1.0).abs() < 1e-6);
    
  • pub fn normalize(self) -> Quat<T>§

    Source

    The quaternion scaled to length 1.

    nabi
    assert((spatial::Quat::IDENTITY * 3.0).normalize() == spatial::Quat::IDENTITY);
    
  • pub fn is_normalized(self) -> bool§

    Source

    Whether the length is 1, within 1e-4 of the squared length.

    nabi
    assert(spatial::Quat::from_rotation_x(1.0).is_normalized());
    
  • pub fn angle_between(self, o: Quat<T>) -> T§

    Source

    The angle in radians between the two rotations, 0..=pi.

    nabi
    use spatial::DQuat;
    
    let a = DQuat::from_rotation_z(0.25);
    assert((a.angle_between(DQuat::IDENTITY) - 0.25).abs() < 1e-12);
    
  • pub fn rotate(self, v: Vec3<T>) -> Vec3<T>§

    Source

    The vector v rotated, the same as self * v.

    nabi
    use spatial::{Quat, Vec3};
    
    let q = Quat::from_rotation_x(f32::PI);
    assert(q.rotate(Vec3::Y).approx_eq(-Vec3::Y, 1e-6));
    
  • pub fn nlerp(self, o: Quat<T>, t: T) -> Quat<T>§

    Source

    Linear interpolation of the components, then normalized: cheaper than slerp, constant speed only for small steps. Takes the short way round.

    nabi
    use spatial::Quat;
    
    let q = Quat::IDENTITY.nlerp(Quat::from_rotation_z(1.0), 0.5);
    assert(q.approx_eq(Quat::from_rotation_z(0.5), 1e-6));
    
  • pub fn slerp(self, o: Quat<T>, t: T) -> Quat<T>§

    Source

    Spherical interpolation: constant angular speed, t = 0 gives self and t = 1 gives o. Takes the short way round; nearly equal rotations fall back to nlerp.

    nabi
    use spatial::Quat;
    
    let q = Quat::<f64>::IDENTITY.slerp(Quat::from_rotation_y(2.0), 0.25);
    assert(q.approx_eq(Quat::from_rotation_y(0.5), 1e-12));
    
  • pub fn approx_eq(self, o: Quat<T>, eps: T) -> bool§

    Source

    Whether every component is within eps of o's. q and -q are the same rotation but are not approximately equal here; compare angle_between for that.

    nabi
    assert(spatial::Quat::IDENTITY.approx_eq(spatial::Quat::from_rotation_x(1e-7), 1e-6));
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether no component is NaN or infinite.

    nabi
    assert(spatial::Quat::IDENTITY.is_finite());
    
Trait implementations 8
  • impl<T: Real> Mul for Quat<T>
  • impl<T: Real> MulAssign for Quat<T>
  • impl<T: Real> Mul<Vec3<T>> for Quat<T>
  • impl<T: Real> Add for Quat<T>
  • impl<T: Real> Sub for Quat<T>
  • impl<T: Real> Mul<T> for Quat<T>
  • impl<T: Real> Neg for Quat<T>
  • impl<T: Real> Display for Quat<T>