wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

Add boolean operations #91

Closed devonjones closed 5 years ago

devonjones commented 5 years ago

I would love it if this library had functions for doing Union/Intersection/Difference. As it is, with just a few new functions, this in jupyter could replace many of the use cases for openscad

wolph commented 5 years ago

It depends on what the methods would have to do. I could simply pass the operation onto numpy but I'm not sure if that would give any usable result.

Uvar commented 5 years ago

What would determine the set operations on 2 meshes? Imagine a rotated version of the same object. Now we have 0 overlap in spatial coordinates of the vertices, but a fully similar object; so clearly spatial coordinates can't be used out-of-the-box.

I can think of similar arguments for area comparisons, volume comparisons, shape comparisons.

As a proper solution to anything doing close to the desirable behaviour would be rather complicated, it's impossible to implement without having a formally defined description of what the functions should do exactly

devonjones commented 5 years ago

What would determine the set operations on 2 meshes? Imagine a rotated version of the same object. Now we have 0 overlap in spatial coordinates of the vertices, but a fully similar object; so clearly spatial coordinates can't be used out-of-the-box.

I can think of similar arguments for area comparisons, volume comparisons, shape comparisons.

As a proper solution to anything doing close to the desirable behaviour would be rather complicated, it's impossible to implement without having a formally defined description of what the functions should do exactly

So what I'm thinking here is in line with what Blender or OpenSCAD would consider a boolean. Both offer Union, Difference and Intersection. If you have a manifold mesh, the operations would be:

Union: the area contained inside the two meshes would now be contained inside the unioned mesh. If the two overlap, edges (and vertices) should be created where the two meshes meet, and edges/vertices/faces in the interior of the mesh should be eliminated.

Difference: the volume of mesh two should be removed from the volume of mesh one

Intersection: the resulting mesh should contain only the area that is inside both meshes.

For your example, a mesh can still be manifold even if it has two separate but distinct interior volumes. The new mesh of two non overlapping meshes that are unioned would just be a mesh that has all verts, edges and faces that are in both.

Again I would point you to almost any 3d cad package (Blender, OpenSCAD, Maya, etc) and test a boolean inside those and it should clarify what I'm asking for.

wolph commented 5 years ago

I'm not sure if this would be a good idea for this library to be honest. It sounds like it would be fairly complicated code which is a bit outside of the scope of this project.

This project is basically just meant for loading and saving a mesh to the STL format. While there are a few basic modifications possible, I think if you need more complicated alterations you would do better with an actual 3d manipulation library or perhaps something like scipy.

While I wouldn't object to a pull request, I'm a bit hesitant of getting it in here because I'm not at all certain I would be able to properly maintain it. Without research I certainly wouldn't know where to start writing an efficient algorithm for these type of manipulations.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.