| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "hwdrivers-precomp.h" // Precompiled headers |
| 11 |
// |
| 12 |
|
| 13 |
#include <mrpt/hwdrivers/CSkeletonTracker.h> |
| 14 |
|
| 15 |
// opengl includes |
| 16 |
#include <mrpt/opengl/CBox.h> |
| 17 |
#include <mrpt/opengl/CCylinder.h> |
| 18 |
#include <mrpt/opengl/CGridPlaneXZ.h> |
| 19 |
#include <mrpt/opengl/CSetOfLines.h> |
| 20 |
#include <mrpt/opengl/CSimpleLine.h> |
| 21 |
#include <mrpt/opengl/CSphere.h> |
| 22 |
#include <mrpt/opengl/CText.h> |
| 23 |
#include <mrpt/opengl/CTexturedPlane.h> |
| 24 |
#include <mrpt/opengl/stock_objects.h> |
| 25 |
#include <mrpt/poses/CPoint3D.h> |
| 26 |
|
| 27 |
#include <chrono> |
| 28 |
#include <thread> |
| 29 |
|
| 30 |
IMPLEMENTS_GENERIC_SENSOR(CSkeletonTracker, mrpt::hwdrivers) |
| 31 |
|
| 32 |
using namespace mrpt::hwdrivers; |
| 33 |
using namespace mrpt::poses; |
| 34 |
using namespace mrpt::obs; |
| 35 |
using namespace mrpt::img; |
| 36 |
using namespace std; |
| 37 |
|
| 38 |
#define skl_states (static_cast<nite::SkeletonState*>(m_skeletons_ptr)) |
| 39 |
#define user_tracker (static_cast<nite::UserTracker*>(m_userTracker_ptr)) |
| 40 |
#define MAX_USERS 10 |
| 41 |
#define FILL_JOINT_DATA(_J1, _J2) \ |
| 42 |
obs->_J1.x = user.getSkeleton().getJoint(_J2).getPosition().x; \ |
| 43 |
obs->_J1.y = user.getSkeleton().getJoint(_J2).getPosition().y; \ |
| 44 |
obs->_J1.z = user.getSkeleton().getJoint(_J2).getPosition().z; \ |
| 45 |
obs->_J1.conf = user.getSkeleton().getJoint(_J2).getPositionConfidence(); |
| 46 |
|
| 47 |
#if MRPT_HAS_NITE2 |
| 48 |
#include "NiTE.h" |
| 49 |
#pragma comment(lib, "NiTE2.lib") |
| 50 |
#endif |
| 51 |
|
| 52 |
string jointNames[] = {"head", "neck", "torso", "left_shoulder", "left_elbow", |
| 53 |
"left_hand", "left_hip", "left_knee", "left_foot", "right_shoulder", |
| 54 |
"right_elbow", "right_hand", "right_hip", "right_knee", "right_foot"}; |
| 55 |
|
| 56 |
/*------------------------------------------------------------- |
| 57 |
CSkeletonTracker |
| 58 |
-------------------------------------------------------------*/ |
| 59 |
CSkeletonTracker::CSkeletonTracker() : m_timeStartTT(), m_sensorPose() |
| 60 |
|
| 61 |
{ |
| 62 |
m_sensorLabel = "skeletonTracker"; |
| 63 |
|
| 64 |
#if MRPT_HAS_OPENNI2 && MRPT_HAS_NITE2 |
| 65 |
m_skeletons_ptr = new nite::SkeletonState[MAX_USERS]; |
| 66 |
m_userTracker_ptr = new nite::UserTracker; |
| 67 |
for (int i = 0; i < MAX_USERS; ++i) skl_states[i] = nite::SKELETON_NONE; |
| 68 |
|
| 69 |
m_linesToPlot.resize(NUM_LINES); |
| 70 |
m_joint_theta.resize(NUM_JOINTS); |
| 71 |
for (int i = 1; i < NUM_JOINTS; ++i) m_joint_theta[i] = (i - 1) * (M_2PI / (NUM_JOINTS - 1)); |
| 72 |
#else |
| 73 |
THROW_EXCEPTION( |
| 74 |
"MRPT has been compiled with 'BUILD_OPENNI2'=OFF or 'BUILD_NITE2'=OFF, " |
| 75 |
"so this class cannot be used."); |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
/*------------------------------------------------------------- |
| 80 |
~CSkeletonTracker |
| 81 |
-------------------------------------------------------------*/ |
| 82 |
CSkeletonTracker::~CSkeletonTracker() |
| 83 |
{ |
| 84 |
#if MRPT_HAS_OPENNI2 && MRPT_HAS_NITE2 |
| 85 |
nite::NiTE::shutdown(); // close tracker |
| 86 |
delete[] skl_states; |
| 87 |
m_skeletons_ptr = nullptr; |
| 88 |
delete user_tracker; |
| 89 |
m_userTracker_ptr = nullptr; |
| 90 |
#endif |
| 91 |
if (m_win) m_win.reset(); |
| 92 |
} |
| 93 |
|
| 94 |
/*------------------------------------------------------------- |
| 95 |
processPreviewNone |
| 96 |
-------------------------------------------------------------*/ |
| 97 |
void CSkeletonTracker::processPreviewNone() |
| 98 |
{ |
| 99 |
using namespace mrpt::opengl; |
| 100 |
|
| 101 |
// show skeleton data |
| 102 |
if (m_showPreview) |
| 103 |
{ |
| 104 |
if (!m_win) |
| 105 |
{ |
| 106 |
string caption = string("Preview of ") + m_sensorLabel; |
| 107 |
m_win = mrpt::gui::CDisplayWindow3D::Create(caption, 800, 600); |
| 108 |
|
| 109 |
Scene::Ptr& scene = m_win->get3DSceneAndLock(); |
| 110 |
scene->insert(std::make_shared<CGridPlaneXZ>(-3, 3, 0, 5, -1.5)); |
| 111 |
|
| 112 |
// set camera parameters |
| 113 |
m_win->setCameraElevationDeg(-90); |
| 114 |
m_win->setCameraAzimuthDeg(90); |
| 115 |
m_win->setCameraZoom(4); |
| 116 |
m_win->setCameraPointingToPoint(0, 0, 0); |
| 117 |
|
| 118 |
// insert initial body |
| 119 |
CSetOfObjects::Ptr body = std::make_shared<CSetOfObjects>(); |
| 120 |
body->setName("body"); |
| 121 |
for (const auto& jointName : jointNames) |
| 122 |
{ |
| 123 |
CSphere::Ptr sph = std::make_shared<CSphere>(0.03f); |
| 124 |
sph->setColor(0, 1, 0); |
| 125 |
sph->setName(jointName); |
| 126 |
body->insert(sph); |
| 127 |
} |
| 128 |
|
| 129 |
// insert initial lines |
| 130 |
CSetOfLines::Ptr lines = std::make_shared<CSetOfLines>(); |
| 131 |
lines->setName("lines"); |
| 132 |
lines->setColor(0, 0, 1); |
| 133 |
body->insert(lines); |
| 134 |
|
| 135 |
scene->insert(body); |
| 136 |
m_win->unlockAccess3DScene(); |
| 137 |
} |
| 138 |
|
| 139 |
if (m_win && m_win->isOpen()) |
| 140 |
{ |
| 141 |
Scene::Ptr& scene = m_win->get3DSceneAndLock(); |
| 142 |
{ |
| 143 |
m_win->addTextMessage(0.35, 0.9, "Please, adopt this position"); |
| 144 |
|
| 145 |
// insert translucid dummy and help text (it will go away when |
| 146 |
// measurements are taken) |
| 147 |
if (!scene->getByName("dummy")) |
| 148 |
{ |
| 149 |
const double SCALE = 0.8; |
| 150 |
const double BODY_RADIUS = 0.22 * SCALE; |
| 151 |
const double BODY_LENGTH = 0.8 * SCALE; |
| 152 |
const double ARM_RADIUS = 0.05 * SCALE; |
| 153 |
const double ARM_LENGTH = 0.4 * SCALE; |
| 154 |
const double LEG_RADIUS = 0.1 * SCALE; |
| 155 |
const double LEG_LENGTH = 0.8 * SCALE; |
| 156 |
const double HEAD_RADIUS = 0.15 * SCALE; |
| 157 |
const double ALPHA_CH = 0.8; |
| 158 |
|
| 159 |
CSetOfObjects::Ptr dummy = std::make_shared<CSetOfObjects>(); |
| 160 |
dummy->setName("dummy"); |
| 161 |
dummy->setPose(math::TPose3D(0, 0, 0, 0, 0, -90.0_deg)); |
| 162 |
{ |
| 163 |
// head |
| 164 |
CSphere::Ptr part = std::make_shared<CSphere>(HEAD_RADIUS); |
| 165 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 166 |
part->setPose(math::TPose3D(0, 0, 0.5 * BODY_LENGTH + HEAD_RADIUS, 0, 0, 0)); |
| 167 |
dummy->insert(part); |
| 168 |
} |
| 169 |
{ |
| 170 |
// body |
| 171 |
CCylinder::Ptr part = |
| 172 |
std::make_shared<CCylinder>(BODY_RADIUS, BODY_RADIUS, BODY_LENGTH); |
| 173 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 174 |
part->setPose(math::TPose3D(0, 0, -BODY_LENGTH / 2, 0, 0, 0)); |
| 175 |
dummy->insert(part); |
| 176 |
} |
| 177 |
{ |
| 178 |
// left arm 0 |
| 179 |
CCylinder::Ptr part = std::make_shared<CCylinder>(ARM_RADIUS, ARM_RADIUS, ARM_LENGTH); |
| 180 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 181 |
part->setPose( |
| 182 |
math::TPose3D(-BODY_RADIUS, 0, 0.5 * BODY_LENGTH - ARM_RADIUS, 0, -90.0_deg, 0)); |
| 183 |
dummy->insert(part); |
| 184 |
} |
| 185 |
{ |
| 186 |
// left arm 1 |
| 187 |
CCylinder::Ptr part = std::make_shared<CCylinder>(ARM_RADIUS, ARM_RADIUS, ARM_LENGTH); |
| 188 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 189 |
part->setPose(math::TPose3D( |
| 190 |
-BODY_RADIUS - ARM_LENGTH + ARM_RADIUS, 0, 0.5 * BODY_LENGTH - ARM_RADIUS, 0, 0, |
| 191 |
0)); |
| 192 |
dummy->insert(part); |
| 193 |
} |
| 194 |
{ |
| 195 |
// right arm 0 |
| 196 |
CCylinder::Ptr part = std::make_shared<CCylinder>(ARM_RADIUS, ARM_RADIUS, ARM_LENGTH); |
| 197 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 198 |
part->setPose( |
| 199 |
math::TPose3D(BODY_RADIUS, 0, 0.5 * BODY_LENGTH - ARM_RADIUS, 0, 90.0_deg, 0)); |
| 200 |
dummy->insert(part); |
| 201 |
} |
| 202 |
{ |
| 203 |
// right arm 1 |
| 204 |
CCylinder::Ptr part = std::make_shared<CCylinder>(ARM_RADIUS, ARM_RADIUS, ARM_LENGTH); |
| 205 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 206 |
part->setPose(math::TPose3D( |
| 207 |
BODY_RADIUS + ARM_LENGTH - ARM_RADIUS, 0, 0.5 * BODY_LENGTH - ARM_RADIUS, 0, 0, 0)); |
| 208 |
dummy->insert(part); |
| 209 |
} |
| 210 |
{ |
| 211 |
// left leg |
| 212 |
CCylinder::Ptr part = std::make_shared<CCylinder>(LEG_RADIUS, LEG_RADIUS, LEG_LENGTH); |
| 213 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 214 |
part->setPose(math::TPose3D( |
| 215 |
-BODY_RADIUS + LEG_RADIUS, 0, -(0.5 * BODY_LENGTH + LEG_LENGTH), 0, 0, 0)); |
| 216 |
dummy->insert(part); |
| 217 |
} |
| 218 |
{ |
| 219 |
// right leg |
| 220 |
CCylinder::Ptr part = std::make_shared<CCylinder>(LEG_RADIUS, LEG_RADIUS, LEG_LENGTH); |
| 221 |
part->setColor(1, 1, 1, ALPHA_CH); |
| 222 |
part->setPose(math::TPose3D( |
| 223 |
BODY_RADIUS - LEG_RADIUS, 0, -(0.5 * BODY_LENGTH + LEG_LENGTH), 0, 0, 0)); |
| 224 |
dummy->insert(part); |
| 225 |
} |
| 226 |
scene->insert(dummy); |
| 227 |
} // end-if |
| 228 |
else |
| 229 |
{ |
| 230 |
CSetOfObjects::Ptr dummy = |
| 231 |
std::dynamic_pointer_cast<CSetOfObjects>(scene->getByName("dummy")); |
| 232 |
dummy->setVisibility(true); |
| 233 |
} |
| 234 |
|
| 235 |
// update joints positions |
| 236 |
CSetOfObjects::Ptr body = |
| 237 |
std::dynamic_pointer_cast<CSetOfObjects>(scene->getByName("body")); |
| 238 |
ASSERT_(body); |
| 239 |
|
| 240 |
for (int i = 0; i < NUM_JOINTS; ++i) |
| 241 |
{ |
| 242 |
CSphere::Ptr s = std::dynamic_pointer_cast<CSphere>(body->getByName(jointNames[i])); |
| 243 |
CPoint3D sphPos; |
| 244 |
if (i == 0) |
| 245 |
sphPos = CPoint3D(0, 0, 0); |
| 246 |
else |
| 247 |
{ |
| 248 |
m_joint_theta[i] += M_2PI / (10 * (NUM_JOINTS - 1)); |
| 249 |
sphPos.x(0.5 * cos(m_joint_theta[i])); |
| 250 |
sphPos.y(0.5 * sin(m_joint_theta[i])); |
| 251 |
sphPos.z(0.0); |
| 252 |
} |
| 253 |
s->setPose(sphPos); |
| 254 |
s->setColor(1, 0, 0); |
| 255 |
s->setRadius(i == 0 ? 0.07 : 0.03); |
| 256 |
} // end-for |
| 257 |
} // end-get3DSceneAndLock |
| 258 |
m_win->unlockAccess3DScene(); |
| 259 |
m_win->forceRepaint(); |
| 260 |
} // end if |
| 261 |
} // end if |
| 262 |
} // end-processPreviewNone |
| 263 |
|
| 264 |
/*------------------------------------------------------------- |
| 265 |
processPreview |
| 266 |
-------------------------------------------------------------*/ |
| 267 |
void CSkeletonTracker::processPreview(const mrpt::obs::CObservationSkeleton::Ptr& obs) |
| 268 |
{ |
| 269 |
using namespace mrpt::opengl; |
| 270 |
|
| 271 |
// show skeleton data |
| 272 |
if (m_showPreview) |
| 273 |
{ |
| 274 |
if (!m_win) |
| 275 |
{ |
| 276 |
string caption = string("Preview of ") + m_sensorLabel; |
| 277 |
m_win = mrpt::gui::CDisplayWindow3D::Create(caption, 800, 600); |
| 278 |
|
| 279 |
Scene::Ptr& scene = m_win->get3DSceneAndLock(); |
| 280 |
scene->insert(std::make_shared<CGridPlaneXZ>(-3, 3, 0, 5, -1.5)); |
| 281 |
|
| 282 |
// set camera parameters |
| 283 |
m_win->setCameraElevationDeg(-90); |
| 284 |
m_win->setCameraAzimuthDeg(90); |
| 285 |
m_win->setCameraZoom(4); |
| 286 |
m_win->setCameraPointingToPoint(0, 0, 0); |
| 287 |
|
| 288 |
// insert initial body |
| 289 |
CSetOfObjects::Ptr body = std::make_shared<CSetOfObjects>(); |
| 290 |
body->setName("body"); |
| 291 |
for (const auto& jointName : jointNames) |
| 292 |
{ |
| 293 |
CSphere::Ptr sph = std::make_shared<CSphere>(0.03f); |
| 294 |
sph->setColor(0, 1, 0); |
| 295 |
sph->setName(jointName); |
| 296 |
body->insert(sph); |
| 297 |
} |
| 298 |
|
| 299 |
// insert initial lines |
| 300 |
CSetOfLines::Ptr lines = std::make_shared<CSetOfLines>(); |
| 301 |
lines->setName("lines"); |
| 302 |
lines->setColor(0, 0, 1); |
| 303 |
body->insert(lines); |
| 304 |
|
| 305 |
scene->insert(body); |
| 306 |
m_win->unlockAccess3DScene(); |
| 307 |
} |
| 308 |
|
| 309 |
if (m_win && m_win->isOpen()) |
| 310 |
{ |
| 311 |
Scene::Ptr& scene = m_win->get3DSceneAndLock(); |
| 312 |
|
| 313 |
// remove help text and dummy |
| 314 |
m_win->clearTextMessages(); |
| 315 |
CSetOfObjects::Ptr dummy = |
| 316 |
std::dynamic_pointer_cast<CSetOfObjects>(scene->getByName("dummy")); |
| 317 |
if (dummy) dummy->setVisibility(false); |
| 318 |
|
| 319 |
{ |
| 320 |
// update joints positions |
| 321 |
CSetOfObjects::Ptr body = |
| 322 |
std::dynamic_pointer_cast<CSetOfObjects>(scene->getByName("body")); |
| 323 |
ASSERT_(body); |
| 324 |
|
| 325 |
for (int i = 0; i < NUM_JOINTS; ++i) |
| 326 |
{ |
| 327 |
CObservationSkeleton::TSkeletonJoint j; |
| 328 |
|
| 329 |
switch (i) |
| 330 |
{ |
| 331 |
case 0: |
| 332 |
j = obs->head; |
| 333 |
break; |
| 334 |
case 1: |
| 335 |
j = obs->neck; |
| 336 |
break; |
| 337 |
case 2: |
| 338 |
j = obs->torso; |
| 339 |
break; |
| 340 |
|
| 341 |
case 3: |
| 342 |
j = obs->left_shoulder; |
| 343 |
break; |
| 344 |
case 4: |
| 345 |
j = obs->left_elbow; |
| 346 |
break; |
| 347 |
case 5: |
| 348 |
j = obs->left_hand; |
| 349 |
break; |
| 350 |
case 6: |
| 351 |
j = obs->left_hip; |
| 352 |
break; |
| 353 |
case 7: |
| 354 |
j = obs->left_knee; |
| 355 |
break; |
| 356 |
case 8: |
| 357 |
j = obs->left_foot; |
| 358 |
break; |
| 359 |
|
| 360 |
case 9: |
| 361 |
j = obs->right_shoulder; |
| 362 |
break; |
| 363 |
case 10: |
| 364 |
j = obs->right_elbow; |
| 365 |
break; |
| 366 |
case 11: |
| 367 |
j = obs->right_hand; |
| 368 |
break; |
| 369 |
case 12: |
| 370 |
j = obs->right_hip; |
| 371 |
break; |
| 372 |
case 13: |
| 373 |
j = obs->right_knee; |
| 374 |
break; |
| 375 |
case 14: |
| 376 |
j = obs->right_foot; |
| 377 |
break; |
| 378 |
} // end-switch |
| 379 |
|
| 380 |
CSphere::Ptr s = std::dynamic_pointer_cast<CSphere>(body->getByName(jointNames[i])); |
| 381 |
s->setPose(mrpt::math::TPose3D(j.x * 1e-3, j.y * 1e-3, j.z * 1e-3, 0, 0, 0)); |
| 382 |
s->setColor(std::min(1.0, 2 * (1 - j.conf)), std::min(1.0, 2 * j.conf), 0); |
| 383 |
s->setRadius(i == 0 ? 0.07 : 0.03); |
| 384 |
} // end-for |
| 385 |
|
| 386 |
// update lines joining joints |
| 387 |
CSetOfLines::Ptr lines = std::dynamic_pointer_cast<CSetOfLines>(body->getByName("lines")); |
| 388 |
ASSERT_(lines); |
| 389 |
|
| 390 |
lines->clear(); |
| 391 |
for (int i = 0; i < NUM_LINES; ++i) |
| 392 |
{ |
| 393 |
pair<JOINT, JOINT> pair = m_linesToPlot[i]; |
| 394 |
CSphere::Ptr s0 = dynamic_pointer_cast<CSphere>(body->getByName(jointNames[pair.first])); |
| 395 |
CSphere::Ptr s1 = dynamic_pointer_cast<CSphere>(body->getByName(jointNames[pair.second])); |
| 396 |
ASSERT_(s0 && s1); |
| 397 |
|
| 398 |
lines->appendLine(s0->getPose().translation(), s1->getPose().translation()); |
| 399 |
} |
| 400 |
} // end-get3DSceneAndLock |
| 401 |
m_win->unlockAccess3DScene(); |
| 402 |
m_win->forceRepaint(); |
| 403 |
} // end if |
| 404 |
} // end if |
| 405 |
} |
| 406 |
|
| 407 |
/*------------------------------------------------------------- |
| 408 |
doProcess |
| 409 |
-------------------------------------------------------------*/ |
| 410 |
void CSkeletonTracker::doProcess() |
| 411 |
{ |
| 412 |
#if MRPT_HAS_OPENNI2 && MRPT_HAS_NITE2 |
| 413 |
if (m_state == ssError) |
| 414 |
{ |
| 415 |
std::this_thread::sleep_for(200ms); |
| 416 |
initialize(); |
| 417 |
} |
| 418 |
|
| 419 |
if (m_state == ssError) return; |
| 420 |
|
| 421 |
nite::UserTrackerFrameRef userTrackerFrame; |
| 422 |
nite::Status niteRc = user_tracker->readFrame(&userTrackerFrame); |
| 423 |
|
| 424 |
if (niteRc != nite::STATUS_OK) |
| 425 |
{ |
| 426 |
printf(" [Skeleton tracker] Get next frame failed\n"); |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
int n_data_ok = 0; |
| 431 |
const nite::Array<nite::UserData>& users = userTrackerFrame.getUsers(); |
| 432 |
m_nUsers = users.getSize(); |
| 433 |
for (int i = 0; i < m_nUsers; ++i) |
| 434 |
{ |
| 435 |
const nite::UserData& user = users[i]; |
| 436 |
|
| 437 |
// update user state |
| 438 |
skl_states[user.getId()] = user.getSkeleton().getState(); |
| 439 |
|
| 440 |
if (user.isNew()) |
| 441 |
{ |
| 442 |
user_tracker->startSkeletonTracking(user.getId()); |
| 443 |
cout << " [Skeleton tracker] New user found" << endl; |
| 444 |
} |
| 445 |
else if (user.getSkeleton().getState() == nite::SKELETON_TRACKED) |
| 446 |
{ |
| 447 |
cout << " [Skeleton tracker] User " << user.getId() << " tracked" << endl; |
| 448 |
CObservationSkeleton::Ptr obs = std::make_shared<CObservationSkeleton>(); |
| 449 |
|
| 450 |
// timestamp |
| 451 |
const uint64_t nowUI = userTrackerFrame.getTimestamp(); |
| 452 |
|
| 453 |
uint64_t AtUI = 0; |
| 454 |
if (m_timeStartUI == 0) |
| 455 |
{ |
| 456 |
m_timeStartUI = nowUI; |
| 457 |
m_timeStartTT = mrpt::Clock::now(); |
| 458 |
} |
| 459 |
else |
| 460 |
AtUI = nowUI - m_timeStartUI; |
| 461 |
|
| 462 |
/* Board time is usec */ |
| 463 |
const auto AtDO = std::chrono::microseconds(AtUI); |
| 464 |
mrpt::system::TTimeStamp ts = m_timeStartTT + AtDO; |
| 465 |
obs->timestamp = ts; |
| 466 |
|
| 467 |
// fill joint data |
| 468 |
FILL_JOINT_DATA(head, nite::JOINT_HEAD) |
| 469 |
FILL_JOINT_DATA(neck, nite::JOINT_NECK) |
| 470 |
FILL_JOINT_DATA(torso, nite::JOINT_TORSO) |
| 471 |
|
| 472 |
FILL_JOINT_DATA(left_shoulder, nite::JOINT_LEFT_SHOULDER) |
| 473 |
FILL_JOINT_DATA(left_elbow, nite::JOINT_LEFT_ELBOW) |
| 474 |
FILL_JOINT_DATA(left_hand, nite::JOINT_LEFT_HAND) |
| 475 |
FILL_JOINT_DATA(left_hip, nite::JOINT_LEFT_HIP) |
| 476 |
FILL_JOINT_DATA(left_knee, nite::JOINT_LEFT_KNEE) |
| 477 |
FILL_JOINT_DATA(left_foot, nite::JOINT_LEFT_FOOT) |
| 478 |
|
| 479 |
FILL_JOINT_DATA(right_shoulder, nite::JOINT_RIGHT_SHOULDER) |
| 480 |
FILL_JOINT_DATA(right_elbow, nite::JOINT_RIGHT_ELBOW) |
| 481 |
FILL_JOINT_DATA(right_hand, nite::JOINT_RIGHT_HAND) |
| 482 |
FILL_JOINT_DATA(right_hip, nite::JOINT_RIGHT_HIP) |
| 483 |
FILL_JOINT_DATA(right_knee, nite::JOINT_RIGHT_KNEE) |
| 484 |
FILL_JOINT_DATA(right_foot, nite::JOINT_RIGHT_FOOT) |
| 485 |
|
| 486 |
// sensor label: |
| 487 |
obs->sensorLabel = m_sensorLabel + "_" + std::to_string(user.getId()); |
| 488 |
|
| 489 |
appendObservation(obs); |
| 490 |
processPreview(obs); |
| 491 |
|
| 492 |
m_toutCounter = 0; |
| 493 |
n_data_ok++; |
| 494 |
} // end-else-if |
| 495 |
} // end-for |
| 496 |
|
| 497 |
if (n_data_ok == 0) // none of the sensors yielded data |
| 498 |
m_toutCounter++; |
| 499 |
|
| 500 |
if (m_toutCounter > 0) |
| 501 |
{ |
| 502 |
processPreviewNone(); |
| 503 |
if ((m_toutCounter % 50) == 0) cout << " [Skeleton tracker] Looking for user..." << endl; |
| 504 |
} |
| 505 |
|
| 506 |
if (m_toutCounter > 2000) |
| 507 |
{ |
| 508 |
m_toutCounter = 0; |
| 509 |
m_state = ssError; |
| 510 |
|
| 511 |
cout << " [Skeleton tracker] No user found after 2000 attempts ..." << endl; |
| 512 |
nite::NiTE::shutdown(); // close tracker |
| 513 |
} |
| 514 |
#else |
| 515 |
THROW_EXCEPTION( |
| 516 |
"MRPT has been compiled with 'BUILD_OPENNI2'=OFF or " |
| 517 |
"'MRPT_HAS_NITE2'=OFF, so this class cannot be used."); |
| 518 |
#endif |
| 519 |
} |
| 520 |
|
| 521 |
/*------------------------------------------------------------- |
| 522 |
initialize |
| 523 |
-------------------------------------------------------------*/ |
| 524 |
void CSkeletonTracker::initialize() |
| 525 |
{ |
| 526 |
#if MRPT_HAS_OPENNI2 && MRPT_HAS_NITE2 |
| 527 |
|
| 528 |
// initialize tracker |
| 529 |
nite::NiTE::initialize(); |
| 530 |
nite::Status niteRc = user_tracker->create(); |
| 531 |
|
| 532 |
if (niteRc != nite::STATUS_OK) |
| 533 |
{ |
| 534 |
printf("Couldn't create user tracker\n"); |
| 535 |
m_state = ssError; |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
printf("Sucessfully created user tracker \n"); |
| 540 |
printf( |
| 541 |
"Start moving around to get detected...\n(PSI pose may be required " |
| 542 |
"for skeleton calibration, depending on the configuration)\n"); |
| 543 |
m_state = ssInitializing; |
| 544 |
} |
| 545 |
// initialize preview joints and lines |
| 546 |
if (m_showPreview) |
| 547 |
{ |
| 548 |
m_linesToPlot[0] = make_pair(NECK, HEAD); |
| 549 |
m_linesToPlot[1] = make_pair(NECK, TORSO); |
| 550 |
m_linesToPlot[2] = make_pair(NECK, LEFT_SHOULDER); |
| 551 |
m_linesToPlot[3] = make_pair(NECK, RIGHT_SHOULDER); |
| 552 |
m_linesToPlot[4] = make_pair(LEFT_SHOULDER, LEFT_ELBOW); |
| 553 |
m_linesToPlot[5] = make_pair(LEFT_ELBOW, LEFT_HAND); |
| 554 |
m_linesToPlot[6] = make_pair(RIGHT_SHOULDER, RIGHT_ELBOW); |
| 555 |
m_linesToPlot[7] = make_pair(RIGHT_ELBOW, RIGHT_HAND); |
| 556 |
m_linesToPlot[8] = make_pair(TORSO, LEFT_HIP); |
| 557 |
m_linesToPlot[9] = make_pair(TORSO, RIGHT_HIP); |
| 558 |
m_linesToPlot[10] = make_pair(LEFT_HIP, LEFT_KNEE); |
| 559 |
m_linesToPlot[11] = make_pair(LEFT_KNEE, LEFT_FOOT); |
| 560 |
m_linesToPlot[12] = make_pair(RIGHT_HIP, RIGHT_KNEE); |
| 561 |
m_linesToPlot[13] = make_pair(RIGHT_KNEE, RIGHT_FOOT); |
| 562 |
} |
| 563 |
#else |
| 564 |
THROW_EXCEPTION( |
| 565 |
"MRPT has been compiled with 'BUILD_OPENNI2'=OFF OR " |
| 566 |
"'MRPT_HAS_NITE2'=OFF, so this class cannot be used."); |
| 567 |
#endif |
| 568 |
} |
| 569 |
|
| 570 |
/*------------------------------------------------------------- |
| 571 |
loadConfig_sensorSpecific |
| 572 |
-------------------------------------------------------------*/ |
| 573 |
void CSkeletonTracker::loadConfig_sensorSpecific( |
| 574 |
const mrpt::config::CConfigFileBase& configSource, const std::string& iniSection) |
| 575 |
{ |
| 576 |
m_sensorPose.setFromValues( |
| 577 |
configSource.read_float(iniSection, "pose_x", 0, false), |
| 578 |
configSource.read_float(iniSection, "pose_y", 0, false), |
| 579 |
configSource.read_float(iniSection, "pose_z", 0, false), |
| 580 |
DEG2RAD(configSource.read_float(iniSection, "pose_yaw", 0, false)), |
| 581 |
DEG2RAD(configSource.read_float(iniSection, "pose_pitch", 0, false)), |
| 582 |
DEG2RAD(configSource.read_float(iniSection, "pose_roll", 0, false))); |
| 583 |
|
| 584 |
m_showPreview = configSource.read_bool(iniSection, "showPreview", m_showPreview, false); |
| 585 |
|
| 586 |
// dump parameters to console |
| 587 |
cout << "---------------------------" << endl; |
| 588 |
cout << "Skeleton Tracker parameters: " << endl; |
| 589 |
cout << "---------------------------" << endl; |
| 590 |
cout << m_sensorPose << endl; |
| 591 |
cout << m_showPreview << endl; |
| 592 |
cout << "---------------------------" << endl << endl; |
| 593 |
} |