Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
DepthSensor.h
1 #ifndef NUITRACK_DEPTHSENSOR_H_
2 #define NUITRACK_DEPTHSENSOR_H_
3 
4 #include <vector>
5 
6 #include "nuitrack/modules/HeaderOnlyAPI_Module.h"
7 #include "nuitrack/types/Export.h"
8 #include "nuitrack/utils/ExceptionTranslator.h"
9 #include "nuitrack/types/DepthFrame.h"
10 #include "nuitrack/types/Vector3.h"
11 #include "nuitrack/types/OutputMode.h"
12 #include "nuitrack/capi/Nuitrack_CAPI.h"
13 #include "nuitrack/capi/DepthSensor_CAPI.h"
14 #include "nuitrack/utils/CallbackStruct.h"
15 
16 #include <iostream>
17 
18 namespace tdv
19 {
20 namespace nuitrack
21 {
36 class DepthSensor : public tdv::nuitrack::HeaderOnlyAPI_Module
37 {
38 public:
39 
44  typedef std::shared_ptr<DepthSensor> Ptr;
45 
52  typedef std::function<void (DepthFrame::Ptr)> OnNewFrame;
53 
62  {
63  NuitrackModule* pimpl = NULL;
64  ExceptionType exception_code = nuitrack_CreateDepthSensor(&pimpl);
65  char exception_message[1024];
66  nuitrack_GetExceptionMessage(pimpl, exception_message, 1024);
67  ExceptionTranslator::generateExceptionByErrorCode(exception_code, exception_message);
68  return std::shared_ptr<DepthSensor>(new DepthSensor(pimpl));
69  }
70 
72  DepthSensor(NuitrackModule* pimpl)
73  {
74  _pimpl = pimpl;
75 
76  _callbackStruct = new CallbackStruct<DepthFrame::Ptr>();
77  _callbackWrapper.setDepthSensor(this);
78  _callbackWrapper.setFunctionAddress(&onNewFrameCallback);
79  nuitrack_registerDepthSensorCallback(_pimpl, &_callbackWrapper);
80  }
81 
82  virtual ~DepthSensor()
83  {
84  nuitrack_DestroyDepthSensor(_pimpl);
85  if(_callbackStruct)
86  delete _callbackStruct;
87  }
88 
92  const OutputMode getOutputMode() const
93  {
94  OutputMode mode;
95  nuitrack_GetDepthSensorOutputMode(_pimpl, &mode);
96  return mode;
97  }
98 
103  bool isMirror() const
104  {
105  return nuitrack_IsDepthSensorMirror(_pimpl);
106  }
107 
120  void setMirror(bool mirror)
121  {
122  nuitrack_SetDepthSensorMirror(_pimpl, mirror);
123  }
124 
132  uint64_t connectOnNewFrame(const OnNewFrame& callback)
133  {
134  return _callbackStruct->addCallback(callback);
135  }
136 
143  void disconnectOnNewFrame(uint64_t handler)
144  {
145  _callbackStruct->deleteCallback(handler);
146  }
147 
152  DepthFrame::Ptr getDepthFrame()
153  {
154  DepthSensorDataPtr data = nuitrack_GetDepthSensorData(_pimpl);
155  DepthFrame::Ptr newFrame(new DepthFrame(data));
156  return newFrame;
157  }
158 
165  {
166  CVector3 c_vec { p.x, p.y, p.z };
167  return Vector3(nuitrack_ctypes_ConvertProjToRealCoordsVector3(_pimpl, c_vec));
168  }
169 
176  virtual Vector3 convertProjToRealCoords(size_t x, size_t y, DepthFrame::DataType depth) const
177  {
178  return Vector3(nuitrack_ctypes_ConvertProjToRealCoordsXYZ(_pimpl, x, y, depth));
179  }
180 
186  virtual Vector3 convertRealToProjCoords(const Vector3& p) const
187  {
188  CVector3 c_vec { p.x, p.y, p.z };
189  return Vector3(nuitrack_ctypes_ConvertRealToProjCoordsVector3(_pimpl, c_vec));
190  }
191 
197  virtual Vector3 convertRealToProjCoords(float x, float y, float z) const
198  {
199  return Vector3(nuitrack_ctypes_ConvertRealToProjCoordsXYZ(_pimpl, x, y, z));
200  }
201 
202  bool canUpdate() const
203  {
204  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
205  }
206 
213  virtual uint64_t getTimestamp()
214  {
215  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
216  }
217 
218 private:
219 
220  static void onNewFrameCallback(DepthSensorData* depthFrameImpl, DepthSensor* sensor)
221  {
222  DepthFrame::Ptr newFrame(new DepthFrame(depthFrameImpl));
223  sensor->_callbackStruct->executeAllCallbacks(newFrame);
224  }
225 
226  NuitrackModule* _pimpl;
227 
228  DepthSensorCallbackWrapper _callbackWrapper;
229 
230  CallbackStruct<DepthFrame::Ptr>* _callbackStruct;
231 };
232 
233 } /* namespace nuitrack */
234 } /* namespace tdv */
235 
236 #endif /* NUITRACK_DEPTHSENSOR_H_ */
ExceptionType
Possible error codes for Nuitrack internal functions.
Definition: Error.h:20
void disconnectOnNewFrame(uint64_t handler)
Remove a callback of the new depth frame request.
Definition: DepthSensor.h:143
void setMirror(bool mirror)
Set mirror mode state.
Definition: DepthSensor.h:120
static DepthSensor::Ptr create()
Create the DepthSensor module object.
Definition: DepthSensor.h:61
std::function< void(DepthFrame::Ptr)> OnNewFrame
The callback type of the new depth frame request.
Definition: DepthSensor.h:52
virtual uint64_t getTimestamp()
Returns the last processed data timestamp in microseconds.
Definition: DepthSensor.h:213
Stores a frame depth map.
Definition: DepthFrame.h:15
Definition: Vector3.h:11
Vector3 convertProjToRealCoords(const Vector3 &p) const
Convert projective coordinates to real world coordinates from Vector3 point.
Definition: DepthSensor.h:164
Stores sensor data properties.
Definition: OutputMode.h:13
Represents the interface to the capabilities of the Nuitrack Depth Sensor module. ...
Definition: DepthSensor.h:36
std::shared_ptr< DepthSensor > Ptr
Smart pointer to access the DepthSensor instance.
Definition: DepthSensor.h:44
Definition: Vector3.h:20
bool isMirror() const
Returns true if mirror mode is enabled, false otherwise.
Definition: DepthSensor.h:103
DepthFrame::Ptr getDepthFrame()
Returns smart pointer to the last available DepthFrame.
Definition: DepthSensor.h:152
uint64_t connectOnNewFrame(const OnNewFrame &callback)
Add a callback for the new depth frame request.
Definition: DepthSensor.h:132
DepthSensor(NuitrackModule *pimpl)
Definition: DepthSensor.h:72
virtual Vector3 convertRealToProjCoords(const Vector3 &p) const
Convert real world coordinates to projective coordinates from Vector3 point.
Definition: DepthSensor.h:186
virtual Vector3 convertRealToProjCoords(float x, float y, float z) const
Convert real world coordinates to projective coordinates from x, y, z.
Definition: DepthSensor.h:197
virtual Vector3 convertProjToRealCoords(size_t x, size_t y, DepthFrame::DataType depth) const
Convert projective coordinates to real world coordinates from x, y and depth map. ...
Definition: DepthSensor.h:176
const OutputMode getOutputMode() const
Returns output mode of depth sensor.
Definition: DepthSensor.h:92