Matrix.h
Go to the documentation of this file.
1 /********************************************************************
2  *
3  * Project Artec 3D Scanning SDK
4  *
5  * Purpose: Definition of template matrix classes for use in
6  * geometric transformations
7  *
8  * Copyright: Artec Group
9  *
10  ********************************************************************/
11 
12 #ifndef _MATRIX_H_
13 #define _MATRIX_H_
14 
16 
17 namespace artec { namespace sdk { namespace base
18 {
19 
20 template< typename Type = double >
21 class Matrix4x4
22 {
23 public:
24  typedef Type value_type;
25 
27 
28  Matrix4x4(Type _00, Type _01, Type _02, Type _03,
29  Type _10, Type _11, Type _12, Type _13,
30  Type _20, Type _21, Type _22, Type _23,
31  Type _30, Type _31, Type _32, Type _33)
32  {
33  m[0][0] = _00; m[0][1] = _01; m[0][2] = _02; m[0][3] = _03;
34  m[1][0] = _10; m[1][1] = _11; m[1][2] = _12; m[1][3] = _13;
35  m[2][0] = _20; m[2][1] = _21; m[2][2] = _22; m[2][3] = _23;
36  m[3][0] = _30; m[3][1] = _31; m[3][2] = _32; m[3][3] = _33;
37 
38  flagBits = General;
39  }
40 
41  template< typename Type2, std::size_t sizeOfArray >
42  explicit Matrix4x4(const Type2 (&values)[sizeOfArray])
43  {
44  int c_assert[size_ == sizeOfArray ? 1 : -1];
45  (c_assert);
46  for(int i = 0; i < size_; i++)
47  data[i] = values[i];
48  flagBits = General;
49  }
50 
51  template< typename Type2 >
52  Matrix4x4(const Type2 * values, int rows2, int cols2)
53  {
54  for (int matrixRow = 0; matrixRow < rows_; ++matrixRow) {
55  for (int matrixCol = 0; matrixCol < cols_; ++matrixCol) {
56  if (matrixCol < cols2 && matrixRow < rows2)
57  m[matrixRow][matrixCol] = values[matrixRow * cols2 + matrixCol];
58  else if (matrixCol == matrixRow)
59  m[matrixRow][matrixCol] = Type(1);
60  else
61  m[matrixRow][matrixCol] = Type(0);
62  }
63  }
64  flagBits = General;
65  }
66 
67  /// Copying constructor
68  Matrix4x4(const Matrix4x4 & second) { *this = second; }
69 
70  /// Copying constructor with size_ conversion
71  template< int rows2, int cols2, typename Type2 >
72  explicit Matrix4x4(const GenericMatrix< rows2, cols2, Type2 > & second) { *this = second; }
73 
74  /// Copying constructor with type conversion
75  template< typename Type2 >
76  explicit Matrix4x4(const Matrix4x4< Type2 > & second)
77  {
78  for(int i = 0; i < size_; i++) data[i] = (Type)second.data[i];
79  flagBits = second.flagBits;
80  }
81 
82  ///@{
83  /// Assignment operators
84  template< int rows2, int cols2, typename Type2 >
86  {
87  for (int matrixRow = 0; matrixRow < rows_; ++matrixRow) {
88  for (int matrixCol = 0; matrixCol < cols_; ++matrixCol) {
89  if (matrixCol < cols2 && matrixRow < rows2)
90  m[matrixRow][matrixCol] = second.m[matrixRow][matrixCol];
91  else if (matrixCol == matrixRow)
92  m[matrixRow][matrixCol] = Type(1);
93  else
94  m[matrixRow][matrixCol] = Type(0);
95  }
96  }
97  flagBits = General;
98  return *this;
99  }
100 
101  Matrix4x4 & operator=(const Matrix4x4 & second)
102  {
103  memcpy(data, second.data, size_*sizeof(Type));
104  flagBits = second.flagBits;
105  return *this;
106  }
107  ///@}
108 
109  ///@{
110  /// Convert matrix4x4 to generic matrix
111  template <int rows, int cols>
113  {
114  return toGenericMatrix<rows, cols, Type>();
115  }
116 
117  template <int rows, int cols, typename Type2>
119  {
121  for (int matrixRow = 0; matrixRow < rows; ++matrixRow) {
122  for (int matrixCol = 0; matrixCol < cols; ++matrixCol) {
123  if (matrixRow < 4 && matrixCol < 4)
124  result.m[matrixRow][matrixCol] = m[matrixRow][matrixCol];
125  else if (matrixCol == matrixRow)
126  result.m[matrixRow][matrixCol] = Type2(1);
127  else
128  result.m[matrixRow][matrixCol] = Type2(0);
129  }
130  }
131  return result;
132  }
133  ///@}
134 
135  /// Fill matrix with value
136  void fill(const Type value)
137  {
138  for(int i = 0; i < size_; i++)
139  data[i] = value;
140  flagBits = General;
141  }
142 
143  ///@{
144  /// Arithmetical operations
145  /// Matrix - Matrix (element-wise)
147  {
148  for(int i = 0; i < size_; i++) data[i] += mat.data[i];
149  flagBits = General;
150  return *this;
151  }
153  {
154  for(int i = 0; i < size_; i++) data[i] -= mat.data[i];
155  flagBits = General;
156  return *this;
157  }
158  Matrix4x4 & operator*=(const Matrix4x4 & other)
159  {
160  if (other.flagBits == Identity) {
161  return *this;
162  }
163  else if (flagBits == Identity) {
164  *this = other;
165  return *this;
166  }
167  *this = *this * other;
168  return *this;
169  }
170 
171  Matrix4x4 operator+(const Matrix4x4 & mat) const
172  {
173  Matrix4x4 m = *this;
174  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] + mat.data[i];
175  m.flagBits = General;
176  return m;
177  }
178 
179  Matrix4x4 operator-(const Matrix4x4 & mat) const
180  {
181  Matrix4x4 m = *this;
182  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] - mat.data[i];
183  m.flagBits = General;
184  return m;
185  }
186  ///@}
187 
188  ///@{
189  /// Matrix - matrix multiplication
190  Matrix4x4 operator*(const Matrix4x4 & mat) const
191  {
192  if (flagBits == Identity)
193  return mat;
194  else if (mat.flagBits == Identity)
195  return *this;
196 
197  Matrix4x4 res;
198  for(int i = 0; i < rows_; i++)
199  for(int j = 0; j < cols_; j++)
200  {
201  Type sum = 0;
202  for(int k = 0; k < cols_; k++)
203  sum += m[i][k]*mat.m[k][j];
204 
205  res.m[i][j] = sum;
206  }
207  return res;
208  }
209 
210  template< int cols2 >
212  {
214  for(int i = 0; i < rows_; i++)
215  for(int j = 0; j < cols2; j++)
216  {
217  Type sum = 0;
218  for(int k = 0; k < cols_; k++)
219  sum += m[i][k]*mat.m[k][j];
220 
221  res.m[i][j] = sum;
222  }
223  return res;
224  }
225  ///@}
226 
227  /// Matrix - point multiplication
228  template <typename Type2>
230  {
232  return point;
233 
234  Point4<Type2> res;
235  for(int i = 0; i < 4; i++)
236  {
237  double sum = 0.0;
238  for(int j = 0; j < 4; j++)
239  sum += m[i][j]*point.data[j];
240 
241  res.data[i] = (Type2)sum;
242  }
243 
244  return res;
245  }
246 
247  Matrix4x4 operator*(const Type & val) const
248  {
249  Matrix4x4 m = *this;
250  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] * val;
251  m.flagBits = General;
252  return m;
253  }
254  /// Special case of matrix-point multiplication: geometric transformation
255  template <typename Type2>
257  {
259  return point;
260 
261  Point3< Type2 > res;
262  for(int i = 0; i < 3; i++)
263  {
264  double sum = 0.0;
265  for(int j = 0; j < 3; j++)
266  sum += m[i][j]*point.data[j];
267 
268  sum += m[i][3];
269  res.data[i] = (Type2)sum;
270  }
271 
272  return res;
273  }
274 
275  ///@{
276  /// Matrix - Scalar
277  Matrix4x4 & operator+=(const Type & val)
278  {
279  for(int i = 0; i < size_; i++) data[i] += val;
280  flagBits = General;
281  return *this;
282  }
283  Matrix4x4 & operator-=(const Type & val)
284  {
285  for(int i = 0; i < size_; i++) data[i] -= val;
286  flagBits = General;
287  return *this;
288  }
289  Matrix4x4 & operator*=(const Type & val)
290  {
291  for(int i = 0; i < size_; i++) data[i] *= val;
292  flagBits = General;
293  return *this;
294  }
295  Matrix4x4 & operator/=(const Type & val)
296  {
297  for(int i = 0; i < size_; i++) data[i] /= val;
298  flagBits = General;
299  return *this;
300  }
301 
302  Matrix4x4 operator+(const Type & val) const
303  {
304  Matrix4x4 m = *this;
305  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] + val;
306  m.flagBits = General;
307  return m;
308  }
309  Matrix4x4 operator-(const Type & val) const
310  {
311  Matrix4x4 m = *this;
312  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] - val;
313  m.flagBits = General;
314  return m;
315  }
316  Matrix4x4 operator/(const Type & val) const
317  {
318  Matrix4x4 m = *this;
319  for(int i = 0; i < size_; i++) m.data[i] = m.data[i] / val;
320  m.flagBits = General;
321  return m;
322  }
323  ///@}
324 
325  /// Unary minus
327  {
328  Matrix4x4 m = *this;
329  for(int i = 0; i < size_; i++) m.data[i] = -m.data[i];
330  flagBits = General;
331  return m;
332  }
333 
334  /// check matrices are equal
335  bool operator==(const Matrix4x4& m)const
336  {
337  for(int i = 0; i < size_; i++)
338  if (data[i] != m.data[i])
339  return false;
340  return true;
341  }
342 
343  /// check matrices are not equal
344  bool operator!=(const Matrix4x4& m)const
345  {
346  return !(*this == m);
347  }
348 
349  Type& operator()(int row, int col)
350  {
351  flagBits = General;
352  return m[row][col];
353  }
354 
355  const Type& operator()(int row, int col) const
356  {
357  return m[row][col];
358  }
359 
360  /// Identity matrix
362  {
363  Matrix4x4 m;
364  m.setToIdentity();
365  return m;
366  }
367  /// Make current matrix to identity
369  {
370  for(int x = 0; x < rows_; x++)
371  for(int y = 0; y < cols_; y++)
372  {
373  m[x][y] = x != y ? Type() : Type(1.0);
374  }
375  flagBits = Identity;
376  }
377 
378  /// Check if matrix is an identity matrix
379  bool isIdentity() const
380  {
381  if (flagBits == Identity)
382  return true;
383 
384  if (m[0][0] != Type(1.0) || m[0][1] != Type() || m[0][2] != Type() || m[0][3] != Type() )
385  return false;
386 
387  if (m[1][0] != Type() || m[1][1] != Type(1.0) || m[1][2] != Type() || m[1][3] != Type() )
388  return false;
389 
390  if (m[2][0] != Type() || m[2][1] != Type() || m[2][2] != Type(1.0) || m[2][3] != Type() )
391  return false;
392 
393  if (m[3][0] != Type() || m[3][1] != Type() || m[3][2] != Type() || m[3][3] != Type(1.0) )
394  return false;
395 
396  flagBits = Identity;
397  return true;
398  }
399 
400  /// Return transposed matrix copy
402  {
403  Matrix4x4 res;
404  for(size_t i = 0; i < rows_; i++)
405  for(size_t j = 0; j < cols_; j++)
406  res.m[i][j] = m[j][i];
407 
408  return res;
409  }
410 
411  /// Inversion
412  /// @return false if matrix is singular and can't be inverted, otherwise true
413  Matrix4x4< Type > inverted(bool *invertible = 0) const;
414 
415  Point3<Type> project(const Point3<Type> & point) const
416  {
417  Point3<Type> pr = *this * point;
418 
419  Type m4sum = 0;
420  for(size_t j = 0; j < 3; j++)
421  m4sum += m.m[3][j] * point.data[j];
422  m4sum += m.m[3][3];
423 
424  for(size_t j = 0; j < 3; j++)
425  pr.data[j] = Type(pr.data[j]/m4sum);
426 
427  return pr;
428  }
429 
430  ///@{
431  /// Rotations (* - all angles in radians)
432  static Matrix4x4 rotationX(Type angle)
433  {
434  Matrix4x4 r;
435  r.fill(0);
436 
437  Type c = std::cos(angle);
438  Type s = std::sin(angle);
439 
440  r.m[1][1] = r.m[2][2] = c;
441  r.m[1][2] = -s; r.m[2][1] = s;
442  r.m[0][0] = r.m[3][3] = 1;
443 
444  return r;
445  }
446  static Matrix4x4 rotationY(Type angle)
447  {
448  Matrix4x4 r;
449  r.fill(0);
450 
451  Type c = std::cos(angle);
452  Type s = std::sin(angle);
453 
454  r.m[0][0] = r.m[2][2] = c;
455  r.m[0][2] = s; r.m[2][0] = -s;
456  r.m[1][1] = r.m[3][3] = 1;
457 
458  return r;
459  }
460  static Matrix4x4 rotationZ(Type angle)
461  {
462  Matrix4x4 r;
463  r.fill(0);
464 
465  Type c = std::cos(angle);
466  Type s = std::sin(angle);
467 
468  r.m[0][0] = r.m[1][1] = c;
469  r.m[0][1] = -s; r.m[1][0] = s;
470  r.m[2][2] = r.m[3][3] = 1;
471 
472  return r;
473  }
474 
475  static Matrix4x4 rotationX(Type angle, const Point3<Type> & center)
476  {
477  Matrix4x4 r = rotationX(angle);
478 
479  Point3<Type> t = -r*center+center;
480  r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z;
481 
482  return r;
483  }
484  static Matrix4x4 rotationY(Type angle, const Point3<Type> & center)
485  {
486  Matrix4x4 r = rotationY(angle);
487 
488  Point3<Type> t = -r*center+center;
489  r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z;
490 
491  return r;
492  }
493  static Matrix4x4 rotationZ(Type angle, const Point3<Type> & center)
494  {
495  Matrix4x4 r = rotationZ(angle);
496 
497  Point3<Type> t = -r*center+center;
498  r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z;
499 
500  return r;
501  }
502  ///@}
503 
504  /// ! Important ! Direction is a unit vector, don't forget to normalize it !!!
505  static Matrix4x4 rotation(Type angle, const Point3<Type> & direction)
506  {
507  Matrix4x4 r;
508 
509  Type c = std::cos(angle);
510  Type s = std::sin(angle);
511 
512  r.m[0][0] = c+(1-c)*direction.x*direction.x;
513  r.m[0][1] = (1-c)*direction.y*direction.x-s*direction.z;
514  r.m[0][2] = (1-c)*direction.z*direction.x+s*direction.y;
515 
516  r.m[1][0] = (1-c)*direction.x*direction.y+s*direction.z;
517  r.m[1][1] = c+(1-c)*direction.y*direction.y;
518  r.m[1][2] = (1-c)*direction.z*direction.y-s*direction.x;
519 
520  r.m[2][0] = (1-c)*direction.x*direction.z-s*direction.y;
521  r.m[2][1] = (1-c)*direction.y*direction.z+s*direction.x;
522  r.m[2][2] = c+(1-c)*direction.z*direction.z;
523 
524  r.m[0][3] = r.m[1][3] = r.m[2][3] = 0;
525  r.m[3][0] = r.m[3][1] = r.m[3][2] = 0; r.m[3][3] = 1;
526 
527  return r;
528  }
529 
530  /// ! Important ! Direction is a unit vector, don't forget to normalize it !!!
531  static Matrix4x4 rotation(Type angle, const Point3<Type> & direction, const Point3<Type> & center)
532  {
533  Matrix4x4 r = rotation(angle, direction);
534 
535  Point3<Type> t = -r*center+center;
536  r.m[0][3] = t.x; r.m[1][3] = t.y; r.m[2][3] = t.z;
537 
538  return r;
539  }
540 
541  /// Translations
542  static Matrix4x4 translation(const Point3<Type> & direction)
543  {
544  Matrix4x4 t;
545  t.setToIdentity();
546 
547  t.m[0][3] = direction.x; t.m[1][3] = direction.y; t.m[2][3] = direction.z;
548  t.flagBits = General;
549 
550  return t;
551  }
552 
553  ///@{
554  /// Scale
555  static Matrix4x4 scale(Type factor)
556  {
557  Matrix4x4 s;
558  s.fill(Type(0.0));
559 
560  s.m[0][0] = s.m[1][1] = s.m[2][2] = factor;
561  s.m[3][3] = 1;
562 
563  return s;
564  }
565  static Matrix4x4 scale(Type factor, const Point3<Type> & center)
566  {
567  Matrix4x4 s = scale(factor);
568 
569  Point3<Type> t = -center*factor + center;
570  s.m[0][3] = t.x; s.m[1][3] = t.y; s.m[2][3] = t.z;
571 
572  return s;
573  }
574  ///@}
575 
576  /// Inverse transformation matrix
577  static Matrix4x4 inverseMotion(const Matrix4x4 &matrix)
578  {
579  /// get rotation and translation components of motion
580  GenericMatrix<3,3,Type> r(matrix);
581  Point3<Type> t( matrix.m[0][3], matrix.m[1][3], matrix.m[2][3] );
582 
583  /// inverse rotation
584  r = transpose(r);
585 
586  /// assemble inverse motion matrix
587  Matrix4x4 i(r);
588  Point3<Type> it = -r*t;
589 
590  i.m[0][3] = it.x; i.m[1][3] = it.y; i.m[2][3] = it.z;
591  i.m[3][0] = i.m[3][1] = i.m[3][2] = 0; i.m[3][3] = 1;
592 
593  return i;
594  }
595 
596  /// Get perspective projection matrix
597  static Matrix4x4 perspective(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
598  {
599  Matrix4x4 res;
600  res.m[0][0] = 2.0f * nearVal / (right - left);
601  res.m[0][1] = 0.0;
602  res.m[0][2] = (right + left) / (right - left);
603  res.m[0][3] = 0.0;
604  res.m[1][0] = 0.0;
605  res.m[1][1] = 2.0f * nearVal / (top - bottom);
606  res.m[1][2] = (top + bottom) / (top - bottom);
607  res.m[1][3] = 0.0;
608  res.m[2][0] = 0.0;
609  res.m[2][1] = 0.0;
610  res.m[2][2] = -(farVal + nearVal) / (farVal - nearVal);
611  res.m[2][3] = -2.0f * farVal * nearVal / (farVal - nearVal);
612  res.m[3][0] = 0.0;
613  res.m[3][1] = 0.0;
614  res.m[3][2] = -1.0f;
615  res.m[3][3] = 0.0;
616  return res;
617  }
618 
619  /// Get perspective projection matrix
620  static Matrix4x4 perspectiveFov(float fovy, float aspect, float zNear, float zFar)
621  {
622  float f = 1.0f / tan(fovy * 0.5f);
623  float dneg = zNear - zFar;
624  return Matrix4x4( f / aspect, 0.0f, 0.0f, 0.0f,
625  0.0f, f, 0.0f, 0.0f,
626  0.0f, 0.0f, (zFar + zNear)/dneg, 2.0f*zNear*zFar/dneg,
627  0.0f, 0.0f, -1.0f, 0.0f );
628  }
629 
630  /// Get orthogonal matrix
631  static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
632  {
633  Matrix4x4 res;
634  res.m[0][0] = 2.0f / (right - left);
635  res.m[0][1] = 0.0;
636  res.m[0][2] = 0.0;
637  res.m[0][3] = -(right + left) / (right - left);
638  res.m[1][0] = 0.0;
639  res.m[1][1] = 2.0f / (top - bottom);
640  res.m[1][2] = 0.0;
641  res.m[1][3] = -(top + bottom) / (top - bottom);
642  res.m[2][0] = 0.0;
643  res.m[2][1] = 0.0;
644  res.m[2][2] = -2.0f / (farVal - nearVal);
645  res.m[2][3] = -(farVal + nearVal) / (farVal - nearVal);
646  res.m[3][0] = 0.0;
647  res.m[3][1] = 0.0;
648  res.m[3][2] = 0.0;
649  res.m[3][3] = 1.0f;
650  return res;
651  }
652 
653  ///@{
654  /// Conversion to plain data pointer operator
655  operator Type * () { flagBits = General; return data; }
656  operator const Type * () const { return data; }
657  ///@}
658  /// Returns the number of elements in the matrix
659  int size() const { return size_; }
660 
661  /// matrix data
662  union
663  {
664  struct
665  {
666  Type m[4][4];
667  };
668 
669  Type data[4*4];
670  };
671 
672  mutable int flagBits; // Flag bits from the enum below.
673 
674  enum {
675  Identity = 0x0001, // Identity matrix
676  General = 0x0002, // General matrix, unknown contents
677  };
678 
679 protected:
680 
681  static int const rows_;
682  static int const cols_;
683  static int const size_;
684 };
685 
686 template< typename Type > int const Matrix4x4<Type>::rows_ = 4;
687 template< typename Type > int const Matrix4x4<Type>::cols_ = 4;
688 template< typename Type > int const Matrix4x4<Type>::size_ = 4 * 4;
689 
690 template< int rows, typename Type >
692 {
694  for(int i = 0; i < rows; i++)
695  for(int j = 0; j < 4; j++)
696  {
697  Type sum = 0;
698  for(int k = 0; k < 4; k++)
699  sum += m1.m[i][k]*m2.m[k][j];
700 
701  res.m[i][j] = sum;
702  }
703  return res;
704 }
705 
706 /// Inversion
707 template< typename Type >
709 {
710  //Copy source matrix
711  Matrix4x4< Type > work = *this;
712 
713  //build identity matrix
714  Matrix4x4< Type > result;
715  result.setToIdentity();
716 
717  const int size = 4;
718  for(int i = 0; i < size; i++)
719  {
720  int j;
721 
722  for(j = i; (j < size) && (isZero(work[j*size+i])); j++) {};
723 
724  // matrix is singular
725  if (j == size)
726  {
727  if (invertible)
728  *invertible = false;
729  return result;
730  }
731 
732  if (i != j)
733  for(int k = 0; k < size; k++)
734  {
735  Type tmp = result[i*size+k]; result[i*size+k] = result[j*size+k]; result[j*size+k] = tmp;
736  tmp = work[i*size+k]; work[i*size+k] = work[j*size+k]; work[j*size+k] = tmp;
737  }
738 
739  Type d = 1/work[i*size+i];
740  for(j = 0; j < size; j++)
741  {
742  result[i*size+j] *= d;
743  work[i*size+j] *= d;
744  }
745 
746  for(j = i+1; j < size; j++)
747  {
748  d = work[j*size+i];
749  for(int k = 0; k < size; k++)
750  {
751  result[j*size+k] -= result[i*size+k] * d;
752  work[j*size+k] -= work[i*size+k] * d;
753  }
754  }
755  }
756 
757  for(int i = size-1; i > 0; i--)
758  for(int j = 0; j < i; j++)
759  {
760  Type d = work[j*size+i];
761  for(int k = 0; k < size; k++)
762  {
763  result[j*size+k] -= result[i*size+k] * d;
764  work[j*size+k] -= work[i*size+k] * d;
765  }
766  }
768 
769  if (invertible)
770  *invertible = true;
771  return result;
772 }
773 
774 /// Return false if matrix is singular and can't be inverted, otherwise true
775 template < typename Type > inline
776  bool invert(const Matrix4x4< Type >& matrix, Matrix4x4< Type >& result)
777 {
778  bool invertable = false;
779  result = matrix.inverted(&invertable);
780  return invertable;
781 }
782 
783 /// Inversion (another one form) Given matrix shouldn't be singular.
784 /// Throws std::runtime_error() if matrix is singular, otherwise return inverse matrix
785 template < typename Type > inline
787 {
788  Matrix4x4< Type > res;
789  if (invert(matrix,res))
790  return res;
791  else
792  throw std::runtime_error("artec::sdk::base::invert: Try to invert singular matrix");
793 }
794 
795 template < typename Type > inline
797 {
798  Point3< Type > pr = (Matrix4x4< Type >)m * point;
799  return Point2< Type >(pr.x / pr.z, pr.y / pr.z);
800 }
801 
802 template < typename Type > inline
804 {
805  return m.project(point);
806 }
807 
808 // Selected matrices
815 
822 
823 } } } // namespace artec::sdk::base
824 
825 #endif //_MATRIX_H_
Point3< Type2 > operator*(const Point3< Type2 > &point) const
Special case of matrix-point multiplication: geometric transformation.
Definition: Matrix.h:256
static Matrix4x4 perspective(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
Get perspective projection matrix.
Definition: Matrix.h:597
Matrix4x4(const Type2 *values, int rows2, int cols2)
Definition: Matrix.h:52
Matrix4x4 & operator/=(const Type &val)
Definition: Matrix.h:295
GenericMatrix< rows, cols, Type > toGenericMatrix() const
Definition: Matrix.h:112
Matrix4x4 & operator-=(const Matrix4x4 &mat)
Definition: Matrix.h:152
GenericMatrix< 2, 2, double > Matrix2x2D
Definition: Matrix.h:816
Matrix4x4 operator-(const Matrix4x4 &mat) const
Definition: Matrix.h:179
Matrix4x4< Type > inverted(bool *invertible=0) const
Inversion.
Definition: Matrix.h:708
static Matrix4x4 rotationX(Type angle, const Point3< Type > &center)
Definition: Matrix.h:475
Point3< Type2 > operator*(const GenericMatrix< 3, 3, Type > &matrix, const Point3< Type2 > &point)
static int const rows_
Definition: Matrix.h:681
static Matrix4x4 rotationZ(Type angle)
Definition: Matrix.h:460
Matrix4x4 & operator*=(const Type &val)
Definition: Matrix.h:289
static Matrix4x4 rotationY(Type angle)
Definition: Matrix.h:446
GenericMatrix< 3, 3, double > Matrix3x3D
Definition: Matrix.h:819
void setToIdentity()
Make current matrix to identity.
Definition: Matrix.h:368
Matrix4x4(Type _00, Type _01, Type _02, Type _03, Type _10, Type _11, Type _12, Type _13, Type _20, Type _21, Type _22, Type _23, Type _30, Type _31, Type _32, Type _33)
Definition: Matrix.h:28
GenericMatrix< 2, 3, float > Matrix2x3F
Definition: Matrix.h:810
Matrix4x4(const Type2(&values)[sizeOfArray])
Definition: Matrix.h:42
bool invert(const GenericMatrix< size, size, Type > &matrix, GenericMatrix< size, size, Type > &result)
Matrix4x4 operator=(const GenericMatrix< rows2, cols2, Type2 > &second)
Definition: Matrix.h:85
Point3< Type > project(const Point3< Type > &point) const
Definition: Matrix.h:415
Matrix4x4 & operator*=(const Matrix4x4 &other)
Definition: Matrix.h:158
static Matrix4x4 scale(Type factor)
Definition: Matrix.h:555
static Matrix4x4 inverseMotion(const Matrix4x4 &matrix)
Inverse transformation matrix.
Definition: Matrix.h:577
static Matrix4x4 perspectiveFov(float fovy, float aspect, float zNear, float zFar)
Get perspective projection matrix.
Definition: Matrix.h:620
static Matrix4x4 rotationX(Type angle)
Definition: Matrix.h:432
Matrix4x4 operator-(const Type &val) const
Definition: Matrix.h:309
Matrix4x4< double > Matrix4x4D
Definition: Matrix.h:821
Matrix4x4 operator*(const Type &val) const
Definition: Matrix.h:247
bool operator!=(const Matrix4x4 &m) const
check matrices are not equal
Definition: Matrix.h:344
static int const size_
Definition: Matrix.h:683
Matrix4x4 operator*(const Matrix4x4 &mat) const
Definition: Matrix.h:190
static Matrix4x4 translation(const Point3< Type > &direction)
Translations.
Definition: Matrix.h:542
Matrix4x4 & operator+=(const Type &val)
Definition: Matrix.h:277
Matrix4x4 & operator+=(const Matrix4x4 &mat)
Definition: Matrix.h:146
void fill(const Type value)
Fill matrix with value.
Definition: Matrix.h:136
GenericMatrix< 2, 2, float > Matrix2x2F
Definition: Matrix.h:809
static Matrix4x4 rotationZ(Type angle, const Point3< Type > &center)
Definition: Matrix.h:493
GenericMatrix< 2, 3, double > Matrix2x3D
Definition: Matrix.h:817
Matrix4x4< float > Matrix4x4F
Definition: Matrix.h:814
bool operator==(const Matrix4x4 &m) const
check matrices are equal
Definition: Matrix.h:335
bool isIdentity() const
Check if matrix is an identity matrix.
Definition: Matrix.h:379
GenericMatrix< 3, 3, float > Matrix3x3F
Definition: Matrix.h:812
const Type & operator()(int row, int col) const
Definition: Matrix.h:355
Matrix4x4(const Matrix4x4 &second)
Copying constructor.
Definition: Matrix.h:68
GenericMatrix< 3, 4, float > Matrix3x4F
Definition: Matrix.h:813
Point4< Type2 > operator*(const Point4< Type2 > &point) const
Matrix - point multiplication.
Definition: Matrix.h:229
Matrix4x4 operator+(const Matrix4x4 &mat) const
Definition: Matrix.h:171
Matrix4x4 & operator-=(const Type &val)
Definition: Matrix.h:283
static Matrix4x4 rotation(Type angle, const Point3< Type > &direction)
! Important ! Direction is a unit vector, don't forget to normalize it !!!
Definition: Matrix.h:505
bool isZero(const Tf &some)
Equality expression for types which are not exact.
Definition: Point.h:25
GenericMatrix< rows, cols, Type2 > toGenericMatrix() const
Definition: Matrix.h:118
Matrix4x4 transposed() const
Return transposed matrix copy.
Definition: Matrix.h:401
static Matrix4x4 scale(Type factor, const Point3< Type > &center)
Definition: Matrix.h:565
Matrix4x4 operator-() const
Unary minus.
Definition: Matrix.h:326
static Matrix4x4 rotationY(Type angle, const Point3< Type > &center)
Definition: Matrix.h:484
GenericMatrix< 2, 4, float > Matrix2x4F
Definition: Matrix.h:811
Matrix4x4 & operator=(const Matrix4x4 &second)
Definition: Matrix.h:101
static int const cols_
Definition: Matrix.h:682
Point2< Type > project(const Point3< Type > &point, const GenericMatrix< 3, 4, Type > &m)
Definition: Matrix.h:796
Matrix4x4(const GenericMatrix< rows2, cols2, Type2 > &second)
Copying constructor with size_ conversion.
Definition: Matrix.h:72
GenericMatrix< 3, 4, double > Matrix3x4D
Definition: Matrix.h:820
Matrix4x4(const Matrix4x4< Type2 > &second)
Copying constructor with type conversion.
Definition: Matrix.h:76
static Matrix4x4 ortho(Type left, Type right, Type bottom, Type top, Type nearVal, Type farVal)
Get orthogonal matrix.
Definition: Matrix.h:631
GenericMatrix< 4, cols2, Type > operator*(const GenericMatrix< 4, cols2, Type > &mat) const
Definition: Matrix.h:211
static Matrix4x4 identity()
Identity matrix.
Definition: Matrix.h:361
Matrix4x4 operator+(const Type &val) const
Definition: Matrix.h:302
Type & operator()(int row, int col)
Definition: Matrix.h:349
GenericMatrix< 2, 4, double > Matrix2x4D
Definition: Matrix.h:818
static Matrix4x4 rotation(Type angle, const Point3< Type > &direction, const Point3< Type > &center)
! Important ! Direction is a unit vector, don't forget to normalize it !!!
Definition: Matrix.h:531
Matrix4x4 operator/(const Type &val) const
Definition: Matrix.h:316