1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#include "mons_math/mat3.h"
#include "mons_math/mat2.h"
#include "mons_math/vec3.h"
const mons_mat3 MONS_MAT3_ZERO = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
};
const mons_mat3 MONS_MAT3_IDENTITY = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
};
mons_mat3 mons_mat3_add(mons_mat3 a, mons_mat3 b) {
mons_mat3 result = {
mons_vec3_add(a.m1, b.m1),
mons_vec3_add(a.m2, b.m2),
mons_vec3_add(a.m3, b.m3),
};
return result;
}
void mons_mat3_add_inplace(mons_mat3 *a, mons_mat3 b) {
mons_vec3_add_inplace(&a->m1, b.m1);
mons_vec3_add_inplace(&a->m2, b.m2);
mons_vec3_add_inplace(&a->m3, b.m3);
}
mons_mat3 mons_mat3_mul_f(mons_mat3 a, float b) {
mons_mat3 result = {
mons_vec3_mul_f(a.m1, b),
mons_vec3_mul_f(a.m2, b),
mons_vec3_mul_f(a.m3, b),
};
return result;
}
mons_mat3 mons_mat3_mul_i(mons_mat3 a, int b) {
mons_mat3 result = {
mons_vec3_mul_i(a.m1, b),
mons_vec3_mul_i(a.m2, b),
mons_vec3_mul_i(a.m3, b),
};
return result;
}
mons_mat3 mons_mat3_mul(mons_mat3 a, mons_mat3 b) {
mons_vec3 b_n1 = mons_mat3_n1(b);
mons_vec3 b_n2 = mons_mat3_n2(b);
mons_vec3 b_n3 = mons_mat3_n3(b);
mons_mat3 result = {
{
mons_vec3_dot(a.m1, b_n1),
mons_vec3_dot(a.m1, b_n2),
mons_vec3_dot(a.m1, b_n3),
},
{
mons_vec3_dot(a.m2, b_n1),
mons_vec3_dot(a.m2, b_n2),
mons_vec3_dot(a.m2, b_n3),
},
{
mons_vec3_dot(a.m3, b_n1),
mons_vec3_dot(a.m3, b_n2),
mons_vec3_dot(a.m3, b_n3),
},
};
return result;
}
void mons_mat3_mul_f_inplace(mons_mat3 *a, float b) {
mons_vec3_mul_f_inplace(&a->m1, b);
mons_vec3_mul_f_inplace(&a->m2, b);
mons_vec3_mul_f_inplace(&a->m3, b);
}
void mons_mat3_mul_i_inplace(mons_mat3 *a, int b) {
mons_vec3_mul_i_inplace(&a->m1, b);
mons_vec3_mul_i_inplace(&a->m2, b);
mons_vec3_mul_i_inplace(&a->m3, b);
}
mons_mat3 mons_mat3_transpose(mons_mat3 a) {
mons_mat3 result = {
mons_mat3_n1(a),
mons_mat3_n2(a),
mons_mat3_n3(a),
};
}
void mons_mat3_transpose_inplace(mons_mat3 *a) { *a = mons_mat3_transpose(*a); }
mons_vec3 mons_mat3_n1(mons_mat3 a) {
mons_vec3 result = {
a.m1.x,
a.m2.x,
a.m3.x,
};
return result;
}
mons_vec3 mons_mat3_n2(mons_mat3 a) {
mons_vec3 result = {
a.m1.y,
a.m2.y,
a.m3.y,
};
return result;
}
mons_vec3 mons_mat3_n3(mons_mat3 a) {
mons_vec3 result = {
a.m1.z,
a.m2.z,
a.m3.z,
};
return result;
}
float mons_mat3_determinant(mons_mat3 a) {
/*
* a * det(
* e f
* h i
* )
* - b * det(
* d f
* g i
* )
* + c * det(
* d e
* g h
* )
*/
mons_mat2 efhi = {
{a.m2.y, a.m2.z},
{a.m3.y, a.m3.z},
};
mons_mat2 dfgi = {
{a.m2.x, a.m2.z},
{a.m3.x, a.m3.z},
};
mons_mat2 degh = {
{a.m2.x, a.m2.y},
{a.m3.x, a.m3.y},
};
return (a.m1.x * mons_mat2_determinant(efhi)) -
(a.m1.y * mons_mat2_determinant(dfgi)) +
(a.m1.z * mons_mat2_determinant(degh));
}
mons_mat3 mons_mat3_minor(mons_mat3 a) {
// Get determinants of minors
// First row elements
mons_mat2 minor_1_1 = {
{a.m2.y, a.m2.z},
{a.m3.y, a.m3.z},
};
float det_1_1 = mons_mat2_determinant(minor_1_1);
mons_mat2 minor_1_2 = {
{a.m2.x, a.m2.z},
{a.m3.x, a.m3.z},
};
float det_1_2 = mons_mat2_determinant(minor_1_2);
mons_mat2 minor_1_3 = {
{a.m2.x, a.m2.y},
{a.m3.x, a.m3.y},
};
float det_1_3 = mons_mat2_determinant(minor_1_3);
// Second row elements
mons_mat2 minor_2_1 = {
{a.m1.y, a.m1.z},
{a.m3.y, a.m3.z},
};
float det_2_1 = mons_mat2_determinant(minor_2_1);
mons_mat2 minor_2_2 = {
{a.m1.x, a.m1.z},
{a.m3.x, a.m3.z},
};
float det_2_2 = mons_mat2_determinant(minor_2_2);
mons_mat2 minor_2_3 = {
{a.m1.x, a.m1.y},
{a.m3.x, a.m3.y},
};
float det_2_3 = mons_mat2_determinant(minor_2_3);
// Third row elements
mons_mat2 minor_3_1 = {
{a.m1.y, a.m1.z},
{a.m2.y, a.m2.z},
};
float det_3_1 = mons_mat2_determinant(minor_3_1);
mons_mat2 minor_3_2 = {
{a.m1.x, a.m1.z},
{a.m2.x, a.m2.z},
};
float det_3_2 = mons_mat2_determinant(minor_3_2);
mons_mat2 minor_3_3 = {
{a.m1.x, a.m1.y},
{a.m2.x, a.m2.y},
};
float det_3_3 = mons_mat2_determinant(minor_3_3);
mons_mat3 result = {
{det_1_1, det_1_2, det_1_3},
{det_2_1, det_2_2, det_2_3},
{det_3_1, det_3_2, det_3_3},
};
return result;
}
mons_mat3 mons_mat3_cofactor(mons_mat3 a) {
mons_mat3 result = mons_mat3_minor(a);
result.m1.y *= -1;
result.m2.x *= -1;
result.m2.z *= -1;
result.m3.y *= -1;
return result;
}
mons_mat3 mons_mat3_adjoint(mons_mat3 a) {
return mons_mat3_transpose(mons_mat3_cofactor(a));
}
mons_mat3 mons_mat3_inverse(mons_mat3 a) {
return mons_mat3_mul_f(mons_mat3_adjoint(a),
1.0 / mons_mat3_determinant(a));
}
int mons_mat3_equal(mons_mat3 a, mons_mat3 b) {
return mons_vec3_equal(a.m1, b.m1) && mons_vec3_equal(a.m2, b.m2) &&
mons_vec3_equal(a.m3, b.m3);
}
|