]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/ImageMPR.cxx
...
[FrontAlgorithms.git] / lib / fpa / VTK / ImageMPR.cxx
1 #include <fpa/VTK/ImageMPR.h>
2
3 #include <vtkCommand.h>
4 #include <vtkInteractorStyleSwitch.h>
5
6 #include <fpa/VTK/SeedWidgetCorrector.h>
7
8 /**
9  */
10 class fpa_VTK_InteractionModeCallback
11   : public vtkCommand
12 {
13 public:
14   static fpa_VTK_InteractionModeCallback* New( )
15     {
16       return( new fpa_VTK_InteractionModeCallback );
17     }
18   virtual void Execute(
19     vtkObject* caller, unsigned long eventId, void* arguments )
20     {
21       if( eventId == vtkCommand::KeyPressEvent )
22       {
23         vtkRenderWindowInteractor* iren = 
24           static_cast< vtkRenderWindowInteractor* >( caller );
25         if( caller == NULL )
26           return;
27
28         switch( iren->GetKeyCode( ) )
29         {
30         case 'x':
31         case 'X':
32         {
33           bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 );
34           if( eSeed )
35           {
36             this->SeedWidget->ProcessEventsOff( );
37             this->WidgetX->InteractionOn( );
38             this->WidgetY->InteractionOn( );
39             this->WidgetZ->InteractionOn( );
40           }
41           else
42           {
43             this->SeedWidget->ProcessEventsOn( );
44             this->WidgetX->InteractionOff( );
45             this->WidgetY->InteractionOff( );
46             this->WidgetZ->InteractionOff( );
47
48           } // fi
49         }
50         break;
51         case 'z':
52         case 'Z':
53         {
54           this->SeedWidget->ProcessEventsOff( );
55           this->WidgetX->InteractionOff( );
56           this->WidgetY->InteractionOff( );
57           this->WidgetZ->InteractionOff( );
58         }
59         break;
60         default:
61           break;
62
63         } // hctiws
64
65       } // fi
66     }
67
68 public:
69   vtkImagePlaneWidget* WidgetX;
70   vtkImagePlaneWidget* WidgetY;
71   vtkImagePlaneWidget* WidgetZ;
72   vtkSeedWidget*       SeedWidget;
73 };
74
75 // -------------------------------------------------------------------------
76 fpa::VTK::ImageMPR::
77 ImageMPR( )
78 {
79   this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( );
80   this->m_OutlineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
81   this->m_OutlineActor = vtkSmartPointer< vtkActor >::New( );
82   this->m_Picker = vtkSmartPointer< vtkCellPicker >::New( );
83   this->m_WidgetX = vtkSmartPointer< vtkImagePlaneWidget >::New( );
84   this->m_WidgetY = vtkSmartPointer< vtkImagePlaneWidget >::New( );
85   this->m_WidgetZ = vtkSmartPointer< vtkImagePlaneWidget >::New( );
86   this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( );
87   this->m_Window = vtkSmartPointer< vtkRenderWindow >::New( );
88   this->m_Interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( );
89   this->m_SeedHandleRepresentation =
90     vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
91   this->m_SeedRepresentation =
92     vtkSmartPointer< vtkSeedRepresentation >::New( );
93   this->m_SeedWidget = vtkSmartPointer<vtkSeedWidget>::New();
94
95   this->m_SeedHandleRepresentation->GetProperty()->SetColor( 1, 0, 1 );
96   this->m_SeedRepresentation->
97     SetHandleRepresentation( this->m_SeedHandleRepresentation );
98   this->m_SeedWidget->SetRepresentation( this->m_SeedRepresentation );
99
100   this->m_WidgetX->KeyPressActivationOff( );
101   this->m_WidgetY->KeyPressActivationOff( );
102   this->m_WidgetZ->KeyPressActivationOff( );
103   this->m_SeedWidget->KeyPressActivationOff( );
104
105   vtkSmartPointer< fpa_VTK_InteractionModeCallback > int_cb =
106     vtkSmartPointer< fpa_VTK_InteractionModeCallback >::New( );
107   int_cb->WidgetX = this->m_WidgetX;
108   int_cb->WidgetY = this->m_WidgetY;
109   int_cb->WidgetZ = this->m_WidgetZ;
110   int_cb->SeedWidget = this->m_SeedWidget;
111   this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb );
112
113   vtkSmartPointer< fpa::VTK::SeedWidgetCorrector > corr_cb =
114     vtkSmartPointer< fpa::VTK::SeedWidgetCorrector >::New( );
115   corr_cb->Configure( this->m_Picker );
116   this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb );
117 }
118
119 // -------------------------------------------------------------------------
120 fpa::VTK::ImageMPR::
121 ~ImageMPR( )
122 {
123 }
124
125 // -------------------------------------------------------------------------
126 void fpa::VTK::ImageMPR::
127 SetImage( vtkImageData* image )
128 {
129   this->m_Image = image;
130
131   // Outline
132   this->m_Outline->SetBounds( this->m_Image->GetBounds( ) );
133   this->m_OutlineMapper->
134     SetInputConnection( this->m_Outline->GetOutputPort( ) );
135   this->m_OutlineActor->SetMapper( this->m_OutlineMapper );
136   this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
137
138   // Local picker
139   this->m_Picker->SetTolerance( 0.005 );
140
141   // Image planes
142   this->m_WidgetX->DisplayTextOn( );
143   this->m_WidgetX->SetInputData( this->m_Image );
144   this->m_WidgetX->SetPlaneOrientationToXAxes( );
145   this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] );
146   this->m_WidgetX->SetPicker( this->m_Picker );
147   this->m_WidgetX->SetKeyPressActivationValue( 'x' );
148   this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 );
149   this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 );
150
151   this->m_WidgetY->DisplayTextOn( );
152   this->m_WidgetY->SetInputData( this->m_Image );
153   this->m_WidgetY->SetPlaneOrientationToYAxes( );
154   this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] );
155   this->m_WidgetY->SetPicker( this->m_Picker );
156   this->m_WidgetY->SetKeyPressActivationValue( 'y' );
157   this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 );
158   this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 );
159   this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
160
161   this->m_WidgetZ->DisplayTextOn( );
162   this->m_WidgetZ->SetInputData( this->m_Image );
163   this->m_WidgetZ->SetPlaneOrientationToZAxes( );
164   this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] );
165   this->m_WidgetZ->SetPicker( this->m_Picker );
166   this->m_WidgetZ->SetKeyPressActivationValue( 'z' );
167   this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 );
168   this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 );
169   this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
170
171   // Rendering stuff
172   this->m_Window->AddRenderer( this->m_Renderer );
173   this->m_Interactor->SetRenderWindow( this->m_Window );
174
175   // Command
176   double spac[ 3 ];
177   this->m_Image->GetSpacing( spac );
178   double min_spacing = spac[ 0 ];
179   for( unsigned int d = 1; d < 3; d++ )
180     min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing;
181
182   vtkInteractorStyleSwitch* iswitch =
183     dynamic_cast< vtkInteractorStyleSwitch* >(
184       this->m_Interactor->GetInteractorStyle( )
185       );
186   if( iswitch != NULL )
187     iswitch->SetCurrentStyleToTrackballCamera( );
188
189   // Add actors
190   this->m_Renderer->AddActor( this->m_OutlineActor );
191
192   // Prepare widgets
193   this->m_WidgetX->SetInteractor( this->m_Interactor );
194   this->m_WidgetY->SetInteractor( this->m_Interactor );
195   this->m_WidgetZ->SetInteractor( this->m_Interactor );
196   this->m_SeedWidget->SetInteractor( this->m_Interactor );
197
198   this->m_WidgetX->On( );
199   this->m_WidgetY->On( );
200   this->m_WidgetZ->On( );
201   this->m_SeedWidget->On( );
202   this->m_SeedWidget->ProcessEventsOff( );
203   this->m_Interactor->SetPicker( this->m_Picker );
204   /*
205     this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker );
206     this->m_Interactor->GetPickingManager( )->EnabledOn( );
207   */
208 }
209
210 // -------------------------------------------------------------------------
211 void fpa::VTK::ImageMPR::
212 SetBackground( double r, double g, double b )
213 {
214   this->m_Renderer->SetBackground( r, g, b );
215 }
216
217 // -------------------------------------------------------------------------
218 void fpa::VTK::ImageMPR::
219 SetSize( unsigned int w, unsigned int h )
220 {
221   this->m_Window->SetSize( w, h );
222 }
223
224 // -------------------------------------------------------------------------
225 void fpa::VTK::ImageMPR::
226 SetWindowLevel( double w, double l )
227 {
228   this->m_WidgetX->SetWindowLevel( w, l );
229   this->m_WidgetY->SetWindowLevel( w, l );
230   this->m_WidgetZ->SetWindowLevel( w, l );
231 }
232
233 // -------------------------------------------------------------------------
234 void fpa::VTK::ImageMPR::
235 AddPolyData( vtkPolyData* pd, double opacity )
236 {
237   unsigned int i = this->m_PolyDatas.size( );
238
239   this->m_PolyDatas.push_back( pd );
240   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
241   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
242
243   this->m_Mappers[ i ]->SetInputData( pd );
244   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
245   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
246   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
247   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
248   this->m_Renderer->AddActor( this->m_Actors[ i ] );
249 }
250
251 // -------------------------------------------------------------------------
252 void fpa::VTK::ImageMPR::
253 AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
254 {
255   unsigned int i = this->m_PolyDatas.size( );
256
257   this->m_PolyDatas.push_back( pd );
258   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
259   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
260
261   this->m_Mappers[ i ]->SetInputData( pd );
262   this->m_Mappers[ i ]->ScalarVisibilityOff( );
263   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
264   this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
265   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
266   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
267   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
268   this->m_Renderer->AddActor( this->m_Actors[ i ] );
269 }
270
271 // -------------------------------------------------------------------------
272 void fpa::VTK::ImageMPR::
273 AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
274 {
275   unsigned int i = this->m_PolyDatas.size( );
276
277   this->m_PolyDatas.push_back( pd );
278   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
279   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
280
281   this->m_Mappers[ i ]->SetInputData( pd );
282   this->m_Mappers[ i ]->SetLookupTable( lut );
283   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
284   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
285   this->m_Renderer->AddActor( this->m_Actors[ i ] );
286 }
287
288 // -------------------------------------------------------------------------
289 unsigned int fpa::VTK::ImageMPR::
290 GetNumberOfSeeds( ) const
291 {
292   return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
293 }
294
295 // -------------------------------------------------------------------------
296 void fpa::VTK::ImageMPR::
297 GetSeed( int n, double* s ) const
298 {
299   vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
300   if( hWdg == NULL )
301     return;
302   vtkHandleRepresentation* hRep =
303     dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
304   if( hRep == NULL )
305     return;
306   hRep->GetWorldPosition( s );
307 }
308
309 // -------------------------------------------------------------------------
310 unsigned int fpa::VTK::ImageMPR::
311 AddSeed( const double& x, const double& y, const double& z ) const
312 {
313   double pos[ 3 ] = { x, y, z };
314
315   int hnd_id = this->m_SeedRepresentation->CreateHandle( pos );
316   vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( );
317   vtkHandleWidget::ComputeWorldToDisplay( this->m_Renderer, x, y, z, pos );
318   this->m_SeedRepresentation->SetSeedDisplayPosition( hnd_id, pos );
319   hnd->SetEnabled( 1 );
320
321   return( this->GetNumberOfSeeds( ) - 1 );
322 }
323
324 // -------------------------------------------------------------------------
325 vtkRenderWindow* fpa::VTK::ImageMPR::
326 GetWindow( ) const
327 {
328   return( this->m_Window );
329 }
330
331 // -------------------------------------------------------------------------
332 vtkRenderer* fpa::VTK::ImageMPR::
333 GetRenderer( ) const
334 {
335   return( this->m_Renderer );
336 }
337
338 // -------------------------------------------------------------------------
339 void fpa::VTK::ImageMPR::
340 Start( )
341 {
342   this->m_WidgetX->On( );
343   this->m_WidgetY->On( );
344   this->m_WidgetZ->On( );
345
346   this->m_Renderer->ResetCamera( );
347   this->m_Interactor->Initialize( );
348   this->m_Interactor->Start( );
349 }
350
351 // -------------------------------------------------------------------------
352 void fpa::VTK::ImageMPR::
353 Render( )
354 {
355   this->m_Window->Render( );
356 }
357
358 // eof - $RCSfile$