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

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 70.0 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://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 * bool ==(vec, vec)   (also complex & quaternion)
1139 * bool !=(vec, vec)   (also complex & quaternion)
1140 * bool >=(vec, vec)
1141 * bool <=(vec, vec)
1142 * bool >(vec, vec)
1143 * bool <(vec, vec)
1144 */
1145#define LOL_VECTOR_VECTOR_BOOL_OP(tname, op, op2, ret, tprefix, t1, t2) \
1146    tprefix \
1147    inline bool operator op(tname<t1> const &a, tname<t2> const &b) \
1148    { \
1149        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1150            if (!(a[n] op2 b[n])) \
1151                return !ret; \
1152        return ret; \
1153    }
1154
1155/*
1156 * vec *(vec, scalar)   (also complex & quaternion)
1157 * vec /(vec, scalar)   (also complex & quaternion)
1158 */
1159#define LOL_VECTOR_SCALAR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
1160    tprefix \
1161    inline tname<tf> operator op(tname<t1> const &a, t2 const &val) \
1162    { \
1163        tname<tf> ret; \
1164        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1165            ret[n] = a[n] op val; \
1166        return ret; \
1167    }
1168
1169/*
1170 * vec *(scalar, vec)   (also complex & quaternion)
1171 * vec /(scalar, vec)   (NOT for complex & quaternion!)
1172 */
1173#define LOL_SCALAR_VECTOR_COERCE_OP(tname, op, tprefix, t1, t2, tf) \
1174    tprefix \
1175    inline tname<tf> operator op(t1 const &val, tname<t2> const &a) \
1176    { \
1177        tname<tf> ret; \
1178        for (size_t n = 0; n < sizeof(a) / sizeof(t2); n++) \
1179            ret[n] = a[n] op val; \
1180        return ret; \
1181    }
1182
1183/*
1184 * vec *=(vec, scalar)   (also complex & quaternion)
1185 * vec /=(vec, scalar)   (also complex & quaternion)
1186 */
1187#define LOL_VECTOR_SCALAR_OP(tname, op, tprefix, type) \
1188    tprefix \
1189    inline tname<type> operator op##=(tname<type> &a, type const &val) \
1190    { \
1191        return a = a op val; \
1192    }
1193
1194#define LOL_UNARY_OPS(tname, tprefix, type) \
1195    tprefix \
1196    inline tname<type> operator -(tname<type> const &a) \
1197    { \
1198        tname<type> ret; \
1199        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1200            ret[n] = -a[n]; \
1201        return ret; \
1202    }
1203
1204#define LOL_UNARY_FUNS(tname, tprefix, type) \
1205    tprefix \
1206    inline type sqlength(tname<type> const &a) \
1207    { \
1208        type acc = 0; \
1209        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1210            acc += a[n] * a[n]; \
1211        return acc; \
1212    } \
1213    \
1214    tprefix \
1215    inline type length(tname<type> const &a) \
1216    { \
1217        return (type)sqrt((double)sqlength(a)); \
1218    } \
1219    \
1220    tprefix \
1221    inline tname<type> normalize(tname<type> const &a) \
1222    { \
1223        type norm = (type)length(a); \
1224        return norm ? a / norm : a * (type)0; \
1225    } \
1226    \
1227    tprefix \
1228    inline tname<type> abs(tname<type> const &a) \
1229    { \
1230        tname<type> ret; \
1231        for (size_t n = 0; n < sizeof(a) / sizeof(type); n++) \
1232            ret[n] = lol::abs(a[n]); \
1233        return ret; \
1234    }
1235
1236#define LOL_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1237    LOL_VECTOR_VECTOR_COERCE_OP(tname, -, tprefix, t1, t2, tf) \
1238    LOL_VECTOR_VECTOR_COERCE_OP(tname, +, tprefix, t1, t2, tf) \
1239    \
1240    LOL_VECTOR_SCALAR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
1241    LOL_VECTOR_SCALAR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
1242    LOL_SCALAR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
1243    \
1244    LOL_VECTOR_VECTOR_BOOL_OP(tname, ==, ==, true, tprefix, t1, t2) \
1245    LOL_VECTOR_VECTOR_BOOL_OP(tname, !=, ==, false, tprefix, t1, t2)
1246
1247#define LOL_BINARY_NONVECTOR_COERCE_FUNS(tname, tprefix, t1, t2, tf) \
1248    tprefix \
1249    inline tf dot(tname<t1> const &a, tname<t2> const &b) \
1250    { \
1251        tf ret = 0; \
1252        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1253            ret += a[n] * b[n]; \
1254        return ret; \
1255    } \
1256    tprefix \
1257    inline tname<tf> lerp(tname<t1> const &a, tname<t2> const &b, tf x) \
1258    { \
1259        tname<tf> ret; \
1260        for (size_t n = 0; n < sizeof(a) / sizeof(t1); n++) \
1261            ret[n] = a[n] + (a[n] - b[n]) * x; \
1262        return ret; \
1263    }
1264
1265#define LOL_BINARY_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1266    LOL_SCALAR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf)
1267
1268#define LOL_VEC_3_COERCE_FUNS(tname, tprefix, t1, t2, tf) \
1269    tprefix \
1270    inline tname<tf> cross(tname<t1> const &a, tname<t2> const &b) \
1271    { \
1272        return tname<tf>((tf)(a.y * b.z) - (tf)(a.z * b.y), \
1273                         (tf)(a.z * b.x) - (tf)(a.x * b.z), \
1274                         (tf)(a.x * b.y) - (tf)(a.y * b.x)); \
1275    }
1276
1277#define LOL_BINARY_NONVECTOR_OPS(tname, tprefix, type) \
1278    LOL_BINARY_NONVECTOR_COERCE_OPS(tname, tprefix, type, type, type) \
1279    \
1280    LOL_VECTOR_VECTOR_OP(tname, -, tprefix, type) \
1281    LOL_VECTOR_VECTOR_OP(tname, +, tprefix, type) \
1282    \
1283    LOL_VECTOR_SCALAR_OP(tname, *, tprefix, type) \
1284    LOL_VECTOR_SCALAR_OP(tname, /, tprefix, type)
1285
1286#define LOL_BINARY_NONVECTOR_FUNS(tname, tprefix, type) \
1287    LOL_BINARY_NONVECTOR_COERCE_FUNS(tname, tprefix, type, type, type) \
1288
1289#define LOL_BINARY_VECTOR_OPS(tname, tprefix, type) \
1290    LOL_BINARY_VECTOR_COERCE_OPS(tname, tprefix, type, type, type)
1291
1292#define LOL_BINARY_VECTOR_FUNS(tname, tprefix, type) \
1293    LOL_VECTOR_MINMAX_FUN(tname, min, tprefix, type) \
1294    LOL_VECTOR_MINMAX_FUN(tname, max, tprefix, type) \
1295    LOL_VECTOR_MINMAX_FUN(tname, fmod, tprefix, type) \
1296    LOL_VECTOR_CLAMP_FUN(tname, tprefix, type)
1297
1298#define LOL_VECTOR_COERCE_OPS(tname, tprefix, t1, t2, tf) \
1299    LOL_VECTOR_VECTOR_COERCE_OP(tname, *, tprefix, t1, t2, tf) \
1300    LOL_VECTOR_VECTOR_COERCE_OP(tname, /, tprefix, t1, t2, tf) \
1301    \
1302    LOL_VECTOR_VECTOR_BOOL_OP(tname, <=, <=, true, tprefix, t1, t2) \
1303    LOL_VECTOR_VECTOR_BOOL_OP(tname, >=, >=, true, tprefix, t1, t2) \
1304    LOL_VECTOR_VECTOR_BOOL_OP(tname, <, <, true, tprefix, t1, t2) \
1305    LOL_VECTOR_VECTOR_BOOL_OP(tname, >, >, true, tprefix, t1, t2)
1306
1307#define LOL_VECTOR_OPS(tname, tprefix, type) \
1308    LOL_VECTOR_COERCE_OPS(tname, static, type, type, type) \
1309    \
1310    LOL_VECTOR_VECTOR_OP(tname, *, tprefix, type) \
1311    LOL_VECTOR_VECTOR_OP(tname, /, tprefix, type)
1312
1313#define LOL_ALL_NONVECTOR_OPS_AND_FUNS(tname) \
1314    LOL_BINARY_NONVECTOR_OPS(tname, template<typename T> static, T) \
1315    LOL_BINARY_NONVECTOR_FUNS(tname, template<typename T> static, T) \
1316    LOL_UNARY_OPS(tname, template<typename T> static, T) \
1317    LOL_UNARY_FUNS(tname, template<typename T> static, T)
1318
1319#define LOL_ALL_VECTOR_OPS_INNER(tname, type) \
1320    LOL_BINARY_VECTOR_OPS(tname, static, type) \
1321    LOL_BINARY_NONVECTOR_OPS(tname, static, type) \
1322    LOL_UNARY_OPS(tname, static, type) \
1323    LOL_VECTOR_OPS(tname, static, type)
1324
1325#define LOL_ALL_VECTOR_FUNS_INNER(tname, type) \
1326    LOL_BINARY_VECTOR_FUNS(tname, static, type) \
1327    LOL_BINARY_NONVECTOR_FUNS(tname, static, type) \
1328    LOL_UNARY_FUNS(tname, static, type)
1329
1330#define LOL_ALL_VECTOR_OPS_AND_FUNS(type) \
1331    namespace x##type \
1332    { \
1333        LOL_ALL_VECTOR_OPS_INNER(Vec2, type) \
1334        LOL_ALL_VECTOR_OPS_INNER(Vec3, type) \
1335        LOL_ALL_VECTOR_OPS_INNER(Vec4, type) \
1336    } \
1337    using namespace x##type; \
1338    LOL_ALL_VECTOR_FUNS_INNER(Vec2, type) \
1339    LOL_ALL_VECTOR_FUNS_INNER(Vec3, type) \
1340    LOL_ALL_VECTOR_FUNS_INNER(Vec4, type) \
1341    LOL_VEC_3_COERCE_FUNS(Vec3, static, type, type, type)
1342
1343#define LOL_VEC_ANY_COERCE_OPS(tname, tlow, thigh) \
1344    LOL_BINARY_NONVECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
1345    LOL_BINARY_NONVECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \
1346    LOL_BINARY_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
1347    LOL_BINARY_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh) \
1348    \
1349    LOL_VECTOR_COERCE_OPS(tname, static, tlow, thigh, thigh) \
1350    LOL_VECTOR_COERCE_OPS(tname, static, thigh, tlow, thigh)
1351
1352#define LOL_VEC_ANY_COERCE_FUNS(tname, tlow, thigh) \
1353    LOL_BINARY_NONVECTOR_COERCE_FUNS(tname, static, tlow, thigh, thigh) \
1354    LOL_BINARY_NONVECTOR_COERCE_FUNS(tname, static, thigh, tlow, thigh)
1355
1356#define LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(tlow, thigh) \
1357    namespace x##tlow##thigh \
1358    { \
1359        LOL_VEC_ANY_COERCE_OPS(Vec2, tlow, thigh) \
1360        LOL_VEC_ANY_COERCE_OPS(Vec3, tlow, thigh) \
1361        LOL_VEC_ANY_COERCE_OPS(Vec4, tlow, thigh) \
1362    } \
1363    LOL_VEC_ANY_COERCE_FUNS(Vec2, tlow, thigh) \
1364    LOL_VEC_ANY_COERCE_FUNS(Vec3, tlow, thigh) \
1365    LOL_VEC_ANY_COERCE_FUNS(Vec4, tlow, thigh) \
1366    LOL_VEC_3_COERCE_FUNS(Vec3, static, tlow, thigh, thigh) \
1367    LOL_VEC_3_COERCE_FUNS(Vec3, static, thigh, tlow, thigh)
1368
1369LOL_ALL_NONVECTOR_OPS_AND_FUNS(Cmplx)
1370LOL_ALL_NONVECTOR_OPS_AND_FUNS(Quat)
1371
1372/* Disable warning about unary operator applied to unsigned type */
1373#if defined _MSC_VER
1374#   pragma warning(push)
1375#   pragma warning(disable: 4146)
1376#endif
1377
1378LOL_ALL_VECTOR_OPS_AND_FUNS(half)
1379LOL_ALL_VECTOR_OPS_AND_FUNS(float)
1380LOL_ALL_VECTOR_OPS_AND_FUNS(double)
1381LOL_ALL_VECTOR_OPS_AND_FUNS(ldouble)
1382LOL_ALL_VECTOR_OPS_AND_FUNS(int8_t)
1383LOL_ALL_VECTOR_OPS_AND_FUNS(uint8_t)
1384LOL_ALL_VECTOR_OPS_AND_FUNS(int16_t)
1385LOL_ALL_VECTOR_OPS_AND_FUNS(uint16_t)
1386LOL_ALL_VECTOR_OPS_AND_FUNS(int32_t)
1387LOL_ALL_VECTOR_OPS_AND_FUNS(uint32_t)
1388LOL_ALL_VECTOR_OPS_AND_FUNS(int64_t)
1389LOL_ALL_VECTOR_OPS_AND_FUNS(uint64_t)
1390
1391#if defined _MSC_VER
1392#   pragma warning(pop)
1393#endif
1394
1395/* Disable warnings in the >= > etc. operators about comparing signed and
1396 * unsigned. Ideally we would like to get these warnings only when the
1397 * inlined operators are actually used, but they seem to be triggered at
1398 * the code parsing step, so the compiler does not yet know whether they
1399 * will be used.
1400 * Also we do this for the whole block of declarations, because GCC prior
1401 * to 4.6.3 does not appear to support _Pragma() inside a macro. */
1402#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1403#   pragma GCC diagnostic push
1404#   pragma GCC diagnostic ignored "-Wsign-compare"
1405#elif defined _MSC_VER
1406#   pragma warning(push)
1407#   pragma warning(disable: 4018)
1408#endif
1409
1410/* Apply the same coercion rules as in the C++ standard. However, instead
1411 * of always promoting smaller types to int, we allow int8_t op int16_t to
1412 * return an int16_t. */
1413LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, uint8_t)
1414LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, int16_t)
1415LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, uint16_t)
1416LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, int32_t)
1417LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, uint32_t)
1418LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, int64_t)
1419LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, uint64_t)
1420LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, float)
1421LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, double)
1422LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, ldouble)
1423
1424LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, int16_t)
1425LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, uint16_t)
1426LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, int32_t)
1427LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, uint32_t)
1428LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, int64_t)
1429LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, uint64_t)
1430LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, float)
1431LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, double)
1432LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, ldouble)
1433
1434LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, uint16_t)
1435LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, int32_t)
1436LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, uint32_t)
1437LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, int64_t)
1438LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, uint64_t)
1439LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, float)
1440LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, double)
1441LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, ldouble)
1442
1443LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, int32_t)
1444LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, uint32_t)
1445LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, int64_t)
1446LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, uint64_t)
1447LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, float)
1448LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, double)
1449LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, ldouble)
1450
1451LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, uint32_t)
1452LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, int64_t)
1453LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, uint64_t)
1454LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, float)
1455LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, double)
1456LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, ldouble)
1457
1458LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, int64_t)
1459LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, uint64_t)
1460LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, float)
1461LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, double)
1462LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, ldouble)
1463
1464LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, uint64_t)
1465LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, float)
1466LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, double)
1467LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, ldouble)
1468
1469LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint64_t, float)
1470LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint64_t, double)
1471LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint64_t, ldouble)
1472
1473LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(float, double)
1474LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(float, ldouble)
1475
1476LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(double, ldouble)
1477
1478/* FIXME: vectors of "half" are deactivated for now, because they
1479 * induce extremely long compilation times (about 17 seconds per TU). */
1480
1481#if 0
1482/* All integer types are promoted to half; all floating point types
1483 * cause half to be promoted. */
1484LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, half)
1485LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, half)
1486LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, half)
1487LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, half)
1488LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, half)
1489LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, half)
1490LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, half)
1491LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint64_t, half)
1492
1493LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(half, float)
1494LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(half, double)
1495LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(half, ldouble)
1496#endif
1497
1498/* FIXME: vectors of "real" are deactivated for now, because we do
1499 * not implement all combinations of operators for these types yet. */
1500
1501#if 0
1502/* All types are promoted to real */
1503LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int8_t, real)
1504LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint8_t, real)
1505LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int16_t, real)
1506LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint16_t, real)
1507LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int32_t, real)
1508LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint32_t, real)
1509LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(int64_t, real)
1510LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(uint64_t, real)
1511LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(half, real)
1512LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(float, real)
1513LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(double, real)
1514LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS(ldouble, real)
1515#endif
1516
1517/* Activate all the namespaces that we created. Delaying this activation
1518 * reduces compilation times significantly. */
1519#define ACTIVATE_COERCE_NAMESPACES_INNER(tlow, thigh) \
1520    namespace x##tlow##thigh {} \
1521    using namespace x##tlow##thigh;
1522
1523#define ACTIVATE_COERCE_NAMESPACES(tlow) \
1524    namespace x##tlow {} \
1525    using namespace x##tlow; \
1526    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, int8_t) \
1527    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, uint8_t) \
1528    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, int16_t) \
1529    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, uint16_t) \
1530    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, int32_t) \
1531    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, uint32_t) \
1532    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, int64_t) \
1533    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, uint64_t) \
1534    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, half) \
1535    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, float) \
1536    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, double) \
1537    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, ldouble) \
1538    ACTIVATE_COERCE_NAMESPACES_INNER(tlow, real)
1539
1540ACTIVATE_COERCE_NAMESPACES(int8_t)
1541ACTIVATE_COERCE_NAMESPACES(uint8_t)
1542ACTIVATE_COERCE_NAMESPACES(int16_t)
1543ACTIVATE_COERCE_NAMESPACES(uint16_t)
1544ACTIVATE_COERCE_NAMESPACES(int32_t)
1545ACTIVATE_COERCE_NAMESPACES(uint32_t)
1546ACTIVATE_COERCE_NAMESPACES(int64_t)
1547ACTIVATE_COERCE_NAMESPACES(uint64_t)
1548ACTIVATE_COERCE_NAMESPACES(half)
1549ACTIVATE_COERCE_NAMESPACES(float)
1550ACTIVATE_COERCE_NAMESPACES(double)
1551ACTIVATE_COERCE_NAMESPACES(ldouble)
1552ACTIVATE_COERCE_NAMESPACES(real)
1553
1554#if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
1555#   pragma GCC diagnostic pop
1556#elif defined _MSC_VER
1557#   pragma warning(pop)
1558#endif
1559
1560#undef LOL_MEMBER_OPS
1561
1562#undef LOL_VECTOR_VECTOR_COERCE_OP
1563#undef LOL_VECTOR_VECTOR_OP
1564#undef LOL_VECTOR_MINMAX_FUN
1565#undef LOL_VECTOR_CLAMP_FUN
1566#undef LOL_VECTOR_VECTOR_BOOL_OP
1567#undef LOL_VECTOR_SCALAR_COERCE_OP
1568#undef LOL_SCALAR_VECTOR_COERCE_OP
1569#undef LOL_VECTOR_SCALAR_OP
1570
1571#undef LOL_BINARY_VECTOR_OPS
1572#undef LOL_BINARY_VECTOR_FUNS
1573#undef LOL_BINARY_NONVECTOR_OPS
1574#undef LOL_BINARY_NONVECTOR_FUNS
1575#undef LOL_UNARY_OPS
1576#undef LOL_UNARY_FUNS
1577#undef LOL_BINARY_NONVECTOR_COERCE_OPS
1578#undef LOL_BINARY_NONVECTOR_COERCE_FUNS
1579#undef LOL_BINARY_VECTOR_COERCE_OPS
1580#undef LOL_VECTOR_COERCE_OPS
1581#undef LOL_VEC_3_COERCE_FUNS
1582#undef LOL_VEC_ANY_COERCE_OPS
1583#undef LOL_VEC_ANY_COERCE_FUNS
1584#undef LOL_VECTOR_OPS
1585
1586#undef LOL_ALL_NONVECTOR_OPS_AND_FUNS
1587#undef LOL_ALL_VECTOR_OPS_INNER
1588#undef LOL_ALL_VECTOR_FUNS_INNER
1589#undef LOL_ALL_VECTOR_OPS_AND_FUNS
1590#undef LOL_ALL_VECTOR_COERCE_OPS_AND_FUNS
1591
1592/*
1593 * Definition of additional functions requiring vector functions
1594 */
1595
1596template<typename T>
1597inline Quat<T> Quat<T>::operator *(Quat<T> const &val) const
1598{
1599    Quat<T> ret;
1600    Vec3<T> v1(x, y, z);
1601    Vec3<T> v2(val.x, val.y, val.z);
1602    Vec3<T> v3 = cross(v1, v2) + w * v2 + val.w * v1;
1603    return Quat<T>(w * val.w - dot(v1, v2), v3.x, v3.y, v3.z);
1604}
1605
1606/*
1607 * Magic vector swizzling (part 2/2)
1608 * Unfortunately these assignment operators cannot be used for now, because
1609 * we would also need to override the default copy assignment operator, and
1610 * in C++98 unions cannot contain such objects. This is why all the swizzling
1611 * magic objects are marked 'const' even those that could be lvalues.
1612 */
1613
1614template<typename T, int N>
1615inline Vec2<T> XVec2<T, N>::operator =(Vec2<T> const &that)
1616{
1617    for (int i = 0; i < 2; i++)
1618        *this[i] = that[i];
1619    return *this;
1620}
1621
1622template<typename T, int N>
1623inline Vec3<T> XVec3<T, N>::operator =(Vec3<T> const &that)
1624{
1625    for (int i = 0; i < 3; i++)
1626        *this[i] = that[i];
1627    return *this;
1628}
1629
1630template<typename T, int N>
1631inline Vec4<T> XVec4<T, N>::operator =(Vec4<T> const &that)
1632{
1633    for (int i = 0; i < 4; i++)
1634        *this[i] = that[i];
1635    return *this;
1636}
1637
1638/*
1639 * 2×2-element matrices
1640 */
1641
1642template <typename T> struct Mat2
1643{
1644    inline Mat2() {}
1645    inline Mat2(Vec2<T> V0, Vec2<T> V1)
1646      : v0(V0), v1(V1) {}
1647
1648    explicit inline Mat2(T val)
1649      : v0(val, (T)0),
1650        v1((T)0, val) {}
1651
1652    explicit inline Mat2(Mat4<T> const &mat)
1653      : v0(mat[0].xy),
1654        v1(mat[1].xy) {}
1655
1656    inline Vec2<T>& operator[](size_t n) { return (&v0)[n]; }
1657    inline Vec2<T> const& operator[](size_t n) const { return (&v0)[n]; }
1658
1659    /* Helpers for transformation matrices */
1660    static Mat2<T> rotate(T angle);
1661
1662    static inline Mat2<T> rotate(Mat2<T> mat, T angle)
1663    {
1664        return rotate(angle) * mat;
1665    }
1666
1667    void printf() const;
1668
1669    template<class U>
1670    friend std::ostream &operator<<(std::ostream &stream, Mat2<U> const &m);
1671
1672    inline Mat2<T> operator +(Mat2<T> const m) const
1673    {
1674        return Mat2<T>(v0 + m[0], v1 + m[1]);
1675    }
1676
1677    inline Mat2<T> operator +=(Mat2<T> const m)
1678    {
1679        return *this = *this + m;
1680    }
1681
1682    inline Mat2<T> operator -(Mat2<T> const m) const
1683    {
1684        return Mat2<T>(v0 - m[0], v1 - m[1]);
1685    }
1686
1687    inline Mat2<T> operator -=(Mat2<T> const m)
1688    {
1689        return *this = *this - m;
1690    }
1691
1692    inline Mat2<T> operator *(Mat2<T> const m) const
1693    {
1694        return Mat2<T>(*this * m[0], *this * m[1]);
1695    }
1696
1697    inline Mat2<T> operator *=(Mat2<T> const m)
1698    {
1699        return *this = *this * m;
1700    }
1701
1702    inline Vec2<T> operator *(Vec2<T> const m) const
1703    {
1704        Vec2<T> ret;
1705        for (int j = 0; j < 2; j++)
1706        {
1707            T tmp = 0;
1708            for (int k = 0; k < 2; k++)
1709                tmp += (*this)[k][j] * m[k];
1710            ret[j] = tmp;
1711        }
1712        return ret;
1713    }
1714
1715    Vec2<T> v0, v1;
1716};
1717
1718/*
1719 * 3×3-element matrices
1720 */
1721
1722template <typename T> struct Mat3
1723{
1724    inline Mat3() {}
1725    inline Mat3(Vec3<T> V0, Vec3<T> V1, Vec3<T> V2)
1726      : v0(V0), v1(V1), v2(V2) {}
1727
1728    explicit inline Mat3(T val)
1729      : v0(val, (T)0, (T)0),
1730        v1((T)0, val, (T)0),
1731        v2((T)0, (T)0, val) {}
1732
1733    explicit inline Mat3(Mat2<T> mat)
1734      : v0(mat[0], (T)0),
1735        v1(mat[1], (T)0),
1736        v2((T)0, (T)0, (T)0) {}
1737
1738    explicit inline Mat3(Mat2<T> mat, T val)
1739      : v0(Vec3<T>(mat[0], (T)0)),
1740        v1(Vec3<T>(mat[1], (T)0)),
1741        v2((T)0, (T)0, val) {}
1742
1743    explicit inline Mat3(Mat4<T> const &mat)
1744      : v0(mat[0].xyz),
1745        v1(mat[1].xyz),
1746        v2(mat[2].xyz) {}
1747
1748    explicit Mat3(Quat<T> const &q);
1749
1750    inline Vec3<T>& operator[](size_t n) { return (&v0)[n]; }
1751    inline Vec3<T> const& operator[](size_t n) const { return (&v0)[n]; }
1752
1753    /* Helpers for transformation matrices */
1754    static Mat3<T> scale(T x);
1755    static Mat3<T> scale(T x, T y, T z);
1756    static Mat3<T> scale(Vec3<T> v);
1757    static Mat3<T> rotate(T angle, T x, T y, T z);
1758    static Mat3<T> rotate(T angle, Vec3<T> v);
1759
1760    static Mat3<T> fromeuler_xyz(Vec3<T> const &v);
1761    static Mat3<T> fromeuler_xzy(Vec3<T> const &v);
1762    static Mat3<T> fromeuler_yxz(Vec3<T> const &v);
1763    static Mat3<T> fromeuler_yzx(Vec3<T> const &v);
1764    static Mat3<T> fromeuler_zxy(Vec3<T> const &v);
1765    static Mat3<T> fromeuler_zyx(Vec3<T> const &v);
1766    static Mat3<T> fromeuler_xyz(T phi, T theta, T psi);
1767    static Mat3<T> fromeuler_xzy(T phi, T theta, T psi);
1768    static Mat3<T> fromeuler_yxz(T phi, T theta, T psi);
1769    static Mat3<T> fromeuler_yzx(T phi, T theta, T psi);
1770    static Mat3<T> fromeuler_zxy(T phi, T theta, T psi);
1771    static Mat3<T> fromeuler_zyx(T phi, T theta, T psi);
1772
1773    static Mat3<T> fromeuler_xyx(Vec3<T> const &v);
1774    static Mat3<T> fromeuler_xzx(Vec3<T> const &v);
1775    static Mat3<T> fromeuler_yxy(Vec3<T> const &v);
1776    static Mat3<T> fromeuler_yzy(Vec3<T> const &v);
1777    static Mat3<T> fromeuler_zxz(Vec3<T> const &v);
1778    static Mat3<T> fromeuler_zyz(Vec3<T> const &v);
1779    static Mat3<T> fromeuler_xyx(T phi, T theta, T psi);
1780    static Mat3<T> fromeuler_xzx(T phi, T theta, T psi);
1781    static Mat3<T> fromeuler_yxy(T phi, T theta, T psi);
1782    static Mat3<T> fromeuler_yzy(T phi, T theta, T psi);
1783    static Mat3<T> fromeuler_zxz(T phi, T theta, T psi);
1784    static Mat3<T> fromeuler_zyz(T phi, T theta, T psi);
1785
1786    static inline Mat3<T> rotate(Mat3<T> mat, T angle, Vec3<T> v)
1787    {
1788        return rotate(angle, v) * mat;
1789    }
1790
1791    void printf() const;
1792
1793    template<class U>
1794    friend std::ostream &operator<<(std::ostream &stream, Mat3<U> const &m);
1795
1796    inline Mat3<T> operator +(Mat3<T> const m) const
1797    {
1798        return Mat3<T>(v0 + m[0], v1 + m[1], v2 + m[2]);
1799    }
1800
1801    inline Mat3<T> operator +=(Mat3<T> const m)
1802    {
1803        return *this = *this + m;
1804    }
1805
1806    inline Mat3<T> operator -(Mat3<T> const m) const
1807    {
1808        return Mat3<T>(v0 - m[0], v1 - m[1], v2 - m[2]);
1809    }
1810
1811    inline Mat3<T> operator -=(Mat3<T> const m)
1812    {
1813        return *this = *this - m;
1814    }
1815
1816    inline Mat3<T> operator *(Mat3<T> const m) const
1817    {
1818        return Mat3<T>(*this * m[0], *this * m[1], *this * m[2]);
1819    }
1820
1821    inline Mat3<T> operator *=(Mat3<T> const m)
1822    {
1823        return *this = *this * m;
1824    }
1825
1826    inline Vec3<T> operator *(Vec3<T> const m) const
1827    {
1828        Vec3<T> ret;
1829        for (int j = 0; j < 3; j++)
1830        {
1831            T tmp = 0;
1832            for (int k = 0; k < 3; k++)
1833                tmp += (*this)[k][j] * m[k];
1834            ret[j] = tmp;
1835        }
1836        return ret;
1837    }
1838
1839    Vec3<T> v0, v1, v2;
1840};
1841
1842/*
1843 * 4×4-element matrices
1844 */
1845
1846template <typename T> struct Mat4
1847{
1848    inline Mat4() {}
1849    inline Mat4(Vec4<T> V0, Vec4<T> V1, Vec4<T> V2, Vec4<T> V3)
1850      : v0(V0), v1(V1), v2(V2), v3(V3) {}
1851
1852    explicit inline Mat4(T val)
1853      : v0(val, (T)0, (T)0, (T)0),
1854        v1((T)0, val, (T)0, (T)0),
1855        v2((T)0, (T)0, val, (T)0),
1856        v3((T)0, (T)0, (T)0, val) {}
1857
1858    explicit inline Mat4(Mat2<T> mat)
1859      : v0(mat[0], (T)0, (T)0),
1860        v1(mat[1], (T)0, (T)0),
1861        v2((T)0, (T)0, (T)0, (T)0),
1862        v3((T)0, (T)0, (T)0, (T)0) {}
1863
1864    explicit inline Mat4(Mat2<T> mat, T val1, T val2)
1865      : v0(mat[0], (T)0, (T)0),
1866        v1(mat[1], (T)0, (T)0),
1867        v2((T)0, (T)0, val1, (T)0),
1868        v3((T)0, (T)0, (T)0, val2) {}
1869
1870    explicit inline Mat4(Mat3<T> mat)
1871      : v0(mat[0], (T)0),
1872        v1(mat[1], (T)0),
1873        v2(mat[2], (T)0),
1874        v3((T)0, (T)0, (T)0, (T)0) {}
1875
1876    explicit inline Mat4(Mat3<T> mat, T val)
1877      : v0(mat[0], (T)0),
1878        v1(mat[1], (T)0),
1879        v2(mat[2], (T)0),
1880        v3((T)0, (T)0, (T)0, val) {}
1881
1882    explicit Mat4(Quat<T> const &q);
1883
1884    inline Vec4<T>& operator[](size_t n) { return (&v0)[n]; }
1885    inline Vec4<T> const& operator[](size_t n) const { return (&v0)[n]; }
1886
1887    /* Helpers for transformation matrices */
1888    static Mat4<T> translate(T x, T y, T z);
1889    static Mat4<T> translate(Vec3<T> v);
1890
1891    static inline Mat4<T> scale(T x)
1892    {
1893        return Mat4<T>(Mat3<T>::scale(x), (T)1);
1894    }
1895
1896    static inline Mat4<T> scale(T x, T y, T z)
1897    {
1898        return Mat4<T>(Mat3<T>::scale(x, y, z), (T)1);
1899    }
1900
1901    static inline Mat4<T> scale(Vec3<T> v)
1902    {
1903        return Mat4<T>(Mat3<T>::scale(v), (T)1);
1904    }
1905
1906    static inline Mat4<T> translate(Mat4<T> const &mat, Vec3<T> v)
1907    {
1908        return translate(v) * mat;
1909    }
1910
1911    static inline Mat4<T> rotate(T angle, T x, T y, T z)
1912    {
1913        return Mat4<T>(Mat3<T>::rotate(angle, x, y, z), (T)1);
1914    }
1915
1916    static inline Mat4<T> rotate(T angle, Vec3<T> v)
1917    {
1918        return Mat4<T>(Mat3<T>::rotate(angle, v), (T)1);
1919    }
1920
1921    static inline Mat4<T> rotate(Mat4<T> &mat, T angle, Vec3<T> v)
1922    {
1923        return rotate(angle, v) * mat;
1924    }
1925
1926    static Mat4<T> fromeuler_xyz(Vec3<T> const &v);
1927    static Mat4<T> fromeuler_xzy(Vec3<T> const &v);
1928    static Mat4<T> fromeuler_yxz(Vec3<T> const &v);
1929    static Mat4<T> fromeuler_yzx(Vec3<T> const &v);
1930    static Mat4<T> fromeuler_zxy(Vec3<T> const &v);
1931    static Mat4<T> fromeuler_zyx(Vec3<T> const &v);
1932    static Mat4<T> fromeuler_xyz(T phi, T theta, T psi);
1933    static Mat4<T> fromeuler_xzy(T phi, T theta, T psi);
1934    static Mat4<T> fromeuler_yxz(T phi, T theta, T psi);
1935    static Mat4<T> fromeuler_yzx(T phi, T theta, T psi);
1936    static Mat4<T> fromeuler_zxy(T phi, T theta, T psi);
1937    static Mat4<T> fromeuler_zyx(T phi, T theta, T psi);
1938
1939    static Mat4<T> fromeuler_xyx(Vec3<T> const &v);
1940    static Mat4<T> fromeuler_xzx(Vec3<T> const &v);
1941    static Mat4<T> fromeuler_yxy(Vec3<T> const &v);
1942    static Mat4<T> fromeuler_yzy(Vec3<T> const &v);
1943    static Mat4<T> fromeuler_zxz(Vec3<T> const &v);
1944    static Mat4<T> fromeuler_zyz(Vec3<T> const &v);
1945    static Mat4<T> fromeuler_xyx(T phi, T theta, T psi);
1946    static Mat4<T> fromeuler_xzx(T phi, T theta, T psi);
1947    static Mat4<T> fromeuler_yxy(T phi, T theta, T psi);
1948    static Mat4<T> fromeuler_yzy(T phi, T theta, T psi);
1949    static Mat4<T> fromeuler_zxz(T phi, T theta, T psi);
1950    static Mat4<T> fromeuler_zyz(T phi, T theta, T psi);
1951
1952    /* Helpers for view matrices */
1953    static Mat4<T> lookat(Vec3<T> eye, Vec3<T> center, Vec3<T> up);
1954
1955    /* Helpers for projection matrices */
1956    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
1957    static Mat4<T> ortho(T width, T height, T near, T far);
1958    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
1959    static Mat4<T> perspective(T fov_y, T width, T height, T near, T far);
1960
1961    void printf() const;
1962
1963    template<class U>
1964    friend std::ostream &operator<<(std::ostream &stream, Mat4<U> const &m);
1965
1966    inline Mat4<T> operator +(Mat4<T> const &m) const
1967    {
1968        return Mat4<T>(v0 + m[0], v1 + m[1], v2 + m[2], v3 + m[3]);
1969    }
1970
1971    inline Mat4<T> operator +=(Mat4<T> const &m)
1972    {
1973        return *this = *this + m;
1974    }
1975
1976    inline Mat4<T> operator -(Mat4<T> const &m) const
1977    {
1978        return Mat4<T>(v0 - m[0], v1 - m[1], v2 - m[2], v3 - m[3]);
1979    }
1980
1981    inline Mat4<T> operator -=(Mat4<T> const &m)
1982    {
1983        return *this = *this - m;
1984    }
1985
1986    inline Mat4<T> operator *(Mat4<T> const &m) const
1987    {
1988        return Mat4<T>(*this * m[0], *this * m[1], *this * m[2], *this * m[3]);
1989    }
1990
1991    inline Mat4<T> operator *=(Mat4<T> const &m)
1992    {
1993        return *this = *this * m;
1994    }
1995
1996    inline Vec4<T> operator *(Vec4<T> const &m) const
1997    {
1998        Vec4<T> ret;
1999        for (int j = 0; j < 4; j++)
2000        {
2001            T tmp = 0;
2002            for (int k = 0; k < 4; k++)
2003                tmp += (*this)[k][j] * m[k];
2004            ret[j] = tmp;
2005        }
2006        return ret;
2007    }
2008
2009    Vec4<T> v0, v1, v2, v3;
2010};
2011
2012template<typename T> T determinant(Mat2<T> const &);
2013template<typename T> T determinant(Mat3<T> const &);
2014template<typename T> T determinant(Mat4<T> const &);
2015
2016template<typename T> Mat2<T> transpose(Mat2<T> const &);
2017template<typename T> Mat3<T> transpose(Mat3<T> const &);
2018template<typename T> Mat4<T> transpose(Mat4<T> const &);
2019
2020template<typename T> Mat2<T> inverse(Mat2<T> const &);
2021template<typename T> Mat3<T> inverse(Mat3<T> const &);
2022template<typename T> Mat4<T> inverse(Mat4<T> const &);
2023
2024/*
2025 * Arbitrarily-sized square matrices; for now this only supports
2026 * naive inversion and is used for the Remez inversion method.
2027 */
2028
2029template<int N, typename T> struct Mat
2030{
2031    inline Mat<N, T>() {}
2032
2033    Mat(T x)
2034    {
2035        for (int j = 0; j < N; j++)
2036            for (int i = 0; i < N; i++)
2037                if (i == j)
2038                    m[i][j] = x;
2039                else
2040                    m[i][j] = 0;
2041    }
2042
2043    /* Naive matrix inversion */
2044    Mat<N, T> inv() const
2045    {
2046        Mat a = *this, b((T)1);
2047
2048        /* Inversion method: iterate through all columns and make sure
2049         * all the terms are 1 on the diagonal and 0 everywhere else */
2050        for (int i = 0; i < N; i++)
2051        {
2052            /* If the expected coefficient is zero, add one of
2053             * the other lines. The first we meet will do. */
2054            if (!a.m[i][i])
2055            {
2056                for (int j = i + 1; j < N; j++)
2057                {
2058                    if (!a.m[i][j])
2059                        continue;
2060                    /* Add row j to row i */
2061                    for (int n = 0; n < N; n++)
2062                    {
2063                        a.m[n][i] += a.m[n][j];
2064                        b.m[n][i] += b.m[n][j];
2065                    }
2066                    break;
2067                }
2068            }
2069
2070            /* Now we know the diagonal term is non-zero. Get its inverse
2071             * and use that to nullify all other terms in the column */
2072            T x = (T)1 / a.m[i][i];
2073            for (int j = 0; j < N; j++)
2074            {
2075                if (j == i)
2076                    continue;
2077                T mul = x * a.m[i][j];
2078                for (int n = 0; n < N; n++)
2079                {
2080                    a.m[n][j] -= mul * a.m[n][i];
2081                    b.m[n][j] -= mul * b.m[n][i];
2082                }
2083            }
2084
2085            /* Finally, ensure the diagonal term is 1 */
2086            for (int n = 0; n < N; n++)
2087            {
2088                a.m[n][i] *= x;
2089                b.m[n][i] *= x;
2090            }
2091        }
2092
2093        return b;
2094    }
2095
2096    T m[N][N];
2097};
2098
2099} /* namespace lol */
2100
2101#endif // __LOL_MATH_VECTOR_H__
2102
Note: See TracBrowser for help on using the repository browser.