Orfeo Toolbox  3.16
itkImageToSpatialObjectRegistrationMethod.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImageToSpatialObjectRegistrationMethod.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-24 20:02:57 $
7  Version: $Revision: 1.9 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkImageToSpatialObjectRegistrationMethod_txx
18 #define __itkImageToSpatialObjectRegistrationMethod_txx
19 
21 
22 namespace itk
23 {
24 
26 template < typename TFixedImage, typename TMovingSpatialObject >
29 {
30  this->SetNumberOfRequiredOutputs( 1 ); // for the Transform
31 
32  m_FixedImage = 0; // has to be provided by the user.
33  m_MovingSpatialObject = 0; // has to be provided by the user.
34  m_Transform = 0; // has to be provided by the user.
35  m_Interpolator = 0; // has to be provided by the user.
36  m_Metric = 0; // has to be provided by the user.
37  m_Optimizer = 0; // has to be provided by the user.
38 
39 
40  m_InitialTransformParameters = ParametersType(1);
41  m_LastTransformParameters = ParametersType(1);
42 
43  m_InitialTransformParameters.Fill( 0.0f );
44  m_LastTransformParameters.Fill( 0.0f );
45 
46 
47  TransformOutputPointer transformDecorator =
48  static_cast< TransformOutputType * >(
49  this->MakeOutput(0).GetPointer() );
50 
51  this->ProcessObject::SetNthOutput( 0, transformDecorator.GetPointer() );
52 }
53 
54 
56 template < typename TFixedImage, typename TMovingSpatialObject >
57 void
60 {
61 
62  if( !m_FixedImage )
63  {
64  itkExceptionMacro(<<"FixedImage is not present");
65  }
66 
67  if( !m_MovingSpatialObject )
68  {
69  itkExceptionMacro(<<"MovingSpatialObject is not present");
70  }
71 
72  if ( !m_Metric )
73  {
74  itkExceptionMacro(<<"Metric is not present" );
75  }
76 
77  if ( !m_Optimizer )
78  {
79  itkExceptionMacro(<<"Optimizer is not present" );
80  }
81 
82  if( !m_Transform )
83  {
84  itkExceptionMacro(<<"Transform is not present");
85  }
86 
87  if( !m_Interpolator )
88  {
89  itkExceptionMacro(<<"Interpolator is not present");
90  }
91 
92  m_Interpolator->SetInputImage( m_FixedImage );
93 
94  // Setup the metric
95  m_Metric->SetFixedImage( m_FixedImage );
96  m_Metric->SetMovingSpatialObject( m_MovingSpatialObject );
97  m_Metric->SetTransform( m_Transform );
98  m_Metric->SetInterpolator( m_Interpolator );
99  m_Metric->Initialize();
100 
101  // Setup the optimizer
102  m_Optimizer->SetCostFunction( m_Metric );
103 
104  // Validate initial transform parameters
105  if ( m_InitialTransformParameters.Size() != m_Metric->GetNumberOfParameters() )
106  {
107  itkWarningMacro( << " WARNING : Size mismatch between initial parameter and transform" );
108  itkWarningMacro( << "Resizing m_InitialTransformParameters to " << m_Transform->GetNumberOfParameters() );
109  m_InitialTransformParameters.set_size(m_Transform->GetNumberOfParameters());
110  m_InitialTransformParameters.Fill( 0.0f );
111  }
112 
113  if ( m_LastTransformParameters.Size() != m_Transform->GetNumberOfParameters() )
114  {
115  m_LastTransformParameters.set_size(m_Transform->GetNumberOfParameters());
116  m_LastTransformParameters.Fill( 0.0f );
117  }
118 
119  m_Optimizer->SetInitialPosition( m_InitialTransformParameters );
120 
121  //
122  // Connect the transform to the Decorator.
123  //
124  TransformOutputType * transformOutput =
125  static_cast< TransformOutputType * >( this->ProcessObject::GetOutput(0) );
126 
127  transformOutput->Set( m_Transform.GetPointer() );
128 }
129 
130 
132 template < typename TFixedImage, typename TMovingSpatialObject >
133 void
136 {
137  // StartRegistration is an old API from before
138  // the RegistrationMethod was a subclass of ProcessObject.
139  // Historically, one could call StartRegistration() instead of
140  // calling Update(). However, when called directly by the user, the
141  // inputs to the RegistrationMethod may not be up to date. This
142  // may cause an unexpected behavior.
143  //
144  // Since we cannot eliminate StartRegistration for backward
145  // compability reasons, we check whether StartRegistration was
146  // called directly or whether Update() (which in turn called
147  // StartRegistration()).
148  if (!m_Updating)
149  {
150  this->Update();
151  }
152  else
153  {
154  try
155  {
156  // initialize the interconnects between components
157  this->Initialize();
158  }
159  catch( ExceptionObject& err )
160  {
161  // pass exception to caller
162  throw err;
163  }
164 
165  try
166  {
167  // do the optimization
168  m_Optimizer->StartOptimization();
169  }
170  catch( ExceptionObject& err )
171  {
172  // An error has occurred in the optimization.
173  // Update the parameters
174  m_LastTransformParameters = m_Optimizer->GetCurrentPosition();
175  // Pass exception to caller
176  throw err;
177  }
178 
179  // get the results
180  m_LastTransformParameters = m_Optimizer->GetCurrentPosition();
181  }
182 
183 }
184 
185 
187 template < typename TFixedImage, typename TMovingSpatialObject >
188 void
190 ::PrintSelf(std::ostream& os, Indent indent) const
191 {
192  Superclass::PrintSelf( os, indent );
193  os << indent << "Metric: " << m_Metric.GetPointer() << std::endl;
194  os << indent << "Optimizer: " << m_Optimizer.GetPointer() << std::endl;
195  os << indent << "Transform: " << m_Transform.GetPointer() << std::endl;
196  os << indent << "Interpolator: " << m_Interpolator.GetPointer() << std::endl;
197  os << indent << "Fixed Image: " << m_FixedImage.GetPointer() << std::endl;
198  os << indent << "Moving SpatialObject: " << m_MovingSpatialObject.GetPointer() << std::endl;
199  os << indent << "Initial Transform Parameters: " << m_InitialTransformParameters << std::endl;
200  os << indent << "Last Transform Parameters: " << m_LastTransformParameters << std::endl;
201 }
202 
203 
204 /*
205  * Generate Data
206  */
207 template < typename TFixedImage, typename TMovingSpatialObject >
208 void
211 {
212  this->StartRegistration();
213 }
214 
218 template < typename TFixedImage, typename TMovingSpatialObject >
221 ::GetOutput() const
222 {
223  return static_cast< const TransformOutputType * >( this->ProcessObject::GetOutput(0) );
224 }
225 
226 template < typename TFixedImage, typename TMovingSpatialObject >
229 ::MakeOutput(unsigned int output)
230 {
231  switch (output)
232  {
233  case 0:
234  return static_cast<DataObject*>(TransformOutputType::New().GetPointer());
235  break;
236  default:
237  itkExceptionMacro("MakeOutput request for an output number larger than the expected number of outputs");
238  return 0;
239  }
240 }
241 
242 
246 template < typename TFixedImage, typename TMovingSpatialObject >
247 unsigned long
249 ::GetMTime() const
250 {
251  unsigned long mtime = Superclass::GetMTime();
252  unsigned long m;
253 
254 
255  // Some of the following should be removed once ivars are put in the
256  // input and output lists
257 
258  if (m_Transform)
259  {
260  m = m_Transform->GetMTime();
261  mtime = (m > mtime ? m : mtime);
262  }
263 
264  if (m_Interpolator)
265  {
266  m = m_Interpolator->GetMTime();
267  mtime = (m > mtime ? m : mtime);
268  }
269 
270  if (m_Metric)
271  {
272  m = m_Metric->GetMTime();
273  mtime = (m > mtime ? m : mtime);
274  }
275 
276  if (m_Optimizer)
277  {
278  m = m_Optimizer->GetMTime();
279  mtime = (m > mtime ? m : mtime);
280  }
281 
282  if (m_FixedImage)
283  {
284  m = m_FixedImage->GetMTime();
285  mtime = (m > mtime ? m : mtime);
286  }
287 
288  if (m_MovingSpatialObject)
289  {
290  m = m_MovingSpatialObject->GetMTime();
291  mtime = (m > mtime ? m : mtime);
292  }
293 
294  return mtime;
295 
296 }
297 
298 
299 } // end namespace itk
300 
301 
302 #endif

Generated at Sat Feb 2 2013 23:45:25 for Orfeo Toolbox with doxygen 1.8.1.1