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 == "f" || KeyPress == "F")
102 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
106 this->SM->SetLocalColorWindowing(VisibleInWindow);
111 this->SM->SetPreset(0);
112 this->SM->UpdateWindowLevel();
117 this->SM->SetPreset(1);
118 this->SM->UpdateWindowLevel();
123 this->SM->SetPreset(2);
124 this->SM->UpdateWindowLevel();
130 this->SM->SetPreset(3);
131 this->SM->UpdateWindowLevel();
136 this->SM->SetPreset(4);
137 this->SM->UpdateWindowLevel();
142 this->SM->SetPreset(5);
143 this->SM->UpdateWindowLevel();
148 this->SM->SetColorMap(0);
149 this->SM->UpdateWindowLevel();
154 this->SM->SetColorMap(1);
155 this->SM->UpdateWindowLevel();
160 this->SM->SetColorMap(2);
161 this->SM->UpdateWindowLevel();
166 this->SM->SetColorMap(3);
167 this->SM->UpdateWindowLevel();
170 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
172 this->SM->SetPreset(7);
173 //this->SM->SetColorMap(1);
174 this->SM->UpdateWindowLevel();
177 if (KeyPress == "minus")
179 this->SM->SetColorWindow(-this->SM->GetColorWindow());
180 this->SM->SetColorMap(-1);
181 this->SM->UpdateWindowLevel();
186 this->SM->ToggleContourSuperposition();
192 this->SM->ToggleInterpolation();
198 this->SM->SetCursorVisibility(0);
208 if (KeyPress == "r" || KeyPress=="R")
210 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
211 this->SM->GetSlicer(VisibleInWindow)->Render();
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);
223 if (KeyPress == "F5")
225 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
226 this->SM->GetSlicer(VisibleInWindow)->Render();
229 if (KeyPress == "F6")
231 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
232 this->SM->GetSlicer(VisibleInWindow)->Render();
235 if (KeyPress == "Up")
237 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
238 this->SM->UpdateSlice(VisibleInWindow);
239 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
241 if (KeyPress == "Down")
243 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
244 this->SM->UpdateSlice(VisibleInWindow);
245 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
247 if (KeyPress == "space")
251 if (KeyPress == "Left")
252 this->SM->SetPreviousTSlice(VisibleInWindow);
253 if (KeyPress == "Right")
254 this->SM->SetNextTSlice(VisibleInWindow);
256 if (KeyPress == "F1")
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);
263 if (KeyPress == "F2")
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);
270 if (KeyPress == "F3")
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);
279 //All type of mouse events
280 if (event == vtkCommand::LeaveEvent)
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();
288 if (event == vtkCommand::StartWindowLevelEvent)
291 this->InitialWindow = this->SM->GetColorWindow();
292 this->InitialLevel = this->SM->GetColorLevel();
294 if (VisibleInWindow > -1)
296 mStartSlicer = VisibleInWindow;
301 if (event == vtkCommand::EndWindowLevelEvent)
307 if (VisibleInWindow > -1)
309 this->SM->Activated();
310 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
311 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
313 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
315 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
316 this->SM->UpdateSlice(VisibleInWindow);
317 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
319 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
322 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
324 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
326 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
327 this->SM->UpdateSlice(VisibleInWindow);
328 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
330 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
333 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
335 double xWorld=0; double yWorld=0; double zWorld=0;
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())
345 case vtkImageViewer2::SLICE_ORIENTATION_XY:
348 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
349 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
350 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
353 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
355 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
356 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
357 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
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];
369 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
370 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
373 this->SM->AddLandmark(xWorld,yWorld,zWorld,
374 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
375 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
378 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
380 this->SM->UpdateViews(1,VisibleInWindow);
381 this->SM->UpdateLinked(VisibleInWindow);
382 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
386 this->SM->GetSlicer(VisibleInWindow)->Render();
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();
393 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
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;
401 this->SM->GetImage()->GetScalarRange(range);
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
412 this->SM->SetColorWindow(window*dx);
413 this->SM->SetColorLevel(level-dy);
414 this->SM->SetPreset(6);
415 this->SM->UpdateWindowLevel();
421 //------------------------------------------------------------------------------
424 //------------------------------------------------------------------------------
425 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
427 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
428 vtkRenderer* renderer;
429 if (VisibleInWindow>-1)
430 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
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);
441 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
442 renderer->WorldToDisplay();
443 renderer->GetDisplayPoint(viewFocus);
445 focalDepth = viewFocus[2];
447 oldPos[0] = renderer->GetCenter()[0];
448 oldPos[1] = renderer->GetCenter()[1];
449 oldPos[2] = focalDepth;
451 distance[0] = 1/factor*
452 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
453 distance[1] = 1/factor*
454 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
456 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
457 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
458 newPos[2] = focalDepth;
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]);
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]);
472 motionVector[0] = newPos[0] - oldPos[0];
473 motionVector[1] = newPos[1] - oldPos[1];
474 motionVector[2] = newPos[2] - oldPos[2];
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]);
482 camera->SetPosition(motionVector[0] + viewPoint[0],
483 motionVector[1] + viewPoint[1],
484 motionVector[2] + viewPoint[2]);
486 if (camera->GetParallelProjection())
488 camera->SetParallelScale(camera->GetParallelScale() / factor);
492 camera->Dolly(factor);
495 if (interactor->GetLightFollowCamera())
497 renderer->UpdateLightsGeometryToFollowCamera();
499 renderer->ResetCameraClippingRange();
500 //interactor->Render();
503 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
505 double flyFrom[3], flyTo[3];
506 double d[3], focalPt[3], position[3], positionFrom[3];
508 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
509 vtkRenderer* renderer=NULL;
510 if (VisibleInWindow>-1)
511 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
515 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
516 interactor->GetEventPosition()[1], 0.0,
519 vtkAssemblyPath *path=NULL;
520 vtkAbstractPropPicker *picker;
521 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
523 path = picker->GetPath();
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);
533 switch (slicer->GetSliceOrientation())
535 case vtkImageViewer2::SLICE_ORIENTATION_XY:
536 flyTo[2] = flyFrom[2];
539 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
540 flyTo[1] = flyFrom[1];
543 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
544 flyTo[0] = flyFrom[0];
551 d[i] = flyTo[i] - flyFrom[i];
553 double distance = vtkMath::Normalize(d);
554 double delta = distance/15;
556 for (i=1; i<=15; i++)
560 focalPt[j] = flyFrom[j] + d[j]*i*delta;
561 position[j] = positionFrom[j] + d[j]*i*delta;
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();