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
00035 #include "DataSkeleton.h"
00036
00037 #include "../lib/common.h"
00038
00039 #include "../generator/User.h"
00040
00041 #include "../objects/KinectDevice.h"
00042 #include "../objects/KinectUser.h"
00043
00045 void FromRotationMatrix(XnSkeletonJointOrientation jointOri, Quaternion* mQuat)
00046 {
00047 Matrix3 kRot = Matrix3(jointOri.orientation.elements[0],-jointOri.orientation.elements[1],jointOri.orientation.elements[2],
00048 -jointOri.orientation.elements[3],jointOri.orientation.elements[4],-jointOri.orientation.elements[5],
00049 jointOri.orientation.elements[6],-jointOri.orientation.elements[7],jointOri.orientation.elements[8]);
00050
00051
00052
00053
00054 float fTrace = kRot[0][0]+kRot[1][1]+kRot[2][2];
00055 float fRoot;
00056
00057 if ( fTrace > 0.0 )
00058 {
00059
00060 fRoot = sqrt(fTrace + 1.0f);
00061 mQuat->w = 0.5f*fRoot;
00062 fRoot = 0.5f/fRoot;
00063 mQuat->x = (kRot[2][1]-kRot[1][2])*fRoot;
00064 mQuat->y = (kRot[0][2]-kRot[2][0])*fRoot;
00065 mQuat->z = (kRot[1][0]-kRot[0][1])*fRoot;
00066 }
00067 else
00068 {
00069
00070 static size_t s_iNext[3] = { 1, 2, 0 };
00071 size_t i = 0;
00072 if ( kRot[1][1] > kRot[0][0] )
00073 i = 1;
00074 if ( kRot[2][2] > kRot[i][i] )
00075 i = 2;
00076 size_t j = s_iNext[i];
00077 size_t k = s_iNext[j];
00078
00079 fRoot = sqrt(kRot[i][i]-kRot[j][j]-kRot[k][k] + 1.0f);
00080 float* apkQuat[3] = { &mQuat->x, &mQuat->y, &mQuat->z };
00081 *apkQuat[i] = 0.5f*fRoot;
00082 fRoot = 0.5f/fRoot;
00083 mQuat->w = (kRot[k][j]-kRot[j][k])*fRoot;
00084 *apkQuat[j] = (kRot[j][i]+kRot[i][j])*fRoot;
00085 *apkQuat[k] = (kRot[k][i]+kRot[i][k])*fRoot;
00086 }
00087 }
00088
00089 namespace NIDevice
00090 {
00091 namespace Core
00092 {
00093 DataSkeleton::DataSkeleton(NIDevice::Objects::KinectUser* existingUser)
00094 {
00095 p_User = existingUser;
00096 }
00097
00098 DataSkeleton::DataSkeleton()
00099 {
00100 p_User = 0;
00101 }
00102
00103 DataSkeleton::~DataSkeleton()
00104 {
00105 p_User = 0;
00106 }
00107
00108 bool DataSkeleton::GetBoneCurrentPosition(XnSkeletonJoint skelJoint, XnPoint3D &position, float minConfidence)
00109 {
00110 boost::mutex::scoped_lock lock(p_User->GetParentDevice()->m_mutex);
00111
00112 XnSkeletonJointPosition posJoint;
00113 if (p_User->GetParentDevice()->GetUserGenerator() != 0)
00114 {
00115 if(p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().IsTracking(p_User->GetId()) &&
00116 p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().IsJointAvailable(skelJoint))
00117
00118 p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().GetSkeletonJointPosition(p_User->GetId(), skelJoint, posJoint);
00119 else
00120 return false;
00121 }
00122 else
00123 return false;
00124
00125 if(posJoint.fConfidence < minConfidence)
00126 {
00127 return false;
00128 }
00129 position = posJoint.position;
00130 return true;
00131 }
00132
00133 bool DataSkeleton::GetBoneCurrentOrientation(XnSkeletonJoint skelJoint, Quaternion* mQuat, float minConfidence)
00134 {
00135 boost::mutex::scoped_lock lock(p_User->GetParentDevice()->m_mutex);
00136
00137 XnSkeletonJointOrientation orientationJoint;
00138 if (p_User->GetParentDevice()->GetUserGenerator() != 0)
00139 {
00140 if(p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().IsTracking(p_User->GetId()) &&
00141 p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().IsJointAvailable(skelJoint))
00142 p_User->GetParentDevice()->GetUserGenerator()->GetGenerator().GetSkeletonCap().GetSkeletonJointOrientation(p_User->GetId(), skelJoint, orientationJoint);
00143 else
00144 return false;
00145 }
00146 else
00147 return false;
00148
00149 if(orientationJoint.fConfidence < minConfidence)
00150 {
00151 return false;
00152 }
00153 FromRotationMatrix(orientationJoint, mQuat);
00154 return true;
00155 }
00156 }
00157 }