From b457a8a56bcc4036bd96cf8516d306d02cb80744 Mon Sep 17 00:00:00 2001
From: Alexis Iakovenko <alexis.iakovenko@gmail.com>
Date: Sun, 14 Jan 2018 17:48:58 +0100
Subject: [PATCH] Use variance for algorithm

---
 Assets/Replay/LiveComparer.cs | 59 ++++++++++++++++++++++++++++-------
 Assets/Scenes/Replay.unity    | 18 +++++------
 2 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/Assets/Replay/LiveComparer.cs b/Assets/Replay/LiveComparer.cs
index b962631..4529398 100644
--- a/Assets/Replay/LiveComparer.cs
+++ b/Assets/Replay/LiveComparer.cs
@@ -11,8 +11,8 @@ public class LiveComparer : MonoBehaviour
 	public GameObject totalScoreLabel = null;
 	public Replay replay = null;
 	public LeapServiceProvider LeapService = null;
-	public float acceptableScore = 99.0f;
 
+	float acceptableScore = 150.0f;
 	float totalScore = 0.0f;
 	float frameScore = 0.0f;
 	float sumScore = 0.0f;
@@ -22,12 +22,25 @@ public class LiveComparer : MonoBehaviour
 	Vector3 rightPalmPosition = Vector3.zero;
 	Vector4 rightPalmRotation = Vector3.zero;
 
+	Vector3 offsetPosition = Vector3.zero;
+
 	public void ResetScore()
 	{
-		acceptableScore = frameScore - 1.0f;
+		//acceptableScore = frameScore - 1.0f;
 		totalScore = 0.0f;
 		frameScore = 0.0f;
 		sumScore = 0.0f;
+
+		UpdateOffsetPosition ();
+	}
+
+	void UpdateOffsetPosition ()
+	{
+		if (replay.currentFrame >= 0 && replay.currentFrame < 100)
+		{
+			FetchPalmsCoordinates ();
+			offsetPosition = leftPalmPosition - replay.gestureToReplay.states [replay.currentFrame].position;
+		}
 	}
 
 	void Update ()
@@ -51,24 +64,45 @@ public class LiveComparer : MonoBehaviour
 	{
 		//for now we only consider the left hand
 		frameScore = 100.0f;
+		acceptableScore = 100.0f;
+
+		float generalFactor = 1;
 
 		float positionFactor = 15.0f;
 		float rotationFactor = 15.0f;
 
-		Vector3 differencePosition = leftPalmPosition - 
+		Vector3 differencePosition = offsetPosition + leftPalmPosition - 
 			replay.gestureToReplay.states [replay.currentFrame].position;
 		Vector4 differenceRotation = leftPalmRotation - 
 			replay.gestureToReplay.states [replay.currentFrame].rotation;
 
-		float absoluteDifferencePosition = Mathf.Abs(differencePosition.x) + 
-			Mathf.Abs(differencePosition.y) + 
-			Mathf.Abs(differencePosition.z);
-		float absoluteDifferenceRotation = Mathf.Abs(differenceRotation.x) + 
-			Mathf.Abs(differenceRotation.y) + 
-			Mathf.Abs(differenceRotation.z) + 
-			Mathf.Abs(differenceRotation.w);
-
-		frameScore -= absoluteDifferencePosition * positionFactor + absoluteDifferenceRotation * rotationFactor;
+		float absoluteDifferencePosition = Mathf.Abs(generalFactor * differencePosition.x) + 
+			Mathf.Abs(generalFactor * differencePosition.y) + 
+			Mathf.Abs(generalFactor * differencePosition.z) * positionFactor;
+		float absoluteDifferenceRotation = Mathf.Abs(generalFactor * differenceRotation.x) + 
+			Mathf.Abs(generalFactor * differenceRotation.y) + 
+			Mathf.Abs(generalFactor * differenceRotation.z) + 
+			Mathf.Abs(generalFactor * differenceRotation.w) * rotationFactor;
+
+		//use variance
+		Vector3 variancePosition = leftPalmPosition - 
+			replay.gestureVariance.states [replay.currentFrame].position;
+		Vector4 varianceRotation = leftPalmRotation - 
+			replay.gestureVariance.states [replay.currentFrame].rotation;
+		float absoluteDifferencePositionVariance = Mathf.Abs(generalFactor * variancePosition.x) + 
+			Mathf.Abs(generalFactor * variancePosition.y) + 
+			Mathf.Abs(generalFactor * variancePosition.z);
+		float absoluteDifferenceRotationVariance = Mathf.Abs(generalFactor * varianceRotation.x) + 
+			Mathf.Abs(generalFactor * varianceRotation.y) + 
+			Mathf.Abs(generalFactor * varianceRotation.z) + 
+			Mathf.Abs(generalFactor * varianceRotation.w);
+
+		//compute score based on difference with variance
+		float scorePosition = absoluteDifferencePositionVariance - absoluteDifferencePosition;
+		float scoreRotation = absoluteDifferenceRotation - absoluteDifferenceRotationVariance;
+
+
+		frameScore -= scorePosition + scoreRotation;
 	}
 
 	void ComputeTotalScore()
@@ -110,6 +144,7 @@ public class LiveComparer : MonoBehaviour
 		}
 		else
 		{
+			//Debug.Log ("[UpdateScoreLabels] acc: " + acceptableScore);
 			if (frameScore > acceptableScore)
 				frameScoreLabel.GetComponent<Text> ().color = new Color (0.0f, 0.7f, 0.0f);
 			else
diff --git a/Assets/Scenes/Replay.unity b/Assets/Scenes/Replay.unity
index f23f183..dced61e 100644
--- a/Assets/Scenes/Replay.unity
+++ b/Assets/Scenes/Replay.unity
@@ -211,7 +211,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 425149262}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -419.99997, y: 219, z: 0}
+  m_LocalPosition: {x: -414.99997, y: 219, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1487093064}
@@ -504,7 +504,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 512389760}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -542, y: 219, z: 0}
+  m_LocalPosition: {x: -537, y: 219, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children:
   - {fileID: 886768371}
@@ -792,7 +792,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 652086751}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -542, y: 166, z: 0}
+  m_LocalPosition: {x: -537, y: 166, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1487093064}
@@ -1116,7 +1116,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 799589078}
   m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
-  m_LocalPosition: {x: -542, y: 136, z: 0}
+  m_LocalPosition: {x: -537, y: 136, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1487093064}
@@ -1529,7 +1529,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 1475768281}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 535, y: 263.5, z: 0}
+  m_LocalPosition: {x: 530, y: 263.5, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children:
   - {fileID: 409326028}
@@ -1719,7 +1719,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 1487093060}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 646, y: 299, z: 0}
+  m_LocalPosition: {x: 641, y: 299, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children:
   - {fileID: 1882596558}
@@ -2162,7 +2162,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 1882596557}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -542, y: 263.5, z: 0}
+  m_LocalPosition: {x: -537, y: 263.5, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children:
   - {fileID: 1741226554}
@@ -2306,7 +2306,7 @@ MonoBehaviour:
   totalScoreLabel: {fileID: 799589078}
   replay: {fileID: 2026651519}
   LeapService: {fileID: 487240168}
-  acceptableScore: 96
+  acceptableScore: 100
 --- !u!4 &1896376828
 Transform:
   m_ObjectHideFlags: 0
@@ -2578,7 +2578,7 @@ RectTransform:
   m_PrefabInternal: {fileID: 0}
   m_GameObject: {fileID: 2041325544}
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: -355, y: 263.5, z: 0}
+  m_LocalPosition: {x: -350, y: 263.5, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_Children: []
   m_Father: {fileID: 1487093064}
-- 
GitLab