mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3k stars 581 forks source link

trimesh.section face indices? #1022

Open DustinReagan opened 4 years ago

DustinReagan commented 4 years ago

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 lay upon?

For some context: I'm trying to remove the faces of the mesh that a specific closed path entity lies on:

# section the mesh
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)
# find that entity's offset into the path's face_index
offset = len([node for path in paths.entities[:idx] for node in path.nodes])
# get the face indices for that path entity
remove_faces =  paths.metadata['face_index'][offset:offset+len(nearest.nodes)]
# make a mask to update the mesh's faces with
mask = np.full(inner.faces.shape[0], True, dtype=bool)
mask[remove_faces] = False
# update the mesh
inner.update_faces(mask)

This results in a mesh with faces missing seemingly at random along the Path3D, rather than in a loop where the specified path entity lies.

DustinReagan commented 4 years ago

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:

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)
mikedh commented 4 years ago

Ah yeah that's a bug. The face_index values won't necessarily correspond after load_path as you point out:

        # return a single cross section in 3D                                                        
        lines, face_index = intersections.mesh_plane(
            mesh=self,
            plane_normal=plane_normal,
            plane_origin=plane_origin,
            return_faces=True,
            **kwargs)

        # if the section didn't hit the mesh return None                                             
        if len(lines) == 0:
            return None

        # otherwise load the line segments into a Path3D object                                      
        path = load_path(lines)

        # LOAD_PATH SCREWED UP THE ORDER SO THIS IS WRONG                                  
        path.metadata['face_index'] = face_index

In the mean time I would call trimesh.intersections.mesh_plane directly as the indexes are almost certainly correct there:

        # return a single cross section in 3D                                                        
        lines, face_index = trimesh.intersections.mesh_plane(
            mesh=self,
            plane_normal=plane_normal,
            plane_origin=plane_origin,
            return_faces=True)