I am generating 2D binary shapes in python. After that, I want them to be converted to a 3D STL file for 3D printing. This kind of works, but only with "simple" shapes like triangles and squares. For more complex shapes I get the following Shape :
So as we can see it kinda looks like the binary image but with some more "artifacts". I use:
- delaunay triangulation (from scipy)
- numpy-stl for generation of the stl file (needs vertices + faces for generation, that is the reason for the triangulation)
- numpy to save/load my shapes
Here is my code:
import numpy as np
from scipy.spatial import Delaunay
from stl import mesh
from numpy import loadtxt
def load_shape(id):
return loadtxt("../shapes/shape_{}.shape".format(id))
def extract_vertices(shape):
arr = []
for ix, x in enumerate(shape):
for iy, y in enumerate(x):
if y == 0:
arr.append([ix, iy])
return np.array(arr)
def vertices_2d_to_3d(vertices, z=10):
x, y = vertices.shape
new = np.zeros((x*2, y + 1))
for i, v in enumerate(vertices):
new[i] = [v[0], v[1], 0]
new[x+i] = [v[0], v[1], z]
return new
shape = load_shape(4)
vertices = extract_vertices(shape)
vertices = vertices_2d_to_3d(vertices, 10)
tri = Delaunay(vertices)
faces = tri.convex_hull
ms = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
ms.vectors[i][j] = vertices[f[j],:]
ms.save('shape.stl')
Can anyone give me some hints on how to get rid of these "artifacts" and tell delaunay triangulation to not connect the vertices that are not in the shape?