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 vvSlicerManagerCallback::vvSlicerManagerCallback()
51 //return the num of the current slicer if visible (-1 else)
52 int vvSlicerManagerCallback::FindSlicerNumber(vtkRenderWindow* renwin)
54 for (int i = 0; i < SM->NumberOfSlicers(); i++)
56 if (SM->GetSlicer(i)->GetRenderWindow() == renwin
57 && SM->GetSlicer(i)->GetRenderer()->GetDraw())
65 void vvSlicerManagerCallback::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->SetPreset(0);
107 this->SM->UpdateWindowLevel();
112 this->SM->SetPreset(1);
113 this->SM->UpdateWindowLevel();
118 this->SM->SetPreset(2);
119 this->SM->UpdateWindowLevel();
125 this->SM->SetPreset(3);
126 this->SM->UpdateWindowLevel();
131 this->SM->SetPreset(4);
132 this->SM->UpdateWindowLevel();
137 this->SM->SetPreset(5);
138 this->SM->UpdateWindowLevel();
143 this->SM->SetColorMap(0);
144 this->SM->UpdateWindowLevel();
149 this->SM->SetColorMap(1);
150 this->SM->UpdateWindowLevel();
155 this->SM->SetColorMap(2);
156 this->SM->UpdateWindowLevel();
161 this->SM->SetColorMap(3);
162 this->SM->UpdateWindowLevel();
165 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
167 this->SM->SetPreset(7);
168 //this->SM->SetColorMap(1);
169 this->SM->UpdateWindowLevel();
172 if (KeyPress == "minus")
174 std::cout << "KeyPress : - " << std::endl;
175 this->SM->SetColorWindow(-this->SM->GetColorWindow());
176 this->SM->SetColorMap(-1);
177 this->SM->UpdateWindowLevel();
182 this->SM->ToggleContourSuperposition();
188 this->SM->ToggleInterpolation();
194 this->SM->SetCursorVisibility(0);
204 if (KeyPress == "r" || KeyPress=="R")
206 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
207 this->SM->GetSlicer(VisibleInWindow)->Render();
212 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
213 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
214 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
215 this->SM->UpdateViews(1,VisibleInWindow);
216 this->SM->UpdateLinked(VisibleInWindow);
219 if (KeyPress == "F5")
221 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
222 this->SM->GetSlicer(VisibleInWindow)->Render();
225 if (KeyPress == "F6")
227 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
228 this->SM->GetSlicer(VisibleInWindow)->Render();
231 if (KeyPress == "Up")
233 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
234 this->SM->UpdateSlice(VisibleInWindow);
235 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
237 if (KeyPress == "Down")
239 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
240 this->SM->UpdateSlice(VisibleInWindow);
241 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
243 if (KeyPress == "space")
247 if (KeyPress == "Left")
248 this->SM->SetPreviousTSlice(VisibleInWindow);
249 if (KeyPress == "Right")
250 this->SM->SetNextTSlice(VisibleInWindow);
252 if (KeyPress == "F1")
254 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
255 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
256 this->SM->UpdateSliceRange(VisibleInWindow);
257 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
259 if (KeyPress == "F2")
261 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
262 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
263 this->SM->UpdateSliceRange(VisibleInWindow);
264 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
266 if (KeyPress == "F3")
268 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
269 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
270 this->SM->UpdateSliceRange(VisibleInWindow);
271 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
275 //All type of mouse events
276 if (event == vtkCommand::LeaveEvent)
278 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
279 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
280 this->SM->GetSlicer(VisibleInWindow)->Render();
284 if (event == vtkCommand::StartWindowLevelEvent)
287 this->InitialWindow = this->SM->GetColorWindow();
288 this->InitialLevel = this->SM->GetColorLevel();
290 if (VisibleInWindow > -1)
292 mStartSlicer = VisibleInWindow;
297 if (event == vtkCommand::EndWindowLevelEvent)
303 if (VisibleInWindow > -1)
305 this->SM->Activated();
306 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
307 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
309 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
311 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
312 this->SM->UpdateSlice(VisibleInWindow);
313 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
315 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
318 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
320 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
322 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
323 this->SM->UpdateSlice(VisibleInWindow);
324 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
326 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
329 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
331 double xWorld=0; double yWorld=0; double zWorld=0;
333 //Move into World Coordinate
334 renderer->DisplayToNormalizedDisplay(x,y);
335 renderer->NormalizedDisplayToViewport(x,y);
336 renderer->ViewportToNormalizedViewport(x,y);
337 renderer->NormalizedViewportToView(x,y,z);
338 renderer->ViewToWorld(x,y,z);
339 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
341 case vtkImageViewer2::SLICE_ORIENTATION_XY:
344 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
345 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
346 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
349 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
351 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
352 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
353 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
357 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
358 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
359 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
360 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
365 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
366 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
369 this->SM->AddLandmark(xWorld,yWorld,zWorld,
370 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
371 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
374 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
376 this->SM->UpdateViews(1,VisibleInWindow);
377 this->SM->UpdateLinked(VisibleInWindow);
378 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
382 this->SM->GetSlicer(VisibleInWindow)->Render();
384 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
385 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
386 //this->SM->GetSlicer(VisibleInWindow)->Render();
389 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
391 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
392 // Adjust the window level here
393 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
394 double window = this->InitialWindow;
395 double level = this->InitialLevel;
397 this->SM->GetImage()->GetScalarRange(range);
399 // Compute normalized delta
400 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
401 isi->GetWindowLevelStartPosition()[0]) / size[0];
402 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
403 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
404 //Window is exponential in nature, use exponential to avoid falling into negative numbers
405 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
406 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
408 this->SM->SetColorWindow(window*dx);
409 this->SM->SetColorLevel(level-dy);
410 this->SM->SetPreset(6);
411 this->SM->UpdateWindowLevel();
418 void vvSlicerManagerCallback::Dolly(double factor, vtkRenderWindowInteractor *interactor)
420 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
421 vtkRenderer* renderer;
422 if (VisibleInWindow>-1)
423 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
429 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
430 double oldPos[3], newPos[3], distance[2];
431 vtkCamera *camera = renderer->GetActiveCamera();
432 camera->GetFocalPoint(viewFocus);
434 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
435 renderer->WorldToDisplay();
436 renderer->GetDisplayPoint(viewFocus);
438 focalDepth = viewFocus[2];
440 oldPos[0] = renderer->GetCenter()[0];
441 oldPos[1] = renderer->GetCenter()[1];
442 oldPos[2] = focalDepth;
444 distance[0] = 1/factor*
445 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
446 distance[1] = 1/factor*
447 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
449 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
450 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
451 newPos[2] = focalDepth;
453 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
454 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
455 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
456 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
457 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
459 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
460 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
461 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
462 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
463 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
465 motionVector[0] = newPos[0] - oldPos[0];
466 motionVector[1] = newPos[1] - oldPos[1];
467 motionVector[2] = newPos[2] - oldPos[2];
469 camera->GetFocalPoint(viewFocus);
470 camera->GetPosition(viewPoint);
471 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
472 motionVector[1] + viewFocus[1],
473 motionVector[2] + viewFocus[2]);
475 camera->SetPosition(motionVector[0] + viewPoint[0],
476 motionVector[1] + viewPoint[1],
477 motionVector[2] + viewPoint[2]);
479 if (camera->GetParallelProjection())
481 camera->SetParallelScale(camera->GetParallelScale() / factor);
485 camera->Dolly(factor);
488 if (interactor->GetLightFollowCamera())
490 renderer->UpdateLightsGeometryToFollowCamera();
492 renderer->ResetCameraClippingRange();
493 //interactor->Render();
496 void vvSlicerManagerCallback::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
498 double flyFrom[3], flyTo[3];
499 double d[3], focalPt[3], position[3], positionFrom[3];
501 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
502 vtkRenderer* renderer=NULL;
503 if (VisibleInWindow>-1)
504 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
508 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
509 interactor->GetEventPosition()[1], 0.0,
512 vtkAssemblyPath *path=NULL;
513 vtkAbstractPropPicker *picker;
514 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
516 path = picker->GetPath();
520 flyTo[0] = picker->GetPickPosition()[0];
521 flyTo[1] = picker->GetPickPosition()[1];
522 flyTo[2] = picker->GetPickPosition()[2];
523 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
524 renderer->GetActiveCamera()->GetPosition(positionFrom);
526 switch (slicer->GetSliceOrientation())
528 case vtkImageViewer2::SLICE_ORIENTATION_XY:
529 flyTo[2] = flyFrom[2];
532 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
533 flyTo[1] = flyFrom[1];
536 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
537 flyTo[0] = flyFrom[0];
544 d[i] = flyTo[i] - flyFrom[i];
546 double distance = vtkMath::Normalize(d);
547 double delta = distance/15;
549 for (i=1; i<=15; i++)
553 focalPt[j] = flyFrom[j] + d[j]*i*delta;
554 position[j] = positionFrom[j] + d[j]*i*delta;
556 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
557 renderer->GetActiveCamera()->SetPosition(position);
558 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
559 renderer->ResetCameraClippingRange();
560 interactor->Render();