1 #include "vvInteractorStyleNavigator.h"
3 #include "vtkAbstractPropPicker.h"
4 #include "vtkAssemblyPath.h"
5 #include "vtkCallbackCommand.h"
7 #include "vtkObjectFactory.h"
9 #include "vtkRenderWindow.h"
10 #include "vtkRenderWindowInteractor.h"
11 #include "vtkRenderer.h"
12 #include <vtkRendererCollection.h>
13 #include "clitkCommon.h"
16 vtkCxxRevisionMacro(vvInteractorStyleNavigator, "DummyRevision");
17 vtkStandardNewMacro(vvInteractorStyleNavigator);
19 //----------------------------------------------------------------------------
20 vvInteractorStyleNavigator::vvInteractorStyleNavigator()
22 this->WindowLevelStartPosition[0] = 0;
23 this->WindowLevelStartPosition[1] = 0;
25 this->WindowLevelCurrentPosition[0] = 0;
26 this->WindowLevelCurrentPosition[1] = 0;
28 this->MotionFactor = 10.0;
31 //----------------------------------------------------------------------------
32 vvInteractorStyleNavigator::~vvInteractorStyleNavigator()
37 void vvInteractorStyleNavigator::FindPokedRenderer(int dummy1,int dummy2)
39 vtkRenderWindow * renwin=this->GetInteractor()->GetRenderWindow();
40 renwin->GetRenderers()->InitTraversal();
43 vtkRenderer* current = renwin->GetRenderers()->GetNextItem();
44 if (current==NULL || current->GetDraw())
46 CurrentRenderer=current;
52 //----------------------------------------------------------------------------
53 void vvInteractorStyleNavigator::StartWindowLevel()
55 if (this->State != VTKIS_NONE)
59 this->StartState(VTKIS_WINDOW_LEVEL);
60 this->InvokeEvent(vtkCommand::StartWindowLevelEvent,this);
63 //----------------------------------------------------------------------------
64 void vvInteractorStyleNavigator::EndWindowLevel()
66 if (this->State != VTKIS_WINDOW_LEVEL)
70 this->InvokeEvent(vtkCommand::EndWindowLevelEvent, this);
74 //----------------------------------------------------------------------------
75 void vvInteractorStyleNavigator::StartPick()
77 if (this->State != VTKIS_NONE)
81 this->StartState(VTKIS_PICK);
82 this->InvokeEvent(vtkCommand::StartPickEvent, this);
85 //----------------------------------------------------------------------------
86 void vvInteractorStyleNavigator::EndPick()
88 if (this->State != VTKIS_PICK)
92 this->InvokeEvent(vtkCommand::EndPickEvent, this);
96 //----------------------------------------------------------------------------
97 void vvInteractorStyleNavigator::OnMouseMove()
99 int x = this->Interactor->GetEventPosition()[0];
100 int y = this->Interactor->GetEventPosition()[1];
104 case VTKIS_WINDOW_LEVEL:
105 this->FindPokedRenderer(x, y);
107 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
111 this->FindPokedRenderer(x, y);
113 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
117 this->FindPokedRenderer(x, y);
119 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
123 this->FindPokedRenderer(x, y);
125 this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
129 this->InvokeEvent(vtkCommand::UserEvent, NULL);
133 // Call parent to handle all other states and perform additional work
137 void vvInteractorStyleNavigator::OnEnter()
139 // int x = this->Interactor->GetEventPosition()[0];
140 //int y = this->Interactor->GetEventPosition()[1];
144 case VTKIS_WINDOW_LEVEL:
154 this->InvokeEvent(vtkCommand::EnterEvent, NULL);
158 // Call parent to handle all other states and perform additional work
162 //----------------------------------------------------------------------------
163 void vvInteractorStyleNavigator::OnLeave()
165 // int x = this->Interactor->GetEventPosition()[0];
166 //int y = this->Interactor->GetEventPosition()[1];
170 case VTKIS_WINDOW_LEVEL:
180 this->InvokeEvent(vtkCommand::LeaveEvent, NULL);
184 // Call parent to handle all other states and perform additional work
188 //----------------------------------------------------------------------------
189 void vvInteractorStyleNavigator::OnRightButtonDown()
191 int x = this->Interactor->GetEventPosition()[0];
192 int y = this->Interactor->GetEventPosition()[1];
194 this->FindPokedRenderer(x, y);
195 if (this->CurrentRenderer == NULL)
200 // Redefine this button to handle window/level
201 this->GrabFocus(this->EventCallbackCommand);
202 if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
204 this->WindowLevelStartPosition[0] = x;
205 this->WindowLevelStartPosition[1] = y;
206 this->StartWindowLevel();
209 // The rest of the button + key combinations remain the same
213 this->Superclass::OnRightButtonDown();
217 //----------------------------------------------------------------------------
218 void vvInteractorStyleNavigator::OnRightButtonUp()
222 case VTKIS_WINDOW_LEVEL:
223 this->EndWindowLevel();
224 if ( this->Interactor )
226 this->ReleaseFocus();
231 // Call parent to handle all other states and perform additional work
233 this->Superclass::OnRightButtonUp();
236 //----------------------------------------------------------------------------
237 void vvInteractorStyleNavigator::OnLeftButtonDown()
239 int x = this->Interactor->GetEventPosition()[0];
240 int y = this->Interactor->GetEventPosition()[1];
242 this->FindPokedRenderer(x, y);
243 if (this->CurrentRenderer == NULL)
248 // Redefine this button to handle pick
249 this->GrabFocus(this->EventCallbackCommand);
250 if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
255 // The rest of the button + key combinations remain the same
259 this->Superclass::OnLeftButtonDown();
263 //----------------------------------------------------------------------------
264 void vvInteractorStyleNavigator::OnLeftButtonUp()
270 if ( this->Interactor )
272 this->ReleaseFocus();
277 // Call parent to handle all other states and perform additional work
279 this->Superclass::OnLeftButtonUp();
282 //----------------------------------------------------------------------------
283 void vvInteractorStyleNavigator::OnMiddleButtonDown()
285 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
286 this->Interactor->GetEventPosition()[1]);
287 if (this->CurrentRenderer == NULL)
291 this->CurrentRenderer->GetRenderWindow()->SetCurrentCursor(8);
292 this->GrabFocus(this->EventCallbackCommand);
296 //----------------------------------------------------------------------------
297 void vvInteractorStyleNavigator::OnMiddleButtonUp()
303 if ( this->Interactor )
305 this->Interactor->GetRenderWindow()->SetCurrentCursor(0);
306 this->ReleaseFocus();
312 //----------------------------------------------------------------------------
313 void vvInteractorStyleNavigator::OnChar()
315 vtkRenderWindowInteractor *rwi = this->Interactor;
317 switch (rwi->GetKeyCode())
322 this->AnimState = VTKIS_ANIM_ON;
323 this->AnimState = VTKIS_ANIM_OFF;
330 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
331 this->Interactor->GetEventPosition()[1]);
332 if (this->CurrentRenderer == NULL)
336 this->GrabFocus(this->EventCallbackCommand);
339 this->Dolly(pow((double)1.1, factor));
341 this->ReleaseFocus();
347 this->FindPokedRenderer(rwi->GetEventPosition()[0],
348 rwi->GetEventPosition()[1]);
349 if (this->CurrentRenderer == NULL)
353 this->GrabFocus(this->EventCallbackCommand);
356 this->Dolly(pow((double)1.1, factor));
358 this->ReleaseFocus();
362 // Disable StereoVision
366 //Do nothing, this is handled in vvSlicerManagerCommand
369 this->Superclass::OnChar();
374 //----------------------------------------------------------------------------
375 void vvInteractorStyleNavigator::OnMouseWheelForward()
377 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
378 this->Interactor->GetEventPosition()[1]);
379 if (this->CurrentRenderer == NULL)
383 this->GrabFocus(this->EventCallbackCommand);
384 if (this->Interactor->GetControlKey())
387 double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
388 this->Dolly(pow((double)1.1, factor));
391 this->ReleaseFocus();
392 this->InvokeEvent(vtkCommand::MouseWheelForwardEvent, this);
395 //----------------------------------------------------------------------------
396 void vvInteractorStyleNavigator::OnMouseWheelBackward()
398 this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
399 this->Interactor->GetEventPosition()[1]);
400 if (this->CurrentRenderer == NULL)
405 this->GrabFocus(this->EventCallbackCommand);
406 if (this->Interactor->GetControlKey())
409 double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
410 this->Dolly(pow((double)1.1, factor));
413 this->ReleaseFocus();
414 this->InvokeEvent(vtkCommand::MouseWheelBackwardEvent, this);
418 //----------------------------------------------------------------------------
419 void vvInteractorStyleNavigator::WindowLevel()
421 vtkRenderWindowInteractor *rwi = this->Interactor;
423 this->WindowLevelCurrentPosition[0] = rwi->GetEventPosition()[0];
424 this->WindowLevelCurrentPosition[1] = rwi->GetEventPosition()[1];
426 this->InvokeEvent(vtkCommand::WindowLevelEvent, this);
429 //----------------------------------------------------------------------------
430 void vvInteractorStyleNavigator::Pick()
432 this->InvokeEvent(vtkCommand::PickEvent, this);
435 //----------------------------------------------------------------------------
436 void vvInteractorStyleNavigator::Pan()
438 if (this->CurrentRenderer == NULL)
443 vtkRenderWindowInteractor *rwi = this->Interactor;
445 double viewFocus[4], focalDepth, viewPoint[3];
446 double newPickPoint[4], oldPickPoint[4], motionVector[3];
448 // Calculate the focal depth since we'll be using it a lot
450 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
451 camera->GetFocalPoint(viewFocus);
452 this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
454 focalDepth = viewFocus[2];
456 this->ComputeDisplayToWorld((double)rwi->GetEventPosition()[0],
457 (double)rwi->GetEventPosition()[1],
461 // Has to recalc old mouse point since the viewport has moved,
462 // so can't move it outside the loop
464 this->ComputeDisplayToWorld((double)rwi->GetLastEventPosition()[0],
465 (double)rwi->GetLastEventPosition()[1],
469 // Camera motion is reversed
471 motionVector[0] = oldPickPoint[0] - newPickPoint[0];
472 motionVector[1] = oldPickPoint[1] - newPickPoint[1];
473 motionVector[2] = oldPickPoint[2] - newPickPoint[2];
475 camera->GetFocalPoint(viewFocus);
476 camera->GetPosition(viewPoint);
477 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
478 motionVector[1] + viewFocus[1],
479 motionVector[2] + viewFocus[2]);
481 camera->SetPosition(motionVector[0] + viewPoint[0],
482 motionVector[1] + viewPoint[1],
483 motionVector[2] + viewPoint[2]);
485 if (rwi->GetLightFollowCamera())
487 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
493 //----------------------------------------------------------------------------
494 void vvInteractorStyleNavigator::Dolly()
496 if (this->CurrentRenderer == NULL)
501 vtkRenderWindowInteractor *rwi = this->Interactor;
502 double *center = this->CurrentRenderer->GetCenter();
503 int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
504 double dyf = this->MotionFactor * (double)(dy) / (double)(center[1]);
505 this->Dolly(pow((double)1.1, dyf));
508 //----------------------------------------------------------------------------
509 void vvInteractorStyleNavigator::Dolly(double factor)
511 if (this->CurrentRenderer == NULL)
516 double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
517 double oldPos[3], newPos[3], distance[2];
518 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
519 camera->GetFocalPoint(viewFocus);
520 this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
522 focalDepth = viewFocus[2];
524 oldPos[0] = this->CurrentRenderer->GetCenter()[0];
525 oldPos[1] = this->CurrentRenderer->GetCenter()[1];
526 oldPos[2] = focalDepth;
528 distance[0] = 1/factor*
529 (this->Interactor->GetEventPosition()[0]-this->CurrentRenderer->GetCenter()[0]);
530 distance[1] = 1/factor*
531 (this->Interactor->GetEventPosition()[1]-this->CurrentRenderer->GetCenter()[1]);
533 newPos[0] = this->Interactor->GetEventPosition()[0] - distance[0];
534 newPos[1] = this->Interactor->GetEventPosition()[1] - distance[1];
535 newPos[2] = focalDepth;
537 this->CurrentRenderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
538 this->CurrentRenderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
539 this->CurrentRenderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
540 this->CurrentRenderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
541 this->CurrentRenderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
543 this->CurrentRenderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
544 this->CurrentRenderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
545 this->CurrentRenderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
546 this->CurrentRenderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
547 this->CurrentRenderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
549 motionVector[0] = newPos[0] - oldPos[0];
550 motionVector[1] = newPos[1] - oldPos[1];
551 motionVector[2] = newPos[2] - oldPos[2];
553 camera->GetFocalPoint(viewFocus);
554 camera->GetPosition(viewPoint);
555 camera->SetFocalPoint(motionVector[0] + viewFocus[0],
556 motionVector[1] + viewFocus[1],
557 motionVector[2] + viewFocus[2]);
559 camera->SetPosition(motionVector[0] + viewPoint[0],
560 motionVector[1] + viewPoint[1],
561 motionVector[2] + viewPoint[2]);
563 if (camera->GetParallelProjection())
565 camera->SetParallelScale(camera->GetParallelScale() / factor);
569 camera->Dolly(factor);
570 if (this->AutoAdjustCameraClippingRange)
572 this->CurrentRenderer->ResetCameraClippingRange();
576 if (this->Interactor->GetLightFollowCamera())
578 this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
580 this->CurrentRenderer->ResetCameraClippingRange();
581 //this->Interactor->Render();
585 //----------------------------------------------------------------------------
586 void vvInteractorStyleNavigator::PrintSelf(ostream& os, vtkIndent indent)
588 this->Superclass::PrintSelf(os, indent);
590 os << indent << "Window Level Current Position: ("
591 << this->WindowLevelCurrentPosition[0] << ", "
592 << this->WindowLevelCurrentPosition[1] << ")" << endl;
594 os << indent << "Window Level Start Position: ("
595 << this->WindowLevelStartPosition[0] << ", "
596 << this->WindowLevelStartPosition[1] << ")" << endl;