1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // ------------------------------------------------------------------------
5 // Inclusion test taken from:
6 // https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
8 #ifndef __cpExtensions__Algorithms__RasterContourFilter__hxx__
9 #define __cpExtensions__Algorithms__RasterContourFilter__hxx__
11 #include <itkImageRegionIteratorWithIndex.h>
12 #include <vtkPolygon.h>
14 // -------------------------------------------------------------------------
15 template< class _TImage >
16 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
17 AddPoint( double x, double y )
22 this->m_Contour.push_back( pnt );
26 // -------------------------------------------------------------------------
27 template< class _TImage >
28 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
29 AddPoint( double p[ 2 ] )
34 this->m_Contour.push_back( pnt );
38 // -------------------------------------------------------------------------
39 template< class _TImage >
40 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
43 this->m_Contour.clear( );
47 // -------------------------------------------------------------------------
48 template< class _TImage >
49 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
50 RasterContourFilter( )
52 m_InsideValue( TPixel( 1 ) ),
53 m_OutsideValue( TPixel( 0 ) )
58 // -------------------------------------------------------------------------
59 template< class _TImage >
60 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
61 ~RasterContourFilter( )
65 // -------------------------------------------------------------------------
66 template< class _TImage >
67 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
70 _TImage* out = this->GetOutput( 0 );
71 out->SetSpacing( this->m_Template->GetSpacing( ) );
72 out->SetRegions( this->m_Template->GetRequestedRegion( ) );
73 out->SetOrigin( this->m_Template->GetOrigin( ) );
74 out->SetDirection( this->m_Template->GetDirection( ) );
78 // -------------------------------------------------------------------------
79 template< class _TImage >
80 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
81 BeforeThreadedGenerateData( )
83 // Keep just indices, not points
84 this->m_Polygon.clear( );
85 _TImage* out = this->GetOutput( 0 );
86 TIndex minIdx, maxIdx;
87 for( auto c = this->m_Contour.begin( ); c != this->m_Contour.end( ); ++c )
90 out->TransformPhysicalPointToIndex( *c, idx );
92 if( this->m_Polygon.size( ) > 0 )
94 if( this->m_Polygon.back( ) != idx )
96 this->m_Polygon.push_back( idx );
97 minIdx[ 0 ] = ( idx[ 0 ] < minIdx[ 0 ] )? idx[ 0 ]: minIdx[ 0 ];
98 minIdx[ 1 ] = ( idx[ 1 ] < minIdx[ 1 ] )? idx[ 1 ]: minIdx[ 1 ];
99 maxIdx[ 0 ] = ( idx[ 0 ] > maxIdx[ 0 ] )? idx[ 0 ]: maxIdx[ 0 ];
100 maxIdx[ 1 ] = ( idx[ 1 ] > maxIdx[ 1 ] )? idx[ 1 ]: maxIdx[ 1 ];
106 this->m_Polygon.push_back( idx );
107 minIdx = maxIdx = idx;
114 typename _TImage::SizeType size;
115 size[ 0 ] = maxIdx[ 0 ] - minIdx[ 0 ] + 1;
116 size[ 1 ] = maxIdx[ 1 ] - minIdx[ 1 ] + 1;
117 this->m_ROI.SetIndex( minIdx );
118 this->m_ROI.SetSize( size );
121 // -------------------------------------------------------------------------
122 template< class _TImage >
123 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
124 AfterThreadedGenerateData( )
128 // -------------------------------------------------------------------------
129 template< class _TImage >
130 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
131 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
133 long nVerts = this->m_Polygon.size( );
134 _TImage* out = this->GetOutput( );
135 itk::ImageRegionIteratorWithIndex< _TImage > iIt( out, region );
136 for( iIt.GoToBegin( ); !iIt.IsAtEnd( ); ++iIt )
138 TIndex p = iIt.GetIndex( );
140 if( this->m_ROI.IsInside( p ) )
143 for( i = 0, j = nVerts - 1; i < nVerts; j = i++ )
145 TIndex pi = this->m_Polygon[ i ];
146 TIndex pj = this->m_Polygon[ j ];
147 double pi0 = double( pi[ 0 ] );
148 double pi1 = double( pi[ 1 ] );
149 double pj0 = double( pj[ 0 ] );
150 double pj1 = double( pj[ 1 ] );
151 double p0 = double( p[ 0 ] );
152 double p1 = double( p[ 1 ] );
153 double ji0 = pj0 - pi0;
154 double ji1 = pj1 - pi1;
155 double i1 = p1 - pi1;
157 ( ( pi1 > p1 ) != ( pj1 > p1 ) ) &&
158 ( p0 < ( ( ji0 * i1 ) / ji1 ) + pi0 )
165 iIt.Set( ( inside )? this->m_InsideValue: this->m_OutsideValue );
170 #endif // __cpExtensions__Algorithms__RasterContourFilter__hxx__