1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use na::{RealField, Rotation3, Unit, UnitQuaternion, U3};
use crate::aliases::{Qua, TMat3, TMat4, TVec3, TVec4};
pub fn quat_cross_vec<N: RealField>(q: &Qua<N>, v: &TVec3<N>) -> TVec3<N> {
UnitQuaternion::new_unchecked(*q) * v
}
pub fn quat_inv_cross_vec<N: RealField>(v: &TVec3<N>, q: &Qua<N>) -> TVec3<N> {
UnitQuaternion::new_unchecked(*q).inverse() * v
}
pub fn quat_extract_real_component<N: RealField>(q: &Qua<N>) -> N {
q.w
}
pub fn quat_fast_mix<N: RealField>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
Unit::new_unchecked(*x)
.nlerp(&Unit::new_unchecked(*y), a)
.into_inner()
}
pub fn quat_length2<N: RealField>(q: &Qua<N>) -> N {
q.norm_squared()
}
pub fn quat_magnitude2<N: RealField>(q: &Qua<N>) -> N {
q.norm_squared()
}
pub fn quat_identity<N: RealField>() -> Qua<N> {
UnitQuaternion::identity().into_inner()
}
pub fn quat_rotate_vec3<N: RealField>(q: &Qua<N>, v: &TVec3<N>) -> TVec3<N> {
UnitQuaternion::new_unchecked(*q) * v
}
pub fn quat_rotate_vec<N: RealField>(q: &Qua<N>, v: &TVec4<N>) -> TVec4<N> {
let rotated = Unit::new_unchecked(*q) * v.fixed_rows::<U3>(0);
TVec4::new(rotated.x, rotated.y, rotated.z, v.w)
}
pub fn quat_rotation<N: RealField>(orig: &TVec3<N>, dest: &TVec3<N>) -> Qua<N> {
UnitQuaternion::rotation_between(orig, dest)
.unwrap_or_else(UnitQuaternion::identity)
.into_inner()
}
pub fn quat_short_mix<N: RealField>(x: &Qua<N>, y: &Qua<N>, a: N) -> Qua<N> {
Unit::new_normalize(*x)
.slerp(&Unit::new_normalize(*y), a)
.into_inner()
}
pub fn quat_to_mat3<N: RealField>(x: &Qua<N>) -> TMat3<N> {
UnitQuaternion::new_unchecked(*x)
.to_rotation_matrix()
.into_inner()
}
pub fn quat_to_mat4<N: RealField>(x: &Qua<N>) -> TMat4<N> {
UnitQuaternion::new_unchecked(*x).to_homogeneous()
}
pub fn mat3_to_quat<N: RealField>(x: &TMat3<N>) -> Qua<N> {
let r = Rotation3::from_matrix_unchecked(*x);
UnitQuaternion::from_rotation_matrix(&r).into_inner()
}
pub fn to_quat<N: RealField>(x: &TMat4<N>) -> Qua<N> {
let rot = x.fixed_slice::<U3, U3>(0, 0).into_owned();
mat3_to_quat(&rot)
}