00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkParallelSparseFieldLevelSetImageFilter_h_
00018 #define __itkParallelSparseFieldLevelSetImageFilter_h_
00019
00020 #include <vector>
00021 #include "itkFiniteDifferenceImageFilter.h"
00022 #include "itkSparseFieldLayer.h"
00023 #include "itkObjectStore.h"
00024 #include "itkNeighborhoodIterator.h"
00025 #include "itkConstNeighborhoodIterator.h"
00026 #include "itkMultiThreader.h"
00027 #include "itkBarrier.h"
00028 #include "itkSemaphore.h"
00029
00030 namespace itk
00031 {
00032
00037 template <class TNodeIndexType>
00038 class ParallelSparseFieldLevelSetNode
00039 {
00040 public:
00041 TNodeIndexType m_Index;
00042 float m_Value;
00043 ParallelSparseFieldLevelSetNode *Next;
00044 ParallelSparseFieldLevelSetNode *Previous;
00045 };
00046
00073 template <class TNeighborhoodType>
00074 class ParallelSparseFieldCityBlockNeighborList
00075 {
00076 public:
00077 typedef TNeighborhoodType NeighborhoodType;
00078 typedef typename NeighborhoodType::OffsetType OffsetType;
00079 typedef typename NeighborhoodType::RadiusType RadiusType;
00080
00081 itkStaticConstMacro(Dimension, unsigned int, NeighborhoodType::Dimension);
00082
00083 const RadiusType &GetRadius() const
00084 {
00085 return m_Radius;
00086 }
00087
00088 const unsigned int &GetArrayIndex(unsigned int i) const
00089 {
00090 return m_ArrayIndex[i];
00091 }
00092
00093 const OffsetType &GetNeighborhoodOffset(unsigned int i) const
00094 {
00095 return m_NeighborhoodOffset[i];
00096 }
00097
00098 const unsigned int &GetSize() const
00099 {
00100 return m_Size;
00101 }
00102
00103 unsigned int GetStride(unsigned int i)
00104 {
00105 return m_StrideTable[i];
00106 }
00107
00108 ParallelSparseFieldCityBlockNeighborList();
00109
00110 ~ParallelSparseFieldCityBlockNeighborList()
00111 {
00112 m_ArrayIndex.clear();
00113 m_NeighborhoodOffset.clear();
00114 }
00115
00116 void Print(std::ostream &os) const;
00117
00118 private:
00119 char pad1[128];
00120 unsigned int m_Size;
00121 RadiusType m_Radius;
00122 std::vector<unsigned int> m_ArrayIndex;
00123 std::vector<OffsetType> m_NeighborhoodOffset;
00124
00127 unsigned int m_StrideTable[Dimension];
00128 char pad2[128];
00129 };
00130
00246 template <class TInputImage, class TOutputImage>
00247 class ITK_EXPORT ParallelSparseFieldLevelSetImageFilter :
00248 public FiniteDifferenceImageFilter<TInputImage, TOutputImage>
00249 {
00250 public:
00251
00253 typedef ParallelSparseFieldLevelSetImageFilter Self;
00254 typedef FiniteDifferenceImageFilter<TInputImage, TOutputImage> Superclass;
00255 typedef SmartPointer<Self> Pointer;
00256 typedef SmartPointer<const Self> ConstPointer;
00257
00259 typedef typename Superclass::TimeStepType TimeStepType;
00260 typedef typename Superclass::FiniteDifferenceFunctionType FiniteDifferenceFunctionType;
00261 typedef typename Superclass::RadiusType RadiusType;
00262 typedef typename Superclass::NeighborhoodScalesType NeighborhoodScalesType;
00263
00265 itkNewMacro(Self);
00266
00268 itkTypeMacro(ParallelSparseFieldLevelSetImageFilter, FiniteDifferenceImageFilter);
00269
00271 typedef TInputImage InputImageType;
00272 typedef TOutputImage OutputImageType;
00273 typedef typename OutputImageType::IndexType IndexType;
00274
00275 itkStaticConstMacro(ImageDimension, unsigned int, TOutputImage::ImageDimension);
00276
00277 typedef typename OutputImageType::PixelType PixelType;
00278
00279 typedef typename OutputImageType::RegionType ThreadRegionType;
00280
00283 typedef typename OutputImageType::ValueType ValueType;
00284
00286 typedef ParallelSparseFieldLevelSetNode<IndexType> LayerNodeType;
00287
00289 typedef SparseFieldLayer<LayerNodeType> LayerType;
00290 typedef typename LayerType::Pointer LayerPointerType;
00291
00293 typedef std::vector<LayerPointerType> LayerListType;
00294
00296 typedef signed char StatusType;
00297
00300 typedef Image<StatusType, itkGetStaticConstMacro(ImageDimension)> StatusImageType;
00301
00304 typedef ObjectStore<LayerNodeType> LayerNodeStorageType;
00305
00306 typedef Offset<itkGetStaticConstMacro(ImageDimension)> OffsetType;
00307
00311 itkSetMacro(NumberOfLayers, StatusType);
00312 itkGetMacro(NumberOfLayers, StatusType);
00314
00316 itkSetMacro(IsoSurfaceValue, ValueType);
00317 itkGetMacro(IsoSurfaceValue, ValueType);
00319
00320 LayerPointerType GetActiveListForIndex (const IndexType index)
00321 {
00322
00323 const unsigned int indexZ= index[m_SplitAxis];
00324
00325 const unsigned int ThreadNum= this->GetThreadNumber (indexZ);
00326
00327 return m_Data[ThreadNum].m_Layers[0];
00328 }
00329
00330 #ifdef ITK_USE_CONCEPT_CHECKING
00331
00332 itkConceptMacro(OutputEqualityComparableCheck,
00333 (Concept::EqualityComparable<PixelType>));
00334 itkConceptMacro(DoubleConvertibleToOutputCheck,
00335 (Concept::Convertible<double, PixelType>));
00336 itkConceptMacro(OutputOStreamWritableCheck,
00337 (Concept::OStreamWritable<PixelType>));
00338
00340 #endif
00341
00342 protected:
00343 ParallelSparseFieldLevelSetImageFilter();
00344 ~ParallelSparseFieldLevelSetImageFilter() {}
00345 virtual void PrintSelf(std::ostream& os, Indent indent) const;
00346
00348 ParallelSparseFieldCityBlockNeighborList < NeighborhoodIterator<OutputImageType> >
00349 m_NeighborList;
00350
00353 double m_ConstantGradientValue;
00354
00356 static ValueType m_ValueOne;
00357
00359 static ValueType m_ValueZero;
00360
00363 static StatusType m_StatusActiveChangingUp;
00364
00367 static StatusType m_StatusActiveChangingDown;
00368
00371 static StatusType m_StatusNull;
00372
00375 static StatusType m_StatusChanging;
00376
00379 static StatusType m_StatusBoundaryPixel;
00380
00385 typename OutputImageType::Pointer m_ShiftedImage;
00386
00391 LayerListType m_Layers;
00392
00396 StatusType m_NumberOfLayers;
00397
00399 typename StatusImageType::Pointer m_StatusImage;
00400 typename OutputImageType::Pointer m_OutputImage;
00401
00403 typename StatusImageType::Pointer m_StatusImageTemp;
00404 typename OutputImageType::Pointer m_OutputImageTemp;
00405
00407 typename LayerNodeStorageType::Pointer m_LayerNodeStore;
00408
00410 ValueType m_IsoSurfaceValue;
00411
00415
00416
00419 virtual void GenerateData();
00420
00425 void CopyInputToOutput();
00426
00428 void AllocateUpdateBuffer() {}
00429
00432 void Initialize();
00433
00438 void ConstructActiveLayer();
00439
00441 void InitializeActiveLayerValues();
00442
00446 void ConstructLayer(StatusType from, StatusType to);
00447
00449 void ProcessStatusList(LayerType *InputList, StatusType ChangeToStatus,
00450 StatusType SearchForStatus, unsigned int ThreadId);
00451
00456 void PropagateAllLayerValues();
00457
00465 void PropagateLayerValues(StatusType from, StatusType to, StatusType promote,
00466 unsigned int InOrOut);
00467
00472 virtual void InitializeBackgroundPixels();
00473
00477 void ThreadedAllocateData(unsigned int ThreadId);
00478 void ThreadedInitializeData(unsigned int ThreadId, const ThreadRegionType & ThreadRegion);
00480
00490 void ComputeInitialThreadBoundaries();
00491
00493 unsigned int GetThreadNumber(unsigned int splitAxisValue);
00494
00496 void GetThreadRegionSplitByBoundary(unsigned int ThreadId, ThreadRegionType& ThreadRegion);
00497
00500 void DeallocateData();
00501
00503 struct ParallelSparseFieldLevelSetThreadStruct
00504 {
00505 ParallelSparseFieldLevelSetImageFilter* Filter;
00506 TimeStepType* TimeStepList;
00507 bool* ValidTimeStepList;
00508 TimeStepType TimeStep;
00509 };
00510
00515 void Iterate();
00516 static ITK_THREAD_RETURN_TYPE IterateThreaderCallback(void * arg);
00518
00523 inline virtual ValueType ThreadedCalculateUpdateValue(const unsigned int itkNotUsed(ThreadId),
00524 const IndexType itkNotUsed(index),
00525 const TimeStepType &dt,
00526 const ValueType &value,
00527 const ValueType &change)
00528 {
00529 return (value + dt * change);
00530 }
00531
00532
00533
00534
00535 virtual void ThreadedProcessPixelEnteringActiveLayer (const IndexType itkNotUsed(index),
00536 const ValueType itkNotUsed(value),
00537 const unsigned int itkNotUsed(ThreadId));
00538
00540 void ApplyUpdate(TimeStepType) {}
00541
00544 virtual void ThreadedApplyUpdate(TimeStepType dt, unsigned int ThreadId);
00545
00547 TimeStepType CalculateChange()
00548 {
00549 return NumericTraits<TimeStepType>::Zero;
00550 }
00551
00554 virtual TimeStepType ThreadedCalculateChange(unsigned int ThreadId);
00555
00562 void ThreadedUpdateActiveLayerValues(TimeStepType dt, LayerType *StatusUpList,
00563 LayerType *StatusDownList, unsigned int ThreadId);
00564
00566 void CopyInsertList(unsigned int ThreadId, LayerPointerType FromListPtr,
00567 LayerPointerType ToListPtr);
00568
00570 void ClearList(unsigned int ThreadId, LayerPointerType ListPtr);
00571
00574 void CopyInsertInterNeighborNodeTransferBufferLayers(unsigned int ThreadId,
00575 LayerPointerType InputList,
00576 unsigned int InOrOut,
00577 unsigned int BufferLayerNumber);
00578
00581 void ClearInterNeighborNodeTransferBufferLayers(unsigned int ThreadId, unsigned int InOrOut,
00582 unsigned int BufferLayerNumber);
00583
00591 void ThreadedProcessFirstLayerStatusLists(unsigned int InputLayerNumber,
00592 unsigned int OutputLayerNumber,
00593 StatusType SearchForStatus,
00594 unsigned int InOrOut,
00595 unsigned int BufferLayerNumber, unsigned int ThreadId);
00596
00602 void ThreadedProcessStatusList(unsigned int InputLayerNumber, unsigned int OutputLayerNumber,
00603 StatusType ChangeToStatus, StatusType SearchForStatus,
00604 unsigned int InOrOut,
00605 unsigned int BufferLayerNumber, unsigned int ThreadId);
00606
00610 void ThreadedProcessOutsideList(unsigned int InputLayerNumber, StatusType ChangeToStatus,
00611 unsigned int InOrOut, unsigned int BufferLayerNumber, unsigned int ThreadId);
00612
00614 void ThreadedPropagateLayerValues(StatusType from, StatusType to, StatusType promote,
00615 unsigned int InorOut, unsigned int ThreadId);
00616
00619 void GetThreadRegionSplitUniformly(unsigned int ThreadId, ThreadRegionType& ThreadRegion);
00620
00626 void ThreadedPostProcessOutput(const ThreadRegionType & regionToProcess);
00627
00638 virtual void CheckLoadBalance();
00639
00642 virtual void ThreadedLoadBalance(unsigned int ThreadId);
00643
00645 void WaitForAll();
00646 void SignalNeighborsAndWait (unsigned int ThreadId);
00647 void SignalNeighbor (unsigned int SemaphoreArrayNumber, unsigned int ThreadId);
00648 void WaitForNeighbor (unsigned int SemaphoreArrayNumber, unsigned int ThreadId);
00650
00653 virtual void ThreadedInitializeIteration (unsigned int ThreadId);
00654
00657
00658
00660 unsigned int m_NumOfThreads;
00661
00663 unsigned int m_SplitAxis;
00664
00666 unsigned int m_ZSize;
00667
00670 bool m_BoundaryChanged;
00671
00673 unsigned int * m_Boundary;
00674
00676 int * m_GlobalZHistogram;
00677
00679 unsigned int * m_MapZToThreadNumber;
00680
00683 int * m_ZCumulativeFrequency;
00684
00686 typename Barrier::Pointer m_Barrier;
00687
00689 struct ThreadData
00690 {
00691 char pad1 [128];
00692
00693 TimeStepType TimeStep;
00694 ThreadRegionType ThreadRegion;
00695 ValueType m_RMSChange;
00696 unsigned int m_Count;
00697
00699 LayerListType m_Layers;
00700
00702 LayerListType * m_LoadTransferBufferLayers;
00703
00705 typename LayerNodeStorageType::Pointer m_LayerNodeStore;
00706
00707 LayerPointerType UpList[2];
00708 LayerPointerType DownList[2];
00709
00712 LayerPointerType** m_InterNeighborNodeTransferBufferLayers[2];
00713
00716 void * globalData;
00717
00719 int * m_ZHistogram;
00720
00724 typename Semaphore::Pointer m_Semaphore[2];
00725
00727 unsigned int m_SemaphoreArrayNumber;
00728
00729 char pad2 [128];
00730 };
00731
00733 ThreadData *m_Data;
00734
00737 bool m_Stop;
00738
00743 bool m_InterpolateSurfaceLocation;
00744
00745 private:
00746
00747 ParallelSparseFieldLevelSetImageFilter(const Self&);
00748 void operator=(const Self&);
00749
00752 bool m_BoundsCheckingActive;
00753 };
00754
00755 }
00756
00757 #ifndef ITK_MANUAL_INSTANTIATION
00758 #include "itkParallelSparseFieldLevelSetImageFilter.txx"
00759 #endif
00760
00761 #endif
00762