]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SeedWidget.cxx
440005622c82bdb01b7be79868c6f31442320e75
[cpPlugins.git] / plugins / Widgets / SeedWidget.cxx
1 #include <plugins/Widgets/SeedWidget.h>
2
3 #include <cpPlugins/DataObjects/Image.h>
4 #include <cpPlugins/DataObjects/Mesh.h>
5
6 #include <cpExtensions/Interaction/SeedWidget.h>
7 #include <cpExtensions/Interaction/ImageSlicePointPlacer.h>
8 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
9
10 #include <vtkHandleWidget.h>
11 #include <vtkImageData.h>
12 #include <vtkPlane.h>
13 #include <vtkPointHandleRepresentation3D.h>
14 #include <vtkProperty.h>
15 #include <vtkRendererCollection.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkSeedRepresentation.h>
18
19 // -------------------------------------------------------------------------
20 cpPluginsWidgets::SeedWidget::
21 SeedWidget( )
22   : Superclass( )
23 {
24   typedef cpPlugins::BaseObjects::DataObject _TData;
25   typedef cpPlugins::DataObjects::Mesh       _TMesh;
26
27   // Create ports
28   this->_ConfigureInput< _TData >( "Input", false, false );
29   this->_ConfigureOutput< _TMesh >( "Output" );
30
31   // Create output data
32   auto out = this->_CreateVTK< vtkPolyData >( );
33   out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
34   out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
35   out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
36   out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
37   out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
38   this->GetOutput( "Output" )->SetVTK( out );
39 }
40
41 // -------------------------------------------------------------------------
42 cpPluginsWidgets::SeedWidget::
43 ~SeedWidget( )
44 {
45   /* TODO
46      for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
47      w->second->EnabledOff( );
48      this->m_Widgets.clear( );
49   */
50 }
51
52 // -------------------------------------------------------------------------
53 void cpPluginsWidgets::SeedWidget::
54 _GenerateData( )
55 {
56   auto image = this->GetInputData< vtkImageData >( "Input" );
57   if( image != NULL )
58     this->_GD0_Image( image );
59   else
60     this->_Error( "Invalid input image." );
61 }
62
63 // -------------------------------------------------------------------------
64 void cpPluginsWidgets::SeedWidget::
65 _GD0_Image( vtkImageData* image )
66 {
67   auto seeds = this->GetOutputData< vtkPolyData >( "Output" );
68   if( this->m_Widgets.size( ) == 0 )
69   {
70     this->m_Command = vtkSmartPointer< TCallback >::New( );
71     this->m_Command->SetSeeds( seeds );
72     for(
73       auto inIt = this->m_Interactors.begin( );
74       inIt != this->m_Interactors.end( );
75       ++inIt
76       )
77     {
78       auto rends = ( *inIt )->GetRenderWindow( )->GetRenderers( );
79       std::set< TImageActor* > all_props;
80       if( rends != NULL )
81       {
82         rends->InitTraversal( );
83         while( vtkRenderer* ren = rends->GetNextItem( ) )
84         {
85           auto props = ren->GetViewProps( );
86           props->InitTraversal( );
87           while( vtkProp* p = props->GetNextProp( ) )
88           {
89             auto image_actor = dynamic_cast< TImageActor* >( p );
90             if( image_actor != NULL )
91             {
92               if( image_actor->GetImage( ) == image )
93                 all_props.insert( image_actor );
94
95             } // fi
96
97           } // elihw
98
99         } // elihw
100
101       } // fi
102       if( all_props.size( ) == 1 )
103       {
104         if( this->m_Widgets.find( *inIt ) == this->m_Widgets.end( ) )
105         {
106           auto act = *( all_props.begin( ) );
107           auto pla = vtkSmartPointer< TPlacer >::New( );
108           auto hnd = vtkSmartPointer< THandleRep >::New( );
109           auto rep = vtkSmartPointer< TSeedRep >::New( );
110           auto wdg = vtkSmartPointer< TWidget >::New( );
111
112           pla->SetImageSlice( act );
113           hnd->GetProperty( )->SetColor( 1, 0, 0 );
114           hnd->SetPointPlacer( pla );
115           rep->SetHandleRepresentation( hnd );
116           wdg->SetRepresentation( rep );
117           wdg->SetInteractor( *inIt );
118           act->AddObserver( vtkCommand::InteractionEvent, this->m_Command );
119           wdg->AddObserver( vtkCommand::PlacePointEvent, this->m_Command );
120           wdg->AddObserver( vtkCommand::CursorChangedEvent, this->m_Command );
121           wdg->EnabledOn( );
122           this->m_Widgets[ *inIt ] = wdg;
123
124         } // fi
125
126       } // fi
127
128     } // rof
129
130     // Associate input text
131     std::string text = this->m_Parameters.GetString( "Text" );
132     std::vector< std::string > tok1, tok2;
133     cpExtensions::Tokenize( tok1, text, "#" );
134     for( auto t1 = tok1.begin( ); t1 != tok1.end( ); ++t1 )
135     {
136       if( *t1 != "" )
137       {
138         cpExtensions::Tokenize( tok2, *t1, " " );
139         double x[ 3 ];
140         for( unsigned int d = 0; d < 3; ++d )
141         {
142           if( d < tok2.size( ) )
143           {
144             std::istringstream str( tok2[ d ] );
145             str >> x[ d ];
146           }
147           else
148             x[ d ] = double( 0 );
149
150         } // rof
151
152         seeds->GetPoints( )->InsertNextPoint( x );
153         seeds->GetVerts( )->InsertNextCell( 1 );
154         seeds->GetVerts( )->InsertCellPoint(
155           seeds->GetPoints( )->GetNumberOfPoints( ) - 1
156           );
157         seeds->Modified( );
158
159       } // fi
160
161     } // rof
162   }
163   else
164   {
165     double x[ 3 ];
166     std::stringstream text;
167     for( long i = 0; i < seeds->GetNumberOfPoints( ); ++i )
168     {
169       seeds->GetPoint( i, x );
170       text << x[ 0 ] << " " << x[ 1 ] << " " << x[ 2 ] << "#";
171
172     } // rof
173     this->m_Parameters.SetString( "Text", text.str( ) );
174
175   } // fi
176 }
177
178 // -------------------------------------------------------------------------
179 cpPluginsWidgets::SeedWidget::TCallback*
180 cpPluginsWidgets::SeedWidget::TCallback::
181 New( )
182 {
183   return( new TCallback );
184 }
185
186 // -------------------------------------------------------------------------
187 void cpPluginsWidgets::SeedWidget::TCallback::
188 Execute( vtkObject* caller, unsigned long id, void* data )
189 {
190   static const double EPS = 1e-5;
191   auto src_act = dynamic_cast< TImageActor* >( caller );
192   auto src_wdg = dynamic_cast< TWidget* >( caller );
193
194   if( id == vtkCommand::InteractionEvent && src_act != NULL )
195   {
196     TWidget* widget = NULL;
197     TSeedRep* seed_rep = NULL;
198
199     auto w = this->m_Widgets.begin( );
200     while( w != this->m_Widgets.end( ) && widget == NULL )
201     {
202       auto rep =
203         dynamic_cast< vtkSeedRepresentation* >(
204           ( *w )->GetRepresentation( )
205           );
206       if( rep != NULL )
207       {
208         auto hnd = rep->GetHandleRepresentation( );
209         if( hnd != NULL )
210         {
211           auto pla =
212             dynamic_cast< SeedWidget::TPlacer* >(
213               hnd->GetPointPlacer( )
214               );
215           if( pla != NULL )
216           {
217             auto act = pla->GetImageSlice( );
218             if( act == src_act )
219             {
220               widget = *w;
221               seed_rep = rep;
222
223             } // fi
224
225           } // fi
226
227         } // fi
228
229       } // fi
230       w++;
231
232     } // elihw
233
234     if( widget != NULL )
235     {
236       // Cut through given seeds
237       auto plane = src_act->GetSlicePlane( );
238       double x[ 3 ];
239       std::vector< long > ids;
240       for( long i = 0; i < this->m_Seeds->GetNumberOfPoints( ); ++i )
241       {
242         this->m_Seeds->GetPoint( i, x );
243         double d = plane->DistanceToPlane( x );
244         if( d <= EPS )
245           ids.push_back( i );
246
247       } // rof
248
249       // Erase seeds
250       widget->CompleteInteraction( );
251       widget->EnabledOff( );
252       int nSeeds = seed_rep->GetNumberOfSeeds ();
253       for( int i = 0; i < nSeeds; ++i )
254       {
255         seed_rep->RemoveLastHandle( );
256         widget->DeleteSeed( seed_rep->GetNumberOfSeeds( ) );
257
258       } // rof
259
260       if( ids.size( ) > 0 )
261       {
262         auto ren = widget->GetInteractor( )->FindPokedRenderer(
263           widget->GetInteractor( )->GetEventPosition( )[ 0 ],
264           widget->GetInteractor( )->GetEventPosition( )[ 1 ]
265           );
266
267         // Add seeds
268         seed_rep->BuildRepresentation( );
269         for( auto id = ids.begin( ); id != ids.end( ); ++id )
270         {
271           this->m_Seeds->GetPoint( *id, x );
272           widget->ComputeWorldToDisplay(
273             ren, x[ 0 ], x[ 1 ], x[ 2 ], x
274             );
275           x[ 2 ] = double( 0 );
276           int s = seed_rep->CreateHandle( x );
277           vtkHandleWidget* curr_hnd = widget->CreateNewHandle( );
278           seed_rep->SetSeedDisplayPosition( s, x );
279           curr_hnd->SetEnabled( 1 );
280
281         } // rof
282
283       } // fi
284
285       // Reactivate widget
286       widget->RestartInteraction( );
287       widget->EnabledOn( );
288       widget->Render( );
289
290     } // fi
291   }
292   else if( id == vtkCommand::PlacePointEvent && src_wdg != NULL )
293   {
294     this->m_Widgets.insert( src_wdg );
295     auto rep =
296       dynamic_cast< vtkSeedRepresentation* >(
297         src_wdg->GetRepresentation( )
298         );
299     if( rep != NULL )
300     {
301       unsigned long nSeeds = rep->GetNumberOfSeeds( );
302       if( nSeeds > 0 )
303       {
304         double pos[ 3 ];
305         rep->GetSeedWorldPosition( nSeeds - 1, pos );
306
307         this->m_Seeds->GetPoints( )->InsertNextPoint( pos );
308         this->m_Seeds->GetVerts( )->InsertNextCell( 1 );
309         this->m_Seeds->GetVerts( )->InsertCellPoint(
310           this->m_Seeds->GetPoints( )->GetNumberOfPoints( ) - 1
311           );
312         this->m_Seeds->Modified( );
313
314       } // fi
315
316     } // fi
317
318   } // fi
319 }
320
321 // -------------------------------------------------------------------------
322 void cpPluginsWidgets::SeedWidget::TCallback::
323 SetSeeds( vtkPolyData* seeds )
324 {
325   this->m_Seeds = seeds;
326 }
327
328 // -------------------------------------------------------------------------
329 cpPluginsWidgets::SeedWidget::TCallback::
330 TCallback( )
331   : vtkCommand( ),
332     m_Seeds( NULL )
333 {
334 }
335
336 // -------------------------------------------------------------------------
337 cpPluginsWidgets::SeedWidget::TCallback::
338 ~TCallback( )
339 {
340 }
341
342 // eof - $RCSfile$