Skip to content

Commit

Permalink
converted normals to unit vectors to fix #108
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Aug 7, 2019
1 parent 87101f8 commit dcbe8cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion stl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ def remove_empty_areas(cls, data):

def update_normals(self):
'''Update the normals for all points'''
self.normals[:] = numpy.cross(self.v1 - self.v0, self.v2 - self.v0)
normals = numpy.cross(self.v1 - self.v0, self.v2 - self.v0)
normal = numpy.linalg.norm(normals)
if normal:
normals /= normal
self.normals[:] = normals

def update_min(self):
self._min = self.vectors.min(axis=(0, 1))
Expand Down
8 changes: 5 additions & 3 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def test_units_2d():
mesh = Mesh(data, remove_empty_areas=False)
mesh.update_units()

assert numpy.allclose(mesh.areas, [.5, .5])
assert numpy.allclose(mesh.normals, [[0, 0, 1.], [0, 0, -1.]])
assert numpy.allclose(mesh.areas, [0.35355338, 0.35355338])
assert numpy.allclose(mesh.normals, [
[0.0, 0.0, 0.70710677],
[0.0, 0.0, -0.70710677]])
assert numpy.allclose(mesh.units, [[0, 0, 1], [0, 0, -1]])


Expand All @@ -48,7 +50,7 @@ def test_units_3d():
mesh.update_units()

assert (mesh.areas - 2 ** .5) < 0.0001
assert numpy.allclose(mesh.normals, [0, -1, 1])
assert numpy.allclose(mesh.normals, [0.0, -0.70710677, 0.70710677])

units = mesh.units[0]
assert units[0] == 0
Expand Down

0 comments on commit dcbe8cd

Please sign in to comment.