class Skeleton2DDataExtract
{
public delegate void Skeleton2DdataCoordEventHandler(object sender,BodydataCoordEventArgs a);
public static event Skeleton2DdataCoordEventHandler Skeleton2DdataCoordReady;
public static void ProcessData(KinectInterop.BodyData data)
{
Vector2 [] p = new Vector2[6];
Vector2 shoulderRight = new Vector2(), shoulderLeft = new Vector2();
for (int i = 0; i < data.joint.Length;i++)
{
JointType j = data.joint.jointType;
switch (j)
{
case JointType.HandLeft:
p[0] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.WristLeft:
p[1] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.ElbowLeft:
p[2] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.ElbowRight:
p[3] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.WristRight:
p[4] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.HandRight:
p[5] = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.ShoulderLeft:
shoulderLeft = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
case JointType.ShoulderRight:
shoulderRight = new Vector2(data.joint.kinectPos.x, data.joint.kinectPos.y);
break;
}
}
var center = new Vector2((shoulderLeft.x + shoulderRight.x) / 2, (shoulderLeft.y + shoulderRight.y) / 2);
for (int i = 0; i < 6; i++)
{
p.x -= center.x;
p.y -= center.y;
}
double shoulderDist =
Mathf.Sqrt(Mathf.Pow((shoulderLeft.x - shoulderRight.x), 2) +
Mathf.Pow((shoulderLeft.y - shoulderRight.y), 2));
for (int i = 0; i < 6; i++)
{
p.x /= (float)shoulderDist;
p.y /= (float)shoulderDist;
}
if (Skeleton2DdataCoordReady !=null)
Skeleton2DdataCoordReady(null, new BodydataCoordEventArgs(p));
}
}
public class BodydataCoordEventArgs
{
private readonly Vector2[] _points;
public BodydataCoordEventArgs(Vector2[] points)
{
_points = (Vector2[])points.Clone();
}
public Vector2 GetPoint(int index)
{
return _points[index];
}
internal List<double> GetCoords()
{
List<double> temp = new List<double>();
for (int i = 0; i < _points.Length; i++)
{
temp.Add(_points.x);
temp.Add(_points.y);
}
return temp;
}
}