]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image/Observer2D.hxx
Architecture revisited.
[FrontAlgorithms.git] / lib / fpa / VTK / Image / Observer2D.hxx
1 #ifndef __FPA__VTK__IMAGE__OBSERVER2D__HXX__
2 #define __FPA__VTK__IMAGE__OBSERVER2D__HXX__
3
4 #include <vtkCellArray.h>
5 #include <vtkPolyDataMapper.h>
6 #include <vtkPoints.h>
7 #include <vtkPointData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
11
12 // -------------------------------------------------------------------------
13 template< class _TFilter, class _TRenderWindow >
14 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
15 SetRenderWindow( _TRenderWindow* rw )
16 {
17   this->m_RenderWindow = rw;
18 }
19
20 // -------------------------------------------------------------------------
21 template< class _TFilter, class _TRenderWindow >
22 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
23 SetPixel(
24   typename TImage::IndexType idx,
25   unsigned char red,
26   unsigned char green,
27   unsigned char blue,
28   unsigned char alpha
29   )
30 {
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( );
41 }
42
43 // -------------------------------------------------------------------------
44 template< class _TFilter, class _TRenderWindow >
45 void fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
46 Render( )
47 {
48   if( this->m_RenderWindow != NULL )
49     this->m_RenderWindow->Render( );
50 }
51
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 )
56 {
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;
64
65   typedef typename _TFilter::TStartBacktrackingEvent _TStartBacktrackingEvent;
66   typedef typename _TFilter::TEndBacktrackingEvent   _TEndBacktrackingEvent;
67   typedef typename _TFilter::TBacktrackingEvent      _TBacktrackingEvent;
68
69   static unsigned char Colors[][4] =
70     {
71       {   0,   0, 127, 127 },
72       {   0, 127, 127, 127 },
73       { 127,   0, 127, 127 },
74       { 127, 127,   0, 127 },
75       {   0,   0,  63, 127 },
76       {   0,  63,  63, 127 },
77       {  63,   0,  63, 127 },
78       {  63,  63,   0, 127 },
79       {  63,  63, 127, 127 },
80       {  63, 127, 127, 127 },
81       { 127,  63, 127, 127 },
82       { 127, 127,  63, 127 },
83       { 127, 127,  63, 127 },
84       { 127,  63,  63, 127 },
85       {  63, 127,  63, 127 },
86       {  63,  63, 127, 127 }
87     };
88
89   auto filter = dynamic_cast< const _TFilter* >( c );
90   if( this->m_RenderWindow == NULL || filter == NULL )
91     return;
92
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 )
97   {
98     const TImage* img = filter->GetInput( );
99     unsigned int minD = TImage::ImageDimension;
100     minD = ( minD < 3 )? minD: 3;
101
102     int e[ 6 ] = { 0 };
103     typename TImage::RegionType reg = img->GetRequestedRegion( );
104     for( unsigned int i = 0; i < minD; i++ )
105     {
106       e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
107       e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
108
109     } // rof
110
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 ] );
115
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 ] );
120
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 );
129
130     this->m_StencilActor->SetInputData( this->m_Stencil );
131     this->m_StencilActor->InterpolateOff( );
132
133     this->m_Count = 0;
134     this->m_RenderCount = reg.GetNumberOfPixels( );
135
136     vtkRenderer* ren =
137       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
138     ren->AddActor( this->m_StencilActor );
139     return;
140
141   } // fi
142
143   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
144   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
145   if( aliveEvt != NULL || frontEvt != NULL )
146   {
147     if( aliveEvt != NULL )
148       this->SetPixel(
149         aliveEvt->Vertex,
150         Colors[ aliveEvt->FrontId ][ 0 ],
151         Colors[ aliveEvt->FrontId ][ 1 ],
152         Colors[ aliveEvt->FrontId ][ 2 ],
153         Colors[ aliveEvt->FrontId ][ 3 ]
154         );
155     else if( frontEvt != NULL )
156       this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
157     this->m_Count++;
158
159     // Render visual debug
160     double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
161     if( double( this->m_Count ) >= per )
162       this->Render( );
163     if( double( this->m_Count ) >= per )
164       this->m_Count = 0;
165
166     return;
167
168   } // fi
169
170   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
171   if( endEvt != NULL )
172   {
173     vtkRenderer* ren =
174       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
175     ren->RemoveActor( this->m_StencilActor );
176     this->Render( );
177     return;
178
179   } // fi
180
181   const _TBacktrackingEvent* backEvt =
182     dynamic_cast< const _TBacktrackingEvent* >( &e );
183   const _TEndBacktrackingEvent* endBackEvt =
184     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
185   if( backEvt != NULL )
186   {
187     this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
188     return;
189
190   } // fi
191
192   if( endBackEvt != NULL )
193   {
194     this->m_RenderWindow->Render( );
195     /* TODO: DEBUG
196        std::cout << "Press enter: " << std::ends;
197        int aux;
198        std::cin >> aux;
199     */
200     return;
201
202   } // fi
203 }
204
205 // -------------------------------------------------------------------------
206 template< class _TFilter, class _TRenderWindow >
207 fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
208 Observer2D( )
209   : Superclass( ),
210     m_RenderWindow( NULL ),
211     m_RenderPercentage( double( 0.01 ) )
212 {
213   this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
214   this->m_StencilActor = vtkSmartPointer< vtkImageActor >::New( );
215 }
216
217 // -------------------------------------------------------------------------
218 template< class _TFilter, class _TRenderWindow >
219 fpa::VTK::Image::Observer2D< _TFilter, _TRenderWindow >::
220 ~Observer2D( )
221 {
222 }
223
224 #endif // __FPA__VTK__IMAGE__OBSERVER2D__HXX__
225
226 // eof - $RCSfile$