| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
macro_rules! impl_affine2_tests { |
| 5 |
($t:ident, $affine2:ident, $vec2:ident, $mat2:ident, $mat3:ident) => { |
| 6 |
const MATRIX1D: [$t; 6] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; |
| 7 |
const MATRIX2D: [[$t; 2]; 3] = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]; |
| 8 |
|
| 9 |
glam_test!(test_affine2_identity, { |
| 10 |
assert_eq!($affine2::IDENTITY, $affine2::IDENTITY * $affine2::IDENTITY); |
| 11 |
assert_eq!($affine2::IDENTITY, $affine2::default()); |
| 12 |
}); |
| 13 |
|
| 14 |
glam_test!(test_affine2_zero, { |
| 15 |
assert_eq!( |
| 16 |
$affine2::ZERO.transform_point2($vec2::new(1., 2.)), |
| 17 |
$vec2::ZERO |
| 18 |
); |
| 19 |
}); |
| 20 |
|
| 21 |
glam_test!(test_affine2_nan, { |
| 22 |
assert!($affine2::NAN.is_nan()); |
| 23 |
assert!(!$affine2::NAN.is_finite()); |
| 24 |
}); |
| 25 |
|
| 26 |
glam_test!(test_affine2_from_cols, { |
| 27 |
let a = $affine2::from_cols( |
| 28 |
$vec2::from_array(MATRIX2D[0]), |
| 29 |
$vec2::from_array(MATRIX2D[1]), |
| 30 |
$vec2::from_array(MATRIX2D[2]), |
| 31 |
); |
| 32 |
assert_eq!(MATRIX2D, a.to_cols_array_2d()); |
| 33 |
|
| 34 |
let a = $affine2::from_cols_array(&MATRIX1D); |
| 35 |
assert_eq!(MATRIX1D, a.to_cols_array()); |
| 36 |
|
| 37 |
let a = $affine2::from_cols_array_2d(&MATRIX2D); |
| 38 |
assert_eq!(MATRIX2D, a.to_cols_array_2d()); |
| 39 |
}); |
| 40 |
|
| 41 |
glam_test!(test_affine2_deref, { |
| 42 |
let a = $affine2::from_cols_array_2d(&MATRIX2D); |
| 43 |
assert_eq!(MATRIX2D[0], a.x_axis.to_array()); |
| 44 |
assert_eq!(MATRIX2D[1], a.y_axis.to_array()); |
| 45 |
assert_eq!(MATRIX2D[2], a.z_axis.to_array()); |
| 46 |
|
| 47 |
let mut b = a; |
| 48 |
b.x_axis *= 0.0; |
| 49 |
b.y_axis *= 0.0; |
| 50 |
b.z_axis *= 0.0; |
| 51 |
assert_eq!($affine2::ZERO, b); |
| 52 |
}); |
| 53 |
|
| 54 |
glam_test!(test_affine2_from_mat2, { |
| 55 |
let m = $mat2::from_cols_array_2d(&[MATRIX2D[0], MATRIX2D[1]]); |
| 56 |
let a = $affine2::from_mat2(m); |
| 57 |
assert_eq!(m, a.matrix2); |
| 58 |
assert_eq!($vec2::ZERO, a.translation); |
| 59 |
|
| 60 |
let t = $vec2::from_array(MATRIX2D[2]); |
| 61 |
let a = $affine2::from_mat2_translation(m, t); |
| 62 |
assert_eq!(MATRIX2D, a.to_cols_array_2d()); |
| 63 |
}); |
| 64 |
|
| 65 |
glam_test!(test_affine2_from_mat3, { |
| 66 |
let m = $mat3::from_cols_array_2d(&[[1.0, 2.0, 0.0], [3.0, 4.0, 0.0], [5.0, 6.0, 1.0]]); |
| 67 |
let a = $affine2::from_mat3(m); |
| 68 |
assert_eq!(MATRIX2D, a.to_cols_array_2d()); |
| 69 |
|
| 70 |
assert_eq!(m, $mat3::from(a)); |
| 71 |
}); |
| 72 |
|
| 73 |
glam_test!(test_affine2_translation, { |
| 74 |
let translate = $affine2::from_translation($vec2::new(1.0, 2.0)); |
| 75 |
assert_eq!(translate.translation, $vec2::new(1.0, 2.0).into()); |
| 76 |
assert_eq!( |
| 77 |
translate.transform_point2($vec2::new(2.0, 3.0)), |
| 78 |
$vec2::new(3.0, 5.0), |
| 79 |
); |
| 80 |
}); |
| 81 |
|
| 82 |
glam_test!(test_affine2_mul, { |
| 83 |
let m = $affine2::from_angle(deg(90.0)); |
| 84 |
let result3 = m.transform_vector2($vec2::Y); |
| 85 |
assert_approx_eq!($vec2::new(-1.0, 0.0), result3); |
| 86 |
|
| 87 |
let m = $affine2::from_angle_translation(deg(90.0), $vec2::new(1.0, 2.0)); |
| 88 |
let result3 = m.transform_vector2($vec2::Y); |
| 89 |
assert_approx_eq!($vec2::new(-1.0, 0.0), result3, 1.0e-6); |
| 90 |
|
| 91 |
let m = $affine2::from_scale_angle_translation( |
| 92 |
$vec2::new(0.5, 1.5), |
| 93 |
deg(90.0), |
| 94 |
$vec2::new(1.0, 2.0), |
| 95 |
); |
| 96 |
let result3 = m.transform_vector2($vec2::Y); |
| 97 |
assert_approx_eq!($vec2::new(-1.5, 0.0), result3, 1.0e-6); |
| 98 |
|
| 99 |
let result3 = m.transform_point2($vec2::Y); |
| 100 |
assert_approx_eq!($vec2::new(-0.5, 2.0), result3, 1.0e-6); |
| 101 |
}); |
| 102 |
|
| 103 |
glam_test!(test_from_scale, { |
| 104 |
let m = $affine2::from_scale($vec2::new(2.0, 4.0)); |
| 105 |
assert_approx_eq!( |
| 106 |
m.transform_point2($vec2::new(1.0, 1.0)), |
| 107 |
$vec2::new(2.0, 4.0) |
| 108 |
); |
| 109 |
}); |
| 110 |
|
| 111 |
glam_test!(test_affine2_inverse, { |
| 112 |
let inv = $affine2::IDENTITY.inverse(); |
| 113 |
assert_approx_eq!($affine2::IDENTITY, inv); |
| 114 |
|
| 115 |
let rot = $affine2::from_angle(deg(90.0)); |
| 116 |
let rot_inv = rot.inverse(); |
| 117 |
assert_approx_eq!($affine2::IDENTITY, rot * rot_inv); |
| 118 |
assert_approx_eq!($affine2::IDENTITY, rot_inv * rot); |
| 119 |
|
| 120 |
let trans = $affine2::from_translation($vec2::new(1.0, 2.0)); |
| 121 |
let trans_inv = trans.inverse(); |
| 122 |
assert_approx_eq!($affine2::IDENTITY, trans * trans_inv); |
| 123 |
assert_approx_eq!($affine2::IDENTITY, trans_inv * trans); |
| 124 |
|
| 125 |
let scale = $affine2::from_scale($vec2::new(4.0, 5.0)); |
| 126 |
let scale_inv = scale.inverse(); |
| 127 |
assert_approx_eq!($affine2::IDENTITY, scale * scale_inv); |
| 128 |
assert_approx_eq!($affine2::IDENTITY, scale_inv * scale); |
| 129 |
|
| 130 |
let m = scale * rot * trans; |
| 131 |
let m_inv = m.inverse(); |
| 132 |
assert_approx_eq!($affine2::IDENTITY, m * m_inv, 1.0e-5); |
| 133 |
assert_approx_eq!($affine2::IDENTITY, m_inv * m, 1.0e-5); |
| 134 |
assert_approx_eq!(m_inv, trans_inv * rot_inv * scale_inv, 1.0e-6); |
| 135 |
|
| 136 |
// Make sure we can invert a shear matrix: |
| 137 |
let m = $affine2::from_angle(0.5) |
| 138 |
* $affine2::from_scale($vec2::new(1.0, 0.5)) |
| 139 |
* $affine2::from_angle(-0.5); |
| 140 |
let m_inv = m.inverse(); |
| 141 |
assert_approx_eq!($affine2::IDENTITY, m * m_inv, 1.0e-5); |
| 142 |
assert_approx_eq!($affine2::IDENTITY, m_inv * m, 1.0e-5); |
| 143 |
|
| 144 |
should_glam_assert!({ $affine2::ZERO.inverse() }); |
| 145 |
}); |
| 146 |
|
| 147 |
glam_test!(test_affine2_decompose, { |
| 148 |
// identity |
| 149 |
let (out_scale, out_rotation, out_translation) = |
| 150 |
$affine2::IDENTITY.to_scale_angle_translation(); |
| 151 |
assert_approx_eq!($vec2::ONE, out_scale); |
| 152 |
assert_eq!(out_rotation, 0.0); |
| 153 |
assert_approx_eq!($vec2::ZERO, out_translation); |
| 154 |
|
| 155 |
// no scale |
| 156 |
let in_scale = $vec2::ONE; |
| 157 |
let in_translation = $vec2::new(-2.0, 4.0); |
| 158 |
let in_rotation = $t::to_radians(-45.0); |
| 159 |
let in_mat = |
| 160 |
$affine2::from_scale_angle_translation(in_scale, in_rotation, in_translation); |
| 161 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_angle_translation(); |
| 162 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 163 |
assert_approx_eq!(in_rotation, out_rotation); |
| 164 |
assert_approx_eq!(in_translation, out_translation); |
| 165 |
assert_approx_eq!( |
| 166 |
in_mat, |
| 167 |
$affine2::from_scale_angle_translation(out_scale, out_rotation, out_translation), |
| 168 |
1e-6 |
| 169 |
); |
| 170 |
|
| 171 |
// positive scale |
| 172 |
let in_scale = $vec2::new(1.0, 2.0); |
| 173 |
let in_mat = |
| 174 |
$affine2::from_scale_angle_translation(in_scale, in_rotation, in_translation); |
| 175 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_angle_translation(); |
| 176 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 177 |
assert_approx_eq!(in_rotation, out_rotation); |
| 178 |
assert_approx_eq!(in_translation, out_translation); |
| 179 |
assert_approx_eq!( |
| 180 |
in_mat, |
| 181 |
$affine2::from_scale_angle_translation(out_scale, out_rotation, out_translation), |
| 182 |
1e-5 |
| 183 |
); |
| 184 |
|
| 185 |
// negative scale |
| 186 |
let in_scale = $vec2::new(-4.0, 1.0); |
| 187 |
let in_mat = |
| 188 |
$affine2::from_scale_angle_translation(in_scale, in_rotation, in_translation); |
| 189 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_angle_translation(); |
| 190 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 191 |
assert_approx_eq!(in_rotation, out_rotation); |
| 192 |
assert_approx_eq!(in_translation, out_translation); |
| 193 |
assert_approx_eq!( |
| 194 |
in_mat, |
| 195 |
$affine2::from_scale_angle_translation(out_scale, out_rotation, out_translation), |
| 196 |
1e-5 |
| 197 |
); |
| 198 |
|
| 199 |
// negative scale |
| 200 |
let in_scale = $vec2::new(4.0, -1.0); |
| 201 |
let in_mat = |
| 202 |
$affine2::from_scale_angle_translation(in_scale, in_rotation, in_translation); |
| 203 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_angle_translation(); |
| 204 |
// out_scale and out_rotation are different but they produce the same matrix |
| 205 |
// assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 206 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 207 |
assert_approx_eq!(in_translation, out_translation); |
| 208 |
assert_approx_eq!( |
| 209 |
in_mat, |
| 210 |
$affine2::from_scale_angle_translation(out_scale, out_rotation, out_translation), |
| 211 |
1e-6 |
| 212 |
); |
| 213 |
}); |
| 214 |
|
| 215 |
glam_test!(test_affine2_ops, { |
| 216 |
let m0 = $affine2::from_cols_array_2d(&MATRIX2D); |
| 217 |
assert_approx_eq!(m0, m0 * $affine2::IDENTITY); |
| 218 |
assert_approx_eq!(m0, $affine2::IDENTITY * m0); |
| 219 |
|
| 220 |
assert_approx_eq!(m0, &$affine2::IDENTITY * m0); |
| 221 |
assert_approx_eq!(m0, $affine2::IDENTITY * &m0); |
| 222 |
assert_approx_eq!(m0, &$affine2::IDENTITY * &m0); |
| 223 |
|
| 224 |
let mut m1 = m0; |
| 225 |
m1 *= $affine2::IDENTITY; |
| 226 |
assert_approx_eq!(m1, m0); |
| 227 |
|
| 228 |
let mut m1 = m0; |
| 229 |
m1 *= &$affine2::IDENTITY; |
| 230 |
assert_approx_eq!(m1, m0); |
| 231 |
|
| 232 |
let mat3 = $mat3::from(m0); |
| 233 |
assert_approx_eq!(mat3, $affine2::IDENTITY * mat3); |
| 234 |
assert_approx_eq!(mat3, mat3 * $affine2::IDENTITY); |
| 235 |
|
| 236 |
assert_approx_eq!(mat3, &$affine2::IDENTITY * mat3); |
| 237 |
assert_approx_eq!(mat3, &mat3 * $affine2::IDENTITY); |
| 238 |
assert_approx_eq!(mat3, $affine2::IDENTITY * &mat3); |
| 239 |
assert_approx_eq!(mat3, mat3 * &$affine2::IDENTITY); |
| 240 |
assert_approx_eq!(mat3, &$affine2::IDENTITY * &mat3); |
| 241 |
assert_approx_eq!(mat3, &mat3 * &$affine2::IDENTITY); |
| 242 |
|
| 243 |
let mut n = mat3; |
| 244 |
n *= $affine2::IDENTITY; |
| 245 |
assert_approx_eq!(mat3, n); |
| 246 |
|
| 247 |
let mut n = mat3; |
| 248 |
n *= &$affine2::IDENTITY; |
| 249 |
assert_approx_eq!(mat3, n); |
| 250 |
}); |
| 251 |
|
| 252 |
glam_test!(test_affine2_fmt, { |
| 253 |
let a = $affine2::from_cols_array_2d(&MATRIX2D); |
| 254 |
assert_eq!(format!("{}", a), "[[1, 2], [3, 4], [5, 6]]"); |
| 255 |
assert_eq!(format!("{:.1}", a), "[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]"); |
| 256 |
}); |
| 257 |
|
| 258 |
glam_test!(test_affine2_to_from_slice, { |
| 259 |
let m = $affine2::from_cols_slice(&MATRIX1D); |
| 260 |
assert_eq!($affine2::from_cols_array(&MATRIX1D), m); |
| 261 |
assert_eq!(MATRIX1D, m.to_cols_array()); |
| 262 |
assert_eq!(MATRIX2D, m.to_cols_array_2d()); |
| 263 |
let mut out: [$t; 6] = Default::default(); |
| 264 |
m.write_cols_to_slice(&mut out); |
| 265 |
assert_eq!(MATRIX1D, out); |
| 266 |
assert_eq!( |
| 267 |
m, |
| 268 |
$affine2::from_cols(MATRIX2D[0].into(), MATRIX2D[1].into(), MATRIX2D[2].into()) |
| 269 |
); |
| 270 |
|
| 271 |
should_panic!({ $affine2::from_cols_slice(&[0.0; 5]) }); |
| 272 |
should_panic!({ $affine2::IDENTITY.write_cols_to_slice(&mut [0.0; 5]) }); |
| 273 |
}); |
| 274 |
|
| 275 |
glam_test!(test_product, { |
| 276 |
let ident = $affine2::IDENTITY; |
| 277 |
assert_eq!([ident, ident].iter().product::<$affine2>(), ident * ident); |
| 278 |
}); |
| 279 |
|
| 280 |
glam_test!(test_affine2_is_finite, { |
| 281 |
assert!($affine2::from_scale($vec2::new(1.0, 1.0)).is_finite()); |
| 282 |
assert!($affine2::from_scale($vec2::new(0.0, 1.0)).is_finite()); |
| 283 |
assert!(!$affine2::from_scale($vec2::new(1.0, $t::NAN)).is_finite()); |
| 284 |
assert!(!$affine2::from_scale($vec2::new(1.0, $t::NEG_INFINITY)).is_finite()); |
| 285 |
}); |
| 286 |
}; |
| 287 |
} |
| 288 |
|
| 289 |
mod affine2 { |
| 290 |
use super::support::{deg, FloatCompare}; |
| 291 |
use glam::{Affine2, Mat2, Mat3, Vec2}; |
| 292 |
|
| 293 |
impl FloatCompare for Affine2 { |
| 294 |
#[inline] |
| 295 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 296 |
self.abs_diff_eq(*other, max_abs_diff) |
| 297 |
} |
| 298 |
#[inline] |
| 299 |
fn abs_diff(&self, other: &Self) -> Self { |
| 300 |
Self { |
| 301 |
matrix2: self.matrix2.abs_diff(&other.matrix2), |
| 302 |
translation: self.translation.abs_diff(&other.translation), |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
glam_test!(test_align, { |
| 308 |
use std::mem; |
| 309 |
if cfg!(not(feature = "scalar-math")) { |
| 310 |
assert_eq!(32, mem::size_of::<Affine2>()); |
| 311 |
assert_eq!(16, mem::align_of::<Affine2>()); |
| 312 |
} else if cfg!(feature = "cuda") { |
| 313 |
assert_eq!(24, mem::size_of::<Affine2>()); |
| 314 |
assert_eq!(8, mem::align_of::<Affine2>()); |
| 315 |
} else { |
| 316 |
assert_eq!(24, mem::size_of::<Affine2>()); |
| 317 |
assert_eq!(4, mem::align_of::<Affine2>()); |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
glam_test!(test_affine2_from_mat3a, { |
| 322 |
use glam::Mat3A; |
| 323 |
let m = Mat3A::from_cols_array_2d(&[[1.0, 2.0, 0.0], [3.0, 4.0, 0.0], [5.0, 6.0, 1.0]]); |
| 324 |
let a = Affine2::from_mat3a(m); |
| 325 |
assert_eq!(MATRIX2D, a.to_cols_array_2d()); |
| 326 |
|
| 327 |
assert_eq!(m, Mat3A::from(a)); |
| 328 |
}); |
| 329 |
|
| 330 |
glam_test!(test_as, { |
| 331 |
use glam::DAffine2; |
| 332 |
assert_eq!( |
| 333 |
DAffine2::from_cols_array(&[1., 2., 3., 4., 5., 6.]), |
| 334 |
Affine2::from_cols_array(&[1., 2., 3., 4., 5., 6.]).as_daffine2(), |
| 335 |
); |
| 336 |
}); |
| 337 |
|
| 338 |
impl_affine2_tests!(f32, Affine2, Vec2, Mat2, Mat3); |
| 339 |
} |
| 340 |
|
| 341 |
mod daffine2 { |
| 342 |
use super::support::{deg, FloatCompare}; |
| 343 |
use glam::{DAffine2, DMat2, DMat3, DVec2}; |
| 344 |
|
| 345 |
impl FloatCompare for DAffine2 { |
| 346 |
#[inline] |
| 347 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 348 |
self.abs_diff_eq(*other, max_abs_diff as f64) |
| 349 |
} |
| 350 |
#[inline] |
| 351 |
fn abs_diff(&self, other: &Self) -> Self { |
| 352 |
Self { |
| 353 |
matrix2: self.matrix2.abs_diff(&other.matrix2), |
| 354 |
translation: self.translation.abs_diff(&other.translation), |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
#[cfg(not(feature = "cuda"))] |
| 360 |
glam_test!(test_align, { |
| 361 |
use std::mem; |
| 362 |
assert_eq!(48, mem::size_of::<DAffine2>()); |
| 363 |
assert_eq!(mem::align_of::<f64>(), mem::align_of::<DAffine2>()); |
| 364 |
}); |
| 365 |
|
| 366 |
#[cfg(feature = "cuda")] |
| 367 |
glam_test!(test_align, { |
| 368 |
use std::mem; |
| 369 |
assert_eq!(48, mem::size_of::<DAffine2>()); |
| 370 |
assert_eq!(16, mem::align_of::<DAffine2>()); |
| 371 |
}); |
| 372 |
|
| 373 |
glam_test!(test_as, { |
| 374 |
use glam::Affine2; |
| 375 |
assert_eq!( |
| 376 |
Affine2::from_cols_array(&[1., 2., 3., 4., 5., 6.]), |
| 377 |
DAffine2::from_cols_array(&[1., 2., 3., 4., 5., 6.]).as_affine2() |
| 378 |
); |
| 379 |
}); |
| 380 |
|
| 381 |
impl_affine2_tests!(f64, DAffine2, DVec2, DMat2, DMat3); |
| 382 |
} |