MatrixXX.h
Go to the documentation of this file.
1#ifndef GfxTL__MATRIXX_HEADER__
2#define GfxTL__MATRIXX_HEADER__
3#ifndef _USE_MATH_DEFINES
4#define _USE_MATH_DEFINES
5#endif
6#include <algorithm>
7#include <cmath>
8#include <cstring>
9#include <iostream>
10
11
12#include <GfxTL/StdOverrides.h>
13#ifdef min
14#undef min
15#endif
16#ifdef max
17#undef max
18#endif
19
20namespace GfxTL
21{
22 // forward declaration
23 template <unsigned int D, class T>
24 class VectorXD;
25
26 // forward declaration
27 template <unsigned int C, unsigned int R, class T>
28 class MatrixXX;
29
30 // matrix operators
31 template <unsigned int C, unsigned int R, class T>
32 MatrixXX<C, R, T>& operator+=(MatrixXX<C, R, T>& m, const MatrixXX<C, R, T>& b);
33 template <unsigned int C, unsigned int R, class T>
34 MatrixXX<C, R, T>& operator-=(MatrixXX<C, R, T>& m, const MatrixXX<C, R, T>& b);
35 template <unsigned int C, unsigned int R, class T>
36 MatrixXX<C, R, T>& operator*=(MatrixXX<C, R, T>& m, const MatrixXX<C, R, T>& b);
37 template <unsigned int C, unsigned int R, class T>
38 MatrixXX<C, R, T>& operator*=(MatrixXX<C, R, T>& m, T s);
39 template <unsigned int C, unsigned int R, class T>
40 MatrixXX<C, R, T>& operator/=(MatrixXX<C, R, T>& m, T s);
41 template <unsigned int C, unsigned int R, class T>
42 bool operator==(const MatrixXX<C, R, T>& a, const MatrixXX<C, R, T>& b);
43 template <unsigned int C, unsigned int R, class T>
44 bool operator!=(const MatrixXX<C, R, T>& a, const MatrixXX<C, R, T>& b);
45
46 namespace Internal
47 {
48 /** Matrix in column major order */
49 template <unsigned int C, unsigned int R, class T>
51 {
52 public:
53 typedef T ScalarType;
54 typedef T value_type;
56
57 enum
58 {
59 Rows = R,
60 Cols = C,
61 Entries = C * R
62 };
63
65 operator[](unsigned int col)
66 {
67 return *(reinterpret_cast<VectorXD<R, T>*>(&_m[col * R]));
68 }
69
70 const VectorXD<R, T>&
71 operator[](unsigned int col) const
72 {
73 return *(reinterpret_cast<const VectorXD<R, T>*>(&_m[col * R]));
74 }
75
76 void
78 {
79 memset(_m, 0, sizeof(_m));
80 }
81
82 T*
84 {
85 return _m;
86 }
87
88 const T*
89 Data() const
90 {
91 return _m;
92 }
93
94 T
96 {
97 T s = 0;
98 for (unsigned int i = 0; i < C * R; ++i)
99 {
100 s += _m[i] * _m[i];
101 }
102 if (s == 0)
103 {
104 return s;
105 }
106 s = T(std::sqrt(s));
107 for (unsigned int i = 0; i < C * R; ++i)
108 {
109 _m[i] /= s;
110 }
111 return s;
112 }
113
114 void
116 {
117 for (unsigned int c = 0; c < C; ++c)
118 for (unsigned int r = 0; r < R; ++r)
119 {
120 (*t)[r][c] = (*this)[c][r];
121 }
122 }
123
124 protected:
126 };
127 }; // namespace Internal
128
129 // matrix class
130 template <unsigned int C, unsigned int R, class T>
131 class MatrixXX : public Internal::BaseMatrixXX<C, R, T>
132 {
133 public:
137
139 {
140 }
141
142 MatrixXX(const SuperType& mat) : SuperType(mat)
143 {
144 }
145
146 MatrixXX(const ThisType& mat)
147 {
148 std::memcpy(SuperType::_m, mat._m, sizeof(SuperType::_m));
149 }
150
151 explicit MatrixXX(T v)
152 {
153 for (unsigned int i = 0; i < SuperType::Entries; ++i)
154 {
155 SuperType::_m[i] = v;
156 }
157 }
158
159 explicit MatrixXX(const T* m)
160 {
161 std::memcpy(SuperType::_m, m, sizeof(SuperType::_m));
162 }
163
165 {
167 (*this)[0] = a;
168 (*this)[1] = b;
169 }
170
172 {
174 (*this)[0] = a;
175 (*this)[1] = b;
176 (*this)[2] = c;
177 }
178
180 const MatrixXX<1, R, T>& b,
181 const MatrixXX<1, R, T>& c,
182 const MatrixXX<1, R, T>& d)
183 {
185 (*this)[0] = a;
186 (*this)[1] = b;
187 (*this)[2] = c;
188 (*this)[3] = c;
189 }
190
191 ThisType&
193 {
194 for (unsigned int i = 0; i < SuperType::Entries; ++i)
195 {
196 SuperType::_m[i] = v;
197 }
198 return *this;
199 }
200
201 ThisType&
203 {
204 for (unsigned int i = 0; i < SuperType::Entries; ++i)
205 {
206 SuperType::_m[i] = v._m[i];
207 }
208 return *this;
209 }
210
213 {
214 for (unsigned int i = 0; i < SuperType::Entries; ++i)
215 {
216 SuperType::_m[i] *= a._m[i];
217 }
218 return *this;
219 }
220
223 {
226 return t;
227 }
228
229 protected:
230 template <unsigned int A, unsigned int B>
232 {
233 static inline void
235 {
236#ifdef WIN32
237 Error_InvalidDimension();
238#else
239 exit(123);
240#endif
241 }
242 };
243
244 template <unsigned int A>
245 struct AssertDim<A, A>
246 {
247 static inline void
249 {
250 }
251 };
252 };
253
254 // matrix specialization
255 template <class T>
256 class MatrixXX<1, 1, T> : public Internal::BaseMatrixXX<1, 1, T>
257 {
258 public:
259 typedef Internal::BaseMatrixXX<1, 1, T> SuperType;
260 typedef MatrixXX<1, 1, T> ThisType;
261 typedef MatrixXX<1, 1, T> TransposedType;
262
263 MatrixXX()
264 {
265 }
266
267 MatrixXX(const SuperType& mat) : SuperType(mat)
268 {
269 }
270
271 MatrixXX(const MatrixXX<1, 1, T>& mat)
272 {
273 std::memcpy(SuperType::_m, mat._m, sizeof(SuperType::_m));
274 }
275
276 MatrixXX(T v)
277 {
278 SuperType::_m[0] = v;
279 }
280
281 explicit MatrixXX(const T* m)
282 {
283 std::memcpy(SuperType::_m, m, sizeof(SuperType::_m));
284 }
285
286 ThisType&
287 operator=(T v)
288 {
289 SuperType::_m[0] = v;
290 return *this;
291 }
292
295 {
296 for (unsigned int i = 0; i < SuperType::Entries; ++i)
297 {
298 SuperType::_m[i] *= a._m[i];
299 }
300 return *this;
301 }
302
303 operator const T() const
304 {
305 return SuperType::_m[0];
306 }
307
308 operator T&()
309 {
310 return SuperType::_m[0];
311 }
312 };
313
314 // matrix operator implemenation
315 template <unsigned int C, unsigned int R, class T>
316 inline MatrixXX<C, R, T>&
318 {
319 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
320 {
321 m.Data()[i] += b.Data()[i];
322 }
323 return m;
324 }
325
326 template <unsigned int C, unsigned int R, class T>
327 inline MatrixXX<C, R, T>&
329 {
330 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
331 {
332 m.Data()[i] -= b.Data()[i];
333 }
334 return m;
335 }
336
337 template <unsigned int C, unsigned int R, class T>
338 inline MatrixXX<C, R, T>&
340 {
341 m = m * b;
342 return m;
343 }
344
345 template <unsigned int C, unsigned int R, class T>
346 inline MatrixXX<C, R, T>&
348 {
349 return m *= (const T)b;
350 }
351
352 template <unsigned int C, unsigned int R, class T>
353 inline MatrixXX<C, R, T>&
355 {
356 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
357 {
358 m.Data()[i] *= s;
359 }
360 return m;
361 }
362
363 template <unsigned int C, unsigned int R, class T>
364 inline MatrixXX<C, R, T>&
366 {
367 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
368 {
369 m.Data()[i] /= s;
370 }
371 return m;
372 }
373
374 template <unsigned int C, unsigned int R, class T>
375 inline MatrixXX<C, R, T>
377 {
379 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
380 {
381 m.Data()[i] = typename MatrixXX<C, R, T>::ScalarType(-1) * a.Data()[i];
382 }
383 return m;
384 }
385
386 template <unsigned int C, unsigned int R, class T>
387 inline bool
389 {
390 return memcmp(a.Data(), b.Data(), sizeof(a)) == 0;
391 }
392
393 template <unsigned int C, unsigned int R, class T>
394 inline bool
396 {
397 return memcmp(a.Data(), b.Data(), sizeof(a)) != 0;
398 }
399
400 template <unsigned int C, unsigned int R, class T>
401 inline const MatrixXX<C, R, T>
403 {
405 c.ComponentMul(b);
406 return c;
407 }
408
409 template <unsigned int C, unsigned int R, class T>
410 inline const MatrixXX<C, R, T>
412 {
413 return ComponentMul(a, a);
414 }
415
416 template <unsigned int C, unsigned int R, class T>
417 inline const MatrixXX<C, R, T>
419 {
421 c *= s;
422 return c;
423 }
424
425 // matrix multiplication
426 template <unsigned int C, unsigned int R, unsigned int C2, class T>
427 inline const MatrixXX<C2, R, T>
429 {
431 for (int c = 0; c < C2; ++c)
432 {
433 for (int r = 0; r < R; ++r)
434 {
435 x[c][r] = a[0][r] * b[c][0];
436 for (int i = 1; i < C; ++i)
437 {
438 x[c][r] += a[i][r] * b[c][i];
439 }
440 }
441 }
442 return x;
443 }
444
445 template <unsigned int C, unsigned int R, class T>
446 inline const MatrixXX<C, R, T>
448 {
449 return ((const T)a) * b;
450 }
451
452 template <unsigned int C, unsigned int R, class T>
453 inline const MatrixXX<C, R, T>
455 {
457 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
458 {
459 c.Data()[i] = a.Data()[i] + b.Data()[i];
460 }
461 return c;
462 }
463
464 template <unsigned int C, unsigned int R, class T>
465 inline const MatrixXX<C, R, T>
467 {
469 for (unsigned int i = 0; i < MatrixXX<C, R, T>::Entries; ++i)
470 {
471 c.Data()[i] = a.Data()[i] - b.Data()[i];
472 }
473 return c;
474 }
475
476 template <unsigned int N, class T>
477 inline T
479 {
480 T t = 0;
481 for (unsigned int i = 0; i < N; ++i)
482 {
483 t += a[i][i];
484 }
485 return t;
486 }
487
488 template <unsigned int N, class T>
489 inline T
491 {
492 T t = 0;
493 for (unsigned int i = 0; i < N; ++i)
494 {
495 if (a[i][i] > 0)
496 {
497 t += a[i][i];
498 }
499 else
500 {
501 t -= a[i][i];
502 }
503 }
504 return t;
505 }
506
507 // this is a rather slow O(n!) algorithm!
508 // it is only suitable for small matrices!
509 // larger matrices should use LU decomposition
510 template <unsigned int N, class T>
511 inline T
513 {
514 int j = 1;
515 T det = 0;
516 for (unsigned int i = 0; i < N; ++i, j *= -1)
517 {
518 MatrixXX<N - 1, N - 1, T> m;
519 for (unsigned int k = 1, kk = 0; kk < N - 1; ++kk, ++k)
520 {
521 for (unsigned int l = 0, ll = 0; ll < N - 1; ++ll, ++l)
522 {
523 if (l == i)
524 {
525 ++l;
526 }
527 m[ll][kk] = a[l][k];
528 }
529 }
530 det += j * Determinant(m);
531 }
532 return det;
533 }
534
535 template <class T>
536 inline T
538 {
539 return a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]) -
540 a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]) +
541 a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
542 }
543
544 template <class T>
545 inline T
547 {
548 return a[0][0] * a[1][1] - a[1][0] * a[0][1];
549 }
550
551 template <class T>
552 inline T
554 {
555 return a[0][0];
556 }
557
558 template <class T>
559 inline void
561 {
562 (*a)[0][0] = std::cos(rad);
563 (*a)[0][1] = std::sin(rad);
564 (*a)[1][0] = -(*a)[0][1];
565 (*a)[1][1] = (*a)[0][0];
566 }
567
568 template <unsigned int N, class T>
569 void
571 {
572 for (unsigned int c = 0; c < N; ++c)
573 {
574 unsigned int r = 0;
575 for (; r < c; ++r)
576 {
577 (*a)[c][r] = 0;
578 }
579 (*a)[c][r] = 1;
580 for (++r; r < N; ++r)
581 {
582 (*a)[c][r] = 0;
583 }
584 }
585 }
586
587 template <unsigned int N, class T>
588 class IdentityMatrixX : public MatrixXX<N, N, T>
589 {
590 public:
592 {
593 Identity(this);
594 }
595 };
596
597 template <unsigned int C, unsigned int R, class T>
598 std::ostream&
599 operator<<(std::ostream& o, const MatrixXX<C, R, T>& mat)
600 {
601 o << "[";
602 for (unsigned int i = 0; i < C; ++i)
603 {
604 o << mat[i];
605 if (i < C - 1)
606 {
607 o << ", ";
608 }
609 }
610 o << "]";
611 return o;
612 }
613
614 template <unsigned int C, unsigned int R, class T>
615 inline MatrixXX<C, R, T>
617 {
618 using namespace std;
620 for (unsigned int c = 0; c < C; ++c)
621 for (unsigned int r = 0; r < R; ++r)
622 {
623 m[c][r] = min(a[c][r], b[c][r]);
624 }
625 return m;
626 }
627
628 template <unsigned int C, unsigned int R, class T>
629 inline MatrixXX<C, R, T>
631 {
632 using namespace std;
634 for (unsigned int c = 0; c < C; ++c)
635 for (unsigned int r = 0; r < R; ++r)
636 {
637 m[c][r] = max(a[c][r], b[c][r]);
638 }
639 return m;
640 }
641
651
652}; // namespace GfxTL
653
654#include <GfxTL/VectorXD.h>
655
656#endif
class A(deque< T, A >)) ARMARX_OVERLOAD_STD_HASH_FOR_ITERABLE((class T
Enables hashing of std::list.
constexpr T c
Matrix in column major order.
Definition MatrixXX.h:51
BaseMatrixXX< C, R, T > ThisType
Definition MatrixXX.h:55
const VectorXD< R, T > & operator[](unsigned int col) const
Definition MatrixXX.h:71
void Transpose(BaseMatrixXX< R, C, T > *t) const
Definition MatrixXX.h:115
VectorXD< R, T > & operator[](unsigned int col)
Definition MatrixXX.h:65
const T * Data() const
Definition MatrixXX.h:89
MatrixXX(const MatrixXX< 1, R, T > &a, const MatrixXX< 1, R, T > &b, const MatrixXX< 1, R, T > &c, const MatrixXX< 1, R, T > &d)
Definition MatrixXX.h:179
MatrixXX(const T *m)
Definition MatrixXX.h:159
ThisType & operator=(T v)
Definition MatrixXX.h:192
MatrixXX< C, R, ScalarT > ThisType
Definition MatrixXX.h:135
TransposedType Transposed()
Definition MatrixXX.h:222
MatrixXX< C, R, T > & ComponentMul(const MatrixXX< C, R, T > &a)
Definition MatrixXX.h:212
MatrixXX(const MatrixXX< 1, R, T > &a, const MatrixXX< 1, R, T > &b, const MatrixXX< 1, R, T > &c)
Definition MatrixXX.h:171
MatrixXX(const ThisType &mat)
Definition MatrixXX.h:146
MatrixXX< R, C, ScalarT > TransposedType
Definition MatrixXX.h:136
Internal::BaseMatrixXX< C, R, ScalarT > SuperType
Definition MatrixXX.h:134
ThisType & operator=(const ThisType &v)
Definition MatrixXX.h:202
MatrixXX(const SuperType &mat)
Definition MatrixXX.h:142
MatrixXX(const MatrixXX< 1, R, T > &a, const MatrixXX< 1, R, T > &b)
Definition MatrixXX.h:164
Definition AABox.h:10
const MatrixXX< C, R, T > SqrComponentMul(const MatrixXX< C, R, T > &a)
Definition MatrixXX.h:411
MatrixXX< C, R, T > min(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:616
T Determinant(const MatrixXX< N, N, T > &a)
Definition MatrixXX.h:512
const MatrixXX< C, R, T > operator+(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:454
MatrixXX< 4, 4, float > Matrix4f
Definition MatrixXX.h:650
const MatrixXX< C, R, T > operator*(T s, const MatrixXX< C, R, T > &a)
Definition MatrixXX.h:418
std::ostream & operator<<(std::ostream &o, const Array< DimT, IteratorT > &a)
Definition Array.h:189
MatrixXX< 3, 3, float > Mat3f
Definition MatrixXX.h:646
MatrixXX< 4, 4, float > Mat4f
Definition MatrixXX.h:647
MatrixXX< C, R, T > operator-(const MatrixXX< C, R, T > &a)
Definition MatrixXX.h:376
MatrixXX< 2, 2, float > Matrix2f
Definition MatrixXX.h:648
MatrixXX< C, R, T > & operator/=(MatrixXX< C, R, T > &m, T s)
Definition MatrixXX.h:365
MatrixXX< 2, 2, float > Mat2f
Definition MatrixXX.h:645
MatrixXX< C, R, T > & operator*=(MatrixXX< C, R, T > &m, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:339
bool operator!=(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:395
void Identity(MatrixXX< N, N, T > *a)
Definition MatrixXX.h:570
MatrixXX< 3, 3, float > Matrix3f
Definition MatrixXX.h:649
MatrixXX< C, R, T > max(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:630
MatrixXX< C, R, T > & operator+=(MatrixXX< C, R, T > &m, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:317
const MatrixXX< C, R, T > ComponentMul(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:402
bool operator==(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:388
T TraceAbs(const MatrixXX< N, N, T > &a)
Definition MatrixXX.h:490
T Trace(const MatrixXX< N, N, T > &a)
Definition MatrixXX.h:478
MatrixXX< 3, 3, float > mat3
Definition MatrixXX.h:643
MatrixXX< C, R, T > & operator-=(MatrixXX< C, R, T > &m, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:328
void Rotation(T rad, MatrixXX< 2, 2, T > *a)
Definition MatrixXX.h:560
MatrixXX< 2, 2, float > mat2
Definition MatrixXX.h:642
MatrixXX< 4, 4, float > mat4
Definition MatrixXX.h:644
double a(double t, double a0, double j)
Definition CtrlUtil.h:45
double v(double t, double v0, double a0, double j)
Definition CtrlUtil.h:39
This file offers overloads of toIce() and fromIce() functions for STL container types.
static void AssertEqual()
Definition MatrixXX.h:234