Skip to content

Instantly share code, notes, and snippets.

@andiCR
Last active August 29, 2015 14:05
Show Gist options
  • Save andiCR/6d21074f1a0883e4f88b to your computer and use it in GitHub Desktop.
Save andiCR/6d21074f1a0883e4f88b to your computer and use it in GitHub Desktop.
Kinect function to compare two bodies (recordedjoints is a variable, joints is a function parameter)
double CompareCurrentBody(IReadOnlyDictionary<JointType, Joint> joints)
{
if (recordedJointPoints == null)
throw new Exception("No recorded points");
var jointConnections = new List<Tuple<JointType, JointType>>()
{
// Torso
new Tuple<JointType, JointType> (JointType.Head, JointType.Neck),
new Tuple<JointType, JointType> (JointType.Neck, JointType.SpineShoulder),
new Tuple<JointType, JointType> (JointType.SpineShoulder, JointType.SpineMid),
new Tuple<JointType, JointType> (JointType.SpineMid, JointType.SpineBase),
new Tuple<JointType, JointType> (JointType.SpineShoulder, JointType.ShoulderRight),
new Tuple<JointType, JointType> (JointType.SpineShoulder, JointType.ShoulderLeft),
new Tuple<JointType, JointType> (JointType.SpineBase, JointType.HipRight),
new Tuple<JointType, JointType> (JointType.SpineBase, JointType.HipLeft),
// Right Arm
new Tuple<JointType, JointType> (JointType.ShoulderRight, JointType.ElbowRight),
new Tuple<JointType, JointType> (JointType.ElbowRight, JointType.WristRight),
new Tuple<JointType, JointType> (JointType.WristRight, JointType.HandRight),
new Tuple<JointType, JointType> (JointType.HandRight, JointType.HandTipRight),
new Tuple<JointType, JointType> (JointType.WristRight, JointType.ThumbRight),
// Left Arm
new Tuple<JointType, JointType> (JointType.ShoulderLeft, JointType.ElbowLeft),
new Tuple<JointType, JointType> (JointType.ElbowLeft, JointType.WristLeft),
new Tuple<JointType, JointType> (JointType.WristLeft, JointType.HandLeft),
new Tuple<JointType, JointType> (JointType.HandLeft, JointType.HandTipLeft),
new Tuple<JointType, JointType> (JointType.WristLeft, JointType.ThumbLeft),
// Right Leg
new Tuple<JointType, JointType> (JointType.HipRight, JointType.KneeRight),
new Tuple<JointType, JointType> (JointType.KneeRight, JointType.AnkleRight),
new Tuple<JointType, JointType> (JointType.AnkleRight, JointType.FootRight),
// Left leg
new Tuple<JointType, JointType> (JointType.HipLeft, JointType.KneeLeft),
new Tuple<JointType, JointType> (JointType.KneeLeft, JointType.AnkleLeft),
new Tuple<JointType, JointType> (JointType.AnkleLeft, JointType.FootLeft)
};
List<double> dots = new List<double>();
Vector3D v1, v2;
// Go through all connections
foreach (var connection in jointConnections)
{
// Ignore if we are not tracking this connection
if (joints[connection.Item1].TrackingState == TrackingState.NotTracked ||
joints[connection.Item2].TrackingState == TrackingState.NotTracked ||
recordedJointPoints[connection.Item1].TrackingState == TrackingState.NotTracked ||
recordedJointPoints[connection.Item2].TrackingState == TrackingState.NotTracked)
{
continue;
}
// Get vectors from each connection
v1 = Vector(connection.Item1, connection.Item2, joints);
v2 = Vector(connection.Item1, connection.Item2, recordedJointPoints);
// Compare with dot product
dots.Add(Vector3D.DotProduct(v1, v2));
}
// Return the average of dot products
return Math.Max(0, dots.Average());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment