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 CAST_OP(elems, dest) \ |
---|
50 | inline operator Vec##dest<T>() const \ |
---|
51 | { \ |
---|
52 | Vec##dest<T> ret; \ |
---|
53 | for (int n = 0; n < elems && n < dest; n++) \ |
---|
54 | ret[n] = (*this)[n]; \ |
---|
55 | return ret; \ |
---|
56 | } |
---|
57 | |
---|
58 | #define OPERATORS(elems) \ |
---|
59 | inline T& operator[](int n) { return *(&x + n); } \ |
---|
60 | inline T const& operator[](int n) const { return *(&x + n); } \ |
---|
61 | \ |
---|
62 | VECTOR_OP(elems, -) \ |
---|
63 | VECTOR_OP(elems, +) \ |
---|
64 | VECTOR_OP(elems, *) \ |
---|
65 | VECTOR_OP(elems, /) \ |
---|
66 | \ |
---|
67 | BOOL_OP(elems, ==, false) \ |
---|
68 | BOOL_OP(elems, !=, true) \ |
---|
69 | \ |
---|
70 | SCALAR_OP(elems, -) \ |
---|
71 | SCALAR_OP(elems, +) \ |
---|
72 | SCALAR_OP(elems, *) \ |
---|
73 | SCALAR_OP(elems, /) \ |
---|
74 | \ |
---|
75 | CAST_OP(elems, 2) \ |
---|
76 | CAST_OP(elems, 3) \ |
---|
77 | CAST_OP(elems, 4) \ |
---|
78 | \ |
---|
79 | template<typename U> \ |
---|
80 | inline operator Vec##elems<U>() const \ |
---|
81 | { \ |
---|
82 | Vec##elems<U> ret; \ |
---|
83 | for (int n = 0; n < elems; n++) \ |
---|
84 | ret[n] = static_cast<U>((*this)[n]); \ |
---|
85 | return ret; \ |
---|
86 | } \ |
---|
87 | \ |
---|
88 | inline T sqlen() const \ |
---|
89 | { \ |
---|
90 | T acc = 0; \ |
---|
91 | for (int n = 0; n < elems; n++) \ |
---|
92 | acc += (*this)[n] * (*this)[n]; \ |
---|
93 | return acc; \ |
---|
94 | } \ |
---|
95 | \ |
---|
96 | inline float len() const \ |
---|
97 | { \ |
---|
98 | return sqrtf((float)sqlen()); \ |
---|
99 | } |
---|
100 | |
---|
101 | template <typename T> struct Vec2; |
---|
102 | template <typename T> struct Vec3; |
---|
103 | template <typename T> struct Vec4; |
---|
104 | |
---|
105 | template <typename T> struct Vec2 |
---|
106 | { |
---|
107 | inline Vec2() { } |
---|
108 | inline Vec2(T val) { x = y = val; } |
---|
109 | inline Vec2(T _x, T _y) { x = _x; y = _y; } |
---|
110 | |
---|
111 | OPERATORS(2) |
---|
112 | |
---|
113 | union { T x; T a; T i; }; |
---|
114 | union { T y; T b; T j; }; |
---|
115 | }; |
---|
116 | |
---|
117 | typedef Vec2<float> float2; |
---|
118 | typedef Vec2<int> int2; |
---|
119 | |
---|
120 | template <typename T> struct Vec3 |
---|
121 | { |
---|
122 | inline Vec3() { } |
---|
123 | inline Vec3(T val) { x = y = z = val; } |
---|
124 | inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } |
---|
125 | |
---|
126 | OPERATORS(3) |
---|
127 | |
---|
128 | union { T x; T a; T i; }; |
---|
129 | union { T y; T b; T j; }; |
---|
130 | union { T z; T c; T k; }; |
---|
131 | }; |
---|
132 | |
---|
133 | typedef Vec3<float> float3; |
---|
134 | typedef Vec3<int> int3; |
---|
135 | |
---|
136 | template <typename T> struct Vec4 |
---|
137 | { |
---|
138 | inline Vec4() { } |
---|
139 | inline Vec4(T val) { x = y = z = w = val; } |
---|
140 | inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; } |
---|
141 | |
---|
142 | OPERATORS(4) |
---|
143 | |
---|
144 | union { T x; T a; T i; }; |
---|
145 | union { T y; T b; T j; }; |
---|
146 | union { T z; T c; T k; }; |
---|
147 | union { T w; T d; T l; }; |
---|
148 | }; |
---|
149 | |
---|
150 | typedef Vec4<float> float4; |
---|
151 | typedef Vec4<int> int4; |
---|
152 | |
---|
153 | #define SCALAR_GLOBAL(elems, op, U) \ |
---|
154 | template<typename T> \ |
---|
155 | static inline Vec##elems<U> operator op(U const &val, \ |
---|
156 | Vec##elems<T> const &that) \ |
---|
157 | { \ |
---|
158 | Vec##elems<U> ret; \ |
---|
159 | for (int n = 0; n < elems; n++) \ |
---|
160 | ret[n] = val op that[n]; \ |
---|
161 | return ret; \ |
---|
162 | } |
---|
163 | |
---|
164 | #define SCALAR_GLOBAL2(elems, op) \ |
---|
165 | SCALAR_GLOBAL(elems, op, int) \ |
---|
166 | SCALAR_GLOBAL(elems, op, float) |
---|
167 | |
---|
168 | #define GLOBALS(elems) \ |
---|
169 | SCALAR_GLOBAL2(elems, -) \ |
---|
170 | SCALAR_GLOBAL2(elems, +) \ |
---|
171 | SCALAR_GLOBAL2(elems, *) \ |
---|
172 | SCALAR_GLOBAL2(elems, /) |
---|
173 | |
---|
174 | GLOBALS(2) |
---|
175 | GLOBALS(3) |
---|
176 | GLOBALS(4) |
---|
177 | |
---|
178 | template <typename T> struct Mat4 |
---|
179 | { |
---|
180 | inline Mat4() { } |
---|
181 | inline Mat4(T val) { for (int i = 0; i < 4; i++) v[i] = val; } |
---|
182 | inline Mat4(Vec4<T> v0, Vec4<T> v1, Vec4<T> v2, Vec4<T> v3) |
---|
183 | { v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; } |
---|
184 | |
---|
185 | inline Vec4<T>& operator[](int n) { return v[n]; } |
---|
186 | inline Vec4<T> const& operator[](int n) const { return v[n]; } |
---|
187 | |
---|
188 | static inline Mat4<T> identity() |
---|
189 | { |
---|
190 | Mat4<T> ret; |
---|
191 | for (int j = 0; j < 4; j++) |
---|
192 | for (int i = 0; i < 4; i++) |
---|
193 | ret[i][j] = i == j; |
---|
194 | return ret; |
---|
195 | } |
---|
196 | |
---|
197 | T det() const; |
---|
198 | Mat4<T> invert() const; |
---|
199 | |
---|
200 | inline Mat4<T> operator +(Mat4<T> const val) const |
---|
201 | { |
---|
202 | Mat4<T> ret; |
---|
203 | for (int j = 0; j < 4; j++) |
---|
204 | for (int i = 0; i < 4; i++) |
---|
205 | ret[i][j] = v[i][j] + val[i][j]; |
---|
206 | return ret; |
---|
207 | } |
---|
208 | |
---|
209 | inline Mat4<T> operator -(Mat4<T> const val) const |
---|
210 | { |
---|
211 | Mat4<T> ret; |
---|
212 | for (int j = 0; j < 4; j++) |
---|
213 | for (int i = 0; i < 4; i++) |
---|
214 | ret[i][j] = v[i][j] - val[i][j]; |
---|
215 | return ret; |
---|
216 | } |
---|
217 | |
---|
218 | inline Mat4<T> operator *(Mat4<T> const val) const |
---|
219 | { |
---|
220 | Mat4<T> ret; |
---|
221 | for (int j = 0; j < 4; j++) |
---|
222 | for (int i = 0; i < 4; i++) |
---|
223 | { |
---|
224 | T tmp = 0; |
---|
225 | for (int k = 0; k < 4; k++) |
---|
226 | tmp += v[k][j] * val[i][k]; |
---|
227 | ret[i][j] = tmp; |
---|
228 | } |
---|
229 | return ret; |
---|
230 | } |
---|
231 | |
---|
232 | inline Vec4<T> operator *(Vec4<T> const val) const |
---|
233 | { |
---|
234 | Vec4<T> ret; |
---|
235 | for (int j = 0; j < 4; j++) |
---|
236 | { |
---|
237 | T tmp = 0; |
---|
238 | for (int i = 0; i < 4; i++) |
---|
239 | tmp += v[i][j] * val[i]; |
---|
240 | ret[j] = tmp; |
---|
241 | } |
---|
242 | return ret; |
---|
243 | } |
---|
244 | |
---|
245 | Vec4<T> v[4]; |
---|
246 | }; |
---|
247 | |
---|
248 | typedef Mat4<float> float4x4; |
---|
249 | typedef Mat4<int> int4x4; |
---|
250 | |
---|
251 | #endif // __DH_MATRIX_H__ |
---|
252 | |
---|