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
605 stars 103 forks source link

vertices and faces from STL #189

Closed Ccaccia73 closed 2 years ago

Ccaccia73 commented 2 years ago

Hi, I'm wondering if it is already possible to do the opposite of this: stl from vertices and faces because I would need it for my project but I can't find a method to retrieve the unique points of an STL file and which of them belong to a face. Thank you

wolph commented 2 years ago

That's part of the internal structure actually.

mesh.points will give you all unique points and mesh.vectors gives you the faces. Additionally the x, y and z points can be fetched separately as well: https://numpy-stl.readthedocs.io/en/latest/stl.html#stl.base.BaseMesh

Ccaccia73 commented 2 years ago

Hi, thank you for the explanation. I might be missing something. I generated the cube.stl file in the example above and if I read it back with

from stl import mesh
m1 = mesh.Mesh.from_file('cube.stl')

if I read

m1.points
m1.vectors

the first gives me back a 12x9 array of [p0, p1, p2] of all the triangles and not the unique points, while the second a 12x3x3 array with all the points coordinates. I am using the mesh module, you are linking the base module, can I read an existing stl file and retrieve unique points and "connectivity matrix"?

EDIT: I also tried:

m2 = base.BaseMesh(m1.data)

obtaining the same results. I'd like to have something like:

do I need to implement it? Thank you

wolph commented 2 years ago

the first gives me back a 12x9 array of [p0, p1, p2] of all the triangles and not the unique points, while the second a 12x3x3 array with all the points coordinates. I am using the mesh module, you are linking the base module, can I read an existing stl file and retrieve unique points and "connectivity matrix"?

Yes, that's correct. If you simply wish to get all unique points we need to reshape the array and make it unique.

To get the unique points for mesh m:

points = m.points.reshape((-1, 3))
unique_points = numpy.unique(points)

The base module is the base class for all Mesh type classes so you can safely refer to that :)

Ccaccia73 commented 2 years ago

Thank you for the clarification. This:

unique_points = np.unique(m.points.reshape((m.points.shape[0] * 3, 3)), axis=0)

did the trick for me. I have put it into a k-d tree so that it is relatively fast to rebuild the connectivity matrix of the faces. Thank you