source:trunk/src/matrix.h@1041Tweet

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

core: new combinations of integer vectors (unsigned, 8-bit, etc.), dot and
cross product, normalize, etc.

• Property svn:keywords set to `Id`
File size: 12.0 KB
Line
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The Matrix classes
13// ------------------
14//
15
16#if !defined __LOL_MATRIX_H__
17#define __LOL_MATRIX_H__
18
19#include <cmath>
20#if !defined __ANDROID__
21#   include <iostream>
22#endif
23
24namespace lol
25{
26
27#define VECTOR_OP(elems, op) \
28    template<typename U> \
29    inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
30    { \
31        Vec##elems<T> ret; \
32        for (int n = 0; n < elems; n++) \
33            ret[n] = (*this)[n] op val[n]; \
34        return ret; \
35    } \
36    \
37    template<typename U> \
38    inline Vec##elems<T> operator op##=(Vec##elems<U> const &val) \
39    { \
40        return *this = (*this) op val; \
41    }
42
43#define BOOL_OP(elems, op, op2, ret) \
44    inline bool operator op(Vec##elems<T> const &val) const \
45    { \
46        for (int n = 0; n < elems; n++) \
47            if (!((*this)[n] op2 val[n])) \
48                return !ret; \
49        return ret; \
50    }
51
52#define SCALAR_OP(elems, op) \
53    inline Vec##elems<T> operator op(T const &val) const \
54    { \
55        Vec##elems<T> ret; \
56        for (int n = 0; n < elems; n++) \
57            ret[n] = (*this)[n] op val; \
58        return ret; \
59    } \
60    \
61    inline Vec##elems<T> operator op##=(T const &val) \
62    { \
63        return *this = (*this) op val; \
64    }
65
66#define OPERATORS(elems) \
67    inline T& operator[](int n) { return *(&x + n); } \
68    inline T const& operator[](int n) const { return *(&x + n); } \
69    \
70    VECTOR_OP(elems, -) \
71    VECTOR_OP(elems, +) \
72    VECTOR_OP(elems, *) \
73    VECTOR_OP(elems, /) \
74    \
75    BOOL_OP(elems, ==, ==, true) \
76    BOOL_OP(elems, !=, ==, false) \
77    BOOL_OP(elems, <=, <=, true) \
78    BOOL_OP(elems, >=, >=, true) \
79    BOOL_OP(elems, <, <, true) \
80    BOOL_OP(elems, >, >, true) \
81    \
82    SCALAR_OP(elems, -) \
83    SCALAR_OP(elems, +) \
84    SCALAR_OP(elems, *) \
85    SCALAR_OP(elems, /) \
86    \
87    template<typename U> \
88    inline operator Vec##elems<U>() const \
89    { \
90        Vec##elems<U> ret; \
91        for (int n = 0; n < elems; n++) \
92            ret[n] = static_cast<U>((*this)[n]); \
93        return ret; \
94    } \
95    \
96    inline Vec##elems<T> operator -() const \
97    { \
98        Vec##elems<T> ret; \
99        for (int n = 0; n < elems; n++) \
100            ret[n] = -(*this)[n]; \
101        return ret; \
102    } \
103    \
104    inline T sqlen() const \
105    { \
106        T acc = 0; \
107        for (int n = 0; n < elems; n++) \
108            acc += (*this)[n] * (*this)[n]; \
109        return acc; \
110    } \
111    \
112    inline float len() const \
113    { \
114        using namespace std; \
115        return sqrtf((float)sqlen()); \
116    } \
117    \
118    template<typename U> \
119    friend U dot(Vec##elems<U>, Vec##elems<U>); \
120    template<typename U> \
121    friend Vec##elems<U> normalize(Vec##elems<U>); \
122    \
123    void printf() const;
124
125#define SWIZZLE2(e1, e2) \
126    inline Vec2<T> e1##e2() const \
127    { \
128        return Vec2<T>(e1, e2); \
129    }
130
131#define SWIZZLE3(e1, e2, e3) \
132    inline Vec3<T> e1##e2##e3() const \
133    { \
134        return Vec3<T>(e1, e2, e3); \
135    }
136
137#define SWIZZLE4(e1, e2, e3, e4) \
138    inline Vec4<T> e1##e2##e3##e4() const \
139    { \
140        return Vec4<T>(e1, e2, e3, e4); \
141    }
142
143#define SWIZZLE22(e1) \
144    SWIZZLE2(e1, x); SWIZZLE2(e1, y);
145#define SWIZZLE23(e1) \
146    SWIZZLE2(e1, x); SWIZZLE2(e1, y); SWIZZLE2(e1, z);
147#define SWIZZLE24(e1) \
148    SWIZZLE2(e1, x); SWIZZLE2(e1, y); SWIZZLE2(e1, z); SWIZZLE2(e1, w);
149
150#define SWIZZLE32(e1, e2) \
151    SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y);
152#define SWIZZLE322(e1) \
153    SWIZZLE32(e1, x); SWIZZLE32(e1, y);
154#define SWIZZLE33(e1, e2) \
155    SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y); SWIZZLE3(e1, e2, z);
156#define SWIZZLE333(e1) \
157    SWIZZLE33(e1, x); SWIZZLE33(e1, y); SWIZZLE33(e1, z);
158#define SWIZZLE34(e1, e2) \
159    SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y); \
160    SWIZZLE3(e1, e2, z); SWIZZLE3(e1, e2, w);
161#define SWIZZLE344(e1) \
162    SWIZZLE34(e1, x); SWIZZLE34(e1, y); \
163    SWIZZLE34(e1, z); SWIZZLE34(e1, w);
164
165#define SWIZZLE42(e1, e2, e3) \
166    SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y);
167#define SWIZZLE422(e1, e2) \
168    SWIZZLE42(e1, e2, x); SWIZZLE42(e1, e2, y);
169#define SWIZZLE4222(e1) \
170    SWIZZLE422(e1, x); SWIZZLE422(e1, y);
171#define SWIZZLE43(e1, e2, e3) \
172    SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y); SWIZZLE4(e1, e2, e3, z);
173#define SWIZZLE433(e1, e2) \
174    SWIZZLE43(e1, e2, x); SWIZZLE43(e1, e2, y); SWIZZLE43(e1, e2, z);
175#define SWIZZLE4333(e1) \
176    SWIZZLE433(e1, x); SWIZZLE433(e1, y); SWIZZLE433(e1, z);
177#define SWIZZLE44(e1, e2, e3) \
178    SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y); \
179    SWIZZLE4(e1, e2, e3, z); SWIZZLE4(e1, e2, e3, w);
180#define SWIZZLE444(e1, e2) \
181    SWIZZLE44(e1, e2, x); SWIZZLE44(e1, e2, y); \
182    SWIZZLE44(e1, e2, z); SWIZZLE44(e1, e2, w);
183#define SWIZZLE4444(e1) \
184    SWIZZLE444(e1, x); SWIZZLE444(e1, y); SWIZZLE444(e1, z); SWIZZLE444(e1, w);
185
186template <typename T> struct Vec2;
187template <typename T> struct Vec3;
188template <typename T> struct Vec4;
189
190/*
191 * 2-element vectors
192 */
193
194template <typename T> struct Vec2
195{
196    inline Vec2() { }
197    inline Vec2(T val) { x = y = val; }
198    inline Vec2(T _x, T _y) { x = _x; y = _y; }
199
200    OPERATORS(2)
201
202    SWIZZLE22(x); SWIZZLE22(y);
203    SWIZZLE322(x); SWIZZLE322(y);
204    SWIZZLE4222(x); SWIZZLE4222(y);
205
206#if !defined __ANDROID__
207    template<typename U>
208    friend std::ostream &operator<<(std::ostream &stream, Vec2<U> const &v);
209#endif
210
211    union { T x; T a; T i; };
212    union { T y; T b; T j; };
213};
214
215typedef Vec2<half> f16vec2;
216typedef Vec2<float> vec2;
217typedef Vec2<int8_t> i8vec2;
218typedef Vec2<uint8_t> u8vec2;
219typedef Vec2<int16_t> i16vec2;
220typedef Vec2<uint16_t> u16vec2;
221typedef Vec2<int32_t> ivec2;
222typedef Vec2<uint32_t> uvec2;
223typedef Vec2<int64_t> i64vec2;
224typedef Vec2<uint64_t> u64vec2;
225
226/*
227 * 3-element vectors
228 */
229
230template <typename T> struct Vec3
231{
232    inline Vec3() { }
233    inline Vec3(T val) { x = y = z = val; }
234    inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
235    inline Vec3(Vec2<T> _xy, T _z) { x = _xy.x; y = _xy.y; z = _z; }
236    inline Vec3(T _x, Vec2<T> _yz) { x = _x; y = _yz.x; z = _yz.y; }
237
238    OPERATORS(3)
239
240    SWIZZLE23(x); SWIZZLE23(y); SWIZZLE23(z);
241    SWIZZLE333(x); SWIZZLE333(y); SWIZZLE333(z);
242    SWIZZLE4333(x); SWIZZLE4333(y); SWIZZLE4333(z);
243
244    template<typename U>
245    friend Vec3<U> cross(Vec3<U>, Vec3<U>);
246
247#if !defined __ANDROID__
248    template<typename U>
249    friend std::ostream &operator<<(std::ostream &stream, Vec3<U> const &v);
250#endif
251
252    union { T x; T a; T i; };
253    union { T y; T b; T j; };
254    union { T z; T c; T k; };
255};
256
257typedef Vec3<half> f16vec3;
258typedef Vec3<float> vec3;
259typedef Vec3<int8_t> i8vec3;
260typedef Vec3<uint8_t> u8vec3;
261typedef Vec3<int16_t> i16vec3;
262typedef Vec3<uint16_t> u16vec3;
263typedef Vec3<int32_t> ivec3;
264typedef Vec3<uint32_t> uvec3;
265typedef Vec3<int64_t> i64vec3;
266typedef Vec3<uint64_t> u64vec3;
267
268/*
269 * 4-element vectors
270 */
271
272template <typename T> struct Vec4
273{
274    inline Vec4() { }
275    inline Vec4(T val) { x = y = z = w = val; }
276    inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; }
277    inline Vec4(Vec2<T> _xy, T _z, T _w) { x = _xy.x; y = _xy.y; z = _z; w = _w; }
278    inline Vec4(T _x, Vec2<T> _yz, T _w) { x = _x; y = _yz.x; z = _yz.y; w = _w; }
279    inline Vec4(T _x, T _y, Vec2<T> _zw) { x = _x; y = _y; z = _zw.x; w = _zw.y; }
280    inline Vec4(Vec2<T> _xy, Vec2<T> _zw) { x = _xy.x; y = _xy.y; z = _zw.x; w = _zw.y; }
281    inline Vec4(Vec3<T> _xyz, T _w) { x = _xyz.x; y = _xyz.y; z = _xyz.z; w = _w; }
282    inline Vec4(T _x, Vec3<T> _yzw) { x = _x; y = _yzw.x; z = _yzw.y; w = _yzw.z; }
283
284    OPERATORS(4)
285
286    SWIZZLE24(x); SWIZZLE24(y); SWIZZLE24(z); SWIZZLE24(w);
287    SWIZZLE344(x); SWIZZLE344(y); SWIZZLE344(z); SWIZZLE344(w);
288    SWIZZLE4444(x); SWIZZLE4444(y); SWIZZLE4444(z); SWIZZLE4444(w);
289
290#if !defined __ANDROID__
291    template<typename U>
292    friend std::ostream &operator<<(std::ostream &stream, Vec4<U> const &v);
293#endif
294
295    union { T x; T a; T i; };
296    union { T y; T b; T j; };
297    union { T z; T c; T k; };
298    union { T w; T d; T l; };
299};
300
301typedef Vec4<half> f16vec4;
302typedef Vec4<float> vec4;
303typedef Vec4<int8_t> i8vec4;
304typedef Vec4<uint8_t> u8vec4;
305typedef Vec4<int16_t> i16vec4;
306typedef Vec4<uint16_t> u16vec4;
307typedef Vec4<int32_t> ivec4;
308typedef Vec4<uint32_t> uvec4;
309typedef Vec4<int64_t> i64vec4;
310typedef Vec4<uint64_t> u64vec4;
311
312#define SCALAR_GLOBAL(elems, op, U) \
313    template<typename T> \
314    static inline Vec##elems<U> operator op(U const &val, \
315                                            Vec##elems<T> const &that) \
316    { \
317        Vec##elems<U> ret; \
318        for (int n = 0; n < elems; n++) \
319            ret[n] = val op that[n]; \
320        return ret; \
321    }
322
323#define SCALAR_GLOBAL2(elems, op) \
324    SCALAR_GLOBAL(elems, op, int) \
325    SCALAR_GLOBAL(elems, op, float)
326
327#define GLOBALS(elems) \
328    SCALAR_GLOBAL2(elems, -) \
329    SCALAR_GLOBAL2(elems, +) \
330    SCALAR_GLOBAL2(elems, *) \
331    SCALAR_GLOBAL2(elems, /)
332
333GLOBALS(2)
334GLOBALS(3)
335GLOBALS(4)
336
337/*
338 * 4×4-element matrices
339 */
340
341template <typename T> struct Mat4
342{
343    inline Mat4() { }
344    inline Mat4(T val)
345    {
346        for (int j = 0; j < 4; j++)
347            for (int i = 0; i < 4; i++)
348                v[i][j] = (i == j) ? val : 0;
349    }
350    inline Mat4(Vec4<T> v0, Vec4<T> v1, Vec4<T> v2, Vec4<T> v3)
351    {
352        v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3;
353    }
354
355    inline Vec4<T>& operator[](int n) { return v[n]; }
356    inline Vec4<T> const& operator[](int n) const { return v[n]; }
357
358    T det() const;
359    Mat4<T> invert() const;
360
361    static Mat4<T> ortho(T left, T right, T bottom, T top, T near, T far);
362    static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
363    static Mat4<T> lookat(Vec3<T> eye, Vec3<T> center, Vec3<T> up);
364    static Mat4<T> perspective(T theta, T width, T height, T near, T far);
365    static Mat4<T> translate(T x, T y, T z);
366    static Mat4<T> translate(Vec3<T> v);
367    static Mat4<T> rotate(T theta, T x, T y, T z);
368    static Mat4<T> rotate(T theta, Vec3<T> v);
369
370    void printf() const;
371
372#if !defined __ANDROID__
373    template<class U>
374    friend std::ostream &operator<<(std::ostream &stream, Mat4<U> const &m);
375#endif
376
377    inline Mat4<T> operator +(Mat4<T> const val) const
378    {
379        Mat4<T> ret;
380        for (int j = 0; j < 4; j++)
381            for (int i = 0; i < 4; i++)
382                ret[i][j] = v[i][j] + val[i][j];
383        return ret;
384    }
385
386    inline Mat4<T> operator +=(Mat4<T> const val)
387    {
388        return *this = *this + val;
389    }
390
391    inline Mat4<T> operator -(Mat4<T> const val) const
392    {
393        Mat4<T> ret;
394        for (int j = 0; j < 4; j++)
395            for (int i = 0; i < 4; i++)
396                ret[i][j] = v[i][j] - val[i][j];
397        return ret;
398    }
399
400    inline Mat4<T> operator -=(Mat4<T> const val)
401    {
402        return *this = *this - val;
403    }
404
405    inline Mat4<T> operator *(Mat4<T> const val) const
406    {
407        Mat4<T> ret;
408        for (int j = 0; j < 4; j++)
409            for (int i = 0; i < 4; i++)
410            {
411                T tmp = 0;
412                for (int k = 0; k < 4; k++)
413                    tmp += v[k][j] * val[i][k];
414                ret[i][j] = tmp;
415            }
416        return ret;
417    }
418
419    inline Mat4<T> operator *=(Mat4<T> const val)
420    {
421        return *this = *this * val;
422    }
423
424    inline Vec4<T> operator *(Vec4<T> const val) const
425    {
426        Vec4<T> ret;
427        for (int j = 0; j < 4; j++)
428        {
429            T tmp = 0;
430            for (int i = 0; i < 4; i++)
431                tmp += v[i][j] * val[i];
432            ret[j] = tmp;
433        }
434        return ret;
435    }
436
437    Vec4<T> v[4];
438};
439
440typedef Mat4<half> f16mat4;
441typedef Mat4<float> mat4;
442typedef Mat4<int8_t> i8mat4;
443typedef Mat4<uint8_t> u8mat4;
444typedef Mat4<int16_t> i16mat4;
445typedef Mat4<uint16_t> u16mat4;
446typedef Mat4<int32_t> imat4;
447typedef Mat4<uint32_t> umat4;
448typedef Mat4<int64_t> i64mat4;
449typedef Mat4<uint64_t> u64mat4;
450
451} /* namespace lol */
452
453#endif // __LOL_MATRIX_H__
454
Note: See TracBrowser for help on using the repository browser.