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

Last change on this file since 2279 was 2279, checked in by touky, 10 years ago

Small fix in template lerp

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