diff --git a/UnityProject/Assembly-CSharp-Editor.csproj b/UnityProject/Assembly-CSharp-Editor.csproj index 45876d2..83d906e 100644 --- a/UnityProject/Assembly-CSharp-Editor.csproj +++ b/UnityProject/Assembly-CSharp-Editor.csproj @@ -66,6 +66,7 @@ + diff --git a/UnityProject/Assembly-CSharp.csproj b/UnityProject/Assembly-CSharp.csproj index 5426df1..173e777 100644 --- a/UnityProject/Assembly-CSharp.csproj +++ b/UnityProject/Assembly-CSharp.csproj @@ -74,6 +74,7 @@ + diff --git a/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs b/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs new file mode 100644 index 0000000..a18819f --- /dev/null +++ b/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs @@ -0,0 +1,62 @@ +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 new file mode 100644 index 0000000..cb588ee --- /dev/null +++ b/UnityProject/Assets/Editor/UnitTest/UnitTest_Holder.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e41847d6d84d439e858e4eadb0232998 +timeCreated: 1572778377 \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/Math/Holder.cs b/UnityProject/Assets/Scripts/Math/Holder.cs new file mode 100644 index 0000000..0db5e5d --- /dev/null +++ b/UnityProject/Assets/Scripts/Math/Holder.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; + +namespace cylvester +{ + public class Holder where T : IEquatable + { + private readonly List elements_; + private readonly int capacity_; + + public Holder(int capacity) + { + capacity_ = capacity; + elements_ = new List(); + } + + public bool Add(T newElement) + { + if (elements_.Count == capacity_) + return false; + + if (elements_.Contains(newElement)) + return false; + + elements_.Add(newElement); + return true; + } + + public bool Exist(T element) + { + return elements_.Contains(element); + } + + public int? IndexOf(T element) + { + var index = elements_.FindIndex(e => e.Equals(element)); + return (index < 0) ? (int?) null : index; + } + + public void Remove(T element) + { + elements_.Remove(element); + } + } +} \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/Math/Holder.cs.meta b/UnityProject/Assets/Scripts/Math/Holder.cs.meta new file mode 100644 index 0000000..cfa141f --- /dev/null +++ b/UnityProject/Assets/Scripts/Math/Holder.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 200646bf9dc64be3905b4ac8dfef1634 +timeCreated: 1572777412 \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/UnitTest.meta b/UnityProject/Assets/Scripts/UnitTest.meta deleted file mode 100644 index 64e07e5..0000000 --- a/UnityProject/Assets/Scripts/UnitTest.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 14b20e54517dff5419ed18c623ac9653 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs b/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs index 90619dc..9611906 100644 --- a/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs +++ b/UnityProject/Assets/Scripts/VideoInput/KinectManagerBehaviour.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using Windows.Kinect; using UnityEngine; using UnityEngine.Events; @@ -7,7 +8,7 @@ using UnityEngine.Events; namespace cylvester { [Serializable] public class UnityInfraredCameraEvent : UnityEvent{ } - [Serializable] public class UnitySkeletonEvent : UnityEvent{ } + [Serializable] public class UnitySkeletonEvent : UnityEvent{ } public class KinectManagerBehaviour : MonoBehaviour { @@ -16,17 +17,22 @@ namespace cylvester [SerializeField] private bool skeleton; [SerializeField] public UnitySkeletonEvent skeletonDataReceived; + + [SerializeField, Range(1, 6)] private int numberOfBodiesTobeTracked = 2; private KinectSensor sensor_; private InfraredFrameReader infraredFrameReader_; private BodyFrameReader bodyFrameReader_; + private BodyIndexFrameReader bodyIndexFrameReader_; private ushort [] irData_; private Texture2D infraredTexture_; private Body[] bodies_; private EventHandler onInfraredFrameArrived_; - private EventHandler onSkeletonFrameArrived_; + private EventHandler onBodyFrameArrived_; + private EventHandler onBodyIndexFrameArrived_; + private Holder trackedIds_; private void Start() { @@ -36,9 +42,11 @@ namespace cylvester InitInfraredCamera(); InitSkeletonTracking(); - + if (!sensor_.IsOpen) + { sensor_.Open(); + } } private void InitInfraredCamera() @@ -77,10 +85,15 @@ namespace cylvester private void InitSkeletonTracking() { - bodies_ = new Body[1]; + bodies_ = new Body[6]; + trackedIds_ = new Holder(numberOfBodiesTobeTracked); + InitBodyFrameReader(); + } + private void InitBodyFrameReader() + { bodyFrameReader_ = sensor_.BodyFrameSource.OpenReader(); - onSkeletonFrameArrived_ = (frameReader, eventArgs) => + onBodyFrameArrived_ = (frameReader, eventArgs) => { if(!skeleton) return; @@ -89,18 +102,35 @@ namespace cylvester { if (bodyFrame == null) return; - Array.Resize(ref bodies_, bodyFrame.BodyCount); + bodyFrame.GetAndRefreshBodyData(bodies_); - skeletonDataReceived.Invoke(bodies_); + foreach (var body in bodies_.Where(body => body.IsTracked)) + { + if (trackedIds_.Exist(body.TrackingId)) + { + var idNumber = trackedIds_.IndexOf(body.TrackingId); + if(idNumber.HasValue) + skeletonDataReceived.Invoke(body, idNumber.Value); + } + else + { + if (trackedIds_.Add(body.TrackingId)) + { + var idNumber = trackedIds_.IndexOf(body.TrackingId); + if (idNumber.HasValue) + skeletonDataReceived.Invoke(body, idNumber.Value); + } + } + } } }; - bodyFrameReader_.FrameArrived += onSkeletonFrameArrived_; + bodyFrameReader_.FrameArrived += onBodyFrameArrived_; } - + private void OnDestroy() { infraredFrameReader_.FrameArrived -= onInfraredFrameArrived_; - bodyFrameReader_.FrameArrived -= onSkeletonFrameArrived_; + bodyFrameReader_.FrameArrived -= onBodyFrameArrived_; } } }