|
30 | 30 |
|
31 | 31 | #include "SimTKcommon/internal/common.h" |
32 | 32 |
|
| 33 | +#include <tuple> |
| 34 | +#include <type_traits> |
| 35 | + |
33 | 36 | namespace SimTK { |
34 | 37 |
|
35 | 38 | /** This class represents a small matrix whose size is known at compile time, |
@@ -336,71 +339,36 @@ template <int M, int N, class ELT, int CS, int RS> class Mat { |
336 | 339 | explicit Mat(int i) |
337 | 340 | { new (this) Mat(ELT(Precision(i))); } |
338 | 341 |
|
339 | | - // A bevy of constructors from individual exact-match elements IN ROW ORDER. |
340 | | - Mat(const ELT& e0,const ELT& e1) |
341 | | - {assert(M*N==2);d[rIx(0)]=e0;d[rIx(1)]=e1;} |
342 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2) |
343 | | - {assert(M*N==3);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;} |
344 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3) |
345 | | - {assert(M*N==4);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;} |
346 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4) |
347 | | - {assert(M*N==5);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4;} |
348 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
349 | | - const ELT& e5) |
350 | | - {assert(M*N==6);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
351 | | - d[rIx(5)]=e5;} |
352 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
353 | | - const ELT& e5,const ELT& e6) |
354 | | - {assert(M*N==7);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
355 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;} |
356 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
357 | | - const ELT& e5,const ELT& e6,const ELT& e7) |
358 | | - {assert(M*N==8);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
359 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;} |
360 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
361 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8) |
362 | | - {assert(M*N==9);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
363 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;} |
364 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
365 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9) |
366 | | - {assert(M*N==10);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
367 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;} |
368 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
369 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
370 | | - const ELT& e10) |
371 | | - {assert(M*N==11);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
372 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10;} |
373 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
374 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
375 | | - const ELT& e10, const ELT& e11) |
376 | | - {assert(M*N==12);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
377 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10; |
378 | | - d[rIx(11)]=e11;} |
379 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
380 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
381 | | - const ELT& e10, const ELT& e11, const ELT& e12) |
382 | | - {assert(M*N==13);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
383 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10; |
384 | | - d[rIx(11)]=e11;d[rIx(12)]=e12;} |
385 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
386 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
387 | | - const ELT& e10, const ELT& e11, const ELT& e12, const ELT& e13) |
388 | | - {assert(M*N==14);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
389 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10; |
390 | | - d[rIx(11)]=e11;d[rIx(12)]=e12;d[rIx(13)]=e13;} |
391 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
392 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
393 | | - const ELT& e10, const ELT& e11, const ELT& e12, const ELT& e13, const ELT& e14) |
394 | | - {assert(M*N==15);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
395 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10; |
396 | | - d[rIx(11)]=e11;d[rIx(12)]=e12;d[rIx(13)]=e13;d[rIx(14)]=e14;} |
397 | | - Mat(const ELT& e0,const ELT& e1,const ELT& e2,const ELT& e3,const ELT& e4, |
398 | | - const ELT& e5,const ELT& e6,const ELT& e7,const ELT& e8,const ELT& e9, |
399 | | - const ELT& e10, const ELT& e11, const ELT& e12, const ELT& e13, const ELT& e14, |
400 | | - const ELT& e15) |
401 | | - {assert(M*N==16);d[rIx(0)]=e0;d[rIx(1)]=e1;d[rIx(2)]=e2;d[rIx(3)]=e3;d[rIx(4)]=e4; |
402 | | - d[rIx(5)]=e5;d[rIx(6)]=e6;d[rIx(7)]=e7;d[rIx(8)]=e8;d[rIx(9)]=e9;d[rIx(10)]=e10; |
403 | | - d[rIx(11)]=e11;d[rIx(12)]=e12;d[rIx(13)]=e13;d[rIx(14)]=e14;d[rIx(15)]=e15;} |
| 342 | +#ifndef SWIG |
| 343 | + // Constructs a `Mat` from individual exact-match elements IN ROW ORDER. |
| 344 | + template< |
| 345 | + typename... Elements, |
| 346 | + typename = std::enable_if_t< |
| 347 | + (M*N==sizeof...(Elements)) && |
| 348 | + (std::is_convertible_v<Elements&&, const E&> && ...) |
| 349 | + > |
| 350 | + > |
| 351 | + Mat(Elements&&... elementsRowByRow) |
| 352 | + { |
| 353 | + assignDataRowByRow( |
| 354 | + std::forward_as_tuple(elementsRowByRow...), |
| 355 | + std::make_integer_sequence<int, sizeof...(Elements)>{} |
| 356 | + ); |
| 357 | + } |
| 358 | +#else |
| 359 | + template <int MM = M, int NN = N, |
| 360 | + typename std::enable_if<(MM==3 && NN==3), int>::type = 0> |
| 361 | + Mat(const E& e00, const E& e01, const E& e02, |
| 362 | + const E& e10, const E& e11, const E& e12, |
| 363 | + const E& e20, const E& e21, const E& e22) |
| 364 | + { |
| 365 | + const E elems[9] = {e00, e01, e02, |
| 366 | + e10, e11, e12, |
| 367 | + e20, e21, e22}; |
| 368 | + for (int idx = 0; idx < 9; ++idx) |
| 369 | + d[rIx(idx)] = elems[idx]; |
| 370 | + } |
| 371 | +#endif |
404 | 372 |
|
405 | 373 | #ifndef SWIG |
406 | 374 | // Construction from 1-6 *exact match* Rows |
@@ -1199,6 +1167,14 @@ template <int M, int N, class ELT, int CS, int RS> class Mat { |
1199 | 1167 | const int col = k % N; // that's modulus, not cross product! |
1200 | 1168 | return row*RS + col*CS; |
1201 | 1169 | } |
| 1170 | + |
| 1171 | +#ifndef SWIG |
| 1172 | + template<typename ElementsRowByRowTuple, int... Idx> |
| 1173 | + void assignDataRowByRow(ElementsRowByRowTuple&& els, std::integer_sequence<int, Idx...>) |
| 1174 | + { |
| 1175 | + ((d[rIx(Idx)] = std::get<Idx>(els)) , ...); |
| 1176 | + } |
| 1177 | +#endif |
1202 | 1178 | }; |
1203 | 1179 |
|
1204 | 1180 | ////////////////////////////////////////////// |
|
0 commit comments