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