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\SpectrumGeneratorEditMode.cs" />
<Compile Include="Assets\Editor\StateManagerEditor.cs" /> <Compile Include="Assets\Editor\StateManagerEditor.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.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_RectangularSelection.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGeneratorEditMode.cs" /> <Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGeneratorEditMode.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_SpectrumGeneratorPlayMode.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\TimbreID\script\IdToText.cs" />
<Compile Include="Assets\Scenes\Examples\VisualEffectGraph\script\FlareBind.cs" /> <Compile Include="Assets\Scenes\Examples\VisualEffectGraph\script\FlareBind.cs" />
<Compile Include="Assets\Scenes\Examples\VisualEffectGraph\script\SmokeBind.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\Smoother.cs" />
<Compile Include="Assets\Scripts\Math\Threshold.cs" /> <Compile Include="Assets\Scripts\Math\Threshold.cs" />
<Compile Include="Assets\Scripts\PdConnection\DspController.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,13 +20,8 @@ namespace cylvester
} }
} }
public void OnSkeletonFrameReceived(Body[] bodies) public void OnSkeletonFrameReceived(Body body, int id)
{ {
foreach (var body in bodies)
{
if(!body.IsTracked)
continue;
var i = 0; var i = 0;
foreach(var pair in body.Joints) foreach(var pair in body.Joints)
{ {
@ -43,4 +38,3 @@ namespace cylvester
} }
} }
} }
}

View file

@ -1,43 +1,43 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Windows.Kinect;
namespace cylvester 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_; private readonly int capacity_;
public Holder(int capacity) public BodyHolder(int capacity)
{ {
capacity_ = 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_) if (elements_.Count == capacity_)
return false; return false;
if (elements_.Contains(newElement)) if (elements_.Contains(newBody))
return false; return false;
elements_.Add(newElement); elements_.Add(newBody);
return true; return true;
} }
public bool Exist(T element) public bool Exist(Body element)
{ {
return elements_.Contains(element); return elements_.Contains(element);
} }
public int? IndexOf(T element) public int? IndexOf(Body element)
{ {
var index = elements_.FindIndex(e => e.Equals(element)); var index = elements_.FindIndex(e => e.Equals(element));
return (index < 0) ? (int?) null : index; return (index < 0) ? (int?) null : index;
} }
public void Remove(T element) public void Remove(Body element)
{ {
elements_.Remove(element); elements_.Remove(element);
} }

View file

@ -32,7 +32,7 @@ namespace cylvester
private EventHandler<InfraredFrameArrivedEventArgs> onInfraredFrameArrived_; private EventHandler<InfraredFrameArrivedEventArgs> onInfraredFrameArrived_;
private EventHandler<BodyFrameArrivedEventArgs> onBodyFrameArrived_; private EventHandler<BodyFrameArrivedEventArgs> onBodyFrameArrived_;
private EventHandler<BodyIndexFrameArrivedEventArgs> onBodyIndexFrameArrived_; private EventHandler<BodyIndexFrameArrivedEventArgs> onBodyIndexFrameArrived_;
private Holder<ulong> trackedIds_; private BodyHolder trackedBodies_;
private void Start() private void Start()
{ {
@ -86,7 +86,7 @@ namespace cylvester
private void InitSkeletonTracking() private void InitSkeletonTracking()
{ {
bodies_ = new Body[6]; bodies_ = new Body[6];
trackedIds_ = new Holder<ulong>(numberOfBodiesTobeTracked); trackedBodies_ = new BodyHolder(numberOfBodiesTobeTracked);
InitBodyFrameReader(); InitBodyFrameReader();
} }
@ -104,25 +104,33 @@ namespace cylvester
return; return;
bodyFrame.GetAndRefreshBodyData(bodies_); 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 (trackedBodies_.Exist(body))
{
var idNumber = trackedBodies_.IndexOf(body);
if (idNumber.HasValue) if (idNumber.HasValue)
skeletonDataReceived.Invoke(body, idNumber.Value); skeletonDataReceived.Invoke(body, idNumber.Value);
} }
else else
{ {
if (trackedIds_.Add(body.TrackingId)) if (trackedBodies_.Add(body))
{ {
var idNumber = trackedIds_.IndexOf(body.TrackingId); var idNumber = trackedBodies_.IndexOf(body);
if (idNumber.HasValue) if (idNumber.HasValue)
skeletonDataReceived.Invoke(body, idNumber.Value); skeletonDataReceived.Invoke(body, idNumber.Value);
} }
} }
} }
} }
foreach (var body in bodies_.Where(t => !t.IsTracked && trackedBodies_.Exist(t)))
{
trackedBodies_.Remove(body);
}
}
}; };
bodyFrameReader_.FrameArrived += onBodyFrameArrived_; bodyFrameReader_.FrameArrived += onBodyFrameArrived_;
} }