Overte C++ Documentation
KinectPlugin.h
1 //
2 // KinectPlugin.h
3 //
4 // Created by Brad Hefta-Gaub on 2016/12/7
5 // Copyright 2016 High Fidelity, Inc.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 
11 #ifndef hifi_KinectPlugin_h
12 #define hifi_KinectPlugin_h
13 
14 #ifdef HAVE_KINECT
15 #ifndef WIN32_LEAN_AND_MEAN
16 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
17 #endif
18 
19 // Windows Header Files
20 #include <windows.h>
21 
22 #include <Shlobj.h>
23 
24 // Kinect Header files
25 #include <Kinect.h>
26 #include <SimpleMovingAverage.h>
27 
28 // Safe release for interfaces
29 template<class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) {
30  if (pInterfaceToRelease != NULL) {
31  pInterfaceToRelease->Release();
32  pInterfaceToRelease = NULL;
33  }
34 }
35 #endif
36 
37 #include <controllers/InputDevice.h>
38 #include <controllers/StandardControls.h>
39 #include <plugins/InputPlugin.h>
40 
41 // Handles interaction with the Kinect SDK
42 class KinectPlugin : public InputPlugin {
43  Q_OBJECT
44 public:
45  bool isHandController() const override;
46  bool isHeadController() const override;
47 
48  // Plugin functions
49  virtual void init() override;
50  virtual bool isSupported() const override;
51  virtual const QString getName() const override { return NAME; }
52  const QString getID() const override { return KINECT_ID_STRING; }
53 
54  virtual bool activate() override;
55  virtual void deactivate() override;
56 
57  virtual void pluginFocusOutEvent() override { _inputDevice->focusOutEvent(); }
58  virtual void pluginUpdate(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override;
59 
60  virtual void saveSettings() const override;
61  virtual void loadSettings() override;
62 
63 private:
64  // add variables for moving average
65  ThreadSafeMovingAverage<glm::quat, 2> _LeftHandOrientationAverage;
66  ThreadSafeMovingAverage<glm::quat, 2> _RightHandOrientationAverage;
67 
68 protected:
69 
70  struct KinectJoint {
71  glm::vec3 position;
72  glm::quat orientation;
73  };
74 
75  class InputDevice : public controller::InputDevice {
76  public:
77  friend class KinectPlugin;
78 
79  InputDevice() : controller::InputDevice("Kinect") {}
80 
81  // Device functions
82  virtual controller::Input::NamedVector getAvailableInputs() const override;
83  virtual QString getDefaultMappingConfig() const override;
84  virtual void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData) override {};
85  virtual void focusOutEvent() override {};
86 
87  void update(float deltaTime, const controller::InputCalibrationData& inputCalibrationData,
88  const std::vector<KinectPlugin::KinectJoint>& joints, const std::vector<KinectPlugin::KinectJoint>& prevJoints);
89 
90  void clearState();
91  };
92 
93  std::shared_ptr<InputDevice> _inputDevice { std::make_shared<InputDevice>() };
94 
95  static const char* NAME;
96  static const char* KINECT_ID_STRING;
97 
98  bool _enabled { false };
99  bool _debug { false };
100  mutable bool _initialized { false };
101 
102  // copy of data directly from the KinectDataReader SDK
103  std::vector<KinectJoint> _joints;
104 
105  // one frame old copy of _joints, used to caluclate angular and linear velocity.
106  std::vector<KinectJoint> _prevJoints;
107 
108 
109  // Kinect SDK related items...
110 
111  bool KinectPlugin::initializeDefaultSensor() const;
112  void updateBody();
113 
114 #ifdef HAVE_KINECT
115  void ProcessBody(INT64 time, int bodyCount, IBody** bodies);
116 
117  // Current Kinect
118  mutable IKinectSensor* _kinectSensor { nullptr };
119  mutable ICoordinateMapper* _coordinateMapper { nullptr };
120 
121  // Body reader
122  mutable IBodyFrameReader* _bodyFrameReader { nullptr };
123 #endif
124 
125 };
126 
127 #endif // hifi_KinectPlugin_h
128