def _orient_normals(self, k): print 'Orienting normals' # find pt with maximum z value index = np.argmax([pt.position[2] for pt in self.points]) root = self.points[index] if root.normal[2] > 0: root.normal *= -1 parents = {} heap = BinaryHeap() for pt in self.points: if pt == root: heap.insert(0, pt) parents[root] = root else: heap.insert(float('inf'), pt) while not heap.is_empty(): pt = heap.extract_min() if pt in parents: prev = parents[pt] else: prev = self.nearest_neighbors(pt, 1, parents.keys())[0] parents[pt] = prev if np.dot(prev.normal, pt.normal) < 0: pt.normal *= -1 neighbors = self.nearest_neighbors(pt, k) for pt2 in neighbors: if pt2 not in parents: old_dist = heap.get_key(pt2) dist = 1. - np.abs(np.dot(pt.normal, pt2.normal)) if dist < old_dist: parents[pt2] = pt heap.update_key(dist, pt2) return parents
def least_cost_path(graph, start, dest, cost): """Find and return a least cost path in graph from start vertex to dest vertex. Efficiency: If E is the number of edges, the run-time is O( E log(E) ). Args: graph (Graph): The digraph defining the edges between the vertices. start: The vertex where the path starts. It is assumed that start is a vertex of graph. dest: The vertex where the path ends. It is assumed that dest is a vertex of graph. cost: A class with a method called "distance" that takes as input an edge (a pair of vertices) and returns the cost of the edge. For more details, see the CostDistance class description below. Returns: list: A potentially empty list (if no path can be found) of the vertices in the graph. If there was a path, the first vertex is always start, the last is always dest in the list. Any two consecutive vertices correspond to some edge in graph. """ reached = {} # empty dictionary events = BinaryHeap() # empty heap events.insert((start, start), 0) # vertex s burns at time 0 while len(events) > 0: edge, time = events.popmin() if edge[1] not in reached: reached[edge[1]] = edge[0] for nbr in graph.neighbours(edge[1]): events.insert((edge[1], nbr), time + cost.distance( (edge[1], nbr))) # if the dest is not in reached, then no route was found if dest not in reached: return [] current = dest route = [current] # go through the reached vertices until we get back to start and append # each vertice that we "stop" at while current != start: current = reached[current] route.append(current) # reverse the list because we made a list that went from the dest to start route = route[::-1] return route
def main(): heap = BinaryHeap(5) heap.insert(2) print("Root node: {}".format(heap.root.data)) leaf = heap.getLeaf(heap.root) print("Leaf: {}".format(leaf.data))