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