Kinect Scol plugin
KinectUser.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 
33 #include "objects/KinectUser.h"
34 #include "core/DataSkeleton.h"
35 #include "DeviceManager.h"
36 #include "generator/User.h"
37 #include "objects/KinectDevice.h"
38 #include "openNiScolPlugin.h"
39 
40 // Constructors
41 KinectUser::KinectUser()
42 {
43 }
44 
45 KinectUser::KinectUser(KinectDevice* pDevice)
46 {
47  p_KinectDevice = pDevice;
48  mDataSkeleton = new DataSkeleton(this);
49  uId = -1;
50 
51  handsData = 0;
52  mleftHand = new KinectUserHand(this, nite::JOINT_LEFT_HAND);
53  mrightHand = new KinectUserHand(this, nite::JOINT_RIGHT_HAND);
54 
55  mCalibrated = false;
56  GenerateColor();
57 }
58 
59 // Destructor
60 KinectUser::~KinectUser()
61 {
62  if (uId > 0)
63  p_KinectDevice->AddExistingId(uId);
64 
65  if (p_KinectDevice->GetUserGenerator())
66  p_KinectDevice->GetUserGenerator()->stopTracking(this);
67 
68  SAFE_DELETE(mDataSkeleton);
69 
70  SAFE_DELETE(mleftHand);
71  SAFE_DELETE(mrightHand);
72 
73  if (handsData != 0)
74  {
75  free(handsData);
76  handsData = 0;
77  }
78 
79  p_KinectDevice = 0;
80 }
81 
82 void KinectUser::GenerateColor()
83 {
84  unsigned int r = rand() %255;
85  unsigned int g = rand() %255;
86  unsigned int b = rand() %255;
87  color = r + (g << 8) + (b << 16);
88 }
89 
90 bool KinectUser::IsCalibrated()
91 {
92  return mCalibrated;
93 }
94 
95 void KinectUser::SetCalibrated(bool state)
96 {
97  mCalibrated = state;
98 }
99 
100 
101 KinectDevice* KinectUser::GetParentDevice()
102 {
103  return p_KinectDevice;
104 }
105 
106 DataSkeleton* KinectUser::GetSkeletonData()
107 {
108  return mDataSkeleton;
109 }
110 
111 void KinectUser::SetId(int id)
112 {
113  uId = id;
114  if(id != -1)
115  GenerateColor();
116 }
117 
118 int KinectUser::GetId()
119 {
120  return uId;
121 }
122 
123 /*
124 unsigned short* KinectUser::GetNearestZone()
125 {
126  int level = 200;
127  if (uId > 0)
128  {
129  if (handsData == 0)
130  {
131  handsData = (unsigned short*)malloc(XN_VGA_X_RES * XN_VGA_Y_RES * sizeof(openni::DepthPixel));
132  memset(handsData, 0, XN_VGA_X_RES * XN_VGA_Y_RES * sizeof(openni::DepthPixel));
133  }
134 
135  xn::SceneMetaData udata;
136  p_KinectDevice->GetUserGenerator()->GetUserPixels(uId, udata);
137  xn::DepthMetaData depthMD;
138  p_KinectDevice->GetDepthGenerator()->GetMetaData(depthMD);
139 
140  int nDist = 10000;
141  for(int y(0); y < (int)depthMD.YRes(); ++y)
142  {
143  for(int x(0); x < (int)depthMD.XRes(); ++x)
144  {
145  int pos = x+y*depthMD.XRes();
146 
147  if((depthMD.Data()[pos] < nDist) && (udata.Data()[pos] != 0))
148  nDist = depthMD.Data()[pos];
149  }
150  }
151 
152  for(int y(0); y < (int)depthMD.YRes(); ++y)
153  {
154  for(int x(0); x < (int)depthMD.XRes(); ++x)
155  {
156  int pos = x+y*depthMD.XRes();
157 
158  if(((depthMD.Data()[pos] * udata.Data()[pos]) >= nDist) && ((depthMD.Data()[pos] * udata.Data()[pos]) <= (nDist + level)))
159  handsData[pos] = depthMD.Data()[pos];
160  else
161  handsData[pos] = 0;
162  }
163  }
164 
165  return handsData;
166  }
167  else
168  return 0;
169 }*/
170 
171 void KinectUser::DetectUserHands(cv::Mat depthMat, cv::Mat depthMatBgr)
172 {
173  mleftHand->Detect(depthMat, depthMatBgr);
174  mrightHand->Detect(depthMat, depthMatBgr);
175 }
176 
177 bool KinectUser::GetBoneImgCoordinates(nite::JointType skelJoint, nite::Point3f &vec, float minConfidence)
178 {
179  bool ret = mDataSkeleton->GetBoneImgCoordinates(skelJoint, vec, minConfidence);
180  if (skelJoint == nite::JOINT_LEFT_HAND)
181  vec = mleftHand->GetHandPos();
182  else if (skelJoint == nite::JOINT_RIGHT_HAND)
183  vec = mrightHand->GetHandPos();
184 
185  return ret;
186 }
187 
188 bool KinectUser::GetBoneCurrentPosition(nite::JointType skelJoint, nite::Point3f &position, float minConfidence)
189 {
190  return mDataSkeleton->GetBoneCurrentPosition(skelJoint, position, minConfidence);
191 }
192 
193 bool KinectUser::GetBoneCurrentOrientation(nite::JointType skelJoint, Quaternion* mQuat, float minConfidence)
194 {
195  return mDataSkeleton->GetBoneCurrentOrientation(skelJoint, mQuat, minConfidence);
196 }
197 
198 bool KinectUser::GetFingersPixelPosition(nite::JointType hand, vector<cv::Point> &vec)
199 {
200  if ((hand == nite::JOINT_LEFT_HAND) && (mleftHand->IsVisible()))
201  {
202  vec = mleftHand->GetFingersPos();
203  return true;
204  }
205  else if ((hand == nite::JOINT_RIGHT_HAND) && (mrightHand->IsVisible()))
206  {
207  vec = mrightHand->GetFingersPos();
208  return true;
209  }
210 
211  return false;
212 }
213 
214 bool KinectUser::IsHandVisible(nite::JointType skelJoint)
215 {
216  if (skelJoint == nite::JOINT_RIGHT_HAND)
217  {
218  return mrightHand->IsVisible();
219  }
220  else if (skelJoint == nite::JOINT_LEFT_HAND)
221  {
222  return mleftHand->IsVisible();
223  }
224 
225  return false;
226 }
Kinect user hand handling. .
Kinect device handling. .
Definition: KinectDevice.h:39
Create Singleton type. .
Definition: DataSkeleton.h:38