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://oncora1.lyon.fnclcc.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"
42 //------------------------------------------------------------------------------
43 vvSlicerManagerCommand::vvSlicerManagerCommand()
48 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 //return the num of the current slicer if visible (-1 else)
53 int vvSlicerManagerCommand::FindSlicerNumber(vtkRenderWindow* renwin)
56 if (renwin != SM->GetSlicer(mSlicerNumber)->GetRenderWindow() ||
57 !SM->GetSlicer(mSlicerNumber)->GetRenderer()->GetDraw())
59 else rvalue = mSlicerNumber;
60 //std::cerr << this << ":" << mSlicerNumber << ": " << rvalue << endl;
63 //------------------------------------------------------------------------------
66 //------------------------------------------------------------------------------
67 void vvSlicerManagerCommand::Execute(vtkObject *caller,
69 void *vtkNotUsed(callData))
72 vvInteractorStyleNavigator *isi =
73 dynamic_cast<vvInteractorStyleNavigator *>(caller);
75 double x = isi->GetInteractor()->GetEventPosition()[0];
76 double y = isi->GetInteractor()->GetEventPosition()[1];
79 int VisibleInWindow = this->FindSlicerNumber(isi->GetInteractor()->GetRenderWindow());
80 vtkRenderer* renderer=NULL;
81 if (VisibleInWindow>-1)
82 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
87 if (event == vtkCommand::StartPickEvent && VisibleInWindow == -1) {
88 for (int i = 0; i < this->SM->NumberOfSlicers(); i++) {
89 if (this->SM->GetSlicer(i)->GetCursorVisibility()) {
90 this->SM->GetSlicer(i)->SetCursorVisibility(0);
91 this->SM->GetSlicer(i)->Render();
95 if ( VisibleInWindow > -1 ) {
96 if (event == vtkCommand::KeyPressEvent) {
97 std::string KeyPress = isi->GetInteractor()->GetKeySym();
98 if (KeyPress == "Tab") {
99 if(isi->GetInteractor()->GetShiftKey())
100 this->SM->PrevImage(VisibleInWindow);
102 this->SM->NextImage(VisibleInWindow);
105 if (KeyPress == "f" || KeyPress == "F") {
106 FlyToPosition(isi->GetInteractor(),this->SM->GetSlicer(VisibleInWindow));
109 if (KeyPress == "w") {
110 this->SM->SetLocalColorWindowing(VisibleInWindow);
113 if (KeyPress == "0") {
114 this->SM->SetPreset(0);
115 this->SM->UpdateWindowLevel();
118 if (KeyPress == "1") {
119 this->SM->SetPreset(1);
120 this->SM->UpdateWindowLevel();
123 if (KeyPress == "2") {
124 this->SM->SetPreset(2);
125 this->SM->UpdateWindowLevel();
129 if (KeyPress == "3") {
130 this->SM->SetPreset(3);
131 this->SM->UpdateWindowLevel();
134 if (KeyPress == "4") {
135 this->SM->SetPreset(4);
136 this->SM->UpdateWindowLevel();
139 if (KeyPress == "5") {
140 this->SM->SetPreset(5);
141 this->SM->UpdateWindowLevel();
144 if (KeyPress == "6") {
145 this->SM->SetColorMap(0);
146 this->SM->UpdateWindowLevel();
149 if (KeyPress == "7") {
150 this->SM->SetColorMap(1);
151 this->SM->UpdateWindowLevel();
154 if (KeyPress == "8") {
155 this->SM->SetColorMap(2);
156 this->SM->UpdateWindowLevel();
159 if (KeyPress == "9") {
160 this->SM->SetColorMap(3);
161 this->SM->UpdateWindowLevel();
164 if (KeyPress == "equal") { //keycodes are in vtkWin32RenderWindowInteractor
165 this->SM->SetPreset(7);
166 //this->SM->SetColorMap(1);
167 this->SM->UpdateWindowLevel();
170 if (KeyPress == "minus") {
171 this->SM->SetColorWindow(-this->SM->GetColorWindow());
172 this->SM->SetColorMap(-1);
173 this->SM->UpdateWindowLevel();
176 if (KeyPress == "c") {
177 this->SM->ToggleContourSuperposition();
181 if (KeyPress == "l") {
182 this->SM->ToggleInterpolation();
186 if (KeyPress == "h") {
187 this->SM->SetCursorVisibility(0);
191 if (KeyPress == "x") {
192 for(int i=0; i<SM->NumberOfSlicers(); i++) {
193 SM->RemoveActor("overlay",0);
199 if (KeyPress == "u") {
204 if (KeyPress == "r" || KeyPress=="R") {
205 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
206 this->SM->GetSlicer(VisibleInWindow)->Render();
209 if (KeyPress == "g") {
210 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
211 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
212 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
213 this->SM->UpdateViews(1,VisibleInWindow);
214 this->SM->UpdateLinked(VisibleInWindow);
217 if (KeyPress == "F5") {
218 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
219 this->SM->GetSlicer(VisibleInWindow)->Render();
222 if (KeyPress == "F6") {
223 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
224 this->SM->GetSlicer(VisibleInWindow)->Render();
227 if (KeyPress == "Up") {
228 // DD("------------ up");
229 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
230 //DD("------------ after set slice");
231 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
232 //DD("------------ after UpdateInfoOnCursorPosition");
233 this->SM->UpdateSlice(VisibleInWindow);
234 //DD("------------ after updateslice");
236 if (KeyPress == "Down") {
237 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
238 this->SM->UpdateSlice(VisibleInWindow);
239 this->SM->UpdateInfoOnCursorPosition(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->UpdateSliceRange(VisibleInWindow);
253 this->SM->UpdateInfoOnCursorPosition(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->UpdateSliceRange(VisibleInWindow);
259 this->SM->UpdateInfoOnCursorPosition(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->UpdateSliceRange(VisibleInWindow);
265 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
270 //All type of mouse events
271 if (event == vtkCommand::LeaveEvent) {
272 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
273 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
274 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) {
303 this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
307 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()) {
313 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
314 this->SM->UpdateSlice(VisibleInWindow);
315 this->SM->UpdateInfoOnCursorPosition(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()) {
321 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
322 this->SM->UpdateSlice(VisibleInWindow);
323 this->SM->UpdateInfoOnCursorPosition(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];
364 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
365 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
367 this->SM->AddLandmark(xWorld,yWorld,zWorld,
368 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
369 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
372 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
373 this->SM->UpdateViews(1,VisibleInWindow);
374 this->SM->UpdateLinked(VisibleInWindow);
375 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
377 this->SM->GetSlicer(VisibleInWindow)->Render();
379 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
380 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
381 //this->SM->GetSlicer(VisibleInWindow)->Render();
384 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
385 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
386 // Adjust the window level here
387 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
388 double window = this->InitialWindow;
389 double level = this->InitialLevel;
391 this->SM->GetImage()->GetScalarRange(range);
393 // Compute normalized delta
394 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
395 isi->GetWindowLevelStartPosition()[0]) / size[0];
396 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
397 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
398 //Window is exponential in nature, use exponential to avoid falling into negative numbers
399 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
400 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
402 this->SM->SetColorWindow(window*dx);
403 this->SM->SetColorLevel(level-dy);
404 this->SM->SetPreset(6);
405 this->SM->UpdateWindowLevel();
411 //------------------------------------------------------------------------------
414 //------------------------------------------------------------------------------
415 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
417 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
418 vtkRenderer* renderer;
419 if (VisibleInWindow>-1)
420 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
425 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
426 double oldPos[3], newPos[3], distance[2];
427 vtkCamera *camera = renderer->GetActiveCamera();
428 camera->GetFocalPoint(viewFocus);
430 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
431 renderer->WorldToDisplay();
432 renderer->GetDisplayPoint(viewFocus);
434 focalDepth = viewFocus[2];
436 oldPos[0] = renderer->GetCenter()[0];
437 oldPos[1] = renderer->GetCenter()[1];
438 oldPos[2] = focalDepth;
440 distance[0] = 1/factor*
441 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
442 distance[1] = 1/factor*
443 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
445 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
446 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
447 newPos[2] = focalDepth;
449 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
450 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
451 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
452 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
453 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
455 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
456 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
457 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
458 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
459 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
461 motionVector[0] = newPos[0] - oldPos[0];
462 motionVector[1] = newPos[1] - oldPos[1];
463 motionVector[2] = newPos[2] - oldPos[2];
465 camera->GetFocalPoint(viewFocus);
466 camera->GetPosition(viewPoint);
467 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
468 motionVector[1] + viewFocus[1],
469 motionVector[2] + viewFocus[2]);
471 camera->SetPosition(motionVector[0] + viewPoint[0],
472 motionVector[1] + viewPoint[1],
473 motionVector[2] + viewPoint[2]);
475 if (camera->GetParallelProjection()) {
476 camera->SetParallelScale(camera->GetParallelScale() / factor);
478 camera->Dolly(factor);
481 if (interactor->GetLightFollowCamera()) {
482 renderer->UpdateLightsGeometryToFollowCamera();
484 renderer->ResetCameraClippingRange();
485 //interactor->Render();
487 //------------------------------------------------------------------------------
490 //------------------------------------------------------------------------------
491 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
493 double flyFrom[3], flyTo[3];
494 double d[3], focalPt[3], position[3], positionFrom[3];
496 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
497 vtkRenderer* renderer=NULL;
498 if (VisibleInWindow>-1)
499 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
503 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
504 interactor->GetEventPosition()[1], 0.0,
507 vtkAssemblyPath *path=NULL;
508 vtkAbstractPropPicker *picker;
509 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
510 path = picker->GetPath();
512 if ( path != NULL ) {
513 flyTo[0] = picker->GetPickPosition()[0];
514 flyTo[1] = picker->GetPickPosition()[1];
515 flyTo[2] = picker->GetPickPosition()[2];
516 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
517 renderer->GetActiveCamera()->GetPosition(positionFrom);
519 switch (slicer->GetSliceOrientation()) {
520 case vtkImageViewer2::SLICE_ORIENTATION_XY:
521 flyTo[2] = flyFrom[2];
524 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
525 flyTo[1] = flyFrom[1];
528 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
529 flyTo[0] = flyFrom[0];
534 for (i=0; i<3; i++) {
535 d[i] = flyTo[i] - flyFrom[i];
537 double distance = vtkMath::Normalize(d);
538 double delta = distance/15;
540 for (i=1; i<=15; i++) {
541 for (j=0; j<3; j++) {
542 focalPt[j] = flyFrom[j] + d[j]*i*delta;
543 position[j] = positionFrom[j] + d[j]*i*delta;
545 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
546 renderer->GetActiveCamera()->SetPosition(position);
547 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
548 renderer->ResetCameraClippingRange();
549 interactor->Render();
553 //------------------------------------------------------------------------------