1 #ifndef __FPA__VTK__IMAGE2DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE2DOBSERVER__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 F, class R >
14 void fpa::VTK::Image2DObserver< F, R >::
15 SetRenderWindow( R* rw )
17 this->m_RenderWindow = rw;
20 // -------------------------------------------------------------------------
21 template< class F, class R >
22 void fpa::VTK::Image2DObserver< F, R >::
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 F, class R >
45 void fpa::VTK::Image2DObserver< F, R >::
48 if( this->m_RenderWindow != NULL )
49 this->m_RenderWindow->Render( );
52 // -------------------------------------------------------------------------
53 template< class F, class R >
54 void fpa::VTK::Image2DObserver< F, R >::
55 Execute( const itk::Object* c, const itk::EventObject& e )
57 typedef typename F::TStartEvent _TStartEvent;
58 typedef typename F::TStartLoopEvent _TStartLoopEvent;
59 typedef typename F::TEndEvent _TEndEvent;
60 typedef typename F::TEndLoopEvent _TEndLoopEvent;
61 typedef typename F::TAliveEvent _TAliveEvent;
62 typedef typename F::TFrontEvent _TFrontEvent;
63 typedef typename F::TFreezeEvent _TFreezeEvent;
65 static unsigned char Colors[][4] =
76 { 63, 127, 127, 127 },
77 { 127, 63, 127, 127 },
78 { 127, 127, 63, 127 },
79 { 127, 127, 63, 127 },
85 const F* filter = dynamic_cast< const F* >( c );
86 if( this->m_RenderWindow == NULL || filter == NULL )
89 const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
90 if( startEvt != NULL )
92 const typename F::TInputImage* img = filter->GetInput( );
93 unsigned int minD = F::TInputImage::ImageDimension;
94 minD = ( minD < 3 )? minD: 3;
97 typename F::TInputImage::RegionType reg = img->GetRequestedRegion( );
98 for( unsigned int i = 0; i < minD; i++ )
100 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
101 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
105 typename F::TInputImage::SpacingType spac = img->GetSpacing( );
106 double s[ 3 ] = { 1, 1, 1 };
107 for( unsigned int i = 0; i < minD; i++ )
108 s[ i ] = double( spac[ i ] );
110 typename F::TInputImage::PointType orig = img->GetOrigin( );
111 double o[ 3 ] = { 0 };
112 for( unsigned int i = 0; i < minD; i++ )
113 o[ i ] = double( orig[ i ] );
115 this->m_Stencil->SetExtent( e );
116 this->m_Stencil->SetSpacing( s );
117 this->m_Stencil->SetOrigin( o );
118 this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
119 for( unsigned int i = 0; i < 3; i++ )
120 this->m_Stencil->GetPointData( )->
121 GetScalars( )->FillComponent( i, 255 );
122 this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 );
124 this->m_StencilActor->SetInputData( this->m_Stencil );
125 this->m_StencilActor->InterpolateOff( );
128 this->m_RenderCount = reg.GetNumberOfPixels( );
131 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
132 ren->AddActor( this->m_StencilActor );
137 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
138 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
139 if( aliveEvt != NULL || frontEvt != NULL )
141 if( aliveEvt != NULL )
144 Colors[ aliveEvt->FrontId ][ 0 ],
145 Colors[ aliveEvt->FrontId ][ 1 ],
146 Colors[ aliveEvt->FrontId ][ 2 ],
147 Colors[ aliveEvt->FrontId ][ 3 ]
149 else if( frontEvt != NULL )
150 this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
153 // Render visual debug
154 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
155 if( double( this->m_Count ) >= per )
157 if( double( this->m_Count ) >= per )
164 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
168 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
169 ren->RemoveActor( this->m_StencilActor );
176 // -------------------------------------------------------------------------
177 template< class F, class R >
178 fpa::VTK::Image2DObserver< F, R >::
181 m_RenderWindow( NULL ),
182 m_RenderPercentage( double( 0.01 ) )
184 this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
185 this->m_StencilActor = vtkSmartPointer< vtkImageActor >::New( );
188 // -------------------------------------------------------------------------
189 template< class F, class R >
190 fpa::VTK::Image2DObserver< F, R >::
195 #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__