-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.h
456 lines (370 loc) · 17.9 KB
/
vec.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#ifndef __VEC_H
#define __VEC_H
#include <math.h>
#include "fxmath.h"
// well known inverse sqrt from quake 3 source :)
inline float Q_rsqrt( float number ){
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f375a86 - ( i >> 1 ); // what the fuck? (more accurate constant also)
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
// ----------------------------------
struct vec2i;
struct vec2f;
struct vec2x;
struct vec3f;
struct vec3x;
struct vec4f;
struct vec4x;
// ----------------------------------
// integer
struct vec2i {
signed long x;
signed long y;
// add/subtract another vec2i
inline vec2i& operator+=(const vec2i & rhs) {x += rhs.x; y += rhs.y; return *this;}
inline vec2i& operator-=(const vec2i & rhs) {x -= rhs.x; y -= rhs.y; return *this;}
// multiply/divide by scalar
inline vec2i& operator*=(const signed rhs) {x *= rhs; y *= rhs; return *this;}
inline vec2i& operator/=(const signed rhs) {x /= rhs; y /= rhs; return *this;}
inline vec2i& operator*=(const float rhs) {x *= rhs; y *= rhs; return *this;}
inline vec2i& operator/=(const float rhs) {x /= rhs; y /= rhs; return *this;}
// signs and other stuff
inline vec2i& operator-() {x = -x; y = -y; return *this;}
// add/subtract two vec2f
inline const vec2i operator+(const vec2i& rhs) {return vec2i(*this) += rhs;}
inline const vec2i operator-(const vec2i& rhs) {return vec2i(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2i
inline const vec2i operator*(const signed rhs) {return vec2i(*this) *= rhs;}
inline const vec2i operator/(const signed rhs) {return vec2i(*this) /= rhs;}
inline operator vec2f();
inline operator vec2x();
};
// dot product
inline signed long dot (const vec2i& lhs, const vec2i& rhs) {return (lhs.x*rhs.x + lhs.y*rhs.y);}
// length
inline signed long abs (const vec2i& lhs) {return sqrt(lhs.x*lhs.x + lhs.y*lhs.y);}
// norm
inline vec2i norm(const vec2i& lhs) {vec2i r = lhs; return (r / abs(r));}
// ----------------------------------
struct vec2f {
float x;
float y;
// add/subtract another vec2f
inline vec2f& operator+=(const vec2f & rhs) {x += rhs.x; y += rhs.y; return *this;}
inline vec2f& operator-=(const vec2f & rhs) {x -= rhs.x; y -= rhs.y; return *this;}
// multiply/divide by scalar
inline vec2f& operator*=(const signed rhs) {x *= rhs; y *= rhs; return *this;}
inline vec2f& operator/=(const signed rhs) {x /= rhs; y /= rhs; return *this;}
inline vec2f& operator*=(const float rhs) {x *= rhs; y *= rhs; return *this;}
inline vec2f& operator/=(const float rhs) {x /= rhs; y /= rhs; return *this;}
// signs and other stuff
inline vec2f& operator-() {x = -x; y = -y; return *this;}
// add/subtract two vec2f
inline const vec2f operator+(const vec2f& rhs) {return vec2f(*this) += rhs;}
inline const vec2f operator-(const vec2f& rhs) {return vec2f(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec2f operator*(const signed rhs) {return vec2f(*this) *= rhs;}
inline const vec2f operator/(const signed rhs) {return vec2f(*this) /= rhs;}
inline const vec2f operator*(const float rhs) {return vec2f(*this) *= rhs;}
inline const vec2f operator/(const float rhs) {return vec2f(*this) /= rhs;}
inline operator vec2i();
inline operator vec2x();
};
// dot product
inline float dot (const vec2f& lhs, const vec2f& rhs) {return (lhs.x*rhs.x + lhs.y*rhs.y);}
// length
inline float abs (const vec2f& lhs) {return sqrt(lhs.x*lhs.x + lhs.y*lhs.y);}
inline float abs2(const vec2f& lhs) {return lhs.x*lhs.x + lhs.y*lhs.y;}
// norm
inline vec2f norm(const vec2f& lhs) {vec2f r = lhs; return r*Q_rsqrt(r.x*r.x + r.y*r.y);}
// ----------------------------------
// fixed point 16:16
struct vec2x {
signed long x;
signed long y;
// add/subtract another vec2f
inline vec2x& operator+=(const vec2x & rhs) {x += rhs.x; y += rhs.y; return *this;}
inline vec2x& operator-=(const vec2x & rhs) {x -= rhs.x; y -= rhs.y; return *this;}
// multiply/divide by scalar
inline vec2x& operator*=(const signed rhs) {x *= rhs; y *= rhs; return *this;}
inline vec2x& operator/=(const signed rhs) {x /= rhs; y /= rhs; return *this;}
inline vec2x& operator*=(const float rhs) {
x = imul16(x, (signed long)(rhs*65536.0f));
y = imul16(y, (signed long)(rhs*65536.0f));
return *this;
}
inline vec2x& operator/=(const float rhs) {
x = idiv16(x, (signed long)(rhs*65536.0f));
y = idiv16(y, (signed long)(rhs*65536.0f));
return *this;
}
// signs and other stuff
inline vec2x& operator-() {x = -x; y = -y; return *this;}
// add/subtract two vec2f
inline const vec2x operator+(const vec2x& rhs) {return vec2x(*this) += rhs;}
inline const vec2x operator-(const vec2x& rhs) {return vec2x(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec2x operator*(const signed rhs) {return vec2x(*this) *= rhs;}
inline const vec2x operator/(const signed rhs) {return vec2x(*this) /= rhs;}
inline const vec2x operator*(const float rhs) {return vec2x(*this) *= rhs;}
inline const vec2x operator/(const float rhs) {return vec2x(*this) /= rhs;}
inline operator vec2f();
inline operator vec2i();
};
// dot product
inline float dot (const vec2x& lhs, const vec2x& rhs) {return (imul16(lhs.x, rhs.x) + imul16(lhs.y, rhs.y));}
// length
inline float abs (const vec2x& lhs) {return sqrt(imul16(lhs.x, lhs.x) + imul16(lhs.y, lhs.y));}
// norm
inline vec2x norm(const vec2x& lhs) {vec2x r = lhs; return (r / abs(r));}
// type conversions
inline vec2f::operator vec2x() {vec2x r; fist(&r.x, x*65536.0f); fist(&r.y, y*65536.0f); return r;}
inline vec2f::operator vec2i() {vec2i r = {x, y}; return r;}
inline vec2x::operator vec2f() {vec2f r = {((float)x / 65536.0f), ((float)y / 65536.0f)}; return r;}
inline vec2x::operator vec2i() {vec2i r = {(x >> 16), (x >> 16)}; return r;}
// ----------------------------------
struct vec3f {
float x;
float y;
float z;
// add/subtract another vec2f
inline vec3f& operator+=(const vec3f & rhs) {x += rhs.x; y += rhs.y; z += rhs.z; return *this;}
inline vec3f& operator-=(const vec3f & rhs) {x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this;}
// multiply/divide by scalar
inline vec3f& operator*=(const signed rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec3f& operator/=(const signed rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
inline vec3f& operator*=(const float rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec3f& operator/=(const float rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
inline vec3f& operator*=(signed &rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec3f& operator/=(signed &rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
inline vec3f& operator*=(float &rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec3f& operator/=(float &rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
// signs and other stuff
inline vec3f& operator-() {x = -x; y = -y; z = -z; return *this;}
// add/subtract two vec2f
inline const vec3f operator+(const vec3f& rhs) {return vec3f(*this) += rhs;}
inline const vec3f operator-(const vec3f& rhs) {return vec3f(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec3f operator*(const signed rhs) {return vec3f(*this) *= rhs;}
inline const vec3f operator/(const signed rhs) {return vec3f(*this) /= rhs;}
inline const vec3f operator*(const float rhs) {return vec3f(*this) *= rhs;}
inline const vec3f operator/(const float rhs) {return vec3f(*this) /= rhs;}
inline operator vec3x();
};
// dot product
inline float dot (const vec3f& lhs, const vec3f& rhs) {return (lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z);}
// cross product
inline vec3f cross(const vec3f& lhs, const vec3f& rhs) {
vec3f r = {lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x};
return r;
}
// length
inline float abs (const vec3f& lhs) {return sqrt(lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z);}
// norm
inline vec3f norm (const vec3f& lhs) {vec3f r = lhs; return r*Q_rsqrt(r.x*r.x + r.y*r.y + r.z*r.z);}
// ----------------------------------
// fixed point 16:16
struct vec3x {
signed long x;
signed long y;
signed long z;
// add/subtract another vec2f
inline vec3x& operator+=(const vec3x & rhs) {x += rhs.x; y += rhs.y; z += rhs.z; return *this;}
inline vec3x& operator-=(const vec3x & rhs) {x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this;}
// multiply/divide by scalar
inline vec3x& operator*=(const float rhs) {
x = imul16(x, (signed long)(rhs*65536.0f));
y = imul16(y, (signed long)(rhs*65536.0f));
z = imul16(z, (signed long)(rhs*65536.0f));
return *this;
}
inline vec3x& operator/=(const float rhs) {
x = idiv16(x, (signed long)(rhs*65536.0f));
y = idiv16(y, (signed long)(rhs*65536.0f));
z = idiv16(z, (signed long)(rhs*65536.0f));
return *this;
}
// signs and other stuff
inline vec3x& operator-() {x = -x; y = -y; z = -z; return *this;}
// add/subtract two vec2f
inline const vec3x operator+(const vec3x& rhs) {return vec3x(*this) += rhs;}
inline const vec3x operator-(const vec3x& rhs) {return vec3x(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec3x operator*(const signed rhs) {return vec3x(*this) *= rhs;}
inline const vec3x operator/(const signed rhs) {return vec3x(*this) /= rhs;}
inline const vec3x operator*(const float rhs) {return vec3x(*this) *= rhs;}
inline const vec3x operator/(const float rhs) {return vec3x(*this) /= rhs;}
inline operator vec3f();
};
// dot product
inline float dot (const vec3x& lhs, const vec3x& rhs) {return (imul16(lhs.x, rhs.x) + imul16(lhs.y, rhs.y) + imul16(lhs.z, rhs.z));}
// cross product
inline vec3x cross(const vec3x& lhs, const vec3x& rhs) {
vec3x r = {imul16(lhs.y, rhs.z) - imul16(lhs.z, rhs.y),
imul16(lhs.z, rhs.x) - imul16(lhs.x, rhs.z),
imul16(lhs.x, rhs.y) - imul16(lhs.y, rhs.x)};
return r;
}
// length
inline float abs (const vec3x& lhs) {return sqrt(imul16(lhs.x, lhs.x) + imul16(lhs.y, lhs.y) + imul16(lhs.z, lhs.z));}
// norm
inline vec3x norm(const vec3x& lhs) {vec3x r = lhs; return (r / abs(r));}
// type conversions
inline vec3f::operator vec3x() {vec3x r; fist(&r.x, x*65536.0f); fist(&r.y, y*65536.0f); fist(&r.z, z*65536.0f); return r;}
inline vec3x::operator vec3f() {vec3f r = {((float)x / 65536.0f), ((float)y / 65536.0f), ((float)z / 65536.0f)}; return r;}
// ----------------------------------
// in fact these are homogenous vectors, i.e. most R4 vector rules are heavily simplified
struct vec4f {
union {float x; float b;};
union {float y; float g;};
union {float z; float r;};
union {float w; float a;};
// add/subtract another vec2f
inline vec4f& operator+=(const vec4f & rhs) {x += rhs.x; y += rhs.y; z += rhs.z; return *this;}
inline vec4f& operator-=(const vec4f & rhs) {x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this;}
// multiply/divide by scalar
inline vec4f& operator*=(const signed rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec4f& operator/=(const signed rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
inline vec4f& operator*=(const float rhs) {x *= rhs; y *= rhs; z *= rhs; return *this;}
inline vec4f& operator/=(const float rhs) {x /= rhs; y /= rhs; z /= rhs; return *this;}
// signs and other stuff
inline vec4f& operator-() {x = -x; y = -y; z = -z; return *this;}
// add/subtract two vec2f
inline const vec4f operator+(const vec4f& rhs) {return vec4f(*this) += rhs;}
inline const vec4f operator-(const vec4f& rhs) {return vec4f(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec4f operator*(const signed rhs) {return vec4f(*this) *= rhs;}
inline const vec4f operator/(const signed rhs) {return vec4f(*this) /= rhs;}
inline const vec4f operator*(const float rhs) {return vec4f(*this) *= rhs;}
inline const vec4f operator/(const float rhs) {return vec4f(*this) /= rhs;}
inline operator vec4x();
};
// dot product
inline float dot (const vec4f& lhs, const vec4f& rhs) {return (lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z);}
// cross product
inline vec4f cross(const vec4f& lhs, const vec4f& rhs) {
vec4f r = {lhs.y * rhs.z - lhs.z * rhs.y,
lhs.z * rhs.x - lhs.x * rhs.z,
lhs.x * rhs.y - lhs.y * rhs.x,
lhs.w};
return r;
}
// length
inline float abs (const vec4f& lhs) {return sqrt(lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z);}
// norm
inline vec4f norm (const vec4f& lhs) {vec4f r = lhs; return r*Q_rsqrt(r.x*r.x + r.y*r.y + r.z*r.z);}
// ----------------------------------
// fixed point 16:16
struct vec4x {
signed long x;
signed long y;
signed long z;
signed long w;
// add/subtract another vec2f
inline vec4x& operator+=(const vec4x & rhs) {x += rhs.x; y += rhs.y; z += rhs.z; return *this;}
inline vec4x& operator-=(const vec4x & rhs) {x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this;}
// multiply/divide by scalar
inline vec4x& operator*=(const float rhs) {
x = imul16(x, (signed long)(rhs*65536.0f));
y = imul16(y, (signed long)(rhs*65536.0f));
z = imul16(z, (signed long)(rhs*65536.0f));
return *this;
}
inline vec4x& operator/=(const float rhs) {
x = idiv16(x, (signed long)(rhs*65536.0f));
y = idiv16(y, (signed long)(rhs*65536.0f));
z = idiv16(z, (signed long)(rhs*65536.0f));
return *this;
}
// signs and other stuff
inline vec4x& operator-() {x = -x; y = -y; z = -z; return *this;}
// add/subtract two vec2f
inline const vec4x operator+(const vec4x& rhs) {return vec4x(*this) += rhs;}
inline const vec4x operator-(const vec4x& rhs) {return vec4x(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec4x operator*(const signed rhs) {return vec4x(*this) *= rhs;}
inline const vec4x operator/(const signed rhs) {return vec4x(*this) /= rhs;}
inline const vec4x operator*(const float rhs) {return vec4x(*this) *= rhs;}
inline const vec4x operator/(const float rhs) {return vec4x(*this) /= rhs;}
inline operator vec4f();
};
// dot product
inline float dot (const vec4x& lhs, const vec4x& rhs) {return (imul16(lhs.x, rhs.x) + imul16(lhs.y, rhs.y) + imul16(lhs.z, rhs.z));}
// cross product
inline vec4x cross(const vec4x& lhs, const vec4x& rhs) {
vec4x r = {imul16(lhs.y, rhs.z) - imul16(lhs.z, rhs.y),
imul16(lhs.z, rhs.x) - imul16(lhs.x, rhs.z),
imul16(lhs.x, rhs.y) - imul16(lhs.y, rhs.x),
lhs.w};
return r;
}
// length
inline float abs (const vec4x& lhs) {return sqrt(imul16(lhs.x, lhs.x) + imul16(lhs.y, lhs.y) + imul16(lhs.z, lhs.z));}
// norm
inline vec4x norm(const vec4x& lhs) {vec4x r = lhs; return (r / abs(r));}
// ----------------------------------
// integer
struct vec4i {
signed long x;
signed long y;
signed long z;
signed long w;
// add/subtract another vec2f
inline vec4i& operator+=(const vec4i & rhs) {x += rhs.x; y += rhs.y; z += rhs.z; w += rhs.w; return *this;}
inline vec4i& operator-=(const vec4i & rhs) {x -= rhs.x; y -= rhs.y; z -= rhs.z; w += rhs.w; return *this;}
// multiply/divide by scalar
inline vec4i& operator*=(const float rhs) {
x *= rhs;
y *= rhs;
z *= rhs;
w *= rhs;
return *this;
}
inline vec4i& operator/=(const float rhs) {
x /= rhs;
y /= rhs;
z /= rhs;
w /= rhs;
return *this;
}
// signs and other stuff
inline vec4i& operator-() {x = -x; y = -y; z = -z; w = -w; return *this;}
// add/subtract two vec2f
inline const vec4i operator+(const vec4i& rhs) {return vec4i(*this) += rhs;}
inline const vec4i operator-(const vec4i& rhs) {return vec4i(*this) -= rhs;}
// multiply/divide by scalar and throw to another vec2f
inline const vec4i operator*(const signed rhs) {return vec4i(*this) *= rhs;}
inline const vec4i operator/(const signed rhs) {return vec4i(*this) /= rhs;}
inline const vec4i operator*(const float rhs) {return vec4i(*this) *= rhs;}
inline const vec4i operator/(const float rhs) {return vec4i(*this) /= rhs;}
inline operator vec4f();
};
// dot product
inline float dot (const vec4i& lhs, const vec4i& rhs) {return (imul16(lhs.x, rhs.x) + imul16(lhs.y, rhs.y) + imul16(lhs.z, rhs.z));}
// cross product
inline vec4x cross(const vec4i& lhs, const vec4i& rhs) {
vec4x r = {imul16(lhs.y, rhs.z) - imul16(lhs.z, rhs.y),
imul16(lhs.z, rhs.x) - imul16(lhs.x, rhs.z),
imul16(lhs.x, rhs.y) - imul16(lhs.y, rhs.x),
lhs.w};
return r;
}
// length
inline float abs (const vec4i& lhs) {return sqrt(imul16(lhs.x, lhs.x) + imul16(lhs.y, lhs.y) + imul16(lhs.z, lhs.z));}
// norm
inline vec4i norm(const vec4i& lhs) {vec4i r = lhs; return (r / abs(r));}
// type conversions
inline vec4f::operator vec4x() {vec4x r; fist(&r.x, x*65536.0f); fist(&r.y, y*65536.0f); fist(&r.z, z*65536.0f); fist(&r.w, w*65536.0f); return r;}
inline vec4x::operator vec4f() {vec4f r = {((float)x / 65536.0f), ((float)y / 65536.0f), ((float)z / 65536.0f), ((float)w / 65536.0f)}; return r;}
#endif