Example #1
0
        private void PreparePolygons(IEnumerable <Poly2Tri.DelaunayTriangle> triangles)
        {
            this.PolygonEdges.Clear();
            this.Polygons.Clear();

            // For building edges we need a mapping of the original triangles to polygons
            Dictionary <Poly2Tri.DelaunayTriangle, Poly2Tri.Point2DList> triangleToPolygon = new Dictionary <Poly2Tri.DelaunayTriangle, Poly2Tri.Point2DList>();
            Dictionary <Poly2Tri.Point2DList, Poly2Tri.DelaunayTriangle> polygonToTriangle = new Dictionary <Poly2Tri.Point2DList, Poly2Tri.DelaunayTriangle>();

            // Initially, our polygons are simply the triangles in polygon form
            foreach (var triangle in triangles)
            {
                var polygonPoints            = triangle.Points.Select(p => new Poly2Tri.Point2D(p.X, p.Y)).ToList();
                Poly2Tri.Point2DList polygon = new Poly2Tri.Point2DList(polygonPoints);

                triangleToPolygon[triangle] = polygon;
                polygonToTriangle[polygon]  = triangle;

                this.Polygons.Add(polygon);
            }

            // Build up the edge list
            foreach (var polygon in this.Polygons)
            {
                // Does this polygon have any neighbors? If so, that's how we make our edges.
                var triangle = polygonToTriangle[polygon];

                // We can have up to 3 neighbors on each triangle
                for (int n = 0; n < 3; ++n)
                {
                    var neighborTriangle = triangle.Neighbors[n];
                    if (neighborTriangle == null)
                    {
                        continue;
                    }

                    if (!triangleToPolygon.ContainsKey(neighborTriangle))
                    {
                        continue;
                    }

                    var neighborPolygon = triangleToPolygon[neighborTriangle];

                    // If the neighbor polygon still has a triangle associated with it then we haven't added its edges yet
                    // Otherwise, we have added her edges and we don't want to do so again
                    if (!polygonToTriangle.ContainsKey(neighborPolygon))
                    {
                        continue;
                    }

                    // Gather the points needed for the edge
                    Poly2Tri.TriangulationPoint triPoint1 = triangle.Points[(n + 1) % 3];
                    Poly2Tri.TriangulationPoint triPoint2 = triangle.Points[(n + 2) % 3];

                    // Create an polygon edge
                    SharedPolygonEdge edge = new SharedPolygonEdge();
                    edge.PolygonA   = polygon;
                    edge.PolygonB   = neighborPolygon;
                    edge.EdgePoint1 = new Poly2Tri.PolygonPoint(triPoint1.X, triPoint1.Y);
                    edge.EdgePoint2 = new Poly2Tri.PolygonPoint(triPoint2.X, triPoint2.Y);
                    this.PolygonEdges.Add(edge);
                }

                // Remove the polygon from the triangle mapping. We are done with it.
                polygonToTriangle.Remove(polygon);
            }
        }
        private void PreparePolygons(IEnumerable<Poly2Tri.DelaunayTriangle> triangles)
        {
            this.PolygonEdges.Clear();
            this.Polygons.Clear();

            // For building edges we need a mapping of the original triangles to polygons
            Dictionary<Poly2Tri.DelaunayTriangle, Poly2Tri.Point2DList> triangleToPolygon = new Dictionary<Poly2Tri.DelaunayTriangle, Poly2Tri.Point2DList>();
            Dictionary<Poly2Tri.Point2DList, Poly2Tri.DelaunayTriangle> polygonToTriangle = new Dictionary<Poly2Tri.Point2DList, Poly2Tri.DelaunayTriangle>();

            // Initially, our polygons are simply the triangles in polygon form
            foreach (var triangle in triangles)
            {
                var polygonPoints = triangle.Points.Select(p => new Poly2Tri.Point2D(p.X, p.Y)).ToList();
                Poly2Tri.Point2DList polygon = new Poly2Tri.Point2DList(polygonPoints);

                triangleToPolygon[triangle] = polygon;
                polygonToTriangle[polygon] = triangle;

                this.Polygons.Add(polygon);
            }

            // Build up the edge list
            foreach (var polygon in this.Polygons)
            {
                // Does this polygon have any neighbors? If so, that's how we make our edges.
                var triangle = polygonToTriangle[polygon];

                // We can have up to 3 neighbors on each triangle
                for (int n = 0; n < 3; ++n)
                {
                    var neighborTriangle = triangle.Neighbors[n];
                    if (neighborTriangle == null)
                        continue;

                    if (!triangleToPolygon.ContainsKey(neighborTriangle))
                        continue;

                    var neighborPolygon = triangleToPolygon[neighborTriangle];

                    // If the neighbor polygon still has a triangle associated with it then we haven't added its edges yet
                    // Otherwise, we have added her edges and we don't want to do so again
                    if (!polygonToTriangle.ContainsKey(neighborPolygon))
                        continue;

                    // Gather the points needed for the edge
                    Poly2Tri.TriangulationPoint triPoint1 = triangle.Points[(n + 1) % 3];
                    Poly2Tri.TriangulationPoint triPoint2 = triangle.Points[(n + 2) % 3];

                    // Create an polygon edge
                    SharedPolygonEdge edge = new SharedPolygonEdge();
                    edge.PolygonA = polygon;
                    edge.PolygonB = neighborPolygon;
                    edge.EdgePoint1 = new Poly2Tri.PolygonPoint(triPoint1.X, triPoint1.Y);
                    edge.EdgePoint2 = new Poly2Tri.PolygonPoint(triPoint2.X, triPoint2.Y);
                    this.PolygonEdges.Add(edge);
                }

                // Remove the polygon from the triangle mapping. We are done with it.
                polygonToTriangle.Remove(polygon);
            }
        }
Example #3
0
        private void CombinePolygonsAlongEdge(int index)
        {
            // We are going to combine all the points from polygon B into polygon A
            var edge = this.PolygonEdges[index];

            // Gather a list of all the points from polygon B to be added
            // This is all the points from polygon B except for the shared edge
            List <Poly2Tri.Point2D> pointsToAdd = new List <Poly2Tri.Point2D>();
            {
                int edgeStartOnBIndex = edge.PolygonB.IndexOf(edge.EdgePoint1);
                int edgeStopOnBIndex  = edge.PolygonB.IndexOf(edge.EdgePoint2);

                int indexToAdd = edge.PolygonB.NextIndex(edgeStartOnBIndex);
                while (indexToAdd != edgeStopOnBIndex)
                {
                    Poly2Tri.Point2D point = edge.PolygonB[indexToAdd];
                    pointsToAdd.Add(point);

                    indexToAdd = edge.PolygonB.NextIndex(indexToAdd);
                }
            }

            // Insert the points to add between the edge points on polygon A
            List <Poly2Tri.Point2D> newPolygonAPoints = edge.PolygonA.ToList();

            {
                int edgeStartOnAIndex = edge.PolygonA.IndexOf(edge.EdgePoint1);
                int nextIndexA        = edge.PolygonA.NextIndex(edgeStartOnAIndex);
                newPolygonAPoints.InsertRange(nextIndexA, pointsToAdd);
            }

            // Create a new polygon A
            Poly2Tri.Point2DList newPolygonA = new Poly2Tri.Point2DList(newPolygonAPoints);
            this.Polygons.Add(newPolygonA);

            // Any furter edges that had PolygonA or PolygonB in it must be updated
            for (int i = index + 1; i < this.PolygonEdges.Count; ++i)
            {
                var furtherEdge = this.PolygonEdges[i];

                if (furtherEdge.PolygonA == edge.PolygonA)
                {
                    furtherEdge.PolygonA = newPolygonA;
                }
                else if (furtherEdge.PolygonA == edge.PolygonB)
                {
                    furtherEdge.PolygonA = newPolygonA;
                }

                if (furtherEdge.PolygonB == edge.PolygonA)
                {
                    furtherEdge.PolygonB = newPolygonA;
                }
                else if (furtherEdge.PolygonB == edge.PolygonB)
                {
                    furtherEdge.PolygonB = newPolygonA;
                }
            }

            // Old PolygonA and PolygonB are removed from the list of polygons
            this.Polygons.Remove(edge.PolygonA);
            this.Polygons.Remove(edge.PolygonB);
        }
        private void CombinePolygonsAlongEdge(int index)
        {
            // We are going to combine all the points from polygon B into polygon A
            var edge = this.PolygonEdges[index];

            // Gather a list of all the points from polygon B to be added
            // This is all the points from polygon B except for the shared edge
            List<Poly2Tri.Point2D> pointsToAdd = new List<Poly2Tri.Point2D>();
            {
                int edgeStartOnBIndex = edge.PolygonB.IndexOf(edge.EdgePoint1);
                int edgeStopOnBIndex = edge.PolygonB.IndexOf(edge.EdgePoint2);

                int indexToAdd = edge.PolygonB.NextIndex(edgeStartOnBIndex);
                while (indexToAdd != edgeStopOnBIndex)
                {
                    Poly2Tri.Point2D point = edge.PolygonB[indexToAdd];
                    pointsToAdd.Add(point);

                    indexToAdd = edge.PolygonB.NextIndex(indexToAdd);
                }
            }

            // Insert the points to add between the edge points on polygon A
            List<Poly2Tri.Point2D> newPolygonAPoints = edge.PolygonA.ToList();
            {
                int edgeStartOnAIndex = edge.PolygonA.IndexOf(edge.EdgePoint1);
                int nextIndexA = edge.PolygonA.NextIndex(edgeStartOnAIndex);
                newPolygonAPoints.InsertRange(nextIndexA, pointsToAdd);
            }

            // Create a new polygon A
            Poly2Tri.Point2DList newPolygonA = new Poly2Tri.Point2DList(newPolygonAPoints);
            this.Polygons.Add(newPolygonA);

            // Any furter edges that had PolygonA or PolygonB in it must be updated
            for (int i = index + 1; i < this.PolygonEdges.Count; ++i)
            {
                var furtherEdge = this.PolygonEdges[i];

                if (furtherEdge.PolygonA == edge.PolygonA)
                {
                    furtherEdge.PolygonA = newPolygonA;
                }
                else if (furtherEdge.PolygonA == edge.PolygonB)
                {
                    furtherEdge.PolygonA = newPolygonA;
                }

                if (furtherEdge.PolygonB == edge.PolygonA)
                {
                    furtherEdge.PolygonB = newPolygonA;
                }
                else if (furtherEdge.PolygonB == edge.PolygonB)
                {
                    furtherEdge.PolygonB = newPolygonA;
                }
            }

            // Old PolygonA and PolygonB are removed from the list of polygons
            this.Polygons.Remove(edge.PolygonA);
            this.Polygons.Remove(edge.PolygonB);
        }