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.
20#include "mlLinearAlgebraDefs.h"
21
22
24
25#include "mlVector2.h"
26
27// All declarations of this header will be in the namespace of mlLinearAlgebra.
29
30//---------------------------------------------------------------
32//---------------------------------------------------------------
33template <class DT>
34class Tmat2 : public FloatingPointMatrix<Tvec2<DT>, 2>
35{
36public:
37
39 typedef DT ComponentType;
40
41 //---------------------------------------------------------------
42 // Constructors, set and get methods.
43 //---------------------------------------------------------------
44 // Constructs a matrix from 4 zero elements.
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.
52 const Tvec2<DT>& row1);
53
54 // Copy constructor from the Tmat2 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.
104
105 // Increments by a Tmat2.
107
108 // Decrements by a Tmat2.
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//---------------------------------------------------------------
148template <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
155template <class DT>
157{
158 this->v[0][0] = this->v[1][1] = diagValue;
159 this->v[1][0] = this->v[0][1] = 0;
160}
161
162
164template <class DT>
166 const Tvec2<DT>& row1)
167{
168 this->v[0] = row0;
169 this->v[1] = row1;
170}
171
173template <class DT>
175{
176 this->v[0] = mat.v[0];
177 this->v[1] = mat.v[1];
178}
179
181template <class DT>
182inline Tmat2<DT>::Tmat2(const float mat[4])
183{
184 setValues(mat);
185}
186
188template <class DT>
189inline Tmat2<DT>::Tmat2(const double mat[4])
190{
191 setValues(mat);
192}
193
195template <class DT>
196inline 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
204template <class DT>
205inline 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
212template <class DT>
213inline 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
222template <class DT>
223inline 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
232template <class DT>
233inline 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
240template <class DT>
241inline Tmat2<DT> Tmat2<DT>::getMat(const DT val)
242{
243 return Tmat2(val, val,
244 val, val);
245}
246
248template <class DT>
249inline void Tmat2<DT>::set(const DT val)
250{
251 this->v[0] = this->v[1] = Tvec2<DT>(val);
252}
253
255template <class DT>
257{
258 return Tmat2<DT>(1, 0, 0, 1);
259}
260
262template <class DT>
263inline 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//---------------------------------------------------------------
278template <class DT>
279inline 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
285template <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
293template <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//--------------------------------------------------------------------
310template <class DT>
312{
313 // Matrix to be returned.
315 // Determinant must be non-zero.
316 DT determinant = det();
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//---------------------------------------------------------------
355template <class DT>
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
366template <class DT>
368{
369 this->v[0] += m.v[0];
370 this->v[1] += m.v[1];
371 return *this;
372}
373
375template <class DT>
377{
378 this->v[0] -= m.v[0];
379 this->v[1] -= m.v[1];
380 return *this;
381}
382
384template <class DT>
385inline 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
393template <class DT>
394inline 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.
413template <class DT>
414inline Tmat2<DT> operator*(const Tmat2<DT>& a, const Tmat2<DT>& b)
415{
416 return Tmat2<DT>(
419 );
420}
421#undef _ML_MAT2_RC
422
424template <class DT>
425inline bool operator==(const Tmat2<DT>& a, const Tmat2<DT>& b)
426{
427 return ((a[0] == b[0]) &&
428 (a[1] == b[1]));
429}
430
432template <class DT>
433inline bool operator!=(const Tmat2<DT>& a, const Tmat2<DT>& b)
434{
435 return !(a == b);
436}
438
439
440//---------------------------------------------------------------
443//---------------------------------------------------------------
445template <class DT>
447{
448 return Tmat2<DT>(a) *= static_cast<DT>(-1.0);
449}
450
452template <class DT>
453inline Tmat2<DT> operator+(const Tmat2<DT>& a, const Tmat2<DT>& b)
454{
455 return Tmat2<DT>(a) += b;
456}
457
459template <class DT>
460inline Tmat2<DT> operator-(const Tmat2<DT>& a, const Tmat2<DT>& b)
461{
462 return Tmat2<DT>(a) -= b;
463}
464
466template <class DT>
467inline Tmat2<DT> operator*(const Tmat2<DT>& a, DT d)
468{
469 return Tmat2<DT>(a) *= d;
470}
471
473template <class DT>
474inline Tmat2<DT> operator*(const DT d, const Tmat2<DT>& a)
475{
476 return Tmat2<DT>(a) *= d;
477}
478
481template <class DT>
482inline Tmat2<DT> operator/(const Tmat2<DT>& a, const DT d)
483{
484 Tmat2<DT> divi(a);
485 divi /= d;
486 return divi;
487}
488
491template <class DT>
492inline 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
499template <class DT>
500inline Tvec2<DT> operator*(const Tvec2<DT>& v, const Tmat2<DT>& a)
501{
502 return a.transpose() * v;
503}
505
506//-----------------------------------------------------------------------------------
509//-----------------------------------------------------------------------------------
510
520
522
523//-------------------------------------------------------------------
524// Support for standard IO streams.
525//-------------------------------------------------------------------
526namespace std
527{
529 template <class DT>
530 inline std::ostream& operator<<(std::ostream& os, const ML_LA_NAMESPACE::Tmat2<DT> &m)
531 {
532 return os << m[0] << '\n' << m[1];
533 }
534
536 template <class DT>
537 inline std::istream& operator>>(std::istream& is, ML_LA_NAMESPACE::Tmat2<DT>& m)
538 {
539 ML_LA_NAMESPACE::Tmat2<DT> m_tmp;
540 is >> m_tmp[0] >> m_tmp[1];
541 if (is){ m = m_tmp; }
542 return is;
543 }
544}
545
546#endif // __mlMatrix2_H
547
548
549
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.
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:823
#define _ML_MAT2_RC(i, j)
Definition mlMatrix2.h:410
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
double(* MLDblFuncPtr)(double)
A function pointer type to a function which returns a double and takes a double as argument.
FloatingPointVector< T, size, DataContainer > operator/(FloatingPointVector< T, size, DataContainer > lhs, MLdouble rhs)
Component wise division of lhs by specialized rhs of type MLdouble.
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition mlMatrix2.h:425
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Return true if yes.
Definition mlMatrix2.h:433
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.
STL namespace.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
istream & operator>>(istream &is, ml::FloatingPointVector< T, size, DataContainer > &v)
Reads a vector from std::istream.