public static MeshGeometry Combine(params MeshGeometry[] geometries) { if (geometries.Length == 1) { return(geometries[0]); } var verts = new List <Vertex>(); var triangles = new List <Triangle>(); foreach (var geom in geometries) { verts.AddRange(geom.Vertices); triangles.AddRange(geom.Triangles); } var newGeom = new MeshGeometry(); newGeom.SetVertices(verts); newGeom.SetTriangles(triangles); newGeom.BreakReferences(); return(newGeom); }
public void SeparateDistinctSurfaces() { var uniqueEdges = Triangles.SelectMany(t => t.Edges).EqualsDistinct().ToList(); Edge.CompareByPosition = false; var edgeFaces = new Dictionary <Edge, List <Triangle> >(); foreach (var tri in Triangles) { for (int i = 0; i < 3; i++) { if (edgeFaces.TryGetValue(tri.Edges[i], out List <Triangle> faces)) { if (!faces.Contains(tri)) { faces.Add(tri); } } else { edgeFaces.Add(tri.Edges[i], new List <Triangle>() { tri }); } } } var triList = Triangles.ToList(); void AddConnectedFaces(Triangle tria, List <Triangle> remainingFaces, List <Triangle> currentFaces) { if (!currentFaces.Any()) { remainingFaces.Remove(tria); currentFaces.Add(tria); } var connectedFaces = tria.Edges.SelectMany(x => edgeFaces[x]).Where(y => y != tria); var facesToAdd = connectedFaces.Intersect(remainingFaces).ToList(); foreach (var face in facesToAdd) { remainingFaces.Remove(face); currentFaces.Add(face); } foreach (var face in facesToAdd) { AddConnectedFaces(face, remainingFaces, currentFaces); } } var geomList = new List <MeshGeometry>(); while (triList.Any()) { var curTri = triList[0]; var curTriList = new List <Triangle>(); AddConnectedFaces(curTri, triList, curTriList); var curGeom = new MeshGeometry(); curGeom.SetTriangles(curTriList, true); curGeom.BreakReferences(); geomList.Add(curGeom); } }