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->GetNumberOfSlicers(); i++) {
89 if (this->SM->GetSlicer(i)->GetCursorVisibility()) {
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 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->SetCursorAndCornerAnnotationVisibility(0);
191 if (KeyPress == "x") {
192 for(int i=0; i<SM->GetNumberOfSlicers(); 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();
207 this->SM->UpdateLinkedNavigation( this->SM->GetSlicer(VisibleInWindow) );
210 if (KeyPress == "g") {
211 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
212 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
213 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
214 this->SM->UpdateViews(1,VisibleInWindow);
215 this->SM->UpdateLinked(VisibleInWindow);
218 if (KeyPress == "F5") {
219 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
220 this->SM->GetSlicer(VisibleInWindow)->Render();
223 if (KeyPress == "F6") {
224 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
225 this->SM->GetSlicer(VisibleInWindow)->Render();
228 if (KeyPress == "Up") {
229 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
230 this->SM->UpdateSlice(VisibleInWindow);
232 if (KeyPress == "Down") {
233 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
234 this->SM->UpdateSlice(VisibleInWindow);
236 if (KeyPress == "space") {
239 if (KeyPress == "Left")
240 this->SM->SetPreviousTSlice(VisibleInWindow);
241 if (KeyPress == "Right")
242 this->SM->SetNextTSlice(VisibleInWindow);
244 if (KeyPress == "F2") {
245 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
246 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
247 this->SM->UpdateSliceRange(VisibleInWindow);
249 if (KeyPress == "F3") {
250 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
251 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
252 this->SM->UpdateSliceRange(VisibleInWindow);
254 if (KeyPress == "F4") {
255 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
256 this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
257 this->SM->UpdateSliceRange(VisibleInWindow);
262 //All type of mouse events
263 if (event == vtkCommand::LeaveEvent) {
264 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
265 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
266 this->SM->GetSlicer(VisibleInWindow)->Render();
271 // Mouse release HERE
272 if (event == vtkCommand::EndPickEvent) {
273 // DD(VisibleInWindow);
274 if (VisibleInWindow > -1)
275 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
276 return; // no return !!!! ???
279 if (event == vtkCommand::StartWindowLevelEvent) {
281 this->InitialWindow = this->SM->GetColorWindow();
282 this->InitialLevel = this->SM->GetColorLevel();
284 if (VisibleInWindow > -1) {
285 mStartSlicer = VisibleInWindow;
290 if (event == vtkCommand::EndWindowLevelEvent) {
294 if (event == vtkCommand::EndInteractionEvent) {
296 this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
300 if (VisibleInWindow > -1) {
301 this->SM->Activated();
302 //if(!this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->GetVisibility())
303 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetVisibility(1);
305 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey()) {
307 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
308 this->SM->UpdateSlice(VisibleInWindow);
309 } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
311 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
312 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
313 } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
315 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
316 this->SM->UpdateSlice(VisibleInWindow);
317 } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
319 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
320 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
326 //Move into World Coordinate
327 renderer->DisplayToNormalizedDisplay(x,y);
328 renderer->NormalizedDisplayToViewport(x,y);
329 renderer->ViewportToNormalizedViewport(x,y);
330 renderer->NormalizedViewportToView(x,y,z);
331 renderer->ViewToWorld(x,y,z);
332 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
333 case vtkImageViewer2::SLICE_ORIENTATION_XY:
336 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
337 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
338 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
341 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
343 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
344 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
345 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
349 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
350 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
351 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
352 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
357 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
358 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
360 this->SM->AddLandmark(xWorld,yWorld,zWorld,
361 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
362 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
365 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
367 this->SM->UpdateViews(1,VisibleInWindow);
368 this->SM->UpdateLinked(VisibleInWindow);
369 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
371 this->SM->GetSlicer(VisibleInWindow)->Render();
373 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
374 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
375 //this->SM->GetSlicer(VisibleInWindow)->Render();
378 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
379 this->SM->GetSlicer(mStartSlicer)->GetAnnotation()->SetVisibility(1);
380 // Adjust the window level here
381 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
382 double window = this->InitialWindow;
383 double level = this->InitialLevel;
385 this->SM->GetImage()->GetScalarRange(range);
387 // Compute normalized delta
388 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
389 isi->GetWindowLevelStartPosition()[0]) / size[0];
390 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
391 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
392 //Window is exponential in nature, use exponential to avoid falling into negative numbers
393 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
394 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
396 this->SM->SetColorWindow(window*dx);
397 this->SM->SetColorLevel(level-dy);
398 this->SM->SetPreset(6);
399 this->SM->UpdateWindowLevel();
405 //------------------------------------------------------------------------------
408 //------------------------------------------------------------------------------
409 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
411 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
412 vtkRenderer* renderer;
413 if (VisibleInWindow>-1)
414 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()) {
470 camera->SetParallelScale(camera->GetParallelScale() / factor);
472 camera->Dolly(factor);
475 if (interactor->GetLightFollowCamera()) {
476 renderer->UpdateLightsGeometryToFollowCamera();
478 renderer->ResetCameraClippingRange();
479 //interactor->Render();
481 //------------------------------------------------------------------------------
484 //------------------------------------------------------------------------------
485 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
487 double flyFrom[3], flyTo[3];
488 double d[3], focalPt[3], position[3], positionFrom[3];
490 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
491 vtkRenderer* renderer=NULL;
492 if (VisibleInWindow>-1)
493 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
497 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
498 interactor->GetEventPosition()[1], 0.0,
501 vtkAssemblyPath *path=NULL;
502 vtkAbstractPropPicker *picker;
503 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
504 path = picker->GetPath();
506 if ( path != NULL ) {
507 flyTo[0] = picker->GetPickPosition()[0];
508 flyTo[1] = picker->GetPickPosition()[1];
509 flyTo[2] = picker->GetPickPosition()[2];
510 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
511 renderer->GetActiveCamera()->GetPosition(positionFrom);
513 switch (slicer->GetSliceOrientation()) {
514 case vtkImageViewer2::SLICE_ORIENTATION_XY:
515 flyTo[2] = flyFrom[2];
518 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
519 flyTo[1] = flyFrom[1];
522 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
523 flyTo[0] = flyFrom[0];
528 for (i=0; i<3; i++) {
529 d[i] = flyTo[i] - flyFrom[i];
531 double distance = vtkMath::Normalize(d);
532 double delta = distance/15;
534 for (i=1; i<=15; i++) {
535 for (j=0; j<3; j++) {
536 focalPt[j] = flyFrom[j] + d[j]*i*delta;
537 position[j] = positionFrom[j] + d[j]*i*delta;
539 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
540 renderer->GetActiveCamera()->SetPosition(position);
541 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
542 renderer->ResetCameraClippingRange();
543 interactor->Render();
547 //------------------------------------------------------------------------------