Cuberite
A lightweight, fast and extensible game server for Minecraft
Matrix4.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 
5 
6 #define _USE_MATH_DEFINES // Enable non-standard math defines (MSVC)
7 #include <math.h>
8 
9 
10 
11 
12 
13 template <typename T>
14 // tolua_begin
15 class Matrix4
16 {
17 
18  TOLUA_TEMPLATE_BIND((T, float, double))
19 
20  // tolua_end
21 
22 public:
23 
24  T cell[16];
25 
26  // tolua_begin
27 
28  inline Matrix4(void)
29  {
30  Identity();
31  }
32 
33  inline Matrix4(const Matrix4 & a_Rhs)
34  {
35  *this = a_Rhs;
36  }
37 
38  // tolua_end
39 
40  inline Matrix4 & operator = (const Matrix4 & a_Rhs)
41  {
42  for (unsigned int i = 0; i < 16; ++i)
43  {
44  cell[i] = a_Rhs.cell[i];
45  }
46  return *this;
47  }
48 
49  // tolua_begin
50 
51  inline T & operator [] (int a_N)
52  {
53  ASSERT(a_N < 16);
54  return cell[a_N];
55  }
56 
57  inline void Identity()
58  {
59  cell[1] = cell[2] = cell[3] = cell[4] = 0;
60  cell[6] = cell[7] = cell[8] = cell[9] = 0;
61  cell[11] = cell[12] = cell[13] = cell[14] = 0;
62 
63  cell[0] = cell[5] = cell[10] = cell[15] = 1;
64  }
65 
66  inline void Init(const Vector3<T> & a_Pos, T a_RX, T a_RY, T a_RZ)
67  {
68  Matrix4<T> t;
69  t.RotateX(a_RZ);
70  RotateY(a_RY);
71  Concatenate(t);
72  t.RotateZ(a_RX);
73  Concatenate(t);
74  Translate(a_Pos);
75  }
76 
77  inline void RotateX(T a_RX)
78  {
79  T sx = static_cast<T>(sin(a_RX * M_PI / 180));
80  T cx = static_cast<T>(cos(a_RX * M_PI / 180));
81 
82  Identity();
83 
84  cell[5] = cx; cell[6] = sx;
85  cell[9] = -sx; cell[10] = cx;
86  }
87 
88  inline void RotateY(T a_RY)
89  {
90  T sy = static_cast<T>(sin(a_RY * M_PI / 180));
91  T cy = static_cast<T>(cos(a_RY * M_PI / 180));
92 
93  Identity();
94 
95  cell[0] = cy; cell[2] = -sy;
96  cell[8] = sy; cell[10] = cy;
97  }
98 
99  inline void RotateZ(T a_RZ)
100  {
101  T sz = static_cast<T>(sin(a_RZ * M_PI / 180));
102  T cz = static_cast<T>(cos(a_RZ * M_PI / 180));
103 
104  Identity();
105 
106  cell[0] = cz; cell[1] = sz;
107  cell[4] = -sz; cell[5] = cz;
108  }
109 
110  inline void Translate(const Vector3<T> & a_Pos)
111  {
112  cell[3] += a_Pos.x;
113  cell[7] += a_Pos.y;
114  cell[11] += a_Pos.z;
115  }
116 
117  inline void SetTranslation(const Vector3<T> & a_Pos)
118  {
119  cell[3] = a_Pos.x;
120  cell[7] = a_Pos.y;
121  cell[11] = a_Pos.z;
122  }
123 
124  inline void Concatenate(const Matrix4 & m2)
125  {
126  Matrix4 res;
127 
128  for (unsigned int c = 0; c < 4; ++c)
129  {
130  for (unsigned int r = 0; r < 4; ++r)
131  {
132  res.cell[r * 4 + c] = (
133  cell[r * 4 + 0] * m2.cell[c + 0] +
134  cell[r * 4 + 1] * m2.cell[c + 4] +
135  cell[r * 4 + 2] * m2.cell[c + 8] +
136  cell[r * 4 + 3] * m2.cell[c + 12]
137  );
138  }
139  }
140 
141  *this = res;
142  }
143 
144  inline Vector3<T> Transform(const Vector3<T> & v) const
145  {
146  T x = cell[0] * v.x + cell[1] * v.y + cell[2] * v.z + cell[3];
147  T y = cell[4] * v.x + cell[5] * v.y + cell[6] * v.z + cell[7];
148  T z = cell[8] * v.x + cell[9] * v.y + cell[10] * v.z + cell[11];
149 
150  return Vector3<T>(x, y, z);
151  }
152 
153  inline void Invert(void)
154  {
155  Matrix4 t;
156 
157  T tx = -cell[3];
158  T ty = -cell[7];
159  T tz = -cell[11];
160 
161  for (unsigned int h = 0; h < 3; ++h)
162  {
163  for (unsigned int v = 0; v < 3; ++v)
164  {
165  t.cell[h + v * 4] = cell[v + h * 4];
166  }
167  }
168 
169  for (unsigned int i = 0; i < 11; ++i)
170  {
171  cell[i] = t.cell[i];
172  }
173 
174  cell[3] = tx * cell[0] + ty * cell[1] + tz * cell[2];
175  cell[7] = tx * cell[4] + ty * cell[5] + tz * cell[6];
176  cell[11] = tx * cell[8] + ty * cell[9] + tz * cell[10];
177  }
178 
179  inline Vector3<T> GetXColumn(void) const
180  {
181  return Vector3<T>(cell[0], cell[1], cell[2]);
182  }
183 
184  inline Vector3<T> GetYColumn(void) const
185  {
186  return Vector3<T>(cell[4], cell[5], cell[6]);
187  }
188 
189  inline Vector3<T> GetZColumn(void) const
190  {
191  return Vector3<T>(cell[8], cell[9], cell[10]);
192  }
193 
194  inline void SetXColumn(const Vector3<T> & a_X)
195  {
196  cell[0] = a_X.x;
197  cell[1] = a_X.y;
198  cell[2] = a_X.z;
199  }
200 
201  inline void SetYColumn(const Vector3<T> & a_Y)
202  {
203  cell[4] = a_Y.x;
204  cell[5] = a_Y.y;
205  cell[6] = a_Y.z;
206  }
207 
208  inline void SetZColumn(const Vector3<T> & a_Z)
209  {
210  cell[8] = a_Z.x;
211  cell[9] = a_Z.y;
212  cell[10] = a_Z.z;
213  }
214 };
215 // tolua_end
216 
217 
218 
219 
220 // tolua_begin
223 // tolua_end
224 
225 
226 
227 
228 
#define TOLUA_TEMPLATE_BIND(x)
Definition: Globals.h:379
#define ASSERT(x)
Definition: Globals.h:276
Matrix4< float > Matrix4f
Definition: Matrix4.h:222
Matrix4< double > Matrix4d
Definition: Matrix4.h:221
Vector3< T > GetXColumn(void) const
Definition: Matrix4.h:179
Matrix4(void)
Definition: Matrix4.h:28
T & operator[](int a_N)
Definition: Matrix4.h:51
void Identity()
Definition: Matrix4.h:57
Vector3< T > GetZColumn(void) const
Definition: Matrix4.h:189
void RotateX(T a_RX)
Definition: Matrix4.h:77
void SetXColumn(const Vector3< T > &a_X)
Definition: Matrix4.h:194
void SetZColumn(const Vector3< T > &a_Z)
Definition: Matrix4.h:208
void Invert(void)
Definition: Matrix4.h:153
void SetYColumn(const Vector3< T > &a_Y)
Definition: Matrix4.h:201
Vector3< T > Transform(const Vector3< T > &v) const
Definition: Matrix4.h:144
Vector3< T > GetYColumn(void) const
Definition: Matrix4.h:184
void RotateZ(T a_RZ)
Definition: Matrix4.h:99
void Concatenate(const Matrix4 &m2)
Definition: Matrix4.h:124
Matrix4(const Matrix4 &a_Rhs)
Definition: Matrix4.h:33
void RotateY(T a_RY)
Definition: Matrix4.h:88
void Translate(const Vector3< T > &a_Pos)
Definition: Matrix4.h:110
void SetTranslation(const Vector3< T > &a_Pos)
Definition: Matrix4.h:117
Matrix4 & operator=(const Matrix4 &a_Rhs)
Definition: Matrix4.h:40
T cell[16]
Definition: Matrix4.h:24
void Init(const Vector3< T > &a_Pos, T a_RX, T a_RY, T a_RZ)
Definition: Matrix4.h:66
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17