]> 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 #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     this->m_PolyDataMapper->SetInputData( this->m_PolyData );
187     this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
188
189     this->m_Count = 0;
190     this->m_RenderCount = reg.GetNumberOfPixels( );
191
192     vtkRenderer* ren =
193       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
194     ren->AddActor( this->m_PolyDataActor );
195     return;
196
197   } // fi
198
199   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
200   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
201   if( aliveEvt != NULL || frontEvt != NULL )
202   {
203     if( aliveEvt != NULL )
204       this->SetPixel(
205         aliveEvt->Vertex,
206         Colors[ aliveEvt->FrontId ][ 0 ],
207         Colors[ aliveEvt->FrontId ][ 1 ],
208         Colors[ aliveEvt->FrontId ][ 2 ],
209         Colors[ aliveEvt->FrontId ][ 3 ]
210         );
211     else if( frontEvt != NULL )
212       this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
213     this->m_Count++;
214
215     // Render visual debug
216     double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
217     if( double( this->m_Count ) >= per )
218       this->Render( );
219     if( double( this->m_Count ) >= per )
220       this->m_Count = 0;
221
222     return;
223
224   } // fi
225
226   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
227   if( endEvt != NULL )
228   {
229     vtkRenderer* ren =
230       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
231     ren->RemoveActor( this->m_PolyDataActor );
232     this->Render( );
233     return;
234
235   } // fi
236
237   const _TBacktrackingEvent* backEvt =
238     dynamic_cast< const _TBacktrackingEvent* >( &e );
239   const _TEndBacktrackingEvent* endBackEvt =
240     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
241   if( backEvt != NULL )
242   {
243     this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
244     return;
245
246   } // fi
247
248   if( endBackEvt != NULL )
249   {
250     this->m_RenderWindow->Render( );
251     /* TODO: DEBUG
252        std::cout << "Press enter: " << std::ends;
253        int aux;
254        std::cin >> aux;
255     */
256     return;
257
258   } // fi
259 }
260
261 // -------------------------------------------------------------------------
262 template< class F, class R >
263 fpa::VTK::Image3DObserver< F, R >::
264 Image3DObserver( )
265   : Superclass( ),
266     m_RenderWindow( NULL ),
267     m_RenderPercentage( double( 0.001 ) )
268 {
269   this->m_Stencil = vtkSmartPointer< vtkImageData >::New( );
270   this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( );
271   this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
272   this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( );
273 }
274
275 // -------------------------------------------------------------------------
276 template< class F, class R >
277 fpa::VTK::Image3DObserver< F, R >::
278 ~Image3DObserver( )
279 {
280 }
281
282 /*
283 #include <vtkCellArray.h>
284 #include <vtkFloatArray.h>
285 #include <vtkPointData.h>
286 #include <vtkPoints.h>
287 #include <vtkRenderer.h>
288 #include <vtkRendererCollection.h>
289
290 // -------------------------------------------------------------------------
291 template< class F, class R >
292 void fpa::VTK::Image3DObserver< F, R >::
293 SetRenderWindow( R* rw )
294 {
295   this->m_RenderWindow = rw;
296 }
297
298 // -------------------------------------------------------------------------
299 template< class F, class R >
300 void fpa::VTK::Image3DObserver< F, R >::
301 Execute( const itk::Object* c, const itk::EventObject& e )
302 {
303   typedef typename F::TStartEvent     _TStartEvent;
304   typedef typename F::TStartLoopEvent _TStartLoopEvent;
305   typedef typename F::TEndEvent       _TEndEvent;
306   typedef typename F::TEndLoopEvent   _TEndLoopEvent;
307   typedef typename F::TAliveEvent     _TAliveEvent;
308   typedef typename F::TFrontEvent     _TFrontEvent;
309   typedef typename F::TFreezeEvent    _TFreezeEvent;
310
311   typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
312   typedef typename F::TEndBacktrackingEvent   _TEndBacktrackingEvent;
313   typedef typename F::TBacktrackingEvent      _TBacktrackingEvent;
314
315   // Check inputs
316   if( this->m_RenderWindow == NULL )
317     return;
318   vtkRenderer* ren =
319     this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
320   if( ren == NULL )
321     return;
322   const F* filter = dynamic_cast< const F* >( c );
323   if( filter == NULL )
324     return;
325     const _TImage* image = filter->GetInput( );
326     if( image == NULL )
327     return;
328
329   const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
330   const _TStartBacktrackingEvent* startBackEvt =
331     dynamic_cast< const _TStartBacktrackingEvent* >( &e );
332   if( startEvt != NULL || startBackEvt != NULL )
333   {
334     // Create actor
335     _TImage::RegionType reg = image->GetLargestPossibleRegion( );
336     _TImage::SizeType siz = reg.GetSize( );
337     if( this->m_Data.GetPointer( ) == NULL )
338     {
339       this->m_Data   = vtkSmartPointer< vtkPolyData >::New( );
340       this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
341       this->m_Actor  = vtkSmartPointer< vtkActor >::New( );
342
343       vtkSmartPointer< vtkPoints > points =
344         vtkSmartPointer< vtkPoints >::New( );
345       vtkSmartPointer< vtkCellArray > vertices =
346         vtkSmartPointer< vtkCellArray >::New( );
347       vtkSmartPointer< vtkFloatArray > scalars =
348         vtkSmartPointer< vtkFloatArray >::New( );
349       this->m_Data->SetPoints( points );
350       this->m_Data->SetVerts( vertices );
351       this->m_Data->GetPointData( )->SetScalars( scalars );
352
353       this->m_Mapper->SetInputData( this->m_Data );
354       this->m_Actor->SetMapper( this->m_Mapper );
355       ren->AddActor( this->m_Actor );
356
357       this->m_Marks = TMarks::New( );
358       this->m_Marks->SetLargestPossibleRegion(
359         image->GetLargestPossibleRegion( )
360         );
361       this->m_Marks->SetRequestedRegion( image->GetRequestedRegion( ) );
362       this->m_Marks->SetBufferedRegion( image->GetBufferedRegion( ) );
363       this->m_Marks->SetOrigin( image->GetOrigin( ) );
364       this->m_Marks->SetSpacing( image->GetSpacing( ) );
365       this->m_Marks->SetDirection( image->GetDirection( ) );
366       this->m_Marks->Allocate( );
367       this->m_Marks->FillBuffer( -1 );
368       this->m_Count = 0;
369       this->m_RenderCount = reg.GetNumberOfPixels( );
370
371     } // fi
372     return;
373
374   } // fi
375
376   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
377   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
378   if( aliveEvt != NULL || frontEvt != NULL )
379   {
380     _TImage::IndexType vertex;
381     if( aliveEvt != NULL )
382       vertex = aliveEvt->Vertex;
383     else if( frontEvt != NULL )
384       vertex = frontEvt->Vertex;
385
386     if( this->m_Marks->GetPixel( vertex ) == -1 )
387     {
388       typename _TImage::PointType pnt;
389       image->TransformIndexToPhysicalPoint( vertex, pnt );
390
391       long pId = this->m_Data->GetNumberOfPoints( );
392       this->m_Data->GetVerts( )->InsertNextCell( 1 );
393       this->m_Data->GetVerts( )->InsertCellPoint( pId );
394       this->m_Data->GetPoints( )->
395         InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
396       this->m_Data->GetPointData( )->
397         GetScalars( )->InsertNextTuple1( 0.5 );
398       this->m_Data->Modified( );
399
400       this->m_Marks->SetPixel( vertex, pId );
401       this->m_Count++;
402
403       // Render visual debug
404       double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
405       if( double( this->m_Count ) >= per )
406         this->m_RenderWindow->Render( );
407       if( double( this->m_Count ) >= per )
408         this->m_Count = 0;
409
410     } // fi
411     return;
412
413   } // fi
414
415   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
416   if( endEvt != NULL )
417   {
418     this->m_RenderWindow->Render( );
419     ren->RemoveActor( this->m_Actor );
420     this->m_RenderWindow->Render( );
421     this->m_Marks = NULL;
422     this->m_Data = NULL;
423     this->m_Mapper = NULL;
424     this->m_Actor = NULL;
425     return;
426
427   } // fi
428
429   const _TBacktrackingEvent* backEvt =
430     dynamic_cast< const _TBacktrackingEvent* >( &e );
431   const _TEndBacktrackingEvent* endBackEvt =
432     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
433   if( backEvt != NULL )
434   {
435       static const unsigned long nColors = 10;
436       double back_id =
437         double( backEvt->FrontId % nColors ) / double( nColors );
438       typename _TImage::PointType pnt;
439       image->TransformIndexToPhysicalPoint( backEvt->Vertex, pnt );
440
441       long pId = this->m_Data->GetNumberOfPoints( );
442       this->m_Data->GetVerts( )->InsertNextCell( 1 );
443       this->m_Data->GetVerts( )->InsertCellPoint( pId );
444       this->m_Data->GetPoints( )->
445         InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
446       this->m_Data->GetPointData( )->
447         GetScalars( )->InsertNextTuple1( back_id );
448       this->m_Data->Modified( );
449       return;
450
451     } // fi
452
453     if( endBackEvt != NULL )
454     {
455       this->m_RenderWindow->Render( );
456          std::cout << "Press enter: " << std::ends;
457          int aux;
458          std::cin >> aux;
459       return;
460
461     } // fi
462 }
463
464 // -------------------------------------------------------------------------
465 template< class F, class R >
466 fpa::VTK::Image3DObserver< F, R >::
467 Image3DObserver( )
468   : Superclass( ),
469     m_RenderWindow( NULL ),
470     m_RenderPercentage( double( 0.0001 ) )
471 {
472 }
473 */
474
475 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__
476
477 // eof - $RCSfile$