MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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> class Tmat2 : public FloatingPointMatrix<Tvec2<DT>, 2>
34{
35public:
37 typedef DT ComponentType;
38
39 //---------------------------------------------------------------
40 // Constructors, set, and get methods.
41 //---------------------------------------------------------------
42 // Constructs a matrix from four zero elements.
44
45 // Constructs a matrix that has the argument \p diagValue as the diagonal values, zero otherwise.
46 Tmat2(const DT diagValue);
47
48 // Composes a matrix from the two row vectors \p row0 and \p row1.
50
51 // Copy constructor from the Tmat2 \p mat.
53
54 // Constructor from four floating point values in an array given by \p mat, row by row.
55 Tmat2(const float mat[4]);
56
57 // Constructor from four double values in an array given by \p mat, row by row.
58 Tmat2(const double mat[4]);
59
60 // Initializes all matrix elements explicitly with scalars,
61 // filling it row by row.
62 Tmat2(const DT in00, const DT in01, const DT in10, const DT in11);
63
64 // Copies contents from float array \p mat into *this, row by row.
65 void setValues(const float mat[4]);
66
67 // Copies contents of *this into float array \p mat, row by row.
68 // Note that range and precision of the float values may not be
69 // sufficient for the DT matrix contents, written row by row.
70 void getValues(float mat[4]) const;
71
72 // Copies contents from DT array \p mat into *this, row by row.
73 void setValues(const double mat[4]);
74
75 // Copies contents of *this into DT array \p mat, row by row.
76 void getValues(double mat[4]) const;
77
78 // Returns a matrix filled with values \p val.
79 static Tmat2<DT> getMat(const DT val);
80
81 // Sets all values to \p val.
82 void set(const DT val);
83
84 // Returns the identity matrix.
86
87 // Sets a diagonal matrix with \p scale on the diagonal.
88 void setScaleMatrix(const DT scale);
89
90
91 //---------------------------------------------------------------
92 // Operators and indexing functionality.
93 //---------------------------------------------------------------
96 bool operator<(const Tmat2<DT>&) const
97 {
98 return false;
99 }
100
101 // Assigns from a Tmat2.
103
104 // Increments by a Tmat2.
106
107 // Decrements by a Tmat2.
109
110 // Multiplies by a constant \p d.
111 const Tmat2<DT>& operator*=(const DT d);
112
113 // Divides by a constant \p d. Division by zero is not handled and must be avoided by caller.
114 const Tmat2<DT>& operator/=(const DT d);
115
116
117 //---------------------------------------------------------------
118 // Special functions
119 //---------------------------------------------------------------
120 // Returns the determinant of the matrix.
121 DT det() const;
122
123 // Transposes the matrix.
125
126 // Returns the inverse.
127 // If a non-NULL boolean pointer is passed to isInvertible,
128 // then \c true is returned in *isInvertible in the case of a
129 // successful inversion or \c false if the inversion is not possible
130 // (function returns the identity then).
131 // If a NULL pointer is passed as isInvertible, the matrix must
132 // be invertible; otherwise, errors will occur.
133 Tmat2<DT> inverse(bool* isInvertible = nullptr) const;
134
135 // Applies the function \p fct to each component.
137
138}; // end of class *Tmat2*
139
140
141//---------------------------------------------------------------
144//---------------------------------------------------------------
146template <class DT> inline Tmat2<DT>::Tmat2()
147{
148 this->v[0] = this->v[1] = Tvec2<DT>(0);
149}
150
152template <class DT> inline Tmat2<DT>::Tmat2(const DT diagValue)
153{
154 this->v[0][0] = this->v[1][1] = diagValue;
155 this->v[1][0] = this->v[0][1] = 0;
156}
157
158
160template <class DT> inline Tmat2<DT>::Tmat2(const Tvec2<DT>& row0, const Tvec2<DT>& row1)
161{
162 this->v[0] = row0;
163 this->v[1] = row1;
164}
165
167template <class DT> inline Tmat2<DT>::Tmat2(const Tmat2<DT>& mat)
168{
169 this->v[0] = mat.v[0];
170 this->v[1] = mat.v[1];
171}
172
174template <class DT> inline Tmat2<DT>::Tmat2(const float mat[4])
175{
176 setValues(mat);
177}
178
180template <class DT> inline Tmat2<DT>::Tmat2(const double mat[4])
181{
182 setValues(mat);
183}
184
186template <class DT> inline Tmat2<DT>::Tmat2(const DT in00, const DT in01, const DT in10, const DT in11)
187{
188 this->v[0][0] = in00;
189 this->v[0][1] = in01;
190 this->v[1][0] = in10;
191 this->v[1][1] = in11;
192}
193
195template <class DT> inline void Tmat2<DT>::setValues(const float mat[4])
196{
197 this->v[0][0] = static_cast<DT>(mat[0]);
198 this->v[0][1] = static_cast<DT>(mat[1]);
199 this->v[1][0] = static_cast<DT>(mat[2]);
200 this->v[1][1] = static_cast<DT>(mat[3]);
201}
202
204template <class DT> inline void Tmat2<DT>::setValues(const double mat[4])
205{
206 this->v[0][0] = static_cast<DT>(mat[0]);
207 this->v[0][1] = static_cast<DT>(mat[1]);
208 this->v[1][0] = static_cast<DT>(mat[2]);
209 this->v[1][1] = static_cast<DT>(mat[3]);
210}
211
215template <class DT> inline void Tmat2<DT>::getValues(float mat[4]) const
216{
217 mat[0] = static_cast<float>(this->v[0][0]);
218 mat[1] = static_cast<float>(this->v[0][1]);
219 mat[2] = static_cast<float>(this->v[1][0]);
220 mat[3] = static_cast<float>(this->v[1][1]);
221}
222
226template <class DT> inline void Tmat2<DT>::getValues(double mat[4]) const
227{
228 mat[0] = static_cast<double>(this->v[0][0]);
229 mat[1] = static_cast<double>(this->v[0][1]);
230 mat[2] = static_cast<double>(this->v[1][0]);
231 mat[3] = static_cast<double>(this->v[1][1]);
232}
233
235template <class DT> inline Tmat2<DT> Tmat2<DT>::getMat(const DT val)
236{
237 return Tmat2(val, val, val, val);
238}
239
241template <class DT> inline void Tmat2<DT>::set(const DT val)
242{
243 this->v[0] = this->v[1] = Tvec2<DT>(val);
244}
245
247template <class DT> inline Tmat2<DT> Tmat2<DT>::getIdentity()
248{
249 return Tmat2<DT>(1, 0, 0, 1);
250}
251
253template <class DT> inline void Tmat2<DT>::setScaleMatrix(const DT scale)
254{
255 this->v[0][0] = scale;
256 this->v[0][1] = 0;
257 this->v[1][0] = 0;
258 this->v[1][1] = scale;
259}
261
262
263//---------------------------------------------------------------
266//---------------------------------------------------------------
268template <class DT> inline DT Tmat2<DT>::det() const
269{
270 return this->v[0][0] * this->v[1][1] - this->v[0][1] * this->v[1][0];
271}
272
274template <class DT> inline Tmat2<DT> Tmat2<DT>::transpose() const
275{
276 return Tmat2<DT>(Tvec2<DT>(this->v[0][0], this->v[1][0]), Tvec2<DT>(this->v[0][1], this->v[1][1]));
277}
278
280template <class DT> inline const Tmat2<DT>& Tmat2<DT>::apply(MLDblFuncPtr fct)
281{
282 this->v[0].apply(fct);
283 this->v[1].apply(fct);
284 return *this;
285}
286
287//--------------------------------------------------------------------
288// Returns the inverse.
289// If a non-NULL boolean pointer is passed to isInvertible,
290// then \c true is returned in *isInvertible in the case of a
291// successful inversion or \c false if the inversion is not possible
292// (function returns the identity then).
293// If a NULL pointer is passed as isInvertible, the matrix must
294// be invertible; otherwise, errors will occur.
295//--------------------------------------------------------------------
296template <class DT> Tmat2<DT> Tmat2<DT>::inverse(bool* isInvertible) const
297{
298 // Matrix to be returned.
300 // Determinant must be non-zero.
301 DT determinant = det();
303 if (isInvertible == nullptr) {
304 printTemplateError("Tmat2<DT> Tmat2<DT>::inverse() const, matrix not invertible", ML_BAD_PARAMETER,
305 "Returning identity");
306 }
307 else {
308 *isInvertible = false;
309 }
310 retMat = getIdentity();
311 }
312 else {
313 // Get inverse to reduce number of expensive divisions.
314 determinant = (static_cast<DT>(1)) / determinant;
315 retMat[0][0] = static_cast<DT>(this->v[1][1] * determinant);
316
317 retMat[1][0] = this->v[1][0];
318 retMat[1][0] *= static_cast<DT>(-determinant);
319
320 retMat[0][1] = this->v[0][1];
321 retMat[0][1] *= static_cast<DT>(-determinant);
322
323 retMat[1][1] = static_cast<DT>(this->v[0][0] * determinant);
324
325 // Set state and return inverse.
326 if (isInvertible != nullptr) {
327 *isInvertible = true;
328 }
329 }
330 return retMat;
331}
333
334
335//---------------------------------------------------------------
338//---------------------------------------------------------------
340template <class DT> inline const Tmat2<DT>& Tmat2<DT>::operator=(const Tmat2<DT>& m)
341{
342 if (&m != this) {
343 this->v[0] = m.v[0];
344 this->v[1] = m.v[1];
345 }
346 return *this;
347}
348
350template <class DT> inline const Tmat2<DT>& Tmat2<DT>::operator+=(const Tmat2<DT>& m)
351{
352 this->v[0] += m.v[0];
353 this->v[1] += m.v[1];
354 return *this;
355}
356
358template <class DT> inline const Tmat2<DT>& Tmat2<DT>::operator-=(const Tmat2<DT>& m)
359{
360 this->v[0] -= m.v[0];
361 this->v[1] -= m.v[1];
362 return *this;
363}
364
366template <class DT> inline const Tmat2<DT>& Tmat2<DT>::operator*=(const DT d)
367{
368 this->v[0] *= d;
369 this->v[1] *= d;
370 return *this;
371}
372
374template <class DT> inline const Tmat2<DT>& Tmat2<DT>::operator/=(const DT d)
375{
376 this->v[0] /= d;
377 this->v[1] /= d;
378 return *this;
379}
381
382
383//---------------------------------------------------------------
384// Global operators and functions for class Tmat2
385//---------------------------------------------------------------
389#define _ML_MAT2_RC(i, j) a[i][0] * b[0][j] + a[i][1] * b[1][j]
390
391// a * b. Standard matrix multiplication.
392template <class DT> inline Tmat2<DT> operator*(const Tmat2<DT>& a, const Tmat2<DT>& b)
393{
394 return Tmat2<DT>(Tvec2<DT>(_ML_MAT2_RC(0, 0), _ML_MAT2_RC(0, 1)), Tvec2<DT>(_ML_MAT2_RC(1, 0), _ML_MAT2_RC(1, 1)));
395}
396#undef _ML_MAT2_RC
397
399template <class DT> inline bool operator==(const Tmat2<DT>& a, const Tmat2<DT>& b)
400{
401 return ((a[0] == b[0]) && (a[1] == b[1]));
402}
403
405template <class DT> inline bool operator!=(const Tmat2<DT>& a, const Tmat2<DT>& b)
406{
407 return !(a == b);
408}
410
411
412//---------------------------------------------------------------
415//---------------------------------------------------------------
417template <class DT> inline Tmat2<DT> operator-(const Tmat2<DT>& a)
418{
419 return Tmat2<DT>(a) *= static_cast<DT>(-1.0);
420}
421
423template <class DT> inline Tmat2<DT> operator+(const Tmat2<DT>& a, const Tmat2<DT>& b)
424{
425 return Tmat2<DT>(a) += b;
426}
427
429template <class DT> inline Tmat2<DT> operator-(const Tmat2<DT>& a, const Tmat2<DT>& b)
430{
431 return Tmat2<DT>(a) -= b;
432}
433
435template <class DT> inline Tmat2<DT> operator*(const Tmat2<DT>& a, DT d)
436{
437 return Tmat2<DT>(a) *= d;
438}
439
441template <class DT> inline Tmat2<DT> operator*(const DT d, const Tmat2<DT>& a)
442{
443 return Tmat2<DT>(a) *= d;
444}
445
448template <class DT> inline Tmat2<DT> operator/(const Tmat2<DT>& a, const DT d)
449{
450 Tmat2<DT> divi(a);
451 divi /= d;
452 return divi;
453}
454
457template <class DT> inline Tvec2<DT> operator*(const Tmat2<DT>& a, const Tvec2<DT>& v)
458{
459 return Tvec2<DT>(a[0][0] * v[0] + a[0][1] * v[1], a[1][0] * v[0] + a[1][1] * v[1]);
460}
461
464template <class DT> inline Tvec2<DT> operator*(const Tvec2<DT>& v, const Tmat2<DT>& a)
465{
466 return a.transpose() * v;
467}
469
470//-----------------------------------------------------------------------------------
473//-----------------------------------------------------------------------------------
474
484
486
487//-------------------------------------------------------------------
488// Support for standard I/O streams.
489//-------------------------------------------------------------------
490namespace std
491{
493 template <class DT> inline std::ostream& operator<<(std::ostream& os, const ML_LA_NAMESPACE::Tmat2<DT>& m)
494 {
495 return os << m[0] << '\n' << m[1];
496 }
497
499 template <class DT> inline std::istream& operator>>(std::istream& is, ML_LA_NAMESPACE::Tmat2<DT>& m)
500 {
501 ML_LA_NAMESPACE::Tmat2<DT> m_tmp;
502 is >> m_tmp[0] >> m_tmp[1];
503 if (is) {
504 m = m_tmp;
505 }
506 return is;
507 }
508} // namespace std
509
510#endif // __mlMatrix2_H
Base class of all matrix classes that 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:34
Tmat2(const float mat[4])
Constructor from four floating point values in an array given by mat, row by row.
Definition mlMatrix2.h:174
void getValues(double mat[4]) const
Copies the contents of *this into mat, row by row.
Definition mlMatrix2.h:226
Tmat2< DT > transpose() const
Returns the transposed matrix.
Definition mlMatrix2.h:274
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:186
const Tmat2< DT > & operator+=(const Tmat2< DT > &m)
Increments by a Tmat2.
Definition mlMatrix2.h:350
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:374
DT det() const
Returns the determinant of this matrix.
Definition mlMatrix2.h:268
const Tmat2< DT > & operator-=(const Tmat2< DT > &m)
Decrements by a Tmat2.
Definition mlMatrix2.h:358
Tmat2< DT > inverse(bool *isInvertible=nullptr) const
Definition mlMatrix2.h:296
Tmat2()
Constructs the matrix from four zero elements.
Definition mlMatrix2.h:146
void setScaleMatrix(const DT scale)
Sets a diagonal matrix with scale on the diagonal.
Definition mlMatrix2.h:253
Tmat2(const Tmat2< DT > &mat)
Copy constructor from the Tmat2 mat.
Definition mlMatrix2.h:167
bool operator<(const Tmat2< DT > &) const
Dummy 'lesser than operator' that always returns false.
Definition mlMatrix2.h:96
const Tmat2< DT > & operator*=(const DT d)
Multiplies by a constant d.
Definition mlMatrix2.h:366
DT ComponentType
A typedef to 'export' the type of components.
Definition mlMatrix2.h:37
const Tmat2< DT > & operator=(const Tmat2< DT > &m)
Assigns from a Tmat2.
Definition mlMatrix2.h:340
void setValues(const double mat[4])
Copies the contents of mat into *this, row by row.
Definition mlMatrix2.h:204
void set(const DT val)
Sets all values to val.
Definition mlMatrix2.h:241
static Tmat2< DT > getIdentity()
Returns the identity matrix.
Definition mlMatrix2.h:247
Tmat2(const Tvec2< DT > &row0, const Tvec2< DT > &row1)
Composes a matrix from the two vectors row0 and row1.
Definition mlMatrix2.h:160
Tmat2(const double mat[4])
Constructor from four double values in an array given by mat, row by row.
Definition mlMatrix2.h:180
Tmat2(const DT diagValue)
Constructs the matrix that has the argument diagValue as the diagonal values, zero otherwise.
Definition mlMatrix2.h:152
const Tmat2< DT > & apply(MLDblFuncPtr fct)
Applies the method fct to all vectors of *this and returns the matrix.
Definition mlMatrix2.h:280
static Tmat2< DT > getMat(const DT val)
Returns a matrix filled with values val.
Definition mlMatrix2.h:235
void getValues(float mat[4]) const
Copies the contents of *this into mat, row by row.
Definition mlMatrix2.h:215
void setValues(const float mat[4])
Copies the contents of mat into *this, row by row.
Definition mlMatrix2.h:195
Declaration of float vector type traits.
Definition mlVector2.h:57
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns 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:879
#define _ML_MAT2_RC(i, j)
Definition mlMatrix2.h:389
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
double(* MLDblFuncPtr)(double)
A function pointer type to a function that 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 ? Returns true if yes.
Definition mlMatrix2.h:399
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 ? Returns true if yes.
Definition mlMatrix2.h:405
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.