Skip to content

Commit

Permalink
Merge pull request #79 from hroncok/allclose
Browse files Browse the repository at this point in the history
Use numpy.allclose() in tests to check float arrays equality
  • Loading branch information
wolph committed May 11, 2018
2 parents 2cc27fc + d1c02b0 commit 1ae4518
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
47 changes: 22 additions & 25 deletions tests/test_mesh.py
Expand Up @@ -33,12 +33,9 @@ def test_units_2d():
mesh = Mesh(data, remove_empty_areas=False)
mesh.update_units()

assert (mesh.areas == [.5, .5]).all()
assert (mesh.normals == [[0, 0, 1.],
[0, 0, -1.]]).all()

assert (mesh.units == [[0, 0, 1],
[0, 0, -1]]).all()
assert numpy.allclose(mesh.areas, [.5, .5])
assert numpy.allclose(mesh.normals, [[0, 0, 1.], [0, 0, -1.]])
assert numpy.allclose(mesh.units, [[0, 0, 1], [0, 0, -1]])


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

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

units = mesh.units[0]
assert units[0] == 0
Expand Down Expand Up @@ -102,28 +99,28 @@ def test_duplicate_polygons():
mesh = Mesh(data, remove_duplicate_polygons=True)
assert mesh.data.size == 3

assert (mesh.vectors[0] == numpy.array([[1, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert (mesh.vectors[1] == numpy.array([[2, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert (mesh.vectors[2] == numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert numpy.allclose(mesh.vectors[0], numpy.array([[1, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
assert numpy.allclose(mesh.vectors[1], numpy.array([[2, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
assert numpy.allclose(mesh.vectors[2], numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))

mesh = Mesh(data, remove_duplicate_polygons=RemoveDuplicates.ALL)
assert mesh.data.size == 3

assert (mesh.vectors[0] == numpy.array([[1, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert (mesh.vectors[1] == numpy.array([[2, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert (mesh.vectors[2] == numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])).all()
assert numpy.allclose(mesh.vectors[0], numpy.array([[1, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
assert numpy.allclose(mesh.vectors[1], numpy.array([[2, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
assert numpy.allclose(mesh.vectors[2], numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))


def test_remove_all_duplicate_polygons():
Expand Down
50 changes: 26 additions & 24 deletions tests/test_rotate.py
Expand Up @@ -49,14 +49,16 @@ def test_rotation():
# substracting .5
data['vectors'] += .5

assert (mesh.vectors == numpy.array([
# We use a slightly higher absolute tolerance here, for ppc64le
# https://github.com/WoLpH/numpy-stl/issues/78
assert numpy.allclose(mesh.vectors, numpy.array([
[[1, 0, 0], [0, 1, 0], [0, 0, 0]],
[[0, 1, 0], [1, 0, 0], [1, 1, 0]],
[[0, 1, 1], [0, 1, 0], [1, 1, 1]],
[[1, 1, 0], [0, 1, 0], [1, 1, 1]],
[[0, 0, 1], [0, 1, 1], [0, 1, 0]],
[[0, 0, 1], [0, 0, 0], [0, 1, 0]],
])).all()
]), atol=1e-07)


def test_rotation_over_point():
Expand Down Expand Up @@ -127,13 +129,13 @@ def test_no_rotation():

# Rotate by 0 degrees
mesh.rotate([0.5, 0.0, 0.0], math.radians(0))
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))

# Use a zero rotation matrix
mesh.rotate([0.0, 0.0, 0.0], math.radians(90))
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))


def test_no_translation():
Expand All @@ -144,13 +146,13 @@ def test_no_translation():
[0, 0, 1]])

mesh = Mesh(data, remove_empty_areas=False)
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))

# Translate mesh with a zero vector
mesh.translate([0.0, 0.0, 0.0])
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))


def test_translation():
Expand All @@ -161,13 +163,13 @@ def test_translation():
[0, 0, 1]])

mesh = Mesh(data, remove_empty_areas=False)
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))

# Translate mesh with vector [1, 2, 3]
mesh.translate([1.0, 2.0, 3.0])
assert (mesh.vectors == numpy.array([
[[1, 3, 4], [2, 2, 4], [1, 2, 4]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[1, 3, 4], [2, 2, 4], [1, 2, 4]]]))


def test_no_transformation():
Expand All @@ -178,14 +180,14 @@ def test_no_transformation():
[0, 0, 1]])

mesh = Mesh(data, remove_empty_areas=False)
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))

# Transform mesh with identity matrix
mesh.transform(numpy.eye(4))
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.all(mesh.areas == 0.5)
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))
assert numpy.allclose(mesh.areas, 0.5)


def test_transformation():
Expand All @@ -196,14 +198,14 @@ def test_transformation():
[0, 0, 1]])

mesh = Mesh(data, remove_empty_areas=False)
assert (mesh.vectors == numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]])).all()
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 1, 1], [1, 0, 1], [0, 0, 1]]]))

# Transform mesh with identity matrix
tr = numpy.zeros((4, 4))
tr[0:3, 0:3] = Mesh.rotation_matrix([0, 0, 1], 0.5 * numpy.pi)
tr[0:3, 3] = [1, 2, 3]
mesh.transform(tr)
assert (mesh.vectors == numpy.array([
[[0, 2, 4], [1, 3, 4], [1, 2, 4]]])).all()
assert numpy.all(mesh.areas == 0.5)
assert numpy.allclose(mesh.vectors, numpy.array([
[[0, 2, 4], [1, 3, 4], [1, 2, 4]]]))
assert numpy.allclose(mesh.areas, 0.5)

0 comments on commit 1ae4518

Please sign in to comment.