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

Separate files by module, class, and interface

parent a38f2b6c
This diff is collapsed.
using System;
using System.Collections.Generic;
using Windows.Kinect;
namespace KinectModule
{
public interface IBody : IEquatable<IBody>
{
bool IsTracked
{ get; }
Dictionary<JointType, IJoint> Joints
{ get; }
}
}
namespace KinectModule
{
public interface IBodyFrame
{
void CopyData(IBody[] buffer);
}
}
\ No newline at end of file
namespace KinectModule
{
public interface IBodyIndexFrame
{
void CopyData(byte[] buffer);
}
}
\ No newline at end of file
namespace KinectModule
{
public interface IColorFrame
{
void CopyData(byte[] buffer);
}
}
\ No newline at end of file
namespace KinectModule
{
public interface IDepthFrame
{
void CopyData(ushort[] buffer);
}
}
\ No newline at end of file
using System;
using Windows.Kinect;
namespace KinectModule
{
public interface IJoint : IEquatable<IJoint>
{
CameraSpacePoint Position
{ get; }
}
}
\ No newline at end of file
namespace KinectModule
{
public interface IMultiSourceFrame
{
IColorFrame LastColorFrame
{ get; }
IBodyIndexFrame LastBodyIndexFrame
{ get; }
IDepthFrame LastDepthFrame
{ get; }
IBodyFrame LastBodyFrame
{ get; }
}
}
\ No newline at end of file
using System;
using UnityEngine;
using Windows.Kinect;
namespace KinectModule
{
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
{
get
{
return IsAccess ? _CoordinateMapper : null;
}
}
public MultiSourceFrame SourceFrame
{
get
{
if (IsAccess)
{
var val = _SourceFrameReader.AcquireLatestFrame();
_NullFrame = (val == null);
return val;
}
_NullFrame = true;
return null;
}
}
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)
{
//TODO: Popup Modal Window
Debug.LogError("No ready Kinect found!");
return;
}
_Sensor.Open();
if (!_Sensor.IsOpen)
{
//TODO: Popup Modal Window
Debug.LogError("Kinect sensor has problem!");
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)
{
_Sensor.Close();
_IsSensorOpen = false;
}
}
private void CloseFrameReader()
{
if (_IsFrameReaderOpen)
{
_SourceFrameReader.Dispose();
_IsFrameReaderOpen = false;
}
}
}
}
\ No newline at end of file
namespace KinectModule
{
public class KinectConstants
{
public const int DepthWidth = 512;
public const int DepthHeight = 424;
public const int ColorWidth = 1920;
public const int ColorHeight = 1080;
public const int BodyCount = 6;
public const int ColorCount = 4;
public const int JointCount = 25;
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using Windows.Kinect;
namespace KinectModule
{
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;
}
}
}
\ No newline at end of file
using System.Linq;
using Windows.Kinect;
namespace KinectModule
{
public class RealBodyFrame : IBodyFrame
{
private Body[] _Buffer = new Body[KinectConstants.BodyCount];
private bool IsValid = false;
public RealBodyFrame(BodyFrame frame)
{
if (frame != null)
{
frame.GetAndRefreshBodyData(_Buffer);
IsValid = true;
}
}
public void CopyData(IBody[] buffer)
{
if (IsValid)
Enumerable.Range(0, KinectConstants.BodyCount)
.ToList()
.ForEach(x => buffer[x] = new RealBody(_Buffer[x]));
}
}
}
\ No newline at end of file
using System;
using System.Runtime.InteropServices;
using Windows.Kinect;
namespace KinectModule
{
public class RealBodyIndexFrame : IBodyIndexFrame
{
private byte[] _Buffer = new byte[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private bool IsValid = false;
public RealBodyIndexFrame(BodyIndexFrame frame)
{
if (frame != null)
{
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);
}
}
}
\ No newline at end of file
using System;
using System.Runtime.InteropServices;
using Windows.Kinect;
namespace KinectModule
{
public class RealColorFrame : IColorFrame
{
private byte[] _Buffer = new byte[KinectConstants.ColorWidth *
KinectConstants.ColorHeight *
KinectConstants.ColorCount];
private bool IsValid = false;
public RealColorFrame(ColorFrame frame)
{
if (frame != null)
{
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);
}
}
}
\ No newline at end of file
using System;
using System.Runtime.InteropServices;
using Windows.Kinect;
namespace KinectModule
{
public class RealDepthFrame : IDepthFrame
{
private ushort[] _Buffer = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private bool IsValid = false;
public RealDepthFrame(DepthFrame frame)
{
if (frame != null)
{
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);
}
}
}
\ No newline at end of file
using System;
using Windows.Kinect;
namespace KinectModule
{
public class RealJoint : IJoint
{
private Joint _Joint;
public CameraSpacePoint Position
{
get
{
return _Joint.Position;
}
}
public RealJoint(Joint joint)
{
_Joint = joint;
}
public override bool Equals(object obj)
{
return 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
using Windows.Kinect;
namespace KinectModule
{
public class RealMultiSourceFrame : IMultiSourceFrame
{
RealColorFrame _ColorFrame;
RealBodyIndexFrame _BodyIndexFrame;
RealDepthFrame _DepthFrame;
RealBodyFrame _BodyFrame;
public IColorFrame LastColorFrame
{
get
{
return _ColorFrame;
}
}
public IBodyIndexFrame LastBodyIndexFrame
{
get
{
return _BodyIndexFrame;
}
}
public IDepthFrame LastDepthFrame
{
get
{
return _DepthFrame;
}
}
public IBodyFrame LastBodyFrame
{
get
{
return _BodyFrame;
}
}
public RealMultiSourceFrame(MultiSourceFrame sourceFrame)
{
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);
}
}
}
\ No newline at end of file
namespace KinectModule
{
public class SourceBuffer
{
private byte[] _ColorBuffer = new byte [KinectConstants.ColorWidth *
KinectConstants.ColorHeight *
KinectConstants.ColorCount],
_BodyIndexBuffer = new byte [KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private ushort[] _DepthBuffer = new ushort[KinectConstants.DepthWidth *
KinectConstants.DepthHeight];
private IBody[] _BodyBuffer = new IBody [KinectConstants.BodyCount];
public byte[] ColorBuffer
{
get
{
return _ColorBuffer;
}
}
public byte[] BodyIndexBuffer
{
get
{
return _BodyIndexBuffer;
}
}
public ushort[] DepthBuffer
{
get
{
return _DepthBuffer;
}
}
public IBody[] BodyBuffer
{
get
{
return _BodyBuffer;
}
}
public void UpdateBuffers(IMultiSourceFrame sourceFrame)
{
sourceFrame.LastColorFrame .CopyData(_ColorBuffer);
sourceFrame.LastBodyIndexFrame.CopyData(_BodyIndexBuffer);
sourceFrame.LastDepthFrame .CopyData(_DepthBuffer);
sourceFrame.LastBodyFrame .CopyData(_BodyBuffer);
}
}
}
\ No newline at end of file
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