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 vvSlicerManagerCallback::vvSlicerManagerCallback()
51 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
55 //return the num of the current slicer if visible (-1 else)
56 int vvSlicerManagerCallback::FindSlicerNumber(vtkRenderWindow* renwin)
59 for (int i = 0; i < SM->NumberOfSlicers(); i++)
61 if (SM->GetSlicer(i)->GetRenderWindow() == renwin
62 && SM->GetSlicer(i)->GetRenderer()->GetDraw())
70 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
74 void vvSlicerManagerCallback::Execute(vtkObject *caller,
76 void *vtkNotUsed(callData))
79 vvInteractorStyleNavigator *isi =
80 dynamic_cast<vvInteractorStyleNavigator *>(caller);
83 double x = isi->GetInteractor()->GetEventPosition()[0];
84 double y = isi->GetInteractor()->GetEventPosition()[1];
87 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
88 vtkRenderer* renderer=NULL;
89 if (VisibleInWindow>-1)
90 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
93 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
95 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
97 if (this->SM->GetSlicer(i)->GetCursorVisibility())
99 this->SM->GetSlicer(i)->SetCursorVisibility(0);
100 this->SM->GetSlicer(i)->Render();
104 if ( VisibleInWindow > -1 )
106 if (event == vtkCommand::KeyPressEvent)
108 std::string KeyPress = isi->GetInteractor()->GetKeySym();
109 if (KeyPress == "f" || KeyPress == "F")
111 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
115 this->SM->SetPreset(0);
116 this->SM->UpdateWindowLevel();
121 this->SM->SetPreset(1);
122 this->SM->UpdateWindowLevel();
127 this->SM->SetPreset(2);
128 this->SM->UpdateWindowLevel();
134 this->SM->SetPreset(3);
135 this->SM->UpdateWindowLevel();
140 this->SM->SetPreset(4);
141 this->SM->UpdateWindowLevel();
146 this->SM->SetPreset(5);
147 this->SM->UpdateWindowLevel();
152 this->SM->SetColorMap(0);
153 this->SM->UpdateWindowLevel();
158 this->SM->SetColorMap(1);
159 this->SM->UpdateWindowLevel();
164 this->SM->SetColorMap(2);
165 this->SM->UpdateWindowLevel();
170 this->SM->SetColorMap(3);
171 this->SM->UpdateWindowLevel();
174 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
176 this->SM->SetPreset(7);
177 //this->SM->SetColorMap(1);
178 this->SM->UpdateWindowLevel();
181 if (KeyPress == "minus")
183 std::cout << "KeyPress : - " << std::endl;
184 this->SM->SetColorWindow(-this->SM->GetColorWindow());
185 this->SM->SetColorMap(-1);
186 this->SM->UpdateWindowLevel();
191 this->SM->ToggleContourSuperposition();
197 this->SM->ToggleInterpolation();
203 this->SM->SetCursorVisibility(0);
213 if (KeyPress == "r" || KeyPress=="R")
215 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
216 this->SM->GetSlicer(VisibleInWindow)->Render();
221 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
222 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
223 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
224 this->SM->UpdateViews(1,VisibleInWindow);
225 this->SM->UpdateLinked(VisibleInWindow);
228 if (KeyPress == "F5")
230 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
231 this->SM->GetSlicer(VisibleInWindow)->Render();
234 if (KeyPress == "F6")
236 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
237 this->SM->GetSlicer(VisibleInWindow)->Render();
240 if (KeyPress == "Up")
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 == "Down")
248 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
249 this->SM->UpdateSlice(VisibleInWindow);
250 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
252 if (KeyPress == "space")
256 if (KeyPress == "Left")
257 this->SM->SetPreviousTSlice(VisibleInWindow);
258 if (KeyPress == "Right")
259 this->SM->SetNextTSlice(VisibleInWindow);
261 if (KeyPress == "F1")
263 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
264 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
265 this->SM->UpdateSliceRange(VisibleInWindow);
266 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
268 if (KeyPress == "F2")
270 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
271 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
272 this->SM->UpdateSliceRange(VisibleInWindow);
273 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
275 if (KeyPress == "F3")
277 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
278 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
279 this->SM->UpdateSliceRange(VisibleInWindow);
280 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
284 //All type of mouse events
285 if (event == vtkCommand::LeaveEvent)
287 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
288 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
289 this->SM->GetSlicer(VisibleInWindow)->Render();
293 if (event == vtkCommand::StartWindowLevelEvent)
296 this->InitialWindow = this->SM->GetColorWindow();
297 this->InitialLevel = this->SM->GetColorLevel();
299 if (VisibleInWindow > -1)
301 mStartSlicer = VisibleInWindow;
306 if (event == vtkCommand::EndWindowLevelEvent)
312 if (VisibleInWindow > -1)
314 this->SM->Activated();
315 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
316 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
318 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
320 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
321 this->SM->UpdateSlice(VisibleInWindow);
322 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
324 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
327 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
329 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
331 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
332 this->SM->UpdateSlice(VisibleInWindow);
333 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
335 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
338 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
340 double xWorld=0; double yWorld=0; double zWorld=0;
342 //Move into World Coordinate
343 renderer->DisplayToNormalizedDisplay(x,y);
344 renderer->NormalizedDisplayToViewport(x,y);
345 renderer->ViewportToNormalizedViewport(x,y);
346 renderer->NormalizedViewportToView(x,y,z);
347 renderer->ViewToWorld(x,y,z);
348 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
350 case vtkImageViewer2::SLICE_ORIENTATION_XY:
353 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
354 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
355 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
358 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
360 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
361 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
362 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
366 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
367 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
368 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
369 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
374 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
375 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
378 this->SM->AddLandmark(xWorld,yWorld,zWorld,
379 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
380 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
383 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
385 this->SM->UpdateViews(1,VisibleInWindow);
386 this->SM->UpdateLinked(VisibleInWindow);
387 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
391 this->SM->GetSlicer(VisibleInWindow)->Render();
393 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
394 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
395 //this->SM->GetSlicer(VisibleInWindow)->Render();
398 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
400 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
401 // Adjust the window level here
402 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
403 double window = this->InitialWindow;
404 double level = this->InitialLevel;
406 this->SM->GetImage()->GetScalarRange(range);
408 // Compute normalized delta
409 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
410 isi->GetWindowLevelStartPosition()[0]) / size[0];
411 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
412 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
413 //Window is exponential in nature, use exponential to avoid falling into negative numbers
414 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
415 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
417 this->SM->SetColorWindow(window*dx);
418 this->SM->SetColorLevel(level-dy);
419 this->SM->SetPreset(6);
420 this->SM->UpdateWindowLevel();
426 //------------------------------------------------------------------------------
429 //------------------------------------------------------------------------------
430 void vvSlicerManagerCallback::Dolly(double factor, vtkRenderWindowInteractor *interactor)
432 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
433 vtkRenderer* renderer;
434 if (VisibleInWindow>-1)
435 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
441 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
442 double oldPos[3], newPos[3], distance[2];
443 vtkCamera *camera = renderer->GetActiveCamera();
444 camera->GetFocalPoint(viewFocus);
446 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
447 renderer->WorldToDisplay();
448 renderer->GetDisplayPoint(viewFocus);
450 focalDepth = viewFocus[2];
452 oldPos[0] = renderer->GetCenter()[0];
453 oldPos[1] = renderer->GetCenter()[1];
454 oldPos[2] = focalDepth;
456 distance[0] = 1/factor*
457 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
458 distance[1] = 1/factor*
459 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
461 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
462 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
463 newPos[2] = focalDepth;
465 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
466 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
467 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
468 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
469 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
471 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
472 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
473 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
474 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
475 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
477 motionVector[0] = newPos[0] - oldPos[0];
478 motionVector[1] = newPos[1] - oldPos[1];
479 motionVector[2] = newPos[2] - oldPos[2];
481 camera->GetFocalPoint(viewFocus);
482 camera->GetPosition(viewPoint);
483 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
484 motionVector[1] + viewFocus[1],
485 motionVector[2] + viewFocus[2]);
487 camera->SetPosition(motionVector[0] + viewPoint[0],
488 motionVector[1] + viewPoint[1],
489 motionVector[2] + viewPoint[2]);
491 if (camera->GetParallelProjection())
493 camera->SetParallelScale(camera->GetParallelScale() / factor);
497 camera->Dolly(factor);
500 if (interactor->GetLightFollowCamera())
502 renderer->UpdateLightsGeometryToFollowCamera();
504 renderer->ResetCameraClippingRange();
505 //interactor->Render();
508 void vvSlicerManagerCallback::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
510 double flyFrom[3], flyTo[3];
511 double d[3], focalPt[3], position[3], positionFrom[3];
513 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
514 vtkRenderer* renderer=NULL;
515 if (VisibleInWindow>-1)
516 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
520 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
521 interactor->GetEventPosition()[1], 0.0,
524 vtkAssemblyPath *path=NULL;
525 vtkAbstractPropPicker *picker;
526 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
528 path = picker->GetPath();
532 flyTo[0] = picker->GetPickPosition()[0];
533 flyTo[1] = picker->GetPickPosition()[1];
534 flyTo[2] = picker->GetPickPosition()[2];
535 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
536 renderer->GetActiveCamera()->GetPosition(positionFrom);
538 switch (slicer->GetSliceOrientation())
540 case vtkImageViewer2::SLICE_ORIENTATION_XY:
541 flyTo[2] = flyFrom[2];
544 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
545 flyTo[1] = flyFrom[1];
548 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
549 flyTo[0] = flyFrom[0];
556 d[i] = flyTo[i] - flyFrom[i];
558 double distance = vtkMath::Normalize(d);
559 double delta = distance/15;
561 for (i=1; i<=15; i++)
565 focalPt[j] = flyFrom[j] + d[j]*i*delta;
566 position[j] = positionFrom[j] + d[j]*i*delta;
568 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
569 renderer->GetActiveCamera()->SetPosition(position);
570 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
571 renderer->ResetCameraClippingRange();
572 interactor->Render();