Search code examples
python-3.xtrimesh

trimesh.section face_index doesn't map to the returned Path3D?


I noticed that the trimesh.section method returns a Path3D with the indices of the intersected faces stuck into the path's metadata['face_index']. I also noticed that the number of face indices corresponds to the number of the path's entities' nodes:

paths = inner.section(plane_origin=origin, plane_normal=norm)
assert(len([node for path in paths.entities for node in path.nodes]) == len(paths.metadata['face_index']))

However, the face_index seems to be out-of-order with regard to the path's entities' nodes. Given a specific path entity, how can i find the faces on the mesh that the path entity lays upon?


Solution

  • I was able to find the faces to remove with a face_adjacency graph, but it seems like there must be a better way to do this, since the work has essentially already been done in the call to section:

    paths = inner.section(plane_origin=origin, plane_normal=norm)
    # find the closed path entity with a centroid nearest to the plane origin
    nearest, idx = _find_nearest_closed_path(origin, paths)
    face_adjacency = trimesh.graph.face_adjacency(inner.faces[paths.metadata['face_index']])
    graph = nx.Graph()
    graph.add_edges_from(face_adjacency)
    ccs = list(nx.connected_components(graph))
    nearest, idx = _find_nearest_closed_path(origin, paths)
    # making a big assumption that the connected_components are in the same order as the Path3D entities...
    remove_faces =  paths.metadata['face_index'][np.asarray(list(ccs[idx]))]
    mask = np.full(inner.faces.shape[0], True, dtype=bool)
    mask[remove_faces] = False
    # update the mesh
    inner.update_faces(mask)