MeVisLab Toolbox Reference
mlMatrix2.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_MATRIX2_H
14 #define ML_MATRIX2_H
15 
17 
18 // Include system independency file and project settings.
19 #include "mlLinearAlgebraSystem.h"
20 #include "mlLinearAlgebraDefs.h"
21 
22 
23 #include "mlFloatingPointMatrix.h"
24 
25 #include "mlVector2.h"
26 
27 // All declarations of this header will be in the namespace of mlLinearAlgebra.
28 ML_LA_START_NAMESPACE
29 
30 //---------------------------------------------------------------
32 //---------------------------------------------------------------
33 template <class DT>
34 class Tmat2 : public FloatingPointMatrix<Tvec2<DT>, 2>
35 {
36 public:
37 
39  typedef DT ComponentType;
40 
41  //---------------------------------------------------------------
42  // Constructors, set and get methods.
43  //---------------------------------------------------------------
44  // Constructs a matrix from 4 zero elements.
45  Tmat2();
46 
47  // Constructs a matrix that has the argument at the diagonal values, zero otherwise
48  Tmat2(const DT diagValue);
49 
50  // Composes a matrix from the two row vectors row0 and row1.
51  Tmat2(const Tvec2<DT>& row0,
52  const Tvec2<DT>& row1);
53 
54  // Copy constructor from the Tmat2 mat.
55  Tmat2(const Tmat2<DT>& mat);
56 
57  // Constructor from 4 floating point values in an array given by mat, row by row.
58  Tmat2(const float mat[4]);
59 
60  // Constructor from 4 double values in an array given by mat, row by row.
61  Tmat2(const double mat[4]);
62 
63  // Initializes all matrix elements explicitly with scalars,
64  // filling it row by row.
65  Tmat2(const DT in00, const DT in01,
66  const DT in10, const DT in11);
67 
68  // Copies contents from float array mat into *this, row by row.
69  void setValues(const float mat[4]);
70 
71  // Copies contents of *this into float array mat, row by row.
72  // Note that range and precision of the float values may not be
73  // sufficient for the DT matrix contents, written row by row.
74  void getValues(float mat[4]) const;
75 
76  // Copies contents from DT array mat into *this, row by row.
77  void setValues(const double mat[4]);
78 
79  // Copies contents of *this into DT array mat, row by row.
80  void getValues(double mat[4]) const;
81 
82  // Returns a matrix filled with values val.
83  static Tmat2<DT> getMat(const DT val);
84 
85  // Sets all values to val.
86  void set(const DT val);
87 
88  // Returns the identity matrix.
90 
91  // Sets a diagonal matrix with scale on diagonal.
92  void setScaleMatrix(const DT scale);
93 
94 
95  //---------------------------------------------------------------
96  // Operators and indexing functionality.
97  //---------------------------------------------------------------
100  bool operator<(const Tmat2<DT> &) const { return false; }
101 
102  // Assigns from a Tmat2.
103  const Tmat2<DT>& operator=(const Tmat2<DT>& m);
104 
105  // Increments by a Tmat2.
106  const Tmat2<DT>& operator+=(const Tmat2<DT>& m);
107 
108  // Decrements by a Tmat2.
109  const Tmat2<DT>& operator-=(const Tmat2<DT>& m);
110 
111  // Multiplies by a constant d.
112  const Tmat2<DT>& operator*=(const DT d);
113 
114  // Divides by a constant d. Division by zero is not handled and must be avoided by caller.
115  const Tmat2<DT>& operator/=(const DT d);
116 
117 
118  //---------------------------------------------------------------
119  // Special functions
120  //---------------------------------------------------------------
121  // Returns the determinant of the matrix.
122  DT det() const;
123 
124  // Transposes the matrix.
126 
127  // Returns the inverse.
128  // If a non-NULL Boolean pointer is passed to isInvertible
129  // then true is returned in *isInvertible in the case of a
130  // successful inversion or false if the inversion is not possible
131  // (function return is the identity then).
132  // If a NULL pointer is passed as isInvertible the matrix must
133  // be invertible, otherwise errors will occur.
134  Tmat2<DT> inverse(bool* isInvertible=nullptr) const;
135 
136  // Applies the function fct to each component.
138 
139 }; // end of class *Tmat2*
140 
141 
142 
143 //---------------------------------------------------------------
146 //---------------------------------------------------------------
148 template <class DT>
150 {
151  this->v[0] = this->v[1] = Tvec2<DT>(0);
152 }
153 
154 // Constructs the matrix that has the argument at the diagonal values, zero otherwise
155 template <class DT>
156 inline Tmat2<DT>::Tmat2(const DT diagValue)
157 {
158  this->v[0][0] = this->v[1][1] = diagValue;
159  this->v[1][0] = this->v[0][1] = 0;
160 }
161 
162 
164 template <class DT>
165 inline Tmat2<DT>::Tmat2(const Tvec2<DT>& row0,
166  const Tvec2<DT>& row1)
167 {
168  this->v[0] = row0;
169  this->v[1] = row1;
170 }
171 
173 template <class DT>
174 inline Tmat2<DT>::Tmat2(const Tmat2<DT>& mat)
175 {
176  this->v[0] = mat.v[0];
177  this->v[1] = mat.v[1];
178 }
179 
181 template <class DT>
182 inline Tmat2<DT>::Tmat2(const float mat[4])
183 {
184  setValues(mat);
185 }
186 
188 template <class DT>
189 inline Tmat2<DT>::Tmat2(const double mat[4])
190 {
191  setValues(mat);
192 }
193 
195 template <class DT>
196 inline Tmat2<DT>::Tmat2(const DT in00, const DT in01,
197  const DT in10, const DT in11)
198 {
199  this->v[0][0] = in00; this->v[0][1] = in01;
200  this->v[1][0] = in10; this->v[1][1] = in11;
201 }
202 
204 template <class DT>
205 inline void Tmat2<DT>::setValues(const float mat[4])
206 {
207  this->v[0][0] = static_cast<DT>(mat[0]); this->v[0][1] = static_cast<DT>(mat[1]);
208  this->v[1][0] = static_cast<DT>(mat[2]); this->v[1][1] = static_cast<DT>(mat[3]);
209 }
210 
212 template <class DT>
213 inline void Tmat2<DT>::setValues(const double mat[4])
214 {
215  this->v[0][0] = static_cast<DT>(mat[0]); this->v[0][1] = static_cast<DT>(mat[1]);
216  this->v[1][0] = static_cast<DT>(mat[2]); this->v[1][1] = static_cast<DT>(mat[3]);
217 }
218 
222 template <class DT>
223 inline void Tmat2<DT>::getValues(float mat[4]) const
224 {
225  mat[0] = static_cast<float>(this->v[0][0]); mat[1] = static_cast<float>(this->v[0][1]);
226  mat[2] = static_cast<float>(this->v[1][0]); mat[3] = static_cast<float>(this->v[1][1]);
227 }
228 
232 template <class DT>
233 inline void Tmat2<DT>::getValues(double mat[4]) const
234 {
235  mat[0] = static_cast<double>(this->v[0][0]); mat[1] = static_cast<double>(this->v[0][1]);
236  mat[2] = static_cast<double>(this->v[1][0]); mat[3] = static_cast<double>(this->v[1][1]);
237 }
238 
240 template <class DT>
241 inline Tmat2<DT> Tmat2<DT>::getMat(const DT val)
242 {
243  return Tmat2(val, val,
244  val, val);
245 }
246 
248 template <class DT>
249 inline void Tmat2<DT>::set(const DT val)
250 {
251  this->v[0] = this->v[1] = Tvec2<DT>(val);
252 }
253 
255 template <class DT>
257 {
258  return Tmat2<DT>(1, 0, 0, 1);
259 }
260 
262 template <class DT>
263 inline void Tmat2<DT>::setScaleMatrix(const DT scale)
264 {
265  this->v[0][0] = scale; this->v[0][1] = 0;
266  this->v[1][0] = 0; this->v[1][1] = scale;
267 }
269 
270 
271 
272 
273 //---------------------------------------------------------------
276 //---------------------------------------------------------------
278 template <class DT>
279 inline DT Tmat2<DT>::det() const
280 {
281  return this->v[0][0]*this->v[1][1] - this->v[0][1]*this->v[1][0];
282 }
283 
285 template <class DT>
287 {
288  return Tmat2<DT>(Tvec2<DT>(this->v[0][0], this->v[1][0]),
289  Tvec2<DT>(this->v[0][1], this->v[1][1]));
290 }
291 
293 template <class DT>
295 {
296  this->v[0].apply(fct);
297  this->v[1].apply(fct);
298  return *this;
299 }
300 
301 //--------------------------------------------------------------------
302 // Returns the inverse.
303 // If a non-NULL Boolean pointer is passed to isInvertible
304 // then true is returned in *isInvertible in the case of a
305 // successful inversion or false if the inversion is not possible
306 // (function return is the identity then).
307 // If a NULL pointer is passed as isInvertible the matrix must
308 // be invertible, otherwise errors will occur.
309 //--------------------------------------------------------------------
310 template <class DT>
311 Tmat2<DT> Tmat2<DT>::inverse(bool* isInvertible) const
312 {
313  // Matrix to be returned.
314  Tmat2<DT> retMat;
315  // Determinant must be non-zero.
316  DT determinant = det();
317  if (MLValueIs0WOM(determinant)) {
318  if (isInvertible == nullptr){
319  printTemplateError("Tmat2<DT> Tmat2<DT>::inverse() const, matrix not invertible",
321  "Returning identity");
322  }
323  else{
324  *isInvertible = false;
325  }
326  retMat = getIdentity();
327  }
328  else{
329  // Get inverse to reduce number of expensive divisions.
330  determinant = (static_cast<DT>(1))/determinant;
331  retMat[0][0] = static_cast<DT>(this->v[1][1]*determinant);
332 
333  retMat[1][0] = this->v[1][0];
334  retMat[1][0] *= static_cast<DT>(-determinant);
335 
336  retMat[0][1] = this->v[0][1];
337  retMat[0][1] *= static_cast<DT>(-determinant);
338 
339  retMat[1][1] = static_cast<DT>(this->v[0][0]*determinant);
340 
341  // Set state and return inverse.
342  if (isInvertible != nullptr){ *isInvertible = true; }
343  }
344  return retMat;
345 }
347 
348 
349 
350 //---------------------------------------------------------------
353 //---------------------------------------------------------------
355 template <class DT>
356 inline const Tmat2<DT>& Tmat2<DT>::operator=(const Tmat2<DT>& m)
357 {
358  if (&m != this){
359  this->v[0] = m.v[0];
360  this->v[1] = m.v[1];
361  }
362  return *this;
363 }
364 
366 template <class DT>
368 {
369  this->v[0] += m.v[0];
370  this->v[1] += m.v[1];
371  return *this;
372 }
373 
375 template <class DT>
377 {
378  this->v[0] -= m.v[0];
379  this->v[1] -= m.v[1];
380  return *this;
381 }
382 
384 template <class DT>
385 inline const Tmat2<DT>& Tmat2<DT>::operator*=(const DT d)
386 {
387  this->v[0] *= d;
388  this->v[1] *= d;
389  return *this;
390 }
391 
393 template <class DT>
394 inline const Tmat2<DT>& Tmat2<DT>::operator/=(const DT d)
395 {
396  this->v[0] /= d;
397  this->v[1] /= d;
398  return *this;
399 }
401 
402 
403 
404 //---------------------------------------------------------------
405 // Global operators and functions for class Tmat2
406 //---------------------------------------------------------------
410 #define _ML_MAT2_RC(i, j) a[i][0]*b[0][j] + a[i][1]*b[1][j]
411 
412 // a * b. Standard matrix multiplication.
413 template <class DT>
414 inline Tmat2<DT> operator*(const Tmat2<DT>& a, const Tmat2<DT>& b)
415 {
416  return Tmat2<DT>(
417  Tvec2<DT>(_ML_MAT2_RC(0,0), _ML_MAT2_RC(0,1)),
418  Tvec2<DT>(_ML_MAT2_RC(1,0), _ML_MAT2_RC(1,1))
419  );
420 }
421 #undef _ML_MAT2_RC
422 
424 template <class DT>
425 inline bool operator==(const Tmat2<DT>& a, const Tmat2<DT>& b)
426 {
427  return ((a[0] == b[0]) &&
428  (a[1] == b[1]));
429 }
430 
432 template <class DT>
433 inline bool operator!=(const Tmat2<DT>& a, const Tmat2<DT>& b)
434 {
435  return !(a == b);
436 }
438 
439 
440 //---------------------------------------------------------------
443 //---------------------------------------------------------------
445 template <class DT>
446 inline Tmat2<DT> operator-(const Tmat2<DT>& a)
447 {
448  return Tmat2<DT>(a) *= static_cast<DT>(-1.0);
449 }
450 
452 template <class DT>
453 inline Tmat2<DT> operator+(const Tmat2<DT>& a, const Tmat2<DT>& b)
454 {
455  return Tmat2<DT>(a) += b;
456 }
457 
459 template <class DT>
460 inline Tmat2<DT> operator-(const Tmat2<DT>& a, const Tmat2<DT>& b)
461 {
462  return Tmat2<DT>(a) -= b;
463 }
464 
466 template <class DT>
467 inline Tmat2<DT> operator*(const Tmat2<DT>& a, DT d)
468 {
469  return Tmat2<DT>(a) *= d;
470 }
471 
473 template <class DT>
474 inline Tmat2<DT> operator*(const DT d, const Tmat2<DT>& a)
475 {
476  return Tmat2<DT>(a) *= d;
477 }
478 
481 template <class DT>
482 inline Tmat2<DT> operator/(const Tmat2<DT>& a, const DT d)
483 {
484  Tmat2<DT> divi(a);
485  divi /= d;
486  return divi;
487 }
488 
491 template <class DT>
492 inline Tvec2<DT> operator*(const Tmat2<DT>& a, const Tvec2<DT>& v)
493 {
494  return Tvec2<DT>(a[0][0]*v[0] + a[0][1]*v[1], a[1][0]*v[0] + a[1][1]*v[1]);
495 }
496 
499 template <class DT>
500 inline Tvec2<DT> operator*(const Tvec2<DT>& v, const Tmat2<DT>& a)
501 {
502  return a.transpose() * v;
503 }
505 
506 //-----------------------------------------------------------------------------------
509 //-----------------------------------------------------------------------------------
510 
520 
521 
522 #if ML_DEPRECATED_SINCE(3,5,0)
523 
526 
530 ML_DEPRECATED typedef Tmat2<MLfloat> matf2;
534 ML_DEPRECATED typedef Tmat2<MLdouble> matd2;
538 ML_DEPRECATED typedef Tmat2<MLldouble> matld2;
542 ML_DEPRECATED typedef Tmat2<MLdouble> mat2;
544 
545 
546 #endif
547 
548 
549 ML_LA_END_NAMESPACE
550 
551 //-------------------------------------------------------------------
552 // Support for standard IO streams.
553 //-------------------------------------------------------------------
554 namespace std
555 {
557  template <class DT>
558  inline std::ostream& operator<<(std::ostream& os, const ML_LA_NAMESPACE::Tmat2<DT> &m)
559  {
560  return os << m[0] << '\n' << m[1];
561  }
562 
564  template <class DT>
565  inline std::istream& operator>>(std::istream& is, ML_LA_NAMESPACE::Tmat2<DT>& m)
566  {
567  ML_LA_NAMESPACE::Tmat2<DT> m_tmp;
568  is >> m_tmp[0] >> m_tmp[1];
569  if (is){ m = m_tmp; }
570  return is;
571  }
572 }
573 
574 #endif // __mlMatrix2_H
575 
576 
577 
#define ML_DEPRECATED
Definition: CSOGroup.h:371
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 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.
Declaration of matrix type traits:
Definition: mlMatrix2.h:35
Tmat2(const float mat[4])
Constructor from 4 floating point values in an array given by mat, row by row.
Definition: mlMatrix2.h:182
void getValues(double mat[4]) const
Copies the contents of *this into mat, row by row.
Definition: mlMatrix2.h:233
Tmat2< DT > transpose() const
Returns the transposed matrix.
Definition: mlMatrix2.h:286
Tmat2(const DT in00, const DT in01, const DT in10, const DT in11)
Initializes all matrix elements explicitly with scalars, row by row.
Definition: mlMatrix2.h:196
const Tmat2< DT > & operator+=(const Tmat2< DT > &m)
Increments by a Tmat2.
Definition: mlMatrix2.h:367
const Tmat2< DT > & operator/=(const DT d)
Divides by a constant d. Division by zero is not handled and must be avoided by caller.
Definition: mlMatrix2.h:394
DT det() const
Returns the determinant of this matrix.
Definition: mlMatrix2.h:279
const Tmat2< DT > & operator-=(const Tmat2< DT > &m)
Decrements by a Tmat2.
Definition: mlMatrix2.h:376
Tmat2< DT > inverse(bool *isInvertible=nullptr) const
Definition: mlMatrix2.h:311
Tmat2()
Constructs the matrix from 4 zero elements.
Definition: mlMatrix2.h:149
void setScaleMatrix(const DT scale)
Sets a diagonal matrix with scale on diagonal.
Definition: mlMatrix2.h:263
Tmat2(const Tmat2< DT > &mat)
Copy constructor from the Tmat2 mat.
Definition: mlMatrix2.h:174
bool operator<(const Tmat2< DT > &) const
Dummy "lesser than operator" which always returns false.
Definition: mlMatrix2.h:100
const Tmat2< DT > & operator*=(const DT d)
Multiplies by a constant d.
Definition: mlMatrix2.h:385
DT ComponentType
A typedef to "export" the type of components.
Definition: mlMatrix2.h:39
const Tmat2< DT > & operator=(const Tmat2< DT > &m)
Assigns from a Tmat2.
Definition: mlMatrix2.h:356
void setValues(const double mat[4])
Copies the contents of mat into *this, row by row.
Definition: mlMatrix2.h:213
void set(const DT val)
Sets all values to val.
Definition: mlMatrix2.h:249
static Tmat2< DT > getIdentity()
Returns the identity matrix.
Definition: mlMatrix2.h:256
Tmat2(const Tvec2< DT > &row0, const Tvec2< DT > &row1)
Composes a matrix from the two vectors row0 and row1.
Definition: mlMatrix2.h:165
Tmat2(const double mat[4])
Constructor from 4 double values in an array given by mat, row by row.
Definition: mlMatrix2.h:189
Tmat2(const DT diagValue)
Definition: mlMatrix2.h:156
const Tmat2< DT > & apply(MLDblFuncPtr fct)
Applies the method fct to all vectors of *this and return the matrix.
Definition: mlMatrix2.h:294
static Tmat2< DT > getMat(const DT val)
Returns a matrix filled with values val.
Definition: mlMatrix2.h:241
void getValues(float mat[4]) const
Copies the contents of *this into mat, row by row.
Definition: mlMatrix2.h:223
void setValues(const float mat[4])
Copies the contents of mat into *this, row by row.
Definition: mlMatrix2.h:205
Declaration of float vector type traits:
Definition: mlVector2.h:57
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0, otherwise false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm;...
Definition: mlTypeDefs.h:925
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
#define _ML_MAT2_RC(i, j)
Definition: mlMatrix2.h:410
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)
Tmat2< MLldouble > Matrix2ld
A 2x2 matrix of type long double.
Definition: mlMatrix2.h:516
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
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.
Tmat2< MLfloat > Matrix2f
A 2x2 matrix of type float.
Definition: mlMatrix2.h:512
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix2.h:425
Tmat2< MLdouble > Matrix2
The standard 2x2 matrix of type double.
Definition: mlMatrix2.h:518
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Return true if yes.
Definition: mlMatrix2.h:433
Tmat2< MLdouble > Matrix2d
A 2x2 matrix of type double.
Definition: mlMatrix2.h:514
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.
FloatingPointVector< T, size, DataContainer > & operator*=(FloatingPointVector< T, size, DataContainer > &op1, MLdouble value)
Arithmetic assignment: Component wise multiplication *this with specialized MLdouble scalar value.