-
Notifications
You must be signed in to change notification settings - Fork 299
/
Mat.h
1378 lines (1190 loc) · 38.4 KB
/
Mat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once
#include <sofa/type/config.h>
#include <sofa/type/fwd.h>
#include <sofa/type/fixed_array.h>
#include <sofa/type/Vec.h>
#include <iostream>
namespace // anonymous
{
template<typename real>
real rabs(const real r)
{
if constexpr (std::is_signed<real>())
return std::abs(r);
else
return r;
}
template<typename real>
bool equalsZero(const real r, const real epsilon = std::numeric_limits<real>::epsilon())
{
return rabs(r) <= epsilon;
}
} // anonymous namespace
namespace sofa::type
{
template <sofa::Size L, sofa::Size C, sofa::Size P, class real>
constexpr Mat<C,P,real> multTranspose(const Mat<L,C,real>& m1, const Mat<L,P,real>& m2) noexcept;
template <sofa::Size L, sofa::Size C, class real>
class Mat
{
public:
static constexpr sofa::Size N = L * C;
typedef VecNoInit<C, real> LineNoInit;
using ArrayLineType = std::array<LineNoInit, L>;
typedef real Real;
typedef Vec<C,real> Line;
typedef Vec<L,real> Col;
typedef sofa::Size Size;
static constexpr Size nbLines = L;
static constexpr Size nbCols = C;
typedef sofa::Size size_type;
typedef Line value_type;
typedef typename ArrayLineType::iterator iterator;
typedef typename ArrayLineType::const_iterator const_iterator;
typedef typename ArrayLineType::reference reference;
typedef typename ArrayLineType::const_reference const_reference;
typedef std::ptrdiff_t difference_type;
static constexpr sofa::Size static_size = L;
static constexpr sofa::Size total_size = L;
static constexpr sofa::Size size() { return static_size; }
ArrayLineType elems{};
constexpr Mat() noexcept = default;
explicit constexpr Mat(NoInit) noexcept
{
}
/// Constructs a 1xC matrix (single-row, multiple columns) or a Lx1 matrix (multiple row, single
/// column) and initializes it from a scalar initializer-list.
/// Allows to build a matrix with the following syntax:
/// sofa::type::Mat<1, 3, int> M {1, 2, 3}
/// or
/// sofa::type::Mat<3, 1, int> M {1, 2, 3}
/// Initializer-list must match matrix column size, otherwise an assert is triggered.
template<sofa::Size TL = L, sofa::Size TC = C, typename = std::enable_if_t<(TL == 1 && TC != 1) || (TC == 1 && TL != 1)> >
constexpr Mat(std::initializer_list<Real>&& scalars) noexcept
{
if constexpr (L == 1 && C != 1)
{
assert(scalars.size() == C);
sofa::Size colId {};
for (auto scalar : scalars)
{
this->elems[0][colId++] = scalar;
}
}
else
{
assert(scalars.size() == L);
sofa::Size rowId {};
for (auto scalar : scalars)
{
this->elems[rowId++][0] = scalar;
}
}
}
/// Constructs a matrix and initializes it from scalar initializer-lists grouped by row.
/// Allows to build a matrix with the following syntax:
/// sofa::type::Mat<3, 3, int> M {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
/// Initializer-lists must match matrix size, otherwise an assert is triggered.
constexpr Mat(std::initializer_list<std::initializer_list<Real>>&& rows) noexcept
{
assert(rows.size() == L);
sofa::Size rowId {};
for (const auto& row : rows)
{
assert(row.size() == C);
sofa::Size colId {};
for (auto scalar : row)
{
this->elems[rowId][colId++] = scalar;
}
++rowId;
}
}
template<typename... ArgsT,
typename = std::enable_if_t< (std::is_convertible_v<ArgsT, Line> && ...) >,
typename = std::enable_if_t< (sizeof...(ArgsT) == L && sizeof...(ArgsT) > 1) >
>
constexpr Mat(ArgsT&&... r) noexcept
: elems{ std::forward<ArgsT>(r)... }
{}
/// Constructor from an element
explicit constexpr Mat(const real& v) noexcept
{
for( Size i=0; i<L; i++ )
for( Size j=0; j<C; j++ )
this->elems[i][j] = v;
}
/// Constructor from another matrix
template<typename real2>
constexpr Mat(const Mat<L,C,real2>& m) noexcept
{
std::copy(m.begin(), m.begin()+L, this->begin());
}
/// Constructor from another matrix with different size (with null default entries and ignoring outside entries)
template<Size L2, Size C2, typename real2>
explicit constexpr Mat(const Mat<L2,C2,real2>& m) noexcept
{
constexpr Size minL = std::min( L, L2 );
constexpr Size minC = std::min( C, C2 );
for( Size l=0 ; l<minL ; ++l )
{
for( Size c=0 ; c< minC; ++c )
this->elems[l][c] = static_cast<real>(m[l][c]);
for( Size c= minC; c<C ; ++c )
this->elems[l][c] = 0;
}
for( Size l= minL; l<L ; ++l )
for( Size c=0 ; c<C ; ++c )
this->elems[l][c] = 0;
}
/// Constructor from an array of elements (stored per line).
template<typename real2>
explicit constexpr Mat(const real2* p) noexcept
{
std::copy(p, p+N, this->begin()->begin());
}
/// number of lines
constexpr Size getNbLines() const
{
return L;
}
/// number of colums
constexpr Size getNbCols() const
{
return C;
}
/// Assignment from an array of elements (stored per line).
constexpr void operator=(const real* p) noexcept
{
std::copy(p, p+N, this->begin()->begin());
}
/// Assignment from another matrix
template<typename real2>
constexpr void operator=(const Mat<L,C,real2>& m) noexcept
{
std::copy(m.begin(), m.begin()+L, this->begin());
}
/// Assignment from a matrix of different size.
template<Size L2, Size C2>
constexpr void operator=(const Mat<L2,C2,real>& m) noexcept
{
std::copy(m.begin(), m.begin()+(L>L2?L2:L), this->begin());
}
template<Size L2, Size C2>
constexpr void getsub(Size L0, Size C0, Mat<L2,C2,real>& m) const noexcept
{
for (Size i=0; i<L2; i++)
for (Size j=0; j<C2; j++)
m[i][j] = this->elems[i+L0][j+C0];
}
template <Size C2>
constexpr void getsub(const Size L0, const Size C0, Vec<C2, real>& m) const noexcept
{
for (Size j = 0; j < C2; j++)
{
m[j] = this->elems[L0][j + C0];
}
}
constexpr void getsub(Size L0, Size C0, real& m) const noexcept
{
m = this->elems[L0][C0];
}
template<Size L2, Size C2>
constexpr void setsub(Size L0, Size C0, const Mat<L2,C2,real>& m) noexcept
{
for (Size i=0; i<L2; i++)
for (Size j=0; j<C2; j++)
this->elems[i+L0][j+C0] = m[i][j];
}
template<Size L2>
constexpr void setsub(Size L0, Size C0, const Vec<L2,real>& v) noexcept
{
assert( C0<C );
assert( L0+L2-1<L );
for (Size i=0; i<L2; i++)
this->elems[i+L0][C0] = v[i];
}
/// Sets each element to 0.
constexpr void clear() noexcept
{
for (Size i=0; i<L; i++)
this->elems[i].clear();
}
/// Sets each element to r.
constexpr void fill(real r) noexcept
{
for (Size i=0; i<L; i++)
this->elems[i].fill(r);
}
/// Read-only access to line i.
constexpr const Line& line(Size i) const noexcept
{
return this->elems[i];
}
/// Copy of column j.
constexpr Col col(Size j) const noexcept
{
Col c;
for (Size i=0; i<L; i++)
c[i]=this->elems[i][j];
return c;
}
/// Write acess to line i.
constexpr LineNoInit& operator[](Size i) noexcept
{
return this->elems[i];
}
/// Read-only access to line i.
constexpr const LineNoInit& operator[](Size i) const noexcept
{
return this->elems[i];
}
/// Write acess to line i.
constexpr LineNoInit& operator()(Size i) noexcept
{
return this->elems[i];
}
/// Read-only access to line i.
constexpr const LineNoInit& operator()(Size i) const noexcept
{
return this->elems[i];
}
/// Write access to element (i,j).
constexpr real& operator()(Size i, Size j) noexcept
{
return this->elems[i][j];
}
/// Read-only access to element (i,j).
constexpr const real& operator()(Size i, Size j) const noexcept
{
return this->elems[i][j];
}
/// Cast into a standard C array of lines (read-only).
constexpr const Line* lptr() const noexcept
{
return this->elems;
}
/// Cast into a standard C array of lines.
constexpr Line* lptr() noexcept
{
return this->elems;
}
/// Cast into a standard C array of elements (stored per line) (read-only).
constexpr const real* ptr() const noexcept
{
return this->elems[0].ptr();
}
/// Cast into a standard C array of elements (stored per line).
constexpr real* ptr() noexcept
{
return this->elems[0].ptr();
}
/// Special access to first line.
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 1> >
constexpr Line& x() noexcept { return this->elems[0]; }
/// Special access to second line.
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 2> >
constexpr Line& y() noexcept { return this->elems[1]; }
/// Special access to third line.
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 3> >
constexpr Line& z() noexcept { return this->elems[2]; }
/// Special access to fourth line.
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 4> >
constexpr Line& w() noexcept { return this->elems[3]; }
/// Special access to first line (read-only).
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 1> >
constexpr const Line& x() const noexcept { return this->elems[0]; }
/// Special access to second line (read-only).
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 2> >
constexpr const Line& y() const noexcept { return this->elems[1]; }
/// Special access to thrid line (read-only).
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 3> >
constexpr const Line& z() const noexcept { return this->elems[2]; }
/// Special access to fourth line (read-only).
template<sofa::Size NbLine = L, typename = std::enable_if_t<NbLine >= 4> >
constexpr const Line& w() const noexcept { return this->elems[3]; }
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == 1 && NbColumn == 1>>
constexpr real toReal() const { return this->elems[0][0]; }
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == 1 && NbColumn == 1>>
constexpr operator real() const { return toReal(); }
/// Set matrix to identity.
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
constexpr void identity() noexcept
{
clear();
for (Size i=0; i<L; i++)
this->elems[i][i]=1;
}
/// Returns the identity matrix
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
static const Mat<L,L,real>& Identity() noexcept
{
static Mat<L,L,real> s_identity = []()
{
Mat<L,L,real> id(NOINIT);
id.identity();
return id;
}();
return s_identity;
}
template<Size S>
static bool canSelfTranspose(const Mat<S, S, real>& lhs, const Mat<S, S, real>& rhs) noexcept
{
return &lhs == &rhs;
}
template<Size I, Size J>
static bool canSelfTranspose(const Mat<I, J, real>& /*lhs*/, const Mat<J, I, real>& /*rhs*/) noexcept
{
return false;
}
/// Set matrix as the transpose of m.
constexpr void transpose(const Mat<C,L,real> &m) noexcept
{
if (canSelfTranspose(*this, m))
{
for (Size i=0; i<L; i++)
{
for (Size j=i+1; j<C; j++)
{
std::swap(this->elems[i][j], this->elems[j][i]);
}
}
}
else
{
for (Size i=0; i<L; i++)
for (Size j=0; j<C; j++)
this->elems[i][j]=m[j][i];
}
}
/// Return the transpose of m.
constexpr Mat<C,L,real> transposed() const noexcept
{
Mat<C,L,real> m(NOINIT);
for (Size i=0; i<L; i++)
for (Size j=0; j<C; j++)
m[j][i]=this->elems[i][j];
return m;
}
/// Transpose the square matrix.
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
constexpr void transpose() noexcept
{
for (Size i=0; i<L; i++)
{
for (Size j=i+1; j<C; j++)
{
std::swap(this->elems[i][j], this->elems[j][i]);
}
}
}
/// @name Tests operators
/// @{
constexpr bool operator==(const Mat<L,C,real>& b) const noexcept
{
for (Size i=0; i<L; i++)
if (this->elems[i] != b[i]) return false;
return true;
}
constexpr bool operator!=(const Mat<L,C,real>& b) const noexcept
{
for (Size i=0; i<L; i++)
if (this->elems[i]!=b[i]) return true;
return false;
}
[[nodiscard]] bool isSymmetric() const
{
if constexpr (L == C)
{
for (Size i=0; i<L; i++)
for (Size j=i+1; j<C; j++)
if( rabs( this->elems[i][j] - this->elems[j][i] ) > EQUALITY_THRESHOLD ) return false;
return true;
}
else
{
return false;
}
}
bool isDiagonal() const noexcept
{
for (Size i=0; i<L; i++)
{
for (Size j=0; j<i-1; j++)
if( rabs( this->elems[i][j] ) > EQUALITY_THRESHOLD ) return false;
for (Size j=i+1; j<C; j++)
if( rabs( this->elems[i][j] ) > EQUALITY_THRESHOLD ) return false;
}
return true;
}
/// @}
// LINEAR ALGEBRA
/// Matrix addition operator.
constexpr Mat<L,C,real> operator+(const Mat<L,C,real>& m) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i = 0; i < L; i++)
r[i] = (*this)[i] + m[i];
return r;
}
/// Matrix subtraction operator.
constexpr Mat<L,C,real> operator-(const Mat<L,C,real>& m) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i = 0; i < L; i++)
r[i] = (*this)[i] - m[i];
return r;
}
/// Matrix negation operator.
constexpr Mat<L,C,real> operator-() const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i = 0; i < L; i++)
r[i] = -(*this)[i];
return r;
}
/// Multiplication operator Matrix * Line.
constexpr Col operator*(const Line& v) const noexcept
{
Col r(NOINIT);
for(Size i=0; i<L; i++)
{
r[i]=(*this)[i][0] * v[0];
for(Size j=1; j<C; j++)
r[i] += (*this)[i][j] * v[j];
}
return r;
}
/// Multiplication with a diagonal Matrix CxC represented as a vector of size C
constexpr Mat<L,C,real> multDiagonal(const Line& d) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
r[i][j]=(*this)[i][j] * d[j];
return r;
}
/// Multiplication of the transposed Matrix * Column
constexpr Line multTranspose(const Col& v) const noexcept
{
Line r(NOINIT);
for(Size i=0; i<C; i++)
{
r[i]=(*this)[0][i] * v[0];
for(Size j=1; j<L; j++)
r[i] += (*this)[j][i] * v[j];
}
return r;
}
/// Transposed Matrix multiplication operator.
/// Result = (*this)^T * m
/// Sizes: [L,C]^T * [L,P] = [C,L] * [L,P] = [C,P]
template <Size P>
constexpr Mat<C,P,real> multTranspose(const Mat<L,P,real>& m) const noexcept
{
return ::sofa::type::multTranspose(*this, m);
}
/// Multiplication with the transposed of the given matrix operator \returns this * mt
template <Size P>
constexpr Mat<L,P,real> multTransposed(const Mat<P,C,real>& m) const noexcept
{
Mat<L,P,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<P; j++)
{
r[i][j]=(*this)[i][0] * m[j][0];
for(Size k=1; k<C; k++)
r[i][j] += (*this)[i][k] * m[j][k];
}
return r;
}
/// Addition with the transposed of the given matrix operator \returns this + mt
constexpr Mat<L,C,real> plusTransposed(const Mat<C,L,real>& m) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
r[i][j] = (*this)[i][j] + m[j][i];
return r;
}
/// Substraction with the transposed of the given matrix operator \returns this - mt
constexpr Mat<L,C,real>minusTransposed(const Mat<C,L,real>& m) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
r[i][j] = (*this)[i][j] - m[j][i];
return r;
}
/// Scalar multiplication operator.
constexpr Mat<L,C,real> operator*(real f) const noexcept
{
Mat<L,C,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
r[i][j] = (*this)[i][j] * f;
return r;
}
/// Scalar matrix multiplication operator.
friend constexpr Mat<L,C,real> operator*(real r, const Mat<L,C,real>& m) noexcept
{
return m*r;
}
/// Scalar division operator.
constexpr Mat<L,C,real> operator/(real f) const
{
Mat<L,C,real> r(NOINIT);
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
r[i][j] = (*this)[i][j] / f;
return r;
}
/// Scalar multiplication assignment operator.
constexpr void operator *=(real r) noexcept
{
for(Size i=0; i<L; i++)
this->elems[i]*=r;
}
/// Scalar division assignment operator.
constexpr void operator /=(real r)
{
for(Size i=0; i<L; i++)
this->elems[i]/=r;
}
/// Addition assignment operator.
constexpr void operator +=(const Mat<L,C,real>& m) noexcept
{
for(Size i=0; i<L; i++)
this->elems[i]+=m[i];
}
/// Addition of the transposed of m
constexpr void addTransposed(const Mat<C,L,real>& m) noexcept
{
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
(*this)[i][j] += m[j][i];
}
/// Substraction of the transposed of m
constexpr void subTransposed(const Mat<C,L,real>& m) noexcept
{
for(Size i=0; i<L; i++)
for(Size j=0; j<C; j++)
(*this)[i][j] -= m[j][i];
}
/// Substraction assignment operator.
constexpr void operator -=(const Mat<L,C,real>& m) noexcept
{
for(Size i=0; i<L; i++)
this->elems[i]-=m[i];
}
/// invert this
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
[[nodiscard]] constexpr Mat<L,C,real> inverted() const
{
static_assert(L == C, "Cannot invert a non-square matrix");
Mat<L,C,real> m = *this;
const bool canInvert = invertMatrix(m, *this);
assert(canInvert);
SOFA_UNUSED(canInvert);
return m;
}
/// Invert square matrix m
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
[[nodiscard]] constexpr bool invert(const Mat<L,C,real>& m)
{
if (&m == this)
{
Mat<L,C,real> mat = m;
const bool res = invertMatrix(*this, mat);
return res;
}
return invertMatrix(*this, m);
}
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
static Mat<L,C,real> transformTranslation(const Vec<C-1,real>& t) noexcept
{
Mat<L,C,real> m;
m.identity();
for (Size i=0; i<C-1; ++i)
m.elems[i][C-1] = t[i];
return m;
}
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
static Mat<L,C,real> transformScale(real s) noexcept
{
Mat<L,C,real> m;
m.identity();
for (Size i=0; i<C-1; ++i)
m.elems[i][i] = s;
return m;
}
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
static Mat<L,C,real> transformScale(const Vec<C-1,real>& s) noexcept
{
Mat<L,C,real> m;
m.identity();
for (Size i=0; i<C-1; ++i)
m.elems[i][i] = s[i];
return m;
}
template<class Quat>
static Mat<L,C,real> transformRotation(const Quat& q) noexcept
{
static_assert(L == C && (L ==4 || L == 3), "transformRotation can only be called with 3x3 or 4x4 matrices.");
Mat<L,C,real> m;
m.identity();
if constexpr(L == 4 && C == 4)
{
q.toHomogeneousMatrix(m);
return m;
}
else // if constexpr(L == 3 && C == 3)
{
q.toMatrix(m);
return m;
}
}
/// @return True if and only if the Matrix is a transformation matrix
constexpr bool isTransform() const
{
for (Size j=0;j<C-1;++j)
if (fabs((*this)(L-1,j)) > EQUALITY_THRESHOLD)
return false;
if (fabs((*this)(L-1,C-1) - 1.) > EQUALITY_THRESHOLD)
return false;
return true;
}
/// Multiplication operator Matrix * Vector considering the matrix as a transformation.
constexpr Vec<C-1,real> transform(const Vec<C-1,real>& v) const noexcept
{
Vec<C-1,real> r(NOINIT);
for(Size i=0; i<C-1; i++)
{
r[i]=(*this)[i][0] * v[0];
for(Size j=1; j<C-1; j++)
r[i] += (*this)[i][j] * v[j];
r[i] += (*this)[i][C-1];
}
return r;
}
/// Invert transformation matrix m
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
constexpr bool transformInvert(const Mat<L,C,real>& m)
{
return transformInvertMatrix(*this, m);
}
/// for square matrices
/// @warning in-place simple symmetrization
/// this = ( this + this.transposed() ) / 2.0
template<sofa::Size NbLine = L, sofa::Size NbColumn = C, typename = std::enable_if_t<NbLine == NbColumn> >
constexpr void symmetrize() noexcept
{
for(Size l=0; l<L; l++)
for(Size c=l+1; c<C; c++)
this->elems[l][c] = this->elems[c][l] = ( this->elems[l][c] + this->elems[c][l] ) * 0.5f;
}
// direct access to data
constexpr const real* data() const noexcept
{
return elems.data();
}
constexpr typename ArrayLineType::iterator begin() noexcept
{
return elems.begin();
}
constexpr typename ArrayLineType::const_iterator begin() const noexcept
{
return elems.begin();
}
constexpr typename ArrayLineType::iterator end() noexcept
{
return elems.end();
}
constexpr typename ArrayLineType::const_iterator end() const noexcept
{
return elems.end();
}
constexpr reference front()
{
return elems[0];
}
constexpr const_reference front() const
{
return elems[0];
}
constexpr reference back()
{
return elems[N - 1];
}
constexpr const_reference back() const
{
return elems[N - 1];
}
};
/// Same as Mat except the values are not initialized by default
template <sofa::Size L, sofa::Size C, typename real>
class MatNoInit : public Mat<L,C,real>
{
public:
constexpr MatNoInit() noexcept
: Mat<L,C,real>(NOINIT)
{
}
/// Assignment from an array of elements (stored per line).
constexpr void operator=(const real* p) noexcept
{
this->Mat<L,C,real>::operator=(p);
}
/// Assignment from another matrix
template<sofa::Size L2, sofa::Size C2, typename real2>
constexpr void operator=(const Mat<L2,C2,real2>& m) noexcept
{
this->Mat<L,C,real>::operator=(m);
}
};
/// Determinant of a 3x3 matrix.
template<class real>
constexpr real determinant(const Mat<3,3,real>& m) noexcept
{
return m(0,0)*m(1,1)*m(2,2)
+ m(1,0)*m(2,1)*m(0,2)
+ m(2,0)*m(0,1)*m(1,2)
- m(0,0)*m(2,1)*m(1,2)
- m(1,0)*m(0,1)*m(2,2)
- m(2,0)*m(1,1)*m(0,2);
}
/// Determinant of a 2x2 matrix.
template<class real>
constexpr real determinant(const Mat<2,2,real>& m) noexcept
{
return m(0,0)*m(1,1)
- m(1,0)*m(0,1);
}
/// Generalized-determinant of a 2x3 matrix.
/// Mirko Radi, "About a Determinant of Rectangular 2×n Matrix and its Geometric Interpretation"
template<class real>
constexpr real determinant(const Mat<2,3,real>& m) noexcept
{
return m(0,0)*m(1,1) - m(0,1)*m(1,0) - ( m(0,0)*m(1,2) - m(0,2)*m(1,0) ) + m(0,1)*m(1,2) - m(0,2)*m(1,1);
}
/// Generalized-determinant of a 3x2 matrix.
/// Mirko Radi, "About a Determinant of Rectangular 2×n Matrix and its Geometric Interpretation"
template<class real>
constexpr real determinant(const Mat<3,2,real>& m) noexcept
{
return m(0,0)*m(1,1) - m(1,0)*m(0,1) - ( m(0,0)*m(2,1) - m(2,0)*m(0,1) ) + m(1,0)*m(2,1) - m(2,0)*m(1,1);
}
// one-norm of a 3 x 3 matrix
template<class real>
real oneNorm(const Mat<3,3,real>& A)
{
real norm = 0.0;
for (sofa::Size i=0; i<3; i++)
{
real columnAbsSum = rabs(A(0,i)) + rabs(A(1,i)) + rabs(A(2,i));
if (columnAbsSum > norm)
norm = columnAbsSum;
}
return norm;
}
// inf-norm of a 3 x 3 matrix
template<class real>
real infNorm(const Mat<3,3,real>& A)
{
real norm = 0.0;
for (sofa::Size i=0; i<3; i++)
{
real rowSum = rabs(A(i,0)) + rabs(A(i,1)) + rabs(A(i,2));
if (rowSum > norm)
norm = rowSum;
}
return norm;
}
/// trace of a square matrix
template<sofa::Size N, class real>
constexpr real trace(const Mat<N,N,real>& m) noexcept
{
real t = m[0][0];
for(sofa::Size i=1 ; i<N ; ++i )
t += m[i][i];
return t;
}
/// diagonal of a square matrix
template<sofa::Size N, class real>
constexpr Vec<N,real> diagonal(const Mat<N,N,real>& m)
{
Vec<N,real> v(NOINIT);
for(sofa::Size i=0 ; i<N ; ++i )
v[i] = m[i][i];
return v;
}
/// Matrix inversion (general case).
template<sofa::Size S, class real>
[[nodiscard]] bool invertMatrix(Mat<S,S,real>& dest, const Mat<S,S,real>& from)
{
sofa::Size i{0}, j{0}, k{0};
Vec<S, sofa::Size> r, c, row, col;
Mat<S,S,real> m1 = from;
Mat<S,S,real> m2;
m2.identity();
for ( k = 0; k < S; k++ )
{
// Choosing the pivot
real pivot = 0;
for (i = 0; i < S; i++)
{
if (row[i])
continue;
for (j = 0; j < S; j++)
{
if (col[j])
continue;
real t = m1[i][j]; if (t<0) t=-t;
if ( t > pivot)
{
pivot = t;