iMSTK
Interactive Medical Simulation Toolkit
imstkVtkOpenVRRenderWindowInteractorImstk.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #ifndef vtkOpenVRRenderWindowInteractorImstk_h
8 #define vtkOpenVRRenderWindowInteractorImstk_h
9 
10 #include "vtkEventData.h" // for ivar
11 #include "vtkNew.h" // ivars
12 #include "vtkRenderWindowInteractor3D.h"
13 #include "vtkRenderingOpenVRModule.h" // For export macro
14 #include <functional> // for ivar
15 #include <map> // for ivar
16 #include <openvr.h> // for ivar
17 #include <string> // for ivar
18 #include <tuple> // for ivar
19 
20 class vtkTransform;
21 class vtkMatrix4x4;
22 class vtkOpenVROverlay;
23 class vtkOpenVRRenderWindow;
24 
25 class vtkOpenVRRenderWindowInteractorImstk : public vtkRenderWindowInteractor3D
26 {
27 public:
32 
33  vtkTypeMacro(vtkOpenVRRenderWindowInteractorImstk, vtkRenderWindowInteractor3D);
34  void PrintSelf(ostream& os, vtkIndent indent);
35 
39  virtual void Initialize();
40 
42 
48  static void SetClassExitMethod(void (* f)(void*), void* arg);
49  static void SetClassExitMethodArgDelete(void (* f)(void*));
51 
56  virtual void ExitCallback();
57 
59 
63  virtual void SetPhysicalTranslation(vtkCamera*, double, double, double);
64  virtual double* GetPhysicalTranslation(vtkCamera*);
65  virtual void SetPhysicalScale(double);
66  virtual double GetPhysicalScale();
68 
74  void ProcessEvents() override;
75 
82  virtual void DoOneEvent(vtkOpenVRRenderWindow* renWin, vtkRenderer* ren, bool doRender);
83 
84  /*
85  * Return the pointer index as a device
86  */
87  vtkEventDataDevice GetPointerDevice();
88 
89  /*
90  * Convert a device pose to pose matrices
91  * \param poseMatrixPhysical Optional output pose matrix in physical frame
92  * \param poseMatrixWorld Optional output pose matrix in world frame
93  */
94  void ConvertOpenVRPoseToMatrices(const vr::TrackedDevicePose_t& tdPose,
95  vtkMatrix4x4* poseMatrixWorld, vtkMatrix4x4* poseMatrixPhysical = nullptr);
96 
97  /*
98  * Convert a device pose to a world coordinate position and orientation
99  * \param pos Output world position
100  * \param wxyz Output world orientation quaternion
101  * \param ppos Output physical position
102  * \param wdir Output world view direction (-Z)
103  */
104  void ConvertPoseToWorldCoordinates(const vr::TrackedDevicePose_t& tdPose, double pos[3],
105  double wxyz[4], double ppos[3], double wdir[3]);
106  void ConvertPoseMatrixToWorldCoordinates(
107  const float poseMatrix[3][4], double pos[3], double wxyz[4], double ppos[3], double wdir[3]);
108 
110 
113  // void GetTouchPadPosition(vtkEventDataDevice, vtkEventDataDeviceInput, float[3]) override;
115 
116  /*
117  * Return starting physical to world matrix
118  */
119  void GetStartingPhysicalToWorldMatrix(vtkMatrix4x4* startingPhysicalToWorldMatrix);
120 
122 
125  void AddAction(std::string path, vtkCommand::EventIds, bool isAnalog);
126  void AddAction(std::string path, bool isAnalog, std::function<void(vtkEventData*)>);
128  // add an event action
129 
131 
135  vtkGetMacro(ActionManifestFileName, std::string);
136  vtkSetMacro(ActionManifestFileName, std::string);
138 
140 
143  vtkGetMacro(ActionSetName, std::string);
144  vtkSetMacro(ActionSetName, std::string);
146 
147 protected:
150 
152 
157  static void (* ClassExitMethod)(void*);
158  static void (* ClassExitMethodArgDelete)(void*);
159  static void* ClassExitMethodArg;
161 
163 
167  virtual int InternalCreateTimer(int timerId, int timerType, unsigned long duration);
168  virtual int InternalDestroyTimer(int platformTimerId);
170 
176  virtual void StartEventLoop();
177 
182  int DeviceInputDownCount[vtkEventDataNumberOfDevices];
183  virtual void RecognizeComplexGesture(vtkEventDataDevice3D* edata);
184 
188  vtkNew<vtkMatrix4x4> StartingPhysicalToWorldMatrix;
189 
191  {
192  public:
193  vr::VRActionHandle_t ActionHandle;
194  vtkCommand::EventIds EventId;
195  std::function<void(vtkEventData*)> Function;
196  bool UseFunction = false;
197  bool IsAnalog = false;
198  };
199 
200  std::map<std::string, ActionData> ActionMap;
201 
202  std::string ActionManifestFileName;
203  std::string ActionSetName;
204 
205  vr::VRActionSetHandle_t ActionsetVTK = vr::k_ulInvalidActionSetHandle;
206 
207  enum TrackerEnum
208  {
209  LeftHand = 0,
210  RightHand,
211  Head,
212  NumberOfTrackers
213  };
214 
216  {
217  vr::VRInputValueHandle_t Source = vr::k_ulInvalidInputValueHandle;
218  // vr::VRActionHandle_t ActionPose = vr::k_ulInvalidActionHandle;
219  // vr::InputPoseActionData_t LastPoseData;
220  vr::TrackedDevicePose_t LastPose;
221  };
222  TrackerActions Trackers[NumberOfTrackers];
223 
224  void HandleGripEvents(vtkEventData* ed);
225 
226 private:
228  void operator=(const vtkOpenVRRenderWindowInteractorImstk&) = delete;
229 };
230 
231 #endif
virtual int InternalCreateTimer(int timerId, int timerType, unsigned long duration)
static vtkOpenVRRenderWindowInteractorImstk * New()
virtual void SetPhysicalTranslation(vtkCamera *, double, double, double)
static void SetClassExitMethod(void(*f)(void *), void *arg)
void GetStartingPhysicalToWorldMatrix(vtkMatrix4x4 *startingPhysicalToWorldMatrix)
void AddAction(std::string path, vtkCommand::EventIds, bool isAnalog)
vtkGetMacro(ActionManifestFileName, std::string)
virtual void DoOneEvent(vtkOpenVRRenderWindow *renWin, vtkRenderer *ren, bool doRender)