Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
UserTracker.h
1 #ifndef NUITRACK_USERTRACKER_H_
2 #define NUITRACK_USERTRACKER_H_
3 
4 #include <functional>
5 
6 #include "nuitrack/modules/HeaderOnlyAPI_Module.h"
7 #include "nuitrack/types/Export.h"
8 #include "nuitrack/types/UserFrame.h"
9 #include "nuitrack/capi/UserTracker_CAPI.h"
10 #include "nuitrack/capi/Nuitrack_CAPI.h"
11 #include "nuitrack/utils/ExceptionTranslator.h"
12 #include "nuitrack/utils/CallbackStruct.h"
13 
14 
15 namespace tdv
16 {
17 namespace nuitrack
18 {
33 class UserTracker : public tdv::nuitrack::HeaderOnlyAPI_Module
34 {
35 public:
40  typedef std::shared_ptr<UserTracker> Ptr;
41 
49  typedef std::function<void (UserFrame::Ptr)> OnUpdate;
50 
51  typedef void (*OnUpdateRawFunctionPtr)(UserFrame*, UserTracker*);
52 
60  typedef std::function<void (int)> OnNewUser;
61 
62  typedef void (*OnNewUserRawFunctionPtr)(int, UserTracker*);
63 
71  typedef std::function<void (int)> OnLostUser;
72 
73  typedef void (*OnLostUserRawFunctionPtr)(int, UserTracker*);
74 
83  {
84  NuitrackModule* pimpl = NULL;
85  ExceptionType exception_code = nuitrack_CreateUserTracker(&pimpl);
86  ExceptionTranslator::generateExceptionByErrorCode(exception_code);;
87  return std::shared_ptr<UserTracker>(new UserTracker(pimpl));
88  }
89 
91  UserTracker(NuitrackModule* pimpl)
92  {
93  _pimpl = pimpl;
94 
95  _onUpdateCallbackStruct = new CallbackStruct<UserFrame::Ptr>();
96  _onNewUserCallbackStruct = new CallbackStruct<int>();
97  _onLostUserCallbackStruct = new CallbackStruct<int>();
98 
99  _callbackWrapper.setUserTracker(this);
100  _callbackWrapper.setOnUpdateAddress(&onUpdateCallback);
101  _callbackWrapper.setOnNewUserAddress(&onNewUserCallback);
102  _callbackWrapper.setOnLostUserAddress(&onLostUserCallback);
103 
104  nuitrack_registerUserTrackerCallback(_pimpl, &_callbackWrapper);
105  }
106 
107  virtual ~UserTracker()
108  {
109  nuitrack_DestroyUserTracker(_pimpl);
110  destroyUserTracker();
111  }
112 
120  uint64_t connectOnUpdate(const OnUpdate& callback)
121  {
122  return _onUpdateCallbackStruct->addCallback(callback);
123  }
124 
131  void disconnectOnUpdate(uint64_t handler)
132  {
133  _onUpdateCallbackStruct->deleteCallback(handler);
134  }
135 
144  uint64_t connectOnNewUser(const OnNewUser& callback)
145  {
146  return _onNewUserCallbackStruct->addCallback(callback);
147  }
148 
155  void disconnectOnNewUser(uint64_t handler)
156  {
157  _onNewUserCallbackStruct->deleteCallback(handler);
158  }
159 
168  uint64_t connectOnLostUser(const OnLostUser& callback)
169  {
170  return _onLostUserCallbackStruct->addCallback(callback);
171  }
172 
179  void disconnectOnLostUser(uint64_t handler)
180  {
181  _onLostUserCallbackStruct->deleteCallback(handler);
182  }
183 
187  UserFrame::Ptr getUserFrame()
188  {
189  UserTrackerDataPtr data = nuitrack_GetUserTrackerData(_pimpl);
190  UserFrame::Ptr newFrame(new UserFrame(data));
191  return newFrame;
192  }
193 
197  float getProcessingTime() const
198  {
199  return nuitrack_GetUserTrackerProcessingTime(_pimpl);
200  }
201 
202  bool canUpdate() const
203  {
204  return nuitrack_GetNuitrackModuleCanUpdate(_pimpl);
205  }
206 
214  virtual uint64_t getTimestamp()
215  {
216  return nuitrack_GetNuitrackModuleTimestamp(_pimpl);
217  }
218 
219 private:
220 
221  static void onUpdateCallback(UserTrackerData* userFrameImpl, UserTracker* tracker)
222  {
223  UserFrame::Ptr newFrame(new UserFrame(userFrameImpl));
224  tracker->_onUpdateCallbackStruct->executeAllCallbacks(newFrame);
225  }
226 
227  static void onNewUserCallback(int usedID, UserTracker* tracker)
228  {
229  tracker->_onNewUserCallbackStruct->executeAllCallbacks(usedID);
230  }
231  static void onLostUserCallback(int usedID, UserTracker* tracker)
232  {
233  tracker->_onLostUserCallbackStruct->executeAllCallbacks(usedID);
234  }
235 
236  void destroyUserTracker()
237  {
238  if(_onUpdateCallbackStruct)
239  delete _onUpdateCallbackStruct;
240  if(_onNewUserCallbackStruct)
241  delete _onNewUserCallbackStruct;
242  if(_onLostUserCallbackStruct)
243  delete _onLostUserCallbackStruct;
244  }
245 
246  NuitrackModule* _pimpl;
247 
248  UserTrackerCallbackWrapper _callbackWrapper;
249 
250  CallbackStruct<UserFrame::Ptr>* _onUpdateCallbackStruct;
251  CallbackStruct<int>* _onNewUserCallbackStruct;
252  CallbackStruct<int>* _onLostUserCallbackStruct;
253 };
254 
255 } /* namespace nuitrack */
256 } /* namespace tdv */
257 
258 
259 #endif /* NUITRACK_USERTRACKER_H_ */
float getProcessingTime() const
Returns last frame processing time in milliseconds.
Definition: UserTracker.h:197
ExceptionType
Possible error codes for Nuitrack internal functions.
Definition: Error.h:20
Represents a user map with information about the floor.
Definition: UserFrame.h:27
void disconnectOnNewUser(uint64_t handler)
Remove a callback of the new user appearance event.
Definition: UserTracker.h:155
static UserTracker::Ptr create()
Create the UserTracker module object.
Definition: UserTracker.h:82
UserFrame::Ptr getUserFrame()
Returns smart pointer to the last avilable UserFrame.
Definition: UserTracker.h:187
std::function< void(UserFrame::Ptr)> OnUpdate
The callback type of the user frame update request.
Definition: UserTracker.h:49
uint64_t connectOnLostUser(const OnLostUser &callback)
Add a callback for the user disappearance event.
Definition: UserTracker.h:168
std::function< void(int)> OnLostUser
The callback type of the user disapearance event.
Definition: UserTracker.h:71
void disconnectOnUpdate(uint64_t handler)
Remove a callback of the user frame update request.
Definition: UserTracker.h:131
virtual uint64_t getTimestamp()
Returns the last processed data timestamp in microseconds.
Definition: UserTracker.h:214
std::function< void(int)> OnNewUser
The callback type of the new user apearance event.
Definition: UserTracker.h:60
void disconnectOnLostUser(uint64_t handler)
Remove a callback of the user disappearance event.
Definition: UserTracker.h:179
uint64_t connectOnNewUser(const OnNewUser &callback)
Add a callback for the new user appearance event.
Definition: UserTracker.h:144
Represents the interface to the capabilities of the Nuitrack User Tracker module. ...
Definition: UserTracker.h:33
uint64_t connectOnUpdate(const OnUpdate &callback)
Add a callback for the user frame update request.
Definition: UserTracker.h:120
UserTracker(NuitrackModule *pimpl)
Definition: UserTracker.h:91
std::shared_ptr< UserTracker > Ptr
Smart pointer to access the UserTracker instance.
Definition: UserTracker.h:40