source: trunk/src/matrix.h @ 294

Last change on this file since 294 was 294, checked in by sam, 11 years ago

Rename Float3 to float3, Int3 to int3 etc. and add the 4-member versions.

  • Property svn:keywords set to Id
File size: 3.8 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
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 Matrix classes
13// ------------------
14//
15
16#if !defined __DH_MATRIX_H__
17#define __DH_MATRIX_H__
18
19#include <cmath>
20
21#define VECTOR_OP(elems, op) \
22    template<typename U> \
23    inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
24    { \
25        Vec##elems<T> ret; \
26        for (int n = 0; n < elems; n++) \
27            ret[n] = (*this)[n] op val[n]; \
28        return ret; \
29    }
30
31#define BOOL_OP(elems, op, ret) \
32    inline bool operator op(Vec##elems<T> const &val) const \
33    { \
34        for (int n = 0; n < elems; n++) \
35            if ((*this)[n] != val[n]) \
36                return ret; \
37        return !ret; \
38    }
39
40#define SCALAR_OP(elems, op) \
41    inline Vec##elems<T> operator op(T const &val) const \
42    { \
43        Vec##elems<T> ret; \
44        for (int n = 0; n < elems; n++) \
45            ret[n] = (*this)[n] op val; \
46        return ret; \
47    }
48
49#define OPERATORS(elems) \
50    inline T& operator[](int n) { return *(&x + n); } \
51    inline T const& operator[](int n) const { return *(&x + n); } \
52    \
53    VECTOR_OP(elems, -) \
54    VECTOR_OP(elems, +) \
55    VECTOR_OP(elems, *) \
56    VECTOR_OP(elems, /) \
57    \
58    BOOL_OP(elems, ==, false) \
59    BOOL_OP(elems, !=, true) \
60    \
61    SCALAR_OP(elems, -) \
62    SCALAR_OP(elems, +) \
63    SCALAR_OP(elems, *) \
64    SCALAR_OP(elems, /) \
65    \
66    template<typename U> \
67    inline operator Vec##elems<U>() const \
68    { \
69        Vec##elems<U> ret; \
70        for (int n = 0; n < elems; n++) \
71            ret[n] = static_cast<U>((*this)[n]); \
72        return ret; \
73    } \
74    \
75    inline T sqlen() const \
76    { \
77        T acc = 0; \
78        for (int n = 0; n < elems; n++) \
79            acc += (*this)[n] * (*this)[n]; \
80        return acc; \
81    } \
82    \
83    inline float len() const \
84    { \
85        return sqrtf((float)sqlen()); \
86    }
87
88template <typename T> struct Vec2
89{
90    inline Vec2() { x = y = 0; }
91    inline Vec2(T val) { x = y = val; }
92    inline Vec2(T _x, T _y) { x = _x; y = _y; }
93
94    OPERATORS(2)
95
96    union { T x; T a; T i; };
97    union { T y; T b; T j; };
98};
99
100typedef Vec2<float> float2;
101typedef Vec2<int> int2;
102
103template <typename T> struct Vec3
104{
105    inline Vec3() { x = y = z = 0; }
106    inline Vec3(T val) { x = y = z = val; }
107    inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
108
109    OPERATORS(3)
110
111    union { T x; T a; T i; };
112    union { T y; T b; T j; };
113    union { T z; T c; T k; };
114};
115
116typedef Vec3<float> float3;
117typedef Vec3<int> int3;
118
119template <typename T> struct Vec4
120{
121    inline Vec4() { x = y = z = w = 0; }
122    inline Vec4(T val) { x = y = z = w = val; }
123    inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; }
124
125    OPERATORS(4)
126
127    union { T x; T a; T i; };
128    union { T y; T b; T j; };
129    union { T z; T c; T k; };
130    union { T w; T d; T l; };
131};
132
133typedef Vec4<float> float4;
134typedef Vec4<int> int4;
135
136#define SCALAR_GLOBAL(elems, op, U) \
137    template<typename T> \
138    static inline Vec##elems<U> operator op(U const &val, \
139                                            Vec##elems<T> const &that) \
140    { \
141        Vec##elems<U> ret; \
142        for (int n = 0; n < elems; n++) \
143            ret[n] = val op that[n]; \
144        return ret; \
145    }
146
147#define SCALAR_GLOBAL2(elems, op) \
148    SCALAR_GLOBAL(elems, op, int) \
149    SCALAR_GLOBAL(elems, op, float)
150
151#define GLOBALS(elems) \
152    SCALAR_GLOBAL2(elems, -) \
153    SCALAR_GLOBAL2(elems, +) \
154    SCALAR_GLOBAL2(elems, *) \
155    SCALAR_GLOBAL2(elems, /)
156
157GLOBALS(2)
158GLOBALS(3)
159GLOBALS(4)
160
161#endif // __DH_MATRIX_H__
162
Note: See TracBrowser for help on using the repository browser.