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 // -------------------------------------------------------------------------
13 template< class F, class R >
14 void fpa::VTK::Image3DObserver< F, R >::
15 SetRenderWindow( R* rw )
17 this->m_RenderWindow = rw;
20 // -------------------------------------------------------------------------
21 template< class F, class R >
22 void fpa::VTK::Image3DObserver< F, R >::
24 typename TImage::IndexType idx,
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 );
45 long id = this->m_Stencil->ComputePointId( ijk );
46 vtkUnsignedCharArray* pd_data =
47 dynamic_cast< vtkUnsignedCharArray* >(
48 this->m_PolyData->GetPointData( )->GetScalars( )
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( );
57 // -------------------------------------------------------------------------
58 template< class F, class R >
59 void fpa::VTK::Image3DObserver< F, R >::
62 if( this->m_RenderWindow != NULL )
63 this->m_RenderWindow->Render( );
66 // -------------------------------------------------------------------------
67 template< class F, class R >
68 void fpa::VTK::Image3DObserver< F, R >::
69 Execute( const itk::Object* c, const itk::EventObject& e )
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;
79 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
80 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
81 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
83 static unsigned char Colors[][4] =
94 { 63, 127, 127, 127 },
95 { 127, 63, 127, 127 },
96 { 127, 127, 63, 127 },
97 { 127, 127, 63, 127 },
103 const F* filter = dynamic_cast< const F* >( c );
104 if( this->m_RenderWindow == NULL || filter == NULL )
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 )
112 const typename F::TInputImage* img = filter->GetInput( );
113 unsigned int minD = F::TInputImage::ImageDimension;
114 minD = ( minD < 3 )? minD: 3;
117 typename F::TInputImage::RegionType reg = img->GetRequestedRegion( );
118 for( unsigned int i = 0; i < minD; i++ )
120 e[ ( i << 1 ) + 0 ] = reg.GetIndex( )[ i ];
121 e[ ( i << 1 ) + 1 ] = reg.GetIndex( )[ i ] + reg.GetSize( )[ i ] - 1;
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 ] );
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 ] );
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 );
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( );
152 pd_data->SetNumberOfComponents( 4 );
153 pd_data->SetNumberOfTuples( nPoints );
154 pd_points->SetNumberOfPoints( nPoints );
156 for( long pId = 0; pId < nPoints; ++pId )
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 );
165 this->m_PolyData->SetPoints( pd_points );
166 this->m_PolyData->SetVerts( pd_verts );
167 this->m_PolyData->GetPointData( )->SetScalars( pd_data );
169 this->m_PolyDataMapper->SetInputData( this->m_PolyData );
170 this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
173 this->m_RenderCount = reg.GetNumberOfPixels( );
176 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
177 ren->AddActor( this->m_PolyDataActor );
182 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
183 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
184 if( aliveEvt != NULL || frontEvt != NULL )
186 if( aliveEvt != NULL )
189 Colors[ aliveEvt->FrontId ][ 0 ],
190 Colors[ aliveEvt->FrontId ][ 1 ],
191 Colors[ aliveEvt->FrontId ][ 2 ],
192 Colors[ aliveEvt->FrontId ][ 3 ]
194 else if( frontEvt != NULL )
195 this->SetPixel( frontEvt->Vertex, 255, 0, 0, 255 );
198 // Render visual debug
199 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
200 if( double( this->m_Count ) >= per )
202 if( double( this->m_Count ) >= per )
209 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
213 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
214 ren->RemoveActor( this->m_PolyDataActor );
220 const _TBacktrackingEvent* backEvt =
221 dynamic_cast< const _TBacktrackingEvent* >( &e );
222 const _TEndBacktrackingEvent* endBackEvt =
223 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
224 if( backEvt != NULL )
226 this->SetPixel( backEvt->Vertex, 0, 0, 255, 255 );
231 if( endBackEvt != NULL )
233 this->m_RenderWindow->Render( );
235 std::cout << "Press enter: " << std::ends;
244 // -------------------------------------------------------------------------
245 template< class F, class R >
246 fpa::VTK::Image3DObserver< F, R >::
249 m_RenderWindow( NULL ),
250 m_RenderPercentage( double( 0.001 ) )
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( );
258 // -------------------------------------------------------------------------
259 template< class F, class R >
260 fpa::VTK::Image3DObserver< F, R >::
266 #include <vtkCellArray.h>
267 #include <vtkFloatArray.h>
268 #include <vtkPointData.h>
269 #include <vtkPoints.h>
270 #include <vtkRenderer.h>
271 #include <vtkRendererCollection.h>
273 // -------------------------------------------------------------------------
274 template< class F, class R >
275 void fpa::VTK::Image3DObserver< F, R >::
276 SetRenderWindow( R* rw )
278 this->m_RenderWindow = rw;
281 // -------------------------------------------------------------------------
282 template< class F, class R >
283 void fpa::VTK::Image3DObserver< F, R >::
284 Execute( const itk::Object* c, const itk::EventObject& e )
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;
294 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
295 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
296 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
299 if( this->m_RenderWindow == NULL )
302 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
305 const F* filter = dynamic_cast< const F* >( c );
308 const _TImage* image = filter->GetInput( );
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 )
318 _TImage::RegionType reg = image->GetLargestPossibleRegion( );
319 _TImage::SizeType siz = reg.GetSize( );
320 if( this->m_Data.GetPointer( ) == NULL )
322 this->m_Data = vtkSmartPointer< vtkPolyData >::New( );
323 this->m_Mapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
324 this->m_Actor = vtkSmartPointer< vtkActor >::New( );
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 );
336 this->m_Mapper->SetInputData( this->m_Data );
337 this->m_Actor->SetMapper( this->m_Mapper );
338 ren->AddActor( this->m_Actor );
340 this->m_Marks = TMarks::New( );
341 this->m_Marks->SetLargestPossibleRegion(
342 image->GetLargestPossibleRegion( )
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 );
352 this->m_RenderCount = reg.GetNumberOfPixels( );
359 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
360 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
361 if( aliveEvt != NULL || frontEvt != NULL )
363 _TImage::IndexType vertex;
364 if( aliveEvt != NULL )
365 vertex = aliveEvt->Vertex;
366 else if( frontEvt != NULL )
367 vertex = frontEvt->Vertex;
369 if( this->m_Marks->GetPixel( vertex ) == -1 )
371 typename _TImage::PointType pnt;
372 image->TransformIndexToPhysicalPoint( vertex, pnt );
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( );
383 this->m_Marks->SetPixel( vertex, pId );
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 )
398 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
401 this->m_RenderWindow->Render( );
402 ren->RemoveActor( this->m_Actor );
403 this->m_RenderWindow->Render( );
404 this->m_Marks = NULL;
406 this->m_Mapper = NULL;
407 this->m_Actor = NULL;
412 const _TBacktrackingEvent* backEvt =
413 dynamic_cast< const _TBacktrackingEvent* >( &e );
414 const _TEndBacktrackingEvent* endBackEvt =
415 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
416 if( backEvt != NULL )
418 static const unsigned long nColors = 10;
420 double( backEvt->FrontId % nColors ) / double( nColors );
421 typename _TImage::PointType pnt;
422 image->TransformIndexToPhysicalPoint( backEvt->Vertex, pnt );
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( );
436 if( endBackEvt != NULL )
438 this->m_RenderWindow->Render( );
439 std::cout << "Press enter: " << std::ends;
447 // -------------------------------------------------------------------------
448 template< class F, class R >
449 fpa::VTK::Image3DObserver< F, R >::
452 m_RenderWindow( NULL ),
453 m_RenderPercentage( double( 0.0001 ) )
458 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__