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