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

Fixed an issue that prevented importing Kinect Frame

parent ed3a1d8e
using NUnit.Framework;
using KinectModule;
using Windows.Kinect;
using KinectModule;
public class KinectAccessManagerTests
{
......
using NUnit.Framework;
using Windows.Kinect;
using KinectModule;
using System.Linq;
using System;
using System.Collections.Generic;
using KinectModule;
// To test the update function and the updated buffer, you need a stub for the source frame class.
......
......@@ -37,4 +37,4 @@ Material:
m_Floats:
- _Cutoff: 0
m_Colors:
- _Color: {r: 0, g: 1, b: 0, a: 1}
- _Color: {r: 0, g: 1, b: 0, a: 0}
......@@ -187,9 +187,8 @@ GameObject:
m_Component:
- component: {fileID: 750753103}
- component: {fileID: 750753108}
- component: {fileID: 750753104}
m_Layer: 5
m_Name: tmpManagers
m_Name: InGameManagers
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -213,7 +212,7 @@ RectTransform:
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &750753104
--- !u!114 &750753108
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
......@@ -221,21 +220,54 @@ MonoBehaviour:
m_GameObject: {fileID: 750753102}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a9d22e7eb4921ca47b18c68e43c0eb6e, type: 3}
m_Script: {fileID: 11500000, guid: 397b5aecede4d754abd2fba781e08f97, type: 3}
m_Name:
m_EditorClassIdentifier:
defaultSound: {fileID: 0}
--- !u!114 &750753108
--- !u!1 &1026530465
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1026530467}
- component: {fileID: 1026530466}
m_Layer: 5
m_Name: GameManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1026530466
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 750753102}
m_GameObject: {fileID: 1026530465}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 397b5aecede4d754abd2fba781e08f97, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!224 &1026530467
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1026530465}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &1217935565
GameObject:
m_ObjectHideFlags: 0
......
......@@ -128,18 +128,18 @@ namespace KinectModule
public const int ColorCount = 4;
public const int JointCount = 25;
}
public class SourceBuffer
{
private byte[] _ColorBuffer = new byte [KinectConstants.ColorWidth *
private byte[] _ColorBuffer = new byte[KinectConstants.ColorWidth *
KinectConstants.ColorHeight *
KinectConstants.ColorCount],
_BodyIndexBuffer = new byte [KinectConstants.DepthWidth *
_BodyIndexBuffer = new byte[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private ushort[] _DepthBuffer = new ushort[KinectConstants.DepthWidth *
private ushort[] _DepthBuffer = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private IBody[] _BodyBuffer = new IBody [KinectConstants.BodyCount];
private IBody[] _BodyBuffer = new IBody[KinectConstants.BodyCount];
public byte[] ColorBuffer
{ get { return _ColorBuffer; } }
public byte[] BodyIndexBuffer
......@@ -148,7 +148,7 @@ namespace KinectModule
{ get { return _DepthBuffer; } }
public IBody[] BodyBuffer
{ get { return _BodyBuffer; } }
public void UpdateBuffers(IMultiSourceFrame sourceFrame)
{
sourceFrame.LastColorFrame.CopyData(_ColorBuffer);
......@@ -157,17 +157,17 @@ namespace KinectModule
sourceFrame.LastBodyFrame.CopyData(_BodyBuffer);
}
}
public class KinectAccessManager : IDisposable
{
private KinectSensor _Sensor;
private CoordinateMapper _CoordinateMapper;
private MultiSourceFrameReader _SourceFrameReader;
private bool _IsSensorOpen = false;
private bool _IsFrameReaderOpen = false;
private bool _NullFrame = false;
public bool IsAccess
{ get { return _IsSensorOpen && _IsFrameReaderOpen; } }
public CoordinateMapper CoordinateMapper
......@@ -188,21 +188,21 @@ namespace KinectModule
}
public bool NullFrame
{ get { return _NullFrame; } }
public void Access()
{
OpenSensor();
OpenFrameReader();
_CoordinateMapper = _Sensor.CoordinateMapper;
}
private void OpenSensor()
{
if (_IsSensorOpen)
return;
_Sensor = KinectSensor.GetDefault();
if (_Sensor == null)
{
......@@ -210,7 +210,7 @@ namespace KinectModule
Debug.LogError("No ready Kinect found!");
return;
}
_Sensor.Open();
if (!_Sensor.IsOpen)
{
......@@ -219,30 +219,30 @@ namespace KinectModule
CloseSensor();
return;
}
_IsSensorOpen = true;
}
private void OpenFrameReader()
{
if (_IsFrameReaderOpen)
return;
_SourceFrameReader = _Sensor.OpenMultiSourceFrameReader(
FrameSourceTypes.Color |
FrameSourceTypes.Depth |
FrameSourceTypes.BodyIndex |
FrameSourceTypes.Body);
_IsFrameReaderOpen = (_SourceFrameReader != null);
}
public void Dispose()
{
CloseFrameReader();
CloseSensor();
}
private void CloseSensor()
{
if (_IsSensorOpen)
......@@ -251,7 +251,7 @@ namespace KinectModule
_IsSensorOpen = false;
}
}
private void CloseFrameReader()
{
if (_IsFrameReaderOpen)
......@@ -261,7 +261,7 @@ namespace KinectModule
}
}
}
public interface IMultiSourceFrame
{
IColorFrame LastColorFrame
......@@ -273,27 +273,27 @@ namespace KinectModule
IBodyFrame LastBodyFrame
{ get; }
}
public interface IColorFrame
{
void CopyData(byte[] buffer);
}
public interface IBodyIndexFrame
{
void CopyData(byte[] buffer);
}
public interface IDepthFrame
{
void CopyData(ushort[] buffer);
}
public interface IBodyFrame
{
void CopyData(IBody[] buffer);
}
public interface IBody : IEquatable<IBody>
{
bool IsTracked
......@@ -301,20 +301,20 @@ namespace KinectModule
Dictionary<JointType, IJoint> Joints
{ get; }
}
public interface IJoint : IEquatable<IJoint>
{
CameraSpacePoint Position
{ get; }
}
public class RealMultiSourceFrame : IMultiSourceFrame
{
RealColorFrame _ColorFrame;
RealBodyIndexFrame _BodyIndexFrame;
RealDepthFrame _DepthFrame;
RealBodyFrame _BodyFrame;
public IColorFrame LastColorFrame
{ get { return _ColorFrame; } }
public IBodyIndexFrame LastBodyIndexFrame
......@@ -323,119 +323,158 @@ namespace KinectModule
{ get { return _DepthFrame; } }
public IBodyFrame LastBodyFrame
{ get { return _BodyFrame; } }
public RealMultiSourceFrame(MultiSourceFrame sourceFrame)
{
_ColorFrame = new RealColorFrame(sourceFrame == null ? null :
sourceFrame.ColorFrameReference.AcquireFrame());
_BodyIndexFrame = new RealBodyIndexFrame(sourceFrame == null ? null :
sourceFrame.BodyIndexFrameReference.AcquireFrame());
_DepthFrame = new RealDepthFrame(sourceFrame == null ? null :
sourceFrame.DepthFrameReference.AcquireFrame());
_BodyFrame = new RealBodyFrame(sourceFrame == null ? null :
sourceFrame.BodyFrameReference.AcquireFrame());
if (sourceFrame == null)
{
_ColorFrame = new RealColorFrame(null);
_BodyIndexFrame = new RealBodyIndexFrame(null);
_DepthFrame = new RealDepthFrame(null);
_BodyFrame = new RealBodyFrame(null);
return;
}
using (var colorFrame = sourceFrame.ColorFrameReference.AcquireFrame())
_ColorFrame = new RealColorFrame(colorFrame);
using (var bodyIndexFrame = sourceFrame.BodyIndexFrameReference.AcquireFrame())
_BodyIndexFrame = new RealBodyIndexFrame(bodyIndexFrame);
using (var depthFrame = sourceFrame.DepthFrameReference.AcquireFrame())
_DepthFrame = new RealDepthFrame(depthFrame);
using (var bodyFrame = sourceFrame.BodyFrameReference.AcquireFrame())
_BodyFrame = new RealBodyFrame(bodyFrame);
}
private class RealColorFrame : IColorFrame
{
private ColorFrame _Frame;
private byte[] _Buffer = new byte[KinectConstants.ColorWidth *
KinectConstants.ColorHeight *
KinectConstants.ColorCount];
private bool IsValid = false;
public RealColorFrame(ColorFrame frame)
{
_Frame = frame;
}
public void CopyData(byte[] buffer)
{
if (_Frame != null)
if (frame != null)
{
var ColorData = GCHandle.Alloc(buffer, GCHandleType.Pinned);
_Frame.CopyConvertedFrameDataToIntPtr(ColorData.AddrOfPinnedObject(), (uint)buffer.Length, ColorImageFormat.Rgba);
var ColorData = GCHandle.Alloc(_Buffer, GCHandleType.Pinned);
frame.CopyConvertedFrameDataToIntPtr(ColorData.AddrOfPinnedObject(), (uint)_Buffer.Length, ColorImageFormat.Rgba);
ColorData.Free();
IsValid = true;
}
}
public void CopyData(byte[] buffer)
{
if (IsValid)
Array.Copy(_Buffer, buffer, _Buffer.Length);
}
}
private class RealBodyIndexFrame : IBodyIndexFrame
{
private BodyIndexFrame _Frame;
private byte[] _Buffer = new byte[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private bool IsValid = false;
public RealBodyIndexFrame(BodyIndexFrame frame)
{
_Frame = frame;
}
public void CopyData(byte[] buffer)
{
if (_Frame != null)
if (frame != null)
{
var BodyIndexData = GCHandle.Alloc(buffer, GCHandleType.Pinned);
_Frame.CopyFrameDataToIntPtr(BodyIndexData.AddrOfPinnedObject(), (uint)buffer.Length);
var BodyIndexData = GCHandle.Alloc(_Buffer, GCHandleType.Pinned);
frame.CopyFrameDataToIntPtr(BodyIndexData.AddrOfPinnedObject(), (uint)_Buffer.Length);
BodyIndexData.Free();
IsValid = true;
}
}
public void CopyData(byte[] buffer)
{
if (IsValid)
Array.Copy(_Buffer, buffer, _Buffer.Length);
}
}
private class RealDepthFrame : IDepthFrame
{
private DepthFrame _Frame;
private ushort[] _Buffer = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private bool IsValid = false;
public RealDepthFrame(DepthFrame frame)
{
_Frame = frame;
}
public void CopyData(ushort[] buffer)
{
if (_Frame != null)
if (frame != null)
{
var DepthData = GCHandle.Alloc(buffer, GCHandleType.Pinned);
_Frame.CopyFrameDataToIntPtr(DepthData.AddrOfPinnedObject(), (uint)buffer.Length* sizeof(ushort));
var DepthData = GCHandle.Alloc(_Buffer, GCHandleType.Pinned);
frame.CopyFrameDataToIntPtr(DepthData.AddrOfPinnedObject(), (uint)_Buffer.Length * sizeof(ushort));
DepthData.Free();
IsValid = true;
}
}
public void CopyData(ushort[] buffer)
{
if (IsValid)
Array.Copy(_Buffer, buffer, _Buffer.Length);
}
}
private class RealBodyFrame : IBodyFrame
{
private BodyFrame _Frame;
private Body[] _Buffer = new Body[KinectConstants.BodyCount];
private bool IsValid = false;
public RealBodyFrame(BodyFrame frame)
{
_Frame = frame;
if (frame != null)
{
frame.GetAndRefreshBodyData(_Buffer);
IsValid = true;
}
}
public void CopyData(IBody[] buffer)
{
if (_Frame != null)
_Frame.GetAndRefreshBodyData(buffer.Select(x => (x as RealBody).Value).ToList());
if (IsValid)
Enumerable.Range(0, KinectConstants.BodyCount)
.ToList()
.ForEach(x => buffer[x] = new RealBody(_Buffer[x]));
}
}
}
public class RealBody : IBody
{
private Body _Body;
public bool IsTracked
{ get { return _Body.IsTracked; } }
public Dictionary<JointType, IJoint> Joints
{ get { return _Body.Joints
{
get
{
return _Body.Joints
.Select(x => new KeyValuePair<JointType, IJoint>(x.Key, new RealJoint(x.Value)))
.ToDictionary(x => x.Key, x => 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);
......@@ -444,40 +483,40 @@ namespace KinectModule
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;
......
......@@ -35,6 +35,8 @@ GraphicsSettings:
- {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
......
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