]> git.lizzy.rs Git - nothing.git/blob - src/math/mat3x3.h
Merge pull request #1042 from tsoding/999
[nothing.git] / src / math / mat3x3.h
1 #ifndef MAT3X3_H_
2 #define MAT3X3_H_
3
4 #include "point.h"
5
6 typedef struct mat3x3 {
7     float M[3][3];
8 } mat3x3;
9
10 static inline
11 mat3x3 make_mat3x3(float a11, float a12, float a13,
12                    float a21, float a22, float a23,
13                    float a31, float a32, float a33)
14 {
15     const mat3x3 m = {
16         .M = {
17             {a11, a12, a13},
18             {a21, a22, a23},
19             {a31, a32, a33}
20         }
21     };
22
23     return m;
24 }
25
26 static inline
27 mat3x3 mat3x3_product(mat3x3 m1, mat3x3 m2)
28 {
29     mat3x3 result;
30
31     for (int i = 0; i < 3; ++i) {
32         for (int j = 0; j < 3; ++j) {
33             result.M[i][j] = 0;
34             for (int k = 0; k < 3; ++k) {
35                 result.M[i][j] += m1.M[i][k] * m2.M[k][j];
36             }
37         }
38     }
39
40     return result;
41 }
42
43 static inline
44 mat3x3 mat3x3_product2(mat3x3 m1, mat3x3 m2, mat3x3 m3)
45 {
46     return mat3x3_product(m1, mat3x3_product(m2, m3));
47 }
48
49 static inline
50 mat3x3 trans_mat(float x, float y)
51 {
52     const mat3x3 m = {
53         .M = {
54             {1.0f, 0.0f, x},
55             {0.0f, 1.0f, y},
56             {0.0f, 0.0f, 1.0f}
57         }
58     };
59
60     return m;
61 }
62
63 static inline
64 mat3x3 trans_mat_vec(Vec v)
65 {
66     return trans_mat(v.x, v.y);
67 }
68
69 static inline
70 mat3x3 rot_mat(float angle)
71 {
72     const mat3x3 m = {
73         .M = {
74             {cosf(angle), -sinf(angle), 0.0f},
75             {sinf(angle), cosf(angle), 0.0f},
76             {0.0f, 0.0f, 1.0f}
77         }
78     };
79
80     return m;
81 }
82
83 static inline
84 mat3x3 scale_mat(float factor)
85 {
86     const mat3x3 m = {
87         .M = {
88             {factor, 0.0f, 0.0f},
89             {0.0f, factor, 0.0f},
90             {0.0f, 0.0f, 1.0f}
91         }
92     };
93
94     return m;
95 }
96
97 static inline
98 Point point_mat3x3_product(Point p, mat3x3 m)
99 {
100     /* Convert p to Homogeneous coordinates */
101     const float homo_p[3] = {p.x, p.y, 1};
102
103     /* Transform p with matrix m */
104     const float trans_p[3] = {
105         homo_p[0] * m.M[0][0] + homo_p[1] * m.M[0][1] + homo_p[2] * m.M[0][2],
106         homo_p[0] * m.M[1][0] + homo_p[1] * m.M[1][1] + homo_p[2] * m.M[1][2],
107         homo_p[0] * m.M[2][0] + homo_p[1] * m.M[2][1] + homo_p[2] * m.M[2][2]
108     };
109
110     /* Convert p back to Cartesian coordinates */
111     const Point result_p = {
112         .x = trans_p[0] / trans_p[2],
113         .y = trans_p[1] / trans_p[2]
114     };
115
116     return result_p;
117 }
118
119 static inline
120 Triangle triangle_mat3x3_product(Triangle t, mat3x3 m)
121 {
122     Triangle t1 = {
123         .p1 = point_mat3x3_product(t.p1, m),
124         .p2 = point_mat3x3_product(t.p2, m),
125         .p3 = point_mat3x3_product(t.p3, m)
126     };
127
128     return t1;
129 }
130
131 #endif  // MAT3X3_H_