Exemplo n.º 1
0
        private static bool IsLine(SketchStroke stroke, int i1, int i2)
        {
            SketchPoint p1 = stroke.Points[i1];
            SketchPoint p2 = stroke.Points[i2];

            return(MathHelpers.EuclideanDistance(p1, p2) / SketchStrokeFeatureExtraction.PathLength(stroke, i1, i2) > LineValidationThreshold);
        }
Exemplo n.º 2
0
        public static SketchPoint Intersection(SketchStroke sketchStroke1, SketchStroke sketchStroke2)
        {
            double minDistance  = double.MaxValue;
            int    minDisIndex1 = 0;
            int    minDisIndex2 = 0;

            List <SketchPoint> points1 = sketchStroke1.Points;
            List <SketchPoint> points2 = sketchStroke2.Points;

            for (int i = 0; i < points1.Count; i++)
            {
                SketchPoint point1 = points1[i];

                for (int j = 0; j < points2.Count; j++)
                {
                    SketchPoint point2 = points2[j];

                    double distance = MathHelpers.EuclideanDistance(point1, point2);

                    if (distance < minDistance)
                    {
                        minDistance  = distance;
                        minDisIndex1 = i;
                        minDisIndex2 = j;
                    }
                }
            }

            if (minDistance > 30)
            {
                return(null);
            }

            return(new SketchPoint((points1[minDisIndex1].X + points2[minDisIndex2].X) / 2.0, (points1[minDisIndex1].Y + points2[minDisIndex2].Y) / 2.0));
        }
Exemplo n.º 3
0
        public static SketchStroke ResampleStroke(SketchStroke stroke, int n)
        {
            SketchStroke resampledStroke = new SketchStroke();

            List <SketchPoint> points    = new List <SketchPoint>();
            List <long>        timeStamp = new List <long>();

            // Copies the points and timeStamps list of the initial stroke
            foreach (SketchPoint p in stroke.Points)
            {
                points.Add(p);
            }
            foreach (long t in stroke.TimeStamp)
            {
                timeStamp.Add(t);
            }

            double totalLength = SketchStrokeFeatureExtraction.PathLength(stroke);
            double increment   = totalLength / (n - 1);
            double dist        = 0;

            resampledStroke.AppendPoint(points[0]);

            for (int i = 1; i < points.Count; i++)
            {
                SketchPoint cur = points[i];
                SketchPoint pre = points[i - 1];

                double d = MathHelpers.EuclideanDistance(cur, pre);

                if (dist + d >= increment)
                {
                    double qx = pre.X + ((increment - dist) / d) * (cur.X - pre.X);
                    double qy = pre.Y + ((increment - dist) / d) * (cur.Y - pre.Y);

                    SketchPoint q = new SketchPoint(qx, qy);

                    resampledStroke.AppendPoint(q);
                    points.Insert(i, q);

                    dist = 0.0;
                }
                else
                {
                    dist = dist + d;
                }
            }

            int oldTimeCount = timeStamp.Count;
            int newTimeCount = resampledStroke.Points.Count;

            for (int j = 0; j < newTimeCount; ++j)
            {
                int index = (int)((double)j / newTimeCount * oldTimeCount);
                resampledStroke.AppendTime(timeStamp[index]);
            }

            return(resampledStroke);
        }
Exemplo n.º 4
0
        public static double PathLength(SketchStroke stroke, int startIndex, int endIndex)
        {
            double length = 0;

            for (int i = startIndex; i <= endIndex - 1; i++)
            {
                length += MathHelpers.EuclideanDistance(stroke.Points[i], stroke.Points[i + 1]);
            }
            return(length);
        }
Exemplo n.º 5
0
        public static double TrioDistance(SketchStroke strokeA, SketchStroke strokeB)
        {
            SketchPoint startA = strokeA.StartPoint;
            SketchPoint startB = strokeB.StartPoint;
            SketchPoint midA   = strokeA.MidPoint;
            SketchPoint midB   = strokeB.MidPoint;
            SketchPoint endA   = strokeA.EndPoint;
            SketchPoint endB   = strokeB.EndPoint;

            return(MathHelpers.EuclideanDistance(startA, startB) +
                   MathHelpers.EuclideanDistance(midA, midB) +
                   MathHelpers.EuclideanDistance(endA, endB));
        }
Exemplo n.º 6
0
        private static void GetCornerIndices(SketchStroke resampledStroke, List <int> cornerIndices)
        {
            cornerIndices.Add(0);

            if (resampledStroke.Points.Count <= WindowRadius * 2)
            {
                return;
            }

            double[] straws = new double[resampledStroke.Points.Count];
            for (int i = WindowRadius; i < resampledStroke.Points.Count - WindowRadius; i++)
            {
                straws[i] = MathHelpers.EuclideanDistance(resampledStroke.Points[i - WindowRadius], resampledStroke.Points[i + WindowRadius]);
            }

            double[] strawsCopy = new double[resampledStroke.Points.Count - 2 * WindowRadius];
            for (int i = WindowRadius; i < resampledStroke.Points.Count - WindowRadius; i++)
            {
                strawsCopy[i - WindowRadius] = straws[i];
            }

            double strawLengthMedian    = MathHelpers.Median(strawsCopy);
            double strawLengthThreshold = strawLengthMedian * MedianStrawLengthMultipliedFactor;

            for (int i = WindowRadius; i < resampledStroke.Points.Count - WindowRadius; i++)
            {
                if (straws[i] < strawLengthThreshold)
                {
                    double localMin      = Double.MaxValue;
                    int    localMinIndex = i;

                    while (i < resampledStroke.Points.Count - WindowRadius && straws[i] < strawLengthThreshold)
                    {
                        if (straws[i] < localMin)
                        {
                            localMin      = straws[i];
                            localMinIndex = i;
                        }

                        i++;
                    }

                    cornerIndices.Add(localMinIndex);
                }
            }

            cornerIndices.Add(resampledStroke.Points.Count - 1);

            postProcessCorners(resampledStroke, cornerIndices, straws);
        }
Exemplo n.º 7
0
        public static string IntersectionRelationship(SketchStroke sketchStroke1, SketchStroke sketchStroke2)
        {
            SketchPoint closestPoint = Intersection(sketchStroke1, sketchStroke2);

            if (closestPoint == null)
            {
                return("none");
            }
            if (MathHelpers.EuclideanDistance(closestPoint, sketchStroke1.StartPoint) < 40)
            {
                return("touch head");
            }
            if (MathHelpers.EuclideanDistance(closestPoint, sketchStroke1.EndPoint) < 40)
            {
                return("touch tail");
            }
            return("cross");
        }
Exemplo n.º 8
0
        public static double Distance(List <SketchPoint> alphaPoints, List <SketchPoint> betaPoints)
        {
            double distances = 0.0;

            var pairs = new List <Tuple <SketchPoint, SketchPoint> >();

            double minDistance, weight, distance;
            int    index;

            SketchPoint minPoint = betaPoints[0];

            foreach (SketchPoint alphaPoint in alphaPoints)
            {
                minDistance = Double.MaxValue;

                // iterate through each beta point to find the min beta point to the alpha point
                index = 1;

                foreach (SketchPoint betaPoint in betaPoints)
                {
                    distance = MathHelpers.EuclideanDistance(alphaPoint, betaPoint);

                    // update the min distance and min point
                    if (minDistance > distance)
                    {
                        minDistance = distance;
                        minPoint    = betaPoint;
                    }
                }

                // update distance between alpha and beta point lists
                weight     = 1 - ((index - 1) / alphaPoints.Count);
                distances += minDistance * weight;

                // pair the alpha point to the min beta point and remove min beta point from list of beta points
                pairs.Add(new Tuple <SketchPoint, SketchPoint>(alphaPoint, minPoint));
                betaPoints.Remove(minPoint);
            }

            return(distances);
        }
Exemplo n.º 9
0
        public static double DirectedHausdorffDistance(List <SketchPoint> pointSetA, List <SketchPoint> pointSetB)
        {
            double maxDis = double.MinValue;

            foreach (SketchPoint pa in pointSetA)
            {
                double minDis = double.MaxValue;
                foreach (SketchPoint pb in pointSetB)
                {
                    double currentDistance = MathHelpers.EuclideanDistance(pa, pb);
                    if (currentDistance < minDis)
                    {
                        minDis = currentDistance;
                    }
                }

                if (minDis > maxDis)
                {
                    maxDis = minDis;
                }
            }

            return(maxDis);
        }
        public static List <SketchStroke> Resample(List <SketchStroke> strokes, int n)
        {
            double totalLength = 0;

            foreach (SketchStroke stroke in strokes)
            {
                totalLength += SketchStrokeFeatureExtraction.PathLength(stroke);
            }

            double I = totalLength / (n - 1);
            double D = 0.0;

            List <SketchStroke> newStrokes = new List <SketchStroke>();

            // Iterates through each stroke points in a list of strokes
            int pointCount = 0;

            foreach (SketchStroke stroke in strokes)
            {
                //Resamples point locations
                List <SketchPoint> points    = stroke.Points;
                List <SketchPoint> newPoints = new List <SketchPoint>();

                newPoints.Add(new SketchPoint(points[0].X, points[0].Y));
                ++pointCount;

                bool isDone = false;

                for (int i = 1; i < points.Count; ++i)
                {
                    double d = MathHelpers.EuclideanDistance(points[i - 1], points[i]);

                    if (D + d >= I)
                    {
                        double qx = points[i - 1].X + ((I - D) / d) * (points[i].X - points[i - 1].X);
                        double qy = points[i - 1].Y + ((I - D) / d) * (points[i].Y - points[i - 1].Y);

                        if (pointCount < n - 1)
                        {
                            newPoints.Add(new SketchPoint(qx, qy));
                            ++pointCount;

                            points.Insert(i, new SketchPoint(qx, qy));
                            D = 0.0;
                        }
                        else
                        {
                            isDone = true;
                        }
                    }
                    else
                    {
                        D += d;
                    }

                    if (isDone)
                    {
                        break;
                    }
                }

                D = 0.0;

                // Resamples time stamps
                List <long> timeStamp    = stroke.TimeStamp;
                List <long> newTimeStamp = new List <long>();

                int oldCount = timeStamp.Count;
                int newCount = newPoints.Count;

                for (int j = 0; j < newCount; ++j)
                {
                    int index = (int)((double)j / newCount * oldCount);
                    newTimeStamp.Add(timeStamp[index]);
                }

                SketchStroke newStroke = new SketchStroke(newPoints, newTimeStamp);

                newStrokes.Add(newStroke);
            }

            return(newStrokes);
        }