]> Creatis software - clitk.git/blob - vv/vvSlicerManagerCommand.cxx
Return 0.0 if libstatgrab is not used
[clitk.git] / vv / vvSlicerManagerCommand.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include "vvSlicerManagerCommand.h"
19 #include "vvSlicerManager.h"
20
21 #include <vtkTextProperty.h>
22 #include <vtkRenderer.h>
23 #include <vtkImageActor.h>
24 #include <vtkRenderWindowInteractor.h>
25 #include <vtkPropPicker.h>
26 #include <vtkCamera.h>
27 #include <vtkImageMapToWindowLevelColors.h>
28 #include <vtkLookupTable.h>
29 #include <vtkMath.h>
30 #include <vtkAbstractPropPicker.h>
31 #include <vtkAssemblyPath.h>
32 #include <vtkCornerAnnotation.h>
33 #include <vtkRenderWindow.h>
34 #include <vvImage.h>
35 #include <vtkImageReslice.h>
36 #include "vvSlicer.h"
37 #include "vvInteractorStyleNavigator.h"
38 #include "vtkTransform.h"
39
40 #include <cmath>
41
42 //------------------------------------------------------------------------------
43 vvSlicerManagerCommand::vvSlicerManagerCommand()
44 {
45   mStartSlicer = -1;
46   mSlicerNumber=-1;
47 }
48 //------------------------------------------------------------------------------
49
50
51 //------------------------------------------------------------------------------
52 //return the num of the current slicer if visible (-1 else)
53 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
54 {
55   int rvalue;
56   if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
57       !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
58     rvalue = -1;
59   else rvalue = mSlicerNumber;
60   //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
61   return rvalue;
62 }
63 //------------------------------------------------------------------------------
64
65
66 //------------------------------------------------------------------------------
67 void vvSlicerManagerCommand::Execute(vtkObject *caller,
68                                      unsigned long event,
69                                      void *vtkNotUsed(callData))
70 {
71   //KeyPress event
72   vvInteractorStyleNavigator *isi =
73     dynamic_cast<vvInteractorStyleNavigator *>(caller);
74   if (isi) {
75     double x = isi->GetInteractor()->GetEventPosition()[0];
76     double y = isi->GetInteractor()->GetEventPosition()[1];
77     double z;
78
79     int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
80     vtkRenderer* renderer=NULL;
81     if (VisibleInWindow>-1)
82       renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
83     newLandmark = false;
84
85
86
87     if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1) {
88       for (int i = 0; i < this->SM->GetNumberOfSlicers(); i++) {
89         if (this->SM->GetSlicer(i)->GetCursorVisibility()) {
90           this->SM->GetSlicer(i)->SetCursorVisibility(0);
91           this->SM->GetSlicer(i)->Render();
92         }
93       }
94     }
95     if ( VisibleInWindow > -1 ) {
96       if (event == vtkCommand::KeyPressEvent) {
97         std::string KeyPress = isi->GetInteractor()->GetKeySym();
98         if (KeyPress == "Tab") {
99           if(isi->GetInteractor()->GetShiftKey())
100             this->SM->PrevImage(VisibleInWindow);
101           else
102             this->SM->NextImage(VisibleInWindow);
103           return;
104         }
105         if (KeyPress == "f" || KeyPress == "F") {
106           FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
107           return;
108         }
109         if (KeyPress == "w") {
110           this->SM->SetLocalColorWindowing(VisibleInWindow);
111           return;
112         }
113         if (KeyPress == "0") {
114           this->SM->SetPreset(0);
115           this->SM->UpdateWindowLevel();
116           return;
117         }
118         if (KeyPress == "1") {
119           this->SM->SetPreset(1);
120           this->SM->UpdateWindowLevel();
121           return;
122         }
123         if (KeyPress == "2") {
124           this->SM->SetPreset(2);
125           this->SM->UpdateWindowLevel();
126
127           return;
128         }
129         if (KeyPress == "3") {
130           this->SM->SetPreset(3);
131           this->SM->UpdateWindowLevel();
132           return;
133         }
134         if (KeyPress == "4") {
135           this->SM->SetPreset(4);
136           this->SM->UpdateWindowLevel();
137           return;
138         }
139         if (KeyPress == "5") {
140           this->SM->SetPreset(5);
141           this->SM->UpdateWindowLevel();
142           return;
143         }
144         if (KeyPress == "6") {
145           this->SM->SetColorMap(0);
146           this->SM->UpdateWindowLevel();
147           return;
148         }
149         if (KeyPress == "7") {
150           this->SM->SetColorMap(1);
151           this->SM->UpdateWindowLevel();
152           return;
153         }
154         if (KeyPress == "8") {
155           this->SM->SetColorMap(2);
156           this->SM->UpdateWindowLevel();
157           return;
158         }
159         if (KeyPress == "9") {
160           this->SM->SetColorMap(3);
161           this->SM->UpdateWindowLevel();
162           return;
163         }
164         if (KeyPress == "equal") { //keycodes are in vtkWin32RenderWindowInteractor
165           this->SM->SetPreset(7);
166           //this->SM->SetColorMap(1);
167           this->SM->UpdateWindowLevel();
168           return;
169         }
170         if (KeyPress == "minus") {
171           this->SM->SetColorWindow(-this->SM->GetColorWindow());
172           this->SM->SetColorMap(-1);
173           this->SM->UpdateWindowLevel();
174           return;
175         }
176         if (KeyPress == "c") {
177           this->SM->ToggleContourSuperposition();
178           this->SM->Render();
179           return;
180         }
181         if (KeyPress == "l") {
182           this->SM->ToggleInterpolation();
183           this->SM->Render();
184           return;
185         }
186         if (KeyPress == "h") {
187           this->SM->SetCursorVisibility(0);
188           this->SM->Render();
189           return;
190         }
191         if (KeyPress == "x") {
192           for(int i=0; i<SM->GetNumberOfSlicers(); i++) {
193             SM->RemoveActor("overlay",0);
194             SM->SetColorMap(0);
195             SM->Render();
196           }
197           return;
198         }
199         if (KeyPress == "u") {
200           this->SM->Reload();
201           this->SM->Render();
202           return;
203         }
204         if (KeyPress == "r" || KeyPress=="R") {
205           this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
206           this->SM->GetSlicer(VisibleInWindow)->Render();
207           this->SM->UpdateLinkedNavigation( this->SM->GetSlicer(VisibleInWindow) );
208           return;
209         }
210         if (KeyPress == "g") {
211           double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
212           this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
213             cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
214           this->SM->UpdateViews(1,VisibleInWindow);
215           this->SM->UpdateLinked(VisibleInWindow);
216           return;
217         }
218         if (KeyPress == "F5") {
219           this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
220           this->SM->GetSlicer(VisibleInWindow)->Render();
221           return;
222         }
223         if (KeyPress == "F6") {
224           this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
225           this->SM->GetSlicer(VisibleInWindow)->Render();
226           return;
227         }
228         if (KeyPress == "Up") {
229           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
230           this->SM->UpdateSlice(VisibleInWindow);
231         }
232         if (KeyPress == "Down") {
233           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
234           this->SM->UpdateSlice(VisibleInWindow);
235         }
236         if (KeyPress == "space") {
237           newLandmark = true;
238         }
239         if (KeyPress == "Left")
240           this->SM->SetPreviousTSlice(VisibleInWindow);
241         if (KeyPress == "Right")
242           this->SM->SetNextTSlice(VisibleInWindow);
243
244         if (KeyPress == "F2") {
245           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
246           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
247           this->SM->UpdateSliceRange(VisibleInWindow);
248         }
249         if (KeyPress == "F3") {
250           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
251           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
252           this->SM->UpdateSliceRange(VisibleInWindow);
253         }
254         if (KeyPress == "F4") {
255           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
256           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
257           this->SM->UpdateSliceRange(VisibleInWindow);
258         }
259
260       }
261
262       //All type of mouse events
263       if (event == vtkCommand::LeaveEvent) {
264         this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
265             -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
266         this->SM->GetSlicer(VisibleInWindow)->Render();
267         return;
268       }
269
270       //DD(event);
271       // Mouse release HERE
272       if (event == vtkCommand::EndPickEvent) {
273         //            DD(VisibleInWindow);
274         if (VisibleInWindow > -1)
275           this->SM->LeftButtonReleaseEvent(VisibleInWindow);
276         return; // no return !!!! ???
277       }
278
279       if (event == vtkCommand::StartWindowLevelEvent) {
280         mStartSlicer = -1;
281         this->InitialWindow = this->SM->GetColorWindow();
282         this->InitialLevel = this->SM->GetColorLevel();
283
284         if (VisibleInWindow > -1) {
285           mStartSlicer = VisibleInWindow;
286         }
287         return;
288       }
289
290       if (event == vtkCommand::EndWindowLevelEvent) {
291         mStartSlicer = -1;
292       }
293
294       if (event == vtkCommand::EndInteractionEvent) {
295         this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
296         return;
297       }
298     }
299     if (VisibleInWindow > -1) {
300       this->SM->Activated();
301       //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
302       this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
303
304       if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey()) {
305         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
306         this->SM->UpdateSlice(VisibleInWindow);
307       } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
308         double factor = 2;
309         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
310         Execute(caller, vtkCommand::EndInteractionEvent, NULL);
311       } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
312         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
313         this->SM->UpdateSlice(VisibleInWindow);
314       } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
315         double factor = -2;
316         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
317         Execute(caller, vtkCommand::EndInteractionEvent, NULL);
318       }
319       double xWorld=0;
320       double yWorld=0;
321       double zWorld=0;
322
323       //Move into World Coordinate
324       renderer->DisplayToNormalizedDisplay(x,y);
325       renderer->NormalizedDisplayToViewport(x,y);
326       renderer->ViewportToNormalizedViewport(x,y);
327       renderer->NormalizedViewportToView(x,y,z);
328       renderer->ViewToWorld(x,y,z);
329       switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
330       case vtkImageViewer2::SLICE_ORIENTATION_XY:
331         xWorld = x;
332         yWorld = y;
333         zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
334                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
335                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
336         break;
337
338       case vtkImageViewer2::SLICE_ORIENTATION_XZ:
339         xWorld = x;
340         yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
341                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
342                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
343         zWorld = z;
344         break;
345
346       case vtkImageViewer2::SLICE_ORIENTATION_YZ:
347         xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
348                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
349                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
350         yWorld = y;
351         zWorld = z;
352         break;
353       }
354       this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
355           this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
356       if (newLandmark) {
357         this->SM->AddLandmark(xWorld,yWorld,zWorld,
358                               this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
359         this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
360         this->SM->Render();
361       }
362       if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
363         this->SM->UpdateViews(1,VisibleInWindow);
364         this->SM->UpdateLinked(VisibleInWindow);
365         this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
366       } else {
367         this->SM->GetSlicer(VisibleInWindow)->Render();
368       }
369       //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
370       //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
371       //this->SM->GetSlicer(VisibleInWindow)->Render();
372     }
373
374     if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
375       this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
376       // Adjust the window level here
377       int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
378       double window = this->InitialWindow;
379       double level = this->InitialLevel;
380       double range[2];
381       this->SM->GetImage()->GetScalarRange(range);
382
383       // Compute normalized delta
384       double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
385                                       isi->GetWindowLevelStartPosition()[0]) / size[0];
386       double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
387                                       isi->GetWindowLevelCurrentPosition()[1]) / size[1];
388       //Window is exponential in nature, use exponential to avoid falling into negative numbers
389       dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
390       dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
391
392       this->SM->SetColorWindow(window*dx);
393       this->SM->SetColorLevel(level-dy);
394       this->SM->SetPreset(6);
395       this->SM->UpdateWindowLevel();
396       this->SM->Render();
397       return;
398     }
399   }
400 }
401 //------------------------------------------------------------------------------
402
403
404 //------------------------------------------------------------------------------
405 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
406 {
407   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
408   vtkRenderer* renderer;
409   if (VisibleInWindow>-1)
410     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
411   else {
412     return;
413   }
414
415   double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
416   double oldPos[3], newPos[3], distance[2];
417   vtkCamera *camera = renderer->GetActiveCamera();
418   camera->GetFocalPoint(viewFocus);
419
420   renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
421   renderer->WorldToDisplay();
422   renderer->GetDisplayPoint(viewFocus);
423
424   focalDepth = viewFocus[2];
425
426   oldPos[0] = renderer->GetCenter()[0];
427   oldPos[1] = renderer->GetCenter()[1];
428   oldPos[2] = focalDepth;
429
430   distance[0] = 1/factor*
431                 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
432   distance[1] = 1/factor*
433                 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
434
435   newPos[0] = interactor->GetEventPosition()[0] - distance[0];
436   newPos[1] = interactor->GetEventPosition()[1] - distance[1];
437   newPos[2] = focalDepth;
438
439   renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
440   renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
441   renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
442   renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
443   renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
444
445   renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
446   renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
447   renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
448   renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
449   renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
450
451   motionVector[0] = newPos[0] - oldPos[0];
452   motionVector[1] = newPos[1] - oldPos[1];
453   motionVector[2] = newPos[2] - oldPos[2];
454
455   camera->GetFocalPoint(viewFocus);
456   camera->GetPosition(viewPoint);
457   camera->SetFocalPoint(motionVector[0] + viewFocus[0],
458                         motionVector[1] + viewFocus[1],
459                         motionVector[2] + viewFocus[2]);
460
461   camera->SetPosition(motionVector[0] + viewPoint[0],
462                       motionVector[1] + viewPoint[1],
463                       motionVector[2] + viewPoint[2]);
464
465   if (camera->GetParallelProjection()) {
466     camera->SetParallelScale(camera->GetParallelScale() / factor);
467   } else {
468     camera->Dolly(factor);
469   }
470
471   if (interactor->GetLightFollowCamera()) {
472     renderer->UpdateLightsGeometryToFollowCamera();
473   }
474   renderer->ResetCameraClippingRange();
475   //interactor->Render();
476 }
477 //------------------------------------------------------------------------------
478
479
480 //------------------------------------------------------------------------------
481 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
482 {
483   double flyFrom[3], flyTo[3];
484   double d[3], focalPt[3], position[3], positionFrom[3];
485   int i, j;
486   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
487   vtkRenderer* renderer=NULL;
488   if (VisibleInWindow>-1)
489     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
490   else
491     return;
492
493   interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
494                                 interactor->GetEventPosition()[1], 0.0,
495                                 renderer);
496
497   vtkAssemblyPath *path=NULL;
498   vtkAbstractPropPicker *picker;
499   if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
500     path = picker->GetPath();
501   }
502   if ( path != NULL ) {
503     flyTo[0] = picker->GetPickPosition()[0];
504     flyTo[1] = picker->GetPickPosition()[1];
505     flyTo[2] = picker->GetPickPosition()[2];
506     renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
507     renderer->GetActiveCamera()->GetPosition(positionFrom);
508
509     switch (slicer->GetSliceOrientation()) {
510     case vtkImageViewer2::SLICE_ORIENTATION_XY:
511       flyTo[2] = flyFrom[2];
512       break;
513
514     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
515       flyTo[1] = flyFrom[1];
516       break;
517
518     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
519       flyTo[0] = flyFrom[0];
520       break;
521     }
522
523
524     for (i=0; i<3; i++) {
525       d[i] = flyTo[i] - flyFrom[i];
526     }
527     double distance = vtkMath::Normalize(d);
528     double delta = distance/15;
529
530     for (i=1; i<=15; i++) {
531       for (j=0; j<3; j++) {
532         focalPt[j] = flyFrom[j] + d[j]*i*delta;
533         position[j] = positionFrom[j] + d[j]*i*delta;
534       }
535       renderer->GetActiveCamera()->SetFocalPoint(focalPt);
536       renderer->GetActiveCamera()->SetPosition(position);
537       renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
538       renderer->ResetCameraClippingRange();
539       interactor->Render();
540     }
541   }
542 }
543 //------------------------------------------------------------------------------