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 )
19 this->m_Polygon->GetPoints( )->InsertNextPoint( x, y, 0 );
23 // -------------------------------------------------------------------------
24 template< class _TImage >
25 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
26 AddPoint( double p[ 2 ] )
28 this->m_Polygon->GetPoints( )->InsertNextPoint( p[ 0 ], p[ 1 ], 0 );
32 // -------------------------------------------------------------------------
33 template< class _TImage >
34 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
35 AddPoints( vtkPoints* points )
38 for( unsigned long i = 0; i < points->GetNumberOfPoints( ); ++i )
40 points->GetPoint( i, p );
41 this->m_Polygon->GetPoints( )->InsertNextPoint( p[ 0 ], p[ 1 ], 0 );
47 // -------------------------------------------------------------------------
48 template< class _TImage >
49 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
52 this->m_Polygon = vtkSmartPointer< vtkPolygon >::New( );
55 // -------------------------------------------------------------------------
56 template< class _TImage >
57 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
58 RasterContourFilter( )
60 m_InsideValue( TPixel( 1 ) ),
61 m_OutsideValue( TPixel( 0 ) )
66 // -------------------------------------------------------------------------
67 template< class _TImage >
68 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
69 ~RasterContourFilter( )
73 // -------------------------------------------------------------------------
74 template< class _TImage >
75 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
78 _TImage* out = this->GetOutput( 0 );
79 out->SetSpacing( this->m_Template->GetSpacing( ) );
80 out->SetRegions( this->m_Template->GetRequestedRegion( ) );
81 out->SetOrigin( this->m_Template->GetOrigin( ) );
82 out->SetDirection( this->m_Template->GetDirection( ) );
86 // -------------------------------------------------------------------------
87 template< class _TImage >
88 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
89 BeforeThreadedGenerateData( )
93 // -------------------------------------------------------------------------
94 template< class _TImage >
95 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
96 AfterThreadedGenerateData( )
100 // -------------------------------------------------------------------------
101 template< class _TImage >
102 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
103 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
105 vtkPoints* points = this->m_Polygon->GetPoints( );
106 unsigned long nPoints = points->GetNumberOfPoints( );
108 static_cast< double* >( points->GetData()->GetVoidPointer( 0 ) );
109 double p[ 3 ], n[ 3 ], b[ 6 ];
110 this->m_Polygon->GetPoints( )->GetBounds( b );
111 n[ 0 ] = n[ 1 ] = double( 0 );
112 n[ 2 ] = double( 1 );
114 _TImage* out = this->GetOutput( );
115 itk::ImageRegionIteratorWithIndex< _TImage > iIt( out, region );
116 for( iIt.GoToBegin( ); !iIt.IsAtEnd( ); ++iIt )
118 TIndex idx = iIt.GetIndex( );
120 out->TransformIndexToPhysicalPoint( idx, pnt );
123 p[ 2 ] = double( 0 );
127 for( i = 0, j = nvert - 1; i < nvert; j = i++ )
129 double pi[ 3 ], pj[ 3 ];
130 points->GetPoint( i, pi );
131 points->GetPoint( j, pj );
134 ( ( pi[ 1 ] > p[ 1 ] ) != ( pj[ 1 ] > p[ 1 ] ) ) &&
135 ( p[ 0 ] < ( pj[ 0 ] - pi[ 0 ] ) * ( p[ 1 ] - pi[ 1 ] ) / ( pj[ 1 ] - pi[ 1 ] ) + pi[ 0 ] )
142 iIt.Set( this->m_InsideValue );
144 iIt.Set( this->m_OutsideValue );
149 #endif // __cpExtensions__Algorithms__RasterContourFilter__hxx__