Skip to content

Commit

Permalink
ENH: Convert the interactor style to use the new vtkMRMLViewInteracto…
Browse files Browse the repository at this point in the history
…rStyle
  • Loading branch information
cpinter committed Jun 30, 2021
1 parent b3465eb commit 02a575c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 46 deletions.
112 changes: 96 additions & 16 deletions VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
#include "vtkMRMLVirtualRealityViewNode.h"

// MRML includes
#include "vtkMRMLScene.h"
#include "vtkMRMLModelNode.h"
#include "vtkMRMLAbstractThreeDViewDisplayableManager.h"
#include "vtkMRMLInteractionEventData.h"
#include "vtkMRMLLinearTransformNode.h"
#include "vtkMRMLModelDisplayableManager.h"
#include "vtkMRMLModelDisplayNode.h"
#include "vtkMRMLVolumeNode.h"
#include "vtkMRMLVolumeRenderingDisplayNode.h"
#include "vtkMRMLModelNode.h"
#include "vtkMRMLScene.h"
#include "vtkMRMLSegmentationNode.h"
#include "vtkMRMLSegmentationDisplayNode.h"
#include "vtkMRMLLinearTransformNode.h"
#include "vtkMRMLAbstractThreeDViewDisplayableManager.h"
#include "vtkMRMLModelDisplayableManager.h"
#include "vtkMRMLVolumeNode.h"
#include "vtkMRMLVolumeRenderingDisplayNode.h"

// VTK includes
#include <vtkCamera.h>
Expand All @@ -47,6 +48,7 @@
#include <vtkTimerLog.h>
#include <vtkTransform.h>
#include <vtkWeakPointer.h>
#include <vtkWorldPointPicker.h>

#include "vtkOpenVRRenderWindow.h"
#include "vtkOpenVRRenderWindowInteractor.h"
Expand Down Expand Up @@ -173,7 +175,6 @@ bool vtkVirtualRealityViewInteractorStyle::vtkInternal::CalculateCombinedControl

//----------------------------------------------------------------------------
vtkVirtualRealityViewInteractorStyle::vtkVirtualRealityViewInteractorStyle()
: DisplayableManagerGroup(nullptr)
{
this->Internal = new vtkInternal();

Expand All @@ -187,6 +188,10 @@ vtkVirtualRealityViewInteractorStyle::vtkVirtualRealityViewInteractorStyle()
}
}

this->AccuratePicker = vtkSmartPointer<vtkCellPicker>::New();
this->AccuratePicker->SetTolerance( .005 );
this->QuickPicker = vtkSmartPointer<vtkWorldPointPicker>::New();

// Create default inputs mapping
this->MapInputToAction(vtkEventDataDevice::RightController,
vtkEventDataDeviceInput::Grip, VTKIS_POSITION_PROP);
Expand All @@ -202,15 +207,14 @@ vtkVirtualRealityViewInteractorStyle::vtkVirtualRealityViewInteractorStyle()
//----------------------------------------------------------------------------
vtkVirtualRealityViewInteractorStyle::~vtkVirtualRealityViewInteractorStyle()
{
this->SetDisplayableManagerGroup(nullptr);
delete this->Internal;
}

//----------------------------------------------------------------------------
void vtkVirtualRealityViewInteractorStyle::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
}

//----------------------------------------------------------------------------
void vtkVirtualRealityViewInteractorStyle::SetInteractor(vtkRenderWindowInteractor *i)
Expand Down Expand Up @@ -279,9 +283,63 @@ void vtkVirtualRealityViewInteractorStyle::ProcessEvents(
// Generic events binding
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
bool vtkVirtualRealityViewInteractorStyle::DelegateInteractionEventToDisplayableManagers(vtkEventData* inputEventData)
{
// Get display and world position
int* displayPositionInt = this->GetInteractor()->GetEventPosition();
vtkRenderer* pokedRenderer = this->GetInteractor()->FindPokedRenderer(displayPositionInt[0], displayPositionInt[1]);
if (!pokedRenderer || !inputEventData)
{
// can happen during application shutdown
return false;
}

vtkNew<vtkMRMLInteractionEventData> ed;
ed->SetType(inputEventData->GetType());
int displayPositionCorrected[2] = { displayPositionInt[0] - pokedRenderer->GetOrigin()[0], displayPositionInt[1] - pokedRenderer->GetOrigin()[1] };
ed->SetDisplayPosition(displayPositionCorrected);
double worldPosition[3] = { 0.0, 0.0, 0.0 };
vtkEventDataDevice3D* inputEventDataDevice3D = inputEventData->GetAsEventDataDevice3D();
if (!inputEventDataDevice3D)
{
vtkErrorMacro("DelegateInteractionEventToDisplayableManagers: Invalid event data type");
return false;
}
inputEventDataDevice3D->GetWorldPosition(worldPosition);
ed->SetDevice(inputEventDataDevice3D->GetDevice());
ed->SetWorldPosition(worldPosition, true);
ed->SetWorldToPhysicalScale(this->GetMagnification());
ed->SetAccuratePicker(this->AccuratePicker);
ed->SetRenderer(this->CurrentRenderer);
std::string interactionContextName;
if (ed->GetDevice() == vtkEventDataDevice::LeftController)
{
interactionContextName = "LeftController"; //TODO: Store these elsewhere
}
else if (ed->GetDevice() == vtkEventDataDevice::RightController)
{
interactionContextName = "RightController"; //TODO: Store these elsewhere
}
else
{
vtkErrorMacro("DelegateInteractionEventToDisplayableManagers: Unrecognized device");
}
ed->SetInteractionContextName(interactionContextName);

ed->SetAttributesFromInteractor(this->GetInteractor());

return this->DelegateInteractionEventDataToDisplayableManagers(ed);
}

//----------------------------------------------------------------------------
void vtkVirtualRealityViewInteractorStyle::OnMove3D(vtkEventData* edata)
{
if (this->DelegateInteractionEventToDisplayableManagers(edata))
{
return;
}

vtkEventDataDevice3D *edd = edata->GetAsEventDataDevice3D();
if (!edd)
{
Expand Down Expand Up @@ -322,6 +380,11 @@ void vtkVirtualRealityViewInteractorStyle::OnMove3D(vtkEventData* edata)
//----------------------------------------------------------------------------
void vtkVirtualRealityViewInteractorStyle::OnButton3D(vtkEventData* edata)
{
if (this->DelegateInteractionEventToDisplayableManagers(edata))
{
return;
}

vtkEventDataDevice3D *bd = edata->GetAsEventDataDevice3D();
if (!bd)
{
Expand Down Expand Up @@ -365,7 +428,7 @@ void vtkVirtualRealityViewInteractorStyle::PositionProp(vtkEventData* ed)
vtkMRMLDisplayableNode* pickedNode = this->Internal->PickedNode[deviceIndex];

if ( pickedNode == nullptr || !pickedNode->GetSelectable()
|| !this->CurrentRenderer || !this->DisplayableManagerGroup )
|| !this->CurrentRenderer || !this->DisplayableManagers )
{
return;
}
Expand Down Expand Up @@ -486,10 +549,10 @@ void vtkVirtualRealityViewInteractorStyle::StartPositionProp(vtkEventDataDevice3
edata->GetWorldPosition(pos);

// Get MRML node to move
for (int i=0; i<this->DisplayableManagerGroup->GetDisplayableManagerCount(); ++i)
for (int i=0; i<this->DisplayableManagers->GetDisplayableManagerCount(); ++i)
{
vtkMRMLAbstractThreeDViewDisplayableManager* currentDisplayableManager =
vtkMRMLAbstractThreeDViewDisplayableManager::SafeDownCast(this->DisplayableManagerGroup->GetNthDisplayableManager(i));
vtkMRMLAbstractThreeDViewDisplayableManager::SafeDownCast(this->DisplayableManagers->GetNthDisplayableManager(i));
if (!currentDisplayableManager)
{
continue;
Expand Down Expand Up @@ -791,13 +854,13 @@ void vtkVirtualRealityViewInteractorStyle::EndAction(int state, vtkEventDataDevi
//---------------------------------------------------------------------------
vtkMRMLScene* vtkVirtualRealityViewInteractorStyle::GetMRMLScene()
{
if (!this->DisplayableManagerGroup
|| this->DisplayableManagerGroup->GetDisplayableManagerCount() == 0)
if (!this->DisplayableManagers
|| this->DisplayableManagers->GetDisplayableManagerCount() == 0)
{
return nullptr;
}

return this->DisplayableManagerGroup->GetNthDisplayableManager(0)->GetMRMLScene();
return this->DisplayableManagers->GetNthDisplayableManager(0)->GetMRMLScene();
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -883,3 +946,20 @@ double vtkVirtualRealityViewInteractorStyle::GetMagnification()

return 1000.0 / rw->GetPhysicalScale();
}

//---------------------------------------------------------------------------
bool vtkVirtualRealityViewInteractorStyle::QuickPick(int x, int y, double pickPoint[3])
{
this->FindPokedRenderer(x, y);
if (this->CurrentRenderer == nullptr)
{
vtkDebugMacro("Pick: couldn't find the poked renderer at event position " << x << ", " << y);
return false;
}

this->QuickPicker->Pick(x, y, 0, this->CurrentRenderer);

this->QuickPicker->GetPickPosition(pickPoint);

return true;
}
25 changes: 15 additions & 10 deletions VirtualReality/MRML/vtkVirtualRealityViewInteractorStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,36 @@

// MRML includes
#include "vtkMRMLDisplayableManagerGroup.h"
#include "vtkMRMLViewInteractorStyle.h"

// VTK includes
#include "vtkObject.h"
#include "vtkInteractorStyle3D.h"
#include "vtkOpenVRRenderWindow.h" // for enums
#include "vtkEventData.h"

#include "vtkSlicerVirtualRealityModuleMRMLExport.h"

class vtkMRMLScene;
class vtkCellPicker;
class vtkWorldPointPicker;

/// \brief Virtual reality interactions
///
/// TODO:
///
class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteractorStyle :
public vtkInteractorStyle3D
public vtkMRMLViewInteractorStyle
{
public:
static vtkVirtualRealityViewInteractorStyle *New();
vtkTypeMacro(vtkVirtualRealityViewInteractorStyle,vtkInteractorStyle3D);
vtkTypeMacro(vtkVirtualRealityViewInteractorStyle,vtkMRMLViewInteractorStyle);
void PrintSelf(ostream& os, vtkIndent indent) override;

/// Set the Interactor wrapper being controlled by this object. (Satisfy superclass API.)
/// Give a chance to displayable managers to process the event.
/// Return true if the event is processed.
bool DelegateInteractionEventToDisplayableManagers(vtkEventData* inputEventData) override;

/// Set the Interactor wrapper being controlled by this object. (Satisfy superclass API.)
void SetInteractor(vtkRenderWindowInteractor *interactor) override;

/// Main process event method
Expand Down Expand Up @@ -140,11 +146,6 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract
//vtkOpenVRMenuWidget *GetMenu() {
// return this->Menu.Get(); }

/// Get the displayable managers
vtkGetObjectMacro(DisplayableManagerGroup, vtkMRMLDisplayableManagerGroup);
/// Set the displayable managers
vtkSetObjectMacro(DisplayableManagerGroup, vtkMRMLDisplayableManagerGroup);

/// Set physical to world magnification. Valid value range is [0.01, 100].
/// Note: Conversion is physicalScale = 1000 / magnification
void SetMagnification(double magnification);
Expand Down Expand Up @@ -181,6 +182,8 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract
//*/
//void AddTooltipForInput(vtkEventDataDevice device, vtkEventDataDeviceInput input);

bool QuickPick(int x, int y, double pickPoint[3]);

protected:
///**
//* Indicates if picking should be updated every frame. If so, the interaction
Expand All @@ -191,7 +194,9 @@ class VTK_SLICER_VIRTUALREALITY_MODULE_MRML_EXPORT vtkVirtualRealityViewInteract

//vtkNew<vtkOpenVRHardwarePicker> HardwarePicker;

vtkMRMLDisplayableManagerGroup* DisplayableManagerGroup;
/// For jump to slice feature (when mouse is moved while shift key is pressed)
vtkSmartPointer<vtkCellPicker> AccuratePicker;
vtkSmartPointer<vtkWorldPointPicker> QuickPicker;

protected:
vtkVirtualRealityViewInteractorStyle();
Expand Down
2 changes: 1 addition & 1 deletion VirtualReality/Widgets/qMRMLVirtualRealityHomeWidget.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ QString qMRMLVirtualRealityHomeWidget::virtualRealityViewNodeID()const
}

//-----------------------------------------------------------------------------
void qMRMLVirtualRealityHomeWidget::setVirtualRealityViewNode(vtkMRMLVirtualRealityViewNode * node)
void qMRMLVirtualRealityHomeWidget::setVirtualRealityViewNode(vtkMRMLVirtualRealityViewNode* node)
{
Q_D(qMRMLVirtualRealityHomeWidget);

Expand Down
Loading

0 comments on commit 02a575c

Please sign in to comment.