KiCad PCB EDA Suite
matrix3x3.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6  *
7  * Matrix class (3x3)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *-
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #ifndef MATRIX3X3_H_
28 #define MATRIX3X3_H_
29 
30 #include <math/vector2d.h>
31 
53 // Forward declaration for template friends
54 template <class T>
55 class MATRIX3x3;
56 
57 template <class T>
58 std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );
59 
60 template <class T>
61 class MATRIX3x3
62 {
63 public:
64  T m_data[3][3];
65 
71  MATRIX3x3();
72 
88  MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 );
89 
95  void SetIdentity();
96 
102  void SetTranslation( VECTOR2<T> aTranslation );
103 
109  VECTOR2<T> GetTranslation() const;
110 
118  void SetRotation( T aAngle );
119 
125  void SetScale( VECTOR2<T> aScale );
126 
132  VECTOR2<T> GetScale() const;
133 
139  T Determinant() const;
140 
151  MATRIX3x3 Inverse() const;
152 
158  MATRIX3x3 Transpose() const;
159 
163  friend std::ostream& operator<<<T>( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );
164 
165 };
166 
167 // Operators
168 
170 template <class T> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB );
171 
173 template <class T> VECTOR2<T> const operator*( MATRIX3x3<T> const& aA, VECTOR2<T> const& aB );
174 
176 template <class T, class S> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, T aScalar );
177 template <class T, class S> MATRIX3x3<T> const operator*( T aScalar, MATRIX3x3<T> const& aMatrix );
178 
179 // ----------------------
180 // --- Implementation ---
181 // ----------------------
182 
183 template <class T>
185 {
186  for( int j = 0; j < 3; j++ )
187  {
188  for( int i = 0; i < 3; i++ )
189  {
190  m_data[i][j] = 0.0;
191  }
192  }
193 }
194 
195 
196 template <class T>
197 MATRIX3x3<T>::MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 )
198 {
199  m_data[0][0] = a00;
200  m_data[0][1] = a01;
201  m_data[0][2] = a02;
202 
203  m_data[1][0] = a10;
204  m_data[1][1] = a11;
205  m_data[1][2] = a12;
206 
207  m_data[2][0] = a20;
208  m_data[2][1] = a21;
209  m_data[2][2] = a22;
210 }
211 
212 
213 template <class T>
215 {
216  for( int j = 0; j < 3; j++ )
217  {
218  for( int i = 0; i < 3; i++ )
219  {
220  if( i == j )
221  m_data[i][j] = 1.0;
222  else
223  m_data[i][j] = 0.0;
224  }
225  }
226 }
227 
228 
229 template <class T>
231 {
232  m_data[0][2] = aTranslation.x;
233  m_data[1][2] = aTranslation.y;
234 }
235 
236 
237 template <class T>
239 {
240  VECTOR2<T> result;
241  result.x = m_data[0][2];
242  result.y = m_data[1][2];
243 
244  return result;
245 }
246 
247 
248 template <class T>
250 {
251  T cosValue = cos( aAngle );
252  T sinValue = sin( aAngle );
253  m_data[0][0] = cosValue;
254  m_data[0][1] = -sinValue;
255  m_data[1][0] = sinValue;
256  m_data[1][1] = cosValue;
257 }
258 
259 
260 template <class T>
262 {
263  m_data[0][0] = aScale.x;
264  m_data[1][1] = aScale.y;
265 }
266 
267 
268 template <class T>
270 {
271  VECTOR2<T> result( m_data[0][0], m_data[1][1] );
272 
273  return result;
274 }
275 
276 
277 template <class T>
278 MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB )
279 {
280  MATRIX3x3<T> result;
281 
282  for( int i = 0; i < 3; i++ )
283  {
284  for( int j = 0; j < 3; j++ )
285  {
286  result.m_data[i][j] = aA.m_data[i][0] * aB.m_data[0][j] +
287  aA.m_data[i][1] * aB.m_data[1][j] +
288  aA.m_data[i][2] * aB.m_data[2][j];
289  }
290  }
291 
292  return result;
293 }
294 
295 
296 template <class T>
297 VECTOR2<T> const operator*( MATRIX3x3<T> const& aMatrix, VECTOR2<T> const& aVector )
298 {
299  VECTOR2<T> result( 0, 0 );
300  result.x = aMatrix.m_data[0][0] * aVector.x + aMatrix.m_data[0][1] * aVector.y
301  + aMatrix.m_data[0][2];
302  result.y = aMatrix.m_data[1][0] * aVector.x + aMatrix.m_data[1][1] * aVector.y
303  + aMatrix.m_data[1][2];
304 
305  return result;
306 }
307 
308 
309 template <class T>
311 {
312  return m_data[0][0] * ( m_data[1][1] * m_data[2][2] - m_data[1][2] * m_data[2][1] )
313  - m_data[0][1] * ( m_data[1][0] * m_data[2][2] - m_data[1][2] * m_data[2][0] )
314  + m_data[0][2] * ( m_data[1][0] * m_data[2][1] - m_data[1][1] * m_data[2][0] );
315 }
316 
317 
318 template <class T, class S>
319 MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aMatrix, S aScalar )
320 {
321  MATRIX3x3<T> result;
322 
323  for( int i = 0; i < 3; i++ )
324  {
325  for( int j = 0; j < 3; j++ )
326  {
327  result.m_data[i][j] = aMatrix.m_data[i][j] * aScalar;
328  }
329  }
330 
331  return result;
332 }
333 
334 
335 template <class T, class S>
336 MATRIX3x3<T> const operator*( S aScalar, MATRIX3x3<T> const& aMatrix )
337 {
338  return aMatrix * aScalar;
339 }
340 
341 
342 template <class T>
344 {
345  MATRIX3x3<T> result;
346 
347  result.m_data[0][0] = m_data[1][1] * m_data[2][2] - m_data[2][1] * m_data[1][2];
348  result.m_data[0][1] = m_data[0][2] * m_data[2][1] - m_data[2][2] * m_data[0][1];
349  result.m_data[0][2] = m_data[0][1] * m_data[1][2] - m_data[1][1] * m_data[0][2];
350 
351  result.m_data[1][0] = m_data[1][2] * m_data[2][0] - m_data[2][2] * m_data[1][0];
352  result.m_data[1][1] = m_data[0][0] * m_data[2][2] - m_data[2][0] * m_data[0][2];
353  result.m_data[1][2] = m_data[0][2] * m_data[1][0] - m_data[1][2] * m_data[0][0];
354 
355  result.m_data[2][0] = m_data[1][0] * m_data[2][1] - m_data[2][0] * m_data[1][1];
356  result.m_data[2][1] = m_data[0][1] * m_data[2][0] - m_data[2][1] * m_data[0][0];
357  result.m_data[2][2] = m_data[0][0] * m_data[1][1] - m_data[1][0] * m_data[0][1];
358 
359  return result * ( 1.0 / Determinant() );
360 }
361 
362 
363 template <class T>
365 {
366  MATRIX3x3<T> result;
367 
368  for( int i = 0; i < 3; i++ )
369  {
370  for( int j = 0; j < 3; j++ )
371  {
372  result.m_data[j][i] = m_data[i][j];
373  }
374  }
375 
376  return result;
377 }
378 
379 
380 template <class T>
381 std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix )
382 {
383  for( int i = 0; i < 3; i++ )
384  {
385  aStream << "| ";
386 
387  for( int j = 0; j < 3; j++ )
388  {
389  aStream << aMatrix.m_data[i][j];
390  aStream << " ";
391  }
392 
393  aStream << "|";
394  aStream << "\n";
395  }
396 
397  return aStream;
398 }
399 
400 
401 /* Default specializations */
403 
404 #endif /* MATRIX3X3_H_ */
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:343
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:249
T
enum T contains all this lexer's tokens.
MATRIX3x3()
Constructor.
Definition: matrix3x3.h:184
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
MATRIX3x3< T > const operator*(MATRIX3x3< T > const &aA, MATRIX3x3< T > const &aB)
Matrix multiplication.
Definition: matrix3x3.h:278
void SetIdentity()
Set the matrix to the identity matrix.
Definition: matrix3x3.h:214
MATRIX3x3< double > MATRIX3x3D
Definition: matrix3x3.h:402
T m_data[3][3]
Definition: matrix3x3.h:64
T Determinant() const
Compute the determinant of the matrix.
Definition: matrix3x3.h:310
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:261
Class MATRIX3x3 describes a general 3x3 matrix.
Definition: matrix3x3.h:55
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:230
MATRIX3x3 Transpose() const
Get the transpose of the matrix.
Definition: matrix3x3.h:364
VECTOR2< T > GetTranslation() const
Get the translation components of the matrix.
Definition: matrix3x3.h:238
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition: matrix3x3.h:269