Medical Imaging Interaction Toolkit  2026.06.00
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 {
30  template <class T, unsigned int NRows = 3, unsigned int NColumns = 3>
31  class Matrix : public itk::Matrix<T, NRows, NColumns>
32  {
33  public:
35  typedef Matrix Self;
36 
37  typedef typename itk::Matrix<T, NRows, NColumns>::InternalMatrixType InternalMatrixType;
38 
40  explicit Matrix() : itk::Matrix<T, NRows, NColumns>() {}
42  explicit Matrix(const Matrix &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
44  Matrix(const itk::Matrix<T, NRows, NColumns> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix)
45  {
46  }
48  inline Matrix(const vnl_matrix<T> &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
50  inline explicit Matrix(InternalMatrixType &matrix) : itk::Matrix<T, NRows, NColumns>(matrix) {}
54  using itk::Matrix<T, NRows, NColumns>::operator=;
55 
63  template <typename ArrayType>
64  void FillMatrix(const ArrayType &array)
65  {
66  for (unsigned i = 0; i < NRows; i++)
67  {
68  for (unsigned j = 0; j < NColumns; j++)
69  {
70  (*this)[i][j] = array[i][j];
71  }
72  }
73  };
74 
80  template <typename MatrixType>
81  void ToArray(MatrixType matrix) const
82  {
83  for (unsigned i = 0; i < NRows; i++)
84  {
85  for (unsigned j = 0; j < NColumns; j++)
86  {
87  matrix[i][j] = (*this)[i][j];
88  }
89  }
90  }
91  };
92 
96 
105  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
106  inline bool MatrixEqualRMS(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
107  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
108  mitk::ScalarType epsilon = mitk::eps)
109  {
110  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
111  {
112  vnl_matrix_fixed<TCoordRep, NRows, NCols> differenceMatrix = matrix1 - matrix2;
113  if (differenceMatrix.rms() < epsilon)
114  {
115  return true;
116  }
117  else
118  {
119  return false;
120  }
121  }
122  else
123  {
124  return false;
125  }
126  }
127 
138  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
139  inline bool MatrixEqualRMS(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
140  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
141  mitk::ScalarType epsilon = mitk::eps)
142  {
143  return mitk::MatrixEqualRMS(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
144  }
145 
154  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
155  inline bool MatrixEqualElementWise(const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix1,
156  const vnl_matrix_fixed<TCoordRep, NRows, NCols> &matrix2,
157  mitk::ScalarType epsilon = mitk::eps)
158  {
159  if ((matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()))
160  {
161  for (unsigned int r = 0; r < NRows; r++)
162  {
163  for (unsigned int c = 0; c < NCols; c++)
164  {
165  TCoordRep difference = matrix1(r, c) - matrix2(r, c);
166  if (DifferenceBiggerOrEqualEps(difference, epsilon))
167  {
168  return false;
169  }
170  }
171  }
172  return true;
173  }
174  else
175  {
176  return false;
177  }
178  }
179 
190  template <typename TCoordRep, unsigned int NRows, unsigned int NCols>
191  inline bool MatrixEqualElementWise(const itk::Matrix<TCoordRep, NRows, NCols> &matrix1,
192  const itk::Matrix<TCoordRep, NRows, NCols> &matrix2,
193  mitk::ScalarType epsilon = mitk::eps)
194  {
195  return mitk::MatrixEqualElementWise(matrix1.GetVnlMatrix(), matrix2.GetVnlMatrix(), epsilon);
196  }
197 }
198 
199 #endif
MITK matrix type extending itk::Matrix with additional constructors and utility methods.
Definition: mitkMatrix.h:32
void ToArray(MatrixType matrix) const
Copy the elements of this matrix into a 2D array.
Definition: mitkMatrix.h:81
Matrix(InternalMatrixType &matrix)
Constructor from internal matrix type.
Definition: mitkMatrix.h:50
Matrix(const vnl_matrix< T > &matrix)
Constructor from vnl_matrix.
Definition: mitkMatrix.h:48
Matrix Self
Standard class typedefs.
Definition: mitkMatrix.h:35
Matrix()
Default constructor.
Definition: mitkMatrix.h:40
itk::Matrix< T, NRows, NColumns >::InternalMatrixType InternalMatrixType
Definition: mitkMatrix.h:37
Matrix(const itk::Matrix< T, NRows, NColumns > &matrix)
Copy constructor for itk compatibility.
Definition: mitkMatrix.h:44
void FillMatrix(const ArrayType &array)
Copy elements from a 2D array into this matrix.
Definition: mitkMatrix.h:64
Matrix(const Matrix &matrix)
Copy constructor.
Definition: mitkMatrix.h:42
Find image slices visible on a given plane.
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:155
MITKCORE_EXPORT const ScalarType eps
Epsilon value for floating point comparison (vnl_math::eps * 100).
Matrix< ScalarType, 2, 2 > Matrix2D
Definition: mitkMatrix.h:93
Matrix< ScalarType, 3, 3 > Matrix3D
Definition: mitkMatrix.h:94
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 using root mean squared error (RMS) of all elements.
Definition: mitkMatrix.h:106
bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon=mitk::eps)
Check if the absolute difference is bigger than or equal to a given epsilon.
Definition: mitkEqual.h:32
double ScalarType
Scalar type used throughout MITK for geometric computations.
Matrix< ScalarType, 4, 4 > Matrix4D
Definition: mitkMatrix.h:95