1 #ifndef __FPA__VTK__IMAGE3DOBSERVER__HXX__
2 #define __FPA__VTK__IMAGE3DOBSERVER__HXX__
4 #include <vtkCellArray.h>
5 #include <vtkPolyDataMapper.h>
7 #include <vtkPointData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
12 #include <vtkPolyDataWriter.h>
15 // -------------------------------------------------------------------------
16 template< class F, class R >
17 void fpa::VTK::Image3DObserver< F, R >::
18 SetRenderWindow( R* rw )
20 this->m_RenderWindow = rw;
23 // -------------------------------------------------------------------------
24 template< class F, class R >
25 void fpa::VTK::Image3DObserver< F, R >::
27 typename TImage::IndexType idx,
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 );
47 long id = this->m_Stencil->ComputePointId( ijk );
48 vtkUnsignedCharArray* pd_data =
49 dynamic_cast< vtkUnsignedCharArray* >(
50 this->m_PolyData->GetPointData( )->GetScalars( )
54 pd_data->SetTuple4( id, red, green, blue, alpha );
57 this->m_Stencil->Modified( );
58 this->m_PolyData->Modified( );
59 this->m_PolyDataMapper->Modified( );
60 this->m_PolyDataActor->Modified( );
63 // -------------------------------------------------------------------------
64 template< class F, class R >
65 void fpa::VTK::Image3DObserver< F, R >::
68 if( this->m_RenderWindow != NULL )
69 this->m_RenderWindow->Render( );
72 // -------------------------------------------------------------------------
73 template< class F, class R >
74 void fpa::VTK::Image3DObserver< F, R >::
75 Execute( const itk::Object* c, const itk::EventObject& e )
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;
85 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
86 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
87 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
89 static unsigned char Colors[][4] =
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 },
109 const F* filter = dynamic_cast< const F* >( c );
110 if( this->m_RenderWindow == NULL || filter == NULL )
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 )
118 const typename F::TInputImage* img = filter->GetInput( );
119 unsigned int minD = F::TInputImage::ImageDimension;
120 minD = ( minD < 3 )? minD: 3;
123 typename F::TInputImage::RegionType reg = img->GetRequestedRegion( );
124 for( unsigned int i = 0; i < minD; i++ )
126 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
127 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
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 ] );
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 ] );
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 );
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( );
158 pd_data->SetNumberOfComponents( 4 );
159 pd_data->SetNumberOfTuples( nPoints );
160 pd_points->SetNumberOfPoints( nPoints );
162 for( long pId = 0; pId < nPoints; ++pId )
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 );
171 this->m_PolyData->SetPoints( pd_points );
172 this->m_PolyData->SetVerts( pd_verts );
173 // this->m_PolyData->GetPointData( )->SetScalars( pd_data );
176 vtkSmartPointer< vtkPolyDataWriter > w =
177 vtkSmartPointer< vtkPolyDataWriter >::New( );
178 w->SetInputData( this->m_PolyData );
179 w->SetFileName( "fpa.vtk" );
187 this->m_PolyDataMapper->SetInputData( this->m_PolyData );
188 this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
191 this->m_RenderCount = reg.GetNumberOfPixels( );
194 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
195 ren->AddActor( this->m_PolyDataActor );
200 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
201 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
202 if( aliveEvt != NULL || frontEvt != NULL )
204 if( aliveEvt != NULL )
207 Colors[ aliveEvt->FrontId ][ 0 ],
208 Colors[ aliveEvt->FrontId ][ 1 ],
209 Colors[ aliveEvt->FrontId ][ 2 ],
210 Colors[ aliveEvt->FrontId ][ 3 ]
212 else if( frontEvt != NULL )
213 this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
216 // Render visual debug
217 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
218 if( double( this->m_Count ) >= per )
220 if( double( this->m_Count ) >= per )
227 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
231 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
232 ren->RemoveActor( this->m_PolyDataActor );
238 const _TBacktrackingEvent* backEvt =
239 dynamic_cast< const _TBacktrackingEvent* >( &e );
240 const _TEndBacktrackingEvent* endBackEvt =
241 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
242 if( backEvt != NULL )
244 this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
249 if( endBackEvt != NULL )
251 this->m_RenderWindow->Render( );
253 std::cout << "Press enter: " << std::ends;
262 // -------------------------------------------------------------------------
263 template< class F, class R >
264 fpa::VTK::Image3DObserver< F, R >::
267 m_RenderWindow( NULL ),
268 m_RenderPercentage( double( 0.001 ) )
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( );
276 // -------------------------------------------------------------------------
277 template< class F, class R >
278 fpa::VTK::Image3DObserver< F, R >::
284 #include <vtkCellArray.h>
285 #include <vtkFloatArray.h>
286 #include <vtkPointData.h>
287 #include <vtkPoints.h>
288 #include <vtkRenderer.h>
289 #include <vtkRendererCollection.h>
291 // -------------------------------------------------------------------------
292 template< class F, class R >
293 void fpa::VTK::Image3DObserver< F, R >::
294 SetRenderWindow( R* rw )
296 this->m_RenderWindow = rw;
299 // -------------------------------------------------------------------------
300 template< class F, class R >
301 void fpa::VTK::Image3DObserver< F, R >::
302 Execute( const itk::Object* c, const itk::EventObject& e )
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;
312 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
313 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
314 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
317 if( this->m_RenderWindow == NULL )
320 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
323 const F* filter = dynamic_cast< const F* >( c );
326 const _TImage* image = filter->GetInput( );
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 )
336 _TImage::RegionType reg = image->GetLargestPossibleRegion( );
337 _TImage::SizeType siz = reg.GetSize( );
338 if( this->m_Data.GetPointer( ) == NULL )
340 this->m_Data = vtkSmartPointer< vtkPolyData >::New( );
341 this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
342 this->m_Actor = vtkSmartPointer< vtkActor >::New( );
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 );
354 this->m_Mapper->SetInputData( this->m_Data );
355 this->m_Actor->SetMapper( this->m_Mapper );
356 ren->AddActor( this->m_Actor );
358 this->m_Marks = TMarks::New( );
359 this->m_Marks->SetLargestPossibleRegion(
360 image->GetLargestPossibleRegion( )
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 );
370 this->m_RenderCount = reg.GetNumberOfPixels( );
377 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
378 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
379 if( aliveEvt != NULL || frontEvt != NULL )
381 _TImage::IndexType vertex;
382 if( aliveEvt != NULL )
383 vertex = aliveEvt->Vertex;
384 else if( frontEvt != NULL )
385 vertex = frontEvt->Vertex;
387 if( this->m_Marks->GetPixel( vertex ) == -1 )
389 typename _TImage::PointType pnt;
390 image->TransformIndexToPhysicalPoint( vertex, pnt );
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( );
401 this->m_Marks->SetPixel( vertex, pId );
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 )
416 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
419 this->m_RenderWindow->Render( );
420 ren->RemoveActor( this->m_Actor );
421 this->m_RenderWindow->Render( );
422 this->m_Marks = NULL;
424 this->m_Mapper = NULL;
425 this->m_Actor = NULL;
430 const _TBacktrackingEvent* backEvt =
431 dynamic_cast< const _TBacktrackingEvent* >( &e );
432 const _TEndBacktrackingEvent* endBackEvt =
433 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
434 if( backEvt != NULL )
436 static const unsigned long nColors = 10;
438 double( backEvt->FrontId % nColors ) / double( nColors );
439 typename _TImage::PointType pnt;
440 image->TransformIndexToPhysicalPoint( backEvt->Vertex, pnt );
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( );
454 if( endBackEvt != NULL )
456 this->m_RenderWindow->Render( );
457 std::cout << "Press enter: " << std::ends;
465 // -------------------------------------------------------------------------
466 template< class F, class R >
467 fpa::VTK::Image3DObserver< F, R >::
470 m_RenderWindow( NULL ),
471 m_RenderPercentage( double( 0.0001 ) )
476 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__