ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkNarrowBandImageFilterBase.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 __itkNarrowBandImageFilterBase_h
00019 #define __itkNarrowBandImageFilterBase_h
00020 
00021 #include "itkFiniteDifferenceImageFilter.h"
00022 #include "itkMultiThreader.h"
00023 #include "itkNarrowBand.h"
00024 #include "itkBarrier.h"
00025 #include "itkObjectStore.h"
00026 
00027 namespace itk
00028 {
00067 template< class TInputImage, class TOutputImage >
00068 class NarrowBandImageFilterBase:
00069   public FiniteDifferenceImageFilter< TInputImage, TOutputImage >
00070 {
00071 public:
00072 
00074   typedef NarrowBandImageFilterBase                                Self;
00075   typedef FiniteDifferenceImageFilter< TInputImage, TOutputImage > Superclass;
00076   typedef SmartPointer< Self >                                     Pointer;
00077   typedef SmartPointer< const Self >                               ConstPointer;
00078 
00080   itkTypeMacro(NarrowBandImageFilterBase, ImageToImageFilter);
00081 
00083   typedef typename Superclass::InputImageType               InputImageType;
00084   typedef typename Superclass::OutputImageType              OutputImageType;
00085   typedef typename Superclass::FiniteDifferenceFunctionType FiniteDifferenceFunctionType;
00086 
00089   itkStaticConstMacro(ImageDimension, unsigned int, Superclass::ImageDimension);
00090 
00093   typedef typename Superclass::PixelType PixelType;
00094 
00096   typedef typename Superclass::TimeStepType TimeStepType;
00097 
00099   typedef typename OutputImageType::IndexType IndexType;
00100 
00103   typedef typename OutputImageType::ValueType ValueType;
00104 
00106   typedef BandNode< IndexType, PixelType > BandNodeType;
00107 
00109   typedef NarrowBand< BandNodeType >          NarrowBandType;
00110   typedef typename NarrowBandType::Pointer    NarrowBandPointer;
00111   typedef typename NarrowBandType::RegionType RegionType;
00112   typedef typename NarrowBandType::Iterator   NarrowBandIterator;
00113 
00115   itkSetMacro(IsoSurfaceValue, ValueType);
00116   itkGetConstMacro(IsoSurfaceValue, ValueType);
00118 
00120   //  itkGetConstMacro( RMSChange, ValueType);
00121 
00126   void InsertNarrowBandNode(const BandNodeType & node)
00127   {
00128     m_NarrowBand->PushBack(node); // add new node
00129     this->Modified();
00130   }
00132 
00133   void InsertNarrowBandNode(const IndexType & index)
00134   {
00135     BandNodeType tmpnode;
00136 
00137     tmpnode.m_Index = index;
00138     m_NarrowBand->PushBack(tmpnode);
00139     this->Modified();
00140   }
00141 
00142   void InsertNarrowBandNode(const IndexType & index,
00143                             const PixelType & value,
00144                             const signed char & nodestate)
00145   {
00146     BandNodeType tmpnode;
00147 
00148     tmpnode.m_Data = value;
00149     tmpnode.m_Index = index;
00150     tmpnode.m_NodeState = nodestate;
00151 
00152     m_NarrowBand->PushBack(tmpnode);
00153     this->Modified();
00154   }
00155 
00159   void SetNarrowBandTotalRadius(const float& val)
00160   {
00161     if ( m_NarrowBand->GetTotalRadius() != val )
00162       {
00163       m_NarrowBand->SetTotalRadius(val);
00164       this->Modified();
00165       }
00166   }
00168 
00170   float GetNarrowBandTotalRadius() const
00171   {
00172     return m_NarrowBand->GetTotalRadius();
00173   }
00174 
00177   void SetNarrowBandInnerRadius(const float& val)
00178   {
00179     if ( m_NarrowBand->GetInnerRadius() != val )
00180       {
00181       m_NarrowBand->SetInnerRadius(val);
00182       this->Modified();
00183       }
00184   }
00186 
00188   float GetNarrowBandInnerRadius() const
00189   {
00190     return m_NarrowBand->GetInnerRadius();
00191   }
00192 
00198   virtual void CreateNarrowBand(){}
00199 
00200   virtual void SetNarrowBand(NarrowBandType *ptr)
00201   {
00202     if ( m_NarrowBand != ptr )
00203       {
00204       m_NarrowBand = ptr;
00205       this->Modified();
00206       }
00207   }
00208 
00209   virtual void CopyInputToOutput();
00210 
00211 protected:
00212   NarrowBandImageFilterBase()
00213   {
00214     m_NarrowBand = NarrowBandType::New();
00215     m_NarrowBand->SetTotalRadius(4);
00216     m_NarrowBand->SetInnerRadius(2);
00217     m_ReinitializationFrequency = 6;
00218     m_IsoSurfaceValue = 0.0;
00219     m_Step    = 0;
00220     m_Touched = false;
00221     m_Barrier = Barrier::New();
00222   }
00223 
00224   virtual ~NarrowBandImageFilterBase() {}
00225   void PrintSelf(std::ostream & os, Indent indent) const;
00226 
00227   NarrowBandPointer m_NarrowBand;
00228 
00232   struct ThreadRegionType {
00233 
00235     NarrowBandIterator first;
00236 
00238     NarrowBandIterator last;
00239   };
00240 
00243   std::vector< RegionType > m_RegionList;
00244 
00247   void GetSplitRegion(const size_t& i, ThreadRegionType & splitRegion);
00248 
00253   virtual void Initialize();
00254 
00259   virtual void InitializeIteration();
00260 
00263   virtual void PostProcessOutput();
00264 
00265   /* This function clears all pixels from the narrow band */
00266   void ClearNarrowBand();
00267 
00269   void WaitForAll();
00270 
00274   virtual void GenerateData();
00275 
00276   /* Variables to control reinitialization */
00277   IdentifierType m_ReinitializationFrequency;
00278   IdentifierType m_Step;
00279 
00280   bool m_Touched;
00281 
00282   std::vector< bool > m_TouchedForThread;
00283 
00284   ValueType m_IsoSurfaceValue;
00285 
00286   typename Barrier::Pointer m_Barrier;
00287 
00288 private:
00289   NarrowBandImageFilterBase(const Self &); //purposely not implemented
00290   void operator=(const Self &);            //purposely not implemented
00291 
00294   struct NarrowBandImageFilterBaseThreadStruct {
00295     NarrowBandImageFilterBase *Filter;
00296     TimeStepType TimeStep;
00297     std::vector< TimeStepType > TimeStepList;
00298     std::vector< bool > ValidTimeStepList;
00299   };
00300 
00301   /* This class does not use AllocateUpdateBuffer to allocate memory for its
00302    * narrow band. This is taken care of in SetNarrowBand, and InsertNarrowBandNode
00303    * functions. This function is here for compatability with the
00304    * FiniteDifferenceSolver framework.
00305    */
00306   virtual void AllocateUpdateBuffer() {}
00307 
00309   static ITK_THREAD_RETURN_TYPE IterateThreaderCallback(void *arg);
00310 
00314   virtual void ThreadedIterate(void *arg, ThreadIdType threadId);
00315 
00319   virtual void ThreadedApplyUpdate(const TimeStepType& dt,
00320                                    const ThreadRegionType & regionToProcess,
00321                                    ThreadIdType threadId);
00322 
00323   virtual void ApplyUpdate(const TimeStepType&){}
00324 
00328   virtual TimeStepType ThreadedCalculateChange(const ThreadRegionType & regionToProcess,
00329                                                ThreadIdType threadId);
00330 
00331   virtual TimeStepType CalculateChange() { return 0; }
00332 };
00333 } // end namespace itk
00334 
00335 #ifndef ITK_MANUAL_INSTANTIATION
00336 #include "itkNarrowBandImageFilterBase.hxx"
00337 #endif
00338 
00339 #endif
00340