-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.h
273 lines (233 loc) · 5.64 KB
/
types.h
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
/*
Copyright 2012 Shiben Bhattacharjee & Naveen Kumar
licensed under the apache license, version 2.0 (the "license");
you may not use this file except in compliance with the license.
you may obtain a copy of the license at
http://www.apache.org/licenses/license-2.0
unless required by applicable law or agreed to in writing, software
distributed under the license is distributed on an "as is" basis,
without warranties or conditions of any kind, either express or implied.
see the license for the specific language governing permissions and
limitations under the license.
*/
#ifndef _MYTYPES_H_
#define _MYTYPES_H_
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EPSILON 0.00001
/* Data Types */
typedef struct Vector
{
float x;
float y;
float z;
}
Vector;
typedef struct Triangle
{
Vector v0;
Vector v1;
Vector v2;
}
Triangle;
typedef struct Matrix
{
float m[4][4];
}
Matrix;
/* Data Type Setters */
void setVector(Vector *v, float x, float y, float z)
{
(*v).x = x;
(*v).y = y;
(*v).z = z;
}
void setTriangle(Triangle *tri, Vector v0, Vector v1, Vector v2)
{
(*tri).v0 = v0;
(*tri).v1 = v1;
(*tri).v2 = v2;
}
void setMatrix(Matrix *mat, float *m)
{
/* Column major storage */
int i, j;
for(j = 0; j < 4; j++)
for(i = 0; i < 4; i++)
(*mat).m[i][j] = m[j + 4 * i];
}
/* Data Type Printers */
void printVector(Vector v)
{
printf("(%.4f, %.4f, %.4f)", v.x, v.y, v.z);
}
void printTriangle(Triangle tri)
{
printf("0:");
printVector(tri.v0);
printf(" 1:");
printVector(tri.v1);
printf(" 2:");
printVector(tri.v2);
}
void printMatrix(Matrix M)
{
/* column major printing */
int i, j;
for(j = 0; j < 4; j++)
printf("%.4f\t%.4f\t%.4f\t%.4f\n", M.m[j][0], M.m[j][1], M.m[j][2], M.m[j][3]);
}
/* utility functions */
float deg2rad(float deg)
{
return M_PI * deg / 180.0f;
}
Vector vecVecMult(Vector a, Vector b)
{
Vector c;
setVector(&c, a.x * b.x, a.y * b.y, a.z * b.z);
return c;
}
Vector floatVecMult(float a, Vector b)
{
Vector c;
setVector(&c, a * b.x, a * b.y, a * b.z);
return c;
}
float dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vector cross(Vector a, Vector b)
{
Vector c;
setVector(&c, (a.y * b.z) - (b.y * a.z),
(a.z * b.x) - (b.z * a.x),
(a.x * b.y) - (b.x * a.y));
return c;
}
Vector add(Vector a, Vector b)
{
Vector c;
setVector(&c, a.x + b.x, a.y + b.y, a.z + b.z);
return c;
}
Vector subtract(Vector a, Vector b)
{
Vector c;
setVector(&c, a.x - b.x, a.y - b.y, a.z - b.z);
return c;
}
Vector negate(Vector a)
{
Vector c;
setVector(&c, -a.x, -a.y, -a.z);
return c;
}
float length(Vector a)
{
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
}
Vector normalize(Vector a)
{
float l = length(a);
Vector c;
if(l > -EPSILON && l < EPSILON) /* If length is zero */
return a;
setVector(&c, a.x/l, a.y/l, a.z/l);
return c;
}
Vector matVecMult(Matrix M, Vector v)
{
/* Column Major (Post multiplication of Column vectors) */
Vector c;
int i;
setVector(&c, M.m[0][0] * v.x + M.m[0][1] * v.y + M.m[0][2] * v.z + M.m[0][3],
M.m[1][0] * v.x + M.m[1][1] * v.y + M.m[1][2] * v.z + M.m[1][3],
M.m[2][0] * v.x + M.m[2][1] * v.y + M.m[2][2] * v.z + M.m[2][3]);
/* Last row multiplication with the Vector is discarded intentionally*/
return c;
}
Matrix matMatMult(Matrix A, Matrix B)
{
Matrix C;
int i, j, k;
for(j = 0; j < 4; j++)
for(i = 0; i < 4; i++)
{
C.m[i][j] = 0.0f;
for(k = 0; k < 4; k++)
C.m[i][j] += A.m[i][k] * B.m[k][j];
}
return C;
}
Matrix identity(void)
{
Matrix C;
float m[16] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
Matrix rotateX(float a)
{
float cosa = cos(deg2rad(a)), sina = sin(deg2rad(a));
Matrix C;
float m[16] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cosa,-sina, 0.0f,
0.0f, sina, cosa, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
Matrix rotateY(float a)
{
float cosa = cos(deg2rad(a)), sina = sin(deg2rad(a));
Matrix C;
float m[16] = {cosa, 0.0f, sina, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-sina, 0.0f, cosa, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
Matrix rotateZ(float a)
{
float cosa = cos(deg2rad(a)), sina = sin(deg2rad(a));
Matrix C;
float m[16] = {cosa,-sina, 0.0f, 0.0f,
sina, cosa, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
Matrix rotate(float ax, float ay, float az)
{
/* Simply doing rotX x rotY x rotZ */
return matMatMult(matMatMult(rotateX(ax), rotateY(ay)), rotateZ(az));
}
Matrix translate(float tx, float ty, float tz)
{
Matrix C;
float m[16] = {1.0f, 0.0f, 0.0f, tx,
0.0f, 1.0f, 0.0f, ty,
0.0f, 0.0f, 1.0f, tz,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
Matrix scale(float sx, float sy, float sz)
{
Matrix C;
float m[16] = {sx, 0.0f, 0.0f, 0.0f,
0.0f, sy, 0.0f, 0.0f,
0.0f, 0.0f, sz, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
setMatrix(&C, m);
return C;
}
#endif