Nuitrack  1.11.2
3D Skeleton Tracking Middleware
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Events Groups Pages
Nuitrack.h
1 #ifndef NUITRACK_HEADER_ONLY_API_H_
2 #define NUITRACK_HEADER_ONLY_API_H_
3 
4 #include <string>
5 #include <memory>
6 #include <functional>
7 
8 #include <stdio.h>
9 
10 #include "nuitrack/types/Export.h"
11 #include "nuitrack/types/Error.h"
12 
13 #include "nuitrack/modules/NuitrackModule.h"
14 #include "nuitrack/modules/DepthSensor.h"
15 #include "nuitrack/modules/ColorSensor.h"
16 #include "nuitrack/modules/UserTracker.h"
17 #include "nuitrack/modules/SkeletonTracker.h"
18 #include "nuitrack/modules/GestureRecognizer.h"
19 #include "nuitrack/modules/HandTracker.h"
20 #include "nuitrack/types/IssuesData.h"
21 #include "nuitrack/types/NuitrackDevice.h"
22 
23 #include "nuitrack/capi/Public_Nuitrack_CAPI.h"
24 #include "nuitrack/capi/IssueTracker_CAPI.h"
25 #include "nuitrack/capi/NuitrackDevice_CAPI.h"
26 
27 namespace tdv
28 {
29 namespace nuitrack
30 {
31 
47 class NUITRACK_LOCAL Nuitrack
48 {
49 public:
60  static void init(const std::string& config = "")
61  {
62  nuitrack_error* e = nullptr;
63  nuitrack_InitializeFromConfig_E(config.c_str(), &e);
64  ExceptionTranslator::handle(e);
65  }
66 
75  static void run()
76  {
77  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_Run());
78  }
79 
89  static void update()
90  {
91  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_Update());
92  }
93 
108  static void update(std::shared_ptr<HeaderOnlyAPI_Module> module)
109  {
110  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_SyncUpdatePublic(module.get()));
111  }
112 
122  static void waitUpdate(std::shared_ptr<HeaderOnlyAPI_Module> module)
123  {
124  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_WaitSyncUpdatePublic(module.get()));
125  }
126 
133  static void release()
134  {
135  CallbackStruct<IssuesData::Ptr>* callbackStruct =
136  (CallbackStruct<IssuesData::Ptr>*)nuitrack_getIssuesCallbackStruct();
137  if(callbackStruct != NULL)
138  {
139  nuitrack_setIssuesCallbackStruct(NULL);
140  delete callbackStruct;
141  }
142  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_Release());
143  }
144 
152  static void setConfigValue(const std::string& key, const std::string& value)
153  {
154  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_SetConfigValue(key.c_str(), value.c_str()));
155  }
156 
164  static std::string getConfigValue(const std::string& key)
165  {
166  const int bufferSize = 5000;
167  std::string result;
168  result.resize(bufferSize);
169  ExceptionTranslator::generateExceptionByErrorCode(
170  nuitrack_GetConfigValue(key.c_str(), (char *)result.c_str(), bufferSize));
171 
172  result.resize(strlen(result.c_str()));
173  return result;
174  }
175 
182  static std::string getInstancesJson()
183  {
184  int size = 0;
185  nuitrack_error* e = nullptr;
186  nuitrack_GetInstancesJsonSize(&size, &e);
187  ExceptionTranslator::handle(e);
188 
189  std::string result;
190  result.resize(size);
191  nuitrack_GetInstancesJsonData((char *)result.c_str(), &e);
192  ExceptionTranslator::handle(e);
193 
194  return result;
195  }
196 
204  static int getVersion()
205  {
206  int version;
207  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_GetVersion(&version));
208 
209  return version;
210  }
211 
219  typedef std::function<void (IssuesData::Ptr)> OnIssueUpdate;
220 
228  static uint64_t connectOnIssuesUpdate(const OnIssueUpdate& callback)
229  {
230  IssueTracker* issueTracker = NULL;
231  nuitrack_getIssueTracker(&issueTracker);
232  if(issueTracker == NULL)
233  return 0;
234 
235  if(nuitrack_getIssuesCallbackStruct() == NULL)
236  {
237  CallbackStruct<IssuesData::Ptr>* ptr = new CallbackStruct<IssuesData::Ptr>();
238  nuitrack_setIssuesCallbackStruct(ptr);
239 
240  IssueTrackerCallbackWrapper* callbackWrapper = new IssueTrackerCallbackWrapper();
241  callbackWrapper->setIssueTracker(issueTracker);
242  callbackWrapper->setFunctionAddress(&onIssuesUpdateCallback);
243  nuitrack_registerIssuesTrackerCallback(callbackWrapper);
244  }
245 
246  return ((CallbackStruct<IssuesData::Ptr>*)(nuitrack_getIssuesCallbackStruct()))->addCallback(callback);
247  }
248 
250  static void onIssuesUpdateCallback(IssueTrackerData* data)
251  {
252  IssuesData::Ptr newData = IssuesData::Ptr(new IssuesData(data));
253  CallbackStruct<IssuesData::Ptr>* callbackStruct =
254  (CallbackStruct<IssuesData::Ptr>*)nuitrack_getIssuesCallbackStruct();
255  if(callbackStruct == NULL)
256  return;
257  callbackStruct->executeAllCallbacks(newData);
258  }
259 
266  static void disconnectOnIssuesUpdate(uint64_t handler)
267  {
268  CallbackStruct<IssuesData::Ptr>* callbackStruct =
269  (CallbackStruct<IssuesData::Ptr>*)nuitrack_getIssuesCallbackStruct();
270  if(callbackStruct == NULL)
271  return;
272  callbackStruct->deleteCallback(handler);
273  }
274 
281  static std::vector<device::NuitrackDevice::Ptr> getDeviceList()
282  {
283  std::vector<device::NuitrackDevice::Ptr> result;
284  int device_limit = 0;
285  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_nuitrackDevice_getDeviceListLimitConst(device_limit));
286  std::vector<NuitrackDeviceDataPtr> bufferOfDevices(device_limit);
287  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_GetDeviceList(bufferOfDevices.data(), device_limit));
288 
289  for(int i = 0; i < device_limit; i++)
290  {
291  if(bufferOfDevices[i] == NULL)
292  break;
293  result.emplace_back(new device::NuitrackDevice(bufferOfDevices[i]));
294  }
295 
296  return result;
297  }
298 
305  static void setDevice(device::NuitrackDevice::Ptr dev)
306  {
307  ExceptionTranslator::generateExceptionByErrorCode(nuitrack_SetDevice(dev->_pimpl));
308  }
309 
316  static std::string getLicense()
317  {
318  const int bufferSize = 5000;
319  std::string result;
320  result.resize(bufferSize);
321  ExceptionTranslator::generateExceptionByErrorCode(
322  nuitrack_GetLicense((char *)result.c_str(), bufferSize));
323 
324  result.resize(strlen(result.c_str()));
325  return result;
326  }
327 };
328 
329 } /* namespace nuitrack */
330 } /* namespace tdv */
331 
332 #endif /* NUITRACK_HEADER_ONLY_API_H_ */
static std::string getConfigValue(const std::string &key)
Get the value of a Nuitrack configuration parameter.
Definition: Nuitrack.h:164
static void update()
Initiate Nuitrack update.
Definition: Nuitrack.h:89
static void setConfigValue(const std::string &key, const std::string &value)
Set the value of a Nuitrack configuration parameter.
Definition: Nuitrack.h:152
static void waitUpdate(std::shared_ptr< HeaderOnlyAPI_Module > module)
Initiate Nuitrack module update and wait for it.
Definition: Nuitrack.h:122
static int getVersion()
Get Nuitrack version. The version is calculated by the formula: major * 10000 + minor * 100 + revisio...
Definition: Nuitrack.h:204
Definition: Error.h:174
std::function< void(IssuesData::Ptr)> OnIssueUpdate
Callback type of the issue update request.
Definition: Nuitrack.h:219
static void run()
Start processing the data provided by the sensor.
Definition: Nuitrack.h:75
static void setDevice(device::NuitrackDevice::Ptr dev)
Setting a device to run.
Definition: Nuitrack.h:305
static uint64_t connectOnIssuesUpdate(const OnIssueUpdate &callback)
Add a callback for the issue update request.
Definition: Nuitrack.h:228
static void init(const std::string &config="")
Initialize Nuitrack.
Definition: Nuitrack.h:60
static void disconnectOnIssuesUpdate(uint64_t handler)
Remove a callback of the issue update request.
Definition: Nuitrack.h:266
Central class for common Nuitrack operations.
Definition: Nuitrack.h:47
Contains information and settings for a device.
Definition: NuitrackDevice.h:19
static std::string getInstancesJson()
Get the JSON string of Nuitrack instance-based API.
Definition: Nuitrack.h:182
static void update(std::shared_ptr< HeaderOnlyAPI_Module > module)
Initiate Nuitrack module data update.
Definition: Nuitrack.h:108
static void release()
Stop data processing and destroy all existing Nuitrack modules.
Definition: Nuitrack.h:133
static void onIssuesUpdateCallback(IssueTrackerData *data)
Definition: Nuitrack.h:250
Stores results of issue detection.
Definition: IssuesData.h:24
std::shared_ptr< IssuesData > Ptr
Smart pointer to access the IssuesData instance.
Definition: IssuesData.h:42
static std::vector< device::NuitrackDevice::Ptr > getDeviceList()
Get a list of available devices.
Definition: Nuitrack.h:281
static std::string getLicense()
Get current device license.
Definition: Nuitrack.h:316