]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/ImageMPR.cxx
1cc4b85f6947b1528e8bed346d46e2edba874ad3
[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 AddPolyData( vtkPolyData* pd, double opacity )
227 {
228   unsigned int i = this->m_PolyDatas.size( );
229
230   this->m_PolyDatas.push_back( pd );
231   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
232   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
233
234   this->m_Mappers[ i ]->SetInputData( pd );
235   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
236   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
237   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
238   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 10 );
239   this->m_Renderer->AddActor( this->m_Actors[ i ] );
240 }
241
242 // -------------------------------------------------------------------------
243 void fpa::VTK::ImageMPR::
244 AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
245 {
246   unsigned int i = this->m_PolyDatas.size( );
247
248   this->m_PolyDatas.push_back( pd );
249   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
250   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
251
252   this->m_Mappers[ i ]->SetInputData( pd );
253   this->m_Mappers[ i ]->ScalarVisibilityOff( );
254   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
255   this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
256   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
257   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
258   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 10 );
259   this->m_Renderer->AddActor( this->m_Actors[ i ] );
260 }
261
262 // -------------------------------------------------------------------------
263 void fpa::VTK::ImageMPR::
264 AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
265 {
266   unsigned int i = this->m_PolyDatas.size( );
267
268   this->m_PolyDatas.push_back( pd );
269   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
270   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
271
272   this->m_Mappers[ i ]->SetInputData( pd );
273   this->m_Mappers[ i ]->SetLookupTable( lut );
274   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
275   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
276   this->m_Renderer->AddActor( this->m_Actors[ i ] );
277 }
278
279
280 // -------------------------------------------------------------------------
281 unsigned int fpa::VTK::ImageMPR::
282 GetNumberOfSeeds( ) const
283 {
284   return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
285 }
286
287 // -------------------------------------------------------------------------
288 void fpa::VTK::ImageMPR::
289 GetSeed( int n, double* s ) const
290 {
291   vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
292   if( hWdg == NULL )
293     return;
294   vtkHandleRepresentation* hRep =
295     dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
296   if( hRep == NULL )
297     return;
298   hRep->GetWorldPosition( s );
299 }
300
301 // -------------------------------------------------------------------------
302 vtkRenderWindow* fpa::VTK::ImageMPR::
303 GetWindow( ) const
304 {
305   return( this->m_Window );
306 }
307
308 // -------------------------------------------------------------------------
309 vtkRenderer* fpa::VTK::ImageMPR::
310 GetRenderer( ) const
311 {
312   return( this->m_Renderer );
313 }
314
315 // -------------------------------------------------------------------------
316 void fpa::VTK::ImageMPR::
317 Start( )
318 {
319   this->m_WidgetX->On( );
320   this->m_WidgetY->On( );
321   this->m_WidgetZ->On( );
322
323   this->m_Renderer->ResetCamera( );
324   this->m_Interactor->Initialize( );
325   this->m_Interactor->Start( );
326 }
327
328 // -------------------------------------------------------------------------
329 void fpa::VTK::ImageMPR::
330 Render( )
331 {
332   this->m_Window->Render( );
333 }
334
335 // eof - $RCSfile$