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

Last change on this file since 1175 was 1175, checked in by sam, 9 years ago

math: move cross product out of the .cpp file.

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