1 /*============================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center (DKFZ)
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
11 ============================================================================*/
13 #include "itkVtkAbstractTransform.h"
14 #include <mitkNumericTypes.h>
15 #include <vtkAbstractTransform.h>
19 template <class TScalarType>
20 itk::VtkAbstractTransform<TScalarType>::VtkAbstractTransform()
21 : m_VtkAbstractTransform(nullptr), m_InverseVtkAbstractTransform(nullptr), m_LastVtkAbstractTransformTimeStamp(0)
25 template <class TScalarType>
26 itk::VtkAbstractTransform<TScalarType>::~VtkAbstractTransform()
28 if (m_VtkAbstractTransform != nullptr)
29 m_VtkAbstractTransform->UnRegister(nullptr);
32 template <class TScalarType>
33 vtkAbstractTransform *itk::VtkAbstractTransform<TScalarType>::GetVtkAbstractTransform() const
35 return m_VtkAbstractTransform;
38 template <class TScalarType>
39 vtkAbstractTransform *itk::VtkAbstractTransform<TScalarType>::GetInverseVtkAbstractTransform() const
41 return m_InverseVtkAbstractTransform;
44 template <class TScalarType>
45 void itk::VtkAbstractTransform<TScalarType>::SetVtkAbstractTransform(vtkAbstractTransform *aVtkAbstractTransform)
47 if (m_VtkAbstractTransform == aVtkAbstractTransform)
50 if (m_VtkAbstractTransform != nullptr)
51 m_VtkAbstractTransform->UnRegister(nullptr);
53 m_VtkAbstractTransform = aVtkAbstractTransform;
54 if (m_VtkAbstractTransform != nullptr)
56 m_VtkAbstractTransform->Register(nullptr);
57 m_InverseVtkAbstractTransform = m_VtkAbstractTransform->GetInverse(); // memory managed by m_VtkAbstractTransform
60 m_LastVtkAbstractTransformTimeStamp = m_VtkAbstractTransform->GetMTime();
66 template <class TScalarType>
67 typename itk::VtkAbstractTransform<TScalarType>::OutputPointType
68 itk::VtkAbstractTransform<TScalarType>::TransformPoint(const InputPointType &point) const
70 assert(m_VtkAbstractTransform != nullptr);
72 OutputPointType outputpoint;
73 vnl_vector<TScalarType> vnl_vec;
74 mitk::ScalarType vtkpt[3];
75 mitk::itk2vtk(point, vtkpt);
76 m_VtkAbstractTransform->TransformPoint(vtkpt, vtkpt);
77 mitk::vtk2itk(vtkpt, outputpoint);
82 template <class TScalarType>
83 typename itk::VtkAbstractTransform<TScalarType>::OutputVectorType
84 itk::VtkAbstractTransform<TScalarType>::TransformVector(const InputVectorType &vect) const
86 assert(m_VtkAbstractTransform != nullptr);
88 OutputVectorType outputvector;
89 vnl_vector<TScalarType> vnl_vec;
90 mitk::ScalarType vtkpt[3] = {0, 0, 0};
91 mitk::ScalarType vtkvec[3];
92 mitk::vnl2vtk<TScalarType, mitk::ScalarType>(vect.GetVnlVector(), vtkvec);
93 m_VtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
94 mitk::vtk2itk(vtkvec, outputvector);
98 // Transform a vnl_vector_fixed
99 template <class TScalarType>
100 typename itk::VtkAbstractTransform<TScalarType>::OutputVnlVectorType
101 itk::VtkAbstractTransform<TScalarType>::TransformVector(const InputVnlVectorType &vect) const
103 assert(m_VtkAbstractTransform != nullptr);
105 OutputVnlVectorType outputvector;
106 mitk::ScalarType vtkpt[3] = {0, 0, 0};
107 mitk::ScalarType vtkvec[3];
108 mitk::vnl2vtk<TScalarType, mitk::ScalarType>(vect, vtkvec);
109 m_VtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
110 mitk::vtk2itk(vtkvec, outputvector);
114 // Transform a CovariantVector
115 template <class TScalarType>
116 typename itk::VtkAbstractTransform<TScalarType>::OutputCovariantVectorType
117 itk::VtkAbstractTransform<TScalarType>::TransformCovariantVector(const InputCovariantVectorType & /*vec*/) const
119 itkExceptionMacro(<< "implement before using!");
120 OutputCovariantVectorType result; // Converted vector
122 // for (unsigned int i = 0; i < NDimensions; i++)
124 // result[i] = NumericTraits<mitk::ScalarType>::Zero;
125 // for (unsigned int j = 0; j < NDimensions; j++)
127 // result[i] += m_Inverse[j][i]*vec[j]; // Inverse transposed
133 // Back transform a point
134 template <class TScalarType>
135 typename VtkAbstractTransform<TScalarType>::InputPointType itk::VtkAbstractTransform<TScalarType>::BackTransform(
136 const OutputPointType &point) const
138 assert(m_VtkAbstractTransform != nullptr);
140 OutputPointType outputpoint;
141 mitk::ScalarType vtkpt[3];
142 mitk::itk2vtk(point, vtkpt);
143 m_InverseVtkAbstractTransform->TransformPoint(vtkpt, vtkpt);
144 mitk::vtk2itk(vtkpt, outputpoint);
148 // Back transform a vector
149 template <class TScalarType>
150 typename VtkAbstractTransform<TScalarType>::InputVectorType itk::VtkAbstractTransform<TScalarType>::BackTransform(
151 const OutputVectorType &vect) const
153 assert(m_VtkAbstractTransform != nullptr);
155 OutputVectorType outputvector;
156 mitk::ScalarType vtkpt[3] = {0, 0, 0};
157 mitk::ScalarType vtkvec[3];
158 mitk::itk2vtk(vect, vtkvec);
159 m_InverseVtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
160 mitk::vtk2itk(vtkvec, outputvector);
164 // Back transform a vnl_vector
165 template <class TScalarType>
166 typename VtkAbstractTransform<TScalarType>::InputVnlVectorType itk::VtkAbstractTransform<TScalarType>::BackTransform(
167 const OutputVnlVectorType &vect) const
169 assert(m_InverseVtkAbstractTransform != nullptr);
171 OutputVnlVectorType outputvector;
172 mitk::ScalarType vtkpt[3] = {0, 0, 0};
173 mitk::ScalarType vtkvec[3];
174 mitk::itk2vtk(vect, vtkvec);
175 m_InverseVtkAbstractTransform->TransformVectorAtPoint(vtkpt, vtkvec, vtkvec);
176 mitk::vtk2itk(vtkvec, outputvector);
180 // Back Transform a CovariantVector
181 template <class TScalarType>
182 typename VtkAbstractTransform<TScalarType>::InputCovariantVectorType
183 itk::VtkAbstractTransform<TScalarType>::BackTransform(const OutputCovariantVectorType &vec) const
185 itkExceptionMacro(<< "implement before using!");
186 // for (unsigned int i = 0; i < NDimensions; i++)
188 // result[i] = NumericTraits<mitk::ScalarType>::Zero;
189 // for (unsigned int j = 0; j < NDimensions; j++)
191 // result[i] += m_Matrix[j][i]*vec[j]; // Direct matrix transposed
197 template <class TScalarType>
198 unsigned long itk::VtkAbstractTransform<TScalarType>::GetMTime() const
200 if ((m_VtkAbstractTransform != nullptr) &&
201 (m_LastVtkAbstractTransformTimeStamp < m_VtkAbstractTransform->GetMTime()))
203 m_LastVtkAbstractTransformTimeStamp = m_VtkAbstractTransform->GetMTime();
207 return Superclass::GetMTime();
210 template <class TScalarType>
211 void itk::VtkAbstractTransform<TScalarType>::SetParameters(const ParametersType &)
216 template <class TScalarType>
217 void itk::VtkAbstractTransform<TScalarType>::SetFixedParameters(const ParametersType &)
222 template <class TScalarType>
223 void itk::VtkAbstractTransform<TScalarType>::ComputeJacobianWithRespectToParameters(const InputPointType &,
224 JacobianType &) const
229 template <class TScalarType>
230 void itk::VtkAbstractTransform<TScalarType>::ComputeJacobianWithRespectToPosition(const InputPointType &,
231 JacobianType &) const