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" );
186 this->m_PolyDataMapper->SetInputData( this->m_PolyData );
187 this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
190 this->m_RenderCount = reg.GetNumberOfPixels( );
193 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
194 ren->AddActor( this->m_PolyDataActor );
199 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
200 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
201 if( aliveEvt != NULL || frontEvt != NULL )
203 if( aliveEvt != NULL )
206 Colors[ aliveEvt->FrontId ][ 0 ],
207 Colors[ aliveEvt->FrontId ][ 1 ],
208 Colors[ aliveEvt->FrontId ][ 2 ],
209 Colors[ aliveEvt->FrontId ][ 3 ]
211 else if( frontEvt != NULL )
212 this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
215 // Render visual debug
216 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
217 if( double( this->m_Count ) >= per )
219 if( double( this->m_Count ) >= per )
226 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
230 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
231 ren->RemoveActor( this->m_PolyDataActor );
237 const _TBacktrackingEvent* backEvt =
238 dynamic_cast< const _TBacktrackingEvent* >( &e );
239 const _TEndBacktrackingEvent* endBackEvt =
240 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
241 if( backEvt != NULL )
243 this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
248 if( endBackEvt != NULL )
250 this->m_RenderWindow->Render( );
252 std::cout << "Press enter: " << std::ends;
261 // -------------------------------------------------------------------------
262 template< class F, class R >
263 fpa::VTK::Image3DObserver< F, R >::
266 m_RenderWindow( NULL ),
267 m_RenderPercentage( double( 0.001 ) )
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( );
275 // -------------------------------------------------------------------------
276 template< class F, class R >
277 fpa::VTK::Image3DObserver< F, R >::
283 #include <vtkCellArray.h>
284 #include <vtkFloatArray.h>
285 #include <vtkPointData.h>
286 #include <vtkPoints.h>
287 #include <vtkRenderer.h>
288 #include <vtkRendererCollection.h>
290 // -------------------------------------------------------------------------
291 template< class F, class R >
292 void fpa::VTK::Image3DObserver< F, R >::
293 SetRenderWindow( R* rw )
295 this->m_RenderWindow = rw;
298 // -------------------------------------------------------------------------
299 template< class F, class R >
300 void fpa::VTK::Image3DObserver< F, R >::
301 Execute( const itk::Object* c, const itk::EventObject& e )
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;
311 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
312 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
313 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
316 if( this->m_RenderWindow == NULL )
319 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
322 const F* filter = dynamic_cast< const F* >( c );
325 const _TImage* image = filter->GetInput( );
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 )
335 _TImage::RegionType reg = image->GetLargestPossibleRegion( );
336 _TImage::SizeType siz = reg.GetSize( );
337 if( this->m_Data.GetPointer( ) == NULL )
339 this->m_Data = vtkSmartPointer< vtkPolyData >::New( );
340 this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
341 this->m_Actor = vtkSmartPointer< vtkActor >::New( );
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 );
353 this->m_Mapper->SetInputData( this->m_Data );
354 this->m_Actor->SetMapper( this->m_Mapper );
355 ren->AddActor( this->m_Actor );
357 this->m_Marks = TMarks::New( );
358 this->m_Marks->SetLargestPossibleRegion(
359 image->GetLargestPossibleRegion( )
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 );
369 this->m_RenderCount = reg.GetNumberOfPixels( );
376 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
377 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
378 if( aliveEvt != NULL || frontEvt != NULL )
380 _TImage::IndexType vertex;
381 if( aliveEvt != NULL )
382 vertex = aliveEvt->Vertex;
383 else if( frontEvt != NULL )
384 vertex = frontEvt->Vertex;
386 if( this->m_Marks->GetPixel( vertex ) == -1 )
388 typename _TImage::PointType pnt;
389 image->TransformIndexToPhysicalPoint( vertex, pnt );
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( );
400 this->m_Marks->SetPixel( vertex, pId );
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 )
415 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
418 this->m_RenderWindow->Render( );
419 ren->RemoveActor( this->m_Actor );
420 this->m_RenderWindow->Render( );
421 this->m_Marks = NULL;
423 this->m_Mapper = NULL;
424 this->m_Actor = NULL;
429 const _TBacktrackingEvent* backEvt =
430 dynamic_cast< const _TBacktrackingEvent* >( &e );
431 const _TEndBacktrackingEvent* endBackEvt =
432 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
433 if( backEvt != NULL )
435 static const unsigned long nColors = 10;
437 double( backEvt->FrontId % nColors ) / double( nColors );
438 typename _TImage::PointType pnt;
439 image->TransformIndexToPhysicalPoint( backEvt->Vertex, pnt );
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( );
453 if( endBackEvt != NULL )
455 this->m_RenderWindow->Render( );
456 std::cout << "Press enter: " << std::ends;
464 // -------------------------------------------------------------------------
465 template< class F, class R >
466 fpa::VTK::Image3DObserver< F, R >::
469 m_RenderWindow( NULL ),
470 m_RenderPercentage( double( 0.0001 ) )
475 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__