MeVisLab Toolbox Reference
mlMatrix4.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_MATRIX4_H
14 #define ML_MATRIX4_H
15 
17 
18 // Include system independent file and project settings.
19 #include "mlLinearAlgebraSystem.h"
20 #include "mlLinearAlgebraDefs.h"
21 
22 #include "mlFloatingPointMatrix.h"
23 
24 #include "mlMatrix3.h"
25 #include "mlVector4.h"
26 #include "mlVector3.h"
27 
28 // All declarations of this header will be in the ML_LA_START_NAMESPACE namespace.
29 ML_LA_START_NAMESPACE
30 
31 //---------------------------------------------------------------
33 //---------------------------------------------------------------
34 template <class DT>
35 class Tmat4 : public FloatingPointMatrix<Tvec4<DT>, 4>
36 {
37 public:
38 
40  typedef DT ComponentType;
41 
42  //---------------------------------------------------------------
43  // Constructors, set and get methods.
44  //---------------------------------------------------------------
45  // Construct matrix from 16 zero elements.
46  Tmat4();
47 
48  // Builds a matrix that has the argument at the diagonal values, zero otherwise
49  Tmat4(const DT diagValue);
50 
51  // Composes a matrix from the four row vectors row0, row1, row2 and row3.
52  Tmat4(const Tvec4<DT> &row0, const Tvec4<DT> &row1, const Tvec4<DT> &row2, const Tvec4<DT> &row3);
53 
54  // Copy constructor from the Tmat4 mat.
55  Tmat4(const Tmat4<DT> &mat);
56 
57  // Constructor from 16 floating point values in an array given by mat, row by row.
58  Tmat4(const float mat[16]);
59 
60  // Constructor from 16 double values in an array given by mat, row by row.
61  Tmat4(const double mat[16]);
62 
63  // Initializes all matrix elements explicitly with scalars,
64  // filling it row by row.
65  Tmat4(const double in00, const double in01, const double in02, const double in03,
66  const double in10, const double in11, const double in12, const double in13,
67  const double in20, const double in21, const double in22, const double in23,
68  const double in30, const double in31, const double in32, const double in33);
69 
70  // Constructs a matrix from three base vectors n0, ... n3 and a translation t,
71  // all given as COLUMN vectors.
72  Tmat4(const Tvec3<DT> &n0, const Tvec3<DT> &n1, const Tvec3<DT> &n3, const Tvec3<DT> &t);
73 
74  // Copies the contents from float array mat into *this, row by row.
75  void setValues(const float mat[16]);
76 
77  // Copies the contents of *this into floating point matrix mat, row by row.
78  // Note that range and precision of the float values may not be
79  // sufficient for double or long double matrix contents.
80  void getValues(float mat[16]) const;
81 
82  // Copies the contents from double array mat into *this, row by row.
83  void setValues(const double mat[16]);
84 
85  // Copies the contents of *this into into double matrix mat, row by row.
86  // Note that range and precision of the double values may not be
87  // sufficient for long double matrix contents.
88  void getValues(double mat[16]) const;
89 
90  // Sets a diagonal matrix with scale on diagonal.
91  void setScaleMatrix(const DT scale);
92 
94  inline Tvec3<DT> transformPoint(const Tvec3<DT>& sourceVec) const
95  {
96  return Tvec3<DT>( (*this) * sourceVec.affinePoint(), true);
97  }
98 
99 #if ML_DEPRECATED_SINCE(3,5,0)
104  inline ML_DEPRECATED void TransformPoint(const Tvec3<DT>& sourceVec, Tvec3<DT>& targetVec) const
105  {
106  targetVec = transformPoint(sourceVec);
107  }
109 #endif
110 
113 
114  //---------------------------------------------------------------
115  // Assignment operators and other simple stuff.
116  //---------------------------------------------------------------
117  // Returns a matrix filled with values val.
118  static Tmat4<DT> getMat(const double val);
119 
120  // Sets all values to val.
121  void set(const DT val);
122 
125  bool operator<(const Tmat4<DT> &) const { return false; }
126 
127  // Assigns from a Tmat4.
128  const Tmat4<DT> &operator=(const Tmat4<DT> &m);
129 
130  // Increments by a Tmat4.
131  const Tmat4<DT> &operator+=(const Tmat4<DT> &m);
132 
133  // Decrements by a Tmat4.
134  const Tmat4<DT> &operator-=(const Tmat4<DT> &m);
135 
136 
137  // Multiplies by a constant d.
138  const Tmat4<DT> &operator*=(const DT d);
139 
140  // Divides by a constant d. Division by zero is not handled and must be avoided by caller.
141  const Tmat4<DT> &operator/=(const DT d);
142 
143 
144  //---------------------------------------------------------------
145  // Special functions
146  //---------------------------------------------------------------
147  // Determines the determinant of a 3x3 matrix given by A,B,C,D,E,F,G,H,I.
148  DT det3(DT A, DT B, DT C, DT D, DT E, DT F, DT G, DT H, DT I) const;
149 
150  // Determines the (sub)determinant of columns given by col1, col2 and col3.
151  template <class IDX_TYP>
152  inline DT determinantLower3(const IDX_TYP col1, const IDX_TYP col2, const IDX_TYP col3) const
153  {
154  /* Determinant of 3x3 matrix with given entries */
155  return det3(this->v[1][col1], this->v[1][col2], this->v[1][col3],
156  this->v[2][col1], this->v[2][col2], this->v[2][col3],
157  this->v[3][col1], this->v[3][col2], this->v[3][col3]);
158  }
159 
160  // Returns the determinant of this.
161  DT det() const;
162 
163  // Returns the transposed *this.
165 
166  // Returns the inverse. Gauss-Jordan elimination with partial pivoting.
167  // If a non-NULL Boolean pointer is passed to isInvertible
168  // then true is returned in *isInvertible in the case of a
169  // successful inversion or false if the inversion is not possible
170  // (function return is the identity then).
171  // If a NULL pointer is passed as isInvertible the matrix must
172  // be invertible, otherwise errors will occur.
173  Tmat4<DT> inverse(bool* isInvertible=nullptr) const;
174 
175  // Returns the identity matrix.
177 
178  // Applies the function fct to each component.
180 
181 }; // end of class *mat4*
182 
183 
184 //---------------------------------------------------------------
187 //---------------------------------------------------------------
188 
190 template <class DT>
192 {
193  this->v[0] = this->v[1] = this->v[2] = this->v[3] = Tvec4<DT>(0);
194 }
195 
196 // Constructs a matrix that has the argument at the diagonal values, zero otherwise
197 template <class DT>
198 inline Tmat4<DT>::Tmat4(const DT diagValue)
199 {
200  this->v[0][0] = this->v[1][1] = this->v[2][2] = this->v[3][3] = diagValue;
201  this->v[1][0] = this->v[2][0] = this->v[3][0] = this->v[2][1] = this->v[3][1] = this->v[3][2] = 0;
202  this->v[0][1] = this->v[0][2] = this->v[0][3] = this->v[1][2] = this->v[1][3] = this->v[2][3] = 0;
203 }
204 
206 template <class DT>
207 inline Tmat4<DT>::Tmat4(const Tvec4<DT> &row0, const Tvec4<DT> &row1, const Tvec4<DT> &row2, const Tvec4<DT> &row3)
208 {
209  this->v[0] = row0;
210  this->v[1] = row1;
211  this->v[2] = row2;
212  this->v[3] = row3;
213 }
214 
216 template <class DT>
217 inline Tmat4<DT>::Tmat4(const Tmat4<DT> &mat)
218 {
219  this->v[0] = mat.v[0];
220  this->v[1] = mat.v[1];
221  this->v[2] = mat.v[2];
222  this->v[3] = mat.v[3];
223 }
224 
226 template <class DT>
227 inline Tmat4<DT>::Tmat4(const float mat[16])
228 {
229  setValues(mat);
230 }
231 
233 template <class DT>
234 inline Tmat4<DT>::Tmat4(const double mat[16])
235 {
236  setValues(mat);
237 }
238 
240 template <class DT>
241 inline Tmat4<DT> Tmat4<DT>::getMat(const double val)
242 {
243  return Tmat4<DT>(val, val, val, val,
244  val, val, val, val,
245  val, val, val, val,
246  val, val, val, val);
247 }
248 
249 template <class DT>
251 {
252  Tmat4<DT> matrix = *this;
253 
254  // delete translation vector
255  matrix[0][3] = matrix[1][3] = matrix[2][3] = 0;
256 
257  // normalize direction vectors
258  Tvec3<DT> dirVec;
259  int i, j;
260  for (i = 0; i < 3; i++)
261  {
262  for (j = 0; j < 3; j++)
263  {
264  dirVec[j] = matrix[j][i];
265  }
266  dirVec.normalize();
267  for (j = 0; j < 3; j++)
268  {
269  matrix[j][i] = dirVec[j];
270  }
271  }
272  return matrix;
273 }
274 
276 template <class DT>
277 inline void Tmat4<DT>::set(const DT val)
278 {
279  this->v[0] = this->v[1] = this->v[2] = this->v[3] = Tvec4<DT>(val);
280 }
281 
283 template <class DT>
284 inline const Tmat4<DT> &Tmat4<DT>::operator=(const Tmat4<DT> &m)
285 {
286  if (&m != this){
287  this->v[0] = m.v[0];
288  this->v[1] = m.v[1];
289  this->v[2] = m.v[2];
290  this->v[3] = m.v[3];
291  }
292  return *this;
293 }
294 
296 template <class DT>
298 {
299  this->v[0] += m.v[0];
300  this->v[1] += m.v[1];
301  this->v[2] += m.v[2];
302  this->v[3] += m.v[3];
303  return *this;
304 }
305 
307 template <class DT>
309 {
310  this->v[0] -= m.v[0];
311  this->v[1] -= m.v[1];
312  this->v[2] -= m.v[2];
313  this->v[3] -= m.v[3];
314  return *this;
315 }
316 
318 template <class DT>
319 inline const Tmat4<DT> &Tmat4<DT>::operator*=(const DT d)
320 {
321  this->v[0] *= d;
322  this->v[1] *= d;
323  this->v[2] *= d;
324  this->v[3] *= d;
325  return *this;
326 }
327 
329 template <class DT>
330 inline const Tmat4<DT> &Tmat4<DT>::operator/=(const DT d)
331 {
332  this->v[0] /= d;
333  this->v[1] /= d;
334  this->v[2] /= d;
335  this->v[3] /= d;
336  return *this;
337 }
338 
340 template <class DT>
342 {
343  this->v[0].apply(fct);
344  this->v[1].apply(fct);
345  this->v[2].apply(fct);
346  this->v[3].apply(fct);
347  return *this;
348 }
350 
351 //---------------------------------------------------------------
354 //---------------------------------------------------------------
356 #define _ML_MAT4_RC(i, j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + \
357 a[i][2]*b[2][j] + a[i][3]*b[3][j]
358 
363 template <class DT>
364 inline Tmat4<DT> operator*(const Tmat4<DT> &a, const Tmat4<DT> &b)
365 {
366  return Tmat4<DT>(
367  Tvec4<DT>(_ML_MAT4_RC(0,0), _ML_MAT4_RC(0,1), _ML_MAT4_RC(0,2), _ML_MAT4_RC(0,3)),
368  Tvec4<DT>(_ML_MAT4_RC(1,0), _ML_MAT4_RC(1,1), _ML_MAT4_RC(1,2), _ML_MAT4_RC(1,3)),
369  Tvec4<DT>(_ML_MAT4_RC(2,0), _ML_MAT4_RC(2,1), _ML_MAT4_RC(2,2), _ML_MAT4_RC(2,3)),
371  );
372 }
373 #undef _ML_MAT4_RC
374 
375 
377 template <class DT>
378 inline bool operator==(const Tmat4<DT> &a, const Tmat4<DT> &b)
379 {
380  return ((a[0] == b[0]) &&
381  (a[1] == b[1]) &&
382  (a[2] == b[2]) &&
383  (a[3] == b[3]));
384 }
385 
387 template <class DT>
388 inline bool operator!=(const Tmat4<DT>& a, const Tmat4<DT>& b)
389 {
390  return !(a == b);
391 }
393 
394 
395 //---------------------------------------------------------------
398 //---------------------------------------------------------------
400 template <class DT>
401 inline Tmat4<DT> operator-(const Tmat4<DT> &a)
402 {
403  return Tmat4<DT>(a) *= static_cast<DT>(-1.0);
404 }
405 
407 template <class DT>
408 inline Tmat4<DT> operator+(const Tmat4<DT> &a, const Tmat4<DT> &b)
409 {
410  return Tmat4<DT>(a) += b;
411 }
412 
414 template <class DT>
415 inline Tmat4<DT> operator-(const Tmat4<DT> &a, const Tmat4<DT> &b)
416 {
417  return Tmat4<DT>(a) -= b;
418 }
419 
421 template <class DT>
422 inline Tmat4<DT> operator*(const Tmat4<DT> &a, const DT d)
423 {
424  return Tmat4<DT>(a) *= d;
425 }
426 
428 template <class DT>
429 inline Tmat4<DT> operator*(const DT d, const Tmat4<DT> &a)
430 {
431  return Tmat4<DT>(a) *= d;
432 }
433 
436 template <class DT>
437 inline Tmat4<DT> operator/(const Tmat4<DT> &a, const DT d)
438 {
439  return Tmat4<DT>(a) /= d;
440 }
442 
443 
444 //--------------------------------------------------------------------
445 //
448 //
449 //--------------------------------------------------------------------
450 
451 //--------------------------------------------------------------------
453 //--------------------------------------------------------------------
454 template <class DT>
455 inline Tmat4<DT>::Tmat4(const Tvec3<DT> &n0, const Tvec3<DT> &n1, const Tvec3<DT> &n2, const Tvec3<DT> &t)
456 {
457  this->v[0][0]=n0[0];
458  this->v[1][0]=n0[1];
459  this->v[2][0]=n0[2];
460 
461  this->v[0][1]=n1[0];
462  this->v[1][1]=n1[1];
463  this->v[2][1]=n1[2];
464 
465  this->v[0][2]=n2[0];
466  this->v[1][2]=n2[1];
467  this->v[2][2]=n2[2];
468 
469  this->v[0][3]=t[0];
470  this->v[1][3]=t[1];
471  this->v[2][3]=t[2];
472 
473  this->v[3][0]=0;
474  this->v[3][1]=0;
475  this->v[3][2]=0;
476  this->v[3][3]=1;
477 }
478 
479 //--------------------------------------------------------------------
481 //--------------------------------------------------------------------
482 template <class DT>
483 Tmat4<DT>::Tmat4(const double in00, const double in01, const double in02, const double in03,
484  const double in10, const double in11, const double in12, const double in13,
485  const double in20, const double in21, const double in22, const double in23,
486  const double in30, const double in31, const double in32, const double in33)
487 {
488  this->v[0][0]=static_cast<DT>(in00); this->v[0][1]=static_cast<DT>(in01); this->v[0][2]=static_cast<DT>(in02); this->v[0][3]=static_cast<DT>(in03);
489  this->v[1][0]=static_cast<DT>(in10); this->v[1][1]=static_cast<DT>(in11); this->v[1][2]=static_cast<DT>(in12); this->v[1][3]=static_cast<DT>(in13);
490  this->v[2][0]=static_cast<DT>(in20); this->v[2][1]=static_cast<DT>(in21); this->v[2][2]=static_cast<DT>(in22); this->v[2][3]=static_cast<DT>(in23);
491  this->v[3][0]=static_cast<DT>(in30); this->v[3][1]=static_cast<DT>(in31); this->v[3][2]=static_cast<DT>(in32); this->v[3][3]=static_cast<DT>(in33);
492 }
493 
494 //--------------------------------------------------------------------
496 //--------------------------------------------------------------------
497 template <class DT>
498 void Tmat4<DT>::setValues(const float mat[16])
499 {
500  this->v[0][0] = mat[ 0]; this->v[0][1] = mat[ 1]; this->v[0][2] = mat[ 2]; this->v[0][3] = mat[ 3];
501  this->v[1][0] = mat[ 4]; this->v[1][1] = mat[ 5]; this->v[1][2] = mat[ 6]; this->v[1][3] = mat[ 7];
502  this->v[2][0] = mat[ 8]; this->v[2][1] = mat[ 9]; this->v[2][2] = mat[10]; this->v[2][3] = mat[11];
503  this->v[3][0] = mat[12]; this->v[3][1] = mat[13]; this->v[3][2] = mat[14]; this->v[3][3] = mat[15];
504 }
505 
506 //--------------------------------------------------------------------
510 //--------------------------------------------------------------------
511 template <class DT>
512 void Tmat4<DT>::getValues(float mat[16]) const
513 {
514  mat[ 0] = static_cast<float>(this->v[0][0]); mat[ 1] = static_cast<float>(this->v[0][1]); mat[ 2] = static_cast<float>(this->v[0][2]); mat[ 3] = static_cast<float>(this->v[0][3]);
515  mat[ 4] = static_cast<float>(this->v[1][0]); mat[ 5] = static_cast<float>(this->v[1][1]); mat[ 6] = static_cast<float>(this->v[1][2]); mat[ 7] = static_cast<float>(this->v[1][3]);
516  mat[ 8] = static_cast<float>(this->v[2][0]); mat[ 9] = static_cast<float>(this->v[2][1]); mat[10] = static_cast<float>(this->v[2][2]); mat[11] = static_cast<float>(this->v[2][3]);
517  mat[12] = static_cast<float>(this->v[3][0]); mat[13] = static_cast<float>(this->v[3][1]); mat[14] = static_cast<float>(this->v[3][2]); mat[15] = static_cast<float>(this->v[3][3]);
518 }
519 
520 //--------------------------------------------------------------------
522 //--------------------------------------------------------------------
523 template <class DT>
524 void Tmat4<DT>::setValues(const double mat[16])
525 {
526  this->v[0][0] = static_cast<DT>(mat[ 0]); this->v[0][1] = static_cast<DT>(mat[ 1]); this->v[0][2] = static_cast<DT>(mat[ 2]); this->v[0][3] = static_cast<DT>(mat[ 3]);
527  this->v[1][0] = static_cast<DT>(mat[ 4]); this->v[1][1] = static_cast<DT>(mat[ 5]); this->v[1][2] = static_cast<DT>(mat[ 6]); this->v[1][3] = static_cast<DT>(mat[ 7]);
528  this->v[2][0] = static_cast<DT>(mat[ 8]); this->v[2][1] = static_cast<DT>(mat[ 9]); this->v[2][2] = static_cast<DT>(mat[10]); this->v[2][3] = static_cast<DT>(mat[11]);
529  this->v[3][0] = static_cast<DT>(mat[12]); this->v[3][1] = static_cast<DT>(mat[13]); this->v[3][2] = static_cast<DT>(mat[14]); this->v[3][3] = static_cast<DT>(mat[15]);
530 }
531 
532 //--------------------------------------------------------------------
536 //--------------------------------------------------------------------
537 template <class DT>
538 void Tmat4<DT>::getValues(double mat[16]) const
539 {
540  mat[ 0] = static_cast<double>(this->v[0][0]); mat[ 1] = static_cast<double>(this->v[0][1]); mat[ 2] = static_cast<double>(this->v[0][2]); mat[ 3] = static_cast<double>(this->v[0][3]);
541  mat[ 4] = static_cast<double>(this->v[1][0]); mat[ 5] = static_cast<double>(this->v[1][1]); mat[ 6] = static_cast<double>(this->v[1][2]); mat[ 7] = static_cast<double>(this->v[1][3]);
542  mat[ 8] = static_cast<double>(this->v[2][0]); mat[ 9] = static_cast<double>(this->v[2][1]); mat[10] = static_cast<double>(this->v[2][2]); mat[11] = static_cast<double>(this->v[2][3]);
543  mat[12] = static_cast<double>(this->v[3][0]); mat[13] = static_cast<double>(this->v[3][1]); mat[14] = static_cast<double>(this->v[3][2]); mat[15] = static_cast<double>(this->v[3][3]);
544 }
545 
546 //--------------------------------------------------------------------
548 //--------------------------------------------------------------------
549 template <class DT>
550 void Tmat4<DT>::setScaleMatrix(const DT scale)
551 {
552  this->v[0][0] = scale; this->v[0][1] = 0; this->v[0][2] = 0; this->v[0][3] = 0;
553  this->v[1][0] = 0; this->v[1][1] = scale; this->v[1][2] = 0; this->v[1][3] = 0;
554  this->v[2][0] = 0; this->v[2][1] = 0; this->v[2][2] = scale; this->v[2][3] = 0;
555  this->v[3][0] = 0; this->v[3][1] = 0; this->v[3][2] = 0; this->v[3][3] = scale;
556 }
557 
558 //--------------------------------------------------------------------
560 //--------------------------------------------------------------------
561 template <class DT>
562 inline DT Tmat4<DT>::det3(const DT A, const DT B, const DT C, const DT D, const DT E, const DT F, const DT G, const DT H, const DT I) const
563 {
564  /* Determinant of 3x3 matrix with given entries */
565  return ((A*E*I + B*F*G + C*D*H) - (A*F*H + B*D*I + C*E*G));
566 }
567 
568 //--------------------------------------------------------------------
570 //--------------------------------------------------------------------
571 template <class DT>
572 inline DT Tmat4<DT>::det() const
573 {
574  return ( this->v[0][0] * determinantLower3(1u, 2u, 3u)
575  - this->v[0][1] * determinantLower3(0u, 2u, 3u)
576  + this->v[0][2] * determinantLower3(0u, 1u, 3u)
577  - this->v[0][3] * determinantLower3(0u, 1u, 2u));
578 }
579 
580 //--------------------------------------------------------------------
582 //--------------------------------------------------------------------
583 template <class DT>
585 {
586  return Tmat4<DT>(this->v[0][0], this->v[1][0], this->v[2][0], this->v[3][0],
587  this->v[0][1], this->v[1][1], this->v[2][1], this->v[3][1],
588  this->v[0][2], this->v[1][2], this->v[2][2], this->v[3][2],
589  this->v[0][3], this->v[1][3], this->v[2][3], this->v[3][3]);
590 }
591 
592 //--------------------------------------------------------------------
594 //--------------------------------------------------------------------
595 template <class DT>
597 {
598  return Tmat4<DT>(1, 0, 0, 0,
599  0, 1, 0, 0,
600  0, 0, 1, 0,
601  0, 0, 0, 1);
602 }
603 
604 //--------------------------------------------------------------------
607 //--------------------------------------------------------------------
608 template <class DT>
610 {
611  return Tmat4<DT>::getIdentity();
612 }
613 
614 //--------------------------------------------------------------------
618 //--------------------------------------------------------------------
619 template <class DT>
621 {
622  return Tmat4<DT>(Tvec4<DT>(1.0, 0.0, 0.0, v[0]),
623  Tvec4<DT>(0.0, 1.0, 0.0, v[1]),
624  Tvec4<DT>(0.0, 0.0, 1.0, v[2]),
625  Tvec4<DT>(0.0, 0.0, 0.0, 1.0));
626 }
627 
628 //--------------------------------------------------------------------
632 //-------------------------------------------------------------------
633 template <class DT>
635 {
636  const DT c = cos(angleRad);
637  const DT s = sin(angleRad);
638  const DT t = 1.0 - c;
639 
640  Axis.normalize();
641  return Tmat4<DT>(Tvec4<DT>(t * Axis[0] * Axis[0] + c,
642  t * Axis[0] * Axis[1] - s * Axis[2],
643  t * Axis[0] * Axis[2] + s * Axis[1],
644  0.0),
645  Tvec4<DT>(t * Axis[0] * Axis[1] + s * Axis[2],
646  t * Axis[1] * Axis[1] + c,
647  t * Axis[1] * Axis[2] - s * Axis[0],
648  0.0),
649  Tvec4<DT>(t * Axis[0] * Axis[2] - s * Axis[1],
650  t * Axis[1] * Axis[2] + s * Axis[0],
651  t * Axis[2] * Axis[2] + c,
652  0.0),
653  Tvec4<DT>(0.0, 0.0, 0.0, 1.0));
654 }
655 
656 //--------------------------------------------------------------------
658 //--------------------------------------------------------------------
659 template <class DT>
660 inline Tmat4<DT> scaling3D(const Tvec3<DT> &scaleVector)
661 {
662  return Tmat4<DT>(Tvec4<DT>(scaleVector[0], 0.0, 0.0, 0.0),
663  Tvec4<DT>(0.0, scaleVector[1], 0.0, 0.0),
664  Tvec4<DT>(0.0, 0.0, scaleVector[2], 0.0),
665  Tvec4<DT>(0.0, 0.0, 0.0, 1.0));
666 }
667 
668 //--------------------------------------------------------------------
672 //--------------------------------------------------------------------
673 template <class DT>
674 inline Tmat4<DT> perspective3D(const DT d)
675 {
676  Tmat4<DT> retVal;
678 
679  retVal[0][0] = 1.0;
680  retVal[1][1] = 1.0;
681  retVal[2][2] = 1.0;
682  retVal[3][2] = 1.0/d;
683  retVal[3][3] = 1.0;
684  return retVal;
685 }
686 
687 
688 //--------------------------------------------------------------------
696 //--------------------------------------------------------------------
697 template <class DT>
698 Tmat4<DT> Tmat4<DT>::inverse(bool* isInvertible) const
699 {
700  // Epsilon for comparison with 0 in inversion process.
701  static const DT Epsilon = static_cast<DT>(10e-14);
702 
703  // Use helper function from tools to invert the matrix.
704  return MLInverseMatHelper(*this,
705  isInvertible,
706  Epsilon,
707  "Tmat4<DT> Tmat4<DT>::inverse(bool* isInvertible) const, matrix not invertable",
708  getIdentity(),
709  4);
710 }
712 
713 
714 //-----------------------------------------------------------------------------------
717 //-----------------------------------------------------------------------------------
718 
728 
729 
730 #if ML_DEPRECATED_SINCE(3,5,0)
731 
734 
738 ML_DEPRECATED typedef Tmat4<MLfloat> matf4;
742 ML_DEPRECATED typedef Tmat4<MLdouble> matd4;
746 ML_DEPRECATED typedef Tmat4<MLldouble> matld4;
750 ML_DEPRECATED typedef Tmat4<MLdouble> mat4;
752 
753 
754 #endif
755 
756 
757 
758 ML_LA_END_NAMESPACE
759 
760 namespace std
761 {
762  //-------------------------------------------------------------------
764  //-------------------------------------------------------------------
765  template <class DT>
766  inline std::ostream &operator<<(std::ostream &os, const ML_LA_NAMESPACE::Tmat4<DT> &m)
767  {
768  return os << m[0] << '\n' << m[1] << '\n' << m[2] << '\n' << m[3];
769  }
770 
771  // Input from stream.
772  template <class DT>
773  inline std::istream &operator>>(std::istream &is, ML_LA_NAMESPACE::Tmat4<DT> &m)
774  {
775  ML_LA_NAMESPACE::Tmat4<DT> m_tmp;
776  is >> m_tmp[0] >> m_tmp[1] >> m_tmp[2] >> m_tmp[3];
777  if (is){ m = m_tmp; }
778  return is;
779  }
781 }
782 
783 #endif // __mlMatrix4_H
784 
785 
786 
787 
#define ML_DEPRECATED
Definition: CSOGroup.h:371
@ G
Definition: SoKeyGrabber.h:58
@ B
Definition: SoKeyGrabber.h:53
@ A
Letters.
Definition: SoKeyGrabber.h:52
@ H
Definition: SoKeyGrabber.h:59
@ C
Definition: SoKeyGrabber.h:54
@ D
Definition: SoKeyGrabber.h:55
@ E
Definition: SoKeyGrabber.h:56
@ I
Definition: SoKeyGrabber.h:60
@ F
Definition: SoKeyGrabber.h:57
A class to administrate an axis coordinate system drawable in OpenGL.
Definition: Axis.h:22
Base class of all matrix classes which holds the data buffer and provides some general access methods...
VectorT v[size]
The rows constituting the matrix.
FloatingPointVector< T, size, DataContainer > operator/(FloatingPointVector< T, size, DataContainer > lhs, MLdouble rhs)
Component wise division of lhs by specialized rhs of type MLdouble.
T normalize()
Normalizes the buffer and returns the Euclidean length of vector before normalization,...
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
FloatingPointVector< T, size, DataContainer > operator+(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component wise addition of lhs and rhs.
FloatingPointVector< T, size, DataContainer > operator-(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component wise subtraction of rhs from lhs.
A four by four matrix class consisting of 4 row vectors.
Definition: mlMatrix4.h:36
Tmat4(const double mat[16])
Constructor from 16 double values in an array given by mat, row by row.
Definition: mlMatrix4.h:234
DT determinantLower3(const IDX_TYP col1, const IDX_TYP col2, const IDX_TYP col3) const
Definition: mlMatrix4.h:152
void set(const DT val)
Sets all values to double val.
Definition: mlMatrix4.h:277
Tmat4< DT > inverse(bool *isInvertible=nullptr) const
Returns the inverse Gauss-Jordan elimination with partial pivoting.
Definition: mlMatrix4.h:698
Tmat4(const Tvec3< DT > &n0, const Tvec3< DT > &n1, const Tvec3< DT > &n3, const Tvec3< DT > &t)
Constructs a matrix from three base vectors and translation (as COLUMN vectors)
Definition: mlMatrix4.h:455
Tmat4(const Tvec4< DT > &row0, const Tvec4< DT > &row1, const Tvec4< DT > &row2, const Tvec4< DT > &row3)
Composes a matrix from the four vectors row0, row1, row2 and row3.
Definition: mlMatrix4.h:207
void getValues(float mat[16]) const
Copies the contents of *this into floating point matrix mat, row by row.
Definition: mlMatrix4.h:512
Tvec3< DT > transformPoint(const Tvec3< DT > &sourceVec) const
Transforms the given sourceVec and returns the result vector.
Definition: mlMatrix4.h:94
Tmat4(const float mat[16])
Constructor from 16 floating point values in an array given by mat, row by row.
Definition: mlMatrix4.h:227
const Tmat4< DT > & operator/=(const DT d)
Divides by an MLdouble constant d. Division by zero is not handled and must be avoided by caller.
Definition: mlMatrix4.h:330
static Tmat4< DT > getIdentity()
Returns the identity matrix.
Definition: mlMatrix4.h:596
DT det() const
Returns the determinant of *this.
Definition: mlMatrix4.h:572
bool operator<(const Tmat4< DT > &) const
Dummy "lesser than operator" which always returns false.
Definition: mlMatrix4.h:125
DT det3(DT A, DT B, DT C, DT D, DT E, DT F, DT G, DT H, DT I) const
Determines the determinant of a 3x3 matrix given by A,B,C,D,E,F,G,H,I.
Definition: mlMatrix4.h:562
void setValues(const double mat[16])
Copies the contents of mat into *this, row by row.
Definition: mlMatrix4.h:524
Tmat4< DT > transpose() const
Returns the transposed *this.
Definition: mlMatrix4.h:584
Tmat4(const double in00, const double in01, const double in02, const double in03, const double in10, const double in11, const double in12, const double in13, const double in20, const double in21, const double in22, const double in23, const double in30, const double in31, const double in32, const double in33)
Initializes all matrix elements explicitly with scalars, row by row.
Definition: mlMatrix4.h:483
static Tmat4< DT > getMat(const double val)
Returns a matrix filled with values val.
Definition: mlMatrix4.h:241
const Tmat4< DT > & operator=(const Tmat4< DT > &m)
Assigns from a Tmat4.
Definition: mlMatrix4.h:284
Tmat4(const Tmat4< DT > &mat)
Copy constructor from the Tmat4 mat.
Definition: mlMatrix4.h:217
Tmat4< DT > getRotationMatrix() const
Returns the rotational part of the matrix (removes scaling and translation).
Definition: mlMatrix4.h:250
void setScaleMatrix(const DT scale)
Sets a diagonal matrix with scale on diagonal.
Definition: mlMatrix4.h:550
const Tmat4< DT > & operator+=(const Tmat4< DT > &m)
Increments by a Tmat4.
Definition: mlMatrix4.h:297
void setValues(const float mat[16])
Copies the contents of mat into *this, row by row.
Definition: mlMatrix4.h:498
const Tmat4< DT > & operator*=(const DT d)
Multiplies by an MLdouble constant d.
Definition: mlMatrix4.h:319
Tmat4(const DT diagValue)
Definition: mlMatrix4.h:198
const Tmat4< DT > & apply(MLDblFuncPtr fct)
Applies the function fct to all vectors of *this and return the matrix.
Definition: mlMatrix4.h:341
void getValues(double mat[16]) const
Copies the contents of *this into mat, row by row.
Definition: mlMatrix4.h:538
const Tmat4< DT > & operator-=(const Tmat4< DT > &m)
Decrements by a Tmat4.
Definition: mlMatrix4.h:308
DT ComponentType
A typedef to "export" the type of components.
Definition: mlMatrix4.h:40
Tmat4()
Constructs a matrix from 16 zero elements.
Definition: mlMatrix4.h:191
Forward declarations to resolve header file dependencies.
Definition: mlVector3.h:66
Tvec4< DT > affinePoint() const
Builds a homogeneous Tvec4 point from *this and return it, i.e.
Definition: mlVector3.h:204
Forward declarations to resolve header file dependencies.
Definition: mlVector4.h:50
#define ML_CHECK_FLOAT_THROW(x)
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
#define _ML_MAT4_RC(i, j)
Helper macro only locally defined for Tmat4 matrix multiplication.
Definition: mlMatrix4.h:356
double(* MLDblFuncPtr)(double)
A function pointer type to a function which returns a double and takes a double as argument.
Definition: mlTypeDefs.h:1282
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition: mlVariant.h:215
void apply(Fnc &&fnc, const std::tuple< Args... > &tuple)
Tmat4< DT > translation3D(const Tvec3< DT > &v)
Returns a 4x4 homogeneous translation matrix with default identity matrix contents and the upper thre...
Definition: mlMatrix4.h:620
Tmat4< DT > rotation3D(Tvec3< DT > Axis, const DT angleRad)
Returns a 4x4 homogeneous 3D rotation matrix describing a rotation with angle angleRad around axis Ax...
Definition: mlMatrix4.h:634
FloatingPointVector< T, size, DataContainer > & operator/=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component wise division of *this by scalar value.
FloatingPointVector< T, size, DataContainer > & operator-=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component wise subtraction of buffer from *this.
Tmat4< DT > scaling3D(const Tvec3< DT > &scaleVector)
Scaling 3D.
Definition: mlMatrix4.h:660
bool operator==(const Tmat4< DT > &a, const Tmat4< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix4.h:378
Tmat4< MLdouble > Matrix4d
A 4x4 matrix of type double.
Definition: mlMatrix4.h:722
Tmat4< DT > perspective3D(const DT d)
Creates a 4x4 homogeneous perspective projection matrix with perspective shortening value given by d ...
Definition: mlMatrix4.h:674
Tmat4< MLdouble > Matrix4
The standard 4x4 matrix of type double.
Definition: mlMatrix4.h:726
bool operator!=(const Tmat4< DT > &a, const Tmat4< DT > &b)
a != b ? Return true if yes.
Definition: mlMatrix4.h:388
Tmat4< DT > identity3D()
Returns a 4x4 homogeneous identity3D matrix; synonym for Tmat4<DT>::getIdentity().
Definition: mlMatrix4.h:609
Tmat4< MLldouble > Matrix4ld
A 4x4 matrix of type long double.
Definition: mlMatrix4.h:724
constexpr Is< T > is(T d)
FloatingPointVector< T, size, DataContainer > & operator+=(FloatingPointVector< T, size, DataContainer > &op1, const FloatingPointVector< T, size, DataContainer > &buffer)
Arithmetic assignment: Component wise addition.
Tmat4< MLfloat > Matrix4f
A 4x4 matrix of type float.
Definition: mlMatrix4.h:720
BASE_TYPE MLInverseMatHelper(const BASE_TYPE &origMat, bool *isInvertible, const typename BASE_TYPE::ComponentType, const char *const ZeroDetErrString, const BASE_TYPE &Identity, const size_t Dim)
Computes an N dimensional inverse from given default parameters.
FloatingPointVector< T, size, DataContainer > & operator*=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component wise multiplication *this with specialized MLdouble scalar value.