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