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

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

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

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