| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
macro_rules! impl_affine3_tests { |
| 5 |
($t:ident, $affine3:ident, $quat:ident, $vec3:ident) => { |
| 6 |
const MATRIX2D: [[$t; 3]; 4] = [ |
| 7 |
[1.0, 2.0, 3.0], |
| 8 |
[4.0, 5.0, 6.0], |
| 9 |
[7.0, 8.0, 9.0], |
| 10 |
[10.0, 11.0, 12.0], |
| 11 |
]; |
| 12 |
|
| 13 |
use core::$t::NAN; |
| 14 |
use core::$t::NEG_INFINITY; |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_affine3_identity() { |
| 18 |
assert_eq!($affine3::IDENTITY, $affine3::IDENTITY * $affine3::IDENTITY); |
| 19 |
assert_eq!($affine3::IDENTITY, $affine3::default()); |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn test_affine3_zero() { |
| 24 |
assert_eq!( |
| 25 |
$affine3::ZERO.transform_point3($vec3::new(1., 2., 3.)), |
| 26 |
$vec3::ZERO |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_affine3_translation() { |
| 32 |
let translate = $affine3::from_translation($vec3::new(1.0, 2.0, 3.0)); |
| 33 |
assert_eq!(translate.translation, $vec3::new(1.0, 2.0, 3.0).into()); |
| 34 |
assert_eq!( |
| 35 |
translate.transform_point3($vec3::new(2.0, 3.0, 4.0)), |
| 36 |
$vec3::new(3.0, 5.0, 7.0), |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_from_rotation() { |
| 42 |
let eps = 2.0 * core::f32::EPSILON; |
| 43 |
let rot_x1 = $affine3::from_rotation_x(deg(180.0)); |
| 44 |
let rot_x2 = $affine3::from_axis_angle($vec3::X, deg(180.0)); |
| 45 |
assert_approx_eq!(rot_x1, rot_x2, eps); |
| 46 |
let rot_y1 = $affine3::from_rotation_y(deg(180.0)); |
| 47 |
let rot_y2 = $affine3::from_axis_angle($vec3::Y, deg(180.0)); |
| 48 |
assert_approx_eq!(rot_y1, rot_y2, eps); |
| 49 |
let rot_z1 = $affine3::from_rotation_z(deg(180.0)); |
| 50 |
let rot_z2 = $affine3::from_axis_angle($vec3::Z, deg(180.0)); |
| 51 |
assert_approx_eq!(rot_z1, rot_z2, eps); |
| 52 |
|
| 53 |
should_glam_assert!({ $affine3::from_axis_angle($vec3::ZERO, 0.0) }); |
| 54 |
should_glam_assert!({ $affine3::from_quat($quat::IDENTITY * 2.0) }); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_affine3_mul() { |
| 59 |
let m = $affine3::from_axis_angle($vec3::Z, deg(90.0)); |
| 60 |
let result3 = m.transform_vector3($vec3::Y); |
| 61 |
assert_approx_eq!($vec3::new(-1.0, 0.0, 0.0), result3); |
| 62 |
|
| 63 |
let m = $affine3::from_scale_rotation_translation( |
| 64 |
$vec3::new(0.5, 1.5, 2.0), |
| 65 |
$quat::from_rotation_x(deg(90.0)), |
| 66 |
$vec3::new(1.0, 2.0, 3.0), |
| 67 |
); |
| 68 |
let result3 = m.transform_vector3($vec3::Y); |
| 69 |
assert_approx_eq!($vec3::new(0.0, 0.0, 1.5), result3, 1.0e-6); |
| 70 |
|
| 71 |
let result3 = m.transform_point3($vec3::Y); |
| 72 |
assert_approx_eq!($vec3::new(1.0, 2.0, 4.5), result3, 1.0e-6); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_from_scale() { |
| 77 |
let m = $affine3::from_scale($vec3::new(2.0, 4.0, 8.0)); |
| 78 |
assert_approx_eq!( |
| 79 |
m.transform_point3($vec3::new(1.0, 1.0, 1.0)), |
| 80 |
$vec3::new(2.0, 4.0, 8.0) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_affine3_inverse() { |
| 86 |
let inv = $affine3::IDENTITY.inverse(); |
| 87 |
assert_approx_eq!($affine3::IDENTITY, inv); |
| 88 |
|
| 89 |
let rotz = $affine3::from_rotation_z(deg(90.0)); |
| 90 |
let rotz_inv = rotz.inverse(); |
| 91 |
assert_approx_eq!($affine3::IDENTITY, rotz * rotz_inv); |
| 92 |
assert_approx_eq!($affine3::IDENTITY, rotz_inv * rotz); |
| 93 |
|
| 94 |
let trans = $affine3::from_translation($vec3::new(1.0, 2.0, 3.0)); |
| 95 |
let trans_inv = trans.inverse(); |
| 96 |
assert_approx_eq!($affine3::IDENTITY, trans * trans_inv); |
| 97 |
assert_approx_eq!($affine3::IDENTITY, trans_inv * trans); |
| 98 |
|
| 99 |
let scale = $affine3::from_scale($vec3::new(4.0, 5.0, 6.0)); |
| 100 |
let scale_inv = scale.inverse(); |
| 101 |
assert_approx_eq!($affine3::IDENTITY, scale * scale_inv); |
| 102 |
assert_approx_eq!($affine3::IDENTITY, scale_inv * scale); |
| 103 |
|
| 104 |
let m = scale * rotz * trans; |
| 105 |
let m_inv = m.inverse(); |
| 106 |
assert_approx_eq!($affine3::IDENTITY, m * m_inv, 1.0e-5); |
| 107 |
assert_approx_eq!($affine3::IDENTITY, m_inv * m, 1.0e-5); |
| 108 |
assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6); |
| 109 |
|
| 110 |
// Make sure we can invert a shear matrix: |
| 111 |
let m = $affine3::from_axis_angle($vec3::X, 0.5) |
| 112 |
* $affine3::from_scale($vec3::new(1.0, 0.5, 2.0)) |
| 113 |
* $affine3::from_axis_angle($vec3::X, -0.5); |
| 114 |
let m_inv = m.inverse(); |
| 115 |
assert_approx_eq!($affine3::IDENTITY, m * m_inv, 1.0e-5); |
| 116 |
assert_approx_eq!($affine3::IDENTITY, m_inv * m, 1.0e-5); |
| 117 |
|
| 118 |
should_glam_assert!({ $affine3::ZERO.inverse() }); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_affine3_decompose() { |
| 123 |
// identity |
| 124 |
let (out_scale, out_rotation, out_translation) = |
| 125 |
$affine3::IDENTITY.to_scale_rotation_translation(); |
| 126 |
assert_approx_eq!($vec3::ONE, out_scale); |
| 127 |
assert!(out_rotation.is_near_identity()); |
| 128 |
assert_approx_eq!($vec3::ZERO, out_translation); |
| 129 |
|
| 130 |
// no scale |
| 131 |
let in_scale = $vec3::ONE; |
| 132 |
let in_translation = $vec3::new(-2.0, 4.0, -0.125); |
| 133 |
let in_rotation = $quat::from_euler( |
| 134 |
glam::EulerRot::YXZ, |
| 135 |
$t::to_radians(-45.0), |
| 136 |
$t::to_radians(180.0), |
| 137 |
$t::to_radians(270.0), |
| 138 |
); |
| 139 |
let in_mat = |
| 140 |
$affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 141 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 142 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 143 |
// out_rotation is different but produces the same matrix |
| 144 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 145 |
assert_approx_eq!(in_translation, out_translation); |
| 146 |
assert_approx_eq!( |
| 147 |
in_mat, |
| 148 |
$affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 149 |
1e-6 |
| 150 |
); |
| 151 |
|
| 152 |
// positive scale |
| 153 |
let in_scale = $vec3::new(1.0, 2.0, 4.0); |
| 154 |
let in_mat = |
| 155 |
$affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 156 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 157 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 158 |
// out_rotation is different but produces the same matrix |
| 159 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 160 |
assert_approx_eq!(in_translation, out_translation); |
| 161 |
assert_approx_eq!( |
| 162 |
in_mat, |
| 163 |
$affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 164 |
1e-6 |
| 165 |
); |
| 166 |
|
| 167 |
// negative scale |
| 168 |
let in_scale = $vec3::new(-4.0, 1.0, 2.0); |
| 169 |
let in_mat = |
| 170 |
$affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 171 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 172 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 173 |
// out_rotation is different but produces the same matrix |
| 174 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 175 |
assert_approx_eq!(in_translation, out_translation); |
| 176 |
assert_approx_eq!( |
| 177 |
in_mat, |
| 178 |
$affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 179 |
1e-5 |
| 180 |
); |
| 181 |
|
| 182 |
// negative scale |
| 183 |
let in_scale = $vec3::new(4.0, -1.0, -2.0); |
| 184 |
let in_mat = |
| 185 |
$affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 186 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 187 |
// out_scale and out_rotation are different but they produce the same matrix |
| 188 |
// assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 189 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 190 |
assert_approx_eq!(in_translation, out_translation); |
| 191 |
assert_approx_eq!( |
| 192 |
in_mat, |
| 193 |
$affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 194 |
1e-6 |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn test_affine3_look_at() { |
| 200 |
let eye = $vec3::new(0.0, 0.0, -5.0); |
| 201 |
let center = $vec3::new(0.0, 0.0, 0.0); |
| 202 |
let up = $vec3::new(1.0, 0.0, 0.0); |
| 203 |
let lh = $affine3::look_at_lh(eye, center, up); |
| 204 |
let rh = $affine3::look_at_rh(eye, center, up); |
| 205 |
let point = $vec3::new(1.0, 0.0, 0.0); |
| 206 |
assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0)); |
| 207 |
assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0)); |
| 208 |
|
| 209 |
should_glam_assert!({ $affine3::look_at_lh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) }); |
| 210 |
should_glam_assert!({ $affine3::look_at_rh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) }); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn test_affine3_ops() { |
| 215 |
let m0 = $affine3::from_cols_array_2d(&MATRIX2D); |
| 216 |
let m0x2 = $affine3::from_cols_array_2d(&[ |
| 217 |
[2.0, 4.0, 6.0], |
| 218 |
[8.0, 10.0, 12.0], |
| 219 |
[14.0, 16.0, 18.0], |
| 220 |
[20.0, 22.0, 24.0], |
| 221 |
]); |
| 222 |
assert_eq!(m0x2, m0 * 2.0); |
| 223 |
assert_eq!(m0x2, 2.0 * m0); |
| 224 |
assert_eq!(m0x2, m0 + m0); |
| 225 |
assert_eq!($affine3::ZERO, m0 - m0); |
| 226 |
assert_approx_eq!(m0, m0 * $affine3::IDENTITY); |
| 227 |
assert_approx_eq!(m0, $affine3::IDENTITY * m0); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn test_affine3_fmt() { |
| 232 |
let a = $affine3::from_cols_array_2d(&MATRIX2D); |
| 233 |
assert_eq!( |
| 234 |
format!("{}", a), |
| 235 |
"[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]" |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn test_affine3_to_from_slice() { |
| 241 |
const MATRIX1D: [$t; 12] = [ |
| 242 |
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, |
| 243 |
]; |
| 244 |
let m = $affine3::from_cols_slice(&MATRIX1D); |
| 245 |
assert_eq!($affine3::from_cols_array(&MATRIX1D), m); |
| 246 |
assert_eq!(MATRIX1D, m.to_cols_array()); |
| 247 |
assert_eq!(MATRIX2D, m.to_cols_array_2d()); |
| 248 |
let mut out: [$t; 12] = Default::default(); |
| 249 |
m.write_cols_to_slice(&mut out); |
| 250 |
assert_eq!(MATRIX1D, out); |
| 251 |
assert_eq!( |
| 252 |
m, |
| 253 |
$affine3::from_cols( |
| 254 |
MATRIX2D[0].into(), |
| 255 |
MATRIX2D[1].into(), |
| 256 |
MATRIX2D[2].into(), |
| 257 |
MATRIX2D[3].into() |
| 258 |
) |
| 259 |
); |
| 260 |
|
| 261 |
should_panic!({ $affine3::from_cols_slice(&[0.0; 11]) }); |
| 262 |
should_panic!({ $affine3::IDENTITY.write_cols_to_slice(&mut [0.0; 11]) }); |
| 263 |
} |
| 264 |
|
| 265 |
#[cfg(feature = "std")] |
| 266 |
#[test] |
| 267 |
fn test_product() { |
| 268 |
let ident = $affine3::IDENTITY; |
| 269 |
assert_eq!( |
| 270 |
vec![ident, ident].iter().product::<$affine3>(), |
| 271 |
ident * ident |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn test_affine3_is_finite() { |
| 277 |
assert!($affine3::from_scale($vec3::new(1.0, 1.0, 1.0)).is_finite()); |
| 278 |
assert!($affine3::from_scale($vec3::new(0.0, 1.0, 1.0)).is_finite()); |
| 279 |
assert!(!$affine3::from_scale($vec3::new(1.0, NAN, 1.0)).is_finite()); |
| 280 |
assert!(!$affine3::from_scale($vec3::new(1.0, 1.0, NEG_INFINITY)).is_finite()); |
| 281 |
} |
| 282 |
}; |
| 283 |
} |
| 284 |
|
| 285 |
mod affine3a { |
| 286 |
use super::support::{deg, FloatCompare}; |
| 287 |
use glam::{Affine3A, Quat, Vec3, Vec3A}; |
| 288 |
|
| 289 |
impl FloatCompare for Affine3A { |
| 290 |
#[inline] |
| 291 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 292 |
self.abs_diff_eq(*other, max_abs_diff) |
| 293 |
} |
| 294 |
#[inline] |
| 295 |
fn abs_diff(&self, other: &Self) -> Self { |
| 296 |
Self { |
| 297 |
matrix3: self.matrix3.abs_diff(&other.matrix3), |
| 298 |
translation: self.translation.abs_diff(&other.translation), |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
#[test] |
| 304 |
fn test_align() { |
| 305 |
use std::mem; |
| 306 |
assert_eq!(64, mem::size_of::<Affine3A>()); |
| 307 |
assert_eq!(16, mem::align_of::<Affine3A>()); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn test_affine3_mul_vec3a() { |
| 312 |
let m = Affine3A::from_axis_angle(Vec3::Z, deg(90.0)); |
| 313 |
let result3 = m.transform_vector3a(Vec3A::Y); |
| 314 |
assert_approx_eq!(Vec3A::new(-1.0, 0.0, 0.0), result3); |
| 315 |
|
| 316 |
let m = Affine3A::from_scale_rotation_translation( |
| 317 |
Vec3::new(0.5, 1.5, 2.0), |
| 318 |
Quat::from_rotation_x(deg(90.0)), |
| 319 |
Vec3::new(1.0, 2.0, 3.0), |
| 320 |
); |
| 321 |
let result3 = m.transform_vector3a(Vec3A::Y); |
| 322 |
assert_approx_eq!(Vec3A::new(0.0, 0.0, 1.5), result3, 1.0e-6); |
| 323 |
|
| 324 |
let result3 = m.transform_point3a(Vec3A::Y); |
| 325 |
assert_approx_eq!(Vec3A::new(1.0, 2.0, 4.5), result3, 1.0e-6); |
| 326 |
} |
| 327 |
|
| 328 |
impl_affine3_tests!(f32, Affine3A, Quat, Vec3); |
| 329 |
} |
| 330 |
|
| 331 |
mod daffine3 { |
| 332 |
use super::support::{deg, FloatCompare}; |
| 333 |
use glam::{DAffine3, DQuat, DVec3}; |
| 334 |
|
| 335 |
impl FloatCompare for DAffine3 { |
| 336 |
#[inline] |
| 337 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 338 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 339 |
} |
| 340 |
#[inline] |
| 341 |
fn abs_diff(&self, other: &Self) -> Self { |
| 342 |
Self { |
| 343 |
matrix3: self.matrix3.abs_diff(&other.matrix3), |
| 344 |
translation: self.translation.abs_diff(&other.translation), |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn test_align() { |
| 351 |
use std::mem; |
| 352 |
assert_eq!(96, mem::size_of::<DAffine3>()); |
| 353 |
assert_eq!(mem::align_of::<f64>(), mem::align_of::<DAffine3>()); |
| 354 |
} |
| 355 |
|
| 356 |
impl_affine3_tests!(f64, DAffine3, DQuat, DVec3); |
| 357 |
} |