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>
35 #include <vtkImageReslice.h>
37 #include "vvInteractorStyleNavigator.h"
38 #include "vtkTransform.h"
42 //------------------------------------------------------------------------------
43 vvSlicerManagerCommand::vvSlicerManagerCommand()
48 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 //return the num of the current slicer if visible (-1 else)
53 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
56 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
57 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
59 else rvalue = mSlicerNumber;
60 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
63 //------------------------------------------------------------------------------
66 //------------------------------------------------------------------------------
67 void vvSlicerManagerCommand::Execute(vtkObject *caller,
69 void *vtkNotUsed(callData))
72 vvInteractorStyleNavigator *isi =
73 dynamic_cast<vvInteractorStyleNavigator *>(caller);
76 double x = isi->GetInteractor()->GetEventPosition()[0];
77 double y = isi->GetInteractor()->GetEventPosition()[1];
80 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
81 vtkRenderer* renderer=NULL;
82 if (VisibleInWindow>-1)
83 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
88 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
90 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
92 if (this->SM->GetSlicer(i)->GetCursorVisibility())
94 this->SM->GetSlicer(i)->SetCursorVisibility(0);
95 this->SM->GetSlicer(i)->Render();
99 if ( VisibleInWindow > -1 )
101 if (event == vtkCommand::KeyPressEvent)
103 std::string KeyPress = isi->GetInteractor()->GetKeySym();
104 if (KeyPress == "Tab")
106 if(isi->GetInteractor()->GetShiftKey())
107 this->SM->PrevImage(VisibleInWindow);
109 this->SM->NextImage(VisibleInWindow);
112 if (KeyPress == "f" || KeyPress == "F")
114 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
119 this->SM->SetLocalColorWindowing(VisibleInWindow);
124 this->SM->SetPreset(0);
125 this->SM->UpdateWindowLevel();
130 this->SM->SetPreset(1);
131 this->SM->UpdateWindowLevel();
136 this->SM->SetPreset(2);
137 this->SM->UpdateWindowLevel();
143 this->SM->SetPreset(3);
144 this->SM->UpdateWindowLevel();
149 this->SM->SetPreset(4);
150 this->SM->UpdateWindowLevel();
155 this->SM->SetPreset(5);
156 this->SM->UpdateWindowLevel();
161 this->SM->SetColorMap(0);
162 this->SM->UpdateWindowLevel();
167 this->SM->SetColorMap(1);
168 this->SM->UpdateWindowLevel();
173 this->SM->SetColorMap(2);
174 this->SM->UpdateWindowLevel();
179 this->SM->SetColorMap(3);
180 this->SM->UpdateWindowLevel();
183 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
185 this->SM->SetPreset(7);
186 //this->SM->SetColorMap(1);
187 this->SM->UpdateWindowLevel();
190 if (KeyPress == "minus")
192 this->SM->SetColorWindow(-this->SM->GetColorWindow());
193 this->SM->SetColorMap(-1);
194 this->SM->UpdateWindowLevel();
199 this->SM->ToggleContourSuperposition();
205 this->SM->ToggleInterpolation();
211 this->SM->SetCursorVisibility(0);
221 if (KeyPress == "r" || KeyPress=="R")
223 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
224 this->SM->GetSlicer(VisibleInWindow)->Render();
229 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
230 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
231 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
232 this->SM->UpdateViews(1,VisibleInWindow);
233 this->SM->UpdateLinked(VisibleInWindow);
236 if (KeyPress == "F5")
238 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
239 this->SM->GetSlicer(VisibleInWindow)->Render();
242 if (KeyPress == "F6")
244 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
245 this->SM->GetSlicer(VisibleInWindow)->Render();
248 if (KeyPress == "Up")
250 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
251 this->SM->UpdateSlice(VisibleInWindow);
252 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
254 if (KeyPress == "Down")
256 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
257 this->SM->UpdateSlice(VisibleInWindow);
258 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
260 if (KeyPress == "space")
264 if (KeyPress == "Left")
265 this->SM->SetPreviousTSlice(VisibleInWindow);
266 if (KeyPress == "Right")
267 this->SM->SetNextTSlice(VisibleInWindow);
269 if (KeyPress == "F2")
271 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
272 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
273 this->SM->UpdateSliceRange(VisibleInWindow);
274 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
276 if (KeyPress == "F3")
278 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
279 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
280 this->SM->UpdateSliceRange(VisibleInWindow);
281 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
283 if (KeyPress == "F4")
285 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
286 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
287 this->SM->UpdateSliceRange(VisibleInWindow);
288 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
293 //All type of mouse events
294 if (event == vtkCommand::LeaveEvent)
296 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
297 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
298 this->SM->GetSlicer(VisibleInWindow)->Render();
303 // Mouse release HERE
304 if (event == vtkCommand::EndPickEvent) {
305 // DD(VisibleInWindow);
306 if (VisibleInWindow > -1)
307 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
308 return; // no return !!!! ???
311 if (event == vtkCommand::StartWindowLevelEvent)
314 this->InitialWindow = this->SM->GetColorWindow();
315 this->InitialLevel = this->SM->GetColorLevel();
317 if (VisibleInWindow > -1)
319 mStartSlicer = VisibleInWindow;
324 if (event == vtkCommand::EndWindowLevelEvent)
330 if (VisibleInWindow > -1)
332 this->SM->Activated();
333 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
334 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
336 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
338 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
339 this->SM->UpdateSlice(VisibleInWindow);
340 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
342 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
345 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
347 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
349 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
350 this->SM->UpdateSlice(VisibleInWindow);
351 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
353 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
356 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
358 double xWorld=0; double yWorld=0; double zWorld=0;
360 //Move into World Coordinate
361 renderer->DisplayToNormalizedDisplay(x,y);
362 renderer->NormalizedDisplayToViewport(x,y);
363 renderer->ViewportToNormalizedViewport(x,y);
364 renderer->NormalizedViewportToView(x,y,z);
365 renderer->ViewToWorld(x,y,z);
366 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
368 case vtkImageViewer2::SLICE_ORIENTATION_XY:
371 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
372 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
373 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
376 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
378 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
379 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
380 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
384 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
385 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
386 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
387 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
392 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
393 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
396 this->SM->AddLandmark(xWorld,yWorld,zWorld,
397 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
398 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
401 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
403 this->SM->UpdateViews(1,VisibleInWindow);
404 this->SM->UpdateLinked(VisibleInWindow);
405 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
409 this->SM->GetSlicer(VisibleInWindow)->Render();
411 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
412 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
413 //this->SM->GetSlicer(VisibleInWindow)->Render();
416 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
418 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
419 // Adjust the window level here
420 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
421 double window = this->InitialWindow;
422 double level = this->InitialLevel;
424 this->SM->GetImage()->GetScalarRange(range);
426 // Compute normalized delta
427 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
428 isi->GetWindowLevelStartPosition()[0]) / size[0];
429 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
430 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
431 //Window is exponential in nature, use exponential to avoid falling into negative numbers
432 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
433 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
435 this->SM->SetColorWindow(window*dx);
436 this->SM->SetColorLevel(level-dy);
437 this->SM->SetPreset(6);
438 this->SM->UpdateWindowLevel();
444 //------------------------------------------------------------------------------
447 //------------------------------------------------------------------------------
448 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
450 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
451 vtkRenderer* renderer;
452 if (VisibleInWindow>-1)
453 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
459 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
460 double oldPos[3], newPos[3], distance[2];
461 vtkCamera *camera = renderer->GetActiveCamera();
462 camera->GetFocalPoint(viewFocus);
464 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
465 renderer->WorldToDisplay();
466 renderer->GetDisplayPoint(viewFocus);
468 focalDepth = viewFocus[2];
470 oldPos[0] = renderer->GetCenter()[0];
471 oldPos[1] = renderer->GetCenter()[1];
472 oldPos[2] = focalDepth;
474 distance[0] = 1/factor*
475 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
476 distance[1] = 1/factor*
477 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
479 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
480 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
481 newPos[2] = focalDepth;
483 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
484 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
485 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
486 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
487 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
489 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
490 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
491 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
492 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
493 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
495 motionVector[0] = newPos[0] - oldPos[0];
496 motionVector[1] = newPos[1] - oldPos[1];
497 motionVector[2] = newPos[2] - oldPos[2];
499 camera->GetFocalPoint(viewFocus);
500 camera->GetPosition(viewPoint);
501 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
502 motionVector[1] + viewFocus[1],
503 motionVector[2] + viewFocus[2]);
505 camera->SetPosition(motionVector[0] + viewPoint[0],
506 motionVector[1] + viewPoint[1],
507 motionVector[2] + viewPoint[2]);
509 if (camera->GetParallelProjection())
511 camera->SetParallelScale(camera->GetParallelScale() / factor);
515 camera->Dolly(factor);
518 if (interactor->GetLightFollowCamera())
520 renderer->UpdateLightsGeometryToFollowCamera();
522 renderer->ResetCameraClippingRange();
523 //interactor->Render();
525 //------------------------------------------------------------------------------
528 //------------------------------------------------------------------------------
529 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
531 double flyFrom[3], flyTo[3];
532 double d[3], focalPt[3], position[3], positionFrom[3];
534 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
535 vtkRenderer* renderer=NULL;
536 if (VisibleInWindow>-1)
537 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
541 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
542 interactor->GetEventPosition()[1], 0.0,
545 vtkAssemblyPath *path=NULL;
546 vtkAbstractPropPicker *picker;
547 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
549 path = picker->GetPath();
553 flyTo[0] = picker->GetPickPosition()[0];
554 flyTo[1] = picker->GetPickPosition()[1];
555 flyTo[2] = picker->GetPickPosition()[2];
556 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
557 renderer->GetActiveCamera()->GetPosition(positionFrom);
559 switch (slicer->GetSliceOrientation())
561 case vtkImageViewer2::SLICE_ORIENTATION_XY:
562 flyTo[2] = flyFrom[2];
565 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
566 flyTo[1] = flyFrom[1];
569 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
570 flyTo[0] = flyFrom[0];
577 d[i] = flyTo[i] - flyFrom[i];
579 double distance = vtkMath::Normalize(d);
580 double delta = distance/15;
582 for (i=1; i<=15; i++)
586 focalPt[j] = flyFrom[j] + d[j]*i*delta;
587 position[j] = positionFrom[j] + d[j]*i*delta;
589 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
590 renderer->GetActiveCamera()->SetPosition(position);
591 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
592 renderer->ResetCameraClippingRange();
593 interactor->Render();
597 //------------------------------------------------------------------------------