Debian Codegraph @

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