Skip to content

Commit

Permalink
Merge branch 'release/2.11.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Mar 27, 2020
2 parents 99948d0 + 01d4bd4 commit b42fd22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion stl/__about__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__package_name__ = 'numpy-stl'
__import_name__ = 'stl'
__version__ = '2.11.0'
__version__ = '2.11.1'
__author__ = 'Rick van Hattem'
__author_email__ = 'Wolph@Wol.ph'
__description__ = ' '.join('''
Expand Down
15 changes: 11 additions & 4 deletions stl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,13 @@ def remove_empty_areas(cls, data):
squared_areas = (normals ** 2).sum(axis=1)
return data[squared_areas > AREA_SIZE_THRESHOLD ** 2]

def update_normals(self):
'''Update the normals for all points'''
def update_normals(self, update_areas=True):
'''Update the normals and areas for all points'''
normals = numpy.cross(self.v1 - self.v0, self.v2 - self.v0)

if update_areas:
self.update_areas(normals)

normal = numpy.linalg.norm(normals, axis=1)
non_zero = normal > 0
if non_zero.any():
Expand All @@ -328,8 +332,11 @@ def update_min(self):
def update_max(self):
self._max = self.vectors.max(axis=(0, 1))

def update_areas(self):
areas = .5 * numpy.sqrt((self.normals ** 2).sum(axis=1))
def update_areas(self, normals=None):
if normals is None:
normals = numpy.cross(self.v1 - self.v0, self.v2 - self.v0)

areas = .5 * numpy.sqrt((normals ** 2).sum(axis=1))
self.areas = areas.reshape((areas.size, 1))

def check(self):
Expand Down
20 changes: 13 additions & 7 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ def test_units_3d():

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

units = mesh.units[0]
assert units[0] == 0
# Due to floating point errors
assert (units[1] + .5 * 2 ** .5) < 0.0001
assert (units[2] - .5 * 2 ** .5) < 0.0001
assert numpy.allclose(mesh.units[0], [0.0, -0.5, 0.5])


def test_duplicate_polygons():
Expand Down Expand Up @@ -173,9 +168,20 @@ def test_empty_areas():
[0, 1, 0],
[1, 0, 0]])

mesh = Mesh(data, remove_empty_areas=False)
mesh = Mesh(data, calculate_normals=False, remove_empty_areas=False)
assert mesh.data.size == 3

# Test the normals recalculation which also calculates the areas by default
mesh.areas[1] = 1
mesh.areas[2] = 2
assert numpy.allclose(mesh.areas, [[0.5], [1.0], [2.0]])

mesh.update_normals(update_areas=False)
assert numpy.allclose(mesh.areas, [[0.5], [1.0], [2.0]])

mesh.update_normals(update_areas=True)
assert numpy.allclose(mesh.areas, [[0.5], [0.0], [0.0]])

mesh = Mesh(data, remove_empty_areas=True)
assert mesh.data.size == 1

Expand Down

0 comments on commit b42fd22

Please sign in to comment.