ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkMultiphaseFiniteDifferenceImageFilter.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 __itkMultiphaseFiniteDifferenceImageFilter_h
19 #define __itkMultiphaseFiniteDifferenceImageFilter_h
20 
21 #include "itkInPlaceImageFilter.h"
23 #include "vnl/vnl_vector.h"
24 #include "itkImageRegionIterator.h"
25 
26 #include "itkListSample.h"
27 #include "itkKdTreeGenerator.h"
28 
29 namespace itk
30 {
160 template< class TInputImage,
161  class TFeatureImage,
162  class TOutputImage,
163  class TFiniteDifferenceFunction = FiniteDifferenceFunction< TOutputImage >,
164  typename TIdCell = unsigned int >
166  public InPlaceImageFilter< TFeatureImage, TOutputImage >
167 {
168 public:
169 
175 
178 
180  itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
181 
183  typedef TInputImage InputImageType;
184  typedef typename InputImageType::Pointer InputImagePointer;
185  typedef typename InputImageType::PointType InputPointType;
186  typedef typename InputPointType::CoordRepType InputCoordRepType;
187  typedef typename InputImageType::IndexType InputIndexType;
189  typedef typename InputImageType::SizeType InputSizeType;
191  typedef typename InputImageType::RegionType InputRegionType;
192  typedef typename InputImageType::PixelType InputPixelType;
193  typedef typename InputImageType::SpacingType InputSpacingType;
195 
196  typedef TFeatureImage FeatureImageType;
197  typedef typename FeatureImageType::Pointer FeatureImagePointer;
198  typedef typename FeatureImageType::RegionType FeatureRegionType;
199  typedef typename FeatureImageType::SizeType FeatureSizeType;
200  typedef typename FeatureImageType::SpacingType FeatureSpacingType;
201  typedef typename FeatureImageType::PointType FeaturePointType;
202  typedef typename FeatureImageType::PixelType FeaturePixelType;
203 
204  typedef TOutputImage OutputImageType;
205  typedef typename OutputImageType::Pointer OutputImagePointer;
206  typedef typename OutputImageType::PixelType OutputPixelType;
207  typedef typename OutputImageType::RegionType OutputRegionType;
208  typedef typename OutputImageType::SizeType OutputSizeType;
210  typedef typename OutputImageType::IndexType OutputIndexType;
212 
213  typedef TIdCell IdCellType;
214  typedef std::vector< IdCellType > VectorIdCellType;
215 
219  typedef TFiniteDifferenceFunction FiniteDifferenceFunctionType;
220  typedef typename FiniteDifferenceFunctionType::Pointer FiniteDifferenceFunctionPointer;
221  typedef typename FiniteDifferenceFunctionType::TimeStepType TimeStepType;
222  typedef typename std::vector< TimeStepType > TimeStepVectorType;
223  typedef typename FiniteDifferenceFunctionType::RadiusType RadiusType;
224 
232 
237  virtual const FiniteDifferenceFunctionPointer GetDifferenceFunction(
238  const IdCellType & functionIndex) const
239  {
240  if ( functionIndex < m_FunctionCount )
241  {
242  return ( this->m_DifferenceFunctions[functionIndex] );
243  }
244  else
245  {
246  return 0;
247  }
248  }
250 
255  virtual void SetDifferenceFunction(const IdCellType & functionIndex,
257  {
258  if ( functionIndex < m_FunctionCount )
259  {
260  this->m_DifferenceFunctions[functionIndex] = function;
261  }
262  }
264 
266  itkSetMacro(NumberOfIterations, unsigned int);
267  itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
269 
272  itkSetMacro(UseImageSpacing, bool);
273  itkBooleanMacro(UseImageSpacing);
274  itkGetConstReferenceMacro(UseImageSpacing, bool);
276 
279  itkSetMacro(MaximumRMSError, double);
280  itkGetConstReferenceMacro(MaximumRMSError, double);
282 
285  itkSetMacro(RMSChange, double);
286  itkGetConstReferenceMacro(RMSChange, double);
288 
290  itkSetMacro(InitializedState, bool);
291  itkGetConstReferenceMacro(InitializedState, bool);
292  itkBooleanMacro(InitializedState);
294 
297  itkSetMacro(ManualReinitialization, bool);
298  itkGetConstReferenceMacro(ManualReinitialization, bool);
299  itkBooleanMacro(ManualReinitialization);
301 
303  itkSetMacro(ElapsedIterations, unsigned int);
304 
306  itkGetConstReferenceMacro(ElapsedIterations, unsigned int);
307 
308  void SetLevelSet(const IdCellType & i, const InputImageType *levelSet)
309  {
310  m_LevelSet[i] = InputImageType::New();
311  m_LevelSet[i]->SetRequestedRegion( levelSet->GetRequestedRegion() );
312  m_LevelSet[i]->SetBufferedRegion( levelSet->GetBufferedRegion() );
313  m_LevelSet[i]->SetLargestPossibleRegion( levelSet->GetLargestPossibleRegion() );
314  m_LevelSet[i]->Allocate();
315  m_LevelSet[i]->CopyInformation(levelSet);
316 
317  ImageRegionConstIterator< InputImageType > in ( levelSet, levelSet->GetBufferedRegion() );
318  ImageRegionIterator< InputImageType > cp ( m_LevelSet[i], levelSet->GetBufferedRegion() );
319 
320  in.GoToBegin();
321  cp.GoToBegin();
322 
323  while ( !in.IsAtEnd() )
324  {
325  cp.Set( in.Get() );
326  ++in;
327  ++cp;
328  }
329  }
330 
331  InputImagePointer GetLevelSet(const IdCellType & i)
332  {
333  if ( i >= m_FunctionCount )
334  {
335  itkExceptionMacro("Request for level set #" << i
336  << " but there are only " << m_FunctionCount);
337  }
338  else
339  {
340  return m_LevelSet[i];
341  }
342  }
343 
344  void SetLookup(VectorIdCellType lookup)
345  {
346  this->m_Lookup = lookup;
347  }
348 
349  void SetKdTree(KdTreeType *kdtree)
350  {
351  this->m_KdTree = kdtree;
352  }
353 
354  void SetFunctionCount(const IdCellType & n)
355  {
356  m_FunctionCount = n;
357 
358  m_DifferenceFunctions.resize(m_FunctionCount, 0);
359 
360  RadiusType radius;
361  radius.Fill(1);
362 
363  for ( unsigned int i = 0; i < this->m_FunctionCount; i++ )
364  {
365  this->m_DifferenceFunctions[i] = FiniteDifferenceFunctionType::New();
366  this->m_DifferenceFunctions[i]->Initialize(radius);
367  }
368 
369  // Initialize the images
370  m_LevelSet.resize(m_FunctionCount, 0);
371 
372  // Initialize the lookup table
373  this->m_Lookup.resize(m_FunctionCount);
374 
375  IdCellType k = 1;
376 
377  typedef typename std::vector< IdCellType >::iterator VectorIteratorType;
378 
379  VectorIteratorType it = this->m_Lookup.begin();
380 
381  while ( it != this->m_Lookup.end() )
382  {
383  *it = k;
384  ++it;
385  ++k;
386  }
387  }
388 
389 protected:
391  {
392  this->m_KdTree = 0;
393  this->m_ElapsedIterations = 0;
394  this->m_MaximumRMSError = vnl_math::eps;
395  this->m_RMSChange = NumericTraits< double >::max();
396  this->m_UseImageSpacing = true;
397  this->m_ManualReinitialization = false;
398  this->m_InitializedState = false;
399  this->m_NumberOfIterations = NumericTraits< unsigned int >::max();
400  this->m_FunctionCount = 0;
401  this->InPlaceOff();
402  }
403 
405 
407  std::vector< InputImagePointer > m_LevelSet;
410 
411  unsigned int m_ElapsedIterations;
413  double m_RMSChange;
414  unsigned int m_NumberOfIterations;
415 
417  std::vector< FiniteDifferenceFunctionPointer > m_DifferenceFunctions;
418 
422 
423  void PrintSelf(std::ostream & os, Indent indent) const;
424 
426  virtual void AllocateUpdateBuffer() = 0;
427 
431  virtual void ApplyUpdate(TimeStepType dt) = 0;
432 
438  virtual TimeStepType CalculateChange() = 0;
439 
443  virtual void CopyInputToOutput() = 0;
444 
448  virtual void GenerateData();
449 
461  virtual void GenerateInputRequestedRegion();
462 
465  virtual bool Halt();
466 
476  virtual bool ThreadedHalt( void *itkNotUsed(threadInfo) )
477  {
478  return this->Halt();
479  }
480 
486  virtual void Initialize() {}
487 
494  virtual void InitializeIteration()
495  {
496  for ( IdCellType i = 0; i < this->m_FunctionCount; i++ )
497  {
498  this->m_DifferenceFunctions[i]->InitializeIteration();
499  }
500  }
502 
515  inline TimeStepType ResolveTimeStep(const TimeStepVectorType & timeStepList,
516  const std::vector< bool > & valid);
517 
520  virtual void PostProcessOutput() {}
521 
522 private:
524  //purposely not implemented
525  void operator=(const Self &); //purposely not implemented
526 
530 
533 };
534 } // end namespace itk
535 
536 #ifndef ITK_MANUAL_INSTANTIATION
537 #include "itkMultiphaseFiniteDifferenceImageFilter.hxx"
538 #endif
539 
540 #endif
541