refactor body id structure

This commit is contained in:
Chikashi Miyama 2019-11-03 13:29:35 +01:00
parent 66a98d60ba
commit be8a4dd1c0
8 changed files with 155 additions and 18 deletions

View file

@ -66,6 +66,7 @@
<Compile Include="Assets\Editor\SpectrumGeneratorEditMode.cs" />
<Compile Include="Assets\Editor\StateManagerEditor.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_Holder.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_RectangularSelection.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGeneratorEditMode.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGeneratorPlayMode.cs" />

View file

@ -74,6 +74,7 @@
<Compile Include="Assets\Scenes\Examples\TimbreID\script\IdToText.cs" />
<Compile Include="Assets\Scenes\Examples\VisualEffectGraph\script\FlareBind.cs" />
<Compile Include="Assets\Scenes\Examples\VisualEffectGraph\script\SmokeBind.cs" />
<Compile Include="Assets\Scripts\Math\Holder.cs" />
<Compile Include="Assets\Scripts\Math\Smoother.cs" />
<Compile Include="Assets\Scripts\Math\Threshold.cs" />
<Compile Include="Assets\Scripts\PdConnection\DspController.cs" />

View file

@ -0,0 +1,62 @@
using NUnit.Framework;
namespace cylvester
{
[TestFixture]
public class UnitTest_Holder
{
[Test]
public void Construction_capacity()
{
var holder = new Holder<int>(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<int>(2);
holder.Add(102);
Assert.AreEqual(0, holder.IndexOf(102));
Assert.IsNull(holder.IndexOf(103));
}
[Test]
public void Add_return()
{
var holder = new Holder<int>(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<int>(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<int>(2);
holder.Add(102);
holder.Remove(102);
Assert.IsNull(holder.IndexOf(102));
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e41847d6d84d439e858e4eadb0232998
timeCreated: 1572778377

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace cylvester
{
public class Holder<T> where T : IEquatable<T>
{
private readonly List<T> elements_;
private readonly int capacity_;
public Holder(int capacity)
{
capacity_ = capacity;
elements_ = new List<T>();
}
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);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 200646bf9dc64be3905b4ac8dfef1634
timeCreated: 1572777412

View file

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 14b20e54517dff5419ed18c623ac9653
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -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<Texture2D>{ }
[Serializable] public class UnitySkeletonEvent : UnityEvent<Body[]>{ }
[Serializable] public class UnitySkeletonEvent : UnityEvent<Body, int>{ }
public class KinectManagerBehaviour : MonoBehaviour
{
@ -17,16 +18,21 @@ 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<InfraredFrameArrivedEventArgs> onInfraredFrameArrived_;
private EventHandler<BodyFrameArrivedEventArgs> onSkeletonFrameArrived_;
private EventHandler<BodyFrameArrivedEventArgs> onBodyFrameArrived_;
private EventHandler<BodyIndexFrameArrivedEventArgs> onBodyIndexFrameArrived_;
private Holder<ulong> trackedIds_;
private void Start()
{
@ -38,8 +44,10 @@ namespace cylvester
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<ulong>(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_;
}
}
}