29 #define PURE __attribute__((pure)) 36 template <
typename TYPE,
size_t SIZE>
39 template <
typename TYPE,
size_t SIZE>
44 template <
typename T>
inline T min(T a, T b) {
return a<b ? a : b; }
45 template <
typename T>
inline T max(T a, T b) {
return a>b ? a : b; }
47 template <
template<
typename T,
size_t S>
class VEC,
48 typename TYPE,
size_t SIZE,
size_t S>
51 const size_t minSize = min(SIZE, S);
52 const size_t maxSize = max(SIZE, S);
53 for (
size_t i=0 ; i<minSize ; i++)
55 for (
size_t i=minSize ; i<maxSize ; i++)
62 template<
typename T,
size_t S>
class VLHS,
63 template<
typename T,
size_t S>
class VRHS,
67 VLHS<TYPE, SIZE> PURE doAdd(
68 const VLHS<TYPE, SIZE>& lhs,
69 const VRHS<TYPE, SIZE>& rhs) {
71 for (
size_t i=0 ; i<SIZE ; i++)
72 r[i] = lhs[i] + rhs[i];
77 template<
typename T,
size_t S>
class VLHS,
78 template<
typename T,
size_t S>
class VRHS,
82 VLHS<TYPE, SIZE> PURE doSub(
83 const VLHS<TYPE, SIZE>& lhs,
84 const VRHS<TYPE, SIZE>& rhs) {
86 for (
size_t i=0 ; i<SIZE ; i++)
87 r[i] = lhs[i] - rhs[i];
92 template<
typename T,
size_t S>
class VEC,
96 VEC<TYPE, SIZE> PURE doMulScalar(
97 const VEC<TYPE, SIZE>& lhs,
98 typename TypeTraits<TYPE>::ParameterType rhs) {
100 for (
size_t i=0 ; i<SIZE ; i++)
106 template<
typename T,
size_t S>
class VEC,
110 VEC<TYPE, SIZE> PURE doScalarMul(
111 typename TypeTraits<TYPE>::ParameterType lhs,
112 const VEC<TYPE, SIZE>& rhs) {
114 for (
size_t i=0 ; i<SIZE ; i++)
140 template<
typename T,
size_t S>
class VLHS,
141 template<
typename T,
size_t S>
class VRHS,
145 inline VLHS<TYPE, SIZE> PURE operator + (
146 const VLHS<TYPE, SIZE>& lhs,
147 const VRHS<TYPE, SIZE>& rhs) {
148 return helpers::doAdd(lhs, rhs);
152 template<
typename T,
size_t S>
class VLHS,
153 template<
typename T,
size_t S>
class VRHS,
157 inline VLHS<TYPE, SIZE> PURE operator - (
158 const VLHS<TYPE, SIZE>& lhs,
159 const VRHS<TYPE, SIZE>& rhs) {
160 return helpers::doSub(lhs, rhs);
164 template<
typename T,
size_t S>
class VEC,
168 inline VEC<TYPE, SIZE> PURE operator * (
169 const VEC<TYPE, SIZE>& lhs,
170 typename TypeTraits<TYPE>::ParameterType rhs) {
171 return helpers::doMulScalar(lhs, rhs);
175 template<
typename T,
size_t S>
class VEC,
179 inline VEC<TYPE, SIZE> PURE operator * (
180 typename TypeTraits<TYPE>::ParameterType lhs,
181 const VEC<TYPE, SIZE>& rhs) {
182 return helpers::doScalarMul(lhs, rhs);
187 template<
typename T,
size_t S>
class VLHS,
188 template<
typename T,
size_t S>
class VRHS,
192 TYPE PURE dot_product(
193 const VLHS<TYPE, SIZE>& lhs,
194 const VRHS<TYPE, SIZE>& rhs) {
196 for (
size_t i=0 ; i<SIZE ; i++)
197 r += lhs[i] * rhs[i];
202 template<
typename T,
size_t S>
class V,
206 TYPE PURE length(
const V<TYPE, SIZE>& v) {
207 return sqrt(dot_product(v, v));
211 template<
typename T,
size_t S>
class V,
215 TYPE PURE length_squared(
const V<TYPE, SIZE>& v) {
216 return dot_product(v, v);
220 template<
typename T,
size_t S>
class V,
224 V<TYPE, SIZE> PURE normalize(
const V<TYPE, SIZE>& v) {
225 return v * (1/length(v));
229 template<
typename T,
size_t S>
class VLHS,
230 template<
typename T,
size_t S>
class VRHS,
233 VLHS<TYPE, 3> PURE cross_product(
234 const VLHS<TYPE, 3>& u,
235 const VRHS<TYPE, 3>& v) {
237 r.x = u.y*v.z - u.z*v.y;
238 r.y = u.z*v.x - u.x*v.z;
239 r.z = u.x*v.y - u.y*v.x;
244 template <
typename TYPE,
size_t SIZE>
247 for (
size_t i=0 ; i<SIZE ; i++)
257 template <
typename TYPE,
size_t SIZE>
260 inline const TYPE& operator[](
size_t i)
const {
return v[i]; }
261 inline TYPE& operator[](
size_t i) {
return v[i]; }
266 struct {
float x, y; };
267 struct {
float s, t; };
269 inline const float& operator[](
size_t i)
const {
return v[i]; }
270 inline float& operator[](
size_t i) {
return v[i]; }
275 struct {
float x, y, z; };
276 struct {
float s, t, r; };
280 inline const float& operator[](
size_t i)
const {
return v[i]; }
281 inline float& operator[](
size_t i) {
return v[i]; }
286 struct {
float x, y, z, w; };
287 struct {
float s, t, r, q; };
293 inline const float& operator[](
size_t i)
const {
return v[i]; }
294 inline float& operator[](
size_t i) {
return v[i]; }
299 template <
typename TYPE,
size_t SIZE>
300 class vec :
public vbase<TYPE, SIZE>
302 typedef typename TypeTraits<TYPE>::ParameterType pTYPE;
307 typedef TYPE value_type;
308 typedef TYPE& reference;
309 typedef TYPE
const& const_reference;
310 typedef size_t size_type;
312 typedef TYPE* iterator;
313 typedef TYPE
const* const_iterator;
314 iterator begin() {
return base::v; }
315 iterator end() {
return base::v + SIZE; }
316 const_iterator begin()
const {
return base::v; }
317 const_iterator end()
const {
return base::v + SIZE; }
318 size_type size()
const {
return SIZE; }
324 vec(
const vec& rhs) : base(rhs) { }
325 vec(
const base& rhs) : base(rhs) { }
331 for (
size_t i=0 ; i<SIZE ; i++)
332 base::operator[](i) = rhs;
335 template <
template<
typename T,
size_t S>
class VEC,
size_t S>
336 explicit vec(
const VEC<TYPE, S>& rhs) {
337 helpers::doAssign(*
this, rhs);
340 explicit vec(TYPE
const* array) {
341 for (
size_t i=0 ; i<SIZE ; i++)
342 base::operator[](i) = array[i];
348 vec& operator = (
const vec& rhs) {
349 base::operator=(rhs);
353 vec& operator = (
const base& rhs) {
354 base::operator=(rhs);
358 vec& operator = (pTYPE rhs) {
359 for (
size_t i=0 ; i<SIZE ; i++)
360 base::operator[](i) = rhs;
364 template <
template<
typename T,
size_t S>
class VEC,
size_t S>
365 vec& operator = (
const VEC<TYPE, S>& rhs) {
366 return helpers::doAssign(*
this, rhs);
372 vec& operator += (
const vec& rhs);
373 vec& operator -= (
const vec& rhs);
374 vec& operator *= (pTYPE rhs);
385 friend inline vec PURE operator + (
const vec& lhs,
const vec& rhs) {
386 return helpers::doAdd(lhs, rhs);
388 friend inline vec PURE operator - (
const vec& lhs,
const vec& rhs) {
389 return helpers::doSub(lhs, rhs);
391 friend inline vec PURE operator * (
const vec& lhs, pTYPE v) {
392 return helpers::doMulScalar(lhs, v);
394 friend inline vec PURE operator * (pTYPE v,
const vec& rhs) {
395 return helpers::doScalarMul(v, rhs);
397 friend inline TYPE PURE dot_product(
const vec& lhs,
const vec& rhs) {
398 return android::dot_product(lhs, rhs);
404 template <
typename TYPE,
size_t SIZE>
407 for (
size_t i=0 ; i<SIZE ; i++)
412 template <
typename TYPE,
size_t SIZE>
415 for (
size_t i=0 ; i<SIZE ; i++)
420 template <
typename TYPE,
size_t SIZE>
423 for (
size_t i=0 ; i<SIZE ; i++)