Debian Codegraph @

debian/missing-sources/glam-0.21.3/tests/mat4.rs

raw
1 #[macro_use]
2 mod support;
3
4 macro_rules! impl_mat4_tests {
5 ($t:ident, $const_new:ident, $newmat4:ident, $newvec4:ident, $newvec3:ident, $mat4:ident, $mat3:ident, $quat:ident, $vec4:ident, $vec3:ident) => {
6 use core::$t::INFINITY;
7 use core::$t::NAN;
8 use core::$t::NEG_INFINITY;
9
10 const IDENTITY: [[$t; 4]; 4] = [
11 [1.0, 0.0, 0.0, 0.0],
12 [0.0, 1.0, 0.0, 0.0],
13 [0.0, 0.0, 1.0, 0.0],
14 [0.0, 0.0, 0.0, 1.0],
15 ];
16 const MATRIX: [[$t; 4]; 4] = [
17 [1.0, 2.0, 3.0, 4.0],
18 [5.0, 6.0, 7.0, 8.0],
19 [9.0, 10.0, 11.0, 12.0],
20 [13.0, 14.0, 15.0, 16.0],
21 ];
22
23 const MATRIX1D: [$t; 16] = [
24 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
25 ];
26
27 glam_test!(test_const, {
28 const M0: $mat4 = $mat4::from_cols(
29 $newvec4(1.0, 2.0, 3.0, 4.0),
30 $newvec4(5.0, 6.0, 7.0, 8.0),
31 $newvec4(9.0, 10.0, 11.0, 12.0),
32 $newvec4(13.0, 14.0, 15.0, 16.0),
33 );
34 const M1: $mat4 = $mat4::from_cols_array(&MATRIX1D);
35 const M2: $mat4 = $mat4::from_cols_array_2d(&MATRIX);
36
37 assert_eq!(MATRIX1D, M0.to_cols_array());
38 assert_eq!(MATRIX1D, M1.to_cols_array());
39 assert_eq!(MATRIX1D, M2.to_cols_array());
40 });
41
42 glam_test!(test_const_macros, {
43 #[allow(deprecated)]
44 const M0: $mat4 = $const_new!([0.0; 16]);
45 #[allow(deprecated)]
46 const M1: $mat4 = $const_new!([
47 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
48 16.0
49 ]);
50 #[allow(deprecated)]
51 const M2: $mat4 = $const_new!(
52 [1.0, 2.0, 3.0, 4.0],
53 [5.0, 6.0, 7.0, 8.0],
54 [9.0, 10.0, 11.0, 12.0],
55 [13.0, 14.0, 15.0, 16.0]
56 );
57 assert_eq!($mat4::ZERO, M0);
58 assert_eq!(
59 $mat4::from_cols_array_2d(&[
60 [1.0, 2.0, 3.0, 4.0],
61 [5.0, 6.0, 7.0, 8.0],
62 [9.0, 10.0, 11.0, 12.0],
63 [13.0, 14.0, 15.0, 16.0]
64 ]),
65 M1
66 );
67 assert_eq!(
68 $mat4::from_cols_array_2d(&[
69 [1.0, 2.0, 3.0, 4.0],
70 [5.0, 6.0, 7.0, 8.0],
71 [9.0, 10.0, 11.0, 12.0],
72 [13.0, 14.0, 15.0, 16.0]
73 ]),
74 M2
75 );
76 });
77
78 glam_test!(test_mat4_identity, {
79 assert_eq!(
80 $mat4::IDENTITY,
81 $mat4::from_cols_array(&[
82 1., 0., 0., 0., //
83 0., 1., 0., 0., //
84 0., 0., 1., 0., //
85 0., 0., 0., 1., //
86 ])
87 );
88 let identity = $mat4::IDENTITY;
89 assert_eq!(IDENTITY, identity.to_cols_array_2d());
90 assert_eq!($mat4::from_cols_array_2d(&IDENTITY), identity);
91 assert_eq!(identity, identity * identity);
92 assert_eq!(identity, $mat4::default());
93 assert_eq!(identity, $mat4::from_diagonal($vec4::ONE));
94 });
95
96 glam_test!(test_mat4_zero, {
97 assert_eq!(
98 $mat4::ZERO,
99 $mat4::from_cols_array(&[
100 0., 0., 0., 0., //
101 0., 0., 0., 0., //
102 0., 0., 0., 0., //
103 0., 0., 0., 0., //
104 ])
105 );
106 });
107
108 glam_test!(test_mat4_nan, {
109 assert!($mat4::NAN.is_nan());
110 assert!(!$mat4::NAN.is_finite());
111 });
112
113 glam_test!(test_mat4_accessors, {
114 let mut m = $mat4::ZERO;
115 m.x_axis = $vec4::new(1.0, 2.0, 3.0, 4.0);
116 m.y_axis = $vec4::new(5.0, 6.0, 7.0, 8.0);
117 m.z_axis = $vec4::new(9.0, 10.0, 11.0, 12.0);
118 m.w_axis = $vec4::new(13.0, 14.0, 15.0, 16.0);
119 assert_eq!($mat4::from_cols_array_2d(&MATRIX), m);
120 assert_eq!($vec4::new(1.0, 2.0, 3.0, 4.0), m.x_axis);
121 assert_eq!($vec4::new(5.0, 6.0, 7.0, 8.0), m.y_axis);
122 assert_eq!($vec4::new(9.0, 10.0, 11.0, 12.0), m.z_axis);
123 assert_eq!($vec4::new(13.0, 14.0, 15.0, 16.0), m.w_axis);
124
125 assert_eq!($vec4::new(1.0, 2.0, 3.0, 4.0), m.col(0));
126 assert_eq!($vec4::new(5.0, 6.0, 7.0, 8.0), m.col(1));
127 assert_eq!($vec4::new(9.0, 10.0, 11.0, 12.0), m.col(2));
128 assert_eq!($vec4::new(13.0, 14.0, 15.0, 16.0), m.col(3));
129
130 assert_eq!($newvec4(1.0, 5.0, 9.0, 13.0), m.row(0));
131 assert_eq!($newvec4(2.0, 6.0, 10.0, 14.0), m.row(1));
132 assert_eq!($newvec4(3.0, 7.0, 11.0, 15.0), m.row(2));
133 assert_eq!($newvec4(4.0, 8.0, 12.0, 16.0), m.row(3));
134
135 *m.col_mut(0) = m.col(0).wzyx();
136 *m.col_mut(1) = m.col(1).wzyx();
137 *m.col_mut(2) = m.col(2).wzyx();
138 *m.col_mut(3) = m.col(3).wzyx();
139 assert_eq!($newvec4(4.0, 3.0, 2.0, 1.0), m.col(0));
140 assert_eq!($newvec4(8.0, 7.0, 6.0, 5.0), m.col(1));
141 assert_eq!($newvec4(12.0, 11.0, 10.0, 9.0), m.col(2));
142 assert_eq!($newvec4(16.0, 15.0, 14.0, 13.0), m.col(3));
143
144 should_panic!({ $mat4::ZERO.col(4) });
145 should_panic!({
146 let mut m = $mat4::ZERO;
147 m.col_mut(4);
148 });
149 should_panic!({ $mat4::ZERO.row(4) });
150 });
151
152 glam_test!(test_mat4_from_axes, {
153 let a = $mat4::from_cols_array_2d(&[
154 [1.0, 2.0, 3.0, 4.0],
155 [5.0, 6.0, 7.0, 8.0],
156 [9.0, 10.0, 11.0, 12.0],
157 [13.0, 14.0, 15.0, 16.0],
158 ]);
159 assert_eq!(MATRIX, a.to_cols_array_2d());
160 let b = $mat4::from_cols(
161 $newvec4(1.0, 2.0, 3.0, 4.0),
162 $newvec4(5.0, 6.0, 7.0, 8.0),
163 $newvec4(9.0, 10.0, 11.0, 12.0),
164 $newvec4(13.0, 14.0, 15.0, 16.0),
165 );
166 assert_eq!(a, b);
167 let c = $newmat4(
168 $newvec4(1.0, 2.0, 3.0, 4.0),
169 $newvec4(5.0, 6.0, 7.0, 8.0),
170 $newvec4(9.0, 10.0, 11.0, 12.0),
171 $newvec4(13.0, 14.0, 15.0, 16.0),
172 );
173 assert_eq!(a, c);
174 let d = b.to_cols_array();
175 let f = $mat4::from_cols_array(&d);
176 assert_eq!(b, f);
177 });
178
179 glam_test!(test_mat4_translation, {
180 let translate = $mat4::from_translation($newvec3(1.0, 2.0, 3.0));
181 assert_eq!(
182 $mat4::from_cols(
183 $newvec4(1.0, 0.0, 0.0, 0.0),
184 $newvec4(0.0, 1.0, 0.0, 0.0),
185 $newvec4(0.0, 0.0, 1.0, 0.0),
186 $newvec4(1.0, 2.0, 3.0, 1.0)
187 ),
188 translate
189 );
190 });
191
192 glam_test!(test_from_rotation, {
193 let rot_x1 = $mat4::from_rotation_x(deg(180.0));
194 let rot_x2 = $mat4::from_axis_angle($vec3::X, deg(180.0));
195 assert_approx_eq!(rot_x1, rot_x2);
196 let rot_y1 = $mat4::from_rotation_y(deg(180.0));
197 let rot_y2 = $mat4::from_axis_angle($vec3::Y, deg(180.0));
198 assert_approx_eq!(rot_y1, rot_y2);
199 let rot_z1 = $mat4::from_rotation_z(deg(180.0));
200 let rot_z2 = $mat4::from_axis_angle($vec3::Z, deg(180.0));
201 assert_approx_eq!(rot_z1, rot_z2);
202
203 assert_approx_eq!($mat4::IDENTITY, $mat4::from_quat($quat::IDENTITY));
204
205 should_glam_assert!({ $mat4::from_axis_angle($vec3::ZERO, 0.0) });
206 should_glam_assert!({ $mat4::from_quat($quat::from_xyzw(0.0, 0.0, 0.0, 0.0)) });
207 });
208
209 glam_test!(test_from_mat3, {
210 let m3 =
211 $mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
212 let m4 = $mat4::from_mat3(m3);
213 assert_eq!(
214 $mat4::from_cols_array_2d(&[
215 [1.0, 2.0, 3.0, 0.0],
216 [4.0, 5.0, 6.0, 0.0],
217 [7.0, 8.0, 9.0, 0.0],
218 [0.0, 0.0, 0.0, 1.0]
219 ]),
220 m4
221 );
222 });
223
224 glam_test!(test_mat4_mul, {
225 let m = $mat4::from_axis_angle($vec3::Z, deg(90.0));
226 let result3 = m.transform_vector3($vec3::Y);
227 assert_approx_eq!($newvec3(-1.0, 0.0, 0.0), result3);
228 assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into());
229 let result4 = m * $vec4::Y;
230 assert_approx_eq!($newvec4(-1.0, 0.0, 0.0, 0.0), result4);
231 assert_approx_eq!(result4, m * $vec4::Y);
232
233 let m = $mat4::from_scale_rotation_translation(
234 $vec3::new(0.5, 1.5, 2.0),
235 $quat::from_rotation_x(deg(90.0)),
236 $vec3::new(1.0, 2.0, 3.0),
237 );
238 let result3 = m.transform_vector3($vec3::Y);
239 assert_approx_eq!($newvec3(0.0, 0.0, 1.5), result3, 1.0e-6);
240 assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into());
241
242 let result3 = m.transform_point3($vec3::Y);
243 assert_approx_eq!($newvec3(1.0, 2.0, 4.5), result3, 1.0e-6);
244 assert_approx_eq!(result3, (m * $vec3::Y.extend(1.0)).truncate().into());
245
246 let m = $mat4::from_cols(
247 $newvec4(8.0, 0.0, 0.0, 0.0),
248 $newvec4(0.0, 4.0, 0.0, 0.0),
249 $newvec4(0.0, 0.0, 2.0, 2.0),
250 $newvec4(0.0, 0.0, 0.0, 0.0),
251 );
252 assert_approx_eq!(
253 $newvec3(4.0, 2.0, 1.0),
254 m.project_point3($newvec3(2.0, 2.0, 2.0))
255 );
256
257 should_glam_assert!({ $mat4::ZERO.transform_vector3($vec3::X) });
258 should_glam_assert!({ $mat4::ZERO.transform_point3($vec3::X) });
259 });
260
261 glam_test!(test_from_ypr, {
262 use glam::EulerRot;
263 let zero = deg(0.0);
264 let yaw = deg(30.0);
265 let pitch = deg(60.0);
266 let roll = deg(90.0);
267 let y0 = $mat4::from_rotation_y(yaw);
268 let y1 = $mat4::from_euler(EulerRot::YXZ, yaw, zero, zero);
269 assert_approx_eq!(y0, y1);
270
271 let x0 = $mat4::from_rotation_x(pitch);
272 let x1 = $mat4::from_euler(EulerRot::YXZ, zero, pitch, zero);
273 assert_approx_eq!(x0, x1);
274
275 let z0 = $mat4::from_rotation_z(roll);
276 let z1 = $mat4::from_euler(EulerRot::YXZ, zero, zero, roll);
277 assert_approx_eq!(z0, z1);
278
279 let yx0 = y0 * x0;
280 let yx1 = $mat4::from_euler(EulerRot::YXZ, yaw, pitch, zero);
281 assert_approx_eq!(yx0, yx1);
282
283 let yxz0 = y0 * x0 * z0;
284 let yxz1 = $mat4::from_euler(EulerRot::YXZ, yaw, pitch, roll);
285 assert_approx_eq!(yxz0, yxz1, 1e-6);
286 });
287
288 glam_test!(test_from_scale, {
289 let m = $mat4::from_scale($vec3::new(2.0, 4.0, 8.0));
290 assert_approx_eq!($vec4::X * 2.0, m.x_axis);
291 assert_approx_eq!($vec4::Y * 4.0, m.y_axis);
292 assert_approx_eq!($vec4::Z * 8.0, m.z_axis);
293 assert_approx_eq!($vec4::W, m.w_axis);
294 assert_approx_eq!(
295 m.transform_point3($vec3::new(1.0, 1.0, 1.0)),
296 $vec3::new(2.0, 4.0, 8.0)
297 );
298
299 should_glam_assert!({ $mat4::from_scale($vec3::ZERO) });
300 });
301
302 glam_test!(test_mat4_transpose, {
303 let m = $newmat4(
304 $newvec4(1.0, 2.0, 3.0, 4.0),
305 $newvec4(5.0, 6.0, 7.0, 8.0),
306 $newvec4(9.0, 10.0, 11.0, 12.0),
307 $newvec4(13.0, 14.0, 15.0, 16.0),
308 );
309 let mt = m.transpose();
310 assert_eq!($newvec4(1.0, 5.0, 9.0, 13.0), mt.x_axis);
311 assert_eq!($newvec4(2.0, 6.0, 10.0, 14.0), mt.y_axis);
312 assert_eq!($newvec4(3.0, 7.0, 11.0, 15.0), mt.z_axis);
313 assert_eq!($newvec4(4.0, 8.0, 12.0, 16.0), mt.w_axis);
314 });
315
316 glam_test!(test_mat4_det, {
317 assert_eq!(0.0, $mat4::ZERO.determinant());
318 assert_eq!(1.0, $mat4::IDENTITY.determinant());
319 assert_eq!(1.0, $mat4::from_rotation_x(deg(90.0)).determinant());
320 assert_eq!(1.0, $mat4::from_rotation_y(deg(180.0)).determinant());
321 assert_eq!(1.0, $mat4::from_rotation_z(deg(270.0)).determinant());
322 assert_eq!(
323 2.0 * 2.0 * 2.0,
324 $mat4::from_scale($newvec3(2.0, 2.0, 2.0)).determinant()
325 );
326 assert_eq!(
327 1.0,
328 $newmat4(
329 $newvec4(0.0, 0.0, 0.0, 1.0),
330 $newvec4(1.0, 0.0, 0.0, 0.0),
331 $newvec4(0.0, 0.0, 1.0, 0.0),
332 $newvec4(0.0, 1.0, 0.0, 0.0),
333 )
334 .determinant()
335 );
336 });
337
338 glam_test!(test_mat4_inverse, {
339 // assert_eq!(None, $mat4::ZERO.inverse());
340 let inv = $mat4::IDENTITY.inverse();
341 // assert_ne!(None, inv);
342 assert_approx_eq!($mat4::IDENTITY, inv);
343
344 let rotz = $mat4::from_rotation_z(deg(90.0));
345 let rotz_inv = rotz.inverse();
346 // assert_ne!(None, rotz_inv);
347 // let rotz_inv = rotz_inv.unwrap();
348 assert_approx_eq!($mat4::IDENTITY, rotz * rotz_inv);
349 assert_approx_eq!($mat4::IDENTITY, rotz_inv * rotz);
350
351 let trans = $mat4::from_translation($newvec3(1.0, 2.0, 3.0));
352 let trans_inv = trans.inverse();
353 // assert_ne!(None, trans_inv);
354 // let trans_inv = trans_inv.unwrap();
355 assert_approx_eq!($mat4::IDENTITY, trans * trans_inv);
356 assert_approx_eq!($mat4::IDENTITY, trans_inv * trans);
357
358 let scale = $mat4::from_scale($newvec3(4.0, 5.0, 6.0));
359 let scale_inv = scale.inverse();
360 // assert_ne!(None, scale_inv);
361 // let scale_inv = scale_inv.unwrap();
362 assert_approx_eq!($mat4::IDENTITY, scale * scale_inv);
363 assert_approx_eq!($mat4::IDENTITY, scale_inv * scale);
364
365 let m = scale * rotz * trans;
366 let m_inv = m.inverse();
367 // assert_ne!(None, m_inv);
368 // let m_inv = m_inv.unwrap();
369 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
370 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
371 assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6);
372
373 // Make sure we can invert a "random" matrix:
374 let m = $mat4::from_cols(
375 $newvec4(1.0, -0.3, 1.0, 1.0),
376 $newvec4(0.5, 0.6, 0.7, 0.8),
377 $newvec4(-0.9, -0.3, 0.0, 12.0),
378 $newvec4(0.13, 0.14, 0.15, 0.16),
379 );
380 let m_inv = m.inverse();
381 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
382 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
383
384 should_glam_assert!({ $mat4::ZERO.inverse() });
385 });
386
387 glam_test!(test_mat4_decompose, {
388 // identity
389 let (out_scale, out_rotation, out_translation) =
390 $mat4::IDENTITY.to_scale_rotation_translation();
391 assert_approx_eq!($vec3::ONE, out_scale);
392 assert!(out_rotation.is_near_identity());
393 assert_approx_eq!($vec3::ZERO, out_translation);
394
395 // no scale
396 let in_scale = $vec3::ONE;
397 let in_translation = $vec3::new(-2.0, 4.0, -0.125);
398 let in_rotation = $quat::from_euler(
399 glam::EulerRot::YXZ,
400 $t::to_radians(-45.0),
401 $t::to_radians(180.0),
402 $t::to_radians(270.0),
403 );
404 let in_mat =
405 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
406 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
407 assert_approx_eq!(in_scale, out_scale, 1e-6);
408 // out_rotation is different but produces the same matrix
409 // assert_approx_eq!(in_rotation, out_rotation);
410 assert_approx_eq!(in_translation, out_translation);
411 assert_approx_eq!(
412 in_mat,
413 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
414 1e-6
415 );
416
417 // positive scale
418 let in_scale = $vec3::new(1.0, 2.0, 4.0);
419 let in_mat =
420 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
421 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
422 assert_approx_eq!(in_scale, out_scale, 1e-6);
423 // out_rotation is different but produces the same matrix
424 // assert_approx_eq!(in_rotation, out_rotation);
425 assert_approx_eq!(in_translation, out_translation);
426 assert_approx_eq!(
427 in_mat,
428 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
429 1e-5
430 );
431
432 // negative scale
433 let in_scale = $vec3::new(-4.0, 1.0, 2.0);
434 let in_mat =
435 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
436 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
437 assert_approx_eq!(in_scale, out_scale, 1e-6);
438 // out_rotation is different but produces the same matrix
439 // assert_approx_eq!(in_rotation, out_rotation);
440 assert_approx_eq!(in_translation, out_translation);
441 assert_approx_eq!(
442 in_mat,
443 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
444 1e-5
445 );
446
447 // negative scale
448 let in_scale = $vec3::new(4.0, -1.0, -2.0);
449 let in_mat =
450 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
451 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
452 // out_scale and out_rotation are different but they produce the same matrix
453 // assert_approx_eq!(in_scale, out_scale, 1e-6);
454 // assert_approx_eq!(in_rotation, out_rotation);
455 assert_approx_eq!(in_translation, out_translation);
456 assert_approx_eq!(
457 in_mat,
458 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
459 1e-6
460 );
461
462 should_glam_assert!({
463 $mat4::from_scale_rotation_translation(
464 $vec3::ONE,
465 $quat::from_xyzw(0.0, 0.0, 0.0, 0.0),
466 $vec3::ZERO,
467 )
468 });
469 should_glam_assert!({
470 $mat4::from_rotation_translation($quat::from_xyzw(0.0, 0.0, 0.0, 0.0), $vec3::ZERO)
471 });
472 // TODO: should check scale
473 // should_glam_assert!({ $mat4::from_scale_rotation_translation($vec3::ZERO, $quat::IDENTITY, $vec3::ZERO) });
474 should_glam_assert!({ $mat4::ZERO.to_scale_rotation_translation() });
475 });
476
477 glam_test!(test_mat4_look_at, {
478 let eye = $vec3::new(0.0, 0.0, -5.0);
479 let center = $vec3::new(0.0, 0.0, 0.0);
480 let up = $vec3::new(1.0, 0.0, 0.0);
481 let lh = $mat4::look_at_lh(eye, center, up);
482 let rh = $mat4::look_at_rh(eye, center, up);
483 let point = $vec3::new(1.0, 0.0, 0.0);
484 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
485 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
486
487 should_glam_assert!({ $mat4::look_at_lh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
488 should_glam_assert!({ $mat4::look_at_rh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
489 });
490
491 glam_test!(test_mat4_perspective_gl_rh, {
492 let projection = $mat4::perspective_rh_gl($t::to_radians(90.0), 2.0, 5.0, 15.0);
493
494 let original = $vec3::new(5.0, 5.0, -15.0);
495 let projected = projection * original.extend(1.0);
496 assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected);
497
498 let original = $vec3::new(5.0, 5.0, -5.0);
499 let projected = projection * original.extend(1.0);
500 assert_approx_eq!($vec4::new(2.5, 5.0, -5.0, 5.0), projected);
501 });
502
503 glam_test!(test_mat4_perspective_lh, {
504 let projection = $mat4::perspective_lh($t::to_radians(90.0), 2.0, 5.0, 15.0);
505
506 let original = $vec3::new(5.0, 5.0, 15.0);
507 let projected = projection * original.extend(1.0);
508 assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected);
509
510 let original = $vec3::new(5.0, 5.0, 5.0);
511 let projected = projection * original.extend(1.0);
512 assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected);
513
514 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 1.0, 0.0) });
515 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 0.0, 1.0) });
516 });
517
518 glam_test!(test_mat4_perspective_infinite_lh, {
519 let projection = $mat4::perspective_infinite_lh($t::to_radians(90.0), 2.0, 5.0);
520
521 let original = $vec3::new(5.0, 5.0, 15.0);
522 let projected = projection * original.extend(1.0);
523 assert_approx_eq!($vec4::new(2.5, 5.0, 10.0, 15.0), projected);
524
525 let original = $vec3::new(5.0, 5.0, 5.0);
526 let projected = projection * original.extend(1.0);
527 assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected);
528
529 should_glam_assert!({ $mat4::perspective_infinite_lh(0.0, 1.0, 0.0) });
530 });
531
532 glam_test!(test_mat4_perspective_infinite_reverse_lh, {
533 let projection = $mat4::perspective_infinite_reverse_lh($t::to_radians(90.0), 2.0, 5.0);
534
535 let original = $vec3::new(5.0, 5.0, 15.0);
536 let projected = projection * original.extend(1.0);
537 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 15.0), projected);
538
539 let original = $vec3::new(5.0, 5.0, 5.0);
540 let projected = projection * original.extend(1.0);
541 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 5.0), projected);
542
543 should_glam_assert!({ $mat4::perspective_infinite_reverse_lh(0.0, 1.0, 0.0) });
544 });
545
546 glam_test!(test_mat4_perspective_rh, {
547 let projection = $mat4::perspective_rh($t::to_radians(90.0), 2.0, 5.0, 15.0);
548
549 let original = $vec3::new(5.0, 5.0, 15.0);
550 let projected = projection * original.extend(1.0);
551 assert_approx_eq!($vec4::new(2.5, 5.0, -30.0, -15.0), projected);
552
553 let original = $vec3::new(5.0, 5.0, 5.0);
554 let projected = projection * original.extend(1.0);
555 assert_approx_eq!($vec4::new(2.5, 5.0, -15.0, -5.0), projected);
556
557 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 1.0, 0.0) });
558 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 0.0, 1.0) });
559 });
560
561 glam_test!(test_mat4_perspective_infinite_rh, {
562 let projection = $mat4::perspective_infinite_rh($t::to_radians(90.0), 2.0, 5.0);
563
564 let original = $vec3::new(5.0, 5.0, 15.0);
565 let projected = projection * original.extend(1.0);
566 assert_approx_eq!($vec4::new(2.5, 5.0, -20.0, -15.0), projected);
567
568 let original = $vec3::new(5.0, 5.0, 5.0);
569 let projected = projection * original.extend(1.0);
570 assert_approx_eq!($vec4::new(2.5, 5.0, -10.0, -5.0), projected);
571
572 should_glam_assert!({ $mat4::perspective_infinite_rh(0.0, 1.0, 0.0) });
573 });
574
575 glam_test!(test_mat4_perspective_infinite_reverse_rh, {
576 let projection = $mat4::perspective_infinite_reverse_rh($t::to_radians(90.0), 2.0, 5.0);
577
578 let original = $vec3::new(5.0, 5.0, 15.0);
579 let projected = projection * original.extend(1.0);
580 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -15.0), projected);
581
582 let original = $vec3::new(5.0, 5.0, 5.0);
583 let projected = projection * original.extend(1.0);
584 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -5.0), projected);
585
586 should_glam_assert!({ $mat4::perspective_infinite_reverse_rh(0.0, 1.0, 0.0) });
587 });
588
589 glam_test!(test_mat4_orthographic_gl_rh, {
590 let projection = $mat4::orthographic_rh_gl(-10.0, 10.0, -5.0, 5.0, 0.0, -10.0);
591 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
592 let projected = projection.mul_vec4(original);
593 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, -2.0, 1.0));
594 });
595
596 glam_test!(test_mat4_orthographic_rh, {
597 let projection = $mat4::orthographic_rh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
598 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
599 let projected = projection.mul_vec4(original);
600 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
601
602 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
603 let projected = projection.mul_vec4(original);
604 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
605 });
606
607 glam_test!(test_mat4_orthographic_lh, {
608 let projection = $mat4::orthographic_lh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
609 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
610 let projected = projection.mul_vec4(original);
611 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
612
613 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
614 let projected = projection.mul_vec4(original);
615 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
616 });
617
618 glam_test!(test_mat4_ops, {
619 let m0 = $mat4::from_cols_array_2d(&MATRIX);
620 let m0x2 = $mat4::from_cols_array_2d(&[
621 [2.0, 4.0, 6.0, 8.0],
622 [10.0, 12.0, 14.0, 16.0],
623 [18.0, 20.0, 22.0, 24.0],
624 [26.0, 28.0, 30.0, 32.0],
625 ]);
626 let m0_neg = $mat4::from_cols_array_2d(&[
627 [-1.0, -2.0, -3.0, -4.0],
628 [-5.0, -6.0, -7.0, -8.0],
629 [-9.0, -10.0, -11.0, -12.0],
630 [-13.0, -14.0, -15.0, -16.0],
631 ]);
632 assert_eq!(m0x2, m0 * 2.0);
633 assert_eq!(m0x2, 2.0 * m0);
634 assert_eq!(m0x2, m0 + m0);
635 assert_eq!($mat4::ZERO, m0 - m0);
636 assert_eq!(m0_neg, -m0);
637 assert_approx_eq!(m0, m0 * $mat4::IDENTITY);
638 assert_approx_eq!(m0, $mat4::IDENTITY * m0);
639
640 let mut m1 = m0;
641 m1 *= 2.0;
642 assert_eq!(m0x2, m1);
643
644 let mut m1 = m0;
645 m1 += m0;
646 assert_eq!(m0x2, m1);
647
648 let mut m1 = m0;
649 m1 -= m0;
650 assert_eq!($mat4::ZERO, m1);
651
652 let mut m1 = $mat4::IDENTITY;
653 m1 *= m0;
654 assert_approx_eq!(m0, m1);
655 });
656
657 glam_test!(test_mat4_fmt, {
658 let a = $mat4::from_cols_array_2d(&MATRIX);
659 assert_eq!(
660 format!("{}", a),
661 "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]"
662 );
663 });
664
665 glam_test!(test_mat4_to_from_slice, {
666 let m = $mat4::from_cols_slice(&MATRIX1D);
667 assert_eq!($mat4::from_cols_array(&MATRIX1D), m);
668 let mut out: [$t; 16] = Default::default();
669 m.write_cols_to_slice(&mut out);
670 assert_eq!(MATRIX1D, out);
671
672 should_panic!({ $mat4::from_cols_slice(&[0.0; 15]) });
673 should_panic!({ $mat4::IDENTITY.write_cols_to_slice(&mut [0.0; 15]) });
674 });
675
676 glam_test!(test_sum, {
677 let id = $mat4::IDENTITY;
678 assert_eq!(vec![id, id].iter().sum::<$mat4>(), id + id);
679 });
680
681 glam_test!(test_product, {
682 let two = $mat4::IDENTITY + $mat4::IDENTITY;
683 assert_eq!(vec![two, two].iter().product::<$mat4>(), two * two);
684 });
685
686 glam_test!(test_mat4_is_finite, {
687 assert!($mat4::IDENTITY.is_finite());
688 assert!(!($mat4::IDENTITY * INFINITY).is_finite());
689 assert!(!($mat4::IDENTITY * NEG_INFINITY).is_finite());
690 assert!(!($mat4::IDENTITY * NAN).is_finite());
691 });
692 };
693 }
694
695 macro_rules! impl_as_ref_tests {
696 ($mat:ident) => {
697 glam_test!(test_as_ref, {
698 let m = $mat::from_cols_array_2d(&MATRIX);
699 assert_eq!(MATRIX1D, *m.as_ref());
700 });
701 glam_test!(test_as_mut, {
702 let mut m = $mat::ZERO;
703 *m.as_mut() = MATRIX1D;
704 assert_eq!($mat::from_cols_array_2d(&MATRIX), m);
705 });
706 };
707 }
708
709 mod mat4 {
710 use super::support::deg;
711 #[allow(deprecated)]
712 use glam::{const_mat4, mat4, swizzles::*, vec3, vec4, Mat3, Mat4, Quat, Vec3, Vec4};
713
714 glam_test!(test_align, {
715 use std::mem;
716 assert_eq!(mem::align_of::<Vec4>(), mem::align_of::<Mat4>());
717 assert_eq!(64, mem::size_of::<Mat4>());
718 });
719
720 glam_test!(test_as, {
721 use glam::DMat4;
722 assert_eq!(
723 DMat4::from_cols_array_2d(&[
724 [1.0, 2.0, 3.0, 4.0],
725 [5.0, 6.0, 7.0, 8.0],
726 [9.0, 10.0, 11.0, 12.0],
727 [13.0, 14.0, 15.0, 16.0]
728 ]),
729 Mat4::from_cols_array_2d(&[
730 [1.0, 2.0, 3.0, 4.0],
731 [5.0, 6.0, 7.0, 8.0],
732 [9.0, 10.0, 11.0, 12.0],
733 [13.0, 14.0, 15.0, 16.0]
734 ])
735 .as_dmat4()
736 );
737 assert_eq!(
738 Mat4::from_cols_array_2d(&[
739 [1.0, 2.0, 3.0, 4.0],
740 [5.0, 6.0, 7.0, 8.0],
741 [9.0, 10.0, 11.0, 12.0],
742 [13.0, 14.0, 15.0, 16.0]
743 ]),
744 DMat4::from_cols_array_2d(&[
745 [1.0, 2.0, 3.0, 4.0],
746 [5.0, 6.0, 7.0, 8.0],
747 [9.0, 10.0, 11.0, 12.0],
748 [13.0, 14.0, 15.0, 16.0]
749 ])
750 .as_mat4()
751 );
752 });
753
754 impl_mat4_tests!(f32, const_mat4, mat4, vec4, vec3, Mat4, Mat3, Quat, Vec4, Vec3);
755 impl_as_ref_tests!(Mat4);
756 }
757
758 mod dmat4 {
759 use super::support::deg;
760 #[allow(deprecated)]
761 use glam::{const_dmat4, dmat4, dvec3, dvec4, swizzles::*, DMat3, DMat4, DQuat, DVec3, DVec4};
762
763 glam_test!(test_align, {
764 use std::mem;
765 assert_eq!(mem::align_of::<DVec4>(), mem::align_of::<DMat4>());
766 assert_eq!(128, mem::size_of::<DMat4>());
767 });
768
769 impl_mat4_tests!(
770 f64,
771 const_dmat4,
772 dmat4,
773 dvec4,
774 dvec3,
775 DMat4,
776 DMat3,
777 DQuat,
778 DVec4,
779 DVec3
780 );
781 impl_as_ref_tests!(DMat4);
782 }