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