ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMultiphaseSparseFiniteDifferenceImageFilter.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 __itkMultiphaseSparseFiniteDifferenceImageFilter_h
00019 #define __itkMultiphaseSparseFiniteDifferenceImageFilter_h
00020 
00021 #include "itkMultiphaseFiniteDifferenceImageFilter.h"
00022 #include "itkZeroCrossingImageFilter.h"
00023 #include "itkImageRegionConstIterator.h"
00024 #include "itkShiftScaleImageFilter.h"
00025 #include "itkNeighborhoodAlgorithm.h"
00026 #include "itkSparseFieldLevelSetImageFilter.h"
00027 
00028 #include <vector>
00029 
00030 namespace itk
00031 {
00176 template< class TInputImage, class TFeatureImage, class TOutputImage, class TFunction,
00177           typename TIdCell = unsigned int >
00178 class ITK_EXPORT MultiphaseSparseFiniteDifferenceImageFilter:
00179   public MultiphaseFiniteDifferenceImageFilter< TInputImage,
00180                                                 TFeatureImage, TOutputImage, TFunction, TIdCell >
00181 {
00182 public:
00183 
00185   typedef MultiphaseSparseFiniteDifferenceImageFilter Self;
00186   typedef MultiphaseFiniteDifferenceImageFilter< TInputImage,
00187                                                  TFeatureImage, TOutputImage, TFunction, TIdCell >     Superclass;
00188   typedef SmartPointer< Self >       Pointer;
00189   typedef SmartPointer< const Self > ConstPointer;
00190 
00192   itkNewMacro(Self);
00193 
00195   itkTypeMacro(MultiphaseSparseFiniteDifferenceImageFilter, MultiphaseFiniteDifferenceImageFilter);
00196 
00197   itkStaticConstMacro(ImageDimension, unsigned int, Superclass::ImageDimension);
00198 
00200   typedef typename Superclass::TimeStepType TimeStepType;
00201 
00203   typedef typename Superclass::InputImageType         InputImageType;
00204   typedef typename Superclass::InputImagePointer      InputImagePointer;
00205   typedef typename Superclass::InputRegionType        InputRegionType;
00206   typedef typename Superclass::InputSizeType          InputSizeType;
00207   typedef typename Superclass::InputSizeValueType     InputSizeValueType;
00208   typedef typename Superclass::InputIndexType         InputIndexType;
00209   typedef typename Superclass::InputIndexValueType    InputIndexValueType;
00210   typedef typename Superclass::InputPixelType         InputPixelType;
00211   typedef typename Superclass::InputPointType         InputPointType;
00212   typedef typename Superclass::InputSpacingType       InputSpacingType;
00213   typedef typename Superclass::InputOffsetValueType   InputOffsetValueType;
00214 
00215   typedef typename Superclass::FeatureImageType    FeatureImageType;
00216   typedef typename Superclass::FeatureSizeType     FeatureSizeType;
00217   typedef typename Superclass::FeatureImagePointer FeatureImagePointer;
00218   typedef typename Superclass::FeatureRegionType   FeatureRegionType;
00219   typedef typename Superclass::FeatureSpacingType  FeatureSpacingType;
00220   typedef typename Superclass::FeaturePointType    FeaturePointType;
00221 
00222   typedef typename Superclass::OutputImageType      OutputImageType;
00223   typedef typename Superclass::OutputImagePointer   OutputImagePointer;
00224   typedef typename Superclass::OutputRegionType     OutputRegionType;
00225   typedef typename Superclass::OutputSizeType       OutputSizeType;
00226   typedef typename Superclass::OutputIndexType      OutputIndexType;
00227   typedef typename Superclass::OutputIndexValueType OutputIndexValueType;
00228   typedef typename Superclass::OutputPixelType      OutputPixelType;
00229 
00230   typedef typename InputImageType::ValueType ValueType;
00231   typedef typename Superclass::IdCellType    IdCellType;
00232 
00233   typedef typename Superclass::FiniteDifferenceFunctionType
00234   FiniteDifferenceFunctionType;
00235   typedef typename Superclass::FiniteDifferenceFunctionPointer
00236   FiniteDifferenceFunctionPointer;
00237   typedef typename FiniteDifferenceFunctionType::FloatOffsetType
00238   FiniteDifferenceFunctionFloatOffsetType;
00239 
00241   typedef SparseFieldLevelSetNode< OutputIndexType > LayerNodeType;
00242 
00244   typedef SparseFieldLayer< LayerNodeType > LayerType;
00245   typedef typename LayerType::Pointer       LayerPointerType;
00246   typedef typename LayerType::Iterator      LayerIterator;
00247   typedef typename LayerType::ConstIterator LayerConstIterator;
00248 
00250   typedef std::vector< LayerPointerType >        LayerListType;
00251   typedef typename LayerListType::iterator       LayerListIterator;
00252   typedef typename LayerListType::const_iterator LayerListConstIterator;
00253 
00255   typedef signed char StatusType;
00256 
00259   typedef Image< StatusType, itkGetStaticConstMacro(ImageDimension) >
00260   StatusImageType;
00261   typedef typename StatusImageType::Pointer StatusImagePointer;
00262 
00263   typedef ZeroCrossingImageFilter< InputImageType, InputImageType >
00264   ZeroCrossingFilterType;
00265   typedef typename ZeroCrossingFilterType::Pointer
00266   ZeroCrossingFilterPointer;
00267 
00268   typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< StatusImageType > BFCType;
00269 
00272   typedef ObjectStore< LayerNodeType >           LayerNodeStorageType;
00273   typedef typename LayerNodeStorageType::Pointer LayerNodeStoragePointer;
00274 
00276   typedef std::vector< ValueType >                  UpdateBufferType;
00277   typedef typename UpdateBufferType::const_iterator UpdateBufferConstIterator;
00278 
00279   typedef SparseFieldCityBlockNeighborList< NeighborhoodIterator< OutputImageType > > NeighborListType;
00280   typedef typename NeighborListType::OffsetType                                       OffsetType;
00281 
00285   itkSetMacro(NumberOfLayers, unsigned int);
00286   itkGetConstMacro(NumberOfLayers, unsigned int);
00288 
00290   itkSetMacro(IsoSurfaceValue, ValueType);
00291   itkGetConstMacro(IsoSurfaceValue, ValueType);
00293 
00298   itkSetMacro(InterpolateSurfaceLocation, bool);
00299   itkGetConstMacro(InterpolateSurfaceLocation, bool);
00301 
00303   void InterpolateSurfaceLocationOn()
00304   { this->SetInterpolateSurfaceLocation(true); }
00305   void InterpolateSurfaceLocationOff()
00306   { this->SetInterpolateSurfaceLocation(false); }
00308 
00309   void SetFunctionCount(const IdCellType & n)
00310   {
00311     this->Superclass::SetFunctionCount(n);
00312 
00313     m_SparseData.resize(this->m_FunctionCount, 0);
00314 
00315     for ( IdCellType i = 0; i < this->m_FunctionCount; i++ )
00316       {
00317       m_SparseData[i] = new SparseDataStruct(i);
00318       }
00319   }
00320 
00321 #ifdef ITK_USE_CONCEPT_CHECKING
00322 
00323   itkConceptMacro( OutputEqualityComparableCheck,
00324                    ( Concept::EqualityComparable< typename TOutputImage::PixelType > ) );
00325   itkConceptMacro( DoubleConvertibleToOutputCheck,
00326                    ( Concept::Convertible< double, typename TOutputImage::PixelType > ) );
00327   itkConceptMacro( OutputOStreamWritableCheck,
00328                    ( Concept::OStreamWritable< typename TOutputImage::PixelType > ) );
00329 
00331 #endif
00332 protected:
00333   MultiphaseSparseFiniteDifferenceImageFilter();
00334   ~MultiphaseSparseFiniteDifferenceImageFilter()
00335   {
00336     while ( !m_SparseData.empty() )
00337       {
00338       if ( m_SparseData.back() )
00339         {
00340         delete m_SparseData.back();
00341         }
00342       m_SparseData.pop_back();
00343       }
00344   }
00346 
00347   virtual void PrintSelf(std::ostream & os, Indent indent) const;
00348 
00349   // This data structure is created for each phase
00350   struct SparseDataStruct {
00351     SparseDataStruct(const IdCellType & index)
00352     {
00353       m_LayerNodeStore = LayerNodeStorageType::New();
00354       m_LayerNodeStore->SetGrowthStrategyToExponential();
00355       m_Index = index;
00356     }
00357 
00362     LayerListType m_Layers;
00363 
00365     StatusImagePointer m_StatusImage;
00366 
00368     LayerNodeStoragePointer m_LayerNodeStore;
00369 
00372     UpdateBufferType m_UpdateBuffer;
00373 
00374     IdCellType m_Index;
00375   };
00376 
00378   NeighborListType m_NeighborList;
00379 
00381   std::vector< ValueType > m_PixelDistance;
00382 
00387   inline virtual ValueType CalculateUpdateValue(
00388     const OutputIndexType & itkNotUsed(idx),
00389     const TimeStepType & dt,
00390     const ValueType & value,
00391     const ValueType & change)
00392   {
00393     return ( value + dt * change );
00394   }
00395 
00399   virtual void PostProcessOutput();
00400 
00405   virtual void InitializeBackgroundPixels();
00406 
00408   void Initialize();
00409 
00414   void CopyInputToOutput();
00415 
00417   void AllocateUpdateBuffer(){}
00418 
00421   void ApplyUpdate(TimeStepType dt);
00422 
00425   TimeStepType CalculateChange();
00426 
00430   void ConstructLayer(SparseDataStruct *sparsePtr, StatusType from, StatusType
00431                       to);
00432 
00437   void ConstructActiveLayer();
00438 
00440   void InitializeActiveLayerValues();
00441 
00444   void InitializeBackgroundConstants();
00445 
00453   void PropagateLayerValues(SparseDataStruct *sparsePtr, StatusType from,
00454                             StatusType to, StatusType promote, int InOrOut);
00455 
00460   void PropagateAllLayerValues();
00461 
00462   void PropagateFunctionLayerValues(unsigned int functionIndex);
00463 
00467   void UpdateActiveLayerValues(TimeStepType dt, LayerType *StatusUpList,
00468                                LayerType *StatusDownList);
00469 
00471   void ProcessStatusList(LayerType *InputList, LayerType *OutputList,
00472                          StatusType ChangeToStatus, StatusType SearchForStatus);
00473 
00475   void ProcessOutsideList(LayerType *OutsideList, StatusType ChangeToStatus);
00476 
00477   void InitializeIteration();
00478 
00479   virtual void UpdatePixel( unsigned int itkNotUsed(functionIndex), unsigned int itkNotUsed(idx),
00480                             NeighborhoodIterator< InputImageType > & itkNotUsed(iterator), ValueType & itkNotUsed(
00481                               newValue),
00482                             bool & itkNotUsed(status) ){}
00483 
00484   itkGetConstMacro(ValueZero, ValueType);
00485   itkGetConstMacro(ValueOne, ValueType);
00486 
00489   static double m_ConstantGradientValue;
00490 
00492   static const ValueType m_ValueOne;
00493 
00495   static const ValueType m_ValueZero;
00496 
00499   static const StatusType m_StatusChanging;
00500 
00503   static const StatusType m_StatusActiveChangingUp;
00504 
00507   static const StatusType m_StatusActiveChangingDown;
00508 
00511   static const StatusType m_StatusBoundaryPixel;
00512 
00515   static const StatusType m_StatusNull;
00516 
00517   std::vector< SparseDataStruct * > m_SparseData;
00518 
00522   unsigned int m_NumberOfLayers;
00523 
00525   ValueType m_IsoSurfaceValue;
00526 
00528   ValueType m_BackgroundValue;
00529 
00534   bool m_InterpolateSurfaceLocation;
00535 private:
00536   MultiphaseSparseFiniteDifferenceImageFilter(const Self &);
00537   void operator=(const Self &);      //purposely not implemented
00539 
00540   unsigned int m_CurrentFunctionIndex;
00541 
00542   double       m_RMSSum;
00543   unsigned int m_RMSCounter;
00544 
00547   bool m_BoundsCheckingActive;
00548 };
00549 } // end namespace itk
00550 
00551 #ifndef ITK_MANUAL_INSTANTIATION
00552 #include "itkMultiphaseSparseFiniteDifferenceImageFilter.hxx"
00553 #endif
00554 
00555 #endif
00556