Debian Codegraph @

debian/missing-sources/glam/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 let t = $vec3::new(10.0, 11.0, 12.0);
184 let m4 = $mat4::from_mat3_translation(m3, t);
185 assert_eq!($mat4::from_cols_array_2d(&[
186 [1.0, 2.0, 3.0, 0.0],
187 [4.0, 5.0, 6.0, 0.0],
188 [7.0, 8.0, 9.0, 0.0],
189 [10.0, 11.0, 12.0, 1.0]
190 ]),
191 m4);
192 });
193
194 glam_test!(test_mat4_mul_vec4, {
195 let m = $mat4::from_axis_angle($vec3::Z, deg(90.0));
196 assert_approx_eq!($vec4::NEG_X, m * $vec4::Y);
197 assert_approx_eq!($vec4::NEG_X, &m * $vec4::Y);
198 assert_approx_eq!($vec4::NEG_X, m * &$vec4::Y);
199 assert_approx_eq!($vec4::NEG_X, &m * &$vec4::Y);
200
201 assert_approx_eq!($vec4::NEG_X, m.mul_vec4($vec4::Y))
202 });
203
204 glam_test!(test_mat4_transform3d, {
205 let m = $mat4::from_axis_angle($vec3::Z, deg(90.0));
206 let result3 = m.transform_vector3($vec3::Y);
207 assert_approx_eq!($newvec3(-1.0, 0.0, 0.0), result3);
208 assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into());
209 let result4 = m * $vec4::Y;
210 assert_approx_eq!($newvec4(-1.0, 0.0, 0.0, 0.0), result4);
211 assert_approx_eq!(result4, m * $vec4::Y);
212
213 let m = $mat4::from_scale_rotation_translation(
214 $vec3::new(0.5, 1.5, 2.0),
215 $quat::from_rotation_x(deg(90.0)),
216 $vec3::new(1.0, 2.0, 3.0),
217 );
218 let result3 = m.transform_vector3($vec3::Y);
219 assert_approx_eq!($newvec3(0.0, 0.0, 1.5), result3, 1.0e-6);
220 assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into());
221
222 let result3 = m.transform_point3($vec3::Y);
223 assert_approx_eq!($newvec3(1.0, 2.0, 4.5), result3, 1.0e-6);
224 assert_approx_eq!(result3, (m * $vec3::Y.extend(1.0)).truncate().into());
225
226 let m = $mat4::from_cols(
227 $newvec4(8.0, 0.0, 0.0, 0.0),
228 $newvec4(0.0, 4.0, 0.0, 0.0),
229 $newvec4(0.0, 0.0, 2.0, 2.0),
230 $newvec4(0.0, 0.0, 0.0, 0.0),
231 );
232 assert_approx_eq!(
233 $newvec3(4.0, 2.0, 1.0),
234 m.project_point3($newvec3(2.0, 2.0, 2.0))
235 );
236
237 should_glam_assert!({ $mat4::ZERO.transform_vector3($vec3::X) });
238 should_glam_assert!({ $mat4::ZERO.transform_point3($vec3::X) });
239 });
240
241 glam_test!(test_from_ypr, {
242 use glam::EulerRot;
243 let zero = deg(0.0);
244 let yaw = deg(30.0);
245 let pitch = deg(60.0);
246 let roll = deg(90.0);
247 let y0 = $mat4::from_rotation_y(yaw);
248 let y1 = $mat4::from_euler(EulerRot::YXZ, yaw, zero, zero);
249 assert_approx_eq!(y0, y1);
250
251 let x0 = $mat4::from_rotation_x(pitch);
252 let x1 = $mat4::from_euler(EulerRot::YXZ, zero, pitch, zero);
253 assert_approx_eq!(x0, x1);
254
255 let z0 = $mat4::from_rotation_z(roll);
256 let z1 = $mat4::from_euler(EulerRot::YXZ, zero, zero, roll);
257 assert_approx_eq!(z0, z1);
258
259 let yx0 = y0 * x0;
260 let yx1 = $mat4::from_euler(EulerRot::YXZ, yaw, pitch, zero);
261 assert_approx_eq!(yx0, yx1, 1e-6);
262
263 let yxz0 = y0 * x0 * z0;
264 let yxz1 = $mat4::from_euler(EulerRot::YXZ, yaw, pitch, roll);
265 assert_approx_eq!(yxz0, yxz1, 1e-6);
266 });
267
268 glam_test!(test_from_scale, {
269 let m = $mat4::from_scale($vec3::new(2.0, 4.0, 8.0));
270 assert_approx_eq!($vec4::X * 2.0, m.x_axis);
271 assert_approx_eq!($vec4::Y * 4.0, m.y_axis);
272 assert_approx_eq!($vec4::Z * 8.0, m.z_axis);
273 assert_approx_eq!($vec4::W, m.w_axis);
274 assert_approx_eq!(
275 m.transform_point3($vec3::new(1.0, 1.0, 1.0)),
276 $vec3::new(2.0, 4.0, 8.0)
277 );
278
279 should_glam_assert!({ $mat4::from_scale($vec3::ZERO) });
280 });
281
282 glam_test!(test_mat4_transpose, {
283 let m = $newmat4(
284 $newvec4(1.0, 2.0, 3.0, 4.0),
285 $newvec4(5.0, 6.0, 7.0, 8.0),
286 $newvec4(9.0, 10.0, 11.0, 12.0),
287 $newvec4(13.0, 14.0, 15.0, 16.0),
288 );
289 let mt = m.transpose();
290 assert_eq!($newvec4(1.0, 5.0, 9.0, 13.0), mt.x_axis);
291 assert_eq!($newvec4(2.0, 6.0, 10.0, 14.0), mt.y_axis);
292 assert_eq!($newvec4(3.0, 7.0, 11.0, 15.0), mt.z_axis);
293 assert_eq!($newvec4(4.0, 8.0, 12.0, 16.0), mt.w_axis);
294 });
295
296 glam_test!(test_mat4_det, {
297 assert_eq!(0.0, $mat4::ZERO.determinant());
298 assert_eq!(1.0, $mat4::IDENTITY.determinant());
299 assert_eq!(1.0, $mat4::from_rotation_x(deg(90.0)).determinant());
300 assert_eq!(1.0, $mat4::from_rotation_y(deg(180.0)).determinant());
301 assert_eq!(1.0, $mat4::from_rotation_z(deg(270.0)).determinant());
302 assert_eq!(
303 2.0 * 2.0 * 2.0,
304 $mat4::from_scale($newvec3(2.0, 2.0, 2.0)).determinant()
305 );
306 assert_eq!(
307 1.0,
308 $newmat4(
309 $newvec4(0.0, 0.0, 0.0, 1.0),
310 $newvec4(1.0, 0.0, 0.0, 0.0),
311 $newvec4(0.0, 0.0, 1.0, 0.0),
312 $newvec4(0.0, 1.0, 0.0, 0.0),
313 )
314 .determinant()
315 );
316 });
317
318 glam_test!(test_mat4_inverse, {
319 // assert_eq!(None, $mat4::ZERO.inverse());
320 let inv = $mat4::IDENTITY.inverse();
321 // assert_ne!(None, inv);
322 assert_approx_eq!($mat4::IDENTITY, inv);
323
324 let rotz = $mat4::from_rotation_z(deg(90.0));
325 let rotz_inv = rotz.inverse();
326 // assert_ne!(None, rotz_inv);
327 // let rotz_inv = rotz_inv.unwrap();
328 assert_approx_eq!($mat4::IDENTITY, rotz * rotz_inv);
329 assert_approx_eq!($mat4::IDENTITY, rotz_inv * rotz);
330
331 let trans = $mat4::from_translation($newvec3(1.0, 2.0, 3.0));
332 let trans_inv = trans.inverse();
333 // assert_ne!(None, trans_inv);
334 // let trans_inv = trans_inv.unwrap();
335 assert_approx_eq!($mat4::IDENTITY, trans * trans_inv);
336 assert_approx_eq!($mat4::IDENTITY, trans_inv * trans);
337
338 let scale = $mat4::from_scale($newvec3(4.0, 5.0, 6.0));
339 let scale_inv = scale.inverse();
340 // assert_ne!(None, scale_inv);
341 // let scale_inv = scale_inv.unwrap();
342 assert_approx_eq!($mat4::IDENTITY, scale * scale_inv);
343 assert_approx_eq!($mat4::IDENTITY, scale_inv * scale);
344
345 let m = scale * rotz * trans;
346 let m_inv = m.inverse();
347 // assert_ne!(None, m_inv);
348 // let m_inv = m_inv.unwrap();
349 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
350 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
351 assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6);
352
353 // Make sure we can invert a "random" matrix:
354 let m = $mat4::from_cols(
355 $newvec4(1.0, -0.3, 1.0, 1.0),
356 $newvec4(0.5, 0.6, 0.7, 0.8),
357 $newvec4(-0.9, -0.3, 0.0, 12.0),
358 $newvec4(0.13, 0.14, 0.15, 0.16),
359 );
360 let m_inv = m.inverse();
361 assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5);
362 assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5);
363
364 should_glam_assert!({ $mat4::ZERO.inverse() });
365 });
366
367 glam_test!(test_mat4_decompose, {
368 // identity
369 let (out_scale, out_rotation, out_translation) =
370 $mat4::IDENTITY.to_scale_rotation_translation();
371 assert_approx_eq!($vec3::ONE, out_scale);
372 assert!(out_rotation.is_near_identity());
373 assert_approx_eq!($vec3::ZERO, out_translation);
374
375 // no scale
376 let in_scale = $vec3::ONE;
377 let in_translation = $vec3::new(-2.0, 4.0, -0.125);
378 let in_rotation = $quat::from_euler(
379 glam::EulerRot::YXZ,
380 $t::to_radians(-45.0),
381 $t::to_radians(180.0),
382 $t::to_radians(270.0),
383 );
384 let in_mat =
385 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
386 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
387 assert_approx_eq!(in_scale, out_scale, 1e-6);
388 // out_rotation is different but produces the same matrix
389 // assert_approx_eq!(in_rotation, out_rotation);
390 assert_approx_eq!(in_translation, out_translation);
391 assert_approx_eq!(
392 in_mat,
393 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
394 1e-6
395 );
396
397 // positive scale
398 let in_scale = $vec3::new(1.0, 2.0, 4.0);
399 let in_mat =
400 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
401 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
402 assert_approx_eq!(in_scale, out_scale, 1e-6);
403 // out_rotation is different but produces the same matrix
404 // assert_approx_eq!(in_rotation, out_rotation);
405 assert_approx_eq!(in_translation, out_translation);
406 assert_approx_eq!(
407 in_mat,
408 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
409 1e-5
410 );
411
412 // negative scale
413 let in_scale = $vec3::new(-4.0, 1.0, 2.0);
414 let in_mat =
415 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
416 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
417 assert_approx_eq!(in_scale, out_scale, 1e-6);
418 // out_rotation is different but produces the same matrix
419 // assert_approx_eq!(in_rotation, out_rotation);
420 assert_approx_eq!(in_translation, out_translation);
421 assert_approx_eq!(
422 in_mat,
423 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
424 1e-5
425 );
426
427 // negative scale
428 let in_scale = $vec3::new(4.0, -1.0, -2.0);
429 let in_mat =
430 $mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation);
431 let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation();
432 // out_scale and out_rotation are different but they produce the same matrix
433 // assert_approx_eq!(in_scale, out_scale, 1e-6);
434 // assert_approx_eq!(in_rotation, out_rotation);
435 assert_approx_eq!(in_translation, out_translation);
436 assert_approx_eq!(
437 in_mat,
438 $mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation),
439 1e-6
440 );
441
442 should_glam_assert!({
443 $mat4::from_scale_rotation_translation(
444 $vec3::ONE,
445 $quat::from_xyzw(0.0, 0.0, 0.0, 0.0),
446 $vec3::ZERO,
447 )
448 });
449 should_glam_assert!({
450 $mat4::from_rotation_translation($quat::from_xyzw(0.0, 0.0, 0.0, 0.0), $vec3::ZERO)
451 });
452 // TODO: should check scale
453 // should_glam_assert!({ $mat4::from_scale_rotation_translation($vec3::ZERO, $quat::IDENTITY, $vec3::ZERO) });
454 should_glam_assert!({ $mat4::ZERO.to_scale_rotation_translation() });
455 });
456
457 glam_test!(test_mat4_look_at, {
458 let eye = $vec3::new(0.0, 0.0, -5.0);
459 let center = $vec3::new(0.0, 0.0, 0.0);
460 let up = $vec3::new(1.0, 0.0, 0.0);
461
462 let point = $vec3::new(1.0, 0.0, 0.0);
463
464 let lh = $mat4::look_at_lh(eye, center, up);
465 let rh = $mat4::look_at_rh(eye, center, up);
466 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
467 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
468
469 let dir = (center - eye).normalize();
470 let lh = $mat4::look_to_lh(eye, dir, up);
471 let rh = $mat4::look_to_rh(eye, dir, up);
472 assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0));
473 assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0));
474
475 should_glam_assert!({ $mat4::look_to_lh($vec3::ONE, $vec3::ONE, $vec3::ZERO) });
476 should_glam_assert!({ $mat4::look_to_lh($vec3::ONE, $vec3::ZERO, $vec3::ONE) });
477 should_glam_assert!({ $mat4::look_to_rh($vec3::ONE, $vec3::ONE, $vec3::ZERO) });
478 should_glam_assert!({ $mat4::look_to_rh($vec3::ONE, $vec3::ZERO, $vec3::ONE) });
479 });
480
481 glam_test!(test_mat4_frustum_gl_rh, {
482 let fov_y_radians = $t::to_radians(90.0);
483 let aspect_ratio = 2.0;
484 let z_near = 5.0;
485 let z_far = 15.0;
486 let f = (0.5 * fov_y_radians).tan();
487 let height = z_near * f;
488 let width = height * aspect_ratio;
489 let projection = $mat4::frustum_rh_gl(-width, width, -height, height, z_near, z_far);
490 let other_projection = $mat4::perspective_rh_gl(fov_y_radians, aspect_ratio, z_near, z_far);
491 assert_approx_eq!(projection, other_projection);
492 });
493
494 glam_test!(test_mat4_frustum_lh, {
495 let fov_y_radians = $t::to_radians(90.0);
496 let aspect_ratio = 2.0;
497 let z_near = 5.0;
498 let z_far = 15.0;
499 let f = (0.5 * fov_y_radians).tan();
500 let height = z_near * f;
501 let width = height * aspect_ratio;
502 let projection = $mat4::frustum_lh(-width, width, -height, height, z_near, z_far);
503 let other_projection = $mat4::perspective_lh(fov_y_radians, aspect_ratio, z_near, z_far);
504 assert_approx_eq!(projection, other_projection);
505 });
506
507 glam_test!(test_mat4_frustum_rh, {
508 let fov_y_radians = $t::to_radians(90.0);
509 let aspect_ratio = 2.0;
510 let z_near = 5.0;
511 let z_far = 15.0;
512 let f = (0.5 * fov_y_radians).tan();
513 let height = z_near * f;
514 let width = height * aspect_ratio;
515 let projection = $mat4::frustum_rh(-width, width, -height, height, z_near, z_far);
516 let other_projection = $mat4::perspective_rh(fov_y_radians, aspect_ratio, z_near, z_far);
517 assert_approx_eq!(projection, other_projection);
518 });
519
520 glam_test!(test_mat4_perspective_gl_rh, {
521 let projection = $mat4::perspective_rh_gl($t::to_radians(90.0), 2.0, 5.0, 15.0);
522
523 let original = $vec3::new(5.0, 5.0, -15.0);
524 let projected = projection * original.extend(1.0);
525 assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected);
526
527 let original = $vec3::new(5.0, 5.0, -5.0);
528 let projected = projection * original.extend(1.0);
529 assert_approx_eq!($vec4::new(2.5, 5.0, -5.0, 5.0), projected);
530 });
531
532 glam_test!(test_mat4_perspective_lh, {
533 let projection = $mat4::perspective_lh($t::to_radians(90.0), 2.0, 5.0, 15.0);
534
535 let original = $vec3::new(5.0, 5.0, 15.0);
536 let projected = projection * original.extend(1.0);
537 assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected, 1e-6);
538
539 let original = $vec3::new(5.0, 5.0, 5.0);
540 let projected = projection * original.extend(1.0);
541 assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected, 1e-6);
542
543 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 1.0, 0.0) });
544 should_glam_assert!({ $mat4::perspective_lh(0.0, 1.0, 0.0, 1.0) });
545 });
546
547 glam_test!(test_mat4_perspective_infinite_lh, {
548 let projection = $mat4::perspective_infinite_lh($t::to_radians(90.0), 2.0, 5.0);
549
550 let original = $vec3::new(5.0, 5.0, 15.0);
551 let projected = projection * original.extend(1.0);
552 assert_approx_eq!($vec4::new(2.5, 5.0, 10.0, 15.0), projected, 1e-6);
553
554 let original = $vec3::new(5.0, 5.0, 5.0);
555 let projected = projection * original.extend(1.0);
556 assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected, 1e-6);
557
558 should_glam_assert!({ $mat4::perspective_infinite_lh(0.0, 1.0, 0.0) });
559 });
560
561 glam_test!(test_mat4_perspective_infinite_reverse_lh, {
562 let projection = $mat4::perspective_infinite_reverse_lh($t::to_radians(90.0), 2.0, 5.0);
563
564 let original = $vec3::new(5.0, 5.0, 15.0);
565 let projected = projection * original.extend(1.0);
566 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 15.0), projected, 1e-6);
567
568 let original = $vec3::new(5.0, 5.0, 5.0);
569 let projected = projection * original.extend(1.0);
570 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 5.0), projected, 1e-6);
571
572 should_glam_assert!({ $mat4::perspective_infinite_reverse_lh(0.0, 1.0, 0.0) });
573 });
574
575 glam_test!(test_mat4_perspective_rh, {
576 let projection = $mat4::perspective_rh($t::to_radians(90.0), 2.0, 5.0, 15.0);
577
578 let original = $vec3::new(5.0, 5.0, 15.0);
579 let projected = projection * original.extend(1.0);
580 assert_approx_eq!($vec4::new(2.5, 5.0, -30.0, -15.0), projected, 1e-6);
581
582 let original = $vec3::new(5.0, 5.0, 5.0);
583 let projected = projection * original.extend(1.0);
584 assert_approx_eq!($vec4::new(2.5, 5.0, -15.0, -5.0), projected, 1e-6);
585
586 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 1.0, 0.0) });
587 should_glam_assert!({ $mat4::perspective_rh(0.0, 1.0, 0.0, 1.0) });
588 });
589
590 glam_test!(test_mat4_perspective_infinite_rh, {
591 let projection = $mat4::perspective_infinite_rh($t::to_radians(90.0), 2.0, 5.0);
592
593 let original = $vec3::new(5.0, 5.0, 15.0);
594 let projected = projection * original.extend(1.0);
595 assert_approx_eq!($vec4::new(2.5, 5.0, -20.0, -15.0), projected);
596
597 let original = $vec3::new(5.0, 5.0, 5.0);
598 let projected = projection * original.extend(1.0);
599 assert_approx_eq!($vec4::new(2.5, 5.0, -10.0, -5.0), projected);
600
601 should_glam_assert!({ $mat4::perspective_infinite_rh(0.0, 1.0, 0.0) });
602 });
603
604 glam_test!(test_mat4_perspective_infinite_reverse_rh, {
605 let projection = $mat4::perspective_infinite_reverse_rh($t::to_radians(90.0), 2.0, 5.0);
606
607 let original = $vec3::new(5.0, 5.0, 15.0);
608 let projected = projection * original.extend(1.0);
609 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -15.0), projected);
610
611 let original = $vec3::new(5.0, 5.0, 5.0);
612 let projected = projection * original.extend(1.0);
613 assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -5.0), projected);
614
615 should_glam_assert!({ $mat4::perspective_infinite_reverse_rh(0.0, 1.0, 0.0) });
616 });
617
618 glam_test!(test_mat4_orthographic_gl_rh, {
619 let projection = $mat4::orthographic_rh_gl(-10.0, 10.0, -5.0, 5.0, 0.0, -10.0);
620 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
621 let projected = projection.mul_vec4(original);
622 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, -2.0, 1.0));
623 });
624
625 glam_test!(test_mat4_orthographic_rh, {
626 let projection = $mat4::orthographic_rh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
627 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
628 let projected = projection.mul_vec4(original);
629 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
630
631 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
632 let projected = projection.mul_vec4(original);
633 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
634 });
635
636 glam_test!(test_mat4_orthographic_lh, {
637 let projection = $mat4::orthographic_lh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0);
638 let original = $vec4::new(5.0, 5.0, -5.0, 1.0);
639 let projected = projection.mul_vec4(original);
640 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0));
641
642 let original = $vec4::new(5.0, 5.0, 5.0, 1.0);
643 let projected = projection.mul_vec4(original);
644 assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0));
645 });
646
647 glam_test!(test_mat4_ops, {
648 let m0 = $mat4::from_cols_array_2d(&ARRAY4X4);
649 let m0x2 = $mat4::from_cols_array_2d(&[
650 [2.0, 4.0, 6.0, 8.0],
651 [10.0, 12.0, 14.0, 16.0],
652 [18.0, 20.0, 22.0, 24.0],
653 [26.0, 28.0, 30.0, 32.0],
654 ]);
655 let m0_neg = $mat4::from_cols_array_2d(&[
656 [-1.0, -2.0, -3.0, -4.0],
657 [-5.0, -6.0, -7.0, -8.0],
658 [-9.0, -10.0, -11.0, -12.0],
659 [-13.0, -14.0, -15.0, -16.0],
660 ]);
661
662 assert_eq!(m0x2, m0 * 2.0);
663 assert_eq!(m0x2, &m0 * 2.0);
664 assert_eq!(m0x2, m0 * &2.0);
665 assert_eq!(m0x2, &m0 * &2.0);
666
667 assert_eq!(m0x2, 2.0 * m0);
668 assert_eq!(m0x2, &2.0 * m0);
669 assert_eq!(m0x2, 2.0 * &m0);
670 assert_eq!(m0x2, &2.0 * &m0);
671
672 assert_eq!(m0, m0x2 / 2.0);
673 assert_eq!(m0, &m0x2 / 2.0);
674 assert_eq!(m0, m0x2 / &2.0);
675 assert_eq!(m0, &m0x2 / &2.0);
676
677 assert_eq!(m0, 2.0 / m0x2);
678 assert_eq!(m0, &2.0 / m0x2);
679 assert_eq!(m0, 2.0 / &m0x2);
680 assert_eq!(m0, &2.0 / &m0x2);
681
682 assert_eq!(m0x2, m0.add_mat4(&m0));
683 assert_eq!(m0x2, m0 + m0);
684 assert_eq!(m0x2, &m0 + m0);
685 assert_eq!(m0x2, m0 + &m0);
686 assert_eq!(m0x2, &m0 + &m0);
687
688 assert_eq!($mat4::ZERO, m0.sub_mat4(&m0));
689 assert_eq!($mat4::ZERO, m0 - m0);
690 assert_eq!($mat4::ZERO, &m0 - m0);
691 assert_eq!($mat4::ZERO, m0 - &m0);
692 assert_eq!($mat4::ZERO, &m0 - &m0);
693
694 assert_eq!(m0_neg, -m0);
695 assert_eq!(m0_neg, -&m0);
696
697 assert_approx_eq!(m0, m0.mul_mat4(&$mat4::IDENTITY));
698 assert_approx_eq!(m0, m0 * $mat4::IDENTITY);
699 assert_approx_eq!(m0, $mat4::IDENTITY * m0);
700 assert_approx_eq!(m0, &$mat4::IDENTITY * m0);
701 assert_approx_eq!(m0, $mat4::IDENTITY * &m0);
702 assert_approx_eq!(m0, &$mat4::IDENTITY * &m0);
703
704 let mut m1 = m0;
705 m1 *= 2.0;
706 assert_eq!(m0x2, m1);
707
708 let mut m1 = m0;
709 m1 *= &2.0;
710 assert_eq!(m0x2, m1);
711
712 let mut m1 = m0x2;
713 m1 /= 2.0;
714 assert_eq!(m0, m1);
715
716 let mut m1 = m0x2;
717 m1 /= &2.0;
718 assert_eq!(m0, m1);
719
720 let mut m1 = m0;
721 m1 += m0;
722 assert_eq!(m0x2, m1);
723
724 let mut m1 = m0;
725 m1 += &m0;
726 assert_eq!(m0x2, m1);
727
728 let mut m1 = m0;
729 m1 -= m0;
730 assert_eq!($mat4::ZERO, m1);
731
732 let mut m1 = m0;
733 m1 -= &m0;
734 assert_eq!($mat4::ZERO, m1);
735
736 let mut m1 = $mat4::IDENTITY;
737 m1 *= m0;
738 assert_eq!(m0, m1);
739
740 let mut m1 = $mat4::IDENTITY;
741 m1 *= &m0;
742 assert_eq!(m0, m1);
743 });
744
745 glam_test!(test_mat4_fmt, {
746 let a = $mat4::from_cols_array_2d(&ARRAY4X4);
747 assert_eq!(
748 format!("{}", a),
749 "[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]"
750 );
751 assert_eq!(
752 format!("{:.1}", a),
753 "[[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]]"
754 );
755 });
756
757 glam_test!(test_mat4_to_from_slice, {
758 let m = $mat4::from_cols_slice(&ARRAY1X16);
759 assert_eq!($mat4::from_cols_array(&ARRAY1X16), m);
760 let mut out: [$t; 16] = Default::default();
761 m.write_cols_to_slice(&mut out);
762 assert_eq!(ARRAY1X16, out);
763
764 should_panic!({ $mat4::from_cols_slice(&[0.0; 15]) });
765 should_panic!({ $mat4::IDENTITY.write_cols_to_slice(&mut [0.0; 15]) });
766 });
767
768 glam_test!(test_sum, {
769 let id = $mat4::IDENTITY;
770 assert_eq!([id, id].iter().sum::<$mat4>(), id + id);
771 assert_eq!([id, id].into_iter().sum::<$mat4>(), id + id);
772 });
773
774 glam_test!(test_product, {
775 let two = $mat4::IDENTITY + $mat4::IDENTITY;
776 assert_eq!([two, two].iter().product::<$mat4>(), two * two);
777 assert_eq!([two, two].into_iter().product::<$mat4>(), two * two);
778 });
779
780 glam_test!(test_mat4_is_finite, {
781 assert!($mat4::IDENTITY.is_finite());
782 assert!(!($mat4::IDENTITY * $t::INFINITY).is_finite());
783 assert!(!($mat4::IDENTITY * $t::NEG_INFINITY).is_finite());
784 assert!(!($mat4::IDENTITY * $t::NAN).is_finite());
785 });
786
787 glam_test!(test_mat4_abs, {
788 let neg = $mat4::IDENTITY * -1.0;
789 assert_eq!(neg.abs(), $mat4::IDENTITY);
790
791 let partial_neg = $mat4::from_cols_array_2d(&[
792 [1.0, -2.0, 3.0, -4.0],
793 [-5.0, 6.0, -7.0, 8.0],
794 [-9.0, 10.0, 11.0, -12.0],
795 [13.0, -14.0, -15.0, 16.0],
796 ]);
797
798 assert_eq!(
799 partial_neg.abs(),
800 $mat4::from_cols_array_2d(&[
801 [1.0, 2.0, 3.0, 4.0],
802 [5.0, 6.0, 7.0, 8.0],
803 [9.0, 10.0, 11.0, 12.0],
804 [13.0, 14.0, 15.0, 16.0],
805 ])
806 );
807 });
808 };
809 }
810
811 macro_rules! impl_as_ref_tests {
812 ($mat:ident) => {
813 glam_test!(test_as_ref, {
814 let m = $mat::from_cols_array_2d(&ARRAY4X4);
815 assert_eq!(ARRAY1X16, *m.as_ref());
816 });
817 glam_test!(test_as_mut, {
818 let mut m = $mat::ZERO;
819 *m.as_mut() = ARRAY1X16;
820 assert_eq!($mat::from_cols_array_2d(&ARRAY4X4), m);
821 });
822 };
823 }
824
825 mod mat4 {
826 use super::support::deg;
827 use glam::{mat4, swizzles::*, vec3, vec4, Mat3, Mat4, Quat, Vec3, Vec4};
828
829 glam_test!(test_align, {
830 use std::mem;
831 assert_eq!(mem::align_of::<Vec4>(), mem::align_of::<Mat4>());
832 assert_eq!(64, mem::size_of::<Mat4>());
833 });
834
835 glam_test!(test_from_mat3a, {
836 use glam::Mat3A;
837 let m3 = Mat3A::from_cols_array_2d(&ARRAY3X3);
838 let m4 = Mat4::from_mat3a(m3);
839 assert_eq!(
840 Mat4::from_cols_array_2d(&[
841 [1.0, 2.0, 3.0, 0.0],
842 [4.0, 5.0, 6.0, 0.0],
843 [7.0, 8.0, 9.0, 0.0],
844 [0.0, 0.0, 0.0, 1.0]
845 ]),
846 m4
847 );
848 });
849
850 glam_test!(test_transform_vec3a, {
851 use glam::Vec3A;
852 let m = Mat4::from_axis_angle(Vec3::Z, deg(90.0));
853 let result3 = m.transform_vector3a(Vec3A::Y);
854 assert_approx_eq!(Vec3A::new(-1.0, 0.0, 0.0), result3);
855
856 let m = Mat4::from_scale_rotation_translation(
857 Vec3::new(0.5, 1.5, 2.0),
858 Quat::from_rotation_x(deg(90.0)),
859 Vec3::new(1.0, 2.0, 3.0),
860 );
861 let result3 = m.transform_vector3a(Vec3A::Y);
862 assert_approx_eq!(Vec3A::new(0.0, 0.0, 1.5), result3, 1.0e-6);
863
864 let result3 = m.transform_point3a(Vec3A::Y);
865 assert_approx_eq!(Vec3A::new(1.0, 2.0, 4.5), result3, 1.0e-6);
866
867 let m = Mat4::from_cols(
868 vec4(8.0, 0.0, 0.0, 0.0),
869 vec4(0.0, 4.0, 0.0, 0.0),
870 vec4(0.0, 0.0, 2.0, 2.0),
871 vec4(0.0, 0.0, 0.0, 0.0),
872 );
873 assert_approx_eq!(
874 Vec3A::new(4.0, 2.0, 1.0),
875 m.project_point3a(Vec3A::new(2.0, 2.0, 2.0))
876 );
877 });
878
879 glam_test!(test_as, {
880 use glam::DMat4;
881 assert_eq!(
882 DMat4::from_cols_array_2d(&[
883 [1.0, 2.0, 3.0, 4.0],
884 [5.0, 6.0, 7.0, 8.0],
885 [9.0, 10.0, 11.0, 12.0],
886 [13.0, 14.0, 15.0, 16.0]
887 ]),
888 Mat4::from_cols_array_2d(&[
889 [1.0, 2.0, 3.0, 4.0],
890 [5.0, 6.0, 7.0, 8.0],
891 [9.0, 10.0, 11.0, 12.0],
892 [13.0, 14.0, 15.0, 16.0]
893 ])
894 .as_dmat4()
895 );
896 assert_eq!(
897 Mat4::from_cols_array_2d(&[
898 [1.0, 2.0, 3.0, 4.0],
899 [5.0, 6.0, 7.0, 8.0],
900 [9.0, 10.0, 11.0, 12.0],
901 [13.0, 14.0, 15.0, 16.0]
902 ]),
903 DMat4::from_cols_array_2d(&[
904 [1.0, 2.0, 3.0, 4.0],
905 [5.0, 6.0, 7.0, 8.0],
906 [9.0, 10.0, 11.0, 12.0],
907 [13.0, 14.0, 15.0, 16.0]
908 ])
909 .as_mat4()
910 );
911 });
912
913 impl_mat4_tests!(f32, mat4, vec4, vec3, Mat4, Mat3, Quat, Vec4, Vec3);
914 impl_as_ref_tests!(Mat4);
915 }
916
917 mod dmat4 {
918 use super::support::deg;
919 use glam::{dmat4, dvec3, dvec4, swizzles::*, DMat3, DMat4, DQuat, DVec3, DVec4};
920
921 glam_test!(test_align, {
922 use std::mem;
923 assert_eq!(mem::align_of::<DVec4>(), mem::align_of::<DMat4>());
924 assert_eq!(128, mem::size_of::<DMat4>());
925 });
926
927 impl_mat4_tests!(f64, dmat4, dvec4, dvec3, DMat4, DMat3, DQuat, DVec4, DVec3);
928 impl_as_ref_tests!(DMat4);
929 }