public static Mesh PolygonsToMesh(List<Polygon> polygons) { //Convert Clipper points to constrained point sets. Mesh mesh = new Mesh (); List<Vector3> verts = new List<Vector3> (); List<int> triangles = new List<int> (); polygons.OrderByDescending(x=> ClipperLib.Clipper.Area(x)); List<Poly2Tri.Polygon> paths = new List<Poly2Tri.Polygon> (); paths.Add ((Poly2Tri.Polygon) polygons[0]); polygons.RemoveAt (0); foreach (Polygon polygon in polygons) { if(ClipperLib.Clipper.Orientation(polygon)) { //Clockwise wound points are not holes and are valid paths. paths.Add(polygon); } else { paths[0].AddHole(polygon); } } Poly2Tri.PolygonSet set = new Poly2Tri.PolygonSet (); foreach (Poly2Tri.Polygon polygon in paths) { set.Add (polygon); } Poly2Tri.P2T.Triangulate (set); foreach(Poly2Tri.Polygon polygon in set.Polygons) { foreach(Poly2Tri.DelaunayTriangle tri in polygon.Triangles) { for(int i = 0; i < 3; i ++) { Vector3 vert = new Vector3(tri.Points[i].Xf,tri.Points[i].Yf,0); if(!verts.Contains(vert)) { verts.Add(vert); } } } } foreach(Poly2Tri.Polygon polygon in set.Polygons) { foreach(Poly2Tri.DelaunayTriangle tri in polygon.Triangles) { for(int i = 2; i >= 0; i--) { triangles.Add(verts.IndexOf(new Vector3(tri.Points[i].Xf,tri.Points[i].Yf,0))); } } } mesh.vertices = verts.ToArray(); mesh.triangles = triangles.ToArray(); return mesh; }
private List<Poly2Tri.DelaunayTriangle> GetTriangleListFromClipperSolution(ClipperLib.PolyTree solution) { Func<ClipperLib.IntPoint, Poly2Tri.PolygonPoint> xfToPolygonPoint = (p) => new Poly2Tri.PolygonPoint(p.X, p.Y); Poly2Tri.PolygonSet polygonSet = new Poly2Tri.PolygonSet(); ClipperLib.PolyNode node = solution.GetFirst(); while (node != null) { // Only interested in closed paths if (!node.IsOpen) { if (node.IsHole) { if (polygonSet.Polygons.Count() > 0) { // Add hole to last polygon entered var polyPoints = node.Contour.Select(xfToPolygonPoint).ToArray(); Poly2Tri.Polygon hole = new Poly2Tri.Polygon(polyPoints); Poly2Tri.Polygon polygon = polygonSet.Polygons.Last(); polygon.AddHole(hole); } } else { // Add a new polygon to the set var polyPoints = node.Contour.Select(xfToPolygonPoint).ToList(); Poly2Tri.Polygon polygon = new Poly2Tri.Polygon(polyPoints); polygonSet.Add(polygon); } } node = node.GetNext(); } // Now triangulate the whole set Poly2Tri.P2T.Triangulate(polygonSet); // Combine all the triangles into one list List<Poly2Tri.DelaunayTriangle> triangles = new List<Poly2Tri.DelaunayTriangle>(); foreach (var polygon in polygonSet.Polygons) { triangles.AddRange(polygon.Triangles); } return triangles; }
private static Poly2Tri.PolygonSet ToP2T(PolygonSet pset) { var rpset = new Poly2Tri.PolygonSet(); foreach (var poly in pset) { var pts = new List <Poly2Tri.PolygonPoint>(); foreach (var p in poly) { pts.Add(new Poly2Tri.PolygonPoint(p.X, p.Y)); } rpset.Add(new Poly2Tri.Polygon(pts)); } return(rpset); }
private List <Poly2Tri.DelaunayTriangle> GetTriangleListFromClipperSolution(ClipperLib.PolyTree solution) { Func <ClipperLib.IntPoint, Poly2Tri.PolygonPoint> xfToPolygonPoint = (p) => new Poly2Tri.PolygonPoint(p.X, p.Y); Poly2Tri.PolygonSet polygonSet = new Poly2Tri.PolygonSet(); ClipperLib.PolyNode node = solution.GetFirst(); while (node != null) { // Only interested in closed paths if (!node.IsOpen) { if (node.IsHole) { if (polygonSet.Polygons.Count() > 0) { // Add hole to last polygon entered var polyPoints = node.Contour.Select(xfToPolygonPoint).ToArray(); Poly2Tri.Polygon hole = new Poly2Tri.Polygon(polyPoints); Poly2Tri.Polygon polygon = polygonSet.Polygons.Last(); polygon.AddHole(hole); } } else { // Add a new polygon to the set var polyPoints = node.Contour.Select(xfToPolygonPoint).ToList(); Poly2Tri.Polygon polygon = new Poly2Tri.Polygon(polyPoints); polygonSet.Add(polygon); } } node = node.GetNext(); } // Now triangulate the whole set Poly2Tri.P2T.Triangulate(polygonSet); // Combine all the triangles into one list List <Poly2Tri.DelaunayTriangle> triangles = new List <Poly2Tri.DelaunayTriangle>(); foreach (var polygon in polygonSet.Polygons) { triangles.AddRange(polygon.Triangles); } return(triangles); }
private static PolygonSet FromP2T(Poly2Tri.PolygonSet pset) { var result = new PolygonSet(); foreach (var poly in pset.Polygons) { foreach (var tri in poly.Triangles) { var rtri = new Polygon(); rtri.Add(new PolygonPoint { X = tri.Points[0].Xf, Y = tri.Points[0].Yf }); rtri.Add(new PolygonPoint { X = tri.Points[1].Xf, Y = tri.Points[1].Yf }); rtri.Add(new PolygonPoint { X = tri.Points[2].Xf, Y = tri.Points[2].Yf }); result.Add(rtri); } } return(result); }
private static Poly2Tri.PolygonSet ToP2T(PolygonSet pset) { var rpset = new Poly2Tri.PolygonSet(); foreach (var poly in pset) { var pts = new List<Poly2Tri.PolygonPoint>(); foreach (var p in poly) pts.Add(new Poly2Tri.PolygonPoint(p.X, p.Y)); rpset.Add(new Poly2Tri.Polygon(pts)); } return rpset; }