// gets the current avatar pose private void GetAvatarPose(float fCurrentTime, bool isMirrored) { KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null || avatarModel == null || poseJoints == null) { return; } poseAvatar.fTime = fCurrentTime; if (poseAvatar.avBoneDirs == null) { poseAvatar.avBoneDirs = new Vector3[poseJoints.Count]; } for (int i = 0; i < poseJoints.Count; i++) { KinectInterop.JointType joint = poseJoints[i]; KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) { Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored)); Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (avatarTransform1 != null && avatarTransform2 != null) { poseAvatar.avBoneDirs[i] = (avatarTransform2.position - avatarTransform1.position).normalized; } } } }
// adds current pose of poseModel to the saved poses list private void AddCurrentPoseToSaved(float fCurrentTime, bool isMirrored) { KinectManager kinectManager = KinectManager.Instance; if (kinectManager == null || poseModel == null || poseJoints == null) { return; } PoseModelData pose = new PoseModelData(); pose.fTime = fCurrentTime; pose.avBoneDirs = new Vector3[poseJoints.Count]; // save model rotation Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation; poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; for (int i = 0; i < poseJoints.Count; i++) { KinectInterop.JointType joint = poseJoints[i]; KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) { Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (poseTransform1 != null && poseTransform2 != null) { pose.avBoneDirs[i] = (poseTransform2.position - poseTransform1.position).normalized; } } } // add pose to the list alSavedPoses.Add(pose); // restore model rotation poseModel.GetBoneTransform(0).rotation = poseSavedRotation; }
// gets angle or percent difference in pose public float GetPoseDifference(bool isMirrored, bool bPercentDiff, ref string sDiffDetails) { float fAngleDiff = 0f; float fMaxDiff = 0f; sDiffDetails = string.Empty; KinectManager kinectManager = KinectManager.Instance; if (!kinectManager || !avatarModel || !poseModel || poseJoints.Count == 0) { return(0f); } // copy model rotation Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation; poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; StringBuilder sbDetails = new StringBuilder(); sbDetails.Append("Joint differences:").AppendLine(); //creating a secondary list to save the original position for joints to be refrenced back to if (poseJointsSaver.Count == 0) { for (int z = 0; z < poseJoints.Count; z++) { poseJointsSaver.Add(poseJoints[z]); } } ///////////////////////////////////////////////// //the joints that will be tacked for each exercise ////////////////////////////////////////////////// if (armExtention == true) //sets the focus on the left shoulder,elbow, and hand of the pose avatar, the paticipants right arm { poseJoints[0] = poseJointsSaver[4]; //shoulder right poseJoints[1] = poseJointsSaver[5]; //elbow right poseJoints[2] = poseJointsSaver[11]; //wrist right for (int c = 3; c < poseJoints.Count; c++) //fills up the rest of the spots, needed otherwise the last joint, wrist right, will not be tracked { poseJoints[c] = poseJointsSaver[13]; } } if (standUp == true) { poseJoints[0] = poseJointsSaver[16]; //hip right poseJoints[1] = poseJointsSaver[17]; //knee right poseJoints[2] = poseJointsSaver[18]; //ankle right poseJoints[3] = poseJointsSaver[19]; //hip left poseJoints[4] = poseJointsSaver[20]; //knee left poseJoints[5] = poseJointsSaver[21]; //ankle left poseJoints[6] = poseJointsSaver[4]; //shoulder right poseJoints[7] = poseJointsSaver[5]; //elbow right poseJoints[8] = poseJointsSaver[3]; //shoulder left poseJoints[9] = poseJointsSaver[6]; //elbow left for (int c = 10; c < poseJoints.Count; c++) //fills up the rest of the spots, needed otherwise the last joint, elbow left, will not be tracked { poseJoints[c] = poseJointsSaver[13]; } } if (lunge == true) { poseJoints[0] = poseJointsSaver[16]; //hip right poseJoints[1] = poseJointsSaver[17]; //knee right poseJoints[2] = poseJointsSaver[18]; //ankle right poseJoints[3] = poseJointsSaver[22]; //foot right poseJoints[4] = poseJointsSaver[19]; //hip left poseJoints[5] = poseJointsSaver[20]; //knee left poseJoints[6] = poseJointsSaver[21]; //ankle left poseJoints[7] = poseJointsSaver[23]; //foot left for (int c = 8; c < poseJoints.Count; c++) //fills up the rest of the spots, needed otherwise the last joint, foot left, will not be tracked { poseJoints[c] = poseJointsSaver[13]; } } for (int i = 0; i < poseJoints.Count; i++) { ; KinectInterop.JointType joint = poseJoints[i]; KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) { Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored)); Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored)); Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); //These are the three steps in extend arm if (step == 0) //move the pose to first position, elbow bent hand bent //Place the code to make avatar start in position 1 elbow bent, wrist bent { nextStep = true; // this is set to true after the pose has been reached to make sure the avater is not skipping ahead before the position has been reached. } //step two extend the arm if (step == 1) { //code to move avatar to the next position, elbow straight but wrist bent nextStep = true; } if (step == 2) // extend wrist foward //Place code to move avatar to final position of this exercise, wrist straight elbow straight { nextStep = true; } //next exercise, sitting to standing if (step == 3) { armExtention = false; //stops focusing on just the right arm joints standUp = true; //changes the joints that are being focused on //Place the code to move to seated position nextStep = true; } if (step == 4) //move elbows to 90 degree angles and move feet backwards //place code to move avatar to elbows bent at 90 degrees holding onto the arm rests { nextStep = true; } if (step == 5) //move pose to stand up //place code to move after to standing position, final position for this exercise { nextStep = true; } //next exercise, lunge if (step == 6) { armExtention = false; //makes sure the joints for this exercise are not being tracked standUp = false; //makes sure the joints for this exercise are not being tracked lunge = true; //changes the joints being focused on to the legs and hips and off the arms //place code to move leg forward and bend back leg for lunge position } if (avatarTransform1 != null && avatarTransform2 != null && poseTransform1 != null && poseTransform2 != null) { Vector3 vAvatarBone = (avatarTransform2.position - avatarTransform1.position).normalized; Vector3 vPoseBone = (poseTransform2.position - poseTransform1.position).normalized; float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; fMaxDiff += 90f; // we assume the max diff could be 90 degrees sbDetails.AppendFormat("{0} - {1:F0} deg.", joint, fDiff).AppendLine(); } else { sbDetails.AppendFormat("{0} - n/a", joint).AppendLine(); } } } poseModel.GetBoneTransform(0).rotation = poseSavedRotation; // calculate percent diff float fPercentDiff = 0f; if (bPercentDiff && fMaxDiff > 0f) { fPercentDiff = fAngleDiff / fMaxDiff; } // details info sbDetails.AppendLine(); sbDetails.AppendFormat("Sum-Diff: - {0:F0} deg out of {1:F0} deg", fAngleDiff, fMaxDiff).AppendLine(); sbDetails.AppendFormat("Percent-Diff: {0:F0}%", fPercentDiff * 100).AppendLine(); sDiffDetails = sbDetails.ToString(); return(bPercentDiff ? fPercentDiff : fAngleDiff); }
// gets angle or percent difference in pose public float GetPoseDifference(bool isMirrored, bool bPercentDiff, ref string sDiffDetails) { float fAngleDiff = 0f; float fMaxDiff = 0f; sDiffDetails = string.Empty; KinectManager kinectManager = KinectManager.Instance; if (!kinectManager || !avatarModel || !poseModel || poseJoints.Count == 0) { return(0f); } // copy model rotation Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation; poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; StringBuilder sbDetails = new StringBuilder(); sbDetails.Append("Joint differences:").AppendLine(); //creating the list for the joints that will be focused in each excercise if (poseJointsSaver.Count == 0) { for (int z = 0; z < poseJoints.Count; z++) { poseJointsSaver.Add(poseJoints[z]); } } if (armExtention == true) //sets the focus on the left shoulder,elbow, and hand of the pose avatar { poseJoints[0] = poseJointsSaver[4]; poseJoints[1] = poseJointsSaver[5]; poseJoints[2] = poseJointsSaver[11]; for (int c = 3; c < poseJoints.Count; c++) { poseJoints[c] = poseJointsSaver[13]; } } if (lunge == true) { // poseJointsSaver.Add(HipLeft); // //poseJoints[1] = KneeLeft; // poseJoints[2] = poseJointsSaver[11]; // for(int c = 3; c < poseJoints.Count; c++){ // poseJoints[c] = poseJointsSaver[13]; // } } if (standUp == true) { } for (int i = 0; i < poseJoints.Count; i++) { // Debug.Log(poseJoints.Count); // Debug.Log(i); KinectInterop.JointType joint = poseJoints[i]; // Debug.Log("joint"); // Debug.Log(joint); // Debug.Log("poseJoints"); // Debug.Log(poseJoints[i]); KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) { Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored)); Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (test < 20) // not going to be needed anymore because we will be using the list of joints I created { test = test + 1; } Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); // Debug.Log("poseTransform1"); // Debug.Log(poseTransform1); // Debug.Log("poseTransform2"); // Debug.Log(poseTransform2); Debug.Log("Test"); Debug.Log(test); Debug.Log("poseTransform1"); Debug.Log(poseTransform1); //These are the three steps in extend arm if (step == 0) //move the pose to first position, elbow bent hand bent //Debug.Log("step1"); { armExtention = false; if (test == 1) //shoulder { holdShoulder = poseTransform1; y = 0; while (y < 1600) { poseTransform1.Rotate(Vector3.down, 5 * Time.deltaTime); y = y + 1; } y = 0; while (y < 1500) { poseTransform1.Rotate(Vector3.right, 5 * Time.deltaTime); y = y + 1; } y = 0; } if (joint == poseJoints[1]) //elbow { holdElbow = poseTransform1; while (y < 1000) { poseTransform1.Rotate(Vector3.left, 5 * Time.deltaTime); y = y + 1; } holdElbow = poseTransform1; } if (joint == poseJoints[2]) //handLeft { holdHand = poseTransform1; while (h < 500) { poseTransform1.Rotate(Vector3.left, 5 * Time.deltaTime); h = h + 1; } } runthough = true; nextStep = true; } //step two extend the arm if (step == 1) { //Debug.Log("step2"); if (runthough == false) { //Debug.Log("step2-1"); //Debug.Log(poseTransform1); if (poseTransform1 == holdElbow) { //Debug.Log("step2-2"); // //Vector3 rotate2 = new Vector3 (-300f,80f,0f); // while(x < 651){ //Debug.Log(x); poseTransform1.Rotate(Vector3.right, 10 * Time.deltaTime); x = x + 1; // } } } if (x == 460) { nextStep = true; runthough = true; } } if (step == 2) // extend hand foward //Debug.Log("step3"); { if (runthough == false) { if (poseTransform1 == holdHand) { // while(y < 800){ poseTransform1.Rotate(Vector3.right, 10 * Time.deltaTime); y = y + 1; //} } if (y > 370) { runthough = true; nextStep = true; } } } if (nextPose == 3 || nextPose == 4) { armExtention = false; lunge = true; if (step == 3) { } } if (avatarTransform1 != null && avatarTransform2 != null && poseTransform1 != null && poseTransform2 != null) { Vector3 vAvatarBone = (avatarTransform2.position - avatarTransform1.position).normalized; Vector3 vPoseBone = (poseTransform2.position - poseTransform1.position).normalized; float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; fMaxDiff += 90f; // we assume the max diff could be 90 degrees sbDetails.AppendFormat("{0} - {1:F0} deg.", joint, fDiff).AppendLine(); } else { sbDetails.AppendFormat("{0} - n/a", joint).AppendLine(); } } } poseModel.GetBoneTransform(0).rotation = poseSavedRotation; // calculate percent diff float fPercentDiff = 0f; if (bPercentDiff && fMaxDiff > 0f) { fPercentDiff = fAngleDiff / fMaxDiff; } // details info sbDetails.AppendLine(); sbDetails.AppendFormat("Sum-Diff: - {0:F0} deg out of {1:F0} deg", fAngleDiff, fMaxDiff).AppendLine(); sbDetails.AppendFormat("Percent-Diff: {0:F0}%", fPercentDiff * 100).AppendLine(); sDiffDetails = sbDetails.ToString(); return(bPercentDiff ? fPercentDiff : fAngleDiff); }
// gets angle or percent difference in pose public float GetPoseDifference(bool isMirrored, bool bPercentDiff, ref string sDiffDetails) { float fAngleDiff = 0f; float fMaxDiff = 0f; sDiffDetails = string.Empty; KinectManager kinectManager = KinectManager.Instance; if (!kinectManager || !avatarModel || !poseModel || poseJoints.Count == 0) { return(0f); } // copy model rotation Quaternion poseSavedRotation = poseModel.GetBoneTransform(0).rotation; poseModel.GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; StringBuilder sbDetails = new StringBuilder(); sbDetails.Append("Joint differences:").AppendLine(); for (int i = 0; i < poseJoints.Count; i++) { KinectInterop.JointType joint = poseJoints[i]; KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) { Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored)); Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored)); Transform poseTransform1 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel.GetBoneTransform(poseModel.GetBoneIndexByJoint(nextJoint, isMirrored)); if (avatarTransform1 != null && avatarTransform2 != null && poseTransform1 != null && poseTransform2 != null) { Vector3 vAvatarBone = (avatarTransform2.position - avatarTransform1.position).normalized; Vector3 vPoseBone = (poseTransform2.position - poseTransform1.position).normalized; float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; fMaxDiff += 90f; // we assume the max diff could be 90 degrees sbDetails.AppendFormat("{0} - {1:F0} deg.", joint, fDiff).AppendLine(); } else { sbDetails.AppendFormat("{0} - n/a", joint).AppendLine(); } } } poseModel.GetBoneTransform(0).rotation = poseSavedRotation; // calculate percent diff float fPercentDiff = 0f; if (bPercentDiff && fMaxDiff > 0f) { fPercentDiff = fAngleDiff / fMaxDiff; } // details info sbDetails.AppendLine(); sbDetails.AppendFormat("Sum-Diff: - {0:F0} deg out of {1:F0} deg", fAngleDiff, fMaxDiff).AppendLine(); sbDetails.AppendFormat("Percent-Diff: {0:F0}%", fPercentDiff * 100).AppendLine(); sDiffDetails = sbDetails.ToString(); return(bPercentDiff ? fPercentDiff : fAngleDiff); }
// gets angle or percent difference in pose public int GetMatchpose(bool isMirrored, bool bPercentDiff, ref string sDiffDetails) { float fAngleDiff = 0f; float fMaxDiff = 0f; sDiffDetails = string.Empty; KinectManager kinectManager = KinectManager.Instance; int AvatarNumb = -1; float fPercentDiff = 0f; float pre_fPercentDiff = 1f; List <float> fPercentDiff_L = new List <float>(); for (int Pi = 0; Pi < poseModel.Count; Pi++) { fAngleDiff = 0f; fMaxDiff = 0f; if (!kinectManager || !avatarModel || !poseModel[Pi] || poseJoints.Count == 0) { return(-1); } // copy model rotation , 모델의 회전 Quaternion poseSavedRotation = poseModel[Pi].GetBoneTransform(0).rotation; poseModel[Pi].GetBoneTransform(0).rotation = avatarModel.GetBoneTransform(0).rotation; StringBuilder sbDetails = new StringBuilder(); sbDetails.Append("Joint differences:").AppendLine(); for (int i = 0; i < poseJoints.Count; i++) { KinectInterop.JointType joint = poseJoints[i]; // 0번 척추부분, 4번부터 왼쪽 어깨, 8번부터 오른쪽 어깨 KinectInterop.JointType nextJoint = kinectManager.GetNextJoint(joint); if (nextJoint != joint && (int)nextJoint >= 0 && (int)nextJoint < KinectInterop.Constants.MaxJointCount) // 다음조인트와 같지않고, 척추가 아니며, 마지막 조인트가 아닐때 { Transform avatarTransform1 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(joint, isMirrored)); // 조인트 Transform avatarTransform2 = avatarModel.GetBoneTransform(avatarModel.GetBoneIndexByJoint(nextJoint, isMirrored)); // 다음조인트 Transform poseTransform1 = poseModel[Pi].GetBoneTransform(poseModel[Pi].GetBoneIndexByJoint(joint, isMirrored)); Transform poseTransform2 = poseModel[Pi].GetBoneTransform(poseModel[Pi].GetBoneIndexByJoint(nextJoint, isMirrored)); if (avatarTransform1 != null && avatarTransform2 != null && poseTransform1 != null && poseTransform2 != null) { // 이게 두 포즈의 본의 각도를 비교해서 차이를 검사하는듯 위치가 아니라, 그렇네 위치가 더 별로겠네 Vector3 vAvatarBone = (avatarTransform2.position - avatarTransform1.position).normalized; Vector3 vPoseBone = (poseTransform2.position - poseTransform1.position).normalized; float fDiff = Vector3.Angle(vPoseBone, vAvatarBone); if (fDiff > 90f) { fDiff = 90f; } fAngleDiff += fDiff; // 각도 차이의 총합을 구하는거같음. fMaxDiff += 90f; // we assume the max diff could be 90 degrees sbDetails.AppendFormat("{0} - {1:F0} deg.", joint, fDiff).AppendLine(); } else { sbDetails.AppendFormat("{0} - n/a", joint).AppendLine(); } } } poseModel[Pi].GetBoneTransform(0).rotation = poseSavedRotation; // calculate percent diff if (bPercentDiff && fMaxDiff > 0f) { fPercentDiff_L.Add(fPercentDiff); fPercentDiff = fAngleDiff / fMaxDiff; if (pre_fPercentDiff > fPercentDiff) { pre_fPercentDiff = fPercentDiff; if ((1f - pre_fPercentDiff) > matchThreshold) { AvatarNumb = Pi + 1; // 0번부터 검사하기 때문에 +1을 해야 맞음 } else { AvatarNumb = 0; // 0번은 아무것도 기본 포즈 } } } // details info sbDetails.AppendLine(); sbDetails.AppendFormat("Sum-Diff: - {0:F0} deg out of {1:F0} deg", fAngleDiff, fMaxDiff).AppendLine(); sbDetails.AppendFormat("Percent-Diff: {0:F0}%", fPercentDiff * 100).AppendLine(); sDiffDetails = sbDetails.ToString(); } fPercentDiff_L.Clear(); return(AvatarNumb); }