webgl_matrix/
vec4.rs

1#[cfg(feature = "Matrix4")]
2use crate::mat4::Mat4;
3use crate::slice_ops::*;
4#[cfg(feature = "Matrix4")]
5use crate::vector::MulVectorMatrix;
6use crate::vector::Vector;
7use std::f32;
8
9pub type Vec4 = [f32; 4];
10
11impl_vector!(Vec4, 4);
12
13#[cfg(feature = "Matrix4")]
14impl MulVectorMatrix<Mat4> for Vec4 {
15    type VectorType = Vec4;
16
17    fn mul_matrix_left(&self, lhs: &Mat4) -> Self::VectorType {
18        let x = self[0];
19        let y = self[1];
20        let z = self[2];
21        let w = self[3];
22
23        [
24            lhs[0] * x + lhs[1] * y + lhs[2] * z + lhs[3] * w,
25            lhs[4] * x + lhs[5] * y + lhs[6] * z + lhs[7] * w,
26            lhs[8] * x + lhs[9] * y + lhs[10] * z + lhs[11] * w,
27            lhs[12] * x + lhs[13] * y + lhs[14] * z + lhs[15] * w,
28        ]
29    }
30
31    fn mul_matrix(&self, rhs: &Mat4) -> Self::VectorType {
32        let x = self[0];
33        let y = self[1];
34        let z = self[2];
35        let w = self[3];
36
37        [
38            rhs[0] * x + rhs[4] * y + rhs[8] * z + rhs[12] * w,
39            rhs[1] * x + rhs[5] * y + rhs[9] * z + rhs[13] * w,
40            rhs[2] * x + rhs[6] * y + rhs[10] * z + rhs[14] * w,
41            rhs[3] * x + rhs[7] * y + rhs[11] * z + rhs[15] * w,
42        ]
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::utils::almost_eq;
50
51    #[test]
52    #[cfg(feature = "Matrix4")]
53    fn vec4_mul_matrix_left() {
54        let a = [
55            1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.,
56        ];
57        let b = [17., 18., 19., 20.];
58
59        let c = b.mul_matrix_left(&a);
60        assert_eq!(c, [190., 486., 782., 1078.]);
61    }
62
63    #[test]
64    #[cfg(feature = "Matrix4")]
65    fn vec4_mul_matrix() {
66        let a = [
67            1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.,
68        ];
69        let b = [17., 18., 19., 20.];
70
71        let c = b.mul_matrix(&a);
72        assert_eq!(c, [538., 612., 686., 760.]);
73    }
74
75    #[test]
76    fn vec4_add() {
77        let a = [1., 2., 3., 4.];
78        let b = [-1., -2., -3., -4.];
79
80        assert_eq!(a.add(&b), [0., 0., 0., 0.]);
81    }
82
83    #[test]
84    fn vec4_sub() {
85        let a = [1., 2., 3., 4.];
86        let b = [1., 2., 3., 4.];
87
88        assert_eq!(a.sub(&b), [0., 0., 0., 0.]);
89    }
90
91    #[test]
92    fn vec4_mul() {
93        let a = [1., 2., 3., 4.];
94        let b = [2., 3., 4., 5.];
95
96        assert_eq!(a.mul(&b), [2., 6., 12., 20.]);
97    }
98
99    #[test]
100    fn vec4_scale() {
101        let a = [1., 2., 3., 4.];
102
103        assert_eq!(a.scale(3.), [3., 6., 9., 12.]);
104    }
105
106    #[test]
107    fn vec4_dot() {
108        let a = [1., 2., 3., 4.];
109        let b = [2., 3., 4., 5.];
110
111        assert_eq!(a.dot(&b), 2. + 6. + 12. + 20.);
112    }
113    #[test]
114    fn vec4_mag() {
115        let b = [2., 3., 4., 5.];
116        assert!(almost_eq(&[b.mag()], &[7.3484693]));
117    }
118    #[test]
119    fn vec4_mag2() {
120        let b = [2., 3., 4., 5.];
121        assert!(almost_eq(&[b.mag2()], &[54.]));
122    }
123}