]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/RasterContourFilter.hxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / lib / cpExtensions / Algorithms / RasterContourFilter.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // ------------------------------------------------------------------------
4
5 // Inclusion test taken from:
6 // https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
7
8 #ifndef __cpExtensions__Algorithms__RasterContourFilter__hxx__
9 #define __cpExtensions__Algorithms__RasterContourFilter__hxx__
10
11 #include <itkImageRegionIteratorWithIndex.h>
12 #include <vtkPolygon.h>
13
14 // -------------------------------------------------------------------------
15 template< class _TImage >
16 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
17 AddPoint( double x, double y )
18 {
19   TPoint pnt;
20   pnt[ 0 ] = x;
21   pnt[ 1 ] = y;
22   this->m_Contour.push_back( pnt );
23   this->Modified( );
24 }
25
26 // -------------------------------------------------------------------------
27 template< class _TImage >
28 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
29 AddPoint( double p[ 2 ] )
30 {
31   TPoint pnt;
32   pnt[ 0 ] = p[ 0 ];
33   pnt[ 1 ] = p[ 2 ];
34   this->m_Contour.push_back( pnt );
35   this->Modified( );
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TImage >
40 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
41 ClearPoints( )
42 {
43   this->m_Contour.clear( );
44   this->Modified( );
45 }
46
47 // -------------------------------------------------------------------------
48 template< class _TImage >
49 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
50 RasterContourFilter( )
51   : Superclass( ),
52     m_InsideValue( TPixel( 1 ) ),
53     m_OutsideValue( TPixel( 0 ) )
54 {
55   this->ClearPoints( );
56 }
57
58 // -------------------------------------------------------------------------
59 template< class _TImage >
60 cpExtensions::Algorithms::RasterContourFilter< _TImage >::
61 ~RasterContourFilter( )
62 {
63 }
64
65 // -------------------------------------------------------------------------
66 template< class _TImage >
67 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
68 AllocateOutputs( )
69 {
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( ) );
75   out->Allocate( );
76 }
77
78 // -------------------------------------------------------------------------
79 template< class _TImage >
80 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
81 BeforeThreadedGenerateData( )
82 {
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 )
88   {
89     TIndex idx;
90     out->TransformPhysicalPointToIndex( *c, idx );
91     bool added = true;
92     if( this->m_Polygon.size( ) > 0 )
93     {
94       if( this->m_Polygon.back( ) != idx )
95       {
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 ];
101
102       } // fi
103     }
104     else
105     {
106       this->m_Polygon.push_back( idx );
107       minIdx = maxIdx = idx;
108
109     } // fi
110
111   } // rof
112
113   // Set ROI
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 );
119 }
120
121 // -------------------------------------------------------------------------
122 template< class _TImage >
123 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
124 AfterThreadedGenerateData( )
125 {
126 }
127
128 // -------------------------------------------------------------------------
129 template< class _TImage >
130 void cpExtensions::Algorithms::RasterContourFilter< _TImage >::
131 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
132 {
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 )
137   {
138     TIndex p = iIt.GetIndex( );
139     bool inside = false;
140     if( this->m_ROI.IsInside( p ) )
141     {
142       long i, j;
143       for( i = 0, j = nVerts - 1; i < nVerts; j = i++ )
144       {
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;
156         if(
157           ( ( pi1 > p1 ) != ( pj1 > p1 ) ) &&
158           ( p0 < ( ( ji0 * i1 ) / ji1 ) + pi0 )
159           )
160           inside = !inside;
161
162       } // rof
163
164     } // fi
165     iIt.Set( ( inside )? this->m_InsideValue: this->m_OutsideValue );
166
167   } // rof
168 }
169
170 #endif // __cpExtensions__Algorithms__RasterContourFilter__hxx__
171
172 // eof - $RCSfile$