nalgebra_glm/gtx/
quaternion.rs

1use na::{Rotation3, Unit, UnitQuaternion};
2
3use crate::aliases::{Qua, TMat3, TMat4, TVec3, TVec4};
4use crate::RealNumber;
5
6/// Rotate the vector `v` by the quaternion `q` assumed to be normalized.
7pub fn quat_cross_vec<T: RealNumber>(q: &Qua<T>, v: &TVec3<T>) -> TVec3<T> {
8    UnitQuaternion::new_unchecked(*q) * v
9}
10
11/// Rotate the vector `v` by the inverse of the quaternion `q` assumed to be normalized.
12pub fn quat_inv_cross_vec<T: RealNumber>(v: &TVec3<T>, q: &Qua<T>) -> TVec3<T> {
13    UnitQuaternion::new_unchecked(*q).inverse() * v
14}
15
16/// The quaternion `w` component.
17pub fn quat_extract_real_component<T: RealNumber>(q: &Qua<T>) -> T {
18    q.w
19}
20
21/// Normalized linear interpolation between two quaternions.
22pub fn quat_fast_mix<T: RealNumber>(x: &Qua<T>, y: &Qua<T>, a: T) -> Qua<T> {
23    Unit::new_unchecked(*x)
24        .nlerp(&Unit::new_unchecked(*y), a)
25        .into_inner()
26}
27
28//pub fn quat_intermediate<T: RealNumber>(prev: &Qua<T>, curr: &Qua<T>, next: &Qua<T>) -> Qua<T> {
29//    unimplemented!()
30//}
31
32/// The squared magnitude of a quaternion `q`.
33pub fn quat_length2<T: RealNumber>(q: &Qua<T>) -> T {
34    q.norm_squared()
35}
36
37/// The squared magnitude of a quaternion `q`.
38pub fn quat_magnitude2<T: RealNumber>(q: &Qua<T>) -> T {
39    q.norm_squared()
40}
41
42/// The quaternion representing the identity rotation.
43pub fn quat_identity<T: RealNumber>() -> Qua<T> {
44    UnitQuaternion::identity().into_inner()
45}
46
47/// Rotates a vector by a quaternion assumed to be normalized.
48pub fn quat_rotate_vec3<T: RealNumber>(q: &Qua<T>, v: &TVec3<T>) -> TVec3<T> {
49    UnitQuaternion::new_unchecked(*q) * v
50}
51
52/// Rotates a vector in homogeneous coordinates by a quaternion assumed to be normalized.
53pub fn quat_rotate_vec<T: RealNumber>(q: &Qua<T>, v: &TVec4<T>) -> TVec4<T> {
54    let rotated = Unit::new_unchecked(*q) * v.fixed_rows::<3>(0);
55    TVec4::new(rotated.x, rotated.y, rotated.z, v.w)
56}
57
58/// The rotation required to align `orig` to `dest`.
59pub fn quat_rotation<T: RealNumber>(orig: &TVec3<T>, dest: &TVec3<T>) -> Qua<T> {
60    UnitQuaternion::rotation_between(orig, dest)
61        .unwrap_or_else(UnitQuaternion::identity)
62        .into_inner()
63}
64
65/// The spherical linear interpolation between two quaternions.
66pub fn quat_short_mix<T: RealNumber>(x: &Qua<T>, y: &Qua<T>, a: T) -> Qua<T> {
67    Unit::new_normalize(*x)
68        .slerp(&Unit::new_normalize(*y), a)
69        .into_inner()
70}
71
72//pub fn quat_squad<T: RealNumber>(q1: &Qua<T>, q2: &Qua<T>, s1: &Qua<T>, s2: &Qua<T>, h: T) -> Qua<T> {
73//    unimplemented!()
74//}
75
76/// Converts a quaternion to a rotation matrix.
77pub fn quat_to_mat3<T: RealNumber>(x: &Qua<T>) -> TMat3<T> {
78    UnitQuaternion::new_unchecked(*x)
79        .to_rotation_matrix()
80        .into_inner()
81}
82
83/// Converts a quaternion to a rotation matrix in homogeneous coordinates.
84pub fn quat_to_mat4<T: RealNumber>(x: &Qua<T>) -> TMat4<T> {
85    UnitQuaternion::new_unchecked(*x).to_homogeneous()
86}
87
88/// Converts a rotation matrix to a quaternion.
89pub fn mat3_to_quat<T: RealNumber>(x: &TMat3<T>) -> Qua<T> {
90    let r = Rotation3::from_matrix_unchecked(*x);
91    UnitQuaternion::from_rotation_matrix(&r).into_inner()
92}
93
94/// Converts a rotation matrix in homogeneous coordinates to a quaternion.
95pub fn to_quat<T: RealNumber>(x: &TMat4<T>) -> Qua<T> {
96    let rot = x.fixed_view::<3, 3>(0, 0).into_owned();
97    mat3_to_quat(&rot)
98}