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