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 SCALAR_OP(elems, op) \ |
---|
32 | inline Vec##elems<T> operator op(T const &val) const \ |
---|
33 | { \ |
---|
34 | Vec##elems<T> ret; \ |
---|
35 | for (int n = 0; n < elems; n++) \ |
---|
36 | ret[n] = (*this)[n] op val; \ |
---|
37 | return ret; \ |
---|
38 | } |
---|
39 | |
---|
40 | #define OPERATORS(elems) \ |
---|
41 | inline T& operator[](int n) { return *(&x + n); } \ |
---|
42 | inline T const& operator[](int n) const { return *(&x + n); } \ |
---|
43 | \ |
---|
44 | VECTOR_OP(elems, -) \ |
---|
45 | VECTOR_OP(elems, +) \ |
---|
46 | VECTOR_OP(elems, *) \ |
---|
47 | VECTOR_OP(elems, /) \ |
---|
48 | \ |
---|
49 | SCALAR_OP(elems, -) \ |
---|
50 | SCALAR_OP(elems, +) \ |
---|
51 | SCALAR_OP(elems, *) \ |
---|
52 | SCALAR_OP(elems, /) \ |
---|
53 | \ |
---|
54 | template<typename U> \ |
---|
55 | inline operator Vec##elems<U>() const \ |
---|
56 | { \ |
---|
57 | Vec##elems<U> ret; \ |
---|
58 | for (int n = 0; n < elems; n++) \ |
---|
59 | ret[n] = static_cast<U>((*this)[n]); \ |
---|
60 | return ret; \ |
---|
61 | } \ |
---|
62 | \ |
---|
63 | inline T sqlen() const \ |
---|
64 | { \ |
---|
65 | T acc = 0; \ |
---|
66 | for (int n = 0; n < elems; n++) \ |
---|
67 | acc += (*this)[n] * (*this)[n]; \ |
---|
68 | return acc; \ |
---|
69 | } \ |
---|
70 | \ |
---|
71 | inline float len() const \ |
---|
72 | { \ |
---|
73 | return sqrtf((float)sqlen()); \ |
---|
74 | } |
---|
75 | |
---|
76 | template <typename T> struct Vec2 |
---|
77 | { |
---|
78 | inline Vec2() { x = y = 0; } |
---|
79 | inline Vec2(T val) { x = y = val; } |
---|
80 | inline Vec2(T _x, T _y) { x = _x; y = _y; } |
---|
81 | |
---|
82 | OPERATORS(2) |
---|
83 | |
---|
84 | union { T x; T a; T i; }; |
---|
85 | union { T y; T b; T j; }; |
---|
86 | }; |
---|
87 | |
---|
88 | typedef Vec2<float> Float2; |
---|
89 | typedef Vec2<int> Int2; |
---|
90 | |
---|
91 | template <typename T> struct Vec3 |
---|
92 | { |
---|
93 | inline Vec3() { x = y = z = 0; } |
---|
94 | inline Vec3(T val) { x = y = z = val; } |
---|
95 | inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } |
---|
96 | |
---|
97 | OPERATORS(3) |
---|
98 | |
---|
99 | union { T x; T a; T i; }; |
---|
100 | union { T y; T b; T j; }; |
---|
101 | union { T z; T c; T k; }; |
---|
102 | }; |
---|
103 | |
---|
104 | typedef Vec3<float> Float3; |
---|
105 | typedef Vec3<int> Int3; |
---|
106 | |
---|
107 | #define SCALAR_GLOBAL(elems, op, U) \ |
---|
108 | template<typename T> \ |
---|
109 | static inline Vec##elems<U> operator op(U const &val, \ |
---|
110 | Vec##elems<T> const &that) \ |
---|
111 | { \ |
---|
112 | Vec##elems<U> ret; \ |
---|
113 | for (int n = 0; n < elems; n++) \ |
---|
114 | ret[n] = val op that[n]; \ |
---|
115 | return ret; \ |
---|
116 | } |
---|
117 | |
---|
118 | #define SCALAR_GLOBAL2(elems, op) \ |
---|
119 | SCALAR_GLOBAL(elems, op, int) \ |
---|
120 | SCALAR_GLOBAL(elems, op, float) |
---|
121 | |
---|
122 | #define GLOBALS(elems) \ |
---|
123 | SCALAR_GLOBAL2(elems, -) \ |
---|
124 | SCALAR_GLOBAL2(elems, +) \ |
---|
125 | SCALAR_GLOBAL2(elems, *) \ |
---|
126 | SCALAR_GLOBAL2(elems, /) |
---|
127 | |
---|
128 | GLOBALS(2) |
---|
129 | GLOBALS(3) |
---|
130 | |
---|
131 | #endif // __DH_MATRIX_H__ |
---|
132 | |
---|