]> 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 cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
10 SpatialObjectMaskImageFilter( )
11   : Superclass( )
12 {
13   this->SetNumberOfRequiredInputs( 1 );
14   this->InPlaceOff( );
15 }
16
17 // -------------------------------------------------------------------------
18 template< class I, class O >
19 cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
20 ~SpatialObjectMaskImageFilter( )
21 {
22 }
23
24 // -------------------------------------------------------------------------
25 template< class I, class O >
26 void cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
27 GenerateOutputInformation( )
28 {
29   const I* in =
30     dynamic_cast< const I* >( this->itk::ProcessObject::GetInput( 0 ) );
31   for( unsigned int idx = 0; idx < this->GetNumberOfOutputs( ); ++idx )
32   {
33     itk::DataObject* out = this->GetOutput( idx );
34     if( out )
35       out->CopyInformation( in );
36
37   } // rof
38 }
39
40 // -------------------------------------------------------------------------
41 template< class I, class O >
42 void cpExtensions::Algorithms::SpatialObjectMaskImageFilter< I, O >::
43 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType threadId )
44 {
45   if( this->m_SpatialObject.IsNull( ) )
46   {
47     itkGenericExceptionMacro( << "No itk::SpatialObject given." );
48     return;
49
50   } // fi
51
52   // Get inputs
53   const I* in =
54     dynamic_cast< const I* >( this->itk::ProcessObject::GetInput( 0 ) );
55   O* out = this->GetOutput( 0 );
56   const auto size0 = region.GetSize( 0 );
57   if( size0 == 0 )
58     return;
59   const auto nLines = region.GetNumberOfPixels( ) / size0;
60
61   // Create iterators
62   itk::ImageScanlineConstIterator< I > iIt( in, region );
63   itk::ImageScanlineIterator< O > oIt( out, region );
64   itk::ProgressReporter progress( this, threadId, nLines );
65
66   // Main loop
67   typename TSpatialObject::PointType pnt;
68   while( !iIt.IsAtEnd( ) )
69   {
70     while( !iIt.IsAtEndOfLine( ) )
71     {
72       auto idx = iIt.GetIndex( );
73       in->TransformIndexToPhysicalPoint( idx, pnt );
74       if( this->m_SpatialObject->IsInside( pnt ) )
75         oIt.Set( TOutPixel( iIt.Get( ) ) );
76       else
77         oIt.Set( this->m_OutsideValue );
78       ++iIt;
79       ++oIt;
80
81     } // elihw
82     iIt.NextLine( );
83     oIt.NextLine( );
84     progress.CompletedPixel( );
85
86   } // elihw
87 }
88
89 #endif // __CPEXTENSIONS__ALGORITHMS__SPATIALOBJECTMASKIMAGEFILTER__HXX__
90
91 // eof - $RCSfile$