-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensorDevice.cpp
More file actions
191 lines (154 loc) · 6.17 KB
/
SensorDevice.cpp
File metadata and controls
191 lines (154 loc) · 6.17 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
#include "SensorDevice.h"
#include "callbacks.h"
#include "SkeletonMath.h"
#include <string>
// Checks to make sure status is good, or prints error and returns error code
inline int CHECK_RC(const unsigned int rc, const char* const description)
{
if (rc != XN_STATUS_OK)
{
printf("%s failed: %s\n", description, xnGetStatusString(rc));
return -1;
}
}
SensorDevice::SensorDevice() : context_(),
userG_(),
pointModeProjective_(TRUE),
needCalibrationPose_(FALSE),
trackedUsers_(),
pose_("Psi"),
loadCalibration_(FALSE),
saveCalibration_(FALSE),
calibrationFilename_("skel.bin"),
smoothingFactor_(0.8)
{}
SensorDevice::~SensorDevice()
{
context_.Shutdown();
}
int SensorDevice::initialize()
{
context_.Init();
XnStatus rc = XN_STATUS_OK;
XnMapOutputMode mapMode;
// create depth and user generators
rc = depthG_.Create(context_);
CHECK_RC(rc, "Create depth generator");
rc = userG_.Create(context_);
CHECK_RC(rc, "Create user generator");
depthG_.GetMapOutputMode(mapMode);
// for now, make output map the native res of Kinect sensor
mapMode.nXRes = XN_VGA_X_RES;
mapMode.nYRes = XN_VGA_Y_RES;
mapMode.nFPS = 30;
depthG_.SetMapOutputMode(mapMode);
// turn on device mirroring
if(TRUE == depthG_.IsCapabilitySupported("Mirror"))
{
//printf("Image mirroring is supported by device.\n");
rc = depthG_.GetMirrorCap().SetMirror(TRUE);
CHECK_RC(rc, "Setting Image Mirroring on depthG");
}
// make sure the user points are reported from the POV of the depth generator
userG_.GetAlternativeViewPointCap().SetViewPoint(depthG_);
userG_.GetSkeletonCap().SetSmoothing(smoothingFactor_);
return 0;
}
// converts the OpenNI positions to simple 3D points
void SensorDevice::convertXnJointToPoint(XnSkeletonJointPosition* const joints, Point* const points, unsigned int numPoints)
{
XnPoint3D xpt;
for(int i = 0; i < numPoints; i++)
{
xpt = joints[i].position;
if(pointModeProjective_)
depthG_.ConvertRealWorldToProjective(1, &xpt, &xpt);
points[i].confidence_ = joints[i].fConfidence;
points[i].x_ = xpt.X;
points[i].y_ = xpt.Y;
points[i].z_ = xpt.Z;
}
}
void SensorDevice::getHandPoints(const unsigned int i, Point* const hands)
{
XnSkeletonJointPosition joints[2];
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_LEFT_HAND, joints[0]);
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_RIGHT_HAND, joints[1]);
convertXnJointToPoint(joints, hands, 2);
}
void SensorDevice::getElbowPoints(const unsigned int i, Point* const elbows)
{
XnSkeletonJointPosition joints[2];
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_LEFT_ELBOW, joints[0]);
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_RIGHT_ELBOW, joints[1]);
convertXnJointToPoint(joints, elbows, 2);
}
void SensorDevice::getArmPoints(const unsigned int i, Point* const arms)
{
getHandPoints(i, arms);
getElbowPoints(i, arms+2);
XnSkeletonJointPosition joints[2];
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_LEFT_ELBOW, joints[4]);
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_RIGHT_ELBOW, joints[5]);
convertXnJointToPoint(joints, arms, 2);
}
void SensorDevice::getShoulderPoints(const unsigned int i, Point* const shoulders)
{
XnSkeletonJointPosition joints[2];
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_LEFT_SHOULDER, joints[0]);
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_RIGHT_SHOULDER, joints[1]);
convertXnJointToPoint(joints, shoulders, 2);
}
void SensorDevice::getHeadPoint(const unsigned int i, Point* const head)
{
XnSkeletonJointPosition joints;
userG_.GetSkeletonCap().GetSkeletonJointPosition(trackedUsers_[i], XN_SKEL_HEAD, joints);
convertXnJointToPoint(&joints, head, 1);
}
bool SensorDevice::isTracking()
{
XnUserID users[64];
XnUInt16 nUsers = userG_.GetNumberOfUsers();
trackedUsers_.clear();
userG_.GetUsers(users, nUsers);
for(int i = 0; i < nUsers; i++)
{
if(userG_.GetSkeletonCap().IsTracking(users[i]))
{
trackedUsers_.push_back(users[i]);
}
}
if (!trackedUsers_.empty())
return TRUE;
else
return FALSE;
}
// set device to look for calibration pose and supply callback functions for user events
int SensorDevice::lookForCalibrationPoseOn()
{
XnCallbackHandle hUserCallbacks, hCalibrationStart, hCalibrationComplete, hPoseDetected, hCalibrationInProgress, hPoseInProgress;
XnStatus rc = XN_STATUS_OK;
userG_.RegisterUserCallbacks(User_NewUser, User_LostUser, this, hUserCallbacks);
userG_.GetSkeletonCap().RegisterToCalibrationStart(UserCalibration_CalibrationStart, this, hCalibrationStart);
userG_.GetSkeletonCap().RegisterToCalibrationComplete(UserCalibration_CalibrationComplete, this, hCalibrationComplete);
if (needCalibrationPose_)
{
if (!userG_.IsCapabilitySupported(XN_CAPABILITY_POSE_DETECTION))
{
printf("Pose required, but not supported by device\n");
return -1;
}
rc = userG_.GetPoseDetectionCap().RegisterToPoseDetected(UserPose_PoseDetected, this, hPoseDetected);
CHECK_RC(rc, "Register to Pose Detected");
userG_.GetSkeletonCap().GetCalibrationPose((XnChar*) pose_.c_str());
}
// turn on tracking of all joints
userG_.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
}
// print the number of poses that the connected device supports
// Kinect: 1 Pose: "Psi"
void SensorDevice::printAvailablePoses()
{
XnUInt32 numPoses = userG_.GetPoseDetectionCap().GetNumberOfPoses();
printf("Number of poses: %d.\n", numPoses);
}