Example #1
0
def test_insert_before():
    l = BiList()
    l.append(42)
    node = l.append(44)
    l.append(45)
    l.insert_before(node, 43)
    assert [el.value for el in l] == [42, 43, 44, 45]
Example #2
0
def test_append():
    l = BiList()
    l.append(42)
    l.append(43)
    l.append(44)
    l.append(45)
    assert len(l) == 4
    assert [el.value for el in l] == [42, 43, 44, 45]
Example #3
0
def test_prepend():
    l = BiList()
    l.prepend(42)
    l.prepend(43)
    l.prepend(44)
    l.prepend(45)
    assert len(l) == 4
    assert [el.value for el in l] == [45, 44, 43, 42]
Example #4
0
def test_remove():
    l = BiList()
    l.append(42)
    node = l.append(43)
    l.append(44)
    l.append(45)
    l.remove(node)
    assert len(l) == 3
    assert [el.value for el in l] == [42, 44, 45]
Example #5
0
def test_empty_list_is_empty():
    l = BiList()
    assert len(l) == 0
    assert list(l) == []
Example #6
0
def test_prepend_empty():
    l = BiList()
    l.prepend(42)
    assert len(l) == 1
    assert [el.value for el in l] == [42]
Example #7
0
def test_single_element_reverse_indexing():
    l = BiList([42])
    assert l[0].value == l[-1].value
Example #8
0
def test_reverse_indexing():
    l = BiList([42, 43, 44])
    assert l[0].value == l[-3].value
    assert l[1].value == l[-2].value
    assert l[2].value == l[-1].value
Example #9
0
def test_out_of_bounds_indexing():
    l = BiList([42])
    assert l[0].value == 42
    with pytest.raises(KeyError):
        l[1]
        l[-1]
Example #10
0
def test_empty_out_of_bounds_indexing():
    l = BiList()
    with pytest.raises(KeyError):
        l[0]
Example #11
0
def test_empty_after_remove():
    l = BiList()
    node = l.append(42)
    l.remove(node)
    assert len(l) == 0
    assert list(l) == []
Example #12
0
def test_list_initialization():
    l = BiList([42, 43, 44])
    assert len(l) == 3
    assert [el.value for el in l] == [42, 43, 44]
Example #13
0
    def _pretriangulate(self):
        """ 1.1 Initial triangulation of points

        Return triangles and edges.

        Yonghe et al. (2013) A Simple Sweep-line Delaunay Triangulation Algorithm
        http://www.academicpub.org/jao/paperInfo.aspx?PaperID=15630
        """
        # Sort points by x coordinate to prepare for sweep-line algorithm.
        self.points.sort(key=lambda v: v.x)

        triangles = []
        # An edge is a tuple of indices for a pair of points, always ordered so the
        # lower index comes before the higher. An edge is associated with a list of
        # triangles sharing it (one or two).
        triangle_edges = {}
        # The hull is the current outer border of the triangulation. It is a
        # bidirectional list of edges (a CCW polygon).
        hull = BiList()

        # Add the first triangle.
        if self.point_on_left_side(self.points[0], self.points[1],
                                   self.points[2]):
            # print("p2 on left side of p0p1")
            e0 = (0, 1)
            e1 = (1, 2)
            e2 = (2, 0)
            triangles.append(self.Triangle(self.points, 0, 1, 2))
        else:
            # print("p2 on right side of p0p1")
            e0 = (1, 0)
            e1 = (0, 2)
            e2 = (2, 1)
            triangles.append(self.Triangle(self.points, 1, 0, 2))
        hull.append(e0)
        hull.append(e1)
        hull.append(e2)
        triangle_edges[self.edge_tuple(e0)] = [0]
        triangle_edges[self.edge_tuple(e1)] = [0]
        triangle_edges[self.edge_tuple(e2)] = [0]

        pts_index = 3
        for pts_index in range(3, len(self.points)):
            next_pt = self.points[pts_index]

            ix = 0
            while ix < len(hull):
                node = hull[ix]
                i0 = node.value[0]
                i1 = node.value[1]
                p0 = self.points[i0]
                p1 = self.points[i1]
                if not self.point_on_left_side(p0, p1, next_pt):
                    tl = len(triangles)

                    triangles.append(
                        self.Triangle(self.points, i1, i0, pts_index))

                    ab = self.edge_tuple(i0, i1)
                    if ab not in triangle_edges:
                        triangle_edges[ab] = []
                    triangle_edges[ab].append(tl)

                    ab = self.edge_tuple(i1, pts_index)
                    if ab not in triangle_edges:
                        triangle_edges[ab] = []
                    triangle_edges[ab].append(tl)

                    ab = self.edge_tuple(pts_index, i0)
                    if ab not in triangle_edges:
                        triangle_edges[ab] = []
                    triangle_edges[ab].append(tl)

                    # Add in reverse order.
                    succ = node.succ
                    pred = node.pred

                    if not succ:
                        succ = hull[0]
                    if not pred:
                        pred = hull[-1]

                    if succ.value == (i1, pts_index):
                        hull.remove(succ)
                    else:
                        hull.insert_after(node, (pts_index, i1))
                        ix += 1

                    if pred.value == (pts_index, i0):
                        hull.remove(pred)
                        ix -= 1
                    else:
                        hull.insert_after(node, (i0, pts_index))
                        ix += 1

                    hull.remove(node)
                    ix -= 1
                ix += 1

        return triangles, triangle_edges