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

Add smoothing option for mesh2 #16

Open
wants to merge 1 commit into
base: pypy
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ stl2pdf
stl2pov
stlinfo
*.inc

# for pycharm IDE
.idea/
13 changes: 11 additions & 2 deletions stl2pov.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ def mesh1(name, vertices):
return "\n".join(lines)


def mesh2(name, vertices):
def mesh2(name, vertices, corner_normals):
"""
Create a POV-ray mesh2 object from facet data.

Arguments:
name: The name of the object.
vertices: An (N,3) numpy array containing the vertex data.
corner_normals: An (N,3) array containing the vertex normal vectors.

Returns:
A string representation of a POV-ray mesh2 object.
Expand All @@ -113,6 +114,9 @@ def mesh2(name, vertices):
# the STL coordinate system and that used in POV-ray
lines += [f" <{p[0]}, {p[2]}, {p[1]}>," for p in points]
lines[-1] = lines[-1][:-1]
if len(corner_normals) > 0:
lines += [" }\n normal_vectors {", " {},".format(len(corner_normals))]
lines += [f" <{v[0]}, {v[2]}, {v[1]}>," for v in corner_normals]
lines += [" }\n face_indices {", f" {len(ifacets)},"]
lines += [f" <{f[0]}, {f[1]}, {f[2]}>," for f in ifacets]
lines[-1] = lines[-1][:-1]
Expand All @@ -138,6 +142,7 @@ def main(argv):
default="utf-8",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("-s", "--smooth", action="store_true", help="add smoothing normals")
parser.add_argument(
"--log",
default="warning",
Expand Down Expand Up @@ -174,9 +179,13 @@ def main(argv):
outs = f"// Generated by stl2pov {__version__}\n"
outs += f"// on {time.asctime()}.\n"
outs += f'// Source file name: "{fn}"\n'
corner_normals = (())
if args.smooth:
logging.info("generating corner normals.")
corner_normals = stl.corner_normals(vertices)
if args.mesh2:
logging.info("generating mesh2 data.")
outs += mesh2(name, vertices)
outs += mesh2(name, vertices, corner_normals)
else:
logging.info("generating mesh data.")
outs += mesh1(name, vertices)
Expand Down
24 changes: 24 additions & 0 deletions stltools/stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ def normals(facets, points):
return vo.indexate(nv)


def corner_normals(vertices):
"""
Calculate corner normals for each shared vertex.

Arguments:
vertices: An (?,3) array containing the vertex data.

Returns:
a (?,3) array containing the corner normals
"""
facets, points = toindexed(vertices)
facet_normal_indices, facet_normal_vectors = normals(facets, points)

# array of empty normal vectors at each vertex
corner_normal_sum = [[0 for i in range(len(points[0]))] for j in range(len(points))]
for facet in range(len(facets)):
for i in range(3):
vertex = facets[facet][i]
facet_normal_index = facet_normal_indices[facet]
for j in range(3):
corner_normal_sum[vertex][j] += facet_normal_vectors[facet_normal_index][j]
return [vo.normalize(v) for v in corner_normal_sum]


def text(name, ifacets, points, inormals, vectors):
"""
Make an STL text representation of a brep.
Expand Down