def test_equal_polygon3D(): poly1 = Polygon([(1, 0, 0), (0, 0, 0), (0, 1, 0), (1, 1, 0)]) poly2 = Polygon([(1, 1, 0), (1, 0, 0), (0, 0, 0), (0, 1, 0)]) assert poly1 == poly2 poly1 = Polygon([(1, 0, 0), (0, 0, 0), (0, 1, 0), (1, 1, 0)]) poly2 = Polygon(reversed([(1, 1, 0), (1, 0, 0), (0, 0, 0), (0, 1, 0)])) assert poly1 != poly2
def test_bounding_box(): poly = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) poly3d = Polygon3D([(0, 0, 0), (0, 1, 1), (1, 1, 1), (1, 0, 0)]) expected = Polygon([(1, 1, 1), (1, 0, 0), (0, 0, 0), (0, 1, 1)]) result = poly3d.bounding_box assert almostequal(result, expected)
def test_polygon_attributes(): poly2d = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) assert len(poly2d) == 4 assert poly2d.xs == [0, 0, 1, 1] assert poly2d.ys == [0, 1, 1, 0] assert poly2d.zs == [0, 0, 0, 0] assert poly2d.vertices_list == [(0, 0), (0, 1), (1, 1), (1, 0)] assert poly2d.vertices == [Vector2D(*v) for v in poly2d]
def test_difference_2D_polys_single(): """Simplest test for difference_2D_polys This has two squares in the horizontal plane which overlap in one place. Fails if the two original polygons do not have the intersection removed. """ s1 = Polygon([(0, 2), (2, 2), (2, 0), (0, 0)]) # clockwise s2 = Polygon([(1, 3), (3, 3), (3, 1), (1, 1)]) # clockwise # clockwise ex_s1 = [Polygon([(0, 2), (1, 2), (1, 1), (2, 1), (2, 0), (0, 0)])] ex_s2 = [Polygon([(1, 3), (3, 3), (3, 1), (2, 1), (2, 2), (1, 2)])] expected = [ex_s1, ex_s2] result = [s1.difference(s2), s2.difference(s1)] assert result[0] == expected[0] assert result[1] == expected[1]
def test_intersect_2D_polys_single(): """Simplest test for intersect_2D_polys This has two squares in the horizontal plane which overlap in one place. Fails if the expected overlapping shape is not returned. """ # surface is already a flat plane with z == 0 s1 = Polygon([(0, 2), (2, 2), (2, 0), (0, 0)]) # clockwise s2 = Polygon([(1, 3), (3, 3), (3, 1), (1, 1)]) # clockwise expected = [Polygon([(1, 2), (2, 2), (2, 1), (1, 1)])] #clockwise result = s1.intersect(s2) for res, exp in zip(result, expected): assert res == exp result = s2.intersect(s1) for res, exp in zip(result, expected): assert res == exp
def test_add_polygon_to_polygon(): # 2D poly1 = Polygon([(1, 0), (0, 0), (0, 1)]) poly2 = Polygon([(1, 0), (1, 0), (1, 0)]) expected = Polygon([(2, 0), (1, 0), (1, 1)]) result = poly1 + poly2 assert almostequal(result, expected) vector = Vector2D(1, 0) result = poly1 + vector assert almostequal(result, expected) vector = Vector3D(1, 0, 0) try: result = poly1 + vector # should fail assert False except ValueError: pass # 3D poly1 = Polygon([(1, 0, 1), (0, 0, 1), (0, 1, 1)]) poly2 = Polygon([(1, 0, 1), (1, 0, 1), (1, 0, 1)]) expected = Polygon([(2, 0, 2), (1, 0, 2), (1, 1, 2)]) result = poly1 + poly2 assert almostequal(result, expected) vector = Vector3D(1, 0, 1) result = poly1 + vector assert almostequal(result, expected) vector = Vector2D(1, 0) try: result = poly1 + vector # should fail assert False except ValueError: pass
def test_rotate(): """Test for rotating 3D polygons into 2D and back again """ # At the origin s1 = Polygon3D([(0, 0, 2), (2, 0, 2), (0, 0, 0)]) # vertical expected = Polygon([(0, 2), (2, 2), (0, 0)]) # convert to 2D result = s1.project_to_2D() assert result == expected # revert to 3D result = result.project_to_3D(s1) assert result == s1 # Away from the origin s1 = Polygon3D([(1, 0, 2), (3, 0, 2), (1, 0, 0)]) # vertical expected = Polygon([(1, 2), (3, 2), (1, 0)]) # convert to 2D result = s1.project_to_2D() assert result == expected # revert to 3D result = result.project_to_3D(s1) assert result == s1 # Away from the origin s1 = Polygon3D([(0, 1, 1), (2, 2, 0), (2, 2, 2), (0, 1, 2)]) # vertical expected = Polygon([(0.0, 1.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0)]) # convert to 2D result = s1.project_to_2D() assert result == expected # revert to 3D result = result.project_to_3D(s1) assert result == s1
def test_polygon_repr(): s2D = Polygon([(0, 0), (2, 0), (2, 0), (0, 0)]) # vertical assert eval(repr(s2D)) == s2D s3D = Polygon3D([(0, 0, 0), (2, 0, 0), (2, 2, 0), (0, 2, 0)]) # vertical assert eval(repr(s3D)) == s3D