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)
55 for (int i = 0; i < SM->NumberOfSlicers(); i++)
57 if (SM->GetSlicer(i)->GetRenderWindow() == renwin
58 && SM->GetSlicer(i)->GetRenderer()->GetDraw())
67 void vvSlicerManagerCallback::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();
86 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
88 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
90 if (this->SM->GetSlicer(i)->GetCursorVisibility())
92 this->SM->GetSlicer(i)->SetCursorVisibility(0);
93 this->SM->GetSlicer(i)->Render();
97 if ( VisibleInWindow > -1 )
99 if (event == vtkCommand::KeyPressEvent)
101 std::string KeyPress = isi->GetInteractor()->GetKeySym();
102 if (KeyPress == "f" || KeyPress == "F")
104 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
108 this->SM->SetPreset(0);
109 this->SM->UpdateWindowLevel();
114 this->SM->SetPreset(1);
115 this->SM->UpdateWindowLevel();
120 this->SM->SetPreset(2);
121 this->SM->UpdateWindowLevel();
127 this->SM->SetPreset(3);
128 this->SM->UpdateWindowLevel();
133 this->SM->SetPreset(4);
134 this->SM->UpdateWindowLevel();
139 this->SM->SetPreset(5);
140 this->SM->UpdateWindowLevel();
145 this->SM->SetColorMap(0);
146 this->SM->UpdateWindowLevel();
151 this->SM->SetColorMap(1);
152 this->SM->UpdateWindowLevel();
157 this->SM->SetColorMap(2);
158 this->SM->UpdateWindowLevel();
163 this->SM->SetColorMap(3);
164 this->SM->UpdateWindowLevel();
167 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
169 this->SM->SetPreset(7);
170 //this->SM->SetColorMap(1);
171 this->SM->UpdateWindowLevel();
174 if (KeyPress == "minus")
176 std::cout << "KeyPress : - " << std::endl;
177 this->SM->SetColorWindow(-this->SM->GetColorWindow());
178 this->SM->SetColorMap(-1);
179 this->SM->UpdateWindowLevel();
184 this->SM->ToggleContourSuperposition();
190 this->SM->ToggleInterpolation();
196 this->SM->SetCursorVisibility(0);
206 if (KeyPress == "r" || KeyPress=="R")
208 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
209 this->SM->GetSlicer(VisibleInWindow)->Render();
214 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
215 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
216 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
217 this->SM->UpdateViews(1,VisibleInWindow);
218 this->SM->UpdateLinked(VisibleInWindow);
221 if (KeyPress == "F5")
223 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
224 this->SM->GetSlicer(VisibleInWindow)->Render();
227 if (KeyPress == "F6")
229 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
230 this->SM->GetSlicer(VisibleInWindow)->Render();
233 if (KeyPress == "Up")
235 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
236 this->SM->UpdateSlice(VisibleInWindow);
237 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
239 if (KeyPress == "Down")
241 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
242 this->SM->UpdateSlice(VisibleInWindow);
243 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
245 if (KeyPress == "space")
249 if (KeyPress == "Left")
250 this->SM->SetPreviousTSlice(VisibleInWindow);
251 if (KeyPress == "Right")
252 this->SM->SetNextTSlice(VisibleInWindow);
254 if (KeyPress == "F1")
256 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
257 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
258 this->SM->UpdateSliceRange(VisibleInWindow);
259 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
261 if (KeyPress == "F2")
263 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
264 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
265 this->SM->UpdateSliceRange(VisibleInWindow);
266 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
268 if (KeyPress == "F3")
270 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
271 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
272 this->SM->UpdateSliceRange(VisibleInWindow);
273 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
277 //All type of mouse events
278 if (event == vtkCommand::LeaveEvent)
280 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
281 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
282 this->SM->GetSlicer(VisibleInWindow)->Render();
286 if (event == vtkCommand::StartWindowLevelEvent)
289 this->InitialWindow = this->SM->GetColorWindow();
290 this->InitialLevel = this->SM->GetColorLevel();
292 if (VisibleInWindow > -1)
294 mStartSlicer = VisibleInWindow;
299 if (event == vtkCommand::EndWindowLevelEvent)
305 if (VisibleInWindow > -1)
307 this->SM->Activated();
308 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
309 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
311 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
313 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
314 this->SM->UpdateSlice(VisibleInWindow);
315 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
317 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
320 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
322 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
324 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
325 this->SM->UpdateSlice(VisibleInWindow);
326 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
328 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
331 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
333 double xWorld=0; double yWorld=0; double zWorld=0;
335 //Move into World Coordinate
336 renderer->DisplayToNormalizedDisplay(x,y);
337 renderer->NormalizedDisplayToViewport(x,y);
338 renderer->ViewportToNormalizedViewport(x,y);
339 renderer->NormalizedViewportToView(x,y,z);
340 renderer->ViewToWorld(x,y,z);
341 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
343 case vtkImageViewer2::SLICE_ORIENTATION_XY:
346 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
347 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
348 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
351 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
353 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
354 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
355 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
359 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
360 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
361 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
362 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
367 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
368 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
371 this->SM->AddLandmark(xWorld,yWorld,zWorld,
372 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
373 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
376 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
378 this->SM->UpdateViews(1,VisibleInWindow);
379 this->SM->UpdateLinked(VisibleInWindow);
380 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
384 this->SM->GetSlicer(VisibleInWindow)->Render();
386 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
387 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
388 //this->SM->GetSlicer(VisibleInWindow)->Render();
391 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
393 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
394 // Adjust the window level here
395 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
396 double window = this->InitialWindow;
397 double level = this->InitialLevel;
399 this->SM->GetImage()->GetScalarRange(range);
401 // Compute normalized delta
402 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
403 isi->GetWindowLevelStartPosition()[0]) / size[0];
404 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
405 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
406 //Window is exponential in nature, use exponential to avoid falling into negative numbers
407 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
408 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
410 this->SM->SetColorWindow(window*dx);
411 this->SM->SetColorLevel(level-dy);
412 this->SM->SetPreset(6);
413 this->SM->UpdateWindowLevel();
420 void vvSlicerManagerCallback::Dolly(double factor, vtkRenderWindowInteractor *interactor)
422 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
423 vtkRenderer* renderer;
424 if (VisibleInWindow>-1)
425 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
431 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
432 double oldPos[3], newPos[3], distance[2];
433 vtkCamera *camera = renderer->GetActiveCamera();
434 camera->GetFocalPoint(viewFocus);
436 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
437 renderer->WorldToDisplay();
438 renderer->GetDisplayPoint(viewFocus);
440 focalDepth = viewFocus[2];
442 oldPos[0] = renderer->GetCenter()[0];
443 oldPos[1] = renderer->GetCenter()[1];
444 oldPos[2] = focalDepth;
446 distance[0] = 1/factor*
447 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
448 distance[1] = 1/factor*
449 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
451 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
452 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
453 newPos[2] = focalDepth;
455 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
456 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
457 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
458 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
459 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
461 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
462 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
463 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
464 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
465 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
467 motionVector[0] = newPos[0] - oldPos[0];
468 motionVector[1] = newPos[1] - oldPos[1];
469 motionVector[2] = newPos[2] - oldPos[2];
471 camera->GetFocalPoint(viewFocus);
472 camera->GetPosition(viewPoint);
473 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
474 motionVector[1] + viewFocus[1],
475 motionVector[2] + viewFocus[2]);
477 camera->SetPosition(motionVector[0] + viewPoint[0],
478 motionVector[1] + viewPoint[1],
479 motionVector[2] + viewPoint[2]);
481 if (camera->GetParallelProjection())
483 camera->SetParallelScale(camera->GetParallelScale() / factor);
487 camera->Dolly(factor);
490 if (interactor->GetLightFollowCamera())
492 renderer->UpdateLightsGeometryToFollowCamera();
494 renderer->ResetCameraClippingRange();
495 //interactor->Render();
498 void vvSlicerManagerCallback::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
500 double flyFrom[3], flyTo[3];
501 double d[3], focalPt[3], position[3], positionFrom[3];
503 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
504 vtkRenderer* renderer=NULL;
505 if (VisibleInWindow>-1)
506 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
510 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
511 interactor->GetEventPosition()[1], 0.0,
514 vtkAssemblyPath *path=NULL;
515 vtkAbstractPropPicker *picker;
516 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
518 path = picker->GetPath();
522 flyTo[0] = picker->GetPickPosition()[0];
523 flyTo[1] = picker->GetPickPosition()[1];
524 flyTo[2] = picker->GetPickPosition()[2];
525 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
526 renderer->GetActiveCamera()->GetPosition(positionFrom);
528 switch (slicer->GetSliceOrientation())
530 case vtkImageViewer2::SLICE_ORIENTATION_XY:
531 flyTo[2] = flyFrom[2];
534 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
535 flyTo[1] = flyFrom[1];
538 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
539 flyTo[0] = flyFrom[0];
546 d[i] = flyTo[i] - flyFrom[i];
548 double distance = vtkMath::Normalize(d);
549 double delta = distance/15;
551 for (i=1; i<=15; i++)
555 focalPt[j] = flyFrom[j] + d[j]*i*delta;
556 position[j] = positionFrom[j] + d[j]*i*delta;
558 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
559 renderer->GetActiveCamera()->SetPosition(position);
560 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
561 renderer->ResetCameraClippingRange();
562 interactor->Render();