Debian Codegraph @

debian/missing-sources/glam-0.28.0/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 let mut m1 = m0;
295 m1 *= $affine3::IDENTITY;
296 assert_approx_eq!(m1, m0);
297
298 let mat4 = $mat4::from(m0);
299 assert_approx_eq!(mat4, $affine3::IDENTITY * mat4);
300 assert_approx_eq!(mat4, mat4 * $affine3::IDENTITY);
301 });
302
303 glam_test!(test_affine3_fmt, {
304 let a = $affine3::from_cols_array_2d(&MATRIX2D);
305 assert_eq!(
306 format!("{}", a),
307 "[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]"
308 );
309 assert_eq!(
310 format!("{:.1}", a),
311 "[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]"
312 );
313 });
314
315 glam_test!(test_affine3_to_from_slice, {
316 let m = $affine3::from_cols_slice(&MATRIX1D);
317 assert_eq!($affine3::from_cols_array(&MATRIX1D), m);
318 assert_eq!(MATRIX1D, m.to_cols_array());
319 assert_eq!(MATRIX2D, m.to_cols_array_2d());
320 let mut out: [$t; 12] = Default::default();
321 m.write_cols_to_slice(&mut out);
322 assert_eq!(MATRIX1D, out);
323 assert_eq!(
324 m,
325 $affine3::from_cols(
326 MATRIX2D[0].into(),
327 MATRIX2D[1].into(),
328 MATRIX2D[2].into(),
329 MATRIX2D[3].into()
330 )
331 );
332
333 should_panic!({ $affine3::from_cols_slice(&[0.0; 11]) });
334 should_panic!({ $affine3::IDENTITY.write_cols_to_slice(&mut [0.0; 11]) });
335 });
336
337 glam_test!(test_product, {
338 let ident = $affine3::IDENTITY;
339 assert_eq!([ident, ident].iter().product::<$affine3>(), ident * ident);
340 });
341
342 glam_test!(test_affine3_is_finite, {
343 assert!($affine3::from_scale($vec3::new(1.0, 1.0, 1.0)).is_finite());
344 assert!($affine3::from_scale($vec3::new(0.0, 1.0, 1.0)).is_finite());
345 assert!(!$affine3::from_scale($vec3::new(1.0, $t::NAN, 1.0)).is_finite());
346 assert!(!$affine3::from_scale($vec3::new(1.0, 1.0, $t::NEG_INFINITY)).is_finite());
347 });
348 };
349 }
350
351 mod affine3a {
352 use super::support::{deg, FloatCompare};
353 use glam::{Affine3A, Mat3, Mat4, Quat, Vec3, Vec3A};
354
355 impl FloatCompare for Affine3A {
356 #[inline]
357 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
358 self.abs_diff_eq(*other, max_abs_diff)
359 }
360 #[inline]
361 fn abs_diff(&self, other: &Self) -> Self {
362 Self {
363 matrix3: self.matrix3.abs_diff(&other.matrix3),
364 translation: self.translation.abs_diff(&other.translation),
365 }
366 }
367 }
368
369 glam_test!(test_align, {
370 use std::mem;
371 assert_eq!(64, mem::size_of::<Affine3A>());
372 assert_eq!(mem::align_of::<Vec3A>(), mem::align_of::<Affine3A>());
373 });
374
375 glam_test!(test_affine3_mul_vec3a, {
376 let m = Affine3A::from_axis_angle(Vec3::Z, deg(90.0));
377 let result3 = m.transform_vector3a(Vec3A::Y);
378 assert_approx_eq!(Vec3A::new(-1.0, 0.0, 0.0), result3);
379
380 let m = Affine3A::from_scale_rotation_translation(
381 Vec3::new(0.5, 1.5, 2.0),
382 Quat::from_rotation_x(deg(90.0)),
383 Vec3::new(1.0, 2.0, 3.0),
384 );
385 let result3 = m.transform_vector3a(Vec3A::Y);
386 assert_approx_eq!(Vec3A::new(0.0, 0.0, 1.5), result3, 1.0e-6);
387
388 let result3 = m.transform_point3a(Vec3A::Y);
389 assert_approx_eq!(Vec3A::new(1.0, 2.0, 4.5), result3, 1.0e-6);
390 });
391
392 impl_affine3_tests!(f32, Affine3A, Quat, Vec3, Mat3, Mat4);
393 }
394
395 mod daffine3 {
396 use super::support::{deg, FloatCompare};
397 use glam::{DAffine3, DMat3, DMat4, DQuat, DVec3};
398
399 impl FloatCompare for DAffine3 {
400 #[inline]
401 fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool {
402 self.abs_diff_eq(*other, max_abs_diff as f64)
403 }
404 #[inline]
405 fn abs_diff(&self, other: &Self) -> Self {
406 Self {
407 matrix3: self.matrix3.abs_diff(&other.matrix3),
408 translation: self.translation.abs_diff(&other.translation),
409 }
410 }
411 }
412
413 glam_test!(test_align, {
414 use std::mem;
415 assert_eq!(96, mem::size_of::<DAffine3>());
416 assert_eq!(mem::align_of::<f64>(), mem::align_of::<DAffine3>());
417 });
418
419 impl_affine3_tests!(f64, DAffine3, DQuat, DVec3, DMat3, DMat4);
420 }