KinectController.cpp
Go to the documentation of this file.
00001 #include <XnOpenNI.h>
00002 #include <XnCodecIDs.h>
00003 
00004 #include "KinectController.h"
00005 
00006 #define CHECK_RC(nRetVal, what)                                                                         \
00007         if (nRetVal != XN_STATUS_OK)                                                                    \
00008         {                                                                                                                               \
00009                 printf("%s failed: %s\n", what, xnGetStatusString(nRetVal));\
00010                 return nRetVal;                                                                                         \
00011         }
00012 
00013 xn::Context KinectController::g_Context;
00014 xn::DepthGenerator KinectController::g_DepthGenerator;
00015 xn::UserGenerator KinectController::g_UserGenerator;
00016 
00017 XnBool KinectController::g_bNeedPose = FALSE;
00018 XnChar KinectController::g_strPose[20] = "";
00019 
00020 KinectController::KinectController()
00021 {
00022 }
00023 
00024 // Accessors
00025 xn::UserGenerator& KinectController::getUserGenerator()
00026 {
00027         return g_UserGenerator;
00028 }
00029 
00030 xn::DepthGenerator& KinectController::getDepthGenerator()
00031 {
00032         return g_DepthGenerator;
00033 }
00034 
00035 xn::Context& KinectController::getContext()
00036 {
00037         return g_Context;
00038 }
00039 
00040 // Callback: New user was detected
00041 void XN_CALLBACK_TYPE User_NewUser(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
00042 {
00043         printf("New User %d\n", nId);
00044         // New user found
00045         if (KinectController::g_bNeedPose)
00046         {
00047                 KinectController::g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(KinectController::g_strPose, nId);
00048         }
00049         else
00050         {
00051                 KinectController::g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
00052         }
00053 }
00054 
00055 // Callback: An existing user was lost
00056 void XN_CALLBACK_TYPE User_LostUser(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
00057 {
00058         printf("Lost user %d\n", nId);
00059 }
00060 
00061 // Callback: Detected a pose
00062 void XN_CALLBACK_TYPE UserPose_PoseDetected(xn::PoseDetectionCapability& capability, const XnChar* strPose, XnUserID nId, void* pCookie)
00063 {
00064         printf("Pose %s detected for user %d\n", strPose, nId);
00065         KinectController::g_UserGenerator.GetPoseDetectionCap().StopPoseDetection(nId);
00066         KinectController::g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
00067 }
00068 
00069 // Callback: Started calibration
00070 void XN_CALLBACK_TYPE UserCalibration_CalibrationStart(xn::SkeletonCapability& capability, XnUserID nId, void* pCookie)
00071 {
00072         printf("Calibration started for user %d\n", nId);
00073 }
00074 
00075 // Callback: Finished calibration
00076 void XN_CALLBACK_TYPE UserCalibration_CalibrationEnd(xn::SkeletonCapability& capability, XnUserID nId, XnBool bSuccess, void* pCookie)
00077 {
00078         if (bSuccess)
00079         {
00080                 // Calibration succeeded
00081                 printf("Calibration complete, start tracking user %d\n", nId);
00082                 KinectController::g_UserGenerator.GetSkeletonCap().StartTracking(nId);
00083         }
00084         else
00085         {
00086                 // Calibration failed
00087                 printf("Calibration failed for user %d\n", nId);
00088                 if (KinectController::g_bNeedPose)
00089                 {
00090                         KinectController::g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(KinectController::g_strPose, nId);
00091                 }
00092                 else
00093                 {
00094                         KinectController::g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
00095                 }
00096         }
00097 }
00098 
00099 
00100 int KinectController::init(const char* path, bool recording)
00101 {
00102         XnStatus nRetVal = XN_STATUS_OK;
00103 
00104         if (recording)
00105         {
00106                 nRetVal = g_Context.Init();
00107                 CHECK_RC(nRetVal, "Init");
00108                 nRetVal = g_Context.OpenFileRecording(path);
00109                 if (nRetVal != XN_STATUS_OK)
00110                 {
00111                         printf("Can't open recording %s: %s\n", path, xnGetStatusString(nRetVal));
00112                         return 1;
00113                 }
00114         }
00115         else
00116         {
00117                 nRetVal = g_Context.InitFromXmlFile(path);
00118                 CHECK_RC(nRetVal, "InitFromXml");
00119         }
00120 
00121         nRetVal = g_Context.FindExistingNode(XN_NODE_TYPE_DEPTH, g_DepthGenerator);
00122         CHECK_RC(nRetVal, "Find depth generator");
00123         nRetVal = g_Context.FindExistingNode(XN_NODE_TYPE_USER, g_UserGenerator);
00124         if (nRetVal != XN_STATUS_OK)
00125         {
00126                 nRetVal = g_UserGenerator.Create(g_Context);
00127                 CHECK_RC(nRetVal, "Find user generator");
00128         }
00129 
00130         XnCallbackHandle hUserCallbacks, hCalibrationCallbacks, hPoseCallbacks;
00131         if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_SKELETON))
00132         {
00133                 printf("Supplied user generator doesn't support skeleton\n");
00134                 return 1;
00135         }
00136         g_UserGenerator.RegisterUserCallbacks(User_NewUser, User_LostUser, NULL, hUserCallbacks);
00137         g_UserGenerator.GetSkeletonCap().RegisterCalibrationCallbacks(UserCalibration_CalibrationStart, UserCalibration_CalibrationEnd, NULL, hCalibrationCallbacks);
00138 
00139         if (g_UserGenerator.GetSkeletonCap().NeedPoseForCalibration())
00140         {
00141                 g_bNeedPose = TRUE;
00142                 if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_POSE_DETECTION))
00143                 {
00144                         printf("Pose required, but not supported\n");
00145                         return 1;
00146                 }
00147                 g_UserGenerator.GetPoseDetectionCap().RegisterToPoseCallbacks(UserPose_PoseDetected, NULL, NULL, hPoseCallbacks);
00148                 g_UserGenerator.GetSkeletonCap().GetCalibrationPose(g_strPose);
00149         }
00150 
00151         g_UserGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
00152 
00153         nRetVal = g_Context.StartGeneratingAll();
00154         CHECK_RC(nRetVal, "StartGenerating");
00155   
00156   return 0;
00157 }
00158 
00159 int KinectController::shutdown()
00160 {
00161         g_Context.Shutdown();
00162   return 0;
00163 }


skeleton_markers
Author(s): Patrick Goebel
autogenerated on Fri Jan 3 2014 11:57:20