/// <summary> /// Builds the cell mesh in a clean way. /// This lends the cell mesh to being referenced for PolyFrame use. /// This needs the face meshes stored in the faces /// </summary> public Mesh CreateCellMesh() { // create a dict for quick find of the vertices // take the faces from each faceMesh and reconstruct them in the cell var cellMesh = new Mesh(); ArchivableDictionary vertDict = new ArchivableDictionary(); for (int i = 0; i < Vertices.Count; i++) { vertDict.Set(i.ToString(), Vertices[i].Id); } cellMesh.UserDictionary.Set("VertexIds", vertDict); cellMesh.UserDictionary.Set("Id", Id); var cellVerts = new Dictionary <int, int>(); foreach (var vert in Vertices) { cellVerts.Add(vert.Id, cellMesh.Vertices.Add(vert.Point)); // vert id : index in cell } foreach (var face in Faces) { //cellMesh.Ngons.AddNgon(new MeshNgon(new List<MeshFace>())); var mFaceList = new List <int>(); foreach (var mFace in face.FMesh.Faces) { var cellMFace = new MeshFace( cellVerts[face.Vertices[mFace.A].Id], cellVerts[face.Vertices[mFace.B].Id], cellVerts[face.Vertices[mFace.C].Id] ); mFaceList.Add(cellMesh.Faces.AddFace(cellMFace)); } cellMesh.Ngons.AddNgon(MeshNgon.Create(face.Vertices.Select(x => cellVerts[x.Id]).ToList(), mFaceList)); } cellMesh.UnifyNormals(); cellMesh.Normals.ComputeNormals(); cellMesh.Normals.UnitizeNormals(); cellMesh.Flip(true, true, true); return(cellMesh); }
protected override void SolveInstance(IGH_DataAccess DA) { Mesh m = new Mesh(); DA.GetData(0, ref m); int i = 0; DA.GetData(1, ref i); int iterations = 0; DA.GetData(2, ref iterations); if (m.IsValid) { List <List <int> > nakedVertices = m.GetNakedVerticesID(iterations); List <Polyline> nakedPolylines = m.GetNakedPolylines(nakedVertices); Mesh mesh = m.DuplicateMesh(); if (nakedVertices.Count != 0) { int check = mesh.Ngons.AddNgon(MeshNgon.Create(nakedVertices[i.Wrap(nakedVertices.Count)], Enumerable.Range(0, m.Faces.Count).ToList())); PreparePreview(mesh, DA.Iteration, nakedPolylines); DA.SetData(0, mesh); DA.SetDataTree(1, GrasshopperUtil.IE2(nakedVertices, DA.Iteration)); DA.SetDataTree(2, GrasshopperUtil.IE(nakedPolylines, DA.Iteration)); } else { DA.SetData(0, m); DA.SetDataTree(1, new Grasshopper.DataTree <int>()); DA.SetDataTree(2, new Grasshopper.DataTree <Polyline>()); } } else { DA.SetData(0, m); DA.SetDataTree(1, new Grasshopper.DataTree <int>()); DA.SetDataTree(2, new Grasshopper.DataTree <Polyline>()); } }
public static Mesh ToRhinoMeshWithNgons(this PlanktonMesh source) { // could add different options for triangulating ngons later Mesh rMesh = new Mesh(); foreach (PlanktonVertex v in source.Vertices) { rMesh.Vertices.Add(v.X, v.Y, v.Z); } for (int i = 0; i < source.Faces.Count; i++) { int[] fvs = source.Faces.GetFaceVertices(i); if (fvs.Length == 3) { rMesh.Faces.AddFace(fvs[0], fvs[1], fvs[2]); } else if (fvs.Length == 4) { rMesh.Faces.AddFace(fvs[0], fvs[1], fvs[2], fvs[3]); } else if (fvs.Length > 4) { // triangulate about face center (fan) var fc = source.Faces.GetFaceCenter(i); rMesh.Vertices.Add(fc.X, fc.Y, fc.Z); var faceIndices = new int[fvs.Length]; for (int j = 0; j < fvs.Length; j++) { faceIndices[j] = rMesh.Faces.AddFace(fvs[j], fvs[(j + 1) % fvs.Length], rMesh.Vertices.Count - 1); } rMesh.Ngons.AddNgon(MeshNgon.Create(fvs, faceIndices)); } } rMesh.Normals.ComputeNormals(); return(rMesh); }
protected override void SolveInstance(IGH_DataAccess DA) { Mesh MA = new Mesh(); Mesh MB = new Mesh(); DA.GetData(0, ref MA); DA.GetData(1, ref MB); Mesh x = new Mesh(); x.Append(MA); x.Append(MB); Mesh lattice = new Mesh(); lattice.Vertices.AddVertices(x.Vertices); var tv = x.GetNGonsTopoBoundaries(); int[][] ev = x.GetAllNGonEdges_TopoVertices(tv, x.GetAllNGonEdges(tv)); int n = (int)(ev.Length * 0.5); for (int i = 0; i < n; i++) { lattice.Faces.AddFace(ev[i][0], ev[i][1], ev[i + n][1], ev[i + n][0]); lattice.Ngons.AddNgon(MeshNgon.Create( new int[] { ev[i][0], ev[i][1], ev[i + n][1], ev[i + n][0] }, new int[] { lattice.Faces.Count - 1 } )); } // lattice.Weld(0.001); lattice.Clean(); base.PreparePreview(lattice, DA.Iteration); DA.SetData(0, lattice); DA.SetData(1, x); }
public static Mesh SubdivideSurfaceEqualDist(Surface surface, int n, int y, double distance, double lerp, ref List <Polyline> po, ref List <Polyline> po1) { Mesh mesh = new Mesh(); //Get curves for divisions (works with surface iso curves or just a list of curves) List <Curve> curves = SubdivideSurfaceIntoIsocurves(surface, n, y); //Remap curves domain for (int i = 0; i < curves.Count; i++) { curves[i].Domain = new Interval(0, 1); //curves[i].Rebuild(100, 3, false); } //Declare sortedList //List<MultiObject> sl = new List<MultiObject>(curves.Count); List <MultiObject>[] sl = new List <MultiObject> [curves.Count]; for (int i = 0; i < curves.Count; i++) { sl[i] = new List <MultiObject>(); } List <int> divisionsAll = new List <int>(); Random random = new Random(); int divisions, divisions1 = 1; try { for (int i = 0; i < curves.Count - 1; i++) { divisions = (int)(Math.Ceiling(curves[i].GetLength() / distance)); divisions1 = (int)(Math.Ceiling(curves[i + 1].GetLength() / distance)); divisions = Math.Max(divisions1, divisions); divisions = Math.Max(1, divisions); divisionsAll.Add(divisions); double[] t = curves[i].DivideByCount(divisions, false); double[] t1 = curves[i + 1].DivideByCount(divisions, false); // double[] t1 = new double[t.Length]; // for(int j = 0; j < t.Length; j++){ // double tempT = 0.001; // curves[i + 1].ClosestPoint(curves[i].PointAt(t[j]), out tempT); // Print(tempT.ToString()); // t1[j] = tempT+0.001; // } double shift = 0; double shift1 = 0; if (divisions != 1) { shift = t[0] * 0.5; shift1 = t1[0] * 0.5; } Polyline polyline = new Polyline(); Polyline polyline1 = new Polyline(); polyline.Add(curves[i].PointAtStart); polyline1.Add(curves[i + 1].PointAtStart); if (i % 2 == 0) { sl[i].Add(new MultiObject(0.0, 2)); sl[i + 1].Add(new MultiObject(0.0, 2)); for (int j = 0; j < t.Length; j++) { double r = 0; //if (sl[i].ContainsKey(t[j] - shift)) { // r = random.NextDouble() * 0.001; // // Rhino.RhinoApp.WriteLine(i.ToString()); //} sl[i].Add(new MultiObject(t[j], 0)); //if (sl[i].ContainsKey(t1[j])) { // r = random.NextDouble() * 0.001; // // Rhino.RhinoApp.WriteLine(i.ToString()); //} sl[i + 1].Add(new MultiObject(t1[j], 1)); polyline.Add(curves[i].PointAt(t[j])); polyline1.Add(curves[i + 1].PointAt(t1[j])); } sl[i].Add(new MultiObject(1.0, 2)); sl[i + 1].Add(new MultiObject(1.0, 2)); } else { if (i == curves.Count - 2) { sl[i + 1].Add(new MultiObject(0.0, 2)); } for (int j = 0; j < t.Length; j++) { double r = 0; //if (sl[i].ContainsKey(t[j] - shift)) { // r = random.NextDouble() * 0.001; // // Rhino.RhinoApp.WriteLine(i.ToString()); //} sl[i].Add(new MultiObject(t[j] - shift, 0)); //if (sl[i].ContainsKey(t1[j] - shift)) { // r = random.NextDouble() * 0.001; // // Rhino.RhinoApp.WriteLine(i.ToString()); //} sl[i + 1].Add(new MultiObject(t1[j] - shift + r, 1)); polyline.Add(curves[i].PointAt(t[j] - shift)); polyline1.Add(curves[i + 1].PointAt(t1[j] - shift)); } if (divisions != 1) { sl[i].Add(new MultiObject(t[t.Length - 1] + shift, 0)); sl[i + 1].Add(new MultiObject(t1[t1.Length - 1] + shift1, 1)); if (i == curves.Count - 2) { sl[i + 1].Add(new MultiObject(1.0, 2)); } polyline.Add(curves[i].PointAt(t[t.Length - 1] + shift)); polyline1.Add(curves[i + 1].PointAt(t1[t1.Length - 1] + shift1)); } else { if (i == curves.Count - 2) { sl[i + 1].Add(new MultiObject(1.0, 2)); } } } polyline.Add(curves[i].PointAtEnd); po.Add(polyline); polyline1.Add(curves[i + 1].PointAtEnd); po1.Add(polyline1); } //Rebuild sorted list for 0.0 0.25 1.0 to 0.0 0.5 1.0 List <double>[] sl_ = new List <double> [curves.Count]; for (int i = 0; i < sl.Length; i++) { sl[i] = sl[i].OrderBy(t => t.curveParameter).ToList(); sl_[i] = MathUtil.Range(0, 1, sl[i].Count).ToList(); } //Create Mesh List <double> key = new List <double>(); //Add Vertices for (int i = 0; i < curves.Count; i++) { for (int j = 0; j < sl_[i].Count; j++) { mesh.Vertices.Add(curves[i].PointAt(NGonsCore.MathUtil.Lerp(sl[i][j].curveParameter, (float)sl_[i][j], (float)Math.Max(0, Math.Min(0.0001 + lerp, 1))))); } } //Add Faces List <Polyline> allP = new List <Polyline>(); List <Polyline> allP1 = new List <Polyline>(); List <List <int> > allID0 = new List <List <int> >(); List <List <int> > allID1 = new List <List <int> >(); int vc = 0; int vc1 = sl[0].Count; for (int i = 0; i < curves.Count - 1; i++) { Polyline p = new Polyline(); Polyline p1 = new Polyline(); var id0 = new List <int>(); var id1 = new List <int>(); int pCount = sl[i].Count; if (i % 2 == 0) { int allPCount = allP.Count; //Solve end parts that are very small if (divisionsAll[i] == 1) { // Rhino.RhinoApp.WriteLine(i.ToString() + " 0 " + pCount.ToString()); for (int j = 0; j < pCount; j++) { p.Add(mesh.Vertices[vc]); id0.Add(vc); vc++; } allP.Add(p); allID0.Add(id0); allP[allP.Count - 1].Reverse(); allID0[allP.Count - 1].Reverse(); for (int j = 0; j < sl[i + 1].Count; j++) { allP[allPCount].Add(mesh.Vertices[vc]); allID0[allPCount].Add(vc); vc++; } } else { for (int j = 0; j < pCount; j++) { int id = sl[i][j].type; p.Add(mesh.Vertices[vc]); id0.Add(vc); if (id != 2 && id != 1) { allP.Add(p); allID0.Add(id0); if (p.Count != 0) { allP[allP.Count - 1].Reverse(); allID0[allP.Count - 1].Reverse(); p = new Polyline(new Point3d[] { mesh.Vertices[vc] }); id0 = new List <int>() { vc }; } //if } //if vc++; }// for j allP.Add(p); allID0.Add(id0); allP[allP.Count - 1].Reverse(); allID0[allP.Count - 1].Reverse(); for (int j = 0; j < sl[i + 1].Count; j++) //sl[i+1].Count { int id = sl[i + 1][j].type; allP[allPCount].Add(mesh.Vertices[vc]); allID0[allPCount].Add(vc); if (id != 2 && id != 0 && p.Count != 0) { allP[allPCount + 1].Add(mesh.Vertices[vc]); allID0[allPCount + 1].Add(vc); allPCount++; }//if vc++; } // for j } //if not very short segments } else //if mod { int allPCount1 = allP1.Count; if (divisionsAll[i] == 1) { // Rhino.RhinoApp.WriteLine("1 " + pCount.ToString()); for (int j = 0; j < pCount; j++) { p1.Add(mesh.Vertices[vc1]); id1.Add(vc1); vc1++; } allP1.Add(p1); allID1.Add(id1); allP1[allP1.Count - 1].Reverse(); allID1[allP1.Count - 1].Reverse(); for (int j = 0; j < sl[i + 1].Count; j++) { allP1[allPCount1].Add(mesh.Vertices[vc1]); allID1[allPCount1].Add(vc1); vc1++; } } else { for (int j = 0; j < pCount; j++) { int id = sl[i][j].type; p1.Add(mesh.Vertices[vc1]); id1.Add(vc1); if (id != 2 && id != 1) { allP1.Add(p1); allID1.Add(id1); if (p1.Count != 0) { allP1[allP1.Count - 1].Reverse(); allID1[allP1.Count - 1].Reverse(); p1 = new Polyline(new Point3d[] { mesh.Vertices[vc1] }); id1 = new List <int>() { vc1 }; } } vc1++; }// for j allP1.Add(p1); allID1.Add(id1); allP1[allP1.Count - 1].Reverse(); allID1[allP1.Count - 1].Reverse(); for (int j = 0; j < sl[i + 1].Count; j++) //sl[i+1].Count { int id = sl[i + 1][j].type; allP1[allPCount1].Add(mesh.Vertices[vc1]); allID1[allPCount1].Add(vc1); if (id != 2 && id != 0 && p1.Count != 0) { allP1[allPCount1 + 1].Add(mesh.Vertices[vc1]); allID1[allPCount1 + 1].Add(vc1); allPCount1++; } vc1++; } // for j } } //mod } // for i int nf = 0; for (int i = 0; i < allP.Count; i++) { allP[i].Close(); MeshFace[] faces = allP[i].TriangulateClosedPolyline(); if (faces != null) { int fl = faces.Length; for (int j = 0; j < fl; j++) { mesh.Faces.AddFace(allID0[i][faces[j].A], allID0[i][faces[j].B], allID0[i][faces[j].C]); } mesh.Ngons.AddNgon(MeshNgon.Create(allID0[i], Enumerable.Range(nf, fl).ToList())); nf += fl; } } for (int i = 0; i < allP1.Count; i++) { allP1[i].Close(); MeshFace[] faces = allP1[i].TriangulateClosedPolyline(); if (faces != null) { int fl = faces.Length; for (int j = 0; j < fl; j++) { mesh.Faces.AddFace(allID1[i][faces[j].A], allID1[i][faces[j].B], allID1[i][faces[j].C]); } mesh.Ngons.AddNgon(MeshNgon.Create(allID1[i], Enumerable.Range(nf, fl).ToList())); nf += fl; } } // List<double> key = new List<double>(); List <double> val = new List <double>(); foreach (MultiObject mo in sl[curves.Count - 1]) { key.Add(mo.curveParameter); val.Add(mo.type); } } catch (Exception e) { Rhino.RhinoApp.WriteLine(e.ToString()); } mesh.WeldUsingRTree(0.0001); return(mesh); }
public static Mesh QuadMeshDistanceFromBrep(Surface surface, int num, int num2, Brep target, double min, double max, double shift, ref double[] distances) { Mesh mesh = new Mesh(); surface.SetDomain(0, new Interval(0.0, 1.0)); surface.SetDomain(1, new Interval(0.0, 1.0)); double num5 = 1.0 / (double)num; double num6 = 1.0 / (double)num2; int v = 0; int f = 0; for (int i = 0; i < num; i++) { for (int j = 0; j < num2; j++) { Point3d pa = surface.PointAt((double)i * num5, (double)j * num6); Point3d pb = surface.PointAt((double)(i + 1) * num5, (double)j * num6); Point3d pc = surface.PointAt((double)(i + 1) * num5, (double)(j + 1) * num6); Point3d pd = surface.PointAt((double)i * num5, (double)(j + 1) * num6); mesh.Vertices.Add(pa); mesh.Vertices.Add(pb); mesh.Vertices.Add(pc); mesh.Vertices.Add(pd); mesh.Faces.AddFace(v + 2, v + 1, v + 0); mesh.Faces.AddFace(v + 3, v + 2, v + 0); MeshNgon ngon = MeshNgon.Create(new[] { v + 0, v + 1, v + 2, v + 3 }, new[] { f + 0, f + 1 }); mesh.Ngons.AddNgon(ngon); v += 4; f += 2; } } mesh.Vertices.CombineIdentical(true, true); mesh.FaceNormals.ComputeFaceNormals(); mesh.Normals.ComputeNormals(); mesh.Compact(); //Assign colors distances = new double[mesh.Vertices.Count]; for (int i = 0; i < mesh.Vertices.Count; i++) { double dist = target.ClosestPoint(mesh.Vertices[i]).DistanceTo(mesh.Vertices[i]); dist -= shift; distances[i] = dist; dist = MathUtil.Constrain(dist, min, max); int temp = MathUtil.MapInt(dist, min, max, 0, 255); //mesh.VertexColors.Add( Color.FromArgb(255, 255-temp,temp)); mesh.VertexColors.Add(Color.FromArgb(255 - temp, 255 - temp, 255 - temp)); //mesh.VertexColors.Add(Color.FromArgb(0,rAverage, gAverage, bAverage)); } return(mesh); }
public Mesh FromPolylines(IEnumerable <Polyline> nurbsCurves, double weld) { //Create Mesh and get face Mesh mesh = new Mesh(); int v = 0; int f = 0; foreach (Polyline polyline in nurbsCurves) { switch (polyline.Count - 1) { case (6): polyline.RemoveAt(polyline.Count - 1); mesh.Vertices.AddVertices(polyline); mesh.Faces.AddFace(v + 2, v + 1, v + 0); mesh.Faces.AddFace(v + 4, v + 3, v + 2); mesh.Faces.AddFace(v + 5, v + 2, v + 0); mesh.Faces.AddFace(v + 4, v + 2, v + 5); mesh.Ngons.AddNgon(MeshNgon.Create(new[] { v, v + 1, v + 2, v + 3, v + 4, v + 5 }, new[] { f + 0, f + 1, f + 2, f + 3 })); v += 6; f += 4; break; case (4): polyline.RemoveAt(polyline.Count - 1); mesh.Vertices.AddVertices(polyline); mesh.Faces.AddFace(2 + v, 1 + v, 0 + v); mesh.Faces.AddFace(3 + v, 2 + v, 0 + v); mesh.Ngons.AddNgon(MeshNgon.Create(new[] { v, 1 + v, 2 + v, 3 + v }, new[] { 0 + f, 1 + f })); v += 4; f += 2; break; case (3): polyline.RemoveAt(polyline.Count - 1); mesh.Vertices.AddVertices(polyline); mesh.Faces.AddFace(2 + v, 1 + v, 0 + v); mesh.Ngons.AddNgon(MeshNgon.Create(new[] { v, 1 + v, 2 + v }, new[] { 0 })); v += 3; f += 1; break; default: Mesh temp = Mesh.CreateFromClosedPolyline(polyline); int[] tempV = new int[temp.Vertices.Count]; for (int i = 0; i < temp.Vertices.Count; i++) { tempV[i] = v + i; } int[] tempF = new int[temp.Faces.Count]; for (int i = 0; i < temp.Faces.Count; i++) { tempF[i] = f + i; } mesh.Append(temp); mesh.Ngons.AddNgon(MeshNgon.Create(tempV, tempF)); v += temp.Vertices.Count; f += temp.Faces.Count; break; } } mesh.FaceNormals.ComputeFaceNormals(); mesh.Normals.ComputeNormals(); mesh.Compact(); if (weld > 0) { mesh.WeldUsingRTree(0.001); mesh.Vertices.CombineIdentical(true, true); mesh.Vertices.CullUnused(); mesh.Weld(3.14159265358979); mesh.UnifyNormals(); mesh.FaceNormals.ComputeFaceNormals(); mesh.Normals.ComputeNormals(); mesh.Compact(); } return(mesh); }
/// <summary> /// creates a mesh patch corresponding to the cell /// also sets the area of the polygon /// </summary> public void FaceMesh() { // pick one vertex and create triangles with all the other pairs double area = 0; Mesh faceMesh = new Mesh(); faceMesh.Vertices.AddVertices(Vertices.Select(x => x.Point)); ArchivableDictionary vertDict = new ArchivableDictionary(); for (int i = 0; i < Vertices.Count; i++) { vertDict.Set(i.ToString(), Vertices[i].Id); } faceMesh.UserDictionary.Set("VertexIds", vertDict); faceMesh.UserDictionary.Set("Id", Id); //this.FMesh.Vertices.Add(Centroid); for (int v = 1; v < faceMesh.Vertices.Count - 1; v++) { // create faces.. faceMesh.Faces.AddFace(0, v, v + 1); Vector3d v1 = Vertices[v].Point - Vertices[0].Point; Vector3d v2 = Vertices[v + 1].Point - Vertices[0].Point; double faceArea = System.Math.Abs(Vector3d.CrossProduct(v1, v2).Length / 2); area = area + faceArea; } if (Vertices.Count > 3) { faceMesh.Ngons.AddNgon(MeshNgon.Create (Enumerable.Range(0, Vertices.Count).ToList(), Enumerable.Range(0, faceMesh.Faces.Count).ToList())); } FMesh = faceMesh.DuplicateMesh(); //FMesh.Normals.ComputeNormals(); //FMesh.FaceNormals.ComputeFaceNormals(); /* * for (int n=0; n<FMesh.Vertices.Count; n++) * { * FMesh.Normals.SetNormal(n, Normal); * } * for (int fn = 0; fn < FMesh.FaceNormals.Count; fn++) * { * FMesh.FaceNormals.SetFaceNormal(fn, Normal); * } */ // check face normal against the PFFace normal and reverse if necessary /* * for (int i=0; i<FMesh.FaceNormals.Count; i++) * { * if ((FMesh.FaceNormals[i] - Normal).Length > FMesh.FaceNormals[i].Length) * { * Vector3f reverse = FMesh.FaceNormals[i]; * reverse.Reverse(); * FMesh.FaceNormals.SetFaceNormal(i, reverse); * * } * } * if ((FMesh.FaceNormals[0] - Normal).Length > FMesh.FaceNormals[0].Length) * { * FMesh.Flip(true, true, true); * * } */ Area = area; }
public static double Circumference(this Mesh mesh, MeshNgon nGon) { return(mesh.NgonBoundary(nGon).Length); }
public static Polyline NgonBoundary(this Mesh mesh, MeshNgon nGon) { return(new Polyline(from index in nGon.BoundaryVertexIndexList() select new Point3d(mesh.Vertices[Convert.ToInt32(index)]))); }
public static Mesh ColorPolyhedron(this Mesh mesh) { //TODO: There seems to be no way to keep nGon Information while coloring a mesh. // get ngon face indices to reconstruct from later var ngonIndices = from ngon in mesh.Ngons select(from index in ngon.FaceIndexList() select Convert.ToInt32(index)); int nGonCount = mesh.Ngons.Count; // unweld all faces mesh.Unweld(0, true); // get tolerance var tol = RhinoDoc.ActiveDoc.ModelAbsoluteTolerance; Dictionary <double, List <int[]> > lengthIndicesDict = new Dictionary <double, List <int[]> >(); // iterate over faces and group by area foreach (var nGon in mesh.GetNgonAndFacesEnumerable()) { var indices = (from index in nGon.BoundaryVertexIndexList() select Convert.ToInt32(index)).ToArray(); var vertices = from index in indices select new Point3d(mesh.Vertices[Convert.ToInt32(index)]); var poly = new Polyline(vertices); var length = Math.Round(poly.Length, (1 / tol).ToString().Length); if (lengthIndicesDict.ContainsKey(length)) { lengthIndicesDict[length].Add(indices); } else { lengthIndicesDict[length] = new List <int[]> { indices }; } } // create color range var colorRange = ColorRange(lengthIndicesDict.Count).ToArray(); // empty array to hold all vertex colors var vertexColors = new Color[mesh.Vertices.Count]; // extracted keys for easier reference by index var keys = lengthIndicesDict.Keys.ToArray(); // iterate over all keys for (int i = 0; i < lengthIndicesDict.Count; i++) { // current color by index of key var curColor = colorRange[i]; // iterate over face-vertex lists in key foreach (var vIndexList in lengthIndicesDict[keys[i]]) { foreach (var index in vIndexList) { // set vertex color by index stored in face-vertex list vertexColors[index] = curColor; } } } // set all colors at once mesh.VertexColors.SetColors(vertexColors); // re-set nGons if (false) { List <MeshNgon> nGons = new List <MeshNgon>(); foreach (var ngonIndex in ngonIndices) { int[] faceIndices = ngonIndex.ToArray(); int[] vertexIndices = new int[faceIndices.Length * 4]; for (int i = 0; i < faceIndices.Length; i++) { vertexIndices[i * 4 + 0] = mesh.Faces[faceIndices[i]].A; vertexIndices[i * 4 + 1] = mesh.Faces[faceIndices[i]].B; vertexIndices[i * 4 + 2] = mesh.Faces[faceIndices[i]].C; vertexIndices[i * 4 + 3] = mesh.Faces[faceIndices[i]].D; } nGons.Add(MeshNgon.Create(vertexIndices, faceIndices)); } mesh.Ngons.AddNgons(nGons); } // return return(mesh); }
public static Mesh[] ProjectPairsMeshToTarget(Mesh mesh, double value, Mesh target) { int n = (int)(mesh.Ngons.Count * 0.5); Polyline[][] p = new Polyline[n][]; Plane[] pl = mesh.GetNgonPlanes(); Mesh[] m = new Mesh[n]; uint[][] id = mesh.GetNGonsBoundaries(); for (int i = 0; i < n; i++) { p[i] = new Polyline[] { new Polyline(), new Polyline() }; //Vector3d vec = Vector3d.ZAxis; for (int j = 0; j < id[i].Length; j++) { Line temp = new Line(mesh.Vertices[(int)id[i][j]], mesh.Vertices[(int)id[i + n][j]]); temp.Transform(Transform.Scale(temp.PointAt(0.5), 100000)); Rhino.Geometry.Intersect.Intersection.LinePlane(temp, pl[i], out double t1); Rhino.Geometry.Intersect.Intersection.LinePlane(temp, pl[i + n], out double t2); Point3d[] pt = Rhino.Geometry.Intersect.Intersection.MeshLine(target, temp, out int[] faceIds); Point3d ptA = pt[0]; Point3d tempPt = mesh.ClosestPoint(pt[0]); double distance = tempPt.DistanceTo(pt[0]); if (pt.Length > 1) { for (int k = 1; k < pt.Length; k++) { //Measure distance between shell mesh and intersection point Point3d tempPt2 = mesh.ClosestPoint(pt[k]); double dist = tempPt2.DistanceTo(pt[k]); if (dist < distance) { ptA = pt[k]; distance = dist; } } } double t3 = temp.ClosestParameter(ptA); Point3d a = temp.PointAt(t2); Point3d b = temp.PointAt(MathUtil.Lerp(t1, t3, value)); p[i][0].Add(a); p[i][1].Add(b); //Vector3d tempVec = Vector3d.Subtract((Vector3d)a, (Vector3d)b); //tempVec.Unitize(); //vec += tempVec; } //vec.Unitize(); //Point3d origin = Point3d.Origin; //origin += p[i][1][0]; //origin += p[i][1][1]; //origin += p[i][1][3]; //origin += p[i][1][4]; //if (origin != Point3d.Origin) // origin /= 4; //Point3d cp = target.ClosestPoint(origin); //vec = Vector3d.Subtract((Vector3d)origin, (Vector3d)cp); //pl[i] = new Plane(p[i][1].CenterPoint(), vec);//p[i][1].plane(); //pl[i] = new Plane(origin, vec);//p[i][1].plane(); //for (int j = 0; j < id[i].Length; j++) { // Line temp = new Line(p[i][0][j], p[i][1][j]); // Rhino.Geometry.Intersect.Intersection.LinePlane(temp, pl[i], out double t11); // p[i][1][j] = temp.PointAt(t11); //} m[i] = new Mesh(); m[i].Vertices.AddVertices(p[i][0].ToArray()); m[i].Vertices.AddVertices(p[i][1].ToArray()); m[i].Faces.AddFace(2, 1, 0); m[i].Faces.AddFace(4, 3, 2); m[i].Faces.AddFace(5, 2, 0); m[i].Faces.AddFace(4, 2, 5); m[i].Ngons.AddNgon(MeshNgon.Create(new[] { 0, 1, 2, 3, 4, 5 }, new[] { 0, 1, 2, 3 })); m[i].Faces.AddFace(2 + 6, 1 + 6, 0 + 6); m[i].Faces.AddFace(4 + 6, 3 + 6, 2 + 6); m[i].Faces.AddFace(5 + 6, 2 + 6, 0 + 6); m[i].Faces.AddFace(4 + 6, 2 + 6, 5 + 6); m[i].Ngons.AddNgon(MeshNgon.Create(new[] { 0 + 6, 1 + 6, 2 + 6, 3 + 6, 4 + 6, 5 + 6 }, new[] { 4, 5, 6, 7 })); for (int j = 0; j < p[i][0].Count - 1; j++) { m[i].Faces.AddFace(j, j + 1, p[i][0].Count + j + 1, p[i][0].Count + j); } m[i].Faces.AddFace(p[i][0].Count - 1, 0, p[i][0].Count, p[i][0].Count * 2 - 1); } return(m); }