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 Vec4x4 |
---|
179 | { |
---|
180 | inline Vec4x4() { } |
---|
181 | inline Vec4x4(T val) { v[0] = v[1] = v[2] = v[3] = val; } |
---|
182 | inline Vec4x4(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 | inline Vec4x4<T> operator +(Vec4x4<T> const val) const |
---|
189 | { |
---|
190 | Vec4x4<T> ret; |
---|
191 | for (int j = 0; j < 4; j++) |
---|
192 | for (int i = 0; i < 4; i++) |
---|
193 | ret[i][j] = v[i][j] + val[i][j]; |
---|
194 | return ret; |
---|
195 | } |
---|
196 | |
---|
197 | inline Vec4x4<T> operator -(Vec4x4<T> const val) const |
---|
198 | { |
---|
199 | Vec4x4<T> ret; |
---|
200 | for (int j = 0; j < 4; j++) |
---|
201 | for (int i = 0; i < 4; i++) |
---|
202 | ret[i][j] = v[i][j] - val[i][j]; |
---|
203 | return ret; |
---|
204 | } |
---|
205 | |
---|
206 | inline Vec4x4<T> operator *(Vec4x4<T> const val) const |
---|
207 | { |
---|
208 | Vec4x4<T> ret; |
---|
209 | for (int j = 0; j < 4; j++) |
---|
210 | for (int i = 0; i < 4; i++) |
---|
211 | { |
---|
212 | T tmp = 0; |
---|
213 | for (int k = 0; k < 4; k++) |
---|
214 | tmp += v[k][j] * val[i][k]; |
---|
215 | ret[i][j] = tmp; |
---|
216 | } |
---|
217 | return ret; |
---|
218 | } |
---|
219 | |
---|
220 | inline Vec4<T> operator *(Vec4<T> const val) const |
---|
221 | { |
---|
222 | Vec4<T> ret; |
---|
223 | for (int j = 0; j < 4; j++) |
---|
224 | { |
---|
225 | T tmp = 0; |
---|
226 | for (int i = 0; i < 4; i++) |
---|
227 | tmp += v[i][j] * val[i]; |
---|
228 | ret[j] = tmp; |
---|
229 | } |
---|
230 | return ret; |
---|
231 | } |
---|
232 | |
---|
233 | Vec4<T> v[4]; |
---|
234 | }; |
---|
235 | |
---|
236 | typedef Vec4x4<float> float4x4; |
---|
237 | typedef Vec4x4<int> int4x4; |
---|
238 | |
---|
239 | #endif // __DH_MATRIX_H__ |
---|
240 | |
---|