Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
ColorSensor.h
1 #ifndef NUITRACK_COLORSENSOR_H_
2 #define NUITRACK_COLORSENSOR_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/RGBFrame.h"
10 #include "nuitrack/types/Vector3.h"
11 #include "nuitrack/types/OutputMode.h"
12 #include "nuitrack/capi/Nuitrack_CAPI.h"
13 #include "nuitrack/capi/ColorSensor_CAPI.h"
14 #include "nuitrack/utils/CallbackStruct.h"
15 
16 #include <iostream>
17 
18 namespace tdv
19 {
20 namespace nuitrack
21 {
33 class ColorSensor : public tdv::nuitrack::HeaderOnlyAPI_Module
34 {
35 public:
36 
41  typedef std::shared_ptr<ColorSensor> Ptr;
42 
49  typedef std::function<void (RGBFrame::Ptr)> OnNewFrame;
50 
59  {
60  NuitrackModule* pimpl = NULL;
61  ExceptionType exception_code = nuitrack_CreateColorSensor(&pimpl);
62  ExceptionTranslator::generateExceptionByErrorCode(exception_code);
63  return std::shared_ptr<ColorSensor>(new ColorSensor(pimpl));
64  }
65 
67  ColorSensor(NuitrackModule* pimpl)
68  {
69  _pimpl = pimpl;
70 
71  _callbackStruct = new CallbackStruct<RGBFrame::Ptr>();
72  _callbackWrapper.setColorSensor(this);
73  _callbackWrapper.setFunctionAddress(&onNewFrameCallback);
74  nuitrack_registerColorSensorCallback(_pimpl, &_callbackWrapper);
75  }
76 
77  virtual ~ColorSensor()
78  {
79  nuitrack_DestroyColorSensor(_pimpl);
80  if(_callbackStruct)
81  delete _callbackStruct;
82  }
83 
87  const OutputMode getOutputMode() const
88  {
89  OutputMode mode;
90  nuitrack_GetColorSensorOutputMode(_pimpl, &mode);
91  return mode;
92  }
93 
101  uint64_t connectOnNewFrame(const OnNewFrame& callback)
102  {
103  return _callbackStruct->addCallback(callback);
104  }
105 
112  void disconnectOnNewFrame(uint64_t handler)
113  {
114  _callbackStruct->deleteCallback(handler);
115  }
116 
120  RGBFrame::Ptr getColorFrame()
121  {
122  ColorSensorDataPtr data = nuitrack_GetColorSensorData(_pimpl);
123  RGBFrame::Ptr newFrame(new RGBFrame(data));
124  return newFrame;
125  }
126 
127  bool canUpdate() const
128  {
129  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
130  }
131 
138  virtual uint64_t getTimestamp()
139  {
140  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
141  }
142 
143 private:
144 
145  static void onNewFrameCallback(ColorSensorData* colorFrameImpl, ColorSensor* sensor)
146  {
147  RGBFrame::Ptr newFrame(new RGBFrame(colorFrameImpl));
148  sensor->_callbackStruct->executeAllCallbacks(newFrame);
149  }
150 
151  NuitrackModule* _pimpl;
152 
153  ColorSensorCallbackWrapper _callbackWrapper;
154 
155  CallbackStruct<RGBFrame::Ptr>* _callbackStruct;
156 };
157 
158 } /* namespace nuitrack */
159 } /* namespace tdv */
160 
161 #endif /* NUITRACK_COLORSENSOR_H_ */
RGBFrame::Ptr getColorFrame()
Returns smart pointer to the last available RGBFrame.
Definition: ColorSensor.h:120
ColorSensor(NuitrackModule *pimpl)
Definition: ColorSensor.h:67
ExceptionType
Possible error codes for Nuitrack internal functions.
Definition: Error.h:20
uint64_t connectOnNewFrame(const OnNewFrame &callback)
Add a callback for the new color frame request.
Definition: ColorSensor.h:101
Stores sensor data properties.
Definition: OutputMode.h:13
const OutputMode getOutputMode() const
Returns output mode of color sensor.
Definition: ColorSensor.h:87
void disconnectOnNewFrame(uint64_t handler)
Remove a callback of the new color frame request.
Definition: ColorSensor.h:112
std::function< void(RGBFrame::Ptr)> OnNewFrame
The callback type of the new color frame request.
Definition: ColorSensor.h:49
std::shared_ptr< ColorSensor > Ptr
Smart pointer to access the ColorSensor instance.
Definition: ColorSensor.h:41
Stores a frame color map.
Definition: RGBFrame.h:16
Represents the interface to the capabilities of the Nuitrack Color Sensor module. ...
Definition: ColorSensor.h:33
virtual uint64_t getTimestamp()
Returns the last processed data timestamp in microseconds.
Definition: ColorSensor.h:138
static ColorSensor::Ptr create()
Create the ColorSensor module object.
Definition: ColorSensor.h:58