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