From 262a43d1b627791484023f6ed8e2534752203532 Mon Sep 17 00:00:00 2001 From: ClementMirabel Date: Wed, 26 Jun 2019 12:53:11 -0400 Subject: [PATCH 1/3] Bring laser back on left trackpad button --- .../vtkVirtualRealityViewInteractorStyle.cxx | 209 +++++++++++++++++- .../vtkVirtualRealityViewInteractorStyle.h | 14 +- 2 files changed, 210 insertions(+), 13 deletions(-) diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx index 960ad2e..ec6a985 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx @@ -24,6 +24,7 @@ #include "vtkMRMLVirtualRealityViewNode.h" // MRML includes +#include "vtkMRMLApplicationLogic.h" #include "vtkMRMLScene.h" #include "vtkMRMLModelNode.h" #include "vtkMRMLModelDisplayNode.h" @@ -41,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -192,7 +194,8 @@ vtkVirtualRealityViewInteractorStyle::vtkVirtualRealityViewInteractorStyle() vtkEventDataDeviceInput::Grip, VTKIS_POSITION_PROP); this->MapInputToAction(vtkEventDataDevice::RightController, vtkEventDataDeviceInput::TrackPad, VTKIS_DOLLY); - + this->MapInputToAction(vtkEventDataDevice::LeftController, + vtkEventDataDeviceInput::TrackPad, VTKIS_PICK); this->MapInputToAction(vtkEventDataDevice::LeftController, vtkEventDataDeviceInput::Grip, VTKIS_POSITION_PROP); @@ -203,6 +206,7 @@ vtkVirtualRealityViewInteractorStyle::vtkVirtualRealityViewInteractorStyle() vtkVirtualRealityViewInteractorStyle::~vtkVirtualRealityViewInteractorStyle() { this->SetDisplayableManagerGroup(nullptr); + this->FocusedDisplayableManager = nullptr; delete this->Internal; } @@ -349,6 +353,32 @@ void vtkVirtualRealityViewInteractorStyle::OnButton3D(vtkEventData* edata) } } +//---------------------------------------------------------------------------- +// Interaction entry points +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::StartPick(vtkEventDataDevice3D *ed) +{ + // turn on ray and update length + this->ShowRay(ed->GetDevice()); + this->UpdateRay(ed->GetDevice()); + + vtkEventDataDevice dev = ed->GetDevice(); + this->Internal->InteractionState[static_cast(dev)] = VTKIS_PICK; +} +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::EndPick(vtkEventDataDevice3D *ed) +{ + // turn off ray + this->HideRay(ed->GetDevice()); + + // perform probe + this->ProbeData(ed->GetDevice()); + + vtkEventDataDevice dev = ed->GetDevice(); + this->Internal->InteractionState[static_cast(dev)] = VTKIS_NONE; +} + + //---------------------------------------------------------------------------- // Interaction methods //---------------------------------------------------------------------------- @@ -557,6 +587,59 @@ void vtkVirtualRealityViewInteractorStyle::EndDolly3D(vtkEventDataDevice3D* ed) this->LastDolly3DEventTime->StopTimer(); } +//---------------------------------------------------------------------------- +// Interaction methods +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::ProbeData(vtkEventDataDevice controller) +{ + vtkRenderer *ren = this->CurrentRenderer; + vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); + vtkOpenVRRenderWindowInteractor *iren = + static_cast(this->Interactor); + + if (!ren || !renWin || !iren) + { + return; + } + + vtkOpenVRModel *cmodel = + renWin->GetTrackedDeviceModel(controller); + if (!cmodel) + { + return; + } + + // Invoke start pick method if defined + this->InvokeEvent(vtkCommand::StartPickEvent, nullptr); + + cmodel->SetVisibility(false); + + // Compute controller position and world orientation + double p0[3]; //Ray start point + double wxyz[4];// Controller orientation + double dummy_ppos[3]; + double wdir[3]; + vr::TrackedDevicePose_t &tdPose = renWin->GetTrackedDevicePose(cmodel->TrackedDevice); + iren->ConvertPoseToWorldCoordinates(tdPose, p0, wxyz, dummy_ppos, wdir); + + // TO DO + //this->HardwarePicker->PickProp(p0, wxyz, ren, ren->GetViewProps()); + + cmodel->SetVisibility(true); + + // // Invoke end pick method if defined + // if (this->HandleObservers && + // this->HasObserver(vtkCommand::EndPickEvent)) + // { + // // TO DO + // // this->InvokeEvent(vtkCommand::EndPickEvent, this->HardwarePicker->GetSelection()); + // } + // else + // { + // this->EndPickCallback(this->HardwarePicker->GetSelection()); + // } +} + //---------------------------------------------------------------------------- // Multitouch interaction methods //---------------------------------------------------------------------------- @@ -730,9 +813,9 @@ void vtkVirtualRealityViewInteractorStyle::StartAction(int state, vtkEventDataDe //case VTKIS_CLIP: // this->StartClip(edata); // break; - //case VTKIS_PICK: - // this->StartPick(edata); - // break; + case VTKIS_PICK: + this->StartPick(edata); + break; //case VTKIS_LOAD_CAMERA_POSE: // this->StartLoadCamPose(edata); // break; @@ -753,9 +836,9 @@ void vtkVirtualRealityViewInteractorStyle::EndAction(int state, vtkEventDataDevi //case VTKIS_CLIP: // this->EndClip(edata); // break; - //case VTKIS_PICK: - // this->EndPick(edata); - // break; + case VTKIS_PICK: + this->EndPick(edata); + break; //case VTKIS_MENU: // this->Menu->SetInteractor(this->Interactor); // this->Menu->Show(edata); @@ -788,6 +871,118 @@ void vtkVirtualRealityViewInteractorStyle::EndAction(int state, vtkEventDataDevi //} } +//---------------------------------------------------------------------------- +// Handle Ray drawing and update +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::ShowRay(vtkEventDataDevice controller) +{ + vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); + if (!renWin || (controller != vtkEventDataDevice::LeftController && + controller != vtkEventDataDevice::RightController)) + { + return; + } + vtkOpenVRModel *cmodel = + renWin->GetTrackedDeviceModel(controller); + if (cmodel) + { + cmodel->SetShowRay(true); + } +} + +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::HideRay(vtkEventDataDevice controller) +{ + vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); + if (!renWin || (controller != vtkEventDataDevice::LeftController && + controller != vtkEventDataDevice::RightController)) + { + return; + } + vtkOpenVRModel *cmodel = + renWin->GetTrackedDeviceModel(controller); + if (cmodel) + { + cmodel->SetShowRay(false); + } +} + +//---------------------------------------------------------------------------- +void vtkVirtualRealityViewInteractorStyle::UpdateRay(vtkEventDataDevice controller) +{ + if (!this->Interactor) + { + return; + } + + vtkRenderer *ren = this->CurrentRenderer; + vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); + vtkOpenVRRenderWindowInteractor *iren = + static_cast(this->Interactor); + + if (!ren || !renWin || !iren) + { + return; + } + + vr::TrackedDeviceIndex_t idx = renWin->GetTrackedDeviceIndexForDevice(controller); + if (idx == vr::k_unTrackedDeviceIndexInvalid) + { + return; + } + vtkOpenVRModel* mod = renWin->GetTrackedDeviceModel(idx); + if (!mod) + { + return; + } + + //Set length to its max if interactive picking is off + // if (!this->HoverPick) + // { + // mod->SetRayLength(ren->GetActiveCamera()->GetClippingRange()[1]); + // return; + // } + + // Compute controller position and world orientation + double p0[3]; //Ray start point + double wxyz[4];// Controller orientation + double dummy_ppos[3]; + double wdir[3]; + vr::TrackedDevicePose_t &tdPose = renWin->GetTrackedDevicePose(mod->TrackedDevice); + iren->ConvertPoseToWorldCoordinates(tdPose, p0, wxyz, dummy_ppos, wdir); + + int idev = static_cast(controller); + + //Keep the same length if a controller is interacting with a prop + // if (this->InteractionProps[idev] != nullptr) + // { + // double* p = this->InteractionProps[idev]->GetPosition(); + // mod->SetRayLength(sqrt(vtkMath::Distance2BetweenPoints(p0, p))); + // return; + // } + + //Compute ray length. + // double p1[3]; + // vtkOpenVRPropPicker* picker = + // static_cast< vtkOpenVRPropPicker* >(this->InteractionPicker); + // picker->PickProp3DRay(p0, wxyz, ren, ren->GetViewProps()); + + //If something is picked, set the length accordingly + // if (this->InteractionPicker->GetProp3D()) + // { + // this->InteractionPicker->GetPickPosition(p1); + // mod->SetRayLength(sqrt(vtkMath::Distance2BetweenPoints(p0, p1))); + // } + // //Otherwise set the length to its max + // else + // { + mod->SetRayLength(ren->GetActiveCamera()->GetClippingRange()[1]); + // } + + return; +} + + //--------------------------------------------------------------------------- vtkMRMLScene* vtkVirtualRealityViewInteractorStyle::GetMRMLScene() { diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h index da62946..88e9f43 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h @@ -23,6 +23,7 @@ // MRML includes #include "vtkMRMLDisplayableManagerGroup.h" +#include "vtkMRMLInteractionEventData.h" // VTK includes #include "vtkObject.h" @@ -65,8 +66,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract /** * Interaction mode entry points. */ - //virtual void StartPick(vtkEventDataDevice3D *); - //virtual void EndPick(vtkEventDataDevice3D *); + virtual void StartPick(vtkEventDataDevice3D *); + virtual void EndPick(vtkEventDataDevice3D *); //virtual void StartLoadCamPose(vtkEventDataDevice3D *); //virtual void EndLoadCamPose(vtkEventDataDevice3D *); virtual void StartPositionProp(vtkEventDataDevice3D *); @@ -93,7 +94,7 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract /** * Methods for interaction. */ - //void ProbeData(vtkEventDataDevice controller); + void ProbeData(vtkEventDataDevice controller); //void LoadNextCameraPose(); virtual void PositionProp(vtkEventData *); //virtual void Clip(vtkEventDataDevice3D *); @@ -124,8 +125,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract //int GetInteractionState(vtkEventDataDevice device) { // return this->InteractionState[static_cast(device)]; } - //void ShowRay(vtkEventDataDevice controller); - //void HideRay(vtkEventDataDevice controller); + void ShowRay(vtkEventDataDevice controller); + void HideRay(vtkEventDataDevice controller); //void ShowBillboard(const std::string &text); //void HideBillboard(); @@ -154,7 +155,7 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract //void EndPickCallback(vtkSelection *sel); ////Ray drawing - //void UpdateRay(vtkEventDataDevice controller); + void UpdateRay(vtkEventDataDevice controller); //vtkNew Menu; //vtkNew MenuRepresentation; @@ -192,6 +193,7 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract //vtkNew HardwarePicker; vtkMRMLDisplayableManagerGroup* DisplayableManagerGroup; + vtkMRMLAbstractDisplayableManager* FocusedDisplayableManager; protected: vtkVirtualRealityViewInteractorStyle(); From 0e4af94ac04c3d850027a607e03a55bf1e2313a3 Mon Sep 17 00:00:00 2001 From: ClementMirabel Date: Thu, 27 Jun 2019 10:37:09 -0400 Subject: [PATCH 2/3] Create PolyData laser with same orientation --- .../vtkVirtualRealityViewInteractorStyle.cxx | 40 +++++++++++++++++++ .../vtkVirtualRealityViewInteractorStyle.h | 3 ++ 2 files changed, 43 insertions(+) diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx index ec6a985..6e33f9d 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx @@ -39,11 +39,13 @@ // VTK includes #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -359,6 +361,10 @@ void vtkVirtualRealityViewInteractorStyle::OnButton3D(vtkEventData* edata) void vtkVirtualRealityViewInteractorStyle::StartPick(vtkEventDataDevice3D *ed) { // turn on ray and update length + if (!this->LaserPoints) + { + this->CreateLaser(ed->GetDevice()); + } this->ShowRay(ed->GetDevice()); this->UpdateRay(ed->GetDevice()); @@ -1078,3 +1084,37 @@ double vtkVirtualRealityViewInteractorStyle::GetMagnification() return 1000.0 / rw->GetPhysicalScale(); } + +void vtkVirtualRealityViewInteractorStyle::CreateLaser(vtkEventDataDevice controller) +{ + vtkRenderer *ren = this->CurrentRenderer; + + vtkNew laserPoints; + laserPoints->SetNumberOfPoints(2); + laserPoints->SetPoint(0, 0.0, 0.0, 0.0); + laserPoints->SetPoint(1, 0.0, 0.0, -ren->GetActiveCamera()->GetClippingRange()[1]); + + vtkNew lines; + lines->InsertNextCell(2); + lines->InsertCellPoint(0); + lines->InsertCellPoint(1); + + vtkNew path; + path->SetPoints(laserPoints); + path->SetLines(lines); + + vtkNew pathModel; + pathModel->SetSelectable(false); + pathModel->SetScene(this->GetMRMLScene()); + pathModel->SetName("Laser"); + pathModel->SetAndObservePolyData(path); + + vtkNew laserModelDisplay; + laserModelDisplay->SetColor(1,1,0); + laserModelDisplay->SetScene(this->GetMRMLScene()); + this->GetMRMLScene()->AddNode(laserModelDisplay); + pathModel->SetAndObserveDisplayNodeID(laserModelDisplay->GetID()); + + this->GetMRMLScene()->AddNode(pathModel); + this->LaserPoints = laserPoints; +} \ No newline at end of file diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h index 88e9f43..f17d21d 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h @@ -177,6 +177,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract void StartAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); void EndAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); + void CreateLaser(vtkEventDataDevice controller); + ///** //* Controls helpers drawing //*/ @@ -194,6 +196,7 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract vtkMRMLDisplayableManagerGroup* DisplayableManagerGroup; vtkMRMLAbstractDisplayableManager* FocusedDisplayableManager; + vtkPoints* LaserPoints; protected: vtkVirtualRealityViewInteractorStyle(); From cb31336c41e4ac8134706475ec956722a197c8fa Mon Sep 17 00:00:00 2001 From: ClementMirabel Date: Thu, 27 Jun 2019 14:43:33 -0400 Subject: [PATCH 3/3] Replaced ray by polydata laser --- .../vtkVirtualRealityViewInteractorStyle.cxx | 203 +++++++----------- .../vtkVirtualRealityViewInteractorStyle.h | 15 +- 2 files changed, 88 insertions(+), 130 deletions(-) diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx index 6e33f9d..0198266 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx @@ -317,6 +317,7 @@ void vtkVirtualRealityViewInteractorStyle::OnMove3D(vtkEventData* edata) // this->InvokeEvent(vtkCommand::InteractionEvent, nullptr); // break; } + this->UpdateLaser(edd->GetDevice()); //// Update rays //if (this->HoverPick) @@ -360,13 +361,9 @@ void vtkVirtualRealityViewInteractorStyle::OnButton3D(vtkEventData* edata) //---------------------------------------------------------------------------- void vtkVirtualRealityViewInteractorStyle::StartPick(vtkEventDataDevice3D *ed) { - // turn on ray and update length - if (!this->LaserPoints) - { - this->CreateLaser(ed->GetDevice()); - } - this->ShowRay(ed->GetDevice()); - this->UpdateRay(ed->GetDevice()); + // turn on laser and update length + this->ShowLaser(ed->GetDevice()); + this->UpdateLaser(ed->GetDevice()); vtkEventDataDevice dev = ed->GetDevice(); this->Internal->InteractionState[static_cast(dev)] = VTKIS_PICK; @@ -375,7 +372,7 @@ void vtkVirtualRealityViewInteractorStyle::StartPick(vtkEventDataDevice3D *ed) void vtkVirtualRealityViewInteractorStyle::EndPick(vtkEventDataDevice3D *ed) { // turn off ray - this->HideRay(ed->GetDevice()); + this->HideLaser(ed->GetDevice()); // perform probe this->ProbeData(ed->GetDevice()); @@ -878,117 +875,23 @@ void vtkVirtualRealityViewInteractorStyle::EndAction(int state, vtkEventDataDevi } //---------------------------------------------------------------------------- -// Handle Ray drawing and update -//---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::ShowRay(vtkEventDataDevice controller) -{ - vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); - if (!renWin || (controller != vtkEventDataDevice::LeftController && - controller != vtkEventDataDevice::RightController)) - { - return; - } - vtkOpenVRModel *cmodel = - renWin->GetTrackedDeviceModel(controller); - if (cmodel) - { - cmodel->SetShowRay(true); - } -} - +// Handle Laser drawing and update //---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::HideRay(vtkEventDataDevice controller) +void vtkVirtualRealityViewInteractorStyle::ShowLaser(vtkEventDataDevice controller) { - vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); - if (!renWin || (controller != vtkEventDataDevice::LeftController && - controller != vtkEventDataDevice::RightController)) - { - return; - } - vtkOpenVRModel *cmodel = - renWin->GetTrackedDeviceModel(controller); - if (cmodel) - { - cmodel->SetShowRay(false); - } + if (!this->LaserPoints) + { + this->CreateLaser(); + } + this->LaserModelDisplay->VisibilityOn(); } //---------------------------------------------------------------------------- -void vtkVirtualRealityViewInteractorStyle::UpdateRay(vtkEventDataDevice controller) +void vtkVirtualRealityViewInteractorStyle::HideLaser(vtkEventDataDevice controller) { - if (!this->Interactor) - { - return; - } - - vtkRenderer *ren = this->CurrentRenderer; - vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); - vtkOpenVRRenderWindowInteractor *iren = - static_cast(this->Interactor); - - if (!ren || !renWin || !iren) - { - return; - } - - vr::TrackedDeviceIndex_t idx = renWin->GetTrackedDeviceIndexForDevice(controller); - if (idx == vr::k_unTrackedDeviceIndexInvalid) - { - return; - } - vtkOpenVRModel* mod = renWin->GetTrackedDeviceModel(idx); - if (!mod) - { - return; - } - - //Set length to its max if interactive picking is off - // if (!this->HoverPick) - // { - // mod->SetRayLength(ren->GetActiveCamera()->GetClippingRange()[1]); - // return; - // } - - // Compute controller position and world orientation - double p0[3]; //Ray start point - double wxyz[4];// Controller orientation - double dummy_ppos[3]; - double wdir[3]; - vr::TrackedDevicePose_t &tdPose = renWin->GetTrackedDevicePose(mod->TrackedDevice); - iren->ConvertPoseToWorldCoordinates(tdPose, p0, wxyz, dummy_ppos, wdir); - - int idev = static_cast(controller); - - //Keep the same length if a controller is interacting with a prop - // if (this->InteractionProps[idev] != nullptr) - // { - // double* p = this->InteractionProps[idev]->GetPosition(); - // mod->SetRayLength(sqrt(vtkMath::Distance2BetweenPoints(p0, p))); - // return; - // } - - //Compute ray length. - // double p1[3]; - // vtkOpenVRPropPicker* picker = - // static_cast< vtkOpenVRPropPicker* >(this->InteractionPicker); - // picker->PickProp3DRay(p0, wxyz, ren, ren->GetViewProps()); - - //If something is picked, set the length accordingly - // if (this->InteractionPicker->GetProp3D()) - // { - // this->InteractionPicker->GetPickPosition(p1); - // mod->SetRayLength(sqrt(vtkMath::Distance2BetweenPoints(p0, p1))); - // } - // //Otherwise set the length to its max - // else - // { - mod->SetRayLength(ren->GetActiveCamera()->GetClippingRange()[1]); - // } - - return; + this->LaserModelDisplay->VisibilityOff(); } - //--------------------------------------------------------------------------- vtkMRMLScene* vtkVirtualRealityViewInteractorStyle::GetMRMLScene() { @@ -1085,14 +988,18 @@ double vtkVirtualRealityViewInteractorStyle::GetMagnification() return 1000.0 / rw->GetPhysicalScale(); } -void vtkVirtualRealityViewInteractorStyle::CreateLaser(vtkEventDataDevice controller) +void vtkVirtualRealityViewInteractorStyle::CreateLaser() { vtkRenderer *ren = this->CurrentRenderer; + if (!ren) + { + return; + } - vtkNew laserPoints; - laserPoints->SetNumberOfPoints(2); - laserPoints->SetPoint(0, 0.0, 0.0, 0.0); - laserPoints->SetPoint(1, 0.0, 0.0, -ren->GetActiveCamera()->GetClippingRange()[1]); + this->LaserPoints = vtkSmartPointer::New(); + this->LaserPoints->SetNumberOfPoints(2); + this->LaserPoints->SetPoint(0, 0.0, 0.0, 0.0); + this->LaserPoints->SetPoint(1, 0.0, 0.0, -ren->GetActiveCamera()->GetClippingRange()[1]); vtkNew lines; lines->InsertNextCell(2); @@ -1100,21 +1007,71 @@ void vtkVirtualRealityViewInteractorStyle::CreateLaser(vtkEventDataDevice contro lines->InsertCellPoint(1); vtkNew path; - path->SetPoints(laserPoints); + path->SetPoints(this->LaserPoints); path->SetLines(lines); vtkNew pathModel; pathModel->SetSelectable(false); + pathModel->HideFromEditorsOn(); pathModel->SetScene(this->GetMRMLScene()); pathModel->SetName("Laser"); pathModel->SetAndObservePolyData(path); - vtkNew laserModelDisplay; - laserModelDisplay->SetColor(1,1,0); - laserModelDisplay->SetScene(this->GetMRMLScene()); - this->GetMRMLScene()->AddNode(laserModelDisplay); - pathModel->SetAndObserveDisplayNodeID(laserModelDisplay->GetID()); + this->LaserModelDisplay = vtkSmartPointer::New(); + this->LaserModelDisplay->SetColor(1,0,0); + this->LaserModelDisplay->SetScene(this->GetMRMLScene()); + this->GetMRMLScene()->AddNode(this->LaserModelDisplay); + pathModel->SetAndObserveDisplayNodeID(this->LaserModelDisplay->GetID()); this->GetMRMLScene()->AddNode(pathModel); - this->LaserPoints = laserPoints; + + this->LaserModelDisplay->AddViewNodeID("vtkMRMLVirtualRealityViewNodeActive"); +} + +void vtkVirtualRealityViewInteractorStyle::UpdateLaser(vtkEventDataDevice controller) +{ + if (!this->LaserPoints) + { + return; + } + + if (!this->LaserModelDisplay->GetVisibility()) + { + return; + } + + vtkRenderer *ren = this->CurrentRenderer; + vtkOpenVRRenderWindow* renWin = vtkOpenVRRenderWindow::SafeDownCast(this->Interactor->GetRenderWindow()); + vtkOpenVRRenderWindowInteractor *iren = + static_cast(this->Interactor); + if (!ren || !renWin || !iren) + { + return; + } + + vr::TrackedDeviceIndex_t idx = renWin->GetTrackedDeviceIndexForDevice(controller); + if (idx == vr::k_unTrackedDeviceIndexInvalid) + { + return; + } + vtkOpenVRModel* mod = renWin->GetTrackedDeviceModel(idx); + if (!mod) + { + return; + } + + // Compute controller position and world orientation + vr::TrackedDevicePose_t &tdPose = renWin->GetTrackedDevicePose(mod->TrackedDevice); + vtkNew leftControllerWorldPose; + iren->ConvertOpenVRPoseToMatrices(tdPose, leftControllerWorldPose); + + double p0[4] = { 0.0, 0.0, 0.0, 1.0 }; + double* LaserClose = leftControllerWorldPose->MultiplyDoublePoint(p0); + this->LaserPoints->SetPoint(0, LaserClose[0], LaserClose[1], LaserClose[2]); + + double p1[4] = { 0.0, 0.0, -ren->GetActiveCamera()->GetClippingRange()[1], 1.0 }; + double* LaserFar = leftControllerWorldPose->MultiplyDoublePoint(p1); + this->LaserPoints->SetPoint(1, LaserFar[0], LaserFar[1], LaserFar[2]); + + this->LaserPoints->Modified(); } \ No newline at end of file diff --git a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h index f17d21d..6a52ed6 100644 --- a/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h +++ b/VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h @@ -24,6 +24,7 @@ // MRML includes #include "vtkMRMLDisplayableManagerGroup.h" #include "vtkMRMLInteractionEventData.h" +#include "vtkMRMLModelDisplayNode.h" // VTK includes #include "vtkObject.h" @@ -125,8 +126,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract //int GetInteractionState(vtkEventDataDevice device) { // return this->InteractionState[static_cast(device)]; } - void ShowRay(vtkEventDataDevice controller); - void HideRay(vtkEventDataDevice controller); + void ShowLaser(vtkEventDataDevice controller); + void HideLaser(vtkEventDataDevice controller); //void ShowBillboard(const std::string &text); //void HideBillboard(); @@ -154,8 +155,9 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract protected: //void EndPickCallback(vtkSelection *sel); - ////Ray drawing - void UpdateRay(vtkEventDataDevice controller); + ////Laser drawing + void CreateLaser(); + void UpdateLaser(vtkEventDataDevice controller); //vtkNew Menu; //vtkNew MenuRepresentation; @@ -177,8 +179,6 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract void StartAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); void EndAction(int VTKIS_STATE, vtkEventDataDevice3D *edata); - void CreateLaser(vtkEventDataDevice controller); - ///** //* Controls helpers drawing //*/ @@ -196,7 +196,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract vtkMRMLDisplayableManagerGroup* DisplayableManagerGroup; vtkMRMLAbstractDisplayableManager* FocusedDisplayableManager; - vtkPoints* LaserPoints; + vtkSmartPointer LaserPoints; + vtkSmartPointer LaserModelDisplay; protected: vtkVirtualRealityViewInteractorStyle();