Пример #1
0
def test_intersect_parallel_harder():
    line1 = Line(1.38, 1.00, -0.00)

    for incr in np.linspace(0, 0.000000001, 100):
        with nose.tools.assert_raises(Parallel):
            line2 = Line(1.38 + incr, 1.00, 0.0)
            line1.intersect(line2)
Пример #2
0
def test_net_graphgen_1():
    # One intersection, no connected
    g = pypenrose.net.gridlines_to_gridgraph([
        Line(0, 1, 1),
        Line(1, 0, 1),
    ])
    assert_graph_props(g, nodes=1, edges=0)
Пример #3
0
def test_net_graphgen_3():
    # Triangle, 3 intersects, 3 edges
    g = pypenrose.net.gridlines_to_gridgraph([
        Line(1, 1, 0),
        Line(1, 0, 0),
        Line(0, 1, 1),
    ])
    assert_graph_props(g, nodes=3, edges=3)
Пример #4
0
def test_net_graphgen_2():
    # Two intersection, one connection
    g = pypenrose.net.gridlines_to_gridgraph([
        Line(0, 1, 1),
        Line(1, 0, 1),
        Line(1, 0, 2),
    ])
    assert_graph_props(g, nodes=2, edges=1)
Пример #5
0
def test_net_graphgen_degenerate():
    # No lines to intersect
    g = pypenrose.net.gridlines_to_gridgraph([])
    assert_graph_props(g, nodes=0, edges=0)

    # Too few lines to intersect
    g = pypenrose.net.gridlines_to_gridgraph([Line(0, 1, 1)])
    assert_graph_props(g, nodes=0, edges=0)

    # All parallel lines
    g = pypenrose.net.gridlines_to_gridgraph([
        Line(0, 1, 1),
        Line(0, 1, 2),
        Line(0, 1, 3),
    ])
    assert_graph_props(g, nodes=0, edges=0)
Пример #6
0
def test_distance_to():
    line1 = Line.from_two_points(0, 0, 1, 1)

    for i in range(5):
        assert np.allclose(line1.distance_to(i, i), 0)

    assert np.allclose(line1.distance_to(1, 0), np.sqrt(2) / 2)
    assert np.allclose(line1.distance_to(0, 1), np.sqrt(2) / 2)
Пример #7
0
def test_signed_distance_manhattan():
    line1 = Line.from_two_points(0, 0, 1, 1)

    for i in range(5):
        # Pts on the line
        nose.tools.assert_equal(line1.signed_distance_manhattan(i, i), 0)

        # Pts one unit above/below
        nose.tools.assert_equal(line1.signed_distance_manhattan(i + 1, i), 1)
        nose.tools.assert_equal(line1.signed_distance_manhattan(i - 1, i), -1)

        nose.tools.assert_equal(line1.signed_distance_manhattan(i, i + 1), -1)
        nose.tools.assert_equal(line1.signed_distance_manhattan(i, i - 1), 1)
Пример #8
0
def test_dense_self_intersection():
    line_count = 5
    xlines, ylines = space.get_nd_grid(line_count, ndim=2)

    # Intersect all the parallel lines.
    # Easy to test, none should intersect
    xys = space.self_intersection(xlines)

    assert xys.shape == (2, line_count, line_count)
    assert np.all(np.isnan(xys))

    line_set = [Line(1, -1, 0), Line(1, 0, 1), Line(0, 1, 1)]

    xys = space.self_intersection(line_set)

    for x in range(len(line_set)):
        for y in range(len(line_set)):
            pt = xys[:, x, y]
            if x == y:
                assert np.all(np.isnan(pt))
            else:
                assert np.all(pt == 1)
Пример #9
0
def get_1d_gridlines(xy, offset, line_count):
    assert (0 <= abs(offset)) and (abs(offset) <= 1)

    lines = []
    for n in range(line_count):
        # point on the line normal from the origin
        x1 = xy[0] * (n + offset - ((line_count - 1) / 2))
        y1 = xy[1] * (n + offset - ((line_count - 1) / 2))

        # Point one step normal
        x2 = x1 + xy[1]
        y2 = y1 - xy[0]

        lines.append(Line.from_two_points(x1, y1, x2, y2))
    return lines
Пример #10
0
def test_line_metric():
    line1 = Line.from_two_points(0, 0, 1, 1)

    for i in range(5):
        r = line1.metric(i, i)
        assert np.allclose(r, np.sqrt(2) * i)

        r = line1.metric(-i, -i)
        assert np.allclose(r, -np.sqrt(2) * i)

        r = line1.metric(i, -i)
        assert np.allclose(r, 0)

        r = line1.metric(-i, i)
        assert np.allclose(r, 0)
Пример #11
0
def test_line_signed_distance_euclidean():
    line1 = Line.from_two_points(0, 0, 1, 1)

    for i in range(5):
        nose.tools.assert_equal(line1.signed_distance_euclidean(i, i), 0)

        nose.tools.assert_equal(line1.signed_distance_euclidean(i, i + 1),
                                -np.sqrt(2) / 2)
        nose.tools.assert_equal(line1.signed_distance_euclidean(i, i - 1),
                                np.sqrt(2) / 2)

        nose.tools.assert_equal(line1.signed_distance_euclidean(i + 1, i),
                                np.sqrt(2) / 2)
        nose.tools.assert_equal(line1.signed_distance_euclidean(i - 1, i),
                                -np.sqrt(2) / 2)
Пример #12
0
def test_alt_constructors():
    line1 = Line.from_point_slope(0, 0, 1)
    line2 = Line.from_two_points(0, 0, 1, 1)

    nose.tools.assert_equal(line1, line2)
    nose.tools.assert_equal(Line(1, 0, 1),
                            Line.from_point_slope(1, 0, float("inf")))

    line3 = Line.from_two_points(0.0, 0.0, 0.0, -1.0)
    line4 = Line.from_two_points(0.0, 0.0, 0.0, 1.0)

    with nose.tools.assert_raises(Parallel):
        line3.intersect(line4)
Пример #13
0
def test_init():
    Line(1, 2, 3)
    Line(0, 2, 3)
    Line(1, 0, 3)

    with nose.tools.assert_raises(AssertionError):
        Line(0, 0, 0)

    with nose.tools.assert_raises(AssertionError):
        Line(0, 0, 45)

    with nose.tools.assert_raises(AssertionError):
        Line(float("inf"), 1, 3)
Пример #14
0
def test_normal_line_through():
    line1 = Line.from_two_points(0, 0, 1, 1)
    line2 = line1.normal_line_through(0, 0)

    xi, yi = line1.intersect(line2)
    assert np.allclose([xi, yi], 0)

    # Test normals to x=1
    line3 = Line(1, 0, 0)
    line4 = line3.normal_line_through(0, 0)
    line5 = line4.normal_line_through(0, 0)
    nose.tools.assert_equal(line3, line5)

    # Test normals to y=1
    line6 = Line(0, 1, 0)
    line7 = line6.normal_line_through(0, 0)
    line8 = line7.normal_line_through(0, 0)
    nose.tools.assert_equal(line8, line6)
Пример #15
0
def test_eq_operator():
    line1 = Line(1, -1, 0)  # y = x
    line2 = Line(-1, 1, 0)  # y = x

    nose.tools.assert_equal(line1, line2)
    nose.tools.assert_equal(line2, line1)

    line3 = Line(5, 4, 3)
    line4 = Line(-1, 3, 7)

    assert line3 != line4

    # Parallel but not equal
    line5 = Line(1, 1, 1)
    line6 = Line(1, 1, 2)

    assert line5 != line6
Пример #16
0
def test_eval():
    line1 = Line(1, -1, 0)  # y = x
    assert line1.x_at(5) == 5, line1.x_at(5)
    assert line1.y_at(5) == 5, line1.y_at(5)
Пример #17
0
def test_string():
    line1 = Line(1, 1, 1)
    nose.tools.assert_equal(str(line1), "Line: 1.00x + 1.00y = 1.00")
Пример #18
0
def test_init_invalid():
    with nose.tools.assert_raises(AssertionError):
        Line(float("inf"), 0, 0)

    with nose.tools.assert_raises(AssertionError):
        Line(0, float("nan"), 0)
Пример #19
0
def test_intersect():
    line1 = Line(0, 1, 1)
    line2 = Line(1, 0, 1)

    i1 = line1.intersect(line2)
    nose.tools.assert_equal(i1, (1, 1))
Пример #20
0
def test_closest_point_to():
    line1 = Line.from_two_points(0, 0, 1, 1)
    ptx, pty = line1.closest_point_to(0, 1)

    assert np.allclose([ptx, pty], 0.5)
Пример #21
0
def test_intersect_colinear():
    line1 = Line(0, 1, 1)

    with nose.tools.assert_raises(Parallel):
        line1.intersect(line1)

    line3 = Line(1, -1, 1)
    line4 = Line(-1, 1, -1)
    with nose.tools.assert_raises(Parallel):
        line3.intersect(line4)

    with nose.tools.assert_raises(Parallel):
        line4.intersect(line3)
Пример #22
0
def test_line_box_plot():
    line1 = Line(1, 1, 0)
    line2 = Line(1, -1, 0)

    figures.plot_line_inside_bounds(line1)
    figures.plot_line_inside_bounds(line2)