def join_split_tree(mesh, height_func, split=False): join_tree = DiGraph() # map of nodes to union-find set that it's in uf_map = {} # map of union-find set id to lowest node in the set. lowest_node_map = {} # a list of (height, node) tuples height_and_nodes = [(height_func(node), node) for node in mesh.nodes()] height_and_nodes.sort(reverse=not split) for h, n in height_and_nodes: uf_map[n] = set([n]) lowest_node_map[id(uf_map[n])] = n for nbr in mesh.neighbors_iter(n): nbr_h = height_func(nbr) if nbr_h == h: raise ValueError("nbr_h == h!!!") if (split and nbr_h > h) or (not split and nbr_h < h) or (uf_map[nbr] is uf_map[n]): continue # uf_merge() can change the uf_map[nbr], and hence, the lowest # point in nbr's union-find set. So we add the edge first before doing the # union-find merge. lowest_in_nbr_uf = lowest_node_map[id(uf_map[nbr])] join_tree.add_edge(lowest_in_nbr_uf, n) uf_merge(uf_map, n, nbr) lowest_node_map[id(uf_map[nbr])] = n return join_tree
def growth_tree(mesh, seed_pts): raise RuntimeError("I think I'm untested and unfinished, check to see...") growth_tree = DiGraph() regions = [(set([seed_pt]), set([seed_pt])) for seed_pt in seed_pts] last_region_pt = {} # pt2frontier = {} # for seed_pt in seed_pts: # pt2frontier[seed_pt] = set([seed_pt]) region_queue = heapq.heapify([(len(reg), reg, front) for reg,front in regions]) for tt in region_queue: _, reg, front = tt last_region_pt[id(reg)] = list(reg)[0] while len(region_queue) > 1: # get the smallest frontier from the frontier_queue rsize, region, frontier = heapq.heappop(region_queue) new_frontier = set() for pt in frontier: # add pt to growth_tree's edge. last_reg_pt = last_region_pt[id(region)] growth_tree.add_edge(last_region_pt, pt)
def contour_tree_from_join_split(join, split, height_func): c_tree = DiGraph() leaves = join_split_peak_pit_nodes(join) + join_split_peak_pit_nodes(split) while len(leaves) > 1: leaf = leaves.pop() if is_upper_leaf(leaf, join, split): this = join other = split else: this = split other = join this_succ = this.successors(leaf) assert len(this_succ) == 1 nbr = this_succ[0] if height_func(leaf) > height_func(nbr): c_tree.add_edge(leaf, nbr) else: c_tree.add_edge(nbr, leaf) reduce_graph(this, leaf) reduce_graph(other, leaf) if is_leaf(nbr, join, split): leaves.append(nbr) return c_tree