private static PolygonIsland[] GetIslands(Point[][] polygons, EdgeIntersection[] intersections) { // Figure out which polygons are in which islands var islands = GetIslandsSprtIndices(polygons, intersections); if (islands.Length == 1) { return new PolygonIsland[] { new PolygonIsland(polygons, intersections) }; } PolygonIsland[] retVal = new PolygonIsland[islands.Length]; // Make new intersections that only know about the polygons in their island for (int cntr = 0; cntr < retVal.Length; cntr++) { List<Point[]> localPolys = new List<Point[]>(); SortedList<int, int> mapping = new SortedList<int, int>(); foreach (int index in islands[cntr].Item1) { mapping.Add(index, localPolys.Count); localPolys.Add(polygons[index]); } EdgeIntersection[] localIntersections = islands[cntr].Item2.Select(o => new EdgeIntersection(mapping[o.Poly1Index], o.Poly1Edge, mapping[o.Poly2Index], o.Poly2Edge, o.IntersectionPoint)).ToArray(); retVal[cntr] = new PolygonIsland(localPolys.ToArray(), localIntersections); } // Exit Function return retVal; }
private static Polygon2D[] MergeIsland(PolygonIsland island) { // Convert into line segments Tuple<Point, Point>[] allSegments = MergeIslandSprtSegments(island); List<Tuple<Point, bool>> mids; // Remove segments that are inside a polygon Tuple<Point, Point>[] segments = MergeIslandSprtRemoveInternal(out mids, allSegments, island.Polygons); // Stitch together the remaining segments //Point[][] polys = StitchSegments2.Stitch(segments); return StitchSegments2.Stitch(segments); //OLD //if (polys == null) //{ // //NOTE: This assumes that the segments form a convex hull, which is likely incorrect // //retVal = GetConvexHull(segments); // throw new ApplicationException("Fix this"); //} //if (polys.Length == 1) //{ // return new Polygon2D[] { new Polygon2D(polys[0]) }; //} //else //{ // return MergeIslandSprtFindAHoles(polys); //} }
private static Tuple<Point, Point>[] MergeIslandSprtSegments(PolygonIsland island) { List<Tuple<Point, Point>> retVal = new List<Tuple<Point, Point>>(); // Shoot through all the polygons in this island for (int polyCntr = 0; polyCntr < island.Polygons.Length; polyCntr++) { // Shoot through each edge of this polygon foreach (Edge2D edge in Math2D.IterateEdges(island.Polygons[polyCntr], null)) { // Get all intersections of this edge EdgeIntersection[] intersections = MergeIslandSprtSegmentsSprtIntersections(polyCntr, edge, island.Intersections); if (intersections.Length == 0) { // Nothing is intersecting this edge, so add the entire edge retVal.Add(Tuple.Create(edge.Point0, edge.Point1Ext)); } else { // This edge has intersections. Convert into segments retVal.AddRange(MergeIslandSprtSegmentsSprtDivide(polyCntr, edge, intersections)); } } } // Exit Function return retVal.ToArray(); }
private static Point[] MergeIsland(PolygonIsland island) { // Convert into line segments Tuple<Point, Point>[] allSegments = MergeIslandSprtSegments(island); List<Tuple<Point, bool>> mids; // Remove segments that are inside a polygon Tuple<Point, Point>[] segments = MergeIslandSprtRemoveInternal(out mids, allSegments, island.Polygons); // Stitch together the remaining segments Point[] retVal = StitchSegments(segments); if (retVal == null) { //NOTE: This assumes that the segments form a convex hull, which is likely incorrect //retVal = GetConvexHull(segments); throw new ApplicationException("Fix this"); } return retVal; }