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