2#ifndef _CRT_SECURE_NO_WARNINGS
3#define _CRT_SECURE_NO_WARNINGS
24COpenNiWin::COpenNiWin(
void)
35 m_is_tracking = FALSE;
36 m_is_detected = FALSE;
37 m_is_mirroring = TRUE;
46 m_enable_face = FALSE;
47 m_enable_speech = FALSE;
48 m_enable_motor = TRUE;
50 m_use_nite_smth = TRUE;
55 m_profile = XN_SKEL_PROFILE_ALL;
63 isDetectShadow = FALSE;
66 clearAvatarDetected();
71BOOL COpenNiWin::init(
void)
73 BOOL ret = COpenNiTool::init(m_use_image);
80void COpenNiWin::free(
void)
90CString COpenNiWin::get_err_message(
void)
92 CString mesg =
mbs2ts((
char*)COpenNiTool::m_err_mesg.buf);
104void COpenNiWin::clearJointsData(
void)
106 for (
int i=0; i<OPENNI_JOINT_NUM; i++) {
107 rotQuat[i].init(-1.0);
108 posVect[i].init(-1.0);
115BOOL COpenNiWin::initRingBuffer(
void)
119 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
122 if (posRing[j].state<0 || rotRing[j].state<0) {
124 m_err_mesg = _T(
"COpenNiWin::initRingBuffer(): WARNING: Out of Memory.");
135void COpenNiWin::freeRingBuffer(
void)
137 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
138 if (posRing[j].enable) posRing[j].free();
139 if (rotRing[j].enable) rotRing[j].free();
145void COpenNiWin::clearRingBuffer(
void)
147 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
148 if (posRing[j].enable) posRing[j].clear();
149 if (rotRing[j].enable) rotRing[j].clear();
155void COpenNiWin::backup2RingBuffer(
void)
157 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
158 if (posVect[j].c>=m_confidence) posRing[j].put(&posVect[j]);
159 if (rotQuat[j].c>=m_confidence) rotRing[j].put(&rotQuat[j]);
171void COpenNiWin::clearAvatarDetected(
void)
176 startPos = Vector<double>(0.0, 0.0, 0.0);
177 currentPos = Vector<double>(0.0, 0.0, 0.0);
178 m_is_detected = FALSE;
186BOOL COpenNiWin::checkAvatarDetected(
void)
190 m_is_detected = TRUE;
196 m_ground_level = Min(m_ground_level, posVect[
NI_SDK_L_ANKLE].z - startPos.z);
204 return m_is_detected;
209void COpenNiWin::setMirroring(BOOL mirror)
211 if (m_is_mirroring!=mirror) {
212 if (device!=NULL && device->context!=NULL) {
213 device->context->SetGlobalMirror(mirror);
216 m_is_mirroring = mirror;
221void COpenNiWin::setTiltMotor(
int ang)
223 if (m_use_motor && m_enable_motor) {
224 m_enable_motor = FALSE;
226 m_enable_motor = TRUE;
232BOOL COpenNiWin::startDetection(BOOL force_pose)
237 if (m_use_nite_smth) smooth = m_nite_smooth;
238 m_force_pose = force_pose;
240 BOOL ret = start_Detection(force_pose, m_profile, smooth);
242 setLEDColor(NI_LED_BLINK_ORANGE);
245 m_err_mesg = _T(
"COpenNiWin::startDetection(): ERROR: Maybe NITE is not installed.");
253BOOL COpenNiWin::stopDetection(
void)
255 BOOL ret = stop_Detection();
256 setLEDColor(NI_LED_GREEN);
263BOOL COpenNiWin::restartDetection(
void)
265 BOOL ret = stopDetection();
266 if (ret) ret = startDetection(m_force_pose);
275void COpenNiWin::makeDisplayImage()
281 if (pViewData==NULL)
return;
283 if (device->imageMD!=NULL && m_use_image) {
284 src = (uByte*)device->imageMD->RGB24Data();
289 for (
int j=0; j<pViewData->ysize; j++) {
290 int ls = j*m_image_scale*device->m_xsize;
292 for(
int i=0; i<pViewData->xsize; i++) {
293 int li = i*m_image_scale;
294 ptr = &(pViewData->point(i, j));
296 ptr[0] = src[index+2];
297 ptr[1] = src[index+1];
306 for(
int j=0; j<pViewData->ysize; j++) {
307 int ls = j*m_image_scale*device->m_xsize;
309 for(
int i=0; i<pViewData->xsize; i++) {
310 int li = i*m_image_scale;
311 ptr = &(pViewData->point(i, j));
312 ptr[0] = ptr[1] = ptr[2] = 224;
324void COpenNiWin::makeDisplayDepth(
CExView* pview)
326 if (pview==NULL)
return;
329 const XnDepthPixel* src = NULL;
330 if (device->depth!=NULL && hasDepthData) {
331 src = device->depthMD->Data();
333 if (src==NULL)
return;
336 uWord max = pview->
cMax;
337 uWord min = pview->
cMin;
339 uWord* wrk = (uWord*)malloc(
sizeof(uWord)*pDepthData->ysize*pDepthData->xsize);
342 for (
int j=0; j<pDepthData->ysize; j++) {
343 int jj = j*pDepthData->xsize;
344 int ls = j*m_depth_scale*device->m_xsize;
346 for (
int i=0; i<pDepthData->xsize; i++) {
348 int li = i*m_depth_scale;
349 if (!m_is_mirroring) li = device->m_xsize - li - 1;
352 wrk[k] = (uWord)src[ls+li];
353 if (wrk[k]>max) wrk[k] = max;
354 else if (wrk[k]<min) wrk[k] = min;
359 float dif = (float)(max - min);
360 uByte* ptr = (uByte*)pDepthData->grptr;
363 for (
int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
364 ptr[i] = (uByte)((max - wrk[i])/dif*255);
368 for (
int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
369 ptr[i] = (uByte)(255 - wrk[i]/256);
383BOOL COpenNiWin::trackingJoints(BOOL use_rot_data)
389 isDetectShadow = detectShadow();
392 if ((
int)tracking_user>0) {
393 XnBool istracking = skeleton->IsTracking(tracking_user);
394 if (!istracking || !isDetectShadow) {
395 clearAvatarDetected();
396 lostTrackingUser((
int)tracking_user);
397 if (istracking) skeleton->StopTracking(tracking_user);
398 setDenyTrackingSearch(tracking_user);
399 tracking_user = (XnUserID)0;
403 if ((
int)tracking_user==0) {
404 m_is_detected = FALSE;
405 tracking_user = getTrackingUser();
406 if ((
int)tracking_user>0 && (
int)tracking_user<=OPENNI_USERS_NUM) {
408 detectTrackingUser((
int)tracking_user);
415 if ((
int)tracking_user>0 && (
int)tracking_user<=OPENNI_USERS_NUM) {
417 getJointsPosData(tracking_user);
418 if (use_rot_data) getJointsRotData(tracking_user);
436 tracking_user = (XnUserID)0;
437 m_is_detected = FALSE;
441 if (ret!=m_is_tracking) {
443 if (ret) setLEDColor(NI_LED_RED);
444 else setLEDColor(NI_LED_BLINK_ORANGE);
454void COpenNiWin::getJointsPosData(XnUserID uid)
456 get_JointsPositionData(uid);
459 for (
int j=1; j<OPENNI_JOINT_NUM; j++) {
460 XnVector3D pos = jointPosData[j];
461 posVect[j].set(-pos.Z/1000., pos.X/1000., pos.Y/1000., 0.0, jointPosConfidence[j]);
465 if (m_profile==XN_SKEL_PROFILE_ALL) {
469 else if (m_profile==XN_SKEL_PROFILE_UPPER) {
478 if (!m_is_detected) checkAvatarDetected();
482 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
483 posVect[j] = posVect[j] - startPos;
484 if (posVect[j].c<m_confidence || crdVect[j].c<0.0) posVect[j].c = -1.0;
494void COpenNiWin::getJointsRotData(XnUserID uid)
496 get_JointsRotationData(uid);
499 for (
int j=1; j<OPENNI_JOINT_NUM; j++) {
500 XnMatrix3X3 rot = jointRotData[j];
502 double m11 = rot.elements[0];
503 double m12 = rot.elements[1];
504 double m13 = rot.elements[2];
505 double m21 = rot.elements[3];
506 double m31 = rot.elements[6];
507 double m32 = rot.elements[7];
508 double m33 = rot.elements[8];
510 Vector<double> eul = RotMatrixElements2EulerXYZ(m11, m12, m13, m21, m31, m32, m33);
511 Vector<double> vct(-eul.x, -eul.y, eul.z);
512 rotQuat[j].setEulerYZX(vct);
513 rotQuat[j].c = jointRotConfidence[j];
515 if (rotQuat[j].c<m_confidence || crdVect[j].c<0.0) rotQuat[j].c = -1.0;
525BOOL COpenNiWin::detectShadow(
void)
530 if (pViewData==NULL)
return FALSE;;
532 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
533 lbl = (XnLabel*)device->sceneMD->Data();
535 if (lbl==NULL)
return FALSE;
537 for(
int j=0; j<pViewData->ysize; j++) {
538 int ls = j*m_image_scale*device->m_xsize;
540 for(
int i=0; i<pViewData->xsize; i++) {
541 int li = i*m_image_scale;
545 if ((
int)label==(int)tracking_user)
return TRUE;
555void COpenNiWin::paintShadow(
void)
561 if (pViewData==NULL)
return;
563 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
564 lbl = (XnLabel*)device->sceneMD->Data();
566 if (lbl==NULL)
return;
569 for(
int j=0; j<pViewData->ysize; j++) {
570 int ls = j*m_image_scale*device->m_xsize;
572 for(
int i=0; i<pViewData->xsize; i++) {
573 int li = i*m_image_scale;
577 ptr = &(pViewData->point(i, j));
586void COpenNiWin::drawSkeleton(
int col,
int line)
610void COpenNiWin::drawJointConnection(
int j1,
int j2,
int col,
int line)
612 if (pViewData==NULL)
return;
614 MSGraph<unsigned int> vp;
615 vp.xs = pViewData->xsize;
616 vp.ys = pViewData->ysize;
618 vp.gp = (
unsigned int*)pViewData->grptr;
620 if (crdVect[j1].c>0.0 && crdVect[j2].c>0.0) {
621 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y, crdVect[j2].x, crdVect[j2].y, col);
622 for (
int i=1; i<line; i++) {
623 MSGraph_Line(vp, crdVect[j1].x+i, crdVect[j1].y, crdVect[j2].x+i, crdVect[j2].y, col);
624 MSGraph_Line(vp, crdVect[j1].x-i, crdVect[j1].y, crdVect[j2].x-i, crdVect[j2].y, col);
625 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y+i, crdVect[j2].x, crdVect[j2].y+i, col);
626 MSGraph_Line(vp, crdVect[j1].x, crdVect[j1].y-i, crdVect[j2].x, crdVect[j2].y-i, col);
633void COpenNiWin::set2DCoordinate(
void)
635 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
636 crdVect[j].init(-1.0);
640 if (device->depth!=NULL && pViewData!=NULL) {
642 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
647 if (m_profile==XN_SKEL_PROFILE_UPPER) {
655 cnfd = (float)jointPosConfidence[j];
659 if (cnfd>=m_confidence) {
663 if (m_profile==XN_SKEL_PROFILE_UPPER) {
675 pos = jointPosData[j];
678 XnPoint3D pt[1] = {pos};
679 device->depth->ConvertRealWorldToProjective(1, pt, pt);
680 int xs = (int)(pt[0].X/m_image_scale);
681 int ys = (int)(pt[0].Y/m_image_scale);
685 if (xs>0 && xs<pViewData->xsize && ys>0 && ys<pViewData->ysize) {
702BOOL COpenNiWin::initSpeech(
void)
706 if (audio==NULL) audio =
new COpenNiAudio(NULL);
708 BOOL ret = audio->init(NULL);
709 if (!ret) m_err_mesg = _T(
"COpenNiWin::initSpeech(): ERROR: Audio Initialization Error!!");
713 speech = makeSpeech();
714 WAVEFORMATEX format = audio->getAudioFormat();
715 IStream* stream = audio->getIStream();
716 ret = speech->init(stream, &format);
717 if (!ret) m_err_mesg = _T(
"COpenNiWin::initSpeech(): ERROR: Speech Initialization Error!!");
735BOOL COpenNiWin::createSpeech(LPCTSTR lang, LPCTSTR grfile)
740 if (speech==NULL) ret = initSpeech();
743 ret = speech->create(lang);
744 if (!ret) m_err_mesg = _T(
"COpenNiWin::createSpeech(): ERROR: Speech Context Creation Error!! Perhaps, Language Pack is not installed.");
748 ret = speech->load(grfile);
750 m_err_mesg = _T(
"COpenNiWin::createSpeech(): ERROR: Grammar File Loading Error!! File = ");
751 m_err_mesg += grfile;
756 if (!ret)
return FALSE;
762BOOL COpenNiWin::startSpeech(
float confidence)
766 if (audio==NULL || speech==NULL) {
767 m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Audio or Speech Instance is/are NULL!!");
771 BOOL ret = audio->startCapture();
772 if (!ret) m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Audio Capture Starting Error!!");
775 ret = speech->start(confidence);
777 audio->stopCapture();
778 m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Speech Platform Starting Error!!");
783 if (m_err_mesg==_T(
"")) m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Unknown Error!!");
792void COpenNiWin::stopSpeech(
void)
799 audio->stopCapture();
807void COpenNiWin::deleteSpeech(BOOL rls)
809 DEBUG_INFO(
"COpenNiWin::deleteSpeech(): START");
815 if (rls) speech->free();
823 DEBUG_INFO(
"COpenNiWin::deleteSpeech(): END");
void NiSetUserColor(int label, uByte *ptr, BOOL use_image=TRUE)
CString mbs2ts(char *str)
unsigned int NiGetSkeletonColor(int label)