Esempio n. 1
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 PointPatternMatchResult GetPointPatternMatchResult(PointPattern compareTo, Point[] points)
        {
            //compare points



            // Ensure we have at least 2 points or recognition will fail as we are unable to interpolate between a single point.
            if (points.Length < 2)
            {
                throw new ArgumentOutOfRangeException("hello" /*nameof(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 double[Precision];
            var myDistance  = new double[Precision];

            // 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  = PointPatternMath.GetInterpolatedPointArray(compareTo.Points, Precision);
            var interpolatedPointArray = PointPatternMath.GetInterpolatedPointArray(points, Precision);

            int   ij            = 0;
            float totalDistance = 0;

            foreach (Point p in interpolatedCompareTo)
            {
                Vector2 v1 = new Vector2((float)p.X, (float)p.Y);
                Vector2 v2 = new Vector2((float)interpolatedPointArray[ij].X, (float)interpolatedPointArray[ij].Y);
                float   d  = Vector2.Distance(v1, v2);
                myDistance[ij] = Vector2.Distance(v1, v2);
                totalDistance  = totalDistance + d;
                ij++;
            }

            // Debug.Log("distance is " + totalDistance);

            // 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 = PointPatternMath.GetPointArrayAngles(interpolatedCompareTo);
            var    angles          = PointPatternMath.GetPointArrayAngles(interpolatedPointArray);
            double averageDistance = myDistance.Average();

            // 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[i] = PointPatternMath.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 = PointPatternMath.GetProbabilityFromDotProduct(dotProducts.Average());

            // Return PointPatternMatchResult object that holds the results of comparison.
            return(new PointPatternMatchResult(compareTo.Name, probability, 1, totalDistance, averageDistance));
        }