Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use numpy to build a stl file? #89

Closed
LiangJinWei opened this issue Oct 31, 2018 · 5 comments
Closed

How to use numpy to build a stl file? #89

LiangJinWei opened this issue Oct 31, 2018 · 5 comments

Comments

@LiangJinWei
Copy link

I have 3 numpy:x、y、z
I want to used them to build a 3D stl model
How can I do?

I had tried used the tutorial on the homepage:"Creating Mesh objects from a list of vertices and faces"
and I also read #19 .
But I still have no idea...
This is my code :

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from stl import mesh

#=====Load and build=======

x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")

tris = #I have no idea...

#=====STL=======
data = np.zeros(len(tris.triangles), dtype=mesh.Mesh.dtype)
mobius_mesh = mesh.Mesh(data, remove_empty_areas=False)
mobius_mesh.x[:] = x_all[tris.triangles]
mobius_mesh.y[:] = y_all[tris.triangles]
mobius_mesh.z[:] = z_all[tris.triangles]
mobius_mesh.save('model.stl')

And here is my x、y、 z
xyz.zip

Please help me....

@wolph
Copy link
Owner

wolph commented Oct 31, 2018

The question is a bit similar to this one: #19 (comment)

Specifically:

The problem is that for STL files you need to have triangulated data, luckily Matplotlib has several solutions for that: http://matplotlib.org/api/tri_api.html

@LiangJinWei
Copy link
Author

Thank you for answer my question! But I have no idea to decide a good triangulated data.
The x、y、z data are created by my laser scanner (40 degree / 1step)

I had tried this way to build a 3D model:https://stackoverflow.com/questions/29800749/delaunay-triangulation-of-points-from-2d-surface-in-3d-with-python.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from stl import mesh

#=====Load and build=======

x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")

model=np.vstack((x_all,y_all,z_all))    
model=np.transpose(model)              

model -= model.mean(axis=0)         
rad = np.linalg.norm(model, axis=1)    
zen = np.arccos(model[:,-1] / rad)      
azi = np.arctan2(model[:,1], model[:,0])
tris = mtri.Triangulation(zen, azi)     

#=====STL=======
data = np.zeros(len(tris.triangles), dtype=mesh.Mesh.dtype)
mobius_mesh = mesh.Mesh(data, remove_empty_areas=False)
mobius_mesh.x[:] = x_all[tris.triangles]
mobius_mesh.y[:] = y_all[tris.triangles]
mobius_mesh.z[:] = z_all[tris.triangles]
mobius_mesh.save('model.stl')

But it will have a big hole througt my model.....

@wolph
Copy link
Owner

wolph commented Nov 8, 2018

It's outside of my expertise to be honest... some form of triangulation is the answer but which method works best for your purpose is something I don't know.

@Uvar
Copy link
Collaborator

Uvar commented Nov 16, 2018

There are many, many ways of doing the triangulation. I remember some software spitting out the scanned surface triangulated, but then having a non-closed backside; so there is going to be chances of holes and other imperfections which you will need to fix anyway.

Maybe this paper will help you to get started.

@stale
Copy link

stale bot commented Jan 20, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants