forked from af3ld/ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3d_matrixS.c
384 lines (290 loc) · 7 KB
/
D3d_matrixS.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <D3d_matrix.h>
#include <string.h>
int D3d_print_mat (double a[4][4]) {
int r, c ;
for (r = 0 ; r < 4 ; r++ ) {
for (c = 0 ; c < 4 ; c++ ) {
printf(" %12.4lf ", a[r][c]) ;
}
printf("\n") ;
}
return 1 ;
}
int D3d_copy_mat (double a[4][4], double b[4][4]) {
int r, c ;
for (r = 0 ; r < 4 ; r++ ) {
for (c = 0 ; c < 4 ; c++ ) {
a[r][c] = b[r][c] ;
}
}
return 1 ;
}
int D3d_mat_mult(double res[4][4], double a[4][4], double b[4][4]) {
int i, j, k;
double temp1, temp2, temp3[4][4];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
temp1 = temp2 = 0;
for (k = 0; k < 4; k++) {
temp1 = a[i][k] * b[k][j];
temp2 += temp1;
}
temp3[i][j] = temp2;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
res[i][j] = temp3[i][j];
}
}
return 1;
}
int D3d_make_identity (double a[4][4]) {
// a = I
int r, c ;
for (r = 0 ; r < 4 ; r++ ) {
for (c = 0 ; c < 4 ; c++ ) {
if (r == c) a[r][c] = 1.0 ;
else a[r][c] = 0.0 ;
}
}
return 1 ;
}
int D3d_translate (double a[4][4], double b[4][4], double dx, double dy, double dz) {
// a = translation*a
// b = b*translation_inverse
double t[4][4] ;
D3d_make_identity(t) ;
t[0][3] = dx ; t[1][3] = dy ; t[2][3] = dz;
D3d_mat_mult(a, t, a) ;
t[0][2] = -dx ; t[1][2] = -dy ;
D3d_mat_mult(b, b, t) ;
return 1 ;
}
int D3d_scale (double a[4][4], double b[4][4], double sx, double sy, double sz) {
double t[4][4];
D3d_make_identity(t);
t[0][0] = sx; t[1][1] = sy; t[2][2] = sz;
D3d_mat_mult(a, t, a);
t[0][0] = 1 / sx; t[1][1] = 1 / sy;
D3d_mat_mult(b, b, t);
return 1;
}
int D3d_rotate_x (double a[4][4], double b[4][4], double rads) {
double t[4][4] = {
{1, 0, 0, 0},
{0, cos(rads), -sin(rads), 0},
{0, sin(rads), cos(rads), 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{1, 0, 0, 0},
{0, cos(rads), sin(rads), 0} ,
{0, -sin(rads), cos(rads), 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_rotate_y (double a[4][4], double b[4][4], double rads) {
double t[4][4] = {
{cos(rads), 0, sin(rads), 0},
{0, 1, 0, 0},
{ -sin(rads), 0, cos(rads), 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{cos(rads), 0, -sin(rads), 0},
{0, 1, 0, 0},
{sin(rads), 0, cos(rads), 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_rotate_z (double a[4][4], double b[4][4], double rads) {
double t[4][4] = {
{cos(rads), -sin(rads), 0, 0},
{sin(rads), cos(rads), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{cos(rads), sin(rads), 0, 0},
{ -sin(rads), cos(rads), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_cs_rotate_x (double a[4][4], double b[4][4], double cs, double sn) {
//rotates by inputting the sin and cos
double t[4][4] = {
{1, 0, 0, 0},
{0, cs, -sn, 0},
{0, sn, cs, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{1, 0, 0, 0},
{0, cs, sn, 0},
{0, -sn, cs, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_cs_rotate_y (double a[4][4], double b[4][4], double cs, double sn) {
//rotates by inputting the sin and cos
double t[4][4] = {
{cs, 0, sn, 0},
{0, 1, 0, 0},
{ -sn, 0, cs, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{cs, 0, -sn, 0},
{0, 1, 0, 0},
{sn, 0, cs, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_cs_rotate_z (double a[4][4], double b[4][4], double cs, double sn) {
//rotates by inputting the sin and cos
double t[4][4] = {
{cs, -sn, 0, 0},
{sn, cs, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(a, t, a);
double q[4][4] = {
{cs, sn, 0, 0},
{ -sn, cs, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
D3d_mat_mult(b, b, q);
return 1;
}
int D3d_negate_x (double a[4][4], double b[4][4]) {
// negate the x....reflects in the yz-plane
// a = reflect*a
// b = b*reflect_inverse
double t[4][4];
D3d_make_identity(t);
t[0][0] = -1;
D3d_mat_mult(a, t, a);
D3d_mat_mult(b, b, t);
return 1;
}
int D3d_negate_y (double a[4][4], double b[4][4]) {
// negate the y....reflects in the xz-plane
// a = reflect*a
// b = b*reflect_inverse
double t[4][4];
D3d_make_identity(t);
t[1][1] = -1;
D3d_mat_mult(a, t, a);
D3d_mat_mult(b, b, t);
return 1;
}
int D3d_negate_z (double a[4][4], double b[4][4]) {
// negate the z....reflects in the xy-plane
// a = reflect*a
// b = b*reflect_inverse
double t[4][4];
D3d_make_identity(t);
t[2][2] = -1;
D3d_mat_mult(a, t, a);
D3d_mat_mult(b, b, t);
return 1;
}
int D3d_mat_mult_points (double *X, double *Y, double *Z,
double m[4][4],
double *x, double *y, double *z, int numpoints) {
// |X0 X1 X2 ...| |x0 x1 x2 ...|
// |Y0 Y1 Y2 ...| = m * |y0 y1 y2 ...|
// |Z0 Z1 Z2 ...| |z0 z1 z2 ...|
// | 1 1 1 ...| | 1 1 1 ...|
int i, j;
double tempX[numpoints], tempY[numpoints], tempZ[numpoints];
for (i = 0; i < 4; i++) {
for (j = 0; j < numpoints; j++) {
if (i == 0) {
tempX[j] = m[i][0] * x[j] + m[i][1] * y[j] + m[i][2] * z[j] + m[i][3];
} else if (i == 1) {
tempY[j] = m[i][0] * x[j] + m[i][1] * y[j] + m[i][2] * z[j] + m[i][3];
} else if (i == 2) {
tempZ[j] = m[i][0] * x[j] + m[i][1] * y[j] + m[i][2] * z[j] + m[i][3];
}
}
}
for (i = 0; i < numpoints; i++) {
X[i] = tempX[i];
Y[i] = tempY[i];
Z[i] = tempZ[i];
}
return 1;
}
int D3d_mat_mult_pt (double P[3], double m[4][4], double Q[3]) {
// multiplies a SINGLE point by a matrix
// | P[0] | | Q[0] |
// | P[1] | = m * | Q[1] |
// | P[2] | | Q[2] |
// | 1 | | 1 |
int i, j;
double temp[3];
for (i = 0; i < 4; i++) {
if (i == 0) {
temp[0] = m[i][0] * Q[0] + m[i][1] * Q[1] + m[i][2] * Q[2] + m[i][3];
} else if (i == 1) {
temp[1] = m[i][0] * Q[0] + m[i][1] * Q[1] + m[i][2] * Q[2] + m[i][3];
} else if (i == 2) {
temp[2] = m[i][0] * Q[0] + m[i][1] * Q[1] + m[i][2] * Q[2] + m[i][3];
}
}
memcpy(P, temp, 3);
return 1;
}
int D3d_x_product (double res[3], double a[3], double b[3]) {
double temp[3] = {
(a[1] * b[2]) - (b[1] * a[2]),
(a[2] * b[0]) - (b[2] * a[0]),
(a[0] * b[1]) - (b[0] * a[1]),
};
res[0] = temp[0];
res[1] = temp[1];
res[2] = temp[2];
return 1;
}
//returns the dot product of two vectors
double D3d_dot_product(double vect1[3], double vect2[3]) {
return (vect1[0] * vect2[0]) + (vect1[1] *
vect2[1]) + (vect1[2] * vect2[2]);
}
int D3d_make_movement_sequence_matrix (
double mat[4][4],
double inv[4][4],
int num_movements,
int *movement_type_list,
double *parameter_list ) {
D3d_make_identity(mat); D3d_make_identity(inv);
printf("This has not been created yet\n");
return 1;
}
int D3d_view (double view[4][4], double view_inverse[4][4],
double eye_x, double eye_y, double eye_z ,
double coi_x, double coi_y, double coi_z ,
double up_x, double up_y, double up_z ) {
printf("This has not been created yet\n");
return 1;
}