]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image2DObserver.hxx
Almost there...
[FrontAlgorithms.git] / lib / fpa / VTK / Image2DObserver.hxx
1 #ifndef __FPA__VTK__IMAGE2DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE2DOBSERVER__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 F, class R >
14 void fpa::VTK::Image2DObserver< F, R >::
15 SetRenderWindow( R* rw )
16 {
17   this->m_RenderWindow = rw;
18 }
19
20 // -------------------------------------------------------------------------
21 template< class F, class R >
22 void fpa::VTK::Image2DObserver< F, R >::
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 F, class R >
45 void fpa::VTK::Image2DObserver< F, R >::
46 Render( )
47 {
48   if( this->m_RenderWindow != NULL )
49     this->m_RenderWindow->Render( );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class F, class R >
54 void fpa::VTK::Image2DObserver< F, R >::
55 Execute( const itk::Object* c, const itk::EventObject& e )
56 {
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;
64
65   static unsigned char Colors[][4] =
66     {
67       {   0,   0, 127, 127 },
68       {   0, 127, 127, 127 },
69       { 127,   0, 127, 127 },
70       { 127, 127,   0, 127 },
71       {   0,   0,  63, 127 },
72       {   0,  63,  63, 127 },
73       {  63,   0,  63, 127 },
74       {  63,  63,   0, 127 },
75       {  63,  63, 127, 127 },
76       {  63, 127, 127, 127 },
77       { 127,  63, 127, 127 },
78       { 127, 127,  63, 127 },
79       { 127, 127,  63, 127 },
80       { 127,  63,  63, 127 },
81       {  63, 127,  63, 127 },
82       {  63,  63, 127, 127 }
83     };
84
85   const F* filter = dynamic_cast< const F* >( c );
86   if( this->m_RenderWindow == NULL || filter == NULL )
87     return;
88
89   const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
90   if( startEvt != NULL )
91   {
92     const typename F::TInputImage* img = filter->GetInput( );
93     unsigned int minD = F::TInputImage::ImageDimension;
94     minD = ( minD < 3 )? minD: 3;
95
96     int e[ 6 ] = { 0 };
97     typename F::TInputImage::RegionType reg = img->GetRequestedRegion( );
98     for( unsigned int i = 0; i < minD; i++ )
99     {
100       e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
101       e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
102
103     } // rof
104
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 ] );
109
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 ] );
114
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 );
123
124     this->m_StencilActor->SetInputData( this->m_Stencil );
125     this->m_StencilActor->InterpolateOff( );
126
127     this->m_Count = 0;
128     this->m_RenderCount = reg.GetNumberOfPixels( );
129
130     vtkRenderer* ren =
131       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
132     ren->AddActor( this->m_StencilActor );
133     return;
134
135   } // fi
136
137   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
138   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
139   if( aliveEvt != NULL || frontEvt != NULL )
140   {
141     if( aliveEvt != NULL )
142       this->SetPixel(
143         aliveEvt->Vertex,
144         Colors[ aliveEvt->FrontId ][ 0 ],
145         Colors[ aliveEvt->FrontId ][ 1 ],
146         Colors[ aliveEvt->FrontId ][ 2 ],
147         Colors[ aliveEvt->FrontId ][ 3 ]
148         );
149     else if( frontEvt != NULL )
150       this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
151     this->m_Count++;
152
153     // Render visual debug
154     double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
155     if( double( this->m_Count ) >= per )
156       this->Render( );
157     if( double( this->m_Count ) >= per )
158       this->m_Count = 0;
159
160     return;
161
162   } // fi
163
164   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
165   if( endEvt != NULL )
166   {
167     vtkRenderer* ren =
168       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
169     ren->RemoveActor( this->m_StencilActor );
170     this->Render( );
171     return;
172
173   } // fi
174 }
175
176 // -------------------------------------------------------------------------
177 template< class F, class R >
178 fpa::VTK::Image2DObserver< F, R >::
179 Image2DObserver( )
180   : Superclass( ),
181     m_RenderWindow( NULL ),
182     m_RenderPercentage( double( 0.01 ) )
183 {
184   this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
185   this->m_StencilActor = vtkSmartPointer< vtkImageActor >::New( );
186 }
187
188 // -------------------------------------------------------------------------
189 template< class F, class R >
190 fpa::VTK::Image2DObserver< F, R >::
191 ~Image2DObserver( )
192 {
193 }
194
195 #endif // __FPA__VTK__IMAGE2DOBSERVER__HXX__
196
197 // eof - $RCSfile$