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

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

math: add min(), max() and clamp() for half, real and vector types.

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