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
00036 #include "KinectDevice.h"
00037
00038 #include "../core/Exception.h"
00039 #include "../generator/Depth.h"
00040 #include "../generator/Image.h"
00041 #include "../generator/User.h"
00042 #include "../generator/Gesture.h"
00043 #include "../generator/Hands.h"
00044
00045 #include "../DeviceManager.h"
00046
00047 #include "../ScolPlugin.h"
00048
00049 namespace NIDevice
00050 {
00051 namespace Objects
00052 {
00053 KinectDevice::KinectDevice(unsigned int id) : Thread()
00054 {
00055 context = 0;
00056 mId = id;
00057 m_deviceNode = 0;
00058 bConnected = false;
00059 bGenerating = false;
00060 bInit = false;
00061 bMirror = false;
00062 iDistCutoff = 10000;
00063 fAngCutoff = 20.0f;
00064 m_DepthGenerator = 0;
00065 m_ImageGenerator = 0;
00066 m_UserGenerator = 0;
00067 m_GestureGenerator = 0;
00068 m_HandsGenerator = 0;
00069 m_sklSmoothing = 0.6f;
00070
00071
00072 sessionManager = 0;
00073
00074 StartThreading(boost::bind(&KinectDevice::GoThread, this));
00075 }
00076
00077 KinectDevice::KinectDevice() : Thread()
00078 {
00079 }
00080
00081 KinectDevice::~KinectDevice()
00082 {
00083 StopThreading();
00084 stopAndDestroyGenerators();
00085 bGenerating = false;
00086 destroyContext();
00087 }
00088
00089 void KinectDevice::createContext()
00090 {
00091
00092 nRetVal = context.Init();
00093 if (nRetVal == XN_STATUS_OK)
00094 bInit = true;
00095 }
00096
00097 void KinectDevice::destroyContext()
00098 {
00099
00100 SAFE_DELETE(sessionManager);
00101
00102 context.Shutdown();
00103 bInit = false;
00104 }
00105
00106 xn::Context& KinectDevice::GetContext()
00107 {
00108 assert(&context);
00109 return context;
00110 }
00111
00112 xn::Context* KinectDevice::GetContextPtr()
00113 {
00114 return &context;
00115 }
00116
00117
00118 XnVSessionManager& KinectDevice::GetSessionManager()
00119 {
00120 assert(sessionManager);
00121 return *sessionManager;
00122 }
00123
00124 XnVSessionManager* KinectDevice::GetSessionManagerPtr()
00125 {
00126 return sessionManager;
00127 }
00128
00129 bool KinectDevice::IsInit()
00130 {
00131 return bInit;
00132 }
00133
00134 bool KinectDevice::IsConnected()
00135 {
00136 return bConnected;
00137 }
00138
00139 bool KinectDevice::IsGenerating()
00140 {
00141 return bGenerating;
00142 }
00143
00144 void KinectDevice::SetMirrorMode(bool state)
00145 {
00146 bMirror = state;
00147
00148 if (m_DepthGenerator)
00149 {
00150 m_DepthGenerator->SetMirror(bMirror);
00151 m_ImageGenerator->SetMirror(bMirror);
00152 m_UserGenerator->SetMirror(bMirror);
00153 m_GestureGenerator->SetMirror(bMirror);
00154 m_HandsGenerator->SetMirror(bMirror);
00155 }
00156 }
00157
00158 bool KinectDevice::GetMirrorMode()
00159 {
00160 return bMirror;
00161 }
00162
00163 void KinectDevice::DestroyKinectUser(NIDevice::Objects::KinectUser* existingKinectUser)
00164 {
00165 listOfUsers.remove(existingKinectUser);
00166 SAFE_DELETE(existingKinectUser);
00167 }
00168
00169 NIDevice::Objects::KinectUser* KinectDevice::CreateKinectUser()
00170 {
00171 NIDevice::Objects::KinectUser* newKinectUser = new NIDevice::Objects::KinectUser(this);
00172 listOfUsers.push_back(newKinectUser);
00173
00174
00175 newKinectUser->SetId(GetExistingAvailableId());
00176 if (GetUserGenerator())
00177 GetUserGenerator()->requestInitialPos(newKinectUser);
00178
00179 return newKinectUser;
00180 }
00181
00182 NIDevice::Objects::KinectUser* KinectDevice::GetUser(int userId)
00183 {
00184 NIDevice::Objects::KinectUser* userFound = 0;
00185 const KinectUserList userList = listOfUsers;
00186 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
00187 while ((iSearchedUser != listOfUsers.end()) && (userFound == 0))
00188 {
00189 if ((*iSearchedUser)->GetId() == userId)
00190 userFound = *iSearchedUser;
00191
00192 iSearchedUser++;
00193 }
00194 return userFound;
00195 }
00196
00197 void KinectDevice::GetValidUsersMap(NIDevice::Objects::KinectUser** tUsers)
00198 {
00199 const KinectUserList userList = listOfUsers;
00200 KinectUserList::iterator iSearchedUser = listOfUsers.begin();
00201 while (iSearchedUser != listOfUsers.end())
00202 {
00203 int uid = (*iSearchedUser)->GetId();
00204 if ((uid > 0) && (uid < 16))
00205 tUsers[uid] = *iSearchedUser;
00206
00207 iSearchedUser++;
00208 }
00209 }
00210
00211 void KinectDevice::GoThread()
00212 {
00213 try
00214 {
00215 while(IsRunning())
00216 {
00217 if(!IsInit())
00218 createContext();
00219
00220 else if(IsInit() && IsConnected() && IsGenerating())
00221 {
00222 nRetVal = context.WaitAndUpdateAll();
00223 if(nRetVal != XN_STATUS_OK)
00224 {
00225 KINECT_EXCEPT(NIDevice::Core::ExceptionKinect, GetNiDeviceMessage(), GetObjectTypeAsString());
00226 }
00227
00228 int ucnt = 0;
00229 int uavailable = GetExistingAvailableId();
00230 NIDevice::Objects::KinectUser* user = GetUser(-1);
00231
00232
00233
00234 if (((uavailable != -1) && user) && (GetUserGenerator()->GetGenerator().IsValid() && GetDepthGenerator()->GetGenerator().IsValid()))
00235 {
00236 xn::SceneMetaData udata;
00237 GetUserGenerator()->GetGenerator().GetUserPixels(uavailable, udata);
00238 const unsigned short* ubuf = udata.Data();
00239 const unsigned short* dbuf = GetDepthGenerator()->GetDepthBuffer(false);
00240
00241 for (int i=0; i < (udata.XRes() * udata.YRes()); i++)
00242 {
00243 if ((ubuf[i] * dbuf[i]) > 0)
00244 ucnt++;
00245 }
00246
00247 if (ucnt > 0)
00248 {
00249 user->SetId(uavailable);
00250 GetUserGenerator()->GetGenerator().GetPoseDetectionCap().StartPoseDetection("Psi", uavailable);
00251 PostMessage(HScol, KINECT_NEW_USER_CB, (int)user, 0);
00252 }
00253 else
00254 AddExistingId(uavailable);
00255 }
00256
00257
00258 sessionManager->Update(&context);
00259 }
00260
00261 else if(IsInit() && !IsConnected())
00262 createAndStartGenerators();
00263
00264 else if(IsInit() && IsConnected() && !IsGenerating())
00265 {
00266 stopAndDestroyGenerators();
00267 destroyContext();
00268 }
00269 }
00270 }
00271 catch (NIDevice::Core::ExceptionKinect& e)
00272 {
00273 MMechostr(MSKDEBUG,"%s",e.GetFullDescription().c_str());
00274 }
00275 }
00276
00277 unsigned int KinectDevice::GetId()
00278 {
00279 return mId;
00280 }
00281
00282 int KinectDevice::GetExistingAvailableId()
00283 {
00284 int found = -1;
00285 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.begin();
00286 if (iuser != listOfAvalaibleUsers.end())
00287 {
00288 found = (int)*iuser;
00289 listOfAvalaibleUsers.erase(iuser);
00290 }
00291
00292 return found;
00293 }
00294
00295 void KinectDevice::AddExistingId(unsigned int id)
00296 {
00297 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.find(id);
00298 if (iuser == listOfAvalaibleUsers.end())
00299 {
00300 listOfAvalaibleUsers.insert(id);
00301 }
00302 }
00303
00304 void KinectDevice::RemoveExistingId(unsigned int id)
00305 {
00306 OpenNIUserList::iterator iuser = listOfAvalaibleUsers.find(id);
00307 if (iuser != listOfAvalaibleUsers.end())
00308 {
00309 listOfAvalaibleUsers.erase(iuser);
00310 }
00311 }
00312
00313
00314 void XN_CALLBACK_TYPE SessionStart(const XnPoint3D &ptPosition, void *userCxt)
00315 {
00316 MMechostr(MSKDEBUG, ">>> NITE session started.\n");
00317 }
00318
00319 void XN_CALLBACK_TYPE SessionEnd(void *userCxt)
00320 {
00321 MMechostr(MSKDEBUG, ">>> NITE session started.\n");
00322 }
00323
00324 void KinectDevice::createAndStartGenerators()
00325 {
00326
00327 m_DepthGenerator = new NIDevice::Generator::Depth(this);
00328 m_ImageGenerator = new NIDevice::Generator::Image(this);
00329 m_UserGenerator = new NIDevice::Generator::User(this);
00330 m_GestureGenerator = new NIDevice::Generator::Gesture(this);
00331 m_HandsGenerator = new NIDevice::Generator::Hands(this);
00332
00333
00334 sessionManager = new XnVSessionManager();
00335 sessionManager->Initialize(&context, "Click,Wave", "RaiseHand");
00336 sessionManager->SetQuickRefocusTimeout(0);
00337 sessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd);
00338
00339
00340 if (m_UserGenerator)
00341 m_UserGenerator->GetGenerator().GetSkeletonCap().SetSmoothing(m_sklSmoothing);
00342
00343
00344 if (m_DepthGenerator)
00345 {
00346 m_DepthGenerator->SetDistCutoff(iDistCutoff);
00347 m_DepthGenerator->SetAngCutoff(fAngCutoff);
00348 }
00349
00350 context.StartGeneratingAll();
00351 context.WaitNoneUpdateAll();
00352
00353 bConnected = true;
00354 bGenerating = true;
00355
00356 SetMirrorMode(bMirror);
00357
00358
00359
00360
00361
00362 PostMessage(HScol, KINECT_CONNECTED_CB, (int)this,NULL);
00363 }
00364
00365 void KinectDevice::stopAndDestroyGenerators()
00366 {
00367
00368 if (NULL != sessionManager)
00369 sessionManager->EndSession();
00370
00371
00372 context.StopGeneratingAll();
00373
00374 if(m_DepthGenerator)
00375 {
00376 SAFE_DELETE(m_DepthGenerator);
00377 }
00378
00379 if(m_ImageGenerator)
00380 {
00381 SAFE_DELETE(m_ImageGenerator);
00382 }
00383
00384 if(m_UserGenerator)
00385 {
00386 SAFE_DELETE(m_UserGenerator);
00387 }
00388
00389 if(m_GestureGenerator)
00390 {
00391 SAFE_DELETE(m_GestureGenerator);
00392 }
00393
00394 if(m_HandsGenerator)
00395 {
00396 SAFE_DELETE(m_HandsGenerator);
00397 }
00398
00399 listOfAvalaibleUsers.clear();
00400
00401 PostMessage(HScol, KINECT_DISCONNECTED_CB, (int)this,NULL);
00402 }
00403
00404 NIDevice::Generator::Depth* KinectDevice::GetDepthGenerator()
00405 {
00406 return m_DepthGenerator;
00407 }
00408
00409 NIDevice::Generator::Image* KinectDevice::GetImageGenerator()
00410 {
00411 return m_ImageGenerator;
00412 }
00413
00414 NIDevice::Generator::User* KinectDevice::GetUserGenerator()
00415 {
00416 return m_UserGenerator;
00417 }
00418
00419 NIDevice::Generator::Gesture* KinectDevice::GetGestureGenerator()
00420 {
00421 return m_GestureGenerator;
00422 }
00423
00424 NIDevice::Generator::Hands* KinectDevice::GetHandsGenerator()
00425 {
00426 return m_HandsGenerator;
00427 }
00428
00429 void KinectDevice::SetSkeletonSmoothing(float value)
00430 {
00431 m_sklSmoothing = value;
00432 if (m_UserGenerator)
00433 m_UserGenerator->GetGenerator().GetSkeletonCap().SetSmoothing(m_sklSmoothing);
00434 }
00435
00436 float KinectDevice::GetSkeletonSmoothing()
00437 {
00438 return m_sklSmoothing;
00439 }
00440
00441 void KinectDevice::SetDistCutoff(int dist)
00442 {
00443 iDistCutoff = dist;
00444 if (m_DepthGenerator)
00445 m_DepthGenerator->SetDistCutoff(dist);
00446 }
00447
00448 int KinectDevice::GetDistCutoff()
00449 {
00450 return iDistCutoff;
00451 }
00452
00453 void KinectDevice::SetAngCutoff(float ang)
00454 {
00455 fAngCutoff = ang;
00456 if (m_DepthGenerator)
00457 m_DepthGenerator->SetAngCutoff(ang);
00458 }
00459
00460 float KinectDevice::GetAngCutoff()
00461 {
00462 return fAngCutoff;
00463 }
00464 }
00465 }