1use vecmath as vm;
2
3use super::{Vector2, Vector3, Vector4};
4mod macros;
5
6
7pub fn mat3<T: Copy>() -> Matrix3<T>
9 where T: vm::traits::Zero + vm::traits::One
10{
11 Matrix3::id()
12}
13
14pub fn mat4<T: Copy>() -> Matrix4<T>
16 where T: vm::traits::Zero + vm::traits::One
17{
18 Matrix4::id()
19}
20
21
22impl_matrix_type!(Matrix3, 3, Vector3, Vector2, {
23 id_func: mat3_id,
24 inv_func: mat3_inv,
25 det_func: mat3_det,
26 transpose_func: mat3_transposed,
27 mat_mul_func: col_mat3_mul,
28 vec_mul_func: col_mat3_transform
29});
30
31impl_matrix_type!(Matrix4, 4, Vector4, Vector3, {
32 id_func: mat4_id,
33 inv_func: mat4_inv,
34 det_func: mat4_det,
35 transpose_func: mat4_transposed,
36 mat_mul_func: col_mat4_mul,
37 vec_mul_func: col_mat4_transform
38});
39
40#[cfg(test)]
41mod tests
42{
43 use super::*;
44
45 #[test]
46 fn construction()
47 {
48 let a = Matrix3::new([
49 [1.0, 0.0, 0.0],
50 [0.0, 1.0, 0.0],
51 [0.0, 0.0, 1.0_f32]]);
52 let b = Matrix3::<f32>::id();
53 let c = Matrix3::new_scale(Vector3::new([1.0, 1.0, 1.0_f32]));
54 assert_eq!(a, b);
55 assert_eq!(b, c);
56 assert_eq!(c, a);
57 }
58
59 #[test]
60 fn multiplication_3d()
61 {
62 let v = Vector3::new([1.0, 2.0, 1.0]);
63 let m0 = Matrix3::new_scale(Vector3::new([2.0, 2.0, 1.0]));
64 let m1 = Matrix3::new_translation(Vector2::new([4.0, 4.0]));
65 let mm = m1 * m0;
66 assert_eq!(m0 * v, Vector3::new([2.0, 4.0, 1.0]));
67 assert_eq!(m1 * v, Vector3::new([5.0, 6.0, 1.0]));
68 assert_eq!(mm * v, Vector3::new([6.0, 8.0, 1.0]));
69 }
70
71 #[test]
72 fn multiplication_4d()
73 {
74 let u = Vector4::new([1.0, 2.0, 3.0, 1.0]);
75 let v = Vector4::new([1.0, 2.0, 3.0, 0.0]);
76 let m = Matrix4::new_translation(Vector3::new([2.0, 3.0, 4.0]));
77 assert_eq!(m * u, Vector4::new([3.0, 5.0, 7.0, 1.0]));
78 assert_eq!(m * v, Vector4::new([1.0, 2.0, 3.0, 0.0]));
79 }
80
81 #[test]
82 fn determinant()
83 {
84 let m = Matrix3::new_scale(Vector3::new([2.0, 2.0, 2.0]));
85 assert_eq!(m.det(), 8.0);
86 }
87
88 #[test]
89 fn inverse()
90 {
91 let m = Matrix3::new_scale(Vector3::new([2.0, 2.0, 2.0]));
92 let im = Matrix3::new_scale(Vector3::new([0.5, 0.5, 0.5]));
93 assert_eq!(m.inv(), im);
94 }
95
96 #[test]
97 fn transpose()
98 {
99 let m = Matrix3::new([
100 [1.0, 2.0, 3.0],
101 [4.0, 5.0, 6.0],
102 [7.0, 8.0, 9.0]]);
103 let mt = Matrix3::new([
104 [1.0, 4.0, 7.0],
105 [2.0, 5.0, 8.0],
106 [3.0, 6.0, 9.0]]);
107 assert_eq!(m.transpose(), mt);
108 }
109
110 #[test]
111 fn transformation_building()
112 {
113 let m = Matrix3::id().scale(Vector3::new([2.0, 3.0, 1.0])).translate(Vector2::new([1.0, 1.0]));
114 let v = Vector3::new([1.0, 1.0, 1.0]);
115 assert_eq!(m * v, Vector3::new([3.0, 4.0, 1.0]));
116 }
117}