KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2021 Kicad Developers, see AUTHORS.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#include <math/vector3.h>
32
54// Forward declaration for template friends
55template <class T>
56class MATRIX3x3;
57
58template <class T>
59std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );
60
61template <class T>
63{
64public:
65 T m_data[3][3];
66
71
76
90 MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 );
91
98
104 void SetTranslation( VECTOR2<T> aTranslation );
105
112
120 void SetRotation( T aAngle );
121
127 void SetScale( VECTOR2<T> aScale );
128
135
141 T Determinant() const;
142
154
161
165 friend std::ostream& operator<<<T>( std::ostream& aStream, const MATRIX3x3<T>& aMatrix );
166
168 bool operator==( const MATRIX3x3<T>& aOtherMatrix ) const;
169
171 bool operator!=( const MATRIX3x3<T>& aOtherMatrix ) const;
172};
173
174// Operators
175
177template <class T> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB );
178
180template <class T> VECTOR2<T> const operator*( MATRIX3x3<T> const& aA, VECTOR2<T> const& aB );
181
182template <class T> VECTOR3<T> const operator*( MATRIX3x3<T> const& aA, VECTOR3<T> const& aB );
183
185template <class T, class S> MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, T aScalar );
186template <class T, class S> MATRIX3x3<T> const operator*( T aScalar, MATRIX3x3<T> const& aMatrix );
187
188// ----------------------
189// --- Implementation ---
190// ----------------------
191
192template <class T>
194{
195 for( int j = 0; j < 3; j++ )
196 {
197 for( int i = 0; i < 3; i++ )
198 {
199 m_data[i][j] = 0.0;
200 }
201 }
202}
203
204
205template <class T>
207{
208 m_data[0][0] = a1.x;
209 m_data[0][1] = a1.y;
210 m_data[0][2] = a1.z;
211
212 m_data[1][0] = a2.x;
213 m_data[1][1] = a2.y;
214 m_data[1][2] = a2.z;
215
216 m_data[2][0] = a3.x;
217 m_data[2][1] = a3.y;
218 m_data[2][2] = a3.z;
219}
220
221
222template <class T>
223MATRIX3x3<T>::MATRIX3x3( T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22 )
224{
225 m_data[0][0] = a00;
226 m_data[0][1] = a01;
227 m_data[0][2] = a02;
228
229 m_data[1][0] = a10;
230 m_data[1][1] = a11;
231 m_data[1][2] = a12;
232
233 m_data[2][0] = a20;
234 m_data[2][1] = a21;
235 m_data[2][2] = a22;
236}
237
238
239template <class T>
241{
242 for( int j = 0; j < 3; j++ )
243 {
244 for( int i = 0; i < 3; i++ )
245 {
246 if( i == j )
247 m_data[i][j] = 1.0;
248 else
249 m_data[i][j] = 0.0;
250 }
251 }
252}
253
254
255template <class T>
257{
258 m_data[0][2] = aTranslation.x;
259 m_data[1][2] = aTranslation.y;
260}
261
262
263template <class T>
265{
266 VECTOR2<T> result;
267 result.x = m_data[0][2];
268 result.y = m_data[1][2];
269
270 return result;
271}
272
273
274template <class T>
276{
277 T cosValue = cos( aAngle );
278 T sinValue = sin( aAngle );
279 m_data[0][0] = cosValue;
280 m_data[0][1] = -sinValue;
281 m_data[1][0] = sinValue;
282 m_data[1][1] = cosValue;
283}
284
285
286template <class T>
288{
289 m_data[0][0] = aScale.x;
290 m_data[1][1] = aScale.y;
291}
292
293
294template <class T>
296{
297 VECTOR2<T> result( m_data[0][0], m_data[1][1] );
298
299 return result;
300}
301
302
303template <class T>
304MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aA, MATRIX3x3<T> const& aB )
305{
306 MATRIX3x3<T> result;
307
308 for( int i = 0; i < 3; i++ )
309 {
310 for( int j = 0; j < 3; j++ )
311 {
312 result.m_data[i][j] = aA.m_data[i][0] * aB.m_data[0][j] +
313 aA.m_data[i][1] * aB.m_data[1][j] +
314 aA.m_data[i][2] * aB.m_data[2][j];
315 }
316 }
317
318 return result;
319}
320
321
322template <class T>
323VECTOR2<T> const operator*( MATRIX3x3<T> const& aMatrix, VECTOR2<T> const& aVector )
324{
325 VECTOR2<T> result( 0, 0 );
326 result.x = aMatrix.m_data[0][0] * aVector.x + aMatrix.m_data[0][1] * aVector.y
327 + aMatrix.m_data[0][2];
328 result.y = aMatrix.m_data[1][0] * aVector.x + aMatrix.m_data[1][1] * aVector.y
329 + aMatrix.m_data[1][2];
330
331 return result;
332}
333
334
335template <class T>
336VECTOR3<T> const operator*( MATRIX3x3<T> const& aMatrix, VECTOR3<T> const& aVector )
337{
338 VECTOR3<T> result( 0, 0, 0 );
339 result.x = aMatrix.m_data[0][0] * aVector.x + aMatrix.m_data[0][1] * aVector.y
340 + aMatrix.m_data[0][2] * aVector.z;
341 result.y = aMatrix.m_data[1][0] * aVector.x + aMatrix.m_data[1][1] * aVector.y
342 + aMatrix.m_data[1][2] * aVector.z;
343 result.z = aMatrix.m_data[2][0] * aVector.x + aMatrix.m_data[2][1] * aVector.y
344 + aMatrix.m_data[2][2] * aVector.z;
345
346 return result;
347}
348
349
350template <class T>
352{
353 return m_data[0][0] * ( m_data[1][1] * m_data[2][2] - m_data[1][2] * m_data[2][1] )
354 - m_data[0][1] * ( m_data[1][0] * m_data[2][2] - m_data[1][2] * m_data[2][0] )
355 + m_data[0][2] * ( m_data[1][0] * m_data[2][1] - m_data[1][1] * m_data[2][0] );
356}
357
358
359template <class T, class S>
360MATRIX3x3<T> const operator*( MATRIX3x3<T> const& aMatrix, S aScalar )
361{
362 MATRIX3x3<T> result;
363
364 for( int i = 0; i < 3; i++ )
365 {
366 for( int j = 0; j < 3; j++ )
367 {
368 result.m_data[i][j] = aMatrix.m_data[i][j] * aScalar;
369 }
370 }
371
372 return result;
373}
374
375
376template <class T, class S>
377MATRIX3x3<T> const operator*( S aScalar, MATRIX3x3<T> const& aMatrix )
378{
379 return aMatrix * aScalar;
380}
381
382
383template <class T>
385{
386 MATRIX3x3<T> result;
387
388 result.m_data[0][0] = m_data[1][1] * m_data[2][2] - m_data[2][1] * m_data[1][2];
389 result.m_data[0][1] = m_data[0][2] * m_data[2][1] - m_data[2][2] * m_data[0][1];
390 result.m_data[0][2] = m_data[0][1] * m_data[1][2] - m_data[1][1] * m_data[0][2];
391
392 result.m_data[1][0] = m_data[1][2] * m_data[2][0] - m_data[2][2] * m_data[1][0];
393 result.m_data[1][1] = m_data[0][0] * m_data[2][2] - m_data[2][0] * m_data[0][2];
394 result.m_data[1][2] = m_data[0][2] * m_data[1][0] - m_data[1][2] * m_data[0][0];
395
396 result.m_data[2][0] = m_data[1][0] * m_data[2][1] - m_data[2][0] * m_data[1][1];
397 result.m_data[2][1] = m_data[0][1] * m_data[2][0] - m_data[2][1] * m_data[0][0];
398 result.m_data[2][2] = m_data[0][0] * m_data[1][1] - m_data[1][0] * m_data[0][1];
399
400 return result * ( 1.0 / Determinant() );
401}
402
403
404template <class T>
406{
407 MATRIX3x3<T> result;
408
409 for( int i = 0; i < 3; i++ )
410 {
411 for( int j = 0; j < 3; j++ )
412 {
413 result.m_data[j][i] = m_data[i][j];
414 }
415 }
416
417 return result;
418}
419
420
421template <class T>
422std::ostream& operator<<( std::ostream& aStream, const MATRIX3x3<T>& aMatrix )
423{
424 for( int i = 0; i < 3; i++ )
425 {
426 aStream << "| ";
427
428 for( int j = 0; j < 3; j++ )
429 {
430 aStream << aMatrix.m_data[i][j];
431 aStream << " ";
432 }
433
434 aStream << "|";
435 aStream << "\n";
436 }
437
438 return aStream;
439}
440
441
442template <class T>
443bool MATRIX3x3<T>::operator==( MATRIX3x3<T> const& aOtherMatrix ) const
444{
445 return aOtherMatrix.m_data[0][0] == m_data[0][0] &&
446 aOtherMatrix.m_data[0][1] == m_data[0][1] &&
447 aOtherMatrix.m_data[0][2] == m_data[0][2] &&
448 aOtherMatrix.m_data[1][0] == m_data[1][0] &&
449 aOtherMatrix.m_data[1][1] == m_data[1][1] &&
450 aOtherMatrix.m_data[1][2] == m_data[1][2] &&
451 aOtherMatrix.m_data[2][0] == m_data[2][0] &&
452 aOtherMatrix.m_data[2][1] == m_data[2][1] &&
453 aOtherMatrix.m_data[2][2] == m_data[2][2];
454}
455
456
457template <class T>
458bool MATRIX3x3<T>::operator!=( MATRIX3x3<T> const& aOtherMatrix ) const
459{
460 return aOtherMatrix.m_data[0][0] != m_data[0][0] ||
461 aOtherMatrix.m_data[0][1] != m_data[0][1] ||
462 aOtherMatrix.m_data[0][2] != m_data[0][2] ||
463 aOtherMatrix.m_data[1][0] != m_data[1][0] ||
464 aOtherMatrix.m_data[1][1] != m_data[1][1] ||
465 aOtherMatrix.m_data[1][2] != m_data[1][2] ||
466 aOtherMatrix.m_data[2][0] != m_data[2][0] ||
467 aOtherMatrix.m_data[2][1] != m_data[2][1] ||
468 aOtherMatrix.m_data[2][2] != m_data[2][2];
469}
470
471
472/* Default specializations */
474
475#endif /* MATRIX3X3_H_ */
MATRIX3x3 describes a general 3x3 matrix.
Definition: matrix3x3.h:63
VECTOR2< T > GetTranslation() const
Get the translation components of the matrix.
Definition: matrix3x3.h:264
MATRIX3x3 Transpose() const
Get the transpose of the matrix.
Definition: matrix3x3.h:405
void SetIdentity()
Set the matrix to the identity matrix.
Definition: matrix3x3.h:240
bool operator==(const MATRIX3x3< T > &aOtherMatrix) const
Not equality operator.
Definition: matrix3x3.h:443
void SetRotation(T aAngle)
Set the rotation components of the matrix.
Definition: matrix3x3.h:275
void SetScale(VECTOR2< T > aScale)
Set the scale components of the matrix.
Definition: matrix3x3.h:287
MATRIX3x3(T a00, T a01, T a02, T a10, T a11, T a12, T a20, T a21, T a22)
Initialize with given matrix members.
Definition: matrix3x3.h:223
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition: matrix3x3.h:295
MATRIX3x3()
Initialize all matrix members to zero.
Definition: matrix3x3.h:193
T m_data[3][3]
Definition: matrix3x3.h:65
void SetTranslation(VECTOR2< T > aTranslation)
Set the translation components of the matrix.
Definition: matrix3x3.h:256
bool operator!=(const MATRIX3x3< T > &aOtherMatrix) const
Definition: matrix3x3.h:458
T Determinant() const
Compute the determinant of the matrix.
Definition: matrix3x3.h:351
MATRIX3x3 Inverse() const
Determine the inverse of the matrix.
Definition: matrix3x3.h:384
MATRIX3x3(VECTOR3< T > a1, VECTOR3< T > a2, VECTOR3< T > a3)
Initialize with 3 vectors.
Definition: matrix3x3.h:206
Define a general 2D-vector/point.
Definition: vector2d.h:71
Define a general 3D-vector.
Definition: vector3.h:55
T y
Definition: vector3.h:64
T z
Definition: vector3.h:65
T x
Definition: vector3.h:63
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
Definition: eda_text.cpp:1210
MATRIX3x3< double > MATRIX3x3D
Definition: matrix3x3.h:473
MATRIX3x3< T > const operator*(MATRIX3x3< T > const &aA, MATRIX3x3< T > const &aB)
Matrix multiplication.
Definition: matrix3x3.h:304