Repository navigation
Expand file tree
/
Copy pathUnityInterface.cpp
More file actions
610 lines (513 loc) · 16.7 KB
/
Copy pathUnityInterface.cpp
File metadata and controls
610 lines (513 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*****************************************************************************
* *
* Unity Wrapper *
* Copyright (C) 2010 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
*****************************************************************************/
//Author: Shlomo Zippel
//-----------------------------------------------------------------------------
// Headers
//-----------------------------------------------------------------------------
#include "UnityInterface.h"
#include <XnOpenNI.h>
#include <XnCppWrapper.h>
#include <XnVSessionManager.h>
#include <XnVPointControl.h>
#include <XnVSelectableSlider1D.h>
#include <XnVPointDenoiser.h>
#include "UsersManager.h"
//-----------------------------------------------------------------------------
// Globals
//-----------------------------------------------------------------------------
// OpenNI stuff
static xn::Context g_Context;
static xn::DepthGenerator g_DepthGenerator;
static xn::UserGenerator g_UserGenerator;
static XnCallbackHandle g_hUserCallbacks = NULL;
static XnCallbackHandle g_hCalibrationCallbacks = NULL;
static XnCallbackHandle g_hPoseCallbacks = NULL;
static unsigned int g_nDepthWidth = 0;
static unsigned int g_nDepthHeight = 0;
static bool g_bSearchingForPlayers = false;
static UsersManager * g_pUsersManager = NULL;
// UI mode stuff
static XnVSessionManager * g_pSessionManager = NULL;
static XnVPointDenoiser * g_pPointDenoiser = NULL;
static XnVPointControl g_PointControl;
static XnVSelectableSlider1D* g_pMainSlider = NULL;
// A dummy focus gesture - doesn't do anything
// We will use this as our focus gesture when the user will force a UI session
// at a specific location
class XnVDummyGesture : public XnVGesture
{
virtual void Update(const xn::Context* pContext) {}
};
XnVDummyGesture * g_pDummyGesture = NULL;
//-----------------------------------------------------------------------------
// Callbacks
//-----------------------------------------------------------------------------
static pfn_user_callback g_pfnNewUser = NULL;
static pfn_user_callback g_pfnCalibrationStarted = NULL;
static pfn_user_callback g_pfnCalibrationSucceeded = NULL;
static pfn_user_callback g_pfnCalibrationFailed = NULL;
static pfn_user_callback g_pfnUserLost = NULL;
static pfn_focus_callback g_pfnFocused = NULL;
static pfn_handpoint_callback g_pfnHandPoint = NULL;
static pfn_item_callback g_pfnHighlighted = NULL;
static pfn_item_callback g_pfnSelected = NULL;
static pfn_value_callback g_pfnValueChanged = NULL;
static pfn_value_callback g_pfnScroll = NULL;
//-----------------------------------------------------------------------------
// Internal event handlers
//-----------------------------------------------------------------------------
// Point create/destroy callbacks
void XN_CALLBACK_TYPE OnPrimaryPointCreate(const XnVHandPointContext* hand, const XnPoint3D& ptFocus, void* cxt)
{
if (NULL != g_pfnFocused)
{
g_pfnFocused(true);
}
}
void XN_CALLBACK_TYPE OnPrimaryPointDestroy(XnUInt32 nID, void* cxt)
{
if (NULL != g_pfnFocused)
{
g_pfnFocused(false);
}
}
void XN_CALLBACK_TYPE OnPrimaryPointUpdate(const XnVHandPointContext* pContext, void* cxt)
{
if (NULL != g_pfnHandPoint)
{
g_pfnHandPoint(pContext->ptPosition.X, pContext->ptPosition.Y, pContext->ptPosition.Z);
}
}
static void XN_CALLBACK_TYPE OnNewUser(xn::UserGenerator& generator, const XnUserID nUserId, void* pCookie)
{
if (g_bSearchingForPlayers)
{
if (NULL != g_pfnNewUser)
{
g_pfnNewUser(nUserId);
}
generator.GetPoseDetectionCap().StartPoseDetection("Psi", nUserId);
}
}
static void XN_CALLBACK_TYPE OnLostUser(xn::UserGenerator& generator, const XnUserID nUserId, void* pCookie)
{
if (NULL != g_pfnUserLost)
{
g_pfnUserLost(nUserId);
}
}
static void XN_CALLBACK_TYPE OnPoseDetected(xn::PoseDetectionCapability& poseDetection, const XnChar* strPose, XnUserID nId, void* pCookie)
{
// Stop detecting the pose
poseDetection.StopPoseDetection(nId);
// Start calibrating if we are looking for players
if (g_bSearchingForPlayers)
{
g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
}
}
static void XN_CALLBACK_TYPE OnCalibrationStart(xn::SkeletonCapability& skeleton, const XnUserID nUserId, void* pCookie)
{
// callback
if (NULL != g_pfnCalibrationStarted)
{
g_pfnCalibrationStarted(nUserId);
}
}
static void XN_CALLBACK_TYPE OnCalibrationEnd(xn::SkeletonCapability& skeleton, const XnUserID nUserId, XnBool bSuccess, void* pCookie)
{
// If this was a successful calibration
if (bSuccess)
{
// start tracking
skeleton.StartTracking(nUserId);
// callback
if (NULL != g_pfnCalibrationSucceeded)
{
g_pfnCalibrationSucceeded(nUserId);
}
}
else
{
// failure callback
if (NULL != g_pfnCalibrationFailed)
{
g_pfnCalibrationFailed(nUserId);
}
// Restart pose detection if still searching for players
if (g_bSearchingForPlayers)
{
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection("Psi", nUserId);
}
}
}
// Main slider
void XN_CALLBACK_TYPE MainSlider_OnHover(XnInt32 nItem, void* cxt)
{
// TODO: Unhighlight old one?
if (NULL != g_pfnHighlighted)
{
g_pfnHighlighted(nItem, true);
}
}
void XN_CALLBACK_TYPE MainSlider_OnSelect(XnInt32 nItem, XnVDirection dir, void* cxt)
{
if (NULL != g_pfnSelected)
{
g_pfnSelected(nItem, dir);
}
}
void XN_CALLBACK_TYPE MainSlider_OnValueChange(XnFloat fValue, void* cxt)
{
if (NULL != g_pfnValueChanged)
{
g_pfnValueChanged(fValue);
}
}
void XN_CALLBACK_TYPE MainSlider_OnScroll(XnFloat fScrollValue, void* pUserCxt)
{
if (NULL != g_pfnScroll)
{
g_pfnScroll(fScrollValue);
}
}
//-----------------------------------------------------------------------------
// UnityInterface API
//-----------------------------------------------------------------------------
XnStatus Init(const char * strXmlPath)
{
XnStatus rc = XN_STATUS_OK;
// Init NITE context
rc = g_Context.InitFromXmlFile(strXmlPath);
if (XN_STATUS_OK != rc)
{
return rc;
}
// Save width & height of our depth generator (also ensures we will have one)
XnMapOutputMode mapOutputMode;
rc = g_Context.FindExistingNode(XN_NODE_TYPE_DEPTH, g_DepthGenerator);
if (XN_STATUS_OK != rc)
{
Shutdown();
return rc;
}
rc = g_DepthGenerator.GetMapOutputMode(mapOutputMode);
if (XN_STATUS_OK != rc)
{
Shutdown();
return rc;
}
g_nDepthWidth = mapOutputMode.nXRes;
g_nDepthHeight = mapOutputMode.nYRes;
// Get our UserGenerator for later (also ensures we have one)
rc = g_Context.FindExistingNode(XN_NODE_TYPE_USER, g_UserGenerator);
if (XN_STATUS_OK != rc)
{
Shutdown();
return rc;
}
g_UserGenerator.RegisterUserCallbacks(OnNewUser, OnLostUser, NULL, g_hUserCallbacks);
g_UserGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
g_UserGenerator.GetSkeletonCap().RegisterCalibrationCallbacks(&OnCalibrationStart, &OnCalibrationEnd, NULL, g_hCalibrationCallbacks);
g_UserGenerator.GetSkeletonCap().SetSmoothing(0.5);
g_UserGenerator.GetPoseDetectionCap().RegisterToPoseCallbacks(&OnPoseDetected, NULL, NULL, g_hPoseCallbacks);
// users manager
g_pUsersManager = new UsersManager(g_UserGenerator);
// Setup a blank point control for the create/destroy events
// We will use those for focus events
g_PointControl.RegisterPrimaryPointCreate(NULL, &OnPrimaryPointCreate);
g_PointControl.RegisterPrimaryPointDestroy(NULL, &OnPrimaryPointDestroy);
g_PointControl.RegisterPrimaryPointUpdate(NULL, &OnPrimaryPointUpdate);
// The point denoiser will smoothen out our hand points
g_pPointDenoiser = new XnVPointDenoiser();
g_pDummyGesture = new XnVDummyGesture();
rc = g_Context.StartGeneratingAll();
if (XN_STATUS_OK != rc)
{
Shutdown();
return rc;
}
return XN_STATUS_OK;
}
void Update(bool async)
{
// update openni
if (async)
{
g_Context.WaitNoneUpdateAll();
}
else
{
g_Context.WaitAndUpdateAll();
}
// update users manager
g_pUsersManager->Update();
// if in UI mode - update ui
if (NULL != g_pSessionManager)
{
g_pSessionManager->Update(&g_Context);
}
}
void Shutdown()
{
// Stop generating events
g_Context.StopGeneratingAll();
// Unregister any callbacks we may have
if (NULL != g_hPoseCallbacks)
{
g_UserGenerator.GetPoseDetectionCap().UnregisterFromPoseCallbacks(g_hPoseCallbacks);
g_hPoseCallbacks = NULL;
}
if (NULL != g_hCalibrationCallbacks)
{
g_UserGenerator.GetSkeletonCap().UnregisterCalibrationCallbacks(g_hCalibrationCallbacks);
g_hCalibrationCallbacks = NULL;
}
if (NULL != g_hUserCallbacks)
{
g_UserGenerator.GetSkeletonCap().UnregisterCalibrationCallbacks(g_hUserCallbacks);
g_hUserCallbacks = NULL;
}
// user manager
delete g_pUsersManager;
g_pUsersManager = NULL;
// UI
if (NULL != g_pSessionManager)
{
delete g_pSessionManager;
g_pSessionManager = NULL;
}
if (NULL != g_pDummyGesture)
{
delete g_pDummyGesture;
g_pDummyGesture = NULL;
}
// Bye bye open ni
g_Context.Shutdown();
}
const char * GetStatusString(const XnStatus rc)
{
return xnGetStatusString(rc);
}
unsigned int GetDepthWidth()
{
return g_nDepthWidth;
}
unsigned int GetDepthHeight()
{
return g_nDepthHeight;
}
void StartLookingForUsers(pfn_user_callback pNewUser, pfn_user_callback pCalibrationStarted, pfn_user_callback pCalibrationFailed, pfn_user_callback pCalibrationSuccess, pfn_user_callback pUserLost)
{
// save our callbacks for later
g_pfnNewUser = pNewUser;
g_pfnCalibrationStarted = pCalibrationStarted;
g_pfnCalibrationFailed = pCalibrationFailed;
g_pfnCalibrationSucceeded = pCalibrationSuccess;
g_pfnUserLost = pUserLost;
// start pose detection for all existing non-calibrated users
XnUInt16 nUsers = g_UserGenerator.GetNumberOfUsers();
XnUserID * pUsers = new XnUserID[nUsers];
g_UserGenerator.GetUsers(pUsers, nUsers);
for (int i = 0; i < nUsers; i++)
{
if (!g_UserGenerator.GetSkeletonCap().IsCalibrated(pUsers[i]) &&
!g_UserGenerator.GetSkeletonCap().IsCalibrating(pUsers[i]))
{
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection("Psi", pUsers[i]);
}
}
delete [] pUsers;
// make sure we try to calibrate future players
g_bSearchingForPlayers = true;
}
void StopLookingForUsers()
{
g_bSearchingForPlayers = false;
}
void LoseUsers()
{
// stop tracking all users
XnUInt16 nUsers = g_UserGenerator.GetNumberOfUsers();
XnUserID * pUsers = new XnUserID[nUsers];
g_UserGenerator.GetUsers(pUsers, nUsers);
for (int i = 0; i < nUsers; i++)
{
if (g_UserGenerator.GetSkeletonCap().IsCalibrating(pUsers[i]))
{
g_UserGenerator.GetSkeletonCap().Reset(pUsers[i]);
if (NULL != g_pfnCalibrationFailed)
{
g_pfnCalibrationFailed(pUsers[i]);
}
}
if (g_UserGenerator.GetSkeletonCap().IsCalibrated(pUsers[i]))
{
g_UserGenerator.GetSkeletonCap().Reset(pUsers[i]);
if (NULL != g_pfnUserLost)
{
g_pfnUserLost(pUsers[i]);
}
}
}
delete [] pUsers;
}
bool GetUserCenterOfMass(XnUserID userID, OUT XnVector3D * pCom)
{
return (XN_STATUS_OK == g_UserGenerator.GetCoM(userID, *pCom));
}
float GetUserPausePoseProgress(XnUserID userID)
{
UserContext * pUser = g_pUsersManager->GetUserContext(userID);
if (NULL == pUser) return 0.0f;
return (float)pUser->pauseGesturePoseDetector.GetDetectionProgress();
}
const XnLabel * GetUsersLabelMap()
{
xn::SceneMetaData smd;
g_UserGenerator.GetUserPixels(0, smd);
return smd.Data();
}
const XnDepthPixel * GetUsersDepthMap()
{
return g_DepthGenerator.GetDepthMap();
}
void SetSkeletonSmoothing(double f)
{
g_UserGenerator.GetSkeletonCap().SetSmoothing(f);
}
bool GetJointTransformation(XnUserID userID, XnSkeletonJoint joint, OUT XnSkeletonJointTransformation* pTransformation)
{
xn::SkeletonCapability skelCap = g_UserGenerator.GetSkeletonCap();
if(userID == 0 || !skelCap.IsTracking(userID))
{
return false;
}
skelCap.GetSkeletonJoint(userID, joint, *pTransformation);
return true;
}
bool GetJointPosition(XnUserID userID, XnSkeletonJoint joint, OUT XnSkeletonJointPosition* pPosition)
{
xn::SkeletonCapability skelCap = g_UserGenerator.GetSkeletonCap();
if(userID == 0 || !skelCap.IsTracking(userID))
{
return false;
}
skelCap.GetSkeletonJointPosition(userID, joint, *pPosition);
return true;
}
bool GetJointOrientation(XnUserID userID, XnSkeletonJoint joint, OUT XnSkeletonJointOrientation* pOrientation)
{
xn::SkeletonCapability skelCap = g_UserGenerator.GetSkeletonCap();
if(userID == 0 || !skelCap.IsTracking(userID))
{
return false;
}
skelCap.GetSkeletonJointOrientation(userID, joint, *pOrientation);
return true;
}
void StartUIMode(pfn_focus_callback pFocus, pfn_handpoint_callback pHandPoint, bool bUseFocusGesture)
{
// cleanup previous UI mode
if (NULL != g_pSessionManager)
{
StopUIMode();
}
// save our callbacks
g_pfnFocused = pFocus;
g_pfnHandPoint = pHandPoint;
// initialize the point tracker
g_pSessionManager = new XnVSessionManager();
g_pSessionManager->Initialize(&g_Context, "Click", "RaiseHand");
if (!bUseFocusGesture)
{
g_pSessionManager->SetGesture(g_pDummyGesture);
g_pSessionManager->SetQRGesture(g_pDummyGesture);
g_pSessionManager->SetQuickRefocusTimeout(0);
}
g_pSessionManager->AddListener(g_pPointDenoiser);
g_pPointDenoiser->AddListener(&g_PointControl);
}
void ForceUISession(float x, float y, float z)
{
XnPoint3D ptFocus;
ptFocus.X = x;
ptFocus.Y = y;
ptFocus.Z = z;
g_pSessionManager->ForceSession(ptFocus);
}
void StopUIMode()
{
g_pfnHandPoint = NULL;
g_pfnFocused = NULL;
if (NULL != g_pSessionManager)
{
delete g_pSessionManager;
g_pSessionManager = NULL;
}
}
void StartScrollingMenu(bool horiz, int items, float scrollsize, pfn_item_callback selected_cb, pfn_item_callback highlighted_cb, pfn_value_callback value_cb, pfn_value_callback scroll_cb)
{
// dont do anything if not in UI mode
if (NULL == g_pSessionManager)
{
return;
}
// cleanup any existing menu
if (NULL != g_pMainSlider)
{
g_pSessionManager->RemoveListener(g_pMainSlider);
delete g_pMainSlider;
g_pMainSlider = NULL;
}
// save callbacks
g_pfnHighlighted = highlighted_cb;
g_pfnSelected = selected_cb;
g_pfnValueChanged = value_cb;
g_pfnScroll = scroll_cb;
// create our new slider
g_pMainSlider = new XnVSelectableSlider1D(items, scrollsize, (horiz) ? AXIS_X : AXIS_Y);
g_pMainSlider->RegisterItemHover(NULL, &MainSlider_OnHover);
g_pMainSlider->RegisterItemSelect(NULL, &MainSlider_OnSelect);
g_pMainSlider->RegisterValueChange(NULL, &MainSlider_OnValueChange);
g_pMainSlider->RegisterScroll(NULL, &MainSlider_OnScroll);
// recenter around focus point
XnPoint3D ptCenter;
g_pSessionManager->GetFocusPoint(ptCenter);
g_pMainSlider->Reposition(ptCenter);
// add it to our session manager
g_pPointDenoiser->AddListener(g_pMainSlider);
}
void StopMenu()
{
// cleanup any existing menu
if (NULL != g_pMainSlider)
{
g_pPointDenoiser->RemoveListener(g_pMainSlider);
delete g_pMainSlider;
g_pMainSlider = NULL;
}
g_pfnHighlighted = NULL;
g_pfnSelected = NULL;
g_pfnValueChanged = NULL;
g_pfnScroll = NULL;
}