forked from halcy/rasterizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectors.c
279 lines (233 loc) · 4.86 KB
/
vectors.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
/**
* Vector maths.
* (c) L. Diener 2010
*/
#include "vectors.h"
#include <math.h>
#include <stdio.h>
// Generate vectors
inline vec2 makeVec2(scalar x, scalar y) {
vec2 v;
v.x = x;
v.y = y;
return v;
}
inline vec3 makeVec3(scalar x, scalar y, scalar z) {
vec3 v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
// Vector-scalar multiplication
inline void mult2(vec2 * r, vec2 a, scalar s) {
r->x = a.x * s;
r->y = a.y * s;
}
inline void mult3(vec3 * r, vec3 a, scalar s) {
r->x = a.x * s;
r->y = a.y * s;
r->z = a.z * s;
}
inline void scale2(vec2 * r, scalar s) {
r->x *= s;
r->y *= s;
}
inline void scale3(vec3 * r, scalar s) {
r->x *= s;
r->y *= s;
r->z *= s;
}
// Vector-scalar division
inline void div2(vec2 * r, vec2 a, scalar s) {
r->x = a.x / s;
r->y = a.y / s;
}
inline void div3(vec3 * r, vec3 a, scalar s) {
r->x = a.x / s;
r->y = a.y / s;
r->z = a.z / s;
}
inline void invScale2(vec2 * r, scalar s) {
r->x /= s;
r->y /= s;
}
inline void invScale3(vec3 * r, scalar s) {
r->x /= s;
r->y /= s;
r->z /= s;
}
// Inverting a vector
inline void inverted2(vec2 * r, vec2 a) {
r->x = -a.x;
r->y = -a.y;
}
inline void inverted3(vec3 * r, vec3 a) {
r->x = -a.x;
r->y = -a.y;
r->z = -a.z;
}
inline void invert2(vec2 * r) {
r->x = -r->x;
r->y = -r->y;
}
inline void invert3(vec3 * r) {
r->x = -r->x;
r->y = -r->y;
r->z = -r->z;
}
// Vector-vector addition
inline void add2(vec2 * r, vec2 a, vec2 b) {
r->x = a.x + b.x;
r->y = a.y + b.y;
}
inline void add3(vec3 * r, vec3 a, vec3 b) {
r->x = a.x + b.x;
r->y = a.y + b.y;
r->z = a.z + b.z;
}
inline void addTo2(vec2 * r, vec2 a) {
r->x += a.x;
r->y += a.y;
}
inline void addTo3(vec3 * r, vec3 a) {
r->x += a.x;
r->y += a.y;
r->z += a.z;
}
// Vector-vector subtraction
inline void sub2(vec2 * r, vec2 a, vec2 b) {
r->x = a.x - b.x;
r->y = a.y - b.y;
}
inline void sub3(vec3 * r, vec3 a, vec3 b) {
r->x = a.x - b.x;
r->y = a.y - b.y;
r->z = a.z - b.z;
}
inline void subFrom2(vec2 * r, vec2 a) {
r->x -= a.x;
r->y -= a.y;
}
inline void subFrom3(vec3 * r, vec3 a) {
r->x -= a.x;
r->y -= a.y;
r->z -= a.z;
}
// Vector-vector multiplication
inline scalar dot2(vec2 a, vec2 b) {
return( a.x*b.x + a.y*b.y );
}
inline scalar dot3(vec3 a, vec3 b) {
return( a.x*b.x + a.y*b.y + a.z*b.z );
}
inline void cross(vec3 * r, vec3 a, vec3 b) {
r->x = a.y * b.z - a.z * b.y;
r->y = a.z * b.x - a.x * a.z;
r->z = a.x * b.y - a.y * a.x;
}
// Vector sizes
inline scalar length2(vec2 v) {
return( sqrt( v.x * v.x + v.y * v.y ) );
}
inline scalar length3(vec3 v) {
return( sqrt( v.x * v.x + v.y * v.y + + v.z * v.z ) );
}
inline scalar dist2(vec2 a, vec2 b) {
scalar xd = a.x - b.x;
scalar yd = a.y - b.y;
return( sqrt( xd * xd + yd * yd ) );
}
inline scalar dist3(vec3 a, vec3 b) {
scalar xd = a.x - b.x;
scalar yd = a.y - b.y;
scalar zd = a.y - b.y;
return( sqrt( xd * xd + yd * yd + zd * zd ) );
}
// Vector normalization
inline void normalized2(vec2 * r, vec2 v) {
scalar l = length2( v );
div2( r, v, l );
}
inline void normalized3(vec3 * r, vec3 v) {
scalar l = length3( v );
div3( r, v, l );
}
inline void normalize2(vec2 * r) {
scalar l = length2( *r );
invScale2( r, l );
}
inline void normalize3(vec3 * r) {
scalar l = length3( *r );
invScale3( r, l );
}
// Projection functions
inline void projectedOnto2(vec2 * r, vec2 a, vec2 b) {
mult2( r, a, dot2(a, b) );
invScale2( r, length2(a) * length2(b) );
}
inline void projectedOnto3(vec3 * r, vec3 a, vec3 b) {
mult3( r, a, dot3(a, b) );
invScale3( r, length3(a) * length3(b) );
}
inline void projectOnto2(vec2 a, vec2 * r) {
scalar l = length2(a) * length2(*r);
mult2( r, a, dot2(a, *r) );
invScale2( r, l );
}
inline void projectOnto3(vec3 a, vec3 * r) {
scalar l = length3(a) * length3(*r);
mult3( r, a, dot3(a, *r) );
invScale3( r, l );
}
// Reflection functions
inline void reflected2(vec2 * r, vec2 i, vec2 n) {
mult2( r, n, dot2( n, i ) * ((scalar)2) );
subFrom2( r, i );
}
inline void reflected3(vec3 * r, vec3 i, vec3 n) {
mult3( r, n, dot3( n, i ) * ((scalar)2) );
subFrom3( r, i );
}
inline void reflect2(vec2 * r, vec2 n) {
vec2 i = *r;
mult2( r, n, dot2( n, i ) * ((scalar)2) );
subFrom2( r, i );
}
inline void reflect3(vec3 * r, vec3 n) {
vec3 i = *r;
mult3( r, n, dot3( n, i ) * ((scalar)2) );
subFrom3( r, i );
}
// Polar coordinates
inline scalar vecAtan(vec2 c) {
return scalarAtan2( c.x, c.y );
}
// Rounding error avoidance
inline void extendTo(vec3 * r, vec3 d) {
// if( d.x > 0 ) {
// r->x = nextScalar( r->x );
// }
// else {
// r->x = prevScalar( r->x );
// }
// if( d.y > 0 ) {
// r->y = nextScalar( r->y );
// }
// else {
// r->y = prevScalar( r->y );
// }
// if( d.z > 0 ) {
// r->z = nextScalar( r->z );
// }
// else {
// r->z = prevScalar( r->z );
// }
vec3 ext;
mult3( &ext, d, 0.1 );
addTo3( r, ext );
}
// Debugging
void vecPrintf3(vec3 v) {
printf( "%f, %f, %f\n", v.x, v.y, v.z );
}