def init(): # inserting a constraint dt = exact_delaunay.Triangulation() pnts = [[0, 0], [5, 0], [10, 0], [5, 5], [3, 0], [6, 2], [12, 4]] for pnt in pnts: dt.add_node(x=pnt) return dt
def test_basic1(): plot = False dt = exact_delaunay.Triangulation() pnts = [[0, 0], [5, 0], [10, 0], [5, 5]] dt.add_node(x=pnts[0]) # This tests insert into empty dt.add_node(x=pnts[1]) # adjacent_vertex dt.add_node(x=pnts[2]) # adjacent_vertex dt.add_node(x=pnts[3]) # adjacent_edge dt.add_node(x=[3, 0]) # colinear if plot: plt.clf() dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5) # This where topo sort is failing plt.pause(0.1) n = dt.add_node(x=[6, 2]) # into cell interior if plot: plt.clf() dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_move1(): plot = False dt = exact_delaunay.Triangulation() pnts = [[0, 0], [5, 0], [10, 0], [5, 5]] dt.add_node(x=pnts[0]) # This tests insert into empty dt.add_node(x=pnts[1]) # adjacent_vertex dt.add_node(x=pnts[2]) # adjacent_vertex dt.add_node(x=pnts[3]) # adjacent_edge dt.add_node(x=[3, 0]) # colinear if plot: plt.clf() dt.plot_nodes() dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5) n = dt.add_node(x=[6, 2]) # into cell interior dt.modify_node(4, x=[3.01, 0.25]) dt.modify_node(4, x=[3.01, -0.25]) if plot: plt.clf() dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5) dt.check_global_delaunay() return dt
def test_delete3(): plot = False dt = exact_delaunay.Triangulation() nodes = [dt.add_node(x=[10, i]) for i in range(10)] dt.delete_node(nodes[0]) dt.delete_node(nodes[4]) if plot: plt.cla() dt.plot_nodes() dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_test_dim_down(): dt = exact_delaunay.Triangulation() n = dt.add_node(x=[0, 0]) for i in range(5): other = dt.add_node(x=[10, i]) assert dt.test_delete_node_dim_down(n) assert not dt.test_delete_node_dim_down(other) friend = dt.add_node(x=[0, 2]) assert not dt.test_delete_node_dim_down(friend) assert not dt.test_delete_node_dim_down(n)
def test_flip2(): plot = False dt = exact_delaunay.Triangulation() dt.add_node(x=[0, 0]) for i in range(5): dt.add_node(x=[10, i]) # This one requires a flip: n = dt.add_node(x=[5, 1]) if plot: plt.cla() dt.plot_nodes() dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_constraints_dim1(): dt = exact_delaunay.Triangulation() pnts = [[0, 0], [5, 0], [10, 0]] for pnt in pnts: dt.add_node(x=pnt) dt.add_constraint(0, 1) dt.add_constraint(1, 2) dt.remove_constraint(0, 1) dt.remove_constraint(1, 2) try: dt.add_constraint(0, 2) assert False except dt.ConstraintCollinearNode: pass #
def test_flip1(): plot = False dt = exact_delaunay.Triangulation() pnts = [[0, 0], [8, 0], [10, 5], [5, 5]] dt.add_node(x=pnts[0]) # This tests insert into empty dt.add_node(x=pnts[1]) # adjacent_vertex dt.add_node(x=pnts[2]) # adjacent_vertex dt.add_node(x=pnts[3]) # adjacent_edge dt.add_node(x=[3, 0]) # colinear if plot: plt.clf() dt.plot_nodes() dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_fuzz1(): plot = False # Fuzzing, regular x = np.arange(5) y = np.arange(5) X, Y = np.meshgrid(x, y) xys = np.array([X.ravel(), Y.ravel()]).T if plot: plt.figure(1).clf() fig, ax = plt.subplots(num=1) ax.plot(xys[:, 0], xys[:, 1], 'go', alpha=0.4) ax.axis([-1, 5, -1, 5]) idxs = np.zeros(len(xys), 'i8') - 1 dt = exact_delaunay.Triangulation() # definitely slows down as the number of nodes gets larger. # starting off with <1s per 100 operations, later more like 2s for repeat in range(1): print "Repeat: ", repeat for step in range(1000): if step % 200 == 0: print " step: ", step toggle = np.random.randint(len(idxs)) if idxs[toggle] < 0: idxs[toggle] = dt.add_node(x=xys[toggle]) else: dt.delete_node(idxs[toggle]) idxs[toggle] = -1 if plot: del ax.lines[1:] ax.texts = [] ax.collections = [] dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=7, facecolor='#ddddff', edgecolor='w', zorder=-5) plt.draw()
def test_delete4(): plot = False dt = exact_delaunay.Triangulation() dt.add_node(x=[0, 0]) dt.add_node(x=[5, 0]) dt.add_node(x=[10, 0]) dt.add_node(x=[-2, 3]) dt.add_node(x=[-2, -3]) # and now delete the one in the middle: dt.delete_node(1) if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=7, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_delete2(): plot = False dt = exact_delaunay.Triangulation() # ExactDelaunay() dt.add_node(x=[0, 0]) for i in range(5): if 0: # sparser nodes for testing if i > 0 and i < 4: continue dt.add_node(x=[10, i]) # This one requires a flip: n = dt.add_node(x=[5, 1]) far = dt.add_node(x=[12, 4]) dt.delete_node(0) if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nr: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=6, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_flip2(): plot = False # testing Delaunay flipping dt = exact_delaunay.Triangulation() # ExactDelaunay() dt.add_node(x=[0, 0]) for i in range(10): dt.add_node(x=[10, i]) dt.add_node(x=[5, 1.0]) dt.add_node(x=[10, 10]) dt.add_node(x=[5, 2]) # and test a flip when the new vertex is outside the convex hull dt.add_node(x=[5, -1]) dt.delete_node(11) if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nrec: str(n)) dt.plot_edges(alpha=0.5, lw=2) dt.plot_cells(lw=13, facecolor='#ddddff', edgecolor='w', zorder=-5)
def test_delete1(): plot = False dt = exact_delaunay.Triangulation() # ExactDelaunay() dt.add_node(x=[0, 0]) for i in range(5): dt.add_node(x=[10, i]) # This one requires a flip: n = dt.add_node(x=[5, 1]) if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nr: str(n)) dt.plot_edges(alpha=0.5, lw=2) bad = (~dt.cells['deleted']) & (dt.cells_area() <= 0) good = (~dt.cells['deleted']) & (dt.cells_area() > 0) dt.plot_cells(lw=8, facecolor='#ddddff', edgecolor='w', zorder=-5, mask=good) dt.plot_cells(lw=8, facecolor='#ffdddd', edgecolor='r', zorder=-5, mask=bad) # deleting n work okay dt.delete_node(n) if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nr: str(n)) dt.plot_edges(alpha=0.5, lw=2) bad = (~dt.cells['deleted']) & (dt.cells_area() <= 0) good = (~dt.cells['deleted']) & (dt.cells_area() > 0) dt.plot_cells(lw=8, facecolor='#ddddff', edgecolor='w', zorder=-5, mask=good) dt.plot_cells(lw=8, facecolor='#ffdddd', edgecolor='r', zorder=-5, mask=bad) # delete a node which defines part of the convex hull: dt.delete_node(5) # works, though may need to patch up edges['cells'] ? dt.delete_node(2) # collinear portion of the convex hull dt.delete_node(3) # everything looks okay. Surprising that we didn't have to # add code to deal with this case... if plot: plt.cla() dt.plot_nodes(labeler=lambda n, nr: str(n)) dt.plot_edges(alpha=0.5, lw=2) bad = (~dt.cells['deleted']) & (dt.cells_area() <= 0) good = (~dt.cells['deleted']) & (dt.cells_area() > 0) dt.plot_cells(lw=8, facecolor='#ddddff', edgecolor='w', zorder=-5, mask=good) dt.plot_cells(lw=8, facecolor='#ffdddd', edgecolor='r', zorder=-5, mask=bad) dt.delete_node(0) if plot: assert dt.dim() == 1 plt.cla() dt.plot_nodes(labeler=lambda n, nr: str(n)) dt.plot_edges(alpha=0.5, lw=2) bad = (~dt.cells['deleted']) & (dt.cells_area() <= 0) good = (~dt.cells['deleted']) & (dt.cells_area() > 0) dt.plot_cells(lw=8, facecolor='#ddddff', edgecolor='w', zorder=-5, mask=good) dt.plot_cells(lw=8, facecolor='#ffdddd', edgecolor='r', zorder=-5, mask=bad)
poly = sources['geom'][39] buff = 200 xyz_sub = xyz.clip_to_polygon(poly.buffer(buff)) ## dem1 = xyz_sub.to_grid(dx=5, dy=10, interp='linear') ## # That's okay, although not that great. # 1st step: build a triangulation, embed the polygon as # constrained edges, see how that looks. cdt = exact_delaunay.Triangulation(extra_node_fields=[('value', np.float64)]) cdt.bulk_init(xyz_sub.X) cdt.nodes['value'] = xyz_sub.F # Add in the nodes and edges of the polygon: poly_pnts = np.array(poly.exterior) if np.all(poly_pnts[-1] == poly_pnts[0]): poly_pnts = poly_pnts[:-1] new_values = xyz_sub.interpolate(poly_pnts, 'nearest') poly_nodes = [] for pnt, value in zip(poly_pnts, new_values): poly_nodes.append(cdt.add_node(x=pnt, value=value)) from stompy import utils for a, b in utils.circular_pairs(poly_nodes):