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
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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 ANDROID_VEC_H
#define ANDROID_VEC_H
#include <math.h>
#include <stdint.h>
#include <stddef.h>
#include "traits.h"
// -----------------------------------------------------------------------
#define PURE __attribute__((pure))
namespace android {
// -----------------------------------------------------------------------
// non-inline helpers
template <typename TYPE, size_t SIZE>
class vec;
template <typename TYPE, size_t SIZE>
class vbase;
namespace helpers {
template <typename T> inline T min(T a, T b) { return a<b ? a : b; }
template <typename T> inline T max(T a, T b) { return a>b ? a : b; }
template < template<typename T, size_t S> class VEC,
typename TYPE, size_t SIZE, size_t S>
vec<TYPE, SIZE>& doAssign(
vec<TYPE, SIZE>& lhs, const VEC<TYPE, S>& rhs) {
const size_t minSize = min(SIZE, S);
const size_t maxSize = max(SIZE, S);
for (size_t i=0 ; i<minSize ; i++)
lhs[i] = rhs[i];
for (size_t i=minSize ; i<maxSize ; i++)
lhs[i] = 0;
return lhs;
}
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE,
size_t SIZE
>
VLHS<TYPE, SIZE> PURE doAdd(
const VLHS<TYPE, SIZE>& lhs,
const VRHS<TYPE, SIZE>& rhs) {
VLHS<TYPE, SIZE> r;
for (size_t i=0 ; i<SIZE ; i++)
r[i] = lhs[i] + rhs[i];
return r;
}
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE,
size_t SIZE
>
VLHS<TYPE, SIZE> PURE doSub(
const VLHS<TYPE, SIZE>& lhs,
const VRHS<TYPE, SIZE>& rhs) {
VLHS<TYPE, SIZE> r;
for (size_t i=0 ; i<SIZE ; i++)
r[i] = lhs[i] - rhs[i];
return r;
}
template <
template<typename T, size_t S> class VEC,
typename TYPE,
size_t SIZE
>
VEC<TYPE, SIZE> PURE doMulScalar(
const VEC<TYPE, SIZE>& lhs,
typename TypeTraits<TYPE>::ParameterType rhs) {
VEC<TYPE, SIZE> r;
for (size_t i=0 ; i<SIZE ; i++)
r[i] = lhs[i] * rhs;
return r;
}
template <
template<typename T, size_t S> class VEC,
typename TYPE,
size_t SIZE
>
VEC<TYPE, SIZE> PURE doScalarMul(
typename TypeTraits<TYPE>::ParameterType lhs,
const VEC<TYPE, SIZE>& rhs) {
VEC<TYPE, SIZE> r;
for (size_t i=0 ; i<SIZE ; i++)
r[i] = lhs * rhs[i];
return r;
}
}; // namespace helpers
// -----------------------------------------------------------------------
// Below we define the mathematical operators for vectors.
// We use template template arguments so we can generically
// handle the case where the right-hand-size and left-hand-side are
// different vector types (but with same value_type and size).
// This is needed for performance when using ".xy{z}" element access
// on vec<>. Without this, an extra conversion to vec<> would be needed.
//
// example:
// vec4_t a;
// vec3_t b;
// vec3_t c = a.xyz + b;
//
// "a.xyz + b" is a mixed-operation between a vbase<> and a vec<>, requiring
// a conversion of vbase<> to vec<>. The template gunk below avoids this,
// by allowing the addition on these different vector types directly
//
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE,
size_t SIZE
>
inline VLHS<TYPE, SIZE> PURE operator + (
const VLHS<TYPE, SIZE>& lhs,
const VRHS<TYPE, SIZE>& rhs) {
return helpers::doAdd(lhs, rhs);
}
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE,
size_t SIZE
>
inline VLHS<TYPE, SIZE> PURE operator - (
const VLHS<TYPE, SIZE>& lhs,
const VRHS<TYPE, SIZE>& rhs) {
return helpers::doSub(lhs, rhs);
}
template <
template<typename T, size_t S> class VEC,
typename TYPE,
size_t SIZE
>
inline VEC<TYPE, SIZE> PURE operator * (
const VEC<TYPE, SIZE>& lhs,
typename TypeTraits<TYPE>::ParameterType rhs) {
return helpers::doMulScalar(lhs, rhs);
}
template <
template<typename T, size_t S> class VEC,
typename TYPE,
size_t SIZE
>
inline VEC<TYPE, SIZE> PURE operator * (
typename TypeTraits<TYPE>::ParameterType lhs,
const VEC<TYPE, SIZE>& rhs) {
return helpers::doScalarMul(lhs, rhs);
}
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE,
size_t SIZE
>
TYPE PURE dot_product(
const VLHS<TYPE, SIZE>& lhs,
const VRHS<TYPE, SIZE>& rhs) {
TYPE r(0);
for (size_t i=0 ; i<SIZE ; i++)
r += lhs[i] * rhs[i];
return r;
}
template <
template<typename T, size_t S> class V,
typename TYPE,
size_t SIZE
>
TYPE PURE length(const V<TYPE, SIZE>& v) {
return sqrt(dot_product(v, v));
}
template <
template<typename T, size_t S> class V,
typename TYPE,
size_t SIZE
>
TYPE PURE length_squared(const V<TYPE, SIZE>& v) {
return dot_product(v, v);
}
template <
template<typename T, size_t S> class V,
typename TYPE,
size_t SIZE
>
V<TYPE, SIZE> PURE normalize(const V<TYPE, SIZE>& v) {
return v * (1/length(v));
}
template <
template<typename T, size_t S> class VLHS,
template<typename T, size_t S> class VRHS,
typename TYPE
>
VLHS<TYPE, 3> PURE cross_product(
const VLHS<TYPE, 3>& u,
const VRHS<TYPE, 3>& v) {
VLHS<TYPE, 3> r;
r.x = u.y*v.z - u.z*v.y;
r.y = u.z*v.x - u.x*v.z;
r.z = u.x*v.y - u.y*v.x;
return r;
}
template <typename TYPE, size_t SIZE>
vec<TYPE, SIZE> PURE operator - (const vec<TYPE, SIZE>& lhs) {
vec<TYPE, SIZE> r;
for (size_t i=0 ; i<SIZE ; i++)
r[i] = -lhs[i];
return r;
}
// -----------------------------------------------------------------------
// This our basic vector type, it just implements the data storage
// and accessors.
template <typename TYPE, size_t SIZE>
struct vbase {
TYPE v[SIZE];
inline const TYPE& operator[](size_t i) const { return v[i]; }
inline TYPE& operator[](size_t i) { return v[i]; }
};
template<> struct vbase<float, 2> {
union {
float v[2];
struct { float x, y; };
struct { float s, t; };
};
inline const float& operator[](size_t i) const { return v[i]; }
inline float& operator[](size_t i) { return v[i]; }
};
template<> struct vbase<float, 3> {
union {
float v[3];
struct { float x, y, z; };
struct { float s, t, r; };
vbase<float, 2> xy;
vbase<float, 2> st;
};
inline const float& operator[](size_t i) const { return v[i]; }
inline float& operator[](size_t i) { return v[i]; }
};
template<> struct vbase<float, 4> {
union {
float v[4];
struct { float x, y, z, w; };
struct { float s, t, r, q; };
vbase<float, 3> xyz;
vbase<float, 3> str;
vbase<float, 2> xy;
vbase<float, 2> st;
};
inline const float& operator[](size_t i) const { return v[i]; }
inline float& operator[](size_t i) { return v[i]; }
};
// -----------------------------------------------------------------------
template <typename TYPE, size_t SIZE>
class vec : public vbase<TYPE, SIZE>
{
typedef typename TypeTraits<TYPE>::ParameterType pTYPE;
typedef vbase<TYPE, SIZE> base;
public:
// STL-like interface.
typedef TYPE value_type;
typedef TYPE& reference;
typedef TYPE const& const_reference;
typedef size_t size_type;
typedef TYPE* iterator;
typedef TYPE const* const_iterator;
iterator begin() { return base::v; }
iterator end() { return base::v + SIZE; }
const_iterator begin() const { return base::v; }
const_iterator end() const { return base::v + SIZE; }
size_type size() const { return SIZE; }
// -----------------------------------------------------------------------
// default constructors
vec() { }
vec(const vec& rhs) : base(rhs) { }
vec(const base& rhs) : base(rhs) { }
// -----------------------------------------------------------------------
// conversion constructors
vec(pTYPE rhs) {
for (size_t i=0 ; i<SIZE ; i++)
base::operator[](i) = rhs;
}
template < template<typename T, size_t S> class VEC, size_t S>
explicit vec(const VEC<TYPE, S>& rhs) {
helpers::doAssign(*this, rhs);
}
explicit vec(TYPE const* array) {
for (size_t i=0 ; i<SIZE ; i++)
base::operator[](i) = array[i];
}
// -----------------------------------------------------------------------
// Assignment
vec& operator = (const vec& rhs) {
base::operator=(rhs);
return *this;
}
vec& operator = (const base& rhs) {
base::operator=(rhs);
return *this;
}
vec& operator = (pTYPE rhs) {
for (size_t i=0 ; i<SIZE ; i++)
base::operator[](i) = rhs;
return *this;
}
template < template<typename T, size_t S> class VEC, size_t S>
vec& operator = (const VEC<TYPE, S>& rhs) {
return helpers::doAssign(*this, rhs);
}
// -----------------------------------------------------------------------
// operation-assignment
vec& operator += (const vec& rhs);
vec& operator -= (const vec& rhs);
vec& operator *= (pTYPE rhs);
// -----------------------------------------------------------------------
// non-member function declaration and definition
// NOTE: we declare the non-member function as friend inside the class
// so that they are known to the compiler when the class is instantiated.
// This helps the compiler doing template argument deduction when the
// passed types are not identical. Essentially this helps with
// type conversion so that you can multiply a vec<float> by an scalar int
// (for instance).
friend inline vec PURE operator + (const vec& lhs, const vec& rhs) {
return helpers::doAdd(lhs, rhs);
}
friend inline vec PURE operator - (const vec& lhs, const vec& rhs) {
return helpers::doSub(lhs, rhs);
}
friend inline vec PURE operator * (const vec& lhs, pTYPE v) {
return helpers::doMulScalar(lhs, v);
}
friend inline vec PURE operator * (pTYPE v, const vec& rhs) {
return helpers::doScalarMul(v, rhs);
}
friend inline TYPE PURE dot_product(const vec& lhs, const vec& rhs) {
return android::dot_product(lhs, rhs);
}
};
// -----------------------------------------------------------------------
template <typename TYPE, size_t SIZE>
vec<TYPE, SIZE>& vec<TYPE, SIZE>::operator += (const vec<TYPE, SIZE>& rhs) {
vec<TYPE, SIZE>& lhs(*this);
for (size_t i=0 ; i<SIZE ; i++)
lhs[i] += rhs[i];
return lhs;
}
template <typename TYPE, size_t SIZE>
vec<TYPE, SIZE>& vec<TYPE, SIZE>::operator -= (const vec<TYPE, SIZE>& rhs) {
vec<TYPE, SIZE>& lhs(*this);
for (size_t i=0 ; i<SIZE ; i++)
lhs[i] -= rhs[i];
return lhs;
}
template <typename TYPE, size_t SIZE>
vec<TYPE, SIZE>& vec<TYPE, SIZE>::operator *= (vec<TYPE, SIZE>::pTYPE rhs) {
vec<TYPE, SIZE>& lhs(*this);
for (size_t i=0 ; i<SIZE ; i++)
lhs[i] *= rhs;
return lhs;
}
// -----------------------------------------------------------------------
typedef vec<float, 2> vec2_t;
typedef vec<float, 3> vec3_t;
typedef vec<float, 4> vec4_t;
// -----------------------------------------------------------------------
}; // namespace android
#endif /* ANDROID_VEC_H */
|