mikedh / trimesh

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

mesh.section_multiplane() is slow #1481

Open venugovh opened 2 years ago

venugovh commented 2 years ago

Hi @mikedh,

You recommended me to benchmark trimesh slicing with cura in issue #1347

I tried benchmarking trimesh slicing against Cura's slicer. Cura's slicer is considerably faster than trimesh. Since it is mentioned on the trimesh homepage that Cura uses trimesh, is it possible for me to call Cura's slicing function within my python script?

As for trimesh's slicing, I am using the section_multiplane function. I noticed that the intersections.mesh_multiplane is fast even for very complex geometries. The problem arises in load_path where it converts the lines into Path2D objects. It takes lot of time to convert complex slices to Path objects. Do you think there is a way to speed this up? Or am I missing something here?

Any help will be really appreciated! Thank you!

mikedh commented 2 years ago

Hey I'd try calling trimesh.intersections.mesh_multiplane(mesh, ...) directly (rather than calling mesh.section_multiplane(...)) as it will return the result as numpy arrays which you can handle however you want without creating the Path2D (which can definitely be quite slow as it's creating connected regions): https://github.com/mikedh/trimesh/blob/main/trimesh/intersections.py#L211

Not sure how to call Cura's slicer from Python, though you might be able to tweak the build of CuraEngine to return just the section without the other stuff.

venugovh commented 2 years ago

Oh! My situation is a bit tricky because I am using the polygons_closed info in Path2D for each slice. Is there a way to achieve the same without taking this expensive route?

mikedh commented 2 years ago

You might be able to use shapely.ops.polygonize on the line segments though iirc that didn't benchmark all that much faster than the trimesh implementation. What's the benchmark look like with just accessing path.paths rather than path.polygons_closed? That would be doing quite a bit less work and might be faster.

venugovh commented 2 years ago

You are correct, the shapely.ops.polygonize did not perform well compared to the trimesh implementation, it even failed to process some slice polygons for that matter!

Could you please elaborate on using path.paths instead of path.polygons_closed? I use the polygons_closed to get a list of interior and exterior polygons on a slice which I then need to work with for other calculations.

Thanks!