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.
use spatial::{Quat, Vec3};
let q = Quat::from_rotation_z(f32::PI / 2.0);
assert((q * Vec3::X).approx_eq(Vec3::Y, 1e-6));
The quaternion of its components, as given: not normalized.
nabiassert(spatial::DQuat::from_xyzw(0.0, 0.0, 0.0, 1.0) == spatial::DQuat::IDENTITY);No rotation.
nabiassert(spatial::Quat::<f32>::identity() == spatial::Quat::IDENTITY);From
[x, y, z, w].nabiassert(spatial::DQuat::from_array([0.0, 0.0, 0.0, 1.0]) == spatial::DQuat::IDENTITY);[x, y, z, w].nabiassert(spatial::Quat::IDENTITY.to_array() == [0.0, 0.0, 0.0, 1.0]);(x, y, z, w)as aVec4.nabiassert(spatial::Quat::IDENTITY.to_vec4() == spatial::Vec4::W);The rotation by
angleradians about the unit vectoraxis, counterclockwise when the axis points at the viewer.nabiuse spatial::{Quat, Vec3}; let q = Quat::from_axis_angle(Vec3::Y, f32::PI); assert((q * Vec3::X).approx_eq(-Vec3::X, 1e-6));The rotation by
angleradians about +x.nabiuse spatial::{Quat, Vec3}; assert((Quat::from_rotation_x(f32::PI / 2.0) * Vec3::Y).approx_eq(Vec3::Z, 1e-6));The rotation by
angleradians about +y.nabiuse spatial::{Quat, Vec3}; assert((Quat::from_rotation_y(f32::PI / 2.0) * Vec3::Z).approx_eq(Vec3::X, 1e-6));The rotation by
angleradians about +z.nabiuse spatial::{Quat, Vec3}; assert((Quat::from_rotation_z(f32::PI / 2.0) * Vec3::X).approx_eq(Vec3::Y, 1e-6));
Sourcepub fn from_euler(§order: EulerOrder, a: T, b: T, c: T) -> Quat<T> The rotation of Euler angles
a,b,cin radians about the axesordernames, in that order: seeEulerOrder.nabiuse 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));
Sourcepub fn to_euler(§self, order: EulerOrder) -> (T, T, T) The Euler angles
(a, b, c)of this rotation inorder, so thatQuat::from_euler(order, a, b, c)gives it back.bis in-pi/2..=pi/2,aandcin-pi..=pi; atb = +-pi/2(gimbal lock)cis 0.nabiuse 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);The rotation part of a rotation matrix (orthonormal columns, no scale).
nabiuse spatial::{Mat3, Quat}; let q = Quat::from_rotation_x(0.7); assert(Quat::from_mat3(Mat3::from_quat(q)).approx_eq(q, 1e-6));The rotation part of a rotation-and-translation matrix.
nabiuse spatial::{Mat4, Quat}; let q = Quat::from_rotation_y(0.3); assert(Quat::from_mat4(Mat4::from_quat(q)).approx_eq(q, 1e-6));The shortest rotation that turns the unit vector
frominto the unit vectorto. Opposite vectors turn by pi about some axis at right angles to them.nabiuse spatial::{Quat, Vec3}; let q = Quat::from_rotation_arc(Vec3::X, Vec3::Z); assert((q * Vec3::X).approx_eq(Vec3::Z, 1e-6));The unit axis and the angle in radians,
0..=pifor a normalized quaternion withw >= 0; no rotation gives(+x, 0).nabiuse 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);(-x, -y, -z, w): the inverse of a unit quaternion.nabilet q = spatial::DQuat::from_rotation_x(0.4); assert((q * q.conjugate()).approx_eq(spatial::DQuat::IDENTITY, 1e-12));The inverse rotation. For a unit quaternion this is
conjugate; any other is divided by its squared length too.nabilet q = spatial::DQuat::from_rotation_y(1.0) * 2.0; assert((q * q.inverse()).approx_eq(spatial::DQuat::IDENTITY, 1e-12));The four-dimensional dot product;
+-1for the same rotation.nabiassert(spatial::Quat::IDENTITY.dot(spatial::Quat::IDENTITY) == 1.0);The squared length.
nabiassert(spatial::Quat::IDENTITY.length_squared() == 1.0);The length; 1 for a rotation.
nabiassert((spatial::Quat::from_rotation_z(2.0).length() - 1.0).abs() < 1e-6);The quaternion scaled to length 1.
nabiassert((spatial::Quat::IDENTITY * 3.0).normalize() == spatial::Quat::IDENTITY);Whether the length is 1, within
1e-4of the squared length.nabiassert(spatial::Quat::from_rotation_x(1.0).is_normalized());The angle in radians between the two rotations,
0..=pi.nabiuse spatial::DQuat; let a = DQuat::from_rotation_z(0.25); assert((a.angle_between(DQuat::IDENTITY) - 0.25).abs() < 1e-12);The vector
vrotated, the same asself * v.nabiuse spatial::{Quat, Vec3}; let q = Quat::from_rotation_x(f32::PI); assert(q.rotate(Vec3::Y).approx_eq(-Vec3::Y, 1e-6));Linear interpolation of the components, then normalized: cheaper than
slerp, constant speed only for small steps. Takes the short way round.nabiuse 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));Spherical interpolation: constant angular speed,
t = 0givesselfandt = 1giveso. Takes the short way round; nearly equal rotations fall back tonlerp.nabiuse 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));Whether every component is within
epsofo's.qand-qare the same rotation but are not approximately equal here; compareangle_betweenfor that.nabiassert(spatial::Quat::IDENTITY.approx_eq(spatial::Quat::from_rotation_x(1e-7), 1e-6));Whether no component is NaN or infinite.
nabiassert(spatial::Quat::IDENTITY.is_finite());