1 #include "vvSlicerManagerCommand.h"
2 #include "vvSlicerManager.h"
4 #include "vtkTextProperty.h"
5 #include "vtkRenderer.h"
6 #include "vtkImageActor.h"
7 #include "vtkRenderWindowInteractor.h"
8 #include "vtkPropPicker.h"
10 #include "vtkImageMapToWindowLevelColors.h"
11 #include "vtkLookupTable.h"
13 #include "vtkAbstractPropPicker.h"
14 #include "vtkAssemblyPath.h"
15 #include "vtkCornerAnnotation.h"
16 #include <vtkRenderWindow.h>
19 #include "vvInteractorStyleNavigator.h"
23 //------------------------------------------------------------------------------
24 vvSlicerManagerCommand::vvSlicerManagerCommand()
29 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
33 //return the num of the current slicer if visible (-1 else)
34 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
37 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
38 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
40 else rvalue = mSlicerNumber;
41 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
44 //------------------------------------------------------------------------------
47 //------------------------------------------------------------------------------
48 void vvSlicerManagerCommand::Execute(vtkObject *caller,
50 void *vtkNotUsed(callData))
53 vvInteractorStyleNavigator *isi =
54 dynamic_cast<vvInteractorStyleNavigator *>(caller);
57 double x = isi->GetInteractor()->GetEventPosition()[0];
58 double y = isi->GetInteractor()->GetEventPosition()[1];
61 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
62 vtkRenderer* renderer=NULL;
63 if (VisibleInWindow>-1)
64 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
67 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1)
69 for (int i = 0; i < this->SM->NumberOfSlicers(); i++)
71 if (this->SM->GetSlicer(i)->GetCursorVisibility())
73 this->SM->GetSlicer(i)->SetCursorVisibility(0);
74 this->SM->GetSlicer(i)->Render();
78 if ( VisibleInWindow > -1 )
80 if (event == vtkCommand::KeyPressEvent)
82 std::string KeyPress = isi->GetInteractor()->GetKeySym();
83 if (KeyPress == "f" || KeyPress == "F")
85 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
89 this->SM->SetLocalColorWindowing(VisibleInWindow);
94 this->SM->SetPreset(0);
95 this->SM->UpdateWindowLevel();
100 this->SM->SetPreset(1);
101 this->SM->UpdateWindowLevel();
106 this->SM->SetPreset(2);
107 this->SM->UpdateWindowLevel();
113 this->SM->SetPreset(3);
114 this->SM->UpdateWindowLevel();
119 this->SM->SetPreset(4);
120 this->SM->UpdateWindowLevel();
125 this->SM->SetPreset(5);
126 this->SM->UpdateWindowLevel();
131 this->SM->SetColorMap(0);
132 this->SM->UpdateWindowLevel();
137 this->SM->SetColorMap(1);
138 this->SM->UpdateWindowLevel();
143 this->SM->SetColorMap(2);
144 this->SM->UpdateWindowLevel();
149 this->SM->SetColorMap(3);
150 this->SM->UpdateWindowLevel();
153 if (KeyPress == "equal") //keycodes are in vtkWin32RenderWindowInteractor
155 this->SM->SetPreset(7);
156 //this->SM->SetColorMap(1);
157 this->SM->UpdateWindowLevel();
160 if (KeyPress == "minus")
162 this->SM->SetColorWindow(-this->SM->GetColorWindow());
163 this->SM->SetColorMap(-1);
164 this->SM->UpdateWindowLevel();
169 this->SM->ToggleContourSuperposition();
175 this->SM->ToggleInterpolation();
181 this->SM->SetCursorVisibility(0);
191 if (KeyPress == "r" || KeyPress=="R")
193 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
194 this->SM->GetSlicer(VisibleInWindow)->Render();
199 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
200 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
201 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
202 this->SM->UpdateViews(1,VisibleInWindow);
203 this->SM->UpdateLinked(VisibleInWindow);
206 if (KeyPress == "F5")
208 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
209 this->SM->GetSlicer(VisibleInWindow)->Render();
212 if (KeyPress == "F6")
214 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
215 this->SM->GetSlicer(VisibleInWindow)->Render();
218 if (KeyPress == "Up")
220 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
221 this->SM->UpdateSlice(VisibleInWindow);
222 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
224 if (KeyPress == "Down")
226 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
227 this->SM->UpdateSlice(VisibleInWindow);
228 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
230 if (KeyPress == "space")
234 if (KeyPress == "Left")
235 this->SM->SetPreviousTSlice(VisibleInWindow);
236 if (KeyPress == "Right")
237 this->SM->SetNextTSlice(VisibleInWindow);
239 if (KeyPress == "F1")
241 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
242 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
243 this->SM->UpdateSliceRange(VisibleInWindow);
244 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
246 if (KeyPress == "F2")
248 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
249 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
250 this->SM->UpdateSliceRange(VisibleInWindow);
251 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
253 if (KeyPress == "F3")
255 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
256 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
257 this->SM->UpdateSliceRange(VisibleInWindow);
258 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
262 //All type of mouse events
263 if (event == vtkCommand::LeaveEvent)
265 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
266 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
267 this->SM->GetSlicer(VisibleInWindow)->Render();
271 if (event == vtkCommand::StartWindowLevelEvent)
274 this->InitialWindow = this->SM->GetColorWindow();
275 this->InitialLevel = this->SM->GetColorLevel();
277 if (VisibleInWindow > -1)
279 mStartSlicer = VisibleInWindow;
284 if (event == vtkCommand::EndWindowLevelEvent)
290 if (VisibleInWindow > -1)
292 this->SM->Activated();
293 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
294 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
296 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey())
298 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
299 this->SM->UpdateSlice(VisibleInWindow);
300 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
302 else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey())
305 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
307 else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey())
309 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
310 this->SM->UpdateSlice(VisibleInWindow);
311 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
313 else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey())
316 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
318 double xWorld=0; double yWorld=0; double zWorld=0;
320 //Move into World Coordinate
321 renderer->DisplayToNormalizedDisplay(x,y);
322 renderer->NormalizedDisplayToViewport(x,y);
323 renderer->ViewportToNormalizedViewport(x,y);
324 renderer->NormalizedViewportToView(x,y,z);
325 renderer->ViewToWorld(x,y,z);
326 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation())
328 case vtkImageViewer2::SLICE_ORIENTATION_XY:
331 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
332 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
333 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
336 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
338 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
339 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
340 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
344 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
345 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
346 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
347 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
352 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
353 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
356 this->SM->AddLandmark(xWorld,yWorld,zWorld,
357 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
358 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
361 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent)
363 this->SM->UpdateViews(1,VisibleInWindow);
364 this->SM->UpdateLinked(VisibleInWindow);
365 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
369 this->SM->GetSlicer(VisibleInWindow)->Render();
371 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
372 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
373 //this->SM->GetSlicer(VisibleInWindow)->Render();
376 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1)
378 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
379 // Adjust the window level here
380 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
381 double window = this->InitialWindow;
382 double level = this->InitialLevel;
384 this->SM->GetImage()->GetScalarRange(range);
386 // Compute normalized delta
387 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
388 isi->GetWindowLevelStartPosition()[0]) / size[0];
389 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
390 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
391 //Window is exponential in nature, use exponential to avoid falling into negative numbers
392 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
393 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
395 this->SM->SetColorWindow(window*dx);
396 this->SM->SetColorLevel(level-dy);
397 this->SM->SetPreset(6);
398 this->SM->UpdateWindowLevel();
404 //------------------------------------------------------------------------------
407 //------------------------------------------------------------------------------
408 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
410 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
411 vtkRenderer* renderer;
412 if (VisibleInWindow>-1)
413 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
419 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
420 double oldPos[3], newPos[3], distance[2];
421 vtkCamera *camera = renderer->GetActiveCamera();
422 camera->GetFocalPoint(viewFocus);
424 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
425 renderer->WorldToDisplay();
426 renderer->GetDisplayPoint(viewFocus);
428 focalDepth = viewFocus[2];
430 oldPos[0] = renderer->GetCenter()[0];
431 oldPos[1] = renderer->GetCenter()[1];
432 oldPos[2] = focalDepth;
434 distance[0] = 1/factor*
435 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
436 distance[1] = 1/factor*
437 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
439 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
440 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
441 newPos[2] = focalDepth;
443 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
444 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
445 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
446 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
447 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
449 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
450 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
451 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
452 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
453 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
455 motionVector[0] = newPos[0] - oldPos[0];
456 motionVector[1] = newPos[1] - oldPos[1];
457 motionVector[2] = newPos[2] - oldPos[2];
459 camera->GetFocalPoint(viewFocus);
460 camera->GetPosition(viewPoint);
461 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
462 motionVector[1] + viewFocus[1],
463 motionVector[2] + viewFocus[2]);
465 camera->SetPosition(motionVector[0] + viewPoint[0],
466 motionVector[1] + viewPoint[1],
467 motionVector[2] + viewPoint[2]);
469 if (camera->GetParallelProjection())
471 camera->SetParallelScale(camera->GetParallelScale() / factor);
475 camera->Dolly(factor);
478 if (interactor->GetLightFollowCamera())
480 renderer->UpdateLightsGeometryToFollowCamera();
482 renderer->ResetCameraClippingRange();
483 //interactor->Render();
486 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
488 double flyFrom[3], flyTo[3];
489 double d[3], focalPt[3], position[3], positionFrom[3];
491 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
492 vtkRenderer* renderer=NULL;
493 if (VisibleInWindow>-1)
494 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
498 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
499 interactor->GetEventPosition()[1], 0.0,
502 vtkAssemblyPath *path=NULL;
503 vtkAbstractPropPicker *picker;
504 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker())))
506 path = picker->GetPath();
510 flyTo[0] = picker->GetPickPosition()[0];
511 flyTo[1] = picker->GetPickPosition()[1];
512 flyTo[2] = picker->GetPickPosition()[2];
513 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
514 renderer->GetActiveCamera()->GetPosition(positionFrom);
516 switch (slicer->GetSliceOrientation())
518 case vtkImageViewer2::SLICE_ORIENTATION_XY:
519 flyTo[2] = flyFrom[2];
522 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
523 flyTo[1] = flyFrom[1];
526 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
527 flyTo[0] = flyFrom[0];
534 d[i] = flyTo[i] - flyFrom[i];
536 double distance = vtkMath::Normalize(d);
537 double delta = distance/15;
539 for (i=1; i<=15; i++)
543 focalPt[j] = flyFrom[j] + d[j]*i*delta;
544 position[j] = positionFrom[j] + d[j]*i*delta;
546 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
547 renderer->GetActiveCamera()->SetPosition(position);
548 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
549 renderer->ResetCameraClippingRange();
550 interactor->Render();