public static IMesh Triangulate2DElements(IMesh mesh) { IMesh triangulated = new IMesh(); foreach (ITopologicVertex v in mesh.Vertices) { triangulated.AddVertex(v.Key, new ITopologicVertex(v)); } ISurfaceElement face; int key = mesh.FindNextVertexKey(); foreach (IElement e in mesh.Elements) { if (e.TopologicDimension == 2) { if (e.VerticesCount == 3) { face = new ISurfaceElement(e.Vertices[0], e.Vertices[1], e.Vertices[2]); triangulated.AddElement(face); } else if (e.VerticesCount == 4) { face = new ISurfaceElement(e.Vertices[0], e.Vertices[1], e.Vertices[3]); triangulated.AddElement(face); face = new ISurfaceElement(e.Vertices[3], e.Vertices[1], e.Vertices[2]); triangulated.AddElement(face); } else { IPoint3D pos = ISubdividor.ComputeAveragePosition(e.Vertices, mesh); ITopologicVertex v = new ITopologicVertex(pos.X, pos.Y, pos.Z, key); triangulated.AddVertex(key, v); for (int i = 1; i <= e.HalfFacetsCount; i++) { int[] hf; e.GetHalfFacet(i, out hf); face = new ISurfaceElement(hf[0], hf[1], key); triangulated.AddElement(face); } key++; } } } triangulated.BuildTopology(); return(triangulated); }
public static IMesh DualMesh(IMesh mesh) { IMesh nM = new IMesh(); // Face center points foreach (IElement e in mesh.Elements) { nM.AddVertex(e.Key, new ITopologicVertex(ISubdividor.ComputeAveragePosition(e.Vertices, mesh))); } int[] data1, data2; PointCloud cloud = new PointCloud(); List <int> global = new List <int>(); int vertexKey = nM.FindNextVertexKey(); foreach (ITopologicVertex v in mesh.Vertices) { data1 = mesh.Topology.GetVertexIncidentElementsSorted(v.Key); if (!mesh.Topology.IsNakedVertex(v.Key)) { nM.AddElement(new ISurfaceElement(data1)); } else { List <int> local = new List <int>(); data2 = mesh.Topology.GetVertexAdjacentVerticesSorted(v.Key); bool flag = false; foreach (int vv in data2) { if (mesh.Topology.IsNakedEdge(vv, v.Key)) { Point3d p = ISubdividor.ComputeAveragePoint(new[] { vv, v.Key }, mesh); int idx = cloud.ClosestPoint(p); if (idx == -1) { flag = true; } else { if (p.DistanceTo(cloud[idx].Location) > 0.01) { flag = true; } else { flag = false; } } if (flag) { cloud.Add(p); nM.AddVertex(vertexKey, new ITopologicVertex(p)); global.Add(vertexKey); local.Add(vertexKey); vertexKey++; } else { local.Add(global[idx]); } } } nM.AddVertex(vertexKey, new ITopologicVertex(v)); local.Insert(1, vertexKey); local.AddRange(data1.Reverse()); vertexKey++; nM.AddElement(new ISurfaceElement(local.ToArray())); } } nM.BuildTopology(true); return(nM); }
public static Mesh TryGetRhinoMesh(IMesh iM, bool trianglesOnly = false) { Mesh rM = new Mesh(); Dictionary <int, int> maps = new Dictionary <int, int>(); int idx = 0; foreach (ITopologicVertex v in iM.Vertices) { rM.Vertices.Add(v.RhinoPoint); maps.Add(v.Key, idx); idx++; } int vKey = iM.FindNextVertexKey(); int[] hf; foreach (IElement e in iM.Elements) { if (e.TopologicDimension == 1) { rM.Faces.AddFace(new MeshFace(maps[e.Vertices[0]], maps[e.Vertices[1]], maps[e.Vertices[0]])); } else if (e.TopologicDimension == 2) { if (e.VerticesCount == 3) { rM.Faces.AddFace(new MeshFace(maps[e.Vertices[0]], maps[e.Vertices[1]], maps[e.Vertices[2]])); } else if (e.VerticesCount == 4) { if (trianglesOnly) { rM.Faces.AddFace(new MeshFace(maps[e.Vertices[0]], maps[e.Vertices[1]], maps[e.Vertices[3]])); rM.Faces.AddFace(new MeshFace(maps[e.Vertices[3]], maps[e.Vertices[1]], maps[e.Vertices[2]])); } else { rM.Faces.AddFace(new MeshFace(maps[e.Vertices[0]], maps[e.Vertices[1]], maps[e.Vertices[2]], maps[e.Vertices[3]])); } } else { IPoint3D pos = ISubdividor.ComputeAveragePosition(e.Vertices, iM); rM.Vertices.Add(new Point3d(pos.X, pos.Y, pos.Z)); maps.Add(vKey, idx); for (int i = 1; i <= e.HalfFacetsCount; i++) { e.GetHalfFacet(i, out hf); rM.Faces.AddFace(new MeshFace(maps[hf[0]], maps[hf[1]], maps[vKey])); } vKey++; idx++; } } else if (e.TopologicDimension == 3) { if (!iM.IsMultidimensionalMesh) { for (int i = 1; i <= e.HalfFacetsCount; i++) { if (e.IsNakedSiblingHalfFacet(i)) { e.GetHalfFacet(i, out hf); if (hf.Length == 3) { rM.Faces.AddFace(new MeshFace(maps[hf[0]], maps[hf[1]], maps[hf[2]])); } else { rM.Faces.AddFace(new MeshFace(maps[hf[0]], maps[hf[1]], maps[hf[2]], maps[hf[3]])); } } } } else { if (e.IsBoundaryElement()) { for (int i = 1; i <= e.HalfFacetsCount; i++) { e.GetHalfFacet(i, out hf); if (hf.Length == 3) { rM.Faces.AddFace(new MeshFace(maps[hf[0]], maps[hf[1]], maps[hf[2]])); } else { rM.Faces.AddFace(new MeshFace(maps[hf[0]], maps[hf[1]], maps[hf[2]], maps[hf[3]])); } } } } } } rM.UnifyNormals(); return(rM); }