00001
00002 #include "WinBaseLib.h"
00003 #include "KinectWin.h"
00004 #include "NiJointsTool.h"
00005
00006 #include "Graph.h"
00007
00008 using namespace jbxl;
00009
00010
00011 #ifdef ENABLE_KINECT_SDK
00012
00013 using namespace jbxwl;
00014
00015
00016 CKinectWin::CKinectWin(void)
00017 {
00018 device = NULL;
00019 audio = NULL;
00020
00021 #ifdef ENABLE_NI_SPEECH
00022 speech = NULL;
00023 #endif
00024
00025 m_err_mesg = _T("");
00026
00027 m_image_scale = 2;
00028 m_depth_scale = 2;
00029 m_skeleton_line = 2;
00030
00031 m_is_detected = FALSE;
00032 m_is_tracking = FALSE;
00033 m_is_mirroring = TRUE;
00034
00035 m_use_image = TRUE;
00036 m_use_led = FALSE;
00037 m_use_motor = FALSE;
00038 m_use_face = FALSE;
00039 m_use_speech = TRUE;
00040
00041
00042 m_enable_face = FALSE;
00043 m_enable_speech = FALSE;
00044 m_enable_motor = TRUE;
00045
00046 m_use_knct_smth = TRUE;
00047 m_confidence = 0.5;
00048 m_profile = KINECT_SKEL_PROFILE_ALL;
00049 m_ground_level = NI_DEFAULT_GROUND_LEVEL;
00050
00051 pViewData = NULL;
00052 pDepthData = NULL;
00053
00054 hasDepthData = FALSE;
00055 isDetectShadow = FALSE;
00056 isDetectFace = FALSE;
00057
00058 tracking_user = 0;
00059 tracking_deny = 0;
00060
00061
00062 smoothParam.fSmoothing = 0.5f;
00063 smoothParam.fCorrection = 0.5f;
00064 smoothParam.fPrediction = 0.5f;
00065 smoothParam.fJitterRadius = 0.05f;
00066 smoothParam.fMaxDeviationRadius = 0.04f;
00067
00068 clearAvatarDetected();
00069 }
00070
00071
00072
00073 CKinectWin::~CKinectWin(void)
00074 {
00075 DEBUG_INFO("DESTRUCTOR: CKinectWin: START\n");
00076 free();
00077 DEBUG_INFO("DESTRUCTOR: CKinectWin: END\n");
00078 }
00079
00080
00081
00082 BOOL CKinectWin::init(void)
00083 {
00084 device = new CKinectDevice();
00085
00086
00087 ::NuiSetDeviceStatusCallback((NuiStatusProc)&KinectStatusProc, this);
00088
00089
00090 DWORD mode = NUI_INITIALIZE_FLAG_USES_COLOR |
00091 NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX |
00092 NUI_INITIALIZE_FLAG_USES_SKELETON |
00093 NUI_INITIALIZE_FLAG_USES_AUDIO;
00094
00095 BOOL ret = device->init(mode, m_use_image);
00096 if (!ret) {
00097 m_err_mesg = device->m_err_mesg;
00098 }
00099
00100
00101 setDevState(NI_STATE_DETECT_STOPPED);
00102
00103 return ret;
00104 }
00105
00106
00107
00108 void CKinectWin::free(void)
00109 {
00110 freeRingBuffer();
00111
00112 deleteDevice();
00113 return;
00114 }
00115
00116
00117
00118 void CKinectWin::deleteDevice(void)
00119 {
00120 DEBUG_INFO("CKinectWin::deleteDevice(): START\n");
00121
00122 deleteNull(device);
00123
00124 DEBUG_INFO("CKinectWin::deleteDevice(): END\n");
00125
00126 return;
00127 }
00128
00129
00130
00131 CString CKinectWin::get_err_message(void)
00132 {
00133 DEBUG_INFO("CKinectWin::get_err_message(): => %s\n", ::ts2mbs(m_err_mesg));
00134 CString mesg = m_err_mesg;
00135 m_err_mesg = _T("");
00136
00137 return mesg;
00138 }
00139
00140
00141
00142 void CKinectWin::clearJointsData(void)
00143 {
00144 for (int i=0; i<KINECT_JOINT_NUM; i++) {
00145 rotQuat[i].init(-1.0);
00146 posVect[i].init(-1.0);
00147 jntAngl[i] = 0.0;
00148 }
00149 }
00150
00151
00152
00153 BOOL CKinectWin::initRingBuffer(void)
00154 {
00155 BOOL ret = TRUE;
00156
00157 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00158 posRing[j].init(NI_RING_BUFFER_SIZE, sizeof(Vector<float>));
00159 rotRing[j].init(NI_RING_BUFFER_SIZE, sizeof(Quaternion<float>));
00160 if (posRing[j].state<0 || rotRing[j].state<0) {
00161 freeRingBuffer();
00162 m_err_mesg = _T("CKinectWin::initRingBuffer(): WARNING: Out of Memory.");
00163 ret = FALSE;
00164 break;
00165 }
00166 }
00167
00168 return ret;
00169 }
00170
00171
00172
00173 void CKinectWin::freeRingBuffer(void)
00174 {
00175 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00176 if (posRing[j].enable) posRing[j].free();
00177 if (rotRing[j].enable) rotRing[j].free();
00178 }
00179 }
00180
00181
00182
00183 void CKinectWin::clearRingBuffer(void)
00184 {
00185 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00186 if (posRing[j].enable) posRing[j].clear();
00187 if (rotRing[j].enable) rotRing[j].clear();
00188 }
00189 }
00190
00191
00192
00193 void CKinectWin::backup2RingBuffer(void)
00194 {
00195 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00196 if (posVect[j].c>=m_confidence) posRing[j].put(&posVect[j]);
00197 if (rotQuat[j].c>=m_confidence) rotRing[j].put(&rotQuat[j]);
00198 }
00199 }
00200
00201
00202
00203
00205
00206
00207 void CKinectWin::clearAvatarDetected(void)
00208 {
00209 clearJointsData();
00210 clearRingBuffer();
00211
00212 startPos = Vector<float>(0.0, 0.0, 0.0);
00213 currentPos = Vector<float>(0.0, 0.0, 0.0);
00214 m_is_detected = FALSE;
00215 m_ground_level= NI_DEFAULT_GROUND_LEVEL;
00216
00217 return;
00218 }
00219
00220
00221
00222 BOOL CKinectWin::checkAvatarDetected(void)
00223 {
00224 if (posVect[NI_SDK_PELVIS].c>m_confidence && crdVect[NI_SDK_PELVIS].c>0.0) {
00225 startPos = posVect[NI_SDK_PELVIS];
00226 m_is_detected = TRUE;
00227
00228
00229 if (posVect[NI_SDK_R_ANKLE].c>m_confidence && crdVect[NI_SDK_R_ANKLE].c>0.0) {
00230 m_ground_level = posVect[NI_SDK_R_ANKLE].z - startPos.z;
00231 if (posVect[NI_SDK_L_FOOT].c>m_confidence && crdVect[NI_SDK_L_FOOT].c>0.0) {
00232 m_ground_level = Min(m_ground_level, posVect[NI_SDK_L_ANKLE].z - startPos.z);
00233 }
00234 }
00235 else if (posVect[NI_SDK_L_FOOT].c>m_confidence && crdVect[NI_SDK_L_FOOT].c>0.0) {
00236 m_ground_level = posVect[NI_SDK_L_ANKLE].z - startPos.z;
00237 }
00238 }
00239
00240 return m_is_detected;
00241 }
00242
00243
00244
00245 void CKinectWin::setTiltMotor(int ang)
00246 {
00247 if (m_use_motor && m_enable_motor) {
00248 m_enable_motor = FALSE;
00249 NuiCameraElevationSetAngle(ang);
00250 Sleep(1000);
00251 m_enable_motor = TRUE;
00252 }
00253 }
00254
00255
00256
00257 void CKinectWin::setMirroring(BOOL mirror)
00258 {
00259 m_is_mirroring = mirror;
00260 }
00261
00262
00263
00264 BOOL CKinectWin::startDetection(void)
00265 {
00266 clearJointsData();
00267
00268 device->smoothParam = NULL;
00269 if (m_use_knct_smth) device->smoothParam = &smoothParam;
00270
00271 tracking_user = 0;
00272 tracking_deny = 0;
00273 BOOL ret = device->start_Detection(m_profile);
00274 if (ret) setLEDColor(NI_LED_BLINK_ORANGE);
00275 else m_err_mesg = device->m_err_mesg;
00276
00277
00278 if (ret && m_use_face) {
00279 ret = device->create_Face();
00280 if (!ret) m_err_mesg = device->m_err_mesg;
00281 }
00282
00283 return ret;
00284 }
00285
00286
00287
00288 BOOL CKinectWin::stopDetection(void)
00289 {
00290 BOOL ret = device->stop_Detection();
00291 if (!ret) m_err_mesg = device->m_err_mesg;
00292 setLEDColor(NI_LED_GREEN);
00293
00294 tracking_user = 0;
00295 tracking_deny = 0;
00296
00297
00298 isDetectFace = FALSE;
00299 device->delete_Face();
00300
00301 return ret;
00302 }
00303
00304
00305
00306 BOOL CKinectWin::restartDetection(void)
00307 {
00308 BOOL ret = stopDetection();
00309 if (ret) ret = startDetection();
00310
00311 return ret;
00312 }
00313
00314
00315
00316 Vector4 CKinectWin::jointPositionData(int j)
00317 {
00318 Vector4 vect;
00319
00320 if (j>=0 && j<KINECT_JOINT_NUM) {
00321 vect = device->jointPosData[j];
00322 }
00323 else {
00324 memset(&vect, 0, sizeof(Vector4));
00325 }
00326
00327 return vect;
00328 }
00329
00330
00331
00332
00333
00334
00335 void CKinectWin::makeDisplayImage()
00336 {
00337 int index;
00338 uByte* src = NULL;
00339 uByte* ptr;
00340
00341 if (pViewData==NULL) return;
00342
00343 if (device->image!=NULL && m_use_image) {
00344 src = (uByte*)device->image->m_data;
00345 }
00346
00347
00348 if (src!=NULL) {
00349 for (int j=0; j<pViewData->ysize; j++) {
00350 int li;
00351 int ls = j*m_image_scale*device->m_xsize;
00352
00353 for (int i=0; i<pViewData->xsize; i++) {
00354
00355 if (m_is_mirroring) li = i*m_image_scale;
00356 else li = (pViewData->xsize-i-1)*m_image_scale;
00357
00358 ptr = &(pViewData->point(i, j));
00359 index = (ls + li)*4;
00360 ptr[0] = src[index];
00361 ptr[1] = src[index+1];
00362 ptr[2] = src[index+2];
00363 ptr[3] = src[index+3];
00364 }
00365 }
00366 }
00367
00368
00369 else {
00370 for (int j=0; j<pViewData->ysize; j++) {
00371 int li;
00372 int ls = j*m_image_scale*device->m_xsize;
00373
00374 for (int i=0; i<pViewData->xsize; i++) {
00375
00376 if (m_is_mirroring) li = i*m_image_scale;
00377 else li = (pViewData->xsize-i-1)*m_image_scale;
00378
00379 ptr = &(pViewData->point(i, j));
00380 ptr[0] = ptr[1] = ptr[2] = 224;
00381 ptr[3] = 0;
00382 }
00383 }
00384 }
00385 }
00386
00387
00388
00389
00390
00391
00392 void CKinectWin::makeDisplayDepth(CExView* pview)
00393 {
00394 if (pview==NULL) return;
00395 pDepthData = &(pview->viewData);
00396
00397 uWord* src = NULL;
00398 if (device->depth!=NULL && hasDepthData) {
00399 src = (uWord*)device->depth->m_data;
00400 }
00401 if (src==NULL) return;
00402
00403
00404 uWord max = pview->cMax;
00405 uWord min = pview->cMin;
00406
00407 uWord* wrk = (uWord*)malloc(sizeof(uWord)*pDepthData->ysize*pDepthData->xsize);
00408
00409
00410 for (int j=0; j<pDepthData->ysize; j++) {
00411 int jj = j*pDepthData->xsize;
00412 int ls = j*m_depth_scale*device->m_xsize;
00413
00414 for (int i=0; i<pDepthData->xsize; i++) {
00415
00416 int li = i*m_depth_scale;
00417 if (!m_is_mirroring) li = device->m_xsize - li - 1;
00418
00419 int k = jj + i;
00420 wrk[k] = ::NuiDepthPixelToDepth(src[ls+li]);
00421 if (wrk[k]>max) wrk[k] = max;
00422 else if (wrk[k]<min) wrk[k] = min;
00423 }
00424 }
00425
00426
00427 float dif = (float)(max - min);
00428 uByte* ptr = (uByte*)pDepthData->grptr;
00429
00430 if (dif!=0.0) {
00431 for (int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
00432 ptr[i] = (uByte)((max - wrk[i])/dif*255);
00433 }
00434 }
00435 else {
00436 for (int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
00437 ptr[i] = (uByte)(255 - wrk[i]/256);
00438 }
00439 }
00440 ::free(wrk);
00441
00442 return;
00443 }
00444
00445
00446
00447
00449
00450
00451 BOOL CKinectWin::trackingJoints(void)
00452 {
00453 m_is_tracking = FALSE;
00454
00455 if (hasDepthData) {
00456
00457
00458 int tuser = tracking_user;
00459 tracking_user = device->wait_Skeleton(tracking_user, tracking_deny);
00460
00461 if (tuser!=tracking_user) {
00462 m_is_detected = FALSE;
00463 if (tracking_user==0) {
00464 clearAvatarDetected();
00465 lostTrackingUser(tuser);
00466 }
00467 else {
00468 clearJointsData();
00469 detectTrackingUser(tracking_user);
00470 }
00471 }
00472
00473
00474 if (tracking_user>0 && tracking_user<=KINECT_USERS_NUM) {
00475
00476 getJointsPosData();
00477
00478 if (m_is_detected) {
00479 convertJointsData();
00480
00481 backup2RingBuffer();
00482 saveJointsData();
00483 loggingJointsData();
00484 }
00485
00486 paintShadow();
00487 if (m_is_detected) {
00488 drawSkeleton(NiGetSkeletonColor(tracking_user), m_skeleton_line);
00489 drawAddition(NiGetSkeletonColor(tracking_user), m_skeleton_line);
00490 }
00491
00492 m_is_tracking = TRUE;
00493 }
00494 }
00495 else {
00496 m_is_detected = FALSE;
00497 }
00498
00499 return m_is_tracking;
00500 }
00501
00502
00503
00504 Vector4 CKinectWin::joint_PositionData(int j)
00505 {
00506 Vector4 vect;
00507
00508 if (j>=0 && j<KINECT_JOINT_NUM) {
00509 vect = device->jointPosData[j];
00510 }
00511 else {
00512 memset(&vect, 0, sizeof(Vector4));
00513 }
00514
00515 return vect;
00516 }
00517
00518
00519
00520
00521
00522 void CKinectWin::getJointsPosData(void)
00523 {
00524 if (!m_is_mirroring) {
00525 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00526 device->jointPosData[j].x = - device->jointPosData[j].x;
00527 }
00528 }
00529
00530
00531 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00532 Vector4 pos = device->jointPosData[j];
00533 int n = j;
00534 if (m_is_mirroring) n = NiSDKMirrorJointNum(j, NiSDK_Kinect);
00535 posVect[n].set(-pos.z, pos.x, pos.y);
00536 }
00537
00538
00539 if (m_profile==KINECT_SKEL_PROFILE_ALL) {
00540 posVect[NI_SDK_TORSO] = 2.0*posVect[NI_SDK_PELVIS] - (posVect[NI_SDK_R_HIP] + posVect[NI_SDK_L_HIP])/2.0;
00541 posVect[NI_SDK_TORSO].c = Min(posVect[NI_SDK_R_HIP].c, posVect[NI_SDK_L_HIP].c);
00542 posVect[NI_SDK_TORSO].c = Min(posVect[NI_SDK_TORSO].c, posVect[NI_SDK_PELVIS].c);
00543 }
00544 else if (m_profile==KINECT_SKEL_PROFILE_UPPER) {
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 Vector<float> torso_down(0.0f, 0.0f, -0.2f);
00557
00558 posVect[NI_SDK_TORSO] = (posVect[NI_SDK_R_SHLDR] + posVect[NI_SDK_L_SHLDR])/2.0 + torso_down;
00559 posVect[NI_SDK_TORSO].c = Min(posVect[NI_SDK_R_SHLDR].c, posVect[NI_SDK_L_SHLDR].c);
00560 posVect[NI_SDK_PELVIS] = posVect[NI_SDK_TORSO] + torso_down;
00561 }
00562
00563
00564 set2DCoordinate();
00565 checkBoneLength();
00566 if (!m_is_detected) checkAvatarDetected();
00567
00568
00569 currentPos = posVect[NI_SDK_PELVIS];
00570 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00571 posVect[j] = posVect[j] - startPos;
00572 if (posVect[j].c<m_confidence || crdVect[j].c<0.0) posVect[j].c = -1.0;
00573 }
00574 checkGroundLevel();
00575 }
00576
00577
00578
00579
00581
00582
00583 BOOL CKinectWin::detectShadow(void)
00584 {
00585 int label = 0;
00586
00587 if (pViewData==NULL) return FALSE;
00588
00589
00590 for (int j=0; j<pViewData->ysize; j++) {
00591 int li;
00592 int lj = j*m_image_scale;
00593
00594 for (int i=0; i<pViewData->xsize; i++) {
00595
00596 if (m_is_mirroring) li = i*m_image_scale;
00597 else li = (pViewData->xsize-i-1)*m_image_scale;
00598
00599
00600 if (getDevState()==NI_STATE_DETECT_EXEC && hasDepthData) {
00601
00602 label = device->depth->get_user_index(li, lj);
00603 if (label>0) {
00604 if (tracking_user==label) return TRUE;
00605 }
00606 }
00607 }
00608 }
00609
00610 return FALSE;
00611 }
00612
00613
00614
00615 void CKinectWin::paintShadow(void)
00616 {
00617 int label = 0;
00618 uByte* ptr;
00619
00620 if (pViewData==NULL) return;
00621
00622 if (getDevState()==NI_STATE_DETECT_EXEC && hasDepthData) {
00623
00624 for (int j=0; j<pViewData->ysize; j++) {
00625 int li;
00626 int lj = j*m_image_scale;
00627
00628 for (int i=0; i<pViewData->xsize; i++) {
00629
00630 if (m_is_mirroring) li = i*m_image_scale;
00631 else li = (pViewData->xsize-i-1)*m_image_scale;
00632
00633
00634 label = device->depth->get_user_index(li, lj);
00635 if (label>0) {
00636 ptr = &(pViewData->point(i, j));
00637 NiSetUserColor(label, ptr, m_use_image);
00638 }
00639 }
00640 }
00641 }
00642 }
00643
00644
00645
00646 void CKinectWin::drawSkeleton(int col, int line)
00647 {
00648 drawJointConnection(NI_SDK_PELVIS, NI_SDK_TORSO, col, line);
00649 drawJointConnection(NI_SDK_TORSO, NI_SDK_NECK, col, line);
00650 drawJointConnection(NI_SDK_NECK, NI_SDK_HEAD, col, line);
00651
00652 drawJointConnection(NI_SDK_NECK, NI_SDK_L_SHLDR, col, line);
00653 drawJointConnection(NI_SDK_L_SHLDR, NI_SDK_L_ELBOW, col, line);
00654 drawJointConnection(NI_SDK_L_ELBOW, NI_SDK_L_WRIST, col, line);
00655 drawJointConnection(NI_SDK_L_WRIST, NI_SDK_L_HAND, col, line);
00656
00657 drawJointConnection(NI_SDK_NECK, NI_SDK_R_SHLDR, col, line);
00658 drawJointConnection(NI_SDK_R_SHLDR, NI_SDK_R_ELBOW, col, line);
00659 drawJointConnection(NI_SDK_R_ELBOW, NI_SDK_R_WRIST, col, line);
00660 drawJointConnection(NI_SDK_R_WRIST, NI_SDK_R_HAND, col, line);
00661
00662 drawJointConnection(NI_SDK_PELVIS, NI_SDK_L_HIP, col, line);
00663 drawJointConnection(NI_SDK_L_HIP, NI_SDK_L_KNEE, col, line);
00664 drawJointConnection(NI_SDK_L_KNEE, NI_SDK_L_ANKLE, col, line);
00665 drawJointConnection(NI_SDK_L_ANKLE, NI_SDK_L_FOOT, col, line);
00666
00667 drawJointConnection(NI_SDK_PELVIS, NI_SDK_R_HIP, col, line);
00668 drawJointConnection(NI_SDK_R_HIP, NI_SDK_R_KNEE, col, line);
00669 drawJointConnection(NI_SDK_R_KNEE, NI_SDK_R_ANKLE, col, line);
00670 drawJointConnection(NI_SDK_R_ANKLE, NI_SDK_R_FOOT, col, line);
00671 }
00672
00673
00674
00675 void CKinectWin::drawJointConnection(int j1, int j2, int col, int line)
00676 {
00677 if (pViewData==NULL) return;
00678
00679 MSGraph<unsigned int> vp;
00680 vp.xs = pViewData->xsize;
00681 vp.ys = pViewData->ysize;
00682 vp.zs = 1;
00683 vp.gp = (unsigned int*)pViewData->grptr;
00684
00685 if (crdVect[j1].c>0.0 && crdVect[j2].c>0.0) {
00686 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y, crdVect[j2].x, crdVect[j2].y, col);
00687 for (int i=1; i<line; i++) {
00688 MSGraph_Line(vp, crdVect[j1].x+i, crdVect[j1].y, crdVect[j2].x+i, crdVect[j2].y, col);
00689 MSGraph_Line(vp, crdVect[j1].x-i, crdVect[j1].y, crdVect[j2].x-i, crdVect[j2].y, col);
00690 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y+i, crdVect[j2].x, crdVect[j2].y+i, col);
00691 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y-i, crdVect[j2].x, crdVect[j2].y-i, col);
00692 }
00693 }
00694 }
00695
00696
00697
00698 void CKinectWin::set2DCoordinate(void)
00699 {
00700 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00701 crdVect[j].init(-1.0);
00702 }
00703
00704
00705 if (device->depth!=NULL && pViewData!=NULL) {
00706
00707 for (int j=0; j<KINECT_JOINT_NUM; j++) {
00708 Vector4 pos;
00709
00710 if (j==NI_SDK_PELVIS || j==NI_SDK_TORSO) {
00711 if (m_profile==KINECT_SKEL_PROFILE_UPPER) {
00712 Vector4 pos_neck = device->jointPosData[NI_SDK_NECK];
00713 Vector4 pos_rshd = device->jointPosData[NI_SDK_R_SHLDR];
00714 Vector4 pos_lshd = device->jointPosData[NI_SDK_L_SHLDR];
00715
00716 pos.x = (FLOAT)((pos_rshd.x + pos_lshd.x)*1.5 - pos_neck.x*2.0);
00717 pos.y = (FLOAT)((pos_rshd.y + pos_lshd.y)*1.5 - pos_neck.y*2.0);
00718 pos.z = (FLOAT)((pos_rshd.z + pos_lshd.z)*1.5 - pos_neck.z*2.0);
00719
00720 }
00721 else if (j==NI_SDK_TORSO) {
00722 Vector4 pos_pelv = device->jointPosData[NI_SDK_PELVIS];
00723 Vector4 pos_rhip = device->jointPosData[NI_SDK_R_HIP];
00724 Vector4 pos_lhip = device->jointPosData[NI_SDK_L_HIP];
00725
00726 pos.x = (FLOAT)(pos_pelv.x*2.0 -(pos_rhip.x+pos_lhip.x)*0.5);
00727 pos.y = (FLOAT)(pos_pelv.y*2.0 -(pos_rhip.y+pos_lhip.y)*0.5);
00728 pos.z = (FLOAT)(pos_pelv.z*2.0 -(pos_rhip.z+pos_lhip.z)*0.5);
00729
00730 }
00731 else {
00732 pos = device->jointPosData[j];
00733 }
00734 }
00735 else {
00736 pos = device->jointPosData[j];
00737 }
00738
00739 float x, y;
00740 NuiTransformSkeletonToDepthImage(pos, &x, &y, NUI_IMAGE_RESOLUTION_640x480);
00741
00742 int xs = (int)x;
00743 int ys = (int)y;
00744 if (xs>0 && ys>0) {
00745 device->depth->get_image_coord(&xs, &ys);
00746 if (xs>=0) xs = xs/m_image_scale;
00747 if (ys>=0) ys = ys/m_image_scale;
00748
00749 crdVect[j].x = xs;
00750 crdVect[j].y = ys;
00751 if (xs>0 && xs<pViewData->xsize && ys>0 && ys<pViewData->ysize) {
00752 crdVect[j].c = 1.0;
00753 }
00754 }
00755 }
00756 }
00757
00758 return;
00759 }
00760
00761
00762
00763
00765
00766
00767 #ifdef ENABLE_NI_SPEECH
00768
00769 BOOL CKinectWin::initSpeech(void)
00770 {
00771 m_err_mesg = _T("");
00772
00773 if (audio==NULL) audio = new CKinectAudio();
00774
00775 BOOL ret = audio->init(device->context, KINECT_AUDIO_SINGLE_AEC);
00776 if (!ret) m_err_mesg = _T("CKinectWin::initSpeech(): ERROR: Audio Initialization Error!!");
00777
00778
00779 if (ret) {
00780 if (speech==NULL) speech = makeSpeech();
00781 WAVEFORMATEX format = audio->getAudioFormat();
00782 IStream* stream = audio->getIStream();
00783 ret = speech->init(stream, &format);
00784 if (!ret) m_err_mesg = _T("CKinectWin::initSpeech(): ERROR: Speech Initialization Error!!");
00785 }
00786
00787
00788 if (!ret) {
00789 if (speech!=NULL) {
00790 speech->free();
00791 deleteNull(speech);
00792 }
00793 deleteNull(audio);
00794 return FALSE;
00795 }
00796
00797 return TRUE;
00798 }
00799
00800
00801
00802 BOOL CKinectWin::createSpeech(LPCTSTR lang, LPCTSTR grfile)
00803 {
00804 m_err_mesg = _T("");
00805
00806 BOOL ret = TRUE;
00807 if (speech==NULL) ret = initSpeech();
00808
00809 if (ret) {
00810 ret = speech->create(lang);
00811 if (!ret) m_err_mesg = _T("CKinectWin::createSpeech(): ERROR: Speech Context Creation Error!!\nPerhaps, Language Pack is not installed.");
00812 }
00813
00814 if (ret) {
00815 ret = speech->load(grfile);
00816 if (!ret) {
00817 m_err_mesg = _T("CKinectWin::createSpeech(): ERROR: Grammar File Loading Error!!\nFile = ");
00818 m_err_mesg += grfile;
00819 }
00820 }
00821
00822
00823 if (!ret) return FALSE;
00824 return TRUE;
00825 }
00826
00827
00828
00829 BOOL CKinectWin::startSpeech(float confidence)
00830 {
00831 m_err_mesg = _T("");
00832
00833 if (audio==NULL || speech==NULL) {
00834 m_err_mesg = _T("CKinectWin::startSpeech(): ERROR: Audio or Speech Instance is/are NULL!!");
00835 return FALSE;
00836 }
00837
00838 BOOL ret = audio->startCapture();
00839 if (!ret) m_err_mesg = _T("CKinectWin::startSpeech(): ERROR: Audio Capture Starting Error!!");
00840
00841 if (ret) {
00842 ret = speech->start(confidence);
00843 if (!ret) {
00844 audio->stopCapture();
00845 m_err_mesg = _T("CKinectWin::startSpeech(): ERROR: Speech Platform Starting Error!!");
00846 }
00847 }
00848
00849 if (!ret) {
00850 if (m_err_mesg==_T("")) m_err_mesg = _T("CKinectWin::startSpeech(): ERROR: Unknown Error!!");
00851 return FALSE;
00852 }
00853
00854 return TRUE;
00855 }
00856
00857
00858
00859 void CKinectWin::stopSpeech(void)
00860 {
00861 if (speech!=NULL) {
00862 speech->stop();
00863 }
00864
00865 if (audio!=NULL) {
00866 audio->stopCapture();
00867 }
00868
00869 return;
00870 }
00871
00872
00873
00874 void CKinectWin::deleteSpeech(BOOL rls)
00875 {
00876 DEBUG_INFO("CKinectWin::deleteSpeech(): START\n");
00877
00878 if (speech!=NULL) {
00879 stopSpeech();
00880
00882 if (rls) speech->free();
00883 delete speech;
00884 speech = NULL;
00885 }
00886
00887
00888 deleteNull(audio);
00889
00890 DEBUG_INFO("CKinectWin::deleteSpeech(): END\n");
00891 return;
00892 }
00893
00894
00895 #endif
00896
00897
00899
00900
00901 Quaternion<float> CKinectWin::getFaceRotation(void)
00902 {
00903 Quaternion<float> quat(1.0, 0.0, 0.0, 0.0, 1.0);
00904 Vector<float> vect;
00905 Vector<float> eulr = device->face->getFaceEulerXYZ();
00906
00907 if (m_is_mirroring) {
00908 vect.set(-eulr.x, -eulr.y, eulr.z);
00909 }
00910 else {
00911 vect.set(-eulr.x, eulr.y, -eulr.z);
00912 }
00913
00914 quat.setEulerYZX(vect);
00915
00916 return quat;
00917 }
00918
00919
00920
00921
00923
00924
00925 void CKinectWin::callback_status_changed(BOOL success, const OLECHAR* instanceName, const OLECHAR* deviceName)
00926 {
00927 if (success) {
00928 DEBUG_INFO("CKinectWin:::called_change_status(): SUCCEEDED: %s %s\n", (char*)instanceName, (char*)deviceName);
00929
00930 }
00931 else {
00932 DEBUG_INFO("CKinectWin::called_change_status(): ERROR: %s %s\n", (char*)instanceName, (char*)deviceName);
00933
00934 }
00935 }
00936
00937
00938
00939
00941
00942
00943 void CALLBACK jbxwl::KinectStatusProc(HRESULT hr, const OLECHAR* instanceName, const OLECHAR* deviceName, void* pUserData)
00944 {
00945 CKinectWin* kinect = (CKinectWin*)pUserData;
00946
00947 if (SUCCEEDED(hr)) {
00948 if (kinect!=NULL) kinect->callback_status_changed(TRUE, instanceName, deviceName);
00949 }
00950 else {
00951 if (kinect!=NULL) kinect->callback_status_changed(FALSE, instanceName, deviceName);
00952 }
00953 }
00954
00955
00956 #endif // ENABLE_KINECT_SDK
00957
00958