]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SplineWidget.cxx
74680df762f5dd83805514c76f8f117dcaac4be4
[cpPlugins.git] / plugins / Widgets / SplineWidget.cxx
1 #include <plugins/Widgets/SplineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
5
6 #include <vtkImageData.h>
7 #include <vtkRenderer.h>
8 #include <vtkSplineWidget.h>
9
10 // -------------------------------------------------------------------------
11 cpPluginsWidgets::SplineWidget::
12 SplineWidget( )
13   : Superclass( ),
14     m_Configured( false )
15 {
16   typedef cpPlugins::BaseObjects::DataObject _TData;
17   typedef cpPlugins::DataObjects::Mesh       _TMesh;
18
19   this->_ConfigureInput< _TData >( "Input", false, false );
20   this->_ConfigureOutput< _TMesh >( "Output" );
21   this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
22   this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
23   this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
24   this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
25   this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
26   this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
27   this->GetOutput( "Output" )->SetVTK( this->m_Contour );
28 }
29
30 // -------------------------------------------------------------------------
31 cpPluginsWidgets::SplineWidget::
32 ~SplineWidget( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 void cpPluginsWidgets::SplineWidget::
38 _GenerateData( )
39 {
40   typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
41
42   auto image = this->GetInputData< vtkImageData >( "Input" );
43   if( image == NULL )
44     this->_Error( "Invalid input image." );
45   if( this->m_Interactors.size( ) == 0 )
46     this->_Error( "Give at least one interactor." );
47
48   auto wdg = this->_CreateVTK< vtkSplineWidget >( );
49   if( this->m_Configured )
50   {
51     wdg->GetPolyData( this->m_Contour.GetPointer( ) );
52     wdg->Off( );
53   }
54   else
55   {
56     auto iIt = this->m_Interactors.begin( );
57     vtkRenderWindowInteractor* iren = NULL;
58     vtkRenderer* ren = NULL;
59     _TActor* actor = NULL;
60     for( ; iIt != this->m_Interactors.end( ); ++iIt )
61     {
62       auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
63       if( r != NULL )
64       {
65         auto props = r->GetViewProps( );
66         if( props != NULL )
67         {
68           props->InitTraversal( );
69           while( vtkProp* prop = props->GetNextProp( ) )
70           {
71             auto a = dynamic_cast< _TActor* >( prop );
72             if( a != NULL )
73               if( a->GetImage( ) == image )
74               {
75                 iren = *iIt;
76                 actor = a;
77                 ren = r;
78
79               } // fi
80
81           } // elihw
82
83         } // fi
84
85       } // fi
86
87     } // rof
88     if( actor == NULL || ren == NULL || iren == NULL )
89       this->_Error( "Invalid actor and/or renderer." );
90
91     // Widget configuration
92     wdg->SetCurrentRenderer( ren );
93     wdg->SetDefaultRenderer( ren );
94     wdg->SetInputData( image );
95     wdg->SetProp3D( actor );
96     wdg->SetInteractor( iren );
97     double bnds[ 6 ];
98     image->GetBounds( bnds );
99     wdg->PlaceWidget(
100       bnds[ 0 ], bnds[ 1 ],
101       bnds[ 2 ], bnds[ 3 ],
102       bnds[ 4 ], bnds[ 5 ]
103       );
104     wdg->ProjectToPlaneOn( );
105     wdg->SetProjectionNormalToZAxes( );
106     wdg->SetProjectionPosition(
107       (
108         actor->GetBounds( )[ 4 ] +
109         actor->GetBounds( )[ 5 ]
110         ) / double( 2 )
111       );
112     wdg->SetHandleSize( 0.005 );
113     wdg->SetNumberOfHandles( 3 );
114     wdg->On( );
115     this->m_Configured = true;
116
117   } // fi
118
119   /* TODO
120
121      if( this->m_Configured )
122      {
123      if( points->GetNumberOfPoints( ) == 0 )
124      {
125      std::stringstream text;
126      bool start = true;
127      for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
128      {
129      for( auto r = ( *w )->Widgets.begin( ); r != ( *w )->Widgets.end( ); ++r )
130      {
131      auto rep =
132      dynamic_cast< vtkSeedRepresentation* >(
133      ( *r )->GetRepresentation( )
134      );
135      if( rep != NULL )
136      {
137      double pos[ 3 ];
138      for( unsigned int i = 0; i < rep->GetNumberOfSeeds( ); ++i )
139      {
140      rep->GetSeedWorldPosition( i, pos );
141      if( !start )
142      text << "#";
143      start = false;
144      text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
145      points->InsertNextPoint( pos );
146
147      } // rof
148
149      } // rof
150      ( *r )->EnabledOff( );
151
152      } // rof
153
154      } // rof
155      this->m_Parameters.SetString( "Text", text.str( ) );
156
157      } // fi
158      }
159      else
160      {
161      auto init_seeds = this->m_Parameters.GetString( "Text" );
162      std::vector< std::string > tokens;
163      cpExtensions::Tokenize( tokens, init_seeds, "#" );
164      for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
165      {
166      std::vector< std::string > coords;
167      cpExtensions::Tokenize( coords, *tIt, " \t" );
168      int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
169      double pos[ 3 ];
170      for( unsigned int d = 0; d < 3; ++d )
171      {
172      pos[ d ] = double( 0 );
173      if( d < dim )
174      {
175      std::istringstream value( coords[ d ] );
176      value >> pos[ d ];
177
178      } // fi
179
180      } // rof
181      verts->InsertNextCell( 1 );
182      verts->InsertCellPoint( points->GetNumberOfPoints( ) );
183      points->InsertNextPoint( pos );
184
185      } // rof
186      this->_Configure( );
187      this->Modified( );
188      this->m_Configured = true;
189
190      } // fi
191      this->GetOutput( "Output" )->SetVTK( pdata );
192   */
193 }
194
195 // -------------------------------------------------------------------------
196 /* TODO
197 void cpPluginsWidgets::SplineWidget::
198 _Configure( )
199 {
200   typedef cpPlugins::DataObjects::Image _TImage;
201
202   auto image = this->GetInput< _TImage >( "Input" );
203   if( image != NULL )
204   {
205     // Update actors
206     auto vtk_image = image->GetVTK< vtkImageData >( );
207     auto iIt = this->m_Interactors.begin( );
208     for( ; iIt != this->m_Interactors.end( ); ++iIt )
209     {
210       auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
211       if( ren != NULL )
212       {
213         auto props = ren->GetViewProps( );
214         if( props != NULL )
215         {
216           props->InitTraversal( );
217           while( vtkProp* prop = props->GetNextProp( ) )
218           {
219             auto actor = dynamic_cast< TImageActor* >( prop );
220             if( actor != NULL )
221               if( actor->GetImage( ) == vtk_image )
222                 this->m_Props[ actor ] = *iIt;
223
224           } // elihw
225
226         } // fi
227
228       } // fi
229
230     } // rof
231
232     // Process image
233     if( this->m_Props.size( ) > 0 )
234     {
235       cpPlugins_Demangle_ImageVisualDims( image->GetITK( ), _GD0_Image );
236       else this->_Error( "Invalid input image." );
237     }
238     else
239       this->_Error( "Could not create a valid widget: no actors." );
240   }
241   else
242     this->_Error( "Could not create a valid widget: no input." );
243 }
244
245 // -------------------------------------------------------------------------
246 template< class _TImage >
247 void cpPluginsWidgets::SplineWidget::
248 _GD0_Image( _TImage* image )
249 {
250   for( auto p = this->m_Props.begin( ); p != this->m_Props.end( ); ++p )
251   {
252     TWidgetData* d =
253       new TWidgetData(
254         this, dynamic_cast< TImageActor* >( p->first ), p->second
255         );
256     this->m_Widgets.push_back( d );
257
258   } // rof
259 }
260
261 // -------------------------------------------------------------------------
262 cpPluginsWidgets::SplineWidget::TWidgetData::
263 TWidgetData(
264   SplineWidget* seedWidget,
265   TImageActor* actor,
266   vtkRenderWindowInteractor* iren
267   )
268 {
269   auto cb = vtkSmartPointer< SplineWidgetCallback >::New( );
270   cb->Widget = seedWidget;
271   cb->Data = this;
272   this->Command = cb;
273   actor->AddObserver( vtkCommand::InteractionEvent, cb );
274
275   auto image = actor->GetImage( );
276   int ori = actor->GetOrientation( );
277   int ext[ 6 ];
278   image->GetExtent( ext );
279   for( int i = ext[ ori << 1 ]; i <= ext[ ( ori << 1 ) + 1 ]; ++i )
280   {
281     auto placer = vtkSmartPointer< _TPlacer >::New( );
282     auto handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
283     auto rep = vtkSmartPointer< vtkSeedRepresentation >::New( );
284     auto wdg = vtkSmartPointer< _TWidget >::New( );
285
286     placer->SetImageSlice( actor );
287     handle->GetProperty( )->SetColor( 1, 0, 0 );
288     handle->SetPointPlacer( placer );
289     rep->SetHandleRepresentation( handle );
290     wdg->SetRepresentation( rep );
291     wdg->SetInteractor( iren );
292     wdg->AddObserver( vtkCommand::PlacePointEvent, cb );
293     wdg->AddObserver( vtkCommand::CursorChangedEvent, cb );
294     wdg->EnabledOff( );
295
296     this->Widgets.push_back( wdg );
297     this->Placers.push_back( placer );
298     this->Handles.push_back( handle );
299     this->Representations.push_back( rep );
300
301   } // rof
302
303   this->ActualWidgetId = actor->GetSliceNumber( );
304   this->Widgets[ this->ActualWidgetId ]->EnabledOn( );
305 }
306
307 // -------------------------------------------------------------------------
308 cpPluginsWidgets::SplineWidget::TWidgetData::
309 ~TWidgetData( )
310 {
311 }
312 */
313
314 // eof - $RCSfile$