| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
use glam::{DQuat, Quat}; |
| 5 |
|
| 6 |
/// Helper to calculate the inner angle in the range [0, 2*PI) |
| 7 |
trait AngleDiff { |
| 8 |
type Output; |
| 9 |
fn angle_diff(self, other: Self) -> Self::Output; |
| 10 |
} |
| 11 |
#[macro_export] |
| 12 |
macro_rules! impl_angle_diff { |
| 13 |
($t:ty, $pi:expr) => { |
| 14 |
impl AngleDiff for $t { |
| 15 |
type Output = $t; |
| 16 |
fn angle_diff(self, other: $t) -> $t { |
| 17 |
const PI2: $t = $pi + $pi; |
| 18 |
let s = self.rem_euclid(PI2); |
| 19 |
let o = other.rem_euclid(PI2); |
| 20 |
if s > o { |
| 21 |
(s - o).min(PI2 + o - s) |
| 22 |
} else { |
| 23 |
(o - s).min(PI2 + s - o) |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
}; |
| 28 |
} |
| 29 |
impl_angle_diff!(f32, std::f32::consts::PI); |
| 30 |
impl_angle_diff!(f64, std::f64::consts::PI); |
| 31 |
|
| 32 |
#[macro_export] |
| 33 |
macro_rules! assert_approx_angle { |
| 34 |
($a:expr, $b:expr, $eps:expr) => {{ |
| 35 |
let (a, b) = ($a, $b); |
| 36 |
let eps = $eps; |
| 37 |
let diff = a.angle_diff(b); |
| 38 |
assert!( |
| 39 |
diff < $eps, |
| 40 |
"assertion failed: `(left !== right)` \ |
| 41 |
(left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)", |
| 42 |
a, |
| 43 |
b, |
| 44 |
eps, |
| 45 |
diff |
| 46 |
); |
| 47 |
}}; |
| 48 |
} |
| 49 |
|
| 50 |
trait EqApprox { |
| 51 |
type EPS; |
| 52 |
fn eq_approx(self, other: Self, axis_eps: Self::EPS, rot_axis: Self::EPS) -> bool; |
| 53 |
} |
| 54 |
|
| 55 |
macro_rules! impl_eq_approx { |
| 56 |
($t:ty, $quat:ident, $pi:expr) => { |
| 57 |
impl EqApprox for $quat { |
| 58 |
type EPS = $t; |
| 59 |
fn eq_approx(self, other: Self, axis_eps: Self::EPS, rot_axis: Self::EPS) -> bool { |
| 60 |
let (s_axis, s_angle) = self.to_axis_angle(); |
| 61 |
let (o_axis, o_angle) = other.to_axis_angle(); |
| 62 |
if s_angle.abs() < rot_axis && o_angle.abs() < rot_axis { |
| 63 |
// No rotation |
| 64 |
true |
| 65 |
} else { |
| 66 |
let a = s_axis.angle_between(o_axis); |
| 67 |
if a < axis_eps { |
| 68 |
// Same axes |
| 69 |
(s_angle - o_angle).abs() < rot_axis |
| 70 |
} else if ($pi - a).abs() < axis_eps { |
| 71 |
// Inverted axes (180°) |
| 72 |
(s_angle + o_angle).abs() < rot_axis |
| 73 |
} else { |
| 74 |
// Other |
| 75 |
false |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
}; |
| 81 |
} |
| 82 |
impl_eq_approx!(f32, Quat, std::f32::consts::PI); |
| 83 |
impl_eq_approx!(f64, DQuat, std::f64::consts::PI); |
| 84 |
|
| 85 |
#[macro_export] |
| 86 |
macro_rules! impl_3axis_test { |
| 87 |
($name:ident, $t:ty, $quat:ident, $euler:path, $U:path, $V:path, $W:path, $vec:ident) => { |
| 88 |
glam_test!($name, { |
| 89 |
let euler = $euler; |
| 90 |
assert!($U != $W); // First and last axis must be different for three axis |
| 91 |
for u in (-176..=176).step_by(44) { |
| 92 |
for v in (-88..=88).step_by(44) { |
| 93 |
for w in (-176..=176).step_by(44) { |
| 94 |
let u1 = (u as $t).to_radians(); |
| 95 |
let v1 = (v as $t).to_radians(); |
| 96 |
let w1 = (w as $t).to_radians(); |
| 97 |
|
| 98 |
let q1: $quat = ($quat::from_axis_angle($U, u1) |
| 99 |
* $quat::from_axis_angle($V, v1) |
| 100 |
* $quat::from_axis_angle($W, w1)) |
| 101 |
.normalize(); |
| 102 |
|
| 103 |
// Test if the rotation is the expected |
| 104 |
let q2: $quat = $quat::from_euler(euler, u1, v1, w1).normalize(); |
| 105 |
assert_approx_eq!(q1, q2, 1e-5); |
| 106 |
|
| 107 |
// Test angle reconstruction |
| 108 |
let (u2, v2, w2) = q1.to_euler(euler); |
| 109 |
let q3 = $quat::from_euler(euler, u2, v2, w2).normalize(); |
| 110 |
|
| 111 |
assert_approx_angle!(u1, u2, 1e-4 as $t); |
| 112 |
assert_approx_angle!(v1, v2, 1e-4 as $t); |
| 113 |
assert_approx_angle!(w1, w2, 1e-4 as $t); |
| 114 |
|
| 115 |
assert_approx_eq!(q1 * $vec::X, q3 * $vec::X, 1e-4); |
| 116 |
assert_approx_eq!(q1 * $vec::Y, q3 * $vec::Y, 1e-4); |
| 117 |
assert_approx_eq!(q1 * $vec::Z, q3 * $vec::Z, 1e-4); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
}); |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
#[macro_export] |
| 126 |
macro_rules! impl_2axis_test { |
| 127 |
($name:ident, $t:ty, $quat:ident, $euler:path, $U:path, $V:path, $W:path, $vec:ident) => { |
| 128 |
glam_test!($name, { |
| 129 |
#[allow(deprecated)] |
| 130 |
let euler = $euler; |
| 131 |
assert!($U == $W); // First and last axis must be different for three axis |
| 132 |
for u in (-176..=176).step_by(44) { |
| 133 |
for v in (-176..=176).step_by(44) { |
| 134 |
for w in (-176..=176).step_by(44) { |
| 135 |
let u1 = (u as $t).to_radians(); |
| 136 |
let v1 = (v as $t).to_radians(); |
| 137 |
let w1 = (w as $t).to_radians(); |
| 138 |
|
| 139 |
let q1: $quat = ($quat::from_axis_angle($U, u1) |
| 140 |
* $quat::from_axis_angle($V, v1) |
| 141 |
* $quat::from_axis_angle($W, w1)) |
| 142 |
.normalize(); |
| 143 |
|
| 144 |
// Test if the rotation is the expected |
| 145 |
let q2 = $quat::from_euler(euler, u1, v1, w1).normalize(); |
| 146 |
assert_approx_eq!(q1, q2, 1e-5); |
| 147 |
|
| 148 |
// Test angle reconstruction |
| 149 |
let (u2, v2, w2) = q1.to_euler(euler); |
| 150 |
let _q3 = $quat::from_euler(euler, u2, v2, w2).normalize(); |
| 151 |
|
| 152 |
// Disabled tests, since no generic tests for ambiguous results in the two-axis results... |
| 153 |
// assert_approx_angle!(u1, u2, 1e-4 as $t); |
| 154 |
// assert_approx_angle!(v1, v2, 1e-4 as $t); |
| 155 |
// assert_approx_angle!(w1, w2, 1e-4 as $t); |
| 156 |
|
| 157 |
// assert_approx_eq!(q1 * $vec::X, q3 * $vec::X, 1e-4); |
| 158 |
// assert_approx_eq!(q1 * $vec::Y, q3 * $vec::Y, 1e-4); |
| 159 |
// assert_approx_eq!(q1 * $vec::Z, q3 * $vec::Z, 1e-4); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
}); |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
macro_rules! impl_all_quat_tests_three_axis { |
| 168 |
($t:ty, $q:ident, $v:ident) => { |
| 169 |
impl_3axis_test!(test_euler_zyx, $t, $q, ER::ZYX, $v::Z, $v::Y, $v::X, $v); |
| 170 |
impl_3axis_test!(test_euler_zxy, $t, $q, ER::ZXY, $v::Z, $v::X, $v::Y, $v); |
| 171 |
impl_3axis_test!(test_euler_yxz, $t, $q, ER::YXZ, $v::Y, $v::X, $v::Z, $v); |
| 172 |
impl_3axis_test!(test_euler_yzx, $t, $q, ER::YZX, $v::Y, $v::Z, $v::X, $v); |
| 173 |
impl_3axis_test!(test_euler_xyz, $t, $q, ER::XYZ, $v::X, $v::Y, $v::Z, $v); |
| 174 |
impl_3axis_test!(test_euler_xzy, $t, $q, ER::XZY, $v::X, $v::Z, $v::Y, $v); |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
macro_rules! impl_all_quat_tests_two_axis { |
| 179 |
($t:ty, $q:ident, $v:ident) => { |
| 180 |
impl_2axis_test!(test_euler_zyz, $t, $q, ER::ZYZ, $v::Z, $v::Y, $v::Z, $v); |
| 181 |
impl_2axis_test!(test_euler_zxz, $t, $q, ER::ZXZ, $v::Z, $v::X, $v::Z, $v); |
| 182 |
impl_2axis_test!(test_euler_yxy, $t, $q, ER::YXY, $v::Y, $v::X, $v::Y, $v); |
| 183 |
impl_2axis_test!(test_euler_yzy, $t, $q, ER::YZY, $v::Y, $v::Z, $v::Y, $v); |
| 184 |
impl_2axis_test!(test_euler_xyx, $t, $q, ER::XYX, $v::X, $v::Y, $v::X, $v); |
| 185 |
impl_2axis_test!(test_euler_xzx, $t, $q, ER::XZX, $v::X, $v::Z, $v::X, $v); |
| 186 |
}; |
| 187 |
} |
| 188 |
|
| 189 |
mod euler { |
| 190 |
use super::AngleDiff; |
| 191 |
use glam::*; |
| 192 |
type ER = EulerRot; |
| 193 |
|
| 194 |
mod quat { |
| 195 |
use super::*; |
| 196 |
|
| 197 |
impl_all_quat_tests_three_axis!(f32, Quat, Vec3); |
| 198 |
|
| 199 |
impl_all_quat_tests_two_axis!(f32, Quat, Vec3); |
| 200 |
} |
| 201 |
|
| 202 |
mod dquat { |
| 203 |
use super::*; |
| 204 |
|
| 205 |
impl_all_quat_tests_three_axis!(f64, DQuat, DVec3); |
| 206 |
|
| 207 |
impl_all_quat_tests_two_axis!(f64, DQuat, DVec3); |
| 208 |
} |
| 209 |
} |