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

how to convert stl to numpy array? #116

Closed zhang-qiang-github closed 4 years ago

zhang-qiang-github commented 4 years ago

The numpy-stl is really a good project. And I have a question. If I have a stl, how can I want to convert it to a numpy array? 1 indicate that the pixel is inside the mesh, and 0 indicate that the pixel is outside the mesh. Any suggestion is helpful!

wolph commented 4 years ago

The meshes actually store all the data in a numpy array. If you have a mesh object you can access the numpy array through: mesh.data. https://numpy-stl.readthedocs.io/en/latest/stl.html#stl.base.BaseMesh It's stored using the following dtype: https://numpy-stl.readthedocs.io/en/latest/stl.html#stl.base.BaseMesh.dtype

As for the question whether a point is within the mesh... that's a surprisingly difficult question. Some meshes are not closed so they don't have a real inside. Some meshes (think of a 3D Klein bottle) only have 1 side which makes the inside the outside as well.

My recommendation would be to take all of the points and build a convex hull so you're sure that the mesh is closed. After that add the hull to a KD-tree or Octree so you can easily test to see if your point is contained within the hull.

stale[bot] commented 4 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.

yangyang117 commented 3 years ago

Hi, I want to know is there anyone get the solution to the problem. Actually I was use some mimics or 3D slicer to reconstruct 3D model, but I want to get the every layer's segment.