Commit 9dfaa79b authored by sumpfralle's avatar sumpfralle

fixed the generation of the triangle's normal (in case they are undefined or...

fixed the generation of the triangle's normal (in case they are undefined or badly formatted) during import (they pointed at the inside of the model before)
fixed the order of vertices when creating a new Triangle instance (clockwise instead of STL's counter-clockwise order is expected)
output a warning once if the direction of the normal does not match the order of the vertices of a triangle (both indicators are redundant and should be consistent)


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@710 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent f7e9ad9c
...@@ -59,6 +59,8 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): ...@@ -59,6 +59,8 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None):
edges = 0 edges = 0
kdtree = None kdtree = None
normal_conflict_warning_seen = False
try: try:
f = open(filename, "rb") f = open(filename, "rb")
except IOError, err_msg: except IOError, err_msg:
...@@ -131,15 +133,21 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): ...@@ -131,15 +133,21 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None):
# not used # not used
attribs = unpack("<H", f.read(2)) attribs = unpack("<H", f.read(2))
dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1))) dotcross = n.dot(p2.sub(p1).cross(p3.sub(p1)))
if a1 == a2 == a3 == 0: if a1 == a2 == a3 == 0:
dotcross = p3.sub(p1).cross(p2.sub(p1)).z dotcross = p2.sub(p1).cross(p3.sub(p1)).z
n = None n = None
if dotcross > 0: if dotcross > 0:
t = Triangle(p1, p2, p3) # Triangle expects the vertices in clockwise order
elif dotcross < 0:
t = Triangle(p1, p3, p2) t = Triangle(p1, p3, p2)
elif dotcross < 0:
if not normal_conflict_warning_seen:
log.warn(("Inconsistent normal/vertices found in line " \
+ "%d of '%s'. Please validate the STL file!") \
% (current_line, filename))
normal_conflict_warning_seen = True
t = Triangle(p1, p2, p3)
else: else:
# the three points are in a line - or two points are identical # the three points are in a line - or two points are identical
# usually this is caused by points, that are too close together # usually this is caused by points, that are too close together
...@@ -209,18 +217,28 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None): ...@@ -209,18 +217,28 @@ def ImportModel(filename, use_kdtree=True, program_locations=None, unit=None):
m = endfacet.match(line) m = endfacet.match(line)
if m: if m:
if not n: if not n:
n = p3.sub(p1).cross(p2.sub(p1)).normalized() n = p2.sub(p1).cross(p3.sub(p1)).normalized()
# validate the normal
# The three vertices of a triangle in an STL file are supposed
# to be in counter-clockwise order. This should match the
# direction of the normal.
if n is None: if n is None:
# invalid triangle (zero-length vector) # invalid triangle (zero-length vector)
dotcross = 0 dotcross = 0
else: else:
# make sure the points are in ClockWise order # make sure the points are in ClockWise order
dotcross = n.dot(p3.sub(p1).cross(p2.sub(p1))) dotcross = n.dot(p2.sub(p1).cross(p3.sub(p1)))
if dotcross > 0: if dotcross > 0:
t = Triangle(p1, p2, p3, n) # Triangle expects the vertices in clockwise order
elif dotcross < 0:
t = Triangle(p1, p3, p2, n) t = Triangle(p1, p3, p2, n)
elif dotcross < 0:
if not normal_conflict_warning_seen:
log.warn(("Inconsistent normal/vertices found in line " \
+ "%d of '%s'. Please validate the STL file!") \
% (current_line, filename))
normal_conflict_warning_seen = True
t = Triangle(p1, p2, p3, n)
else: else:
# The three points are in a line - or two points are # The three points are in a line - or two points are
# identical. Usually this is caused by points, that are too # identical. Usually this is caused by points, that are too
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment