]> Creatis software - clitk.git/blob - vv/vvSlicerManagerCommand.cxx
Added linked navigation (zoom and pan)
[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->NumberOfSlicers(); 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->NumberOfSlicers(); 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           return;
208         }
209         if (KeyPress == "g") {
210           double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
211           this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
212             cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
213           this->SM->UpdateViews(1,VisibleInWindow);
214           this->SM->UpdateLinked(VisibleInWindow);
215           return;
216         }
217         if (KeyPress == "F5") {
218           this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
219           this->SM->GetSlicer(VisibleInWindow)->Render();
220           return;
221         }
222         if (KeyPress == "F6") {
223           this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
224           this->SM->GetSlicer(VisibleInWindow)->Render();
225           return;
226         }
227         if (KeyPress == "Up") {
228           DD("------------ up");
229           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
230           DD("------------ after set slice");
231           this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
232           DD("------------ after UpdateInfoOnCursorPosition");
233           this->SM->UpdateSlice(VisibleInWindow);
234           DD("------------ after updateslice");
235         }
236         if (KeyPress == "Down") {
237           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
238           this->SM->UpdateSlice(VisibleInWindow);
239           this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
240         }
241         if (KeyPress == "space") {
242           newLandmark = true;
243         }
244         if (KeyPress == "Left")
245           this->SM->SetPreviousTSlice(VisibleInWindow);
246         if (KeyPress == "Right")
247           this->SM->SetNextTSlice(VisibleInWindow);
248
249         if (KeyPress == "F2") {
250           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
251           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
252           this->SM->UpdateSliceRange(VisibleInWindow);
253           this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
254         }
255         if (KeyPress == "F3") {
256           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
257           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
258           this->SM->UpdateSliceRange(VisibleInWindow);
259           this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
260         }
261         if (KeyPress == "F4") {
262           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
263           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
264           this->SM->UpdateSliceRange(VisibleInWindow);
265           this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
266         }
267
268       }
269
270       //All type of mouse events
271       if (event == vtkCommand::LeaveEvent) {
272         this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
273             -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
274         this->SM->GetSlicer(VisibleInWindow)->Render();
275         return;
276       }
277
278       //DD(event);
279       // Mouse release HERE
280       if (event == vtkCommand::EndPickEvent) {
281         //            DD(VisibleInWindow);
282         if (VisibleInWindow > -1)
283           this->SM->LeftButtonReleaseEvent(VisibleInWindow);
284         return; // no return !!!! ???
285       }
286
287       if (event == vtkCommand::StartWindowLevelEvent) {
288         mStartSlicer = -1;
289         this->InitialWindow = this->SM->GetColorWindow();
290         this->InitialLevel = this->SM->GetColorLevel();
291
292         if (VisibleInWindow > -1) {
293           mStartSlicer = VisibleInWindow;
294         }
295         return;
296       }
297
298       if (event == vtkCommand::EndWindowLevelEvent) {
299         mStartSlicer = -1;
300       }
301
302       if (event == vtkCommand::EndInteractionEvent) {
303         this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
304         return;
305       }
306     }
307     if (VisibleInWindow > -1) {
308       this->SM->Activated();
309       //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
310       this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
311
312       if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey()) {
313         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
314         this->SM->UpdateSlice(VisibleInWindow);
315         this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
316       } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
317         double factor = 2;
318         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
319       } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
320         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
321         this->SM->UpdateSlice(VisibleInWindow);
322         this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
323       } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
324         double factor = -2;
325         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
326       }
327       double xWorld=0;
328       double yWorld=0;
329       double zWorld=0;
330
331       //Move into World Coordinate
332       renderer->DisplayToNormalizedDisplay(x,y);
333       renderer->NormalizedDisplayToViewport(x,y);
334       renderer->ViewportToNormalizedViewport(x,y);
335       renderer->NormalizedViewportToView(x,y,z);
336       renderer->ViewToWorld(x,y,z);
337       switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
338       case vtkImageViewer2::SLICE_ORIENTATION_XY:
339         xWorld = x;
340         yWorld = y;
341         zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
342                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
343                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
344         break;
345
346       case vtkImageViewer2::SLICE_ORIENTATION_XZ:
347         xWorld = x;
348         yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
349                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
350                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
351         zWorld = z;
352         break;
353
354       case vtkImageViewer2::SLICE_ORIENTATION_YZ:
355         xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
356                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
357                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
358         yWorld = y;
359         zWorld = z;
360         break;
361       }
362       this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
363           this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
364       if (newLandmark) {
365         this->SM->AddLandmark(xWorld,yWorld,zWorld,
366                               this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
367         this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
368         this->SM->Render();
369       }
370       if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
371         this->SM->UpdateViews(1,VisibleInWindow);
372         this->SM->UpdateLinked(VisibleInWindow);
373         this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
374       } else {
375         this->SM->GetSlicer(VisibleInWindow)->Render();
376       }
377       //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
378       //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
379       //this->SM->GetSlicer(VisibleInWindow)->Render();
380     }
381
382     if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
383       this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
384       // Adjust the window level here
385       int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
386       double window = this->InitialWindow;
387       double level = this->InitialLevel;
388       double range[2];
389       this->SM->GetImage()->GetScalarRange(range);
390
391       // Compute normalized delta
392       double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
393                                       isi->GetWindowLevelStartPosition()[0]) / size[0];
394       double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
395                                       isi->GetWindowLevelCurrentPosition()[1]) / size[1];
396       //Window is exponential in nature, use exponential to avoid falling into negative numbers
397       dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
398       dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
399
400       this->SM->SetColorWindow(window*dx);
401       this->SM->SetColorLevel(level-dy);
402       this->SM->SetPreset(6);
403       this->SM->UpdateWindowLevel();
404       this->SM->Render();
405       return;
406     }
407   }
408 }
409 //------------------------------------------------------------------------------
410
411
412 //------------------------------------------------------------------------------
413 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
414 {
415   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
416   vtkRenderer* renderer;
417   if (VisibleInWindow>-1)
418     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
419   else {
420     return;
421   }
422
423   double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
424   double oldPos[3], newPos[3], distance[2];
425   vtkCamera *camera = renderer->GetActiveCamera();
426   camera->GetFocalPoint(viewFocus);
427
428   renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
429   renderer->WorldToDisplay();
430   renderer->GetDisplayPoint(viewFocus);
431
432   focalDepth = viewFocus[2];
433
434   oldPos[0] = renderer->GetCenter()[0];
435   oldPos[1] = renderer->GetCenter()[1];
436   oldPos[2] = focalDepth;
437
438   distance[0] = 1/factor*
439                 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
440   distance[1] = 1/factor*
441                 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
442
443   newPos[0] = interactor->GetEventPosition()[0] - distance[0];
444   newPos[1] = interactor->GetEventPosition()[1] - distance[1];
445   newPos[2] = focalDepth;
446
447   renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
448   renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
449   renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
450   renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
451   renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
452
453   renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
454   renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
455   renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
456   renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
457   renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
458
459   motionVector[0] = newPos[0] - oldPos[0];
460   motionVector[1] = newPos[1] - oldPos[1];
461   motionVector[2] = newPos[2] - oldPos[2];
462
463   camera->GetFocalPoint(viewFocus);
464   camera->GetPosition(viewPoint);
465   camera->SetFocalPoint(motionVector[0] + viewFocus[0],
466                         motionVector[1] + viewFocus[1],
467                         motionVector[2] + viewFocus[2]);
468
469   camera->SetPosition(motionVector[0] + viewPoint[0],
470                       motionVector[1] + viewPoint[1],
471                       motionVector[2] + viewPoint[2]);
472
473   if (camera->GetParallelProjection()) {
474     camera->SetParallelScale(camera->GetParallelScale() / factor);
475   } else {
476     camera->Dolly(factor);
477   }
478
479   if (interactor->GetLightFollowCamera()) {
480     renderer->UpdateLightsGeometryToFollowCamera();
481   }
482   renderer->ResetCameraClippingRange();
483   //interactor->Render();
484 }
485 //------------------------------------------------------------------------------
486
487
488 //------------------------------------------------------------------------------
489 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
490 {
491   double flyFrom[3], flyTo[3];
492   double d[3], focalPt[3], position[3], positionFrom[3];
493   int i, j;
494   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
495   vtkRenderer* renderer=NULL;
496   if (VisibleInWindow>-1)
497     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
498   else
499     return;
500
501   interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
502                                 interactor->GetEventPosition()[1], 0.0,
503                                 renderer);
504
505   vtkAssemblyPath *path=NULL;
506   vtkAbstractPropPicker *picker;
507   if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
508     path = picker->GetPath();
509   }
510   if ( path != NULL ) {
511     flyTo[0] = picker->GetPickPosition()[0];
512     flyTo[1] = picker->GetPickPosition()[1];
513     flyTo[2] = picker->GetPickPosition()[2];
514     renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
515     renderer->GetActiveCamera()->GetPosition(positionFrom);
516
517     switch (slicer->GetSliceOrientation()) {
518     case vtkImageViewer2::SLICE_ORIENTATION_XY:
519       flyTo[2] = flyFrom[2];
520       break;
521
522     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
523       flyTo[1] = flyFrom[1];
524       break;
525
526     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
527       flyTo[0] = flyFrom[0];
528       break;
529     }
530
531
532     for (i=0; i<3; i++) {
533       d[i] = flyTo[i] - flyFrom[i];
534     }
535     double distance = vtkMath::Normalize(d);
536     double delta = distance/15;
537
538     for (i=1; i<=15; i++) {
539       for (j=0; j<3; j++) {
540         focalPt[j] = flyFrom[j] + d[j]*i*delta;
541         position[j] = positionFrom[j] + d[j]*i*delta;
542       }
543       renderer->GetActiveCamera()->SetFocalPoint(focalPt);
544       renderer->GetActiveCamera()->SetPosition(position);
545       renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
546       renderer->ResetCameraClippingRange();
547       interactor->Render();
548     }
549   }
550 }
551 //------------------------------------------------------------------------------