Debian Codegraph @

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