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
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 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/>. *
* *
****************************************************************************/
// --------------------------------
// Includes
// --------------------------------
#include "Device.h"
#include "Draw.h"
#include <math.h>
#include <XnLog.h>
// --------------------------------
// Defines
// --------------------------------
#define MAX_STRINGS 20
// --------------------------------
// Global Variables
// --------------------------------
Context g_Context;
ScriptNode g_scriptNode;
DeviceStringProperty g_PrimaryStream;
DeviceParameter g_Registration;
DeviceParameter g_Resolution;
bool g_bIsDepthOn = false;
bool g_bIsImageOn = false;
bool g_bIsIROn = false;
bool g_bIsAudioOn = false;
bool g_bIsPlayerOn = false;
Device g_Device;
DepthGenerator g_Depth;
ImageGenerator g_Image;
IRGenerator g_IR;
AudioGenerator g_Audio;
Player g_Player;
DepthMetaData g_DepthMD;
ImageMetaData g_ImageMD;
IRMetaData g_irMD;
AudioMetaData g_AudioMD;
ProductionNode* g_pPrimary = NULL;
// --------------------------------
// Code
// --------------------------------
void initConstants()
{
// Primary Streams
int nIndex = 0;
g_PrimaryStream.pValues[nIndex++] = "Any";
g_PrimaryStream.pValues[nIndex++] = xnProductionNodeTypeToString(XN_NODE_TYPE_DEPTH);
g_PrimaryStream.pValues[nIndex++] = xnProductionNodeTypeToString(XN_NODE_TYPE_IMAGE);
g_PrimaryStream.pValues[nIndex++] = xnProductionNodeTypeToString(XN_NODE_TYPE_IR);
g_PrimaryStream.pValues[nIndex++] = xnProductionNodeTypeToString(XN_NODE_TYPE_AUDIO);
g_PrimaryStream.nValuesCount = nIndex;
// Registration
nIndex = 0;
g_Registration.pValues[nIndex++] = FALSE;
g_Registration.pValueToName[FALSE] = "Off";
g_Registration.pValues[nIndex++] = TRUE;
g_Registration.pValueToName[TRUE] = "Depth -> Image";
g_Registration.nValuesCount = nIndex;
// Resolutions
nIndex = 0;
g_Resolution.pValues[nIndex++] = XN_RES_QVGA;
g_Resolution.pValueToName[XN_RES_QVGA] = Resolution(XN_RES_QVGA).GetName();
g_Resolution.pValues[nIndex++] = XN_RES_VGA;
g_Resolution.pValueToName[XN_RES_VGA] = Resolution(XN_RES_VGA).GetName();
g_Resolution.pValues[nIndex++] = XN_RES_SXGA;
g_Resolution.pValueToName[XN_RES_SXGA] = Resolution(XN_RES_SXGA).GetName();
g_Resolution.pValues[nIndex++] = XN_RES_UXGA;
g_Resolution.pValueToName[XN_RES_UXGA] = Resolution(XN_RES_UXGA).GetName();
g_Resolution.nValuesCount = nIndex;
}
void XN_CALLBACK_TYPE onErrorStateChanged(XnStatus errorState, void* /*pCookie*/)
{
if (errorState != XN_STATUS_OK)
{
setErrorState(xnGetStatusString(errorState));
}
else
{
setErrorState(NULL);
}
}
void openCommon()
{
XnStatus nRetVal = XN_STATUS_OK;
g_bIsDepthOn = false;
g_bIsImageOn = false;
g_bIsIROn = false;
g_bIsAudioOn = false;
g_bIsPlayerOn = false;
NodeInfoList list;
nRetVal = g_Context.EnumerateExistingNodes(list);
if (nRetVal == XN_STATUS_OK)
{
for (NodeInfoList::Iterator it = list.Begin(); it != list.End(); ++it)
{
switch ((*it).GetDescription().Type)
{
case XN_NODE_TYPE_DEVICE:
(*it).GetInstance(g_Device);
break;
case XN_NODE_TYPE_DEPTH:
g_bIsDepthOn = true;
(*it).GetInstance(g_Depth);
break;
case XN_NODE_TYPE_IMAGE:
g_bIsImageOn = true;
(*it).GetInstance(g_Image);
break;
case XN_NODE_TYPE_IR:
g_bIsIROn = true;
(*it).GetInstance(g_IR);
break;
case XN_NODE_TYPE_AUDIO:
g_bIsAudioOn = true;
(*it).GetInstance(g_Audio);
break;
case XN_NODE_TYPE_PLAYER:
g_bIsPlayerOn = true;
(*it).GetInstance(g_Player);
}
}
}
XnCallbackHandle hDummy;
g_Context.RegisterToErrorStateChange(onErrorStateChanged, NULL, hDummy);
initConstants();
readFrame();
}
XnStatus openDeviceFile(const char* csFile)
{
XnStatus nRetVal = g_Context.Init();
XN_IS_STATUS_OK(nRetVal);
nRetVal = g_Context.OpenFileRecording(csFile, g_Player);
XN_IS_STATUS_OK(nRetVal);
openCommon();
return XN_STATUS_OK;
}
XnStatus openDeviceFromXml(const char* csXmlFile, EnumerationErrors& errors)
{
XnStatus nRetVal = XN_STATUS_OK;
nRetVal = g_Context.InitFromXmlFile(csXmlFile, g_scriptNode, &errors);
XN_IS_STATUS_OK(nRetVal);
openCommon();
return (XN_STATUS_OK);
}
XnStatus openDeviceFromXmlWithChoice(const char* csXmlFile, EnumerationErrors& errors)
{
XnStatus nRetVal = XN_STATUS_OK;
xnLogInitFromXmlFile(csXmlFile);
nRetVal = g_Context.Init();
XN_IS_STATUS_OK(nRetVal);
// find devices
NodeInfoList list;
nRetVal = g_Context.EnumerateProductionTrees(XN_NODE_TYPE_DEVICE, NULL, list, &errors);
XN_IS_STATUS_OK(nRetVal);
printf("The following devices were found:\n");
int i = 1;
for (NodeInfoList::Iterator it = list.Begin(); it != list.End(); ++it, ++i)
{
NodeInfo deviceNodeInfo = *it;
Device deviceNode;
deviceNodeInfo.GetInstance(deviceNode);
XnBool bExists = deviceNode.IsValid();
if (!bExists)
{
g_Context.CreateProductionTree(deviceNodeInfo, deviceNode);
// this might fail.
}
if (deviceNode.IsValid() && deviceNode.IsCapabilitySupported(XN_CAPABILITY_DEVICE_IDENTIFICATION))
{
const XnUInt32 nStringBufferSize = 200;
XnChar strDeviceName[nStringBufferSize];
XnChar strSerialNumber[nStringBufferSize];
XnUInt32 nLength = nStringBufferSize;
deviceNode.GetIdentificationCap().GetDeviceName(strDeviceName, nLength);
nLength = nStringBufferSize;
deviceNode.GetIdentificationCap().GetSerialNumber(strSerialNumber, nLength);
printf("[%d] %s (%s)\n", i, strDeviceName, strSerialNumber);
}
else
{
printf("[%d] %s\n", i, deviceNodeInfo.GetCreationInfo());
}
// release the device if we created it
if (!bExists && deviceNode.IsValid())
{
deviceNode.Release();
}
}
printf("\n");
printf("Choose device to open (1): ");
int chosen = 1;
scanf("%d", &chosen);
// create it
NodeInfoList::Iterator it = list.Begin();
for (i = 1; i < chosen; ++i)
{
it++;
}
NodeInfo deviceNode = *it;
nRetVal = g_Context.CreateProductionTree(deviceNode, g_Device);
XN_IS_STATUS_OK(nRetVal);
// now run the rest of the XML
nRetVal = g_Context.RunXmlScriptFromFile(csXmlFile, g_scriptNode, &errors);
XN_IS_STATUS_OK(nRetVal);
openCommon();
return (XN_STATUS_OK);
}
void closeDevice()
{
g_Player.Release();
g_Device.Release();
g_Depth.Release();
g_Image.Release();
g_IR.Release();
g_Audio.Release();
g_scriptNode.Release();
g_Context.Release();
}
void readFrame()
{
XnStatus rc = XN_STATUS_OK;
if (g_pPrimary != NULL)
{
rc = g_Context.WaitOneUpdateAll(*g_pPrimary);
}
else
{
rc = g_Context.WaitAnyUpdateAll();
}
if (rc != XN_STATUS_OK)
{
printf("Error: %s\n", xnGetStatusString(rc));
}
if (g_Depth.IsValid())
{
g_Depth.GetMetaData(g_DepthMD);
}
if (g_Image.IsValid())
{
g_Image.GetMetaData(g_ImageMD);
}
if (g_IR.IsValid())
{
g_IR.GetMetaData(g_irMD);
}
if (g_Audio.IsValid())
{
g_Audio.GetMetaData(g_AudioMD);
}
}
void changeRegistration(int nValue)
{
if (!g_Depth.IsValid() || !g_Depth.IsCapabilitySupported(XN_CAPABILITY_ALTERNATIVE_VIEW_POINT))
{
return;
}
if (!nValue)
{
g_Depth.GetAlternativeViewPointCap().ResetViewPoint();
}
else if (g_Image.IsValid())
{
g_Depth.GetAlternativeViewPointCap().SetViewPoint(g_Image);
}
}
void changePrimaryStream(int nIndex)
{
if (nIndex == 0)
{
g_pPrimary = NULL;
return;
}
XnProductionNodeType Type;
xnProductionNodeTypeFromString(g_PrimaryStream.pValues[nIndex], &Type);
switch (Type)
{
case XN_NODE_TYPE_DEPTH:
g_pPrimary = getDepthGenerator();
break;
case XN_NODE_TYPE_IMAGE:
g_pPrimary = getImageGenerator();
break;
case XN_NODE_TYPE_IR:
g_pPrimary = getIRGenerator();
break;
case XN_NODE_TYPE_AUDIO:
g_pPrimary = getAudioGenerator();
break;
}
}
void createStream(Generator& generator, XnProductionNodeType type)
{
EnumerationErrors errors;
XnStatus nRetVal = g_Context.CreateAnyProductionTree(type, NULL, generator, &errors);
if (nRetVal == XN_STATUS_NO_NODE_PRESENT)
{
XnChar strError[1024];
errors.ToString(strError, 1024);
displayMessage("%s", strError);
return;
}
else if (nRetVal != XN_STATUS_OK)
{
displayMessage("Open failed: %s", xnGetStatusString(nRetVal));
return;
}
}
void toggleStream(Generator& generator, XnProductionNodeType type, bool* bIsOn)
{
XnStatus nRetVal = XN_STATUS_OK;
if (!generator.IsValid())
{
createStream(generator, type);
}
if (!generator.IsValid())
{
// failed creating the stream
return;
}
if (generator.IsGenerating())
{
generator.StopGenerating();
}
else
{
nRetVal = generator.StartGenerating();
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to turn on %s: %s", generator.GetInfo().GetInstanceName(), xnGetStatusString(nRetVal));
return;
}
}
*bIsOn = (generator.IsGenerating() == TRUE);
}
void toggleDepthState(int )
{
toggleStream(g_Depth, XN_NODE_TYPE_DEPTH, &g_bIsDepthOn);
}
void toggleImageState(int )
{
toggleStream(g_Image, XN_NODE_TYPE_IMAGE, &g_bIsImageOn);
}
void toggleIRState(int )
{
toggleStream(g_IR, XN_NODE_TYPE_IR, &g_bIsIROn);
}
void toggleAudioState(int )
{
toggleStream(g_Audio, XN_NODE_TYPE_AUDIO, &g_bIsAudioOn);
}
void toggleMirror(int )
{
XnStatus nRetVal = g_Context.SetGlobalMirror(!g_Context.GetGlobalMirror());
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to set global mirror: %s", xnGetStatusString(nRetVal));
}
}
void seekFrame(int nDiff)
{
XnStatus nRetVal = XN_STATUS_OK;
if (isPlayerOn())
{
const XnChar* strNodeName = NULL;
if (g_pPrimary != NULL)
{
strNodeName = g_pPrimary->GetName();
}
else if (g_Depth.IsValid())
{
strNodeName = g_Depth.GetName();
}
else if (g_Image.IsValid())
{
strNodeName = g_Image.GetName();
}
else if (g_IR.IsValid())
{
strNodeName = g_IR.GetName();
}
else if (g_Audio.IsValid())
{
strNodeName = g_Audio.GetName();
}
nRetVal = g_Player.SeekToFrame(strNodeName, nDiff, XN_PLAYER_SEEK_CUR);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to seek: %s", xnGetStatusString(nRetVal));
return;
}
XnUInt32 nFrame = 0;
XnUInt32 nNumFrames = 0;
nRetVal = g_Player.TellFrame(strNodeName, nFrame);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to tell frame: %s", xnGetStatusString(nRetVal));
return;
}
nRetVal = g_Player.GetNumFrames(strNodeName, nNumFrames);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to get number of frames: %s", xnGetStatusString(nRetVal));
return;
}
displayMessage("Seeked %s to frame %u/%u", strNodeName, nFrame, nNumFrames);
}
}
bool isDepthOn()
{
return (g_bIsDepthOn);
}
bool isImageOn()
{
return (g_bIsImageOn);
}
bool isIROn()
{
return (g_bIsIROn);
}
bool isAudioOn()
{
return (g_bIsAudioOn);
}
bool isPlayerOn()
{
return (g_bIsPlayerOn);
}
void setResolution(MapGenerator* pGenerator, int res)
{
if (pGenerator == NULL)
{
displayMessage("Stream does not exist!");
return;
}
XnMapOutputMode Mode;
pGenerator->GetMapOutputMode(Mode);
Mode.nXRes = Resolution((XnResolution)res).GetXResolution();
Mode.nYRes = Resolution((XnResolution)res).GetYResolution();
XnStatus nRetVal = pGenerator->SetMapOutputMode(Mode);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to set resolution: %s", xnGetStatusString(nRetVal));
}
}
void setFPS(MapGenerator* pGenerator, int fps)
{
if (pGenerator == NULL)
{
displayMessage("Stream does not exist!");
return;
}
XnMapOutputMode Mode;
pGenerator->GetMapOutputMode(Mode);
Mode.nFPS = fps;
XnStatus nRetVal = pGenerator->SetMapOutputMode(Mode);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to set FPS: %s", xnGetStatusString(nRetVal));
}
}
void setDepthResolution(int res)
{
setResolution(getDepthGenerator(), res);
}
void setDepthFPS(int fps)
{
setFPS(getDepthGenerator(), fps);
}
void setImageResolution(int res)
{
setResolution(getImageGenerator(), res);
}
void setImageFPS(int fps)
{
setFPS(getImageGenerator(), fps);
}
void setIRResolution(int res)
{
setResolution(getIRGenerator(), res);
}
void setIRFPS(int fps)
{
setFPS(getIRGenerator(), fps);
}
void setStreamCropping(MapGenerator* pGenerator, XnCropping* pCropping)
{
if (pGenerator == NULL)
{
displayMessage("Stream does not exist!");
return;
}
if (!pGenerator->IsCapabilitySupported(XN_CAPABILITY_CROPPING))
{
displayMessage("Stream does not support cropping!");
return;
}
XnStatus nRetVal = pGenerator->GetCroppingCap().SetCropping(*pCropping);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to set cropping: %s", xnGetStatusString(nRetVal));
return;
}
}
void setPlaybackSpeed(int ratioDiff)
{
if (g_Player.IsValid())
{
XnDouble dNewSpeed = g_Player.GetPlaybackSpeed() * pow(2.0, (XnDouble)ratioDiff);
XnStatus nRetVal = g_Player.SetPlaybackSpeed(dNewSpeed);
if (nRetVal != XN_STATUS_OK)
{
displayMessage("Failed to set playback speed: %s", xnGetStatusString(nRetVal));
}
}
else
{
displayMessage("Can't set playback speed - input is not a recording!");
}
}
XnDouble getPlaybackSpeed()
{
if (g_Player.IsValid())
{
return g_Player.GetPlaybackSpeed();
}
else
{
return 1.0;
}
}
Device* getDevice()
{
return g_Device.IsValid() ? &g_Device : NULL;
}
DepthGenerator* getDepthGenerator()
{
return g_Depth.IsValid() ? &g_Depth : NULL;
}
ImageGenerator* getImageGenerator()
{
return g_Image.IsValid() ? &g_Image : NULL;
}
IRGenerator* getIRGenerator()
{
return g_IR.IsValid() ? &g_IR : NULL;
}
AudioGenerator* getAudioGenerator()
{
return g_Audio.IsValid() ? &g_Audio : NULL;
}
const DepthMetaData* getDepthMetaData()
{
return g_Depth.IsValid() ? &g_DepthMD : NULL;
}
const ImageMetaData* getImageMetaData()
{
return g_Image.IsValid() ? &g_ImageMD : NULL;
}
const IRMetaData* getIRMetaData()
{
return g_IR.IsValid() ? &g_irMD : NULL;
}
const AudioMetaData* getAudioMetaData()
{
return g_Audio.IsValid() ? &g_AudioMD : NULL;
}
|