Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
GestureRecognizer.h
1 #ifndef NUITRACK_GESTURE_RECOGNIZER_H_
2 #define NUITRACK_GESTURE_RECOGNIZER_H_
3 
4 #include <functional>
5 
6 #include "nuitrack/modules/HeaderOnlyAPI_Module.h"
7 #include "nuitrack/types/Export.h"
8 #include "nuitrack/types/GestureData.h"
9 #include "nuitrack/capi/GestureRecognizer_CAPI.h"
10 #include "nuitrack/utils/ExceptionTranslator.h"
11 #include "nuitrack/utils/CallbackStruct.h"
12 
13 namespace tdv
14 {
15 namespace nuitrack
16 {
17 
33 class GestureRecognizer : public tdv::nuitrack::HeaderOnlyAPI_Module
34 {
35 public:
36  typedef std::shared_ptr<GestureRecognizer> Ptr;
37 
46  typedef std::function<void (GestureData::Ptr)> OnNewGestures;
47  typedef void (*OnNewGesturesRawFunctionPtr)(GestureData*, GestureRecognizer*);
48 
57  typedef std::function<void (UserStateData::Ptr)> OnUserStateChange;
58  typedef void (*OnUserStateChangeRawFunctionPtr)(UserStateData*, GestureRecognizer*);
59 
68  typedef std::function<void (UserGesturesStateData::Ptr)> OnUpdate;
69  typedef void (*OnUpdateRawFunctionPtr)(UserGesturesStateData*, GestureRecognizer*);
70 
78  static GestureRecognizer::Ptr create()
79  {
80  NuitrackModule* pimpl = NULL;
81  ExceptionType exception_code = nuitrack_CreateGestureRecognizer(&pimpl);
82  ExceptionTranslator::generateExceptionByErrorCode(exception_code);
83  return std::shared_ptr<GestureRecognizer>(new GestureRecognizer(pimpl));
84  }
85 
87  GestureRecognizer(NuitrackModule* pimpl)
88  {
89  _pimpl = pimpl;
90 
91  _onNewGesturesCallbackStruct = new CallbackStruct<GestureData::Ptr>();
92  _onUserStateChangeCallbackStruct = new CallbackStruct<UserStateData::Ptr>();
93  _onUpdateCallbackStruct = new CallbackStruct<UserGesturesStateData::Ptr>();
94 
95  _callbackWrapper.setGestureRecognizer(this);
96  _callbackWrapper.setOnNewGestureAddress(&onNewGesturesCallback);
97  _callbackWrapper.setOnUserStateChangeAddress(&onUserStateChangeCallback);
98  _callbackWrapper.setOnUpdateAddress(&onUpdateCallback);
99  nuitrack_registerGestureRecognizerCallback(_pimpl, &_callbackWrapper);
100  }
101 
102  virtual ~GestureRecognizer()
103  {
104  nuitrack_DestroyGestureRecognizer(_pimpl);
106  }
107 
116  uint64_t connectOnNewGestures(const OnNewGestures& callback)
117  {
118  return _onNewGesturesCallbackStruct->addCallback(callback);
119  }
120 
127  void disconnectOnNewGestures(uint64_t handler)
128  {
129  _onNewGesturesCallbackStruct->deleteCallback(handler);
130  }
131 
140  uint64_t connectOnUserStateChange(const OnUserStateChange& callback)
141  {
142  return _onUserStateChangeCallbackStruct->addCallback(callback);
143  }
144 
151  void disconnectOnUserStateChange(uint64_t handler)
152  {
153  _onUserStateChangeCallbackStruct->deleteCallback(handler);
154  }
155 
164  uint64_t connectOnUpdate(const OnUpdate& callback)
165  {
166  return _onUpdateCallbackStruct->addCallback(callback);
167  }
168 
175  void disconnectOnUpdate(uint64_t handler)
176  {
177  _onUpdateCallbackStruct->deleteCallback(handler);
178  }
179 
183  float getProcessingTime() const
184  {
185  return nuitrack_GetGestureRecognizerProcessingTime(_pimpl);
186  }
187 
196  void setControlGesturesStatus(bool status)
197  {
198  nuitrack_UserGesturesSetControlStatus(_pimpl, status);
199  }
200 
203  {
206  if(_onUserStateChangeCallbackStruct)
207  delete _onUserStateChangeCallbackStruct;
208  if(_onUpdateCallbackStruct)
209  delete _onUpdateCallbackStruct;
210  }
211 
212  bool canUpdate() const
213  {
214  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
215  }
216 
223  virtual uint64_t getTimestamp()
224  {
225  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
226  }
227 
228 protected:
230  static void onNewGesturesCallback(GestureRecognizerData* data, GestureRecognizer* tracker)
231  {
232  GestureData::Ptr newFrame(new GestureData(data));
233  tracker->_onNewGesturesCallbackStruct->executeAllCallbacks(newFrame);
234  }
236  static void onUserStateChangeCallback(UsersStateChanges* data, GestureRecognizer* tracker)
237  {
238  UserStateData::Ptr newFrame = UserStateData::Ptr(new UserStateData(data));
239  tracker->_onUserStateChangeCallbackStruct->executeAllCallbacks(newFrame);
240  }
242  static void onUpdateCallback(GestureRecognizerUserGesturesStateData* data, GestureRecognizer* tracker)
243  {
244  UserGesturesStateData::Ptr newFrame = UserGesturesStateData::Ptr(new UserGesturesStateData(data));
245  tracker->_onUpdateCallbackStruct->executeAllCallbacks(newFrame);
246  }
248  NuitrackModule* _pimpl;
250  GestureRecognizerCallbackWrapper _callbackWrapper;
252  CallbackStruct<GestureData::Ptr>* _onNewGesturesCallbackStruct;
253  CallbackStruct<UserStateData::Ptr>* _onUserStateChangeCallbackStruct;
254  CallbackStruct<UserGesturesStateData::Ptr>* _onUpdateCallbackStruct;
255 };
256 
257 } /* namespace nuitrack */
258 } /* namespace tdv */
259 
260 #endif /* NUITRACK_GESTURE_RECOGNIZER_H_ */
Represents the interface to the capabilities of the Nuitrack Gesture Recognizer module.
Definition: GestureRecognizer.h:33
std::function< void(UserStateData::Ptr)> OnUserStateChange
The callback type of the user state change event.
Definition: GestureRecognizer.h:57
static GestureRecognizer::Ptr create()
Create the GestureRecognizer module object.
Definition: GestureRecognizer.h:78
float getProcessingTime() const
Returns last gesture recognition time in milliseconds.
Definition: GestureRecognizer.h:183
Stores the results of user state estimation.
Definition: GestureData.h:84
void disconnectOnUserStateChange(uint64_t handler)
Remove a callback of the user state change event.
Definition: GestureRecognizer.h:151
ExceptionType
Possible error codes for Nuitrack internal functions.
Definition: Error.h:20
void destroyGestureRecognizer()
Definition: GestureRecognizer.h:202
Stores the results of gesture estimation.
Definition: GestureData.h:16
void disconnectOnNewGestures(uint64_t handler)
Remove a callback of the new gesture detection event.
Definition: GestureRecognizer.h:127
GestureRecognizerCallbackWrapper _callbackWrapper
Definition: GestureRecognizer.h:250
void setControlGesturesStatus(bool status)
Set control gesture recognition state.
Definition: GestureRecognizer.h:196
Stores the results of estimation of the state of gestures for every tracked user. ...
Definition: GestureData.h:152
static void onNewGesturesCallback(GestureRecognizerData *data, GestureRecognizer *tracker)
Definition: GestureRecognizer.h:230
NuitrackModule * _pimpl
Definition: GestureRecognizer.h:248
std::function< void(UserGesturesStateData::Ptr)> OnUpdate
The callback type of the user gesture data update request.
Definition: GestureRecognizer.h:68
uint64_t connectOnUpdate(const OnUpdate &callback)
Add a callback for the gesture state data update request.
Definition: GestureRecognizer.h:164
GestureRecognizer(NuitrackModule *pimpl)
Definition: GestureRecognizer.h:87
uint64_t connectOnNewGestures(const OnNewGestures &callback)
Add a callback for the new gesture detection event.
Definition: GestureRecognizer.h:116
CallbackStruct< GestureData::Ptr > * _onNewGesturesCallbackStruct
Definition: GestureRecognizer.h:252
static void onUserStateChangeCallback(UsersStateChanges *data, GestureRecognizer *tracker)
Definition: GestureRecognizer.h:236
std::function< void(GestureData::Ptr)> OnNewGestures
The callback type of the new gesture detection event.
Definition: GestureRecognizer.h:46
static void onUpdateCallback(GestureRecognizerUserGesturesStateData *data, GestureRecognizer *tracker)
Definition: GestureRecognizer.h:242
virtual uint64_t getTimestamp()
Returns the last processed data timestamp in microseconds.
Definition: GestureRecognizer.h:223
uint64_t connectOnUserStateChange(const OnUserStateChange &callback)
Add a callback for the user state change event.
Definition: GestureRecognizer.h:140
void disconnectOnUpdate(uint64_t handler)
Remove a callback of the gesture state data update request.
Definition: GestureRecognizer.h:175