Debian Codegraph @

debian/missing-sources/glam-0.29.3/tests/mat4.rs

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