Debian Codegraph @

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