Skip to content
Snippets Groups Projects
Commit 098c7b87 authored by Alexis Iakovenko's avatar Alexis Iakovenko
Browse files

Add normalize function for temporary gesture

parent 74790ac7
No related branches found
No related tags found
No related merge requests found
...@@ -31,27 +31,20 @@ public class CompareGestureMean : MonoBehaviour ...@@ -31,27 +31,20 @@ public class CompareGestureMean : MonoBehaviour
TemporaryGesture meanGesture = new TemporaryGesture (); TemporaryGesture meanGesture = new TemporaryGesture ();
for (int i = 0; i < gestures [0].states.Count; i++) for (int i = 0; i < gestures [0].states.Count; i++)
{ {
float meanX = 0; Vector3 meanPosition = new Vector3(0, 0, 0);
float meanY = 0;
float meanZ = 0;
foreach (TemporaryGesture gesture in gestures) foreach (TemporaryGesture gesture in gestures)
{ meanPosition += gesture.states [i].position;
meanX += gesture.states [i].x; meanPosition /= gestures.Count;
meanY += gesture.states [i].y;
meanZ += gesture.states [i].z;
}
meanX /= gestures.Count;
meanY /= gestures.Count;
meanZ /= gestures.Count;
TemporaryGestureState meanState = new TemporaryGestureState (); TemporaryGestureState meanState = new TemporaryGestureState ();
meanState.timestamp = gestures [0].states [i].timestamp; meanState.timestamp = gestures [0].states [i].timestamp;
meanState.x = meanX; meanState.position = meanPosition;
meanState.y = meanY;
meanState.z = meanZ;
meanGesture.states.Add (meanState); meanGesture.states.Add (meanState);
} }
meanGesture.Normalize ();
return meanGesture; return meanGesture;
} }
...@@ -61,11 +54,8 @@ public class CompareGestureMean : MonoBehaviour ...@@ -61,11 +54,8 @@ public class CompareGestureMean : MonoBehaviour
for (int i = 0; i < gesture.states.Count; i++) for (int i = 0; i < gesture.states.Count; i++)
{ {
float differenceX = Mathf.Abs(gesture.states[i].x - meanGesture.states[i].x); Vector3 difference = gesture.states [i].position - meanGesture.states [i].position;
float differenceY = Mathf.Abs(gesture.states[i].y - meanGesture.states[i].y); score -= Mathf.Abs(difference.x) + Mathf.Abs(difference.y) + Mathf.Abs(difference.z);
float differenceZ = Mathf.Abs(gesture.states[i].z - meanGesture.states[i].z);
score -= differenceX + differenceY + differenceZ;
} }
Debug.Log ("Comparing gesture " + gesture.ToString() + " with mean gesture. Score: " + score); Debug.Log ("Comparing gesture " + gesture.ToString() + " with mean gesture. Score: " + score);
......
...@@ -18,6 +18,56 @@ public class TemporaryGesture ...@@ -18,6 +18,56 @@ public class TemporaryGesture
{ {
this.name = name; this.name = name;
Load (); Load ();
//VerifyNormalization ();
Normalize ();
//VerifyNormalization ();
}
public void Normalize ()
{
//set 0 as origin/centroid
//find mean position
Vector3 meanPosition = new Vector3(0, 0, 0);
foreach (TemporaryGestureState state in states)
meanPosition += state.position;
meanPosition /= states.Count;
//subtract mean position to all positions
foreach (TemporaryGestureState state in states)
state.position -= meanPosition;
//have furthest point have distance 1
//find furthest point length
float furthestDistance = 0;
foreach (TemporaryGestureState state in states)
{
float length = state.position.magnitude;
if (length > furthestDistance)
furthestDistance = length;
}
//divide length to all points
foreach (TemporaryGestureState state in states)
state.position /= furthestDistance;
}
public void VerifyNormalization ()
{
Vector3 meanPosition = new Vector3(0, 0, 0);
foreach (TemporaryGestureState state in states)
meanPosition += state.position;
meanPosition /= states.Count;
Debug.Log ("[VerifyNormalization] mean position: " + meanPosition);
float furthestDistance = -1;
foreach (TemporaryGestureState state in states)
{
float length = state.position.sqrMagnitude;
if (length > furthestDistance)
furthestDistance = length;
}
Debug.Log ("[VerifyNormalization] furthestDistance: " + furthestDistance);
} }
public void Load () public void Load ()
...@@ -40,9 +90,9 @@ public class TemporaryGesture ...@@ -40,9 +90,9 @@ public class TemporaryGesture
TemporaryGestureState state = new TemporaryGestureState (); TemporaryGestureState state = new TemporaryGestureState ();
state.timestamp = int.Parse(line [2]); state.timestamp = int.Parse(line [2]);
state.x = float.Parse(line [13]); state.position.x = float.Parse(line [13]);
state.y = float.Parse(line [14]); state.position.y = float.Parse(line [14]);
state.z = float.Parse(line [15]); state.position.z = float.Parse(line [15]);
states.Add (state); states.Add (state);
//Debug.Log(state); //Debug.Log(state);
......
...@@ -5,12 +5,10 @@ using UnityEngine; ...@@ -5,12 +5,10 @@ using UnityEngine;
public class TemporaryGestureState { public class TemporaryGestureState {
public int timestamp = 0; public int timestamp = 0;
public float x = 0; public Vector3 position = new Vector3(0, 0, 0);
public float y = 0;
public float z = 0;
public override string ToString() public override string ToString()
{ {
return "State timestamp " + timestamp + ", x: " + x + ", y: " + y + ", z: " + z; return "State timestamp " + timestamp + ", x: " + position.x + ", y: " + position.y + ", z: " + position.z;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment