]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SeedWidget.cxx
...
[cpPlugins.git] / plugins / Widgets / SeedWidget.cxx
1 #include <plugins/Widgets/SeedWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4
5 #include <vtkCommand.h>
6 #include <vtkImageActor.h>
7 #include <vtkImageData.h>
8 #include <vtkProperty.h>
9 #include <vtkRenderer.h>
10 #include <vtkRendererCollection.h>
11 #include <vtkRenderWindow.h>
12 #include <vtkRenderWindowInteractor.h>
13 #include <cpExtensions/QT/SimpleMPRWidget.h>
14 #include <cpExtensions/Interaction/SeedWidget.h>
15
16 // -------------------------------------------------------------------------
17 // This callback is responsible for changing update time
18 namespace cpPluginsWidgets
19 {
20   /**
21    */
22   class SeedWidgetCallback
23     : public vtkCommand
24   {
25   public:
26     static SeedWidgetCallback* New( )
27       { return( new SeedWidgetCallback ); }
28     virtual void Execute( vtkObject* caller, unsigned long id, void* data )
29       {
30         if( id == vtkCommand::InteractionEvent )
31         {
32           std::cout << id << std::endl;
33           auto actor = dynamic_cast< SeedWidget::TImageActor* >( caller );
34           if( actor != NULL )
35           {
36             int slice = actor->GetSliceNumber( );
37
38             std::cout << slice << std::endl;
39
40           } // fi
41           /* TODO
42              typedef cpPluginsWidgets::SeedWidget::TImageActor _TImageActor;
43           */
44           /* TODO
45              if( this->Data->ActualWidgetId != slice )
46              {
47              this->Data->Widgets[ this->Data->ActualWidgetId ]->EnabledOff( );
48              this->Data->Widgets[ slice ]->EnabledOn( );
49              this->Data->ActualWidgetId = slice;
50              } // fi
51              }
52              else
53           */
54         }
55         else if( id == vtkCommand::PlacePointEvent )
56         {
57           // TODO: id == vtkCommand::CursorChangedEvent ||
58           auto wdg = dynamic_cast< SeedWidget::TWidget* >( caller );
59           if( wdg != NULL )
60           {
61             auto rep =
62               dynamic_cast< vtkSeedRepresentation* >(
63                 wdg->GetRepresentation( )
64                 );
65             if( rep != NULL )
66             {
67               unsigned long nSeeds = rep->GetNumberOfSeeds( );
68               if( nSeeds > 0 )
69               {
70                 double pos[ 3 ];
71                 rep->GetSeedWorldPosition( nSeeds - 1, pos );
72
73                 Seeds->GetPoints( )->InsertNextPoint( pos );
74                 Seeds->GetVerts( )->InsertNextCell( 1 );
75                 Seeds->GetVerts( )->InsertCellPoint( nSeeds - 1 );
76
77               } // fi
78
79             } // fi
80
81           } // fi
82
83         } // fi
84       }
85
86   protected:
87     SeedWidgetCallback( )
88       : vtkCommand( ),
89         Seeds( NULL )
90                       /*
91                         ,
92                         Widget( NULL ),
93                         Data( NULL )
94                       */
95       { }
96     virtual ~SeedWidgetCallback( ) { }
97   public:
98     vtkPolyData* Seeds;
99     /*
100       public:
101       SeedWidget* Widget;
102       SeedWidget::TWidgetData* Data;
103     */
104   };
105
106 } // ecapseman
107
108 // -------------------------------------------------------------------------
109 cpPluginsWidgets::SeedWidget::
110 SeedWidget( )
111   : Superclass( )
112           /* TODO
113              ,
114              m_Configured( false )
115           */
116 {
117   typedef cpPlugins::BaseObjects::DataObject _TData;
118   typedef cpPlugins::DataObjects::Mesh _TMesh;
119
120   // Create ports
121   this->_ConfigureInput< _TData >( "Input", false, false );
122   this->_ConfigureOutput< _TMesh >( "Output" );
123
124   // Create output data
125   auto out = this->_CreateVTK< vtkPolyData >( );
126   out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
127   out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
128   out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
129   out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
130   out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
131   this->GetOutput( "Output" )->SetVTK( out );
132 }
133
134 // -------------------------------------------------------------------------
135 cpPluginsWidgets::SeedWidget::
136 ~SeedWidget( )
137 {
138   for( auto i = this->m_Data.begin( ); i != this->m_Data.end( ); ++i )
139     delete i->second;
140   this->m_Data.clear( );
141 }
142
143 // -------------------------------------------------------------------------
144 void cpPluginsWidgets::SeedWidget::
145 _GenerateData( )
146 {
147   auto image = this->GetInputData< vtkImageData >( "Input" );
148   if( image != NULL )
149     this->_GD0_Image( image );
150   else
151     this->_Error( "Invalid input image." );
152
153   // TODO: std::string init_value = this->m_Parameters.GetString( "Text" );
154
155   /* TODO
156      static vtkPolyData* prev_pdata = NULL;
157      auto pdata = this->_CreateVTK< vtkPolyData >( );
158      if( prev_pdata != pdata )
159      {
160      pdata->SetPoints( vtkPoints::New( ) );
161      pdata->SetVerts( vtkCellArray::New( ) );
162      pdata->SetLines( vtkCellArray::New( ) );
163      pdata->SetPolys( vtkCellArray::New( ) );
164      pdata->SetStrips( vtkCellArray::New( ) );
165      prev_pdata = pdata;
166
167      } // fi
168      auto points = pdata->GetPoints( );
169      auto verts = pdata->GetVerts( );
170
171      if( this->m_Configured )
172      {
173      if( points->GetNumberOfPoints( ) == 0 )
174      {
175      std::stringstream text;
176      bool start = true;
177      for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
178      {
179      for( auto r = ( *w )->Widgets.begin( ); r != ( *w )->Widgets.end( ); ++r )
180      {
181      auto rep =
182      dynamic_cast< vtkSeedRepresentation* >(
183      ( *r )->GetRepresentation( )
184      );
185      if( rep != NULL )
186      {
187      double pos[ 3 ];
188      for( unsigned int i = 0; i < rep->GetNumberOfSeeds( ); ++i )
189      {
190      rep->GetSeedWorldPosition( i, pos );
191      if( !start )
192      text << "#";
193      start = false;
194      text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
195      points->InsertNextPoint( pos );
196
197      } // rof
198
199      } // rof
200      ( *r )->EnabledOff( );
201
202      } // rof
203
204      } // rof
205      this->m_Parameters.SetString( "Text", text.str( ) );
206
207      } // fi
208      }
209      else
210      {
211      auto init_seeds = this->m_Parameters.GetString( "Text" );
212      std::vector< std::string > tokens;
213      cpExtensions::Tokenize( tokens, init_seeds, "#" );
214      for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
215      {
216      std::vector< std::string > coords;
217      cpExtensions::Tokenize( coords, *tIt, " \t" );
218      int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
219      double pos[ 3 ];
220      for( unsigned int d = 0; d < 3; ++d )
221      {
222      pos[ d ] = double( 0 );
223      if( d < dim )
224      {
225      std::istringstream value( coords[ d ] );
226      value >> pos[ d ];
227
228      } // fi
229
230      } // rof
231      verts->InsertNextCell( 1 );
232      verts->InsertCellPoint( points->GetNumberOfPoints( ) );
233      points->InsertNextPoint( pos );
234
235      } // rof
236      this->_Configure( );
237      this->Modified( );
238      this->m_Configured = true;
239
240      } // fi
241      this->GetOutput( "Output" )->SetVTK( pdata );
242   */
243 }
244
245 // -------------------------------------------------------------------------
246 void cpPluginsWidgets::SeedWidget::
247 _Configure( )
248 {
249   /* TODO
250      typedef cpPlugins::DataObjects::Image _TImage;
251
252      auto image = this->GetInput< _TImage >( "Input" );
253      if( image != NULL )
254      {
255      // Update actors
256      auto vtk_image = image->GetVTK< vtkImageData >( );
257      auto iIt = this->m_Interactors.begin( );
258      for( ; iIt != this->m_Interactors.end( ); ++iIt )
259      {
260      auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
261      if( ren != NULL )
262      {
263      auto props = ren->GetViewProps( );
264      if( props != NULL )
265      {
266      props->InitTraversal( );
267      while( vtkProp* prop = props->GetNextProp( ) )
268      {
269      auto actor = dynamic_cast< TImageActor* >( prop );
270      if( actor != NULL )
271      if( actor->GetImage( ) == vtk_image )
272      this->m_Props[ actor ] = *iIt;
273
274      } // elihw
275
276      } // fi
277
278      } // fi
279
280      } // rof
281
282      // Process image
283      if( this->m_Props.size( ) > 0 )
284      {
285      cpPlugins_Demangle_ImageVisualDims( image->GetITK( ), _GD0_Image );
286      else this->_Error( "Invalid input image." );
287      }
288      else
289      this->_Error( "Could not create a valid widget: no actors." );
290      }
291      else
292      this->_Error( "Could not create a valid widget: no input." );
293   */
294 }
295
296 // -------------------------------------------------------------------------
297 void cpPluginsWidgets::SeedWidget::
298 _GD0_Image( vtkImageData* image )
299 {
300   if( this->m_Data.size( ) == 0 )
301   {
302     auto cb = vtkSmartPointer< SeedWidgetCallback >::New( );
303     cb->Seeds = this->GetOutputData< vtkPolyData >( "Output" );
304     this->m_Command = cb;
305
306     for(
307       auto inIt = this->m_Interactors.begin( );
308       inIt != this->m_Interactors.end( );
309       ++inIt
310       )
311     {
312       auto rends = ( *inIt )->GetRenderWindow( )->GetRenderers( );
313       std::set< TImageActor* > all_props;
314       if( rends != NULL )
315       {
316         rends->InitTraversal( );
317         while( vtkRenderer* ren = rends->GetNextItem( ) )
318         {
319           auto props = ren->GetViewProps( );
320           props->InitTraversal( );
321           while( vtkProp* p = props->GetNextProp( ) )
322           {
323             auto image_actor = dynamic_cast< TImageActor* >( p );
324             if( image_actor != NULL )
325             {
326               if( image_actor->GetImage( ) == image )
327                 all_props.insert( image_actor );
328
329             }
330
331           } // elihw
332
333         } // elihw
334
335       } // fi
336       if( all_props.size( ) == 1 )
337       {
338         this->m_Data[ *inIt ] =
339           new TWidgetData(
340             this, *( all_props.begin( ) ), *inIt, this->m_Command
341             );
342
343       } // fi
344
345     } // rof
346   }
347   else
348   {
349     // akjhdaksjhd
350   }
351
352 }
353
354 // -------------------------------------------------------------------------
355 cpPluginsWidgets::SeedWidget::TWidgetData::
356 TWidgetData(
357   SeedWidget* seedWidget,
358   TImageActor* actor,
359   vtkRenderWindowInteractor* iren,
360   vtkCommand* cmd
361   )
362 {
363   auto cb = dynamic_cast< SeedWidgetCallback* >( cmd );
364   actor->AddObserver( vtkCommand::InteractionEvent, cb );
365   /* TODO
366      auto cb = vtkSmartPointer< SeedWidgetCallback >::New( );
367      cb->Widget = seedWidget;
368      cb->Data = this;
369      this->Command = cb;
370   */
371   /* TODO
372      auto image = actor->GetImage( );
373      int ori = actor->GetOrientation( );
374      int ext[ 6 ];
375      image->GetExtent( ext );
376      for( int i = ext[ ori << 1 ]; i <= ext[ ( ori << 1 ) + 1 ]; ++i )
377      {
378   */
379   this->Placer = vtkSmartPointer< _TPlacer >::New( );
380   this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
381   this->Representation = vtkSmartPointer< vtkSeedRepresentation >::New( );
382   this->Widget = vtkSmartPointer< TWidget >::New( );
383
384   this->Placer->SetImageSlice( actor );
385   this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
386   this->Handle->SetPointPlacer( this->Placer );
387   this->Representation->SetHandleRepresentation( this->Handle );
388   this->Widget->SetRepresentation( this->Representation );
389   this->Widget->SetInteractor( iren );
390   if( cb != NULL )
391   {
392     this->Widget->AddObserver( vtkCommand::PlacePointEvent, cb );
393     this->Widget->AddObserver( vtkCommand::CursorChangedEvent, cb );
394   }
395   this->Widget->EnabledOn( );
396
397   /* TODO
398      this->Widgets.push_back( wdg );
399      this->Placers.push_back( placer );
400      this->Handles.push_back( handle );
401      this->Representations.push_back( rep );
402   */
403
404   /* TODO
405      } // rof
406      this->ActualWidgetId = actor->GetSliceNumber( );
407      this->Widgets[ this->ActualWidgetId ]->EnabledOn( );
408   */
409
410   /* TODO
411      auto cb = vtkSmartPointer< SeedWidgetCallback >::New( );
412      cb->Widget = seedWidget;
413      cb->Data = this;
414      this->Command = cb;
415      actor->AddObserver( vtkCommand::InteractionEvent, cb );
416
417      auto image = actor->GetImage( );
418      int ori = actor->GetOrientation( );
419      int ext[ 6 ];
420      image->GetExtent( ext );
421      for( int i = ext[ ori << 1 ]; i <= ext[ ( ori << 1 ) + 1 ]; ++i )
422      {
423      auto placer = vtkSmartPointer< _TPlacer >::New( );
424      auto handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
425      auto rep = vtkSmartPointer< vtkSeedRepresentation >::New( );
426      auto wdg = vtkSmartPointer< TWidget >::New( );
427
428      placer->SetImageSlice( actor );
429      handle->GetProperty( )->SetColor( 1, 0, 0 );
430      handle->SetPointPlacer( placer );
431      rep->SetHandleRepresentation( handle );
432      wdg->SetRepresentation( rep );
433      wdg->SetInteractor( iren );
434      wdg->AddObserver( vtkCommand::PlacePointEvent, cb );
435      wdg->AddObserver( vtkCommand::CursorChangedEvent, cb );
436      wdg->EnabledOff( );
437
438      this->Widgets.push_back( wdg );
439      this->Placers.push_back( placer );
440      this->Handles.push_back( handle );
441      this->Representations.push_back( rep );
442
443      } // rof
444
445      this->ActualWidgetId = actor->GetSliceNumber( );
446      this->Widgets[ this->ActualWidgetId ]->EnabledOn( );
447   */
448 }
449
450 // -------------------------------------------------------------------------
451 cpPluginsWidgets::SeedWidget::TWidgetData::
452 ~TWidgetData( )
453 {
454 }
455
456 // eof - $RCSfile$