forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
349 lines (293 loc) · 10.2 KB
/
matrix.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright (C) 2010 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <math.h>
#define DEFINE_MATRIX_GLOBALS
#include "matrix.h"
/* Matrices need to be row-major and use row-major memory layout with
* pre-multiplication
*
* OR
*
* Matrices need to be column major and use column-major memory layout
* with post-multiplication.
*
* In row major storage adjacent columns are adjacent in memory and a
* row is contiguous, and a column is not contiguous. In column major
* storage, adjacent rows are adjacent in memory, and columns are
* contiguous, and rows are not contiguous.
*
* This code is based on a very nice explanation found here:
* http://www.kmjn.org/notes/3d_rendering_intro.html by Mark J. Nelson
*
*/
void mat44_product(const struct mat44 *lhs, const struct mat44 *rhs,
struct mat44 *output)
{
int i, j, k;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
output->m[i][j] = 0;
for (k = 0; k < 4; k++)
output->m[i][j] += lhs->m[k][j] * rhs->m[i][k];
}
}
}
/* for post muliplication, mat44 must be column major and stored column major order */
void mat44_x_mat41(const struct mat44 *lhs, const struct mat41 *rhs,
struct mat41 *output)
{
/*
lhs rhs output
| a b c d | | x | |ax + by + cz + dw|
| e f g h | | y | = |ex + fy + gz + hw|
| i j k l | | z | |ix + jy + kz + lw|
| m n o p | | w | |mx + ny + oz + pw|
assumed to be stored in memory like { {a, e, i, m}, {b, f, j, n}... }
so, lhs->m[3][2] == g. address like: lhs->[column][row].
*/
int row, col;
for (row = 0; row < 4; row++) {
output->m[row] = 0;
for (col = 0; col < 4; col++)
output->m[row] += lhs->m[col][row] * rhs->m[col];
}
}
/* for pre muliplication, mat44 must be row major and stored row major order */
void mat41_x_mat44(const struct mat41 *lhs, const struct mat44 *rhs,
struct mat41 *output)
{
/*
lhs rhs output
--------- | a b c d | | xa + yb + zc + wd |
x y z w | e f g h | = | xe + yf + zg + wh |
--------- | i j k l | | xi + yj + zk + wl |
| m n o p | | xm + yn + zo + wp |
assumed to be stored in memory like { {a, b, c, d}, {e, f, g, h}... }
so, rhs->m[3][2] == j, address like: rhs->[row][col].
*/
int row, col;
for (row = 0; row < 4; row++) {
output->m[row] = 0;
for (col = 0; col < 4; col++)
output->m[row] += lhs->m[col] * rhs->m[row][col];
}
}
/* column major... */
void mat41_translate(struct mat41 *rhs, float tx, float ty, float tz, struct mat41 *output)
{
struct mat44 translate = {{{ 1, 0, 0, 0 }, /* column major, so this looks xposed */
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ tx, ty, tz, 1}}};
mat44_x_mat41(&translate, rhs, output);
}
/* column major... */
void mat41_rotate_x(struct mat41 *rhs, float angle, struct mat41 *output)
{
struct mat44 rotatex = {{{ 1, 0, 0, 0 },
{ 0, cosf(angle), sinf(angle), 0 },
{ 0, -sinf(angle), cosf(angle), 0 },
{ 0, 0, 0, 1 }}};
mat44_x_mat41(&rotatex, rhs, output);
}
/* column major... */
void mat41_rotate_y(struct mat41 *rhs, float angle, struct mat41 *output)
{
struct mat44 rotatey = {{{ cosf(angle), 0, -sinf(angle), 0},
{ 0, 1, 0, 0},
{ sinf(angle), 0, cosf(angle), 0},
{ 0, 0, 0, 1}}};
mat44_x_mat41(&rotatey, rhs, output);
}
/* column major... */
void mat41_rotate_z(struct mat41 *rhs, float angle, struct mat41 *output)
{
struct mat44 rotatez = {{{ cosf(angle), sinf(angle), 0, 0},
{ -sinf(angle), cosf(angle), 0, 0},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1}}};
mat44_x_mat41(&rotatez, rhs, output);
}
void mat41_scale(struct mat41 *rhs, float scale, struct mat41 *output)
{
struct mat44 scalem = {{{ scale, 0, 0, 0 },
{ 0, scale, 0, 0 },
{ 0, 0, scale, 0 },
{ 0, 0, 0, 1 }}};
mat44_x_mat41(&scalem, rhs, output);
}
void mat44_translate(struct mat44 *rhs, float tx, float ty, float tz,
struct mat44 *output)
{
struct mat44 translate = {{{ 1, 0, 0, 0 }, /* column major, so this looks xposed */
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ tx, ty, tz, 1}}};
mat44_product(&translate, rhs, output);
}
void mat44_rotate_x(struct mat44 *rhs, float angle, struct mat44 *output)
{
struct mat44 rotatex = {{{ 1, 0, 0, 0 },
{ 0, cosf(angle), sinf(angle), 0 },
{ 0, -sinf(angle), cosf(angle), 0 },
{ 0, 0, 0, 1 }}};
mat44_product(rhs, &rotatex, output);
}
void mat44_rotate_y(struct mat44 *rhs, float angle, struct mat44 *output)
{
struct mat44 rotatey = {{{ cosf(angle), 0, -sinf(angle), 0},
{ 0, 1, 0, 0},
{ sinf(angle), 0, cosf(angle), 0},
{ 0, 0, 0, 1}}};
mat44_product(rhs, &rotatey, output);
}
void mat44_rotate_z(struct mat44 *rhs, float angle, struct mat44 *output)
{
struct mat44 rotatez = {{{ cosf(angle), sinf(angle), 0, 0},
{ -sinf(angle), cosf(angle), 0, 0},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1}}};
mat44_product(rhs, &rotatez, output);
}
void mat44_scale(struct mat44 *rhs, float scale, struct mat44 *output)
{
struct mat44 scalem = {{{ scale, 0, 0, 0 },
{ 0, scale, 0, 0 },
{ 0, 0, scale, 0 },
{ 0, 0, 0, 1 }}};
mat44_product(rhs, &scalem, output);
}
float dist3d(float dx, float dy, float dz)
{
return sqrt(dx * dx + dy * dy + dz * dz);
}
float dist3dsqrd(float dx, float dy, float dz)
{
return dx * dx + dy * dy + dz * dz;
}
/* safe to call with v == output */
void normalize_vector(struct mat41 *v, struct mat41 *output)
{
float d;
d = dist3d(v->m[0], v->m[1], v->m[2]);
output->m[0] = v->m[0] / d;
output->m[1] = v->m[1] / d;
output->m[2] = v->m[2] / d;
}
void mat41_cross_mat41(struct mat41 *v1, struct mat41 *v2, struct mat41 *output)
{
/* A x B = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1); a vector quantity */
output->m[0] = v1->m[1] * v2->m[2] - v1->m[2] * v2->m[1];
output->m[1] = v1->m[2] * v2->m[0] - v1->m[0] * v2->m[2];
output->m[2] = v1->m[0] * v2->m[1] - v1->m[1] * v2->m[0];
output->m[3] = 1.0;
}
void print44(struct mat44 *m)
{
printf("%lf %lf %lf %lf\n", m->m[0][0], m->m[1][0], m->m[2][0], m->m[3][0]);
printf("%lf %lf %lf %lf\n", m->m[0][1], m->m[1][1], m->m[2][1], m->m[3][1]);
printf("%lf %lf %lf %lf\n", m->m[0][2], m->m[1][2], m->m[2][2], m->m[3][2]);
printf("%lf %lf %lf %lf\n", m->m[0][3], m->m[1][3], m->m[2][3], m->m[3][3]);
}
void print41(struct mat41 *m)
{
printf("%lf %lf %lf %lf\n", m->m[0], m->m[1], m->m[2], m->m[3]);
}
float mat41_dot_mat41(struct mat41 *m1, struct mat41 *m2)
{
return m1->m[0] * m2->m[0] + m1->m[1] * m2->m[1] + m1->m[2] * m2->m[2];
}
/*
* Rotate vector v around axis by angle, store answer in rhs.
* based on stackoverflow code here:
* http://stackoverflow.com/questions/7582398/rotate-a-vector-about-another-vector
*/
void mat41_rotate_mat41(struct mat41 *rhs, struct mat41 *v, struct mat41 *axis, float angle)
{
float c = cosf(angle);
float s = sinf(angle);
float C = 1.0 - c;
float Q[3][3];
Q[0][0] = axis->m[0] * axis->m[0] * C + c;
Q[0][1] = axis->m[1] * axis->m[0] * C + axis->m[2] * s;
Q[0][2] = axis->m[2] * axis->m[0] * C - axis->m[1] * s;
Q[1][0] = axis->m[1] * axis->m[0] * C - axis->m[2] * s;
Q[1][1] = axis->m[1] * axis->m[1] * C + c;
Q[1][2] = axis->m[2] * axis->m[1] * C + axis->m[0] * s;
Q[2][0] = axis->m[0] * axis->m[2] * C + axis->m[1] * s;
Q[2][1] = axis->m[2] * axis->m[1] * C - axis->m[0] * s;
Q[2][2] = axis->m[2] * axis->m[2] * C + c;
rhs->m[0] = v->m[0] * Q[0][0] + v->m[0] * Q[0][1] + v->m[0] * Q[0][2];
rhs->m[1] = v->m[1] * Q[1][0] + v->m[1] * Q[1][1] + v->m[1] * Q[1][2];
rhs->m[2] = v->m[2] * Q[2][0] + v->m[2] * Q[2][1] + v->m[2] * Q[2][2];
rhs->m[3] = 1.0;
}
#ifdef TEST_MATRIX
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[])
{
struct mat44 answer;
struct mat44 identity = {{ { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }}};
struct mat44 t = {{ { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 1, 2, 3, 1 }}};
#define angle (90.0 * M_PI / 180.0)
#if 0
struct mat44 rotatex = {{{ 1, 0, 0, 0 },
{ 0, cosf(angle), sinf(angle), 0 },
{ 0, -sinf(angle), cosf(angle), 0 },
{ 0, 0, 0, 1 }}};
#endif
struct mat44 rotatex = {{{ 1, 0, 0, 0 },
{ 0, cosf(angle), -sinf(angle), 0 },
{ 0, sinf(angle), cosf(angle), 0 },
{ 0, 0, 0, 1 }}};
struct mat44 abc = {{ { 0, 4, 8, 12 },
{ 1, 5, 9, 13 },
{ 2, 6, 10, 14 },
{ 3, 7, 11, 15 }}};
struct mat41 p = {{ 2, 2, 2, 0 }};
struct mat41 p1 = {{ 1, 2, 3, 0 }};
struct mat41 p2 = {{ 0 }};
struct mat41 a = { { 0 } };
struct mat41 a2 = { { 0 } };
int row, column;
mat44_product(&identity, &t, &answer);
mat44_x_mat41(&identity, &p, &a);
mat44_x_mat41(&t, &p, &a2);
mat44_x_mat41(&rotatex, &p1, &p2);
for (column = 0; column < 4; column++) {
for (row = 0; row < 4; row++) {
printf("%f ", answer.m[row][column]);
}
printf("\n");
}
printf("a = %lf %lf %lf %lf\n", a.m[0], a.m[1], a.m[2], a.m[3]);
printf("a2 = %lf %lf %lf %lf\n", a2.m[0], a2.m[1], a2.m[2], a2.m[3]);
printf("p2 = %lf %lf %lf %lf\n", p2.m[0], p2.m[1], p2.m[2], p2.m[3]);
mat44_product(&identity, &abc, &answer);
printf("answer =\n");
print44(&answer);
return 0;
}
#endif