Nuitrack  1.6.0
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  return nuitrack_ConvertProjToRealCoordsVector3(_pimpl, p);
167  }
168 
175  virtual Vector3 convertProjToRealCoords(size_t x, size_t y, DepthFrame::DataType depth) const
176  {
177  return nuitrack_ConvertProjToRealCoordsXYZ(_pimpl, x, y, depth);
178  }
179 
185  virtual Vector3 convertRealToProjCoords(const Vector3& p) const
186  {
187  return nuitrack_ConvertRealToProjCoordsVector3(_pimpl, p);
188  }
189 
195  virtual Vector3 convertRealToProjCoords(float x, float y, float z) const
196  {
197  return nuitrack_ConvertRealToProjCoordsXYZ(_pimpl, x, y, z);
198  }
199 
200  bool canUpdate() const
201  {
202  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
203  }
204 
211  virtual uint64_t getTimestamp()
212  {
213  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
214  }
215 
216 private:
217 
218  static void onNewFrameCallback(DepthSensorData* depthFrameImpl, DepthSensor* sensor)
219  {
220  DepthFrame::Ptr newFrame(new DepthFrame(depthFrameImpl));
221  sensor->_callbackStruct->executeAllCallbacks(newFrame);
222  }
223 
224  NuitrackModule* _pimpl;
225 
226  DepthSensorCallbackWrapper _callbackWrapper;
227 
228  CallbackStruct<DepthFrame::Ptr>* _callbackStruct;
229 };
230 
231 } /* namespace nuitrack */
232 } /* namespace tdv */
233 
234 #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:211
Stores a frame depth map.
Definition: DepthFrame.h:15
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:9
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:185
virtual Vector3 convertRealToProjCoords(float x, float y, float z) const
Convert real world coordinates to projective coordinates from x, y, z.
Definition: DepthSensor.h:195
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:175
const OutputMode getOutputMode() const
Returns output mode of depth sensor.
Definition: DepthSensor.h:92