ANIMA  4.0
animaNumberedThreadImageToImageFilter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <itkImageToImageFilter.h>
4 #include <mutex>
5 #include <itkVariableLengthVector.h>
6 
7 namespace anima
8 {
9 
14 template <typename TInputImage, typename TOutputImage>
16  public itk::ImageToImageFilter < TInputImage, TOutputImage >
17 {
18 public:
21  typedef itk::ImageToImageFilter <TInputImage, TOutputImage> Superclass;
22  typedef itk::SmartPointer<Self> Pointer;
23  typedef itk::SmartPointer<const Self> ConstPointer;
24  typedef typename itk::ImageSource<TOutputImage>::ThreadStruct ThreadStruct;
25 
26  static constexpr unsigned int OutputImageDimension = TOutputImage::ImageDimension;
27  typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
28 
30  itkNewMacro(Self)
31 
32 
33  itkTypeMacro(NumberedThreadImageToImageFilter, itk::ImageToImageFilter)
34 
35  itkSetMacro(NumberOfPointsToProcess, unsigned int)
36 
37  itkSetMacro(ComputationRegion, OutputImageRegionType)
38  itkGetMacro(ComputationRegion, OutputImageRegionType)
39 
40 protected:
42  {
43  m_NumberOfProcessedPoints = 0;
44  m_ComputationRegion.SetSize(0,0);
45  m_HighestProcessedSlice = 0;
46  m_ProcessedDimension = 0;
47  }
48 
50 
51  virtual void GenerateData() ITK_OVERRIDE;
52  virtual void BeforeThreadedGenerateData() ITK_OVERRIDE;
53 
54  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ThreaderMultiSplitCallback(void *arg);
55  virtual void ThreadProcessSlices();
56 
57  unsigned int GetSafeThreadId();
58  void SafeReleaseThreadId(unsigned int threadId);
59 
61 
62 
64  template <typename ScalarRealType>
65  void
66  InitializeZeroPixel(TOutputImage *image, itk::VariableLengthVector <ScalarRealType> &zeroPixel)
67  {
68  unsigned int vectorSize = image->GetVectorLength();
69  zeroPixel = itk::VariableLengthVector <ScalarRealType> (vectorSize);
70  zeroPixel.Fill(0.0);
71  }
72 
74  template <typename PixelType>
75  void
76  InitializeZeroPixel(TOutputImage *itkNotUsed(image), PixelType &zeroPixel)
77  {
78  zeroPixel = itk::NumericTraits <PixelType>::ZeroValue();
79  }
80 
81 private:
82  ITK_DISALLOW_COPY_AND_ASSIGN(NumberedThreadImageToImageFilter);
83 
84  std::mutex m_LockThreadIdNumber;
85  std::vector <unsigned int> m_ThreadIdsVector;
86 
87  std::mutex m_LockProcessedPoints;
88  unsigned int m_NumberOfProcessedPoints;
89  unsigned int m_NumberOfPointsToProcess;
90 
91  std::mutex m_LockHighestProcessedSlice;
92  int m_HighestProcessedSlice;
93  unsigned int m_ProcessedDimension;
94 
95  // Optimization of multithread code, compute only on region defined from mask... Uninitialized in constructor.
96  OutputImageRegionType m_ComputationRegion;
97 };
98 
99 } //end namespace anima
100 
Implements a class to handle thread number in a dynamic way for multithreaded methods needing thread ...
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ThreaderMultiSplitCallback(void *arg)
void InitializeZeroPixel(TOutputImage *, PixelType &zeroPixel)
Utility function to initialize output images pixel to zero for all images except vector images...
void InitializeZeroPixel(TOutputImage *image, itk::VariableLengthVector< ScalarRealType > &zeroPixel)
Utility function to initialize output images pixel to zero for vector images.
itk::ImageSource< TOutputImage >::ThreadStruct ThreadStruct
itk::ImageToImageFilter< TInputImage, TOutputImage > Superclass