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

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

math: chage quaternion constructor to wxyz order because it matches the
mathematical writing, and add static constructors to create quaternions
from a rotation.

  • Property svn:keywords set to Id
File size: 57.3 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 W) : x(0), y(0), z(0), w(W) {}
913    inline Quat(T W, T X, T Y, T Z) : 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    static Quat<T> rotate(T angle, T x, T y, T z);
921    static Quat<T> rotate(T angle, Vec3<T> const &v);
922
923    inline Quat<T> operator *(Quat<T> const &val) const
924    {
925        Quat<T> ret;
926        Vec3<T> v1(x, y, z);
927        Vec3<T> v2(val.x, val.y, val.z);
928        Vec3<T> v3 = cross(v1, v2) + w * v2 + val.w * v1;
929        return Quat<T>(w * val.w - dot(v1, v2), v3.x, v3.y, v3.z);
930    }
931
932    inline Quat<T> operator *=(Quat<T> const &val)
933    {
934        return *this = (*this) * val;
935    }
936
937    inline Quat<T> operator ~() const
938    {
939        return Quat<T>(w, -x, -y, -z);
940    }
941
942#if !defined __ANDROID__
943    template<typename U>
944    friend std::ostream &operator<<(std::ostream &stream, Quat<U> const &v);
945#endif
946
947    /* Storage order is still xyzw because operator[] uses &this->x */
948    T x, y, z, w;
949};
950
951template<typename T>
952inline T norm(Quat<T> const &val)
953{
954    return sqlen(val);
955}
956
957template<typename T>
958static inline Quat<T> re(Quat<T> const &val)
959{
960    return ~val / norm(val);
961}
962
963template<typename T>
964static inline Quat<T> operator /(T x, Quat<T> const &y)
965{
966    return x * re(y);
967}
968
969template<typename T>
970static inline Quat<T> operator /(Quat<T> x, Quat<T> const &y)
971{
972    return x * re(y);
973}
974
975/*
976 * Common operators for all vector types, including quaternions
977 */
978
979#define DECLARE_VECTOR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
980    tprefix \
981    inline tname<tf> operator op(tname<t1> const &a, tname<t2> const &b) \
982    { \
983        tname<tf> ret; \
984        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
985            ret[n] = a[n] op b[n]; \
986        return ret; \
987    }
988
989#define DECLARE_VECTOR_VECTOR_OP(tname, op, tprefix, type) \
990    tprefix \
991    inline tname<type> operator op##=(tname<type> &a, tname<type> const &b) \
992    { \
993        return a = a op b; \
994    }
995
996#define DECLARE_VECTOR_VECTOR_BOOLOP(tname, op, op2, ret, tprefix, t1, t2) \
997    tprefix \
998    inline bool operator op(tname<t1> const &a, tname<t2> const &b) \
999    { \
1000        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1001            if (!(a[n] op2 b[n])) \
1002                return !ret; \
1003        return ret; \
1004    }
1005
1006#define DECLARE_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
1007    tprefix \
1008    inline tname<tf> operator op(tname<t1> const &a, t2 const &val) \
1009    { \
1010        tname<tf> ret; \
1011        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1012            ret[n] = a[n] op val; \
1013        return ret; \
1014    } \
1015    \
1016    tprefix \
1017    inline tname<tf> operator op(t1 const &val, tname<t2> const &a) \
1018    { \
1019        tname<tf> ret; \
1020        for (size_t n = 0; n < sizeof(a) / sizeof(t2); n++) \
1021            ret[n] = a[n] op val; \
1022        return ret; \
1023    }
1024
1025#define DECLARE_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
1026    tprefix \
1027    inline tname<type> operator op##=(tname<type> &a, type const &val) \
1028    { \
1029        return a = a op val; \
1030    }
1031
1032#define DECLARE_UNARY_OPS(tname, tprefix, type) \
1033    tprefix \
1034    inline tname<type> operator -(tname<type> const &a) \
1035    { \
1036        tname<type> ret; \
1037        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1038            ret[n] = -a[n]; \
1039        return ret; \
1040    } \
1041    \
1042    tprefix \
1043    inline type sqlen(tname<type> const &a) \
1044    { \
1045        type acc = 0; \
1046        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1047            acc += a[n] * a[n]; \
1048        return acc; \
1049    } \
1050    \
1051    tprefix \
1052    inline double len(tname<type> const &a) \
1053    { \
1054        using std::sqrt; \
1055        return sqrt((double)sqlen(a)); \
1056    } \
1057    \
1058    tprefix \
1059    inline tname<type> normalize(tname<type> const &val) \
1060    { \
1061        type norm = (type)len(val); \
1062        return norm ? val / norm : val * (type)0; \
1063    }
1064
1065#define DECLARE_BINARY_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1066    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
1067    DECLARE_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
1068    \
1069    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, -, tprefix, t1, t2, tf) \
1070    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, +, tprefix, t1, t2, tf) \
1071    \
1072    DECLARE_VECTOR_VECTOR_BOOLOP(tname, ==, ==, true, tprefix, t1, t2) \
1073    DECLARE_VECTOR_VECTOR_BOOLOP(tname, !=, ==, false, tprefix, t1, t2) \
1074    \
1075    tprefix \
1076    inline tf dot(tname<t1> const &a, tname<t2> const &b) \
1077    { \
1078        tf ret = 0; \
1079        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1080            ret += a[n] * b[n]; \
1081        return ret; \
1082    }
1083
1084#define DECLARE_VEC_3_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1085    tprefix \
1086    inline tname<tf> cross(tname<t1> const &a, tname<t2> const &b) \
1087    { \
1088        return tname<tf>((tf)(a.y * b.z) - (tf)(a.z * b.y), \
1089                         (tf)(a.z * b.x) - (tf)(a.x * b.z), \
1090                         (tf)(a.x * b.y) - (tf)(a.y * b.x)); \
1091    }
1092
1093#define DECLARE_BINARY_OPS(tname, tprefix, type) \
1094    DECLARE_BINARY_COERCE_OPS(tname, tprefix, type, type, type) \
1095    \
1096    DECLARE_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
1097    DECLARE_VECTOR_SCALAR_OP(tname, /, tprefix, type) \
1098    \
1099    DECLARE_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
1100    DECLARE_VECTOR_VECTOR_OP(tname, +, tprefix, type)
1101
1102#define DECLARE_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1103    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
1104    DECLARE_VECTOR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
1105    \
1106    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <=, <=, true, tprefix, t1, t2) \
1107    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >=, >=, true, tprefix, t1, t2) \
1108    DECLARE_VECTOR_VECTOR_BOOLOP(tname, <, <, true, tprefix, t1, t2) \
1109    DECLARE_VECTOR_VECTOR_BOOLOP(tname, >, >, true, tprefix, t1, t2)
1110
1111#define DECLARE_VECTOR_OPS(tname, tprefix, type) \
1112    DECLARE_VECTOR_COERCE_OPS(tname, static, type, type, type) \
1113    \
1114    DECLARE_VECTOR_VECTOR_OP(tname, *, tprefix, type) \
1115    DECLARE_VECTOR_VECTOR_OP(tname, /, tprefix, type)
1116
1117#define DECLARE_ALL_NONVECTOR_OPS(tname) \
1118    DECLARE_BINARY_OPS(tname, template<typename T> static, T) \
1119    DECLARE_UNARY_OPS(tname, template<typename T> static, T)
1120
1121#define DECLARE_ALL_VECTOR_OPS_INNER(tname, type) \
1122    DECLARE_BINARY_OPS(tname, static, type) \
1123    DECLARE_UNARY_OPS(tname, static, type) \
1124    DECLARE_VECTOR_OPS(tname, static, type) \
1125
1126#define DECLARE_ALL_VECTOR_OPS(type) \
1127    DECLARE_ALL_VECTOR_OPS_INNER(Vec2, type) \
1128    DECLARE_ALL_VECTOR_OPS_INNER(Vec3, type) \
1129    DECLARE_ALL_VECTOR_OPS_INNER(Vec4, type) \
1130    \
1131    DECLARE_VEC_3_COERCE_OPS(Vec3, static, type, type, type)
1132
1133#define DECLARE_VEC_ANY_COERCE_OPS(tname, tlow, thigh) \
1134    DECLARE_BINARY_COERCE_OPS(tname, static, tlow, thigh, thigh) \
1135    DECLARE_BINARY_COERCE_OPS(tname, static, thigh, tlow, thigh) \
1136    \
1137    DECLARE_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
1138    DECLARE_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh)
1139
1140#define DECLARE_ALL_VECTOR_COERCE_OPS(tlow, thigh) \
1141    DECLARE_VEC_ANY_COERCE_OPS(Vec2, tlow, thigh) \
1142    DECLARE_VEC_ANY_COERCE_OPS(Vec3, tlow, thigh) \
1143    DECLARE_VEC_ANY_COERCE_OPS(Vec4, tlow, thigh) \
1144    \
1145    DECLARE_VEC_3_COERCE_OPS(Vec3, static, tlow, thigh, thigh) \
1146    DECLARE_VEC_3_COERCE_OPS(Vec3, static, thigh, tlow, thigh)
1147
1148DECLARE_ALL_NONVECTOR_OPS(Cmplx)
1149DECLARE_ALL_NONVECTOR_OPS(Quat)
1150
1151/* Disable warning about unary operator applied to unsigned type */
1152#if defined _MSC_VER
1153#   pragma warning(push)
1154#   pragma warning(disable: 4146)
1155#endif
1156
1157DECLARE_ALL_VECTOR_OPS(half)
1158DECLARE_ALL_VECTOR_OPS(float)
1159DECLARE_ALL_VECTOR_OPS(double)
1160DECLARE_ALL_VECTOR_OPS(int8_t)
1161DECLARE_ALL_VECTOR_OPS(uint8_t)
1162DECLARE_ALL_VECTOR_OPS(int16_t)
1163DECLARE_ALL_VECTOR_OPS(uint16_t)
1164DECLARE_ALL_VECTOR_OPS(int32_t)
1165DECLARE_ALL_VECTOR_OPS(uint32_t)
1166DECLARE_ALL_VECTOR_OPS(int64_t)
1167DECLARE_ALL_VECTOR_OPS(uint64_t)
1168
1169#if defined _MSC_VER
1170#   pragma warning(pop)
1171#endif
1172
1173/* Disable warnings in the >= > etc. operators about comparing signed and
1174 * unsigned. Ideally we would like to get these warnings only when the
1175 * inlined operators are actually used, but they seem to be triggered at
1176 * the code parsing step, so the compiler does not yet know whether they
1177 * will be used.
1178 * Also we do this for the whole block of declarations, because GCC prior
1179 * to 4.6.3 does not appear to support _Pragma() inside a macro. */
1180#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1181#   pragma GCC diagnostic push
1182#   pragma GCC diagnostic ignored "-Wsign-compare"
1183#elif defined _MSC_VER
1184#   pragma warning(push)
1185#   pragma warning(disable: 4018)
1186#endif
1187
1188/* Apply the same coercion rules as in the C++ standard. However, instead
1189 * of always promoting smaller types to int, we allow int8_t op int16_t to
1190 * return an int16_t. */
1191DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint8_t)
1192DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int16_t)
1193DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint16_t)
1194DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int32_t)
1195DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint32_t)
1196DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, int64_t)
1197DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, uint64_t)
1198DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, float)
1199DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, double)
1200DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, long double)
1201
1202DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int16_t)
1203DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint16_t)
1204DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int32_t)
1205DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint32_t)
1206DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, int64_t)
1207DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, uint64_t)
1208DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, float)
1209DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, double)
1210DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, long double)
1211
1212DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint16_t)
1213DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, int32_t)
1214DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint32_t)
1215DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, int64_t)
1216DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, uint64_t)
1217DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, float)
1218DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, double)
1219DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, long double)
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)
1225DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, float)
1226DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, double)
1227DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, long double)
1228
1229DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint32_t)
1230DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, int64_t)
1231DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, uint64_t)
1232DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, float)
1233DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, double)
1234DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, long double)
1235
1236DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, int64_t)
1237DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, uint64_t)
1238DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, float)
1239DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, double)
1240DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, long double)
1241
1242DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, uint64_t)
1243DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, float)
1244DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, double)
1245DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, long double)
1246
1247DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, float)
1248DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, double)
1249DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, long double)
1250
1251DECLARE_ALL_VECTOR_COERCE_OPS(float, double)
1252DECLARE_ALL_VECTOR_COERCE_OPS(float, long double)
1253
1254DECLARE_ALL_VECTOR_COERCE_OPS(double, long double)
1255
1256/* FIXME: vectors of "half" are deactivated for now, because they
1257 * induce extremely long compilation times (about 17 seconds per TU). */
1258
1259#if 0
1260/* All integer types are promoted to half; all floating point types
1261 * cause half to be promoted. */
1262DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, half)
1263DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, half)
1264DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, half)
1265DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, half)
1266DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, half)
1267DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, half)
1268DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, half)
1269DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, half)
1270
1271DECLARE_ALL_VECTOR_COERCE_OPS(half, float)
1272DECLARE_ALL_VECTOR_COERCE_OPS(half, double)
1273DECLARE_ALL_VECTOR_COERCE_OPS(half, long double)
1274#endif
1275
1276/* FIXME: vectors of "real" are deactivated for now, because we do
1277 * not implement all combinations of operators for these types yet. */
1278
1279#if 0
1280/* All types are promoted to real */
1281DECLARE_ALL_VECTOR_COERCE_OPS(int8_t, real)
1282DECLARE_ALL_VECTOR_COERCE_OPS(uint8_t, real)
1283DECLARE_ALL_VECTOR_COERCE_OPS(int16_t, real)
1284DECLARE_ALL_VECTOR_COERCE_OPS(uint16_t, real)
1285DECLARE_ALL_VECTOR_COERCE_OPS(int32_t, real)
1286DECLARE_ALL_VECTOR_COERCE_OPS(uint32_t, real)
1287DECLARE_ALL_VECTOR_COERCE_OPS(int64_t, real)
1288DECLARE_ALL_VECTOR_COERCE_OPS(uint64_t, real)
1289DECLARE_ALL_VECTOR_COERCE_OPS(half, real)
1290DECLARE_ALL_VECTOR_COERCE_OPS(float, real)
1291DECLARE_ALL_VECTOR_COERCE_OPS(double, real)
1292DECLARE_ALL_VECTOR_COERCE_OPS(long double, real)
1293#endif
1294
1295#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1296#   pragma GCC diagnostic pop
1297#elif defined _MSC_VER
1298#   pragma warning(pop)
1299#endif
1300
1301#undef DECLARE_VECTOR_TYPEDEFS
1302#undef DECLARE_MEMBER_OPS
1303#undef DECLARE_VECTOR_VECTOR_OP
1304#undef DECLARE_VECTOR_VECTOR_BOOLOP
1305#undef DECLARE_VECTOR_SCALAR_OP
1306#undef DECLARE_BINARY_OPS
1307#undef DECLARE_UNARY_OPS
1308#undef DECLARE_ALL_NONVECTOR_OPS
1309#undef DECLARE_ALL_VECTOR_OPS_INNER
1310#undef DECLARE_ALL_VECTOR_OPS
1311
1312/*
1313 * Magic vector swizzling (part 2/2)
1314 * Unfortunately these assignment operators cannot be used for now, because
1315 * we would also need to override the default copy assignment operator, and
1316 * in C++98 unions cannot contain such objects. This is why all the swizzling
1317 * magic objects are marked 'const' even those that could be lvalues.
1318 */
1319
1320template<typename T, int N>
1321inline Vec2<T> XVec2<T, N>::operator =(Vec2<T> const &that)
1322{
1323    for (int i = 0; i < 2; i++)
1324        *this[i] = that[i];
1325    return *this;
1326}
1327
1328template<typename T, int N>
1329inline Vec3<T> XVec3<T, N>::operator =(Vec3<T> const &that)
1330{
1331    for (int i = 0; i < 3; i++)
1332        *this[i] = that[i];
1333    return *this;
1334}
1335
1336template<typename T, int N>
1337inline Vec4<T> XVec4<T, N>::operator =(Vec4<T> const &that)
1338{
1339    for (int i = 0; i < 4; i++)
1340        *this[i] = that[i];
1341    return *this;
1342}
1343
1344/*
1345 * 2×2-element matrices
1346 */
1347
1348template <typename T> struct Mat2
1349{
1350    inline Mat2() {}
1351    inline Mat2(Vec2<T> V0, Vec2<T> V1)
1352      : v0(V0), v1(V1) {}
1353
1354    explicit inline Mat2(T val)
1355      : v0(val, (T)0),
1356        v1((T)0, val) {}
1357
1358    explicit inline Mat2(Mat4<T> const &mat)
1359      : v0(mat[0].xy),
1360        v1(mat[1].xy) {}
1361
1362    inline Vec2<T>& operator[](size_t n) { return (&v0)[n]; }
1363    inline Vec2<T> const& operator[](size_t n) const { return (&v0)[n]; }
1364
1365    /* Helpers for transformation matrices */
1366    static Mat2<T> rotate(T angle);
1367
1368    static inline Mat2<T> rotate(Mat2<T> mat, T angle)
1369    {
1370        return rotate(angle) * mat;
1371    }
1372
1373    void printf() const;
1374
1375#if !defined __ANDROID__
1376    template<class U>
1377    friend std::ostream &operator<<(std::ostream &stream, Mat2<U> const &m);
1378#endif
1379
1380    inline Mat2<T> operator +(Mat2<T> const m) const
1381    {
1382        return Mat2<T>(v0 + m[0], v1 + m[1]);
1383    }
1384
1385    inline Mat2<T> operator +=(Mat2<T> const m)
1386    {
1387        return *this = *this + m;
1388    }
1389
1390    inline Mat2<T> operator -(Mat2<T> const m) const
1391    {
1392        return Mat2<T>(v0 - m[0], v1 - m[1]);
1393    }
1394
1395    inline Mat2<T> operator -=(Mat2<T> const m)
1396    {
1397        return *this = *this - m;
1398    }
1399
1400    inline Mat2<T> operator *(Mat2<T> const m) const
1401    {
1402        return Mat2<T>(*this * m[0], *this * m[1]);
1403    }
1404
1405    inline Mat2<T> operator *=(Mat2<T> const m)
1406    {
1407        return *this = *this * m;
1408    }
1409
1410    inline Vec2<T> operator *(Vec2<T> const m) const
1411    {
1412        Vec2<T> ret;
1413        for (int j = 0; j < 2; j++)
1414        {
1415            T tmp = 0;
1416            for (int k = 0; k < 2; k++)
1417                tmp += (*this)[k][j] * m[k];
1418            ret[j] = tmp;
1419        }
1420        return ret;
1421    }
1422
1423    Vec2<T> v0, v1;
1424};
1425
1426/*
1427 * 3×3-element matrices
1428 */
1429
1430template <typename T> struct Mat3
1431{
1432    inline Mat3() {}
1433    inline Mat3(Vec3<T> V0, Vec3<T> V1, Vec3<T> V2)
1434      : v0(V0), v1(V1), v2(V2) {}
1435
1436    explicit inline Mat3(T val)
1437      : v0(val, (T)0, (T)0),
1438        v1((T)0, val, (T)0),
1439        v2((T)0, (T)0, val) {}
1440
1441    explicit inline Mat3(Mat2<T> mat)
1442      : v0(mat[0], (T)0),
1443        v1(mat[1], (T)0),
1444        v2((T)0, (T)0, (T)0) {}
1445
1446    explicit inline Mat3(Mat2<T> mat, T val)
1447      : v0(Vec3<T>(mat[0], (T)0)),
1448        v1(Vec3<T>(mat[1], (T)0)),
1449        v2((T)0, (T)0, val) {}
1450
1451    explicit inline Mat3(Mat4<T> const &mat)
1452      : v0(mat[0].xyz),
1453        v1(mat[1].xyz),
1454        v2(mat[2].xyz) {}
1455
1456    inline Vec3<T>& operator[](size_t n) { return (&v0)[n]; }
1457    inline Vec3<T> const& operator[](size_t n) const { return (&v0)[n]; }
1458
1459    /* Helpers for transformation matrices */
1460    static Mat3<T> scale(T x, T y, T z);
1461    static Mat3<T> scale(Vec3<T> v);
1462    static Mat3<T> rotate(T angle, T x, T y, T z);
1463    static Mat3<T> rotate(T angle, Vec3<T> v);
1464    static Mat3<T> rotate(Quat<T> q);
1465
1466    static inline Mat3<T> rotate(Mat3<T> mat, T angle, Vec3<T> v)
1467    {
1468        return rotate(angle, v) * mat;
1469    }
1470
1471    void printf() const;
1472
1473#if !defined __ANDROID__
1474    template<class U>
1475    friend std::ostream &operator<<(std::ostream &stream, Mat3<U> const &m);
1476#endif
1477
1478    inline Mat3<T> operator +(Mat3<T> const m) const
1479    {
1480        return Mat3<T>(v0 + m[0], v1 + m[1], v2 + m[2]);
1481    }
1482
1483    inline Mat3<T> operator +=(Mat3<T> const m)
1484    {
1485        return *this = *this + m;
1486    }
1487
1488    inline Mat3<T> operator -(Mat3<T> const m) const
1489    {
1490        return Mat3<T>(v0 - m[0], v1 - m[1], v2 - m[2]);
1491    }
1492
1493    inline Mat3<T> operator -=(Mat3<T> const m)
1494    {
1495        return *this = *this - m;
1496    }
1497
1498    inline Mat3<T> operator *(Mat3<T> const m) const
1499    {
1500        return Mat3<T>(*this * m[0], *this * m[1], *this * m[2]);
1501    }
1502
1503    inline Mat3<T> operator *=(Mat3<T> const m)
1504    {
1505        return *this = *this * m;
1506    }
1507
1508    inline Vec3<T> operator *(Vec3<T> const m) const
1509    {
1510        Vec3<T> ret;
1511        for (int j = 0; j < 3; j++)
1512        {
1513            T tmp = 0;
1514            for (int k = 0; k < 3; k++)
1515                tmp += (*this)[k][j] * m[k];
1516            ret[j] = tmp;
1517        }
1518        return ret;
1519    }
1520
1521    Vec3<T> v0, v1, v2;
1522};
1523
1524/*
1525 * 4×4-element matrices
1526 */
1527
1528template <typename T> struct Mat4
1529{
1530    inline Mat4() {}
1531    inline Mat4(Vec4<T> V0, Vec4<T> V1, Vec4<T> V2, Vec4<T> V3)
1532      : v0(V0), v1(V1), v2(V2), v3(V3) {}
1533
1534    explicit inline Mat4(T val)
1535      : v0(val, (T)0, (T)0, (T)0),
1536        v1((T)0, val, (T)0, (T)0),
1537        v2((T)0, (T)0, val, (T)0),
1538        v3((T)0, (T)0, (T)0, val) {}
1539
1540    explicit inline Mat4(Mat2<T> mat)
1541      : v0(mat[0], (T)0, (T)0),
1542        v1(mat[1], (T)0, (T)0),
1543        v2((T)0, (T)0, (T)0, (T)0),
1544        v3((T)0, (T)0, (T)0, (T)0) {}
1545
1546    explicit inline Mat4(Mat2<T> mat, T val1, T val2)
1547      : v0(mat[0], (T)0, (T)0),
1548        v1(mat[1], (T)0, (T)0),
1549        v2((T)0, (T)0, val1, (T)0),
1550        v3((T)0, (T)0, (T)0, val2) {}
1551
1552    explicit inline Mat4(Mat3<T> mat)
1553      : v0(mat[0], (T)0),
1554        v1(mat[1], (T)0),
1555        v2(mat[2], (T)0),
1556        v3((T)0, (T)0, (T)0, (T)0) {}
1557
1558    explicit inline Mat4(Mat3<T> mat, T val)
1559      : v0(mat[0], (T)0),
1560        v1(mat[1], (T)0),
1561        v2(mat[2], (T)0),
1562        v3((T)0, (T)0, (T)0, val) {}
1563
1564    inline Vec4<T>& operator[](size_t n) { return (&v0)[n]; }
1565    inline Vec4<T> const& operator[](size_t n) const { return (&v0)[n]; }
1566
1567    /* Helpers for transformation matrices */
1568    static Mat4<T> translate(T x, T y, T z);
1569    static Mat4<T> translate(Vec3<T> v);
1570
1571    static inline Mat4<T> scale(T x, T y, T z)
1572    {
1573        return Mat4<T>(Mat3<T>::scale(x, y, z), (T)1);
1574    }
1575
1576    static inline Mat4<T> scale(Vec3<T> v)
1577    {
1578        return Mat4<T>(Mat3<T>::scale(v), (T)1);
1579    }
1580
1581    static inline Mat4<T> translate(Mat4<T> const &mat, Vec3<T> v)
1582    {
1583        return translate(v) * mat;
1584    }
1585
1586    static inline Mat4<T> rotate(T angle, T x, T y, T z)
1587    {
1588        return Mat4<T>(Mat3<T>::rotate(angle, x, y, z), (T)1);
1589    }
1590
1591    static inline Mat4<T> rotate(T angle, Vec3<T> v)
1592    {
1593        return Mat4<T>(Mat3<T>::rotate(angle, v), (T)1);
1594    }
1595
1596    static inline Mat4<T> rotate(Quat<T> q)
1597    {
1598        return Mat4<T>(Mat3<T>::rotate(q), (T)1);
1599    }
1600
1601    static inline Mat4<T> rotate(Mat4<T> &mat, T angle, Vec3<T> v)
1602    {
1603        return rotate(angle, v) * mat;
1604    }
1605
1606    /* Helpers for view matrices */
1607    static Mat4<T> lookat(Vec3<T> eye, Vec3<T> center, Vec3<T> up);
1608
1609    /* Helpers for projection matrices */
1610    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
1611    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
1612    static Mat4<T> perspective(T fov_y, T width, T height, T near, T far);
1613
1614    void printf() const;
1615
1616#if !defined __ANDROID__
1617    template<class U>
1618    friend std::ostream &operator<<(std::ostream &stream, Mat4<U> const &m);
1619#endif
1620
1621    inline Mat4<T> operator +(Mat4<T> const &m) const
1622    {
1623        return Mat4<T>(v0 + m[0], v1 + m[1], v2 + m[2], v3 + m[3]);
1624    }
1625
1626    inline Mat4<T> operator +=(Mat4<T> const &m)
1627    {
1628        return *this = *this + m;
1629    }
1630
1631    inline Mat4<T> operator -(Mat4<T> const &m) const
1632    {
1633        return Mat4<T>(v0 - m[0], v1 - m[1], v2 - m[2], v3 - m[3]);
1634    }
1635
1636    inline Mat4<T> operator -=(Mat4<T> const &m)
1637    {
1638        return *this = *this - m;
1639    }
1640
1641    inline Mat4<T> operator *(Mat4<T> const &m) const
1642    {
1643        return Mat4<T>(*this * m[0], *this * m[1], *this * m[2], *this * m[3]);
1644    }
1645
1646    inline Mat4<T> operator *=(Mat4<T> const &m)
1647    {
1648        return *this = *this * m;
1649    }
1650
1651    inline Vec4<T> operator *(Vec4<T> const &m) const
1652    {
1653        Vec4<T> ret;
1654        for (int j = 0; j < 4; j++)
1655        {
1656            T tmp = 0;
1657            for (int k = 0; k < 4; k++)
1658                tmp += (*this)[k][j] * m[k];
1659            ret[j] = tmp;
1660        }
1661        return ret;
1662    }
1663
1664    Vec4<T> v0, v1, v2, v3;
1665};
1666
1667template<typename T> T determinant(Mat2<T> const &);
1668template<typename T> T determinant(Mat3<T> const &);
1669template<typename T> T determinant(Mat4<T> const &);
1670
1671template<typename T> Mat2<T> transpose(Mat2<T> const &);
1672template<typename T> Mat3<T> transpose(Mat3<T> const &);
1673template<typename T> Mat4<T> transpose(Mat4<T> const &);
1674
1675template<typename T> Mat2<T> inverse(Mat2<T> const &);
1676template<typename T> Mat3<T> inverse(Mat3<T> const &);
1677template<typename T> Mat4<T> inverse(Mat4<T> const &);
1678
1679/*
1680 * Arbitrarily-sized square matrices; for now this only supports
1681 * naive inversion and is used for the Remez inversion method.
1682 */
1683
1684template<int N, typename T> struct Mat
1685{
1686    inline Mat<N, T>() {}
1687
1688    Mat(T x)
1689    {
1690        for (int j = 0; j < N; j++)
1691            for (int i = 0; i < N; i++)
1692                if (i == j)
1693                    m[i][j] = x;
1694                else
1695                    m[i][j] = 0;
1696    }
1697
1698    /* Naive matrix inversion */
1699    Mat<N, T> inv() const
1700    {
1701        Mat a = *this, b((T)1);
1702
1703        /* Inversion method: iterate through all columns and make sure
1704         * all the terms are 1 on the diagonal and 0 everywhere else */
1705        for (int i = 0; i < N; i++)
1706        {
1707            /* If the expected coefficient is zero, add one of
1708             * the other lines. The first we meet will do. */
1709            if (!a.m[i][i])
1710            {
1711                for (int j = i + 1; j < N; j++)
1712                {
1713                    if (!a.m[i][j])
1714                        continue;
1715                    /* Add row j to row i */
1716                    for (int n = 0; n < N; n++)
1717                    {
1718                        a.m[n][i] += a.m[n][j];
1719                        b.m[n][i] += b.m[n][j];
1720                    }
1721                    break;
1722                }
1723            }
1724
1725            /* Now we know the diagonal term is non-zero. Get its inverse
1726             * and use that to nullify all other terms in the column */
1727            T x = (T)1 / a.m[i][i];
1728            for (int j = 0; j < N; j++)
1729            {
1730                if (j == i)
1731                    continue;
1732                T mul = x * a.m[i][j];
1733                for (int n = 0; n < N; n++)
1734                {
1735                    a.m[n][j] -= mul * a.m[n][i];
1736                    b.m[n][j] -= mul * b.m[n][i];
1737                }
1738            }
1739
1740            /* Finally, ensure the diagonal term is 1 */
1741            for (int n = 0; n < N; n++)
1742            {
1743                a.m[n][i] *= x;
1744                b.m[n][i] *= x;
1745            }
1746        }
1747
1748        return b;
1749    }
1750
1751    T m[N][N];
1752};
1753
1754} /* namespace lol */
1755
1756#endif // __LOL_MATH_VECTOR_H__
1757
Note: See TracBrowser for help on using the repository browser.