using System.Collections.Generic; using System.Linq; using Clipper2Lib; namespace MikroPic.EdaTools.v1.Base.Geometry { public static class EdaPolygonExtensions { /// /// Clona un poligon. /// /// El poligon a clonar. /// El resultat de l'operacio. /// public static EdaPolygon Clone(this EdaPolygon polygon) { return new EdaPolygon(polygon.Outline, polygon.Holes); } /// /// Transforma un poligon. /// /// El poligon a transformar. /// La transformacio. /// El poligon transformat. /// public static EdaPolygon Transform(this EdaPolygon polygon, EdaTransformation transformation) { // Transforma el contorn. // var outline = transformation.Transform(polygon.Outline); // Transforma els forats. // List> holes = null; if (polygon.HasHoles) { holes = new List>(); foreach (var hole in polygon.Holes) holes.Add(transformation.Transform(hole)); } return new EdaPolygon(outline, holes); } /// /// Transforma una coleccio de poligons. /// /// Els poligons. /// La transformacio. /// Els poligons transformats. /// public static IEnumerable Transform(this IEnumerable polygons, EdaTransformation transformation) { var result = new List(); foreach (var polygon in polygons) result.Add(polygon.Transform(transformation)); return result; } public static IEnumerable Combine(this EdaPolygon polygon, params EdaPolygon[] combinePolygons) { return Combine(polygon, (IEnumerable)combinePolygons); } public static IEnumerable Combine(this EdaPolygon polygon, IEnumerable combinePolygons) { var cp = new Clipper64(); cp.AddSubject(ToPath64(polygon.Outline)); foreach (var combinePolygon in combinePolygons) cp.AddClip(ToPath64(combinePolygon.Outline)); var tree = new PolyTree64(); cp.Execute(ClipType.Union, FillRule.NonZero, tree); return ToPolygons(tree); } /// /// Subtraccio d'un o diversos poligons. /// /// El poligon. /// Els poligons a substreure. /// El resultat de l'operacio. Pot ser mes d'un poligon. /// public static IEnumerable Substract(this EdaPolygon polygon, params EdaPolygon[] substractPolygons) { return polygon.Substract((IEnumerable)substractPolygons); } /// /// Subtraccio d'un o diversos poligons. /// /// El poligon. /// Els poligons a substreure. /// El resultat de l'operacio. Pot ser mes d0un poligon. /// public static IEnumerable Substract(this EdaPolygon polygon, IEnumerable substractPolygons) { var cp = new Clipper64(); cp.AddSubject(ToPath64(polygon.Outline)); foreach (var substractPolygon in substractPolygons) cp.AddClip(ToPath64(substractPolygon.Outline)); var tree = new PolyTree64(); cp.Execute(ClipType.Difference, FillRule.NonZero, tree); return ToPolygons(tree); } /// /// Canvia el tamany d'un poligon. /// /// El poligon. /// El increment de tamany. /// El resultat. /// public static EdaPolygon Offset(this EdaPolygon polygon, int delta) { var cpo = new ClipperOffset(); var path = new Paths64(); path.Add(ToPath64(polygon.Outline)); if (polygon.HasHoles) foreach (var hole in polygon.Holes) path.Add(ToPath64(hole, true)); cpo.AddPaths(path, JoinType.Round, EndType.Polygon); var solution = cpo.Execute(delta); List outline = ToPoints(solution[0]); List> holes = null; if (solution.Count > 1) { holes = new List>(solution.Count - 1); for (int i = 1; i < solution.Count; i++) holes.Add(ToPoints(solution[i], true)); } return new EdaPolygon(outline, holes); } public static IEnumerable Offset(this IEnumerable polygons, int delta) { var result = new List(); foreach (var polygon in polygons) result.Add(Offset(polygon, delta)); return result; } public static EdaPolygon Clean(this EdaPolygon polygon) { double epsilon = 2500; var outline = ToPoints(Clipper.RamerDouglasPeucker(ToPath64(polygon.Outline), epsilon)); List> holes = null; if (polygon.HasHoles) { holes = new List>(); foreach (var hole in polygon.Holes) holes.Add(ToPoints(Clipper.RamerDouglasPeucker(ToPath64(hole), epsilon))); } return new EdaPolygon(outline, holes); } public static IEnumerable Clean(this IEnumerable polygons) { var result = new List(); foreach (var polygon in polygons) result.Add(Clean(polygon)); return result; } private static Path64 ToPath64(IEnumerable points, bool reverse = false) { var path = new Path64(points.Select(i => new Point64(i.X, i.Y))); if (reverse) path.Reverse(); return path; } private static List ToPoints(Path64 path, bool reverse = false) { var points = new List(path.Select(i => new EdaPoint((int)i.X, (int)i.Y))); if (reverse) points.Reverse(); return points; } private static IEnumerable ToPolygons(PolyPath64 tree) { var polygons = new List(tree.Count); if (tree.Count > 0) foreach (PolyPath64 item in tree) { List> holes = null; if (item.Count > 0) { holes = new List>(); foreach (PolyPath64 subItem in item) { var hole = new List(subItem.Polygon.Select(i => new EdaPoint((int)i.X, (int)i.Y))); hole.Reverse(); holes.Add(hole); } } var contour = new List(item.Polygon.Select(i => new EdaPoint((int)i.X, (int)i.Y))); polygons.Add(new EdaPolygon(contour, holes)); } return polygons; } } }