Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkMatrix_h
14 #define mitkMatrix_h
15 
16 #include <itkMatrix.h>
17 
18 #include "mitkArray.h"
19 #include "mitkEqual.h"
20 #include "mitkNumericConstants.h"
21 
22 namespace mitk
23 {
24  template <class T, unsigned int NRows = 3, unsigned int NColumns = 3>
25  class Matrix : public itk::Matrix<T, NRows, NColumns>
26  {
27  public:
29  typedef Matrix Self;
30 
31  typedef typename itk::Matrix<T, NRows, NColumns>::InternalMatrixType InternalMatrixType;
32 
34  explicit Matrix<T, NRows, NColumns>() : itk::Matrix<T, NRows, NColumns>() {}
36  explicit Matrix<T, NRows, NColumns>(const Matrix &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
38  Matrix<T, NRows, NColumns>(const itk::Matrix<T, NRows, NColumns> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix)
39  {
40  }
42  inline Matrix<T, NRows, NColumns>(const vnl_matrix<T> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
44  inline explicit Matrix<T, NRows, NColumns>(InternalMatrixType &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
48  using itk::Matrix<T, NRows, NColumns>::operator=;
49 
56  template <typename ArrayType>
57  void FillMatrix(const ArrayType &array)
58  {
59  for (unsigned i = 0; i < NRows; i++)
60  {
61  for (unsigned j = 0; j < NColumns; j++)
62  {
63  (*this)[i][j] = array[i][j];
64  }
65  }
66  };
67 
71  template <typename MatrixType>
72  void ToArray(MatrixType matrix) const
73  {
74  for (unsigned i = 0; i < NRows; i++)
75  {
76  for (unsigned j = 0; j < NColumns; j++)
77  {
78  matrix[i][j] = (*this)[i][j];
79  }
80  }
81  }
82  };
83 
87 
95  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
96  inline bool MatrixEqualRMS(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
97  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
98  mitk::ScalarType epsilon = mitk::eps)
99  {
100  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
101  {
102  vnl_matrix_fixed<TCoordRep, NRows, NCols> differenceMatrix = matrix1 - matrix2;
103  if (differenceMatrix.rms() < epsilon)
104  {
105  return true;
106  }
107  else
108  {
109  return false;
110  }
111  }
112  else
113  {
114  return false;
115  }
116  }
117 
125  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
126  inline bool MatrixEqualRMS(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
127  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
128  mitk::ScalarType epsilon = mitk::eps)
129  {
130  return mitk::MatrixEqualRMS(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
131  }
132 
139  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
140  inline bool MatrixEqualElementWise(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
141  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
142  mitk::ScalarType epsilon = mitk::eps)
143  {
144  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
145  {
146  for (unsigned int r = 0; r < NRows; r++)
147  {
148  for (unsigned int c = 0; c < NCols; c++)
149  {
150  TCoordRep difference = matrix1(r, c) - matrix2(r, c);
151  if (DifferenceBiggerOrEqualEps(difference, epsilon))
152  {
153  return false;
154  }
155  }
156  }
157  return true;
158  }
159  else
160  {
161  return false;
162  }
163  }
164 
171  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
172  inline bool MatrixEqualElementWise(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
173  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
174  mitk::ScalarType epsilon = mitk::eps)
175  {
176  return mitk::MatrixEqualElementWise(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
177  }
178 }
179 
180 #endif
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitkArray.h
mitk::Matrix::Self
Matrix Self
Definition: mitkMatrix.h:29
mitk::Matrix4D
Matrix< ScalarType, 4, 4 > Matrix4D
Definition: mitkMatrix.h:86
mitk::Matrix
Definition: mitkMatrix.h:25
mitkNumericConstants.h
mitk::Matrix::FillMatrix
void FillMatrix(const ArrayType &array)
Definition: mitkMatrix.h:57
mitk::Matrix2D
Matrix< ScalarType, 2, 2 > Matrix2D
Definition: mitkMatrix.h:84
mitk::Matrix::InternalMatrixType
itk::Matrix< T, NRows, NColumns >::InternalMatrixType InternalMatrixType
Definition: mitkMatrix.h:31
mitkEqual.h
mitk::Matrix::ToArray
void ToArray(MatrixType matrix) const
Definition: mitkMatrix.h:72
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
mitk::MatrixEqualRMS
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:96
mitk::DifferenceBiggerOrEqualEps
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Definition: mitkEqual.h:32
mitk::Matrix3D
Matrix< ScalarType, 3, 3 > Matrix3D
Definition: mitkMatrix.h:85
mitk::MatrixEqualElementWise
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:140
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20