00001 
00009 #include  "OpenNiTool.h"
00010 #include  "tools++.h"
00011 
00012 
00013 using namespace jbxl;
00014 
00015 
00016 #ifdef  ENABLE_OPENNI
00017 
00018 
00019 COpenNiTool::COpenNiTool(void)
00020 {
00021     device     = NULL;
00022     dev_backup = NULL;
00023 
00024     tracking_user = 0;
00025     tracking_deny = 0;
00026 
00027     m_err_mesg = make_Buffer(LMESG);
00028 
00029     clear_JointsData();
00030 }
00031 
00032 
00033 
00034 BOOL  COpenNiTool::init(BOOL use_image)
00035 {
00036     device = new COpenNiDevice();
00037     BOOL ret = device->init(use_image);
00038     if (ret) {
00039         
00040         if (device->context!=NULL) device->context->SetGlobalMirror(TRUE);
00041     }
00042     else {
00043         copy_Buffer(&device->m_err_mesg, &m_err_mesg);
00044     }
00045 
00046     
00047     device->m_state = NI_STATE_DETECT_STOPPED;
00048 
00049     return ret;
00050 }
00051 
00052 
00053 
00054 void  COpenNiTool::free(void)
00055 {
00056     delete_Device();
00057 
00058     free_Buffer(&m_err_mesg);
00059 }
00060 
00061 
00062 
00063 void  COpenNiTool::delete_Device(void)
00064 {
00065     if (device!=NULL) {
00066         delete(device);
00067         device = NULL;
00068     }
00069     return;
00070 }
00071 
00072 
00073 
00074 void  COpenNiTool::clear_JointsData(void)
00075 {
00076     clear_JointsPosData();
00077     clear_JointsRotData();
00078 }
00079 
00080 
00081 
00082 void  COpenNiTool::clear_JointsPosData(void)
00083 {
00084     memset(jointPosData, 0, sizeof(XnVector3D) *OPENNI_JOINT_NUM);
00085     memset(jointPosConfidence, 0, sizeof(double)*OPENNI_JOINT_NUM);
00086 }
00087 
00088 
00089 
00090 void  COpenNiTool::clear_JointsRotData(void)
00091 {
00092     memset(jointRotData, 0, sizeof(XnMatrix3X3)*OPENNI_JOINT_NUM);
00093     memset(jointRotConfidence, 0, sizeof(double)*OPENNI_JOINT_NUM);
00094 }
00095 
00096 
00097 
00098 
00100 
00101 
00102 void  COpenNiTool::get_JointsPositionData(unsigned int nId)
00103 {
00104     XnSkeletonJointPosition  joint;
00105     
00106 
00107     if (device->skeleton!=NULL) {
00108         for (int j=1; j<OPENNI_JOINT_NUM; j++) {
00109             device->skeleton->GetSkeletonJointPosition(nId, (XnSkeletonJoint)j, joint);
00110             jointPosData[j] = joint.position;
00111             jointPosConfidence[j] = joint.fConfidence;
00112         }
00113     }
00114 }
00115 
00116 
00117 
00118 void  COpenNiTool::get_JointsRotationData(unsigned int nId)
00119 {
00120     XnSkeletonJointOrientation  joint;
00121     
00122 
00123     if (device->skeleton!=NULL) {
00124         for (int j=1; j<OPENNI_JOINT_NUM; j++) {
00125             device->skeleton->GetSkeletonJointOrientation(nId, (XnSkeletonJoint)j, joint);
00126             jointRotData[j] = joint.orientation;
00127             jointRotConfidence[j] = joint.fConfidence;
00128         }
00129     }
00130 }
00131 
00132 
00133 
00134 XnVector3D  COpenNiTool::joint_PositionData(int j)
00135 { 
00136     XnVector3D vect;
00137 
00138     if (j>=0 && j<OPENNI_JOINT_NUM) {
00139         vect = jointPosData[j];
00140     }
00141     else {
00142         memset(&vect, 0, sizeof(XnVector3D));
00143     }
00144 
00145     return vect;
00146 }
00147 
00148 
00149 
00150 XnMatrix3X3  COpenNiTool::joint_RotationData(int j)
00151 {
00152     XnMatrix3X3 mtrx;
00153 
00154     if (j>=0 && j<OPENNI_JOINT_NUM) {
00155         mtrx = jointRotData[j];
00156     }
00157     else {
00158         memset(&mtrx, 0, sizeof(XnMatrix3X3));
00159     }
00160 
00161     return mtrx;
00162 }
00163 
00164 
00165 
00166 double  COpenNiTool::joint_PositionConfidence(int j)
00167 { 
00168     double cnfd = 0.0;
00169 
00170     if (j>=0 && j<OPENNI_JOINT_NUM) {
00171         cnfd = (double)jointPosConfidence[j];
00172     }
00173 
00174     return cnfd;
00175 }
00176 
00177 
00178 
00179 double  COpenNiTool::joint_RotationConfidence(int j)
00180 { 
00181     double cnfd = 0.0;
00182 
00183     if (j>=0 && j<OPENNI_JOINT_NUM) {
00184         cnfd = (double)jointRotConfidence[j];
00185     }
00186 
00187     return cnfd;
00188 }
00189 
00190 
00191 
00192 
00194 
00195 
00196 BOOL  COpenNiTool::start_Detection(int profile, double smooth)
00197 {
00198     if (device->m_state==NI_STATE_DETECT_EXEC) {
00199         copy_s2Buffer("COpenNiTool:start_Detection WARNING: detection is already executed", &m_err_mesg);
00200         return FALSE;
00201     }
00202 
00203     device->m_state = NI_STATE_DETECT_STARTING;
00204     clear_JointsData();
00205     tracking_user = 0;
00206     tracking_deny = 0;
00207 
00208     BOOL     ret = device->create_User();
00209     if (ret) ret = device->setup_Tracking(profile, smooth);
00210     if (ret) ret = device->setup_CallBacks();
00211 
00212     if (!ret) {
00213         device->m_state = NI_STATE_DETECT_STOPPING;
00214         device->clear_Skeleton();
00215         device->delete_User();
00216         device->m_state = NI_STATE_DETECT_STOPPED;
00217         copy_s2Buffer("COpenNiTool:start_Detection ERROR: Myabe NITE is not installed!!", &m_err_mesg);
00218         return FALSE;
00219     }
00220 
00221     device->m_state = NI_STATE_DETECT_EXEC;
00222 
00223     return TRUE;
00224 }
00225 
00226 
00227 
00228 BOOL  COpenNiTool::stop_Detection(void)
00229 {
00230     if (device->m_state==NI_STATE_DETECT_STOPPED) {
00231         copy_s2Buffer("COpenNiTool:stop_Detection WARNING: detection is already stopped", &m_err_mesg);
00232         return FALSE;
00233     }
00234 
00235     device->m_state = NI_STATE_DETECT_STOPPING;
00236     Sleep(NI_STOP_WAIT_TIME);
00237 
00238     device->clear_Tracking();
00239     device->clear_CallBacks();
00240     device->clear_Skeleton();
00241     device->delete_User();
00242 
00243     tracking_user = (unsigned int)0;
00244     tracking_deny = (unsigned int)0;
00245 
00246     device->m_state = NI_STATE_DETECT_STOPPED;
00247     
00248     return TRUE;
00249 }
00250 
00251 
00252 
00253 unsigned int  COpenNiTool::get_TrackingUser(void)
00254 {
00255     XnUserID id = (unsigned int)0;
00256 
00257     if (device->user!=NULL && device->skeleton!=NULL) {
00258         device->nUsers = OPENNI_USERS_NUM;
00259         device->user->GetUsers(device->dUsers, device->nUsers);
00260 
00261         int start = 0;
00262         if (tracking_deny!=0) {
00263             for (int i=0; i<device->nUsers; i++) {
00264                 if (tracking_deny==device->dUsers[i]) {
00265                     start = i + 1;
00266                     break;
00267                 }
00268             }
00269         }
00270         
00271         for (int i=0; i<device->nUsers; i++) {
00272             int idx = (start + i) % device->nUsers;
00273             if (device->skeleton->IsTracking(device->dUsers[idx])) {
00274                 id = device->dUsers[idx];
00275                 break;
00276             }
00277         }
00278     }
00279 
00280     return id;
00281 }
00282 
00283 
00284 
00285 void  COpenNiTool::set_DenyTrackingSearch(unsigned int user)
00286 {
00287     if ((int)user>0) tracking_deny = user;
00288     else             tracking_deny = 0;
00289 
00290     return;
00291 }
00292 
00293 
00294 
00295 
00297 
00298 BOOL  COpenNiTool::backupDevice(void)
00299 {
00300     dev_backup = device;
00301     device = new COpenNiDevice();
00302     
00303     if (device==NULL) {
00304         device = dev_backup;
00305         dev_backup = NULL;
00306         return FALSE;
00307     }
00308     return TRUE;
00309 }
00310 
00311 
00312 
00313 BOOL  COpenNiTool::restoreDevice(void)
00314 {
00315     if (dev_backup==NULL) return FALSE;
00316     
00317     delete(device);
00318     device = dev_backup;
00319     dev_backup = NULL;
00320     return TRUE;
00321 }
00322 
00323 
00324 
00325 #endif      // ifdef ENABLE_OPENNI