public void UpdateSkeleton(int userId, OpenNISkeleton skeleton) { // make sure we have skeleton data for this user if (!skeletonCapbility.IsTracking(userId)) { return; } // Use torso as root SkeletonJointTransformation skelTrans = new SkeletonJointTransformation(); skelTrans = skeletonCapbility.GetSkeletonJoint(userId, SkeletonJoint.Torso); if (skeleton.absolute) { Point3D pos = skelTrans.Position.Position; skeleton.UpdateRoot(new Vector3(pos.X,pos.Y,pos.Z)); } else { Point3D pos = skelTrans.Position.Position; Vector3 v3dpos = new Vector3(pos.X, pos.Y, -pos.Z); Vector3 calPos = userCalibrationPosition[userId]; skeleton.UpdateRoot(calPos - v3dpos); } // update each joint with data from OpenNI foreach (SkeletonJoint joint in Enum.GetValues(typeof(SkeletonJoint))) { if (skeletonCapbility.IsJointAvailable(joint)) { skelTrans = skeletonCapbility.GetSkeletonJoint(userId, joint); skeleton.UpdateJoint(joint, skelTrans); } } }
/// @brief Gets a reference joint position and orientation on the joint. /// /// This method gets the reference joint information for the joint. The reference /// is the joint position and transformation at the time when the user was chosen AND /// started tracking. /// @param joint The joint we want information on. /// @param referenceTransform [out] The reference Transform. /// @return True on success and false on failure (e.g. an illegal joint or the user is not tracking). public bool GetReferenceSkeletonJointTransform(SkeletonJoint joint, out SkeletonJointTransformation referenceTransform) { referenceTransform=NIPlayerCandidateObject.m_InitializedZero; if(!Valid) return false; return m_user.GetReferenceSkeletonJointTransform(joint, out referenceTransform); }
/// @brief Gets a reference joint position and orientation on the joint. /// /// This method gets the reference joint information for the joint. The reference /// is the joint position and transformation at the time when the user was chosen AND /// started tracking. /// @param joint The joint we want information on. /// @param referenceTransform [out] The reference Transform. /// @return True on success and false on failure (e.g. an illegal joint or the user is not tracking). public bool GetReferenceSkeletonJointTransform(SkeletonJoint joint, out SkeletonJointTransformation referenceTransform) { referenceTransform = m_InitializedZero; if (m_playerStatus != UserStatus.Tracking || m_openNIUserID <= 0) return false; referenceTransform = m_referenceSkeletonJointTransform[joint]; return true; }
/// @brief Gets the current joint transformation for a specific joint /// /// @param joint The joint we want the transformation to. /// @param curTransform [out] The current joint transformation /// @return True on success and false on failure (e.g. the user is not tracking). /// @note An exception might occur if there is an error (e.g. an illegal joint is used). public bool GetSkeletonJoint(SkeletonJoint joint, out SkeletonJointTransformation curTransform) { curTransform = NIPlayerCandidateObject.m_InitializedZero; if (!Tracking) return false; if (m_user.Skeleton == null) return false; try { curTransform = m_user.Skeleton.GetSkeletonJoint(m_user.OpenNIUserID, joint); } catch { return false; } return true; }
public void UpdateSkeleton(int userId, OpenNISkeleton skeleton) { // make sure we have skeleton data for this user if (!skeletonCapbility.IsTracking(userId)) { return; } // Use torso as root SkeletonJointTransformation skelTrans = new SkeletonJointTransformation(); skelTrans = skeletonCapbility.GetSkeletonJoint(userId, SkeletonJoint.Torso); skeleton.UpdateRoot(skelTrans.Position.Position); // update each joint with data from OpenNI foreach (SkeletonJoint joint in Enum.GetValues(typeof(SkeletonJoint))) { if (skeletonCapbility.IsJointAvailable(joint)) { skelTrans = skeletonCapbility.GetSkeletonJoint(userId, joint); skeleton.UpdateJoint(joint, skelTrans); } } }
// EnuemrateActiveJoints public SkeletonJointTransformation GetSkeletonJoint(UserID user, SkeletonJoint joint) { SkeletonJointTransformation transformation = new SkeletonJointTransformation(); int status = SafeNativeMethods.xnGetSkeletonJoint(this.InternalObject, user, joint, ref transformation); WrapperUtils.ThrowOnError(status); return transformation; }
/// @brief updates a single joint /// /// This method updates a single joint. The decision of what to update (orientation, position) /// depends on m_updateOrientation and m_updateJointPositions. Only joints with high confidence /// are updated. @note it is possible to update only position or only orientation even though both /// are expected if the confidence of one is low. /// @param centerOffset the new central position /// @param joint the joint we want to update /// @param skelTrans the new transformation of the joint protected void UpdateJoint(SkeletonJoint joint, SkeletonJointTransformation skelTrans, SkeletonJointPosition centerOffset) { // make sure something is hooked up to this joint if ((int)joint >= m_jointTransforms.Length || !m_jointTransforms[(int)joint]) { return; } // if we have debug lines to draw we need to collect the data. if (m_linesDebugger!=null) { Vector3 pos = CalcJointPosition(joint, ref skelTrans, ref centerOffset) + transform.position; float posConf = skelTrans.Position.Confidence; Quaternion rot = CalcRotationForJoint(joint, ref skelTrans, ref centerOffset); float rotConf = skelTrans.Orientation.Confidence; m_linesDebugger.UpdateJointInfoForJoint(joint, pos, posConf, rot, rotConf); } // modify orientation (if needed and confidence is high enough) if (m_updateOrientation && skelTrans.Orientation.Confidence >= 0.5) { m_jointTransforms[(int)joint].rotation=CalcRotationForJoint(joint, ref skelTrans, ref centerOffset); } // modify position (if needed, and confidence is high enough) if (m_updateJointPositions && skelTrans.Position.Confidence>=0.5f) { m_jointTransforms[(int)joint].localPosition = CalcJointPosition(joint, ref skelTrans, ref centerOffset); } }
/// @brief Utility method to calculate the rotation of a joint /// /// This method receives joint information and calculates the rotation of the joint in Unity /// coordinate system. /// @param centerOffset the new central position /// @param joint the joint we want to calculate the rotation for /// @param skelTrans the new transformation of the joint /// @return the rotation of the joint in Unity coordinate system protected Quaternion CalcRotationForJoint(SkeletonJoint joint,ref SkeletonJointTransformation skelTrans, ref SkeletonJointPosition centerOffset) { // In order to convert the skeleton's orientation to Unity orientation we will // use the Quaternion.LookRotation method to create the relevant rotation Quaternion. // for Quaternion.LookRotation to work it needs a "forward" vector and an "upward" vector. // These are generally the "Z" and "Y" axes respectively in the sensor's coordinate // system. The orientation received from the skeleton holds these values in their // appropriate members. // Get the forward axis from "z". Point3D sensorForward = Point3D.ZeroPoint; sensorForward.X = skelTrans.Orientation.Z1; sensorForward.Y = skelTrans.Orientation.Z2; sensorForward.Z = skelTrans.Orientation.Z3; // convert it to Unity Vector3 worldForward = NIConvertCoordinates.ConvertPos(sensorForward); worldForward *= -1.0f; // because the Unity "forward" axis is opposite to the world's "z" axis. if (worldForward.magnitude == 0) return Quaternion.identity; // we don't have a good point to work with. // Get the upward axis from "Y". Point3D sensorUpward = Point3D.ZeroPoint; sensorUpward.X = skelTrans.Orientation.Y1; sensorUpward.Y = skelTrans.Orientation.Y2; sensorUpward.Z = skelTrans.Orientation.Y3; // convert it to Unity Vector3 worldUpwards = NIConvertCoordinates.ConvertPos(sensorUpward); if (worldUpwards.magnitude == 0) return Quaternion.identity; // we don't have a good point to work with. Quaternion jointRotation = Quaternion.LookRotation(worldForward, worldUpwards); Quaternion newRotation = transform.rotation * jointRotation * m_jointsInitialRotations[(int)joint]; // we try to limit the speed of the change. return Quaternion.Slerp(m_jointTransforms[(int)joint].rotation, newRotation, Time.deltaTime * m_rotationDampening); }
/// @brief Utility method to calculate the @b LOCAL position of a joint /// /// This method receives joint information and calculates the @b LOCAL position rotation of the joint /// (compare to its parent transform) in Unity coordinate system. /// @param centerOffset the new central position /// @param joint the joint we want to calculate the position for /// @param skelTrans the new transformation of the joint /// @return the @b LOCAL position rotation of the joint (compare to its parent transform) in /// Unity coordinate system protected Vector3 CalcJointPosition(SkeletonJoint joint, ref SkeletonJointTransformation skelTrans, ref SkeletonJointPosition centerOffset) { Vector3 v3pos=NIConvertCoordinates.ConvertPos(skelTrans.Position.Position); Vector3 v3Center = NIConvertCoordinates.ConvertPos(centerOffset.Position); v3pos -= v3Center; return v3pos*m_scale; }
void skeletonCapbility_CalibrationEnd(object sender, CalibrationEndEventArgs e) { if (e.Success) { if (AttemptToCalibrate) { SendMessage("CalibrationComplete", e, SendMessageOptions.DontRequireReceiver); this.skeletonCapbility.StartTracking(e.ID); SkeletonJointTransformation skelTrans = new SkeletonJointTransformation(); skelTrans = skeletonCapbility.GetSkeletonJoint(e.ID, SkeletonJoint.Torso); Point3D pos = skelTrans.Position.Position; userCalibrationPosition[e.ID] = new Vector3(pos.X,pos.Y,-pos.Z); calibratedUsers.Add(e.ID); } } else { SendMessage("CalibrationFailed", e, SendMessageOptions.DontRequireReceiver); if (AttemptToCalibrate) { this.poseDetectionCapability.StartPoseDetection(calibPose, e.ID); } } calibratingUsers.Remove(e.ID); }
public void UpdateJoint(SkeletonJoint joint, SkeletonJointTransformation skelTrans) { // save raw data jointData[(int)joint] = skelTrans; // make sure something is hooked up to this joint if (!transforms[(int)joint]) { return; } // modify orientation (if confidence is high enough) if (UpdateOrientation && skelTrans.Orientation.Confidence > 0.5) { // Z coordinate in OpenNI is opposite from Unity // Convert the OpenNI 3x3 rotation matrix to unity quaternion while reversing the Z axis Vector3 worldZVec = new Vector3(-skelTrans.Orientation.Z1, -skelTrans.Orientation.Z2, skelTrans.Orientation.Z3); Vector3 worldYVec = new Vector3(skelTrans.Orientation.Y1, skelTrans.Orientation.Y2, -skelTrans.Orientation.Y3); Quaternion jointRotation = Quaternion.LookRotation(worldZVec, worldYVec); Quaternion newRotation = transform.rotation * jointRotation * initialRotations[(int)joint]; transforms[(int)joint].rotation = Quaternion.Slerp(transforms[(int)joint].rotation, newRotation, Time.deltaTime * RotationDamping); } // modify position (if needed, and confidence is high enough) if (UpdateJointPositions) { Vector3 v3pos = new Vector3(skelTrans.Position.Position.X, skelTrans.Position.Position.Y, -skelTrans.Position.Position.Z); transforms[(int)joint].localPosition = Vector3.Scale(v3pos, Scale) - rootPosition; } }
public void JointFromJSON(SkeletonJoint j, Hashtable dict) { ArrayList positionList = (ArrayList)dict["Position"]; Hashtable oriHash = (Hashtable) dict["Orientation"]; ArrayList orientationList = (ArrayList) oriHash["Data"]; SkeletonJointOrientation sjo = new SkeletonJointOrientation(); sjo.X1 = 1.0f; SkeletonJointPosition sjp = new SkeletonJointPosition(); SkeletonJointTransformation xform = new SkeletonJointTransformation(); // object -> double ->float is okay, but object->float isn't // (the object is a Double) sjp.Position = new Point3D((float)(double)positionList[0], (float)(double)positionList[1], (float)(double)positionList[2]); sjo.X1 = (float)(double)orientationList[0]; sjo.X2 = (float)(double)orientationList[1]; sjo.X3 = (float)(double)orientationList[2]; sjo.Y1 = (float)(double)orientationList[3]; sjo.Y2 = (float)(double)orientationList[4]; sjo.Y3 = (float)(double)orientationList[5]; sjo.Z1 = (float)(double)orientationList[6]; sjo.Z2 = (float)(double)orientationList[7]; sjo.Z3 = (float)(double)orientationList[8]; sjo.Confidence = (float)(double)oriHash["Confidence"]; xform.Orientation = sjo; xform.Position = sjp; UpdateJoint(j, xform); }
public static extern XnStatus xnGetSkeletonJoint(XnNodeHandle hInstance, XnUserID user, SkeletonJoint joint, ref SkeletonJointTransformation transformation);