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