forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkMathPrivate.hxx
957 lines (857 loc) · 42.4 KB
/
vtkMathPrivate.hxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMathPrivate.hxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* @class vtkMathPrivate
* @brief Internal toolkit used in some vtkMath methods.
*
* vtkMathPrivate provides meta-classes helpers that are used in some vtkMath
* methods.
* @sa
* vtkMath
* vtkMatrixUtilities
*/
#ifndef vtkMathPrivate_hxx
#define vtkMathPrivate_hxx
#include "vtkCommonCoreModule.h" //required for correct implementation
#include "vtkMatrixUtilities.h"
#include <type_traits>
namespace vtkMathPrivate
{
static constexpr int VTK_MATH_PRIVATE_PACK_SIZE = 4;
//=============================================================================
// This class computes the dot product between row RowT of matrix M1
// and column ColT of matrix M2.
// The template parameter IdxT is on index ahead from computation.
// Template parameters LayoutT1 and LayoutT2 respectively reindex
// input matrices M1 and M2 according to MatrixLayout enumeration
// M1 (or M1^T if LayoutT1 == vtkMatrixUtilities::Layout::Transpose) is a matrix of RowsT x
// MidDimT M2 (or M2^T if LayoutT2 == vtkMatrixUtilities::Layout::Transpose) is a matrix of MidDimT
// x ColsDimT
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT,
class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
class LayoutT2 = vtkMatrixUtilities::Layout::Identity, int IdxT = 0, int PackSizeT = MidDimT>
class ContractRowWithCol
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
return ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2, IdxT,
Shift>::Compute(M1, M2) +
ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2,
IdxT + Shift, PackSizeT - Shift>::Compute(M1, M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
static constexpr int Shift =
(MidDimT - IdxT) / VTK_MATH_PRIVATE_PACK_SIZE ? VTK_MATH_PRIVATE_PACK_SIZE : MidDimT - IdxT - 1;
};
//=============================================================================
// Specialization for when there are 4 components left to compute
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2, int IdxT>
class ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2, IdxT, 4>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
using Wrap1 = vtkMatrixUtilities::Wrapper<RowsT, MidDimT, MatrixT1, LayoutT1>;
using Wrap2 = vtkMatrixUtilities::Wrapper<MidDimT, ColsT, MatrixT2, LayoutT2>;
return Wrap1::template Get<RowT, IdxT>(M1) * Wrap2::template Get<IdxT, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 1>(M1) * Wrap2::template Get<IdxT + 1, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 2>(M1) * Wrap2::template Get<IdxT + 2, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 3>(M1) * Wrap2::template Get<IdxT + 3, ColT>(M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// Specialization for when there are 3 components left to compute
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2, int IdxT>
class ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2, IdxT, 3>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
using Wrap1 = vtkMatrixUtilities::Wrapper<RowsT, MidDimT, MatrixT1, LayoutT1>;
using Wrap2 = vtkMatrixUtilities::Wrapper<MidDimT, ColsT, MatrixT2, LayoutT2>;
return Wrap1::template Get<RowT, IdxT>(M1) * Wrap2::template Get<IdxT, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 1>(M1) * Wrap2::template Get<IdxT + 1, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 2>(M1) * Wrap2::template Get<IdxT + 2, ColT>(M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// Specialization for when there are 2 components left to compute
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2, int IdxT>
class ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2, IdxT, 2>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
using Wrap1 = vtkMatrixUtilities::Wrapper<RowsT, MidDimT, MatrixT1, LayoutT1>;
using Wrap2 = vtkMatrixUtilities::Wrapper<MidDimT, ColsT, MatrixT2, LayoutT2>;
return Wrap1::template Get<RowT, IdxT>(M1) * Wrap2::template Get<IdxT, ColT>(M2) +
Wrap1::template Get<RowT, IdxT + 1>(M1) * Wrap2::template Get<IdxT + 1, ColT>(M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// Specialization for when there is 1 component left to compute
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2, int IdxT>
class ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2, IdxT, 1>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
using Wrap1 = vtkMatrixUtilities::Wrapper<RowsT, MidDimT, MatrixT1, LayoutT1>;
using Wrap2 = vtkMatrixUtilities::Wrapper<MidDimT, ColsT, MatrixT2, LayoutT2>;
return Wrap1::template Get<RowT, IdxT>(M1) * Wrap2::template Get<IdxT, ColT>(M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// This class handles row and col contraction when at least one of the 2 input
// matrices are diagonal
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2>
class DiagContractRowWithCol;
//=============================================================================
// Specialization for when M1 is diagonal
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT2>
class DiagContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT,
vtkMatrixUtilities::Layout::Diag, LayoutT2>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
return M1[RowT] * M2[Mapper2::template GetIndex<RowT, ColT>()];
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
using Mapper2 = vtkMatrixUtilities::Mapper<MidDimT, ColsT, LayoutT2>;
};
//=============================================================================
// Specialization for when M2 is diagonal
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1>
class DiagContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1,
vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1& M1, const MatrixT2& M2)
{
return M1[Mapper1::template GetIndex<RowT, ColT>()] * M2[ColT];
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT >= 0 && RowT < RowsT;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT >= 0 && ColT < ColsT;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
using Mapper1 = vtkMatrixUtilities::Mapper<RowsT, MidDimT, LayoutT1>;
};
//=============================================================================
// This class returns zero. Is is used for rectangular diagonal matrices, when
// lines / rows are filled with zeros.
template <class ScalarT>
class NullContractRowWithCol
{
public:
template <class MatrixT1, class MatrixT2>
static ScalarT Compute(const MatrixT1&, const MatrixT2&)
{
return ScalarT(0);
}
};
//=============================================================================
// Helper class to choose between regular contraction class vs diagonal
// handling contraction class.
// By default, Type is an instance of ContractRowWithCol
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2, bool HasOneDiagonalMatrixT = false>
struct ContractRowWithColSwitch
{
typedef ContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2> Type;
static constexpr bool NO_INPUT_MATRIX_CAN_BE_DIAGONAL =
!std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value &&
!std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
static_assert(NO_INPUT_MATRIX_CAN_BE_DIAGONAL,
"LayoutT1 and LayoutT2 cannot equal vtkMatrixUtilities::Layout::Diag in this setup");
};
//=============================================================================
// Specialization for diagonal matrices.
// Rectangular diagonal matrices are handled using NullContractRowWithCol.
template <class ScalarT, int RowsT, int MidDimT, int ColsT, int RowT, int ColT, class LayoutT1,
class LayoutT2>
struct ContractRowWithColSwitch<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2,
true>
{
static constexpr bool IsDiagonal1 =
std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value;
static constexpr bool IsDiagonal2 =
std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
// If on of the diagonal matrix is rectangular and we are out of the diagonal
// length, all remaning values are null.
static constexpr bool UseNullContractRowWithCol =
(IsDiagonal1 && RowT >= MidDimT) || (IsDiagonal2 && ColT >= MidDimT);
typedef typename std::conditional<UseNullContractRowWithCol, NullContractRowWithCol<ScalarT>,
DiagContractRowWithCol<ScalarT, RowsT, MidDimT, ColsT, RowT, ColT, LayoutT1, LayoutT2>>::type
Type;
static constexpr bool NEEDS_AT_LEAST_ONE_DIAGONAL_INPUT_MATRIX = IsDiagonal1 || IsDiagonal2;
static_assert(NEEDS_AT_LEAST_ONE_DIAGONAL_INPUT_MATRIX,
"LayoutT1 or LayoutT2 must equal vtkMatrixUtilities::Layout::Diag in this setup");
};
namespace detail
{
//=============================================================================
// Class in charge for actually multiplying 2 matrices.
// This method is called by MultiplyMatrix::Compute for chunks of size
// VTK_MATH_PRIVATE_PACK_SIZE at most, specified in ColPackSizeT.
// This class mostly consists on explicitly onfold computation for those chunks.
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int RowT, int ColT,
int ColPackSizeT>
class MultiplyMatrix;
//=============================================================================
// Specialization for a chunk of size 4
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int RowT, int ColT>
class MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowT, ColT, 4>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT3>::value_type;
using Wrap3 = vtkMatrixUtilities::Wrapper<RowsT, ColsT, MatrixT3>;
Wrap3::template Get<RowT, ColT>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT, ColsT,
RowT, ColT, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 1>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 1, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 2>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 2, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 3>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 3, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT < RowsT && RowT >= 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT + 3 < ColsT && ColT >= 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
static constexpr bool OneMatrixIsDiagonal =
std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value ||
std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
};
//=============================================================================
// Specialization for a chunk of size 3
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int RowT, int ColT>
class MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowT, ColT, 3>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT3>::value_type;
using Wrap3 = vtkMatrixUtilities::Wrapper<RowsT, ColsT, MatrixT3>;
Wrap3::template Get<RowT, ColT>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT, ColsT,
RowT, ColT, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 1>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 1, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 2>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 2, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT < RowsT && RowT >= 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT + 2 < ColsT && ColT >= 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
static constexpr bool OneMatrixIsDiagonal =
std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value ||
std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
};
//=============================================================================
// Specialization for a chunk of size 2
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int RowT, int ColT>
class MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowT, ColT, 2>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT3>::value_type;
using Wrap3 = vtkMatrixUtilities::Wrapper<RowsT, ColsT, MatrixT3>;
Wrap3::template Get<RowT, ColT>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT, ColsT,
RowT, ColT, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
Wrap3::template Get<RowT, ColT + 1>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT,
ColsT, RowT, ColT + 1, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT < RowsT && RowT >= 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT + 1 < ColsT && ColT >= 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
static constexpr bool OneMatrixIsDiagonal =
std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value ||
std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
};
//=============================================================================
// Specialization for a chunk of size 1
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int RowT, int ColT>
class MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowT, ColT, 1>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT3>::value_type;
using Wrap3 = vtkMatrixUtilities::Wrapper<RowsT, ColsT, MatrixT3>;
Wrap3::template Get<RowT, ColT>(M3) = ContractRowWithColSwitch<Scalar, RowsT, MidDimT, ColsT,
RowT, ColT, LayoutT1, LayoutT2, OneMatrixIsDiagonal>::Type::Compute(M1, M2);
}
private:
static constexpr bool ROW_OUT_OF_BOUNDS = RowT < RowsT && RowT >= 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = ColT < ColsT && ColT >= 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
static constexpr bool OneMatrixIsDiagonal =
std::is_same<LayoutT1, vtkMatrixUtilities::Layout::Diag>::value ||
std::is_same<LayoutT2, vtkMatrixUtilities::Layout::Diag>::value;
};
//=============================================================================
// Specialization when both input matrices are diagonal for a chunk of size 4
template <int SizeT, int IdxT>
class MultiplyMatrix<SizeT, SizeT, SizeT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, IdxT, IdxT, 4>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
M3[IdxT] = M1[IdxT] * M2[IdxT];
M3[IdxT + 1] = M1[IdxT + 1] * M2[IdxT + 1];
M3[IdxT + 2] = M1[IdxT + 2] * M2[IdxT + 2];
M3[IdxT + 3] = M1[IdxT + 3] * M2[IdxT + 3];
}
};
//=============================================================================
// Specialization when both input matrices are diagonal for a chunk of size 3
template <int SizeT, int IdxT>
class MultiplyMatrix<SizeT, SizeT, SizeT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, IdxT, IdxT, 3>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
M3[IdxT] = M1[IdxT] * M2[IdxT];
M3[IdxT + 1] = M1[IdxT + 1] * M2[IdxT + 1];
M3[IdxT + 2] = M1[IdxT + 2] * M2[IdxT + 2];
}
};
//=============================================================================
// Specialization when both input matrices are diagonal for a chunk of size 2
template <int SizeT, int IdxT>
class MultiplyMatrix<SizeT, SizeT, SizeT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, IdxT, IdxT, 2>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
M3[IdxT] = M1[IdxT] * M2[IdxT];
M3[IdxT + 1] = M1[IdxT + 1] * M2[IdxT + 1];
}
};
//=============================================================================
// Specialization when both input matrices are diagonal for a chunk of size 1
template <int SizeT, int IdxT>
class MultiplyMatrix<SizeT, SizeT, SizeT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, IdxT, IdxT, 1>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
M3[IdxT] = M1[IdxT] * M2[IdxT];
}
};
} // namespace detail
//=============================================================================
// Multiply matrices such that M3 = M1 * M2.
// Template parameters LayoutT1 and LayoutT2 respectively reindex
// input matrices M1 and M2 following MatrixLayout options
// Hence, if LayoutT1 == vtkMatrixUtilities::Layout::Transpose, then M3 = M1^T * M2, and so on.
// M1 (or M1^T if LayoutT1 == vtkMatrixUtilities::Layout::Transpose) is a matrix of RowsT x
// MidDimT M2 (or M2^T if LayoutT2 == vtkMatrixUtilities::Layout::Transpose) is a matrix of MidDimT
// x ColsDimT
//
// RemainingRowSizeT should be disregarded when first instantiating this class. It is
// the number of remaning elements in the current row to process.
//
// To compute the multiplication, each component of the output matrix
// is computed chunk by chunk (of size VTK_MATH_PRIVATE_PACK_SIZE),
// starting at the top left, sweeping the rows one by one.
template <int RowsT, int MidDimT, int ColsT, class LayoutT1 = vtkMatrixUtilities::Layout::Identity,
class LayoutT2 = vtkMatrixUtilities::Layout::Identity, int NextRowT = 1, int NextColT = 1,
int RemainingRowSizeT = ColsT>
class MultiplyMatrix
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
detail::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, Row, Col, Shift>::Compute(
M1, M2, M3);
MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, ForwardRow, ForwardCol,
ForwardRemainingRowSize>::Compute(M1, M2, M3);
}
private:
static constexpr int Row = NextRowT - 1;
static constexpr int Col = NextColT - 1;
// We go to a new row when the last chunk is lower or equal to
// VTK_MATH_PRIVATE_PACK_SIZE
static constexpr bool GoToNewRow = !((ColsT - Col - 1) / VTK_MATH_PRIVATE_PACK_SIZE);
// This is the chunk size, which is from what we should shift in recursive
// calls
static constexpr int Shift = GoToNewRow ? ColsT - Col : VTK_MATH_PRIVATE_PACK_SIZE;
// Row / col / pack size for the next recursive call, depending on if we
// change row or not.
static constexpr int ForwardRow = GoToNewRow ? NextRowT + 1 : NextRowT;
static constexpr int ForwardCol = GoToNewRow ? 1 : NextColT + Shift;
static constexpr int ForwardRemainingRowSize = GoToNewRow ? ColsT : RemainingRowSizeT - Shift;
static constexpr bool ROW_OUT_OF_BOUNDS = Row < RowsT && Row >= 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = Col < ColsT && Col >= 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// Specialization for when both input matrices are diagonal
// Warning: RowsT, MidDimT and ColsT MUST match
template <int RowsT, int MidDimT, int ColsT, int NextIdxT, int RemainingRowSizeT>
class MultiplyMatrix<RowsT, MidDimT, ColsT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, NextIdxT, NextIdxT, RemainingRowSizeT>
{
public:
template <class MatrixT1, class MatrixT2, class MatrixT3>
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3)
{
detail::MultiplyMatrix<RowsT, MidDimT, ColsT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, NextIdxT - 1, NextIdxT - 1, Shift>::Compute(M1, M2, M3);
MultiplyMatrix<RowsT, MidDimT, ColsT, vtkMatrixUtilities::Layout::Diag,
vtkMatrixUtilities::Layout::Diag, NextIdxT + Shift, NextIdxT + Shift,
RemainingRowSizeT - Shift>::Compute(M1, M2, M3);
}
private:
static constexpr bool Shift = VTK_MATH_PRIVATE_PACK_SIZE;
static constexpr bool DIAGONAL_MATRICES_DIMENSIONS_DONT_MATCH =
RowsT == MidDimT && RowsT == ColsT;
static_assert(DIAGONAL_MATRICES_DIMENSIONS_DONT_MATCH, "There must be RowsT = MidDimT = colsT");
static constexpr bool ROW_OUT_OF_BOUNDS = NextIdxT <= RowsT && NextIdxT > 0;
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds");
static constexpr bool COL_OUT_OF_BOUNDS = NextIdxT <= ColsT && NextIdxT > 0;
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds");
};
//=============================================================================
// Macro defining the specialization for when both input matrices are diagonal,
// implementing the last chunk to compute. They should be specialized
// for integers below VTK_MATH_PRIVATE_PACK_SIZE
#define vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro(RemainingRowSize) \
template <int RowsT, int MidDimT, int ColsT, int NextIdxT> \
class MultiplyMatrix<RowsT, MidDimT, ColsT, vtkMatrixUtilities::Layout::Diag, \
vtkMatrixUtilities::Layout::Diag, NextIdxT, NextIdxT, RemainingRowSize> \
{ \
public: \
template <class MatrixT1, class MatrixT2, class MatrixT3> \
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3) \
{ \
detail::MultiplyMatrix<RowsT, MidDimT, ColsT, vtkMatrixUtilities::Layout::Diag, \
vtkMatrixUtilities::Layout::Diag, NextIdxT - 1, NextIdxT - 1, \
RemainingRowSize>::Compute(M1, M2, M3); \
} \
\
private: \
static constexpr bool DIAGONAL_MATRICES_DIMENSIONS_DONT_MATCH = \
RowsT == MidDimT && RowsT == ColsT; \
static_assert( \
DIAGONAL_MATRICES_DIMENSIONS_DONT_MATCH, "There must be RowsT = MidDimT = colsT"); \
static constexpr bool ROW_OUT_OF_BOUNDS = NextIdxT <= RowsT && NextIdxT > 0; \
static_assert(ROW_OUT_OF_BOUNDS, "RowT is out of bounds"); \
static constexpr bool COL_OUT_OF_BOUNDS = NextIdxT <= ColsT && NextIdxT > 0; \
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds"); \
};
//=============================================================================
vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro(1);
vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro(2);
vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro(3);
vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro(4);
#undef vtkEndForBothDiagonalMultiplyMatrixSpecializationMacro
//=============================================================================
// Macro defining specialization for the last chunk to compute. They should be
// specialized for integers below VTK_MATH_PRIVATE_PACK_SIZE
#define vtkLastRowMultiplyMatrixSpecializationMacro(RemainingRowSize) \
template <int RowsT, int MidDimT, int ColsT, class LayoutT1, class LayoutT2, int NextColT> \
class MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowsT, NextColT, \
RemainingRowSize> \
{ \
public: \
template <class MatrixT1, class MatrixT2, class MatrixT3> \
static void Compute(const MatrixT1& M1, const MatrixT2& M2, MatrixT3& M3) \
{ \
detail::MultiplyMatrix<RowsT, MidDimT, ColsT, LayoutT1, LayoutT2, RowsT - 1, NextColT - 1, \
RemainingRowSize>::Compute(M1, M2, M3); \
} \
\
private: \
static constexpr bool COL_OUT_OF_BOUNDS = NextColT - 1 < ColsT && NextColT >= 1; \
static_assert(COL_OUT_OF_BOUNDS, "ColT is out of bounds"); \
};
//=============================================================================
vtkLastRowMultiplyMatrixSpecializationMacro(1);
vtkLastRowMultiplyMatrixSpecializationMacro(2);
vtkLastRowMultiplyMatrixSpecializationMacro(3);
vtkLastRowMultiplyMatrixSpecializationMacro(4);
#undef vtkLastRowMultiplyMatrixSpecializationMacro
//=============================================================================
// Class computing the determinant of square matrices SizeT x SizeT.
// The template parameter LayoutT is a struct embedded in MatrixLayout
template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity>
class Determinant;
//=============================================================================
// Specialization for diagonal 3x3 matrices of size
template <>
class Determinant<3, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT>
static typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type Compute(
const MatrixT& M)
{
return M[0] * M[1] * M[3];
}
};
//=============================================================================
// Specialization for diagonal 2x2 matrices
template <>
class Determinant<2, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT>
static typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type Compute(
const MatrixT& M)
{
return M[0] * M[1];
}
};
//=============================================================================
// Specialization for 1x1 matrices
template <class LayoutT>
class Determinant<1, LayoutT>
{
public:
template <class MatrixT>
static typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type Compute(
const MatrixT& M)
{
return M[0];
}
};
//=============================================================================
// Specialization for 2x2 non-diagonal matrices
template <class LayoutT>
class Determinant<2, LayoutT>
{
public:
template <class MatrixT>
static typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type Compute(
const MatrixT& M)
{
using Wrap = vtkMatrixUtilities::Wrapper<2, 2, MatrixT, LayoutT>;
return Wrap::template Get<0, 0>(M) * Wrap::template Get<1, 1>(M) -
Wrap::template Get<1, 0>(M) * Wrap::template Get<0, 1>(M);
}
private:
};
//=============================================================================
// Specialization for 3x3 non-diagonal matrices
template <class LayoutT>
class Determinant<3, LayoutT>
{
public:
template <class MatrixT>
static typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type Compute(
const MatrixT& M)
{
using Wrap = vtkMatrixUtilities::Wrapper<3, 3, MatrixT, LayoutT>;
return Wrap::template Get<0, 0>(M) * Wrap::template Get<1, 1>(M) * Wrap::template Get<2, 2>(M) +
Wrap::template Get<0, 1>(M) * Wrap::template Get<1, 2>(M) * Wrap::template Get<2, 0>(M) +
Wrap::template Get<0, 2>(M) * Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 1>(M) -
Wrap::template Get<0, 0>(M) * Wrap::template Get<1, 2>(M) * Wrap::template Get<2, 1>(M) -
Wrap::template Get<0, 1>(M) * Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<0, 2>(M) * Wrap::template Get<1, 1>(M) * Wrap::template Get<2, 0>(M);
}
};
//=============================================================================
// Class inverting square matrices SizeT x SizeT.
// The template parameter LayoutT is a struct embedded in MatrixLayout
template <int SizeT, class LayoutT = vtkMatrixUtilities::Layout::Identity>
class InvertMatrix;
//=============================================================================
// Specialization for 2x2 non-diagonal matrices
template <class LayoutT>
class InvertMatrix<2, LayoutT>
{
public:
template <class MatrixT1, class MatrixT2>
static void Compute(const MatrixT1& M1, MatrixT2& M2)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT2>::value_type;
using Wrap1 = vtkMatrixUtilities::Wrapper<2, 2, MatrixT1, LayoutT>;
using Wrap2 = vtkMatrixUtilities::Wrapper<2, 2, MatrixT2>;
Scalar detInv = 1.0 / Determinant<2, LayoutT>::Compute(M1);
Wrap2::template Get<0, 0>(M2) = detInv * Wrap1::template Get<1, 1>(M1);
Wrap2::template Get<1, 0>(M2) = -detInv * Wrap1::template Get<1, 0>(M1);
Wrap2::template Get<0, 1>(M2) = -detInv * Wrap1::template Get<0, 1>(M1);
Wrap2::template Get<1, 1>(M2) = detInv * Wrap1::template Get<0, 0>(M1);
}
};
//=============================================================================
// Specialization for 3x3 non-diagonal matrices
template <class LayoutT>
class InvertMatrix<3, LayoutT>
{
public:
template <class MatrixT1, class MatrixT2>
static void Compute(const MatrixT1& M1, MatrixT2& M2)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT2>::value_type;
using Wrap1 = vtkMatrixUtilities::Wrapper<3, 3, MatrixT1, LayoutT>;
using Wrap2 = vtkMatrixUtilities::Wrapper<3, 3, MatrixT2>;
Scalar detInv = 1.0 /
(Wrap1::template Get<0, 0>(M1) *
(Wrap1::template Get<1, 1>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 1>(M1) * Wrap1::template Get<1, 2>(M1)) -
Wrap1::template Get<0, 1>(M1) *
(Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<1, 2>(M1)) +
Wrap1::template Get<0, 2>(M1) *
(Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<2, 1>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<1, 1>(M1)));
Wrap2::template Get<0, 0>(M2) = detInv *
(Wrap1::template Get<1, 1>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 1>(M1) * Wrap1::template Get<1, 2>(M1));
Wrap2::template Get<1, 0>(M2) = -detInv *
(Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<1, 2>(M1));
Wrap2::template Get<2, 0>(M2) = detInv *
(Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<2, 1>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<1, 1>(M1));
Wrap2::template Get<0, 1>(M2) = -detInv *
(Wrap1::template Get<0, 1>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 1>(M1) * Wrap1::template Get<0, 2>(M1));
Wrap2::template Get<1, 1>(M2) = detInv *
(Wrap1::template Get<0, 0>(M1) * Wrap1::template Get<2, 2>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<0, 2>(M1));
Wrap2::template Get<2, 1>(M2) = -detInv *
(Wrap1::template Get<0, 0>(M1) * Wrap1::template Get<2, 1>(M1) -
Wrap1::template Get<2, 0>(M1) * Wrap1::template Get<0, 1>(M1));
Wrap2::template Get<0, 2>(M2) = detInv *
(Wrap1::template Get<0, 1>(M1) * Wrap1::template Get<1, 2>(M1) -
Wrap1::template Get<1, 1>(M1) * Wrap1::template Get<0, 2>(M1));
Wrap2::template Get<1, 2>(M2) = -detInv *
(Wrap1::template Get<0, 0>(M1) * Wrap1::template Get<1, 2>(M1) -
Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<0, 2>(M1));
Wrap2::template Get<2, 2>(M2) = detInv *
(Wrap1::template Get<0, 0>(M1) * Wrap1::template Get<1, 1>(M1) -
Wrap1::template Get<1, 0>(M1) * Wrap1::template Get<0, 1>(M1));
}
};
//=============================================================================
// Specialization for 1x1 matrices
template <class LayoutT>
class InvertMatrix<1, LayoutT>
{
public:
template <class MatrixT1, class MatrixT2>
static void Compute(const MatrixT1& M1, MatrixT2& M2)
{
M2[0] = 1.0 / M1[0];
}
};
//=============================================================================
// Specialization for 2x2 diagonal matrices
template <>
class InvertMatrix<2, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT1, class MatrixT2>
static void Compute(const MatrixT1& M1, MatrixT2& M2)
{
M2[0] = 1.0 / M1[0];
M2[1] = 1.0 / M1[1];
}
};
//=============================================================================
// Specialization for 3x3 diagonal matrices
template <>
class InvertMatrix<3, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT1, class MatrixT2>
static void Compute(const MatrixT1& M1, MatrixT2& M2)
{
M2[0] = 1.0 / M1[0];
M2[1] = 1.0 / M1[1];
M2[2] = 1.0 / M1[2];
}
};
//=============================================================================
// Class solving systems M*y = x for square matrices RowsT x ColsT.
// The template parameter LayoutT is a struct embedded in MatrixLayout
// This class is currently specialized for 1x1, 2x2 and 3x3 matrices
template <int RowsT, int ColsT, class LayoutT = vtkMatrixUtilities::Layout::Identity>
class LinearSolve;
//=============================================================================
// Specialization for 1x1 matrices
template <class LayoutT>
class LinearSolve<1, 1, LayoutT>
{
public:
template <class MatrixT, class VectorT1, class VectorT2>
static void Compute(const MatrixT& M, const VectorT1& x, VectorT2& y)
{
y[0] = x[0] / M[0];
}
};
//=============================================================================
// Specialization for 2x2 matrices
template <class LayoutT>
class LinearSolve<2, 2, LayoutT>
{
public:
template <class MatrixT, class VectorT1, class VectorT2>
static void Compute(const MatrixT& M, const VectorT1& x, VectorT2& y)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type;
using Wrap = vtkMatrixUtilities::Wrapper<2, 2, MatrixT, LayoutT>;
Scalar detInv = 1.0 / Determinant<2, LayoutT>::Compute(M);
y[0] = (x[0] * Wrap::template Get<1, 1>(M) - x[1] * Wrap::template Get<0, 1>(M)) * detInv;
y[1] = (-x[0] * Wrap::template Get<1, 0>(M) + x[1] * Wrap::template Get<0, 0>(M)) * detInv;
}
};
//=============================================================================
// Specialization for 3x3 matrices
template <class LayoutT>
class LinearSolve<3, 3, LayoutT>
{
public:
template <class MatrixT, class VectorT1, class VectorT2>
static void Compute(const MatrixT& M, const VectorT1& x, VectorT2& y)
{
using Scalar = typename vtkMatrixUtilities::ScalarTypeExtractor<MatrixT>::value_type;
using Wrap = vtkMatrixUtilities::Wrapper<3, 3, MatrixT, LayoutT>;
Scalar detInv = 1.0 /
(Wrap::template Get<0, 0>(M) *
(Wrap::template Get<1, 1>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 1>(M) * Wrap::template Get<1, 2>(M)) -
Wrap::template Get<0, 1>(M) *
(Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<1, 2>(M)) +
Wrap::template Get<0, 2>(M) *
(Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 1>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<1, 1>(M)));
y[0] = detInv *
(x[0] *
(Wrap::template Get<1, 1>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 1>(M) * Wrap::template Get<1, 2>(M)) -
x[1] *
(Wrap::template Get<0, 1>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 1>(M) * Wrap::template Get<0, 2>(M)) +
x[2] *
(Wrap::template Get<0, 1>(M) * Wrap::template Get<1, 2>(M) -
Wrap::template Get<1, 1>(M) * Wrap::template Get<0, 2>(M)));
y[1] = detInv *
(-x[0] *
(Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<1, 2>(M)) +
x[1] *
(Wrap::template Get<0, 0>(M) * Wrap::template Get<2, 2>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<0, 2>(M)) -
x[2] *
(Wrap::template Get<0, 0>(M) * Wrap::template Get<1, 2>(M) -
Wrap::template Get<1, 0>(M) * Wrap::template Get<0, 2>(M)));
y[2] = detInv *
(x[0] *
(Wrap::template Get<1, 0>(M) * Wrap::template Get<2, 1>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<1, 1>(M)) -
x[1] *
(Wrap::template Get<0, 0>(M) * Wrap::template Get<2, 1>(M) -
Wrap::template Get<2, 0>(M) * Wrap::template Get<0, 1>(M)) +
x[2] *
(Wrap::template Get<0, 0>(M) * Wrap::template Get<1, 1>(M) -
Wrap::template Get<1, 0>(M) * Wrap::template Get<0, 1>(M)));
}
};
//=============================================================================
// Specialization for 2x2 diagonal matrices
template <>
class LinearSolve<2, 2, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT, class VectorT1, class VectorT2>
static void Compute(const MatrixT& M, const VectorT1& x, VectorT2& y)
{
y[0] = x[0] / M[0];
y[1] = x[1] / M[1];
}
};
//=============================================================================
// Specialization for 3x3 diagonal matrices
template <>
class LinearSolve<3, 3, vtkMatrixUtilities::Layout::Diag>
{
public:
template <class MatrixT, class VectorT1, class VectorT2>
static void Compute(const MatrixT& M, const VectorT1& x, VectorT2& y)
{
y[0] = x[0] / M[0];
y[1] = x[1] / M[1];
y[2] = x[2] / M[2];
}
};
} // namespace vtkMathPrivate
#endif