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