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 std::cout << "coucou End" << std::endl;
282 // DD(VisibleInWindow);
283 if (VisibleInWindow > -1)
284 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
285 return; // no return !!!! ???
288 if (event == vtkCommand::StartWindowLevelEvent) {
290 this->InitialWindow = this->SM->GetColorWindow();
291 this->InitialLevel = this->SM->GetColorLevel();
293 if (VisibleInWindow > -1) {
294 mStartSlicer = VisibleInWindow;
299 if (event == vtkCommand::EndWindowLevelEvent) {
303 if (event == vtkCommand::EndInteractionEvent) {
305 this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
309 if (VisibleInWindow > -1) {
310 this->SM->Activated();
311 this->SM->GetSlicer(VisibleInWindow)->SetCornerAnnotationVisibility(1);
313 if (event == vtkCommand::MouseWheelForwardEvent && !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::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
319 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
320 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
321 } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
323 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
324 this->SM->UpdateSlice(VisibleInWindow);
325 } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
327 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
328 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
334 //Move into World Coordinate
335 renderer->DisplayToNormalizedDisplay(x,y);
336 renderer->NormalizedDisplayToViewport(x,y);
337 renderer->ViewportToNormalizedViewport(x,y);
338 renderer->NormalizedViewportToView(x,y,z);
339 renderer->ViewToWorld(x,y,z);
340 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
341 case vtkImageViewer2::SLICE_ORIENTATION_XY:
344 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
345 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
346 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
349 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
351 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
352 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
353 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
357 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
358 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
359 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
360 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
367 // this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
368 // this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
369 // // We propagate the mouse position
370 // this->SM->EmitMousePositionUpdated(VisibleInWindow);
372 double p[3]; p[0] = xWorld; p[1] = yWorld; p[2] = zWorld;
374 this->SM->GetSlicer(VisibleInWindow)->GetSlicingTransform()->TransformPoint(p, pt);
376 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(pt[0],pt[1],pt[2],
377 this->SM->GetSlicer(VisibleInWindow)->GetMaxCurrentTSlice());
379 // We propagate the mouse position
380 this->SM->EmitMousePositionUpdated(VisibleInWindow);
382 //>>>>>>> 921642d767beba2442dacc8fdb40dc36396e1b7d
385 double pLand[3]; pLand[0] = xWorld; pLand[1] = yWorld; pLand[2] = zWorld;
387 this->SM->GetSlicer(VisibleInWindow)->GetConcatenatedTransform()->TransformPoint(pLand, ptLand);
388 this->SM->AddNewLandmark(ptLand[0],ptLand[1],ptLand[2],
389 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
390 this->SM->GetSlicer(VisibleInWindow)->RemoveLandmarks();
391 //this->SM->GetSlicer(VisibleInWindow)->DisplayLandmarks();
394 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
395 std::cout << "coucou Start" << std::endl;
397 this->SM->UpdateViews(1,VisibleInWindow);
398 this->SM->UpdateLinked(VisibleInWindow);
399 this->SM->ForceUpdateDisplayExtent();
401 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
403 this->SM->GetSlicer(VisibleInWindow)->Render();
405 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
406 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
407 //this->SM->GetSlicer(VisibleInWindow)->Render();
410 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
411 this->SM->GetSlicer(mStartSlicer)->SetCornerAnnotationVisibility(1);
412 // Adjust the window level here
413 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
414 double window = this->InitialWindow;
415 double level = this->InitialLevel;
417 this->SM->GetImage()->GetScalarRange(range);
419 // Compute normalized delta
420 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
421 isi->GetWindowLevelStartPosition()[0]) / size[0];
422 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
423 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
424 //Window is exponential in nature, use exponential to avoid falling into negative numbers
425 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
426 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
428 this->SM->SetColorWindow(window*dx);
429 this->SM->SetColorLevel(level-dy);
430 this->SM->SetPreset(WL_USER);
432 this->SM->UpdateWindowLevel();
437 //------------------------------------------------------------------------------
440 //------------------------------------------------------------------------------
441 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
443 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
444 vtkRenderer* renderer;
445 if (VisibleInWindow>-1)
446 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
451 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
452 double oldPos[3], newPos[3], distance[2];
453 vtkCamera *camera = renderer->GetActiveCamera();
454 camera->GetFocalPoint(viewFocus);
456 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
457 renderer->WorldToDisplay();
458 renderer->GetDisplayPoint(viewFocus);
460 focalDepth = viewFocus[2];
462 oldPos[0] = renderer->GetCenter()[0];
463 oldPos[1] = renderer->GetCenter()[1];
464 oldPos[2] = focalDepth;
466 distance[0] = 1/factor*
467 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
468 distance[1] = 1/factor*
469 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
471 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
472 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
473 newPos[2] = focalDepth;
475 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
476 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
477 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
478 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
479 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
481 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
482 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
483 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
484 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
485 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
487 motionVector[0] = newPos[0] - oldPos[0];
488 motionVector[1] = newPos[1] - oldPos[1];
489 motionVector[2] = newPos[2] - oldPos[2];
491 camera->GetFocalPoint(viewFocus);
492 camera->GetPosition(viewPoint);
493 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
494 motionVector[1] + viewFocus[1],
495 motionVector[2] + viewFocus[2]);
497 camera->SetPosition(motionVector[0] + viewPoint[0],
498 motionVector[1] + viewPoint[1],
499 motionVector[2] + viewPoint[2]);
501 if (camera->GetParallelProjection()) {
502 camera->SetParallelScale(camera->GetParallelScale() / factor);
504 camera->Dolly(factor);
507 if (interactor->GetLightFollowCamera()) {
508 renderer->UpdateLightsGeometryToFollowCamera();
510 renderer->ResetCameraClippingRange();
511 //interactor->Render();
513 //------------------------------------------------------------------------------
516 //------------------------------------------------------------------------------
517 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
519 double flyFrom[3], flyTo[3];
520 double d[3], focalPt[3], position[3], positionFrom[3];
522 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
523 vtkRenderer* renderer=NULL;
524 if (VisibleInWindow>-1)
525 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
529 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
530 interactor->GetEventPosition()[1], 0.0,
533 vtkAssemblyPath *path=NULL;
534 vtkAbstractPropPicker *picker;
535 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
536 path = picker->GetPath();
538 if ( path != NULL ) {
539 flyTo[0] = picker->GetPickPosition()[0];
540 flyTo[1] = picker->GetPickPosition()[1];
541 flyTo[2] = picker->GetPickPosition()[2];
542 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
543 renderer->GetActiveCamera()->GetPosition(positionFrom);
545 switch (slicer->GetSliceOrientation()) {
546 case vtkImageViewer2::SLICE_ORIENTATION_XY:
547 flyTo[2] = flyFrom[2];
550 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
551 flyTo[1] = flyFrom[1];
554 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
555 flyTo[0] = flyFrom[0];
560 for (i=0; i<3; i++) {
561 d[i] = flyTo[i] - flyFrom[i];
563 double distance = vtkMath::Normalize(d);
564 double delta = distance/15;
566 for (i=1; i<=15; i++) {
567 for (j=0; j<3; j++) {
568 focalPt[j] = flyFrom[j] + d[j]*i*delta;
569 position[j] = positionFrom[j] + d[j]*i*delta;
571 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
572 renderer->GetActiveCamera()->SetPosition(position);
573 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
574 renderer->ResetCameraClippingRange();
575 interactor->Render();
579 //------------------------------------------------------------------------------