Project

General

Profile

Kinect Scol plugin
User.cpp
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2012 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
31 #include "generator/User.h"
32 #include "DeviceManager.h"
33 #include "objects/KinectDevice.h"
34 #include "objects/KinectUser.h"
35 #include "openNiScolPlugin.h"
36 
37 User::User(KinectDevice* pDevice)
38 {
39  width = 0;
40  height = 0;
41  mParentDevice = pDevice;
42  mData = 0;
43  mReadData = 0;
44  mUserTracker = new nite::UserTracker;
45 
46  openni::Device& dev = mParentDevice->GetDevice();
47  if (mUserTracker->create(&dev) == nite::STATUS_OK)
48  {
49  // update the generator sizes with the depth size
50  if (mUserTracker->readFrame(&mTrackerFrame) == nite::STATUS_OK)
51  {
52  width = mTrackerFrame.getDepthFrame().getWidth();
53  height = mTrackerFrame.getDepthFrame().getHeight();
54  mParentDevice->SetGeneratorsSize(width, height);
55  }
56  else
57  pDevice->GetGeneratorsSize(width, height);
58 
59  mData = (nite::UserId*)malloc(width * height * sizeof(nite::UserId));
60  memset(mData, 0, width * height * sizeof(nite::UserId));
61 
62  mReadData = (nite::UserId*)malloc(width * height * sizeof(nite::UserId));
63  memset(mReadData, 0, width * height * sizeof(nite::UserId));
64  }
65  else
66  {
67  MMechostr(MSKDEBUG, "OpenNI device : could not create User generator\n");
68  }
69 }
70 
71 User::User()
72 {
73 }
74 
75 User::~User()
76 {
77  boost::mutex::scoped_lock l(mmutex);
78  if (mData)
79  free(mData);
80 
81  if (mReadData)
82  free(mReadData);
83 
84  mTrackerFrame.release();
85  delete(mUserTracker);
86 }
87 
88 KinectDevice* User::GetParentDevice()
89 {
90  return mParentDevice;
91 }
92 
93 nite::UserTracker* User::GetUserTraker()
94 {
95  return mUserTracker;
96 }
97 
98 nite::UserTrackerFrameRef* User::GetTrakerFrame()
99 {
100  return &mTrackerFrame;
101 }
102 
103 openni::VideoFrameRef User::UpdateData()
104 {
105  boost::mutex::scoped_lock l(mmutex);
106 
107  openni::VideoFrameRef depthFrame;
108  if (mParentDevice->GetDevice().isValid() && mData && mUserTracker->isValid())
109  {
110  if (mUserTracker->readFrame(&mTrackerFrame) != nite::STATUS_OK)
111  return depthFrame;
112 
113  depthFrame = mTrackerFrame.getDepthFrame();
114 
115  const nite::UserMap& userLabels = mTrackerFrame.getUserMap();
116  const nite::UserId* pLabels = userLabels.getPixels();
117 
118  memcpy(mData, pLabels, width * height * sizeof(nite::UserId));
119 
120  const nite::Array<nite::UserData>& users = mTrackerFrame.getUsers();
121  for (int i = 0; i < users.getSize(); ++i)
122  {
123  const nite::UserData& user = users[i];
124  KinectUser* kuser = mParentDevice->GetUser(user.getId());
125 
126  //updateUserState(user, userTrackerFrame.getTimestamp());
127  if (user.isNew())
128  {
129  mParentDevice->AddExistingId(user.getId());
130  }
131  else if (user.isLost())
132  {
133  mParentDevice->RemoveExistingId(user.getId());
134 
135  if(kuser)
136  {
137  //User update is done in thread loop
138  kuser->SetId(-1);
139  kuser->SetCalibrated(false);
140  OBJpostEvent(KINECT_LOST_USER_CB, SCOL_PTR kuser, 0);
141  }
142  }
143  else if (user.isVisible())
144  {
145  if (users[i].getSkeleton().getState() == nite::SKELETON_NONE && kuser)
146  {
147  if (mUserTracker->startSkeletonTracking(user.getId()) == nite::STATUS_OK)
148  OBJpostEvent(KINECT_USER_CALIBRATION_START_CB, SCOL_PTR kuser, 0);
149  }
150  else if (kuser && !kuser->IsCalibrated() && users[i].getSkeleton().getState() == nite::SKELETON_TRACKED)
151  {
152  kuser->SetCalibrated(true);
153  OBJpostEvent(KINECT_USER_CALIBRATION_END_CB, SCOL_PTR kuser, 0);
154  }
155  }
156  }
157  }
158  return depthFrame;
159 }
160 
161 bool User::IsValid()
162 {
163  return mUserTracker->isValid();
164 }
165 
166 const nite::UserId* User::GetBuffer()
167 {
168  return mData;
169 }
170 
171 void User::SetSmoothing(float sklSmoothing)
172 {
173  if (mUserTracker->isValid())
174  mUserTracker->setSkeletonSmoothingFactor(sklSmoothing);
175 }
176 
177 void User::GetValidUserPixels(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
178 {
179  // Get buffer
180  {
181  boost::mutex::scoped_lock l(mmutex);
182  {
183  if (mData && mReadData)
184  memcpy(mReadData, mData, width * height * sizeof(nite::UserId));
185  else
186  return;
187  }
188  }
189 
190  if ((Bcolor->TailleW != width) || (Bcolor->TailleH != height))
191  return;
192 
193  if ((Balpha->TailleW != width) || (Balpha->TailleH != height))
194  return;
195 
196  bool isMirror = GetParentDevice()->GetMirrorMode();
197 
198  //using GetUser(id) per pixel take too long, so we use a temporary tab indexed with ids
199  KinectUser* tUsers[64];
200  memset(tUsers, 0, sizeof(KinectUser*) * 64);
201  GetParentDevice()->GetValidUsersMap(tUsers);
202 
203  // Browse all pixels
204  long x = 0, y = 0;
205  unsigned long srcByte = 0, destByte = 0;
206  for (y = 0; y < height; y++)
207  {
208  for (x = 0; x < width; x++)
209  {
210  unsigned int fixed_x = x;
211  /*if(!isMirror)
212  {
213  fixed_x = (rgbW - 1) - x;
214  }
215  */
216 
217  srcByte = y*width + fixed_x;
218  destByte = x*3 + y*width*3;
219 
220  int id = mReadData[srcByte];
221 
222  int color = 0;
223 
224  KinectUser* user = 0;
225  if (id < 64)
226  {
227  user = tUsers[id];
228  if (user != 0)
229  color = user->GetColor();
230  }
231 
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;
236  }
237  }
238 }
239 
240 void User::stopTracking(KinectUser* user)
241 {
242  if(user->GetId() > 0 && mUserTracker->isValid())
243  {
244  mUserTracker->stopSkeletonTracking(user->GetId());
245  }
246 }
247 
248 void User::startTracking(KinectUser* user)
249 {
250  if(user->GetId() > 0 && mUserTracker->isValid())
251  {
252  mUserTracker->startSkeletonTracking(user->GetId());
253  }
254 }
255 
256 /*
257 void User::registerCallbacks()
258 {
259  if (IsValid())
260  {
261  xn::SkeletonCapability skl = GetSkeletonCap();
262  skl.SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
263  skl.SetSmoothing(0.6f);
264  RegisterUserCallbacks(NewUser, LostUser, GetParentDevice(), mhUserCallbacks);
265  skl.RegisterToCalibrationStart(CalibrationStart, GetParentDevice(), mhCalibrationStart);
266  skl.RegisterToCalibrationComplete(CalibrationEnd, GetParentDevice(), mhCalibrationEnd);
267  }
268 }
269 
270 void User::unRegisterCallbacks()
271 {
272  if (IsValid())
273  {
274  if (mhUserCallbacks)
275  {
276  UnregisterUserCallbacks(mhUserCallbacks);
277  mhUserCallbacks = 0;
278  }
279  if(mhCalibrationStart)
280  {
281  GetSkeletonCap().UnregisterFromCalibrationStart(mhCalibrationStart);
282  mhCalibrationStart = 0;
283  }
284 
285  if(mhCalibrationEnd)
286  {
287  GetSkeletonCap().UnregisterFromCalibrationComplete(mhCalibrationEnd);
288  mhCalibrationEnd = 0;
289  }
290  }
291 }
292 */
Kinect user handling. .
Definition: KinectUser.h:38
Kinect device handling. .
Definition: KinectDevice.h:39