]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image/Observer3D.hxx
...
[FrontAlgorithms.git] / lib / fpa / VTK / Image / Observer3D.hxx
1 #ifndef __FPA__VTK__IMAGE__OBSERVER3D__HXX__
2 #define __FPA__VTK__IMAGE__OBSERVER3D__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 // -------------------------------------------------------------------------
13 template< class F, class R >
14 void fpa::VTK::Image::Observer3D< F, R >::
15 SetRenderWindow( R* rw )
16 {
17   this->m_RenderWindow = rw;
18 }
19
20 // -------------------------------------------------------------------------
21 template< class F, class R >
22 void fpa::VTK::Image::Observer3D< F, R >::
23 Render( )
24 {
25   if( this->m_RenderWindow != NULL )
26     this->m_RenderWindow->Render( );
27 }
28
29 // -------------------------------------------------------------------------
30 template< class F, class R >
31 void fpa::VTK::Image::Observer3D< F, R >::
32 Execute( const itk::Object* c, const itk::EventObject& e )
33 {
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;
41
42   typedef typename F::TStartBacktrackingEvent _TStartBacktrackingEvent;
43   typedef typename F::TEndBacktrackingEvent   _TEndBacktrackingEvent;
44   typedef typename F::TBacktrackingEvent      _TBacktrackingEvent;
45
46   static unsigned char Colors[][4] =
47     {
48       {   0,   0, 127, 127 },
49       {   0, 127, 127, 127 },
50       { 127,   0, 127, 127 },
51       { 127, 127,   0, 127 },
52       {   0,   0,  63, 127 },
53       {   0,  63,  63, 127 },
54       {  63,   0,  63, 127 },
55       {  63,  63,   0, 127 },
56       {  63,  63, 127, 127 },
57       {  63, 127, 127, 127 },
58       { 127,  63, 127, 127 },
59       { 127, 127,  63, 127 },
60       { 127, 127,  63, 127 },
61       { 127,  63,  63, 127 },
62       {  63, 127,  63, 127 },
63       {  63,  63, 127, 127 }
64     };
65
66   const F* filter = dynamic_cast< const F* >( c );
67   if( this->m_RenderWindow == NULL || filter == NULL )
68     return;
69   auto image = filter->GetInput( );
70   if( image == NULL )
71     return;
72
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 )
77   {
78     this->m_Count = 0;
79     this->m_RenderCount =
80       image->GetLargestPossibleRegion( ).GetNumberOfPixels( );
81     this->m_PointsToReplace.clear( );
82     this->m_PointsInFront.clear( );
83
84     vtkRenderer* ren =
85       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
86     ren->AddActor( this->m_PolyDataActor );
87     return;
88
89   } // fi
90
91   const _TFrontEvent* frontEvt = dynamic_cast< const _TFrontEvent* >( &e );
92   if( frontEvt != NULL )
93   {
94     typename F::TInputImage::PointType pnt;
95     image->TransformIndexToPhysicalPoint( frontEvt->Vertex, pnt );
96     if( this->m_PointsToReplace.empty( ) )
97     {
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;
104     }
105     else
106     {
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 );
111
112     } // fi
113     this->m_PolyData->Modified( );
114     this->m_PolyDataMapper->Modified( );
115     this->m_PolyDataActor->Modified( );
116
117     // Render visual debug
118     this->m_Count++;
119     double per = double( this->m_RenderCount ) * this->m_RenderPercentage;
120     if( double( this->m_Count ) >= per )
121       this->Render( );
122     if( double( this->m_Count ) >= per )
123       this->m_Count = 0;
124
125     return;
126
127   } // fi
128
129   const _TAliveEvent* aliveEvt = dynamic_cast< const _TAliveEvent* >( &e );
130   if( aliveEvt != NULL )
131   {
132     auto pIt = this->m_PointsInFront.find( aliveEvt->Vertex );
133     if( pIt != this->m_PointsInFront.end( ) )
134     {
135       this->m_PointsToReplace[ pIt->first ] = pIt->second;
136       this->m_PointsInFront.erase( pIt );
137
138     } // fi
139     return;
140
141   } // fi
142
143   const _TEndEvent* endEvt = dynamic_cast< const _TEndEvent* >( &e );
144   if( endEvt != NULL )
145   {
146     vtkRenderer* ren =
147       this->m_RenderWindow->GetRenderers( )->GetFirstRenderer( );
148     ren->RemoveActor( this->m_PolyDataActor );
149     this->Render( );
150     return;
151
152   } // fi
153
154   const _TBacktrackingEvent* backEvt =
155     dynamic_cast< const _TBacktrackingEvent* >( &e );
156   const _TEndBacktrackingEvent* endBackEvt =
157     dynamic_cast< const _TEndBacktrackingEvent* >( &e );
158   if( backEvt != NULL )
159   {
160     // TODO: 
161     return;
162
163   } // fi
164
165   if( endBackEvt != NULL )
166   {
167     this->m_RenderWindow->Render( );
168
169     /* TODO: DEBUG
170        std::cout << "Press enter: " << std::ends;
171        int aux;
172        std::cin >> aux;
173     */
174     return;
175
176   } // fi
177 }
178
179 // -------------------------------------------------------------------------
180 template< class F, class R >
181 fpa::VTK::Image::Observer3D< F, R >::
182 Observer3D( )
183   : Superclass( ),
184     m_RenderWindow( NULL ),
185     m_RenderPercentage( double( 0.001 ) )
186 {
187   this->m_PolyData = vtkSmartPointer< vtkPolyData >::New( );
188   this->m_PolyDataMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
189   this->m_PolyDataActor =vtkSmartPointer< vtkActor >::New( );
190
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 );
200 }
201
202 // -------------------------------------------------------------------------
203 template< class F, class R >
204 fpa::VTK::Image::Observer3D< F, R >::
205 ~Observer3D( )
206 {
207 }
208
209 #endif // __FPA__VTK__IMAGE__OBSERVER3D__HXX__
210
211 // eof - $RCSfile$