-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgebra.c
170 lines (140 loc) · 3.46 KB
/
algebra.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
#include <math.h>
#include <stdio.h>
#include "algebra.h"
/* Diverse funktioner från Grafik gk. */
// Not actually used in the project, keeping for reference
Matrix Scale(Vector v) { /* get scalematrix */
Matrix m = { v.x, 0, 0, 0,
0, v.y, 0, 0,
0, 0, v.z, 0,
0, 0, 0, 1 };
return m;
}
Matrix Translate(Vector v) { /* get translationmatrix */
Matrix m = {1, 0, 0, v.x,
0, 1, 0, v.y,
0, 0, 1, v.z,
0, 0, 0, 1 };
return m;
}
Matrix Rotate(float theta, angle a) { /* get rotatationmatrix */
Matrix Rx = { 1, 0, 0, 0,
0, (float)cos(theta), (float)-(sin(theta)), 0,
0, (float)sin(theta), (float)cos(theta), 0,
0, 0, 0, 1 };
Matrix Ry = { (float)cos(theta), 0, (float)sin(theta), 0,
0, 1, 0, 0,
(float)-(sin(theta)), 0, (float)cos(theta), 0,
0, 0, 0, 1 };
Matrix Rz = { (float)cos(theta), (float)-(sin(theta)), 0, 0,
(float)sin(theta), (float)cos(theta), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/* which matrix to return depends on which angle user wants */
switch (a) {
case ROT_X:
return Rx;
case ROT_Y:
return Ry;
case ROT_Z:
return Rz;
}
return Rx;
}
Vector CrossProduct(Vector a, Vector b) {
Vector v = { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x };
return v;
}
float DotProduct(Vector a, Vector b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
Vector Subtract(Vector a, Vector b) {
Vector v = { a.x-b.x, a.y-b.y, a.z-b.z };
return v;
}
Vector Add(Vector a, Vector b) {
Vector v = { a.x+b.x, a.y+b.y, a.z+b.z };
return v;
}
float Length(Vector a) {
return (float)sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
Vector Normalize(Vector a) {
float len = Length(a);
Vector v = { a.x/len, a.y/len, a.z/len };
return v;
}
Vector ScalarVecDiv(float t, Vector a) {
Vector b = { a.x/t, a.y/t, a.z/t };
return b;
}
Vector ScalarVecMul(float t, Vector a) {
Vector b = { a.x*t, a.y*t, a.z*t };
return b;
}
HomVector MatVecMul(Matrix a, HomVector b) {
HomVector h;
h.x = b.x*a.e[0][0] + b.y*a.e[0][1] + b.z*a.e[0][2] + b.w*a.e[0][3];
h.y = b.x*a.e[1][0] + b.y*a.e[1][1] + b.z*a.e[1][2] + b.w*a.e[1][3];
h.z = b.x*a.e[2][0] + b.y*a.e[2][1] + b.z*a.e[2][2] + b.w*a.e[2][3];
h.w = b.x*a.e[3][0] + b.y*a.e[3][1] + b.z*a.e[3][2] + b.w*a.e[3][3];
return h;
}
float VecVecMul(HomVector a, HomVector b) {
float d;
d = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
return d;
}
Vector Homogenize(HomVector h) {
Vector a;
if (h.w == 0.0) {
fprintf(stderr, "Homogenize: w = 0\n");
a.x = a.y = a.z = 9999999;
return a;
}
a.x = h.x / h.w;
a.y = h.y / h.w;
a.z = h.z / h.w;
return a;
}
Matrix MatMatMul(Matrix a, Matrix b) {
Matrix c;
int i, j, k;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
c.e[i][j] = 0.0;
for (k = 0; k < 4; k++)
c.e[i][j] += a.e[i][k] * b.e[k][j];
}
}
return c;
}
void PrintMatrix(char *name, Matrix a) {
int i, j;
puts(name);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
printf("%6.5lf ", a.e[i][j]);
printf("\n");
}
}
void PrintVector(char *name, Vector a) {
printf("%s: %6.5lf %6.5lf %6.5lf\n", name, a.x, a.y, a.z);
}
void PrintHomVector(char *name, HomVector a) {
printf("%s: %6.5lf %6.5lf %6.5lf %6.5lf\n", name, a.x, a.y, a.z, a.w);
}
Vector Normal(Vector v[]) {
Vector n = CrossProduct(Subtract(v[1], v[0]), Subtract(v[2], v[0]));
return n;
}
Matrix Transpose(Matrix a) {
int i,j;
Matrix b;
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
b.e[i][j]=a.e[j][i];
}
}
return b;
}