Steeriously  0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Matrix.hpp
1 #ifndef MATRIX2D_H
2 #define MATRIX2D_H
3 
4 #include <vector>
5 
6 #include <steeriously/Utilities.hpp>
7 #include <steeriously/Vector2.hpp>
8 
9 namespace steer {
10 
15 class Matrix2D
16 {
17 private:
18 
23  struct Matrix
24  {
25 
26  float _11, _12, _13;
27  float _21, _22, _23;
28  float _31, _32, _33;
29 
30  Matrix()
31  {
32  _11=0.0; _12=0.0; _13=0.0;
33  _21=0.0; _22=0.0; _23=0.0;
34  _31=0.0; _32=0.0; _33=0.0;
35  }
36 
37  };
38 
39  Matrix m_Matrix;
40 
41  //multiplies m_Matrix with mIn
47  inline void MatrixMultiply(Matrix &mIn);
48 
49 public:
50 
56  {
57  //initialize the matrix to an identity matrix
58  Identity();
59  }
60 
61  //create an identity matrix
66  inline void Identity();
67 
68  //create a transformation matrix
75  inline void Translate(float x, float y);
76 
77  //create a scale matrix
84  inline void Scale(float xScale, float yScale);
85 
86  //create a rotation matrix
92  inline void Rotate(float rotation);
93 
94  //create a rotation matrix from a fwd and side 2D vector
101  inline void Rotate(const steer::Vector2 &fwd, const steer::Vector2 &side);
102 
103  //applys a transformation matrix to a std::vector of points
109  inline void TransformVector2Ds(std::vector<steer::Vector2> &vPoints);
110 
111  //applys a transformation matrix to a point
117  inline void TransformVector2Ds(steer::Vector2 &vPoint);
118 
119  //accessors to the matrix elements
120  void _11(float val){m_Matrix._11 = val;}
121  void _12(float val){m_Matrix._12 = val;}
122  void _13(float val){m_Matrix._13 = val;}
123 
124  void _21(float val){m_Matrix._21 = val;}
125  void _22(float val){m_Matrix._22 = val;}
126  void _23(float val){m_Matrix._23 = val;}
127 
128  void _31(float val){m_Matrix._31 = val;}
129  void _32(float val){m_Matrix._32 = val;}
130  void _33(float val){m_Matrix._33 = val;}
131 
132 };
133 
134 }
135 
136 //multiply two matrices together
137 inline void steer::Matrix2D::MatrixMultiply(Matrix &mIn)
138 {
139  steer::Matrix2D::Matrix mat_temp;
140 
141  //first row
142  mat_temp._11 = (m_Matrix._11*mIn._11) + (m_Matrix._12*mIn._21) + (m_Matrix._13*mIn._31);
143  mat_temp._12 = (m_Matrix._11*mIn._12) + (m_Matrix._12*mIn._22) + (m_Matrix._13*mIn._32);
144  mat_temp._13 = (m_Matrix._11*mIn._13) + (m_Matrix._12*mIn._23) + (m_Matrix._13*mIn._33);
145 
146  //second
147  mat_temp._21 = (m_Matrix._21*mIn._11) + (m_Matrix._22*mIn._21) + (m_Matrix._23*mIn._31);
148  mat_temp._22 = (m_Matrix._21*mIn._12) + (m_Matrix._22*mIn._22) + (m_Matrix._23*mIn._32);
149  mat_temp._23 = (m_Matrix._21*mIn._13) + (m_Matrix._22*mIn._23) + (m_Matrix._23*mIn._33);
150 
151  //third
152  mat_temp._31 = (m_Matrix._31*mIn._11) + (m_Matrix._32*mIn._21) + (m_Matrix._33*mIn._31);
153  mat_temp._32 = (m_Matrix._31*mIn._12) + (m_Matrix._32*mIn._22) + (m_Matrix._33*mIn._32);
154  mat_temp._33 = (m_Matrix._31*mIn._13) + (m_Matrix._32*mIn._23) + (m_Matrix._33*mIn._33);
155 
156  m_Matrix = mat_temp;
157 }
158 
159 //applies a 2D transformation matrix to a std::vector of Vector2Ds
160 inline void steer::Matrix2D::TransformVector2Ds(std::vector<steer::Vector2> &vPoint)
161 {
162  for (unsigned int i=0; i<vPoint.size(); ++i)
163  {
164  float tempX =(m_Matrix._11*vPoint[i].x) + (m_Matrix._21*vPoint[i].y) + (m_Matrix._31);
165 
166  float tempY = (m_Matrix._12*vPoint[i].x) + (m_Matrix._22*vPoint[i].y) + (m_Matrix._32);
167 
168  vPoint[i].x = tempX;
169 
170  vPoint[i].y = tempY;
171 
172  }
173 }
174 
175 //applies a 2D transformation matrix to a single Vector2D
177 {
178 
179  float tempX =(m_Matrix._11*vPoint.x) + (m_Matrix._21*vPoint.y) + (m_Matrix._31);
180 
181  float tempY = (m_Matrix._12*vPoint.x) + (m_Matrix._22*vPoint.y) + (m_Matrix._32);
182 
183  vPoint.x = tempX;
184 
185  vPoint.y = tempY;
186 }
187 
188 
189 
190 //create an identity matrix
192 {
193  m_Matrix._11 = 1; m_Matrix._12 = 0; m_Matrix._13 = 0;
194 
195  m_Matrix._21 = 0; m_Matrix._22 = 1; m_Matrix._23 = 0;
196 
197  m_Matrix._31 = 0; m_Matrix._32 = 0; m_Matrix._33 = 1;
198 
199 }
200 
201 //create a transformation matrix
202 inline void steer::Matrix2D::Translate(float x, float y)
203 {
204  steer::Matrix2D::Matrix mat;
205 
206  mat._11 = 1; mat._12 = 0; mat._13 = 0;
207 
208  mat._21 = 0; mat._22 = 1; mat._23 = 0;
209 
210  mat._31 = x; mat._32 = y; mat._33 = 1;
211 
212  //and multiply
213  MatrixMultiply(mat);
214 }
215 
216 //create a scale matrix
217 inline void steer::Matrix2D::Scale(float xScale, float yScale)
218 {
219  steer::Matrix2D::Matrix mat;
220 
221  mat._11 = xScale; mat._12 = 0; mat._13 = 0;
222 
223  mat._21 = 0; mat._22 = yScale; mat._23 = 0;
224 
225  mat._31 = 0; mat._32 = 0; mat._33 = 1;
226 
227  //and multiply
228  MatrixMultiply(mat);
229 }
230 
231 
232 //create a rotation matrix
233 inline void steer::Matrix2D::Rotate(float rot)
234 {
235  steer::Matrix2D::Matrix mat;
236 
237  float Sin = sin(rot);
238  float Cos = cos(rot);
239 
240  mat._11 = Cos; mat._12 = Sin; mat._13 = 0;
241 
242  mat._21 = -Sin; mat._22 = Cos; mat._23 = 0;
243 
244  mat._31 = 0; mat._32 = 0;mat._33 = 1;
245 
246  //and multiply
247  MatrixMultiply(mat);
248 }
249 
250 
251 //create a rotation matrix from a 2D vector
252 inline void steer::Matrix2D::Rotate(const steer::Vector2 &fwd, const steer::Vector2 &side)
253 {
254  steer::Matrix2D::Matrix mat;
255 
256  mat._11 = fwd.x; mat._12 = fwd.y; mat._13 = 0;
257 
258  mat._21 = side.x; mat._22 = side.y; mat._23 = 0;
259 
260  mat._31 = 0; mat._32 = 0;mat._33 = 1;
261 
262  //and multiply
263  MatrixMultiply(mat);
264 }
265 
266 #endif //MATRIX2D_HPP
267