public override IPolygon Generate(double param0, double param1, double param2) { // Number of points on the outer circle int n = GetParamValueInt(0, param0); double radius = GetParamValueInt(1, param1); // Current radius and step size double r, h = radius / n; var input = new Polygon(n + 1); // Inner cirlce (radius = 1) (hole) r = 1; input.AddContour(CreateCircle(r, (int)(r / h), 1), 1, new Point(0, 0)); // Center cirlce r = (radius + 1.0) / 2.0; input.AddContour(CreateCircle(r, (int)(r / h), 2), 2); //count = input.Count; // Outer cirlce r = radius; input.AddContour(CreateCircle(r, (int)(r / h), 3), 3); // Regions: |++++++|++++++|---| // r 1 0 input.Regions.Add(new RegionPointer((r + 3.0) / 4.0, 0, 1)); input.Regions.Add(new RegionPointer((3 * r + 1.0) / 4.0, 0, 2)); return(input); }
private Polygon BuildGpcPolygonFromPointArray(Point[] pointArray) { Polygon polygon = new Polygon(); VertexList contour = new VertexList(pointArray); polygon.AddContour(contour, false); return polygon; }
// manually implement triangulation algorithm // library takes like 8 seconds public static List <VertexPositionColor> Tesselate(List <List <ContourVertex> > contours, Microsoft.Xna.Framework.Color color) { // sometimes this seems to get stuck in an infinite loop? var task = Task.Run(() => { try { Polygon polygon = new Polygon(); foreach (var contour in contours) { if (contour.Count > 2) { bool isHole = contour[0].Data != null && ((bool)contour[0].Data); List <Vertex> blah = new List <Vertex>(); foreach (var v in contour) { blah.Add(new Vertex(v.Position.X, v.Position.Y)); } polygon.AddContour(blah, 0, isHole); } } List <VertexPositionColor> triangles = new List <VertexPositionColor>(); if (contours.Count == 0) { return(triangles); } float z = 0; var mesh = polygon.Triangulate(); foreach (var triangle in mesh.Triangles) { for (int j = 0; j < 3; j++) { var pos = triangle.GetVertex(j); var pos2 = triangle.GetVertex((j + 1) % 3); // TODO: why 1-y? triangles.Add(new VertexPositionColor(new Vector3((float)pos.X, (float)pos.Y, z), color)); //triangles.Add(new VertexPositionColor(new Vector3((float)pos2.X, (float)pos2.Y, z), Color.Green)); } } return(triangles); } catch (Exception ex) { return(null); } }); if (task.Wait(TimeSpan.FromMinutes(2))) { if (task.Result == null) { throw new NotImplementedException(); } return(task.Result); } else { throw new NotImplementedException(); } }
public override IPolygon Generate(double param0, double param1, double param2) { int n = GetParamValueInt(0, param0); int m = n / 2; var input = new Polygon(n + 1); double ro, r = 10; double step = 2 * Math.PI / m; var inner = new List <Vertex>(m); // Inner ring for (int i = 0; i < m; i++) { inner.Add(new Vertex(r * Math.Cos(i * step), r * Math.Sin(i * step))); } input.AddContour(inner, 1); r = 1.5 * r; var outer = new List <Vertex>(n); step = 2 * Math.PI / n; double offset = step / 2; // Outer ring for (int i = 0; i < n; i++) { ro = r; if (i % 2 == 0) { ro = r + r * Util.Random.NextDouble() * (param1 / 100); } outer.Add(new Vertex(ro * Math.Cos(i * step + offset), ro * Math.Sin(i * step + offset))); } input.AddContour(outer, 2); input.Holes.Add(new Point(0, 0)); return(input); }
private void HandleComplexCase(MeshData meshData, MultiPlaneMeshIndex meshIndex, GradientWrapper gradient, Skeleton skeleton, EdgeResult edge, float roofOffset, float roofHeight) { var polygon = edge.Polygon; var distances = skeleton.Distances; using (var trianglePolygon = new Polygon(polygon.Count, ObjectPool)) { trianglePolygon.AddContour(polygon.Select(p => new Point(p.X, p.Y)).ToList()); var mesh = trianglePolygon.Triangulate(); bool planeIsAdded = false; foreach (var triangle in mesh.Triangles) { var p0 = new Vector2d(triangle.vertices[0].X, triangle.vertices[0].Y); var p1 = new Vector2d(triangle.vertices[1].X, triangle.vertices[1].Y); var p2 = new Vector2d(triangle.vertices[2].X, triangle.vertices[2].Y); double y; if (distances.TryGetValue(p0, out y) && y > 0) { y = roofHeight + roofOffset; } else { y = roofOffset; } var v0 = new Vector3((float)p0.X, (float)y, (float)p0.Y); if (distances.TryGetValue(p1, out y) && y > 0) { y = roofHeight + roofOffset; } else { y = roofOffset; } var v1 = new Vector3((float)p1.X, (float)y, (float)p1.Y); if (distances.TryGetValue(p2, out y) && y > 0) { y = roofHeight + roofOffset; } else { y = roofOffset; } var v2 = new Vector3((float)p2.X, (float)y, (float)p2.Y); if (!planeIsAdded) { meshIndex.AddPlane(v0, v1, v2, meshData.NextIndex); planeIsAdded = true; } AddTriangle(meshData, gradient, v0, v1, v2); } } }
private static void ConvertPolygon(List <PolygonVertices> src, Polygon dest) { dest.RemoveAllContours(); foreach (var polygon in src) { dest.AddContour(polygon); } }
public override IPolygon Generate(double param0, double param1, double param2) { int n = GetParamValueInt(1, param1); var input = new Polygon(n + 4); double r = GetParamValueInt(2, param2); // Generate circle (hole) input.AddContour(CreateCircle(r, n, 1), 1, new Point(0, 0)); n = GetParamValueInt(0, param0); // Generate box input.AddContour(CreateRectangle(new Rectangle(-50, -50, 100, 100), n, 2), 2); return(input); }
static public void ConvertPolygon(List <DIPolygon> src, Polygon dest) { dest.RemoveAllContours(); foreach (var polygon in src) { dest.AddContour(polygon); } }
static public Polygon ConvertPolygon(List <DIPolygon> src) { Polygon dest = new Polygon(); foreach (var polygon in src) { dest.AddContour(polygon); } return(dest); }
public void BuildShapeCache() { if (shapecache) { return; } shapecache = true; foreach (var a in OutlineShapes) { if (a.Hole == false) { var polygon = new Polygon(); List <Vertex> V = new List <Vertex>(); for (int i = 0; i < a.Count(); i++) { V.Add(new Vertex(a.Vertices[i].X, a.Vertices[i].Y)); } polygon.AddContour(V); var options = new ConstraintOptions() { ConformingDelaunay = false }; var quality = new QualityOptions() { }; var mesh = polygon.Triangulate(options, quality); foreach (var t in mesh.Triangles) { var A = t.GetVertex(0); var B = t.GetVertex(1); var C = t.GetVertex(2); ShapeCacheTriangles.Add(new Triangle() { A = new PointD(A.X, A.Y), B = new PointD(B.X, B.Y), C = new PointD(C.X, C.Y) }); } } } }
public void FillPath(Color c, GraphicsPath path) { GraphicsPathIterator pathIterator = new GraphicsPathIterator(path); var polygon = new Polygon(); for (int i = 0; i < pathIterator.SubpathCount; i++) { int strIdx, endIdx; bool bClosedCurve; pathIterator.NextSubpath(out strIdx, out endIdx, out bClosedCurve); List <TriangleNet.Geometry.Vertex> V = new List <TriangleNet.Geometry.Vertex>(); for (int j = strIdx; j <= endIdx; j++) { V.Add(new TriangleNet.Geometry.Vertex(path.PathPoints[j].X, path.PathPoints[j].Y)); } polygon.AddContour(V); } var options = new ConstraintOptions() { ConformingDelaunay = true }; var quality = new QualityOptions() { MinimumAngle = 25 }; var mesh = polygon.Triangulate(options, quality); foreach (var t in mesh.Triangles) { var A = t.GetVertex(0); var B = t.GetVertex(1); var C = t.GetVertex(2); AddTriangle((float)A.X, (float)A.Y, (float)B.X, (float)B.Y, (float)C.X, (float)C.Y, c); } }
public void FillShape(SolidBrush P, PolyLine Shape) { // GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); if (P.Color.A < 255) { GL.Enable(EnableCap.Blend); } var polygon = new Polygon(); List <Vertex> V = new List <Vertex>(); for (int i = 0; i < Shape.Count(); i++) { V.Add(new Vertex(Shape.Vertices[i].X, Shape.Vertices[i].Y)); } polygon.AddContour(V); var options = new ConstraintOptions() { ConformingDelaunay = true }; var quality = new QualityOptions() { MinimumAngle = 25 }; var mesh = polygon.Triangulate(options, quality); GL.Begin(BeginMode.Triangles); GL.Color4(P.Color.R, P.Color.G, P.Color.B, P.Color.A); foreach (var t in mesh.Triangles) { var A = t.GetVertex(0); var B = t.GetVertex(1); var C = t.GetVertex(2); GL.Vertex2(A.X, A.Y); GL.Vertex2(B.X, B.Y); GL.Vertex2(C.X, C.Y); } GL.End(); }
protected Core.Geometry.Triangle.Mesh CreateMesh(List <Vector2d> footprint) { using (var polygon = new Polygon(footprint.Count, ObjectPool)) { var list = ObjectPool.NewList <Point>(footprint.Count); list.AddRange(footprint.Select(point => new Point(point.X, point.Y))); polygon.AddContour(list); return(polygon.Triangulate( new ConstraintOptions { ConformingDelaunay = false, SegmentSplitting = 0 }, new QualityOptions { MaximumArea = 20 })); } }
private MeshRegion CreateMeshRegions(Path clipRect, MeshCanvas.Region region, RenderMode renderMode, ref Rectangle2d rect, bool useContours = false) { using (var polygon = new Polygon(256, _objectPool)) { var simplifiedPath = ClipByRectangle(clipRect, region.Shape); var contours = new List <List <Point> >(useContours ? simplifiedPath.Count : 0); foreach (var path in simplifiedPath) { var area = Clipper.Area(path); // skip small polygons to prevent triangulation issues if (Math.Abs(area / DoubleScale) < 0.001) { continue; } var vertices = GetVertices(path, renderMode, ref rect); // sign of area defines polygon orientation polygon.AddContour(vertices, area < 0); if (useContours) { contours.Add(vertices); } } contours.ForEach(c => c.Reverse()); var mesh = polygon.Points.Any() ? GetMesh(polygon, renderMode) : null; return(new MeshRegion { GradientKey = region.GradientKey, ElevationNoiseFreq = region.ElevationNoiseFreq, ColorNoiseFreq = region.ColorNoiseFreq, ModifyMeshAction = region.ModifyMeshAction, Mesh = mesh, Contours = contours }); } }
public void FillPath(Color c, PointF [] path) { var polygon = new Polygon(); List <TriangleNet.Geometry.Vertex> V = new List <TriangleNet.Geometry.Vertex>(); for (int j = 0; j < path.Length; j++) { V.Add(new TriangleNet.Geometry.Vertex(path[j].X, path[j].Y)); } polygon.AddContour(V); if (V.Count >= 3) { try { var options = new ConstraintOptions() { }; var quality = new QualityOptions() { }; var mesh = polygon.Triangulate(options, quality); foreach (var t in mesh.Triangles) { var A = t.GetVertex(0); var B = t.GetVertex(1); var C = t.GetVertex(2); AddTriangle((float)A.X, (float)A.Y, (float)B.X, (float)B.Y, (float)C.X, (float)C.Y, c); } } catch (Exception) { } } }
// Store polygon from dxf file public List <Polygon> getArea(string filename) { // read the dxf file DxfDocument dxfTest; dxfTest = OpenProfile(filename); int numberSegments = 16; int blockNumber = -1; var polygons = new List <Polygon>(); var Poly = new Polygon(); // loop over all relevant blacks and store the hatch boundaries foreach (var bl in dxfTest.Blocks) { // loop over the enteties in the block and decompose them if they belong to an aluminum layer foreach (var ent in bl.Entities) { if (ent.Layer.Name.ToString() == "0S-Alu hatch") { Poly = new Polygon(); blockNumber++; HatchPattern hp = HatchPattern.Solid; Hatch myHatch = new Hatch(hp, false); myHatch = (Hatch)ent; int pathNumber = -1; foreach (var bPath in myHatch.BoundaryPaths) { pathNumber++; var contour = new List <Vertex>(); // Store the contour for (int i = 0; i < bPath.Edges.Count; i++) { switch (bPath.Edges[i].Type.ToString().ToLower()) { case "line": var myLine = (netDxf.Entities.HatchBoundaryPath.Line)bPath.Edges[i]; var vLine = new Vertex(); vLine.X = myLine.Start.X; vLine.Y = myLine.Start.Y; contour.Add(vLine); break; case "arc": var myArc = (netDxf.Entities.HatchBoundaryPath.Arc)bPath.Edges[i]; double delta = (myArc.EndAngle - myArc.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vArc = new Vertex(); double angleArc = (myArc.StartAngle + j * delta) * Math.PI / 180.0; if (myArc.IsCounterclockwise == true) { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(angleArc); } else { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(Math.PI + angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(Math.PI - angleArc); } contour.Add(vArc); } break; case "ellipse": var myEllipse = (netDxf.Entities.HatchBoundaryPath.Ellipse)bPath.Edges[i]; double deltaEllipse = (myEllipse.EndAngle - myEllipse.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vEllipse = new Vertex(); var ellipseRadius = Math.Sqrt(Math.Pow(myEllipse.EndMajorAxis.X, 2) + Math.Pow(myEllipse.EndMajorAxis.Y, 2)); double angleEllipse = (myEllipse.StartAngle + j * deltaEllipse) * Math.PI / 180.0; if (myEllipse.IsCounterclockwise == true) { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(angleEllipse); } else { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(Math.PI + angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(Math.PI - angleEllipse); } contour.Add(vEllipse); } break; } } bool hole = true; // Add to the poly if (blockNumber == 0 || blockNumber == 1) { if (pathNumber == 0) { hole = false; } Poly.AddContour(points: contour, marker: 0, hole: hole); } } polygons.Add(Poly); } } } numberSegments = 16; // changed now blockNumber = -1; foreach (var bl in dxfTest.Blocks) { // loop over the enteties in the block and decompose them if they belong to an aluminum layer foreach (var ent in bl.Entities) { if (ent.Layer.Name.ToString() == "0S-Plastic hatch") { Poly = new Polygon(); blockNumber++; HatchPattern hp = HatchPattern.Solid; Hatch myHatch = new Hatch(hp, false); myHatch = (Hatch)ent; int pathNumber = -1; foreach (var bPath in myHatch.BoundaryPaths) { pathNumber++; var contour = new List <Vertex>(); // Store the contour for (int i = 0; i < bPath.Edges.Count; i++) { switch (bPath.Edges[i].Type.ToString().ToLower()) { case "line": var myLine = (netDxf.Entities.HatchBoundaryPath.Line)bPath.Edges[i]; var vLine = new Vertex(); vLine.X = myLine.Start.X; vLine.Y = myLine.Start.Y; contour.Add(vLine); break; case "arc": var myArc = (netDxf.Entities.HatchBoundaryPath.Arc)bPath.Edges[i]; double delta = (myArc.EndAngle - myArc.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vArc = new Vertex(); double angleArc = (myArc.StartAngle + j * delta) * Math.PI / 180.0; if (myArc.IsCounterclockwise == true) { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(angleArc); } else { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(Math.PI + angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(Math.PI - angleArc); } contour.Add(vArc); } break; case "ellipse": var myEllipse = (netDxf.Entities.HatchBoundaryPath.Ellipse)bPath.Edges[i]; double deltaEllipse = (myEllipse.EndAngle - myEllipse.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vEllipse = new Vertex(); var ellipseRadius = Math.Sqrt(Math.Pow(myEllipse.EndMajorAxis.X, 2) + Math.Pow(myEllipse.EndMajorAxis.Y, 2)); double angleEllipse = (myEllipse.StartAngle + j * deltaEllipse) * Math.PI / 180.0; if (myEllipse.IsCounterclockwise == true) { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(angleEllipse); } else { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(Math.PI + angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(Math.PI - angleEllipse); } contour.Add(vEllipse); } break; } } bool hole = true; // Add to the poly if (blockNumber == 0 || blockNumber == 1) { if (pathNumber == 0) { hole = false; } Poly.AddContour(points: contour, marker: 0, hole: hole); } } polygons.Add(Poly); } } } numberSegments = 16; // changed now blockNumber = -1; foreach (var bl in dxfTest.Blocks) { // loop over the enteties in the block and decompose them if they belong to an aluminum layer foreach (var ent in bl.Entities) { if (ent.Layer.Name.ToString() == "0S-PT hatch") { Poly = new Polygon(); blockNumber++; HatchPattern hp = HatchPattern.Solid; Hatch myHatch = new Hatch(hp, false); myHatch = (Hatch)ent; int pathNumber = -1; foreach (var bPath in myHatch.BoundaryPaths) { pathNumber++; var contour = new List <Vertex>(); // Store the contour for (int i = 0; i < bPath.Edges.Count; i++) { switch (bPath.Edges[i].Type.ToString().ToLower()) { case "line": var myLine = (netDxf.Entities.HatchBoundaryPath.Line)bPath.Edges[i]; var vLine = new Vertex(); vLine.X = myLine.Start.X; vLine.Y = myLine.Start.Y; contour.Add(vLine); break; case "arc": var myArc = (netDxf.Entities.HatchBoundaryPath.Arc)bPath.Edges[i]; double delta = (myArc.EndAngle - myArc.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vArc = new Vertex(); double angleArc = (myArc.StartAngle + j * delta) * Math.PI / 180.0; if (myArc.IsCounterclockwise == true) { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(angleArc); } else { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(Math.PI + angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(Math.PI - angleArc); } contour.Add(vArc); } break; case "ellipse": var myEllipse = (netDxf.Entities.HatchBoundaryPath.Ellipse)bPath.Edges[i]; double deltaEllipse = (myEllipse.EndAngle - myEllipse.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vEllipse = new Vertex(); var ellipseRadius = Math.Sqrt(Math.Pow(myEllipse.EndMajorAxis.X, 2) + Math.Pow(myEllipse.EndMajorAxis.Y, 2)); double angleEllipse = (myEllipse.StartAngle + j * deltaEllipse) * Math.PI / 180.0; if (myEllipse.IsCounterclockwise == true) { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(angleEllipse); } else { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(Math.PI + angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(Math.PI - angleEllipse); } contour.Add(vEllipse); } break; } } bool hole = true; // Add to the poly if (blockNumber == 0 || blockNumber == 1) { if (pathNumber == 0) { hole = false; } Poly.AddContour(points: contour, marker: 0, hole: hole); } } polygons.Add(Poly); } } } return(polygons); }
//private void Triangulate() //{ // double minAngle = 0; // double maxAngle = 0; // double maxArea = 0; // Polygon p = new Polygon(); // var options = new ConstraintOptions(); // foreach (var item in points) // vertexes.Add(new Vertex {X = item.X , Y = item.Y }); // //set graphics // p.AddContour(vertexes); // options = new ConstraintOptions() { ConformingDelaunay = true , Convex = false }; // //set graphics // Graphics graph = pictureBox1.CreateGraphics(); // Graphics gr = pictureBox1.CreateGraphics(); // int w = pictureBox1.ClientSize.Width / 2; // int h = pictureBox1.ClientSize.Height / 2; // //graph.TranslateTransform(w, h); // gr.TranslateTransform(w, h); // //graph.ScaleTransform(1, -1); // //triangulation // var quality = new QualityOptions() { MinimumAngle = minAngle, MaximumAngle = maxAngle, MaximumArea = maxArea }; // var mesh = p.Triangulate(options, quality); // ListOfTriangles = mesh.Triangles.ToList(); // CT = mesh.Vertices.ToList(); // NTG = getBoundsVertices(mesh); // for (int i = 0; i < ListOfTriangles.Count; ++i) // { // graph.DrawPolygon(Pens.Black, new PointF[3] { new PointF((float)ListOfTriangles[i].GetVertex(0).X, (float)ListOfTriangles[i].GetVertex(0).Y), // new PointF((float)ListOfTriangles[i].GetVertex(1).X, (float)ListOfTriangles[i].GetVertex(1).Y), // new PointF((float)ListOfTriangles[i].GetVertex(2).X, (float)ListOfTriangles[i].GetVertex(2).Y) }); // PointF centroid = new PointF(((float)ListOfTriangles[i].GetVertex(0).X + (float)ListOfTriangles[i].GetVertex(1).X + (float)ListOfTriangles[i].GetVertex(2).X) / 3.0f, // ((float)ListOfTriangles[i].GetVertex(0).Y + (float)ListOfTriangles[i].GetVertex(1).Y + (float)ListOfTriangles[i].GetVertex(2).Y) / 3.0f); // centroid.Y = -centroid.Y; // centroid.Y -= 5; // centroid.X -= 5; // if (ListOfTriangles.Count < 200 && (maxArea >= 800 || maxArea == 0)) // { // DrawText(centroid, ListOfTriangles[i].ID.ToString(), gr, true); // } // } // if (ListOfTriangles.Count < 200 && (maxArea >= 800 || maxArea == 0)) // { // for (int i = 0; i < CT.Count; i++) // { // DrawTextRed(new PointF((float)CT[i].X, (float)-CT[i].Y), CT[i].ID.ToString(), gr); // } // } // textBox1.Text = ListOfTriangles.Count.ToString(); // foreach (var item in ListOfTriangles) // richTextBox1.Text += item.GetVertex(0).X+" : "+ item.GetVertex(0).Y + " - "+ item.GetVertex(1).X + " : " + item.GetVertex(1).Y + " - "+ item.GetVertex(2).X + " : " + item.GetVertex(2).Y + "\n"; //} private void Triangulate() { double minAngle = 0; double maxAngle = 0; double maxArea = 0; Polygon p = new Polygon(); var options = new ConstraintOptions(); foreach (var item in points) { vertexes.Add(new Vertex { X = item.X, Y = item.Y }); } p.AddContour(vertexes); options = new ConstraintOptions() { ConformingDelaunay = true, Convex = false }; Graphics graph = pictureBox1.CreateGraphics(); Graphics gr = pictureBox1.CreateGraphics(); //int w = pictureBox1.ClientSize.Width / 2; //int h = pictureBox1.ClientSize.Height / 2; //graph.TranslateTransform(w, h); //gr.TranslateTransform(w, h); //graph.ScaleTransform(1, -1); //triangulation var quality = new QualityOptions() { MinimumAngle = minAngle, MaximumAngle = maxAngle, MaximumArea = maxArea }; var mesh = p.Triangulate(options, quality); ListOfTriangles = mesh.Triangles.ToList(); CT = mesh.Vertices.ToList(); NTG = getBoundsVertices(mesh); for (int i = 0; i < ListOfTriangles.Count; ++i) { graph.DrawPolygon(Pens.Black, new PointF[3] { new PointF((float)ListOfTriangles[i].GetVertex(0).X, (float)ListOfTriangles[i].GetVertex(0).Y), new PointF((float)ListOfTriangles[i].GetVertex(1).X, (float)ListOfTriangles[i].GetVertex(1).Y), new PointF((float)ListOfTriangles[i].GetVertex(2).X, (float)ListOfTriangles[i].GetVertex(2).Y) }); PointF centroid = new PointF(((float)ListOfTriangles[i].GetVertex(0).X + (float)ListOfTriangles[i].GetVertex(1).X + (float)ListOfTriangles[i].GetVertex(2).X) / 3.0f, ((float)ListOfTriangles[i].GetVertex(0).Y + (float)ListOfTriangles[i].GetVertex(1).Y + (float)ListOfTriangles[i].GetVertex(2).Y) / 3.0f); centroid.Y = -centroid.Y; centroid.Y -= 5; centroid.X -= 5; if (ListOfTriangles.Count < 200 && (maxArea >= 800 || maxArea == 0)) { DrawText(centroid, ListOfTriangles[i].ID.ToString(), gr, true); } } if (ListOfTriangles.Count < 200 && (maxArea >= 800 || maxArea == 0)) { for (int i = 0; i < CT.Count; i++) { DrawTextRed(new PointF((float)CT[i].X, (float)-CT[i].Y), CT[i].ID.ToString(), gr); } } ABCD(); triangulate.Visible = false; button1.Visible = false; for (int i = 0; i < ListOfTriangles.Count; ++i) { richTextBox1.Text += (i + 1) + " ) " + Math.Round(ListOfTriangles[i].GetVertex(0).X, 3) + " : " + Math.Round(ListOfTriangles[i].GetVertex(0).Y, 3) + " - " + Math.Round(ListOfTriangles[i].GetVertex(1).X, 3) + " : " + Math.Round(ListOfTriangles[i].GetVertex(1).Y, 3) + " - " + Math.Round(ListOfTriangles[i].GetVertex(2).X, 3) + " : " + Math.Round(ListOfTriangles[i].GetVertex(2).Y, 3) + "\nA : " + A[i] + " B : " + B[i] + " C : " + C[i] + " D : " + D[i] + "\nCenter : " + Center[i] + "\nR : " + Math.Round(R[i], 3) + "\n\n"; } }
// find polygon depth public double dxfPolydepth(string filename) { // read the dxf file DxfDocument dxfTest; var poly = new Polygon(); // dxfTest = OpenProfile("382290.dxf"); dxfTest = OpenProfile(filename); int numberSegments = 16; int blockNumber = -1; // loop over all relevant blacks and store the hatch boundaries foreach (var bl in dxfTest.Blocks) { // loop over the enteties in the block and decompose them if they belong to an aluminum layer foreach (var ent in bl.Entities) { if (ent.Layer.Name.ToString().Contains("0S-Alu hatch")) //(ent.Layer.Name.ToString() == "0S-Alu hatch") { blockNumber++; HatchPattern hp = HatchPattern.Solid; Hatch myHatch = new Hatch(hp, false); myHatch = (Hatch)ent; int pathNumber = -1; foreach (var bPath in myHatch.BoundaryPaths) { pathNumber++; // define the contour list var contour = new List <Vertex>(); for (int i = 0; i < bPath.Edges.Count; i++) { switch (bPath.Edges[i].Type.ToString().ToLower()) { case "line": var myLine = (netDxf.Entities.HatchBoundaryPath.Line)bPath.Edges[i]; var vLine = new Vertex(); vLine.X = myLine.Start.X; vLine.Y = myLine.Start.Y; contour.Add(vLine); break; case "arc": var myArc = (netDxf.Entities.HatchBoundaryPath.Arc)bPath.Edges[i]; double delta = (myArc.EndAngle - myArc.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vArc = new Vertex(); double angleArc = (myArc.StartAngle + j * delta) * Math.PI / 180.0; if (myArc.IsCounterclockwise == true) { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(angleArc); } else { vArc.X = myArc.Center.X + myArc.Radius * Math.Cos(Math.PI + angleArc); vArc.Y = myArc.Center.Y + myArc.Radius * Math.Sin(Math.PI - angleArc); } contour.Add(vArc); } break; case "ellipse": var myEllipse = (netDxf.Entities.HatchBoundaryPath.Ellipse)bPath.Edges[i]; double deltaEllipse = (myEllipse.EndAngle - myEllipse.StartAngle) / numberSegments; for (int j = 0; j < numberSegments; j++) { var vEllipse = new Vertex(); var ellipseRadius = Math.Sqrt(Math.Pow(myEllipse.EndMajorAxis.X, 2) + Math.Pow(myEllipse.EndMajorAxis.Y, 2)); double angleEllipse = (myEllipse.StartAngle + j * deltaEllipse) * Math.PI / 180.0; if (myEllipse.IsCounterclockwise == true) { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(angleEllipse); } else { vEllipse.X = myEllipse.Center.X + ellipseRadius * Math.Cos(Math.PI + angleEllipse); vEllipse.Y = myEllipse.Center.Y + ellipseRadius * Math.Sin(Math.PI - angleEllipse); } contour.Add(vEllipse); } break; } } bool hole = true; if (pathNumber == 0) { hole = false; } poly.AddContour(points: contour, marker: 0, hole: hole); } } } } // Get the depth double ytop = double.NegativeInfinity; double ybottom = double.PositiveInfinity; foreach (var vertex in poly.Points) { if (ytop <= vertex.Y) { ytop = vertex.Y; } if (ybottom >= vertex.Y) { ybottom = vertex.Y; } } return(ytop - ybottom); }