]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image3DObserver.hxx
...
[FrontAlgorithms.git] / lib / fpa / VTK / Image3DObserver.hxx
1 #ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE3DOBSERVER__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::Image3DObserver< 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::Image3DObserver< 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   std::cout << idx << std::endl;
32   this->m_Stencil->SetScalarComponentFromDouble
33     ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 0, red );
34   this->m_Stencil->SetScalarComponentFromDouble
35     ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 1, green );
36   this->m_Stencil->SetScalarComponentFromDouble
37     ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 2, blue );
38   this->m_Stencil->SetScalarComponentFromDouble
39     ( idx[ 0 ], idx[ 1 ], idx[ 2 ], 3, alpha );
40
41   int ijk[ 3 ];
42   ijk[ 0 ] = idx[ 0 ];
43   ijk[ 1 ] = idx[ 1 ];
44   ijk[ 2 ] = idx[ 2 ];
45   long id = this->m_Stencil->ComputePointId( ijk );
46   vtkUnsignedCharArray* pd_data =
47     dynamic_cast< vtkUnsignedCharArray* >(
48       this->m_PolyData->GetPointData( )->GetScalars( )
49       );
50   pd_data->SetTuple4( id, red, green, blue, alpha );
51   this->m_Stencil->Modified( );
52   this->m_PolyData->Modified( );
53   this->m_PolyDataMapper->Modified( );
54   this->m_PolyDataActor->Modified( );
55 }
56
57 // -------------------------------------------------------------------------
58 template< class F, class R >
59 void fpa::VTK::Image3DObserver< F, R >::
60 Render( )
61 {
62   if( this->m_RenderWindow != NULL )
63     this->m_RenderWindow->Render( );
64 }
65
66 // -------------------------------------------------------------------------
67 template< class F, class R >
68 void fpa::VTK::Image3DObserver< F, R >::
69 Execute( const itk::Object* c, const itk::EventObject& e )
70 {
71   typedef typename F::TStartEvent     _TStartEvent;
72   typedef typename F::TStartLoopEvent _TStartLoopEvent;
73   typedef typename F::TEndEvent       _TEndEvent;
74   typedef typename F::TEndLoopEvent   _TEndLoopEvent;
75   typedef typename F::TAliveEvent     _TAliveEvent;
76   typedef typename F::TFrontEvent     _TFrontEvent;
77   typedef typename F::TFreezeEvent    _TFreezeEvent;
78
79   typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
80   typedef typename F::TEndBacktrackingEvent   _TEndBacktrackingEvent;
81   typedef typename F::TBacktrackingEvent      _TBacktrackingEvent;
82
83   static unsigned char Colors[][4] =
84     {
85       {   0,   0, 127, 127 },
86       {   0, 127, 127, 127 },
87       { 127,   0, 127, 127 },
88       { 127, 127,   0, 127 },
89       {   0,   0,  63, 127 },
90       {   0,  63,  63, 127 },
91       {  63,   0,  63, 127 },
92       {  63,  63,   0, 127 },
93       {  63,  63, 127, 127 },
94       {  63, 127, 127, 127 },
95       { 127,  63, 127, 127 },
96       { 127, 127,  63, 127 },
97       { 127, 127,  63, 127 },
98       { 127,  63,  63, 127 },
99       {  63, 127,  63, 127 },
100       {  63,  63, 127, 127 }
101     };
102
103   const F* filter = dynamic_cast< const F* >( c );
104   if( this->m_RenderWindow == NULL || filter == NULL )
105     return;
106
107   const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
108   const _TStartBacktrackingEvent* startBackEvt =
109     dynamic_cast< const _TStartBacktrackingEvent* >( &e );
110   if( startEvt != NULL || startBackEvt != NULL )
111   {
112     const typename F::TInputImage* img = filter->GetInput( );
113     unsigned int minD = F::TInputImage::ImageDimension;
114     minD = ( minD < 3 )? minD: 3;
115
116     int e[ 6 ] = { 0 };
117     typename F::TInputImage::RegionType reg = img->GetRequestedRegion( );
118     for( unsigned int i = 0; i < minD; i++ )
119     {
120       e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
121       e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
122
123     } // rof
124
125     typename F::TInputImage::SpacingType spac = img->GetSpacing( );
126     double s[ 3 ] = { 1, 1, 1 };
127     for( unsigned int i = 0; i < minD; i++ )
128       s[ i ] = double( spac[ i ] );
129
130     typename F::TInputImage::PointType orig = img->GetOrigin( );
131     double o[ 3 ] = { 0 };
132     for( unsigned int i = 0; i < minD; i++ )
133       o[ i ] = double( orig[ i ] );
134
135     this->m_Stencil->SetExtent( e );
136     this->m_Stencil->SetSpacing( s );
137     this->m_Stencil->SetOrigin( o );
138     this->m_Stencil->AllocateScalars( VTK_UNSIGNED_CHAR, 4 );
139     for( unsigned int i = 0; i < 3; i++ )
140       this->m_Stencil->GetPointData( )->
141         GetScalars( )->FillComponent( i, 255 );
142     this->m_Stencil->GetPointData( )->GetScalars( )->FillComponent( 3, 0 );
143
144     vtkSmartPointer< vtkPoints > pd_points =
145       vtkSmartPointer< vtkPoints >::New( );
146     vtkSmartPointer< vtkCellArray > pd_verts =
147       vtkSmartPointer< vtkCellArray >::New( );
148     vtkSmartPointer< vtkUnsignedCharArray > pd_data =
149       vtkSmartPointer< vtkUnsignedCharArray >::New( );
150     long nPoints = this->m_Stencil->GetNumberOfPoints( );
151
152     pd_data->SetNumberOfComponents( 4 );
153     pd_data->SetNumberOfTuples( nPoints );
154     pd_points->SetNumberOfPoints( nPoints );
155     double point[ 3 ];
156     for( long pId = 0; pId < nPoints; ++pId )
157     {
158       this->m_Stencil->GetPoint( pId, point );
159       pd_points->InsertNextPoint( point );
160       pd_verts->InsertNextCell( 1 );
161       pd_verts->InsertCellPoint( pId );
162       pd_data->SetTuple4( pId, 0, 0, 0, 0 );
163
164     } // rof
165     this->m_PolyData->SetPoints( pd_points );
166     this->m_PolyData->SetVerts( pd_verts );
167     this->m_PolyData->GetPointData( )->SetScalars( pd_data );
168
169     this->m_PolyDataMapper->SetInputData( this->m_PolyData );
170     this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
171
172     this->m_Count = 0;
173     this->m_RenderCount = reg.GetNumberOfPixels( );
174
175     vtkRenderer* ren =
176       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
177     ren->AddActor( this->m_PolyDataActor );
178     return;
179
180   } // fi
181
182   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
183   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
184   if( aliveEvt != NULL || frontEvt != NULL )
185   {
186     if( aliveEvt != NULL )
187       this->SetPixel(
188         aliveEvt->Vertex,
189         Colors[ aliveEvt->FrontId ][ 0 ],
190         Colors[ aliveEvt->FrontId ][ 1 ],
191         Colors[ aliveEvt->FrontId ][ 2 ],
192         Colors[ aliveEvt->FrontId ][ 3 ]
193         );
194     else if( frontEvt != NULL )
195       this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
196     this->m_Count++;
197
198     // Render visual debug
199     double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
200     if( double( this->m_Count ) >= per )
201       this->Render( );
202     if( double( this->m_Count ) >= per )
203       this->m_Count = 0;
204
205     return;
206
207   } // fi
208
209   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
210   if( endEvt != NULL )
211   {
212     vtkRenderer* ren =
213       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
214     ren->RemoveActor( this->m_PolyDataActor );
215     this->Render( );
216     return;
217
218   } // fi
219
220   const _TBacktrackingEvent* backEvt =
221     dynamic_cast< const _TBacktrackingEvent* >( &e );
222   const _TEndBacktrackingEvent* endBackEvt =
223     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
224   if( backEvt != NULL )
225   {
226     this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
227     return;
228
229   } // fi
230
231   if( endBackEvt != NULL )
232   {
233     this->m_RenderWindow->Render( );
234     /* TODO: DEBUG
235        std::cout << "Press enter: " << std::ends;
236        int aux;
237        std::cin >> aux;
238     */
239     return;
240
241   } // fi
242 }
243
244 // -------------------------------------------------------------------------
245 template< class F, class R >
246 fpa::VTK::Image3DObserver< F, R >::
247 Image3DObserver( )
248   : Superclass( ),
249     m_RenderWindow( NULL ),
250     m_RenderPercentage( double( 0.001 ) )
251 {
252   this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
253   this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( );
254   this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
255   this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( );
256 }
257
258 // -------------------------------------------------------------------------
259 template< class F, class R >
260 fpa::VTK::Image3DObserver< F, R >::
261 ~Image3DObserver( )
262 {
263 }
264
265 /*
266 #include <vtkCellArray.h>
267 #include <vtkFloatArray.h>
268 #include <vtkPointData.h>
269 #include <vtkPoints.h>
270 #include <vtkRenderer.h>
271 #include <vtkRendererCollection.h>
272
273 // -------------------------------------------------------------------------
274 template< class F, class R >
275 void fpa::VTK::Image3DObserver< F, R >::
276 SetRenderWindow( R* rw )
277 {
278   this->m_RenderWindow = rw;
279 }
280
281 // -------------------------------------------------------------------------
282 template< class F, class R >
283 void fpa::VTK::Image3DObserver< F, R >::
284 Execute( const itk::Object* c, const itk::EventObject& e )
285 {
286   typedef typename F::TStartEvent     _TStartEvent;
287   typedef typename F::TStartLoopEvent _TStartLoopEvent;
288   typedef typename F::TEndEvent       _TEndEvent;
289   typedef typename F::TEndLoopEvent   _TEndLoopEvent;
290   typedef typename F::TAliveEvent     _TAliveEvent;
291   typedef typename F::TFrontEvent     _TFrontEvent;
292   typedef typename F::TFreezeEvent    _TFreezeEvent;
293
294   typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
295   typedef typename F::TEndBacktrackingEvent   _TEndBacktrackingEvent;
296   typedef typename F::TBacktrackingEvent      _TBacktrackingEvent;
297
298   // Check inputs
299   if( this->m_RenderWindow == NULL )
300     return;
301   vtkRenderer* ren =
302     this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
303   if( ren == NULL )
304     return;
305   const F* filter = dynamic_cast< const F* >( c );
306   if( filter == NULL )
307     return;
308     const _TImage* image = filter->GetInput( );
309     if( image == NULL )
310     return;
311
312   const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
313   const _TStartBacktrackingEvent* startBackEvt =
314     dynamic_cast< const _TStartBacktrackingEvent* >( &e );
315   if( startEvt != NULL || startBackEvt != NULL )
316   {
317     // Create actor
318     _TImage::RegionType reg = image->GetLargestPossibleRegion( );
319     _TImage::SizeType siz = reg.GetSize( );
320     if( this->m_Data.GetPointer( ) == NULL )
321     {
322       this->m_Data   = vtkSmartPointer< vtkPolyData >::New( );
323       this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
324       this->m_Actor  = vtkSmartPointer< vtkActor >::New( );
325
326       vtkSmartPointer< vtkPoints > points =
327         vtkSmartPointer< vtkPoints >::New( );
328       vtkSmartPointer< vtkCellArray > vertices =
329         vtkSmartPointer< vtkCellArray >::New( );
330       vtkSmartPointer< vtkFloatArray > scalars =
331         vtkSmartPointer< vtkFloatArray >::New( );
332       this->m_Data->SetPoints( points );
333       this->m_Data->SetVerts( vertices );
334       this->m_Data->GetPointData( )->SetScalars( scalars );
335
336       this->m_Mapper->SetInputData( this->m_Data );
337       this->m_Actor->SetMapper( this->m_Mapper );
338       ren->AddActor( this->m_Actor );
339
340       this->m_Marks = TMarks::New( );
341       this->m_Marks->SetLargestPossibleRegion(
342         image->GetLargestPossibleRegion( )
343         );
344       this->m_Marks->SetRequestedRegion( image->GetRequestedRegion( ) );
345       this->m_Marks->SetBufferedRegion( image->GetBufferedRegion( ) );
346       this->m_Marks->SetOrigin( image->GetOrigin( ) );
347       this->m_Marks->SetSpacing( image->GetSpacing( ) );
348       this->m_Marks->SetDirection( image->GetDirection( ) );
349       this->m_Marks->Allocate( );
350       this->m_Marks->FillBuffer( -1 );
351       this->m_Count = 0;
352       this->m_RenderCount = reg.GetNumberOfPixels( );
353
354     } // fi
355     return;
356
357   } // fi
358
359   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
360   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
361   if( aliveEvt != NULL || frontEvt != NULL )
362   {
363     _TImage::IndexType vertex;
364     if( aliveEvt != NULL )
365       vertex = aliveEvt->Vertex;
366     else if( frontEvt != NULL )
367       vertex = frontEvt->Vertex;
368
369     if( this->m_Marks->GetPixel( vertex ) == -1 )
370     {
371       typename _TImage::PointType pnt;
372       image->TransformIndexToPhysicalPoint( vertex, pnt );
373
374       long pId = this->m_Data->GetNumberOfPoints( );
375       this->m_Data->GetVerts( )->InsertNextCell( 1 );
376       this->m_Data->GetVerts( )->InsertCellPoint( pId );
377       this->m_Data->GetPoints( )->
378         InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
379       this->m_Data->GetPointData( )->
380         GetScalars( )->InsertNextTuple1( 0.5 );
381       this->m_Data->Modified( );
382
383       this->m_Marks->SetPixel( vertex, pId );
384       this->m_Count++;
385
386       // Render visual debug
387       double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
388       if( double( this->m_Count ) >= per )
389         this->m_RenderWindow->Render( );
390       if( double( this->m_Count ) >= per )
391         this->m_Count = 0;
392
393     } // fi
394     return;
395
396   } // fi
397
398   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
399   if( endEvt != NULL )
400   {
401     this->m_RenderWindow->Render( );
402     ren->RemoveActor( this->m_Actor );
403     this->m_RenderWindow->Render( );
404     this->m_Marks = NULL;
405     this->m_Data = NULL;
406     this->m_Mapper = NULL;
407     this->m_Actor = NULL;
408     return;
409
410   } // fi
411
412   const _TBacktrackingEvent* backEvt =
413     dynamic_cast< const _TBacktrackingEvent* >( &e );
414   const _TEndBacktrackingEvent* endBackEvt =
415     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
416   if( backEvt != NULL )
417   {
418       static const unsigned long nColors = 10;
419       double back_id =
420         double( backEvt->FrontId % nColors ) / double( nColors );
421       typename _TImage::PointType pnt;
422       image->TransformIndexToPhysicalPoint( backEvt->Vertex, pnt );
423
424       long pId = this->m_Data->GetNumberOfPoints( );
425       this->m_Data->GetVerts( )->InsertNextCell( 1 );
426       this->m_Data->GetVerts( )->InsertCellPoint( pId );
427       this->m_Data->GetPoints( )->
428         InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
429       this->m_Data->GetPointData( )->
430         GetScalars( )->InsertNextTuple1( back_id );
431       this->m_Data->Modified( );
432       return;
433
434     } // fi
435
436     if( endBackEvt != NULL )
437     {
438       this->m_RenderWindow->Render( );
439          std::cout << "Press enter: " << std::ends;
440          int aux;
441          std::cin >> aux;
442       return;
443
444     } // fi
445 }
446
447 // -------------------------------------------------------------------------
448 template< class F, class R >
449 fpa::VTK::Image3DObserver< F, R >::
450 Image3DObserver( )
451   : Superclass( ),
452     m_RenderWindow( NULL ),
453     m_RenderPercentage( double( 0.0001 ) )
454 {
455 }
456 */
457
458 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__
459
460 // eof - $RCSfile$