Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkMatrixConvert.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 mitkMatrixConvert_h
14 #define mitkMatrixConvert_h
15 
16 #include <mitkBaseGeometry.h>
17 #include <mitkItkMatrixHack.h>
18 #include <vtkMatrix4x4.h>
19 
20 namespace mitk
21 {
37  template <class TTransformType>
38  void TransferVtkMatrixToItkTransform(const vtkMatrix4x4 *vtkmatrix, TTransformType *itkTransform)
39  {
40  if (itkTransform == nullptr)
41  return;
42 
43  typename TTransformType::MatrixType::InternalMatrixType &vnlMatrix =
44  const_cast<typename TTransformType::MatrixType::InternalMatrixType &>(itkTransform->GetMatrix().GetVnlMatrix());
45 
46  for (int i = 0; i < 3; ++i)
47  for (int j = 0; j < 3; ++j)
48  vnlMatrix[i][j] = vtkmatrix->GetElement(i, j);
49  // *This* ensures m_MatrixMTime.Modified(), which is therewith not equal to
50  // m_InverseMatrixMTime, thus a new inverse will be calculated (when
51  // requested).
52  static_cast<mitk::ItkMatrixHack<TTransformType> *>(itkTransform)->MatrixChanged();
53 
54  typename TTransformType::OffsetType offset;
55  offset[0] = vtkmatrix->GetElement(0, 3);
56  offset[1] = vtkmatrix->GetElement(1, 3);
57  offset[2] = vtkmatrix->GetElement(2, 3);
58  itkTransform->SetOffset(offset);
59  }
60 
74  template <class TTransformType>
75  void TransferItkTransformToVtkMatrix(const TTransformType *itkTransform, vtkMatrix4x4 *vtkmatrix)
76  {
77  int i, j;
78  for (i = 0; i < 3; ++i)
79  for (j = 0; j < 3; ++j)
80  vtkmatrix->SetElement(i, j, itkTransform->GetMatrix().GetVnlMatrix().get(i, j));
81  for (i = 0; i < 3; ++i)
82  vtkmatrix->SetElement(i, 3, itkTransform->GetOffset()[i]);
83  for (i = 0; i < 3; ++i)
84  vtkmatrix->SetElement(3, i, 0.0);
85  vtkmatrix->SetElement(3, 3, 1);
86  }
87 
99  template <class TTransformType1, class TTransformType2>
100  void ConvertItkTransform(const TTransformType1 *sourceTransform, TTransformType2 *destTransform)
101  {
102  if ((sourceTransform == nullptr) || (destTransform == nullptr))
103  return;
104 
105  destTransform->SetMatrix(sourceTransform->GetMatrix());
106  destTransform->SetOffset(sourceTransform->GetOffset());
107  }
108 
121  template <class TMatrixType>
122  void GetRotation(const mitk::BaseGeometry *geometry, TMatrixType &itkmatrix)
123  {
124  const mitk::Vector3D &spacing = geometry->GetSpacing();
125  auto &geometryVnlMatrix = geometry->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix();
126 
127  typename TMatrixType::InternalMatrixType &outputVnlMatrix =
128  itkmatrix.GetVnlMatrix();
129 
130  for (int i = 0; i < 3; ++i)
131  for (int j = 0; j < 3; ++j)
132  outputVnlMatrix[i][j] = geometryVnlMatrix[i][j] / spacing[j];
133  }
134 
149  template <class TTransformType>
150  void GetWorldToItkPhysicalTransform(const mitk::BaseGeometry *geometry, TTransformType *itkTransform)
151  {
152  if (itkTransform == nullptr)
153  return;
154 
155  // get rotation matrix and offset from Geometry and transfer in TTransformType types
156  typename TTransformType::MatrixType rotationMatrix;
157  GetRotation(geometry, rotationMatrix);
158 
159  const typename mitk::BaseGeometry::TransformType::OffsetType &geometryOffset =
160  geometry->GetIndexToWorldTransform()->GetOffset();
161 
162  vnl_vector<typename TTransformType::MatrixType::ValueType> vnlOffset(3);
163  vnlOffset[0] = geometryOffset[0];
164  vnlOffset[1] = geometryOffset[1];
165  vnlOffset[2] = geometryOffset[2];
166 
167  // do calculations
168  typename TTransformType::MatrixType::InternalMatrixType inverseRotationVnlMatrix = rotationMatrix.GetTranspose();
169 
170  vnlOffset -= inverseRotationVnlMatrix * vnlOffset;
171 
172  typename TTransformType::OutputVectorType offset; // vnl_vector<TTransformType::MatrixType::ValueType> offset;
173  offset[0] = vnlOffset[0];
174  offset[1] = vnlOffset[1];
175  offset[2] = vnlOffset[2];
176  itkTransform->SetOffset(offset);
177 
178  // copy in destination itkTransform
179  typename TTransformType::MatrixType::InternalMatrixType &destVnlMatrix =
180  itkTransform->GetMatrix().GetVnlMatrix();
181 
182  for (int i = 0; i < 3; ++i)
183  for (int j = 0; j < 3; ++j)
184  destVnlMatrix[i][j] = inverseRotationVnlMatrix[i][j];
185  // *This* ensures m_MatrixMTime.Modified(), which is therewith not equal to
186  // m_InverseMatrixMTime, thus a new inverse will be calculated (when
187  // requested).
188  static_cast<mitk::ItkMatrixHack<TTransformType> *>(itkTransform)->MatrixChanged();
189  }
190 }
191 
192 #endif
Abstract base class describing the geometry of a data object.
mitk::Vector3D GetSpacing() const
Get the spacing (voxel size) in mm per unit for each axis.
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the affine transform that maps index coordinates to world coordinates.
Internal hack to set m_MatrixMTime of itk::MatrixOffsetTransformBase correctly after changing the mat...
Find image slices visible on a given plane.
void ConvertItkTransform(const TTransformType1 *sourceTransform, TTransformType2 *destTransform)
Convert between two ITK transform types by copying matrix and offset.
void TransferItkTransformToVtkMatrix(const TTransformType *itkTransform, vtkMatrix4x4 *vtkmatrix)
Transfer an ITK transform into a VTK 4x4 matrix.
void GetRotation(const mitk::BaseGeometry *geometry, TMatrixType &itkmatrix)
Extract the rotation matrix from a geometry, removing spacing.
void TransferVtkMatrixToItkTransform(const vtkMatrix4x4 *vtkmatrix, TTransformType *itkTransform)
Transfer a VTK 4x4 matrix into an ITK transform.
void GetWorldToItkPhysicalTransform(const mitk::BaseGeometry *geometry, TTransformType *itkTransform)
Compute the world-to-ITK-physical-space transform from a geometry.