]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Visualization/ImageInteractorStyle.cxx
d017cb2c3a8148f1aac4b43eed5def93db266643
[cpPlugins.git] / lib / cpPlugins / Extensions / Visualization / ImageInteractorStyle.cxx
1 #include <cpPlugins/Extensions/Visualization/ImageInteractorStyle.h>
2
3 #include <vtkAnnotatedCubeActor.h>
4 #include <vtkAxesActor.h>
5 #include <vtkCallbackCommand.h>
6 #include <vtkCamera.h>
7 #include <vtkCommand.h>
8 #include <vtkPropAssembly.h>
9 #include <vtkProperty.h>
10 #include <vtkRendererCollection.h>
11 #include <vtkRenderWindow.h>
12 #include <vtkRenderWindowInteractor.h>
13
14 #include <cpPlugins/Extensions/Visualization/ImageSliceActors.h>
15 #include <cpPlugins/Extensions/Visualization/MPRActors.h>
16
17 // -------------------------------------------------------------------------
18 const int cpPlugins::Extensions::Visualization::
19 ImageInteractorStyle::SliceEvent = vtkCommand::UserEvent + 1;
20
21 // -------------------------------------------------------------------------
22 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
23 Self* cpPlugins::Extensions::Visualization::ImageInteractorStyle::
24 New( )
25 {
26   return( new Self( ) );
27 }
28
29 // -------------------------------------------------------------------------
30 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
31 Configure( ImageSliceActors* slice_actors, MPRActors* mpr_actors )
32 {
33   this->m_SliceActors = slice_actors;
34   this->m_MPRActors = mpr_actors;
35   this->SetModeToNavigation( );
36   this->PropPicker->AddPickList( slice_actors->GetImageActor( ) );
37   this->Modified( );
38 }
39
40 // -------------------------------------------------------------------------
41 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
42 AssociateInteractor( vtkRenderWindowInteractor* interactor )
43 {
44   if( interactor != NULL )
45   {
46     this->AssociatedInteractors.push_back( interactor );
47     this->Modified( );
48
49   } // fi
50 }
51
52 // -------------------------------------------------------------------------
53 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
54 SetModeToNavigation( )
55 {
56   this->Mode = Self::NavigationMode;
57 }
58
59 // -------------------------------------------------------------------------
60 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
61 SetModeToDeformation( )
62 {
63   this->Mode = Self::DeformationMode;
64 }
65
66 // -------------------------------------------------------------------------
67 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
68 SetInteractor( vtkRenderWindowInteractor* interactor, const int& axis )
69 {
70   this->Superclass::SetInteractor( interactor );
71   this->OrientationWidget->SetInteractor( interactor );
72   interactor->SetInteractorStyle( this );
73   if( interactor == NULL )
74     return;
75
76   // TODO: interactor->SetPicker( this->PropPicker );
77
78   // Get camera, avoiding segfaults
79   vtkRenderer* ren =
80     interactor->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
81   if( ren == NULL )
82     return;
83   vtkCamera* cam = ren->GetActiveCamera( );
84   if( cam == NULL )
85     return;
86
87   // Parallel projections are better when displaying 2D images
88   cam->ParallelProjectionOn( );
89   cam->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
90   if( axis == 0 )
91   {
92     cam->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
93     cam->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
94   }
95   else if( axis == 1 )
96   {
97     cam->SetPosition( double( 0 ), double( 1 ), double(  0 ) );
98     cam->SetViewUp  ( double( 0 ), double( 0 ), double( -1 ) );
99   }
100   else // if( axis == 2 )
101   {
102     cam->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
103     cam->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
104
105   } // fi
106   ren->ResetCamera( );
107
108   // Enable 2D orientation widget
109   this->OrientationWidget->SetEnabled( 1 );
110   this->OrientationWidget->InteractiveOff( );
111 }
112
113 // -------------------------------------------------------------------------
114 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
115 OnMouseMove( )
116 {
117   double pos[ 3 ];
118   if( this->CursorMoving && this->m_MPRActors != NULL )
119   {
120     bool picked = this->_PickPosition( pos );
121     if( picked )
122     {
123       for( int i = 0; i < 3; ++i )
124         if( this->m_SliceActors->GetAxis( ) != i )
125           this->m_MPRActors->SetSlice( i, pos[ i ] );
126       this->Interactor->Render( );
127       this->_RenderAssociateInteractors( );
128
129     } // fi
130
131   } // fi
132 }
133
134 // -------------------------------------------------------------------------
135 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
136 OnLeftButtonDown( )
137 {
138   int x = this->Interactor->GetEventPosition( )[ 0 ];
139   int y = this->Interactor->GetEventPosition( )[ 1 ];
140
141   this->FindPokedRenderer( x, y );
142   if( this->CurrentRenderer == NULL )
143     return;
144
145   // Redefine this button to handle window/level
146   this->GrabFocus( this->EventCallbackCommand );
147
148   if( this->Interactor->GetControlKey( ) )
149     this->StartCursorMoving( );
150
151   /* TODO
152      if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
153      {
154      this->WindowLevelStartPosition[0] = x;
155      this->WindowLevelStartPosition[1] = y;
156      this->StartWindowLevel();
157      }
158
159      // If shift is held down, do a rotation
160      else if (this->InteractionMode == VTKIS_IMAGE3D &&
161      this->Interactor->GetShiftKey())
162      {
163      this->StartRotate();
164      }
165
166      // If ctrl is held down in slicing mode, slice the image
167      else if (this->InteractionMode == VTKIS_IMAGE_SLICING &&
168      this->Interactor->GetControlKey())
169      {
170      this->StartSlice();
171      }
172
173      // The rest of the button + key combinations remain the same
174
175      else
176      {
177      this->Superclass::OnLeftButtonDown();
178      }
179   */
180 }
181
182 // -------------------------------------------------------------------------
183 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
184 OnLeftButtonUp( )
185 {
186   if( this->CursorMoving )
187     this->EndCursorMoving( );
188 }
189
190 // -------------------------------------------------------------------------
191 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
192 OnMiddleButtonDown( )
193 {
194   std::cout << "middown" << std::endl;
195 }
196
197 // -------------------------------------------------------------------------
198 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
199 OnMiddleButtonUp( )
200 {
201   std::cout << "midup" << std::endl;
202 }
203
204 // -------------------------------------------------------------------------
205 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
206 OnRightButtonDown( )
207 {
208   std::cout << "ridown" << std::endl;
209 }
210
211 // -------------------------------------------------------------------------
212 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
213 OnRightButtonUp( )
214 {
215   std::cout << "riup" << std::endl;
216 }
217
218 // -------------------------------------------------------------------------
219 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
220 OnMouseWheelForward( )
221 {
222   if( this->m_SliceActors == NULL || this->Interactor == NULL )
223     return;
224   int off = 1;
225   if( this->Interactor->GetShiftKey( ) == 1 )
226     off *= 10;
227   int s = this->m_SliceActors->GetSliceNumber( ) + off;
228   int maxs = this->m_SliceActors->GetSliceNumberMaxValue( );
229   this->m_SliceActors->SetSliceNumber( ( s < maxs )? s: maxs );
230   this->Interactor->Render( );
231   this->_RenderAssociateInteractors( );
232 }
233
234 // -------------------------------------------------------------------------
235 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
236 OnMouseWheelBackward( )
237 {
238   if( this->m_SliceActors == NULL || this->Interactor == NULL )
239     return;
240   int off = 1;
241   if( this->Interactor->GetShiftKey( ) == 1 )
242     off *= 10;
243   int s = this->m_SliceActors->GetSliceNumber( ) - off;
244   int mins = this->m_SliceActors->GetSliceNumberMinValue( );
245   this->m_SliceActors->SetSliceNumber( ( mins < s )? s: mins );
246   this->Interactor->Render( );
247   this->_RenderAssociateInteractors( );
248 }
249
250 // -------------------------------------------------------------------------
251 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
252 OnChar( )
253 {
254   std::cout << "char" << std::endl;
255 }
256
257 // -------------------------------------------------------------------------
258 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
259 Rotate( )
260 {
261   std::cout << "Rotate" << std::endl;
262 }
263
264 // -------------------------------------------------------------------------
265 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
266 Pan( )
267 {
268   std::cout << "pan" << std::endl;
269 }
270
271 // -------------------------------------------------------------------------
272 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
273 Spin( )
274 {
275   std::cout << "spin" << std::endl;
276 }
277
278 // -------------------------------------------------------------------------
279 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
280 Zoom( )
281 {
282   std::cout << "Zoom" << std::endl;
283 }
284
285 // -------------------------------------------------------------------------
286 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
287 WindowLevel( )
288 {
289   std::cout << "wl" << std::endl;
290 }
291
292 // -------------------------------------------------------------------------
293 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
294 Pick( )
295 {
296   std::cout << "Pick" << std::endl;
297 }
298
299 // -------------------------------------------------------------------------
300 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
301 Slice( )
302 {
303   std::cout << "Slice" << std::endl;
304 }
305
306 // -------------------------------------------------------------------------
307 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
308 StartWindowLevel( )
309 {
310   std::cout << "swl" << std::endl;
311 }
312
313 // -------------------------------------------------------------------------
314 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
315 EndWindowLevel( )
316 {
317   std::cout << "ewl" << std::endl;
318 }
319
320 // -------------------------------------------------------------------------
321 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
322 StartPick( )
323 {
324   std::cout << "sp" << std::endl;
325 }
326
327 // -------------------------------------------------------------------------
328 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
329 EndPick( )
330 {
331   std::cout << "ep" << std::endl;
332 }
333
334 // -------------------------------------------------------------------------
335 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
336 StartSlice( )
337 {
338   std::cout << "ss" << std::endl;
339 }
340
341 // -------------------------------------------------------------------------
342 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
343 EndSlice( )
344 {
345   std::cout << "es" << std::endl;
346 }
347
348 // -------------------------------------------------------------------------
349 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
350 StartCursorMoving( )
351 {
352   if( this->CursorMoving )
353     return;
354   this->CursorMoving = true;
355 }
356
357 // -------------------------------------------------------------------------
358 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
359 EndCursorMoving( )
360 {
361   if( !( this->CursorMoving ) )
362     return;
363   this->CursorMoving = false;
364 }
365
366 // -------------------------------------------------------------------------
367 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
368 ImageInteractorStyle( )
369   : Superclass( ),
370     Mode( Self::NavigationMode ),
371     m_SliceActors( NULL ),
372     m_MPRActors( NULL ),
373     CursorMoving( false )
374 {
375   // Orientation marks
376   vtkSmartPointer< vtkAnnotatedCubeActor > cube =
377     vtkSmartPointer< vtkAnnotatedCubeActor >::New( );
378   cube->GetCubeProperty( )->SetColor( 0.9, 0.7, 0.2 );
379   cube->GetTextEdgesProperty( )->SetLineWidth( 1 );
380   cube->GetTextEdgesProperty( )->SetDiffuse( 0 );
381   cube->GetTextEdgesProperty( )->SetAmbient( 1 );
382   cube->GetTextEdgesProperty( )->SetColor( 0.18, 0.28, 0.23 );
383   cube->GetXPlusFaceProperty( )->SetColor( 1, 0, 0 );
384   cube->GetXPlusFaceProperty( )->SetInterpolationToFlat( );
385   cube->GetXMinusFaceProperty( )->SetColor( 1, 0, 0 );
386   cube->GetXMinusFaceProperty( )->SetInterpolationToFlat( );
387   cube->GetYPlusFaceProperty( )->SetColor( 0, 1, 0 );
388   cube->GetYPlusFaceProperty( )->SetInterpolationToFlat( );
389   cube->GetYMinusFaceProperty( )->SetColor( 0, 1, 0 );
390   cube->GetYMinusFaceProperty( )->SetInterpolationToFlat( );
391   cube->GetZPlusFaceProperty( )->SetColor( 0, 0, 1 );
392   cube->GetZPlusFaceProperty( )->SetInterpolationToFlat( );
393   cube->GetZMinusFaceProperty( )->SetColor( 0, 0, 1 );
394   cube->GetZMinusFaceProperty( )->SetInterpolationToFlat( );
395
396   vtkSmartPointer< vtkAxesActor > axes =
397     vtkSmartPointer< vtkAxesActor >::New( );
398   axes->AxisLabelsOff( );
399   axes->SetShaftTypeToCylinder( );
400   axes->SetTotalLength( 2, 2, 2 );
401
402   vtkSmartPointer< vtkPropAssembly > actors =
403     vtkSmartPointer< vtkPropAssembly >::New( );
404   actors->AddPart( cube );
405   actors->AddPart( axes );
406
407   this->OrientationWidget =
408     vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
409   this->OrientationWidget->SetOutlineColor( 0.93, 0.57, 0.13 );
410   this->OrientationWidget->SetOrientationMarker( actors );
411   this->OrientationWidget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
412
413   this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
414   this->PropPicker->PickFromListOn( );
415 }
416
417 // -------------------------------------------------------------------------
418 cpPlugins::Extensions::Visualization::ImageInteractorStyle::
419 ~ImageInteractorStyle( )
420 {
421 }
422
423 // -------------------------------------------------------------------------
424 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
425 _RenderAssociateInteractors( )
426 {
427   std::vector< vtkRenderWindowInteractor* >::iterator rIt =
428     this->AssociatedInteractors.begin( );
429   for( ; rIt != this->AssociatedInteractors.end( ); ++rIt )
430     ( *rIt )->Render( );
431 }
432
433 // -------------------------------------------------------------------------
434 bool cpPlugins::Extensions::Visualization::ImageInteractorStyle::
435 _PickPosition( double pos[ 3 ] )
436 {
437   if( this->m_SliceActors == NULL )
438     return( false );
439
440   double x = double( this->Interactor->GetEventPosition( )[ 0 ] );
441   double y = double( this->Interactor->GetEventPosition( )[ 1 ] );
442   this->FindPokedRenderer( x, y );
443   int success =
444     this->PropPicker->Pick( x, y, double( 0 ), this->CurrentRenderer );
445   if( success == 0 )
446     return( false );
447   this->PropPicker->GetPickPosition( pos );
448   return( true );
449 }
450
451 // -------------------------------------------------------------------------
452 void cpPlugins::Extensions::Visualization::ImageInteractorStyle::
453 _UpdateCursor( )
454 {
455   std::cout << "upcur" << std::endl;
456 }
457
458 // eof - $RCSfile$