Esempio n. 1
0
    public void DrawSymbol(MhGesture gesture)
    {
        foreach (Vector2 v in gesture.points)
        {
            Vector3 v3screen = new Vector3(v.x, v.y);
            Vector3 v3Word   = Camera.main.ScreenToWorldPoint(v3screen);

            Vector3 pos = new Vector3();

            pos.x = v3Word.x;
            pos.y = v3Word.y;
            MhGesturePoint newPoint = InstancePoint(pos);
            if (lastVector == Vector2.zero)
            {
                lastVector    = v;
                firstPoint    = newPoint;
                lastGestPoint = newPoint;
            }
            else
            {
                lastGestPoint.SetNextPoint(newPoint);
                lastVector    = v;
                lastGestPoint = newPoint;
            }
        }
    }
    public void SaveGesture()
    {
        MhGesture gesture = gestureInput.GetCurrentgesture();

        gesture.Name = inputField.text;
        MhGestureManager.AddGesture(gesture);
        MhGestureManager.SaveGestures();
        LoadSymbols();
    }
    public void RecalculatePointNumber()
    {
        string    symbolName = dropDownList.options[dropDownList.value].text;
        MhGesture gest       = MhGestureManager.FindGesture(symbolName);

        var interpolatedPointArray = MhPointPatternMath.GetInterpolatedPointArray(gest.points, (int)recalculatePointNumber.value, gest);

        gest.points = new List <Vector2>(interpolatedPointArray);
        ClearSymbol();
        DrawSymbol();
    }
    public void UpdatedSelectedSymbol()
    {
        string symbolName = dropDownList.options[dropDownList.value].text;

        MhGestureManager.RemoveGesture(symbolName);
        MhGesture gesture = gestureInput.GetCurrentgesture();

        gesture.Name = symbolName;
        MhGestureManager.AddGesture(gesture);
        MhGestureManager.SaveGestures();
    }
    public static bool RemoveGesture(string name)
    {
        MhGesture gestureToRemove = FindGesture(name);

        if (gestureToRemove != null)
        {
            preInterpolatedPoints.Remove(gestureToRemove);
            return(gestures.Remove(gestureToRemove));
        }
        Debug.LogWarning(string.Format("Removing gesture problem, gesture with name: {0} was not found {1}", name));
        return(false);
    }
Esempio n. 6
0
    /// <summary>
    /// Compares a points of a single gesture, to the points in a single saved gesture, and returns a accuracy probability.
    /// </summary>
    /// <param name="compareTo">Learned PointPattern from PointPatternSet to compare gesture points to.</param>
    /// <param name="points">Points of the current gesture being analyzed.</param>
    /// <returns>Returns the accuracy probability of the learned PointPattern to the current gesture.</returns>
    public MhPointPatternMatchResult GetPointPatternMatchResult(MhGesture compareTo, List <Vector2> points, int interpolationPoints)
    {
        // Ensure we have at least 2 points or recognition will fail as we are unable to interpolate between a single point.
        if (points.Count < 2 && interpolationPoints < 2)
        {
            throw new ArgumentOutOfRangeException("To few points or small inperpolation points");
        }

        // We'll use an array of doubles that matches the number of interpolation points to hold
        // the dot products of each angle comparison.
        var dotProducts = new List <float>(interpolationPoints + 1);

        // We'll need to interpolate the incoming points array and the points of the learned gesture.
        // We do this for each comparison so that we can change the precision at any time and not lose
        // or original learned gesture to multiple interpolations.
        var interpolatedCompareTo  = MhPointPatternMath.GetInterpolatedPointArray(compareTo.Points, interpolationPoints, compareTo);
        var interpolatedPointArray = MhPointPatternMath.GetInterpolatedPointArray(points, interpolationPoints, null);

        // Next we'll get an array of angles for each interpolated point in the learned and current gesture.
        // We'll get the same number of angles corresponding to the total number of interpolated points.
        var anglesCompareTo = MhPointPatternMath.GetPointArrayAngles(interpolatedCompareTo);
        var angles          = MhPointPatternMath.GetPointArrayAngles(interpolatedPointArray);

        // Now that we have angles for each gesture, we'll get the dot product of every angle equal to
        // the total number of interpolation points.
        for (var i = 0; i <= anglesCompareTo.Length - 1; i++)
        {
            dotProducts.Add(MhPointPatternMath.GetDotProduct(anglesCompareTo[i], angles[i]));
        }

        //
        // Convert average dot product to probability since we're using the deviation
        // of the average of the dot products of every interpolated point in a gesture.
        var probability = MhPointPatternMath.GetProbabilityFromDotProduct(dotProducts.Average());

        //first 5th percent and last 5th percent is more important
        for (var i = 0; i <= (anglesCompareTo.Length / 20.0) - 1; i++)
        {
            dotProducts.Add(MhPointPatternMath.GetDotProduct(anglesCompareTo[i], angles[i]));
        }
        //last 5th percent
        for (int i = (int)((anglesCompareTo.Length / 100.0) * 95.0 - 1); i <= anglesCompareTo.Length - 1; i++)
        {
            dotProducts.Add(MhPointPatternMath.GetDotProduct(anglesCompareTo[i], angles[i]));
        }

        var probabilityAlt = MhPointPatternMath.GetProbabilityFromDotProduct(dotProducts.Average());

        // Return PointPatternMatchResult object that holds the results of comparison.
        return(new MhPointPatternMatchResult(compareTo.Name, probability, 1));
    }
Esempio n. 7
0
    public MhGesture GetCurrentgesture()
    {
        MhGesture      gesture = new MhGesture();
        MhGesturePoint point   = firstPoint;

        while (point != null)
        {
            Vector3 currentScreenPos   = Camera.main.WorldToScreenPoint(point.transform.position);
            Vector2 currentScreenPos2D = new Vector2(currentScreenPos.x, currentScreenPos.y);
            point = point.after;
            gesture.Points.Add(currentScreenPos2D);
        }
        return(gesture);
    }
 public void DrawSymbol()
 {
     ClearSymbol();
     if (dropDownList.options.Count > 0)
     {
         string    symbolName = dropDownList.options[dropDownList.value].text;
         MhGesture gest       = MhGestureManager.FindGesture(symbolName);
         if (gest == null)
         {
             Debug.LogError("Symbol with name " + symbolName + " was not found");
             return;
         }
         gestureInput.DrawSymbol(gest);
     }
 }
Esempio n. 9
0
    public KeyValuePair <MhGesture, float> CompareGesture()
    {
        MhGesture currentGesture = GetCurrentgesture();
        var       results        = MhGestureManager.analyzer.GetPointPatternMatchResults(currentGesture.Points);
        var       results2       = MhGestureManager.analyzer.GetPointPatternMatchResultsAdv(currentGesture.Points);

        if (results.Length == 0)
        {
            return(new KeyValuePair <MhGesture, float>());
        }

        var topResult = results[0];

        Debug.Log("Best Match: " + topResult.Name + " , Probability: " + topResult.Probability);
        Debug.Log("Advance Comparison Best Match: " + results2[0].Name + " , Probability: " + results2[0].Probability);

        return(new KeyValuePair <MhGesture, float>(MhGestureManager.FindGesture(topResult.Name), (float)topResult.Probability));
    }
Esempio n. 10
0
    public static List <Vector2> GetInterpolatedPointArray(List <Vector2> points, int segments, MhGesture gesture)
    {
        //cached data
        if (gesture != null && MhGestureManager.preInterpolatedPoints.ContainsKey(gesture) && MhGestureManager.preInterpolatedPoints[gesture].ContainsKey(segments))
        {
            return(MhGestureManager.preInterpolatedPoints[gesture][segments]);
        }
        // Create an empty return collection to store interpolated points
        var interpolatedPoints = new List <Vector2>(segments);

        // Precalculate desired segment length and define helper variables
        var desiredSegmentLength = GetPointArrayLength(points) / segments;
        var currentSegmentLength = 0f; // Initialize to zero

        // Add first point in point pattern to return array and save it for use in the interpolation process
        var lastTestPoint = points[0]; // Initialize to first point in point pattern

        interpolatedPoints.Add(lastTestPoint);

        // Enumerate points starting with second point (if any)
        for (var currentIndex = 1; currentIndex < points.Count; currentIndex++)
        {
            // Store current index point in helper variable
            var currentPoint = points[currentIndex];

            // Calculate distance between last added point and current point in point pattern
            // and use calculated length to calculate test segment length for next point to add
            var incrementToCurrentlength = GetDistance(lastTestPoint, currentPoint);
            var testSegmentLength        = currentSegmentLength + incrementToCurrentlength;

            // Does the test segment length meet our desired length requirement
            if (testSegmentLength < desiredSegmentLength)
            {
                // Desired segment length has not been satisfied so we don't need to add an interpolated point
                // save this test point and move on to next test point
                currentSegmentLength = testSegmentLength;
                lastTestPoint        = currentPoint;
                continue;
            }

            // Test segment length has met or exceeded our desired segment length
            // so lets calculate how far we overshot our desired segment length and calculate
            // an interpolation position to use to derive our new interpolation point
            var interpolationPosition = (desiredSegmentLength - currentSegmentLength) * (1.0f / incrementToCurrentlength);

            // Use interpolation position to derive our new interpolation point
            var interpolatedPoint = GetInterpolatedPoint(lastTestPoint, currentPoint, interpolationPosition);
            interpolatedPoints.Add(interpolatedPoint);

            // Sometimes rounding errors cause us to attempt to add more points than the user has requested.
            // If we've reached our segment count limit, exit loop
            if (interpolatedPoints.Count == segments)
            {
                break;
            }

            // Store new interpolated point as last test point for use in next segment calculations
            // reset current segment length and jump back to the last index because we aren't done with original line segment
            lastTestPoint        = interpolatedPoint;
            currentSegmentLength = 0;
            currentIndex--;
        }

        //add to cache
        if (gesture != null)
        {
            if (MhGestureManager.preInterpolatedPoints.ContainsKey(gesture))
            {
                MhGestureManager.preInterpolatedPoints[gesture].Add(segments, interpolatedPoints);
            }
            else
            {
                Dictionary <int, List <Vector2> > newPointVector = new Dictionary <int, List <Vector2> >();
                newPointVector.Add(segments, interpolatedPoints);
                MhGestureManager.preInterpolatedPoints.Add(gesture, newPointVector);
            }
        }

        // Return interpolated point array
        return(interpolatedPoints);
    }
Esempio n. 11
0
    public override bool Equals(object obj)
    {
        MhGesture gesture = obj as MhGesture;

        return((gesture != null) && String.Equals(name, gesture.name));
    }
 public static void AddGesture(MhGesture gesture)
 {
     gestures.Add(gesture);
     analyzer.PointPatternSet.Add(gesture);
 }
Esempio n. 13
0
 /// <summary>
 /// Compares a points of a single gesture, to the points in a single saved gesture, and returns a accuracy probability.
 /// </summary>
 /// <param name="compareTo">Learned PointPattern from PointPatternSet to compare gesture points to.</param>
 /// <param name="points">Points of the current gesture being analyzed.</param>
 /// <returns>Returns the accuracy probability of the learned PointPattern to the current gesture.</returns>
 public MhPointPatternMatchResult GetPointPatternMatchResult(MhGesture compareTo, List <Vector2> points)
 {
     return(GetPointPatternMatchResult(compareTo, points, Precision));
 }