1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #include "vvSlicerManagerCommand.h"
19 #include "vvSlicerManager.h"
21 #include <vtkTextProperty.h>
22 #include <vtkRenderer.h>
23 #include <vtkImageActor.h>
24 #include <vtkRenderWindowInteractor.h>
25 #include <vtkPropPicker.h>
26 #include <vtkCamera.h>
27 #include <vtkImageMapToWindowLevelColors.h>
28 #include <vtkLookupTable.h>
30 #include <vtkAbstractPropPicker.h>
31 #include <vtkAssemblyPath.h>
32 #include <vtkCornerAnnotation.h>
33 #include <vtkRenderWindow.h>
35 #include <vtkImageReslice.h>
37 #include "vvInteractorStyleNavigator.h"
38 #include "vtkTransform.h"
41 //------------------------------------------------------------------------------
42 vvSlicerManagerCommand::vvSlicerManagerCommand()
47 //------------------------------------------------------------------------------
50 //------------------------------------------------------------------------------
51 //return the num of the current slicer if visible (-1 else)
52 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
55 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
56 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
58 else rvalue = mSlicerNumber;
59 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
62 //------------------------------------------------------------------------------
65 //------------------------------------------------------------------------------
66 void vvSlicerManagerCommand::Execute(vtkObject *caller,
68 void *vtkNotUsed(callData))
71 vvInteractorStyleNavigator *isi =
72 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();
86 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1) {
87 for (int i = 0; i < this->SM->GetNumberOfSlicers(); i++) {
88 if (this->SM->GetSlicer(i)->GetCursorVisibility() && !this->SM->IsLinked()) {
89 this->SM->GetSlicer(i)->SetCursorVisibility(0);
90 this->SM->GetSlicer(i)->SetCornerAnnotationVisibility(0);
91 this->SM->GetSlicer(i)->Render();
95 if ( VisibleInWindow > -1 ) {
96 if (event == vtkCommand::KeyPressEvent) {
97 std::string KeyPress = isi->GetInteractor()->GetKeySym();
98 bool bCtrlKey = isi->GetInteractor()->GetControlKey();
99 if (KeyPress == "Tab") {
100 if(isi->GetInteractor()->GetShiftKey())
101 this->SM->PrevImage(VisibleInWindow);
103 this->SM->NextImage(VisibleInWindow);
106 if (KeyPress == "f" || KeyPress == "F") {
107 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
110 if (KeyPress == "w") {
111 this->SM->SetLocalColorWindowing(VisibleInWindow, bCtrlKey);
114 if (KeyPress == "0" || KeyPress == "KP_0") {
115 this->SM->SetPreset(WL_AUTO);
116 this->SM->UpdateWindowLevel();
119 if (KeyPress == "1" || KeyPress == "KP_1") {
120 this->SM->SetPreset(WL_HOUNSFIELD);
121 this->SM->UpdateWindowLevel();
124 if (KeyPress == "2" || KeyPress == "KP_2") {
125 this->SM->SetPreset(WL_SOFTTISSUE);
126 this->SM->UpdateWindowLevel();
130 if (KeyPress == "3" || KeyPress == "KP_3") {
131 this->SM->SetPreset(WL_LUNGS);
132 this->SM->UpdateWindowLevel();
135 if (KeyPress == "4" || KeyPress == "KP_4") {
136 this->SM->SetPreset(WL_BONES);
137 this->SM->UpdateWindowLevel();
140 if (KeyPress == "5" || KeyPress == "KP_5") {
141 this->SM->SetPreset(WL_HEAD);
142 this->SM->UpdateWindowLevel();
145 if (KeyPress == "6" || KeyPress == "KP_6") {
146 this->SM->SetColorMap(0);
147 this->SM->UpdateWindowLevel();
150 if (KeyPress == "7" || KeyPress == "KP_7") {
151 this->SM->SetColorMap(1);
152 this->SM->UpdateWindowLevel();
155 if (KeyPress == "8" || KeyPress == "KP_8") {
156 this->SM->SetColorMap(2);
157 this->SM->UpdateWindowLevel();
160 if (KeyPress == "9" || KeyPress == "KP_9") {
161 this->SM->SetColorMap(3);
162 this->SM->UpdateWindowLevel();
165 if (KeyPress == "equal") { //keycodes are in vtkWin32RenderWindowInteractor
166 this->SM->SetPreset(WL_VENTILATION);
167 //this->SM->SetColorMap(1);
168 this->SM->UpdateWindowLevel();
171 if (KeyPress == "minus") {
172 this->SM->SetColorWindow(-this->SM->GetColorWindow());
173 this->SM->SetColorMap(-1);
174 this->SM->UpdateWindowLevel();
177 if (KeyPress == "c") {
178 this->SM->ToggleContourSuperposition();
182 if (KeyPress == "l") {
183 this->SM->ToggleInterpolation();
187 if (KeyPress == "h") {
188 this->SM->SetCursorAndCornerAnnotationVisibility(0);
192 if (KeyPress == "x") {
193 for(int i=0; i<SM->GetNumberOfSlicers(); i++) {
194 SM->RemoveActor("overlay",0);
200 if (KeyPress == "u") {
205 if (KeyPress == "r" || KeyPress=="R") {
206 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
207 this->SM->GetSlicer(VisibleInWindow)->Render();
208 this->SM->UpdateLinkedNavigation( this->SM->GetSlicer(VisibleInWindow) );
211 if (KeyPress == "g") {
213 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(0,0,0,0);
215 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
216 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
217 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
219 this->SM->UpdateViews(1,VisibleInWindow);
220 this->SM->UpdateLinked(VisibleInWindow);
223 if (KeyPress == "F5") {
224 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
225 this->SM->GetSlicer(VisibleInWindow)->Render();
228 if (KeyPress == "F6") {
229 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
230 this->SM->GetSlicer(VisibleInWindow)->Render();
233 if (KeyPress == "Up") {
234 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
235 this->SM->UpdateSlice(VisibleInWindow);
237 if (KeyPress == "Down") {
238 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
239 this->SM->UpdateSlice(VisibleInWindow);
241 if (KeyPress == "space") {
244 if (KeyPress == "Left")
245 this->SM->SetPreviousTSlice(VisibleInWindow);
246 if (KeyPress == "Right")
247 this->SM->SetNextTSlice(VisibleInWindow);
249 if (KeyPress == "F2") {
250 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
251 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
252 this->SM->SetSliceOrientation(VisibleInWindow, 0);
253 this->SM->UpdateSliceRange(VisibleInWindow);
255 if (KeyPress == "F3") {
256 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
257 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
258 this->SM->SetSliceOrientation(VisibleInWindow, 1);
259 this->SM->UpdateSliceRange(VisibleInWindow);
261 if (KeyPress == "F4") {
262 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
263 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
264 this->SM->SetSliceOrientation(VisibleInWindow, 2);
265 this->SM->UpdateSliceRange(VisibleInWindow);
268 this->SM->EmitKeyPressed(KeyPress);
271 //All type of mouse events
272 if (event == vtkCommand::LeaveEvent) {
273 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
274 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetMaxCurrentTSlice());
275 this->SM->GetSlicer(VisibleInWindow)->Render();
279 // Mouse release HERE
280 if (event == vtkCommand::EndPickEvent) {
281 // DD(VisibleInWindow);
282 if (VisibleInWindow > -1)
283 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
284 return; // no return !!!! ???
287 if (event == vtkCommand::StartWindowLevelEvent) {
289 this->InitialWindow = this->SM->GetColorWindow();
290 this->InitialLevel = this->SM->GetColorLevel();
292 if (VisibleInWindow > -1) {
293 mStartSlicer = VisibleInWindow;
298 if (event == vtkCommand::EndWindowLevelEvent) {
302 if (event == vtkCommand::EndInteractionEvent) {
304 this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
308 if (VisibleInWindow > -1) {
309 this->SM->Activated();
310 this->SM->GetSlicer(VisibleInWindow)->SetCornerAnnotationVisibility(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 } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
318 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
319 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
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 } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
326 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
327 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
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()) {
340 case vtkImageViewer2::SLICE_ORIENTATION_XY:
343 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
344 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
345 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
348 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
350 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
351 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
352 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
356 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
357 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
358 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
359 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
366 // this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
367 // this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
368 // // We propagate the mouse position
369 // this->SM->EmitMousePositionUpdated(VisibleInWindow);
371 double p[3]; p[0] = xWorld; p[1] = yWorld; p[2] = zWorld;
373 this->SM->GetSlicer(VisibleInWindow)->GetSlicingTransform()->TransformPoint(p, pt);
375 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(pt[0],pt[1],pt[2],
376 this->SM->GetSlicer(VisibleInWindow)->GetMaxCurrentTSlice());
378 // We propagate the mouse position
379 this->SM->EmitMousePositionUpdated(VisibleInWindow);
381 //>>>>>>> 921642d767beba2442dacc8fdb40dc36396e1b7d
384 double pLand[3]; pLand[0] = xWorld; pLand[1] = yWorld; pLand[2] = zWorld;
386 this->SM->GetSlicer(VisibleInWindow)->GetConcatenatedTransform()->TransformPoint(pLand, ptLand);
387 this->SM->AddNewLandmark(ptLand[0],ptLand[1],ptLand[2],
388 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
389 this->SM->GetSlicer(VisibleInWindow)->RemoveLandmarks();
390 //this->SM->GetSlicer(VisibleInWindow)->DisplayLandmarks();
393 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
395 this->SM->UpdateViews(1,VisibleInWindow);
396 this->SM->UpdateLinked(VisibleInWindow);
397 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
399 this->SM->GetSlicer(VisibleInWindow)->Render();
401 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
402 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
403 //this->SM->GetSlicer(VisibleInWindow)->Render();
406 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
407 this->SM->GetSlicer(mStartSlicer)->SetCornerAnnotationVisibility(1);
408 // Adjust the window level here
409 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
410 double window = this->InitialWindow;
411 double level = this->InitialLevel;
413 this->SM->GetImage()->GetScalarRange(range);
415 // Compute normalized delta
416 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
417 isi->GetWindowLevelStartPosition()[0]) / size[0];
418 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
419 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
420 //Window is exponential in nature, use exponential to avoid falling into negative numbers
421 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
422 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
424 this->SM->SetColorWindow(window*dx);
425 this->SM->SetColorLevel(level-dy);
426 this->SM->SetPreset(WL_USER);
428 this->SM->UpdateWindowLevel();
433 //------------------------------------------------------------------------------
436 //------------------------------------------------------------------------------
437 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
439 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
440 vtkRenderer* renderer;
441 if (VisibleInWindow>-1)
442 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
447 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
448 double oldPos[3], newPos[3], distance[2];
449 vtkCamera *camera = renderer->GetActiveCamera();
450 camera->GetFocalPoint(viewFocus);
452 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
453 renderer->WorldToDisplay();
454 renderer->GetDisplayPoint(viewFocus);
456 focalDepth = viewFocus[2];
458 oldPos[0] = renderer->GetCenter()[0];
459 oldPos[1] = renderer->GetCenter()[1];
460 oldPos[2] = focalDepth;
462 distance[0] = 1/factor*
463 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
464 distance[1] = 1/factor*
465 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
467 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
468 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
469 newPos[2] = focalDepth;
471 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
472 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
473 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
474 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
475 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
477 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
478 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
479 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
480 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
481 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
483 motionVector[0] = newPos[0] - oldPos[0];
484 motionVector[1] = newPos[1] - oldPos[1];
485 motionVector[2] = newPos[2] - oldPos[2];
487 camera->GetFocalPoint(viewFocus);
488 camera->GetPosition(viewPoint);
489 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
490 motionVector[1] + viewFocus[1],
491 motionVector[2] + viewFocus[2]);
493 camera->SetPosition(motionVector[0] + viewPoint[0],
494 motionVector[1] + viewPoint[1],
495 motionVector[2] + viewPoint[2]);
497 if (camera->GetParallelProjection()) {
498 camera->SetParallelScale(camera->GetParallelScale() / factor);
500 camera->Dolly(factor);
503 if (interactor->GetLightFollowCamera()) {
504 renderer->UpdateLightsGeometryToFollowCamera();
506 renderer->ResetCameraClippingRange();
507 //interactor->Render();
509 //------------------------------------------------------------------------------
512 //------------------------------------------------------------------------------
513 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
515 double flyFrom[3], flyTo[3];
516 double d[3], focalPt[3], position[3], positionFrom[3];
518 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
519 vtkRenderer* renderer=NULL;
520 if (VisibleInWindow>-1)
521 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
525 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
526 interactor->GetEventPosition()[1], 0.0,
529 vtkAssemblyPath *path=NULL;
530 vtkAbstractPropPicker *picker;
531 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
532 path = picker->GetPath();
534 if ( path != NULL ) {
535 flyTo[0] = picker->GetPickPosition()[0];
536 flyTo[1] = picker->GetPickPosition()[1];
537 flyTo[2] = picker->GetPickPosition()[2];
538 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
539 renderer->GetActiveCamera()->GetPosition(positionFrom);
541 switch (slicer->GetSliceOrientation()) {
542 case vtkImageViewer2::SLICE_ORIENTATION_XY:
543 flyTo[2] = flyFrom[2];
546 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
547 flyTo[1] = flyFrom[1];
550 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
551 flyTo[0] = flyFrom[0];
556 for (i=0; i<3; i++) {
557 d[i] = flyTo[i] - flyFrom[i];
559 double distance = vtkMath::Normalize(d);
560 double delta = distance/15;
562 for (i=1; i<=15; i++) {
563 for (j=0; j<3; j++) {
564 focalPt[j] = flyFrom[j] + d[j]*i*delta;
565 position[j] = positionFrom[j] + d[j]*i*delta;
567 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
568 renderer->GetActiveCamera()->SetPosition(position);
569 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
570 renderer->ResetCameraClippingRange();
571 interactor->Render();
575 //------------------------------------------------------------------------------