]> Creatis software - clitk.git/blob - vv/vvSlicerManagerCommand.cxx
remove antique RCS headers
[clitk.git] / vv / vvSlicerManagerCommand.cxx
1 /*=========================================================================
2
3 Program:   vv
4 Language:  C++
5 Author :   Pierre Seroul (pierre.seroul@gmail.com)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24 #include "vvSlicerManagerCommand.h"
25 #include "vvSlicerManager.h"
26
27 #include "vtkTextProperty.h"
28 #include "vtkRenderer.h"
29 #include "vtkImageActor.h"
30 #include "vtkRenderWindowInteractor.h"
31 #include "vtkPropPicker.h"
32 #include "vtkCamera.h"
33 #include "vtkImageMapToWindowLevelColors.h"
34 #include "vtkLookupTable.h"
35 #include "vtkMath.h"
36 #include "vtkAbstractPropPicker.h"
37 #include "vtkAssemblyPath.h"
38 #include "vtkCornerAnnotation.h"
39 #include <vtkRenderWindow.h>
40
41 #include "vvSlicer.h"
42 #include "vvInteractorStyleNavigator.h"
43
44 #include <cmath>
45
46 vvSlicerManagerCallback::vvSlicerManagerCallback()
47 {
48     mStartSlicer = -1;
49 }
50
51 //return the num of the current slicer if visible (-1 else)
52 int vvSlicerManagerCallback::FindSlicerNumber(vtkRenderWindow* renwin)
53 {
54     for (int i = 0; i < SM->NumberOfSlicers(); i++)
55     {
56         if (SM->GetSlicer(i)->GetRenderWindow() == renwin
57                 && SM->GetSlicer(i)->GetRenderer()->GetDraw())
58         {
59             return i;
60         }
61     }
62     return -1;
63 }
64
65 void vvSlicerManagerCallback::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 == "0")
105                 {
106                     this->SM->SetPreset(0);
107                     this->SM->UpdateWindowLevel();
108                     return;
109                 }
110                 if (KeyPress == "1")
111                 {
112                     this->SM->SetPreset(1);
113                     this->SM->UpdateWindowLevel();
114                     return;
115                 }
116                 if (KeyPress == "2")
117                 {
118                     this->SM->SetPreset(2);
119                     this->SM->UpdateWindowLevel();
120
121                     return;
122                 }
123                 if (KeyPress == "3")
124                 {
125                     this->SM->SetPreset(3);
126                     this->SM->UpdateWindowLevel();
127                     return;
128                 }
129                 if (KeyPress == "4")
130                 {
131                     this->SM->SetPreset(4);
132                     this->SM->UpdateWindowLevel();
133                     return;
134                 }
135                 if (KeyPress == "5")
136                 {
137                     this->SM->SetPreset(5);
138                     this->SM->UpdateWindowLevel();
139                     return;
140                 }
141                 if (KeyPress == "6")
142                 {
143                     this->SM->SetColorMap(0);
144                     this->SM->UpdateWindowLevel();
145                     return;
146                 }
147                 if (KeyPress == "7")
148                 {
149                     this->SM->SetColorMap(1);
150                     this->SM->UpdateWindowLevel();
151                     return;
152                 }
153                 if (KeyPress == "8")
154                 {
155                     this->SM->SetColorMap(2);
156                     this->SM->UpdateWindowLevel();
157                     return;
158                 }
159                 if (KeyPress == "9")
160                 {
161                     this->SM->SetColorMap(3);
162                     this->SM->UpdateWindowLevel();
163                     return;
164                 }
165                 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
166                 {
167                     this->SM->SetPreset(7);
168                     //this->SM->SetColorMap(1);
169                     this->SM->UpdateWindowLevel();
170                     return;
171                 }
172                 if (KeyPress == "minus")
173                 {
174                     std::cout << "KeyPress : - " << std::endl;
175                     this->SM->SetColorWindow(-this->SM->GetColorWindow());
176                     this->SM->SetColorMap(-1);
177                     this->SM->UpdateWindowLevel();
178                     return;
179                 }
180                 if (KeyPress == "u")
181                 {
182                     this->SM->ToggleContourSuperposition();
183                     this->SM->Render();
184                     return;
185                 }
186                 if (KeyPress == "i")
187                 {
188                     this->SM->ToggleInterpolation();
189                     this->SM->Render();
190                     return;
191                 }
192                 if (KeyPress == "h")
193                 {
194                     this->SM->SetCursorVisibility(0);
195                     this->SM->Render();
196                     return;
197                 }
198                 if (KeyPress == "l")
199                 {
200                     this->SM->Reload();
201                     this->SM->Render();
202                     return;
203                 }
204                 if (KeyPress == "r" or KeyPress=="R")
205                 {
206                     this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
207                     this->SM->GetSlicer(VisibleInWindow)->Render();
208                     return;
209                 }
210                 if (KeyPress == "g")
211                 {
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                 {
221                     this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
222                     this->SM->GetSlicer(VisibleInWindow)->Render();
223                     return;
224                 }
225                 if (KeyPress == "F6")
226                 {
227                     this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
228                     this->SM->GetSlicer(VisibleInWindow)->Render();
229                     return;
230                 }
231                 if (KeyPress == "Up")
232                 {
233                     this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
234                     this->SM->UpdateSlice(VisibleInWindow);
235                     this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
236                 }
237                 if (KeyPress == "Down")
238                 {
239                     this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
240                     this->SM->UpdateSlice(VisibleInWindow);
241                     this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
242                 }
243                 if (KeyPress == "space")
244                 {
245                     newLandmark = true;
246                 }
247                 if (KeyPress == "Left")
248                     this->SM->SetPreviousTSlice(VisibleInWindow);
249                 if (KeyPress == "Right")
250                     this->SM->SetNextTSlice(VisibleInWindow);
251
252                 if (KeyPress == "F1")
253                 {
254                     this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
255                     this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
256                     this->SM->UpdateSliceRange(VisibleInWindow);
257                     this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
258                 }
259                 if (KeyPress == "F2")
260                 {
261                     this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
262                     this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
263                     this->SM->UpdateSliceRange(VisibleInWindow);
264                     this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
265                 }
266                 if (KeyPress == "F3")
267                 {
268                     this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
269                     this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
270                     this->SM->UpdateSliceRange(VisibleInWindow);
271                     this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
272                 }
273             }
274
275             //All type of mouse events
276             if (event == vtkCommand::LeaveEvent)
277             {
278                 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
279                         -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
280                 this->SM->GetSlicer(VisibleInWindow)->Render();
281                 return;
282             }
283
284             if (event == vtkCommand::StartWindowLevelEvent)
285             {
286                 mStartSlicer = -1;
287                 this->InitialWindow = this->SM->GetColorWindow();
288                 this->InitialLevel = this->SM->GetColorLevel();
289
290                 if (VisibleInWindow > -1)
291                 {
292                     mStartSlicer = VisibleInWindow;
293                 }
294                 return;
295             }
296
297             if (event == vtkCommand::EndWindowLevelEvent)
298             {
299                 mStartSlicer = -1;
300             }
301
302         }
303         if (VisibleInWindow > -1)
304         {
305             this->SM->Activated();
306             //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
307             this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
308
309             if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
310             {
311                 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
312                 this->SM->UpdateSlice(VisibleInWindow);
313                 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
314             }
315             else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
316             {
317                 double factor = 2;
318                 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
319             }
320             else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
321             {
322                 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
323                 this->SM->UpdateSlice(VisibleInWindow);
324                 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
325             }
326             else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
327             {
328                 double factor = -2;
329                 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
330             }
331             double xWorld=0; double yWorld=0; double zWorld=0;
332
333             //Move into World Coordinate
334             renderer->DisplayToNormalizedDisplay(x,y);
335             renderer->NormalizedDisplayToViewport(x,y);
336             renderer->ViewportToNormalizedViewport(x,y);
337             renderer->NormalizedViewportToView(x,y,z);
338             renderer->ViewToWorld(x,y,z);
339             switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
340             {
341             case vtkImageViewer2::SLICE_ORIENTATION_XY:
342                 xWorld = x;
343                 yWorld = y;
344                 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
345                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
346                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
347                 break;
348
349             case vtkImageViewer2::SLICE_ORIENTATION_XZ:
350                 xWorld = x;
351                 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
352                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
353                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
354                 zWorld = z;
355                 break;
356
357             case vtkImageViewer2::SLICE_ORIENTATION_YZ:
358                 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
359                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
360                          this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
361                 yWorld = y;
362                 zWorld = z;
363                 break;
364             }
365             this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
366                     this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
367             if (newLandmark)
368             {
369                 this->SM->AddLandmark(xWorld,yWorld,zWorld,
370                                       this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
371                 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
372                 this->SM->Render();
373             }
374             if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
375             {
376                 this->SM->UpdateViews(1,VisibleInWindow);
377                 this->SM->UpdateLinked(VisibleInWindow);
378                 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
379             }
380             else
381             {
382                 this->SM->GetSlicer(VisibleInWindow)->Render();
383             }
384             //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
385                     //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
386             //this->SM->GetSlicer(VisibleInWindow)->Render();
387         }
388
389         if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
390         {
391             this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
392             // Adjust the window level here
393             int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
394             double window = this->InitialWindow;
395             double level = this->InitialLevel;
396             double range[2];
397             this->SM->GetImage()->GetScalarRange(range);
398
399             // Compute normalized delta
400             double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
401                          isi->GetWindowLevelStartPosition()[0]) / size[0];
402             double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
403                          isi->GetWindowLevelCurrentPosition()[1]) / size[1];
404             //Window is exponential in nature, use exponential to avoid falling into negative numbers
405             dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
406             dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
407
408             this->SM->SetColorWindow(window*dx);
409             this->SM->SetColorLevel(level-dy);
410             this->SM->SetPreset(6);
411             this->SM->UpdateWindowLevel();
412             this->SM->Render();
413             return;
414         }
415     }
416 }
417
418 void vvSlicerManagerCallback::Dolly(double factor, vtkRenderWindowInteractor *interactor)
419 {
420     int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
421     vtkRenderer* renderer;
422     if (VisibleInWindow>-1) 
423         renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
424     else
425     {
426         return;
427     }
428
429     double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
430     double oldPos[3], newPos[3], distance[2];
431     vtkCamera *camera = renderer->GetActiveCamera();
432     camera->GetFocalPoint(viewFocus);
433
434     renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
435     renderer->WorldToDisplay();
436     renderer->GetDisplayPoint(viewFocus);
437
438     focalDepth = viewFocus[2];
439
440     oldPos[0] = renderer->GetCenter()[0];
441     oldPos[1] = renderer->GetCenter()[1];
442     oldPos[2] = focalDepth;
443
444     distance[0] = 1/factor*
445                   (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
446     distance[1] = 1/factor*
447                   (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
448
449     newPos[0] = interactor->GetEventPosition()[0] - distance[0];
450     newPos[1] = interactor->GetEventPosition()[1] - distance[1];
451     newPos[2] = focalDepth;
452
453     renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
454     renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
455     renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
456     renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
457     renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
458
459     renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
460     renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
461     renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
462     renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
463     renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
464
465     motionVector[0] = newPos[0] - oldPos[0];
466     motionVector[1] = newPos[1] - oldPos[1];
467     motionVector[2] = newPos[2] - oldPos[2];
468
469     camera->GetFocalPoint(viewFocus);
470     camera->GetPosition(viewPoint);
471     camera->SetFocalPoint(motionVector[0] + viewFocus[0],
472                           motionVector[1] + viewFocus[1],
473                           motionVector[2] + viewFocus[2]);
474
475     camera->SetPosition(motionVector[0] + viewPoint[0],
476                         motionVector[1] + viewPoint[1],
477                         motionVector[2] + viewPoint[2]);
478
479     if (camera->GetParallelProjection())
480     {
481         camera->SetParallelScale(camera->GetParallelScale() / factor);
482     }
483     else
484     {
485         camera->Dolly(factor);
486     }
487
488     if (interactor->GetLightFollowCamera())
489     {
490         renderer->UpdateLightsGeometryToFollowCamera();
491     }
492     renderer->ResetCameraClippingRange();
493     //interactor->Render();
494 }
495
496 void vvSlicerManagerCallback::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
497 {
498     double flyFrom[3], flyTo[3];
499     double d[3], focalPt[3], position[3], positionFrom[3];
500     int i, j;
501     int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
502     vtkRenderer* renderer=NULL;
503     if (VisibleInWindow>-1) 
504         renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
505     else
506         return;
507
508     interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
509                                   interactor->GetEventPosition()[1], 0.0,
510                                   renderer);
511
512     vtkAssemblyPath *path=NULL;
513     vtkAbstractPropPicker *picker;
514     if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
515     {
516         path = picker->GetPath();
517     }
518     if ( path != NULL )
519     {
520         flyTo[0] = picker->GetPickPosition()[0];
521         flyTo[1] = picker->GetPickPosition()[1];
522         flyTo[2] = picker->GetPickPosition()[2];
523         renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
524         renderer->GetActiveCamera()->GetPosition(positionFrom);
525
526         switch (slicer->GetSliceOrientation())
527         {
528         case vtkImageViewer2::SLICE_ORIENTATION_XY:
529             flyTo[2] = flyFrom[2];
530             break;
531
532         case vtkImageViewer2::SLICE_ORIENTATION_XZ:
533             flyTo[1] = flyFrom[1];
534             break;
535
536         case vtkImageViewer2::SLICE_ORIENTATION_YZ:
537             flyTo[0] = flyFrom[0];
538             break;
539         }
540
541
542         for (i=0; i<3; i++)
543         {
544             d[i] = flyTo[i] - flyFrom[i];
545         }
546         double distance = vtkMath::Normalize(d);
547         double delta = distance/15;
548
549         for (i=1; i<=15; i++)
550         {
551             for (j=0; j<3; j++)
552             {
553                 focalPt[j] = flyFrom[j] + d[j]*i*delta;
554                 position[j] = positionFrom[j] + d[j]*i*delta;
555             }
556             renderer->GetActiveCamera()->SetFocalPoint(focalPt);
557             renderer->GetActiveCamera()->SetPosition(position);
558             renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
559             renderer->ResetCameraClippingRange();
560             interactor->Render();
561         }
562     }
563 }