public Mesh Mesh2DMinimalBox(Mesh mesh) { List <Point3d> x = new List <Point3d>(); Rhino.Geometry.Collections.MeshTopologyVertexList vs = mesh.TopologyVertices; for (int i = 0; i < vs.Count; i++) { x.Add(new Point3d(vs[i])); } Grasshopper.Kernel.Geometry.Node2List list = new Grasshopper.Kernel.Geometry.Node2List(x); Polyline pl = Grasshopper.Kernel.Geometry.ConvexHull.Solver.ComputeHull(list); double t = double.MaxValue; Transform xform = new Transform(); for (int i = 0; i < pl.Count - 1; i++) { Vector3d Xaxis = pl[i + 1] - pl[i]; Vector3d Yaxis = Vector3d.CrossProduct(Xaxis, Vector3d.ZAxis); Plane p = new Plane(pl[i], Xaxis, Yaxis); Polyline pl2 = new Polyline(pl); pl2.Transform(Transform.PlaneToPlane(p, Plane.WorldXY)); Rhino.Geometry.BoundingBox box = pl2.BoundingBox; double area = (box.Max.X - box.Min.X) * (box.Max.Y - box.Min.Y); if (area < t) { t = area; xform = Transform.PlaneToPlane(p, Plane.WorldXY); } } mesh.Transform(xform); return(mesh); }
public List <Grasshopper.Kernel.Geometry.Voronoi.Cell2> GetVoronoiCells(List <Point3d> pts, Brep b) { Surface fa = b.Faces[0].ToNurbsSurface(); List <Point2d> uvs = new List <Point2d>(); foreach (Point3d p in pts) { double u; double v; fa.ClosestPoint(p, out u, out v); uvs.Add(new Point2d(u, v)); } var nodes = new Grasshopper.Kernel.Geometry.Node2List(); foreach (Point2d p in uvs) { nodes.Append(new Grasshopper.Kernel.Geometry.Node2(p.X, p.Y)); } var outline = new Grasshopper.Kernel.Geometry.Node2List(); for (int i = 0; i < b.Vertices.Count; ++i) { double u; double v; fa.ClosestPoint(b.Vertices[i].Location, out u, out v); outline.Append(new Grasshopper.Kernel.Geometry.Node2(u, v)); } return(Grasshopper.Kernel.Geometry.Voronoi.Solver.Solve_BruteForce(nodes, outline)); }
private void MakeGSAMesh() { GSAmesh = new Mesh(); //convert point3d to node2 //grasshopper requres that nodes are saved within a Node2List for Delaunay var node2s = new Grasshopper.Kernel.Geometry.Node2List(); for (int i = 0; i < nodes.Count; i++) { //map nodes onto the mid plane Point3d mappedPt = new Point3d(); midPlane.RemapToPlaneSpace(nodes[i], out mappedPt); node2s.Append(new Grasshopper.Kernel.Geometry.Node2(mappedPt.X, mappedPt.Y)); GSAmesh.Vertices.Add(nodes[i]); } //solve Delaunay var delMesh = new Mesh(); var faces = new List <Grasshopper.Kernel.Geometry.Delaunay.Face>(); faces = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Faces(node2s, 1); //output in 2d delMesh = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Mesh(node2s, 1, ref faces); if (delMesh != null) { foreach (MeshFace f in delMesh.Faces) { GSAmesh.Faces.AddFace(f); } } }
public Polyline MinimalBox(List <Point3d> x) { Grasshopper.Kernel.Geometry.Node2List list = new Grasshopper.Kernel.Geometry.Node2List(x); Polyline pl = Grasshopper.Kernel.Geometry.ConvexHull.Solver.ComputeHull(list); // List<Polyline> boxes = new List<Polyline>(); Polyline output = new Polyline(); double t = double.MaxValue; for (int i = 0; i < pl.Count - 1; i++) { Vector3d Xaxis = pl[i + 1] - pl[i]; Vector3d Yaxis = Vector3d.CrossProduct(Xaxis, Vector3d.ZAxis); Plane p = new Plane(pl[i], Xaxis, Yaxis); Polyline pl2 = new Polyline(pl); pl2.Transform(Transform.PlaneToPlane(p, Plane.WorldXY)); Rhino.Geometry.BoundingBox box = pl2.BoundingBox; Polyline pl3 = new Polyline(); pl3.Add(box.Corner(false, false, false)); pl3.Add(box.Corner(false, true, false)); pl3.Add(box.Corner(true, true, false)); pl3.Add(box.Corner(true, false, false)); pl3.Add(box.Corner(false, false, false)); double area = pl3[1].DistanceTo(pl3[0]) * pl3[1].DistanceTo(pl3[2]); if (area < t) { t = area; pl3.Transform(Transform.PlaneToPlane(Plane.WorldXY, p)); output = pl3; } // boxes.Add(pl3); } return(output); }
public static Mesh DelaunayMesh2(List <Point3d> pts) { //convert point3d to node2 //grasshopper requres that nodes are saved within a Node2List for Delaunay // pts = DeepCopy(pts); var nodes = new Grasshopper.Kernel.Geometry.Node2List(); foreach (Point3d pt in pts) { nodes.Append(new Grasshopper.Kernel.Geometry.Node2(pt.X, pt.Y)); } //solve Delaunay var delMesh2d = new Mesh(); var delMesh3d = new Mesh(); var faces = new List <Grasshopper.Kernel.Geometry.Delaunay.Face>(); faces = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Faces(nodes, 1); //output delMesh2d = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Mesh(nodes, 1, ref faces); delMesh2d.Compact(); delMesh3d.Faces.AddFaces(delMesh2d.Faces); delMesh3d.Vertices.AddVertices(pts); delMesh3d.Compact(); return(delMesh3d); }
//get deluanay mesh from points public static Mesh pointsToDeluanayMesh(List <Point3d> pts) { //convert point3d to node2 //grasshopper requres that nodes are saved within a Node2List for Delaunay Plane fitPlane = new Plane(); Plane.FitPlaneToPoints(pts, out fitPlane); var nodes = new Grasshopper.Kernel.Geometry.Node2List(); for (int i = 0; i < pts.Count; i++) { double x, y = new double(); fitPlane.ClosestParameter(pts[i], out x, out y); nodes.Append(new Grasshopper.Kernel.Geometry.Node2(x, y)); } //solve Delaunay Mesh delMesh = new Mesh(); var faces = new List <Grasshopper.Kernel.Geometry.Delaunay.Face>(); faces = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Faces(nodes, 1); //output delMesh = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Mesh(nodes, 1, ref faces); for (int i = 0; i < delMesh.Vertices.Count; i++) { delMesh.Vertices.SetVertex(i, pts[i].X, pts[i].Y, pts[i].Z); } return(delMesh); }
public static Mesh DelaunayMesh(List <Point3d> points) { var nodes = new Grasshopper.Kernel.Geometry.Node2List(); for (int i = 0; i < points.Count; i++) { nodes.Append(new Grasshopper.Kernel.Geometry.Node2(points[i].X, points[i].Y)); //notice how we only read in the X and Y coordinates, this is why points should be mapped onto the XY plane } var faces = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Faces(nodes, 1); Mesh delMesh = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Mesh(nodes, 1, ref faces); return(delMesh); }
public bool isHullVertice(int t, Polyline pl) { Grasshopper.Kernel.Geometry.Node2List list2 = new Grasshopper.Kernel.Geometry.Node2List(); List <int> hull = new List <int>(); for (int i = 0; i < pl.Count; i++) { Grasshopper.Kernel.Geometry.Node2 node = new Grasshopper.Kernel.Geometry.Node2(pl[i].X, pl[i].Y); list2.Append(node); } Grasshopper.Kernel.Geometry.ConvexHull.Solver.Compute(list2, hull); return(hull.Contains(t)); }
public Polyline MinimalBox2D(List<Point3d> x) { Grasshopper.Kernel.Geometry.Node2List list = new Grasshopper.Kernel.Geometry.Node2List(x); Polyline pl = Grasshopper.Kernel.Geometry.ConvexHull.Solver.ComputeHull(list); // List<Polyline> boxes = new List<Polyline>(); Polyline output = new Polyline(); double t = double.MaxValue; for(int i = 0;i < pl.Count - 1;i++){ Vector3d Xaxis = pl[i + 1] - pl[i]; Vector3d Yaxis = Vector3d.CrossProduct(Xaxis, Vector3d.ZAxis); Plane p = new Plane(pl[i], Xaxis, Yaxis); Polyline pl2 = new Polyline(pl); pl2.Transform(Transform.PlaneToPlane(p, Plane.WorldXY)); Rhino.Geometry.BoundingBox box = pl2.BoundingBox; Polyline pl3 = new Polyline(); pl3.Add(box.Corner(false, false, false)); pl3.Add(box.Corner(false, true, false)); pl3.Add(box.Corner(true, true, false)); pl3.Add(box.Corner(true, false, false)); pl3.Add(box.Corner(false, false, false)); double area = pl3[1].DistanceTo(pl3[0]) * pl3[1].DistanceTo(pl3[2]); if(area < t){t = area; pl3.Transform(Transform.PlaneToPlane(Plane.WorldXY, p));output = pl3;} // boxes.Add(pl3); } return output; }
public void intersect(Plane p) { for (int i = 0; i < pts.Count; i++) { double db = p.DistanceTo(pts[i].pos); if (Math.Abs(db) < RhinoDoc.ActiveDoc.ModelAbsoluteTolerance) { pts[i].condition = 1; } else if (db > 0) { pts[i].condition = 2; } else if (db < 0) { pts[i].condition = 0; } } /////////////////////// int ii = 0; while (ii < edges.Count) { if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 0) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 1 && edges[ii].p2.condition == 0) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 1 && edges[ii].p2.condition == 1) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 1) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 2) { double u; Line line = new Line(edges[ii].p1.pos, edges[ii].p2.pos); Rhino.Geometry.Intersect.Intersection.LinePlane(line, p, out u); pts.Add(new vertex(line.PointAt(u), this.center.DistanceTo(line.PointAt(u)))); edges[ii].p1 = pts[pts.Count - 1]; ii++; } else if (edges[ii].p1.condition == 2 && edges[ii].p2.condition == 0) { double u; Line line = new Line(edges[ii].p1.pos, edges[ii].p2.pos); Rhino.Geometry.Intersect.Intersection.LinePlane(line, p, out u); pts.Add(new vertex(line.PointAt(u), this.center.DistanceTo(line.PointAt(u)))); edges[ii].p2 = pts[pts.Count - 1]; ii++; } else { ii++; } } clearnull(); ////////////////////////////////// Transform w2p = Transform.PlaneToPlane(Plane.WorldXY, p); Transform p2w = Transform.PlaneToPlane(p, Plane.WorldXY); Grasshopper.Kernel.Geometry.Node2List ls = new Grasshopper.Kernel.Geometry.Node2List(); List <int> count = new List <int>(); for (int i = 0; i < pts.Count; i++) { if (pts[i].condition == 1 || pts[i].condition == -1) { pts[i].pos.Transform(w2p); ls.Append(new Grasshopper.Kernel.Geometry.Node2(pts[i].pos.X, pts[i].pos.Y)); pts[i].pos.Transform(p2w); count.Add(i); } } if (count.Count == 2) { edges.Add(new edge(pts[count[0]], pts[count[1]])); } else if (count.Count > 2) { List <int> count2 = new List <int>(); Grasshopper.Kernel.Geometry.ConvexHull.Solver.Compute(ls, count2); for (int i = 0; i < count2.Count; i++) { int c = i + 1; if (c == count2.Count) { c = 0; } edges.Add(new edge(pts[count[count2[i]]], pts[count[count2[c]]])); } } }
protected override void SolveInstance(IGH_DataAccess DA) { checked { var Points = new List <Point3d>(); DA.GetDataList(0, Points); var attrition = 1.0; DA.GetData("Attrition", ref attrition); var nodeTwoList = new Grasshopper.Kernel.Geometry.Node2List(Points); var delaunayFaces = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Faces(nodeTwoList, 1); var delaunayMesh = Grasshopper.Kernel.Geometry.Delaunay.Solver.Solve_Mesh(nodeTwoList, 1, ref delaunayFaces); delaunayMesh.Weld(Math.PI); var list = new List <object>(); var circles = new SurfaceComponents.MeshComponents.Component_MeshFaceCircles(); var param = circles.Params.Input[0] as Grasshopper.Kernel.GH_PersistentGeometryParam <Grasshopper.Kernel.Types.GH_Mesh>; param.PersistentData.ClearData(); param.PersistentData.Append(new GH_Mesh(delaunayMesh)); circles.ExpireSolution(true); //add to a dummy document so we can read outputs var doc = new Grasshopper.Kernel.GH_Document(); doc.AddObject(circles, false); //read output circles circles.Params.Output[0].CollectData(); var ratio = new double[circles.Params.Output[1].VolatileDataCount]; for (int i = 0; i < circles.Params.Output[0].VolatileDataCount; ++i) { //ratio[i] = circles.Params.Output[1].VolatileData.get_Branch(0)[i]; GH_Convert.ToDouble(circles.Params.Output[1].VolatileData.get_Branch(0)[i], out ratio[i], GH_Conversion.Both); list.Add(circles.Params.Output[0].VolatileData.get_Branch(0)[i]); } var arcsList = new double[list.Count]; var arcs = new AnalysisComponents.Component_DeconstructArc(); var arcParams = arcs.Params.Input[0] as Grasshopper.Kernel.GH_PersistentParam <Grasshopper.Kernel.Types.GH_Arc>; arcParams.PersistentData.ClearData(); var circle = new Arc[delaunayMesh.Faces.Count]; for (int i = 0; i < list.Count; ++i) { GH_Convert.ToArc(list[i], ref circle[i], GH_Conversion.Both); arcParams.PersistentData.Append(new GH_Arc(circle[i])); } arcs.ExpireSolution(true); var docOne = new Grasshopper.Kernel.GH_Document(); docOne.AddObject(arcs, false); arcs.Params.Output[0].CollectData(); for (int i = 0; i < arcs.Params.Output[1].VolatileDataCount; ++i) { GH_Convert.ToDouble(arcs.Params.Output[1].VolatileData.get_Branch(0)[i], out arcsList[i], GH_Conversion.Both); //arcsList.Add(arcs.Params.Output[1].VolatileData.get_Branch(0)[i]); } var faceMesh = new List <object>(); var verticesMesh = new List <object>(); var deMesh = new SurfaceComponents.MeshComponents.Component_DeconstructMesh(); var meshParams = deMesh.Params.Input[0] as Grasshopper.Kernel.GH_PersistentParam <Grasshopper.Kernel.Types.GH_Mesh>; meshParams.PersistentData.ClearData(); meshParams.PersistentData.Append(new GH_Mesh(delaunayMesh)); deMesh.ExpireSolution(true); var docTwo = new Grasshopper.Kernel.GH_Document(); docTwo.AddObject(deMesh, false); deMesh.Params.Output[0].CollectData(); for (int i = 0; i < deMesh.Params.Output[0].VolatileDataCount; ++i) { verticesMesh.Add(deMesh.Params.Output[0].VolatileData.get_Branch(0)[i]); } for (int i = 0; i < deMesh.Params.Output[1].VolatileDataCount; ++i) { faceMesh.Add(deMesh.Params.Output[1].VolatileData.get_Branch(0)[i]); } var faceCullRadius = RadiusSorting(faceMesh, arcsList); Array.Sort(ratio); var splitListIndex = Convert.ToInt32((ratio[ratio.Length - 1] * faceMesh.Count) * attrition); //var splitListIndex = Convert.ToInt32(Attrition); var splitList = SplitList(faceCullRadius, splitListIndex); var constructMesh = new Mesh(); var meshPoints = new Point3d[verticesMesh.Count]; for (int i = 0; i < verticesMesh.Count; ++i) { GH_Convert.ToPoint3d(verticesMesh[i], ref meshPoints[i], GH_Conversion.Both); constructMesh.Vertices.Add(meshPoints[i]); } var meshFaces = new Grasshopper.Kernel.Types.GH_MeshFace[splitList.Count]; for (int i = 0; i < splitList.Count; ++i) { GH_Convert.ToGHMeshFace(splitList[i], GH_Conversion.Both, ref meshFaces[i]); constructMesh.Faces.AddFace(meshFaces[i].Value); } var ConcaveHull = constructMesh.GetNakedEdges(); DA.SetDataList(0, ConcaveHull); } }
public Mesh Mesh2DMinimalBox(Mesh mesh) { List<Point3d> x = new List<Point3d>(); Rhino.Geometry.Collections.MeshTopologyVertexList vs = mesh.TopologyVertices; for (int i = 0; i < vs.Count; i++) { x.Add(new Point3d(vs[i])); } Grasshopper.Kernel.Geometry.Node2List list = new Grasshopper.Kernel.Geometry.Node2List(x); Polyline pl = Grasshopper.Kernel.Geometry.ConvexHull.Solver.ComputeHull(list); double t = double.MaxValue; Transform xform = new Transform(); for (int i = 0; i < pl.Count - 1; i++) { Vector3d Xaxis = pl[i + 1] - pl[i]; Vector3d Yaxis = Vector3d.CrossProduct(Xaxis, Vector3d.ZAxis); Plane p = new Plane(pl[i], Xaxis, Yaxis); Polyline pl2 = new Polyline(pl); pl2.Transform(Transform.PlaneToPlane(p, Plane.WorldXY)); Rhino.Geometry.BoundingBox box = pl2.BoundingBox; double area = (box.Max.X - box.Min.X) * (box.Max.Y - box.Min.Y); if (area < t) { t = area; xform = Transform.PlaneToPlane(p, Plane.WorldXY); } } mesh.Transform(xform); return mesh; }
public bool isHullVertice(int t, Polyline pl) { Grasshopper.Kernel.Geometry.Node2List list2 = new Grasshopper.Kernel.Geometry.Node2List(); List<int> hull = new List<int>(); for (int i = 0; i < pl.Count; i++) { Grasshopper.Kernel.Geometry.Node2 node = new Grasshopper.Kernel.Geometry.Node2(pl[i].X, pl[i].Y); list2.Append(node); } Grasshopper.Kernel.Geometry.ConvexHull.Solver.Compute(list2, hull); return hull.Contains(t); }
public void intersect(Plane p) { for (int i = 0; i < pts.Count; i++) { double db = p.DistanceTo(pts[i].pos); if (Math.Abs(db) < RhinoDoc.ActiveDoc.ModelAbsoluteTolerance) { pts[i].condition = 1; } else if (db > 0) { pts[i].condition = 2; } else if (db < 0) { pts[i].condition = 0; } } /////////////////////// int ii = 0; while (ii < edges.Count) { if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 0) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 1 && edges[ii].p2.condition == 0) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 1 && edges[ii].p2.condition == 1) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 1) { edges.RemoveAt(ii); } else if (edges[ii].p1.condition == 0 && edges[ii].p2.condition == 2) { double u; Line line = new Line(edges[ii].p1.pos, edges[ii].p2.pos); Rhino.Geometry.Intersect.Intersection.LinePlane(line, p, out u); pts.Add(new vertex(line.PointAt(u), this.center.DistanceTo(line.PointAt(u)))); edges[ii].p1 = pts[pts.Count - 1]; ii++; } else if (edges[ii].p1.condition == 2 && edges[ii].p2.condition == 0) { double u; Line line = new Line(edges[ii].p1.pos, edges[ii].p2.pos); Rhino.Geometry.Intersect.Intersection.LinePlane(line, p, out u); pts.Add(new vertex(line.PointAt(u), this.center.DistanceTo(line.PointAt(u)))); edges[ii].p2 = pts[pts.Count - 1]; ii++; } else { ii++; } } clearnull(); ////////////////////////////////// Transform w2p = Transform.PlaneToPlane(Plane.WorldXY, p); Transform p2w = Transform.PlaneToPlane(p, Plane.WorldXY); Grasshopper.Kernel.Geometry.Node2List ls = new Grasshopper.Kernel.Geometry.Node2List(); List<int> count = new List<int>(); for (int i = 0; i < pts.Count; i++) { if (pts[i].condition == 1 || pts[i].condition == -1) { pts[i].pos.Transform(w2p); ls.Append(new Grasshopper.Kernel.Geometry.Node2(pts[i].pos.X, pts[i].pos.Y)); pts[i].pos.Transform(p2w); count.Add(i); } } if (count.Count == 2) edges.Add(new edge(pts[count[0]], pts[count[1]])); else if (count.Count > 2) { List<int> count2 = new List<int>(); Grasshopper.Kernel.Geometry.ConvexHull.Solver.Compute(ls, count2); for (int i = 0; i < count2.Count; i++) { int c = i + 1; if (c == count2.Count) c = 0; edges.Add(new edge(pts[count[count2[i]]], pts[count[count2[c]]])); } } }