Newer
Older

Christian Godenschwager
committed
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file Matrix3.h
//! \ingroup core
//! \author Klaus Iglberger
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//! \brief Implementation of a 3x3 matrix
//
//======================================================================================================================
#pragma once
#include "FPClassify.h"
#include "MathTrait.h"
#include "Vector3.h"
#include "core/debug/Debug.h"
#include "core/mpi/Datatype.h"

Christian Godenschwager
committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "core/mpi/RecvBuffer.h"
#include "core/mpi/SendBuffer.h"
#include <boost/static_assert.hpp>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
namespace walberla {
namespace math {
//**********************************************************************************************************************
// Definitions
//**********************************************************************************************************************
//! High-order return value.
/*! Abbreviation for the evaluation of the higher-order data type in a numerical operation. */
#define HIGH typename MathTrait<Type,Other>::High
//======================================================================================================================
//
// CLASS DEFINITION
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\brief Efficient, generic implementation of a 3x3 matrix.
// \ingroup math
//
// The Matrix3 class is the representation of a 3x3 matrix with a total of 9 statically allocated
// elements of arbitrary type. The naming convention of the elements is as follows:
\f[\left(\begin{array}{*{3}{c}}
xx & xy & xz \\
yx & yy & yz \\
zx & zy & zz \\
\end{array}\right)\f]\n
// These elements can be accessed directly with the 1D subscript operator or with the 2D function
// operator. The numbering of the matrix elements is
\f[\left(\begin{array}{*{3}{c}}
0 & 1 & 2 \\
3 & 4 & 5 \\
6 & 7 & 8 \\
\end{array}\right)\f]
*/
template< typename Type >
class Matrix3
{
private:
//**Friend declarations*************************************************************************
/*! \cond internal */
template< typename Other > friend class Matrix3;
/*! \endcond */
//*******************************************************************************************************************
public:
//**Constructors*****************************************************************************************************
explicit inline Matrix3();
explicit inline Matrix3( Type init );
explicit inline Matrix3( const Vector3<Type>& a, const Vector3<Type>& b, const Vector3<Type>& c );
explicit inline Matrix3( Type xx, Type xy, Type xz, Type yx, Type yy, Type yz, Type zx, Type zy, Type zz );
explicit inline Matrix3( const Type* init );
template< typename Axis, typename Angle >
explicit Matrix3( Vector3<Axis> axis, Angle angle );
inline Matrix3( const Matrix3& m );
template< typename Other >
inline Matrix3( const Matrix3<Other>& m );
inline static Matrix3 makeDiagonalMatrix( const Type xx, const Type yy, const Type zz );
inline static Matrix3 makeDiagonalMatrix( const Type d );
inline static Matrix3 makeIdentityMatrix();
//*******************************************************************************************************************
//**Destructor*******************************************************************************************************
// No explicitly declared destructor.
//*******************************************************************************************************************
//**Operators********************************************************************************************************
/*!\name Operators */
//@{
inline Matrix3& operator= ( Type set );
inline Matrix3& operator= ( const Matrix3& set );
template< typename Other > inline Matrix3& operator= ( const Matrix3<Other>& set );
template< typename Other > inline bool operator==( const Matrix3<Other>& rhs ) const;
template< typename Other > inline bool operator!=( const Matrix3<Other>& rhs ) const;
inline Type& operator[]( uint_t index );
inline const Type& operator[]( uint_t index ) const;
inline Type& operator()( uint_t i, uint_t j );
inline const Type& operator()( uint_t i, uint_t j ) const;
//@}
//*******************************************************************************************************************
//**Arithmetic operators************************************************************************
/*!\name Arithmetic operators
// \brief The return type of the arithmetic operators depends on the involved data types of
// \brief the matrices. HIGH denotes the more significant data type of the arithmetic operation
// \brief (for further detail see the MathTrait class description).
*/
//@{
template< typename Other > inline Matrix3& operator+=( const Matrix3<Other>& rhs );
template< typename Other > inline Matrix3& operator-=( const Matrix3<Other>& rhs );
template< typename Other > inline Matrix3& operator*=( Other rhs );
template< typename Other > inline Matrix3& operator*=( const Matrix3<Other>& rhs );
template< typename Other > inline const Matrix3<HIGH> operator+ ( const Matrix3<Other>& rhs ) const;
template< typename Other > inline const Matrix3<HIGH> operator- ( const Matrix3<Other>& rhs ) const;
template< typename Other > inline const Matrix3<HIGH> operator* ( Other rhs ) const;
template< typename Other > inline const Vector3<HIGH> operator* ( const Vector3<Other>& rhs ) const;
template< typename Other > inline const Matrix3<HIGH> operator* ( const Matrix3<Other>& rhs ) const;
//@}
//*******************************************************************************************************************
//**Utility functions***************************************************************************
/*!\name Utility functions
// \brief The return type of the utility functions depends on the involved data types of the
// \brief matrices. HIGH denotes the more significant data type of the utility operations
// \brief (for further detail see the MathTrait class description).
*/
//@{
inline Type getDeterminant() const;
inline Matrix3& transpose();
inline const Matrix3 getTranspose() const;
inline Matrix3& invert();
inline const Matrix3 getInverse() const;
template< typename Other > inline const Vector3<HIGH> multTranspose( const Vector3<Other>& rhs ) const;
template< typename Other > inline const Matrix3<HIGH> rotate( const Matrix3<Other>& m ) const;
template< typename Other > inline const Matrix3<HIGH> diagRotate( const Matrix3<Other>& m ) const;
inline bool isSingular() const;
inline bool isSymmetric() const;
inline bool isZero() const;
inline const Matrix3 getCholesky() const;
template< typename Other > inline const Vector3<HIGH> solve( const Vector3<Other> &rhs ) const;
inline real_t trace() const;
//@}
//*******************************************************************************************************************
//**Euler rotations*****************************************************************************
//! Order of the Euler rotation
/*! This codes are needed for the EulerAngles function in order to calculate the Euler angles
for a specific combination of rotations. */
enum EulerRotation {
XYZs = 0, //!< Rotation order x, y, z in a static frame.
ZYXr = 1, //!< Rotation order z, y, x in a rotating frame.
XYXs = 2, //!< Rotation order x, y, x in a static frame.
XYXr = 3, //!< Rotation order x, y, z in a rotating frame.
XZYs = 4, //!< Rotation order x, z, y in a static frame.
YZXr = 5, //!< Rotation order y, z, x in a rotating frame.
XZXs = 6, //!< Rotation order x, z, x in a static frame.
XZXr = 7, //!< Rotation order x, z, x in a rotating frame.
YZXs = 8, //!< Rotation order y, z, x in a static frame.
XZYr = 9, //!< Rotation order x, z, y in a rotating frame.
YZYs = 10, //!< Rotation order y, z, y in a static frame.
YZYr = 11, //!< Rotation order y, z, y in a rotating frame.
YXZs = 12, //!< Rotation order y, x, z in a static frame.
ZXYr = 13, //!< Rotation order z, x, y in a rotating frame.
YXYs = 14, //!< Rotation order y, x, y in a static frame.
YXYr = 15, //!< Rotation order y, x, y in a rotating frame.
ZXYs = 16, //!< Rotation order z, x, y in a static frame.
YXZr = 17, //!< Rotation order y, x, z in a rotating frame.
ZXZs = 18, //!< Rotation order z, x, z in a static frame.
ZXZr = 19, //!< Rotation order z, x, z in a rotating frame.
ZYXs = 20, //!< Rotation order z, y, x in a static frame.
XYZr = 21, //!< Rotation order x, y, z in a rotating frame.
ZYZs = 22, //!< Rotation order z, y, z in a static frame.
ZYZr = 23 //!< Rotation order z, y, z in a rotating frame.
};
/*!\name Euler rotations
// \brief For the classification of the Euler rotation, the following characteristics are
// \brief defined:\n
// \brief - Inner axis: the inner axis is the axis of the first rotation matrix multiplied
// \brief to a vector.
// \brief - Parity: the parity is even, if the inner axis X is followed by the middle axis
// \brief Y, or Y is followed by Z, or Z is followed by X; otherwise parity is odd.
// \brief - Repetition: repetition tells, if the first and last axes are the same or different.
// \brief - Frame: the frame refers to the frame from which the Euler angles are calculated.
// \brief
// \brief Altogether, there are 24 possible Euler rotations. The possibilities are consisting
// \brief of the choice of the inner axis (X,Y or Z), the parity (even or odd), repetition
// \brief (yes or no) and the frame (static or rotating). E.g., an Euler order of XYZs stands
// \brief for the rotation order of x-, y- and z-axis in a static frame (inner axis: X, parity:
// \brief even, repetition: no, frame: static), whereas YXYr stands for the rotation order y-,
// \brief x- and y-axis in a rotating frame ( inner axis: Y, parity: odd, repetition: yes,
// \brief frame: rotating).
*/
//@{
const Vector3<Type> getEulerAngles( EulerRotation order ) const;
inline const Vector3<Type> getEulerAnglesXYZ() const;
//@}
//*******************************************************************************************************************
private:
//**Member variables****************************************************************************
/*!\name Member variables */
//@{
Type v_[9]; //!< The nine statically allocated matrix elements.
/*!< Access to the matrix elements is gained via the subscript or function call
operator. The order of the elements is
\f[\left(\begin{array}{*{3}{c}}
0 & 1 & 2 \\
3 & 4 & 5 \\
6 & 7 & 8 \\
\end{array}\right)\f] */
//@}
//*******************************************************************************************************************
};
//**********************************************************************************************************************
//======================================================================================================================
//
// CONSTRUCTORS
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3()
// \brief The default constructor for Matrix3.
//
// The diagonal matrix elements are initialized with 1, all other elements are initialized
// with 0.
*/
template< typename Type >
inline Matrix3<Type>::Matrix3()
{
v_[0] = v_[4] = v_[8] = Type(1);
v_[1] = v_[2] = v_[3] = v_[5] = v_[6] = v_[7] = Type(0);
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( Type init )
// \brief Constructor for a homogeneous initialization of all elements.
//
// \param init Initial value for all matrix elements.
*/
template< typename Type >
inline Matrix3<Type>::Matrix3( Type init )
{
v_[0] = v_[1] = v_[2] = v_[3] = v_[4] = v_[5] = v_[6] = v_[7] = v_[8] = init;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
// \brief Constructor for a direct initialization of all matrix elements
//
// \param a The first column of the matrix.
// \param b The second column of the matrix.
// \param c The third column of the matrix.
//**********************************************************************************************************************
template< typename Type >
inline Matrix3<Type>::Matrix3( const Vector3<Type>& a, const Vector3<Type>& b, const Vector3<Type>& c )
{
v_[0] = a[0]; v_[1] = b[0]; v_[2] = c[0];
v_[3] = a[1]; v_[4] = b[1]; v_[5] = c[1];
v_[6] = a[2]; v_[7] = b[2]; v_[8] = c[2];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( Type xx, Type xy, Type xz, Type yx, Type yy, Type yz, Type zx, Type zy, Type zz )
// \brief Constructor for a direct initialization of all matrix elements
//
// \param xx The initial value for the xx-component.
// \param xy The initial value for the xy-component.
// \param xz The initial value for the xz-component.
// \param yx The initial value for the yx-component.
// \param yy The initial value for the yy-component.
// \param yz The initial value for the yz-component.
// \param zx The initial value for the zx-component.
// \param zy The initial value for the zy-component.
// \param zz The initial value for the zz-component.
*/
template< typename Type >
inline Matrix3<Type>::Matrix3( Type xx, Type xy, Type xz,
Type yx, Type yy, Type yz,
Type zx, Type zy, Type zz )
{
v_[0] = xx; v_[1] = xy; v_[2] = xz;
v_[3] = yx; v_[4] = yy; v_[5] = yz;
v_[6] = zx; v_[7] = zy; v_[8] = zz;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( const Type* init )
// \brief Constructor for an array initializer
//
// \param init Pointer to the initialization array.
//
// The array is assumed to have at least nine valid elements.
*/
template< typename Type >
inline Matrix3<Type>::Matrix3( const Type* init )
{
v_[0] = init[0];
v_[1] = init[1];
v_[2] = init[2];
v_[3] = init[3];
v_[4] = init[4];
v_[5] = init[5];
v_[6] = init[6];
v_[7] = init[7];
v_[8] = init[8];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( Vector3<Axis> axis, Angle angle )
// \brief Rotation matrix constructor.
//
// \param axis The rotation axis.
// \param angle The rotation angle.
//
// This constructor is only defined for floating point vectors. The attempt to use this
// constructor for vectors of integral data type results in a compile time error.
*/
template< typename Type >
template< typename Axis, typename Angle >
Matrix3<Type>::Matrix3( Vector3<Axis> axis, Angle angle )
{
BOOST_STATIC_ASSERT( !std::numeric_limits<Type>::is_integer );
BOOST_STATIC_ASSERT( !std::numeric_limits<Axis>::is_integer );
BOOST_STATIC_ASSERT( !std::numeric_limits<Angle>::is_integer );
const Angle sina( std::sin(angle) );
const Angle cosa( std::cos(angle) );
const Angle tmp( Angle(1)-cosa );
normalize(axis);
v_[0] = cosa + axis[0]*axis[0]*tmp;
v_[1] = axis[0]*axis[1]*tmp - axis[2]*sina;
v_[2] = axis[0]*axis[2]*tmp + axis[1]*sina;
v_[3] = axis[1]*axis[0]*tmp + axis[2]*sina;
v_[4] = cosa + axis[1]*axis[1]*tmp;
v_[5] = axis[1]*axis[2]*tmp - axis[0]*sina;
v_[6] = axis[2]*axis[0]*tmp - axis[1]*sina;
v_[7] = axis[2]*axis[1]*tmp + axis[0]*sina;
v_[8] = cosa + axis[2]*axis[2]*tmp;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( const Matrix3& m )
// \brief The copy constructor for Matrix3.
//
// \param m Matrix to be copied.
//
// The copy constructor is explicitly defined in order to enable/facilitate NRV optimization.
*/
template< typename Type >
inline Matrix3<Type>::Matrix3( const Matrix3& m )
{
v_[0] = m.v_[0];
v_[1] = m.v_[1];
v_[2] = m.v_[2];
v_[3] = m.v_[3];
v_[4] = m.v_[4];
v_[5] = m.v_[5];
v_[6] = m.v_[6];
v_[7] = m.v_[7];
v_[8] = m.v_[8];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>::Matrix3( const Matrix3<Other>& m )
// \brief Conversion constructor from different Matrix3 instances.
//
// \param m Matrix to be copied.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>::Matrix3( const Matrix3<Other>& m )
{
v_[0] = m.v_[0];
v_[1] = m.v_[1];
v_[2] = m.v_[2];
v_[3] = m.v_[3];
v_[4] = m.v_[4];
v_[5] = m.v_[5];
v_[6] = m.v_[6];
v_[7] = m.v_[7];
v_[8] = m.v_[8];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type xx, const Type yy, const Type zz )
// \brief Named constructor to create a diagonal matrix. All non-diagonal elements are initialized with zero.
//
// \param xx value for element (0,0).
// \param yy value for element (1,1).
// \param zz value for element (2,2).
*/
template< typename Type >
Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type xx, const Type yy, const Type zz )
{
return Matrix3<Type>( xx, Type(), Type(), Type(), yy, Type(), Type(), Type(), zz );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type d )
// \brief Named constructor to create a diagonal matrix. All non-diagonal elements are initialized with zero.
//
// \param d value for diagonal elements.
*/
template< typename Type >
Matrix3<Type> Matrix3<Type>::makeDiagonalMatrix( const Type d )
{
return makeDiagonalMatrix( d, d, d );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type> Matrix3<Type>::makeIdentityMatrix()
// \brief Named constructor to create the identity matrix.
//
// All diagonal elements are initialized to one, alls others to zero.
//
*/
template< typename Type >
Matrix3<Type> Matrix3<Type>::makeIdentityMatrix()
{
return makeDiagonalMatrix( Type(1) );
}
//**********************************************************************************************************************
//======================================================================================================================
//
// OPERATORS
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator=( Type set )
// \brief Homogenous assignment to all matrix elements.
//
// \param set Scalar value to be assigned to all matrix elements.
// \return Reference to the assigned matrix.
*/
template< typename Type >
inline Matrix3<Type>& Matrix3<Type>::operator=( Type set )
{
v_[0] = set;
v_[1] = set;
v_[2] = set;
v_[3] = set;
v_[4] = set;
v_[5] = set;
v_[6] = set;
v_[7] = set;
v_[8] = set;
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator=( const Matrix3& set )
// \brief Copy assignment operator for Matrix3.
//
// \param set Matrix to be copied.
// \return Reference to the assigned matrix.
//
// Explicit definition of a copy assignment operator for performance reasons.
*/
template< typename Type >
inline Matrix3<Type>& Matrix3<Type>::operator=( const Matrix3& set )
{
// This implementation is faster than the synthesized default copy assignment operator and
// faster than an implementation with the C library function 'memcpy' in combination with a
// protection against self-assignment. Additionally, this version goes without a protection
// against self-assignment.
v_[0] = set.v_[0];
v_[1] = set.v_[1];
v_[2] = set.v_[2];
v_[3] = set.v_[3];
v_[4] = set.v_[4];
v_[5] = set.v_[5];
v_[6] = set.v_[6];
v_[7] = set.v_[7];
v_[8] = set.v_[8];
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator=( const Matrix3<Other>& set )
// \brief Assignment operator for different Matrix3 instances.
//
// \param set Matrix to be copied.
// \return Reference to the assigned matrix.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>& Matrix3<Type>::operator=( const Matrix3<Other>& set )
{
// This implementation is faster than the synthesized default copy assignment operator and
// faster than an implementation with the C library function 'memcpy' in combination with a
// protection against self-assignment. Additionally, this version goes without a protection
// against self-assignment.
v_[0] = set.v_[0];
v_[1] = set.v_[1];
v_[2] = set.v_[2];
v_[3] = set.v_[3];
v_[4] = set.v_[4];
v_[5] = set.v_[5];
v_[6] = set.v_[6];
v_[7] = set.v_[7];
v_[8] = set.v_[8];
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn bool Matrix3<Type>::operator==( const Matrix3<Other>& rhs ) const
// \brief Equality operator for the comparison of two matrices.
//
// \param rhs The right-hand-side matrix for the comparison.
// \return bool
*/
template< typename Type >
template< typename Other >
inline bool Matrix3<Type>::operator==( const Matrix3<Other>& rhs ) const
{
// In order to compare the vector and the scalar value, the data values of the lower-order
// data type are converted to the higher-order data type.
if( !equal( v_[0], rhs.v_[0] ) ||
!equal( v_[1], rhs.v_[1] ) ||
!equal( v_[2], rhs.v_[2] ) ||
!equal( v_[3], rhs.v_[3] ) ||
!equal( v_[4], rhs.v_[4] ) ||
!equal( v_[5], rhs.v_[5] ) ||
!equal( v_[6], rhs.v_[6] ) ||
!equal( v_[7], rhs.v_[7] ) ||
!equal( v_[8], rhs.v_[8] ) )
return false;
else return true;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn bool Matrix3<Type>::operator!=( const Matrix3<Other>& rhs ) const
// \brief Inequality operator for the comparison of two matrices.
//
// \param rhs The right-hand-side matrix for the comparison.
// \return bool
*/
template< typename Type >
template< typename Other >
inline bool Matrix3<Type>::operator!=( const Matrix3<Other>& rhs ) const
{
// In order to compare the vector and the scalar value, the data values of the lower-order
// data type are converted to the higher-order data type.
if( !equal( v_[0], rhs.v_[0] ) ||
!equal( v_[1], rhs.v_[1] ) ||
!equal( v_[2], rhs.v_[2] ) ||
!equal( v_[3], rhs.v_[3] ) ||
!equal( v_[4], rhs.v_[4] ) ||
!equal( v_[5], rhs.v_[5] ) ||
!equal( v_[6], rhs.v_[6] ) ||
!equal( v_[7], rhs.v_[7] ) ||
!equal( v_[8], rhs.v_[8] ) )
return true;
else return false;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Type& Matrix3<Type>::operator[]( uint_t index )
// \brief 1D-access to the matrix elements.
//
// \param index Access index. The index has to be in the range \f$[0..8]\f$.
// \return Reference to the accessed value.
*/
template< typename Type >
inline Type& Matrix3<Type>::operator[]( uint_t index )
{
WALBERLA_ASSERT_LESS( index, 9 ,"Invalid matrix access index" );
return v_[index];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Type& Matrix3<Type>::operator[]( uint_t index ) const
// \brief 1D-access to the matrix elements.
//
// \param index Access index. The index has to be in the range \f$[0..8]\f$.
// \return Reference-to-const to the accessed value.
*/
template< typename Type >
inline const Type& Matrix3<Type>::operator[]( uint_t index ) const
{
WALBERLA_ASSERT_LESS( index, 9 ,"Invalid matrix access index" );
return v_[index];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Type& Matrix3<Type>::operator()( uint_t i, uint_t j )
// \brief 2D-access to the matrix elements.
//
// \param i Access index for the row. The index has to be in the range [0..2].
// \param j Access index for the column. The index has to be in the range [0..2].
// \return Reference to the accessed value.
*/
template< typename Type >
inline Type& Matrix3<Type>::operator()( uint_t i, uint_t j )
{
WALBERLA_ASSERT( i<3 && j<3,"Invalid matrix access index" );
return v_[i*3+j];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Type& Matrix3<Type>::operator()( uint_t i, uint_t j ) const
// \brief 2D-access to the matrix elements.
//
// \param i Access index for the row. The index has to be in the range [0..2].
// \param j Access index for the column. The index has to be in the range [0..2].
// \return Reference-to-const to the accessed value.
*/
template< typename Type >
inline const Type& Matrix3<Type>::operator()( uint_t i, uint_t j ) const
{
WALBERLA_ASSERT( i<3 && j<3 ,"Invalid matrix access index" );
return v_[i*3+j];
}
//**********************************************************************************************************************
//======================================================================================================================
//
// ARITHMETIC OPERATORS
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator+=( const Matrix3<Other>& rhs )
// \brief Addition assignment operator for the addition of two matrices (\f$ A+=B \f$).
//
// \param rhs The right-hand-side matrix to be added to the matrix.
// \return Reference to the matrix.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>& Matrix3<Type>::operator+=( const Matrix3<Other>& rhs )
{
v_[0] += rhs.v_[0];
v_[1] += rhs.v_[1];
v_[2] += rhs.v_[2];
v_[3] += rhs.v_[3];
v_[4] += rhs.v_[4];
v_[5] += rhs.v_[5];
v_[6] += rhs.v_[6];
v_[7] += rhs.v_[7];
v_[8] += rhs.v_[8];
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator-=( const Matrix3<Other>& rhs )
// \brief Subtraction assignment operator for the subtraction of two matrices (\f$ A-=B \f$).
//
// \param rhs The right-hand-side matrix to be subtracted from the matrix.
// \return Reference to the matrix.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>& Matrix3<Type>::operator-=( const Matrix3<Other>& rhs )
{
v_[0] -= rhs.v_[0];
v_[1] -= rhs.v_[1];
v_[2] -= rhs.v_[2];
v_[3] -= rhs.v_[3];
v_[4] -= rhs.v_[4];
v_[5] -= rhs.v_[5];
v_[6] -= rhs.v_[6];
v_[7] -= rhs.v_[7];
v_[8] -= rhs.v_[8];
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator*=( Other rhs )
// \brief Multiplication assignment operator for the multiplication between a matrix and
// \brief a scalar value (\f$ A*=s \f$).
//
// \param rhs The right-hand-side scalar value for the multiplication.
// \return Reference to the matrix.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>& Matrix3<Type>::operator*=( Other rhs )
{
v_[0] *= rhs;
v_[1] *= rhs;
v_[2] *= rhs;
v_[3] *= rhs;
v_[4] *= rhs;
v_[5] *= rhs;
v_[6] *= rhs;
v_[7] *= rhs;
v_[8] *= rhs;
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::operator*=( const Matrix3<Other>& rhs )
// \brief Multiplication assignment operator for the multiplication between two matrices
// \brief (\f$ A*=B \f$).
//
// \param rhs The right-hand-side matrix for the multiplication.
// \return Reference to the matrix.
*/
template< typename Type >
template< typename Other >
inline Matrix3<Type>& Matrix3<Type>::operator*=( const Matrix3<Other>& rhs )
{
// Creating a temporary due to data dependencies
Matrix3 tmp( v_[0]*rhs.v_[0] + v_[1]*rhs.v_[3] + v_[2]*rhs.v_[6],
v_[0]*rhs.v_[1] + v_[1]*rhs.v_[4] + v_[2]*rhs.v_[7],
v_[0]*rhs.v_[2] + v_[1]*rhs.v_[5] + v_[2]*rhs.v_[8],
v_[3]*rhs.v_[0] + v_[4]*rhs.v_[3] + v_[5]*rhs.v_[6],
v_[3]*rhs.v_[1] + v_[4]*rhs.v_[4] + v_[5]*rhs.v_[7],
v_[3]*rhs.v_[2] + v_[4]*rhs.v_[5] + v_[5]*rhs.v_[8],
v_[6]*rhs.v_[0] + v_[7]*rhs.v_[3] + v_[8]*rhs.v_[6],
v_[6]*rhs.v_[1] + v_[7]*rhs.v_[4] + v_[8]*rhs.v_[7],
v_[6]*rhs.v_[2] + v_[7]*rhs.v_[5] + v_[8]*rhs.v_[8] );
return this->operator=( tmp );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<HIGH> Matrix3<Type>::operator+( const Matrix3<Other>& rhs ) const
// \brief Addition operator for the addition of two matrices (\f$ A=B+C \f$).
//
// \param rhs The right-hand-side matrix to be added to the matrix.
// \return The sum of the two matrices.
*/
template< typename Type >
template< typename Other >
inline const Matrix3<HIGH> Matrix3<Type>::operator+( const Matrix3<Other>& rhs ) const
{
return Matrix3<HIGH>( v_[0] + rhs.v_[0],
v_[1] + rhs.v_[1],
v_[2] + rhs.v_[2],
v_[3] + rhs.v_[3],
v_[4] + rhs.v_[4],
v_[5] + rhs.v_[5],
v_[6] + rhs.v_[6],
v_[7] + rhs.v_[7],
v_[8] + rhs.v_[8] );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<HIGH> Matrix3<Type>::operator-( const Matrix3<Other>& rhs ) const
// \brief Subtraction operator for the subtraction of two matrices (\f$ A=B-C \f$).
//
// \param rhs The right-hand-side matrix to be subtracted from the matrix.
// \return The difference of the two matrices.
*/
template< typename Type >
template< typename Other >
inline const Matrix3<HIGH> Matrix3<Type>::operator-( const Matrix3<Other>& rhs ) const
{
return Matrix3<HIGH>( v_[0] - rhs.v_[0],
v_[1] - rhs.v_[1],
v_[2] - rhs.v_[2],
v_[3] - rhs.v_[3],
v_[4] - rhs.v_[4],
v_[5] - rhs.v_[5],
v_[6] - rhs.v_[6],
v_[7] - rhs.v_[7],
v_[8] - rhs.v_[8] );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<Type> operator-( const Matrix3<Type>& rhs )
// \brief Negation operator for the negation of one matrix.
//
// \param rhs The right-hand-side matrix of the operator.
// \return The negative matrix.
*/
template< typename Type >
inline const Matrix3<Type> operator-( const Matrix3<Type>& rhs )
{
return Matrix3<Type>( -rhs[0],
-rhs[1],
-rhs[2],
-rhs[3],
-rhs[4],
-rhs[5],
-rhs[6],
-rhs[7],
-rhs[8] );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<HIGH> Matrix3<Type>::operator*( Other rhs ) const
// \brief Multiplication operator for the multiplication of a matrix and a scalar value
// \brief (\f$ A=B*s \f$).
//
// \param rhs The right-hand-side scalar value for the multiplication.
// \return The scaled result matrix.
*/
template< typename Type >
template< typename Other >
inline const Matrix3<HIGH> Matrix3<Type>::operator*( Other rhs ) const
{
return Matrix3<HIGH>( v_[0]*rhs, v_[1]*rhs, v_[2]*rhs,
v_[3]*rhs, v_[4]*rhs, v_[5]*rhs,
v_[6]*rhs, v_[7]*rhs, v_[8]*rhs );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Vector3<HIGH> Matrix3<Type>::operator*( const Vector3<Other>& rhs ) const
// \brief Multiplication operator for the multiplication of a matrix and a vector
// \brief (\f$ \vec{a}=B*\vec{c} \f$).
//
// \param rhs The right-hand-side vector for the multiplication.
// \return The resulting vector.
*/
template< typename Type >
template< typename Other >
inline const Vector3<HIGH> Matrix3<Type>::operator*( const Vector3<Other>& rhs ) const
{
return Vector3<HIGH>( v_[0]*rhs[0] + v_[1]*rhs[1] + v_[2]*rhs[2],
v_[3]*rhs[0] + v_[4]*rhs[1] + v_[5]*rhs[2],
v_[6]*rhs[0] + v_[7]*rhs[1] + v_[8]*rhs[2] );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<HIGH> Matrix3<Type>::operator*( const Matrix3<Other>& rhs ) const
// \brief Multiplication operator for the multiplication of two matrices (\f$ A=B*C \f$).
//
// \param rhs The right-hand-side matrix for the multiplication.
// \return The resulting matrix.
*/
template< typename Type >
template< typename Other >
inline const Matrix3<HIGH> Matrix3<Type>::operator*( const Matrix3<Other>& rhs ) const
{
return Matrix3<HIGH>( v_[0]*rhs.v_[0] + v_[1]*rhs.v_[3] + v_[2]*rhs.v_[6],
v_[0]*rhs.v_[1] + v_[1]*rhs.v_[4] + v_[2]*rhs.v_[7],
v_[0]*rhs.v_[2] + v_[1]*rhs.v_[5] + v_[2]*rhs.v_[8],
v_[3]*rhs.v_[0] + v_[4]*rhs.v_[3] + v_[5]*rhs.v_[6],
v_[3]*rhs.v_[1] + v_[4]*rhs.v_[4] + v_[5]*rhs.v_[7],
v_[3]*rhs.v_[2] + v_[4]*rhs.v_[5] + v_[5]*rhs.v_[8],
v_[6]*rhs.v_[0] + v_[7]*rhs.v_[3] + v_[8]*rhs.v_[6],
v_[6]*rhs.v_[1] + v_[7]*rhs.v_[4] + v_[8]*rhs.v_[7],
v_[6]*rhs.v_[2] + v_[7]*rhs.v_[5] + v_[8]*rhs.v_[8] );
}
//**********************************************************************************************************************
//======================================================================================================================
//
// UTILITY FUNCTIONS
//
//======================================================================================================================
//**********************************************************************************************************************
/*!\fn Type Matrix3<Type>::getDeterminant() const
// \brief Calculation of the determinant of the matrix.
//
// \return The determinant of the matrix.
*/
template< typename Type >
inline Type Matrix3<Type>::getDeterminant() const
{
return v_[0]*v_[4]*v_[8] + v_[1]*v_[5]*v_[6] + v_[2]*v_[3]*v_[7] -
v_[6]*v_[4]*v_[2] - v_[7]*v_[5]*v_[0] - v_[8]*v_[3]*v_[1];
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::transpose()
// \brief Transposing the matrix.
//
// \return Reference to the transposed matrix.
*/
template< typename Type >
inline Matrix3<Type>& Matrix3<Type>::transpose()
{
std::swap( v_[1], v_[3] );
std::swap( v_[2], v_[6] );
std::swap( v_[5], v_[7] );
return *this;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn const Matrix3<Type> Matrix3<Type>::getTranspose() const
// \brief Calculation of the transpose of the matrix.
//
// \return The transpose of the matrix.
*/
template< typename Type >
inline const Matrix3<Type> Matrix3<Type>::getTranspose() const
{
return Matrix3( v_[0], v_[3], v_[6], v_[1], v_[4], v_[7], v_[2], v_[5], v_[8] );
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*!\fn Matrix3<Type>& Matrix3<Type>::invert()
// \brief Inverting the matrix.
//
// \return Reference to the inverted matrix.
//
// The calculation is performed with the matrix inversion by Cramer. This function is only
// defined for matrices of floating point type. The attempt to use this function with matrices