Allolib  1.0
C++ Components For Interactive Multimedia
al_Mat.hpp
1 #ifndef INCLUDE_AL_MAT_HPP
2 #define INCLUDE_AL_MAT_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Generic fixed-size n-by-n matrix
40 
41  File author(s):
42  Lance Putnam, 2010, putnam.lance@gmail.com
43  Graham Wakefield, 2010, grrrwaaa@gmail.com
44 */
45 
46 #include <stdio.h>
47 #include <cmath>
48 #include "al/math/al_Vec.hpp"
49 
50 namespace al {
51 
52 template <int N, class T>
53 class Mat;
54 
57 typedef Mat<2, int> Mat2i;
60 typedef Mat<3, int> Mat3i;
63 typedef Mat<4, int> Mat4i;
64 
65 // Forward iterates from 0 to n-1. Current index is 'i'.
66 #define IT(n) for (int i = 0; i < (n); ++i)
67 
69 static struct MatNoInit {
70 } MAT_NO_INIT;
71 
73 
77 template <int N, class T>
78 class Mat {
79  public:
81  T mElems[N * N];
82 
83  //--------------------------------------------------------------------------
84  // Constructors
85 
87  Mat() { set(T(0)); }
88 
90  template <class U>
91  Mat(const U* arr) {
92  set(arr);
93  }
94 
96  template <class U>
97  Mat(const Mat<N, U>& src) {
98  set(src.elems());
99  }
100 
102  Mat(const MatNoInit& v) {}
103 
105  Mat(const T& r1c1, const T& r1c2, const T& r2c1, const T& r2c2) {
106  set(r1c1, r1c2, r2c1, r2c2);
107  }
108 
110  Mat(const T& r1c1, const T& r1c2, const T& r1c3, const T& r2c1, const T& r2c2,
111  const T& r2c3, const T& r3c1, const T& r3c2, const T& r3c3) {
112  set(r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3);
113  }
114 
116  Mat(const T& r1c1, const T& r1c2, const T& r1c3, const T& r1c4, const T& r2c1,
117  const T& r2c2, const T& r2c3, const T& r2c4, const T& r3c1, const T& r3c2,
118  const T& r3c3, const T& r3c4, const T& r4c1, const T& r4c2, const T& r4c3,
119  const T& r4c4) {
120  set(r1c1, r1c2, r1c3, r1c4, r2c1, r2c2, r2c3, r2c4, r3c1, r3c2, r3c3, r3c4,
121  r4c1, r4c2, r4c3, r4c4);
122  }
123 
124  //--------------------------------------------------------------------------
125  // Factory Methods
126 
128  static Mat identity() { return Mat(MAT_NO_INIT).setIdentity(); }
129 
131  static Mat rotation(double angle, int dim1, int dim2) {
132  double cs = cos(angle);
133  double sn = sin(angle);
134 
135  Mat m(MAT_NO_INIT);
136  m.setIdentity();
137  m(dim1, dim1) = cs;
138  m(dim1, dim2) = -sn;
139  m(dim2, dim1) = sn;
140  m(dim2, dim2) = cs;
141  return m;
142  }
143 
145  template <class V>
146  static Mat scaling(const Vec<N - 1, V>& v) {
147  Mat m;
148  for (int r = 0; r < N - 1; ++r) m(r, r) = v[r];
149  m(N - 1, N - 1) = T(1);
150  return m;
151  }
152 
154  template <class V>
155  static Mat scaling(V v) {
156  return scaling(Vec<N - 1, V>(v));
157  }
158 
160  template <typename... Vals>
161  static Mat scaling(Vals... vals) {
162  return scaling(Vec<(sizeof...(Vals)), T>(vals...));
163  }
164 
166  template <class V>
167  static Mat translation(const Vec<N - 1, V>& v) {
168  Mat m(MAT_NO_INIT);
169  m.setIdentity();
170  for (int r = 0; r < N - 1; ++r) m(r, N - 1) = v[r];
171  return m;
172  }
173 
175  template <typename... Vals>
176  static Mat translation(Vals... vals) {
177  return translation(Vec<(sizeof...(Vals)), T>(vals...));
178  }
179 
180  //--------------------------------------------------------------------------
181  // Memory Operations
182 
184  static Mat& pun(T* src) { return *(Mat*)(src); }
185  static const Mat& pun(const T* src) { return *(const Mat*)(src); }
186 
188  static int size() { return N * N; }
189 
191  const T* elems() const { return mElems; }
192 
194  T* elems() { return mElems; }
195 
197  T& operator[](int i) { return elems()[i]; }
198 
200  const T& operator[](int i) const { return elems()[i]; }
201 
203  T& operator()(int i, int j) { return (*this)[j * N + i]; }
204 
206  const T& operator()(int i, int j) const { return (*this)[j * N + i]; }
207 
209  Vec<N, T> col(int i) const { return Vec<N, T>(elems() + i * N); }
210 
212  Vec<N, T> row(int i) const { return Vec<N, T>(elems() + i, N); }
213 
215  Vec<N, T> diagonal() const { return Vec<N, T>(elems(), N + 1); }
216 
219  for (int j = 0; j < N - 1; ++j) { // row and column
220  for (int i = j + 1; i < N; ++i) { // offset into row or column
221  T& a = (*this)(i, j);
222  T& b = (*this)(j, i);
223  T c = a;
224  a = b;
225  b = c; // swap elements
226  }
227  }
228  return *this;
229  }
230 
232  template <int M>
233  Mat<M, T> sub(int row = 0, int col = 0) const {
234  Mat<M, T> res(MAT_NO_INIT);
235  for (int j = 0; j < M; ++j) {
236  for (int i = 0; i < M; ++i) {
237  res(j, i) = (*this)(j + row, i + col);
238  }
239  }
240  return res;
241  }
242 
244  Mat<N - 1, T> submatrix(int row, int col) const {
245  Mat<N - 1, T> res(MAT_NO_INIT);
246  for (int j = 0, js = 0; j < N - 1; ++j, ++js) {
247  js += int(js == row);
248  for (int i = 0, is = 0; i < N - 1; ++i, ++is) {
249  is += int(is == col);
250  res(j, i) = (*this)(js, is);
251  }
252  }
253  return res;
254  }
255 
257  Vec<N * N, T>& vec() { return *(Vec<N * N, T>*)(this); }
258 
259  //--------------------------------------------------------------------------
260  // Basic Arithmetic Operations
261 
262  Mat& operator*=(const Mat& v) { return multiply(*this, Mat(*this), v); }
263  Mat& operator+=(const Mat& v) {
264  IT(size()) { (*this)[i] += v[i]; }
265  return *this;
266  }
267  Mat& operator-=(const Mat& v) {
268  IT(size()) { (*this)[i] -= v[i]; }
269  return *this;
270  }
271  Mat& operator+=(const T& v) {
272  IT(size()) { (*this)[i] += v; }
273  return *this;
274  }
275  Mat& operator-=(const T& v) {
276  IT(size()) { (*this)[i] -= v; }
277  return *this;
278  }
279  Mat& operator*=(const T& v) {
280  IT(size()) { (*this)[i] *= v; }
281  return *this;
282  }
283  Mat& operator/=(const T& v) {
284  IT(size()) { (*this)[i] /= v; }
285  return *this;
286  }
287 
288  Mat operator-() const {
289  Mat r(MAT_NO_INIT);
290  IT(size()) { r[i] = -(*this)[i]; }
291  return r;
292  }
293  Mat operator+(const Mat& v) const { return Mat(*this) += v; }
294  Mat operator-(const Mat& v) const { return Mat(*this) -= v; }
295  Mat operator*(const Mat& v) const { return Mat(*this) *= v; }
296  Mat operator+(const T& v) const { return Mat(*this) += v; }
297  Mat operator-(const T& v) const { return Mat(*this) -= v; }
298  Mat operator*(const T& v) const { return Mat(*this) *= v; }
299  Mat operator/(const T& v) const { return Mat(*this) /= v; }
300 
302 
305  static Mat& multiply(Mat& r, const Mat& a, const Mat& b) {
306  for (int j = 0; j < N; ++j) {
307  const Vec<N, T>& bcol = b.col(j);
308  for (int i = 0; i < N; ++i) {
309  r(i, j) = a.row(i).dot(bcol);
310  }
311  }
312  return r;
313  }
314 
316  template <class U>
317  static Vec<N, U>& multiply(Vec<N, U>& r, const Mat& m,
318  const Vec<N, U>& vCol) {
319  IT(N) { r[i] = m.row(i).dot(vCol); }
320  return r;
321  }
322 
324  template <class U>
325  static Vec<N, U>& multiply(Vec<N, U>& r, const Vec<N, U>& vRow,
326  const Mat& m) {
327  IT(N) { r[i] = vRow.dot(m.col(i)); }
328  return r;
329  }
330 
332  Mat& set(const T& v) {
333  IT(size()) { (*this)[i] = v; }
334  return *this;
335  }
336 
338  template <class U>
339  Mat& set(const Mat<N, U>& v) {
340  return set(v.elems());
341  }
342 
344  template <class U>
345  Mat& set(const U* arr) {
346  IT(size()) { (*this)[i] = arr[i]; }
347  return *this;
348  }
349 
351 
356  template <class U>
357  Mat& set(const U* arr, int numElements, int matOffset, int matStride = 1) {
358  IT(numElements) { (*this)[i * matStride + matOffset] = arr[i]; }
359  return *this;
360  }
361 
363  Mat& set(const T& r1c1, const T& r1c2, const T& r2c1, const T& r2c2,
364  int row = 0, int col = 0) {
365  setCol2(r1c1, r2c1, col, row);
366  setCol2(r1c2, r2c2, col + 1, row);
367  return *this;
368  }
369 
371  Mat& set(const T& r1c1, const T& r1c2, const T& r1c3, const T& r2c1,
372  const T& r2c2, const T& r2c3, const T& r3c1, const T& r3c2,
373  const T& r3c3, int row = 0, int col = 0) {
374  setCol3(r1c1, r2c1, r3c1, col, row);
375  setCol3(r1c2, r2c2, r3c2, col + 1, row);
376  setCol3(r1c3, r2c3, r3c3, col + 2, row);
377  return *this;
378  }
379 
381  Mat& set(const T& r1c1, const T& r1c2, const T& r1c3, const T& r1c4,
382  const T& r2c1, const T& r2c2, const T& r2c3, const T& r2c4,
383  const T& r3c1, const T& r3c2, const T& r3c3, const T& r3c4,
384  const T& r4c1, const T& r4c2, const T& r4c3, const T& r4c4,
385  int row = 0, int col = 0) {
386  setCol4(r1c1, r2c1, r3c1, r4c1, col, row);
387  setCol4(r1c2, r2c2, r3c2, r4c2, col + 1, row);
388  setCol4(r1c3, r2c3, r3c3, r4c3, col + 2, row);
389  setCol4(r1c4, r2c4, r3c4, r4c4, col + 3, row);
390  return *this;
391  }
392 
394  Mat& setCol2(const T& v1, const T& v2, int col = 0, int row = 0) {
395  (*this)(row, col) = v1;
396  (*this)(row + 1, col) = v2;
397  return *this;
398  }
399 
401  Mat& setCol3(const T& v1, const T& v2, const T& v3, int col = 0,
402  int row = 0) {
403  (*this)(row, col) = v1;
404  (*this)(row + 1, col) = v2;
405  (*this)(row + 2, col) = v3;
406  return *this;
407  }
408 
410  Mat& setCol4(const T& v1, const T& v2, const T& v3, const T& v4, int col = 0,
411  int row = 0) {
412  (*this)(row, col) = v1;
413  (*this)(row + 1, col) = v2;
414  (*this)(row + 2, col) = v3;
415  (*this)(row + 3, col) = v4;
416  return *this;
417  }
418 
421  for (int i = 0; i < N; ++i) (*this)[i * (N + 1)] = T(1);
422 
423  for (int i = 0; i < N - 1; ++i) {
424  for (int j = i + 1; j < N + i + 1; ++j) {
425  (*this)[i * N + j] = T(0);
426  }
427  }
428  return *this;
429  }
430 
431  //--------------------------------------------------------------------------
432  // Linear Operations
433 
435  T cofactor(int row, int col) const {
436  T minor = determinant(submatrix(row, col));
437  T cofactors[] = {minor, -minor};
438  // cofactor sign: + if row+col even, - otherwise
439  int sign = (row ^ col) & 1;
440  return cofactors[sign];
441  }
442 
445  Mat<N, T> res(MAT_NO_INIT);
446  for (int r = 0; r < N; ++r) {
447  for (int c = 0; c < N; ++c) {
448  res(r, c) = cofactor(r, c);
449  }
450  }
451  return res;
452  }
453 
455  T trace() const { return diagonal().sum(); }
456 
457  Mat<N, T> inversed() const {
458  Mat<N, T> res(*this);
459  invert(res);
460  return res;
461  }
462 
463  // Affine transformations
464 
466 
470  Mat& rotate(double angle, int dim1, int dim2) {
471  double cs = cos(angle);
472  double sn = sin(angle);
473  for (int R = 0; R < N - 1; ++R) {
474  const T& v1 = (*this)(R, dim1);
475  const T& v2 = (*this)(R, dim2);
476  T t = v1 * cs + v2 * sn;
477  (*this)(R, dim2) = v2 * cs - v1 * sn;
478  (*this)(R, dim1) = t;
479  }
480  return *this;
481  }
482 
484  template <int M>
485  Mat& rotateGlobal(double angle, int dim1, int dim2) {
486  double cs = cos(angle);
487  double sn = sin(angle);
488  for (int C = 0; C < M; ++C) {
489  const T& v1 = (*this)(dim1, C);
490  const T& v2 = (*this)(dim2, C);
491  T t = v1 * cs - v2 * sn;
492  (*this)(dim2, C) = v2 * cs + v1 * sn;
493  (*this)(dim1, C) = t;
494  }
495  return *this;
496  }
497 
499  Mat& rotateGlobal(double angle, int dim1, int dim2) {
500  return rotateGlobal<N - 1>(angle, dim1, dim2);
501  }
502 
504  template <class V>
505  Mat& scale(const Vec<N - 1, V>& amount) {
506  for (int C = 0; C < N - 1; ++C) {
507  for (int R = 0; R < N - 1; ++R) {
508  (*this)(R, C) *= amount[C];
509  }
510  }
511  return *this;
512  }
513 
515  template <class V>
516  Mat& scale(const V& amount) {
517  return scale(Vec<N - 1, V>(amount));
518  }
519 
521  template <typename... Vals>
522  Mat& scale(Vals... vals) {
523  return scale(Vec<(sizeof...(Vals)), T>(vals...));
524  }
525 
527  template <class V>
528  Mat& scaleGlobal(const Vec<N - 1, V>& amount) {
529  for (int R = 0; R < N - 1; ++R) {
530  for (int C = 0; C < N - 1; ++C) {
531  (*this)(R, C) *= amount[R];
532  }
533  }
534  return *this;
535  }
536 
538  template <class V>
539  Mat& translate(const Vec<N - 1, V>& amount) {
540  for (int R = 0; R < N - 1; ++R) {
541  (*this)(R, N - 1) += amount[R];
542  }
543  return *this;
544  }
545 
547  template <class V>
548  Mat& translate(const V& amount) {
549  return translate(Vec<N - 1, V>(amount));
550  }
551 
553  template <typename... Vals>
554  Mat& translate(Vals... vals) {
555  return translate(Vec<(sizeof...(Vals)), T>(vals...));
556  }
557 
559  void print(std::ostream& stream) const;
560 };
561 
562 // -----------------------------------------------------------------------------
563 // The following are functions that either cannot be defined as class methods
564 // (due to syntax rules or specialization) or simply are not object oriented.
565 
566 // Non-member binary arithmetic operations
567 template <int N, class T>
568 inline Mat<N, T> operator+(const T& s, const Mat<N, T>& v) {
569  return v + s;
570 }
571 
572 template <int N, class T>
573 inline Mat<N, T> operator-(const T& s, const Mat<N, T>& v) {
574  return -v + s;
575 }
576 
577 template <int N, class T>
578 inline Mat<N, T> operator*(const T& s, const Mat<N, T>& v) {
579  return v * s;
580 }
581 
582 // Basic Vec/Mat Arithmetic
583 template <int N, class T, class U>
584 inline Vec<N, U> operator*(const Mat<N, T>& m, const Vec<N, U>& vCol) {
585  Vec<N, U> r;
586  return Mat<N, T>::multiply(r, m, vCol);
587 }
588 
589 template <int N, class T, class U>
590 inline Vec<N, U> operator*(const Vec<N, U>& vRow, const Mat<N, T>& m) {
591  Vec<N, U> r;
592  return Mat<N, T>::multiply(r, vRow, m);
593 }
594 
598 template <class T>
599 T determinant(const Mat<1, T>& m) {
600  return m(0, 0);
601 }
602 
606 template <class T>
607 T determinant(const Mat<2, T>& m) {
608  return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
609 }
610 
614 template <class T>
615 T determinant(const Mat<3, T>& m) {
616  return m(0, 0) * (m(1, 1) * m(2, 2) - m(1, 2) * m(2, 1)) +
617  m(0, 1) * (m(1, 2) * m(2, 0) - m(1, 0) * m(2, 2)) +
618  m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0));
619 }
620 
622 
627 template <int N, class T>
628 T determinant(const Mat<N, T>& m) {
629  T res = 0;
630  for (int i = 0; i < N; ++i) {
631  T entry = m(0, i);
632  if (entry != T(0)) {
633  res += entry * m.cofactor(0, i);
634  }
635  }
636  return res;
637 }
638 
642 template <class T>
643 bool invert(Mat<1, T>& m) {
644  T det = determinant(m);
645  if (det != 0) {
646  m(0, 0) /= det;
647  return true;
648  }
649  return false;
650 }
651 
655 template <class T>
656 bool invert(Mat<2, T>& m) {
657  T det = determinant(m);
658  if (det != 0) {
659  m = Mat<2, T>(m(1, 1), -m(0, 1), -m(1, 0), m(0, 0)) /= det;
660  return true;
661  }
662  return false;
663 }
664 
668 template <int N, class T>
669 bool invert(Mat<N, T>& m) {
670  // Get cofactor matrix, C
671  Mat<N, T> C = m.cofactorMatrix();
672 
673  // Compute determinant
674  T det = T(0);
675  for (int i = 0; i < N; ++i) {
676  det += m(0, i) * C(0, i);
677  }
678 
679  // Divide adjugate matrix, C^T, by determinant
680  if (det != T(0)) {
681  m = (C.transpose() *= T(1) / det);
682  return true;
683  }
684 
685  return false;
686 }
687 
688 //----------------------------
689 // Member function definitions
690 
691 template <int N, class T>
692 void Mat<N, T>::print(std::ostream& stream) const {
693  for (int R = 0; R < N; ++R) {
694  stream << (R == 0 ? " {" : "");
695  for (int C = 0; C < N; ++C) {
696  stream << "% " << double((*this)(R, C))
697  << ((R != N - 1) || (C != N - 1) ? ", " : "}");
698  }
699  stream << std::endl;
700  }
701 }
702 
703 #undef IT
704 } // namespace al
705 #endif
Fixed-size n-by-n square matrix.
Definition: al_Mat.hpp:78
Mat & set(const T &r1c1, const T &r1c2, const T &r1c3, const T &r2c1, const T &r2c2, const T &r2c3, const T &r3c1, const T &r3c2, const T &r3c3, int row=0, int col=0)
Set 3-by-3 (sub)matrix from arguments.
Definition: al_Mat.hpp:371
Mat & setCol4(const T &v1, const T &v2, const T &v3, const T &v4, int col=0, int row=0)
Set a (sub)column.
Definition: al_Mat.hpp:410
static Vec< N, U > & multiply(Vec< N, U > &r, const Mat &m, const Vec< N, U > &vCol)
Computes product of matrix multiplied by column vector, r = m * vCol.
Definition: al_Mat.hpp:317
Mat & set(const U *arr)
Set elements in column-major order from C array.
Definition: al_Mat.hpp:345
const T * elems() const
Get read-only pointer to elements.
Definition: al_Mat.hpp:191
Mat(const U *arr)
Definition: al_Mat.hpp:91
Mat & set(const U *arr, int numElements, int matOffset, int matStride=1)
Set elements in column-major order from C array.
Definition: al_Mat.hpp:357
T & operator()(int i, int j)
Set element at row i, column j.
Definition: al_Mat.hpp:203
static Mat scaling(const Vec< N - 1, V > &v)
Get a scaling transform matrix.
Definition: al_Mat.hpp:146
static Mat translation(const Vec< N - 1, V > &v)
Get a translation transform matrix.
Definition: al_Mat.hpp:167
Mat< N - 1, T > submatrix(int row, int col) const
Returns a submatrix by removing one row and column.
Definition: al_Mat.hpp:244
Vec< N, T > diagonal() const
Return diagonal.
Definition: al_Mat.hpp:215
static Mat rotation(double angle, int dim1, int dim2)
Get a rotation transform matrix.
Definition: al_Mat.hpp:131
Mat(const Mat< N, U > &src)
Definition: al_Mat.hpp:97
const T & operator()(int i, int j) const
Get element at row i, column j.
Definition: al_Mat.hpp:206
Mat & setIdentity()
Set elements on diagonal to one and all others to zero.
Definition: al_Mat.hpp:420
void print(std::ostream &stream) const
Print to file (stream)
Definition: al_Mat.hpp:692
Mat & scale(const V &amount)
Scale transformation matrix by uniform amount.
Definition: al_Mat.hpp:516
Mat & translate(const V &amount)
Translate transformation matrix by same amount in all directions.
Definition: al_Mat.hpp:548
Mat(const T &r1c1, const T &r1c2, const T &r1c3, const T &r2c1, const T &r2c2, const T &r2c3, const T &r3c1, const T &r3c2, const T &r3c3)
3x3 matrix constructor with element initialization
Definition: al_Mat.hpp:110
static Mat scaling(Vals... vals)
Get a scaling transform matrix.
Definition: al_Mat.hpp:161
Mat & set(const T &r1c1, const T &r1c2, const T &r2c1, const T &r2c2, int row=0, int col=0)
Set 2-by-2 (sub)matrix from arguments.
Definition: al_Mat.hpp:363
static Mat identity()
Get identity matrix.
Definition: al_Mat.hpp:128
Mat & rotate(double angle, int dim1, int dim2)
Rotate transformation matrix on a local plane (A' = AR)
Definition: al_Mat.hpp:470
Mat & transpose()
Transpose elements.
Definition: al_Mat.hpp:218
Vec< N, T > row(int i) const
Return row i as vector.
Definition: al_Mat.hpp:212
T & operator[](int i)
Set element at index with no bounds checking.
Definition: al_Mat.hpp:197
Mat(const T &r1c1, const T &r1c2, const T &r2c1, const T &r2c2)
2x2 matrix constructor with element initialization
Definition: al_Mat.hpp:105
static int size()
Returns total number of elements.
Definition: al_Mat.hpp:188
static Mat & pun(T *src)
Returns C array type punned into a matrix.
Definition: al_Mat.hpp:184
Mat & setCol3(const T &v1, const T &v2, const T &v3, int col=0, int row=0)
Set a (sub)column.
Definition: al_Mat.hpp:401
Mat & set(const T &r1c1, const T &r1c2, const T &r1c3, const T &r1c4, const T &r2c1, const T &r2c2, const T &r2c3, const T &r2c4, const T &r3c1, const T &r3c2, const T &r3c3, const T &r3c4, const T &r4c1, const T &r4c2, const T &r4c3, const T &r4c4, int row=0, int col=0)
Set 4-by-4 (sub)matrix from arguments.
Definition: al_Mat.hpp:381
static Mat & multiply(Mat &r, const Mat &a, const Mat &b)
Computes matrix product r = a * b.
Definition: al_Mat.hpp:305
Mat & setCol2(const T &v1, const T &v2, int col=0, int row=0)
Set a (sub)column.
Definition: al_Mat.hpp:394
Mat & scale(const Vec< N - 1, V > &amount)
Scale transformation matrix.
Definition: al_Mat.hpp:505
Mat< M, T > sub(int row=0, int col=0) const
Get an MxM submatrix.
Definition: al_Mat.hpp:233
Mat & rotateGlobal(double angle, int dim1, int dim2)
Rotate submatrix on a global plane (A' = RA)
Definition: al_Mat.hpp:485
Mat(const T &r1c1, const T &r1c2, const T &r1c3, const T &r1c4, const T &r2c1, const T &r2c2, const T &r2c3, const T &r2c4, const T &r3c1, const T &r3c2, const T &r3c3, const T &r3c4, const T &r4c1, const T &r4c2, const T &r4c3, const T &r4c4)
4x4 matrix constructor with element initialization
Definition: al_Mat.hpp:116
static Mat translation(Vals... vals)
Get a translation transform matrix.
Definition: al_Mat.hpp:176
const T & operator[](int i) const
Get element at index with no bounds checking.
Definition: al_Mat.hpp:200
Vec< N, T > col(int i) const
Return column i as vector.
Definition: al_Mat.hpp:209
static Vec< N, U > & multiply(Vec< N, U > &r, const Vec< N, U > &vRow, const Mat &m)
Computes product of row vector multiplied by matrix, r = vRow * m.
Definition: al_Mat.hpp:325
T * elems()
Get read-write pointer to elements.
Definition: al_Mat.hpp:194
Mat()
Default constructor that initializes elements to zero.
Definition: al_Mat.hpp:87
Mat & set(const T &v)
Set all elements to value.
Definition: al_Mat.hpp:332
Mat & scale(Vals... vals)
Scale transformation matrix.
Definition: al_Mat.hpp:522
Mat & set(const Mat< N, U > &v)
Set elements from another matrix.
Definition: al_Mat.hpp:339
Mat(const MatNoInit &v)
Construct without initializing elements.
Definition: al_Mat.hpp:102
Mat & translate(const Vec< N - 1, V > &amount)
Translate transformation matrix.
Definition: al_Mat.hpp:539
Vec< N *N, T > & vec()
Return matrix punned as a vector.
Definition: al_Mat.hpp:257
T trace() const
Get trace (sum of diagonal elements)
Definition: al_Mat.hpp:455
Mat< N, T > cofactorMatrix() const
Get cofactor matrix.
Definition: al_Mat.hpp:444
Mat & translate(Vals... vals)
Translate transformation matrix.
Definition: al_Mat.hpp:554
T cofactor(int row, int col) const
Get cofactor.
Definition: al_Mat.hpp:435
T mElems[N *N]
Column-major array of elements.
Definition: al_Mat.hpp:81
static Mat scaling(V v)
Get a scaling transform matrix.
Definition: al_Mat.hpp:155
Mat & scaleGlobal(const Vec< N - 1, V > &amount)
Scale transformation matrix global coordinates.
Definition: al_Mat.hpp:528
Mat & rotateGlobal(double angle, int dim1, int dim2)
Rotate transformation matrix on a global plane (A' = RA)
Definition: al_Mat.hpp:499
Fixed-size n-vector.
Definition: al_Vec.hpp:117
T dot(const Vec< N, U > &v) const
Returns dot (inner) product between vectors.
Definition: al_Vec.hpp:406
Definition: al_App.hpp:23
bool invert(Mat< 1, T > &m)
Definition: al_Mat.hpp:643
Mat< 4, int > Mat4i
integer 4x4 matrix
Definition: al_Mat.hpp:63
Mat< 3, float > Mat3f
float 3x3 matrix
Definition: al_Mat.hpp:58
Mat< 3, int > Mat3i
integer 3x3 matrix
Definition: al_Mat.hpp:60
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
Definition: al_Vec.hpp:634
Mat< 2, double > Mat2d
double 2x2 matrix
Definition: al_Mat.hpp:56
Mat< 2, int > Mat2i
integer 2x2 matrix
Definition: al_Mat.hpp:57
Mat< 4, double > Mat4d
double 4x4 matrix
Definition: al_Mat.hpp:62
Mat< 3, double > Mat3d
double 3x3 matrix
Definition: al_Mat.hpp:59
Mat< 2, float > Mat2f
float 2x2 matrix
Definition: al_Mat.hpp:53
T determinant(const Mat< 1, T > &m)
Definition: al_Mat.hpp:599
Mat< 4, float > Mat4f
float 4x4 matrix
Definition: al_Mat.hpp:61