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

Last change on this file since 2054 was 2054, checked in by sam, 10 years ago

math: implement abs() and fmod() for vector types and the half class.

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