marian42 / mesh_to_sdf

Calculate signed distance fields for arbitrary meshes
https://pypi.org/project/mesh-to-sdf/
MIT License
991 stars 107 forks source link

Out of memory crash on loading mesh, fixable with small change #42

Open thegnarwhals opened 1 year ago

thegnarwhals commented 1 year ago

0a6acbe0724b4fd0901b5a42c19c8952.zip

This is a zipped .glb scene containing a car. Unzipping and running the following script

import numpy as np
import trimesh
from mesh_to_sdf import mesh_to_sdf

model_path = "0a6acbe0724b4fd0901b5a42c19c8952.glb"
mesh = trimesh.load(model_path)
sample_points = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]])
sdf = mesh_to_sdf(mesh, sample_points, surface_point_method="scan", sign_method="depth")
print(sdf)

gives

Killed (out of memory)

Having debugged, this has been narrowed down to the line near the top of __init__.py:

        mesh = mesh.dump().sum()

which calls sum() on a numpy array of Trimesh. This apparently does some parallelisation which is too memory intensive for this geometry. If we do

        mesh = mesh.dump(concatenate=True)

which uses trimesh's inbuilt mesh concatenation, the script completes with expected output

[-0.4379601  0.8417097  2.3150892]

I will open a pull request with this change.

xiexh20 commented 1 year ago

I had the same issue, I tried your fix but it still raised OOM error (killed), the mesh I use is from shapenet: ShapeNetCore.v2/04256520/ccb08f869edde5e6d8adf1ca87495d41