Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
SkeletonTracker.h
1 #ifndef NUITRACK_SKELETONTRACKER_H_
2 #define NUITRACK_SKELETONTRACKER_H_
3 
4 #include <functional>
5 
6 #include "nuitrack/modules/HeaderOnlyAPI_Module.h"
7 #include "nuitrack/types/Export.h"
8 #include "nuitrack/types/SkeletonData.h"
9 #include "nuitrack/capi/SkeletonTracker_CAPI.h"
10 #include "nuitrack/utils/ExceptionTranslator.h"
11 #include "nuitrack/utils/CallbackStruct.h"
12 
13 namespace tdv
14 {
15 namespace nuitrack
16 {
32 class SkeletonTracker : public tdv::nuitrack::HeaderOnlyAPI_Module
33 {
34 public:
39  typedef std::shared_ptr<SkeletonTracker> Ptr;
40 
49  typedef std::function<void (SkeletonData::Ptr)> OnUpdate;
50  typedef void (*OnUpdateRawFunctionPtr)(SkeletonData*, SkeletonTracker*);
51 
59  typedef std::function<void (SkeletonTracker::Ptr, int)> OnNewUser;
60  typedef void (*OnNewUserRawFunctionPtr)(SkeletonTracker*, int);
61 
69  typedef std::function<void (SkeletonTracker::Ptr, int)> OnLostUser;
70  typedef void (*OnLostUserRawFunctionPtr)(SkeletonTracker*, int);
71 
80  {
81  NuitrackModule* pimpl = NULL;
82  ExceptionType errorCode = nuitrack_CreateSkeletonTracker(&pimpl);
83  ExceptionTranslator::generateExceptionByErrorCode(errorCode);
84  return std::shared_ptr<SkeletonTracker>(new SkeletonTracker(pimpl));
85  }
86 
88  SkeletonTracker(NuitrackModule* pimpl)
89  {
90  _pimpl = pimpl;
91  _isCopy = 0;
92 
93  _onUpdateCallbackStruct = new CallbackStruct<SkeletonData::Ptr>();
94  _onNewUserCallbackStruct = new CallbackStruct2p<SkeletonTracker::Ptr, int>();
95  _onLostUserCallbackStruct = new CallbackStruct2p<SkeletonTracker::Ptr, int>();
96 
97  _callbackWrapper.setSkeletonTracker(this);
98  _callbackWrapper.setOnUpdateAddress(&onUpdateCallback);
99  _callbackWrapper.setOnNewUserAddress(&onNewUserCallback);
100  _callbackWrapper.setOnLostUserAddress(&onLostUserCallback);
101  nuitrack_registerSkeletonTrackerCallback(_pimpl, &_callbackWrapper);
102  }
103 
105  SkeletonTracker(NuitrackModule* pimpl, CallbackStruct<SkeletonData::Ptr>* onUpdateCallbackStruct,
106  CallbackStruct2p<SkeletonTracker::Ptr, int>* onNewUserCallbackStruct,
107  CallbackStruct2p<SkeletonTracker::Ptr, int>* onLostUserCallbackStruct)
108  {
109  _pimpl = pimpl;
110  _isCopy = 1;
111 
112  _onUpdateCallbackStruct = onUpdateCallbackStruct;
113  _onNewUserCallbackStruct = onNewUserCallbackStruct;
114  _onLostUserCallbackStruct = onLostUserCallbackStruct;
115  }
116 
117  virtual ~SkeletonTracker()
118  {
119  if(_isCopy == 0)
120  {
121  nuitrack_DestroySkeletonTracker(_pimpl);
123  }
124  }
125 
133  uint64_t connectOnUpdate(const OnUpdate& callback)
134  {
135  return _onUpdateCallbackStruct->addCallback(callback);
136  }
137 
144  void disconnectOnUpdate(uint64_t handler)
145  {
146  _onUpdateCallbackStruct->deleteCallback(handler);
147  }
148 
157  uint64_t connectOnNewUser(const OnNewUser& callback)
158  {
159  return _onNewUserCallbackStruct->addCallback(callback);
160  }
161 
168  void disconnectOnNewUser(uint64_t handler)
169  {
170  _onNewUserCallbackStruct->deleteCallback(handler);
171  }
172 
181  uint64_t connectOnLostUser(const OnLostUser& callback)
182  {
183  return _onLostUserCallbackStruct->addCallback(callback);
184  }
185 
192  void disconnectOnLostUser(uint64_t handler)
193  {
194  _onLostUserCallbackStruct->deleteCallback(handler);
195  }
196 
205  void setNumActiveUsers(int numUsers)
206  {
207  nuitrack_SetNumActiveUsers(_pimpl, numUsers);
208  }
209 
215  bool isAutoTracking() const
216  {
217  return nuitrack_IsAutoTracking(_pimpl);
218  }
219 
235  void setAutoTracking(bool tracking)
236  {
237  nuitrack_SetAutoTracking(_pimpl, tracking);
238  }
239 
247  void startTracking(int userID)
248  {
249  nuitrack_StartTracking(_pimpl, userID);
250  }
251 
259  void stopTracking(int userID)
260  {
261  nuitrack_StopTracking(_pimpl, userID);
262  }
263 
271  bool isTracking(int userID)
272  {
273  return nuitrack_IsTracking(_pimpl, userID);
274  }
275 
279  SkeletonData::Ptr getSkeletons()
280  {
281  SkeletonTrackerDataPtr data = nuitrack_GetSkeletonTrackerData(_pimpl);
282  SkeletonData::Ptr newFrame(new SkeletonData(data));
283  return newFrame;
284  }
285 
289  float getProcessingTime() const
290  {
291  return nuitrack_GetSkeletonTrackerProcessingTime(_pimpl);
292  }
293 
294  bool canUpdate() const
295  {
296  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
297  }
298 
305  virtual uint64_t getTimestamp()
306  {
307  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
308  }
309 
310 protected:
312  static void onUpdateCallback(SkeletonTrackerData* frame, SkeletonTracker* tracker)
313  {
314  SkeletonData::Ptr newFrame(new SkeletonData(frame));
315 
316  tracker->_onUpdateCallbackStruct->executeAllCallbacks(newFrame);
317  }
318 
320  static void onNewUserCallback(SkeletonTracker* tracker, int usedID)
321  {
322 
324  new SkeletonTracker(tracker->_pimpl, tracker->_onUpdateCallbackStruct,
326 
327  tracker->_onNewUserCallbackStruct->executeAllCallbacks(ptr, usedID);
328  }
329 
331  static void onLostUserCallback( SkeletonTracker* tracker, int usedID)
332  {
333 
335  new SkeletonTracker(tracker->_pimpl, tracker->_onUpdateCallbackStruct,
337 
338  tracker->_onLostUserCallbackStruct->executeAllCallbacks(ptr, usedID);
339  }
340 
343  {
350  }
351 
352  NuitrackModule* _pimpl;
353  int8_t _isCopy;
354 
355  CallbackStruct<SkeletonData::Ptr>* _onUpdateCallbackStruct;
356  CallbackStruct2p<SkeletonTracker::Ptr, int>* _onNewUserCallbackStruct;
357  CallbackStruct2p<SkeletonTracker::Ptr, int>* _onLostUserCallbackStruct;
358 
359  SkeletonTrackerCallbackWrapper _callbackWrapper;
360 };
361 
362 } /* namespace nuitrack */
363 } /* namespace tdv */
364 
365 #endif /* NUITRACK_SKELETONTRACKER_H_ */
SkeletonData::Ptr getSkeletons()
Returns smart pointer to the last avilable SkeletonData.
Definition: SkeletonTracker.h:279
float getProcessingTime() const
Returns last skeleton data processing time in milliseconds.
Definition: SkeletonTracker.h:289
void setNumActiveUsers(int numUsers)
Set maximum number of users for tracking.
Definition: SkeletonTracker.h:205
std::shared_ptr< SkeletonTracker > Ptr
Smart pointer to access the SkeletonTracker instance.
Definition: SkeletonTracker.h:39
ExceptionType
Possible error codes for Nuitrack internal functions.
Definition: Error.h:20
static void onNewUserCallback(SkeletonTracker *tracker, int usedID)
Definition: SkeletonTracker.h:320
void stopTracking(int userID)
Stop tracking of a user.
Definition: SkeletonTracker.h:259
Stores the data of all available skeletons at a certain point in time.
Definition: SkeletonData.h:19
SkeletonTracker(NuitrackModule *pimpl)
Definition: SkeletonTracker.h:88
void setAutoTracking(bool tracking)
Set user skeleton auto tracking state.
Definition: SkeletonTracker.h:235
static void onUpdateCallback(SkeletonTrackerData *frame, SkeletonTracker *tracker)
Definition: SkeletonTracker.h:312
CallbackStruct2p< SkeletonTracker::Ptr, int > * _onNewUserCallbackStruct
Definition: SkeletonTracker.h:356
SkeletonTracker(NuitrackModule *pimpl, CallbackStruct< SkeletonData::Ptr > *onUpdateCallbackStruct, CallbackStruct2p< SkeletonTracker::Ptr, int > *onNewUserCallbackStruct, CallbackStruct2p< SkeletonTracker::Ptr, int > *onLostUserCallbackStruct)
Definition: SkeletonTracker.h:105
uint64_t connectOnUpdate(const OnUpdate &callback)
Add a callback for the skeleton data update request.
Definition: SkeletonTracker.h:133
NuitrackModule * _pimpl
Definition: SkeletonTracker.h:352
virtual uint64_t getTimestamp()
Returns the last processed data timestamp in microseconds.
Definition: SkeletonTracker.h:305
Represents the interface to the capabilities of the Nuitrack Skeleton Tracker module.
Definition: SkeletonTracker.h:32
uint64_t connectOnLostUser(const OnLostUser &callback)
Add a callback for the active user disappearance event.
Definition: SkeletonTracker.h:181
void startTracking(int userID)
Start tracking of a user.
Definition: SkeletonTracker.h:247
void disconnectOnUpdate(uint64_t handler)
Remove a callback of the skeleton data update request.
Definition: SkeletonTracker.h:144
bool isTracking(int userID)
Get user tracking state.
Definition: SkeletonTracker.h:271
std::function< void(SkeletonTracker::Ptr, int)> OnNewUser
The callback type of the new active user apearance event.
Definition: SkeletonTracker.h:59
static void onLostUserCallback(SkeletonTracker *tracker, int usedID)
Definition: SkeletonTracker.h:331
void disconnectOnLostUser(uint64_t handler)
Remove a callback of the active user disappearance event.
Definition: SkeletonTracker.h:192
CallbackStruct2p< SkeletonTracker::Ptr, int > * _onLostUserCallbackStruct
Definition: SkeletonTracker.h:357
CallbackStruct< SkeletonData::Ptr > * _onUpdateCallbackStruct
Definition: SkeletonTracker.h:355
uint64_t connectOnNewUser(const OnNewUser &callback)
Add a callback for the new active user appearance event.
Definition: SkeletonTracker.h:157
bool isAutoTracking() const
Returns true if auto tracking is enabled, false otherwise.
Definition: SkeletonTracker.h:215
std::function< void(SkeletonData::Ptr)> OnUpdate
The callback type of the skeleton data update request.
Definition: SkeletonTracker.h:49
void disconnectOnNewUser(uint64_t handler)
Remove a callback of the new active user appearance event.
Definition: SkeletonTracker.h:168
SkeletonTrackerCallbackWrapper _callbackWrapper
Definition: SkeletonTracker.h:359
std::function< void(SkeletonTracker::Ptr, int)> OnLostUser
The callback type of the active user disapearance event.
Definition: SkeletonTracker.h:69
int8_t _isCopy
Definition: SkeletonTracker.h:353
static SkeletonTracker::Ptr create()
Create the SkeletonTracker module object.
Definition: SkeletonTracker.h:79
void destroySkeletonTracker()
Definition: SkeletonTracker.h:342