-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
366 lines (293 loc) · 7.48 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "matrix.h"
/*======== struct matrix * make_bezier() ==========
Inputs:
Returns: The correct 4x4 matrix that can be used
to generate the coefiecients for a bezier curve
====================*/
struct matrix * make_bezier() {
struct matrix *bm = new_matrix(4, 4);
bm->lastcol = 4;
bm->m[0][0] = -1;
bm->m[0][1] = 3;
bm->m[0][2] = -3;
bm->m[0][3] = 1;
bm->m[1][0] = 3;
bm->m[1][1] = -6;
bm->m[1][2] = 3;
bm->m[1][3] = 0;
bm->m[2][0] = -3;
bm->m[2][1] = 3;
bm->m[2][2] = 0;
bm->m[2][3] = 0;
bm->m[3][0] = 1;
bm->m[3][1] = 0;
bm->m[3][2] = 0;
bm->m[3][3] = 0;
return bm;
}
/*======== struct matrix * make_hermite() ==========
Inputs:
Returns:
The correct 4x4 matrix that can be used to generate
the coefiecients for a hermite curve
====================*/
struct matrix * make_hermite() {
struct matrix *bm = new_matrix(4, 4);
bm->lastcol = 4;
bm->m[0][0] = 2;
bm->m[0][1] = -2;
bm->m[0][2] = 1;
bm->m[0][3] = 1;
bm->m[1][0] = -3;
bm->m[1][1] = 3;
bm->m[1][2] = -2;
bm->m[1][3] = -1;
bm->m[2][0] = 0;
bm->m[2][1] = 0;
bm->m[2][2] = 1;
bm->m[2][3] = 0;
bm->m[3][0] = 1;
bm->m[3][1] = 0;
bm->m[3][2] = 0;
bm->m[3][3] = 0;
return bm;
}
/*======== struct matrix * generate_curve_coefs() ==========
Inputs: double p0
double p1
double p2
double p3
int type
Returns:
A matrix containing the values for a, b, c and d of the
equation at^3 + bt^2 + ct + d for the curve defined
by p0, p1, p2 and p3.
For hermite curves, p0 and p1 are endpoints, p2 and p3
are rates of change
Type determines whether the curve is bezier or hermite
====================*/
struct matrix * generate_curve_coefs( double p0, double p1,
double p2, double p3, int type) {
struct matrix *curve;
struct matrix *coefs = new_matrix(4, 1);
coefs->lastcol = 1;
coefs->m[0][0] = p0;
coefs->m[1][0] = p1;
coefs->m[2][0] = p2;
coefs->m[3][0] = p3;
// print_matrix(coefs);
//printf("\n");
if (type == HERMITE)
curve = make_hermite();
else
curve = make_bezier();
matrix_mult(curve, coefs);
free_matrix(curve);
return coefs;
}
/*======== struct matrix * make_translate() ==========
Inputs: int x
int y
int z
Returns: The translation matrix created using x, y and z
as the translation offsets.
====================*/
struct matrix * make_translate(double x, double y, double z) {
struct matrix *t = new_matrix(4, 4);
ident(t);
t->m[0][3] = x;
t->m[1][3] = y;
t->m[2][3] = z;
return t;
}
/*======== struct matrix * make_scale() ==========
Inputs: int x
int y
int z
Returns: The translation matrix creates using x, y and z
as the scale factors
====================*/
struct matrix * make_scale(double x, double y, double z) {
struct matrix *t = new_matrix(4, 4);
ident(t);
t->m[0][0] = x;
t->m[1][1] = y;
t->m[2][2] = z;
return t;
}
/*======== struct matrix * make_rotX() ==========
Inputs: double theta
Returns: The rotation matrix created using theta as the
angle of rotation and X as the axis of rotation.
====================*/
struct matrix * make_rotX(double theta) {
struct matrix *t = new_matrix(4, 4);
ident(t);
t->m[1][1] = cos(theta);
t->m[1][2] = -1 * sin(theta);
t->m[2][1] = sin(theta);
t->m[2][2] = cos(theta);
return t;
}
/*======== struct matrix * make_rotY() ==========
Inputs: double theta
char c
Returns: The rotation matrix created using theta as the
angle of rotation and Y as the axis of rotation.
====================*/
struct matrix * make_rotY(double theta) {
struct matrix *t = new_matrix(4, 4);
ident(t);
t->m[0][0] = cos(theta);
t->m[2][0] = -1 * sin(theta);
t->m[0][2] = sin(theta);
t->m[2][2] = cos(theta);
return t;
}
/*======== struct matrix * make_rotZ() ==========
Inputs: double theta
char c
Returns: The rotation matrix created using theta as the
angle of rotation and Z as the axis of rotation.
====================*/
struct matrix * make_rotZ(double theta) {
struct matrix *t = new_matrix(4, 4);
ident(t);
t->m[0][0] = cos(theta);
t->m[0][1] = -1 * sin(theta);
t->m[1][0] = sin(theta);
t->m[1][1] = cos(theta);
return t;
}
/*-------------- void print_matrix() --------------
Inputs: struct matrix *m
Returns:
print the matrix
*/
void print_matrix(struct matrix *m) {
int r, c;
for (r=0; r < m->rows; r++) {
for (c=0; c < m->lastcol; c++)
printf("%0.2f ", m->m[r][c]);
printf("\n");
}
}//end print_matrix
/*-------------- void ident() --------------
Inputs: struct matrix *m <-- assumes m is a square matrix
Returns:
turns m in to an identity matrix
*/
void ident(struct matrix *m) {
int r, c;
for (r=0; r < m->rows; r++)
for (c=0; c < m->cols; c++)
if ( r == c )
m->m[r][c] = 1;
else
m->m[r][c] = 0;
m->lastcol = m->cols;
}//end ident
/*-------------- void scalar_mult() --------------
Inputs: double x
struct matrix *m
Returns:
multiply each element of m by x
*/
void scalar_mult(double x, struct matrix *m) {
int r, c;
for (r=0; r < m->rows; r++)
for (c=0; c < m->lastcol; c++)
m->m[r][c] *= x;
}//end scalar_mult
/*-------------- void matrix_mult() --------------
Inputs: struct matrix *a
struct matrix *b
Returns:
a*b -> b
*/
void matrix_mult(struct matrix *a, struct matrix *b) {
int r, c;
struct matrix *tmp;
tmp = new_matrix(4, 1);
for (c=0; c < b->lastcol; c++) {
//copy current col (point) to tmp
for (r=0; r < b->rows; r++)
tmp->m[r][0] = b->m[r][c];
for (r=0; r < b->rows; r++)
b->m[r][c] = a->m[r][0] * tmp->m[0][0] +
a->m[r][1] * tmp->m[1][0] +
a->m[r][2] * tmp->m[2][0] +
a->m[r][3] * tmp->m[3][0];
}
free_matrix(tmp);
}//end matrix_mult
/*===============================================
These Functions do not need to be modified
===============================================*/
/*-------------- struct matrix *new_matrix() --------------
Inputs: int rows
int cols
Returns:
Once allocated, access the matrix as follows:
m->m[r][c]=something;
if (m->lastcol)...
*/
struct matrix *new_matrix(int rows, int cols) {
double **tmp;
int i;
struct matrix *m;
tmp = (double **)malloc(rows * sizeof(double *));
for (i=0;i<rows;i++) {
tmp[i]=(double *)malloc(cols * sizeof(double));
}
m=(struct matrix *)malloc(sizeof(struct matrix));
m->m=tmp;
m->rows = rows;
m->cols = cols;
m->lastcol = 0;
return m;
}
/*-------------- void free_matrix() --------------
Inputs: struct matrix *m
Returns:
1. free individual rows
2. free array holding row pointers
3. free actual matrix
*/
void free_matrix(struct matrix *m) {
int i;
for (i=0;i<m->rows;i++) {
free(m->m[i]);
}
free(m->m);
free(m);
}
/*======== void grow_matrix() ==========
Inputs: struct matrix *m
int newcols
Returns:
Reallocates the memory for m->m such that it now has
newcols number of collumns
====================*/
void grow_matrix(struct matrix *m, int newcols) {
int i;
for (i=0;i<m->rows;i++) {
m->m[i] = realloc(m->m[i],newcols*sizeof(double));
}
m->cols = newcols;
}
/*-------------- void copy_matrix() --------------
Inputs: struct matrix *a
struct matrix *b
Returns:
copy matrix a to matrix b
*/
void copy_matrix(struct matrix *a, struct matrix *b) {
int r, c;
for (r=0; r < a->rows; r++)
for (c=0; c < a->cols; c++)
b->m[r][c] = a->m[r][c];
}