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