From 6a7f5ba52723ee2606130d75ce508cfc5d239c87 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 21 Dec 2023 04:06:25 -0500 Subject: [PATCH] STYLE: Simplify interactor style leveraging base class implementation Removes functions `OnSelect3D`, `OnMove3D`, `OnViewerMovement3D`, `StartAction`, `EndAction`, `Movement3D` and `SetInteractor` functions that are available in the base class. --- .../vtkVirtualRealityViewInteractorStyle.cxx | 320 ------------------ .../vtkVirtualRealityViewInteractorStyle.h | 23 -- 2 files changed, 343 deletions(-) diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx index d60ceb6..725e8a2 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -251,222 +250,12 @@ void vtkVirtualRealityViewInteractorStyle::PrintSelf(ostream& os, vtkIndent inde this->Superclass::PrintSelf(os,indent); } -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::SetInteractor(vtkRenderWindowInteractor *i) -{ - if (i == this->Interactor) - { - return; - } - - this->Superclass::SetInteractor(i); -} - //---------------------------------------------------------------------------- void vtkVirtualRealityViewInteractorStyle::SetDisplayableManagers(vtkMRMLDisplayableManagerGroup* displayableManagers) { this->DisplayableManagers = displayableManagers; } -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::OnMove3D(vtkEventData* edata) -{ - vtkEventDataDevice3D* edd = edata->GetAsEventDataDevice3D(); - if (!edd) - { - return; - } - - // Retrieve device type - int idev = static_cast(edd->GetDevice()); - - if (edd->GetDevice() == vtkEventDataDevice::HeadMountedDisplay) - { - edd->GetWorldDirection(this->HeadsetDir); - } - - // Update current state - int x = this->Interactor->GetEventPosition()[0]; - int y = this->Interactor->GetEventPosition()[1]; - - // Set current state and interaction prop - this->InteractionProp = this->InteractionProps[idev]; - - auto interactionState = this->InteractionState[idev]; - switch (interactionState) - { - case VTKIS_POSITION_PROP: - this->FindPokedRenderer(x, y); - this->PositionProp(edd); - this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); - break; - case VTKIS_DOLLY: - case VTKIS_GROUNDMOVEMENT: - case VTKIS_ELEVATION: - this->FindPokedRenderer(x, y); - this->Movement3D(interactionState, edd); - this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); - break; - case VTKIS_CLIP: - this->FindPokedRenderer(x, y); - this->Clip(edd); - this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); - break; - case VTKIS_USCALE: - this->FindPokedRenderer(x, y); - this->UniformScale(); - this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); - break; - default: - vtkDebugMacro(<< "OnMove3D: unknown interaction state " << idev << ": " - << this->InteractionState[idev]); - break; - } - - // Update rays - this->UpdateRay(edd->GetDevice()); -} - -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::OnSelect3D(vtkEventData* edata) -{ - vtkEventDataDevice3D* bd = edata->GetAsEventDataDevice3D(); - if (!bd) - { - return; - } - - int x = this->Interactor->GetEventPosition()[0]; - int y = this->Interactor->GetEventPosition()[1]; - this->FindPokedRenderer(x, y); - - decltype(this->InputMap)::key_type key(vtkCommand::Select3DEvent, bd->GetAction()); - auto it = this->InputMap.find(key); - if (it == this->InputMap.end()) - { - return; - } - - int state = it->second; - - // if grab mode then convert event data into where the ray is intersecting geometry - switch (bd->GetAction()) - { - case vtkEventDataAction::Press: - case vtkEventDataAction::Touch: - this->StartAction(state, bd); - break; - case vtkEventDataAction::Release: - case vtkEventDataAction::Untouch: - this->EndAction(state, bd); - break; - default: - break; - } -} - -//------------------------------------------------------------------------------ -void vtkVirtualRealityViewInteractorStyle::OnViewerMovement3D(vtkEventData* edata) -{ - if (this->Style == vtkVRInteractorStyle::FLY_STYLE) - { - this->Movement3D(VTKIS_DOLLY, edata); - } - else if (this->Style == vtkVRInteractorStyle::GROUNDED_STYLE) - { - this->Movement3D(VTKIS_GROUNDMOVEMENT, edata); - } -} - -//------------------------------------------------------------------------------ -void vtkVirtualRealityViewInteractorStyle::Movement3D(int interactionState, vtkEventData* edata) -{ - vtkEventDataDevice3D* edd = edata->GetAsEventDataDevice3D(); - if (!edd) - { - return; - } - - // Retrieve device type - int idev = static_cast(edd->GetDevice()); - - // Update current state - int x = this->Interactor->GetEventPosition()[0]; - int y = this->Interactor->GetEventPosition()[1]; - this->FindPokedRenderer(x, y); - - // Set current state and interaction prop - this->InteractionProp = this->InteractionProps[idev]; - - double const* pos = edd->GetTrackPadPosition(); - - if (edd->GetAction() == vtkEventDataAction::Press) - { - this->StartAction(interactionState, edd); - this->LastTrackPadPosition[0] = 0.0; - this->LastTrackPadPosition[1] = 0.0; - this->LastGroundMovementTrackPadPosition[0] = 0.0; - this->LastGroundMovementTrackPadPosition[1] = 0.0; - this->LastElevationTrackPadPosition[0] = 0.0; - this->LastElevationTrackPadPosition[1] = 0.0; - return; - } - - if (edd->GetAction() == vtkEventDataAction::Release) - { - this->EndAction(interactionState, edd); - return; - } - - // If the input event is from a joystick and is away from the center then - // call start. When the joystick returns to the center, call end. - if ((edd->GetInput() == vtkEventDataDeviceInput::Joystick || - edd->GetInput() == vtkEventDataDeviceInput::TrackPad) && - this->InteractionState[idev] != interactionState && fabs(pos[1]) > 0.1) - { - this->StartAction(interactionState, edd); - this->LastTrackPadPosition[0] = 0.0; - this->LastTrackPadPosition[1] = 0.0; - this->LastGroundMovementTrackPadPosition[0] = 0.0; - this->LastGroundMovementTrackPadPosition[1] = 0.0; - this->LastElevationTrackPadPosition[0] = 0.0; - this->LastElevationTrackPadPosition[1] = 0.0; - return; - } - - if (this->InteractionState[idev] == interactionState) - { - // Stop when returning to the center on the joystick - if ((edd->GetInput() == vtkEventDataDeviceInput::Joystick || - edd->GetInput() == vtkEventDataDeviceInput::TrackPad) && - fabs(pos[1]) < 0.1) - { - this->EndAction(interactionState, edd); - return; - } - - // Do the 3D movement corresponding to the interaction state - switch (interactionState) - { - case VTKIS_DOLLY: - this->Dolly3D(edd); - break; - case VTKIS_GROUNDMOVEMENT: - this->GroundMovement3D(edd); - break; - case VTKIS_ELEVATION: - this->Elevation3D(edd); - break; - default: - break; - } - - this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); - return; - } -} - - //---------------------------------------------------------------------------- // Interaction methods //---------------------------------------------------------------------------- @@ -823,115 +612,6 @@ int vtkVirtualRealityViewInteractorStyle::GetMappedAction(vtkCommand::EventIds e return VTKIS_NONE; } -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::StartAction(int state, vtkEventDataDevice3D* edata) -{ - switch (state) - { - case VTKIS_POSITION_PROP: - this->StartPositionProp(edata); - break; - case VTKIS_DOLLY: - this->StartMovement3D(state, edata); - this->LastDolly3DEventTime->StartTimer(); - break; - case VTKIS_GROUNDMOVEMENT: - this->StartMovement3D(state, edata); - this->LastGroundMovement3DEventTime->StartTimer(); - break; - case VTKIS_ELEVATION: - this->StartMovement3D(state, edata); - this->LastElevation3DEventTime->StartTimer(); - break; - case VTKIS_CLIP: - this->StartClip(edata); - break; - case VTKIS_PICK: - this->StartPick(edata); - break; - case VTKIS_LOAD_CAMERA_POSE: - this->StartLoadCamPose(edata); - break; - case VTKIS_MENU: - // Menu is only displayed upon action end (e.g. button release) - break; - default: - vtkDebugMacro(<< "StartAction: unknown state " << state); - break; - } -} - -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::EndAction(int state, vtkEventDataDevice3D* edata) -{ - switch (state) - { - case VTKIS_POSITION_PROP: - this->EndPositionProp(edata); - break; - case VTKIS_DOLLY: - this->EndMovement3D(edata); - this->LastDolly3DEventTime->StopTimer(); - break; - case VTKIS_GROUNDMOVEMENT: - this->EndMovement3D(edata); - this->LastGroundMovement3DEventTime->StopTimer(); - break; - case VTKIS_ELEVATION: - this->EndMovement3D(edata); - this->LastElevation3DEventTime->StopTimer(); - break; - case VTKIS_CLIP: - this->EndClip(edata); - break; - case VTKIS_PICK: - this->EndPick(edata); - break; - case VTKIS_MENU: - this->Menu->SetInteractor(this->Interactor); - this->Menu->Show(edata); - break; - case VTKIS_LOAD_CAMERA_POSE: - this->EndLoadCamPose(edata); - break; - case VTKIS_TOGGLE_DRAW_CONTROLS: - this->ToggleDrawControls(); - break; - case VTKIS_EXIT: - if (this->Interactor) - { - this->Interactor->ExitCallback(); - } - break; - case VTKIS_TELEPORTATION: - this->Teleportation3D(edata); - break; - case VTKIS_USCALE: - this->EndUniformScale(); - break; - default: - vtkDebugMacro(<< "EndAction: unknown state " << state); - break; - } - - // Reset complex gesture state because a button has been released - for (int d = 0; d < vtkEventDataNumberOfDevices; ++d) - { - switch (this->InteractionState[d]) - { - case VTKIS_PAN: - case VTKIS_ZOOM: - case VTKIS_ROTATE: - this->InteractionState[d] = VTKIS_NONE; - break; - default: - vtkDebugMacro(<< "EndAction: unknown interaction state " << d << ": " - << this->InteractionState[d]); - break; - } - } -} - //--------------------------------------------------------------------------- vtkMRMLScene* vtkVirtualRealityViewInteractorStyle::GetMRMLScene() { diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h index a96de99..b95dc09 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h @@ -50,21 +50,11 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract vtkTypeMacro(vtkVirtualRealityViewInteractorStyle,vtkOpenVRInteractorStyle); void PrintSelf(ostream& os, vtkIndent indent) override; - /// Set the Interactor wrapper being controlled by this object. (Satisfy superclass API.) - void SetInteractor(vtkRenderWindowInteractor *interactor) override; - void SetDisplayableManagers(vtkMRMLDisplayableManagerGroup* displayableManagers); /// Get MRML scene from the displayable manager group (the first displayable manager's if any) vtkMRMLScene* GetMRMLScene(); - //@{ - /// Override generic event bindings to call the corresponding action. - void OnSelect3D(vtkEventData *edata) override; - void OnMove3D(vtkEventData *edata) override; - void OnViewerMovement3D(vtkEventData* edata) override; - //@} - //@{ /** * Interaction mode entry points. @@ -104,10 +94,6 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract vtkGetMacro(GrabEnabled, int); vtkBooleanMacro(GrabEnabled, int); - //// allow the user to add options to the menu - //vtkOpenVRMenuWidget *GetMenu() { - // return this->Menu.Get(); } - vtkCellPicker* GetAccuratePicker(); /// Set physical to world magnification. Valid value range is [0.01, 100]. @@ -117,10 +103,6 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract protected: - // Utility routines - void StartAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); - void EndAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); - bool QuickPick(int x, int y, double pickPoint[3]); protected: @@ -135,11 +117,6 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract vtkVirtualRealityViewInteractorStyle(); ~vtkVirtualRealityViewInteractorStyle() override; - /** - * Update the 3D movement according to the given interaction state. - */ - void Movement3D(int interactionState, vtkEventData* edata); - vtkWeakPointer DisplayableManagers; private: