source: trunk/src/lol/math/vector.h @ 1148

Last change on this file since 1148 was 1148, checked in by sam, 11 years ago

math: allow vectors of half and real; they don't support swizzling or rgba
getter aliases.

  • Property svn:keywords set to Id
File size: 42.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The vector, complex, quaternion and matrix classes
13// --------------------------------------------------
14//
15
16#if !defined __LOL_MATH_VECTOR_H__
17#define __LOL_MATH_VECTOR_H__
18
19#include <stdint.h>
20#include <cmath>
21#if !defined __ANDROID__
22#   include <iostream>
23#endif
24
25#include "half.h"
26#include "lol/math/real.h"
27
28namespace lol
29{
30
31#define DECLARE_VECTOR_TYPEDEFS(tname, suffix) \
32    template <typename T> struct tname; \
33    typedef tname<half> f16##suffix; \
34    typedef tname<float> suffix; \
35    typedef tname<double> f64##suffix; \
36    typedef tname<int8_t> i8##suffix; \
37    typedef tname<uint8_t> u8##suffix; \
38    typedef tname<int16_t> i16##suffix; \
39    typedef tname<uint16_t> u16##suffix; \
40    typedef tname<int32_t> i##suffix; \
41    typedef tname<uint32_t> u##suffix; \
42    typedef tname<int64_t> i64##suffix; \
43    typedef tname<uint64_t> u64##suffix; \
44    typedef tname<real> r##suffix; \
45
46DECLARE_VECTOR_TYPEDEFS(Vec2, vec2)
47DECLARE_VECTOR_TYPEDEFS(Cmplx, cmplx)
48DECLARE_VECTOR_TYPEDEFS(Vec3, vec3)
49DECLARE_VECTOR_TYPEDEFS(Vec4, vec4)
50DECLARE_VECTOR_TYPEDEFS(Quat, quat)
51DECLARE_VECTOR_TYPEDEFS(Mat4, mat4)
52
53/*
54 * Magic vector swizzling (part 1/2)
55 */
56
57template<typename T, int N> struct XVec2
58{
59    inline Vec2<T> operator =(Vec2<T> const &that);
60
61    static int const I = (N >> 4) & 3;
62    static int const J = (N >> 0) & 3;
63    T ptr[1 + (I > J ? I : J)];
64};
65
66template<typename T, int N> struct XVec3
67{
68    inline Vec3<T> operator =(Vec3<T> const &that);
69
70    static int const I = (N >> 8) & 3;
71    static int const J = (N >> 4) & 3;
72    static int const K = (N >> 0) & 3;
73    T ptr[1 + (I > J ? I > K ? I : K
74                     : J > K ? J : K)];
75};
76
77template<typename T, int N> struct XVec4
78{
79    inline Vec4<T> operator =(Vec4<T> const &that);
80
81    static int const I = (N >> 12) & 3;
82    static int const J = (N >> 8) & 3;
83    static int const K = (N >> 4) & 3;
84    static int const L = (N >> 0) & 3;
85    T ptr[1 + (I > J ? I > K ? I > L ? I : L : K > L ? K : L
86                     : J > K ? J > L ? J : L : K > L ? K : L)];
87};
88
89/*
90 * Helper macro for vector type member functions
91 */
92
93#define DECLARE_MEMBER_OPS(tname) \
94    inline T& operator[](int n) { return *(&this->x + n); } \
95    inline T const& operator[](int n) const { return *(&this->x + n); } \
96    \
97    void printf() const; \
98    \
99    template<typename U> \
100    inline operator tname<U>() const \
101    { \
102        tname<U> ret; \
103        for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \
104            ret[n] = static_cast<U>((*this)[n]); \
105        return ret; \
106    }
107
108/*
109 * 2-element vectors
110 */
111
112template <typename T> struct BVec2
113{
114    explicit inline BVec2() {}
115    explicit inline BVec2(T X, T Y) : x(X), y(Y) {}
116
117    union
118    {
119        struct { T x, y; };
120        struct { T r, g; };
121        struct { T s, t; };
122
123        XVec2<T,0x00> const xx, rr, ss;
124        XVec2<T,0x01> const xy, rg, st; /* lvalue */
125        XVec2<T,0x10> const yx, gr, ts; /* lvalue */
126        XVec2<T,0x11> const yy, gg, tt;
127
128        XVec3<T,0x000> const xxx, rrr, sss;
129        XVec3<T,0x001> const xxy, rrg, sst;
130        XVec3<T,0x010> const xyx, rgr, sts;
131        XVec3<T,0x011> const xyy, rgg, stt;
132        XVec3<T,0x100> const yxx, grr, tss;
133        XVec3<T,0x101> const yxy, grg, tst;
134        XVec3<T,0x110> const yyx, ggr, tts;
135        XVec3<T,0x111> const yyy, ggg, ttt;
136
137        XVec4<T,0x0000> const xxxx, rrrr, ssss;
138        XVec4<T,0x0001> const xxxy, rrrg, ssst;
139        XVec4<T,0x0010> const xxyx, rrgr, ssts;
140        XVec4<T,0x0011> const xxyy, rrgg, sstt;
141        XVec4<T,0x0100> const xyxx, rgrr, stss;
142        XVec4<T,0x0101> const xyxy, rgrg, stst;
143        XVec4<T,0x0110> const xyyx, rggr, stts;
144        XVec4<T,0x0111> const xyyy, rggg, sttt;
145        XVec4<T,0x1000> const yxxx, grrr, tsss;
146        XVec4<T,0x1001> const yxxy, grrg, tsst;
147        XVec4<T,0x1010> const yxyx, grgr, tsts;
148        XVec4<T,0x1011> const yxyy, grgg, tstt;
149        XVec4<T,0x1100> const yyxx, ggrr, ttss;
150        XVec4<T,0x1101> const yyxy, ggrg, ttst;
151        XVec4<T,0x1110> const yyyx, gggr, ttts;
152        XVec4<T,0x1111> const yyyy, gggg, tttt;
153    };
154};
155
156template <> struct BVec2<half>
157{
158    explicit inline BVec2() {}
159    explicit inline BVec2(half X, half Y) : x(X), y(Y) {}
160
161    half x, y;
162};
163
164template <> struct BVec2<real>
165{
166    explicit inline BVec2() {}
167    explicit inline BVec2(real X, real Y) : x(X), y(Y) {}
168
169    real x, y;
170};
171
172template <typename T> struct Vec2 : BVec2<T>
173{
174    inline Vec2() {}
175    inline Vec2(T X, T Y) : BVec2<T>(X, Y) {}
176
177    explicit inline Vec2(T X) : BVec2<T>(X, X) {}
178
179    template<int N>
180    inline Vec2(XVec2<T, N> const &v)
181      : BVec2<T>(v.ptr[v.I], v.ptr[v.J]) {}
182
183    template<typename U, int N>
184    explicit inline Vec2(XVec2<U, N> const &v)
185      : BVec2<T>(v.ptr[v.I], v.ptr[v.J]) {}
186
187    DECLARE_MEMBER_OPS(Vec2)
188
189#if !defined __ANDROID__
190    template<typename U>
191    friend std::ostream &operator<<(std::ostream &stream, Vec2<U> const &v);
192#endif
193};
194
195/*
196 * 2-element complexes
197 */
198
199template <typename T> struct Cmplx
200{
201    inline Cmplx() {}
202    inline Cmplx(T X) : x(X), y(0) {}
203    inline Cmplx(T X, T Y) : x(X), y(Y) {}
204
205    DECLARE_MEMBER_OPS(Cmplx)
206
207    inline Cmplx<T> operator *(Cmplx<T> const &val) const
208    {
209        return Cmplx<T>(x * val.x - y * val.y, x * val.y + y * val.x);
210    }
211
212    inline Cmplx<T> operator *=(Cmplx<T> const &val)
213    {
214        return *this = (*this) * val;
215    }
216
217    inline Cmplx<T> operator ~() const
218    {
219        return Cmplx<T>(x, -y);
220    }
221
222    inline T norm() const { return len(*this); }
223#if !defined __ANDROID__
224    template<typename U>
225    friend std::ostream &operator<<(std::ostream &stream, Cmplx<U> const &v);
226#endif
227
228    T x, y;
229};
230
231template<typename T>
232static inline Cmplx<T> re(Cmplx<T> const &val)
233{
234    return ~val / sqlen(val);
235}
236
237template<typename T>
238static inline Cmplx<T> operator /(T a, Cmplx<T> const &b)
239{
240    return a * re(b);
241}
242
243template<typename T>
244static inline Cmplx<T> operator /(Cmplx<T> a, Cmplx<T> const &b)
245{
246    return a * re(b);
247}
248
249template<typename T>
250static inline bool operator ==(Cmplx<T> const &a, T b)
251{
252    return (a.x == b) && !a.y;
253}
254
255template<typename T>
256static inline bool operator !=(Cmplx<T> const &a, T b)
257{
258    return (a.x != b) || a.y;
259}
260
261template<typename T>
262static inline bool operator ==(T a, Cmplx<T> const &b) { return b == a; }
263
264template<typename T>
265static inline bool operator !=(T a, Cmplx<T> const &b) { return b != a; }
266
267/*
268 * 3-element vectors
269 */
270
271template <typename T> struct BVec3
272{
273    explicit inline BVec3() {}
274    explicit inline BVec3(T X, T Y, T Z) : x(X), y(Y), z(Z) {}
275
276    union
277    {
278        struct { T x, y, z; };
279        struct { T r, g, b; };
280        struct { T s, t, p; };
281
282        XVec2<T,0x00> const xx, rr, ss;
283        XVec2<T,0x01> const xy, rg, st; /* lvalue */
284        XVec2<T,0x02> const xz, rb, sp; /* lvalue */
285        XVec2<T,0x10> const yx, gr, ts; /* lvalue */
286        XVec2<T,0x11> const yy, gg, tt;
287        XVec2<T,0x12> const yz, gb, tp; /* lvalue */
288        XVec2<T,0x20> const zx, br, ps; /* lvalue */
289        XVec2<T,0x21> const zy, bg, pt; /* lvalue */
290        XVec2<T,0x22> const zz, bb, pp;
291
292        XVec3<T,0x000> const xxx, rrr, sss;
293        XVec3<T,0x001> const xxy, rrg, sst;
294        XVec3<T,0x002> const xxz, rrb, ssp;
295        XVec3<T,0x010> const xyx, rgr, sts;
296        XVec3<T,0x011> const xyy, rgg, stt;
297        XVec3<T,0x012> const xyz, rgb, stp; /* lvalue */
298        XVec3<T,0x020> const xzx, rbr, sps;
299        XVec3<T,0x021> const xzy, rbg, spt; /* lvalue */
300        XVec3<T,0x022> const xzz, rbb, spp;
301        XVec3<T,0x100> const yxx, grr, tss;
302        XVec3<T,0x101> const yxy, grg, tst;
303        XVec3<T,0x102> const yxz, grb, tsp; /* lvalue */
304        XVec3<T,0x110> const yyx, ggr, tts;
305        XVec3<T,0x111> const yyy, ggg, ttt;
306        XVec3<T,0x112> const yyz, ggb, ttp;
307        XVec3<T,0x120> const yzx, gbr, tps; /* lvalue */
308        XVec3<T,0x121> const yzy, gbg, tpt;
309        XVec3<T,0x122> const yzz, gbb, tpp;
310        XVec3<T,0x200> const zxx, brr, pss;
311        XVec3<T,0x201> const zxy, brg, pst; /* lvalue */
312        XVec3<T,0x202> const zxz, brb, psp;
313        XVec3<T,0x210> const zyx, bgr, pts; /* lvalue */
314        XVec3<T,0x211> const zyy, bgg, ptt;
315        XVec3<T,0x212> const zyz, bgb, ptp;
316        XVec3<T,0x220> const zzx, bbr, pps;
317        XVec3<T,0x221> const zzy, bbg, ppt;
318        XVec3<T,0x222> const zzz, bbb, ppp;
319
320        XVec4<T,0x0000> const xxxx, rrrr, ssss;
321        XVec4<T,0x0001> const xxxy, rrrg, ssst;
322        XVec4<T,0x0002> const xxxz, rrrb, sssp;
323        XVec4<T,0x0010> const xxyx, rrgr, ssts;
324        XVec4<T,0x0011> const xxyy, rrgg, sstt;
325        XVec4<T,0x0012> const xxyz, rrgb, sstp;
326        XVec4<T,0x0020> const xxzx, rrbr, ssps;
327        XVec4<T,0x0021> const xxzy, rrbg, sspt;
328        XVec4<T,0x0022> const xxzz, rrbb, sspp;
329        XVec4<T,0x0100> const xyxx, rgrr, stss;
330        XVec4<T,0x0101> const xyxy, rgrg, stst;
331        XVec4<T,0x0102> const xyxz, rgrb, stsp;
332        XVec4<T,0x0110> const xyyx, rggr, stts;
333        XVec4<T,0x0111> const xyyy, rggg, sttt;
334        XVec4<T,0x0112> const xyyz, rggb, sttp;
335        XVec4<T,0x0120> const xyzx, rgbr, stps;
336        XVec4<T,0x0121> const xyzy, rgbg, stpt;
337        XVec4<T,0x0122> const xyzz, rgbb, stpp;
338        XVec4<T,0x0200> const xzxx, rbrr, spss;
339        XVec4<T,0x0201> const xzxy, rbrg, spst;
340        XVec4<T,0x0202> const xzxz, rbrb, spsp;
341        XVec4<T,0x0210> const xzyx, rbgr, spts;
342        XVec4<T,0x0211> const xzyy, rbgg, sptt;
343        XVec4<T,0x0212> const xzyz, rbgb, sptp;
344        XVec4<T,0x0220> const xzzx, rbbr, spps;
345        XVec4<T,0x0221> const xzzy, rbbg, sppt;
346        XVec4<T,0x0222> const xzzz, rbbb, sppp;
347        XVec4<T,0x1000> const yxxx, grrr, tsss;
348        XVec4<T,0x1001> const yxxy, grrg, tsst;
349        XVec4<T,0x1002> const yxxz, grrb, tssp;
350        XVec4<T,0x1010> const yxyx, grgr, tsts;
351        XVec4<T,0x1011> const yxyy, grgg, tstt;
352        XVec4<T,0x1012> const yxyz, grgb, tstp;
353        XVec4<T,0x1020> const yxzx, grbr, tsps;
354        XVec4<T,0x1021> const yxzy, grbg, tspt;
355        XVec4<T,0x1022> const yxzz, grbb, tspp;
356        XVec4<T,0x1100> const yyxx, ggrr, ttss;
357        XVec4<T,0x1101> const yyxy, ggrg, ttst;
358        XVec4<T,0x1102> const yyxz, ggrb, ttsp;
359        XVec4<T,0x1110> const yyyx, gggr, ttts;
360        XVec4<T,0x1111> const yyyy, gggg, tttt;
361        XVec4<T,0x1112> const yyyz, gggb, tttp;
362        XVec4<T,0x1120> const yyzx, ggbr, ttps;
363        XVec4<T,0x1121> const yyzy, ggbg, ttpt;
364        XVec4<T,0x1122> const yyzz, ggbb, ttpp;
365        XVec4<T,0x1200> const yzxx, gbrr, tpss;
366        XVec4<T,0x1201> const yzxy, gbrg, tpst;
367        XVec4<T,0x1202> const yzxz, gbrb, tpsp;
368        XVec4<T,0x1210> const yzyx, gbgr, tpts;
369        XVec4<T,0x1211> const yzyy, gbgg, tptt;
370        XVec4<T,0x1212> const yzyz, gbgb, tptp;
371        XVec4<T,0x1220> const yzzx, gbbr, tpps;
372        XVec4<T,0x1221> const yzzy, gbbg, tppt;
373        XVec4<T,0x1222> const yzzz, gbbb, tppp;
374        XVec4<T,0x2000> const zxxx, brrr, psss;
375        XVec4<T,0x2001> const zxxy, brrg, psst;
376        XVec4<T,0x2002> const zxxz, brrb, pssp;
377        XVec4<T,0x2010> const zxyx, brgr, psts;
378        XVec4<T,0x2011> const zxyy, brgg, pstt;
379        XVec4<T,0x2012> const zxyz, brgb, pstp;
380        XVec4<T,0x2020> const zxzx, brbr, psps;
381        XVec4<T,0x2021> const zxzy, brbg, pspt;
382        XVec4<T,0x2022> const zxzz, brbb, pspp;
383        XVec4<T,0x2100> const zyxx, bgrr, ptss;
384        XVec4<T,0x2101> const zyxy, bgrg, ptst;
385        XVec4<T,0x2102> const zyxz, bgrb, ptsp;
386        XVec4<T,0x2110> const zyyx, bggr, ptts;
387        XVec4<T,0x2111> const zyyy, bggg, pttt;
388        XVec4<T,0x2112> const zyyz, bggb, pttp;
389        XVec4<T,0x2120> const zyzx, bgbr, ptps;
390        XVec4<T,0x2121> const zyzy, bgbg, ptpt;
391        XVec4<T,0x2122> const zyzz, bgbb, ptpp;
392        XVec4<T,0x2200> const zzxx, bbrr, ppss;
393        XVec4<T,0x2201> const zzxy, bbrg, ppst;
394        XVec4<T,0x2202> const zzxz, bbrb, ppsp;
395        XVec4<T,0x2210> const zzyx, bbgr, ppts;
396        XVec4<T,0x2211> const zzyy, bbgg, pptt;
397        XVec4<T,0x2212> const zzyz, bbgb, pptp;
398        XVec4<T,0x2220> const zzzx, bbbr, ppps;
399        XVec4<T,0x2221> const zzzy, bbbg, pppt;
400        XVec4<T,0x2222> const zzzz, bbbb, pppp;
401    };
402};
403
404template <> struct BVec3<half>
405{
406    explicit inline BVec3() {}
407    explicit inline BVec3(half X, half Y, half Z) : x(X), y(Y), z(Z) {}
408
409    half x, y, z;
410};
411
412template <> struct BVec3<real>
413{
414    explicit inline BVec3() {}
415    explicit inline BVec3(real X, real Y, real Z) : x(X), y(Y), z(Z) {}
416
417    real x, y, z;
418};
419
420template <typename T> struct Vec3 : BVec3<T>
421{
422    inline Vec3() {}
423    inline Vec3(T X, T Y, T Z) : BVec3<T>(X, Y, Z) {}
424    inline Vec3(Vec2<T> XY, T Z) : BVec3<T>(XY.x, XY.y, Z) {}
425    inline Vec3(T X, Vec2<T> YZ) : BVec3<T>(X, YZ.x, YZ.y) {}
426
427    explicit inline Vec3(T X) : BVec3<T>(X, X, X) {}
428
429    template<int N>
430    inline Vec3(XVec3<T, N> const &v)
431      : BVec3<T>(v.ptr[v.I], v.ptr[v.J], v.ptr[v.K]) {}
432
433    template<typename U, int N>
434    explicit inline Vec3(XVec3<U, N> const &v)
435      : BVec3<T>(v.ptr[v.I], v.ptr[v.J], v.ptr[v.K]) {}
436
437    DECLARE_MEMBER_OPS(Vec3)
438
439    template<typename U>
440    friend Vec3<U> cross(Vec3<U>, Vec3<U>);
441
442#if !defined __ANDROID__
443    template<typename U>
444    friend std::ostream &operator<<(std::ostream &stream, Vec3<U> const &v);
445#endif
446};
447
448/*
449 * 4-element vectors
450 */
451
452template <typename T> struct BVec4
453{
454    explicit inline BVec4() {}
455    explicit inline BVec4(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {}
456
457    union
458    {
459        struct { T x, y, z, w; };
460        struct { T r, g, b, a; };
461        struct { T s, t, p, q; };
462
463        XVec2<T,0x00> const xx, rr, ss;
464        XVec2<T,0x01> const xy, rg, st; /* lvalue */
465        XVec2<T,0x02> const xz, rb, sp; /* lvalue */
466        XVec2<T,0x03> const xw, ra, sq; /* lvalue */
467        XVec2<T,0x10> const yx, gr, ts; /* lvalue */
468        XVec2<T,0x11> const yy, gg, tt;
469        XVec2<T,0x12> const yz, gb, tp; /* lvalue */
470        XVec2<T,0x13> const yw, ga, tq; /* lvalue */
471        XVec2<T,0x20> const zx, br, ps; /* lvalue */
472        XVec2<T,0x21> const zy, bg, pt; /* lvalue */
473        XVec2<T,0x22> const zz, bb, pp;
474        XVec2<T,0x23> const zw, ba, pq; /* lvalue */
475        XVec2<T,0x30> const wx, ar, qs; /* lvalue */
476        XVec2<T,0x31> const wy, ag, qt; /* lvalue */
477        XVec2<T,0x32> const wz, ab, qp; /* lvalue */
478        XVec2<T,0x33> const ww, aa, qq;
479
480        XVec3<T,0x000> const xxx, rrr, sss;
481        XVec3<T,0x001> const xxy, rrg, sst;
482        XVec3<T,0x002> const xxz, rrb, ssp;
483        XVec3<T,0x003> const xxw, rra, ssq;
484        XVec3<T,0x010> const xyx, rgr, sts;
485        XVec3<T,0x011> const xyy, rgg, stt;
486        XVec3<T,0x012> const xyz, rgb, stp; /* lvalue */
487        XVec3<T,0x013> const xyw, rga, stq; /* lvalue */
488        XVec3<T,0x020> const xzx, rbr, sps;
489        XVec3<T,0x021> const xzy, rbg, spt; /* lvalue */
490        XVec3<T,0x022> const xzz, rbb, spp;
491        XVec3<T,0x023> const xzw, rba, spq; /* lvalue */
492        XVec3<T,0x030> const xwx, rar, sqs;
493        XVec3<T,0x031> const xwy, rag, sqt; /* lvalue */
494        XVec3<T,0x032> const xwz, rab, sqp; /* lvalue */
495        XVec3<T,0x033> const xww, raa, sqq;
496        XVec3<T,0x100> const yxx, grr, tss;
497        XVec3<T,0x101> const yxy, grg, tst;
498        XVec3<T,0x102> const yxz, grb, tsp; /* lvalue */
499        XVec3<T,0x103> const yxw, gra, tsq; /* lvalue */
500        XVec3<T,0x110> const yyx, ggr, tts;
501        XVec3<T,0x111> const yyy, ggg, ttt;
502        XVec3<T,0x112> const yyz, ggb, ttp;
503        XVec3<T,0x113> const yyw, gga, ttq;
504        XVec3<T,0x120> const yzx, gbr, tps; /* lvalue */
505        XVec3<T,0x121> const yzy, gbg, tpt;
506        XVec3<T,0x122> const yzz, gbb, tpp;
507        XVec3<T,0x123> const yzw, gba, tpq; /* lvalue */
508        XVec3<T,0x130> const ywx, gar, tqs; /* lvalue */
509        XVec3<T,0x131> const ywy, gag, tqt;
510        XVec3<T,0x132> const ywz, gab, tqp; /* lvalue */
511        XVec3<T,0x133> const yww, gaa, tqq;
512        XVec3<T,0x200> const zxx, brr, pss;
513        XVec3<T,0x201> const zxy, brg, pst; /* lvalue */
514        XVec3<T,0x202> const zxz, brb, psp;
515        XVec3<T,0x203> const zxw, bra, psq; /* lvalue */
516        XVec3<T,0x210> const zyx, bgr, pts; /* lvalue */
517        XVec3<T,0x211> const zyy, bgg, ptt;
518        XVec3<T,0x212> const zyz, bgb, ptp;
519        XVec3<T,0x213> const zyw, bga, ptq; /* lvalue */
520        XVec3<T,0x220> const zzx, bbr, pps;
521        XVec3<T,0x221> const zzy, bbg, ppt;
522        XVec3<T,0x222> const zzz, bbb, ppp;
523        XVec3<T,0x223> const zzw, bba, ppq;
524        XVec3<T,0x230> const zwx, bar, pqs; /* lvalue */
525        XVec3<T,0x231> const zwy, bag, pqt; /* lvalue */
526        XVec3<T,0x232> const zwz, bab, pqp;
527        XVec3<T,0x233> const zww, baa, pqq;
528        XVec3<T,0x300> const wxx, arr, qss;
529        XVec3<T,0x301> const wxy, arg, qst; /* lvalue */
530        XVec3<T,0x302> const wxz, arb, qsp; /* lvalue */
531        XVec3<T,0x303> const wxw, ara, qsq;
532        XVec3<T,0x310> const wyx, agr, qts; /* lvalue */
533        XVec3<T,0x311> const wyy, agg, qtt;
534        XVec3<T,0x312> const wyz, agb, qtp; /* lvalue */
535        XVec3<T,0x313> const wyw, aga, qtq;
536        XVec3<T,0x320> const wzx, abr, qps; /* lvalue */
537        XVec3<T,0x321> const wzy, abg, qpt; /* lvalue */
538        XVec3<T,0x322> const wzz, abb, qpp;
539        XVec3<T,0x323> const wzw, aba, qpq;
540        XVec3<T,0x330> const wwx, aar, qqs;
541        XVec3<T,0x331> const wwy, aag, qqt;
542        XVec3<T,0x332> const wwz, aab, qqp;
543        XVec3<T,0x333> const www, aaa, qqq;
544
545        XVec4<T,0x0000> const xxxx, rrrr, ssss;
546        XVec4<T,0x0001> const xxxy, rrrg, ssst;
547        XVec4<T,0x0002> const xxxz, rrrb, sssp;
548        XVec4<T,0x0003> const xxxw, rrra, sssq;
549        XVec4<T,0x0010> const xxyx, rrgr, ssts;
550        XVec4<T,0x0011> const xxyy, rrgg, sstt;
551        XVec4<T,0x0012> const xxyz, rrgb, sstp;
552        XVec4<T,0x0013> const xxyw, rrga, sstq;
553        XVec4<T,0x0020> const xxzx, rrbr, ssps;
554        XVec4<T,0x0021> const xxzy, rrbg, sspt;
555        XVec4<T,0x0022> const xxzz, rrbb, sspp;
556        XVec4<T,0x0023> const xxzw, rrba, sspq;
557        XVec4<T,0x0030> const xxwx, rrar, ssqs;
558        XVec4<T,0x0031> const xxwy, rrag, ssqt;
559        XVec4<T,0x0032> const xxwz, rrab, ssqp;
560        XVec4<T,0x0033> const xxww, rraa, ssqq;
561        XVec4<T,0x0100> const xyxx, rgrr, stss;
562        XVec4<T,0x0101> const xyxy, rgrg, stst;
563        XVec4<T,0x0102> const xyxz, rgrb, stsp;
564        XVec4<T,0x0103> const xyxw, rgra, stsq;
565        XVec4<T,0x0110> const xyyx, rggr, stts;
566        XVec4<T,0x0111> const xyyy, rggg, sttt;
567        XVec4<T,0x0112> const xyyz, rggb, sttp;
568        XVec4<T,0x0113> const xyyw, rgga, sttq;
569        XVec4<T,0x0120> const xyzx, rgbr, stps;
570        XVec4<T,0x0121> const xyzy, rgbg, stpt;
571        XVec4<T,0x0122> const xyzz, rgbb, stpp;
572        XVec4<T,0x0123> const xyzw, rgba, stpq; /* lvalue */
573        XVec4<T,0x0130> const xywx, rgar, stqs;
574        XVec4<T,0x0131> const xywy, rgag, stqt;
575        XVec4<T,0x0132> const xywz, rgab, stqp; /* lvalue */
576        XVec4<T,0x0133> const xyww, rgaa, stqq;
577        XVec4<T,0x0200> const xzxx, rbrr, spss;
578        XVec4<T,0x0201> const xzxy, rbrg, spst;
579        XVec4<T,0x0202> const xzxz, rbrb, spsp;
580        XVec4<T,0x0203> const xzxw, rbra, spsq;
581        XVec4<T,0x0210> const xzyx, rbgr, spts;
582        XVec4<T,0x0211> const xzyy, rbgg, sptt;
583        XVec4<T,0x0212> const xzyz, rbgb, sptp;
584        XVec4<T,0x0213> const xzyw, rbga, sptq; /* lvalue */
585        XVec4<T,0x0220> const xzzx, rbbr, spps;
586        XVec4<T,0x0221> const xzzy, rbbg, sppt;
587        XVec4<T,0x0222> const xzzz, rbbb, sppp;
588        XVec4<T,0x0223> const xzzw, rbba, sppq;
589        XVec4<T,0x0230> const xzwx, rbar, spqs;
590        XVec4<T,0x0231> const xzwy, rbag, spqt; /* lvalue */
591        XVec4<T,0x0232> const xzwz, rbab, spqp;
592        XVec4<T,0x0233> const xzww, rbaa, spqq;
593        XVec4<T,0x0300> const xwxx, rarr, sqss;
594        XVec4<T,0x0301> const xwxy, rarg, sqst;
595        XVec4<T,0x0302> const xwxz, rarb, sqsp;
596        XVec4<T,0x0303> const xwxw, rara, sqsq;
597        XVec4<T,0x0310> const xwyx, ragr, sqts;
598        XVec4<T,0x0311> const xwyy, ragg, sqtt;
599        XVec4<T,0x0312> const xwyz, ragb, sqtp; /* lvalue */
600        XVec4<T,0x0313> const xwyw, raga, sqtq;
601        XVec4<T,0x0320> const xwzx, rabr, sqps;
602        XVec4<T,0x0321> const xwzy, rabg, sqpt; /* lvalue */
603        XVec4<T,0x0322> const xwzz, rabb, sqpp;
604        XVec4<T,0x0323> const xwzw, raba, sqpq;
605        XVec4<T,0x0330> const xwwx, raar, sqqs;
606        XVec4<T,0x0331> const xwwy, raag, sqqt;
607        XVec4<T,0x0332> const xwwz, raab, sqqp;
608        XVec4<T,0x0333> const xwww, raaa, sqqq;
609        XVec4<T,0x1000> const yxxx, grrr, tsss;
610        XVec4<T,0x1001> const yxxy, grrg, tsst;
611        XVec4<T,0x1002> const yxxz, grrb, tssp;
612        XVec4<T,0x1003> const yxxw, grra, tssq;
613        XVec4<T,0x1010> const yxyx, grgr, tsts;
614        XVec4<T,0x1011> const yxyy, grgg, tstt;
615        XVec4<T,0x1012> const yxyz, grgb, tstp;
616        XVec4<T,0x1013> const yxyw, grga, tstq;
617        XVec4<T,0x1020> const yxzx, grbr, tsps;
618        XVec4<T,0x1021> const yxzy, grbg, tspt;
619        XVec4<T,0x1022> const yxzz, grbb, tspp;
620        XVec4<T,0x1023> const yxzw, grba, tspq; /* lvalue */
621        XVec4<T,0x1030> const yxwx, grar, tsqs;
622        XVec4<T,0x1031> const yxwy, grag, tsqt;
623        XVec4<T,0x1032> const yxwz, grab, tsqp; /* lvalue */
624        XVec4<T,0x1033> const yxww, graa, tsqq;
625        XVec4<T,0x1100> const yyxx, ggrr, ttss;
626        XVec4<T,0x1101> const yyxy, ggrg, ttst;
627        XVec4<T,0x1102> const yyxz, ggrb, ttsp;
628        XVec4<T,0x1103> const yyxw, ggra, ttsq;
629        XVec4<T,0x1110> const yyyx, gggr, ttts;
630        XVec4<T,0x1111> const yyyy, gggg, tttt;
631        XVec4<T,0x1112> const yyyz, gggb, tttp;
632        XVec4<T,0x1113> const yyyw, ggga, tttq;
633        XVec4<T,0x1120> const yyzx, ggbr, ttps;
634        XVec4<T,0x1121> const yyzy, ggbg, ttpt;
635        XVec4<T,0x1122> const yyzz, ggbb, ttpp;
636        XVec4<T,0x1123> const yyzw, ggba, ttpq;
637        XVec4<T,0x1130> const yywx, ggar, ttqs;
638        XVec4<T,0x1131> const yywy, ggag, ttqt;
639        XVec4<T,0x1132> const yywz, ggab, ttqp;
640        XVec4<T,0x1133> const yyww, ggaa, ttqq;
641        XVec4<T,0x1200> const yzxx, gbrr, tpss;
642        XVec4<T,0x1201> const yzxy, gbrg, tpst;
643        XVec4<T,0x1202> const yzxz, gbrb, tpsp;
644        XVec4<T,0x1203> const yzxw, gbra, tpsq; /* lvalue */
645        XVec4<T,0x1210> const yzyx, gbgr, tpts;
646        XVec4<T,0x1211> const yzyy, gbgg, tptt;
647        XVec4<T,0x1212> const yzyz, gbgb, tptp;
648        XVec4<T,0x1213> const yzyw, gbga, tptq;
649        XVec4<T,0x1220> const yzzx, gbbr, tpps;
650        XVec4<T,0x1221> const yzzy, gbbg, tppt;
651        XVec4<T,0x1222> const yzzz, gbbb, tppp;
652        XVec4<T,0x1223> const yzzw, gbba, tppq;
653        XVec4<T,0x1230> const yzwx, gbar, tpqs; /* lvalue */
654        XVec4<T,0x1231> const yzwy, gbag, tpqt;
655        XVec4<T,0x1232> const yzwz, gbab, tpqp;
656        XVec4<T,0x1233> const yzww, gbaa, tpqq;
657        XVec4<T,0x1300> const ywxx, garr, tqss;
658        XVec4<T,0x1301> const ywxy, garg, tqst;
659        XVec4<T,0x1302> const ywxz, garb, tqsp; /* lvalue */
660        XVec4<T,0x1303> const ywxw, gara, tqsq;
661        XVec4<T,0x1310> const ywyx, gagr, tqts;
662        XVec4<T,0x1311> const ywyy, gagg, tqtt;
663        XVec4<T,0x1312> const ywyz, gagb, tqtp;
664        XVec4<T,0x1313> const ywyw, gaga, tqtq;
665        XVec4<T,0x1320> const ywzx, gabr, tqps; /* lvalue */
666        XVec4<T,0x1321> const ywzy, gabg, tqpt;
667        XVec4<T,0x1322> const ywzz, gabb, tqpp;
668        XVec4<T,0x1323> const ywzw, gaba, tqpq;
669        XVec4<T,0x1330> const ywwx, gaar, tqqs;
670        XVec4<T,0x1331> const ywwy, gaag, tqqt;
671        XVec4<T,0x1332> const ywwz, gaab, tqqp;
672        XVec4<T,0x1333> const ywww, gaaa, tqqq;
673        XVec4<T,0x2000> const zxxx, brrr, psss;
674        XVec4<T,0x2001> const zxxy, brrg, psst;
675        XVec4<T,0x2002> const zxxz, brrb, pssp;
676        XVec4<T,0x2003> const zxxw, brra, pssq;
677        XVec4<T,0x2010> const zxyx, brgr, psts;
678        XVec4<T,0x2011> const zxyy, brgg, pstt;
679        XVec4<T,0x2012> const zxyz, brgb, pstp;
680        XVec4<T,0x2013> const zxyw, brga, pstq; /* lvalue */
681        XVec4<T,0x2020> const zxzx, brbr, psps;
682        XVec4<T,0x2021> const zxzy, brbg, pspt;
683        XVec4<T,0x2022> const zxzz, brbb, pspp;
684        XVec4<T,0x2023> const zxzw, brba, pspq;
685        XVec4<T,0x2030> const zxwx, brar, psqs;
686        XVec4<T,0x2031> const zxwy, brag, psqt; /* lvalue */
687        XVec4<T,0x2032> const zxwz, brab, psqp;
688        XVec4<T,0x2033> const zxww, braa, psqq;
689        XVec4<T,0x2100> const zyxx, bgrr, ptss;
690        XVec4<T,0x2101> const zyxy, bgrg, ptst;
691        XVec4<T,0x2102> const zyxz, bgrb, ptsp;
692        XVec4<T,0x2103> const zyxw, bgra, ptsq; /* lvalue */
693        XVec4<T,0x2110> const zyyx, bggr, ptts;
694        XVec4<T,0x2111> const zyyy, bggg, pttt;
695        XVec4<T,0x2112> const zyyz, bggb, pttp;
696        XVec4<T,0x2113> const zyyw, bgga, pttq;
697        XVec4<T,0x2120> const zyzx, bgbr, ptps;
698        XVec4<T,0x2121> const zyzy, bgbg, ptpt;
699        XVec4<T,0x2122> const zyzz, bgbb, ptpp;
700        XVec4<T,0x2123> const zyzw, bgba, ptpq;
701        XVec4<T,0x2130> const zywx, bgar, ptqs; /* lvalue */
702        XVec4<T,0x2131> const zywy, bgag, ptqt;
703        XVec4<T,0x2132> const zywz, bgab, ptqp;
704        XVec4<T,0x2133> const zyww, bgaa, ptqq;
705        XVec4<T,0x2200> const zzxx, bbrr, ppss;
706        XVec4<T,0x2201> const zzxy, bbrg, ppst;
707        XVec4<T,0x2202> const zzxz, bbrb, ppsp;
708        XVec4<T,0x2203> const zzxw, bbra, ppsq;
709        XVec4<T,0x2210> const zzyx, bbgr, ppts;
710        XVec4<T,0x2211> const zzyy, bbgg, pptt;
711        XVec4<T,0x2212> const zzyz, bbgb, pptp;
712        XVec4<T,0x2213> const zzyw, bbga, pptq;
713        XVec4<T,0x2220> const zzzx, bbbr, ppps;
714        XVec4<T,0x2221> const zzzy, bbbg, pppt;
715        XVec4<T,0x2222> const zzzz, bbbb, pppp;
716        XVec4<T,0x2223> const zzzw, bbba, pppq;
717        XVec4<T,0x2230> const zzwx, bbar, ppqs;
718        XVec4<T,0x2231> const zzwy, bbag, ppqt;
719        XVec4<T,0x2232> const zzwz, bbab, ppqp;
720        XVec4<T,0x2233> const zzww, bbaa, ppqq;
721        XVec4<T,0x2300> const zwxx, barr, pqss;
722        XVec4<T,0x2301> const zwxy, barg, pqst; /* lvalue */
723        XVec4<T,0x2302> const zwxz, barb, pqsp;
724        XVec4<T,0x2303> const zwxw, bara, pqsq;
725        XVec4<T,0x2310> const zwyx, bagr, pqts; /* lvalue */
726        XVec4<T,0x2311> const zwyy, bagg, pqtt;
727        XVec4<T,0x2312> const zwyz, bagb, pqtp;
728        XVec4<T,0x2313> const zwyw, baga, pqtq;
729        XVec4<T,0x2320> const zwzx, babr, pqps;
730        XVec4<T,0x2321> const zwzy, babg, pqpt;
731        XVec4<T,0x2322> const zwzz, babb, pqpp;
732        XVec4<T,0x2323> const zwzw, baba, pqpq;
733        XVec4<T,0x2330> const zwwx, baar, pqqs;
734        XVec4<T,0x2331> const zwwy, baag, pqqt;
735        XVec4<T,0x2332> const zwwz, baab, pqqp;
736        XVec4<T,0x2333> const zwww, baaa, pqqq;
737        XVec4<T,0x3000> const wxxx, arrr, qsss;
738        XVec4<T,0x3001> const wxxy, arrg, qsst;
739        XVec4<T,0x3002> const wxxz, arrb, qssp;
740        XVec4<T,0x3003> const wxxw, arra, qssq;
741        XVec4<T,0x3010> const wxyx, argr, qsts;
742        XVec4<T,0x3011> const wxyy, argg, qstt;
743        XVec4<T,0x3012> const wxyz, argb, qstp; /* lvalue */
744        XVec4<T,0x3013> const wxyw, arga, qstq;
745        XVec4<T,0x3020> const wxzx, arbr, qsps;
746        XVec4<T,0x3021> const wxzy, arbg, qspt; /* lvalue */
747        XVec4<T,0x3022> const wxzz, arbb, qspp;
748        XVec4<T,0x3023> const wxzw, arba, qspq;
749        XVec4<T,0x3030> const wxwx, arar, qsqs;
750        XVec4<T,0x3031> const wxwy, arag, qsqt;
751        XVec4<T,0x3032> const wxwz, arab, qsqp;
752        XVec4<T,0x3033> const wxww, araa, qsqq;
753        XVec4<T,0x3100> const wyxx, agrr, qtss;
754        XVec4<T,0x3101> const wyxy, agrg, qtst;
755        XVec4<T,0x3102> const wyxz, agrb, qtsp; /* lvalue */
756        XVec4<T,0x3103> const wyxw, agra, qtsq;
757        XVec4<T,0x3110> const wyyx, aggr, qtts;
758        XVec4<T,0x3111> const wyyy, aggg, qttt;
759        XVec4<T,0x3112> const wyyz, aggb, qttp;
760        XVec4<T,0x3113> const wyyw, agga, qttq;
761        XVec4<T,0x3120> const wyzx, agbr, qtps; /* lvalue */
762        XVec4<T,0x3121> const wyzy, agbg, qtpt;
763        XVec4<T,0x3122> const wyzz, agbb, qtpp;
764        XVec4<T,0x3123> const wyzw, agba, qtpq;
765        XVec4<T,0x3130> const wywx, agar, qtqs;
766        XVec4<T,0x3131> const wywy, agag, qtqt;
767        XVec4<T,0x3132> const wywz, agab, qtqp;
768        XVec4<T,0x3133> const wyww, agaa, qtqq;
769        XVec4<T,0x3200> const wzxx, abrr, qpss;
770        XVec4<T,0x3201> const wzxy, abrg, qpst; /* lvalue */
771        XVec4<T,0x3202> const wzxz, abrb, qpsp;
772        XVec4<T,0x3203> const wzxw, abra, qpsq;
773        XVec4<T,0x3210> const wzyx, abgr, qpts; /* lvalue */
774        XVec4<T,0x3211> const wzyy, abgg, qptt;
775        XVec4<T,0x3212> const wzyz, abgb, qptp;
776        XVec4<T,0x3213> const wzyw, abga, qptq;
777        XVec4<T,0x3220> const wzzx, abbr, qpps;
778        XVec4<T,0x3221> const wzzy, abbg, qppt;
779        XVec4<T,0x3222> const wzzz, abbb, qppp;
780        XVec4<T,0x3223> const wzzw, abba, qppq;
781        XVec4<T,0x3230> const wzwx, abar, qpqs;
782        XVec4<T,0x3231> const wzwy, abag, qpqt;
783        XVec4<T,0x3232> const wzwz, abab, qpqp;
784        XVec4<T,0x3233> const wzww, abaa, qpqq;
785        XVec4<T,0x3300> const wwxx, aarr, qqss;
786        XVec4<T,0x3301> const wwxy, aarg, qqst;
787        XVec4<T,0x3302> const wwxz, aarb, qqsp;
788        XVec4<T,0x3303> const wwxw, aara, qqsq;
789        XVec4<T,0x3310> const wwyx, aagr, qqts;
790        XVec4<T,0x3311> const wwyy, aagg, qqtt;
791        XVec4<T,0x3312> const wwyz, aagb, qqtp;
792        XVec4<T,0x3313> const wwyw, aaga, qqtq;
793        XVec4<T,0x3320> const wwzx, aabr, qqps;
794        XVec4<T,0x3321> const wwzy, aabg, qqpt;
795        XVec4<T,0x3322> const wwzz, aabb, qqpp;
796        XVec4<T,0x3323> const wwzw, aaba, qqpq;
797        XVec4<T,0x3330> const wwwx, aaar, qqqs;
798        XVec4<T,0x3331> const wwwy, aaag, qqqt;
799        XVec4<T,0x3332> const wwwz, aaab, qqqp;
800        XVec4<T,0x3333> const wwww, aaaa, qqqq;
801    };
802};
803
804template <> struct BVec4<half>
805{
806    explicit inline BVec4() {}
807    explicit inline BVec4(half X, half Y, half Z, half W)
808     : x(X), y(Y), z(Z), w(W) {}
809
810    half x, y, z, w;
811};
812
813template <> struct BVec4<real>
814{
815    explicit inline BVec4() {}
816    explicit inline BVec4(real X, real Y, real Z, real W)
817     : x(X), y(Y), z(Z), w(W) {}
818
819    real x, y, z, w;
820};
821
822template <typename T> struct Vec4 : BVec4<T>
823{
824    inline Vec4() {}
825    inline Vec4(T X, T Y, T Z, T W) : BVec4<T>(X, Y, Z, W) {}
826    inline Vec4(Vec2<T> XY, T Z, T W) : BVec4<T>(XY.x, XY.y, Z, W) {}
827    inline Vec4(T X, Vec2<T> YZ, T W) : BVec4<T>(X, YZ.x, YZ.y, W) {}
828    inline Vec4(T X, T Y, Vec2<T> ZW) : BVec4<T>(X, Y, ZW.x, ZW.y) {}
829    inline Vec4(Vec2<T> XY, Vec2<T> ZW) : BVec4<T>(XY.x, XY.y, ZW.x, ZW.y) {}
830    inline Vec4(Vec3<T> XYZ, T W) : BVec4<T>(XYZ.x, XYZ.y, XYZ.z, W) {}
831    inline Vec4(T X, Vec3<T> YZW) : BVec4<T>(X, YZW.x, YZW.y, YZW.z) {}
832
833    explicit inline Vec4(T X) : BVec4<T>(X, X, X, X) {}
834
835    template<int N>
836    inline Vec4(XVec4<T, N> const &v)
837      : BVec4<T>(v.ptr[v.I], v.ptr[v.J], v.ptr[v.K], v.ptr[v.L]) {}
838
839    template<typename U, int N>
840    explicit inline Vec4(XVec4<U, N> const &v)
841      : BVec4<T>(v.ptr[v.I], v.ptr[v.J], v.ptr[v.K], v.ptr[v.L]) {}
842
843    DECLARE_MEMBER_OPS(Vec4)
844
845#if !defined __ANDROID__
846    template<typename U>
847    friend std::ostream &operator<<(std::ostream &stream, Vec4<U> const &v);
848#endif
849};
850
851/*
852 * 4-element quaternions
853 */
854
855template <typename T> struct Quat
856{
857    inline Quat() {}
858    inline Quat(T X) : x(0), y(0), z(0), w(X) {}
859    inline Quat(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {}
860
861    Quat(Mat4<T> const &m);
862
863    DECLARE_MEMBER_OPS(Quat)
864
865    inline Quat<T> operator *(Quat<T> const &val) const
866    {
867        Quat<T> ret;
868        Vec3<T> v1(x, y, z);
869        Vec3<T> v2(val.x, val.y, val.z);
870        Vec3<T> v3 = cross(v1, v2) + w * v2 + val.w * v1;
871        return Quat<T>(v3.x, v3.y, v3.z, w * val.w - dot(v1, v2));
872    }
873
874    inline Quat<T> operator *=(Quat<T> const &val)
875    {
876        return *this = (*this) * val;
877    }
878
879    inline Quat<T> operator ~() const
880    {
881        return Quat<T>(-x, -y, -z, w);
882    }
883
884#if !defined __ANDROID__
885    template<typename U>
886    friend std::ostream &operator<<(std::ostream &stream, Quat<U> const &v);
887#endif
888
889    T x, y, z, w;
890};
891
892template<typename T>
893inline T norm(Quat<T> const &val)
894{
895    return sqlen(val);
896}
897
898template<typename T>
899static inline Quat<T> re(Quat<T> const &val)
900{
901    return ~val / norm(val);
902}
903
904template<typename T>
905static inline Quat<T> operator /(T x, Quat<T> const &y)
906{
907    return x * re(y);
908}
909
910template<typename T>
911static inline Quat<T> operator /(Quat<T> x, Quat<T> const &y)
912{
913    return x * re(y);
914}
915
916/*
917 * Common operators for all vector types, including quaternions
918 */
919
920#define DECLARE_VECTOR_OP(tname, op, tprefix, T) \
921    tprefix \
922    static inline tname<T> operator op(tname<T> const &a, tname<T> const &b) \
923    { \
924        tname<T> ret; \
925        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
926            ret[n] = a[n] op b[n]; \
927        return ret; \
928    } \
929    \
930    tprefix \
931    static inline tname<T> operator op##=(tname<T> &a, tname<T> const &b) \
932    { \
933        return a = a op b; \
934    }
935
936#define DECLARE_BOOL_OP(tname, op, op2, ret, tprefix, T) \
937    tprefix \
938    static inline bool operator op(tname<T> const &a, tname<T> const &b) \
939    { \
940        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
941            if (!(a[n] op2 b[n])) \
942                return !ret; \
943        return ret; \
944    }
945
946#define DECLARE_SCALAR_OP(tname, op, tprefix, T) \
947    tprefix \
948    static inline tname<T> operator op(tname<T> const &a, T const &val) \
949    { \
950        tname<T> ret; \
951        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
952            ret[n] = a[n] op val; \
953        return ret; \
954    } \
955    \
956    tprefix \
957    static inline tname<T> operator op(T const &val, tname<T> const &a) \
958    { \
959        tname<T> ret; \
960        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
961            ret[n] = a[n] op val; \
962        return ret; \
963    } \
964    \
965    tprefix \
966    static inline tname<T> operator op##=(tname<T> &a, T const &val) \
967    { \
968        return a = a op val; \
969    }
970
971/* FIXME: this is not used yet */
972#define SCALAR_PROMOTE_OP(tname, op, U) \
973    template<typename T> \
974    static inline tname<U> operator op(U const &val, tname<T> const &a) \
975    { \
976        tname<U> ret; \
977        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
978            ret[n] = val op a[n]; \
979        return ret; \
980    }
981
982#define DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \
983    DECLARE_SCALAR_OP(tname, *, tprefix, T) \
984    DECLARE_SCALAR_OP(tname, /, tprefix, T) \
985    \
986    DECLARE_VECTOR_OP(tname, -, tprefix, T) \
987    DECLARE_VECTOR_OP(tname, +, tprefix, T) \
988    \
989    DECLARE_BOOL_OP(tname, ==, ==, true, tprefix, T) \
990    DECLARE_BOOL_OP(tname, !=, ==, false, tprefix, T) \
991    \
992    tprefix \
993    static inline tname<T> operator -(tname<T> const &a) \
994    { \
995        tname<T> ret; \
996        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
997            ret[n] = -a[n]; \
998        return ret; \
999    } \
1000    \
1001    tprefix \
1002    static inline T sqlen(tname<T> const &a) \
1003    { \
1004        T acc = 0; \
1005        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
1006            acc += a[n] * a[n]; \
1007        return acc; \
1008    } \
1009    \
1010    tprefix \
1011    static inline double len(tname<T> const &a) \
1012    { \
1013        using namespace std; \
1014        return sqrt((double)sqlen(a)); \
1015    } \
1016    \
1017    tprefix \
1018    static inline T dot(tname<T> const &a, tname<T> const &b) \
1019    { \
1020        T ret = 0; \
1021        for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \
1022            ret += a[n] * b[n]; \
1023        return ret; \
1024    } \
1025    \
1026    tprefix \
1027    static inline tname<T> normalize(tname<T> const &val) \
1028    { \
1029        T norm = (T)len(val); \
1030        return norm ? val / norm : val * (T)0; \
1031    }
1032
1033#define DECLARE_GLOBAL_TEMPLATE_OPS(tname) \
1034    DECLARE_OTHER_GLOBAL_OPS(tname, template<typename T>, T)
1035
1036#define DECLARE_ALL_GLOBAL_OPS(tname, tprefix, T) \
1037    DECLARE_VECTOR_OP(tname, *, tprefix, T) \
1038    DECLARE_VECTOR_OP(tname, /, tprefix, T) \
1039    \
1040    DECLARE_OTHER_GLOBAL_OPS(tname, tprefix, T) \
1041    \
1042    DECLARE_BOOL_OP(tname, <=, <=, true, tprefix, T) \
1043    DECLARE_BOOL_OP(tname, >=, >=, true, tprefix, T) \
1044    DECLARE_BOOL_OP(tname, <, <, true, tprefix, T) \
1045    DECLARE_BOOL_OP(tname, >, >, true, tprefix, T)
1046
1047/* FIXME: a few problems need to be fixed before we can use "half" here. It
1048 * will probably never work until we switch to C++11 because it's not really
1049 * a POD class. */
1050#define DECLARE_GLOBAL_TYPED_OPS(tname) \
1051    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, float) \
1052    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, double) \
1053    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int8_t) \
1054    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint8_t) \
1055    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int16_t) \
1056    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint16_t) \
1057    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int32_t) \
1058    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint32_t) \
1059    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, int64_t) \
1060    DECLARE_ALL_GLOBAL_OPS(tname, /* empty */, uint64_t)
1061
1062DECLARE_GLOBAL_TEMPLATE_OPS(Cmplx)
1063DECLARE_GLOBAL_TEMPLATE_OPS(Quat)
1064
1065DECLARE_GLOBAL_TYPED_OPS(Vec2)
1066DECLARE_GLOBAL_TYPED_OPS(Vec3)
1067DECLARE_GLOBAL_TYPED_OPS(Vec4)
1068
1069#undef DECLARE_VECTOR_TYPEDEFS
1070#undef DECLARE_MEMBER_OPS
1071#undef DECLARE_VECTOR_OP
1072#undef DECLARE_BOOL_OP
1073#undef DECLARE_SCALAR_OP
1074#undef DECLARE_GLOBAL_OPS
1075#undef DECLARE_GLOBAL_TEMPLATE_OPS
1076#undef DECLARE_ALL_GLOBAL_OPS
1077#undef DECLARE_GLOBAL_TYPED_OPS
1078
1079/*
1080 * Magic vector swizzling (part 2/2)
1081 * Unfortunately these assignment operators cannot be used for now, because
1082 * we would also need to override the default copy assignment operator, and
1083 * in C++98 unions cannot contain such objects. This is why all the swizzling
1084 * magic objects are marked 'const' even those that could be lvalues.
1085 */
1086
1087template<typename T, int N>
1088inline Vec2<T> XVec2<T, N>::operator =(Vec2<T> const &that)
1089{
1090    ptr[I] = that[0]; ptr[J] = that[1];
1091    return *this;
1092}
1093
1094template<typename T, int N>
1095inline Vec3<T> XVec3<T, N>::operator =(Vec3<T> const &that)
1096{
1097    ptr[I] = that[0]; ptr[J] = that[1]; ptr[K] = that[2];
1098    return *this;
1099}
1100
1101template<typename T, int N>
1102inline Vec4<T> XVec4<T, N>::operator =(Vec4<T> const &that)
1103{
1104    ptr[I] = that[0]; ptr[J] = that[1]; ptr[K] = that[2]; ptr[L] = that[3];
1105    return *this;
1106}
1107
1108/*
1109 * 4×4-element matrices
1110 */
1111
1112template <typename T> struct Mat4
1113{
1114    inline Mat4() {}
1115    inline Mat4(Vec4<T> V0, Vec4<T> V1, Vec4<T> V2, Vec4<T> V3)
1116      : v0(V0), v1(V1), v2(V2), v3(V3) {}
1117
1118    explicit inline Mat4(T val)
1119      : v0(val, (T)0, (T)0, (T)0),
1120        v1((T)0, val, (T)0, (T)0),
1121        v2((T)0, (T)0, val, (T)0),
1122        v3((T)0, (T)0, (T)0, val) {}
1123
1124    inline Vec4<T>& operator[](int n) { return (&v0)[n]; }
1125    inline Vec4<T> const& operator[](int n) const { return (&v0)[n]; }
1126
1127    T det() const;
1128    Mat4<T> invert() const;
1129
1130    /* Helpers for transformation matrices */
1131    static Mat4<T> translate(T x, T y, T z);
1132    static Mat4<T> translate(Vec3<T> v);
1133    static Mat4<T> rotate(T angle, T x, T y, T z);
1134    static Mat4<T> rotate(T angle, Vec3<T> v);
1135    static Mat4<T> rotate(Quat<T> q);
1136
1137    static inline Mat4<T> translate(Mat4<T> mat, Vec3<T> v)
1138    {
1139        return translate(v) * mat;
1140    }
1141
1142    static inline Mat4<T> rotate(Mat4<T> mat, T angle, Vec3<T> v)
1143    {
1144        return rotate(angle, v) * mat;
1145    }
1146
1147    /* Helpers for view matrices */
1148    static Mat4<T> lookat(Vec3<T> eye, Vec3<T> center, Vec3<T> up);
1149
1150    /* Helpers for projection matrices */
1151    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
1152    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
1153    static Mat4<T> perspective(T fov_y, T width, T height, T near, T far);
1154
1155    void printf() const;
1156
1157#if !defined __ANDROID__
1158    template<class U>
1159    friend std::ostream &operator<<(std::ostream &stream, Mat4<U> const &m);
1160#endif
1161
1162    inline Mat4<T> operator +(Mat4<T> const m) const
1163    {
1164        return Mat4<T>(v0 + m[0], v1 + m[1], v2 + m[2], v3 + m[3]);
1165    }
1166
1167    inline Mat4<T> operator +=(Mat4<T> const m)
1168    {
1169        return *this = *this + m;
1170    }
1171
1172    inline Mat4<T> operator -(Mat4<T> const m) const
1173    {
1174        return Mat4<T>(v0 - m[0], v1 - m[1], v2 - m[2], v3 - m[3]);
1175    }
1176
1177    inline Mat4<T> operator -=(Mat4<T> const m)
1178    {
1179        return *this = *this - m;
1180    }
1181
1182    inline Mat4<T> operator *(Mat4<T> const m) const
1183    {
1184        return Mat4<T>(*this * m[0], *this * m[1], *this * m[2], *this * m[3]);
1185    }
1186
1187    inline Mat4<T> operator *=(Mat4<T> const m)
1188    {
1189        return *this = *this * m;
1190    }
1191
1192    inline Vec4<T> operator *(Vec4<T> const m) const
1193    {
1194        Vec4<T> ret;
1195        for (int j = 0; j < 4; j++)
1196        {
1197            T tmp = 0;
1198            for (int k = 0; k < 4; k++)
1199                tmp += (*this)[k][j] * m[k];
1200            ret[j] = tmp;
1201        }
1202        return ret;
1203    }
1204
1205    Vec4<T> v0, v1, v2, v3;
1206};
1207
1208/*
1209 * Arbitrarily-sized square matrices; for now this only supports
1210 * naive inversion and is used for the Remez inversion method.
1211 */
1212
1213template<int N, typename T> struct Mat
1214{
1215    inline Mat<N, T>() {}
1216
1217    Mat(T x)
1218    {
1219        for (int j = 0; j < N; j++)
1220            for (int i = 0; i < N; i++)
1221                if (i == j)
1222                    m[i][j] = x;
1223                else
1224                    m[i][j] = 0;
1225    }
1226
1227    /* Naive matrix inversion */
1228    Mat<N, T> inv() const
1229    {
1230        Mat a = *this, b((T)1);
1231
1232        /* Inversion method: iterate through all columns and make sure
1233         * all the terms are 1 on the diagonal and 0 everywhere else */
1234        for (int i = 0; i < N; i++)
1235        {
1236            /* If the expected coefficient is zero, add one of
1237             * the other lines. The first we meet will do. */
1238            if (!a.m[i][i])
1239            {
1240                for (int j = i + 1; j < N; j++)
1241                {
1242                    if (!a.m[i][j])
1243                        continue;
1244                    /* Add row j to row i */
1245                    for (int n = 0; n < N; n++)
1246                    {
1247                        a.m[n][i] += a.m[n][j];
1248                        b.m[n][i] += b.m[n][j];
1249                    }
1250                    break;
1251                }
1252            }
1253
1254            /* Now we know the diagonal term is non-zero. Get its inverse
1255             * and use that to nullify all other terms in the column */
1256            T x = (T)1 / a.m[i][i];
1257            for (int j = 0; j < N; j++)
1258            {
1259                if (j == i)
1260                    continue;
1261                T mul = x * a.m[i][j];
1262                for (int n = 0; n < N; n++)
1263                {
1264                    a.m[n][j] -= mul * a.m[n][i];
1265                    b.m[n][j] -= mul * b.m[n][i];
1266                }
1267            }
1268
1269            /* Finally, ensure the diagonal term is 1 */
1270            for (int n = 0; n < N; n++)
1271            {
1272                a.m[n][i] *= x;
1273                b.m[n][i] *= x;
1274            }
1275        }
1276
1277        return b;
1278    }
1279
1280    T m[N][N];
1281};
1282
1283} /* namespace lol */
1284
1285#endif // __LOL_MATH_VECTOR_H__
1286
Note: See TracBrowser for help on using the repository browser.