1 /*=========================================================================
5 Author : Pierre Seroul (pierre.seroul@gmail.com)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
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.
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.
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/>.
23 =========================================================================*/
24 #include "vvSlicerManagerCommand.h"
25 #include "vvSlicerManager.h"
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"
36 #include "vtkAbstractPropPicker.h"
37 #include "vtkAssemblyPath.h"
38 #include "vtkCornerAnnotation.h"
39 #include <vtkRenderWindow.h>
42 #include "vvInteractorStyleNavigator.h"
46 //------------------------------------------------------------------------------
47 vvSlicerManagerCommand::vvSlicerManagerCommand()
52 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 //return the num of the current slicer if visible (-1 else)
57 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
60 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
61 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
63 else rvalue = mSlicerNumber;
64 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 void vvSlicerManagerCommand::Execute(vtkObject *caller,
73 void *vtkNotUsed(callData))
76 vvInteractorStyleNavigator *isi =
77 dynamic_cast<vvInteractorStyleNavigator *>(caller);
80 double x = isi->GetInteractor()->GetEventPosition()[0];
81 double y = isi->GetInteractor()->GetEventPosition()[1];
84 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
85 vtkRenderer* renderer=NULL;
86 if (VisibleInWindow>-1)
87 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
90 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
92 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
94 if (this->SM->GetSlicer(i)->GetCursorVisibility())
96 this->SM->GetSlicer(i)->SetCursorVisibility(0);
97 this->SM->GetSlicer(i)->Render();
101 if ( VisibleInWindow > -1 )
103 if (event == vtkCommand::KeyPressEvent)
105 std::string KeyPress = isi->GetInteractor()->GetKeySym();
106 if (KeyPress == "f" || KeyPress == "F")
108 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
112 this->SM->SetLocalColorWindowing(VisibleInWindow);
117 this->SM->SetPreset(0);
118 this->SM->UpdateWindowLevel();
123 this->SM->SetPreset(1);
124 this->SM->UpdateWindowLevel();
129 this->SM->SetPreset(2);
130 this->SM->UpdateWindowLevel();
136 this->SM->SetPreset(3);
137 this->SM->UpdateWindowLevel();
142 this->SM->SetPreset(4);
143 this->SM->UpdateWindowLevel();
148 this->SM->SetPreset(5);
149 this->SM->UpdateWindowLevel();
154 this->SM->SetColorMap(0);
155 this->SM->UpdateWindowLevel();
160 this->SM->SetColorMap(1);
161 this->SM->UpdateWindowLevel();
166 this->SM->SetColorMap(2);
167 this->SM->UpdateWindowLevel();
172 this->SM->SetColorMap(3);
173 this->SM->UpdateWindowLevel();
176 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
178 this->SM->SetPreset(7);
179 //this->SM->SetColorMap(1);
180 this->SM->UpdateWindowLevel();
183 if (KeyPress == "minus")
185 this->SM->SetColorWindow(-this->SM->GetColorWindow());
186 this->SM->SetColorMap(-1);
187 this->SM->UpdateWindowLevel();
192 this->SM->ToggleContourSuperposition();
198 this->SM->ToggleInterpolation();
204 this->SM->SetCursorVisibility(0);
214 if (KeyPress == "r" || KeyPress=="R")
216 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
217 this->SM->GetSlicer(VisibleInWindow)->Render();
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);
229 if (KeyPress == "F5")
231 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
232 this->SM->GetSlicer(VisibleInWindow)->Render();
235 if (KeyPress == "F6")
237 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
238 this->SM->GetSlicer(VisibleInWindow)->Render();
241 if (KeyPress == "Up")
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 == "Down")
249 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
250 this->SM->UpdateSlice(VisibleInWindow);
251 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
253 if (KeyPress == "space")
257 if (KeyPress == "Left")
258 this->SM->SetPreviousTSlice(VisibleInWindow);
259 if (KeyPress == "Right")
260 this->SM->SetNextTSlice(VisibleInWindow);
262 if (KeyPress == "F1")
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);
269 if (KeyPress == "F2")
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);
276 if (KeyPress == "F3")
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);
285 //All type of mouse events
286 if (event == vtkCommand::LeaveEvent)
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();
294 if (event == vtkCommand::StartWindowLevelEvent)
297 this->InitialWindow = this->SM->GetColorWindow();
298 this->InitialLevel = this->SM->GetColorLevel();
300 if (VisibleInWindow > -1)
302 mStartSlicer = VisibleInWindow;
307 if (event == vtkCommand::EndWindowLevelEvent)
313 if (VisibleInWindow > -1)
315 this->SM->Activated();
316 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
317 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
319 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
321 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
322 this->SM->UpdateSlice(VisibleInWindow);
323 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
325 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
328 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
330 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
332 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
333 this->SM->UpdateSlice(VisibleInWindow);
334 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
336 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
339 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
341 double xWorld=0; double yWorld=0; double zWorld=0;
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())
351 case vtkImageViewer2::SLICE_ORIENTATION_XY:
354 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
355 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
356 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
359 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
361 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
362 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
363 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
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];
375 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
376 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
379 this->SM->AddLandmark(xWorld,yWorld,zWorld,
380 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
381 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
384 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
386 this->SM->UpdateViews(1,VisibleInWindow);
387 this->SM->UpdateLinked(VisibleInWindow);
388 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
392 this->SM->GetSlicer(VisibleInWindow)->Render();
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();
399 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
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;
407 this->SM->GetImage()->GetScalarRange(range);
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
418 this->SM->SetColorWindow(window*dx);
419 this->SM->SetColorLevel(level-dy);
420 this->SM->SetPreset(6);
421 this->SM->UpdateWindowLevel();
427 //------------------------------------------------------------------------------
430 //------------------------------------------------------------------------------
431 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
433 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
434 vtkRenderer* renderer;
435 if (VisibleInWindow>-1)
436 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
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);
447 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
448 renderer->WorldToDisplay();
449 renderer->GetDisplayPoint(viewFocus);
451 focalDepth = viewFocus[2];
453 oldPos[0] = renderer->GetCenter()[0];
454 oldPos[1] = renderer->GetCenter()[1];
455 oldPos[2] = focalDepth;
457 distance[0] = 1/factor*
458 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
459 distance[1] = 1/factor*
460 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
462 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
463 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
464 newPos[2] = focalDepth;
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]);
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]);
478 motionVector[0] = newPos[0] - oldPos[0];
479 motionVector[1] = newPos[1] - oldPos[1];
480 motionVector[2] = newPos[2] - oldPos[2];
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]);
488 camera->SetPosition(motionVector[0] + viewPoint[0],
489 motionVector[1] + viewPoint[1],
490 motionVector[2] + viewPoint[2]);
492 if (camera->GetParallelProjection())
494 camera->SetParallelScale(camera->GetParallelScale() / factor);
498 camera->Dolly(factor);
501 if (interactor->GetLightFollowCamera())
503 renderer->UpdateLightsGeometryToFollowCamera();
505 renderer->ResetCameraClippingRange();
506 //interactor->Render();
509 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
511 double flyFrom[3], flyTo[3];
512 double d[3], focalPt[3], position[3], positionFrom[3];
514 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
515 vtkRenderer* renderer=NULL;
516 if (VisibleInWindow>-1)
517 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
521 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
522 interactor->GetEventPosition()[1], 0.0,
525 vtkAssemblyPath *path=NULL;
526 vtkAbstractPropPicker *picker;
527 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
529 path = picker->GetPath();
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);
539 switch (slicer->GetSliceOrientation())
541 case vtkImageViewer2::SLICE_ORIENTATION_XY:
542 flyTo[2] = flyFrom[2];
545 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
546 flyTo[1] = flyFrom[1];
549 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
550 flyTo[0] = flyFrom[0];
557 d[i] = flyTo[i] - flyFrom[i];
559 double distance = vtkMath::Normalize(d);
560 double delta = distance/15;
562 for (i=1; i<=15; i++)
566 focalPt[j] = flyFrom[j] + d[j]*i*delta;
567 position[j] = positionFrom[j] + d[j]*i*delta;
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();