2#ifndef _CRT_SECURE_NO_WARNINGS
3#define _CRT_SECURE_NO_WARNINGS
17#if defined(ENABLE_OPENNI) || defined(ENABLE_OPENNI2)
34 m_is_tracking = FALSE;
35 m_is_detected = FALSE;
36 m_is_mirroring = TRUE;
45 m_enable_face = FALSE;
46 m_enable_speech = FALSE;
47 m_enable_motor = TRUE;
49 m_use_nite_smth = TRUE;
54 m_profile = XN_SKEL_PROFILE_ALL;
62 isDetectShadow = FALSE;
65 clearAvatarDetected();
70BOOL COpenNiWin::init(
void)
72 BOOL ret = COpenNiTool::init(m_use_image);
79void COpenNiWin::free(
void)
88CString COpenNiWin::get_err_message(
void)
90 CString mesg =
mbs2ts((
char*)COpenNiTool::m_err_mesg.buf);
102void COpenNiWin::clearJointsData(
void)
104 for (
int i=0; i<OPENNI_JOINT_NUM; i++) {
105 rotQuat[i].init(-1.0);
106 posVect[i].init(-1.0);
107 crdVect[i].init(-1.0);
116BOOL COpenNiWin::initRingBuffer(
void)
120 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
123 if (posRing[j].state<0 || rotRing[j].state<0) {
125 m_err_mesg = _T(
"COpenNiWin::initRingBuffer(): WARNING: Out of Memory.");
136void COpenNiWin::freeRingBuffer(
void)
138 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
139 if (posRing[j].enable) posRing[j].free();
140 if (rotRing[j].enable) rotRing[j].free();
146void COpenNiWin::clearRingBuffer(
void)
148 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
149 if (posRing[j].enable) posRing[j].clear();
150 if (rotRing[j].enable) rotRing[j].clear();
156void COpenNiWin::backup2RingBuffer(
void)
158 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
159 if (posVect[j].c>=m_confidence) posRing[j].put(&posVect[j]);
160 if (rotQuat[j].c>=m_confidence) rotRing[j].put(&rotQuat[j]);
172void COpenNiWin::clearAvatarDetected(
void)
177 startPos = Vector<float>(0.0, 0.0, 0.0);
178 currentPos = Vector<float>(0.0, 0.0, 0.0);
179 m_is_detected = FALSE;
187BOOL COpenNiWin::checkAvatarDetected(
void)
191 m_is_detected = TRUE;
197 m_ground_level = Min(m_ground_level, posVect[
NI_SDK_L_ANKLE].z - startPos.z);
205 return m_is_detected;
210void COpenNiWin::setMirroring(BOOL mirror)
213 setGlobalMirror(mirror);
214 m_is_mirroring = mirror;
219void COpenNiWin::setTiltMotor(
int ang)
221 if (m_use_motor && m_enable_motor) {
222 m_enable_motor = FALSE;
224 m_enable_motor = TRUE;
230BOOL COpenNiWin::startDetection(
void)
235 if (m_use_nite_smth) smooth = m_nite_smooth;
240 BOOL ret = start_Detection(m_profile, smooth);
242 setLEDColor(NI_LED_BLINK_ORANGE);
245 m_err_mesg = _T(
"COpenNiWin::startDetection(): ERROR: Maybe NITE is not installed.");
248 DEBUG_INFO(
"COpenNiWin::startDetection(): started Detection.");
254BOOL COpenNiWin::stopDetection(
void)
256 BOOL ret = stop_Detection();
257 setLEDColor(NI_LED_GREEN);
264BOOL COpenNiWin::restartDetection(
void)
266 BOOL ret = stopDetection();
267 if (ret) ret = startDetection();
276void COpenNiWin::makeDisplayImage()
282 if (pViewData==NULL)
return;
284 if (m_use_image) src = getImageData();
287 for (
int j=0; j<pViewData->ysize; j++) {
288 int ls = j*m_image_scale*getXSize();
290 for(
int i=0; i<pViewData->xsize; i++) {
291 int li = i*m_image_scale;
293 if (!m_is_mirroring) {
294 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
295 li = getXSize() - li - 1;
299 ptr = &(pViewData->point(i, j));
301 ptr[0] = src[index+2];
302 ptr[1] = src[index+1];
311 for(
int j=0; j<pViewData->ysize; j++) {
312 int ls = j*m_image_scale*getXSize();
314 for(
int i=0; i<pViewData->xsize; i++) {
315 int li = i*m_image_scale;
316 ptr = &(pViewData->point(i, j));
317 ptr[0] = ptr[1] = ptr[2] = 224;
329void COpenNiWin::makeDisplayDepth(
CExView* pview)
331 if (pview==NULL)
return;
336 src = (uWord*)getDepthData();
338 if (src==NULL)
return;
341 uWord max = pview->
cMax;
342 uWord min = pview->
cMin;
344 uWord* wrk = (uWord*)malloc(
sizeof(uWord)*pDepthData->ysize*pDepthData->xsize);
347 for (
int j=0; j<pDepthData->ysize; j++) {
348 int jj = j*pDepthData->xsize;
349 int ls = j*m_depth_scale*getXSize();
351 for (
int i=0; i<pDepthData->xsize; i++) {
352 int li = i*m_depth_scale;
354 if (!m_is_mirroring) {
355 if (m_library==NiSDK_OpenNI || getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
356 li = getXSize() - li - 1;
362 if (wrk[k]>max) wrk[k] = max;
363 else if (wrk[k]<min) wrk[k] = min;
368 float dif = (float)(max - min);
369 uByte* ptr = (uByte*)pDepthData->grptr;
372 for (
int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
373 ptr[i] = (uByte)((max - wrk[i])/dif*255);
377 for (
int i=0; i<pDepthData->ysize*pDepthData->xsize; i++) {
378 ptr[i] = (uByte)(255 - wrk[i]/256);
392BOOL COpenNiWin::trackingJoints(BOOL use_rot_data)
398 if (m_library==NiSDK_OpenNI) isDetectShadow = detectShadow();
399 else isDetectShadow = TRUE;
402 if (tracking_user>0) {
403 BOOL istracking = isTracking(tracking_user);
404 if (!istracking || !isDetectShadow) {
406 clearAvatarDetected();
407 lostTrackingUser((
int)tracking_user);
408 if (istracking) stopTracking(tracking_user);
409 setDenyTrackingSearch(tracking_user);
414 if ((
int)tracking_user==0) {
415 m_is_detected = FALSE;
416 tracking_user = getTrackingUser();
417 if ((
int)tracking_user>0 && (
int)tracking_user<=OPENNI_USERS_NUM) {
420 detectTrackingUser((
int)tracking_user);
427 if ((
int)tracking_user>0 && (
int)tracking_user<=OPENNI_USERS_NUM) {
429 getJointsPosData(tracking_user);
430 if (use_rot_data) getJointsRotData(tracking_user);
452 m_is_detected = FALSE;
456 if (ret!=m_is_tracking) {
458 if (ret) setLEDColor(NI_LED_RED);
459 else setLEDColor(NI_LED_BLINK_ORANGE);
469void COpenNiWin::getJointsPosData(
unsigned int uid)
471 get_JointsPositionData(uid);
474 for (
int j=1; j<OPENNI_JOINT_NUM; j++) {
475 XnVector3D pos = jointPosData[j];
476 posVect[j].set(-pos.Z/1000., pos.X/1000., pos.Y/1000., 0.0, jointPosConfidence[j]);
480 if (m_profile==XN_SKEL_PROFILE_ALL) {
484 else if (m_profile==XN_SKEL_PROFILE_UPPER) {
493 if (!m_is_detected) checkAvatarDetected();
497 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
498 posVect[j] = posVect[j] - startPos;
499 if (posVect[j].c<m_confidence || crdVect[j].c<0.0) posVect[j].c = -1.0;
509void COpenNiWin::getJointsRotData(
unsigned int uid)
542BOOL COpenNiWin::detectShadow(
void)
545 unsigned short label = 0;
546 unsigned short* lbl = NULL;
548 if (pViewData==NULL)
return FALSE;;
550 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
551 lbl = (
unsigned short*)getSceneData();
553 if (lbl==NULL)
return FALSE;
555 for(
int j=0; j<pViewData->ysize; j++) {
556 int ls = j*m_image_scale*getXSize();
558 for(
int i=0; i<pViewData->xsize; i++) {
559 int li = i*m_image_scale;
563 if ((
int)label==(
int)tracking_user)
return TRUE;
573void COpenNiWin::paintShadow(
void)
575 unsigned short label = 0;
576 unsigned short* lbl = NULL;
577 unsigned short* ldp = NULL;
583 if (pViewData==NULL)
return;
585 if (getDevState()==NI_STATE_DETECT_EXEC && hasUserData) {
586 lbl = (
unsigned short*)getSceneData();
587 ldp = (
unsigned short*)getDepthData();
589 if (lbl==NULL && ldp==NULL)
return;
592 for(
int j=0; j<pViewData->ysize; j++) {
593 int lj = j*m_image_scale;
594 int ls = lj*getXSize();
596 for(
int i=0; i<pViewData->xsize; i++) {
597 int li = i*m_image_scale;
599 if (!m_is_mirroring) {
600 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
601 li = getXSize() - li - 1;
607 ptr = &(pViewData->point(i, j));
625void COpenNiWin::drawSkeleton(
int col,
int line)
649void COpenNiWin::drawJointConnection(
int j1,
int j2,
int col,
int line)
651 if (pViewData==NULL)
return;
653 MSGraph<unsigned int> vp;
654 vp.xs = pViewData->xsize;
655 vp.ys = pViewData->ysize;
657 vp.gp = (
unsigned int*)pViewData->grptr;
659 if (crdVect[j1].c>0.0 && crdVect[j2].c>0.0) {
660 int xs = crdVect[j1].x;
661 int xe = crdVect[j2].x;
663 if (!m_is_mirroring) {
664 if (getEnuDevice()==EnuDev_Kinect || getEnuDevice()==EnuDev_OniFile) {
670 MSGraph_Line(vp, xs, crdVect[j1].y, xe, crdVect[j2].y, col);
671 for (
int i=1; i<line; i++) {
672 MSGraph_Line(vp, xs+i, crdVect[j1].y, xe+i, crdVect[j2].y, col);
673 MSGraph_Line(vp, xs-i, crdVect[j1].y, xe-i, crdVect[j2].y, col);
674 MSGraph_Line(vp, xs, crdVect[j1].y+i, xe, crdVect[j2].y+i, col);
675 MSGraph_Line(vp, xs, crdVect[j1].y-i, xe, crdVect[j2].y-i, col);
682void COpenNiWin::set2DCoordinate(
void)
684 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
685 crdVect[j].init(-1.0);
689 if (hasDepthGen() && pViewData!=NULL) {
691 for (
int j=0; j<OPENNI_JOINT_NUM; j++) {
696 if (m_profile==XN_SKEL_PROFILE_UPPER) {
704 cnfd = (float)jointPosConfidence[j];
708 if (cnfd>=m_confidence) {
712 if (m_profile==XN_SKEL_PROFILE_UPPER) {
724 pos = jointPosData[j];
727 XnPoint3D pt[1] = {pos};
728 convert2Projective(1, pt, pt);
729 int xs = (int)(pt[0].X/m_image_scale);
730 int ys = (int)(pt[0].Y/m_image_scale);
734 if (xs>0 && xs<pViewData->xsize && ys>0 && ys<pViewData->ysize) {
751BOOL COpenNiWin::initSpeech(
void)
755 if (audio==NULL) audio =
new COpenNiAudio(NULL);
757 BOOL ret = audio->init(NULL);
758 if (!ret) m_err_mesg = _T(
"COpenNiWin::initSpeech(): ERROR: Audio Initialization Error!!");
762 speech = makeSpeech();
763 WAVEFORMATEX format = audio->getAudioFormat();
764 IStream* stream = audio->getIStream();
765 ret = speech->init(stream, &format);
766 if (!ret) m_err_mesg = _T(
"COpenNiWin::initSpeech(): ERROR: Speech Initialization Error!!");
784BOOL COpenNiWin::createSpeech(LPCTSTR lang, LPCTSTR grfile)
789 if (speech==NULL) ret = initSpeech();
792 ret = speech->create(lang);
793 if (!ret) m_err_mesg = _T(
"COpenNiWin::createSpeech(): ERROR: Speech Context Creation Error!! Perhaps, Language Pack is not installed.");
797 ret = speech->load(grfile);
799 m_err_mesg = _T(
"COpenNiWin::createSpeech(): ERROR: Grammar File Loading Error!! File = ");
800 m_err_mesg += grfile;
805 if (!ret)
return FALSE;
811BOOL COpenNiWin::startSpeech(
float confidence)
815 if (audio==NULL || speech==NULL) {
816 m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Audio or Speech Instance is/are NULL!!");
820 BOOL ret = audio->startCapture();
821 if (!ret) m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Audio Capture Starting Error!!");
824 ret = speech->start(confidence);
826 audio->stopCapture();
827 m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Speech Platform Starting Error!!");
832 if (m_err_mesg==_T(
"")) m_err_mesg = _T(
"COpenNiWin::startSpeech(): ERROR: Unknown Error!!");
841void COpenNiWin::stopSpeech(
void)
848 audio->stopCapture();
856void COpenNiWin::deleteSpeech(BOOL rls)
858 DEBUG_INFO(
"COpenNiWin::deleteSpeech(): START");
864 if (rls) speech->free();
872 DEBUG_INFO(
"COpenNiWin::deleteSpeech(): END");
void NiSetUserColor(int label, uByte *ptr, BOOL use_image=TRUE)
CString mbs2ts(char *str)
unsigned int NiGetSkeletonColor(int label)