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