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

Last change on this file since 2346 was 2346, checked in by touky, 6 years ago

comment : Angle in degree
Because goldfishes have no memory.

  • Property svn:keywords set to Id
File size: 62.1 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    //Angle in degree
940    static Quat<T> rotate(T angle, T x, T y, T z);
941    static Quat<T> rotate(T angle, Vec3<T> const &v);
942
943    /* Convert from Euler angles. The axes in fromeuler_xyx are
944     * x, then y', then x", ie. the axes are attached to the model.
945     * If you want to rotate around static axes, just reverse the order
946     * of the arguments. */
947    static Quat<T> fromeuler_xyx(Vec3<T> const &v);
948    static Quat<T> fromeuler_xzx(Vec3<T> const &v);
949    static Quat<T> fromeuler_yxy(Vec3<T> const &v);
950    static Quat<T> fromeuler_yzy(Vec3<T> const &v);
951    static Quat<T> fromeuler_zxz(Vec3<T> const &v);
952    static Quat<T> fromeuler_zyz(Vec3<T> const &v);
953    static Quat<T> fromeuler_xyx(T phi, T theta, T psi);
954    static Quat<T> fromeuler_xzx(T phi, T theta, T psi);
955    static Quat<T> fromeuler_yxy(T phi, T theta, T psi);
956    static Quat<T> fromeuler_yzy(T phi, T theta, T psi);
957    static Quat<T> fromeuler_zxz(T phi, T theta, T psi);
958    static Quat<T> fromeuler_zyz(T phi, T theta, T psi);
959
960    /* Convert from Tait-Bryan angles (incorrectly called Euler angles,
961     * but since everyone does it…). The axes in fromeuler_xyz are
962     * x, then y', then z", ie. the axes are attached to the model.
963     * If you want to apply yaw around x, pitch around y, and roll
964     * around z, use fromeuler_xyz.
965     * If you want to rotate around static axes, reverse the order in
966     * the function name (_zyx instead of _xyz) AND reverse the order
967     * of the arguments. */
968    static Quat<T> fromeuler_xyz(Vec3<T> const &v);
969    static Quat<T> fromeuler_xzy(Vec3<T> const &v);
970    static Quat<T> fromeuler_yxz(Vec3<T> const &v);
971    static Quat<T> fromeuler_yzx(Vec3<T> const &v);
972    static Quat<T> fromeuler_zxy(Vec3<T> const &v);
973    static Quat<T> fromeuler_zyx(Vec3<T> const &v);
974    static Quat<T> fromeuler_xyz(T phi, T theta, T psi);
975    static Quat<T> fromeuler_xzy(T phi, T theta, T psi);
976    static Quat<T> fromeuler_yxz(T phi, T theta, T psi);
977    static Quat<T> fromeuler_yzx(T phi, T theta, T psi);
978    static Quat<T> fromeuler_zxy(T phi, T theta, T psi);
979    static Quat<T> fromeuler_zyx(T phi, T theta, T psi);
980
981    inline Quat<T> operator *(Quat<T> const &val) const;
982
983    inline Quat<T> operator *=(Quat<T> const &val)
984    {
985        return *this = (*this) * val;
986    }
987
988    inline Quat<T> operator ~() const
989    {
990        return Quat<T>(w, -x, -y, -z);
991    }
992
993    inline Vec3<T> transform(Vec3<T> const &v)
994    {
995        Quat<T> p = Quat<T>(0, v.x, v.y, v.z);
996        Quat<T> q = *this * p / *this;
997        return Vec3<T>(q.x, q.y, q.z);
998    }
999
1000    template<typename U>
1001    friend std::ostream &operator<<(std::ostream &stream, Quat<U> const &v);
1002
1003    /* XXX: storage order is wxyz, unlike vectors! */
1004    T w, x, y, z;
1005};
1006
1007template<typename T>
1008inline T norm(Quat<T> const &val)
1009{
1010    return sqlength(val);
1011}
1012
1013template<typename T>
1014static inline Quat<T> re(Quat<T> const &val)
1015{
1016    return ~val / norm(val);
1017}
1018
1019template<typename T>
1020static inline Quat<T> operator /(T x, Quat<T> const &y)
1021{
1022    return x * re(y);
1023}
1024
1025template<typename T>
1026static inline Quat<T> operator /(Quat<T> const &x, Quat<T> const &y)
1027{
1028    return x * re(y);
1029}
1030
1031template<typename T>
1032extern Quat<T> slerp(Quat<T> const &qa, Quat<T> const &qb, T f);
1033
1034/*
1035 * Common operators for all vector types, including quaternions
1036 */
1037
1038/*
1039 * vec +(vec, vec)   (also complex & quaternion)
1040 * vec -(vec, vec)   (also complex & quaternion)
1041 * vec *(vec, vec)
1042 * vec /(vec, vec)
1043 */
1044#define LOL_VECTOR_VECTOR_OP(tname, op, tprefix, type) \
1045    tprefix \
1046    inline tname<type> operator op(tname<type> const &a, tname<type> const &b) \
1047    { \
1048        tname<type> ret; \
1049        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1050            ret[n] = a[n] op b[n]; \
1051        return ret; \
1052    }
1053
1054/*
1055 * vec +=(vec, vec)   (also complex & quaternion)
1056 * vec -=(vec, vec)   (also complex & quaternion)
1057 * vec *=(vec, vec)
1058 * vec /=(vec, vec)
1059 */
1060#define LOL_VECTOR_VECTOR_NONCONST_OP(tname, op, tprefix, type) \
1061    tprefix \
1062    inline tname<type> operator op##=(tname<type> &a, tname<type> const &b) \
1063    { \
1064        return a = a op b; \
1065    }
1066
1067/*
1068 * vec min(vec, vec)     (also max, fmod)
1069 * vec min(vec, scalar)  (also max, fmod)
1070 * vec min(scalar, vec)  (also max, fmod)
1071 */
1072#define LOL_VECTOR_MINMAX_FUN(tname, op, tprefix, type) \
1073    tprefix \
1074    inline tname<type> op(tname<type> const &a, tname<type> const &b) \
1075    { \
1076        using lol::op; \
1077        tname<type> ret; \
1078        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1079            ret[n] = op(a[n], b[n]); \
1080        return ret; \
1081    } \
1082    \
1083    tprefix \
1084    inline tname<type> op(tname<type> const &a, type const &b) \
1085    { \
1086        using lol::op; \
1087        tname<type> ret; \
1088        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1089            ret[n] = op(a[n], b); \
1090        return ret; \
1091    } \
1092    \
1093    tprefix \
1094    inline tname<type> op(type const &a, tname<type> const &b) \
1095    { \
1096        using lol::op; \
1097        tname<type> ret; \
1098        for (size_t n = 0; n < sizeof(b) / sizeof(type); n++) \
1099            ret[n] = op(a, b[n]); \
1100        return ret; \
1101    }
1102
1103/*
1104 * vec clamp(vec, vec, vec)
1105 * vec clamp(vec, vec, scalar)
1106 * vec clamp(vec, scalar, vec)
1107 * vec clamp(vec, scalar, scalar)
1108 */
1109#define LOL_VECTOR_CLAMP_FUN(tname, tprefix, type) \
1110    tprefix \
1111    inline tname<type> clamp(tname<type> const &x, \
1112                             tname<type> const &a, tname<type> const &b) \
1113    { \
1114        return max(min(x, b), a); \
1115    } \
1116    \
1117    tprefix \
1118    inline tname<type> clamp(tname<type> const &x, \
1119                             type const &a, tname<type> const &b) \
1120    { \
1121        return max(min(x, b), a); \
1122    } \
1123    \
1124    tprefix \
1125    inline tname<type> clamp(tname<type> const &x, \
1126                             tname<type> const &a, type const &b) \
1127    { \
1128        return max(min(x, b), a); \
1129    } \
1130    \
1131    tprefix \
1132    inline tname<type> clamp(tname<type> const &x, \
1133                             type const &a, type const &b) \
1134    { \
1135        return max(min(x, b), a); \
1136    }
1137
1138/*
1139 * vec mix(vec, vec, vec)
1140 * vec mix(vec, vec, scalar)
1141 */
1142#define LOL_VECTOR_MIX_FUN(tname, tprefix, type) \
1143    tprefix \
1144    inline tname<type> mix(tname<type> const &x, \
1145                           tname<type> const &y, tname<type> const &a) \
1146    { \
1147        return x + a * (y - x); \
1148    } \
1149    \
1150    tprefix \
1151    inline tname<type> mix(tname<type> const &x, \
1152                           tname<type> const &y, type const &a) \
1153    { \
1154        return x + a * (y - x); \
1155    }
1156
1157/*
1158 * bool ==(vec, vec)   (also complex & quaternion)
1159 * bool !=(vec, vec)   (also complex & quaternion)
1160 * bool >=(vec, vec)
1161 * bool <=(vec, vec)
1162 * bool >(vec, vec)
1163 * bool <(vec, vec)
1164 */
1165#define LOL_VECTOR_VECTOR_BOOL_OP(tname, op, op2, ret, tprefix, type) \
1166    tprefix \
1167    inline bool operator op(tname<type> const &a, tname<type> const &b) \
1168    { \
1169        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1170            if (!(a[n] op2 b[n])) \
1171                return !ret; \
1172        return ret; \
1173    }
1174
1175/*
1176 * vec *(vec, scalar)   (also complex & quaternion)
1177 * vec /(vec, scalar)   (also complex & quaternion)
1178 */
1179#define LOL_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
1180    tprefix \
1181    inline tname<type> operator op(tname<type> const &a, type const &val) \
1182    { \
1183        tname<type> ret; \
1184        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1185            ret[n] = a[n] op val; \
1186        return ret; \
1187    }
1188
1189/*
1190 * vec *(scalar, vec)   (also complex & quaternion)
1191 * vec /(scalar, vec)   (NOT for complex & quaternion!)
1192 */
1193#define LOL_SCALAR_VECTOR_OP(tname, op, tprefix, type) \
1194    tprefix \
1195    inline tname<type> operator op(type const &val, tname<type> const &a) \
1196    { \
1197        tname<type> ret; \
1198        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1199            ret[n] = a[n] op val; \
1200        return ret; \
1201    }
1202
1203/*
1204 * vec *=(vec, scalar)   (also complex & quaternion)
1205 * vec /=(vec, scalar)   (also complex & quaternion)
1206 */
1207#define LOL_VECTOR_SCALAR_NONCONST_OP(tname, op, tprefix, type) \
1208    tprefix \
1209    inline tname<type> operator op##=(tname<type> &a, type const &val) \
1210    { \
1211        return a = a op val; \
1212    }
1213
1214#define LOL_UNARY_OPS(tname, tprefix, type) \
1215    tprefix \
1216    inline tname<type> operator -(tname<type> const &a) \
1217    { \
1218        tname<type> ret; \
1219        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1220            ret[n] = -a[n]; \
1221        return ret; \
1222    }
1223
1224#define LOL_UNARY_FUNS(tname, tprefix, type) \
1225    tprefix \
1226    inline type sqlength(tname<type> const &a) \
1227    { \
1228        type acc = 0; \
1229        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1230            acc += a[n] * a[n]; \
1231        return acc; \
1232    } \
1233    \
1234    tprefix \
1235    inline type length(tname<type> const &a) \
1236    { \
1237        return (type)sqrt((double)sqlength(a)); \
1238    } \
1239    \
1240    tprefix \
1241    inline tname<type> fract(tname<type> const &a) \
1242    { \
1243        tname<type> ret; \
1244        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1245            ret[n] = fract(a[n]); \
1246        return ret; \
1247    } \
1248    \
1249    tprefix \
1250    inline tname<type> normalize(tname<type> const &a) \
1251    { \
1252        type norm = (type)length(a); \
1253        return norm ? a / norm : a * (type)0; \
1254    } \
1255    \
1256    tprefix \
1257    inline tname<type> abs(tname<type> const &a) \
1258    { \
1259        tname<type> ret; \
1260        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1261            ret[n] = lol::abs(a[n]); \
1262        return ret; \
1263    }
1264
1265#define LOL_BINARY_NONVECTOR_FUNS(tname, tprefix, type) \
1266    tprefix \
1267    inline type dot(tname<type> const &a, tname<type> const &b) \
1268    { \
1269        type ret = 0; \
1270        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1271            ret += a[n] * b[n]; \
1272        return ret; \
1273    } \
1274    \
1275    tprefix \
1276    inline tname<type> lerp(tname<type> const &a, \
1277                            tname<type> const &b, type x) \
1278    { \
1279        tname<type> ret; \
1280        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1281            ret[n] = a[n] + (b[n] - a[n]) * x; \
1282        return ret; \
1283    }
1284
1285#define LOL_VEC_3_FUNS(tname, tprefix, type) \
1286    tprefix \
1287    inline tname<type> cross(tname<type> const &a, tname<type> const &b) \
1288    { \
1289        return tname<type>((type)(a.y * b.z - a.z * b.y), \
1290                           (type)(a.z * b.x - a.x * b.z), \
1291                           (type)(a.x * b.y - a.y * b.x)); \
1292    }
1293
1294#define LOL_BINARY_NONVECTOR_OPS(tname, tprefix, type) \
1295    LOL_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
1296    LOL_VECTOR_VECTOR_OP(tname, +, tprefix, type) \
1297    LOL_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
1298    LOL_VECTOR_SCALAR_OP(tname, /, tprefix, type) \
1299    LOL_SCALAR_VECTOR_OP(tname, *, tprefix, type) \
1300    \
1301    LOL_VECTOR_VECTOR_NONCONST_OP(tname, -, tprefix, type) \
1302    LOL_VECTOR_VECTOR_NONCONST_OP(tname, +, tprefix, type) \
1303    LOL_VECTOR_SCALAR_NONCONST_OP(tname, *, tprefix, type) \
1304    LOL_VECTOR_SCALAR_NONCONST_OP(tname, /, tprefix, type) \
1305    \
1306    LOL_VECTOR_VECTOR_BOOL_OP(tname, ==, ==, true, tprefix, type) \
1307    LOL_VECTOR_VECTOR_BOOL_OP(tname, !=, ==, false, tprefix, type)
1308
1309#define LOL_BINARY_VECTOR_OPS(tname, tprefix, type) \
1310    LOL_SCALAR_VECTOR_OP(tname, /, tprefix, type)
1311
1312#define LOL_BINARY_VECTOR_FUNS(tname, tprefix, type) \
1313    LOL_VECTOR_MINMAX_FUN(tname, min, tprefix, type) \
1314    LOL_VECTOR_MINMAX_FUN(tname, max, tprefix, type) \
1315    LOL_VECTOR_MINMAX_FUN(tname, fmod, tprefix, type) \
1316    LOL_VECTOR_CLAMP_FUN(tname, tprefix, type) \
1317    LOL_VECTOR_MIX_FUN(tname, tprefix, type) \
1318    \
1319    LOL_VECTOR_VECTOR_BOOL_OP(tname, <=, <=, true, tprefix, type) \
1320    LOL_VECTOR_VECTOR_BOOL_OP(tname, >=, >=, true, tprefix, type) \
1321    LOL_VECTOR_VECTOR_BOOL_OP(tname, <, <, true, tprefix, type) \
1322    LOL_VECTOR_VECTOR_BOOL_OP(tname, >, >, true, tprefix, type)
1323
1324#define LOL_VECTOR_OPS(tname, tprefix, type) \
1325    LOL_VECTOR_VECTOR_OP(tname, *, tprefix, type) \
1326    LOL_VECTOR_VECTOR_OP(tname, /, tprefix, type) \
1327    \
1328    LOL_VECTOR_VECTOR_NONCONST_OP(tname, *, tprefix, type) \
1329    LOL_VECTOR_VECTOR_NONCONST_OP(tname, /, tprefix, type)
1330
1331#define LOL_ALL_NONVECTOR_OPS_AND_FUNS(tname) \
1332    LOL_BINARY_NONVECTOR_OPS(tname, template<typename T> static, T) \
1333    LOL_BINARY_NONVECTOR_FUNS(tname, template<typename T> static, T) \
1334    LOL_UNARY_OPS(tname, template<typename T> static, T) \
1335    LOL_UNARY_FUNS(tname, template<typename T> static, T)
1336
1337#define LOL_ALL_VECTOR_OPS_INNER(tname, type) \
1338    LOL_BINARY_VECTOR_OPS(tname, static, type) \
1339    LOL_BINARY_NONVECTOR_OPS(tname, static, type) \
1340    LOL_UNARY_OPS(tname, static, type) \
1341    LOL_VECTOR_OPS(tname, static, type)
1342
1343#define LOL_ALL_VECTOR_FUNS_INNER(tname, type) \
1344    LOL_BINARY_VECTOR_FUNS(tname, static, type) \
1345    LOL_BINARY_NONVECTOR_FUNS(tname, static, type) \
1346    LOL_UNARY_FUNS(tname, static, type)
1347
1348#define LOL_ALL_VECTOR_OPS_AND_FUNS(type) \
1349    namespace x##type \
1350    { \
1351        LOL_ALL_VECTOR_OPS_INNER(Vec2, type) \
1352        LOL_ALL_VECTOR_OPS_INNER(Vec3, type) \
1353        LOL_ALL_VECTOR_OPS_INNER(Vec4, type) \
1354    } \
1355    using namespace x##type; \
1356    LOL_ALL_VECTOR_FUNS_INNER(Vec2, type) \
1357    LOL_ALL_VECTOR_FUNS_INNER(Vec3, type) \
1358    LOL_ALL_VECTOR_FUNS_INNER(Vec4, type) \
1359    LOL_VEC_3_FUNS(Vec3, static, type)
1360
1361LOL_ALL_NONVECTOR_OPS_AND_FUNS(Cmplx)
1362LOL_ALL_NONVECTOR_OPS_AND_FUNS(Quat)
1363
1364/* Disable warning about unary operator applied to unsigned type */
1365#if defined _MSC_VER
1366#   pragma warning(push)
1367#   pragma warning(disable: 4146)
1368#endif
1369
1370LOL_ALL_VECTOR_OPS_AND_FUNS(half)
1371LOL_ALL_VECTOR_OPS_AND_FUNS(float)
1372LOL_ALL_VECTOR_OPS_AND_FUNS(double)
1373LOL_ALL_VECTOR_OPS_AND_FUNS(ldouble)
1374LOL_ALL_VECTOR_OPS_AND_FUNS(real)
1375
1376LOL_ALL_VECTOR_OPS_AND_FUNS(int8_t)
1377LOL_ALL_VECTOR_OPS_AND_FUNS(uint8_t)
1378LOL_ALL_VECTOR_OPS_AND_FUNS(int16_t)
1379LOL_ALL_VECTOR_OPS_AND_FUNS(uint16_t)
1380LOL_ALL_VECTOR_OPS_AND_FUNS(int32_t)
1381LOL_ALL_VECTOR_OPS_AND_FUNS(uint32_t)
1382LOL_ALL_VECTOR_OPS_AND_FUNS(int64_t)
1383LOL_ALL_VECTOR_OPS_AND_FUNS(uint64_t)
1384
1385#if defined _MSC_VER
1386#   pragma warning(pop)
1387#endif
1388
1389#undef LOL_MEMBER_OPS
1390
1391#undef LOL_VECTOR_VECTOR_OP
1392#undef LOL_VECTOR_VECTOR_NONCONST_OP
1393#undef LOL_VECTOR_MINMAX_FUN
1394#undef LOL_VECTOR_CLAMP_FUN
1395#undef LOL_VECTOR_MIX_FUN
1396#undef LOL_VECTOR_VECTOR_BOOL_OP
1397#undef LOL_VECTOR_SCALAR_OP
1398#undef LOL_SCALAR_VECTOR_OP
1399#undef LOL_VECTOR_SCALAR_OP
1400
1401#undef LOL_BINARY_VECTOR_OPS
1402#undef LOL_BINARY_VECTOR_FUNS
1403#undef LOL_BINARY_NONVECTOR_OPS
1404#undef LOL_BINARY_NONVECTOR_FUNS
1405#undef LOL_UNARY_OPS
1406#undef LOL_UNARY_FUNS
1407#undef LOL_VEC_3_FUNS
1408#undef LOL_VECTOR_OPS
1409
1410#undef LOL_ALL_NONVECTOR_OPS_AND_FUNS
1411#undef LOL_ALL_VECTOR_OPS_INNER
1412#undef LOL_ALL_VECTOR_FUNS_INNER
1413#undef LOL_ALL_VECTOR_OPS_AND_FUNS
1414
1415/*
1416 * Definition of additional functions requiring vector functions
1417 */
1418
1419template<typename T>
1420inline Quat<T> Quat<T>::operator *(Quat<T> const &val) const
1421{
1422    Quat<T> ret;
1423    Vec3<T> v1(x, y, z);
1424    Vec3<T> v2(val.x, val.y, val.z);
1425    Vec3<T> v3 = cross(v1, v2) + w * v2 + val.w * v1;
1426    return Quat<T>(w * val.w - dot(v1, v2), v3.x, v3.y, v3.z);
1427}
1428
1429/*
1430 * Magic vector swizzling (part 2/2)
1431 * Unfortunately these assignment operators cannot be used for now, because
1432 * we would also need to override the default copy assignment operator, and
1433 * in C++98 unions cannot contain such objects. This is why all the swizzling
1434 * magic objects are marked 'const' even those that could be lvalues.
1435 */
1436
1437template<typename T, int N>
1438inline Vec2<T> XVec2<T, N>::operator =(Vec2<T> const &that)
1439{
1440    for (int i = 0; i < 2; i++)
1441        *this[i] = that[i];
1442    return *this;
1443}
1444
1445template<typename T, int N>
1446inline Vec3<T> XVec3<T, N>::operator =(Vec3<T> const &that)
1447{
1448    for (int i = 0; i < 3; i++)
1449        *this[i] = that[i];
1450    return *this;
1451}
1452
1453template<typename T, int N>
1454inline Vec4<T> XVec4<T, N>::operator =(Vec4<T> const &that)
1455{
1456    for (int i = 0; i < 4; i++)
1457        *this[i] = that[i];
1458    return *this;
1459}
1460
1461/*
1462 * 2×2-element matrices
1463 */
1464
1465template <typename T> struct Mat2
1466{
1467    inline Mat2() {}
1468    inline Mat2(Vec2<T> V0, Vec2<T> V1)
1469      : v0(V0), v1(V1) {}
1470
1471    explicit inline Mat2(T val)
1472      : v0(val, (T)0),
1473        v1((T)0, val) {}
1474
1475    explicit inline Mat2(Mat4<T> const &mat)
1476      : v0(mat[0].xy),
1477        v1(mat[1].xy) {}
1478
1479    inline Vec2<T>& operator[](size_t n) { return (&v0)[n]; }
1480    inline Vec2<T> const& operator[](size_t n) const { return (&v0)[n]; }
1481
1482    /* Helpers for transformation matrices */
1483    //Angle in degree
1484    static Mat2<T> rotate(T angle);
1485    static inline Mat2<T> rotate(Mat2<T> mat, T angle)
1486    {
1487        return rotate(angle) * mat;
1488    }
1489
1490    void printf() const;
1491
1492    template<class U>
1493    friend std::ostream &operator<<(std::ostream &stream, Mat2<U> const &m);
1494
1495    inline Mat2<T> operator +(Mat2<T> const m) const
1496    {
1497        return Mat2<T>(v0 + m[0], v1 + m[1]);
1498    }
1499
1500    inline Mat2<T> operator +=(Mat2<T> const m)
1501    {
1502        return *this = *this + m;
1503    }
1504
1505    inline Mat2<T> operator -(Mat2<T> const m) const
1506    {
1507        return Mat2<T>(v0 - m[0], v1 - m[1]);
1508    }
1509
1510    inline Mat2<T> operator -=(Mat2<T> const m)
1511    {
1512        return *this = *this - m;
1513    }
1514
1515    inline Mat2<T> operator *(Mat2<T> const m) const
1516    {
1517        return Mat2<T>(*this * m[0], *this * m[1]);
1518    }
1519
1520    inline Mat2<T> operator *=(Mat2<T> const m)
1521    {
1522        return *this = *this * m;
1523    }
1524
1525    inline Vec2<T> operator *(Vec2<T> const m) const
1526    {
1527        Vec2<T> ret;
1528        for (int j = 0; j < 2; j++)
1529        {
1530            T tmp = 0;
1531            for (int k = 0; k < 2; k++)
1532                tmp += (*this)[k][j] * m[k];
1533            ret[j] = tmp;
1534        }
1535        return ret;
1536    }
1537
1538    Vec2<T> v0, v1;
1539};
1540
1541/*
1542 * 3×3-element matrices
1543 */
1544
1545template <typename T> struct Mat3
1546{
1547    inline Mat3() {}
1548    inline Mat3(Vec3<T> V0, Vec3<T> V1, Vec3<T> V2)
1549      : v0(V0), v1(V1), v2(V2) {}
1550
1551    explicit inline Mat3(T val)
1552      : v0(val, (T)0, (T)0),
1553        v1((T)0, val, (T)0),
1554        v2((T)0, (T)0, val) {}
1555
1556    explicit inline Mat3(Mat2<T> mat)
1557      : v0(mat[0], (T)0),
1558        v1(mat[1], (T)0),
1559        v2((T)0, (T)0, (T)0) {}
1560
1561    explicit inline Mat3(Mat2<T> mat, T val)
1562      : v0(Vec3<T>(mat[0], (T)0)),
1563        v1(Vec3<T>(mat[1], (T)0)),
1564        v2((T)0, (T)0, val) {}
1565
1566    explicit inline Mat3(Mat4<T> const &mat)
1567      : v0(mat[0].xyz),
1568        v1(mat[1].xyz),
1569        v2(mat[2].xyz) {}
1570
1571    explicit Mat3(Quat<T> const &q);
1572
1573    inline Vec3<T>& operator[](size_t n) { return (&v0)[n]; }
1574    inline Vec3<T> const& operator[](size_t n) const { return (&v0)[n]; }
1575
1576    /* Helpers for transformation matrices */
1577    static Mat3<T> scale(T x);
1578    static Mat3<T> scale(T x, T y, T z);
1579    static Mat3<T> scale(Vec3<T> v);
1580    //Angle in degree
1581    static Mat3<T> rotate(T angle, T x, T y, T z);
1582    static Mat3<T> rotate(T angle, Vec3<T> v);
1583
1584    static Mat3<T> fromeuler_xyz(Vec3<T> const &v);
1585    static Mat3<T> fromeuler_xzy(Vec3<T> const &v);
1586    static Mat3<T> fromeuler_yxz(Vec3<T> const &v);
1587    static Mat3<T> fromeuler_yzx(Vec3<T> const &v);
1588    static Mat3<T> fromeuler_zxy(Vec3<T> const &v);
1589    static Mat3<T> fromeuler_zyx(Vec3<T> const &v);
1590    static Mat3<T> fromeuler_xyz(T phi, T theta, T psi);
1591    static Mat3<T> fromeuler_xzy(T phi, T theta, T psi);
1592    static Mat3<T> fromeuler_yxz(T phi, T theta, T psi);
1593    static Mat3<T> fromeuler_yzx(T phi, T theta, T psi);
1594    static Mat3<T> fromeuler_zxy(T phi, T theta, T psi);
1595    static Mat3<T> fromeuler_zyx(T phi, T theta, T psi);
1596
1597    static Mat3<T> fromeuler_xyx(Vec3<T> const &v);
1598    static Mat3<T> fromeuler_xzx(Vec3<T> const &v);
1599    static Mat3<T> fromeuler_yxy(Vec3<T> const &v);
1600    static Mat3<T> fromeuler_yzy(Vec3<T> const &v);
1601    static Mat3<T> fromeuler_zxz(Vec3<T> const &v);
1602    static Mat3<T> fromeuler_zyz(Vec3<T> const &v);
1603    static Mat3<T> fromeuler_xyx(T phi, T theta, T psi);
1604    static Mat3<T> fromeuler_xzx(T phi, T theta, T psi);
1605    static Mat3<T> fromeuler_yxy(T phi, T theta, T psi);
1606    static Mat3<T> fromeuler_yzy(T phi, T theta, T psi);
1607    static Mat3<T> fromeuler_zxz(T phi, T theta, T psi);
1608    static Mat3<T> fromeuler_zyz(T phi, T theta, T psi);
1609
1610    //Angle in degree
1611    static inline Mat3<T> rotate(Mat3<T> mat, T angle, Vec3<T> v)
1612    {
1613        return rotate(angle, v) * mat;
1614    }
1615
1616    void printf() const;
1617
1618    template<class U>
1619    friend std::ostream &operator<<(std::ostream &stream, Mat3<U> const &m);
1620
1621    inline Mat3<T> operator +(Mat3<T> const m) const
1622    {
1623        return Mat3<T>(v0 + m[0], v1 + m[1], v2 + m[2]);
1624    }
1625
1626    inline Mat3<T> operator +=(Mat3<T> const m)
1627    {
1628        return *this = *this + m;
1629    }
1630
1631    inline Mat3<T> operator -(Mat3<T> const m) const
1632    {
1633        return Mat3<T>(v0 - m[0], v1 - m[1], v2 - m[2]);
1634    }
1635
1636    inline Mat3<T> operator -=(Mat3<T> const m)
1637    {
1638        return *this = *this - m;
1639    }
1640
1641    inline Mat3<T> operator *(Mat3<T> const m) const
1642    {
1643        return Mat3<T>(*this * m[0], *this * m[1], *this * m[2]);
1644    }
1645
1646    inline Mat3<T> operator *=(Mat3<T> const m)
1647    {
1648        return *this = *this * m;
1649    }
1650
1651    inline Vec3<T> operator *(Vec3<T> const m) const
1652    {
1653        Vec3<T> ret;
1654        for (int j = 0; j < 3; j++)
1655        {
1656            T tmp = 0;
1657            for (int k = 0; k < 3; k++)
1658                tmp += (*this)[k][j] * m[k];
1659            ret[j] = tmp;
1660        }
1661        return ret;
1662    }
1663
1664    Vec3<T> v0, v1, v2;
1665};
1666
1667/*
1668 * 4×4-element matrices
1669 */
1670
1671template <typename T> struct Mat4
1672{
1673    inline Mat4() {}
1674    inline Mat4(Vec4<T> V0, Vec4<T> V1, Vec4<T> V2, Vec4<T> V3)
1675      : v0(V0), v1(V1), v2(V2), v3(V3) {}
1676
1677    explicit inline Mat4(T val)
1678      : v0(val, (T)0, (T)0, (T)0),
1679        v1((T)0, val, (T)0, (T)0),
1680        v2((T)0, (T)0, val, (T)0),
1681        v3((T)0, (T)0, (T)0, val) {}
1682
1683    explicit inline Mat4(Mat2<T> mat)
1684      : v0(mat[0], (T)0, (T)0),
1685        v1(mat[1], (T)0, (T)0),
1686        v2((T)0, (T)0, (T)0, (T)0),
1687        v3((T)0, (T)0, (T)0, (T)0) {}
1688
1689    explicit inline Mat4(Mat2<T> mat, T val1, T val2)
1690      : v0(mat[0], (T)0, (T)0),
1691        v1(mat[1], (T)0, (T)0),
1692        v2((T)0, (T)0, val1, (T)0),
1693        v3((T)0, (T)0, (T)0, val2) {}
1694
1695    explicit inline Mat4(Mat3<T> mat)
1696      : v0(mat[0], (T)0),
1697        v1(mat[1], (T)0),
1698        v2(mat[2], (T)0),
1699        v3((T)0, (T)0, (T)0, (T)0) {}
1700
1701    explicit inline Mat4(Mat3<T> mat, T val)
1702      : v0(mat[0], (T)0),
1703        v1(mat[1], (T)0),
1704        v2(mat[2], (T)0),
1705        v3((T)0, (T)0, (T)0, val) {}
1706
1707    explicit Mat4(Quat<T> const &q);
1708
1709    inline Vec4<T>& operator[](size_t n) { return (&v0)[n]; }
1710    inline Vec4<T> const& operator[](size_t n) const { return (&v0)[n]; }
1711
1712    /* Helpers for transformation matrices */
1713    static Mat4<T> translate(T x, T y, T z);
1714    static Mat4<T> translate(Vec3<T> v);
1715
1716    static inline Mat4<T> scale(T x)
1717    {
1718        return Mat4<T>(Mat3<T>::scale(x), (T)1);
1719    }
1720
1721    static inline Mat4<T> scale(T x, T y, T z)
1722    {
1723        return Mat4<T>(Mat3<T>::scale(x, y, z), (T)1);
1724    }
1725
1726    static inline Mat4<T> scale(Vec3<T> v)
1727    {
1728        return Mat4<T>(Mat3<T>::scale(v), (T)1);
1729    }
1730
1731    static inline Mat4<T> translate(Mat4<T> const &mat, Vec3<T> v)
1732    {
1733        return translate(v) * mat;
1734    }
1735
1736    //Angle in degree
1737    static inline Mat4<T> rotate(T angle, T x, T y, T z)
1738    {
1739        return Mat4<T>(Mat3<T>::rotate(angle, x, y, z), (T)1);
1740    }
1741
1742    //Angle in degree
1743    static inline Mat4<T> rotate(T angle, Vec3<T> v)
1744    {
1745        return Mat4<T>(Mat3<T>::rotate(angle, v), (T)1);
1746    }
1747
1748    //Angle in degree
1749    static inline Mat4<T> rotate(Mat4<T> &mat, T angle, Vec3<T> v)
1750    {
1751        return rotate(angle, v) * mat;
1752    }
1753
1754    static Mat4<T> fromeuler_xyz(Vec3<T> const &v);
1755    static Mat4<T> fromeuler_xzy(Vec3<T> const &v);
1756    static Mat4<T> fromeuler_yxz(Vec3<T> const &v);
1757    static Mat4<T> fromeuler_yzx(Vec3<T> const &v);
1758    static Mat4<T> fromeuler_zxy(Vec3<T> const &v);
1759    static Mat4<T> fromeuler_zyx(Vec3<T> const &v);
1760    static Mat4<T> fromeuler_xyz(T phi, T theta, T psi);
1761    static Mat4<T> fromeuler_xzy(T phi, T theta, T psi);
1762    static Mat4<T> fromeuler_yxz(T phi, T theta, T psi);
1763    static Mat4<T> fromeuler_yzx(T phi, T theta, T psi);
1764    static Mat4<T> fromeuler_zxy(T phi, T theta, T psi);
1765    static Mat4<T> fromeuler_zyx(T phi, T theta, T psi);
1766
1767    static Mat4<T> fromeuler_xyx(Vec3<T> const &v);
1768    static Mat4<T> fromeuler_xzx(Vec3<T> const &v);
1769    static Mat4<T> fromeuler_yxy(Vec3<T> const &v);
1770    static Mat4<T> fromeuler_yzy(Vec3<T> const &v);
1771    static Mat4<T> fromeuler_zxz(Vec3<T> const &v);
1772    static Mat4<T> fromeuler_zyz(Vec3<T> const &v);
1773    static Mat4<T> fromeuler_xyx(T phi, T theta, T psi);
1774    static Mat4<T> fromeuler_xzx(T phi, T theta, T psi);
1775    static Mat4<T> fromeuler_yxy(T phi, T theta, T psi);
1776    static Mat4<T> fromeuler_yzy(T phi, T theta, T psi);
1777    static Mat4<T> fromeuler_zxz(T phi, T theta, T psi);
1778    static Mat4<T> fromeuler_zyz(T phi, T theta, T psi);
1779
1780    /* Helpers for view matrices */
1781    static Mat4<T> lookat(Vec3<T> eye, Vec3<T> center, Vec3<T> up);
1782
1783    /* Helpers for projection matrices */
1784    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
1785    static Mat4<T> ortho(T width, T height, T near, T far);
1786    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
1787    static Mat4<T> perspective(T fov_y, T width, T height, T near, T far);
1788
1789    void printf() const;
1790
1791    template<class U>
1792    friend std::ostream &operator<<(std::ostream &stream, Mat4<U> const &m);
1793
1794    inline Mat4<T> operator +(Mat4<T> const &m) const
1795    {
1796        return Mat4<T>(v0 + m[0], v1 + m[1], v2 + m[2], v3 + m[3]);
1797    }
1798
1799    inline Mat4<T> operator +=(Mat4<T> const &m)
1800    {
1801        return *this = *this + m;
1802    }
1803
1804    inline Mat4<T> operator -(Mat4<T> const &m) const
1805    {
1806        return Mat4<T>(v0 - m[0], v1 - m[1], v2 - m[2], v3 - m[3]);
1807    }
1808
1809    inline Mat4<T> operator -=(Mat4<T> const &m)
1810    {
1811        return *this = *this - m;
1812    }
1813
1814    inline Mat4<T> operator *(Mat4<T> const &m) const
1815    {
1816        return Mat4<T>(*this * m[0], *this * m[1], *this * m[2], *this * m[3]);
1817    }
1818
1819    inline Mat4<T> operator *=(Mat4<T> const &m)
1820    {
1821        return *this = *this * m;
1822    }
1823
1824    inline Vec4<T> operator *(Vec4<T> const &m) const
1825    {
1826        Vec4<T> ret;
1827        for (int j = 0; j < 4; j++)
1828        {
1829            T tmp = 0;
1830            for (int k = 0; k < 4; k++)
1831                tmp += (*this)[k][j] * m[k];
1832            ret[j] = tmp;
1833        }
1834        return ret;
1835    }
1836
1837    Vec4<T> v0, v1, v2, v3;
1838};
1839
1840template<typename T> T determinant(Mat2<T> const &);
1841template<typename T> T determinant(Mat3<T> const &);
1842template<typename T> T determinant(Mat4<T> const &);
1843
1844template<typename T> Mat2<T> transpose(Mat2<T> const &);
1845template<typename T> Mat3<T> transpose(Mat3<T> const &);
1846template<typename T> Mat4<T> transpose(Mat4<T> const &);
1847
1848template<typename T> Mat2<T> inverse(Mat2<T> const &);
1849template<typename T> Mat3<T> inverse(Mat3<T> const &);
1850template<typename T> Mat4<T> inverse(Mat4<T> const &);
1851
1852/*
1853 * Arbitrarily-sized square matrices; for now this only supports
1854 * naive inversion and is used for the Remez inversion method.
1855 */
1856
1857template<int N, typename T> struct Mat
1858{
1859    inline Mat<N, T>() {}
1860
1861    Mat(T x)
1862    {
1863        for (int j = 0; j < N; j++)
1864            for (int i = 0; i < N; i++)
1865                if (i == j)
1866                    m[i][j] = x;
1867                else
1868                    m[i][j] = 0;
1869    }
1870
1871    /* Naive matrix inversion */
1872    Mat<N, T> inv() const
1873    {
1874        Mat a = *this, b((T)1);
1875
1876        /* Inversion method: iterate through all columns and make sure
1877         * all the terms are 1 on the diagonal and 0 everywhere else */
1878        for (int i = 0; i < N; i++)
1879        {
1880            /* If the expected coefficient is zero, add one of
1881             * the other lines. The first we meet will do. */
1882            if (!a.m[i][i])
1883            {
1884                for (int j = i + 1; j < N; j++)
1885                {
1886                    if (!a.m[i][j])
1887                        continue;
1888                    /* Add row j to row i */
1889                    for (int n = 0; n < N; n++)
1890                    {
1891                        a.m[n][i] += a.m[n][j];
1892                        b.m[n][i] += b.m[n][j];
1893                    }
1894                    break;
1895                }
1896            }
1897
1898            /* Now we know the diagonal term is non-zero. Get its inverse
1899             * and use that to nullify all other terms in the column */
1900            T x = (T)1 / a.m[i][i];
1901            for (int j = 0; j < N; j++)
1902            {
1903                if (j == i)
1904                    continue;
1905                T mul = x * a.m[i][j];
1906                for (int n = 0; n < N; n++)
1907                {
1908                    a.m[n][j] -= mul * a.m[n][i];
1909                    b.m[n][j] -= mul * b.m[n][i];
1910                }
1911            }
1912
1913            /* Finally, ensure the diagonal term is 1 */
1914            for (int n = 0; n < N; n++)
1915            {
1916                a.m[n][i] *= x;
1917                b.m[n][i] *= x;
1918            }
1919        }
1920
1921        return b;
1922    }
1923
1924    T m[N][N];
1925};
1926
1927} /* namespace lol */
1928
1929#endif // __LOL_MATH_VECTOR_H__
1930
Note: See TracBrowser for help on using the repository browser.