| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
/// Helper to get the 'canonical' version of a `Quat`. We define the canonical of quat `q` as: |
| 5 |
/// * `q`, if q.w > epsilon |
| 6 |
/// * `-q`, if q.w < -epsilon |
| 7 |
/// * `(0, 0, 0, 1)` otherwise |
| 8 |
/// The rationale is that q and -q represent the same rotation, and any (_, _, _, 0) represent no rotation at all. |
| 9 |
trait CanonicalQuat: Copy { |
| 10 |
fn canonical(self) -> Self; |
| 11 |
} |
| 12 |
|
| 13 |
macro_rules! impl_canonical_quat { |
| 14 |
($t:ty) => { |
| 15 |
impl CanonicalQuat for $t { |
| 16 |
fn canonical(self) -> Self { |
| 17 |
match self { |
| 18 |
_ if self.w >= 1e-5 => self, |
| 19 |
_ if self.w <= -1e-5 => -self, |
| 20 |
_ => <$t>::from_xyzw(0.0, 0.0, 0.0, 1.0), |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
}; |
| 25 |
} |
| 26 |
|
| 27 |
impl_canonical_quat!(glam::Quat); |
| 28 |
impl_canonical_quat!(glam::DQuat); |
| 29 |
|
| 30 |
/// Helper to set some alternative epsilons based on the floating point type used |
| 31 |
trait EulerEpsilon { |
| 32 |
/// epsilon for comparing quaterions built from eulers and axis-angles |
| 33 |
const Q_EPS: f32; |
| 34 |
|
| 35 |
/// epsilon for comparing quaternion round-tripped through eulers (quat -> euler -> quat) |
| 36 |
const E_EPS: f32; |
| 37 |
} |
| 38 |
impl EulerEpsilon for f32 { |
| 39 |
const Q_EPS: f32 = 1e-5; |
| 40 |
// TODO: sse2 impl passes with 1e-5 but scalar-math does not, why? |
| 41 |
const E_EPS: f32 = 2e-4; |
| 42 |
} |
| 43 |
|
| 44 |
impl EulerEpsilon for f64 { |
| 45 |
const Q_EPS: f32 = 1e-8; |
| 46 |
const E_EPS: f32 = 1e-8; |
| 47 |
} |
| 48 |
|
| 49 |
macro_rules! impl_3axis_test { |
| 50 |
($name:ident, $t:ty, $quat:ident, $euler:path, $U:path, $V:path, $W:path, $vec:ident) => { |
| 51 |
glam_test!($name, { |
| 52 |
let euler = $euler; |
| 53 |
assert!($U != $W); // First and last axis must be different for three axis |
| 54 |
for u in (-180..=180).step_by(15) { |
| 55 |
for v in (-180..=180).step_by(15) { |
| 56 |
for w in (-180..=180).step_by(15) { |
| 57 |
let u1 = (u as $t).to_radians(); |
| 58 |
let v1 = (v as $t).to_radians(); |
| 59 |
let w1 = (w as $t).to_radians(); |
| 60 |
|
| 61 |
let q1: $quat = ($quat::from_axis_angle($U, u1) |
| 62 |
* $quat::from_axis_angle($V, v1) |
| 63 |
* $quat::from_axis_angle($W, w1)) |
| 64 |
.normalize(); |
| 65 |
|
| 66 |
// Test if the rotation is the expected |
| 67 |
let q2: $quat = $quat::from_euler(euler, u1, v1, w1).normalize(); |
| 68 |
assert_approx_eq!(q1.canonical(), q2.canonical(), <$t>::Q_EPS); |
| 69 |
|
| 70 |
// Test quat reconstruction from angles |
| 71 |
let (u2, v2, w2) = q2.to_euler(euler); |
| 72 |
let q3 = $quat::from_euler(euler, u2, v2, w2).normalize(); |
| 73 |
assert_approx_eq!( |
| 74 |
q2.canonical(), |
| 75 |
q3.canonical(), |
| 76 |
<$t>::E_EPS, |
| 77 |
format!( |
| 78 |
"angles {:?} -> {:?}", |
| 79 |
(u, v, w), |
| 80 |
(u2.to_degrees(), v2.to_degrees(), w2.to_degrees()) |
| 81 |
) |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
}); |
| 87 |
}; |
| 88 |
} |
| 89 |
|
| 90 |
macro_rules! impl_all_quat_tests_three_axis { |
| 91 |
($t:ty, $q:ident, $v:ident) => { |
| 92 |
impl_3axis_test!(test_euler_zyx, $t, $q, ER::ZYX, $v::Z, $v::Y, $v::X, $v); |
| 93 |
impl_3axis_test!(test_euler_zxy, $t, $q, ER::ZXY, $v::Z, $v::X, $v::Y, $v); |
| 94 |
impl_3axis_test!(test_euler_yxz, $t, $q, ER::YXZ, $v::Y, $v::X, $v::Z, $v); |
| 95 |
impl_3axis_test!(test_euler_yzx, $t, $q, ER::YZX, $v::Y, $v::Z, $v::X, $v); |
| 96 |
impl_3axis_test!(test_euler_xyz, $t, $q, ER::XYZ, $v::X, $v::Y, $v::Z, $v); |
| 97 |
impl_3axis_test!(test_euler_xzy, $t, $q, ER::XZY, $v::X, $v::Z, $v::Y, $v); |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
mod euler { |
| 102 |
use super::{CanonicalQuat, EulerEpsilon}; |
| 103 |
use glam::*; |
| 104 |
type ER = EulerRot; |
| 105 |
|
| 106 |
mod quat { |
| 107 |
use super::*; |
| 108 |
|
| 109 |
impl_all_quat_tests_three_axis!(f32, Quat, Vec3); |
| 110 |
} |
| 111 |
|
| 112 |
mod dquat { |
| 113 |
use super::*; |
| 114 |
|
| 115 |
impl_all_quat_tests_three_axis!(f64, DQuat, DVec3); |
| 116 |
} |
| 117 |
} |