forked from whoozle/clunk
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathv3.h
528 lines (455 loc) · 13.7 KB
/
v3.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#ifndef CLUNK_V3_H__
#define CLUNK_V3_H__
/* libClunk - cross-platform 3D audio API built on top SDL library
* Copyright (C) 2007-2008 Netive Media Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#if !(defined(__GNUC__) || defined(__GNUG__) || defined(__attribute__))
# define __attribute__(p) /* nothing */
#endif
namespace clunk {
/*!
\brief 3d vector
\tparam T type of the axis. usually int or float.
*/
template <typename T> class v3 {
public:
///x component
T x;
///y component
T y;
///z component
T z;
///default ctor: initializes all components with zeroes.
inline v3<T>() : x(0), y(0), z(0) {}
///initializes all components with given values
inline v3<T>(const T x, const T y, const T z) : x(x), y(y), z(z) {}
///nullify vector
inline void clear() { x = y = z = 0; }
///returns true if x == y == z == 0 ?
inline const bool is0() const {
return x== 0 && y == 0 && z == 0;
}
/*!
\brief normalizes vector.
\return old length of this vector
*/
inline const T normalize() {
const T len = length();
if (len == (T)0 || len ==(T)1)
return len;
x /= len;
y /= len;
z /= len;
return len;
}
/*!
\brief normalizes vector with given length
\param[in] nlen length
\return old length of this vector
*/
inline const T normalize(const T nlen) {
const T len = length();
if (len == (T)0 || len == nlen)
return len;
x *= nlen / len;
y *= nlen / len;
z *= nlen / len;
return len;
}
inline const T dot_product(const v3<T> &v) const {
return x * v.x + y * v.y + z * v.z;
}
///returns length of this vector
inline const T length() const {
const T ql = x * x + y * y + z * z;
if (ql == (T)0 || ql == (T)1)
return ql;
return (T)sqrt(ql);
}
///returns square of length. To avoid sqrt if needed.
inline const T quick_length() const {
return (T)(x * x + y * y + z * z);
}
///converts to vector of another type
template <typename T2>
inline v3<T2> convert() const { return v3<T2>((T2)x, (T2)y, (T2)z); }
///returns distance between two points
inline const T distance(const v3<T>& other) const {
v3<T>d(*this);
d -= other;
return d.length();
}
///return square distance between two points
inline const T quick_distance(const v3<T>& other) const {
const T dx = x - other.x;
const T dy = y - other.y;
const T dz = z - other.z;
return (dx * dx + dy * dy + dz * dz);
}
///allows v3 be placed in sorted STL container such std::map
inline const bool operator<(const v3<T> &other) const {
if (x != other.x) {
return x < other.x;
}
if (y != other.y) {
return y < other.y;
}
return z < other.z;
}
///negate all components
inline const v3<T> operator-() const {
return v3<T>(-x, -y, -z);
}
///test equality
inline const bool operator==(const v3<T> &other) const {
return x == other.x && y == other.y && z == other.z;
}
///test inequality
inline const bool operator!=(const v3<T> &other) const {
return x != other.x || y != other.y || z != other.z;
}
///adds another vector
inline const v3<T>& operator+=(const v3<T>& other) {
x += other.x; y += other.y; z += other.z;
return *this;
}
///substracts another vector
inline const v3<T>& operator-=(const v3<T>& other) {
x -= other.x; y -= other.y; z -= other.z;
return *this;
}
///multiplies another vector
inline const v3<T>& operator*=(const v3<T>& other) {
x *= other.x; y *= other.y; z *= other.z;
return *this;
}
///divide with another vector
inline const v3<T>& operator/=(const v3<T>& other) {
x /= other.x; y /= other.y; z /= other.z;
return *this;
}
///multiplication
inline const v3<T> operator*(const v3<T>& other) const {
return v3<T>(x * other.x, y * other.y, z * other.z);
}
///summing
inline const v3<T> operator+(const v3<T>& other) const {
return v3<T>(x + other.x, y + other.y, z + other.z);
}
///substraction
inline const v3<T> operator-(const v3<T>& other) const {
return v3<T>(x - other.x, y - other.y, z - other.z);
}
///division
inline const v3<T> operator/(const v3<T>& other) const {
return v3<T>(x / other.x, y / other.y, z / other.z);
}
///multiplies all components with constant
inline const v3<T> operator*(const T& other) const {
return v3<T>(x * other, y * other, z * other);
}
///sums all components with constant
inline const v3<T> operator+(const T& other) const {
return v3<T>(x + other, y + other, z + other);
}
///substracts all components with constant
inline const v3<T> operator-(const T& other) const {
return v3<T>(x - other, y - other, z - other);
}
///divides all components by constant
inline const v3<T> operator/(const T& other) const {
return v3<T>(x / other, y / other, z / other);
}
///divides this vector by constant
inline const v3<T>& operator/=(const T& other) {
x /= other;
y /= other;
z /= other;
return *this;
}
///multiplies this vector with constant
inline const v3<T>& operator*=(const T& other) {
x *= other;
y *= other;
z *= other;
return *this;
}
///sums this vector with constant
inline const v3<T>& operator+=(const T& other) {
x += other;
y += other;
z += other;
return *this;
}
///substracts this vector with constant
inline const v3<T>& operator-=(const T& other) {
x -= other;
y -= other;
z -= other;
return *this;
}
///adds constant to the vector
inline const v3<T> operator+(const T a) {
return v3<T>(x + a, y + a, z + a);
}
///subs constant from the vector
inline const v3<T> operator-(const T a) {
return v3<T>(x - a, y - a, z - a);
}
///muls constant to the vector
inline const v3<T> operator*(const T a) {
return v3<T>(x * a, y * a, z * a);
}
///divs constant to the vector
inline const v3<T> operator/(const T a) {
return v3<T>(x / a, y / a, z / a);
}
};
///external operator+
template <typename T>
inline const v3<T> operator+(const T a, const v3<T> &v) {
return v3<T>(v.x + a, v.y + a, v.z + a);
}
///external operator-
template <typename T>
inline const v3<T> operator-(const T a, const v3<T> &v) {
return v3<T>(a - v.x, a - v.y, a - v.z);
}
///external operator*
template <typename T>
inline const v3<T> operator*(const T a, const v3<T> &v) {
return v3<T>(v.x * a, v.y * a, v.z * a);
}
///external operator/
template <typename T>
inline const v3<T> operator/(const T a, const v3<T> &v) {
return v3<T>(a / v.x, a / v.y, a / v.z);
}
#if 0 //defined CLUNK_USES_SSE
# include <xmmintrin.h>
template <>
class v3<float> {
__m128 value;
public:
template <int POS>
class float_placeholder {
__m128 &value;
public:
inline float_placeholder(__m128 &value) __attribute__((__always_inline__)) : value(value) {}
inline float get() const __attribute__((__always_inline__)) {
float r[4];
_mm_storeu_ps(r, value);
return r[POS];
}
inline void set(float v) __attribute__((__always_inline__)) {
__m128 mask = _mm_cmpgt_ps(_mm_set_ps(POS == 2?1:0, POS == 1?1:0, POS == 0?1:0, 0), _mm_setzero_ps());
value = _mm_or_ps(
_mm_andnot_ps(mask, value),
_mm_and_ps(mask, _mm_set1_ps(v))
);
}
inline operator float() const __attribute__((__always_inline__)) {
return get();
}
inline const float_placeholder<POS>& operator=(const float_placeholder<POS> &other) __attribute__((__always_inline__)) {
set(other.get());
return *this;
}
inline const float_placeholder<POS>& operator=(float v) __attribute__((__always_inline__)) {
set(v);
return *this;
}
};
float_placeholder<0> x;
float_placeholder<1> y;
float_placeholder<2> z;
inline v3<float>(): value(_mm_setzero_ps()), x(value), y(value), z(value) {}
inline v3<float>(__m128 v): value(v), x(value), y(value), z(value) {}
inline v3<float>(const float xv, const float yv, const float zv) :
value(_mm_set_ps(zv, yv, xv, 0)), x(value), y(value), z(value) {}
inline void clear() { value = _mm_setzero_ps(); }
inline const bool is0() const {
return (_mm_movemask_ps(_mm_cmpeq_ps(value, _mm_setzero_ps())) & 7 ) == 7;
}
inline const float normalize() {
float len = length();
if (len == (float)0 || len ==(float)1)
return len;
value = _mm_div_ps(value, _mm_set1_ps(len));
return len;
}
inline const float normalize(const float nlen) {
const float len = length();
if (len == (float)0 || len == nlen)
return len;
__m128 k = _mm_div_ps(_mm_set1_ps(nlen), _mm_set1_ps(len));
value = _mm_div_ps(value, k);
return len;
}
inline const float dot_product(const v3<float> &v) const {
float fixme[4];
_mm_storeu_ps(fixme, _mm_mul_ps(value, v.value));
return fixme[0] + fixme[1] + fixme[2] + fixme[3];
}
inline const float length() const {
float v[4];
_mm_storeu_ps(v, _mm_mul_ps(value, value));
const float ql = v[0] + v[1] + v[2];
if (ql == 0 || ql == 1.0f)
return ql;
return (float)sqrt(ql);
}
inline float quick_length() const {
float v[4];
_mm_storeu_ps(v, _mm_mul_ps(value, value));
return v[0] + v[1] + v[2];
}
template <typename T2>
inline v3<T2> convert() const {
float v[4];
_mm_storeu_ps(v, value);
return v3<T2>((T2)v[0], (T2)v[1], (T2)v[2]);
}
inline const float distance(const v3<float>& other) const {
v3<float>d(*this);
d -= other;
return d.length();
}
inline float quick_distance(const v3<float>& other) const {
float v[4];
__m128 dv = _mm_sub_ps(value, other.value);
_mm_storeu_ps(v, _mm_mul_ps(dv, dv));
return v[0] + v[1] + v[2];
}
inline const bool operator<(const v3<float> &other) const {
int c = _mm_movemask_ps(_mm_cmpneq_ps(value, other.value));
int l = _mm_movemask_ps(_mm_cmplt_ps(value, other.value));
if (c & 1) {
return l & 1;
}
if (c & 2) {
return l & 2;
}
return l & 4;
}
inline const v3<float> operator-() const {
return _mm_sub_ps(_mm_setzero_ps(), value);
}
inline const bool operator==(const v3<float> &other) const {
int mask = _mm_movemask_ps(_mm_cmpeq_ps(value, other.value));
return (mask & 7) == 7;
}
///test inequality
inline const bool operator!=(const v3<float> &other) const {
int mask = _mm_movemask_ps(_mm_cmpeq_ps(value, other.value));
return (mask & 7) != 7;
}
///adds another vector
inline const v3<float>& operator+=(const v3<float>& other) {
value = _mm_add_ps(value, other.value);
return *this;
}
///substracts another vector
inline const v3<float>& operator-=(const v3<float>& other) {
value = _mm_sub_ps(value, other.value);
return *this;
}
///multiplies another vector
inline const v3<float>& operator*=(const v3<float>& other) {
value = _mm_mul_ps(value, other.value);
return *this;
}
///divide with another vector
inline const v3<float>& operator/=(const v3<float>& other) {
value = _mm_div_ps(value, other.value);
return *this;
}
///multiplication
inline const v3<float> operator*(const v3<float>& other) const {
return v3<float>(_mm_mul_ps(value, other.value));
}
///summing
inline const v3<float> operator+(const v3<float>& other) const {
return v3<float>(_mm_add_ps(value, other.value));
}
///substraction
inline const v3<float> operator-(const v3<float>& other) const {
return v3<float>(_mm_sub_ps(value, other.value));
}
///division
inline const v3<float> operator/(const v3<float>& other) const {
return v3<float>(_mm_div_ps(value, other.value));
}
///multiplies all components with constant
inline const v3<float> operator*(const float& other) const {
return v3<float>(_mm_mul_ps(value, _mm_set1_ps(other)));
}
///sums all components with constant
inline const v3<float> operator+(const float& other) const {
return v3<float>(_mm_add_ps(value, _mm_set1_ps(other)));
}
///substracts all components with constant
inline const v3<float> operator-(const float& other) const {
return v3<float>(_mm_sub_ps(value, _mm_set1_ps(other)));
}
///divides all components by constant
inline const v3<float> operator/(const float& other) const {
return v3<float>(_mm_div_ps(value, _mm_set1_ps(other)));
}
///divides this vector by constant
inline const v3<float>& operator/=(const float& other) {
value = _mm_div_ps(value, _mm_set1_ps(other));
return *this;
}
///multiplies this vector with constant
inline const v3<float>& operator*=(const float& other) {
value = _mm_mul_ps(value, _mm_set1_ps(other));
return *this;
}
///sums this vector with constant
inline const v3<float>& operator+=(const float& other) {
value = _mm_add_ps(value, _mm_set1_ps(other));
return *this;
}
///substracts this vector with constant
inline const v3<float>& operator-=(const float& other) {
value = _mm_sub_ps(value, _mm_set1_ps(other));
return *this;
}
///adds constant to the vector
inline const v3<float> operator+(const float a) {
return v3<float>(_mm_add_ps(value, _mm_set1_ps(a)));
}
///subs constant from the vector
inline const v3<float> operator-(const float a) {
return v3<float>(_mm_sub_ps(value, _mm_set1_ps(a)));
}
///muls constant to the vector
inline const v3<float> operator*(const float a) {
return v3<float>(_mm_mul_ps(value, _mm_set1_ps(a)));
}
///divs constant to the vector
inline const v3<float> operator/(const float a) {
return v3<float>(_mm_div_ps(value, _mm_set1_ps(a)));
}
};
#endif //CLUNK_USES_SSE
} //namespace clunk
#endif