14COpenNiDevice::COpenNiDevice(
void)
45 calibCallbacks = NULL;
51BOOL COpenNiDevice::init(BOOL use_image)
59 BOOL ret = create_Context();
60 if (ret && use_image) create_Image();
61 if (ret) ret = create_Depth();
68BOOL COpenNiDevice::init(
char* fname, BOOL use_image)
73 copy_s2Buffer(
"ERROR: COpenNiDevice::init: File name is NULL", &m_err_mesg);
77 BOOL ret = create_Context(fname);
78 if (ret) ret = create_Player(
TRUE);
80 copy_s2Buffer(
"ERROR: COpenNiDevice::init: create error!!", &m_err_mesg);
84 xn::NodeInfoList nodeList;
85 player->EnumerateNodes(nodeList);
87 for (xn::NodeInfoList::Iterator node=nodeList.Begin(); node!=nodeList.End(); node++) {
88 if ((*node).GetDescription().Type==XN_NODE_TYPE_IMAGE) {
90 ret = create_Image(
TRUE);
94 if ((*node).GetDescription().Type==XN_NODE_TYPE_DEPTH) {
95 ret = create_Depth(
TRUE);
100 if (image!=NULL && use_image) {
101 image->GetMapOutputMode(outputMode);
103 else if (depth!=NULL) {
104 depth->GetMapOutputMode(outputMode);
107 copy_s2Buffer(
"ERROR: COpenNiDevice::init: No Generators", &m_err_mesg);
116void COpenNiDevice::free(
void)
136BOOL COpenNiDevice::create_Context(
char* fname)
139 context =
new xn::Context();
141 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Context: context is NULL", &m_err_mesg);
146 XnStatus rc = context->Init();
147 if (rc==XN_STATUS_OK && fname!=NULL) rc = context->OpenFileRecording(fname);
149 if (rc!=XN_STATUS_OK) {
150 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Context: ", &m_err_mesg);
158BOOL COpenNiDevice::create_Image(BOOL easy)
160 if (!m_has_camera)
return FALSE;
163 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Image: context is NULL", &m_err_mesg);
168 image =
new xn::ImageGenerator();
170 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Image: fail to create image generator", &m_err_mesg);
171 m_has_camera =
FALSE;
176 XnStatus rc = XN_STATUS_ERROR;
178 rc = context->FindExistingNode(XN_NODE_TYPE_IMAGE, *image);
181 rc = image->Create(*context);
182 if (rc==XN_STATUS_OK) rc = image->StartGenerating();
183 if (rc==XN_STATUS_OK) rc = image->SetMapOutputMode(outputMode);
186 if (rc!=XN_STATUS_OK) {
187 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Image: ", &m_err_mesg);
193 imageMD =
new xn::ImageMetaData();
201BOOL COpenNiDevice::create_Depth(BOOL easy)
204 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Depth: context is NULL", &m_err_mesg);
209 depth =
new xn::DepthGenerator();
211 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Depth: fail to create depth generator", &m_err_mesg);
216 XnStatus rc = XN_STATUS_ERROR;
218 rc = context->FindExistingNode(XN_NODE_TYPE_DEPTH, *depth);
221 rc = depth->Create(*context);
222 if (rc==XN_STATUS_OK) rc = depth->StartGenerating();
223 if (rc==XN_STATUS_OK) rc = depth->SetMapOutputMode(outputMode);
224 if (rc==XN_STATUS_OK && image!=NULL) {
225 rc = depth->GetAlternativeViewPointCap().SetViewPoint(*image);
234 if (rc!=XN_STATUS_OK) {
235 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Depth: ", &m_err_mesg);
241 depthMD =
new xn::DepthMetaData();
249BOOL COpenNiDevice::create_User(BOOL easy)
252 copy_s2Buffer(
"ERROR: COpenNiDevice::create_User: context is NULL", &m_err_mesg);
257 user =
new xn::UserGenerator();
259 copy_s2Buffer(
"ERROR: COpenNiDevice::create_User: fail to create user generator", &m_err_mesg);
264 XnStatus rc = XN_STATUS_ERROR;
266 rc = context->FindExistingNode(XN_NODE_TYPE_USER, *user);
269 rc = user->Create(*context);
270 if (rc==XN_STATUS_OK) rc = user->StartGenerating();
273 if (rc!=XN_STATUS_OK) {
274 copy_s2Buffer(
"ERROR: COpenNiDevice::create_User: ", &m_err_mesg);
280 sceneMD =
new xn::SceneMetaData();
285BOOL COpenNiDevice::create_Player(BOOL easy)
288 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Player: context is NULL", &m_err_mesg);
293 player =
new xn::Player();
295 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Player: fail to create player generator", &m_err_mesg);
300 XnStatus rc = XN_STATUS_ERROR;
302 rc = context->FindExistingNode(XN_NODE_TYPE_PLAYER, *player);
305 rc = player->Create(*context,
"oni");
308 if (rc!=XN_STATUS_OK) {
309 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Player: ", &m_err_mesg);
322void COpenNiDevice::delete_Context(
void)
332void COpenNiDevice::delete_Image(
void)
335 if (image->IsValid()) {
336 if (image->IsGenerating()) image->StopGenerating();
341 if (imageMD!=NULL)
delete(imageMD);
348void COpenNiDevice::delete_Depth(
void)
351 if (depth->IsValid()) {
352 if (depth->IsGenerating()) depth->StopGenerating();
357 if (depthMD!=NULL)
delete(depthMD);
364void COpenNiDevice::delete_User(
void)
367 if (user->IsValid()) {
368 if (user->IsGenerating()) user->StopGenerating();
373 if (sceneMD!=NULL)
delete(sceneMD);
380void COpenNiDevice::delete_Player(
void)
394BOOL COpenNiDevice::setup_Tracking(
int profile,
double smooth)
397 copy_s2Buffer(
"ERROR: COpenNiDevice::setup_Tracking: user is NULL", &m_err_mesg);
400 if (!user->IsCapabilitySupported(XN_CAPABILITY_SKELETON)) {
401 copy_s2Buffer(
"ERROR: COpenNiDevice::setup_Tracking: not support skeleton", &m_err_mesg);
405 if (skeleton==NULL) {
406 skeleton =
new xn::SkeletonCapability(*user);
407 if (skeleton==NULL) {
408 copy_s2Buffer(
"ERROR: COpenNiDevice::setup_Tracking: fail to make skeleton capability", &m_err_mesg);
414 skeleton->SetSkeletonProfile((XnSkeletonProfile)profile);
415 skeleton->SetSmoothing((XnFloat)smooth);
426void COpenNiDevice::clear_Skeleton(
void)
428 if (skeleton!=NULL)
delete(skeleton);
433void COpenNiDevice::clear_Tracking(
void)
437 user->GetUsers(dUsers, nUsers);
438 for (
int i=0; i<nUsers; i++) {
439 if (skeleton!=NULL && skeleton->IsTracking(dUsers[i])) {
440 skeleton->StopTracking(dUsers[i]);
451BOOL COpenNiDevice::create_Recorder(
char* file_name, BOOL use_image)
454 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Recorder: context is NULL", &m_err_mesg);
458 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Recorder: depth is NULL", &m_err_mesg);
462 recorder =
new xn::Recorder();
463 rc = recorder->Create(*context);
464 if (rc==XN_STATUS_OK) rc = recorder->SetDestination(XN_RECORD_MEDIUM_FILE, file_name);
465 if (rc==XN_STATUS_OK && use_image && image!=NULL) rc = recorder->AddNodeToRecording(*image, XN_CODEC_JPEG);
466 if (rc==XN_STATUS_OK) rc = recorder->AddNodeToRecording(*depth, XN_CODEC_16Z);
468 if (rc!=XN_STATUS_OK) {
469 copy_s2Buffer(
"ERROR: COpenNiDevice::create_Recorder: ", &m_err_mesg);
478void COpenNiDevice::delete_Recorder(
void)
480 if (recorder!=NULL) {
481 int prev_state = m_state;
485 if (depth!=NULL) recorder->RemoveNodeFromRecording(*depth);
486 if (image!=NULL) recorder->RemoveNodeFromRecording(*image);
491 m_state = prev_state;
498BOOL COpenNiDevice::start_Recorde(
char* file_name, BOOL use_image)
503 int prev_state = m_state;
507 ret = create_Recorder(file_name, use_image);
509 rc = recorder->Record();
510 if (rc!=XN_STATUS_OK) {
511 copy_s2Buffer(
"ERROR: COpenNiDevice::start_Recorde: ", &m_err_mesg);
517 m_state = prev_state;
527void COpenNiDevice::stop_Recorde(
void)
537BOOL COpenNiDevice::open_USB_Device(
void)
540 if (rc!=XN_STATUS_OK && rc!=XN_STATUS_USB_ALREADY_INIT)
return FALSE;
545 if (rc!=XN_STATUS_OK) {
550 rc = ::xnUSBOpenDeviceByPath(*devPath, &usb_dev);
551 if (rc!=XN_STATUS_OK) {
552 ::xnUSBFreeDevicesList(devPath);
556 rc = ::xnUSBOpenDevice(OPENNI_VID_MICROSOFT, OPENNI_PID_NUI_MOTOR, NULL, NULL, &usb_dev);
557 if (rc!=XN_STATUS_OK) {
567void COpenNiDevice::close_USB_Device(
void)
570 ::xnUSBCloseDevice(usb_dev);
576 ::xnUSBFreeDevicesList(devPath);
585void COpenNiDevice::set_LED_Color(
int col)
587 if (usb_dev==NULL)
return;
589 ::xnUSBSendControl(usb_dev, XN_USB_CONTROL_TYPE_VENDOR, 0x06, (XnUInt16)col, 0, NULL, 0, 0);
595void COpenNiDevice::set_Tilt_Motor(
int ang)
597 if (usb_dev==NULL)
return;
600 ::xnUSBSendControl(usb_dev, XN_USB_CONTROL_TYPE_VENDOR, 0x31, (XnUInt16)(ang*2), 0, NULL, 0, 0);
612BOOL COpenNiDevice::setup_CallBacks()
614 BOOL ret = set_UserCallbacks (&userDetect, &userLost,
this);
615 if (ret) ret = set_CalibCallbacks(&calibStart, &calibEnd,
this);
616 if (!ret)
return FALSE;
622void COpenNiDevice::clear_CallBacks()
624 unset_UserCallbacks();
625 unset_CalibCallbacks();
629BOOL COpenNiDevice::set_UserCallbacks(xn::UserGenerator::UserHandler newUser, xn::UserGenerator::UserHandler lostUser,
void* cookie)
632 copy_s2Buffer(
"ERROR: COpenNiDevice::set_UserCallbacks: user is NULL", &m_err_mesg);
636 rc = user->RegisterUserCallbacks(newUser, lostUser, cookie, userCallbacks);
637 if (rc!=XN_STATUS_OK) {
638 copy_s2Buffer(
"ERROR: COpenNiDevice::set_UserCallbacks: ", &m_err_mesg);
640 unset_UserCallbacks();
647BOOL COpenNiDevice::set_CalibCallbacks(xn::SkeletonCapability::CalibrationStart calibStart, xn::SkeletonCapability::CalibrationEnd calibEnd,
void* cookie)
649 if (skeleton==NULL) {
650 copy_s2Buffer(
"ERROR: COpenNiDevice::set_CalibCallbacks: skeleton is NULL", &m_err_mesg);
654 rc = skeleton->RegisterCalibrationCallbacks(calibStart, calibEnd, cookie, calibCallbacks);
655 if (rc!=XN_STATUS_OK) {
656 copy_s2Buffer(
"ERROR: COpenNiDevice::set_CalibCallbacks: ", &m_err_mesg);
658 unset_CalibCallbacks();
665void COpenNiDevice::unset_UserCallbacks()
667 if (user!=NULL && userCallbacks!=NULL) {
668 user->UnregisterUserCallbacks(userCallbacks);
669 userCallbacks = NULL;
675void COpenNiDevice::unset_CalibCallbacks()
677 if (skeleton!=NULL && calibCallbacks!=NULL) {
678 skeleton->UnregisterCalibrationCallbacks(calibCallbacks);
679 calibCallbacks = NULL;
690void XN_CALLBACK_TYPE jbxl::userDetect(xn::UserGenerator& user,
unsigned int nId,
void* cookie)
692 DEBUG_INFO(
"INFO: jbxl::userDetect(): User is detected. (%d)", nId);
693 if (cookie==NULL)
return;
695 user.GetSkeletonCap().RequestCalibration(nId,
TRUE);
697 DEBUG_INFO(
"INFO: jbxl::userDetect(): Start Pose Detection. (%d)", nId);
701void XN_CALLBACK_TYPE jbxl::userLost(xn::UserGenerator& user,
unsigned int nId,
void* cookie)
703 DEBUG_INFO(
"INFO: jbxl::userLost(): Lost user. (%d)", nId);
705 if (user.GetSkeletonCap().IsValid()) {
706 if (user.GetSkeletonCap().IsTracking(nId)) {
707 user.GetSkeletonCap().StopTracking(nId);
708 DEBUG_INFO(
"INFO: jbxl::userLost(): Stop Tracking. (%d)", nId);
716void XN_CALLBACK_TYPE jbxl::calibStart(xn::SkeletonCapability& skeleton,
unsigned int nId,
void* cookie)
718 DEBUG_INFO(
"INFO: jbxl::calibStart(): Start Calibration. (%d)", nId);
722void XN_CALLBACK_TYPE jbxl::calibEnd(xn::SkeletonCapability& skeleton,
unsigned int nId, XnBool success,
void* cookie)
724 DEBUG_INFO(
"INFO: jbxl::calibEnd(): End Calibration. (%d)", nId);
725 if (cookie==NULL)
return;
728 xn::UserGenerator* user = ((COpenNiDevice*)cookie)->user;
732 user->GetSkeletonCap().StartTracking(nId);
733 DEBUG_INFO(
"INFO: jbxl::calibEnd(): Start Tracking. (%d)", nId);
#define NI_STATE_SAVE_WORKING
#define NI_WORKING_WAIT_TIME
#define OPENNI_DEPTH_YSIZE
#define NI_STATE_DETECT_STOPPED
#define OPENNI_DEPTH_XSIZE
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
#define copy_s2Buffer(src, dst)
copy_b2Buffer()
#define cat_s2Buffer(src, dst)
cat_b2Buffer()