def _create_twins(self, site1, site2): half_edge = Hedge(None, None, site1, self._faces.get(site1, None)) half_edge._twin = Hedge(None, half_edge, site2, self._faces.get(site2, None)) self.edges.append(half_edge) self._faces[site1] = half_edge self._faces[site2] = half_edge._twin log('\t\tCreated new half-edge: %s' % half_edge) log('\t\t\t _faces = %s' % self._faces) return half_edge
def insert(self, item, pq, edge_list): """ Insert the specified item into the BST. """ print("insert") new_value = {'point': item, 'break_point': None, 'pointer': None} #print (value['point']) #We are only comparing the x value of the site to the breakpoint site_x = new_value['point'][0] site_y = new_value['point'][1] print(site_x) # Get the sort key for this value. #if self._sort_key is None: # sort_key = value #else: # sort_key = self._sort_key(value) # Walk down the tree until we find an empty node. node = self._root traversed = [] while node and node[_VALUE]['break_point'] is not None: #print node # Save parent node #parent = node traversed.append(node) #print("breakTEST: " + str(site_x < node[_SORT_KEY](node[_VALUE], site_y))) if site_x < node[_SORT_KEY](node[_VALUE], site_y): print("left: "), print(site_x < node[_SORT_KEY](node[_VALUE], site_y)) node = node[_LEFT] else: print("rightright") node = node[_RIGHT] #split the node if node == []: print("root") node[:] = [[], [], None, new_value, self._sort_key] return else: traversed.append(node) print(" split node") #print node #delete circle event from queue if node[_VALUE]['pointer'] is not None: pq.delete(node[_VALUE]['pointer']) node[_VALUE]['pointer'] = None left_value = node[_VALUE] # print ("left_value: " + str(left_value)) hedge1 = Hedge() hedge2 = Hedge() hedge1.twin = hedge2 hedge2.twin = hedge1 edge_list.hedges.append(hedge1) edge_list.hedges.append(hedge2) # face1 = Face() # face1.wedge = hedge1 #grand_parent = parent node[_VALUE] = { 'point': None, 'break_point': (left_value['point'], item), 'hedge': hedge1 } print("left_value: " + str(left_value)) parent = node node = node[_LEFT] node[:] = [[], [], parent, left_value, self._sort_key] # Save node for circle event check left1 = left_value['point'] left_node = node # print ("left_value1: " + str(left1[_VALUE])) # print ("left_value1: " + str(left1[_VALUE]['break_point'][0])) # print ("left_value1: " + str(left1)) print("parent_value: " + str(parent[_VALUE])) node = parent[_RIGHT] node[:] = [[], [], parent, { 'point': None, 'break_point': (item, left_value['point']), 'hedge': hedge2 }, self._sort_key] parent = node # print ("left_value: " + str(left_value)) node = node[_LEFT] node[:] = [[], [], parent, new_value, self._sort_key] # Save node for circle event check new_node = node node = parent[_RIGHT] node[:] = [[], [], parent, left_value, self._sort_key] # Save node for circle event check right1 = left1 right_node = node # Check for new circle events # there has to be a better way to find the triplet traversed_right = traversed[:] # find the leftmost arc of the triplet #print traversed n = traversed.pop() # print ("left_value1: " + str(left1)) # print ("new_node: " + str(new_node[_VALUE]['point'])) print("before left") print(n[_VALUE]['break_point'][0]) print(left_value['point']) while traversed and n[_VALUE]['break_point'][0] == left1: #print (n[_VALUE]['break_point'][0]) print("pop LEFT") n = traversed.pop() #pass if traversed: left2 = n[_VALUE]['break_point'][0] print("left_value2: " + str(left2)) print("left_value1: " + str(left1)) print("new_node: " + str(new_node[_VALUE]['point'])) circle_event = self._get_circle_event(left2, left1, new_node[_VALUE]['point'], site_y) if circle_event is not None: circle_event_site = (circle_event[0], (left_node)) left_node[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) #print (circle_event_site) left_node[_VALUE]['pointer'] = circle_event_site pq.add(circle_event_site) # find the rightmost arc of the triplet n = traversed_right.pop() # Get rid of the original splitted node if traversed_right: n = traversed_right.pop() print("before right") print(n[_VALUE]['break_point'][1]) print(left_value['point']) while traversed_right and n[_VALUE]['break_point'][1] == right1: n = traversed_right.pop() #pass #if traversed_right: if True: right2 = n[_VALUE]['break_point'][1] print("right_value2: " + str(right2)) print("right_value1: " + str(right1)) print("new_node: " + str(new_node[_VALUE]['point'])) # circle_event = self._get_circle_event(right2, right1, new_node) circle_event = self._get_circle_event(new_node[_VALUE]['point'], right1, right2, site_y) if circle_event is not None: circle_event_site = (circle_event[0], right_node) right_node[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) right_node[_VALUE]['pointer'] = circle_event_site pq.add(circle_event_site)
def handle_circle(site): print ("handle_circle: ") point = site[0] node = site[1] print(point) print(node[_VALUE]['break_point']) print(node[_VALUE]['point']) #remove possible circle events involving this site predecessor = beach_line.pred(node) successor = beach_line.succ(node) print("pre") print(predecessor[_VALUE]['point']) print(predecessor[_PARENT][_VALUE]['break_point']) print("endpre") print(successor[_VALUE]['point']) if predecessor[_VALUE]['pointer'] is not None: print("not None") #print(predecessor[_VALUE]['pointer']) sites.delete(predecessor[_VALUE]['pointer']) predecessor[_VALUE]['pointer'] = None print(predecessor[_PARENT][_VALUE]['break_point']) if successor[_VALUE]['pointer'] is not None: sites.delete(successor[_VALUE]['pointer']) successor[_VALUE]['pointer'] = None r = node[_VALUE]['radius'] #Add breakpoint to the vertex list ver = Vertex(point[0], point[1] - r) edge_list.vertices.append(ver) #Update beach_line and add pointers to hedges to new vertex parent = node[_PARENT] site = node[_VALUE]['point'] del node[:] new_hedge1 = Hedge() new_hedge2 = Hedge() new_hedge1.origin = ver ver.hedgelist.append(new_hedge1) new_hedge1.twin = new_hedge2 new_hedge2.twin = new_hedge1 edge_list.hedges.append(new_hedge1) edge_list.hedges.append(new_hedge2) right_hedge = parent[_VALUE]['hedge'] left_hedge = parent[_PARENT][_VALUE]['hedge'] if right_hedge.origin is None: right_hedge.origin = ver ver.hedgelist.append(right_hedge) else: right_hedge.twin.orig = ver ver.hedgelist.append(right_hedge.twin) if left_hedge.origin is None: left_hedge.origin = ver ver.hedgelist.append(left_hedge) else: left_hedge.twin.orig = ver ver.hedgelist.append(left_hedge.twin) #We always delete the parent node = parent parent = node[_PARENT] print("before delete" + str(predecessor[_PARENT][_VALUE]['break_point'])) #Deleted leaf was left child if node[_RIGHT]: new_right = node[_VALUE]['break_point'][1] node[:] = node[_RIGHT] node[_PARENT] = parent print("was right" + str(predecessor[_PARENT][_VALUE]['break_point'])) while parent[_VALUE]['break_point'][1] != site: parent = parent[_PARENT] bp_left = parent[_VALUE]['break_point'][0] parent[_VALUE]['break_point'] = (bp_left, new_right) else: new_left = node[_VALUE]['break_point'][0] print("was left" + str(predecessor[_PARENT][_VALUE]['break_point'])) node[:] = node[_LEFT] print("was left" + str(parent[_VALUE]['break_point'])) print("was left" + str(node[_VALUE]['point'])) print("was left" + str(node[_PARENT][_VALUE]['point'])) print("was left" + str(predecessor[_PARENT][_VALUE]['break_point'])) node[_PARENT] = parent predecessor[_PARENT] = parent print("was left" + str(node[_PARENT][_VALUE]['break_point'])) print("was left" + str(predecessor[_PARENT][_VALUE]['break_point'])) # skip original grandparent parent = parent[_PARENT] while parent[_VALUE]['break_point'][0] != site: parent = parent[_PARENT] bp_right = parent[_VALUE]['break_point'][1] parent[_VALUE]['break_point'] = (new_left, bp_right) #link new hedge to node parent[_VALUE]['hedge'] = new_hedge1 # check new arc triples # Former left in the middle print("left1" + str(predecessor[_PARENT][_VALUE]['break_point'])) left1 = beach_line.pred(predecessor) right1 = beach_line.succ(predecessor) circle_event = beach_line._get_circle_event(left1[_VALUE]['point'], predecessor[_VALUE]['point'], right1[_VALUE]['point'], site[1]) if circle_event is not None: circle_event_site = (circle_event[0], predecessor) predecessor[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) predecessor[_VALUE]['pointer'] = circle_event_site sites.add(circle_event_site) # Former right in the middle left2 = beach_line.pred(successor) right2 = beach_line.succ(successor) circle_event = beach_line._get_circle_event(left2[_VALUE]['point'], successor[_VALUE]['point'], right2[_VALUE]['point'], site[1]) if circle_event is not None: circle_event_site = (circle_event[0], successor) successor[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) successor[_VALUE]['pointer'] = circle_event_site sites.add(circle_event_site)
def insert(self, item, pq, edge_list): """ Insert the specified item into the BST. """ print ("insert") new_value = {'point': item, 'break_point': None, 'pointer': None} #print (value['point']) #We are only comparing the x value of the site to the breakpoint site_x = new_value['point'][0] site_y = new_value['point'][1] print (site_x) # Get the sort key for this value. #if self._sort_key is None: # sort_key = value #else: # sort_key = self._sort_key(value) # Walk down the tree until we find an empty node. node = self._root traversed = [] while node and node[_VALUE]['break_point'] is not None: #print node # Save parent node #parent = node traversed.append(node) #print("breakTEST: " + str(site_x < node[_SORT_KEY](node[_VALUE], site_y))) if site_x < node[_SORT_KEY](node[_VALUE], site_y): print("left: "), print (site_x < node[_SORT_KEY](node[_VALUE], site_y)) node = node[_LEFT] else: print("rightright") node = node[_RIGHT] #split the node if node == []: print ("root") node[:] = [[], [], None, new_value, self._sort_key] return else: traversed.append(node) print (" split node") #print node #delete circle event from queue if node[_VALUE]['pointer'] is not None: pq.delete(node[_VALUE]['pointer']) node[_VALUE]['pointer'] = None left_value = node[_VALUE] # print ("left_value: " + str(left_value)) hedge1 = Hedge() hedge2 = Hedge() hedge1.twin = hedge2 hedge2.twin = hedge1 edge_list.hedges.append(hedge1) edge_list.hedges.append(hedge2) # face1 = Face() # face1.wedge = hedge1 #grand_parent = parent node[_VALUE] = {'point': None, 'break_point' : (left_value['point'], item), 'hedge': hedge1} print ("left_value: " + str(left_value)) parent = node node = node[_LEFT] node[:] = [[], [], parent, left_value, self._sort_key] # Save node for circle event check left1 = left_value['point'] left_node = node # print ("left_value1: " + str(left1[_VALUE])) # print ("left_value1: " + str(left1[_VALUE]['break_point'][0])) # print ("left_value1: " + str(left1)) print ("parent_value: " + str(parent[_VALUE])) node = parent[_RIGHT] node[:] = [[], [], parent, {'point': None, 'break_point' : (item, left_value['point']), 'hedge': hedge2}, self._sort_key] parent = node # print ("left_value: " + str(left_value)) node = node[_LEFT] node[:] = [[], [], parent, new_value, self._sort_key] # Save node for circle event check new_node = node node = parent[_RIGHT] node[:] = [[], [], parent, left_value, self._sort_key] # Save node for circle event check right1 = left1 right_node = node # Check for new circle events # there has to be a better way to find the triplet traversed_right = traversed[:] # find the leftmost arc of the triplet #print traversed n = traversed.pop() # print ("left_value1: " + str(left1)) # print ("new_node: " + str(new_node[_VALUE]['point'])) print ("before left") print (n[_VALUE]['break_point'][0]) print (left_value['point']) while traversed and n[_VALUE]['break_point'][0] == left1: #print (n[_VALUE]['break_point'][0]) print("pop LEFT") n = traversed.pop() #pass if traversed: left2 = n[_VALUE]['break_point'][0] print ("left_value2: " + str(left2)) print ("left_value1: " + str(left1)) print ("new_node: " + str(new_node[_VALUE]['point'])) circle_event = self._get_circle_event(left2, left1, new_node[_VALUE]['point'], site_y) if circle_event is not None: circle_event_site = (circle_event[0], (left_node)) left_node[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) #print (circle_event_site) left_node[_VALUE]['pointer'] = circle_event_site pq.add(circle_event_site) # find the rightmost arc of the triplet n = traversed_right.pop() # Get rid of the original splitted node if traversed_right: n = traversed_right.pop() print ("before right") print (n[_VALUE]['break_point'][1]) print (left_value['point']) while traversed_right and n[_VALUE]['break_point'][1] == right1: n = traversed_right.pop() #pass #if traversed_right: if True: right2 = n[_VALUE]['break_point'][1] print ("right_value2: " + str(right2)) print ("right_value1: " + str(right1)) print ("new_node: " + str(new_node[_VALUE]['point'])) # circle_event = self._get_circle_event(right2, right1, new_node) circle_event = self._get_circle_event(new_node[_VALUE]['point'], right1, right2, site_y) if circle_event is not None: circle_event_site = (circle_event[0], right_node) right_node[_VALUE]['radius'] = circle_event[1] #print(type(circle_event_site)) right_node[_VALUE]['pointer'] = circle_event_site pq.add(circle_event_site)