00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #include "User.h"
00035
00036 #include "../core/Exception.h"
00037 #include "../DeviceManager.h"
00038 #include "../objects/KinectDevice.h"
00039
00040 #include "../ScolPlugin.h"
00041
00042 void XN_CALLBACK_TYPE NewUser(xn::UserGenerator& generator, const XnUserID nUserId, void* pCookie)
00043 {
00044 NIDevice::Objects::KinectDevice* This = (NIDevice::Objects::KinectDevice*)pCookie;
00045 NIDevice::Objects::KinectUser* user = 0;
00046 boost::mutex::scoped_lock lock(This->m_mutex);
00047
00048
00049 xn::SceneMetaData udata;
00050 This->GetUserGenerator()->GetGenerator().GetUserPixels(nUserId, udata);
00051 const unsigned short* ubuf = udata.Data();
00052 const unsigned short* dbuf = This->GetDepthGenerator()->GetDepthBuffer(false);
00053
00054 int ucnt = 0;
00055 for (int i=0; i < (udata.XRes() * udata.YRes()); i++)
00056 {
00057 if ((ubuf[i] * dbuf[i]) > 0)
00058 ucnt++;
00059 }
00060
00061 user = This->GetUser(-1);
00062
00063 if(user && (ucnt > 0))
00064 {
00065 user->SetId(nUserId);
00066 generator.GetPoseDetectionCap().StartPoseDetection("Psi", nUserId);
00067 PostMessage(HScol, KINECT_NEW_USER_CB, (int)user, 0);
00068 }
00069 else
00070 {
00071 This->AddExistingId(nUserId);
00072 }
00073 }
00074
00075 void XN_CALLBACK_TYPE LostUser(xn::UserGenerator& generator, const XnUserID nUserId, void* pCookie)
00076 {
00077 NIDevice::Objects::KinectDevice* This = (NIDevice::Objects::KinectDevice*)pCookie;
00078 NIDevice::Objects::KinectUser* user = 0;
00079 boost::mutex::scoped_lock lock(This->m_mutex);
00080
00081 user = This->GetUser(nUserId);
00082 int ucnt = 0;
00083
00084 if(user)
00085 {
00086 int uavailable = This->GetExistingAvailableId();
00087
00088
00089
00090 if (uavailable != -1)
00091 {
00092 xn::SceneMetaData udata;
00093 This->GetUserGenerator()->GetGenerator().GetUserPixels(uavailable, udata);
00094 const unsigned short* ubuf = udata.Data();
00095 const unsigned short* dbuf = This->GetDepthGenerator()->GetDepthBuffer(false);
00096
00097 for (int i=0; i < (udata.XRes() * udata.YRes()); i++)
00098 {
00099 if ((ubuf[i] * dbuf[i]) > 0)
00100 ucnt++;
00101 }
00102
00103 if (ucnt > 0)
00104 user->SetId(uavailable);
00105 else
00106 {
00107 This->AddExistingId(uavailable);
00108 user->SetId(-1);
00109 }
00110 }
00111 else
00112 user->SetId(-1);
00113
00114 PostMessage(HScol, KINECT_LOST_USER_CB, (int)user, 0);
00115
00116 if ((uavailable != -1) && (ucnt > 0))
00117 {
00118 generator.GetPoseDetectionCap().StartPoseDetection("Psi", uavailable);
00119 PostMessage(HScol, KINECT_NEW_USER_CB, (int)user, 0);
00120 }
00121 }
00122
00123 This->RemoveExistingId(nUserId);
00124 }
00125
00126 void XN_CALLBACK_TYPE CalibrationStart(xn::SkeletonCapability& skeleton, const XnUserID nUserId, void* pCookie)
00127 {
00128 NIDevice::Objects::KinectDevice* device = (NIDevice::Objects::KinectDevice*)pCookie;
00129 NIDevice::Objects::KinectUser* user = 0;
00130 user = device->GetUser(nUserId);
00131 if(user)
00132 {
00133 PostMessage(HScol, KINECT_USER_CALIBRATION_START_CB, (int)user, 0);
00134 }
00135 }
00136
00137 void XN_CALLBACK_TYPE CalibrationEnd(xn::SkeletonCapability& skeleton, const XnUserID nUserId, XnBool bSuccess, void* pCookie)
00138 {
00139 NIDevice::Objects::KinectDevice* device = (NIDevice::Objects::KinectDevice*)pCookie;
00140 NIDevice::Objects::KinectUser* user = 0;
00141 user = device->GetUser(nUserId);
00142 if(user)
00143 {
00144 if (bSuccess)
00145 {
00146 device->GetUserGenerator()->GetGenerator().GetSkeletonCap().StartTracking(nUserId);
00147 PostMessage(HScol, KINECT_USER_CALIBRATION_END_CB, (int)user, 0);
00148 }
00149 else
00150 {
00151 device->GetUserGenerator()->GetGenerator().GetPoseDetectionCap().StartPoseDetection("Psi", nUserId);
00152 }
00153 }
00154 }
00155
00156 void XN_CALLBACK_TYPE PoseDetected(xn::PoseDetectionCapability& poseDetection, const XnChar* strPose, XnUserID nId, void* pCookie)
00157 {
00158 NIDevice::Objects::KinectDevice* device = (NIDevice::Objects::KinectDevice*)pCookie;
00159 NIDevice::Objects::KinectUser* user = 0;
00160 user = device->GetUser(nId);
00161 if(user)
00162 {
00163 device->GetUserGenerator()->GetGenerator().GetPoseDetectionCap().StopPoseDetection(nId);
00164 device->GetUserGenerator()->GetGenerator().GetSkeletonCap().RequestCalibration(nId, true);
00165 PostMessage(HScol, KINECT_USER_POSE_DETECTED_CB, (int)user, 0);
00166 }
00167 }
00168
00169 void XN_CALLBACK_TYPE PoseLost(xn::PoseDetectionCapability& poseDetection, const XnChar* strPose, XnUserID nId, void* pCookie)
00170 {
00171 NIDevice::Objects::KinectDevice* device = (NIDevice::Objects::KinectDevice*)pCookie;
00172 NIDevice::Objects::KinectUser* user = 0;
00173 user = device->GetUser(nId);
00174 if(user)
00175 {
00176 PostMessage(HScol, KINECT_USER_POSE_LOST_CB, (int)user, 0);
00177 }
00178 }
00179
00180 namespace NIDevice
00181 {
00182 namespace Generator
00183 {
00184 User::User(NIDevice::Objects::KinectDevice* pDevice) : NIDevice::Core::GenericGenerator<xn::UserGenerator, XN_NODE_TYPE_USER>(pDevice)
00185 {
00186 registerCallbacks();
00187 }
00188
00189 User::User(): NIDevice::Core::GenericGenerator<xn::UserGenerator, XN_NODE_TYPE_USER>(0)
00190 {
00191 }
00192
00193 User::~User()
00194 {
00195 unRegisterCallbacks();
00196 }
00197
00198 const unsigned short* User::GetUserPixels()
00199 {
00200 GetGenerator().GetUserPixels(0, g_sceneMD);
00201 return (const_cast<unsigned short*>(g_sceneMD.Data()));
00202 }
00203
00204 void User::GetImageSize(int &w, int &h)
00205 {
00206 w = g_sceneMD.XRes();
00207 h = g_sceneMD.YRes();
00208 }
00209
00210 void User::requestInitialPos(NIDevice::Objects::KinectUser* user)
00211 {
00212 if(user->GetId() > 0 && GetGenerator())
00213 {
00214 GetGenerator().GetPoseDetectionCap().StartPoseDetection("Psi", user->GetId());
00215 PostMessage(HScol, KINECT_NEW_USER_CB, (int)user, 0);
00216 }
00217 }
00218
00219 void User::stopTracking(NIDevice::Objects::KinectUser* user)
00220 {
00221 if(user->GetId() > 0 && GetGenerator())
00222 {
00223 GetGenerator().GetSkeletonCap().StopTracking(user->GetId());
00224 }
00225 }
00226
00227 void User::registerCallbacks()
00228 {
00229 xn::SkeletonCapability skl = GetGenerator().GetSkeletonCap();
00230 skl.SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
00231 skl.SetSmoothing(0.6f);
00232 GetGenerator().RegisterUserCallbacks(NewUser, LostUser, GetParentDevice(), hUserCallbacks);
00233 skl.RegisterCalibrationCallbacks(CalibrationStart, CalibrationEnd, GetParentDevice(), m_hCalibrationCallbacks);
00234 GetGenerator().GetPoseDetectionCap().RegisterToPoseCallbacks(PoseDetected, PoseLost, GetParentDevice(), m_hPoseCallbacks);
00235
00236
00237 GetParentDevice()->GetContext().StartGeneratingAll();
00238 }
00239
00240 void User::unRegisterCallbacks()
00241 {
00242 if (hUserCallbacks)
00243 {
00244 GetGenerator().UnregisterUserCallbacks(hUserCallbacks);
00245 hUserCallbacks = 0;
00246 }
00247
00248 if(m_hCalibrationCallbacks)
00249 {
00250 GetGenerator().GetSkeletonCap().UnregisterCalibrationCallbacks(m_hCalibrationCallbacks);
00251 m_hCalibrationCallbacks = 0;
00252 }
00253
00254 if(m_hPoseCallbacks)
00255 {
00256 GetGenerator().GetPoseDetectionCap().UnregisterFromPoseCallbacks(m_hPoseCallbacks);
00257 m_hPoseCallbacks = 0;
00258 }
00259 }
00260 }
00261 }