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

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

math: remove coercion rules in the vector classes, they increase the
compilation time for very little benefit and maybe even confusion.

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