Skip to content

Doing only 2 triangles to complete the base. #2

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
EXTENSIONS = [ext_1]

setup(name='stl_tools',
version='0.3.0',
version='0.3.2',
install_requires=['numpy', 'scipy', 'matplotlib'],
description="Generate STL files from numpy arrays and text",
author='Tristan Hearn',
Expand Down
95 changes: 78 additions & 17 deletions stl_tools/numpy2stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def numpy2stl(A, fn, scale=0.1, mask_val=None, ascii=False,
Returns: (None)
"""

A = A.astype('float32')

print " =============> min array value", np.amin(A)

m, n = A.shape
if n >= m and rotate:
# rotate to best fit a printing platform
Expand All @@ -123,7 +127,7 @@ def numpy2stl(A, fn, scale=0.1, mask_val=None, ascii=False,
else: # use python + numpy
facets = []
mask = np.zeros((m, n))
print("Creating top mesh...")
print("Calculting min value...")
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 Down Expand Up @@ -155,7 +159,7 @@ def numpy2stl(A, fn, scale=0.1, mask_val=None, ascii=False,
facets = np.array(facets)

if solid:
print("Computed edges...")
print("Pushing border edges down...")
edge_mask = np.sum([roll2d(mask, (i, k))
for i, k in product([-1, 0, 1], repeat=2)],
axis=0)
Expand All @@ -169,21 +173,78 @@ def numpy2stl(A, fn, scale=0.1, mask_val=None, ascii=False,
zvals = facets[:, 5::3]
zmin, zthickness = zvals.min(), zvals.ptp()

minval = zmin - min_thickness_percent * zthickness

bottom = []
print("Extending edges, creating 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)
minval = np.float32(zmin - min_thickness_percent * zthickness)

for i in xrange(len(A[0])):
A[0][i] = minval
A[-1][i] = minval

for i in xrange(len(A)):
A[i][0] = minval
A[i][-1] = minval

facets = []

print("Creating top mesh...")
for i, k in product(range(m - 1), range(n - 1)):

this_pt = np.array([i - m / 2., k - n / 2., A[i, k]])
top_right = np.array([i - m / 2., k + 1 - n / 2., A[i, k + 1]])
bottom_left = np.array([i + 1. - m / 2., k - n / 2., A[i + 1, k]])
bottom_right = np.array(
[i + 1. - m / 2., k + 1 - n / 2., A[i + 1, k + 1]])

n1, n2 = np.zeros(3), np.zeros(3)

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_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:

print("Creating bottom...")

normals = [0,0,0]

i, k = (0, 0)
left_top = [i - m / 2., k - n / 2., minval]
i, k = (m-1, 0)
right_top = [i - m / 2., k - n / 2., minval]
i, k = (0, n-1)
left_bottom = [i - m / 2., k - n / 2., minval]
i, k = (m-1, n-1)
right_bottom = [i - m / 2., k - n / 2., minval]

bottom = [
np.array(
normals +
right_top +
left_top +
left_bottom
, 'float32'),
np.array(
normals +
right_top +
left_bottom +
right_bottom
, 'float32')
]

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

Expand Down