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