/// <summary> /// Merges the user bodies, detected by the multiple sensors. /// </summary> public List <KinectInterop.BodyData> MergeUserBodies(ref ulong lastBodyFrameTime, BoneOrientationConstraints boneConstraints) { // get list of all bodies List <KinectInterop.BodyData> alAllBodies = GetAllBodiesList(ref lastBodyFrameTime, boneConstraints); // build mergeable body sets List <List <KinectInterop.BodyData> > mergeableBodySets = new List <List <KinectInterop.BodyData> >(); while (alAllBodies.Count > 0) { List <KinectInterop.BodyData> alCloseBodies = new List <KinectInterop.BodyData>(); alCloseBodies.Add(alAllBodies[0]); alAllBodies.RemoveAt(0); FindOverlappingBodies(ref alCloseBodies, ref alAllBodies, MAX_DISTANCE_TO_CLOSE_BODY, mergeableBodySets.Count); mergeableBodySets.Add(alCloseBodies); } // merge the bodies List <KinectInterop.BodyData> alMergedBodies = new List <KinectInterop.BodyData>(); for (int i = 0; i < adUserIdToSensorTrackingId.Length; i++) { adUserIdToSensorTrackingId[i].Clear(); } List <string> lostUsers = new List <string>(); lostUsers.AddRange(dictSensorUserIdToUserId.Keys); for (int i = 0; i < mergeableBodySets.Count; i++) { KinectInterop.BodyData mergedBody = GetMergedBody(mergeableBodySets[i], i, ref lostUsers); alMergedBodies.Add(mergedBody); } // clean up mergeableBodySets.Clear(); mergeableBodySets = null; alAllBodies = null; if (lostUsers.Count > 0) { foreach (string sensorUserId in lostUsers) { if (dictSensorUserIdToUserId.ContainsKey(sensorUserId) && (Time.time - dictSensorUserIdToLastUsed[sensorUserId]) >= WAIT_TIME_BEFORE_REMOVAL) { dictSensorUserIdToUserId.Remove(sensorUserId); dictSensorUserIdToFirstUsed.Remove(sensorUserId); dictSensorUserIdToLastUsed.Remove(sensorUserId); //Debug.Log("Removed lost sensor-user-id from dict: " + sensorUserId); } } lostUsers.Clear(); lostUsers = null; } return(alMergedBodies); }
// returns list of all bodies, tracked by all sensors private List <KinectInterop.BodyData> GetAllBodiesList(ref ulong lastBodyFrameTime, BoneOrientationConstraints boneConstraints) { List <KinectInterop.BodyData> alAllBodies = new List <KinectInterop.BodyData>(); for (int s = 0; s < sensorDatas.Count; s++) { if (SINGLE_SENSOR_INDEX >= 0 && s != SINGLE_SENSOR_INDEX) { continue; } KinectInterop.SensorData sensorData = sensorDatas[s]; uint sensorBodyCount = sensorData.trackedBodiesCount; if (sensorBodyCount > 0 && sensorData.lastBodyFrameTime > lastBodyFrameTime) { lastBodyFrameTime = sensorData.lastBodyFrameTime; } for (uint b = 0; b < sensorBodyCount; b++) { KinectInterop.BodyData bodyData = new KinectInterop.BodyData((int)KinectInterop.JointType.Count); sensorData.alTrackedBodies[b].CopyTo(ref bodyData); bodyData.sensorIndex = s; // filter orientation constraints if (boneConstraints != null) { boneConstraints.Constrain(ref bodyData); } alAllBodies.Add(bodyData); } //break; } if (alAllBodies.Count > 0) { //Debug.Log("Found " + alAllBodies.Count + " total bodies."); } return(alAllBodies); }