1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 #include "itkVtkAbstractTransform.h"
18 #include <mitkNumericTypes.h>
19 #include <vtkAbstractTransform.h>
23 template <class TScalarType>
24 itk::VtkAbstractTransform<TScalarType>::VtkAbstractTransform()
25 : m_VtkAbstractTransform(nullptr), m_InverseVtkAbstractTransform(nullptr), m_LastVtkAbstractTransformTimeStamp(0)
29 template <class TScalarType>
30 itk::VtkAbstractTransform<TScalarType>::~VtkAbstractTransform()
32 if (m_VtkAbstractTransform != nullptr)
33 m_VtkAbstractTransform->UnRegister(nullptr);
36 template <class TScalarType>
37 vtkAbstractTransform *itk::VtkAbstractTransform<TScalarType>::GetVtkAbstractTransform() const
39 return m_VtkAbstractTransform;
42 template <class TScalarType>
43 vtkAbstractTransform *itk::VtkAbstractTransform<TScalarType>::GetInverseVtkAbstractTransform() const
45 return m_InverseVtkAbstractTransform;
48 template <class TScalarType>
49 void itk::VtkAbstractTransform<TScalarType>::SetVtkAbstractTransform(vtkAbstractTransform *aVtkAbstractTransform)
51 if (m_VtkAbstractTransform == aVtkAbstractTransform)
54 if (m_VtkAbstractTransform != nullptr)
55 m_VtkAbstractTransform->UnRegister(nullptr);
57 m_VtkAbstractTransform = aVtkAbstractTransform;
58 if (m_VtkAbstractTransform != nullptr)
60 m_VtkAbstractTransform->Register(nullptr);
61 m_InverseVtkAbstractTransform = m_VtkAbstractTransform->GetInverse(); // memory managed by m_VtkAbstractTransform
64 m_LastVtkAbstractTransformTimeStamp = m_VtkAbstractTransform->GetMTime();
70 template <class TScalarType>
71 typename itk::VtkAbstractTransform<TScalarType>::OutputPointType
72 itk::VtkAbstractTransform<TScalarType>::TransformPoint(const InputPointType &point) const
74 assert(m_VtkAbstractTransform != nullptr);
76 OutputPointType outputpoint;
77 vnl_vector<TScalarType> vnl_vec;
78 mitk::ScalarType vtkpt[3];
79 mitk::itk2vtk(point, vtkpt);
80 m_VtkAbstractTransform->TransformPoint(vtkpt, vtkpt);
81 mitk::vtk2itk(vtkpt, outputpoint);
86 template <class TScalarType>
87 typename itk::VtkAbstractTransform<TScalarType>::OutputVectorType
88 itk::VtkAbstractTransform<TScalarType>::TransformVector(const InputVectorType &vect) const
90 assert(m_VtkAbstractTransform != nullptr);
92 OutputVectorType outputvector;
93 vnl_vector<TScalarType> vnl_vec;
94 mitk::ScalarType vtkpt[3] = {0, 0, 0};
95 mitk::ScalarType vtkvec[3];
96 mitk::vnl2vtk<TScalarType, mitk::ScalarType>(vect.GetVnlVector(), vtkvec);
97 m_VtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
98 mitk::vtk2itk(vtkvec, outputvector);
102 // Transform a vnl_vector_fixed
103 template <class TScalarType>
104 typename itk::VtkAbstractTransform<TScalarType>::OutputVnlVectorType
105 itk::VtkAbstractTransform<TScalarType>::TransformVector(const InputVnlVectorType &vect) const
107 assert(m_VtkAbstractTransform != nullptr);
109 OutputVnlVectorType outputvector;
110 mitk::ScalarType vtkpt[3] = {0, 0, 0};
111 mitk::ScalarType vtkvec[3];
112 mitk::vnl2vtk<TScalarType, mitk::ScalarType>(vect, vtkvec);
113 m_VtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
114 mitk::vtk2itk(vtkvec, outputvector);
118 // Transform a CovariantVector
119 template <class TScalarType>
120 typename itk::VtkAbstractTransform<TScalarType>::OutputCovariantVectorType
121 itk::VtkAbstractTransform<TScalarType>::TransformCovariantVector(const InputCovariantVectorType & /*vec*/) const
123 itkExceptionMacro(<< "implement before using!");
124 OutputCovariantVectorType result; // Converted vector
126 // for (unsigned int i = 0; i < NDimensions; i++)
128 // result[i] = NumericTraits<mitk::ScalarType>::Zero;
129 // for (unsigned int j = 0; j < NDimensions; j++)
131 // result[i] += m_Inverse[j][i]*vec[j]; // Inverse transposed
137 // Back transform a point
138 template <class TScalarType>
139 typename VtkAbstractTransform<TScalarType>::InputPointType itk::VtkAbstractTransform<TScalarType>::BackTransform(
140 const OutputPointType &point) const
142 assert(m_VtkAbstractTransform != nullptr);
144 OutputPointType outputpoint;
145 mitk::ScalarType vtkpt[3];
146 mitk::itk2vtk(point, vtkpt);
147 m_InverseVtkAbstractTransform->TransformPoint(vtkpt, vtkpt);
148 mitk::vtk2itk(vtkpt, outputpoint);
152 // Back transform a vector
153 template <class TScalarType>
154 typename VtkAbstractTransform<TScalarType>::InputVectorType itk::VtkAbstractTransform<TScalarType>::BackTransform(
155 const OutputVectorType &vect) const
157 assert(m_VtkAbstractTransform != nullptr);
159 OutputVectorType outputvector;
160 mitk::ScalarType vtkpt[3] = {0, 0, 0};
161 mitk::ScalarType vtkvec[3];
162 mitk::itk2vtk(vect, vtkvec);
163 m_InverseVtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
164 mitk::vtk2itk(vtkvec, outputvector);
168 // Back transform a vnl_vector
169 template <class TScalarType>
170 typename VtkAbstractTransform<TScalarType>::InputVnlVectorType itk::VtkAbstractTransform<TScalarType>::BackTransform(
171 const OutputVnlVectorType &vect) const
173 assert(m_InverseVtkAbstractTransform != nullptr);
175 OutputVnlVectorType outputvector;
176 mitk::ScalarType vtkpt[3] = {0, 0, 0};
177 mitk::ScalarType vtkvec[3];
178 mitk::itk2vtk(vect, vtkvec);
179 m_InverseVtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
180 mitk::vtk2itk(vtkvec, outputvector);
184 // Back Transform a CovariantVector
185 template <class TScalarType>
186 typename VtkAbstractTransform<TScalarType>::InputCovariantVectorType
187 itk::VtkAbstractTransform<TScalarType>::BackTransform(const OutputCovariantVectorType &vec) const
189 itkExceptionMacro(<< "implement before using!");
190 // for (unsigned int i = 0; i < NDimensions; i++)
192 // result[i] = NumericTraits<mitk::ScalarType>::Zero;
193 // for (unsigned int j = 0; j < NDimensions; j++)
195 // result[i] += m_Matrix[j][i]*vec[j]; // Direct matrix transposed
201 template <class TScalarType>
202 unsigned long itk::VtkAbstractTransform<TScalarType>::GetMTime() const
204 if ((m_VtkAbstractTransform != nullptr) &&
205 (m_LastVtkAbstractTransformTimeStamp < m_VtkAbstractTransform->GetMTime()))
207 m_LastVtkAbstractTransformTimeStamp = m_VtkAbstractTransform->GetMTime();
211 return Superclass::GetMTime();
214 template <class TScalarType>
215 void itk::VtkAbstractTransform<TScalarType>::SetParameters(const ParametersType &)
220 template <class TScalarType>
221 void itk::VtkAbstractTransform<TScalarType>::SetFixedParameters(const ParametersType &)
226 template <class TScalarType>
227 void itk::VtkAbstractTransform<TScalarType>::ComputeJacobianWithRespectToParameters(const InputPointType &,
228 JacobianType &) const
233 template <class TScalarType>
234 void itk::VtkAbstractTransform<TScalarType>::ComputeJacobianWithRespectToPosition(const InputPointType &,
235 JacobianType &) const