31 #include "generator/User.h" 32 #include "DeviceManager.h" 33 #include "objects/KinectDevice.h" 34 #include "objects/KinectUser.h" 35 #include "openNiScolPlugin.h" 41 mParentDevice = pDevice;
44 mUserTracker =
new nite::UserTracker;
46 openni::Device& dev = mParentDevice->GetDevice();
47 if (mUserTracker->create(&dev) == nite::STATUS_OK)
50 if (mUserTracker->readFrame(&mTrackerFrame) == nite::STATUS_OK)
52 width = mTrackerFrame.getDepthFrame().getWidth();
53 height = mTrackerFrame.getDepthFrame().getHeight();
54 mParentDevice->SetGeneratorsSize(width, height);
57 pDevice->GetGeneratorsSize(width, height);
59 mData = (nite::UserId*)malloc(width * height *
sizeof(nite::UserId));
60 memset(mData, 0, width * height *
sizeof(nite::UserId));
62 mReadData = (nite::UserId*)malloc(width * height *
sizeof(nite::UserId));
63 memset(mReadData, 0, width * height *
sizeof(nite::UserId));
67 MMechostr(MSKDEBUG,
"OpenNI device : could not create User generator\n");
77 boost::mutex::scoped_lock l(mmutex);
84 mTrackerFrame.release();
93 nite::UserTracker* User::GetUserTraker()
98 nite::UserTrackerFrameRef* User::GetTrakerFrame()
100 return &mTrackerFrame;
103 openni::VideoFrameRef User::UpdateData()
105 boost::mutex::scoped_lock l(mmutex);
107 openni::VideoFrameRef depthFrame;
108 if (mParentDevice->GetDevice().isValid() && mData && mUserTracker->isValid())
110 if (mUserTracker->readFrame(&mTrackerFrame) != nite::STATUS_OK)
113 depthFrame = mTrackerFrame.getDepthFrame();
115 const nite::UserMap& userLabels = mTrackerFrame.getUserMap();
116 const nite::UserId* pLabels = userLabels.getPixels();
118 memcpy(mData, pLabels, width * height *
sizeof(nite::UserId));
120 const nite::Array<nite::UserData>& users = mTrackerFrame.getUsers();
121 for (
int i = 0; i < users.getSize(); ++i)
123 const nite::UserData& user = users[i];
124 KinectUser* kuser = mParentDevice->GetUser(user.getId());
129 mParentDevice->AddExistingId(user.getId());
131 else if (user.isLost())
133 mParentDevice->RemoveExistingId(user.getId());
139 kuser->SetCalibrated(
false);
140 OBJpostEvent(KINECT_LOST_USER_CB, SCOL_PTR kuser, 0);
143 else if (user.isVisible())
145 if (users[i].getSkeleton().getState() == nite::SKELETON_NONE && kuser)
147 if (mUserTracker->startSkeletonTracking(user.getId()) == nite::STATUS_OK)
148 OBJpostEvent(KINECT_USER_CALIBRATION_START_CB, SCOL_PTR kuser, 0);
150 else if (kuser && !kuser->IsCalibrated() && users[i].getSkeleton().getState() == nite::SKELETON_TRACKED)
152 kuser->SetCalibrated(
true);
153 OBJpostEvent(KINECT_USER_CALIBRATION_END_CB, SCOL_PTR kuser, 0);
163 return mUserTracker->isValid();
166 const nite::UserId* User::GetBuffer()
171 void User::SetSmoothing(
float sklSmoothing)
173 if (mUserTracker->isValid())
174 mUserTracker->setSkeletonSmoothingFactor(sklSmoothing);
177 void User::GetValidUserPixels(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
181 boost::mutex::scoped_lock l(mmutex);
183 if (mData && mReadData)
184 memcpy(mReadData, mData, width * height *
sizeof(nite::UserId));
190 if ((Bcolor->TailleW != width) || (Bcolor->TailleH != height))
193 if ((Balpha->TailleW != width) || (Balpha->TailleH != height))
196 bool isMirror = GetParentDevice()->GetMirrorMode();
201 GetParentDevice()->GetValidUsersMap(tUsers);
205 unsigned long srcByte = 0, destByte = 0;
206 for (y = 0; y < height; y++)
208 for (x = 0; x < width; x++)
210 unsigned int fixed_x = x;
217 srcByte = y*width + fixed_x;
218 destByte = x*3 + y*width*3;
220 int id = mReadData[srcByte];
229 color = user->GetColor();
232 Bcolor->bits[destByte] = color &0xFF;
233 Bcolor->bits[destByte+1] = color>>8 &0xFF;
234 Bcolor->bits[destByte+2] = color>>16 &0xFF;
235 Balpha->bits[(x + y*width)] = (user == 0) || (
id == 0) ? 0 : 0xFF;
242 if(user->GetId() > 0 && mUserTracker->isValid())
244 mUserTracker->stopSkeletonTracking(user->GetId());
250 if(user->GetId() > 0 && mUserTracker->isValid())
252 mUserTracker->startSkeletonTracking(user->GetId());
Kinect device handling. .