/*Douglas-Peucker line simplification algorithm
         * INPUT: A set of points P in sequence and a tolerance value
         * OUTPUT: A simplified chain after removing some of the points from P
         */
        public static void PolygonalChainSimplification(Microsoft.Msagl.Core.Geometry.Point[] PointList, int start, int end, double epsilon)
        {
            // Find the point with the maximum distance
            double dmax = 0, d = 0;
            int    index = 0;

            for (int i = start + 1; i <= end - 1; i++)
            {
                d = PointToSegmentDistance.GetDistance(PointList[start], PointList[end], PointList[i]);
                if (d > dmax)
                {
                    index = i;
                    dmax  = d;
                }
            }


            // If max distance is greater than epsilon, recursively simplify
            if (dmax > epsilon)
            {
                // Recursive call
                PolygonalChainSimplification(PointList, start, index, epsilon);
                PolygonalChainSimplification(PointList, index, end, epsilon);
            }
            else
            {
                for (int i = start + 1; i <= end - 1; i++)
                {
                    PointList[i] = new Core.Geometry.Point(-1, -1);
                }
            }
        }
        private static double GetFacewidth(List <Vertex> face)
        {
            int index = 0;

            LineSegment[] segments = new LineSegment[face.Count];
            Vertex[,] segmentVertices = new Vertex[face.Count, 2];
            Vertex initialVertex = null, oldVertex = null;

            Core.Geometry.Point u1, u2;
            foreach (var boundaryVertex in face)
            {
                if (oldVertex == null)
                {
                    initialVertex = oldVertex = boundaryVertex;
                    continue;
                }
                u1 = new Core.Geometry.Point(boundaryVertex.XLoc, boundaryVertex.YLoc);
                u2 = new Core.Geometry.Point(oldVertex.XLoc, oldVertex.YLoc);
                segmentVertices[index, 0] = boundaryVertex;
                segmentVertices[index, 1] = oldVertex;
                segments[index++]         = new LineSegment(u1, u2);
                oldVertex = boundaryVertex;
            }
            u1 = new Core.Geometry.Point(oldVertex.XLoc, oldVertex.YLoc);
            u2 = new Core.Geometry.Point(initialVertex.XLoc, initialVertex.YLoc);
            segmentVertices[index, 0] = initialVertex;
            segmentVertices[index, 1] = oldVertex;
            segments[index]           = new LineSegment(u1, u2);

            double facewidth = Double.MaxValue;

            for (int i = 0; i < face.Count; i++)
            {
                for (int j = 0; j < face.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (segments[i].Start == segments[j].Start || segments[i].End == segments[j].End ||
                        segments[i].End == segments[j].Start || segments[i].Start == segments[j].End)
                    {
                        continue;
                    }

                    double width;
                    width = PointToSegmentDistance.GetDistance(segmentVertices[i, 0], segmentVertices[i, 1],
                                                               segmentVertices[j, 0]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[i, 0], segmentVertices[i, 1],
                                                               segmentVertices[j, 1]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[j, 0], segmentVertices[j, 1],
                                                               segmentVertices[i, 0]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[j, 0], segmentVertices[j, 1],
                                                               segmentVertices[i, 1]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                }
            }
            return(facewidth);
        }
Example #3
0
        private static double GetFacewidth(List <Vertex> face)
        {
            int index = 0;

            LineSegment[] segments = new LineSegment[face.Count];
#if SHARPKIT //https://code.google.com/p/sharpkit/issues/detail?id=340
            throw new InvalidOperationException();
#else
            Vertex[,] segmentVertices = new Vertex[face.Count, 2];
            Vertex initialVertex = null, oldVertex = null;
            Core.Geometry.Point u1, u2;
            foreach (var boundaryVertex in face)
            {
                if (oldVertex == null)
                {
                    initialVertex = oldVertex = boundaryVertex;
                    continue;
                }
                u1 = new Core.Geometry.Point(boundaryVertex.XLoc, boundaryVertex.YLoc);
                u2 = new Core.Geometry.Point(oldVertex.XLoc, oldVertex.YLoc);
                segmentVertices[index, 0] = boundaryVertex;
                segmentVertices[index, 1] = oldVertex;
                segments[index++]         = new LineSegment(u1, u2);
                oldVertex = boundaryVertex;
            }
            u1 = new Core.Geometry.Point(oldVertex.XLoc, oldVertex.YLoc);
            u2 = new Core.Geometry.Point(initialVertex.XLoc, initialVertex.YLoc);
            segmentVertices[index, 0] = initialVertex;
            segmentVertices[index, 1] = oldVertex;
            segments[index]           = new LineSegment(u1, u2);

            double facewidth = Double.MaxValue;
            for (int i = 0; i < face.Count; i++)
            {
                for (int j = 0; j < face.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (segments[i].Start == segments[j].Start || segments[i].End == segments[j].End ||
                        segments[i].End == segments[j].Start || segments[i].Start == segments[j].End)
                    {
                        continue;
                    }

                    double width;
                    width = PointToSegmentDistance.GetDistance(segmentVertices[i, 0], segmentVertices[i, 1],
                                                               segmentVertices[j, 0]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[i, 0], segmentVertices[i, 1],
                                                               segmentVertices[j, 1]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[j, 0], segmentVertices[j, 1],
                                                               segmentVertices[i, 0]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                    width = PointToSegmentDistance.GetDistance(segmentVertices[j, 0], segmentVertices[j, 1],
                                                               segmentVertices[i, 1]);
                    if (facewidth > width)
                    {
                        facewidth = width;
                    }
                }
            }
            return(facewidth);
#endif
        }