Debian Codegraph @

debian/missing-sources/glam/tests/affine3.rs

raw
1 #[macro_use]
2 mod support;
3
4 macro_rules! impl_affine3_tests {
5 ($t:ident, $affine3:ident, $quat:ident, $vec3:ident, $mat3:ident, $mat4:ident) => {
6 const MATRIX1D: [$t; 12] = [
7 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
8 ];
9 const MATRIX2D: [[$t; 3]; 4] = [
10 [1.0, 2.0, 3.0],
11 [4.0, 5.0, 6.0],
12 [7.0, 8.0, 9.0],
13 [10.0, 11.0, 12.0],
14 ];
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_nan, {
29 assert!($affine3::NAN.is_nan());
30 assert!(!$affine3::NAN.is_finite());
31 });
32
33 glam_test!(test_affine3_from_cols, {
34 let a = $affine3::from_cols(
35 $vec3::from_array(MATRIX2D[0]).into(),
36 $vec3::from_array(MATRIX2D[1]).into(),
37 $vec3::from_array(MATRIX2D[2]).into(),
38 $vec3::from_array(MATRIX2D[3]).into(),
39 );
40 assert_eq!(MATRIX2D, a.to_cols_array_2d());
41
42 let a = $affine3::from_cols_array(&MATRIX1D);
43 assert_eq!(MATRIX1D, a.to_cols_array());
44
45 let a = $affine3::from_cols_array_2d(&MATRIX2D);
46 assert_eq!(MATRIX2D, a.to_cols_array_2d());
47 });
48
49 glam_test!(test_affine3_deref, {
50 let a = $affine3::from_cols_array_2d(&MATRIX2D);
51 assert_eq!(MATRIX2D[0], a.x_axis.to_array());
52 assert_eq!(MATRIX2D[1], a.y_axis.to_array());
53 assert_eq!(MATRIX2D[2], a.z_axis.to_array());
54 assert_eq!(MATRIX2D[3], a.w_axis.to_array());
55
56 let mut b = a;
57 b.x_axis *= 0.0;
58 b.y_axis *= 0.0;
59 b.z_axis *= 0.0;
60 b.w_axis *= 0.0;
61 assert_eq!($affine3::ZERO, b);
62 });
63
64 glam_test!(test_affine3_from_mat3, {
65 let m = $mat3::from_cols_array_2d(&[MATRIX2D[0], MATRIX2D[1], MATRIX2D[2]]);
66 let a = $affine3::from_mat3(m);
67 assert_eq!(m, a.matrix3.into());
68 assert_eq!($vec3::ZERO, a.translation.into());
69
70 let t = $vec3::from_array(MATRIX2D[3]);
71 let a = $affine3::from_mat3_translation(m, t);
72 assert_eq!(MATRIX2D, a.to_cols_array_2d());
73 });
74
75 glam_test!(test_affine2_from_mat4, {
76 let m = $mat4::from_cols_array_2d(&[
77 [1.0, 2.0, 3.0, 0.0],
78 [4.0, 5.0, 6.0, 0.0],
79 [7.0, 8.0, 9.0, 0.0],
80 [10.0, 11.0, 12.0, 1.0],
81 ]);
82 let a = $affine3::from_mat4(m);
83 assert_eq!(MATRIX2D, a.to_cols_array_2d());
84
85 assert_eq!(m, $mat4::from(a));
86 });
87
88 glam_test!(test_affine3_translation, {
89 let translate = $affine3::from_translation($vec3::new(1.0, 2.0, 3.0));
90 assert_eq!(translate.translation, $vec3::new(1.0, 2.0, 3.0).into());
91 assert_eq!(
92 translate.transform_point3($vec3::new(2.0, 3.0, 4.0)),
93 $vec3::new(3.0, 5.0, 7.0),
94 );
95 });
96
97 glam_test!(test_from_rotation, {
98 let eps = 2.0 * f32::EPSILON;
99 let rot_x1 = $affine3::from_rotation_x(deg(180.0));
100 let rot_x2 = $affine3::from_axis_angle($vec3::X, deg(180.0));
101 assert_approx_eq!(rot_x1, rot_x2, eps);
102 let rot_y1 = $affine3::from_rotation_y(deg(180.0));
103 let rot_y2 = $affine3::from_axis_angle($vec3::Y, deg(180.0));
104 assert_approx_eq!(rot_y1, rot_y2, eps);
105 let rot_z1 = $affine3::from_rotation_z(deg(180.0));
106 let rot_z2 = $affine3::from_axis_angle($vec3::Z, deg(180.0));
107 assert_approx_eq!(rot_z1, rot_z2, eps);
108
109 assert_approx_eq!(
110 $affine3::from_rotation_x(deg(180.0)),
111 $affine3::from_quat($quat::from_rotation_x(deg(180.0)))
112 );
113
114 assert_approx_eq!(
115 $quat::from_affine3(&$affine3::from_rotation_x(deg(180.0))),
116 $quat::from_rotation_x(deg(180.0))
117 );
118
119 let m = $affine3::from_rotation_translation(
120 $quat::from_rotation_x(deg(90.0)),
121 $vec3::new(1.0, 2.0, 3.0),
122 );
123 let result3 = m.transform_vector3($vec3::Y);
124 assert_approx_eq!($vec3::new(0.0, 0.0, 1.0), result3, 1.0e-6);
125
126 should_glam_assert!({ $affine3::from_axis_angle($vec3::ZERO, 0.0) });
127 should_glam_assert!({ $affine3::from_quat($quat::IDENTITY * 2.0) });
128 });
129
130 glam_test!(test_affine3_mul, {
131 let m = $affine3::from_axis_angle($vec3::Z, deg(90.0));
132 let result3 = m.transform_vector3($vec3::Y);
133 assert_approx_eq!($vec3::new(-1.0, 0.0, 0.0), result3);
134
135 let m = $affine3::from_scale_rotation_translation(
136 $vec3::new(0.5, 1.5, 2.0),
137 $quat::from_rotation_x(deg(90.0)),
138 $vec3::new(1.0, 2.0, 3.0),
139 );
140 let result3 = m.transform_vector3($vec3::Y);
141 assert_approx_eq!($vec3::new(0.0, 0.0, 1.5), result3, 1.0e-6);
142
143 let result3 = m.transform_point3($vec3::Y);
144 assert_approx_eq!($vec3::new(1.0, 2.0, 4.5), result3, 1.0e-6);
145 });
146
147 glam_test!(test_from_scale, {
148 let m = $affine3::from_scale($vec3::new(2.0, 4.0, 8.0));
149 assert_approx_eq!(
150 m.transform_point3($vec3::new(1.0, 1.0, 1.0)),
151 $vec3::new(2.0, 4.0, 8.0)
152 );
153 });
154
155 glam_test!(test_affine3_inverse, {
156 let inv = $affine3::IDENTITY.inverse();
157 assert_approx_eq!($affine3::IDENTITY, inv);
158
159 let rotz = $affine3::from_rotation_z(deg(90.0));
160 let rotz_inv = rotz.inverse();
161 assert_approx_eq!($affine3::IDENTITY, rotz * rotz_inv);
162 assert_approx_eq!($affine3::IDENTITY, rotz_inv * rotz);
163
164 let trans = $affine3::from_translation($vec3::new(1.0, 2.0, 3.0));
165 let trans_inv = trans.inverse();
166 assert_approx_eq!($affine3::IDENTITY, trans * trans_inv);
167 assert_approx_eq!($affine3::IDENTITY, trans_inv * trans);
168
169 let scale = $affine3::from_scale($vec3::new(4.0, 5.0, 6.0));
170 let scale_inv = scale.inverse();
171 assert_approx_eq!($affine3::IDENTITY, scale * scale_inv);
172 assert_approx_eq!($affine3::IDENTITY, scale_inv * scale);
173
174 let m = scale * rotz * trans;
175 let m_inv = m.inverse();
176 assert_approx_eq!($affine3::IDENTITY, m * m_inv, 1.0e-5);
177 assert_approx_eq!($affine3::IDENTITY, m_inv * m, 1.0e-5);
178 assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6);
179
180 // Make sure we can invert a shear matrix:
181 let m = $affine3::from_axis_angle($vec3::X, 0.5)
182 * $affine3::from_scale($vec3::new(1.0, 0.5, 2.0))
183 * $affine3::from_axis_angle($vec3::X, -0.5);
184 let m_inv = m.inverse();
185 assert_approx_eq!($affine3::IDENTITY, m * m_inv, 1.0e-5);
186 assert_approx_eq!($affine3::IDENTITY, m_inv * m, 1.0e-5);
187
188 should_glam_assert!({ $affine3::ZERO.inverse() });
189 });
190
191 glam_test!(test_affine3_decompose, {
192 // identity
193 let (out_scale, out_rotation, out_translation) =
194 $affine3::IDENTITY.to_scale_rotation_translation();
195 assert_approx_eq!($vec3::ONE, out_scale);
196 assert!(out_rotation.is_near_identity());
197 assert_approx_eq!($vec3::ZERO, out_translation);
198
199 // no scale
200 let in_scale = $vec3::ONE;
201 let in_translation = $vec3::new(-2.0, 4.0, -0.125);
202 let in_rotation = $quat::from_euler(
203 glam::EulerRot::YXZ,
204 $t::to_radians(-45.0),
205 $t::to_radians(180.0),
206 $t::to_radians(270.0),
207 );
208 let in_mat =
209 $affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
210 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
211 assert_approx_eq!(in_scale, out_scale, 1e-6);
212 // out_rotation is different but produces the same matrix
213 // assert_approx_eq!(in_rotation, out_rotation);
214 assert_approx_eq!(in_translation, out_translation);
215 assert_approx_eq!(
216 in_mat,
217 $affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
218 1e-6
219 );
220
221 // positive scale
222 let in_scale = $vec3::new(1.0, 2.0, 4.0);
223 let in_mat =
224 $affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
225 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
226 assert_approx_eq!(in_scale, out_scale, 1e-6);
227 // out_rotation is different but produces the same matrix
228 // assert_approx_eq!(in_rotation, out_rotation);
229 assert_approx_eq!(in_translation, out_translation);
230 assert_approx_eq!(
231 in_mat,
232 $affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
233 1e-5
234 );
235
236 // negative scale
237 let in_scale = $vec3::new(-4.0, 1.0, 2.0);
238 let in_mat =
239 $affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
240 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
241 assert_approx_eq!(in_scale, out_scale, 1e-6);
242 // out_rotation is different but produces the same matrix
243 // assert_approx_eq!(in_rotation, out_rotation);
244 assert_approx_eq!(in_translation, out_translation);
245 assert_approx_eq!(
246 in_mat,
247 $affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
248 1e-5
249 );
250
251 // negative scale
252 let in_scale = $vec3::new(4.0, -1.0, -2.0);
253 let in_mat =
254 $affine3::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
255 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
256 // out_scale and out_rotation are different but they produce the same matrix
257 // assert_approx_eq!(in_scale, out_scale, 1e-6);
258 // assert_approx_eq!(in_rotation, out_rotation);
259 assert_approx_eq!(in_translation, out_translation);
260 assert_approx_eq!(
261 in_mat,
262 $affine3::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
263 1e-6
264 );
265 });
266
267 glam_test!(test_affine3_look_at, {
268 let eye = $vec3::new(0.0, 0.0, -5.0);
269 let center = $vec3::new(0.0, 0.0, 0.0);
270 let up = $vec3::new(1.0, 0.0, 0.0);
271
272 let point = $vec3::new(1.0, 0.0, 0.0);
273
274 let lh = $affine3::look_at_lh(eye, center, up);
275 let rh = $affine3::look_at_rh(eye, center, up);
276 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
277 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
278
279 let dir = center - eye;
280 let lh = $affine3::look_to_lh(eye, dir, up);
281 let rh = $affine3::look_to_rh(eye, dir, up);
282 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
283 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
284
285 should_glam_assert!({ $affine3::look_at_lh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
286 should_glam_assert!({ $affine3::look_at_rh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
287 });
288
289 glam_test!(test_affine3_ops, {
290 let m0 = $affine3::from_cols_array_2d(&MATRIX2D);
291 assert_approx_eq!(m0, m0 * $affine3::IDENTITY);
292 assert_approx_eq!(m0, $affine3::IDENTITY * m0);
293
294 assert_approx_eq!(m0, &$affine3::IDENTITY * m0);
295 assert_approx_eq!(m0, $affine3::IDENTITY * &m0);
296 assert_approx_eq!(m0, &$affine3::IDENTITY * &m0);
297
298 let mut m1 = m0;
299 m1 *= $affine3::IDENTITY;
300 assert_approx_eq!(m1, m0);
301
302 let mut m1 = m0;
303 m1 *= &$affine3::IDENTITY;
304 assert_approx_eq!(m1, m0);
305
306 let mat4 = $mat4::from(m0);
307 assert_approx_eq!(mat4, $affine3::IDENTITY * mat4);
308 assert_approx_eq!(mat4, mat4 * $affine3::IDENTITY);
309
310 assert_approx_eq!(mat4, &$affine3::IDENTITY * mat4);
311 assert_approx_eq!(mat4, &mat4 * $affine3::IDENTITY);
312 assert_approx_eq!(mat4, $affine3::IDENTITY * &mat4);
313 assert_approx_eq!(mat4, mat4 * &$affine3::IDENTITY);
314 assert_approx_eq!(mat4, &$affine3::IDENTITY * &mat4);
315 assert_approx_eq!(mat4, &mat4 * &$affine3::IDENTITY);
316
317 let mut n = mat4;
318 n *= $affine3::IDENTITY;
319 assert_approx_eq!(mat4, n);
320
321 let mut n = mat4;
322 n *= &$affine3::IDENTITY;
323 assert_approx_eq!(mat4, n);
324 });
325
326 glam_test!(test_affine3_fmt, {
327 let a = $affine3::from_cols_array_2d(&MATRIX2D);
328 assert_eq!(
329 format!("{}", a),
330 "[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]"
331 );
332 assert_eq!(
333 format!("{:.1}", a),
334 "[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]"
335 );
336 });
337
338 glam_test!(test_affine3_to_from_slice, {
339 let m = $affine3::from_cols_slice(&MATRIX1D);
340 assert_eq!($affine3::from_cols_array(&MATRIX1D), m);
341 assert_eq!(MATRIX1D, m.to_cols_array());
342 assert_eq!(MATRIX2D, m.to_cols_array_2d());
343 let mut out: [$t; 12] = Default::default();
344 m.write_cols_to_slice(&mut out);
345 assert_eq!(MATRIX1D, out);
346 assert_eq!(
347 m,
348 $affine3::from_cols(
349 MATRIX2D[0].into(),
350 MATRIX2D[1].into(),
351 MATRIX2D[2].into(),
352 MATRIX2D[3].into()
353 )
354 );
355
356 should_panic!({ $affine3::from_cols_slice(&[0.0; 11]) });
357 should_panic!({ $affine3::IDENTITY.write_cols_to_slice(&mut [0.0; 11]) });
358 });
359
360 glam_test!(test_product, {
361 let ident = $affine3::IDENTITY;
362 assert_eq!([ident, ident].iter().product::<$affine3>(), ident * ident);
363 });
364
365 glam_test!(test_affine3_is_finite, {
366 assert!($affine3::from_scale($vec3::new(1.0, 1.0, 1.0)).is_finite());
367 assert!($affine3::from_scale($vec3::new(0.0, 1.0, 1.0)).is_finite());
368 assert!(!$affine3::from_scale($vec3::new(1.0, $t::NAN, 1.0)).is_finite());
369 assert!(!$affine3::from_scale($vec3::new(1.0, 1.0, $t::NEG_INFINITY)).is_finite());
370 });
371 };
372 }
373
374 mod affine3a {
375 use super::support::{deg, FloatCompare};
376 use glam::{Affine3A, Mat3, Mat4, Quat, Vec3, Vec3A};
377
378 impl FloatCompare for Affine3A {
379 #[inline]
380 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
381 self.abs_diff_eq(*other, max_abs_diff)
382 }
383 #[inline]
384 fn abs_diff(&self, other: &Self) -> Self {
385 Self {
386 matrix3: self.matrix3.abs_diff(&other.matrix3),
387 translation: self.translation.abs_diff(&other.translation),
388 }
389 }
390 }
391
392 glam_test!(test_align, {
393 use std::mem;
394 assert_eq!(64, mem::size_of::<Affine3A>());
395 assert_eq!(mem::align_of::<Vec3A>(), mem::align_of::<Affine3A>());
396 });
397
398 glam_test!(test_affine3_mul_vec3a, {
399 let m = Affine3A::from_axis_angle(Vec3::Z, deg(90.0));
400 let result3 = m.transform_vector3a(Vec3A::Y);
401 assert_approx_eq!(Vec3A::new(-1.0, 0.0, 0.0), result3);
402
403 let m = Affine3A::from_scale_rotation_translation(
404 Vec3::new(0.5, 1.5, 2.0),
405 Quat::from_rotation_x(deg(90.0)),
406 Vec3::new(1.0, 2.0, 3.0),
407 );
408 let result3 = m.transform_vector3a(Vec3A::Y);
409 assert_approx_eq!(Vec3A::new(0.0, 0.0, 1.5), result3, 1.0e-6);
410
411 let result3 = m.transform_point3a(Vec3A::Y);
412 assert_approx_eq!(Vec3A::new(1.0, 2.0, 4.5), result3, 1.0e-6);
413 });
414
415 glam_test!(test_as, {
416 use glam::DAffine3;
417 assert_eq!(
418 DAffine3::from_cols_array(&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]),
419 Affine3A::from_cols_array(&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
420 .as_daffine3(),
421 );
422 });
423
424 impl_affine3_tests!(f32, Affine3A, Quat, Vec3, Mat3, Mat4);
425 }
426
427 mod daffine3 {
428 use super::support::{deg, FloatCompare};
429 use glam::{DAffine3, DMat3, DMat4, DQuat, DVec3};
430
431 impl FloatCompare for DAffine3 {
432 #[inline]
433 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
434 self.abs_diff_eq(*other, max_abs_diff as f64)
435 }
436 #[inline]
437 fn abs_diff(&self, other: &Self) -> Self {
438 Self {
439 matrix3: self.matrix3.abs_diff(&other.matrix3),
440 translation: self.translation.abs_diff(&other.translation),
441 }
442 }
443 }
444
445 glam_test!(test_align, {
446 use std::mem;
447 assert_eq!(96, mem::size_of::<DAffine3>());
448 assert_eq!(mem::align_of::<f64>(), mem::align_of::<DAffine3>());
449 });
450
451 glam_test!(test_as, {
452 use glam::Affine3A;
453 assert_eq!(
454 Affine3A::from_cols_array(&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]),
455 DAffine3::from_cols_array(&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
456 .as_affine3a(),
457 );
458 });
459
460 impl_affine3_tests!(f64, DAffine3, DQuat, DVec3, DMat3, DMat4);
461 }