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();
298 // Mouse release HERE
299 if (event == vtkCommand::EndPickEvent) {
300 // DD(VisibleInWindow);
301 if (VisibleInWindow > -1)
302 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
303 return; // no return !!!! ???
306 if (event == vtkCommand::StartWindowLevelEvent)
309 this->InitialWindow = this->SM->GetColorWindow();
310 this->InitialLevel = this->SM->GetColorLevel();
312 if (VisibleInWindow > -1)
314 mStartSlicer = VisibleInWindow;
319 if (event == vtkCommand::EndWindowLevelEvent)
325 if (VisibleInWindow > -1)
327 this->SM->Activated();
328 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
329 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
331 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
333 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
334 this->SM->UpdateSlice(VisibleInWindow);
335 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
337 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
340 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
342 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
344 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
345 this->SM->UpdateSlice(VisibleInWindow);
346 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
348 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
351 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
353 double xWorld=0; double yWorld=0; double zWorld=0;
355 //Move into World Coordinate
356 renderer->DisplayToNormalizedDisplay(x,y);
357 renderer->NormalizedDisplayToViewport(x,y);
358 renderer->ViewportToNormalizedViewport(x,y);
359 renderer->NormalizedViewportToView(x,y,z);
360 renderer->ViewToWorld(x,y,z);
361 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
363 case vtkImageViewer2::SLICE_ORIENTATION_XY:
366 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
367 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
368 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
371 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
373 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
374 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
375 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
379 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
380 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
381 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
382 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
387 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
388 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
391 this->SM->AddLandmark(xWorld,yWorld,zWorld,
392 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
393 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
396 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
398 this->SM->UpdateViews(1,VisibleInWindow);
399 this->SM->UpdateLinked(VisibleInWindow);
400 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
404 this->SM->GetSlicer(VisibleInWindow)->Render();
406 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
407 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
408 //this->SM->GetSlicer(VisibleInWindow)->Render();
411 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
413 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
414 // Adjust the window level here
415 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
416 double window = this->InitialWindow;
417 double level = this->InitialLevel;
419 this->SM->GetImage()->GetScalarRange(range);
421 // Compute normalized delta
422 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
423 isi->GetWindowLevelStartPosition()[0]) / size[0];
424 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
425 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
426 //Window is exponential in nature, use exponential to avoid falling into negative numbers
427 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
428 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
430 this->SM->SetColorWindow(window*dx);
431 this->SM->SetColorLevel(level-dy);
432 this->SM->SetPreset(6);
433 this->SM->UpdateWindowLevel();
439 //------------------------------------------------------------------------------
442 //------------------------------------------------------------------------------
443 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
445 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
446 vtkRenderer* renderer;
447 if (VisibleInWindow>-1)
448 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
454 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
455 double oldPos[3], newPos[3], distance[2];
456 vtkCamera *camera = renderer->GetActiveCamera();
457 camera->GetFocalPoint(viewFocus);
459 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
460 renderer->WorldToDisplay();
461 renderer->GetDisplayPoint(viewFocus);
463 focalDepth = viewFocus[2];
465 oldPos[0] = renderer->GetCenter()[0];
466 oldPos[1] = renderer->GetCenter()[1];
467 oldPos[2] = focalDepth;
469 distance[0] = 1/factor*
470 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
471 distance[1] = 1/factor*
472 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
474 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
475 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
476 newPos[2] = focalDepth;
478 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
479 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
480 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
481 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
482 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
484 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
485 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
486 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
487 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
488 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
490 motionVector[0] = newPos[0] - oldPos[0];
491 motionVector[1] = newPos[1] - oldPos[1];
492 motionVector[2] = newPos[2] - oldPos[2];
494 camera->GetFocalPoint(viewFocus);
495 camera->GetPosition(viewPoint);
496 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
497 motionVector[1] + viewFocus[1],
498 motionVector[2] + viewFocus[2]);
500 camera->SetPosition(motionVector[0] + viewPoint[0],
501 motionVector[1] + viewPoint[1],
502 motionVector[2] + viewPoint[2]);
504 if (camera->GetParallelProjection())
506 camera->SetParallelScale(camera->GetParallelScale() / factor);
510 camera->Dolly(factor);
513 if (interactor->GetLightFollowCamera())
515 renderer->UpdateLightsGeometryToFollowCamera();
517 renderer->ResetCameraClippingRange();
518 //interactor->Render();
520 //------------------------------------------------------------------------------
523 //------------------------------------------------------------------------------
524 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
526 double flyFrom[3], flyTo[3];
527 double d[3], focalPt[3], position[3], positionFrom[3];
529 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
530 vtkRenderer* renderer=NULL;
531 if (VisibleInWindow>-1)
532 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
536 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
537 interactor->GetEventPosition()[1], 0.0,
540 vtkAssemblyPath *path=NULL;
541 vtkAbstractPropPicker *picker;
542 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
544 path = picker->GetPath();
548 flyTo[0] = picker->GetPickPosition()[0];
549 flyTo[1] = picker->GetPickPosition()[1];
550 flyTo[2] = picker->GetPickPosition()[2];
551 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
552 renderer->GetActiveCamera()->GetPosition(positionFrom);
554 switch (slicer->GetSliceOrientation())
556 case vtkImageViewer2::SLICE_ORIENTATION_XY:
557 flyTo[2] = flyFrom[2];
560 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
561 flyTo[1] = flyFrom[1];
564 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
565 flyTo[0] = flyFrom[0];
572 d[i] = flyTo[i] - flyFrom[i];
574 double distance = vtkMath::Normalize(d);
575 double delta = distance/15;
577 for (i=1; i<=15; i++)
581 focalPt[j] = flyFrom[j] + d[j]*i*delta;
582 position[j] = positionFrom[j] + d[j]*i*delta;
584 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
585 renderer->GetActiveCamera()->SetPosition(position);
586 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
587 renderer->ResetCameraClippingRange();
588 interactor->Render();
592 //------------------------------------------------------------------------------