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

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

doc: fix claims in the documentation introduction and disable vector.h
subtelties to avoid polluting Doxygen.

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