Commit f8d04f21 authored by 16이상민's avatar 16이상민

Test Complete for class 'SourceBuffer'

parent 098e2943
......@@ -3,6 +3,7 @@ using Windows.Kinect;
using KinectModule;
using System.Linq;
using System;
using System.Collections.Generic;
// To test the update function and the updated buffer, you need a stub for the source frame class.
......@@ -70,7 +71,66 @@ public class SourceBufferTests
var actual = obj.ColorBuffer;
Assert.AreEqual(expected, actual, "Colorbuffer should be filled with green.");
Assert.AreEqual(expected, actual, "ColorBuffer should be filled with green.");
}
[Test]
public void BodyIndexBuffer_Equal_Filled_1()
{
var frame = new MultiSourceFrameStub(new ColorFrameDummy(),
new Fill1BodyIndexFrameStub(),
new DepthFrameDummy(),
new BodyFrameDummy());
var obj = new SourceBuffer();
obj.UpdateBuffers(frame);
var expected = new byte[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
frame.LastBodyIndexFrame.CopyData(expected);
var actual = obj.BodyIndexBuffer;
Assert.AreEqual(expected, actual, "BodyIndexBuffer should be filled with 1.");
}
[Test]
public void DepthBuffer_Equal_Filled_8000()
{
var frame = new MultiSourceFrameStub(new ColorFrameDummy(),
new BodyIndexFrameDummy(),
new Fill8000DepthFrameStub(),
new BodyFrameDummy());
var obj = new SourceBuffer();
obj.UpdateBuffers(frame);
var expected = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
frame.LastDepthFrame.CopyData(expected);
var actual = obj.DepthBuffer;
Assert.AreEqual(expected, actual, "DepthBuffer should be filled with 8000.");
}
[Test]
public void BodyBuffer_Equal_Clean()
{
var frame = new MultiSourceFrameStub(new ColorFrameDummy(),
new BodyIndexFrameDummy(),
new DepthFrameDummy(),
new CleanBodyFrameStub());
var obj = new SourceBuffer();
obj.UpdateBuffers(frame);
var expected = new IBody[KinectConstants.BodyCount];
frame.LastBodyFrame.CopyData(expected);
var actual = obj.BodyBuffer;
Assert.AreEqual(expected, actual, "BodyBuffer should be clean.");
}
}
......@@ -107,7 +167,7 @@ public class GreenColorFrameStub : IColorFrame
private static uint[] data =
Enumerable.Repeat<uint>(0x00FF00FF,
KinectConstants.ColorWidth *
KinectConstants.ColorHeight).ToArray<uint>();
KinectConstants.ColorHeight).ToArray();
public void CopyData(byte[] buffer)
{
......@@ -115,6 +175,41 @@ public class GreenColorFrameStub : IColorFrame
}
}
public class Fill1BodyIndexFrameStub : IBodyIndexFrame
{
public void CopyData(byte[] buffer)
{
for (int i = 0; i < buffer.Length; ++i)
buffer[i] = 1;
}
}
public class Fill8000DepthFrameStub : IDepthFrame
{
public void CopyData(ushort[] buffer)
{
for (int i = 0; i < buffer.Length; ++i)
buffer[i] = 8000;
}
}
public class CleanBodyFrameStub : IBodyFrame
{
public void CopyData(IBody[] buffer)
{
for (int i = 0; i < buffer.Length; ++i)
{
var tmp = new BodyFake();
var joints = tmp.Joints;
for(int j = 0; j < KinectConstants.JointCount; ++j)
joints[joints.ElementAt(j).Key] = new JointStub();
tmp.IsTracked = false;
buffer[i] = tmp;
}
}
}
public class ColorFrameDummy : IColorFrame
{
public void CopyData(byte[] buffer)
......@@ -135,6 +230,105 @@ public class DepthFrameDummy : IDepthFrame
public class BodyFrameDummy : IBodyFrame
{
public void CopyData(Body[] buffer)
public void CopyData(IBody[] buffer)
{ }
}
public class BodyFake : IBody
{
private bool _IsTracked = false;
private Dictionary<JointType, IJoint> _Joints = new Dictionary<JointType, IJoint>
{
{ JointType.AnkleLeft, new JointStub() },
{ JointType.AnkleRight, new JointStub() },
{ JointType.ElbowLeft, new JointStub() },
{ JointType.ElbowRight, new JointStub() },
{ JointType.FootLeft, new JointStub() },
{ JointType.FootRight, new JointStub() },
{ JointType.HandLeft, new JointStub() },
{ JointType.HandRight, new JointStub() },
{ JointType.HandTipLeft, new JointStub() },
{ JointType.HandTipRight, new JointStub() },
{ JointType.Head, new JointStub() },
{ JointType.HipLeft, new JointStub() },
{ JointType.HipRight, new JointStub() },
{ JointType.KneeLeft, new JointStub() },
{ JointType.KneeRight, new JointStub() },
{ JointType.Neck, new JointStub() },
{ JointType.ShoulderLeft, new JointStub() },
{ JointType.ShoulderRight, new JointStub() },
{ JointType.SpineBase, new JointStub() },
{ JointType.SpineMid, new JointStub() },
{ JointType.SpineShoulder, new JointStub() },
{ JointType.ThumbLeft, new JointStub() },
{ JointType.ThumbRight, new JointStub() },
{ JointType.WristLeft, new JointStub() },
{ JointType.WristRight, new JointStub() }
};
public bool IsTracked
{
get
{
return _IsTracked;
}
set
{
_IsTracked = value;
}
}
public Dictionary<JointType, IJoint> Joints
{
get
{
return _Joints;
}
}
public override bool Equals(object obj)
{
return this.Equals(obj as IBody);
}
public bool Equals(IBody obj)
{
var keys = Joints.Select(x => x.Key);
foreach (var key in keys)
{
if (!Joints[key].Equals(obj.Joints[key]))
return false;
}
return IsTracked.Equals(obj.IsTracked);
}
public override int GetHashCode()
{
return 0;
}
}
public class JointStub : IJoint
{
private CameraSpacePoint _Position = new CameraSpacePoint { X = 0, Y = 0, Z = 0 };
public CameraSpacePoint Position
{ get { return _Position; } }
public override bool Equals(object obj)
{
return this.Equals(obj as IJoint);
}
public bool Equals(IJoint obj)
{
return Math.Abs(Position.X - obj.Position.X) < 0.05 &&
Math.Abs(Position.Y - obj.Position.Y) < 0.05 &&
Math.Abs(Position.Z - obj.Position.Z) < 0.05;
}
public override int GetHashCode()
{
return (int)Position.X + (int)Position.Y + (int)Position.Z;
}
}
\ No newline at end of file
......@@ -26,7 +26,7 @@ public class CoordinateMapperManager : MonoBehaviour
{ get { return _SourceBuffer.BodyIndexBuffer; } }
public DepthSpacePoint[] DepthCoordinates
{ get { return _DepthCoordinates; } }
public Body[] BodyBuffer
public KinectModule.IBody[] BodyBuffer
{ get { return _SourceBuffer.BodyBuffer; } }
void Awake()
......@@ -114,6 +114,7 @@ namespace KinectModule
public const int ColorHeight = 1080;
public const int BodyCount = 6;
public const int ColorCount = 4;
public const int JointCount = 25;
}
public class SourceBuffer
......@@ -125,7 +126,7 @@ namespace KinectModule
KinectConstants.DepthHeight];
private ushort[] _DepthBuffer = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private Body[] _BodyBuffer = new Body[KinectConstants.BodyCount];
private IBody[] _BodyBuffer = new IBody[KinectConstants.BodyCount];
// 1. Do not return null values.
// 2. The updated value should reflect the data of the source frame.
......@@ -136,7 +137,7 @@ namespace KinectModule
{ get { return _BodyIndexBuffer; } }
public ushort[] DepthBuffer
{ get { return _DepthBuffer; } }
public Body[] BodyBuffer
public IBody[] BodyBuffer
{ get { return _BodyBuffer; } }
// 1. The data of the source frame must be reflected in the buffer.
......@@ -283,7 +284,21 @@ namespace KinectModule
public interface IBodyFrame
{
void CopyData(Body[] buffer);
void CopyData(IBody[] buffer);
}
public interface IBody : IEquatable<IBody>
{
bool IsTracked
{ get; }
Dictionary<JointType, IJoint> Joints
{ get; }
}
public interface IJoint : IEquatable<IJoint>
{
CameraSpacePoint Position
{ get; }
}
public class RealMultiSourceFrame : IMultiSourceFrame
......@@ -381,11 +396,82 @@ namespace KinectModule
_Frame = frame;
}
public void CopyData(Body[] buffer)
public void CopyData(IBody[] buffer)
{
if (_Frame != null)
_Frame.GetAndRefreshBodyData(buffer);
_Frame.GetAndRefreshBodyData(buffer.Select(x => (x as RealBody).Value).ToList());
}
}
}
public class RealBody : IBody
{
private Body _Body;
public bool IsTracked
{ get { return _Body.IsTracked; } }
public Dictionary<JointType, IJoint> Joints
{ get { return _Body.Joints
.Select(x => new KeyValuePair<JointType, IJoint>(x.Key, new RealJoint(x.Value)))
.ToDictionary(x => x.Key, x => x.Value); } }
public Body Value
{ get { return _Body; } }
public RealBody(Body body)
{
_Body = body;
}
public override bool Equals(object obj)
{
return this.Equals(obj as IBody);
}
public bool Equals(IBody obj)
{
var keys = Joints.Select(x => x.Key);
foreach (var key in keys)
{
if (!Joints[key].Equals(obj.Joints[key]))
return false;
}
return IsTracked.Equals(obj.IsTracked);
}
public override int GetHashCode()
{
return 0;
}
}
public class RealJoint : IJoint
{
private Windows.Kinect.Joint _Joint;
public CameraSpacePoint Position
{ get { return _Joint.Position; } }
public RealJoint(Windows.Kinect.Joint joint)
{
_Joint = joint;
}
public override bool Equals(object obj)
{
return this.Equals(obj as IJoint);
}
public bool Equals(IJoint obj)
{
return Math.Abs(Position.X - obj.Position.X) < 0.05 &&
Math.Abs(Position.Y - obj.Position.Y) < 0.05 &&
Math.Abs(Position.Z - obj.Position.Z) < 0.05;
}
public override int GetHashCode()
{
return (int)Position.X + (int)Position.Y + (int)Position.Z;
}
}
}
......@@ -30,7 +30,6 @@ public class InputManager : MonoBehaviour {
}
public MotionState CurrentMotionState { get; set; }
public Dictionary<JointType, Windows.Kinect.Joint> Joints { get; set; }
public bool IsButtonPressed
{
......
......@@ -9,7 +9,7 @@ public class MotionView : MonoBehaviour {
Pair<float, MotionState> _Recent;
public GameObject CoordinateMapperManager;
CoordinateMapperManager _coordinateMapperManager;
Body[] body;
KinectModule.IBody[] body;
CameraSpacePoint spineMidRecent,
handLeftRecent,
handRightRecent,
......@@ -146,8 +146,6 @@ public class MotionView : MonoBehaviour {
if (kneeLeft.Y - kneeLeftBase.Y >= 0.5f
|| kneeRight.Y - kneeRightBase.Y >= 0.5f)
s |= MotionState.ON_THE_TABLE;
InputManager.Instance.Joints = body[idx].Joints;
spineMidRecent = spineMid;
handLeftRecent = handLeft;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment