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