]> Creatis software - clitk.git/blob - vv/vvSlicerManagerCommand.cxx
h now hides corner annotations as well
[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)->SetCornerAnnotationVisibility(0);
92           this->SM->GetSlicer(i)->Render();
93         }
94       }
95     }
96     if ( VisibleInWindow > -1 ) {
97       if (event == vtkCommand::KeyPressEvent) {
98         std::string KeyPress = isi->GetInteractor()->GetKeySym();
99         if (KeyPress == "Tab") {
100           if(isi->GetInteractor()->GetShiftKey())
101             this->SM->PrevImage(VisibleInWindow);
102           else
103             this->SM->NextImage(VisibleInWindow);
104           return;
105         }
106         if (KeyPress == "f" || KeyPress == "F") {
107           FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
108           return;
109         }
110         if (KeyPress == "w") {
111           this->SM->SetLocalColorWindowing(VisibleInWindow);
112           return;
113         }
114         if (KeyPress == "0") {
115           this->SM->SetPreset(0);
116           this->SM->UpdateWindowLevel();
117           return;
118         }
119         if (KeyPress == "1") {
120           this->SM->SetPreset(1);
121           this->SM->UpdateWindowLevel();
122           return;
123         }
124         if (KeyPress == "2") {
125           this->SM->SetPreset(2);
126           this->SM->UpdateWindowLevel();
127
128           return;
129         }
130         if (KeyPress == "3") {
131           this->SM->SetPreset(3);
132           this->SM->UpdateWindowLevel();
133           return;
134         }
135         if (KeyPress == "4") {
136           this->SM->SetPreset(4);
137           this->SM->UpdateWindowLevel();
138           return;
139         }
140         if (KeyPress == "5") {
141           this->SM->SetPreset(5);
142           this->SM->UpdateWindowLevel();
143           return;
144         }
145         if (KeyPress == "6") {
146           this->SM->SetColorMap(0);
147           this->SM->UpdateWindowLevel();
148           return;
149         }
150         if (KeyPress == "7") {
151           this->SM->SetColorMap(1);
152           this->SM->UpdateWindowLevel();
153           return;
154         }
155         if (KeyPress == "8") {
156           this->SM->SetColorMap(2);
157           this->SM->UpdateWindowLevel();
158           return;
159         }
160         if (KeyPress == "9") {
161           this->SM->SetColorMap(3);
162           this->SM->UpdateWindowLevel();
163           return;
164         }
165         if (KeyPress == "equal") { //keycodes are in vtkWin32RenderWindowInteractor
166           this->SM->SetPreset(7);
167           //this->SM->SetColorMap(1);
168           this->SM->UpdateWindowLevel();
169           return;
170         }
171         if (KeyPress == "minus") {
172           this->SM->SetColorWindow(-this->SM->GetColorWindow());
173           this->SM->SetColorMap(-1);
174           this->SM->UpdateWindowLevel();
175           return;
176         }
177         if (KeyPress == "c") {
178           this->SM->ToggleContourSuperposition();
179           this->SM->Render();
180           return;
181         }
182         if (KeyPress == "l") {
183           this->SM->ToggleInterpolation();
184           this->SM->Render();
185           return;
186         }
187         if (KeyPress == "h") {
188           this->SM->SetCursorAndCornerAnnotationVisibility(0);
189           this->SM->Render();
190           return;
191         }
192         if (KeyPress == "x") {
193           for(int i=0; i<SM->GetNumberOfSlicers(); i++) {
194             SM->RemoveActor("overlay",0);
195             SM->SetColorMap(0);
196             SM->Render();
197           }
198           return;
199         }
200         if (KeyPress == "u") {
201           this->SM->Reload();
202           this->SM->Render();
203           return;
204         }
205         if (KeyPress == "r" || KeyPress=="R") {
206           this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
207           this->SM->GetSlicer(VisibleInWindow)->Render();
208           this->SM->UpdateLinkedNavigation( this->SM->GetSlicer(VisibleInWindow) );
209           return;
210         }
211         if (KeyPress == "g") {
212           double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
213           this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
214             cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
215           this->SM->UpdateViews(1,VisibleInWindow);
216           this->SM->UpdateLinked(VisibleInWindow);
217           return;
218         }
219         if (KeyPress == "F5") {
220           this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
221           this->SM->GetSlicer(VisibleInWindow)->Render();
222           return;
223         }
224         if (KeyPress == "F6") {
225           this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
226           this->SM->GetSlicer(VisibleInWindow)->Render();
227           return;
228         }
229         if (KeyPress == "Up") {
230           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
231           this->SM->UpdateSlice(VisibleInWindow);
232         }
233         if (KeyPress == "Down") {
234           this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
235           this->SM->UpdateSlice(VisibleInWindow);
236         }
237         if (KeyPress == "space") {
238           newLandmark = true;
239         }
240         if (KeyPress == "Left")
241           this->SM->SetPreviousTSlice(VisibleInWindow);
242         if (KeyPress == "Right")
243           this->SM->SetNextTSlice(VisibleInWindow);
244
245         if (KeyPress == "F2") {
246           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
247           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
248           this->SM->UpdateSliceRange(VisibleInWindow);
249         }
250         if (KeyPress == "F3") {
251           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
252           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
253           this->SM->UpdateSliceRange(VisibleInWindow);
254         }
255         if (KeyPress == "F4") {
256           this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
257           this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
258           this->SM->UpdateSliceRange(VisibleInWindow);
259         }
260
261       }
262
263       //All type of mouse events
264       if (event == vtkCommand::LeaveEvent) {
265         this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
266             -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
267         this->SM->GetSlicer(VisibleInWindow)->Render();
268         return;
269       }
270
271       //DD(event);
272       // Mouse release HERE
273       if (event == vtkCommand::EndPickEvent) {
274         //            DD(VisibleInWindow);
275         if (VisibleInWindow > -1)
276           this->SM->LeftButtonReleaseEvent(VisibleInWindow);
277         return; // no return !!!! ???
278       }
279
280       if (event == vtkCommand::StartWindowLevelEvent) {
281         mStartSlicer = -1;
282         this->InitialWindow = this->SM->GetColorWindow();
283         this->InitialLevel = this->SM->GetColorLevel();
284
285         if (VisibleInWindow > -1) {
286           mStartSlicer = VisibleInWindow;
287         }
288         return;
289       }
290
291       if (event == vtkCommand::EndWindowLevelEvent) {
292         mStartSlicer = -1;
293       }
294
295       if (event == vtkCommand::EndInteractionEvent) {
296         this->SM->Picked();
297         this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
298         return;
299       }
300     }
301     if (VisibleInWindow > -1) {
302       this->SM->Activated();
303       //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
304       this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
305
306       if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey()) {
307         this->SM->Picked();
308         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
309         this->SM->UpdateSlice(VisibleInWindow);
310       } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
311         double factor = 2;
312         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
313         Execute(caller, vtkCommand::EndInteractionEvent, NULL);
314       } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
315         this->SM->Picked();
316         this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
317         this->SM->UpdateSlice(VisibleInWindow);
318       } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
319         double factor = -2;
320         this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
321         Execute(caller, vtkCommand::EndInteractionEvent, NULL);
322       }
323       double xWorld=0;
324       double yWorld=0;
325       double zWorld=0;
326
327       //Move into World Coordinate
328       renderer->DisplayToNormalizedDisplay(x,y);
329       renderer->NormalizedDisplayToViewport(x,y);
330       renderer->ViewportToNormalizedViewport(x,y);
331       renderer->NormalizedViewportToView(x,y,z);
332       renderer->ViewToWorld(x,y,z);
333       switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
334       case vtkImageViewer2::SLICE_ORIENTATION_XY:
335         xWorld = x;
336         yWorld = y;
337         zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
338                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
339                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
340         break;
341
342       case vtkImageViewer2::SLICE_ORIENTATION_XZ:
343         xWorld = x;
344         yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
345                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
346                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
347         zWorld = z;
348         break;
349
350       case vtkImageViewer2::SLICE_ORIENTATION_YZ:
351         xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
352                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
353                  this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
354         yWorld = y;
355         zWorld = z;
356         break;
357       }
358       this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
359           this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
360       if (newLandmark) {
361         this->SM->AddLandmark(xWorld,yWorld,zWorld,
362                               this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
363         this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
364         this->SM->Render();
365       }
366       if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
367         this->SM->Picked();
368         this->SM->UpdateViews(1,VisibleInWindow);
369         this->SM->UpdateLinked(VisibleInWindow);
370         this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
371       } else {
372         this->SM->GetSlicer(VisibleInWindow)->Render();
373       }
374       //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
375       //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
376       //this->SM->GetSlicer(VisibleInWindow)->Render();
377     }
378
379     if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
380       this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
381       // Adjust the window level here
382       int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
383       double window = this->InitialWindow;
384       double level = this->InitialLevel;
385       double range[2];
386       this->SM->GetImage()->GetScalarRange(range);
387
388       // Compute normalized delta
389       double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
390                                       isi->GetWindowLevelStartPosition()[0]) / size[0];
391       double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
392                                       isi->GetWindowLevelCurrentPosition()[1]) / size[1];
393       //Window is exponential in nature, use exponential to avoid falling into negative numbers
394       dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
395       dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
396
397       this->SM->SetColorWindow(window*dx);
398       this->SM->SetColorLevel(level-dy);
399       this->SM->SetPreset(6);
400       this->SM->UpdateWindowLevel();
401       this->SM->Render();
402       return;
403     }
404   }
405 }
406 //------------------------------------------------------------------------------
407
408
409 //------------------------------------------------------------------------------
410 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
411 {
412   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
413   vtkRenderer* renderer;
414   if (VisibleInWindow>-1)
415     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
416   else {
417     return;
418   }
419
420   double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
421   double oldPos[3], newPos[3], distance[2];
422   vtkCamera *camera = renderer->GetActiveCamera();
423   camera->GetFocalPoint(viewFocus);
424
425   renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
426   renderer->WorldToDisplay();
427   renderer->GetDisplayPoint(viewFocus);
428
429   focalDepth = viewFocus[2];
430
431   oldPos[0] = renderer->GetCenter()[0];
432   oldPos[1] = renderer->GetCenter()[1];
433   oldPos[2] = focalDepth;
434
435   distance[0] = 1/factor*
436                 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
437   distance[1] = 1/factor*
438                 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
439
440   newPos[0] = interactor->GetEventPosition()[0] - distance[0];
441   newPos[1] = interactor->GetEventPosition()[1] - distance[1];
442   newPos[2] = focalDepth;
443
444   renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
445   renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
446   renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
447   renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
448   renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
449
450   renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
451   renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
452   renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
453   renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
454   renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
455
456   motionVector[0] = newPos[0] - oldPos[0];
457   motionVector[1] = newPos[1] - oldPos[1];
458   motionVector[2] = newPos[2] - oldPos[2];
459
460   camera->GetFocalPoint(viewFocus);
461   camera->GetPosition(viewPoint);
462   camera->SetFocalPoint(motionVector[0] + viewFocus[0],
463                         motionVector[1] + viewFocus[1],
464                         motionVector[2] + viewFocus[2]);
465
466   camera->SetPosition(motionVector[0] + viewPoint[0],
467                       motionVector[1] + viewPoint[1],
468                       motionVector[2] + viewPoint[2]);
469
470   if (camera->GetParallelProjection()) {
471     camera->SetParallelScale(camera->GetParallelScale() / factor);
472   } else {
473     camera->Dolly(factor);
474   }
475
476   if (interactor->GetLightFollowCamera()) {
477     renderer->UpdateLightsGeometryToFollowCamera();
478   }
479   renderer->ResetCameraClippingRange();
480   //interactor->Render();
481 }
482 //------------------------------------------------------------------------------
483
484
485 //------------------------------------------------------------------------------
486 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
487 {
488   double flyFrom[3], flyTo[3];
489   double d[3], focalPt[3], position[3], positionFrom[3];
490   int i, j;
491   int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
492   vtkRenderer* renderer=NULL;
493   if (VisibleInWindow>-1)
494     renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
495   else
496     return;
497
498   interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
499                                 interactor->GetEventPosition()[1], 0.0,
500                                 renderer);
501
502   vtkAssemblyPath *path=NULL;
503   vtkAbstractPropPicker *picker;
504   if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
505     path = picker->GetPath();
506   }
507   if ( path != NULL ) {
508     flyTo[0] = picker->GetPickPosition()[0];
509     flyTo[1] = picker->GetPickPosition()[1];
510     flyTo[2] = picker->GetPickPosition()[2];
511     renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
512     renderer->GetActiveCamera()->GetPosition(positionFrom);
513
514     switch (slicer->GetSliceOrientation()) {
515     case vtkImageViewer2::SLICE_ORIENTATION_XY:
516       flyTo[2] = flyFrom[2];
517       break;
518
519     case vtkImageViewer2::SLICE_ORIENTATION_XZ:
520       flyTo[1] = flyFrom[1];
521       break;
522
523     case vtkImageViewer2::SLICE_ORIENTATION_YZ:
524       flyTo[0] = flyFrom[0];
525       break;
526     }
527
528
529     for (i=0; i<3; i++) {
530       d[i] = flyTo[i] - flyFrom[i];
531     }
532     double distance = vtkMath::Normalize(d);
533     double delta = distance/15;
534
535     for (i=1; i<=15; i++) {
536       for (j=0; j<3; j++) {
537         focalPt[j] = flyFrom[j] + d[j]*i*delta;
538         position[j] = positionFrom[j] + d[j]*i*delta;
539       }
540       renderer->GetActiveCamera()->SetFocalPoint(focalPt);
541       renderer->GetActiveCamera()->SetPosition(position);
542       renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
543       renderer->ResetCameraClippingRange();
544       interactor->Render();
545     }
546   }
547 }
548 //------------------------------------------------------------------------------