From 646ac3414331238b84705447b99f520740f4daba Mon Sep 17 00:00:00 2001 From: Chikashi Miyama Date: Sun, 3 Nov 2019 15:19:11 +0100 Subject: [PATCH] refactor body tracking manager --- UnityProject/Assembly-CSharp-Editor.csproj | 1 - UnityProject/Assembly-CSharp.csproj | 2 +- .../Assets/Editor/UnitTest/UnitTest_Holder.cs | 62 ------------------- .../Editor/UnitTest/UnitTest_Holder.cs.meta | 3 - .../KinectSkeleton/script/Skeleton.cs | 26 +++----- .../Scripts/Math/{Holder.cs => BodyHolder.cs} | 22 +++---- .../{Holder.cs.meta => BodyHolder.cs.meta} | 0 .../VideoInput/KinectManagerBehaviour.cs | 34 ++++++---- 8 files changed, 43 insertions(+), 107 deletions(-) delete mode 100644 UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs delete mode 100644 UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs.meta rename UnityProject/Assets/Scripts/Math/{Holder.cs => BodyHolder.cs} (59%) rename UnityProject/Assets/Scripts/Math/{Holder.cs.meta => BodyHolder.cs.meta} (100%) diff --git a/UnityProject/Assembly-CSharp-Editor.csproj b/UnityProject/Assembly-CSharp-Editor.csproj index 83d906e..45876d2 100644 --- a/UnityProject/Assembly-CSharp-Editor.csproj +++ b/UnityProject/Assembly-CSharp-Editor.csproj @@ -66,7 +66,6 @@ - diff --git a/UnityProject/Assembly-CSharp.csproj b/UnityProject/Assembly-CSharp.csproj index 173e777..97c1e6b 100644 --- a/UnityProject/Assembly-CSharp.csproj +++ b/UnityProject/Assembly-CSharp.csproj @@ -74,7 +74,7 @@ - + diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs b/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs deleted file mode 100644 index a18819f..0000000 --- a/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs +++ /dev/null @@ -1,62 +0,0 @@ -using NUnit.Framework; - -namespace cylvester -{ - [TestFixture] - public class UnitTest_Holder - { - [Test] - public void Construction_capacity() - { - var holder = new Holder(2); - - holder.Add(1); - holder.Add(2); - holder.Add(3); - - Assert.AreEqual(0, holder.IndexOf(1)); - Assert.AreEqual(1, holder.IndexOf(2)); - Assert.IsNull(holder.IndexOf(3)); - } - - [Test] - public void Add() - { - var holder = new Holder(2); - holder.Add(102); - - Assert.AreEqual(0, holder.IndexOf(102)); - Assert.IsNull(holder.IndexOf(103)); - } - - [Test] - public void Add_return() - { - var holder = new Holder(2); - Assert.IsTrue(holder.Add(102)); - Assert.IsTrue(holder.Add(103)); - Assert.IsFalse(holder.Add(103)); - } - - [Test] - public void Add_unique() - { - var holder = new Holder(2); - holder.Add(102); - holder.Add(102); // doesn't affect - holder.Add(103); - - Assert.IsNull(holder.IndexOf(103)); - } - - [Test] - public void Remove() - { - var holder = new Holder(2); - holder.Add(102); - holder.Remove(102); - - Assert.IsNull(holder.IndexOf(102)); - } - } -} \ No newline at end of file diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs.meta b/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs.meta deleted file mode 100644 index cb588ee..0000000 --- a/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: e41847d6d84d439e858e4eadb0232998 -timeCreated: 1572778377 \ No newline at end of file diff --git a/UnityProject/Assets/Scenes/Examples/KinectSkeleton/script/Skeleton.cs b/UnityProject/Assets/Scenes/Examples/KinectSkeleton/script/Skeleton.cs index 708e8e0..c1aface 100644 --- a/UnityProject/Assets/Scenes/Examples/KinectSkeleton/script/Skeleton.cs +++ b/UnityProject/Assets/Scenes/Examples/KinectSkeleton/script/Skeleton.cs @@ -20,26 +20,20 @@ namespace cylvester } } - public void OnSkeletonFrameReceived(Body[] bodies) + public void OnSkeletonFrameReceived(Body body, int id) { - foreach (var body in bodies) + var i = 0; + foreach(var pair in body.Joints) { - if(!body.IsTracked) - continue; - - var i = 0; - foreach(var pair in body.Joints) + var joint = pair.Value; + if(joint.TrackingState == TrackingState.NotTracked) + balls_[i].SetActive(false); + else { - var joint = pair.Value; - if(joint.TrackingState == TrackingState.NotTracked) - balls_[i].SetActive(false); - else - { - balls_[i].SetActive(true); - balls_[i].transform.position = new Vector3(joint.Position.X * 10f , joint.Position.Y * 10f, 0f); - } - i++; + balls_[i].SetActive(true); + balls_[i].transform.position = new Vector3(joint.Position.X * 10f , joint.Position.Y * 10f, 0f); } + i++; } } } diff --git a/UnityProject/Assets/Scripts/Math/Holder.cs b/UnityProject/Assets/Scripts/Math/BodyHolder.cs similarity index 59% rename from UnityProject/Assets/Scripts/Math/Holder.cs rename to UnityProject/Assets/Scripts/Math/BodyHolder.cs index 0db5e5d..7337ea6 100644 --- a/UnityProject/Assets/Scripts/Math/Holder.cs +++ b/UnityProject/Assets/Scripts/Math/BodyHolder.cs @@ -1,43 +1,43 @@ -using System; using System.Collections.Generic; +using Windows.Kinect; namespace cylvester { - public class Holder where T : IEquatable + public class BodyHolder { - private readonly List elements_; + private readonly List elements_; private readonly int capacity_; - public Holder(int capacity) + public BodyHolder(int capacity) { capacity_ = capacity; - elements_ = new List(); + elements_ = new List(); } - public bool Add(T newElement) + public bool Add(Body newBody) { if (elements_.Count == capacity_) return false; - if (elements_.Contains(newElement)) + if (elements_.Contains(newBody)) return false; - elements_.Add(newElement); + elements_.Add(newBody); return true; } - public bool Exist(T element) + public bool Exist(Body element) { return elements_.Contains(element); } - public int? IndexOf(T element) + public int? IndexOf(Body element) { var index = elements_.FindIndex(e => e.Equals(element)); return (index < 0) ? (int?) null : index; } - public void Remove(T element) + public void Remove(Body element) { elements_.Remove(element); } diff --git a/UnityProject/Assets/Scripts/Math/Holder.cs.meta b/UnityProject/Assets/Scripts/Math/BodyHolder.cs.meta similarity index 100% rename from UnityProject/Assets/Scripts/Math/Holder.cs.meta rename to UnityProject/Assets/Scripts/Math/BodyHolder.cs.meta diff --git a/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs b/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs index 9611906..1675121 100644 --- a/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs +++ b/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs @@ -32,7 +32,7 @@ namespace cylvester private EventHandler onInfraredFrameArrived_; private EventHandler onBodyFrameArrived_; private EventHandler onBodyIndexFrameArrived_; - private Holder trackedIds_; + private BodyHolder trackedBodies_; private void Start() { @@ -86,7 +86,7 @@ namespace cylvester private void InitSkeletonTracking() { bodies_ = new Body[6]; - trackedIds_ = new Holder(numberOfBodiesTobeTracked); + trackedBodies_ = new BodyHolder(numberOfBodiesTobeTracked); InitBodyFrameReader(); } @@ -104,24 +104,32 @@ namespace cylvester return; bodyFrame.GetAndRefreshBodyData(bodies_); - foreach (var body in bodies_.Where(body => body.IsTracked)) + foreach (var body in bodies_.Where(t => t.IsTracked)) { - if (trackedIds_.Exist(body.TrackingId)) + if (body.IsTracked) { - var idNumber = trackedIds_.IndexOf(body.TrackingId); - if(idNumber.HasValue) - skeletonDataReceived.Invoke(body, idNumber.Value); - } - else - { - if (trackedIds_.Add(body.TrackingId)) + if (trackedBodies_.Exist(body)) { - var idNumber = trackedIds_.IndexOf(body.TrackingId); - if (idNumber.HasValue) + var idNumber = trackedBodies_.IndexOf(body); + if (idNumber.HasValue) skeletonDataReceived.Invoke(body, idNumber.Value); } + else + { + if (trackedBodies_.Add(body)) + { + var idNumber = trackedBodies_.IndexOf(body); + if (idNumber.HasValue) + skeletonDataReceived.Invoke(body, idNumber.Value); + } + } } } + + foreach (var body in bodies_.Where(t => !t.IsTracked && trackedBodies_.Exist(t))) + { + trackedBodies_.Remove(body); + } } }; bodyFrameReader_.FrameArrived += onBodyFrameArrived_;