ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMultiphaseFiniteDifferenceImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMultiphaseFiniteDifferenceImageFilter_h
00019 #define __itkMultiphaseFiniteDifferenceImageFilter_h
00020 
00021 #include "itkInPlaceImageFilter.h"
00022 #include "itkFiniteDifferenceFunction.h"
00023 #include "vnl/vnl_vector.h"
00024 #include "itkImageRegionIterator.h"
00025 
00026 #include "itkListSample.h"
00027 #include "itkKdTreeGenerator.h"
00028 
00029 namespace itk
00030 {
00160 template< class TInputImage,
00161           class TFeatureImage,
00162           class TOutputImage,
00163           class TFiniteDifferenceFunction = FiniteDifferenceFunction< TOutputImage >,
00164           typename TIdCell = unsigned int >
00165 class ITK_EXPORT MultiphaseFiniteDifferenceImageFilter:
00166   public InPlaceImageFilter< TFeatureImage, TOutputImage >
00167 {
00168 public:
00169 
00171   typedef MultiphaseFiniteDifferenceImageFilter             Self;
00172   typedef InPlaceImageFilter< TFeatureImage, TOutputImage > Superclass;
00173   typedef SmartPointer< Self >                              Pointer;
00174   typedef SmartPointer< const Self >                        ConstPointer;
00175 
00177   itkTypeMacro(MultiphaseFiniteDifferenceImageFilter, InPlaceImageFilter);
00178 
00180   itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
00181 
00183   typedef TInputImage                               InputImageType;
00184   typedef typename InputImageType::Pointer          InputImagePointer;
00185   typedef typename InputImageType::PointType        InputPointType;
00186   typedef typename InputPointType::CoordRepType     InputCoordRepType;
00187   typedef typename InputImageType::IndexType        InputIndexType;
00188   typedef typename InputIndexType::IndexValueType   InputIndexValueType;
00189   typedef typename InputImageType::SizeType         InputSizeType;
00190   typedef typename InputSizeType::SizeValueType     InputSizeValueType;
00191   typedef typename InputImageType::RegionType       InputRegionType;
00192   typedef typename InputImageType::PixelType        InputPixelType;
00193   typedef typename InputImageType::SpacingType      InputSpacingType;
00194   typedef typename InputImageType::OffsetValueType  InputOffsetValueType;
00195 
00196   typedef TFeatureImage                          FeatureImageType;
00197   typedef typename FeatureImageType::Pointer     FeatureImagePointer;
00198   typedef typename FeatureImageType::RegionType  FeatureRegionType;
00199   typedef typename FeatureImageType::SizeType    FeatureSizeType;
00200   typedef typename FeatureImageType::SpacingType FeatureSpacingType;
00201   typedef typename FeatureImageType::PointType   FeaturePointType;
00202   typedef typename FeatureImageType::PixelType   FeaturePixelType;
00203 
00204   typedef TOutputImage                             OutputImageType;
00205   typedef typename OutputImageType::Pointer        OutputImagePointer;
00206   typedef typename OutputImageType::PixelType      OutputPixelType;
00207   typedef typename OutputImageType::RegionType     OutputRegionType;
00208   typedef typename OutputImageType::SizeType       OutputSizeType;
00209   typedef typename OutputImageType::SizeValueType  OutputSizeValueType;
00210   typedef typename OutputImageType::IndexType      OutputIndexType;
00211   typedef typename OutputImageType::IndexValueType OutputIndexValueType;
00212 
00213   typedef TIdCell                   IdCellType;
00214   typedef std::vector< IdCellType > VectorIdCellType;
00215 
00219   typedef TFiniteDifferenceFunction                           FiniteDifferenceFunctionType;
00220   typedef typename FiniteDifferenceFunctionType::Pointer      FiniteDifferenceFunctionPointer;
00221   typedef typename FiniteDifferenceFunctionType::TimeStepType TimeStepType;
00222   typedef typename std::vector< TimeStepType >                TimeStepVectorType;
00223   typedef typename FiniteDifferenceFunctionType::RadiusType   RadiusType;
00224 
00225   typedef Vector< float, itkGetStaticConstMacro(ImageDimension) >
00226   CentroidVectorType;
00227   typedef Statistics::ListSample< CentroidVectorType > SampleType;
00228   typedef Statistics::KdTreeGenerator< SampleType >    KdTreeGeneratorType;
00229   typedef typename KdTreeGeneratorType::Pointer        KdTreeGeneratorPointer;
00230   typedef typename KdTreeGeneratorType::KdTreeType     KdTreeType;
00231   typedef typename KdTreeType::Pointer                 KdTreePointer;
00232 
00237   virtual const FiniteDifferenceFunctionPointer GetDifferenceFunction(
00238     const IdCellType & functionIndex) const
00239   {
00240     if ( functionIndex < m_FunctionCount )
00241       {
00242       return ( this->m_DifferenceFunctions[functionIndex] );
00243       }
00244     else
00245       {
00246       return 0;
00247       }
00248   }
00250 
00255   virtual void SetDifferenceFunction(const IdCellType & functionIndex,
00256                                      FiniteDifferenceFunctionPointer function)
00257   {
00258     if ( functionIndex < m_FunctionCount )
00259       {
00260       this->m_DifferenceFunctions[functionIndex] = function;
00261       }
00262   }
00264 
00266   itkSetMacro(NumberOfIterations, unsigned int);
00267   itkGetConstReferenceMacro(NumberOfIterations, unsigned int);
00269 
00272   itkSetMacro(UseImageSpacing, bool);
00273   itkBooleanMacro(UseImageSpacing);
00274   itkGetConstReferenceMacro(UseImageSpacing, bool);
00276 
00279   itkSetMacro(MaximumRMSError, double);
00280   itkGetConstReferenceMacro(MaximumRMSError, double);
00282 
00285   itkSetMacro(RMSChange, double);
00286   itkGetConstReferenceMacro(RMSChange, double);
00288 
00290   itkSetMacro(InitializedState, bool);
00291   itkGetConstReferenceMacro(InitializedState, bool);
00292   itkBooleanMacro(InitializedState);
00294 
00297   itkSetMacro(ManualReinitialization, bool);
00298   itkGetConstReferenceMacro(ManualReinitialization, bool);
00299   itkBooleanMacro(ManualReinitialization);
00301 
00303   itkSetMacro(ElapsedIterations, unsigned int);
00304 
00306   itkGetConstReferenceMacro(ElapsedIterations, unsigned int);
00307 
00308   void SetLevelSet(const IdCellType & i, const InputImageType *levelSet)
00309   {
00310     m_LevelSet[i] = InputImageType::New();
00311     m_LevelSet[i]->SetRequestedRegion( levelSet->GetRequestedRegion() );
00312     m_LevelSet[i]->SetBufferedRegion( levelSet->GetBufferedRegion() );
00313     m_LevelSet[i]->SetLargestPossibleRegion( levelSet->GetLargestPossibleRegion() );
00314     m_LevelSet[i]->Allocate();
00315     m_LevelSet[i]->CopyInformation(levelSet);
00316 
00317     ImageRegionConstIterator< InputImageType > in ( levelSet, levelSet->GetBufferedRegion() );
00318     ImageRegionIterator< InputImageType >      cp ( m_LevelSet[i], levelSet->GetBufferedRegion() );
00319 
00320     in.GoToBegin();
00321     cp.GoToBegin();
00322 
00323     while ( !in.IsAtEnd() )
00324       {
00325       cp.Set( in.Get() );
00326       ++in;
00327       ++cp;
00328       }
00329   }
00330 
00331   InputImagePointer GetLevelSet(const IdCellType & i)
00332   {
00333     if ( i >= m_FunctionCount )
00334       {
00335       itkExceptionMacro("Request for level set #" << i
00336                                                   << " but there are only " << m_FunctionCount);
00337       }
00338     else
00339       {
00340       return m_LevelSet[i];
00341       }
00342   }
00343 
00344   void SetLookup(VectorIdCellType lookup)
00345   {
00346     this->m_Lookup = lookup;
00347   }
00348 
00349   void SetKdTree(KdTreeType *kdtree)
00350   {
00351     this->m_KdTree = kdtree;
00352   }
00353 
00354   void SetFunctionCount(const IdCellType & n)
00355   {
00356     m_FunctionCount = n;
00357 
00358     m_DifferenceFunctions.resize(m_FunctionCount, 0);
00359 
00360     RadiusType radius;
00361     radius.Fill(1);
00362 
00363     for ( unsigned int i = 0; i < this->m_FunctionCount; i++ )
00364       {
00365       this->m_DifferenceFunctions[i] = FiniteDifferenceFunctionType::New();
00366       this->m_DifferenceFunctions[i]->Initialize(radius);
00367       }
00368 
00369     // Initialize the images
00370     m_LevelSet.resize(m_FunctionCount, 0);
00371 
00372     // Initialize the lookup table
00373     this->m_Lookup.resize(m_FunctionCount);
00374 
00375     IdCellType k = 1;
00376 
00377     typedef typename std::vector< IdCellType >::iterator VectorIteratorType;
00378 
00379     VectorIteratorType it = this->m_Lookup.begin();
00380 
00381     while ( it != this->m_Lookup.end() )
00382       {
00383       *it = k;
00384       ++it;
00385       ++k;
00386       }
00387   }
00388 
00389 protected:
00390   MultiphaseFiniteDifferenceImageFilter()
00391   {
00392     this->m_KdTree = 0;
00393     this->m_ElapsedIterations = 0;
00394     this->m_MaximumRMSError = vnl_math::eps;
00395     this->m_RMSChange = NumericTraits< double >::max();
00396     this->m_UseImageSpacing = true;
00397     this->m_ManualReinitialization = false;
00398     this->m_InitializedState = false;
00399     this->m_NumberOfIterations = NumericTraits< unsigned int >::max();
00400     this->m_FunctionCount = 0;
00401     this->InPlaceOff();
00402   }
00403 
00404   ~MultiphaseFiniteDifferenceImageFilter(){}
00405 
00406   IdCellType                       m_FunctionCount;
00407   std::vector< InputImagePointer > m_LevelSet;
00408   VectorIdCellType                 m_Lookup;
00409   KdTreePointer                    m_KdTree;
00410 
00411   unsigned int m_ElapsedIterations;
00412   double       m_MaximumRMSError;
00413   double       m_RMSChange;
00414   unsigned int m_NumberOfIterations;
00415 
00417   std::vector< FiniteDifferenceFunctionPointer > m_DifferenceFunctions;
00418 
00421   bool m_UseImageSpacing;
00422 
00423   void PrintSelf(std::ostream & os, Indent indent) const;
00424 
00426   virtual void AllocateUpdateBuffer() = 0;
00427 
00431   virtual void ApplyUpdate(TimeStepType dt) = 0;
00432 
00438   virtual TimeStepType CalculateChange() = 0;
00439 
00443   virtual void CopyInputToOutput() = 0;
00444 
00448   virtual void GenerateData();
00449 
00461   virtual void GenerateInputRequestedRegion();
00462 
00465   virtual bool Halt();
00466 
00476   virtual bool ThreadedHalt( void *itkNotUsed(threadInfo) )
00477   {
00478     return this->Halt();
00479   }
00480 
00486   virtual void Initialize() {}
00487 
00494   virtual void InitializeIteration()
00495   {
00496     for ( IdCellType i = 0; i < this->m_FunctionCount; i++ )
00497       {
00498       this->m_DifferenceFunctions[i]->InitializeIteration();
00499       }
00500   }
00502 
00515   inline TimeStepType ResolveTimeStep(const TimeStepVectorType & timeStepList,
00516                                       const std::vector< bool > & valid);
00517 
00520   virtual void PostProcessOutput() {}
00521 private:
00522   MultiphaseFiniteDifferenceImageFilter(const Self &);
00523   //purposely not implemented
00524   void operator=(const Self &); //purposely not implemented
00526 
00529   bool m_ManualReinitialization;
00530 
00532   bool m_InitializedState;
00533 };
00534 } // end namespace itk
00535 
00536 #ifndef ITK_MANUAL_INSTANTIATION
00537 #include "itkMultiphaseFiniteDifferenceImageFilter.hxx"
00538 #endif
00539 
00540 #endif
00541