aboutsummaryrefslogtreecommitdiff
path: root/mons_math/src/mat4.c
blob: bb92ca8bc24c010827840ffa9fe22ad30386d1ac (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "mons_math/mat4.h"
#include "mons_math/mat3.h"
#include "mons_math/vec4.h"
#include <stdio.h>

mons_mat4 mons_mat4_add(mons_mat4 a, mons_mat4 b) {
    mons_mat4 result = {
        mons_vec4_add(a.m1, b.m1),
        mons_vec4_add(a.m2, b.m2),
        mons_vec4_add(a.m3, b.m3),
        mons_vec4_add(a.m4, b.m4),
    };
    return result;
}

void mons_mat4_add_inplace(mons_mat4 *a, mons_mat4 b) {
    mons_vec4_add_inplace(&a->m1, b.m1);
    mons_vec4_add_inplace(&a->m2, b.m2);
    mons_vec4_add_inplace(&a->m3, b.m3);
    mons_vec4_add_inplace(&a->m4, b.m4);
}

mons_mat4 mons_mat4_mul_f(mons_mat4 a, float b) {
    mons_mat4 result = {
        mons_vec4_mul_f(a.m1, b),
        mons_vec4_mul_f(a.m2, b),
        mons_vec4_mul_f(a.m3, b),
        mons_vec4_mul_f(a.m4, b),
    };
    return result;
}

mons_mat4 mons_mat4_mul_i(mons_mat4 a, int b) {
    mons_mat4 result = {
        mons_vec4_mul_i(a.m1, b),
        mons_vec4_mul_i(a.m2, b),
        mons_vec4_mul_i(a.m3, b),
        mons_vec4_mul_i(a.m4, b),
    };
    return result;
}

void mons_mat4_mul_f_inplace(mons_mat4 *a, float b) {
    mons_vec4_mul_f_inplace(&a->m1, b);
    mons_vec4_mul_f_inplace(&a->m2, b);
    mons_vec4_mul_f_inplace(&a->m3, b);
    mons_vec4_mul_f_inplace(&a->m4, b);
}

void mons_mat4_mul_i_inplace(mons_mat4 *a, int b) {
    mons_vec4_mul_i_inplace(&a->m1, b);
    mons_vec4_mul_i_inplace(&a->m2, b);
    mons_vec4_mul_i_inplace(&a->m3, b);
    mons_vec4_mul_i_inplace(&a->m4, b);
}

mons_mat4 mons_mat4_mul(mons_mat4 a, mons_mat4 b) {
    mons_vec4 b_n1 = mons_mat4_n1(b);
    mons_vec4 b_n2 = mons_mat4_n2(b);
    mons_vec4 b_n3 = mons_mat4_n3(b);
    mons_vec4 b_n4 = mons_mat4_n4(b);

    mons_mat4 result = {
        {
            mons_vec4_dot(a.m1, b_n1),
            mons_vec4_dot(a.m1, b_n2),
            mons_vec4_dot(a.m1, b_n3),
            mons_vec4_dot(a.m1, b_n4),
        },
        {
            mons_vec4_dot(a.m2, b_n1),
            mons_vec4_dot(a.m2, b_n2),
            mons_vec4_dot(a.m2, b_n3),
            mons_vec4_dot(a.m2, b_n4),
        },
        {
            mons_vec4_dot(a.m3, b_n1),
            mons_vec4_dot(a.m3, b_n2),
            mons_vec4_dot(a.m3, b_n3),
            mons_vec4_dot(a.m3, b_n4),
        },
        {
            mons_vec4_dot(a.m4, b_n1),
            mons_vec4_dot(a.m4, b_n2),
            mons_vec4_dot(a.m4, b_n3),
            mons_vec4_dot(a.m4, b_n4),
        },
    };
    return result;
}

mons_mat4 mons_mat4_transpose(mons_mat4 a) {
    mons_mat4 result = {
        mons_mat4_n1(a),
        mons_mat4_n2(a),
        mons_mat4_n3(a),
        mons_mat4_n4(a),
    };
    return result;
}

void mons_mat4_transpose_inplace(mons_mat4 *a) { *a = mons_mat4_transpose(*a); }

mons_vec4 mons_mat4_n1(mons_mat4 a) {
    mons_vec4 result = {
        a.m1.x,
        a.m2.x,
        a.m3.x,
        a.m4.x,
    };
    return result;
}

mons_vec4 mons_mat4_n2(mons_mat4 a) {
    mons_vec4 result = {
        a.m1.y,
        a.m2.y,
        a.m3.y,
        a.m4.y,
    };
    return result;
}

mons_vec4 mons_mat4_n3(mons_mat4 a) {
    mons_vec4 result = {
        a.m1.z,
        a.m2.z,
        a.m3.z,
        a.m4.z,
    };
    return result;
}

mons_vec4 mons_mat4_n4(mons_mat4 a) {
    mons_vec4 result = {
        a.m1.w,
        a.m2.w,
        a.m3.w,
        a.m4.w,
    };
    return result;
}

float mons_mat4_determinant(mons_mat4 a) {
    mons_mat3 minor_1_1 = {
        {a.m2.y, a.m2.z, a.m2.w},
        {a.m3.y, a.m3.z, a.m3.w},
        {a.m4.y, a.m4.z, a.m4.w},
    };
    mons_mat3 minor_1_2 = {
        {a.m2.x, a.m2.z, a.m2.w},
        {a.m3.x, a.m3.z, a.m3.w},
        {a.m4.x, a.m4.z, a.m4.w},
    };
    mons_mat3 minor_1_3 = {
        {a.m2.x, a.m2.y, a.m2.w},
        {a.m3.x, a.m3.y, a.m3.w},
        {a.m4.x, a.m4.y, a.m4.w},
    };
    mons_mat3 minor_1_4 = {
        {a.m2.x, a.m2.y, a.m2.z},
        {a.m3.x, a.m3.y, a.m3.z},
        {a.m4.x, a.m4.y, a.m4.z},
    };

    return (a.m1.x * mons_mat3_determinant(minor_1_1)) -
           (a.m1.y * mons_mat3_determinant(minor_1_2)) +
           (a.m1.z * mons_mat3_determinant(minor_1_3)) -
           (a.m1.w * mons_mat3_determinant(minor_1_4));
}

mons_mat4 mons_mat4_minor(mons_mat4 a) {
    // First Row
    mons_mat3 minor_1_1 = {
        {a.m2.y, a.m2.z, a.m2.w},
        {a.m3.y, a.m3.z, a.m3.w},
        {a.m4.y, a.m4.z, a.m4.w},
    };
    float det_1_1 = mons_mat3_determinant(minor_1_1);
    mons_mat3 minor_1_2 = {
        {a.m2.x, a.m2.z, a.m2.w},
        {a.m3.x, a.m3.z, a.m3.w},
        {a.m4.x, a.m4.z, a.m4.w},
    };
    float det_1_2 = mons_mat3_determinant(minor_1_2);
    mons_mat3 minor_1_3 = {
        {a.m2.x, a.m2.y, a.m2.w},
        {a.m3.x, a.m3.y, a.m3.w},
        {a.m4.x, a.m4.y, a.m4.w},
    };
    float det_1_3 = mons_mat3_determinant(minor_1_3);
    mons_mat3 minor_1_4 = {
        {a.m2.x, a.m2.y, a.m2.z},
        {a.m3.x, a.m3.y, a.m3.z},
        {a.m4.x, a.m4.y, a.m4.z},
    };
    float det_1_4 = mons_mat3_determinant(minor_1_4);
    // Second Row
    mons_mat3 minor_2_1 = {
        {a.m1.y, a.m1.z, a.m1.w},
        {a.m3.y, a.m3.z, a.m3.w},
        {a.m4.y, a.m4.z, a.m4.w},
    };
    float det_2_1 = mons_mat3_determinant(minor_2_1);
    mons_mat3 minor_2_2 = {
        {a.m1.x, a.m1.z, a.m1.w},
        {a.m3.x, a.m3.z, a.m3.w},
        {a.m4.x, a.m4.z, a.m4.w},
    };
    float det_2_2 = mons_mat3_determinant(minor_2_2);
    mons_mat3 minor_2_3 = {
        {a.m1.x, a.m1.y, a.m1.w},
        {a.m3.x, a.m3.y, a.m3.w},
        {a.m4.x, a.m4.y, a.m4.w},
    };
    float det_2_3 = mons_mat3_determinant(minor_2_3);
    mons_mat3 minor_2_4 = {
        {a.m1.x, a.m1.y, a.m1.z},
        {a.m3.x, a.m3.y, a.m3.z},
        {a.m4.x, a.m4.y, a.m4.z},
    };
    float det_2_4 = mons_mat3_determinant(minor_2_4);
    // Third Row
    mons_mat3 minor_3_1 = {
        {a.m1.y, a.m1.z, a.m1.w},
        {a.m2.y, a.m2.z, a.m2.w},
        {a.m4.y, a.m4.z, a.m4.w},
    };
    float det_3_1 = mons_mat3_determinant(minor_3_1);
    mons_mat3 minor_3_2 = {
        {a.m1.x, a.m1.z, a.m1.w},
        {a.m2.x, a.m2.z, a.m2.w},
        {a.m4.x, a.m4.z, a.m4.w},
    };
    float det_3_2 = mons_mat3_determinant(minor_3_2);
    mons_mat3 minor_3_3 = {
        {a.m1.x, a.m1.y, a.m1.w},
        {a.m2.x, a.m2.y, a.m2.w},
        {a.m4.x, a.m4.y, a.m4.w},
    };
    float det_3_3 = mons_mat3_determinant(minor_3_3);
    mons_mat3 minor_3_4 = {
        {a.m1.x, a.m1.y, a.m1.z},
        {a.m2.x, a.m2.y, a.m2.z},
        {a.m4.x, a.m4.y, a.m4.z},
    };
    float det_3_4 = mons_mat3_determinant(minor_3_4);
    // Fourth Row
    mons_mat3 minor_4_1 = {
        {a.m1.y, a.m1.z, a.m1.w},
        {a.m2.y, a.m2.z, a.m2.w},
        {a.m3.y, a.m3.z, a.m3.w},
    };
    float det_4_1 = mons_mat3_determinant(minor_4_1);
    mons_mat3 minor_4_2 = {
        {a.m1.x, a.m1.z, a.m1.w},
        {a.m2.x, a.m2.z, a.m2.w},
        {a.m3.x, a.m3.z, a.m3.w},
    };
    float det_4_2 = mons_mat3_determinant(minor_4_2);
    mons_mat3 minor_4_3 = {
        {a.m1.x, a.m1.y, a.m1.w},
        {a.m2.x, a.m2.y, a.m2.w},
        {a.m3.x, a.m3.y, a.m3.w},
    };
    float det_4_3 = mons_mat3_determinant(minor_4_3);
    mons_mat3 minor_4_4 = {
        {a.m1.x, a.m1.y, a.m1.z},
        {a.m2.x, a.m2.y, a.m2.z},
        {a.m3.x, a.m3.y, a.m3.z},
    };
    float det_4_4 = mons_mat3_determinant(minor_4_4);

    mons_mat4 result = {
        {det_1_1, det_1_2, det_1_3, det_1_4},
        {det_2_1, det_2_2, det_2_3, det_2_4},
        {det_3_1, det_3_2, det_3_3, det_3_4},
        {det_4_1, det_4_2, det_4_3, det_4_4},
    };
    return result;
}

mons_mat4 mons_mat4_cofactor(mons_mat4 a) {
    mons_mat4 result = mons_mat4_minor(a);
    result.m1.y *= -1;
    result.m1.w *= -1;
    result.m2.x *= -1;
    result.m2.z *= -1;
    result.m3.y *= -1;
    result.m3.w *= -1;
    result.m4.x *= -1;
    result.m4.z *= -1;
    return result;
}

mons_mat4 mons_mat4_adjoint(mons_mat4 a) {
    return mons_mat4_transpose(mons_mat4_cofactor(a));
}

mons_mat4 mons_mat4_inverse(mons_mat4 a) {
    return mons_mat4_mul_f(mons_mat4_adjoint(a),
                           1.0 / mons_mat4_determinant(a));
}

int mons_mat4_equal(mons_mat4 a, mons_mat4 b) {
    return mons_vec4_equal(a.m1, b.m1)
        && mons_vec4_equal(a.m2, b.m2)
        && mons_vec4_equal(a.m3, b.m3)
        && mons_vec4_equal(a.m4, b.m4);
}

void mons_mat4_mul_inplace(mons_mat4 *a, mons_mat4 b) {
    *a = mons_mat4_mul(*a, b);
}

void mons_mat4_print(mons_mat4 mat) {
    printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.x, mat.m2.x, mat.m3.x, mat.m4.x);
    printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.y, mat.m2.y, mat.m3.y, mat.m4.y);
    printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.z, mat.m2.z, mat.m3.z, mat.m4.z);
    printf("[%.3f %.3f %.3f %.3f]\n", mat.m1.w, mat.m2.w, mat.m3.w, mat.m4.w);
}