1 #ifndef __FPA__VTK__IMAGE__OBSERVER2D__HXX__
2 #define __FPA__VTK__IMAGE__OBSERVER2D__HXX__
4 #include <vtkCellArray.h>
5 #include <vtkPolyDataMapper.h>
7 #include <vtkPointData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
12 // -------------------------------------------------------------------------
13 template< class _TFilter, class _TRenderWindow >
14 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
15 SetRenderWindow( _TRenderWindow* rw )
17 this->m_RenderWindow = rw;
20 // -------------------------------------------------------------------------
21 template< class _TFilter, class _TRenderWindow >
22 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
24 typename TImage::IndexType idx,
31 this->m_Stencil->SetScalarComponentFromDouble
32 ( idx[ 0 ], idx[ 1 ], 0, 0, red );
33 this->m_Stencil->SetScalarComponentFromDouble
34 ( idx[ 0 ], idx[ 1 ], 0, 1, green );
35 this->m_Stencil->SetScalarComponentFromDouble
36 ( idx[ 0 ], idx[ 1 ], 0, 2, blue );
37 this->m_Stencil->SetScalarComponentFromDouble
38 ( idx[ 0 ], idx[ 1 ], 0, 3, alpha );
39 this->m_Stencil->Modified( );
40 this->m_StencilActor->Modified( );
43 // -------------------------------------------------------------------------
44 template< class _TFilter, class _TRenderWindow >
45 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
48 if( this->m_RenderWindow != NULL )
49 this->m_RenderWindow->Render( );
52 // -------------------------------------------------------------------------
53 template< class _TFilter, class _TRenderWindow >
54 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
55 Execute( const itk::Object* c, const itk::EventObject& e )
57 typedef typename _TFilter::TStartEvent _TStartEvent;
58 typedef typename _TFilter::TStartLoopEvent _TStartLoopEvent;
59 typedef typename _TFilter::TEndEvent _TEndEvent;
60 typedef typename _TFilter::TEndLoopEvent _TEndLoopEvent;
61 typedef typename _TFilter::TAliveEvent _TAliveEvent;
62 typedef typename _TFilter::TFrontEvent _TFrontEvent;
63 typedef typename _TFilter::TFreezeEvent _TFreezeEvent;
65 typedef typename _TFilter::TStartBacktrackingEvent _TStartBacktrackingEvent;
66 typedef typename _TFilter::TEndBacktrackingEvent _TEndBacktrackingEvent;
67 typedef typename _TFilter::TBacktrackingEvent _TBacktrackingEvent;
69 static unsigned char Colors[][4] =
80 { 63, 127, 127, 127 },
81 { 127, 63, 127, 127 },
82 { 127, 127, 63, 127 },
83 { 127, 127, 63, 127 },
89 auto filter = dynamic_cast< const _TFilter* >( c );
90 if( this->m_RenderWindow == NULL || filter == NULL )
93 const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
94 const _TStartBacktrackingEvent* startBackEvt =
95 dynamic_cast< const _TStartBacktrackingEvent* >( &e );
96 if( startEvt != NULL || startBackEvt != NULL )
98 const TImage* img = filter->GetInput( );
99 unsigned int minD = TImage::ImageDimension;
100 minD = ( minD < 3 )? minD: 3;
103 typename TImage::RegionType reg = img->GetRequestedRegion( );
104 for( unsigned int i = 0; i < minD; i++ )
106 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
107 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
111 typename TImage::SpacingType spac = img->GetSpacing( );
112 double s[ 3 ] = { 1, 1, 1 };
113 for( unsigned int i = 0; i < minD; i++ )
114 s[ i ] = double( spac[ i ] );
116 typename TImage::PointType orig = img->GetOrigin( );
117 double o[ 3 ] = { 0 };
118 for( unsigned int i = 0; i < minD; i++ )
119 o[ i ] = double( orig[ i ] );
121 this->m_Stencil->SetExtent( e );
122 this->m_Stencil->SetSpacing( s );
123 this->m_Stencil->SetOrigin( o );
124 this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
125 for( unsigned int i = 0; i < 3; i++ )
126 this->m_Stencil->GetPointData( )->
127 GetScalars( )->FillComponent( i, 255 );
128 this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 );
130 this->m_StencilActor->SetInputData( this->m_Stencil );
131 this->m_StencilActor->InterpolateOff( );
134 this->m_RenderCount = reg.GetNumberOfPixels( );
137 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
138 ren->AddActor( this->m_StencilActor );
143 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
144 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
145 if( aliveEvt != NULL || frontEvt != NULL )
147 if( aliveEvt != NULL )
150 Colors[ aliveEvt->FrontId ][ 0 ],
151 Colors[ aliveEvt->FrontId ][ 1 ],
152 Colors[ aliveEvt->FrontId ][ 2 ],
153 Colors[ aliveEvt->FrontId ][ 3 ]
155 else if( frontEvt != NULL )
156 this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
159 // Render visual debug
160 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
161 if( double( this->m_Count ) >= per )
163 if( double( this->m_Count ) >= per )
170 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
174 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
175 ren->RemoveActor( this->m_StencilActor );
181 const _TBacktrackingEvent* backEvt =
182 dynamic_cast< const _TBacktrackingEvent* >( &e );
183 const _TEndBacktrackingEvent* endBackEvt =
184 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
185 if( backEvt != NULL )
187 this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
192 if( endBackEvt != NULL )
194 this->m_RenderWindow->Render( );
196 std::cout << "Press enter: " << std::ends;
205 // -------------------------------------------------------------------------
206 template< class _TFilter, class _TRenderWindow >
207 fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
210 m_RenderWindow( NULL ),
211 m_RenderPercentage( double( 0.01 ) )
213 this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
214 this->m_StencilActor = vtkSmartPointer< vtkImageActor >::New( );
217 // -------------------------------------------------------------------------
218 template< class _TFilter, class _TRenderWindow >
219 fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
224 #endif // __FPA__VTK__IMAGE__OBSERVER2D__HXX__