ITK  4.10.0
Insight Segmentation and Registration Toolkit
itkMINCTransformAdapter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMINCTransformAdapter_h
19 #define itkMINCTransformAdapter_h
20 
21 #include "itkObject.h"
22 #include "itkPoint.h"
23 #include "itkVector.h"
24 #include "itkCovariantVector.h"
25 #include "vnl/vnl_matrix_fixed.h"
26 #include "vnl/vnl_vector_fixed.h"
27 #include "vnl/vnl_det.h"
28 #include "vnl/vnl_vector_fixed_ref.h"
29 #include "vnl/vnl_vector.h"
30 #include "itkTransform.h"
31 #include "itkObjectFactory.h"
32 
33 //minc header
34 #include "itk_minc2.h"
35 
36 namespace itk
37 {
38 
47 template<typename TParametersValueType=double, unsigned int NInputDimensions=3,unsigned int NOutputDimensions=3>
48  class MINCTransformAdapter : public Transform<TParametersValueType, NInputDimensions, NOutputDimensions>
49 {
50 public:
53 
55 
58 
60 
62  itkNewMacro(Self);
63 
65  itkTypeMacro( MINCTransformAdapter, Transform );
66 
68  itkStaticConstMacro(InputSpaceDimension, unsigned int, NInputDimensions);
69  itkStaticConstMacro(OutputSpaceDimension, unsigned int, NOutputDimensions);
71 
73  typedef double ScalarType;
74 
78 
81 
86 
91 
94 
96 
100 
102  typedef vnl_vector_fixed<TParametersValueType, NInputDimensions> InputVnlVectorType;
103  typedef vnl_vector_fixed<TParametersValueType, NOutputDimensions> OutputVnlVectorType;
104 
106  virtual OutputPointType TransformPoint(const InputPointType &point ) const ITK_OVERRIDE
107  {
108  if(!m_Initialized)
109  {
110  return point;
111  }
112 
114  {
115  return point;
116  }
117 
118  OutputPointType pnt;
119  //works only for 3D->3D transforms
120  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), point[0], point[1], point[2], &pnt[0], &pnt[1], &pnt[2]);
121 
122  return pnt;
123  }
124 
126  void estimate_local_jacobian(const InputPointType &orig, vnl_matrix_fixed< double, 3, 3 > &m)
127  {
128  double u1,v1,w1;
129  double u2,v2,w2;
130  const double delta=1e-4;
131 
132  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0]-delta, orig[1], orig[2],&u1, &v1, &w1);
133  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0]+delta, orig[1], orig[2],&u2, &v2, &w2);
134  m(0,0)=(u2-u1)/(2*delta);
135  m(0,1)=(v2-v1)/(2*delta);
136  m(0,2)=(w2-w1)/(2*delta);
137 
138  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0], orig[1]-delta, orig[2],&u1, &v1, &w1);
139  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm) , orig[0], orig[1]+delta, orig[2],&u2, &v2, &w2);
140  m(1,0)=(u2-u1)/(2*delta);
141  m(1,1)=(v2-v1)/(2*delta);
142  m(1,2)=(w2-w1)/(2*delta);
143 
144  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), orig[0], orig[1], orig[2]-delta,&u1, &v1, &w1);
145  general_transform_point((m_Invert ? &m_Xfm_inv : &m_Xfm), orig[0], orig[1], orig[2]+delta,&u2, &v2, &w2);
146  m(2,0)=(u2-u1)/(2*delta);
147  m(2,1)=(v2-v1)/(2*delta);
148  m(2,2)=(w2-w1)/(2*delta);
149  }
150 
152  OutputVectorType TransformVector( const InputVectorType& vector, const InputPointType & ) const ITK_OVERRIDE
153  {
154  itkExceptionMacro( << "Not Implemented" );
155  return vector;
156  }
158 
160  OutputVnlVectorType TransformVector( const InputVnlVectorType& vector, const InputPointType & ) const ITK_OVERRIDE
161  {
162  itkExceptionMacro( << "Not Implemented" );
163  return vector;
164  }
166 
168  OutputVectorType TransformVector( const InputVectorType& vector) const ITK_OVERRIDE
169  {
170  return Superclass::TransformVector(vector);
171  }
172 
174  OutputVnlVectorType TransformVector( const InputVnlVectorType& vector) const ITK_OVERRIDE
175  {
176  return Superclass::TransformVector(vector);
177  }
178 
180  OutputVectorPixelType TransformVector( const InputVectorPixelType& vector) const ITK_OVERRIDE
181  {
182  return Superclass::TransformVector(vector);
183  }
184 
186  OutputVectorPixelType TransformVector(
187  const InputVectorPixelType& vector,
188  const InputPointType & ) const ITK_OVERRIDE
189  {
190  itkExceptionMacro( << "Not Implemented" );
191  return vector;
192  }
194 
196  virtual OutputCovariantVectorType TransformCovariantVector(
197  const InputCovariantVectorType &vector
198  , const InputPointType & ) const ITK_OVERRIDE
199  {
200  itkExceptionMacro( << "Not Implemented" );
201  return vector;
202  }
204 
206  virtual OutputCovariantVectorType TransformCovariantVector(
207  const InputCovariantVectorType &vector) const ITK_OVERRIDE
208  {
210  }
211 
213  virtual OutputVectorPixelType TransformCovariantVector(
214  const InputVectorPixelType &vector) const ITK_OVERRIDE
215  {
217  }
218 
220  virtual OutputVectorPixelType TransformCovariantVector(
221  const InputVectorPixelType &vector, const InputPointType & ) const ITK_OVERRIDE
222  {
223  itkExceptionMacro( << "Not Implemented" );
224  return vector;
225  }
227 
230  virtual void SetIdentity( void )
231  {
232  cleanup();
233  }
234 
235  virtual void SetFixedParameters(const FixedParametersType &) ITK_OVERRIDE
236  {
237  itkExceptionMacro( << "Not Implemented" );
238  }
239 
241  const InputPointType &,
242  JacobianType &) const ITK_OVERRIDE
243  {
244  itkExceptionMacro( << "Not Implemented" );
245  }
246 
247  virtual NumberOfParametersType GetNumberOfParameters(void) const ITK_OVERRIDE
248  {
249  //this transform is defined by XFM file
250  itkExceptionMacro( << "Not Defined" );
251  return 0;
252  }
253 
256  virtual void SetParameters(const ParametersType &) ITK_OVERRIDE
257  {
258  itkExceptionMacro( << "Not Implemented" );
259  }
260 
261  virtual const ParametersType & GetParameters(void) const ITK_OVERRIDE
262  {
263  itkExceptionMacro( << "Not Implemented" );
264  return m_Parameters;
265  }
266 
267  void OpenXfm(const char *xfm)
268  {
269  cleanup();
270  if(input_transform_file((char*)xfm, &m_Xfm) != VIO_OK)
271  itkExceptionMacro( << "Error reading XFM:" << xfm );
272  m_Initialized=true;
273  }
274 
275  void Invert(void)
276  {
277  if(!m_Initialized)
278  itkExceptionMacro( << "XFM not initialized" );
280  {
281  create_inverse_general_transform(&m_Xfm,&m_Xfm_inv);
283  }
284  m_Invert= !m_Invert;
285  }
286 
287 protected:
289  Transform<TParametersValueType, NInputDimensions, NOutputDimensions>(0),
290  m_Invert(false),
291  m_Initialized(false),
292  m_Initialized_invert(false)
293  {
294  if(NInputDimensions!=3 || NOutputDimensions!=3)
295  itkExceptionMacro(<< "Sorry, only 3D to 3d minc xfm transform is currently implemented");
296  }
297 
299  {
300  cleanup();
301  }
302 
303  void cleanup(void)
304  {
305  if(m_Initialized)
306  {
307  delete_general_transform(&m_Xfm);
308  }
310  {
311  delete_general_transform(&m_Xfm_inv);
312  }
313  m_Initialized=false;
314  m_Initialized_invert=false;
315  }
316 
317  ParametersType m_Parameters;
318 
319  mutable VIO_General_transform m_Xfm;
320  mutable VIO_General_transform m_Xfm_inv;
321 
322  bool m_Invert;
325 
326 private:
327  MINCTransformAdapter ( const Self & ) ITK_DELETE_FUNCTION;
328  void operator= ( const Self & ) ITK_DELETE_FUNCTION;
329 };
330 
331 }
332 #endif //itkMINCTransformAdapter_h
OutputVectorPixelType TransformVector(const InputVectorPixelType &vector, const InputPointType &) const override
vnl_vector_fixed< TParametersValueType, NOutputDimensions > OutputVnlVectorType
static const unsigned int InputSpaceDimension
virtual void ComputeJacobianWithRespectToParameters(const InputPointType &, JacobianType &) const override
virtual OutputPointType TransformPoint(const InputPointType &point) const override
Superclass::ParametersType ParametersType
Definition: itkTransform.h:117
vnl_vector_fixed< TParametersValueType, NInputDimensions > InputVnlVectorType
CovariantVector< TParametersValueType, itkGetStaticConstMacro(InputSpaceDimension)> InputCovariantVectorType
virtual OutputVectorType TransformVector(const InputVectorType &) const
Definition: itkTransform.h:193
OutputVectorPixelType TransformVector(const InputVectorPixelType &vector) const override
Vector< TParametersValueType, itkGetStaticConstMacro(OutputSpaceDimension)> OutputVectorType
virtual NumberOfParametersType GetNumberOfParameters(void) const override
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
Definition: itkArray.h:30
Superclass::NumberOfParametersType NumberOfParametersType
Definition: itkTransform.h:184
Transform< TParametersValueType, NInputDimensions, NOutputDimensions > Superclass
OutputVectorType TransformVector(const InputVectorType &vector, const InputPointType &) const override
static ITK_CONSTEXPR double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:58
OutputVnlVectorType TransformVector(const InputVnlVectorType &vector) const override
virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType &vector, const InputPointType &) const override
Superclass::FixedParametersType FixedParametersType
Definition: itkTransform.h:115
virtual OutputVectorPixelType TransformCovariantVector(const InputVectorPixelType &vector, const InputPointType &) const override
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:82
Represents an array whose length can be defined at run-time.
Array2D< ParametersValueType > JacobianType
Definition: itkTransform.h:125
ITK wrapper around MINC general transform functions, supports all the transformations that MINC XFM s...
virtual OutputVectorPixelType TransformCovariantVector(const InputVectorPixelType &vector) const override
virtual void SetFixedParameters(const FixedParametersType &) override
void estimate_local_jacobian(const InputPointType &orig, vnl_matrix_fixed< double, 3, 3 > &m)
use finate element difference to estimate local jacobian
OutputVectorType TransformVector(const InputVectorType &vector) const override
virtual const ParametersType & GetParameters(void) const override
Superclass::JacobianType JacobianType
CovariantVector< TParametersValueType, itkGetStaticConstMacro(OutputSpaceDimension)> OutputCovariantVectorType
Vector< TParametersValueType, itkGetStaticConstMacro(InputSpaceDimension)> InputVectorType
virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType &vector) const override
virtual void SetParameters(const ParametersType &) override
VariableLengthVector< TParametersValueType > OutputVectorPixelType
Superclass::FixedParametersType FixedParametersType
virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType &) const
Definition: itkTransform.h:239
OutputVnlVectorType TransformVector(const InputVnlVectorType &vector, const InputPointType &) const override
Superclass::ParametersType ParametersType
Point< TParametersValueType, NInputDimensions > InputPointType
Point< TParametersValueType, NInputDimensions > OutputPointType
Superclass::NumberOfParametersType NumberOfParametersType
SmartPointer< const Self > ConstPointer
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
VariableLengthVector< TParametersValueType > InputVectorPixelType
static const unsigned int OutputSpaceDimension