Esempio n. 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);
            }
        }
Esempio n. 2
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);
            }
        }