Debian Codegraph @

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