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

Surface Normal Offset #175

Closed FaezAlkadi closed 3 years ago

FaezAlkadi commented 3 years ago

Great work I was just wondering Is there any way to offset an STL surface using numpy-stl ? something like what Alec has done in the link below: https://www.alecjacobson.com/weblog/?p=4708

Thank you

wolph commented 3 years ago

You should have all variables available to make the calculation: https://numpy-stl.readthedocs.io/en/latest/stl.html?highlight=v0#stl.base.BaseMesh

I would assume that you can use either mesh.vectors or mesh.points with mesh.normals to offset in the right direction.

How you actually want to do it can be tricky though... But I think this paper might be able to help a bit with that: http://www.cad-journal.net/files/vol_1/CAD_1(1-4)_2004_285-291.pdf

FaezAlkadi commented 3 years ago

I have used the following code to call the STL file from my external folder: Folder = os.path.join('0001-STL/test.stl'); Then used the following code to extract data from the STL file: your_mesh = mesh.Mesh.from_file(Folder)

I successfully was able to get all the variables (your_mesh .vectors, your_mesh .points, your_mesh .normals, your_mesh .x, your_mesh .y , your_mesh .z ........ etc)

Now I wish to get the data of the offsetted STL file with name your_mesh _1 for example.

I would appreciate it if you could tell me how I do that ?

Thank you so much.

wolph commented 3 years ago

You can copy a mesh like this:

your_mesh_1 = mesh.Mesh(your_mesh.data.copy())

And you can save it to a file like this:

your_mesh.save('your_mesh.stl')
FaezAlkadi commented 3 years ago

Thanks for your reply I was actually asking how to offset the stl surface and get the data of the your_mesh1 file.

for example if I use pymadcad as in this link: where the inputs are ([points, ...], [triangles, ...]) and output is my_thick_mesh

from madcad import *

surface = Mesh([points, ...], [triangles, ...])
my_thick_mesh = thicken(surface, distance)

from that I would end up with a new data named my_thick_mesh for the offsetted part (thickened part)

SO, my question is:

How can I use numpy-stlthe same way I use pymadcad where my inputs are ( your_mesh , and the distance) instead of ([points, ...], [triangles, ...]),
and my output is the your_mesh1 data, instead of my_thick_mesh

Thank you again

wolph commented 3 years ago

I haven't worked with pymadcad yet so I can't be sure, but it looks like pymadcad is already using this library to read stl files: https://github.com/jimy-byerley/pymadcad/blob/b7757d15f8ae952f7b25460dc7c3674f6569d391/madcad/io.py#L155-L178

Copying that code, I believe you can do this:


# stlmesh is your numpy-stl Mesh instance
trinum = stlmesh.points.shape[0]
ptsbuff = stlmesh.points.reshape(trinum*3, 3).astype('f8')
pts = glm.array(ptsbuff).to_list()
faces = [(i, i+1, i+2)  for i in range(0, 3*trinum, 3)]
# This is the pymadcad Mesh
mesh = Mesh(pts, faces)
mesh.options['name'] = stlmesh.name

thick_mesh = thicken(mesh, distance)
FaezAlkadi commented 3 years ago

I have seen this before. The problem is that pymadcadis not working on my machine. So I tried to see if numpy-stl has a specific function that works as same as thicken function.

Thank you

wolph commented 3 years ago

No, not really. This library was meant as a quick way of storing and loading STL files, performing operations on that mesh are largely outside of the scope of this library.

FaezAlkadi commented 3 years ago

Thank you for clarification.