public PolygonClosedWithNormalsD2D(PolygonClosedD2D template) { var numPoints = template.Points.Length + template.SharpPoints.Count; _points = new PointD2D[numPoints]; _normals = new PointD2D[numPoints]; var srcPoints = template.Points; var srcCount = srcPoints.Length; var startPoint = srcPoints[srcCount - 1]; int destIdx = 0; for (int i = 0; i < srcCount; ++i) { var toHereVector = srcPoints[i] - startPoint; var fromHereVector = srcPoints[(i + 1) % srcCount] - srcPoints[i]; if (template.SharpPoints.Contains(srcPoints[i])) { var normal = GetNormal(toHereVector, template.IsHole); _points[destIdx] = srcPoints[i]; _normals[destIdx] = normal; ++destIdx; normal = GetNormal(fromHereVector, template.IsHole); _points[destIdx] = srcPoints[i]; _normals[destIdx] = normal; ++destIdx; } else { var normal = GetNormal(toHereVector + fromHereVector, template.IsHole); _points[destIdx] = srcPoints[i]; _normals[destIdx] = normal; ++destIdx; } startPoint = srcPoints[i]; } if (!(numPoints == destIdx)) { throw new InvalidProgramException(); } }
private static void ClipperPolyTreeToPolygonListRecursively(ClipperLib.PolyNode node, HashSet<ClipperLib.IntPoint> sharpPoints, HashSet<ClipperLib.IntPoint> allPoints, List<PolygonClosedD2D> polygonList, IDictionary<ClipperLib.PolyNode, PolygonClosedD2D> dictClipperNodeToNode) { if (node.Contour != null && node.Contour.Count != 0) { var pointsInThisPolygon = node.Contour.Select(clipperPt => new PointD2D(clipperPt.X / 65536.0, clipperPt.Y / 65536.0)); var sharpPointsInThisPolygon = node.Contour.Where(clipperPt => sharpPoints.Contains(clipperPt)).Select(clipperPt => new PointD2D(clipperPt.X / 65536.0, clipperPt.Y / 65536.0)); var polygon = new PolygonClosedD2D(pointsInThisPolygon.ToArray(), new HashSet<PointD2D>(sharpPointsInThisPolygon)); polygon.IsHole = node.IsHole; polygonList.Add(polygon); if (node.IsHole) { polygon.Parent = dictClipperNodeToNode[node.Parent]; } dictClipperNodeToNode.Add(node, polygon); } if (0 != node.ChildCount) { foreach (var childNode in node.Childs) { ClipperPolyTreeToPolygonListRecursively(childNode, sharpPoints, allPoints, polygonList, dictClipperNodeToNode); } } }
public static Poly2Tri.Polygon GetTriangles(PolygonClosedD2D polygons) { var mainPolygon = new Poly2Tri.Polygon(polygons.Points.Select(pt => new Poly2Tri.PolygonPoint(pt.X, pt.Y))); Poly2Tri.P2T.Triangulate(mainPolygon); return mainPolygon; }
/// <summary> /// Triangulates the specified polygons. The result are indexed triangles. /// </summary> /// <param name="polygon">The polygon to triangulate.</param> /// <returns>Instance of the <see cref="IndexedTriangles"/> class, which holds the triangle vertices as well as the indices.</returns> private static int[] Triangulate(PolygonClosedD2D polygon) { var triangles = GetTriangles(polygon); var pointList = new List<PointD2D>(); var pointToIndex = new Dictionary<PointD2D, int>(); for (int i = 0; i < polygon.Points.Length; ++i) pointToIndex.Add(polygon.Points[i], i); var indexList = new List<int>(); foreach (var triangle in triangles.Triangles) { var p0 = new PointD2D(triangle.Points[0].X, triangle.Points[0].Y); var p1 = new PointD2D(triangle.Points[1].X, triangle.Points[1].Y); var p2 = new PointD2D(triangle.Points[2].X, triangle.Points[2].Y); int i0, i1, i2; if (!pointToIndex.TryGetValue(p0, out i0)) { throw new InvalidOperationException("Should work except when our cross section was implified by pol2Tri"); } if (!pointToIndex.TryGetValue(p1, out i1)) { throw new InvalidOperationException("Should work except when our cross section was implified by pol2Tri"); } if (!pointToIndex.TryGetValue(p2, out i2)) { throw new InvalidOperationException("Should work except when our cross section was implified by pol2Tri"); } indexList.Add(i0); indexList.Add(i1); indexList.Add(i2); } return indexList.ToArray(); }
public PolygonClosedD2D(PolygonClosedD2D template, double scale) { _points = template._points.Select(p => new PointD2D(p.X * scale, p.Y * scale)).ToArray(); _sharpPoints = new HashSet <PointD2D>(template._sharpPoints.Select(p => new PointD2D(p.X * scale, p.Y * scale))); }
public PolygonClosedD2D(PolygonClosedD2D template, double scale) { _points = template._points.Select(p => new PointD2D(p.X * scale, p.Y * scale)).ToArray(); _sharpPoints = new HashSet<PointD2D>(template._sharpPoints.Select(p => new PointD2D(p.X * scale, p.Y * scale))); }