-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnite2.cpp
More file actions
1306 lines (1108 loc) · 41.1 KB
/
nite2.cpp
File metadata and controls
1306 lines (1108 loc) · 41.1 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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define NO_IMPORT_ARRAY
#include "boost_python.h"
#include "nite2.h"
#include <NiTE.h>
#include "wrapper_utils.h"
#include "openni2.h"
#ifdef _MSC_VER
#define ALWAYS_INLINE __forceinline
#elif __GNUC__
#define ALWAYS_INLINE __attribute__((always_inline))
#else
#define ALWAYS_INLINE inline
#endif
//-----------------------------------------------------------------------------------------------
class NiteWrapper : private nite::NiTE
{
public:
static inline nite::Status initialize()
{
ScopedGILRelease release_gil;
return nite::NiTE::initialize();
}
static inline void shutdown()
{
ScopedGILRelease release_gil;
nite::NiTE::shutdown();
}
static inline VersionWrapper<nite::Version> getVersion()
{
return nite::NiTE::getVersion();
}
};
//-----------------------------------------------------------------------------------------------
template<typename T>
static void extract_point3_from_py_obj(const bp::object & obj, T & x, T & y, T & z)
{
T new_x, new_y, new_z;
try
{
new_x = bp::extract<T>(obj[0]);
new_y = bp::extract<T>(obj[1]);
new_z = bp::extract<T>(obj[2]);
}
catch(...)
{
try
{
new_x = bp::extract<T>(obj.attr("x"));
new_y = bp::extract<T>(obj.attr("y"));
new_z = bp::extract<T>(obj.attr("z"));
}
catch(...)
{
throw std::runtime_error("cannot extract point3 from python object");
}
}
x = new_x;
y = new_y;
z = new_z;
}
template<typename T>
static void extract_point4_from_py_obj(const bp::object & obj, T & w, T & x, T & y, T & z)
{
T new_w, new_x, new_y, new_z;
try
{
new_w = bp::extract<T>(obj[0]);
new_x = bp::extract<T>(obj[1]);
new_y = bp::extract<T>(obj[2]);
new_z = bp::extract<T>(obj[3]);
}
catch(...)
{
try
{
new_w = bp::extract<T>(obj.attr("w"));
new_x = bp::extract<T>(obj.attr("x"));
new_y = bp::extract<T>(obj.attr("y"));
new_z = bp::extract<T>(obj.attr("z"));
}
catch(...)
{
throw std::runtime_error("cannot extract point4 from python object");
}
}
w = new_w;
x = new_x;
y = new_y;
z = new_z;
}
//-----------------------------------------------------------------------------------------------
class Point3fWrapper : private nite::Point3f
{
public:
inline Point3fWrapper()
: nite::Point3f()
{
}
inline Point3fWrapper(const NitePoint3f & p)
: nite::Point3f(p.x, p.y, p.z)
{
}
inline Point3fWrapper(const Point3fWrapper & p)
: nite::Point3f(p.x, p.y, p.z)
{
}
inline Point3fWrapper(float x, float y, float z)
: nite::Point3f(x, y, z)
{
}
inline Point3fWrapper(const bp::object & obj)
{
try
{
*this = bp::extract<Point3fWrapper>(obj);
}
catch(...)
{
try
{
extract_point3_from_py_obj(obj, x, y, z);
}
catch(...)
{
throw std::runtime_error("cannot create Point3fWrapper from python object");
}
}
}
inline float getX() const { return x; }
inline float getY() const { return y; }
inline float getZ() const { return z; }
inline void setX(float x_) { x = x_; }
inline void setY(float y_) { y = y_; }
inline void setZ(float z_) { z = z_; }
inline void set(float x_, float y_, float z_)
{
x = x_;
y = y_;
z = z_;
}
inline int getTupleLength() const
{
return 3;
}
inline float getTupleElem(int n) const
{
switch(n)
{
case 0: return x;
case 1: return y;
case 2: return z;
default:
// translated by Boost.Python to IndexError (not RangeError)
throw std::out_of_range("Point contains only 3 elements");
}
}
inline void setTupleElem(int n, float val)
{
switch(n)
{
case 0: x = val; break;
case 1: y = val; break;
case 2: z = val; break;
default:
// translated by Boost.Python to IndexError (not RangeError)
throw std::out_of_range("Point contains only 3 elements");
}
}
inline const nite::Point3f & getPoint3fConstRef()
{
return static_cast<const nite::Point3f &>(*this);
}
};
inline std::ostream & operator << (std::ostream & output, const Point3fWrapper & p)
{
return output << "(x = " << p.getX() << ", y = " << p.getY() << ", z = " << p.getZ() << ')';
}
//-----------------------------------------------------------------------------------------------
class QuaternionWrapper : private nite::Quaternion
{
public:
inline QuaternionWrapper()
: nite::Quaternion()
{
}
inline QuaternionWrapper(const NiteQuaternion & p)
: nite::Quaternion(p.w, p.x, p.y, p.z)
{
}
inline QuaternionWrapper(const QuaternionWrapper & p)
: nite::Quaternion(p.w, p.x, p.y, p.z)
{
}
inline QuaternionWrapper(float w, float x, float y, float z)
: nite::Quaternion(w, x, y, z)
{
}
inline QuaternionWrapper(const bp::object & obj)
{
try
{
*this = bp::extract<QuaternionWrapper>(obj);
}
catch(...)
{
try
{
extract_point4_from_py_obj(obj, w, x, y, z);
}
catch(...)
{
throw std::runtime_error("cannot create QuaternionWrapper from python object");
}
}
}
inline float getW() const { return w; }
inline float getX() const { return x; }
inline float getY() const { return y; }
inline float getZ() const { return z; }
inline void setW(float w_) { z = w_; }
inline void setX(float x_) { x = x_; }
inline void setY(float y_) { y = y_; }
inline void setZ(float z_) { z = z_; }
inline void set(float w_, float x_, float y_, float z_)
{
w = w_;
x = x_;
y = y_;
z = z_;
}
inline int getTupleLength() const
{
return 4;
}
inline float getTupleElem(int n) const
{
switch(n)
{
case 0: return w;
case 1: return x;
case 2: return y;
case 3: return z;
default:
// translated to by boost::python to IndexError
throw std::out_of_range("Quaternion contains only 4 elements");
}
}
inline void setTupleElem(int n, float val)
{
switch(n)
{
case 0: w = val; break;
case 1: x = val; break;
case 2: y = val; break;
case 3: z = val; break;
default:
// translated to by boost::python to IndexError
throw std::out_of_range("Quaternion contains only 4 elements");
}
}
};
inline std::ostream & operator << (std::ostream & output, const QuaternionWrapper & p)
{
return output << "(w = " << p.getW() << ", x = " << p.getX() << ", y = " << p.getY() << ", z = " << p.getZ() << ')';
}
//-----------------------------------------------------------------------------------------------
class BoundingBoxWrapper
{
public:
inline BoundingBoxWrapper(const nite::BoundingBox & b)
: m_min(b.min)
, m_max(b.max)
{
}
inline const Point3fWrapper getMin() const
{
return m_min;
}
inline const Point3fWrapper getMax() const
{
return m_max;
}
private:
const Point3fWrapper m_min;
const Point3fWrapper m_max;
};
//-----------------------------------------------------------------------------------------------
class SkeletonJointWrapper : private nite::SkeletonJoint
{
public:
inline SkeletonJointWrapper(const nite::SkeletonJoint & j)
: nite::SkeletonJoint(j)
{
}
inline nite::JointType getType() const
{
return nite::SkeletonJoint::getType();
}
inline Point3fWrapper getPosition() const
{
return Point3fWrapper(nite::SkeletonJoint::getPosition());
}
inline float getPositionConfidence() const
{
return nite::SkeletonJoint::getPositionConfidence();
}
inline QuaternionWrapper getOrientation() const
{
return QuaternionWrapper(nite::SkeletonJoint::getOrientation());
}
inline float getOrientationConfidence() const
{
return nite::SkeletonJoint::getOrientationConfidence();
}
};
//-----------------------------------------------------------------------------------------------
class PlaneWrapper
{
public:
inline PlaneWrapper(const nite::Plane & p = nite::Plane())
: m_point(p.point)
, m_normal(p.normal)
{
}
inline PlaneWrapper & operator = (const nite::Plane & p)
{
m_point = p.point;
m_normal = p.normal;
return *this;
}
inline const Point3fWrapper getPoint() const
{
return m_point;
}
inline const Point3fWrapper getNormal() const
{
return m_normal;
}
private:
Point3fWrapper m_point;
Point3fWrapper m_normal;
};
//-----------------------------------------------------------------------------------------------
class SkeletonWrapper : private nite::Skeleton
{
public:
inline SkeletonWrapper(const nite::Skeleton & s)
: nite::Skeleton(s)
{
}
inline const SkeletonJointWrapper getJoint(nite::JointType type) const
{
return SkeletonJointWrapper(nite::Skeleton::getJoint(type));
}
inline nite::SkeletonState getState() const
{
return nite::Skeleton::getState();
}
};
//-----------------------------------------------------------------------------------------------
class UserDataWrapper : private nite::UserData
{
public:
inline UserDataWrapper(const nite::UserData & ud)
: nite::UserData(ud)
{
}
inline nite::UserId getId() const
{
return nite::UserData::getId();
}
inline const BoundingBoxWrapper getBoundingBox() const
{
return BoundingBoxWrapper(nite::UserData::getBoundingBox());
}
inline const Point3fWrapper getCenterOfMass() const
{
return Point3fWrapper(nite::UserData::getCenterOfMass());
}
inline bool isNew() const
{
return nite::UserData::isNew();
}
inline bool isVisible() const
{
return nite::UserData::isVisible();
}
inline bool isLost() const
{
return nite::UserData::isLost();
}
inline const SkeletonWrapper getSkeleton() const
{
return SkeletonWrapper(nite::UserData::getSkeleton());
}
inline const nite::PoseData getPose(nite::PoseType type) const
{
return nite::PoseData(nite::UserData::getPose(type));
}
};
//-----------------------------------------------------------------------------------------------
class UserTrackerFrameWrapper
{
public:
inline UserTrackerFrameWrapper(const nite::UserTrackerFrameRef & f)
{
m_isValid = f.isValid();
if(m_isValid)
{
m_floorConfidence = f.getFloorConfidence();
m_floor = f.getFloor();
m_timestamp = f.getTimestamp();
m_frameIndex = f.getFrameIndex();
const nite::Array<nite::UserData> & users = f.getUsers();
const int n_users = users.getSize();
m_user_ids.resize(n_users);
for(int i = 0; i < n_users; i++)
{
const nite::UserData & user_data = users[i];
m_users.append(UserDataWrapper(user_data));
m_user_ids[i] = user_data.getId();
}
const nite::UserMap & m = f.getUserMap();
const nite::UserId * pixels = m.getPixels();
if(pixels)
{
if(m.getStride() != int(sizeof(nite::UserId)) * m.getWidth())
throw std::runtime_error("data format where stride != sizeof(UserId)*width is not currently supported");
// size of array is equal to sizeof(UserId) * height * stride
npy_intp dims[2] = { m.getHeight(), m.getWidth() };
PyObject * py_obj = PyArray_SimpleNewFromData(2, dims, NPY_SHORT, (void*)pixels);
bp::handle<> handle(py_obj);
bp::numeric::array arr(handle);
// The problem of returning arr is twofold: firstly the user can modify
// the data which will betray the const-correctness
// Secondly the lifetime of the data is managed by the C++ API and not the lifetime
// of the numpy array whatsoever. But we have a simply solution..
m_userMap = arr.copy(); // copy the object. numpy owns the copy now.
}
// TODO...
//m_depthFrame = VideoFrameWrapper(f.getDepthFrame());
}
else
{
m_floorConfidence = 0.0;
m_timestamp = 0;
m_frameIndex = -1;
}
}
inline bool isValid() const
{
return m_isValid;
}
inline bp::object getUserById(nite::UserId id) const
{
for(unsigned int i = 0; i < m_user_ids.size(); i++)
if(m_user_ids[i] == id)
return m_users[i];
return bp::object();
}
inline const bp::list getUsers() const
{
return m_users;
}
inline float getFloorConfidence() const
{
return m_floorConfidence;
}
inline const PlaneWrapper getFloor() const
{
return m_floor;
}
inline const VideoFrameWrapper getDepthFrame()
{
return m_depthFrame;
}
const bp::object getUserMap() const
{
return m_userMap;
}
inline uint64_t getTimestamp() const
{
return m_timestamp;
}
inline int getFrameIndex() const
{
return m_frameIndex;
}
private:
bool m_isValid;
float m_floorConfidence;
PlaneWrapper m_floor;
bp::list m_users;
std::vector<nite::UserId> m_user_ids;
bp::object m_userMap;
VideoFrameWrapper m_depthFrame;
uint64_t m_timestamp;
int m_frameIndex;
};
//-----------------------------------------------------------------------------------------------
class HandDataWrapper
{
public:
inline HandDataWrapper(const nite::HandData & d)
: m_id(d.getId())
, m_position(d.getPosition())
, m_flags(d.isNew(), d.isLost(), d.isTracking(), d.isTouchingFov())
{}
inline nite::HandId getId() const
{
return m_id;
}
inline const Point3fWrapper getPosition() const
{
return m_position;
}
inline bool isNew() const
{
return m_flags.isNew;
}
inline bool isLost() const
{
return m_flags.isLost;
}
inline bool isTracking() const
{
return m_flags.isTracking;
}
inline bool isTouchingFov() const
{
return m_flags.isTouchingFov;
}
private:
const nite::HandId m_id;
const Point3fWrapper m_position;
const struct Flags
{
inline Flags(bool isNew_, bool isLost_, bool isTracking_, bool isTouchingFov_)
: isNew(isNew_)
, isLost(isLost_)
, isTracking(isTracking_)
, isTouchingFov(isTouchingFov_)
{}
unsigned char isNew : 1;
unsigned char isLost : 1;
unsigned char isTracking : 1;
unsigned char isTouchingFov : 1;
} m_flags;
};
//-----------------------------------------------------------------------------------------------
class GestureDataWrapper
{
public:
inline GestureDataWrapper(const nite::GestureData & d)
: m_type(d.getType())
, m_currentPosition(d.getCurrentPosition())
, m_flags(d.isComplete(), d.isInProgress())
{
}
inline nite::GestureType getType() const
{
return m_type;
}
inline const Point3fWrapper getCurrentPosition() const
{
return m_currentPosition;
}
inline bool isComplete() const
{
return m_flags.isComplete;
}
inline bool isInProgress() const
{
return m_flags.isInProgress;
}
private:
const nite::GestureType m_type;
const Point3fWrapper m_currentPosition;
const struct Flags
{
inline Flags(bool isComplete_, bool isInProgress_)
: isComplete(isComplete_)
, isInProgress(isInProgress_)
{}
unsigned char isComplete : 1;
unsigned char isInProgress : 1;
} m_flags;
};
//-----------------------------------------------------------------------------------------------
class HandTrackerFrameWrapper
{
public:
inline HandTrackerFrameWrapper(const nite::HandTrackerFrameRef & f)
{
m_isValid = f.isValid();
if(m_isValid)
{
m_timetamp = f.getTimestamp();
m_frameIndex = f.getFrameIndex();
const nite::Array<nite::HandData> & hd = f.getHands();
for(int i = 0; i < hd.getSize(); i++)
m_hands.append(HandDataWrapper(hd[i]));
const nite::Array<nite::GestureData> & gd = f.getGestures();
for(int i = 0; i < gd.getSize(); i++)
m_gestures.append(GestureDataWrapper(gd[i]));
m_depthFrame = VideoFrameWrapper(f.getDepthFrame());
}
else
{
m_timetamp = 0;
m_frameIndex = -1;
}
}
inline bool isValid() const
{
return m_isValid;
}
inline const bp::list getHands() const
{
return m_hands;
}
inline const bp::list getGestures() const
{
return m_gestures;
}
inline VideoFrameWrapper getDepthFrame() const
{
return m_depthFrame;
}
inline uint64_t getTimestamp() const
{
return m_timetamp;
}
inline int getFrameIndex() const
{
return m_frameIndex;
}
private:
bool m_isValid;
bp::list m_hands;
bp::list m_gestures;
VideoFrameWrapper m_depthFrame;
uint64_t m_timetamp;
int m_frameIndex;
};
//-----------------------------------------------------------------------------------------------
class UserTrackerWrapper : private nite::UserTracker,
private nite::UserTracker::NewFrameListener
{
public:
inline UserTrackerWrapper()
{
}
inline ~UserTrackerWrapper()
{
if(!m_frameListeners.empty())
nite::UserTracker::removeNewFrameListener(this);
}
inline bool isValid() const
{
return nite::UserTracker::isValid();
}
inline nite::Status create(const bp::object & device = bp::object())
{
if(device.is_none())
{
ScopedGILRelease release_gil;
return nite::UserTracker::create();
}
else
{
DeviceWrapper & dev = bp::extract<DeviceWrapper&>(device);
ScopedGILRelease release_gil;
return nite::UserTracker::create(dev.getDevicePointer());
}
}
inline void destroy()
{
ScopedGILRelease release_gil;
nite::UserTracker::destroy();
}
inline bp::tuple readFrame()
{
nite::Status status;
nite::UserTrackerFrameRef frameRef;
{
ScopedGILRelease release_gil;
status = nite::UserTracker::readFrame(&frameRef);
}
if(status != nite::STATUS_OK)
return bp::make_tuple(status, bp::object());
else
return bp::make_tuple(status, UserTrackerFrameWrapper(frameRef));
}
inline nite::Status setSkeletonSmoothingFactor(float factor)
{
return nite::UserTracker::setSkeletonSmoothingFactor(factor);
}
inline float getSkeletonSmoothingFactor() const
{
return nite::UserTracker::getSkeletonSmoothingFactor();
}
inline nite::Status startSkeletonTracking(nite::UserId id)
{
return nite::UserTracker::startSkeletonTracking(id);
}
inline void stopSkeletonTracking(nite::UserId id)
{
nite::UserTracker::stopSkeletonTracking(id);
}
inline nite::Status startPoseDetection(nite::UserId user, nite::PoseType type)
{
return nite::UserTracker::startPoseDetection(user, type);
}
inline void stopPoseDetection(nite::UserId user, nite::PoseType type)
{
nite::UserTracker::stopPoseDetection(user, type);
}
inline bp::tuple convertJointCoordinatesToDepth(float x, float y, float z) const
{
float outX, outY;
nite::Status status = nite::UserTracker::convertJointCoordinatesToDepth(x, y, z, &outX, &outY);
return bp::make_tuple(status, outX, outY);
}
inline bp::tuple convertJointCoordinatesToDepth_obj(const bp::object & obj) const
{
float x, y, z;
float outX, outY;
extract_point3_from_py_obj(obj, x, y, z);
nite::Status status = nite::UserTracker::convertJointCoordinatesToDepth(x, y, z, &outX, &outY);
return bp::make_tuple(status, outX, outY);
}
inline bp::tuple convertDepthCoordinatesToJoint(int x, int y, int z) const
{
float outX, outY;
nite::Status status = nite::UserTracker::convertDepthCoordinatesToJoint(x, y, z, &outX, &outY);
return bp::make_tuple(status, outX, outY);
}
inline bp::tuple convertDepthCoordinatesToJoint_obj(const bp::object & obj) const
{
int x, y, z;
float outX, outY;
extract_point3_from_py_obj(obj, x, y, z);
nite::Status status = nite::UserTracker::convertDepthCoordinatesToJoint(x, y, z, &outX, &outY);
return bp::make_tuple(status, outX, outY);
}
inline void addNewFrameListener(const bp::object & new_frame_callback)
{
if(addListener(m_frameListeners, new_frame_callback))
nite::UserTracker::addNewFrameListener(this);
}
inline void removeNewFrameListener(const bp::object & new_frame_callback)
{
if(removeListener(m_frameListeners, new_frame_callback))
nite::UserTracker::removeNewFrameListener(this);
}
private:
virtual void onNewFrame(nite::UserTracker &) override
{
GILStateScopedEnsure gilEnsure;
for(auto & obj : m_frameListeners)
{
try
{
obj();
}
catch(...)
{
}
}
}
std::vector<bp::object> m_frameListeners;
};
//-----------------------------------------------------------------------------------------------
class HandTrackerWrapper : private nite::HandTracker,
private nite::HandTracker::NewFrameListener
{
public:
inline HandTrackerWrapper()
{
}
inline ~HandTrackerWrapper()
{
if(!m_frameListeners.empty())
nite::HandTracker::removeNewFrameListener(this);
}
inline bool isValid() const
{
return nite::HandTracker::isValid();
}
inline nite::Status create(const bp::object & device = bp::object())
{
if(device.is_none())
{
ScopedGILRelease release_gil;
return nite::HandTracker::create();
}
else
{
DeviceWrapper & dev = bp::extract<DeviceWrapper&>(device);
ScopedGILRelease release_gil;
return nite::HandTracker::create(dev.getDevicePointer());
}
}
inline void destroy()
{
ScopedGILRelease release_gil;
nite::HandTracker::destroy();
}
inline bp::tuple readFrame()
{
nite::Status status;
nite::HandTrackerFrameRef frame;
{
ScopedGILRelease release_gil;
status = nite::HandTracker::readFrame(&frame);
}
if(status != nite::STATUS_OK)
return bp::make_tuple(status, bp::object());
else
return bp::make_tuple(status, HandTrackerFrameWrapper(frame));
}
inline nite::Status setSmoothingFactor(float factor)
{
return nite::HandTracker::setSmoothingFactor(factor);
}
inline float getSmoothingFactor() const
{
return nite::HandTracker::getSmoothingFactor();
}
inline bp::tuple startHandTracking(const bp::object & position)
{
nite::HandId newHandId;
nite::Status status = nite::HandTracker::startHandTracking(Point3fWrapper(position).getPoint3fConstRef(), &newHandId);
if(status != nite::STATUS_OK)
return bp::make_tuple(status, bp::object());
else
return bp::make_tuple(status, newHandId);
}
inline void stopHandTracking(nite::HandId id)
{