refactor body tracking manager

This commit is contained in:
Chikashi Miyama 2019-11-03 15:19:11 +01:00
parent be8a4dd1c0
commit 646ac34143
8 changed files with 43 additions and 107 deletions

View file

@ -66,7 +66,6 @@
<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,7 +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\BodyHolder.cs" />
<Compile Include="Assets\Scripts\Math\Smoother.cs" />
<Compile Include="Assets\Scripts\Math\Threshold.cs" />
<Compile Include="Assets\Scripts\PdConnection\DspController.cs" />

View file

@ -1,62 +0,0 @@
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

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

View file

@ -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++;
}
}
}

View file

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

View file

@ -32,7 +32,7 @@ namespace cylvester
private EventHandler<InfraredFrameArrivedEventArgs> onInfraredFrameArrived_;
private EventHandler<BodyFrameArrivedEventArgs> onBodyFrameArrived_;
private EventHandler<BodyIndexFrameArrivedEventArgs> onBodyIndexFrameArrived_;
private Holder<ulong> trackedIds_;
private BodyHolder trackedBodies_;
private void Start()
{
@ -86,7 +86,7 @@ namespace cylvester
private void InitSkeletonTracking()
{
bodies_ = new Body[6];
trackedIds_ = new Holder<ulong>(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_;