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

Last change on this file since 1157 was 1157, checked in by sam, 10 years ago

math: minor tweak to avoid using macros with empty parameters.

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