1 /*=========================================================================
4 Module: $RCSfile: vvSlicerManagerCommand.cxx,v $
6 Date: $Date: 2010/01/06 13:31:57 $
7 Version: $Revision: 1.1 $
8 Author : Pierre Seroul (pierre.seroul@gmail.com)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
27 #include "vvSlicerManagerCommand.h"
28 #include "vvSlicerManager.h"
30 #include "vtkTextProperty.h"
31 #include "vtkRenderer.h"
32 #include "vtkImageActor.h"
33 #include "vtkRenderWindowInteractor.h"
34 #include "vtkPropPicker.h"
35 #include "vtkCamera.h"
36 #include "vtkImageMapToWindowLevelColors.h"
37 #include "vtkLookupTable.h"
39 #include "vtkAbstractPropPicker.h"
40 #include "vtkAssemblyPath.h"
41 #include "vtkCornerAnnotation.h"
42 #include <vtkRenderWindow.h>
45 #include "vvInteractorStyleNavigator.h"
49 vvSlicerManagerCallback::vvSlicerManagerCallback()
54 //return the num of the current slicer if visible (-1 else)
55 int vvSlicerManagerCallback::FindSlicerNumber(vtkRenderWindow* renwin)
57 for (int i = 0; i < SM->NumberOfSlicers(); i++)
59 if (SM->GetSlicer(i)->GetRenderWindow() == renwin
60 && SM->GetSlicer(i)->GetRenderer()->GetDraw())
68 void vvSlicerManagerCallback::Execute(vtkObject *caller,
70 void *vtkNotUsed(callData))
73 vvInteractorStyleNavigator *isi =
74 dynamic_cast<vvInteractorStyleNavigator *>(caller);
77 double x = isi->GetInteractor()->GetEventPosition()[0];
78 double y = isi->GetInteractor()->GetEventPosition()[1];
81 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
82 vtkRenderer* renderer=NULL;
83 if (VisibleInWindow>-1)
84 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
87 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
89 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
91 if (this->SM->GetSlicer(i)->GetCursorVisibility())
93 this->SM->GetSlicer(i)->SetCursorVisibility(0);
94 this->SM->GetSlicer(i)->Render();
98 if ( VisibleInWindow > -1 )
100 if (event == vtkCommand::KeyPressEvent)
102 std::string KeyPress = isi->GetInteractor()->GetKeySym();
103 if (KeyPress == "f" || KeyPress == "F")
105 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
109 this->SM->SetPreset(0);
110 this->SM->UpdateWindowLevel();
115 this->SM->SetPreset(1);
116 this->SM->UpdateWindowLevel();
121 this->SM->SetPreset(2);
122 this->SM->UpdateWindowLevel();
128 this->SM->SetPreset(3);
129 this->SM->UpdateWindowLevel();
134 this->SM->SetPreset(4);
135 this->SM->UpdateWindowLevel();
140 this->SM->SetPreset(5);
141 this->SM->UpdateWindowLevel();
146 this->SM->SetColorMap(0);
147 this->SM->UpdateWindowLevel();
152 this->SM->SetColorMap(1);
153 this->SM->UpdateWindowLevel();
158 this->SM->SetColorMap(2);
159 this->SM->UpdateWindowLevel();
164 this->SM->SetColorMap(3);
165 this->SM->UpdateWindowLevel();
168 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
170 this->SM->SetPreset(7);
171 //this->SM->SetColorMap(1);
172 this->SM->UpdateWindowLevel();
175 if (KeyPress == "minus")
177 std::cout << "KeyPress : - " << std::endl;
178 this->SM->SetColorWindow(-this->SM->GetColorWindow());
179 this->SM->SetColorMap(-1);
180 this->SM->UpdateWindowLevel();
185 this->SM->ToggleContourSuperposition();
191 this->SM->ToggleInterpolation();
197 this->SM->SetCursorVisibility(0);
207 if (KeyPress == "r" or KeyPress=="R")
209 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
210 this->SM->GetSlicer(VisibleInWindow)->Render();
215 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
216 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
217 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
218 this->SM->UpdateViews(1,VisibleInWindow);
219 this->SM->UpdateLinked(VisibleInWindow);
222 if (KeyPress == "F5")
224 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
225 this->SM->GetSlicer(VisibleInWindow)->Render();
228 if (KeyPress == "F6")
230 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
231 this->SM->GetSlicer(VisibleInWindow)->Render();
234 if (KeyPress == "Up")
236 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
237 this->SM->UpdateSlice(VisibleInWindow);
238 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
240 if (KeyPress == "Down")
242 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
243 this->SM->UpdateSlice(VisibleInWindow);
244 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
246 if (KeyPress == "space")
250 if (KeyPress == "Left")
251 this->SM->SetPreviousTSlice(VisibleInWindow);
252 if (KeyPress == "Right")
253 this->SM->SetNextTSlice(VisibleInWindow);
255 if (KeyPress == "F1")
257 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
258 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
259 this->SM->UpdateSliceRange(VisibleInWindow);
260 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
262 if (KeyPress == "F2")
264 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
265 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
266 this->SM->UpdateSliceRange(VisibleInWindow);
267 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
269 if (KeyPress == "F3")
271 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
272 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
273 this->SM->UpdateSliceRange(VisibleInWindow);
274 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
278 //All type of mouse events
279 if (event == vtkCommand::LeaveEvent)
281 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
282 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
283 this->SM->GetSlicer(VisibleInWindow)->Render();
287 if (event == vtkCommand::StartWindowLevelEvent)
290 this->InitialWindow = this->SM->GetColorWindow();
291 this->InitialLevel = this->SM->GetColorLevel();
293 if (VisibleInWindow > -1)
295 mStartSlicer = VisibleInWindow;
300 if (event == vtkCommand::EndWindowLevelEvent)
306 if (VisibleInWindow > -1)
308 this->SM->Activated();
309 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
310 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
312 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
314 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
315 this->SM->UpdateSlice(VisibleInWindow);
316 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
318 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
321 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
323 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
325 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
326 this->SM->UpdateSlice(VisibleInWindow);
327 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
329 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
332 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
334 double xWorld=0; double yWorld=0; double zWorld=0;
336 //Move into World Coordinate
337 renderer->DisplayToNormalizedDisplay(x,y);
338 renderer->NormalizedDisplayToViewport(x,y);
339 renderer->ViewportToNormalizedViewport(x,y);
340 renderer->NormalizedViewportToView(x,y,z);
341 renderer->ViewToWorld(x,y,z);
342 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
344 case vtkImageViewer2::SLICE_ORIENTATION_XY:
347 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
348 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
349 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
352 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
354 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
355 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
356 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
360 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
361 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
362 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
363 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
368 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
369 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
372 this->SM->AddLandmark(xWorld,yWorld,zWorld,
373 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
374 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
377 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
379 this->SM->UpdateViews(1,VisibleInWindow);
380 this->SM->UpdateLinked(VisibleInWindow);
381 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
385 this->SM->GetSlicer(VisibleInWindow)->Render();
387 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
388 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
389 //this->SM->GetSlicer(VisibleInWindow)->Render();
392 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
394 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
395 // Adjust the window level here
396 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
397 double window = this->InitialWindow;
398 double level = this->InitialLevel;
400 this->SM->GetImage()->GetScalarRange(range);
402 // Compute normalized delta
403 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
404 isi->GetWindowLevelStartPosition()[0]) / size[0];
405 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
406 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
407 //Window is exponential in nature, use exponential to avoid falling into negative numbers
408 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
409 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
411 this->SM->SetColorWindow(window*dx);
412 this->SM->SetColorLevel(level-dy);
413 this->SM->SetPreset(6);
414 this->SM->UpdateWindowLevel();
421 void vvSlicerManagerCallback::Dolly(double factor, vtkRenderWindowInteractor *interactor)
423 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
424 vtkRenderer* renderer;
425 if (VisibleInWindow>-1)
426 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
432 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
433 double oldPos[3], newPos[3], distance[2];
434 vtkCamera *camera = renderer->GetActiveCamera();
435 camera->GetFocalPoint(viewFocus);
437 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
438 renderer->WorldToDisplay();
439 renderer->GetDisplayPoint(viewFocus);
441 focalDepth = viewFocus[2];
443 oldPos[0] = renderer->GetCenter()[0];
444 oldPos[1] = renderer->GetCenter()[1];
445 oldPos[2] = focalDepth;
447 distance[0] = 1/factor*
448 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
449 distance[1] = 1/factor*
450 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
452 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
453 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
454 newPos[2] = focalDepth;
456 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
457 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
458 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
459 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
460 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
462 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
463 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
464 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
465 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
466 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
468 motionVector[0] = newPos[0] - oldPos[0];
469 motionVector[1] = newPos[1] - oldPos[1];
470 motionVector[2] = newPos[2] - oldPos[2];
472 camera->GetFocalPoint(viewFocus);
473 camera->GetPosition(viewPoint);
474 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
475 motionVector[1] + viewFocus[1],
476 motionVector[2] + viewFocus[2]);
478 camera->SetPosition(motionVector[0] + viewPoint[0],
479 motionVector[1] + viewPoint[1],
480 motionVector[2] + viewPoint[2]);
482 if (camera->GetParallelProjection())
484 camera->SetParallelScale(camera->GetParallelScale() / factor);
488 camera->Dolly(factor);
491 if (interactor->GetLightFollowCamera())
493 renderer->UpdateLightsGeometryToFollowCamera();
495 renderer->ResetCameraClippingRange();
496 //interactor->Render();
499 void vvSlicerManagerCallback::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
501 double flyFrom[3], flyTo[3];
502 double d[3], focalPt[3], position[3], positionFrom[3];
504 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
505 vtkRenderer* renderer=NULL;
506 if (VisibleInWindow>-1)
507 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
511 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
512 interactor->GetEventPosition()[1], 0.0,
515 vtkAssemblyPath *path=NULL;
516 vtkAbstractPropPicker *picker;
517 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
519 path = picker->GetPath();
523 flyTo[0] = picker->GetPickPosition()[0];
524 flyTo[1] = picker->GetPickPosition()[1];
525 flyTo[2] = picker->GetPickPosition()[2];
526 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
527 renderer->GetActiveCamera()->GetPosition(positionFrom);
529 switch (slicer->GetSliceOrientation())
531 case vtkImageViewer2::SLICE_ORIENTATION_XY:
532 flyTo[2] = flyFrom[2];
535 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
536 flyTo[1] = flyFrom[1];
539 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
540 flyTo[0] = flyFrom[0];
547 d[i] = flyTo[i] - flyFrom[i];
549 double distance = vtkMath::Normalize(d);
550 double delta = distance/15;
552 for (i=1; i<=15; i++)
556 focalPt[j] = flyFrom[j] + d[j]*i*delta;
557 position[j] = positionFrom[j] + d[j]*i*delta;
559 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
560 renderer->GetActiveCamera()->SetPosition(position);
561 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
562 renderer->ResetCameraClippingRange();
563 interactor->Render();