Esempio n. 1
0
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
Esempio n. 2
0
def test_point():
    pt1 = Vector3D(0.0, 0.0, 0.0)
    pt2 = Vector3D(1.0, 1.0, 1.0)

    assert pt2 - pt1 == pt2
    assert pt1 - pt2 == Vector3D(-1, -1, -1)

    assert pt2 + pt2 == Vector3D(2, 2, 2)
Esempio n. 3
0
def test_surface_is_clockwise():
    """Test if a surface is clockwise as seen from a given point.
    """
    poly = Polygon3D(
        reversed([
            Vector3D(0.0, 0.0, 0.0),
            Vector3D(1.0, 0.0, 0.0),
            Vector3D(1.0, 1.0, 0.0),
            Vector3D(0.0, 1.0, 0.0)
        ]))
    poly_inv = Polygon3D([
        Vector3D(0.0, 0.0, 0.0),
        Vector3D(1.0, 0.0, 0.0),
        Vector3D(1.0, 1.0, 0.0),
        Vector3D(0.0, 1.0, 0.0)
    ])

    pt = Vector3D(0.5, 0.5, 1.0)  # point above the plane

    assert poly.is_clockwise(pt)
    assert not poly_inv.is_clockwise(pt)
Esempio n. 4
0
def test_polygon3d_attributes():
    poly3d = Polygon3D([(0, 0, 0), (0, 1, 1), (1, 1, 1), (1, 0, 0)])
    assert len(poly3d) == 4
    assert poly3d.xs == [0, 0, 1, 1]
    assert poly3d.ys == [0, 1, 1, 0]
    assert poly3d.zs == [0, 1, 1, 0]
    assert poly3d.vertices_list == [(0, 0, 0), (0, 1, 1), (1, 1, 1), (1, 0, 0)]
    assert poly3d.vertices == [Vector3D(*v) for v in poly3d]
    assert poly3d.distance == 0
    assert poly3d.is_horizontal == False
    assert poly3d.normal_vector == [0.0, 0.5, -0.5]
    poly3d_2 = Polygon3D([(0, 1, 1), (0, 2, 2), (1, 2, 2), (1, 1, 1)])
    assert poly3d_2.normal_vector == [0.0, 0.5, -0.5]
    assert poly3d_2.projection_axis == 1
    result = poly3d.is_coplanar(poly3d_2)
    assert result
Esempio n. 5
0
def test_closest():
    pt = Vector3D(0, 0, 0)
    poly = Polygon3D([(1, 1, 1), (2, 2, 3), (3, 4, 5)])
    assert pt.closest(poly) == Vector3D(1, 1, 1)
Esempio n. 6
0
def test_on_poly_edge():
    poly = Polygon3D([(0, 4, 0), (0, 0, 0), (4, 0, 0), (4, 4, 0)])
    edge1 = Segment(Vector3D(0, 1, 0), Vector3D(0, 2, 0))
    edge2 = Segment(Vector3D(1, 1, 0), Vector3D(1, 2, 0))
    assert edge1.on_poly_edge(poly)
    assert not edge2.on_poly_edge(poly)
Esempio n. 7
0
def test_normalize():
    v = Vector3D(1, 1, 1)
    v.normalize()
    for i in v:
        assert almostequal(i, 0.57735026)
Esempio n. 8
0
def test_polygon_index():
    poly = Polygon3D([(0, 4, 0), (0, 0, 0), (4, 0, 0), (4, 4, 0)])
    assert poly[1] == Vector3D(0, 0, 0)
    assert poly.index(Vector3D(0, 0, 0)) == 1
Esempio n. 9
0
def test_set_length():
    v = Vector3D(1, 1, 1)
    v.set_length(1)
    for i in v:
        assert almostequal(i, 0.57735026)
Esempio n. 10
0
def test_invert():
    v = Vector3D(1, 2, 3)
    assert v.invert() == Vector3D(-1, -2, -3)
Esempio n. 11
0
def test_surface_normal():
    poly = Polygon3D([
        Vector3D(0.0, 0.0, 0.0),
        Vector3D(1.0, 0.0, 0.0),
        Vector3D(1.0, 1.0, 0.0),
        Vector3D(0.0, 1.0, 0.0)
    ])
    assert list(poly.normal_vector) == [0.0, 0.0,
                                        1.0]  # for a horizontal surface

    poly = Polygon3D(
        reversed([
            Vector3D(0.0, 0.0, 0.0),
            Vector3D(1.0, 0.0, 0.0),
            Vector3D(1.0, 1.0, 0.0),
            Vector3D(0.0, 1.0, 0.0)
        ]))
    assert list(poly.normal_vector) == [0.0, 0.0,
                                        -1.0]  # for a horizontal surface

    poly = Polygon3D([
        Vector3D(0.0, 0.0, 0.0),
        Vector3D(2.0, 1.0, 0.0),
        Vector3D(4.0, 0.0, 0.0),
        Vector3D(4.0, 3.0, 0.0),
        Vector3D(2.0, 2.0, 0.0),
        Vector3D(0.0, 3.0, 0.0)
    ])
    assert list(poly.normal_vector) == [0.0, 0.0,
                                        1.0]  # for a horizontal surface

    poly = Polygon3D(
        reversed([
            Vector3D(0.0, 0.0, 0.0),
            Vector3D(2.0, 1.0, 0.0),
            Vector3D(4.0, 0.0, 0.0),
            Vector3D(4.0, 3.0, 0.0),
            Vector3D(2.0, 2.0, 0.0),
            Vector3D(0.0, 3.0, 0.0)
        ]))
    assert list(poly.normal_vector) == [0.0, 0.0,
                                        -1.0]  # for a horizontal surface

    poly = Polygon3D([[1., 1.1, 0.5], [1., 1.1, 0.], [1., 2.1, 0.],
                      [1., 2.1, 0.5]])
    assert list(poly.normal_vector) == [1.0, 0.0,
                                        0.0]  # for a horizontal surface