]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/SpatialObjectMaskImageFilter.hxx
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / SpatialObjectMaskImageFilter.hxx
1 #ifndef __CPEXTENSIONS__ALGORITHMS__SPATIALOBJECTMASKIMAGEFILTER__HXX__
2 #define __CPEXTENSIONS__ALGORITHMS__SPATIALOBJECTMASKIMAGEFILTER__HXX__
3
4 #include <itkImageScanlineIterator.h>
5 #include <itkProgressReporter.h>
6
7 // -------------------------------------------------------------------------
8 template< class I, class O >
9 O* cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
10 GetPositiveOutput( )
11 {
12   return( this->GetOutput( 0 ) );
13 }
14
15 // -------------------------------------------------------------------------
16 template< class I, class O >
17 const O* cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
18 GetPositiveOutput( ) const
19 {
20   return( this->GetOutput( 0 ) );
21 }
22
23 // -------------------------------------------------------------------------
24 template< class I, class O >
25 O* cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
26 GetNegativeOutput( )
27 {
28   return( this->GetOutput( 1 ) );
29 }
30
31 // -------------------------------------------------------------------------
32 template< class I, class O >
33 const O* cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
34 GetNegativeOutput( ) const
35 {
36   return( this->GetOutput( 1 ) );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class I, class O >
41 cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
42 SpatialObjectMaskImageFilter( )
43   : Superclass( )
44 {
45   this->SetNumberOfRequiredInputs( 1 );
46   this->SetNumberOfRequiredOutputs( 2 );
47   this->SetNthOutput( 0, O::New( ) );
48   this->SetNthOutput( 1, O::New( ) );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class I, class O >
53 cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
54 ~SpatialObjectMaskImageFilter( )
55 {
56 }
57
58 // -------------------------------------------------------------------------
59 template< class I, class O >
60 void cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
61 GenerateOutputInformation( )
62 {
63   const I* in =
64     dynamic_cast< const I* >( this->itk::ProcessObject::GetInput( 0 ) );
65   for( unsigned int idx = 0; idx < this->GetNumberOfOutputs( ); ++idx )
66   {
67     itk::DataObject* out = this->GetOutput( idx );
68     if( out )
69       out->CopyInformation( in );
70
71   } // rof
72 }
73
74 // -------------------------------------------------------------------------
75 template< class I, class O >
76 void cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
77 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId )
78 {
79   if( this->m_SpatialObject.IsNull( ) )
80   {
81     itkGenericExceptionMacro( << "No itk::SpatialObject given." );
82     return;
83
84   } // fi
85
86   // Get inputs
87   const I* in =
88     dynamic_cast< const I* >( this->itk::ProcessObject::GetInput( 0 ) );
89   O* pos_out = this->GetPositiveOutput( );
90   O* neg_out = this->GetNegativeOutput( );
91   const auto size0 = region.GetSize( 0 );
92   if( size0 == 0 )
93     return;
94   const auto nLines = region.GetNumberOfPixels( ) / size0;
95
96   // Create iterators
97   itk::ImageScanlineConstIterator< I > iIt( in, region );
98   itk::ImageScanlineIterator< O > pos_oIt( pos_out, region );
99   itk::ImageScanlineIterator< O > neg_oIt( neg_out, region );
100   itk::ProgressReporter progress( this, threadId, nLines );
101
102   // Main loop
103   typename TSpatialObject::PointType pnt;
104   while( !iIt.IsAtEnd( ) )
105   {
106     while( !iIt.IsAtEndOfLine( ) )
107     {
108       auto idx = iIt.GetIndex( );
109       in->TransformIndexToPhysicalPoint( idx, pnt );
110       if( this->m_SpatialObject->IsInside( pnt ) )
111       {
112         pos_oIt.Set( TOutPixel( iIt.Get( ) ) );
113         neg_oIt.Set( this->m_OutsideValue );
114       }
115       else
116       {
117         neg_oIt.Set( TOutPixel( iIt.Get( ) ) );
118         pos_oIt.Set( this->m_OutsideValue );
119
120       } // fi
121       ++iIt;
122       ++pos_oIt;
123       ++neg_oIt;
124
125     } // elihw
126     iIt.NextLine( );
127     pos_oIt.NextLine( );
128     neg_oIt.NextLine( );
129     progress.CompletedPixel( );
130
131   } // elihw
132 }
133
134 #endif // __CPEXTENSIONS__ALGORITHMS__SPATIALOBJECTMASKIMAGEFILTER__HXX__
135
136 // eof - $RCSfile$