Skip to content

Commit f6312e4

Browse files
author
Tristan
committed
final changes for solid mesh feature
1 parent cb0a9ca commit f6312e4

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ numpy2stl(A, "examples/Lena.stl", scale=0.1)
6868
Source image vs. output geometry:
6969
![Alt text](http://i.imgur.com/CdZzhBp.png "Screenshot")
7070

71-
[Click to view STL (view as wireframe)](examples/Lena.stl)
71+
[Click to view STL](examples/Lena.stl)
7272

7373
---
7474

@@ -90,7 +90,7 @@ Equivalent command-line syntax:
9090
```
9191

9292
![Alt text](http://i.imgur.com/LFvw5Yn.png "Screenshot")
93-
[Click to view STL (view as wireframe)](examples/NASA.stl)
93+
[Click to view STL](examples/NASA.stl)
9494

9595
---
9696

@@ -112,7 +112,7 @@ Equivalent command-line syntax:
112112
Source image vs. output geometry:
113113
![Alt text](http://i.imgur.com/70wFtCR.png "Screenshot")
114114

115-
[Click to view STL (view as wireframe)](examples/OpenMDAO-logo.stl)
115+
[Click to view STL](examples/OpenMDAO-logo.stl)
116116

117117
---
118118

@@ -144,7 +144,7 @@ Source image vs. output geometry:
144144
![Alt text](examples/Greens-Theorem_Navier-Stokes.png "Screenshot")
145145
![Alt text](http://i.imgur.com/TgHlFGK.png "Screenshot")
146146

147-
[Click to view STL (view as wireframe)](examples/Greens-Theorem_Navier-Stokes.stl)
147+
[Click to view STL](examples/Greens-Theorem_Navier-Stokes.stl)
148148

149149

150150
## Library usage:

stl_tools/numpy2stl.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import numpy as np
55

6-
import pylab
6+
77
ASCII_FACET = """ facet normal {face[0]:e} {face[1]:e} {face[2]:e}
88
outer loop
99
vertex {face[3]:e} {face[4]:e} {face[5]:e}
@@ -62,7 +62,8 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
6262
max_width=235.,
6363
max_depth=140.,
6464
max_height=150.,
65-
solid=True):
65+
solid=True,
66+
min_thickness_percent=0.1):
6667
"""
6768
Reads a numpy array, and outputs an STL file
6869
@@ -86,7 +87,14 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
8687
object (in mm). Match this to
8788
the dimensions of a 3D printer
8889
platform
89-
90+
solid (bool): sets whether to create a solid geometry (with sides and
91+
a bottom) or not.
92+
min_thickness_percent (float) : when creating the solid bottom face, this
93+
multiplier sets the minimum thickness in
94+
the final geometry (shallowest interior
95+
point to bottom face), as a percentage of
96+
the thickness of the model computed up to
97+
that point.
9098
Returns: (None)
9199
"""
92100
m, n = A.shape
@@ -98,6 +106,7 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
98106
facets = []
99107
mask = np.zeros((m, n))
100108
for i, k in product(range(m - 1), range(n - 1)):
109+
print("Creating top mesh...")
101110

102111
this_pt = np.array([i - m / 2., k - n / 2., A[i, k]])
103112
top_right = np.array([i - m / 2., k + 1 - n / 2., A[i, k + 1]])
@@ -134,17 +143,23 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
134143
facets = np.array(facets)
135144

136145
if solid:
146+
print("Computed edges...")
137147
edge_mask = np.sum([roll2d(mask, (i, k))
138148
for i, k in product([-1, 0, 1], repeat=2)], axis=0)
139149
edge_mask[np.where(edge_mask == 9.)] = 0.
140150
edge_mask[np.where(edge_mask != 0.)] = 1.
141-
edge_mask[0::m - 1,:] = 1.
151+
edge_mask[0::m - 1, :] = 1.
142152
edge_mask[:, 0::n - 1] = 1.
143153
X, Y = np.where(edge_mask == 1.)
144154
locs = zip(X - m / 2., Y - n / 2.)
145-
minval = facets[:, 5::3].min() - 0.1 * facets[:, 5::3].ptp()
146-
#[0, 0, 0, 1, 1, z, 1, 1, z, 1, 1, z]
155+
156+
zvals = facets[:, 5::3]
157+
zmin, zthickness = zvals.min(), zvals.ptp()
158+
159+
minval = zmin - min_thickness_percent * zthickness
160+
147161
bottom = []
162+
print("Extending edges, creating bottom...")
148163
for i, facet in enumerate(facets):
149164
if (facet[3], facet[4]) in locs:
150165
facets[i][5] = minval
@@ -153,7 +168,8 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
153168
if (facet[9], facet[10]) in locs:
154169
facets[i][11] = minval
155170
this_bottom = np.concatenate(
156-
[facet[:3], facet[6:8], [minval], facet[3:5], [minval], facet[9:11], [minval]])
171+
[facet[:3], facet[6:8], [minval], facet[3:5], [minval],
172+
facet[9:11], [minval]])
157173
bottom.append(this_bottom)
158174

159175
facets = np.concatenate([facets, bottom])
@@ -171,11 +187,3 @@ def numpy2stl(A, fn, scale=0.1, mask_val=-np.inf, ascii=False,
171187
facets = facets * float(max_height) / zsize
172188

173189
writeSTL(facets, fn, ascii=ascii)
174-
175-
176-
if __name__ == "__main__":
177-
from scipy.ndimage import gaussian_filter
178-
A = 256 * pylab.imread("openmdao.png")
179-
A = A[:,:, 0] + 1.*A[:,:, 3] # Compose some elements from RGBA to give depth
180-
A = gaussian_filter(A, 2) # smoothing
181-
numpy2stl(A, "OpenMDAO-logo.stl", scale=0.05, mask_val=1.)

0 commit comments

Comments
 (0)