trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

Segfault on mesh boolean intersection

Open mathisTH opened this issue 6 months ago • 1 comments

Hi !

I'm facing a really strange behavior on mesh boolean intersection using manifold3D engine.

Here is my code snippet to reproduce the error its nothing really fancy, both of my meshes are volume

import trimesh

def intersect_meshes(mesh_path1, mesh_path2):

    # Load the meshes
    mesh1 = trimesh.load_mesh(mesh_path1)
    mesh2 = trimesh.load_mesh(mesh_path2)

    # Intersection: mesh1 with mesh2
    intersection_1_2 = mesh1.intersection(mesh2)
    if intersection_1_2.is_empty:
        print("Intersection of Mesh 1 with Mesh 2 is empty.")
    else:
        print(f"Intersection of Mesh 1 with Mesh 2 resulted in a mesh with {len(intersection_1_2.faces)} faces.")

    print("Intersection of Mesh 1 with Mesh 2 ok")
    
    intersection_2_1 = mesh2.intersection(mesh1)
    if intersection_2_1.is_empty:
        print("Intersection of Mesh 2 with Mesh 1 is empty.")
    else:
        print(f"Intersection of Mesh 2 with Mesh 1 resulted in a mesh with {len(intersection_2_1.faces)} faces.")

    print("Intersection of Mesh 2 with Mesh 1 ok")
            
if __name__ == '__main__':
    
    mesh1_file = 'mesh1.obj' 
    mesh2_file = 'mesh2.obj'  
    
    intersect_meshes(mesh1_file, mesh2_file)

There is two different behavior sometimes it crash Segfault and sometimes it hang infinitly. From my investigations i would say that it comes from vertices/faces order of the meshes cause for my example shuffling them sometimes fix the issue.

Here's my setup python 3.12 trimesh==4.6.8 manifold3d==3.1.0

Is this a known issue? Or is there some good practice to apply when creating meshes? Currently my meshes are the outputs of alphashape on a 3d point cloud.

meshes.zip

mathisTH avatar May 28 '25 11:05 mathisTH

Hey, did you get a traceback (python -X faulthandler script.py)? Not to punt to upstream, but this is likely to be deep inside manifold3d and they would definitely appreciate isolated failure cases.

Mesh booleans are just pretty difficult, manifold has helped a lot but it's still a very tough problem especially with not perfect inputs.

If you don't care about a perfectly matching triangulation, running marching cubes on the vertices (i.e. trimesh.voxel.ops.points_to_marching_cubes(vertices, pitch=np.ptp(vertices, axis=0).max() / 250) is pretty easy) to produce a cleaner mesh before the booleans probably helps, or you could do the booleans on voxel grids then mesh that final result with marching cubes.

mikedh avatar Jun 15 '25 16:06 mikedh