예제 #1
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]
예제 #2
0
def test_empty_after_remove():
    l = BiList()
    node = l.append(42)
    l.remove(node)
    assert len(l) == 0
    assert list(l) == []
예제 #3
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