Commit c2c3a593 authored by Alexander Krause's avatar Alexander Krause

Refactoring GestureDatabase

parent acc8acda
......@@ -13,8 +13,6 @@ namespace ExplorVizGestureControl.Gestures
{
public static class GestureDatabase
{
#region Variables
public struct PhIZ
{
public Point center;
......@@ -25,40 +23,21 @@ namespace ExplorVizGestureControl.Gestures
public double z;
}
#region Variables
private static IReadOnlyDictionary<string, Func<bool>> gestureDictionary;
public static Vector3D elbowLeft;
public static Vector3D elbowRight;
public static Vector3D handLeft;
public static Vector3D handRight;
public static Vector3D shoulderLeft;
public static Vector3D shoulderRight;
public static Vector3D wristRight;
public static Vector3D wristLeft;
public static Vector3D spineMid;
public static Vector3D spineBase;
public static Vector3D head;
public static Vector3D oldSpineBase;
public static Vector3D handRightTip;
public static Vector3D handLeftTip;
public static PhIZ phiz;
public static CameraSpacePoint[] joints;
private static CameraSpacePoint[] joints;
private static CameraSpacePoint[] previousFrameJoints;
public static HandState handRightState;
public static HandState handLeftState;
private static int maxCapacity;
private static int newestOldHead = 0;
private static Vector3D[] headsStack;
private static int[] gesturesProgress;
private static CameraSpacePoint[] headsStack;
private static int[] flags;
#endregion
#region Initialization
......@@ -69,17 +48,15 @@ namespace ExplorVizGestureControl.Gestures
tempGestureDictionary.Add("Start_Reset", StartOrReset);
gestureDictionary = tempGestureDictionary;
gesturesProgress = new int[gestureDictionary.Count];
foreach (int i in gesturesProgress)
flags = new int[gestureDictionary.Count + 1];
foreach (int i in flags)
{
gesturesProgress[i] = 0;
flags[i] = 0;
}
phiz = new PhIZ();
maxCapacity = 10;
headsStack = new Vector3D[maxCapacity];
int jointCount = Enum.GetNames(typeof(JointType)).Length;
headsStack = new CameraSpacePoint[maxCapacity];
}
#endregion
......@@ -88,63 +65,43 @@ namespace ExplorVizGestureControl.Gestures
public static void refreshJoints(CameraSpacePoint[] filteredJoints, Vector3D[] accumulatedJoint, HandState right, HandState left)
{
GestureDatabase.joints = filteredJoints;
GestureDatabase.joints[(int)JointType.ShoulderRight].X = (float)accumulatedJoint[(int)JointType.ShoulderRight].X;
GestureDatabase.joints[(int)JointType.ShoulderRight].Y = (float)accumulatedJoint[(int)JointType.ShoulderRight].Y;
GestureDatabase.joints[(int)JointType.ShoulderRight].Z = (float)accumulatedJoint[(int)JointType.ShoulderRight].Z;
int jtElbowLeft = (int)JointType.ElbowLeft;
int jtElbowRight = (int)JointType.ElbowRight;
int jtHandLeft = (int)JointType.HandLeft;
int jtHandRight = (int)JointType.HandRight;
int jtShoulderLeft = (int)JointType.ShoulderLeft;
int jtShoulderRight = (int)JointType.ShoulderRight;
int jtWristLeft = (int)JointType.WristLeft;
int jtWristRight = (int)JointType.WristRight;
int jtSpineMid = (int)JointType.SpineMid;
int jtSpineBase = (int)JointType.SpineBase;
int jtHead = (int)JointType.Head;
int jtHandRightTip = (int)JointType.HandTipRight;
int jtHandLeftTip = (int)JointType.HandTipLeft;
oldSpineBase = spineBase;
elbowLeft = new Vector3D(filteredJoints[jtElbowLeft].X, filteredJoints[jtElbowLeft].Y, filteredJoints[jtElbowLeft].Z);
elbowRight = new Vector3D(filteredJoints[jtElbowRight].X, filteredJoints[jtElbowRight].Y, filteredJoints[jtElbowRight].Z);
handLeft = new Vector3D(filteredJoints[jtHandLeft].X, filteredJoints[jtHandLeft].Y, filteredJoints[jtHandLeft].Z);
handRight = new Vector3D(filteredJoints[jtHandRight].X, filteredJoints[jtHandRight].Y, filteredJoints[jtHandRight].Z);
shoulderRight = new Vector3D(accumulatedJoint[jtShoulderRight].X, accumulatedJoint[jtShoulderRight].Y, accumulatedJoint[jtShoulderRight].Z);
shoulderLeft = new Vector3D(filteredJoints[jtShoulderLeft].X, filteredJoints[jtShoulderLeft].Y, filteredJoints[jtShoulderLeft].Z);
wristRight = new Vector3D(filteredJoints[jtWristRight].X, filteredJoints[jtWristRight].Y, filteredJoints[jtWristRight].Z);
wristLeft = new Vector3D(filteredJoints[jtWristLeft].X, filteredJoints[jtWristLeft].Y, filteredJoints[jtWristLeft].Z);
spineMid = new Vector3D(filteredJoints[jtSpineMid].X, filteredJoints[jtSpineMid].Y, filteredJoints[jtSpineMid].Z);
spineBase = new Vector3D(filteredJoints[jtSpineBase].X, filteredJoints[jtSpineBase].Y, filteredJoints[jtSpineBase].Z);
head = new Vector3D(filteredJoints[jtHead].X, filteredJoints[jtHead].Y, filteredJoints[jtHead].Z);
handRightTip = new Vector3D(filteredJoints[jtHandRightTip].X, filteredJoints[jtHandRightTip].Y, filteredJoints[jtHandRightTip].Z);
handLeftTip = new Vector3D(filteredJoints[jtHandLeftTip].X, filteredJoints[jtHandLeftTip].Y, filteredJoints[jtHandLeftTip].Z);
phiz.center = new Point(shoulderRight.X + 0.3, shoulderRight.Y);
int flagIndex = 0;
if (flags[flagIndex] == 0)
{
previousFrameJoints = filteredJoints;
}
else
{
previousFrameJoints = joints;
}
joints = filteredJoints;
joints[(int)JointType.ShoulderRight].X = (float)accumulatedJoint[(int)JointType.ShoulderRight].X;
joints[(int)JointType.ShoulderRight].Y = (float)accumulatedJoint[(int)JointType.ShoulderRight].Y;
joints[(int)JointType.ShoulderRight].Z = (float)accumulatedJoint[(int)JointType.ShoulderRight].Z;
phiz.center = new Point(joints[(int)JointType.ShoulderRight].X + 0.3, joints[(int)JointType.ShoulderRight].Y);
phiz.topRightCorner = new Point(phiz.center.X + 0.2, phiz.center.Y + 0.3);
phiz.botRightCorner = new Point(phiz.center.X + 0.2, phiz.center.Y - 0.1);
phiz.topLeftCorner = new Point(phiz.center.X - 0.2, phiz.center.Y + 0.3);
phiz.botLeftCorner = new Point(phiz.center.X - 0.2, phiz.center.Y - 0.1);
phiz.z = shoulderRight.Z;
phiz.z = joints[(int)JointType.ShoulderRight].Z;
handLeftState = left;
handRightState = right;
headsStack[newestOldHead] = head;
headsStack[newestOldHead] = joints[(int)JointType.Head];
newestOldHead = ++newestOldHead % maxCapacity;
// newestOldHead == 0 <=> one circle <=> full array
if (newestOldHead == 0) gesturesProgress[0] = 1;
if (newestOldHead == 0) flags[1] = 1;
}
#endregion
#region States
// Hand states
#region HandStates
public static bool RightHandClosed()
{
......@@ -162,42 +119,42 @@ namespace ExplorVizGestureControl.Gestures
public static Boolean RightElbowBelowRightHand()
{
return elbowRight.Y < handRight.Y;
return joints[(int)JointType.ElbowLeft].Y < joints[(int)JointType.HandRight].Y;
}
public static Boolean RightHandBelowRightShoulder()
{
return handRight.Y < shoulderRight.Y;
return joints[(int)JointType.HandRight].Y < joints[(int)JointType.ShoulderRight].Y;
}
public static Boolean RightHandMoreToTheRightThanRightElbow()
{
return handRight.X > elbowRight.X;
return joints[(int)JointType.HandRight].X < joints[(int)JointType.ElbowRight].X;
}
public static Boolean RightShoulderBelowRightElbow()
{
return elbowRight.Y > shoulderRight.Y;
return joints[(int)JointType.ElbowRight].Y < joints[(int)JointType.ShoulderRight].Y;
}
public static Boolean LeftElbowBelowLeftHand()
{
return elbowLeft.Y < handLeft.Y;
return joints[(int)JointType.ElbowLeft].Y < joints[(int)JointType.HandLeft].Y;
}
public static Boolean LeftHandBelowLeftShoulder()
{
return handLeft.Y < shoulderLeft.Y;
return joints[(int)JointType.HandLeft].Y < joints[(int)JointType.ShoulderLeft].Y;
}
public static Boolean LeftHandMoreToTheLeftThanLeftElbow()
{
return handLeft.X < elbowLeft.X;
return joints[(int)JointType.HandLeft].X < joints[(int)JointType.ElbowLeft].X;
}
public static Boolean LeftShoulderBelowLeftElbow()
{
return elbowLeft.Y > shoulderLeft.Y;
return joints[(int)JointType.ElbowLeft].Y < joints[(int)JointType.ShoulderLeft].Y;
}
public static Boolean BothHandsOpen()
......@@ -212,29 +169,37 @@ namespace ExplorVizGestureControl.Gestures
public static Boolean BothHandsParallel(double maxDistanceInX)
{
return (Math.Abs(handRight.X - handLeft.X) < maxDistanceInX &&
Math.Abs(Math.Abs(handRight.Y) - Math.Abs(handLeft.Y)) < 0.3 &&
Math.Abs(handRight.Z - handLeft.Z) < 0.3);
return (Math.Abs(joints[(int)JointType.HandRight].X - joints[(int)JointType.HandLeft].X) < maxDistanceInX &&
Math.Abs(Math.Abs(joints[(int)JointType.HandRight].Y) - Math.Abs(joints[(int)JointType.HandLeft].Y)) < 0.3 &&
Math.Abs(joints[(int)JointType.HandRight].Z - joints[(int)JointType.HandLeft].Z) < 0.3);
}
public static Boolean BothHandsAboveHead()
{
return handRight.Y >= head.Y && handLeft.Y >= head.Y;
return joints[(int)JointType.HandRight].Y >= joints[(int)JointType.Head].Y &&
joints[(int)JointType.HandLeft].Y >= joints[(int)JointType.Head].Y;
}
public static Boolean BothHandsAboveSpineBase()
{
return handLeft.Y > spineBase.Y && handRight.Y > spineBase.Y;
return joints[(int)JointType.HandLeft].Y > joints[(int)JointType.SpineBase].Y &&
joints[(int)JointType.HandRight].Y > joints[(int)JointType.SpineBase].Y;
}
public static Boolean HandsHaveAcceptableDistance(double offset = 0.0, char axis = 'x')
{
if (axis.Equals('x')) return Math.Abs(Math.Abs(handLeft.X) - Math.Abs(handRight.X)) <= Math.Abs(Math.Abs(shoulderLeft.X) - Math.Abs(shoulderRight.X)) + offset;
if (axis.Equals('x')) return Math.Abs(Math.Abs(joints[(int)JointType.HandLeft].X) -
Math.Abs(joints[(int)JointType.HandRight].X)) <=
Math.Abs(Math.Abs(joints[(int)JointType.ShoulderLeft].X) - Math.Abs(joints[(int)JointType.ShoulderRight].X)) + offset;
if (axis.Equals('y')) return Math.Abs(Math.Abs(handLeft.Y) - Math.Abs(handRight.Y)) <= Math.Abs(Math.Abs(shoulderLeft.Y) - Math.Abs(shoulderRight.Y)) + offset;
if (axis.Equals('y')) return Math.Abs(Math.Abs(joints[(int)JointType.HandLeft].Y) -
Math.Abs(joints[(int)JointType.HandRight].Y)) <=
Math.Abs(Math.Abs(joints[(int)JointType.ShoulderLeft].Y) - Math.Abs(joints[(int)JointType.ShoulderRight].Y)) + offset;
if (axis.Equals('z')) return Math.Abs(Math.Abs(handLeft.Z) - Math.Abs(handRight.Z)) <= Math.Abs(Math.Abs(shoulderLeft.Z) - Math.Abs(shoulderRight.Z)) + offset;
if (axis.Equals('z')) return Math.Abs(Math.Abs(joints[(int)JointType.HandLeft].Z) -
Math.Abs(joints[(int)JointType.HandRight].Z)) <=
Math.Abs(Math.Abs(joints[(int)JointType.ShoulderLeft].Z) - Math.Abs(joints[(int)JointType.ShoulderRight].Z)) + offset;
return false;
......@@ -258,15 +223,15 @@ namespace ExplorVizGestureControl.Gestures
{
Boolean[] confidenceArray = new Boolean[maxCapacity];
int gestureIndex = 0;
int flagIndex = 1;
if (gesturesProgress[gestureIndex] == 0) return false;
if (flags[flagIndex] == 0) return false;
int arrayLength = headsStack.Length;
for(int i = 0; i < arrayLength; i++) {
if (head.Y > headsStack[i].Y + 0.05) {
if (joints[(int)JointType.Head].Y > headsStack[i].Y + 0.05) {
confidenceArray[i] = true;
} else {
confidenceArray[i] = false;
......
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