Debian Codegraph @

debian/missing-sources/glam-0.19.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_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 });
304
305 glam_test!(test_mat4_inverse, {
306 // assert_eq!(None, $mat4::ZERO.inverse());
307 let inv = $mat4::IDENTITY.inverse();
308 // assert_ne!(None, inv);
309 assert_approx_eq!($mat4::IDENTITY, inv);
310
311 let rotz = $mat4::from_rotation_z(deg(90.0));
312 let rotz_inv = rotz.inverse();
313 // assert_ne!(None, rotz_inv);
314 // let rotz_inv = rotz_inv.unwrap();
315 assert_approx_eq!($mat4::IDENTITY, rotz * rotz_inv);
316 assert_approx_eq!($mat4::IDENTITY, rotz_inv * rotz);
317
318 let trans = $mat4::from_translation($newvec3(1.0, 2.0, 3.0));
319 let trans_inv = trans.inverse();
320 // assert_ne!(None, trans_inv);
321 // let trans_inv = trans_inv.unwrap();
322 assert_approx_eq!($mat4::IDENTITY, trans * trans_inv);
323 assert_approx_eq!($mat4::IDENTITY, trans_inv * trans);
324
325 let scale = $mat4::from_scale($newvec3(4.0, 5.0, 6.0));
326 let scale_inv = scale.inverse();
327 // assert_ne!(None, scale_inv);
328 // let scale_inv = scale_inv.unwrap();
329 assert_approx_eq!($mat4::IDENTITY, scale * scale_inv);
330 assert_approx_eq!($mat4::IDENTITY, scale_inv * scale);
331
332 let m = scale * rotz * trans;
333 let m_inv = m.inverse();
334 // assert_ne!(None, m_inv);
335 // let m_inv = m_inv.unwrap();
336 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
337 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
338 assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6);
339
340 // Make sure we can invert a "random" matrix:
341 let m = $mat4::from_cols(
342 $newvec4(1.0, -0.3, 1.0, 1.0),
343 $newvec4(0.5, 0.6, 0.7, 0.8),
344 $newvec4(-0.9, -0.3, 0.0, 12.0),
345 $newvec4(0.13, 0.14, 0.15, 0.16),
346 );
347 let m_inv = m.inverse();
348 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
349 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
350
351 should_glam_assert!({ $mat4::ZERO.inverse() });
352 });
353
354 glam_test!(test_mat4_decompose, {
355 // identity
356 let (out_scale, out_rotation, out_translation) =
357 $mat4::IDENTITY.to_scale_rotation_translation();
358 assert_approx_eq!($vec3::ONE, out_scale);
359 assert!(out_rotation.is_near_identity());
360 assert_approx_eq!($vec3::ZERO, out_translation);
361
362 // no scale
363 let in_scale = $vec3::ONE;
364 let in_translation = $vec3::new(-2.0, 4.0, -0.125);
365 let in_rotation = $quat::from_euler(
366 glam::EulerRot::YXZ,
367 $t::to_radians(-45.0),
368 $t::to_radians(180.0),
369 $t::to_radians(270.0),
370 );
371 let in_mat =
372 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
373 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
374 assert_approx_eq!(in_scale, out_scale, 1e-6);
375 // out_rotation is different but produces the same matrix
376 // assert_approx_eq!(in_rotation, out_rotation);
377 assert_approx_eq!(in_translation, out_translation);
378 assert_approx_eq!(
379 in_mat,
380 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
381 1e-6
382 );
383
384 // positive scale
385 let in_scale = $vec3::new(1.0, 2.0, 4.0);
386 let in_mat =
387 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
388 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
389 assert_approx_eq!(in_scale, out_scale, 1e-6);
390 // out_rotation is different but produces the same matrix
391 // assert_approx_eq!(in_rotation, out_rotation);
392 assert_approx_eq!(in_translation, out_translation);
393 assert_approx_eq!(
394 in_mat,
395 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
396 1e-6
397 );
398
399 // negative scale
400 let in_scale = $vec3::new(-4.0, 1.0, 2.0);
401 let in_mat =
402 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
403 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
404 assert_approx_eq!(in_scale, out_scale, 1e-6);
405 // out_rotation is different but produces the same matrix
406 // assert_approx_eq!(in_rotation, out_rotation);
407 assert_approx_eq!(in_translation, out_translation);
408 assert_approx_eq!(
409 in_mat,
410 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
411 1e-5
412 );
413
414 // negative scale
415 let in_scale = $vec3::new(4.0, -1.0, -2.0);
416 let in_mat =
417 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
418 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
419 // out_scale and out_rotation are different but they produce the same matrix
420 // assert_approx_eq!(in_scale, out_scale, 1e-6);
421 // assert_approx_eq!(in_rotation, out_rotation);
422 assert_approx_eq!(in_translation, out_translation);
423 assert_approx_eq!(
424 in_mat,
425 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
426 1e-6
427 );
428
429 should_glam_assert!({
430 $mat4::from_scale_rotation_translation(
431 $vec3::ONE,
432 $quat::from_xyzw(0.0, 0.0, 0.0, 0.0),
433 $vec3::ZERO,
434 )
435 });
436 should_glam_assert!({
437 $mat4::from_rotation_translation($quat::from_xyzw(0.0, 0.0, 0.0, 0.0), $vec3::ZERO)
438 });
439 // TODO: should check scale
440 // should_glam_assert!({ $mat4::from_scale_rotation_translation($vec3::ZERO, $quat::IDENTITY, $vec3::ZERO) });
441 should_glam_assert!({ $mat4::ZERO.to_scale_rotation_translation() });
442 });
443
444 glam_test!(test_mat4_look_at, {
445 let eye = $vec3::new(0.0, 0.0, -5.0);
446 let center = $vec3::new(0.0, 0.0, 0.0);
447 let up = $vec3::new(1.0, 0.0, 0.0);
448 let lh = $mat4::look_at_lh(eye, center, up);
449 let rh = $mat4::look_at_rh(eye, center, up);
450 let point = $vec3::new(1.0, 0.0, 0.0);
451 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
452 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
453
454 should_glam_assert!({ $mat4::look_at_lh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
455 should_glam_assert!({ $mat4::look_at_rh($vec3::ONE, $vec3::ZERO, $vec3::ZERO) });
456 });
457
458 glam_test!(test_mat4_perspective_gl_rh, {
459 let projection = $mat4::perspective_rh_gl($t::to_radians(90.0), 2.0, 5.0, 15.0);
460
461 let original = $vec3::new(5.0, 5.0, -15.0);
462 let projected = projection * original.extend(1.0);
463 assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected);
464
465 let original = $vec3::new(5.0, 5.0, -5.0);
466 let projected = projection * original.extend(1.0);
467 assert_approx_eq!($vec4::new(2.5, 5.0, -5.0, 5.0), projected);
468 });
469
470 glam_test!(test_mat4_perspective_lh, {
471 let projection = $mat4::perspective_lh($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, 0.0, 5.0), projected);
480
481 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 1.0, 0.0) });
482 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 0.0, 1.0) });
483 });
484
485 glam_test!(test_mat4_perspective_infinite_lh, {
486 let projection = $mat4::perspective_infinite_lh($t::to_radians(90.0), 2.0, 5.0);
487
488 let original = $vec3::new(5.0, 5.0, 15.0);
489 let projected = projection * original.extend(1.0);
490 assert_approx_eq!($vec4::new(2.5, 5.0, 10.0, 15.0), projected);
491
492 let original = $vec3::new(5.0, 5.0, 5.0);
493 let projected = projection * original.extend(1.0);
494 assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected);
495
496 should_glam_assert!({ $mat4::perspective_infinite_lh(0.0, 1.0, 0.0) });
497 });
498
499 glam_test!(test_mat4_perspective_infinite_reverse_lh, {
500 let projection = $mat4::perspective_infinite_reverse_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, 5.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, 5.0, 5.0), projected);
509
510 should_glam_assert!({ $mat4::perspective_infinite_reverse_lh(0.0, 1.0, 0.0) });
511 });
512
513 glam_test!(test_mat4_perspective_rh, {
514 let projection = $mat4::perspective_rh($t::to_radians(90.0), 2.0, 5.0, 15.0);
515
516 let original = $vec3::new(5.0, 5.0, 15.0);
517 let projected = projection * original.extend(1.0);
518 assert_approx_eq!($vec4::new(2.5, 5.0, -30.0, -15.0), projected);
519
520 let original = $vec3::new(5.0, 5.0, 5.0);
521 let projected = projection * original.extend(1.0);
522 assert_approx_eq!($vec4::new(2.5, 5.0, -15.0, -5.0), projected);
523
524 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 1.0, 0.0) });
525 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 0.0, 1.0) });
526 });
527
528 glam_test!(test_mat4_perspective_infinite_rh, {
529 let projection = $mat4::perspective_infinite_rh($t::to_radians(90.0), 2.0, 5.0);
530
531 let original = $vec3::new(5.0, 5.0, 15.0);
532 let projected = projection * original.extend(1.0);
533 assert_approx_eq!($vec4::new(2.5, 5.0, -20.0, -15.0), projected);
534
535 let original = $vec3::new(5.0, 5.0, 5.0);
536 let projected = projection * original.extend(1.0);
537 assert_approx_eq!($vec4::new(2.5, 5.0, -10.0, -5.0), projected);
538
539 should_glam_assert!({ $mat4::perspective_infinite_rh(0.0, 1.0, 0.0) });
540 });
541
542 glam_test!(test_mat4_perspective_infinite_reverse_rh, {
543 let projection = $mat4::perspective_infinite_reverse_rh($t::to_radians(90.0), 2.0, 5.0);
544
545 let original = $vec3::new(5.0, 5.0, 15.0);
546 let projected = projection * original.extend(1.0);
547 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -15.0), projected);
548
549 let original = $vec3::new(5.0, 5.0, 5.0);
550 let projected = projection * original.extend(1.0);
551 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -5.0), projected);
552
553 should_glam_assert!({ $mat4::perspective_infinite_reverse_rh(0.0, 1.0, 0.0) });
554 });
555
556 glam_test!(test_mat4_orthographic_gl_rh, {
557 let projection = $mat4::orthographic_rh_gl(-10.0, 10.0, -5.0, 5.0, 0.0, -10.0);
558 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
559 let projected = projection.mul_vec4(original);
560 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, -2.0, 1.0));
561 });
562
563 glam_test!(test_mat4_orthographic_rh, {
564 let projection = $mat4::orthographic_rh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
565 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
566 let projected = projection.mul_vec4(original);
567 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
568
569 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
570 let projected = projection.mul_vec4(original);
571 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
572 });
573
574 glam_test!(test_mat4_orthographic_lh, {
575 let projection = $mat4::orthographic_lh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
576 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
577 let projected = projection.mul_vec4(original);
578 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
579
580 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
581 let projected = projection.mul_vec4(original);
582 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
583 });
584
585 glam_test!(test_mat4_ops, {
586 let m0 = $mat4::from_cols_array_2d(&MATRIX);
587 let m0x2 = $mat4::from_cols_array_2d(&[
588 [2.0, 4.0, 6.0, 8.0],
589 [10.0, 12.0, 14.0, 16.0],
590 [18.0, 20.0, 22.0, 24.0],
591 [26.0, 28.0, 30.0, 32.0],
592 ]);
593 assert_eq!(m0x2, m0 * 2.0);
594 assert_eq!(m0x2, 2.0 * m0);
595 assert_eq!(m0x2, m0 + m0);
596 assert_eq!($mat4::ZERO, m0 - m0);
597 assert_approx_eq!(m0, m0 * $mat4::IDENTITY);
598 assert_approx_eq!(m0, $mat4::IDENTITY * m0);
599
600 let mut m1 = m0;
601 m1 *= 2.0;
602 assert_eq!(m0x2, m1);
603
604 let mut m1 = m0;
605 m1 += m0;
606 assert_eq!(m0x2, m1);
607
608 let mut m1 = m0;
609 m1 -= m0;
610 assert_eq!($mat4::ZERO, m1);
611
612 let mut m1 = $mat4::IDENTITY;
613 m1 *= m0;
614 assert_approx_eq!(m0, m1);
615 });
616
617 glam_test!(test_mat4_fmt, {
618 let a = $mat4::from_cols_array_2d(&MATRIX);
619 assert_eq!(
620 format!("{}", a),
621 "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]"
622 );
623 });
624
625 glam_test!(test_mat4_to_from_slice, {
626 const MATRIX1D: [$t; 16] = [
627 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,
628 16.0,
629 ];
630 let m = $mat4::from_cols_slice(&MATRIX1D);
631 assert_eq!($mat4::from_cols_array(&MATRIX1D), m);
632 let mut out: [$t; 16] = Default::default();
633 m.write_cols_to_slice(&mut out);
634 assert_eq!(MATRIX1D, out);
635
636 should_panic!({ $mat4::from_cols_slice(&[0.0; 15]) });
637 should_panic!({ $mat4::IDENTITY.write_cols_to_slice(&mut [0.0; 15]) });
638 });
639
640 glam_test!(test_sum, {
641 let id = $mat4::IDENTITY;
642 assert_eq!(vec![id, id].iter().sum::<$mat4>(), id + id);
643 });
644
645 glam_test!(test_product, {
646 let two = $mat4::IDENTITY + $mat4::IDENTITY;
647 assert_eq!(vec![two, two].iter().product::<$mat4>(), two * two);
648 });
649
650 glam_test!(test_mat4_is_finite, {
651 assert!($mat4::IDENTITY.is_finite());
652 assert!(!($mat4::IDENTITY * INFINITY).is_finite());
653 assert!(!($mat4::IDENTITY * NEG_INFINITY).is_finite());
654 assert!(!($mat4::IDENTITY * NAN).is_finite());
655 });
656 };
657 }
658
659 mod mat4 {
660 use super::support::deg;
661 use glam::{const_mat4, mat4, swizzles::*, vec3, vec4, Mat3, Mat4, Quat, Vec3, Vec4};
662
663 glam_test!(test_align, {
664 use std::mem;
665 assert_eq!(64, mem::size_of::<Mat4>());
666 if cfg!(feature = "scalar-math") {
667 assert_eq!(4, mem::align_of::<Mat4>());
668 } else {
669 assert_eq!(16, mem::align_of::<Mat4>());
670 }
671 });
672
673 glam_test!(test_as, {
674 use glam::DMat4;
675 assert_eq!(
676 DMat4::from_cols_array_2d(&[
677 [1.0, 2.0, 3.0, 4.0],
678 [5.0, 6.0, 7.0, 8.0],
679 [9.0, 10.0, 11.0, 12.0],
680 [13.0, 14.0, 15.0, 16.0]
681 ]),
682 Mat4::from_cols_array_2d(&[
683 [1.0, 2.0, 3.0, 4.0],
684 [5.0, 6.0, 7.0, 8.0],
685 [9.0, 10.0, 11.0, 12.0],
686 [13.0, 14.0, 15.0, 16.0]
687 ])
688 .as_dmat4()
689 );
690 assert_eq!(
691 Mat4::from_cols_array_2d(&[
692 [1.0, 2.0, 3.0, 4.0],
693 [5.0, 6.0, 7.0, 8.0],
694 [9.0, 10.0, 11.0, 12.0],
695 [13.0, 14.0, 15.0, 16.0]
696 ]),
697 DMat4::from_cols_array_2d(&[
698 [1.0, 2.0, 3.0, 4.0],
699 [5.0, 6.0, 7.0, 8.0],
700 [9.0, 10.0, 11.0, 12.0],
701 [13.0, 14.0, 15.0, 16.0]
702 ])
703 .as_mat4()
704 );
705 });
706
707 impl_mat4_tests!(f32, const_mat4, mat4, vec4, vec3, Mat4, Mat3, Quat, Vec4, Vec3);
708 }
709
710 mod dmat4 {
711 use super::support::deg;
712 use glam::{const_dmat4, dmat4, dvec3, dvec4, swizzles::*, DMat3, DMat4, DQuat, DVec3, DVec4};
713
714 glam_test!(test_align, {
715 use std::mem;
716 assert_eq!(128, mem::size_of::<DMat4>());
717 assert_eq!(mem::align_of::<f64>(), mem::align_of::<DMat4>());
718 });
719
720 impl_mat4_tests!(
721 f64,
722 const_dmat4,
723 dmat4,
724 dvec4,
725 dvec3,
726 DMat4,
727 DMat3,
728 DQuat,
729 DVec4,
730 DVec3
731 );
732 }