1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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"
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>
30 #include <vtkAbstractPropPicker.h>
31 #include <vtkAssemblyPath.h>
32 #include <vtkCornerAnnotation.h>
33 #include <vtkRenderWindow.h>
36 #include "vvInteractorStyleNavigator.h"
40 //------------------------------------------------------------------------------
41 vvSlicerManagerCommand::vvSlicerManagerCommand()
46 //------------------------------------------------------------------------------
49 //------------------------------------------------------------------------------
50 //return the num of the current slicer if visible (-1 else)
51 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
54 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
55 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
57 else rvalue = mSlicerNumber;
58 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
61 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
65 void vvSlicerManagerCommand::Execute(vtkObject *caller,
67 void *vtkNotUsed(callData))
70 vvInteractorStyleNavigator *isi =
71 dynamic_cast<vvInteractorStyleNavigator *>(caller);
74 double x = isi->GetInteractor()->GetEventPosition()[0];
75 double y = isi->GetInteractor()->GetEventPosition()[1];
78 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
79 vtkRenderer* renderer=NULL;
80 if (VisibleInWindow>-1)
81 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
84 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
86 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
88 if (this->SM->GetSlicer(i)->GetCursorVisibility())
90 this->SM->GetSlicer(i)->SetCursorVisibility(0);
91 this->SM->GetSlicer(i)->Render();
95 if ( VisibleInWindow > -1 )
97 if (event == vtkCommand::KeyPressEvent)
99 std::string KeyPress = isi->GetInteractor()->GetKeySym();
100 if (KeyPress == "Tab")
102 if(isi->GetInteractor()->GetShiftKey())
103 this->SM->PrevImage(VisibleInWindow);
105 this->SM->NextImage(VisibleInWindow);
108 if (KeyPress == "f" || KeyPress == "F")
110 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
115 this->SM->SetLocalColorWindowing(VisibleInWindow);
120 this->SM->SetPreset(0);
121 this->SM->UpdateWindowLevel();
126 this->SM->SetPreset(1);
127 this->SM->UpdateWindowLevel();
132 this->SM->SetPreset(2);
133 this->SM->UpdateWindowLevel();
139 this->SM->SetPreset(3);
140 this->SM->UpdateWindowLevel();
145 this->SM->SetPreset(4);
146 this->SM->UpdateWindowLevel();
151 this->SM->SetPreset(5);
152 this->SM->UpdateWindowLevel();
157 this->SM->SetColorMap(0);
158 this->SM->UpdateWindowLevel();
163 this->SM->SetColorMap(1);
164 this->SM->UpdateWindowLevel();
169 this->SM->SetColorMap(2);
170 this->SM->UpdateWindowLevel();
175 this->SM->SetColorMap(3);
176 this->SM->UpdateWindowLevel();
179 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
181 this->SM->SetPreset(7);
182 //this->SM->SetColorMap(1);
183 this->SM->UpdateWindowLevel();
186 if (KeyPress == "minus")
188 this->SM->SetColorWindow(-this->SM->GetColorWindow());
189 this->SM->SetColorMap(-1);
190 this->SM->UpdateWindowLevel();
195 this->SM->ToggleContourSuperposition();
201 this->SM->ToggleInterpolation();
207 this->SM->SetCursorVisibility(0);
217 if (KeyPress == "r" || KeyPress=="R")
219 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
220 this->SM->GetSlicer(VisibleInWindow)->Render();
225 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
226 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
227 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
228 this->SM->UpdateViews(1,VisibleInWindow);
229 this->SM->UpdateLinked(VisibleInWindow);
232 if (KeyPress == "F5")
234 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
235 this->SM->GetSlicer(VisibleInWindow)->Render();
238 if (KeyPress == "F6")
240 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
241 this->SM->GetSlicer(VisibleInWindow)->Render();
244 if (KeyPress == "Up")
246 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
247 this->SM->UpdateSlice(VisibleInWindow);
248 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
250 if (KeyPress == "Down")
252 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
253 this->SM->UpdateSlice(VisibleInWindow);
254 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
256 if (KeyPress == "space")
260 if (KeyPress == "Left")
261 this->SM->SetPreviousTSlice(VisibleInWindow);
262 if (KeyPress == "Right")
263 this->SM->SetNextTSlice(VisibleInWindow);
265 if (KeyPress == "F2")
267 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
268 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
269 this->SM->UpdateSliceRange(VisibleInWindow);
270 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
272 if (KeyPress == "F3")
274 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
275 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
276 this->SM->UpdateSliceRange(VisibleInWindow);
277 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
279 if (KeyPress == "F4")
281 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
282 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
283 this->SM->UpdateSliceRange(VisibleInWindow);
284 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
288 //All type of mouse events
289 if (event == vtkCommand::LeaveEvent)
291 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
292 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
293 this->SM->GetSlicer(VisibleInWindow)->Render();
297 if (event == vtkCommand::StartWindowLevelEvent)
300 this->InitialWindow = this->SM->GetColorWindow();
301 this->InitialLevel = this->SM->GetColorLevel();
303 if (VisibleInWindow > -1)
305 mStartSlicer = VisibleInWindow;
310 if (event == vtkCommand::EndWindowLevelEvent)
316 if (VisibleInWindow > -1)
318 this->SM->Activated();
319 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
320 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
322 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
324 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
325 this->SM->UpdateSlice(VisibleInWindow);
326 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
328 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
331 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
333 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
335 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
336 this->SM->UpdateSlice(VisibleInWindow);
337 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
339 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
342 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
344 double xWorld=0; double yWorld=0; double zWorld=0;
346 //Move into World Coordinate
347 renderer->DisplayToNormalizedDisplay(x,y);
348 renderer->NormalizedDisplayToViewport(x,y);
349 renderer->ViewportToNormalizedViewport(x,y);
350 renderer->NormalizedViewportToView(x,y,z);
351 renderer->ViewToWorld(x,y,z);
352 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
354 case vtkImageViewer2::SLICE_ORIENTATION_XY:
357 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
358 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
359 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
362 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
364 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
365 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
366 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
370 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
371 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
372 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
373 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
378 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
379 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
382 this->SM->AddLandmark(xWorld,yWorld,zWorld,
383 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
384 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
387 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
389 this->SM->UpdateViews(1,VisibleInWindow);
390 this->SM->UpdateLinked(VisibleInWindow);
391 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
395 this->SM->GetSlicer(VisibleInWindow)->Render();
397 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
398 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
399 //this->SM->GetSlicer(VisibleInWindow)->Render();
402 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
404 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
405 // Adjust the window level here
406 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
407 double window = this->InitialWindow;
408 double level = this->InitialLevel;
410 this->SM->GetImage()->GetScalarRange(range);
412 // Compute normalized delta
413 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
414 isi->GetWindowLevelStartPosition()[0]) / size[0];
415 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
416 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
417 //Window is exponential in nature, use exponential to avoid falling into negative numbers
418 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
419 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
421 this->SM->SetColorWindow(window*dx);
422 this->SM->SetColorLevel(level-dy);
423 this->SM->SetPreset(6);
424 this->SM->UpdateWindowLevel();
430 //------------------------------------------------------------------------------
433 //------------------------------------------------------------------------------
434 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
436 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
437 vtkRenderer* renderer;
438 if (VisibleInWindow>-1)
439 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
445 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
446 double oldPos[3], newPos[3], distance[2];
447 vtkCamera *camera = renderer->GetActiveCamera();
448 camera->GetFocalPoint(viewFocus);
450 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
451 renderer->WorldToDisplay();
452 renderer->GetDisplayPoint(viewFocus);
454 focalDepth = viewFocus[2];
456 oldPos[0] = renderer->GetCenter()[0];
457 oldPos[1] = renderer->GetCenter()[1];
458 oldPos[2] = focalDepth;
460 distance[0] = 1/factor*
461 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
462 distance[1] = 1/factor*
463 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
465 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
466 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
467 newPos[2] = focalDepth;
469 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
470 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
471 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
472 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
473 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
475 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
476 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
477 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
478 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
479 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
481 motionVector[0] = newPos[0] - oldPos[0];
482 motionVector[1] = newPos[1] - oldPos[1];
483 motionVector[2] = newPos[2] - oldPos[2];
485 camera->GetFocalPoint(viewFocus);
486 camera->GetPosition(viewPoint);
487 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
488 motionVector[1] + viewFocus[1],
489 motionVector[2] + viewFocus[2]);
491 camera->SetPosition(motionVector[0] + viewPoint[0],
492 motionVector[1] + viewPoint[1],
493 motionVector[2] + viewPoint[2]);
495 if (camera->GetParallelProjection())
497 camera->SetParallelScale(camera->GetParallelScale() / factor);
501 camera->Dolly(factor);
504 if (interactor->GetLightFollowCamera())
506 renderer->UpdateLightsGeometryToFollowCamera();
508 renderer->ResetCameraClippingRange();
509 //interactor->Render();
512 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
514 double flyFrom[3], flyTo[3];
515 double d[3], focalPt[3], position[3], positionFrom[3];
517 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
518 vtkRenderer* renderer=NULL;
519 if (VisibleInWindow>-1)
520 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
524 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
525 interactor->GetEventPosition()[1], 0.0,
528 vtkAssemblyPath *path=NULL;
529 vtkAbstractPropPicker *picker;
530 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
532 path = picker->GetPath();
536 flyTo[0] = picker->GetPickPosition()[0];
537 flyTo[1] = picker->GetPickPosition()[1];
538 flyTo[2] = picker->GetPickPosition()[2];
539 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
540 renderer->GetActiveCamera()->GetPosition(positionFrom);
542 switch (slicer->GetSliceOrientation())
544 case vtkImageViewer2::SLICE_ORIENTATION_XY:
545 flyTo[2] = flyFrom[2];
548 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
549 flyTo[1] = flyFrom[1];
552 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
553 flyTo[0] = flyFrom[0];
560 d[i] = flyTo[i] - flyFrom[i];
562 double distance = vtkMath::Normalize(d);
563 double delta = distance/15;
565 for (i=1; i<=15; i++)
569 focalPt[j] = flyFrom[j] + d[j]*i*delta;
570 position[j] = positionFrom[j] + d[j]*i*delta;
572 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
573 renderer->GetActiveCamera()->SetPosition(position);
574 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
575 renderer->ResetCameraClippingRange();
576 interactor->Render();