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 >::
25 if( this->m_RenderWindow != NULL )
26 this->m_RenderWindow->Render( );
29 // -------------------------------------------------------------------------
30 template< class F, class R >
31 void fpa::VTK::Image3DObserver< F, R >::
32 Execute( const itk::Object* c, const itk::EventObject& e )
34 typedef typename F::TStartEvent _TStartEvent;
35 typedef typename F::TStartLoopEvent _TStartLoopEvent;
36 typedef typename F::TEndEvent _TEndEvent;
37 typedef typename F::TEndLoopEvent _TEndLoopEvent;
38 typedef typename F::TAliveEvent _TAliveEvent;
39 typedef typename F::TFrontEvent _TFrontEvent;
40 typedef typename F::TFreezeEvent _TFreezeEvent;
42 typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
43 typedef typename F::TEndBacktrackingEvent _TEndBacktrackingEvent;
44 typedef typename F::TBacktrackingEvent _TBacktrackingEvent;
46 static unsigned char Colors[][4] =
57 { 63, 127, 127, 127 },
58 { 127, 63, 127, 127 },
59 { 127, 127, 63, 127 },
60 { 127, 127, 63, 127 },
66 const F* filter = dynamic_cast< const F* >( c );
67 if( this->m_RenderWindow == NULL || filter == NULL )
69 auto image = filter->GetInput( );
73 const _TStartEvent* startEvt = dynamic_cast< const _TStartEvent* >( &e );
74 const _TStartBacktrackingEvent* startBackEvt =
75 dynamic_cast< const _TStartBacktrackingEvent* >( &e );
76 if( startEvt != NULL || startBackEvt != NULL )
80 image->GetLargestPossibleRegion( ).GetNumberOfPixels( );
81 this->m_PointsToReplace.clear( );
82 this->m_PointsInFront.clear( );
85 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
86 ren->AddActor( this->m_PolyDataActor );
91 const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
92 if( frontEvt != NULL )
94 typename F::TInputImage::PointType pnt;
95 image->TransformIndexToPhysicalPoint( frontEvt->Vertex, pnt );
96 if( this->m_PointsToReplace.empty( ) )
98 unsigned long nPoints = this->m_PolyData->GetNumberOfPoints( );
99 this->m_PolyData->GetPoints( )->
100 InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
101 this->m_PolyData->GetVerts( )->InsertNextCell( 1 );
102 this->m_PolyData->GetVerts( )->InsertCellPoint( nPoints );
103 this->m_PointsInFront[ frontEvt->Vertex ] = nPoints;
107 auto pIt = this->m_PointsToReplace.begin( );
108 this->m_PolyData->GetPoints( )->
109 SetPoint( pIt->second, pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
110 this->m_PointsToReplace.erase( pIt );
113 this->m_PolyData->Modified( );
114 this->m_PolyDataMapper->Modified( );
115 this->m_PolyDataActor->Modified( );
117 // Render visual debug
119 double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
120 if( double( this->m_Count ) >= per )
122 if( double( this->m_Count ) >= per )
129 const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
130 if( aliveEvt != NULL )
132 auto pIt = this->m_PointsInFront.find( aliveEvt->Vertex );
133 if( pIt != this->m_PointsInFront.end( ) )
135 this->m_PointsToReplace[ pIt->first ] = pIt->second;
136 this->m_PointsInFront.erase( pIt );
143 const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
147 this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
148 ren->RemoveActor( this->m_PolyDataActor );
154 const _TBacktrackingEvent* backEvt =
155 dynamic_cast< const _TBacktrackingEvent* >( &e );
156 const _TEndBacktrackingEvent* endBackEvt =
157 dynamic_cast< const _TEndBacktrackingEvent* >( &e );
158 if( backEvt != NULL )
165 if( endBackEvt != NULL )
167 this->m_RenderWindow->Render( );
170 std::cout << "Press enter: " << std::ends;
179 // -------------------------------------------------------------------------
180 template< class F, class R >
181 fpa::VTK::Image3DObserver< F, R >::
184 m_RenderWindow( NULL ),
185 m_RenderPercentage( double( 0.001 ) )
187 this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( );
188 this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
189 this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( );
191 vtkSmartPointer< vtkPoints > points =
192 vtkSmartPointer< vtkPoints >::New( );
193 vtkSmartPointer< vtkCellArray > verts =
194 vtkSmartPointer< vtkCellArray >::New( );
195 this->m_PolyData->SetPoints( points );
196 this->m_PolyData->SetVerts( verts );
197 this->m_PolyDataMapper->SetInputData( this->m_PolyData );
198 this->m_PolyDataActor->SetMapper( this->m_PolyDataMapper );
199 this->m_PolyDataActor->GetProperty( )->SetColor( 0, 1, 0 );
202 // -------------------------------------------------------------------------
203 template< class F, class R >
204 fpa::VTK::Image3DObserver< F, R >::
209 #endif // __FPA__VTK__IMAGE3DOBSERVER__HXX__