Medical Imaging Interaction Toolkit  2023.04.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 {
22  template <class TTransformType>
23  void TransferVtkMatrixToItkTransform(const vtkMatrix4x4 *vtkmatrix, TTransformType *itkTransform)
24  {
25  if (itkTransform == nullptr)
26  return;
27 
28  typename TTransformType::MatrixType::InternalMatrixType &vnlMatrix =
29  const_cast<typename TTransformType::MatrixType::InternalMatrixType &>(itkTransform->GetMatrix().GetVnlMatrix());
30 
31  for (int i = 0; i < 3; ++i)
32  for (int j = 0; j < 3; ++j)
33  vnlMatrix[i][j] = vtkmatrix->GetElement(i, j);
34  // *This* ensures m_MatrixMTime.Modified(), which is therewith not equal to
35  // m_InverseMatrixMTime, thus a new inverse will be calculated (when
36  // requested).
37  static_cast<mitk::ItkMatrixHack<TTransformType> *>(itkTransform)->MatrixChanged();
38 
39  typename TTransformType::OffsetType offset;
40  offset[0] = vtkmatrix->GetElement(0, 3);
41  offset[1] = vtkmatrix->GetElement(1, 3);
42  offset[2] = vtkmatrix->GetElement(2, 3);
43  itkTransform->SetOffset(offset);
44  }
45 
46  template <class TTransformType>
47  void TransferItkTransformToVtkMatrix(const TTransformType *itkTransform, vtkMatrix4x4 *vtkmatrix)
48  {
49  int i, j;
50  for (i = 0; i < 3; ++i)
51  for (j = 0; j < 3; ++j)
52  vtkmatrix->SetElement(i, j, itkTransform->GetMatrix().GetVnlMatrix().get(i, j));
53  for (i = 0; i < 3; ++i)
54  vtkmatrix->SetElement(i, 3, itkTransform->GetOffset()[i]);
55  for (i = 0; i < 3; ++i)
56  vtkmatrix->SetElement(3, i, 0.0);
57  vtkmatrix->SetElement(3, 3, 1);
58  }
59 
60  template <class TTransformType1, class TTransformType2>
61  void ConvertItkTransform(const TTransformType1 *sourceTransform, TTransformType2 *destTransform)
62  {
63  if ((sourceTransform == nullptr) || (destTransform == nullptr))
64  return;
65 
66  destTransform->SetMatrix(sourceTransform->GetMatrix());
67  destTransform->SetOffset(sourceTransform->GetOffset());
68  }
69 
70  template <class TMatrixType>
71  void GetRotation(const mitk::BaseGeometry *geometry, TMatrixType &itkmatrix)
72  {
73  const mitk::Vector3D &spacing = geometry->GetSpacing();
74  auto &geometryVnlMatrix = geometry->GetIndexToWorldTransform()->GetMatrix().GetVnlMatrix();
75 
76  typename TMatrixType::InternalMatrixType &outputVnlMatrix =
77  itkmatrix.GetVnlMatrix();
78 
79  for (int i = 0; i < 3; ++i)
80  for (int j = 0; j < 3; ++j)
81  outputVnlMatrix[i][j] = geometryVnlMatrix[i][j] / spacing[j];
82  }
83 
84  template <class TTransformType>
85  void GetWorldToItkPhysicalTransform(const mitk::BaseGeometry *geometry, TTransformType *itkTransform)
86  {
87  if (itkTransform == nullptr)
88  return;
89 
90  // get rotation matrix and offset from Geometry and transfer in TTransformType types
91  typename TTransformType::MatrixType rotationMatrix;
92  GetRotation(geometry, rotationMatrix);
93 
94  const typename mitk::BaseGeometry::TransformType::OffsetType &geometryOffset =
95  geometry->GetIndexToWorldTransform()->GetOffset();
96 
97  vnl_vector<typename TTransformType::MatrixType::ValueType> vnlOffset(3);
98  vnlOffset[0] = geometryOffset[0];
99  vnlOffset[1] = geometryOffset[1];
100  vnlOffset[2] = geometryOffset[2];
101 
102  // do calculations
103  typename TTransformType::MatrixType::InternalMatrixType inverseRotationVnlMatrix = rotationMatrix.GetTranspose();
104 
105  vnlOffset -= inverseRotationVnlMatrix * vnlOffset;
106 
107  typename TTransformType::OutputVectorType offset; // vnl_vector<TTransformType::MatrixType::ValueType> offset;
108  offset[0] = vnlOffset[0];
109  offset[1] = vnlOffset[1];
110  offset[2] = vnlOffset[2];
111  itkTransform->SetOffset(offset);
112 
113  // copy in destination itkTransform
114  typename TTransformType::MatrixType::InternalMatrixType &destVnlMatrix =
115  itkTransform->GetMatrix().GetVnlMatrix();
116 
117  for (int i = 0; i < 3; ++i)
118  for (int j = 0; j < 3; ++j)
119  destVnlMatrix[i][j] = inverseRotationVnlMatrix[i][j];
120  // *This* ensures m_MatrixMTime.Modified(), which is therewith not equal to
121  // m_InverseMatrixMTime, thus a new inverse will be calculated (when
122  // requested).
123  static_cast<mitk::ItkMatrixHack<TTransformType> *>(itkTransform)->MatrixChanged();
124  }
125 }
126 
127 #endif
mitk::GetRotation
void GetRotation(const mitk::BaseGeometry *geometry, TMatrixType &itkmatrix)
Definition: mitkMatrixConvert.h:71
mitk::TransferVtkMatrixToItkTransform
void TransferVtkMatrixToItkTransform(const vtkMatrix4x4 *vtkmatrix, TTransformType *itkTransform)
Definition: mitkMatrixConvert.h:23
mitk::GetWorldToItkPhysicalTransform
void GetWorldToItkPhysicalTransform(const mitk::BaseGeometry *geometry, TTransformType *itkTransform)
Definition: mitkMatrixConvert.h:85
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::TransferItkTransformToVtkMatrix
void TransferItkTransformToVtkMatrix(const TTransformType *itkTransform, vtkMatrix4x4 *vtkmatrix)
Definition: mitkMatrixConvert.h:47
mitk::Vector< ScalarType, 3 >
mitk::BaseGeometry
BaseGeometry Describes the geometry of a data object.
Definition: mitkBaseGeometry.h:94
mitk::BaseGeometry::GetSpacing
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
mitkBaseGeometry.h
mitkItkMatrixHack.h
mitk::BaseGeometry::GetIndexToWorldTransform
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
mitk::ConvertItkTransform
void ConvertItkTransform(const TTransformType1 *sourceTransform, TTransformType2 *destTransform)
Definition: mitkMatrixConvert.h:61
mitk::ItkMatrixHack
Internal hack to set m_MatrixMTime of itk::MatrixOffsetTransformBase correctly after changing the mat...
Definition: mitkItkMatrixHack.h:31