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"
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() && !this->SM->IsLinked()) {
90 this->SM->GetSlicer(i)->SetCursorVisibility(0);
91 this->SM->GetSlicer(i)->SetCornerAnnotationVisibility(0);
92 this->SM->GetSlicer(i)->Render();
96 if ( VisibleInWindow > -1 ) {
97 if (event == vtkCommand::KeyPressEvent) {
98 std::string KeyPress = isi->GetInteractor()->GetKeySym();
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);
114 if (KeyPress == "0") {
115 this->SM->SetPreset(0);
116 this->SM->UpdateWindowLevel();
119 if (KeyPress == "1") {
120 this->SM->SetPreset(1);
121 this->SM->UpdateWindowLevel();
124 if (KeyPress == "2") {
125 this->SM->SetPreset(2);
126 this->SM->UpdateWindowLevel();
130 if (KeyPress == "3") {
131 this->SM->SetPreset(3);
132 this->SM->UpdateWindowLevel();
135 if (KeyPress == "4") {
136 this->SM->SetPreset(4);
137 this->SM->UpdateWindowLevel();
140 if (KeyPress == "5") {
141 this->SM->SetPreset(5);
142 this->SM->UpdateWindowLevel();
145 if (KeyPress == "6") {
146 this->SM->SetColorMap(0);
147 this->SM->UpdateWindowLevel();
150 if (KeyPress == "7") {
151 this->SM->SetColorMap(1);
152 this->SM->UpdateWindowLevel();
155 if (KeyPress == "8") {
156 this->SM->SetColorMap(2);
157 this->SM->UpdateWindowLevel();
160 if (KeyPress == "9") {
161 this->SM->SetColorMap(3);
162 this->SM->UpdateWindowLevel();
165 if (KeyPress == "equal") { //keycodes are in vtkWin32RenderWindowInteractor
166 this->SM->SetPreset(7);
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 std::cout << "KeyPress == \"h\"\n";
189 this->SM->SetCursorAndCornerAnnotationVisibility(0);
193 if (KeyPress == "x") {
194 for(int i=0; i<SM->GetNumberOfSlicers(); i++) {
195 SM->RemoveActor("overlay",0);
201 if (KeyPress == "u") {
206 if (KeyPress == "r" || KeyPress=="R") {
207 this->SM->GetSlicer(VisibleInWindow)->ResetCamera();
208 this->SM->GetSlicer(VisibleInWindow)->Render();
209 this->SM->UpdateLinkedNavigation( this->SM->GetSlicer(VisibleInWindow) );
212 if (KeyPress == "g") {
213 double* cursorPos = this->SM->GetSlicer(VisibleInWindow)->GetCursorPosition();
214 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(
215 cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
216 this->SM->UpdateViews(1,VisibleInWindow);
217 this->SM->UpdateLinked(VisibleInWindow);
220 if (KeyPress == "F5") {
221 this->SM->GetSlicer(VisibleInWindow)->FlipHorizontalView();
222 this->SM->GetSlicer(VisibleInWindow)->Render();
225 if (KeyPress == "F6") {
226 this->SM->GetSlicer(VisibleInWindow)->FlipVerticalView();
227 this->SM->GetSlicer(VisibleInWindow)->Render();
230 if (KeyPress == "Up") {
231 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
232 this->SM->UpdateSlice(VisibleInWindow);
234 if (KeyPress == "Down") {
235 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
236 this->SM->UpdateSlice(VisibleInWindow);
238 if (KeyPress == "space") {
241 if (KeyPress == "Left")
242 this->SM->SetPreviousTSlice(VisibleInWindow);
243 if (KeyPress == "Right")
244 this->SM->SetNextTSlice(VisibleInWindow);
246 if (KeyPress == "F2") {
247 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Sagital\n<slice>");
248 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(0);
249 this->SM->SetSliceOrientation(VisibleInWindow, 0);
250 this->SM->UpdateSliceRange(VisibleInWindow);
252 if (KeyPress == "F3") {
253 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Coronal\n<slice>");
254 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(1);
255 this->SM->SetSliceOrientation(VisibleInWindow, 1);
256 this->SM->UpdateSliceRange(VisibleInWindow);
258 if (KeyPress == "F4") {
259 this->SM->GetSlicer(VisibleInWindow)->GetAnnotation()->SetText(2,"Axial\n<slice>");
260 //this->SM->GetSlicer(VisibleInWindow)->SetSliceOrientation(2);
261 this->SM->SetSliceOrientation(VisibleInWindow, 2);
262 this->SM->UpdateSliceRange(VisibleInWindow);
267 //All type of mouse events
268 if (event == vtkCommand::LeaveEvent) {
269 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
270 -VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
271 this->SM->GetSlicer(VisibleInWindow)->Render();
276 // Mouse release HERE
277 if (event == vtkCommand::EndPickEvent) {
278 // DD(VisibleInWindow);
279 if (VisibleInWindow > -1)
280 this->SM->LeftButtonReleaseEvent(VisibleInWindow);
281 return; // no return !!!! ???
284 if (event == vtkCommand::StartWindowLevelEvent) {
286 this->InitialWindow = this->SM->GetColorWindow();
287 this->InitialLevel = this->SM->GetColorLevel();
289 if (VisibleInWindow > -1) {
290 mStartSlicer = VisibleInWindow;
295 if (event == vtkCommand::EndWindowLevelEvent) {
299 if (event == vtkCommand::EndInteractionEvent) {
301 this->SM->UpdateLinkedNavigation(this->SM->GetSlicer(VisibleInWindow),true);
305 if (VisibleInWindow > -1) {
306 this->SM->Activated();
307 this->SM->GetSlicer(VisibleInWindow)->SetCornerAnnotationVisibility(1);
309 if (event == vtkCommand::MouseWheelForwardEvent && !isi->GetInteractor()->GetControlKey()) {
311 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()+1);
312 this->SM->UpdateSlice(VisibleInWindow);
313 } else if (event == vtkCommand::MouseWheelForwardEvent && isi->GetInteractor()->GetControlKey()) {
315 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
316 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
317 } else if (event == vtkCommand::MouseWheelBackwardEvent && !isi->GetInteractor()->GetControlKey()) {
319 this->SM->GetSlicer(VisibleInWindow)->SetSlice(this->SM->GetSlicer(VisibleInWindow)->GetSlice()-1);
320 this->SM->UpdateSlice(VisibleInWindow);
321 } else if (event == vtkCommand::MouseWheelBackwardEvent && isi->GetInteractor()->GetControlKey()) {
323 this->Dolly(pow((double)1.1, factor),isi->GetInteractor());
324 Execute(caller, vtkCommand::EndInteractionEvent, NULL);
330 //Move into World Coordinate
331 renderer->DisplayToNormalizedDisplay(x,y);
332 renderer->NormalizedDisplayToViewport(x,y);
333 renderer->ViewportToNormalizedViewport(x,y);
334 renderer->NormalizedViewportToView(x,y,z);
335 renderer->ViewToWorld(x,y,z);
336 switch (this->SM->GetSlicer(VisibleInWindow)->GetSliceOrientation()) {
337 case vtkImageViewer2::SLICE_ORIENTATION_XY:
340 zWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
341 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[2] +
342 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[2];
345 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
347 yWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
348 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[1] +
349 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[1];
353 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
354 xWorld = this->SM->GetSlicer(VisibleInWindow)->GetSlice()*
355 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetSpacing()[0] +
356 this->SM->GetSlicer(VisibleInWindow)->GetInput()->GetOrigin()[0];
361 this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(xWorld,yWorld,zWorld,
362 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
364 this->SM->AddLandmark(xWorld,yWorld,zWorld,
365 this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
366 this->SM->GetSlicer(VisibleInWindow)->UpdateLandmarks();
369 if (event == vtkCommand::PickEvent || event == vtkCommand::StartPickEvent) {
371 this->SM->UpdateViews(1,VisibleInWindow);
372 this->SM->UpdateLinked(VisibleInWindow);
373 this->SM->UpdateInfoOnCursorPosition(VisibleInWindow);
375 this->SM->GetSlicer(VisibleInWindow)->Render();
377 //this->SM->GetSlicer(VisibleInWindow)->SetCurrentPosition(-VTK_DOUBLE_MAX,-VTK_DOUBLE_MAX,
378 //-VTK_DOUBLE_MAX,this->SM->GetSlicer(VisibleInWindow)->GetTSlice());
379 //this->SM->GetSlicer(VisibleInWindow)->Render();
382 if (event == vtkCommand::WindowLevelEvent && mStartSlicer > -1) {
383 this->SM->GetSlicer(mStartSlicer)->SetCornerAnnotationVisibility(1);
384 // Adjust the window level here
385 int *size = isi->GetInteractor()->GetRenderWindow()->GetSize();
386 double window = this->InitialWindow;
387 double level = this->InitialLevel;
389 this->SM->GetImage()->GetScalarRange(range);
391 // Compute normalized delta
392 double dx = static_cast<double>(isi->GetWindowLevelCurrentPosition()[0] -
393 isi->GetWindowLevelStartPosition()[0]) / size[0];
394 double dy = static_cast<double>(isi->GetWindowLevelStartPosition()[1] -
395 isi->GetWindowLevelCurrentPosition()[1]) / size[1];
396 //Window is exponential in nature, use exponential to avoid falling into negative numbers
397 dx = std::exp(1.0 * (dx*fabs(dx) + dx)) ; //Quadratic behavior for more reactive interface
398 dy = 0.15 * (dy*fabs(dy) + dy) * (range[1]-range[0]);//Quadratic behavior for more reactive interface
400 this->SM->SetColorWindow(window*dx);
401 this->SM->SetColorLevel(level-dy);
402 this->SM->SetPreset(6);
403 this->SM->UpdateWindowLevel();
409 //------------------------------------------------------------------------------
412 //------------------------------------------------------------------------------
413 void vvSlicerManagerCommand::Dolly(double factor, vtkRenderWindowInteractor *interactor)
415 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
416 vtkRenderer* renderer;
417 if (VisibleInWindow>-1)
418 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
423 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
424 double oldPos[3], newPos[3], distance[2];
425 vtkCamera *camera = renderer->GetActiveCamera();
426 camera->GetFocalPoint(viewFocus);
428 renderer->SetWorldPoint(viewFocus[0], viewFocus[0], viewFocus[0], 1.0);
429 renderer->WorldToDisplay();
430 renderer->GetDisplayPoint(viewFocus);
432 focalDepth = viewFocus[2];
434 oldPos[0] = renderer->GetCenter()[0];
435 oldPos[1] = renderer->GetCenter()[1];
436 oldPos[2] = focalDepth;
438 distance[0] = 1/factor*
439 (interactor->GetEventPosition()[0]-renderer->GetCenter()[0]);
440 distance[1] = 1/factor*
441 (interactor->GetEventPosition()[1]-renderer->GetCenter()[1]);
443 newPos[0] = interactor->GetEventPosition()[0] - distance[0];
444 newPos[1] = interactor->GetEventPosition()[1] - distance[1];
445 newPos[2] = focalDepth;
447 renderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
448 renderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
449 renderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
450 renderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
451 renderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
453 renderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
454 renderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
455 renderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
456 renderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
457 renderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
459 motionVector[0] = newPos[0] - oldPos[0];
460 motionVector[1] = newPos[1] - oldPos[1];
461 motionVector[2] = newPos[2] - oldPos[2];
463 camera->GetFocalPoint(viewFocus);
464 camera->GetPosition(viewPoint);
465 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
466 motionVector[1] + viewFocus[1],
467 motionVector[2] + viewFocus[2]);
469 camera->SetPosition(motionVector[0] + viewPoint[0],
470 motionVector[1] + viewPoint[1],
471 motionVector[2] + viewPoint[2]);
473 if (camera->GetParallelProjection()) {
474 camera->SetParallelScale(camera->GetParallelScale() / factor);
476 camera->Dolly(factor);
479 if (interactor->GetLightFollowCamera()) {
480 renderer->UpdateLightsGeometryToFollowCamera();
482 renderer->ResetCameraClippingRange();
483 //interactor->Render();
485 //------------------------------------------------------------------------------
488 //------------------------------------------------------------------------------
489 void vvSlicerManagerCommand::FlyToPosition(vtkRenderWindowInteractor *interactor,vvSlicer* slicer)
491 double flyFrom[3], flyTo[3];
492 double d[3], focalPt[3], position[3], positionFrom[3];
494 int VisibleInWindow = this->FindSlicerNumber(interactor->GetRenderWindow());
495 vtkRenderer* renderer=NULL;
496 if (VisibleInWindow>-1)
497 renderer=this->SM->GetSlicer(VisibleInWindow)->GetRenderer();
501 interactor->GetPicker()->Pick(interactor->GetEventPosition()[0],
502 interactor->GetEventPosition()[1], 0.0,
505 vtkAssemblyPath *path=NULL;
506 vtkAbstractPropPicker *picker;
507 if ( (picker=vtkAbstractPropPicker::SafeDownCast(interactor->GetPicker()))) {
508 path = picker->GetPath();
510 if ( path != NULL ) {
511 flyTo[0] = picker->GetPickPosition()[0];
512 flyTo[1] = picker->GetPickPosition()[1];
513 flyTo[2] = picker->GetPickPosition()[2];
514 renderer->GetActiveCamera()->GetFocalPoint(flyFrom);
515 renderer->GetActiveCamera()->GetPosition(positionFrom);
517 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];
532 for (i=0; i<3; i++) {
533 d[i] = flyTo[i] - flyFrom[i];
535 double distance = vtkMath::Normalize(d);
536 double delta = distance/15;
538 for (i=1; i<=15; i++) {
539 for (j=0; j<3; j++) {
540 focalPt[j] = flyFrom[j] + d[j]*i*delta;
541 position[j] = positionFrom[j] + d[j]*i*delta;
543 renderer->GetActiveCamera()->SetFocalPoint(focalPt);
544 renderer->GetActiveCamera()->SetPosition(position);
545 renderer->GetActiveCamera()->Dolly(0.3/15 + 1.0);
546 renderer->ResetCameraClippingRange();
547 interactor->Render();
551 //------------------------------------------------------------------------------