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

Last change on this file since 1257 was 1257, checked in by sam, 8 years ago

math: add inversion code for 2×2 and 3×3 matrices, and transposition
code for all matrices.

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