Orfeo Toolbox  3.16
itkMultiphaseFiniteDifferenceImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMultiphaseFiniteDifferenceImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-09-08 20:09:41 $
7  Version: $Revision: 1.8 $
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 
18 #ifndef __itkMultiphaseFiniteDifferenceImageFilter_txx
19 #define __itkMultiphaseFiniteDifferenceImageFilter_txx
20 
22 #include "itkImageRegionIterator.h"
24 #include "itkExceptionObject.h"
25 #include "itkEventObject.h"
26 
27 namespace itk {
28 
29 template < class TInputImage,
30  class TFeatureImage,
31  class TOutputImage,
32  class TFiniteDifferenceFunction,
33  typename TIdCell >
34 void
35 MultiphaseFiniteDifferenceImageFilter< TInputImage,
36  TFeatureImage,
37  TOutputImage,
38  TFiniteDifferenceFunction,
39  TIdCell >
40 ::GenerateData()
41 {
42  if( ! this->m_FunctionCount )
43  {
44  itkExceptionMacro("Number of level set functions not specified. "
45  << "Please set using SetFunctionCount()" );
46  }
47 
48  if( ! this->m_InitializedState )
49  {
50  // Set the coefficients for the deriviatives
51  double coeffs[ImageDimension];
52  unsigned int i;
53  if (m_UseImageSpacing)
54  {
55  for (i = 0; i < ImageDimension; i++)
56  {
57  coeffs[i] = 1.0 / m_LevelSet[0]->GetSpacing()[i];
58  }
59  }
60  else
61  {
62  for (i = 0; i < ImageDimension; i++)
63  {
64  coeffs[i] = 1.0;
65  }
66  }
67 
68  for( IdCellType id = 0; id < this->m_FunctionCount; id++ )
69  {
70  this->m_DifferenceFunctions[id]->SetScaleCoefficients(coeffs);
71  }
72 
73  // Allocate the output image -- inherited method
74  this->AllocateOutputs();
75 
76  // Copy the input image to the output image. Algorithms will operate
77  // directly on the output image and the update buffer.
78  this->CopyInputToOutput();
79 
80  // Perform any other necessary pre-iteration initialization.
81  this->Initialize();
82 
83  // Allocate the internal update buffer. This takes place entirely within
84  // the subclass, since this class cannot define an update buffer type.
85  this->AllocateUpdateBuffer();
86 
87  this->SetInitializedState( true );
88  }
89 
90  // Iterative algorithm
91  TimeStepType dt;
92 
93  // An optional method for precalculating global values, or setting
94  // up for the next iteration
95  this->InitializeIteration();
96  this->m_RMSChange = NumericTraits<double>::max();
97 
98  while ( ! this->Halt() )
99  {
100  dt = this->CalculateChange();
101 
102  this->ApplyUpdate( dt );
103 
104  this->m_ElapsedIterations++;
105 
106  // Invoke the iteration event.
107  this->InvokeEvent( IterationEvent() );
108 
109  if( this->GetAbortGenerateData() )
110  {
111  this->InvokeEvent( IterationEvent() );
112  this->ResetPipeline();
113  throw ProcessAborted(__FILE__,__LINE__);
114  }
115 
116  this->InitializeIteration();
117  }
118 
119  // Reset the state once execution is completed
120  if( !this->m_ManualReinitialization )
121  {
122  this->SetInitializedState(true);
123  }
124 
125  // Any further processing of the solution can be done here.
126  this->PostProcessOutput();
127 }
128 
132 template < class TInputImage,
133  class TFeatureImage,
134  class TOutputImage,
135  class TFiniteDifferenceFunction,
136  typename TIdCell >
137 void
139  TFeatureImage,
140  TOutputImage,
141  TFiniteDifferenceFunction,
142  TIdCell >
143 ::GenerateInputRequestedRegion()
144 {
145  // call the superclass' implementation of this method
146  // copy the output requested region to the input requested region
147  Superclass::GenerateInputRequestedRegion();
148 
149  // get pointers to the input
150  FeatureImagePointer inputPtr =
151  const_cast< FeatureImageType* >( this->GetInput(0) );
152 
153  if ( inputPtr.IsNull() )
154  {
155  return;
156  }
157 
158  if( this->m_DifferenceFunctions[0].IsNull() )
159  {
160  return;
161  }
162 
163  // Get the size of the neighborhood on which we are going to operate. This
164  // radius is supplied by the difference function we are using.
165  RadiusType radius = this->m_DifferenceFunctions[0]->GetRadius();
166 
167  // Try to set up a buffered region that will accommodate our
168  // neighborhood operations. This may not be possible and we
169  // need to be careful not to request a region outside the largest
170  // possible region, because the pipeline will give us whatever we
171  // ask for.
172 
173  // get a copy of the input requested region (should equal the output
174  // requested region)
175  FeatureRegionType inputRequestedRegion;
176  inputRequestedRegion = inputPtr->GetRequestedRegion();
177 
178  // pad the input requested region by the operator radius
179  inputRequestedRegion.PadByRadius( radius );
180 
181  // crop the input requested region at the input's largest possible region
182  if ( inputRequestedRegion.Crop( inputPtr->GetLargestPossibleRegion() ) )
183  {
184  inputPtr->SetRequestedRegion( inputRequestedRegion );
185  return;
186  }
187  else
188  {
189  // Couldn't crop the region (requested region is outside the largest
190  // possible region). Throw an exception.
191 
192  // store what we tried to request (prior to trying to crop)
193  inputPtr->SetRequestedRegion( inputRequestedRegion );
194 
195  // build an exception
196  InvalidRequestedRegionError e(__FILE__, __LINE__);
197  e.SetLocation(ITK_LOCATION);
198  e.SetDescription("Requested region is (at least partially) outside the "
199  "largest possible region.");
200  e.SetDataObject(inputPtr);
201  throw e;
202  }
203 }
204 
205 template < class TInputImage,
206  class TFeatureImage,
207  class TOutputImage,
208  class TFiniteDifferenceFunction,
209  typename TIdCell >
210 typename MultiphaseFiniteDifferenceImageFilter< TInputImage, TFeatureImage,
211  TOutputImage, TFiniteDifferenceFunction, TIdCell >::TimeStepType
213  TFeatureImage,
214  TOutputImage,
215  TFiniteDifferenceFunction,
216  TIdCell >
217 ::ResolveTimeStep( const TimeStepVectorType &timeStepList,
218  const std::vector< bool >& valid )
219 {
220  TimeStepType oMin = NumericTraits<TimeStepType>::Zero;
221  size_t size = timeStepList.size();
222 
223  if( size == valid.size() )
224  {
225  bool flag = false;
226  size_t k = 0;
227  size_t i;
228 
229  for( i = 0; i < size; ++i)
230  {
231  if (valid[i])
232  {
233  oMin = timeStepList[i];
234  k = i;
235  flag = true;
236  break;
237  }
238  }
239 
240  if( !flag )
241  {
242  itkExceptionMacro("No Values");
243  }
244 
245  // find minimum value
246  for ( i = k; i < size; ++i)
247  {
248  if ( valid[i] && (timeStepList[i] < oMin) )
249  {
250  oMin = timeStepList[i];
251  }
252  }
253  }
254 
255  return oMin;
256 }
257 
258 template < class TInputImage,
259  class TFeatureImage,
260  class TOutputImage,
261  class TFiniteDifferenceFunction,
262  typename TIdCell >
263 bool
265  TFeatureImage,
266  TOutputImage,
267  TFiniteDifferenceFunction,
268  TIdCell >
269 ::Halt()
270 {
271  float progress = 0.;
272 
273  if( this->m_NumberOfIterations != 0 )
274  {
275  progress = static_cast<float>( this->GetElapsedIterations() )
276  / static_cast<float>( this->m_NumberOfIterations );
277  }
278  this->UpdateProgress( progress );
279 
280  return ( (this->GetElapsedIterations() >= this->m_NumberOfIterations) ||
281  ( this->GetMaximumRMSError() >= this->m_RMSChange ) );
282 }
283 
284 
285 template < class TInputImage,
286  class TFeatureImage,
287  class TOutputImage,
288  class TFiniteDifferenceFunction,
289  typename TIdCell >
290 void
292  TFeatureImage,
293  TOutputImage,
294  TFiniteDifferenceFunction,
295  TIdCell >
296 ::PrintSelf(std::ostream& os, Indent indent) const
297 {
298  Superclass::PrintSelf(os, indent);
299 
300  os << indent << "ElapsedIterations: " << this->m_ElapsedIterations << std::endl;
301  os << indent << "UseImageSpacing: " << (m_UseImageSpacing ? "On" : "Off") << std::endl;
302  os << indent << "State: " << this->m_InitializedState << std::endl;
303  os << indent << "MaximumRMSError: " << m_MaximumRMSError << std::endl;
304  os << indent << "NumberOfIterations: " << this->m_NumberOfIterations << std::endl;
305  os << indent << "ManualReinitialization: " << this->m_ManualReinitialization << std::endl;
306  os << indent << "RMSChange: " << m_RMSChange << std::endl;
307  os << std::endl;
308 
309  if ( this->m_FunctionCount )
310  {
311  if( this->m_DifferenceFunctions[0] )
312  {
313  os << indent << "DifferenceFunction: " << std::endl;
314  for( IdCellType i = 0; i < this->m_FunctionCount; ++i )
315  {
316  this->m_DifferenceFunctions[i]->Print(os,indent.GetNextIndent());
317  }
318  }
319  }
320  else
321  {
322  os << indent << "DifferenceFunction: " << "(None)" << std::endl;
323  }
324 
325  os << std::endl;
326 }
327 
328 
329 }// end namespace itk
330 
331 #endif

Generated at Sat Feb 2 2013 23:54:29 for Orfeo Toolbox with doxygen 1.8.1.1