Skip to content

Commit

Permalink
fixed improper tesselation order leading to holes in mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan committed Oct 11, 2013
1 parent 685e370 commit cb0a9ca
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 deletions.
Binary file modified examples/Greens-Theorem_Navier-Stokes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/Greens-Theorem_Navier-Stokes.stl
Binary file not shown.
Binary file modified examples/Lena.stl
Binary file not shown.
Binary file modified examples/NASA.stl
Binary file not shown.
Binary file modified examples/OpenMDAO-logo.stl
Binary file not shown.
51 changes: 48 additions & 3 deletions stl_tools/numpy2stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np


import pylab
ASCII_FACET = """ facet normal {face[0]:e} {face[1]:e} {face[2]:e}
outer loop
vertex {face[3]:e} {face[4]:e} {face[5]:e}
Expand Down Expand Up @@ -53,11 +53,16 @@ def writeSTL(facets, file_name, ascii=False):
f.close()


def roll2d(image, shifts):
return np.roll(np.roll(image, shifts[0], axis=0), shifts[1], axis=1)


def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
calc_normals=False,
max_width=235.,
max_depth=140.,
max_height=150.):
max_height=150.,
solid=True):
"""
Reads a numpy array, and outputs an STL file
Expand Down Expand Up @@ -91,6 +96,7 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
A = scale * (A - A.min())

facets = []
mask = np.zeros((m, n))
for i, k in product(range(m - 1), range(n - 1)):

this_pt = np.array([i - m / 2., k - n / 2., A[i, k]])
Expand All @@ -111,16 +117,47 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
if (this_pt[-1] > mask_val and top_right[-1] > mask_val and
bottom_left[-1] > mask_val):

facet = np.concatenate([n1, top_right, this_pt, bottom_left])
facet = np.concatenate([n1, top_right, this_pt, bottom_right])
mask[i, k] = 1
mask[i, k + 1] = 1
mask[i + 1, k] = 1
facets.append(facet)

if (this_pt[-1] > mask_val and bottom_right[-1] > mask_val and
bottom_left[-1] > mask_val):

facet = np.concatenate([n2, bottom_right, this_pt, bottom_left])
facets.append(facet)
mask[i, k] = 1
mask[i + 1, k + 1] = 1
mask[i + 1, k] = 1
facets = np.array(facets)

if solid:
edge_mask = np.sum([roll2d(mask, (i, k))
for i, k in product([-1, 0, 1], repeat=2)], axis=0)
edge_mask[np.where(edge_mask == 9.)] = 0.
edge_mask[np.where(edge_mask != 0.)] = 1.
edge_mask[0::m - 1,:] = 1.
edge_mask[:, 0::n - 1] = 1.
X, Y = np.where(edge_mask == 1.)
locs = zip(X - m / 2., Y - n / 2.)
minval = facets[:, 5::3].min() - 0.1 * facets[:, 5::3].ptp()
#[0, 0, 0, 1, 1, z, 1, 1, z, 1, 1, z]
bottom = []
for i, facet in enumerate(facets):
if (facet[3], facet[4]) in locs:
facets[i][5] = minval
if (facet[6], facet[7]) in locs:
facets[i][8] = minval
if (facet[9], facet[10]) in locs:
facets[i][11] = minval
this_bottom = np.concatenate(
[facet[:3], facet[6:8], [minval], facet[3:5], [minval], facet[9:11], [minval]])
bottom.append(this_bottom)

facets = np.concatenate([facets, bottom])

xsize = facets[:, 3::3].ptp()
if xsize > max_width:
facets = facets * float(max_width) / xsize
Expand All @@ -134,3 +171,11 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
facets = facets * float(max_height) / zsize

writeSTL(facets, fn, ascii=ascii)


if __name__ == "__main__":
from scipy.ndimage import gaussian_filter
A = 256 * pylab.imread("openmdao.png")
A = A[:,:, 0] + 1.*A[:,:, 3] # Compose some elements from RGBA to give depth
A = gaussian_filter(A, 2) # smoothing
numpy2stl(A, "OpenMDAO-logo.stl", scale=0.05, mask_val=1.)
2 changes: 1 addition & 1 deletion stl_tools/text2png.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import matplotlib as mpl
mpl.use('Agg')
mpl.use('Agg', warn=False)
import matplotlib.pyplot as plt


Expand Down

0 comments on commit cb0a9ca

Please sign in to comment.