]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/Image3DMPR.cxx
...
[cpPlugins.git] / lib / cpExtensions / Visualization / Image3DMPR.cxx
1 #include <cpExtensions/Visualization/Image3DMPR.h>
2
3 #include <vtkCommand.h>
4 #include <vtkInteractorStyleSwitch.h>
5
6 #include <cpExtensions/Visualization/SeedWidgetCorrector.h>
7
8 /**
9  */
10 class cpPlugins_Extensions_Visualization_InteractionModeCallback
11   : public vtkCommand
12 {
13 public:
14   static cpPlugins_Extensions_Visualization_InteractionModeCallback* New( )
15     {
16       return( new cpPlugins_Extensions_Visualization_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 cpExtensions::Visualization::Image3DMPR::
77 Image3DMPR( )
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   typedef
106     cpPlugins_Extensions_Visualization_InteractionModeCallback
107     _TInteractionCB;
108   vtkSmartPointer< _TInteractionCB > int_cb =
109     vtkSmartPointer< _TInteractionCB >::New( );
110   int_cb->WidgetX = this->m_WidgetX;
111   int_cb->WidgetY = this->m_WidgetY;
112   int_cb->WidgetZ = this->m_WidgetZ;
113   int_cb->SeedWidget = this->m_SeedWidget;
114   this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb );
115
116   typedef
117     cpExtensions::Visualization::SeedWidgetCorrector
118     _TCorrector;
119   vtkSmartPointer< _TCorrector > corr_cb =
120     vtkSmartPointer< _TCorrector >::New( );
121   corr_cb->Configure( this->m_Picker );
122   this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb );
123 }
124
125 // -------------------------------------------------------------------------
126 cpExtensions::Visualization::Image3DMPR::
127 ~Image3DMPR( )
128 {
129 }
130
131 // -------------------------------------------------------------------------
132 void cpExtensions::Visualization::Image3DMPR::
133 SetImage( vtkImageData* image )
134 {
135   this->m_Image = image;
136
137   // Outline
138   this->m_Outline->SetBounds( this->m_Image->GetBounds( ) );
139   this->m_OutlineMapper->
140     SetInputConnection( this->m_Outline->GetOutputPort( ) );
141   this->m_OutlineActor->SetMapper( this->m_OutlineMapper );
142   this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
143
144   // Local picker
145   this->m_Picker->SetTolerance( 0.005 );
146
147   // Image planes
148   this->m_WidgetX->DisplayTextOn( );
149   this->m_WidgetX->SetInputData( this->m_Image );
150   this->m_WidgetX->SetPlaneOrientationToXAxes( );
151   this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] );
152   this->m_WidgetX->SetPicker( this->m_Picker );
153   this->m_WidgetX->SetKeyPressActivationValue( 'x' );
154   this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 );
155   this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 );
156
157   this->m_WidgetY->DisplayTextOn( );
158   this->m_WidgetY->SetInputData( this->m_Image );
159   this->m_WidgetY->SetPlaneOrientationToYAxes( );
160   this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] );
161   this->m_WidgetY->SetPicker( this->m_Picker );
162   this->m_WidgetY->SetKeyPressActivationValue( 'y' );
163   this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 );
164   this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 );
165   this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
166
167   this->m_WidgetZ->DisplayTextOn( );
168   this->m_WidgetZ->SetInputData( this->m_Image );
169   this->m_WidgetZ->SetPlaneOrientationToZAxes( );
170   this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] );
171   this->m_WidgetZ->SetPicker( this->m_Picker );
172   this->m_WidgetZ->SetKeyPressActivationValue( 'z' );
173   this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 );
174   this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 );
175   this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) );
176
177   // Rendering stuff
178   this->m_Window->AddRenderer( this->m_Renderer );
179   this->m_Interactor->SetRenderWindow( this->m_Window );
180
181   // Command
182   double spac[ 3 ];
183   this->m_Image->GetSpacing( spac );
184   double min_spacing = spac[ 0 ];
185   for( unsigned int d = 1; d < 3; d++ )
186     min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing;
187
188   vtkInteractorStyleSwitch* iswitch =
189     dynamic_cast< vtkInteractorStyleSwitch* >(
190       this->m_Interactor->GetInteractorStyle( )
191       );
192   if( iswitch != NULL )
193     iswitch->SetCurrentStyleToTrackballCamera( );
194
195   // Add actors
196   this->m_Renderer->AddActor( this->m_OutlineActor );
197
198   // Prepare widgets
199   this->m_WidgetX->SetInteractor( this->m_Interactor );
200   this->m_WidgetY->SetInteractor( this->m_Interactor );
201   this->m_WidgetZ->SetInteractor( this->m_Interactor );
202   this->m_SeedWidget->SetInteractor( this->m_Interactor );
203
204   this->m_WidgetX->On( );
205   this->m_WidgetY->On( );
206   this->m_WidgetZ->On( );
207   this->m_SeedWidget->On( );
208   this->m_SeedWidget->ProcessEventsOff( );
209   this->m_Interactor->SetPicker( this->m_Picker );
210   /*
211     this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker );
212     this->m_Interactor->GetPickingManager( )->EnabledOn( );
213   */
214 }
215
216 // -------------------------------------------------------------------------
217 void cpExtensions::Visualization::Image3DMPR::
218 SetBackground( double r, double g, double b )
219 {
220   this->m_Renderer->SetBackground( r, g, b );
221 }
222
223 // -------------------------------------------------------------------------
224 void cpExtensions::Visualization::Image3DMPR::
225 SetSize( unsigned int w, unsigned int h )
226 {
227   this->m_Window->SetSize( w, h );
228 }
229
230 // -------------------------------------------------------------------------
231 void cpExtensions::Visualization::Image3DMPR::
232 SetWindowLevel( double w, double l )
233 {
234   this->m_WidgetX->SetWindowLevel( w, l );
235   this->m_WidgetY->SetWindowLevel( w, l );
236   this->m_WidgetZ->SetWindowLevel( w, l );
237 }
238
239 // -------------------------------------------------------------------------
240 void cpExtensions::Visualization::Image3DMPR::
241 AddPolyData( vtkPolyData* pd, double opacity )
242 {
243   unsigned int i = this->m_PolyDatas.size( );
244
245   this->m_PolyDatas.push_back( pd );
246   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
247   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
248
249   this->m_Mappers[ i ]->SetInputData( pd );
250   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
251   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
252   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
253   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
254   this->m_Renderer->AddActor( this->m_Actors[ i ] );
255 }
256
257 // -------------------------------------------------------------------------
258 void cpExtensions::Visualization::Image3DMPR::
259 AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
260 {
261   unsigned int i = this->m_PolyDatas.size( );
262
263   this->m_PolyDatas.push_back( pd );
264   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
265   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
266
267   this->m_Mappers[ i ]->SetInputData( pd );
268   this->m_Mappers[ i ]->ScalarVisibilityOff( );
269   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
270   this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b );
271   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
272   this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 );
273   this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 );
274   this->m_Renderer->AddActor( this->m_Actors[ i ] );
275 }
276
277 // -------------------------------------------------------------------------
278 void cpExtensions::Visualization::Image3DMPR::
279 AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
280 {
281   unsigned int i = this->m_PolyDatas.size( );
282
283   this->m_PolyDatas.push_back( pd );
284   this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
285   this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
286
287   this->m_Mappers[ i ]->SetInputData( pd );
288   this->m_Mappers[ i ]->SetLookupTable( lut );
289   this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
290   this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
291   this->m_Renderer->AddActor( this->m_Actors[ i ] );
292 }
293
294 // -------------------------------------------------------------------------
295 unsigned int cpExtensions::Visualization::Image3DMPR::
296 GetNumberOfSeeds( ) const
297 {
298   return( this->m_SeedRepresentation->GetNumberOfSeeds( ) );
299 }
300
301 // -------------------------------------------------------------------------
302 void cpExtensions::Visualization::Image3DMPR::
303 GetSeed( int n, double* s ) const
304 {
305   vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n );
306   if( hWdg == NULL )
307     return;
308   vtkHandleRepresentation* hRep =
309     dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) );
310   if( hRep == NULL )
311     return;
312   hRep->GetWorldPosition( s );
313 }
314
315 // -------------------------------------------------------------------------
316 unsigned int cpExtensions::Visualization::Image3DMPR::
317 AddSeed( const double& x, const double& y, const double& z ) const
318 {
319   double pos[ 3 ] = { x, y, z };
320
321   int hnd_id = this->m_SeedRepresentation->CreateHandle( pos );
322   vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( );
323   this->m_SeedRepresentation->GetHandleRepresentation( hnd_id )->
324     SetWorldPosition( pos );
325   hnd->SetEnabled( 1 );
326
327   return( this->GetNumberOfSeeds( ) - 1 );
328 }
329
330 // -------------------------------------------------------------------------
331 vtkRenderWindow* cpExtensions::Visualization::Image3DMPR::
332 GetWindow( ) const
333 {
334   return( this->m_Window );
335 }
336
337 // -------------------------------------------------------------------------
338 vtkRenderer* cpExtensions::Visualization::Image3DMPR::
339 GetRenderer( ) const
340 {
341   return( this->m_Renderer );
342 }
343
344 // -------------------------------------------------------------------------
345 void cpExtensions::Visualization::Image3DMPR::
346 Start( )
347 {
348   this->m_WidgetX->On( );
349   this->m_WidgetY->On( );
350   this->m_WidgetZ->On( );
351
352   this->m_Renderer->ResetCamera( );
353   this->m_Interactor->Initialize( );
354   this->m_Interactor->Start( );
355 }
356
357 // -------------------------------------------------------------------------
358 void cpExtensions::Visualization::Image3DMPR::
359 Render( )
360 {
361   this->m_Window->Render( );
362 }
363
364 // eof - $RCSfile$