Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMatrix.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef MITKMATRIX_H_
18 #define MITKMATRIX_H_
19 
20 #include <itkMatrix.h>
21 
22 #include "mitkArray.h"
23 #include "mitkEqual.h"
24 #include "mitkNumericConstants.h"
25 
26 namespace mitk
27 {
28  template <class T, unsigned int NRows = 3, unsigned int NColumns = 3>
29  class Matrix : public itk::Matrix<T, NRows, NColumns>
30  {
31  public:
33  typedef Matrix Self;
34 
35  typedef typename itk::Matrix<T, NRows, NColumns>::InternalMatrixType InternalMatrixType;
36 
38  explicit Matrix<T, NRows, NColumns>() : itk::Matrix<T, NRows, NColumns>() {}
40  explicit Matrix<T, NRows, NColumns>(const Matrix &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
42  Matrix<T, NRows, NColumns>(const itk::Matrix<T, NRows, NColumns> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix)
43  {
44  }
46  inline Matrix<T, NRows, NColumns>(const vnl_matrix<T> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
48  inline explicit Matrix<T, NRows, NColumns>(InternalMatrixType &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
52  using itk::Matrix<T, NRows, NColumns>::operator=;
53 
60  template <typename ArrayType>
61  void FillMatrix(const ArrayType &array)
62  {
63  for (unsigned i = 0; i < NRows; i++)
64  {
65  for (unsigned j = 0; j < NColumns; j++)
66  {
67  (*this)[i][j] = array[i][j];
68  }
69  }
70  };
71 
75  template <typename MatrixType>
76  void ToArray(MatrixType matrix) const
77  {
78  for (unsigned i = 0; i < NRows; i++)
79  {
80  for (unsigned j = 0; j < NColumns; j++)
81  {
82  matrix[i][j] = (*this)[i][j];
83  }
84  }
85  }
86  };
87 
91 
99  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
100  inline bool MatrixEqualRMS(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
101  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
102  mitk::ScalarType epsilon = mitk::eps)
103  {
104  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
105  {
106  vnl_matrix_fixed<TCoordRep, NRows, NCols> differenceMatrix = matrix1 - matrix2;
107  if (differenceMatrix.rms() < epsilon)
108  {
109  return true;
110  }
111  else
112  {
113  return false;
114  }
115  }
116  else
117  {
118  return false;
119  }
120  }
121 
129  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
130  inline bool MatrixEqualRMS(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
131  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
132  mitk::ScalarType epsilon = mitk::eps)
133  {
134  return mitk::MatrixEqualRMS(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
135  }
136 
143  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
144  inline bool MatrixEqualElementWise(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
145  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
146  mitk::ScalarType epsilon = mitk::eps)
147  {
148  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
149  {
150  for (unsigned int r = 0; r < NRows; r++)
151  {
152  for (unsigned int c = 0; c < NCols; c++)
153  {
154  TCoordRep difference = matrix1(r, c) - matrix2(r, c);
155  if (DifferenceBiggerOrEqualEps(difference, epsilon))
156  {
157  return false;
158  }
159  }
160  }
161  return true;
162  }
163  else
164  {
165  return false;
166  }
167  }
168 
175  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
176  inline bool MatrixEqualElementWise(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
177  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
178  mitk::ScalarType epsilon = mitk::eps)
179  {
180  return mitk::MatrixEqualElementWise(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
181  }
182 }
183 
184 #endif /* MITKMATRIX_H_ */
Matrix Self
Definition: mitkMatrix.h:33
bool MatrixEqualElementWise(const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix1, const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix2, mitk::ScalarType epsilon=mitk::eps)
Check for element-wise matrix equality with a user defined accuracy.
Definition: mitkMatrix.h:144
itk::Matrix< T, NRows, NColumns >::InternalMatrixType InternalMatrixType
Definition: mitkMatrix.h:35
double ScalarType
Matrix< ScalarType, 3, 3 > Matrix3D
Definition: mitkMatrix.h:89
DataCollection - Class to facilitate loading/accessing structured data.
void ToArray(MatrixType matrix) const
Definition: mitkMatrix.h:76
void FillMatrix(const ArrayType &array)
Definition: mitkMatrix.h:61
Matrix< ScalarType, 2, 2 > Matrix2D
Definition: mitkMatrix.h:88
bool MatrixEqualRMS(const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix1, const vnl_matrix_fixed< TCoordRep, NRows, NCols > &matrix2, mitk::ScalarType epsilon=mitk::eps)
Check for matrix equality with a user defined accuracy. As an equality metric the root mean squared e...
Definition: mitkMatrix.h:100
MITKCORE_EXPORT const ScalarType eps
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Definition: mitkEqual.h:26
Matrix< ScalarType, 4, 4 > Matrix4D
Definition: mitkMatrix.h:90