def compute_fixed_expanding(self, x, y, cur_edge_id, length): result = [] stack = Stack() # is_node = self.get_next_node_id(x, y) > -1 stack.push(SegItem(x, y, -1, -1, length, cur_edge_id, is_node)) while stack.get_size() > 0: # DEBUG # stack.print_all() # print "END OF LIST" # item = stack.get() # if item.length == 0: # result.append(item) # continue # case 1, is_node == True if item.is_node == True: node_id = self.get_next_node_id(item.x, item.y) for end_node_id in self.adj[ node_id]: #traverse adjacent edges... edge_len = get_edge_length(self.nodes[node_id], self.nodes[end_node_id]) if edge_len < item.length: remaining_len = item.length - edge_len # result.append( EdgeSegment( self.nodes[node_id].x, self.nodes[node_id].y, self.nodes[end_node_id].x, self.nodes[end_node_id].y, self.node_pair_to_edge[(node_id, end_node_id)])) # for edge_id in self.node_to_edges[ end_node_id]: #one choice for each adjacent edge stack.push( SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * ( self.nodes[end_node_id].x - item.x) / edge_len end_y = item.y + item.length * ( self.nodes[end_node_id].y - item.y) / edge_len result.append( EdgeSegment( self.nodes[node_id].x, self.nodes[node_id].y, end_x, end_y, self.node_pair_to_edge[(node_id, end_node_id)])) # case 2, is_node == False else: for end_node_id in (self.edges[item.cur_edge_id].start_node_id, self.edges[item.cur_edge_id].end_node_id): segment_len = get_segment_length(self.nodes[end_node_id], item.x, item.y) if segment_len < item.length: remaining_len = item.length - segment_len # end_node_id.xy go first to comply with convention: first point always graph node !! result.append( EdgeSegment(self.nodes[end_node_id].x, self.nodes[end_node_id].y, item.x, item.y, item.cur_edge_id)) # for edge_id in self.node_to_edges[end_node_id]: stack.push( SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * ( self.nodes[end_node_id].x - item.x) / segment_len end_y = item.y + item.length * ( self.nodes[end_node_id].y - item.y) / segment_len result.append( EdgeSegment(item.x, item.y, end_x, end_y, item.cur_edge_id)) # DEBUG # print "stack.max_size", stack.max_size # # print "length(result) = ", length(result) # for item in result: # print "%15d %8.2f %10.2f %10.2f %10.2f %10.2f" % (item.cur_edge_id, EdgeSegment.length(item), \ # item.start_x, item.start_y, item.end_x, item.end_y) return result
def read_map(self, path, map_name): start = time.clock() # 1. NODES f = open(path + map_name + ".node", "rb") self.nodes = {} data = f.read() #print type(data[:20]) cur = 0 min_x, min_y = 1000000000, 1000000000 max_x, max_y = -1000000000, -1000000000 while cur < len(data): # node name node_name_len = ord(data[cur]) #print node_name_len node_name = data[cur + 1:cur + node_name_len + 1] #print node_name # node id (big endian) #print repr(data[cur+node_name_len+2 : cur+node_name_len+10]) node_id = struct.unpack( '>Q', data[cur + node_name_len + 1:cur + node_name_len + 9])[0] #print node_id # node x (big endian) node_x = struct.unpack( '>i', data[cur + node_name_len + 9:cur + node_name_len + 13])[0] #print node_x # node y (big endian) node_y = struct.unpack( '>i', data[cur + node_name_len + 13:cur + node_name_len + 17])[0] #print node_y cur = cur + node_name_len + 17 self.nodes[node_id] = Node(node_id, node_x, node_y) self.xy_to_node_id[(node_x, node_y)] = node_id #print node_id, node_x, node_y # if min_x > node_x: min_x = node_x if min_y > node_y: min_y = node_y if max_x < node_x: max_x = node_x if max_y < node_y: max_y = node_y f.close() print "min,max (X,Y)", min_x, min_y, max_x, max_y dx = max_x - min_x dy = max_y - min_y print "dx, dy :", dx, dy self.min_x = min_x self.min_y = min_y self.max_x = max_x self.max_y = max_y self.dx = dx self.dy = dy self.area = self.dx * self.dy print "map.area", self.area # 2. EDGES f = open(path + map_name + ".edge", "rb") self.edges = {} data = f.read() cur = 0 while cur < len(data): # start_node id (big endian) start_node_id = struct.unpack('>Q', data[cur:cur + 8])[0] #print start_node_id # end_node id (big endian) end_node_id = struct.unpack('>Q', data[cur + 8:cur + 16])[0] #print end_node_id # edge name edge_name_len = ord(data[cur + 16]) #print edge_name_len edge_name = data[cur + 17:cur + 17 + edge_name_len] #print "%c" % edge_name # node x (big endian) edge_id = struct.unpack( '>Q', data[cur + edge_name_len + 17:cur + edge_name_len + 25])[0] #print edge_id # edge_class (big endian) edge_class = struct.unpack( '>i', data[cur + edge_name_len + 25:cur + edge_name_len + 29])[0] #print edge_class #print start_node_id, end_node_id, edge_name_len, edge_name, edge_id, edge_class #print start_node_id, end_node_id cur = cur + edge_name_len + 29 # CHECK if start_node_id == end_node_id: # print "ERROR (start_node_id == end_node_id) at edge_id =", edge_id continue if (start_node_id, end_node_id) in self.node_pair_to_edge: # print "ERROR (duplicate edge) at edge_id =", edge_id continue self.edges[edge_id] = Edge(edge_id, start_node_id, end_node_id, edge_class) self.total_map_len += get_edge_length(self.nodes[start_node_id], self.nodes[end_node_id]) # try: self.node_to_edges[start_node_id].append(edge_id) except: self.node_to_edges[start_node_id] = [] self.node_to_edges[start_node_id].append(edge_id) # try: self.node_to_edges[end_node_id].append(edge_id) except: self.node_to_edges[end_node_id] = [] self.node_to_edges[end_node_id].append(edge_id) # try: self.adj[start_node_id].append(end_node_id) except: self.adj[start_node_id] = [] self.adj[start_node_id].append(end_node_id) try: self.adj[end_node_id].append(start_node_id) except: self.adj[end_node_id] = [] self.adj[end_node_id].append(start_node_id) # self.node_pair_to_edge[(start_node_id, end_node_id)] = edge_id self.node_pair_to_edge[(end_node_id, start_node_id)] = edge_id f.close() print "total_map_len =", self.total_map_len #timing elapsed = (time.clock() - start) print "Elapsed ", elapsed print len(self.nodes) print len(self.edges)
def compute_fixed_expanding(self, x, y, cur_edge_id, length): result = [] # list of EdgeSegment stack = Stack() # is_node = self.get_next_node_id(x, y) > -1 stack.push( SegItem(x, y, -1, -1, length, cur_edge_id, is_node)) while stack.get_size() > 0: # DEBUG # stack.print_all() # print "END OF LIST" # item = stack.get() # if item.length == 0: # result.append(item) # continue # case 1, is_node == True if item.is_node == True: node_id = self.get_next_node_id(item.x, item.y) for end_node_id in self.adj[node_id]: #traverse adjacent edges... edge_len = get_edge_length(self.nodes[node_id], self.nodes[end_node_id]) if edge_len < item.length: remaining_len = item.length - edge_len # result.append(EdgeSegment(self.nodes[node_id].x, self.nodes[node_id].y, self.nodes[end_node_id].x, self.nodes[end_node_id].y, self.node_pair_to_edge[(node_id, end_node_id)])) # for edge_id in self.node_to_edges[end_node_id]: #one choice for each adjacent edge stack.push(SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * (self.nodes[end_node_id].x - item.x) / edge_len end_y = item.y + item.length * (self.nodes[end_node_id].y - item.y) / edge_len result.append(EdgeSegment(self.nodes[node_id].x, self.nodes[node_id].y, end_x, end_y, self.node_pair_to_edge[(node_id, end_node_id)])) # case 2, is_node == False else: for end_node_id in (self.edges[item.cur_edge_id].start_node_id, self.edges[item.cur_edge_id].end_node_id): segment_len = get_segment_length(self.nodes[end_node_id], item.x, item.y) if segment_len < item.length: remaining_len = item.length - segment_len # end_node_id.xy go first to comply with convention: first point always graph node !! result.append(EdgeSegment(self.nodes[end_node_id].x, self.nodes[end_node_id].y, item.x, item.y, item.cur_edge_id)) # for edge_id in self.node_to_edges[end_node_id]: stack.push(SegItem(self.nodes[end_node_id].x, self.nodes[end_node_id].y, -1, -1, remaining_len, edge_id, True)) else: end_x = item.x + item.length * (self.nodes[end_node_id].x - item.x) / segment_len end_y = item.y + item.length * (self.nodes[end_node_id].y - item.y) / segment_len result.append(EdgeSegment(item.x, item.y, end_x, end_y, item.cur_edge_id)) # DEBUG # print "stack.max_size", stack.max_size # # print "length(result) = ", length(result) # for item in result: # print "%15d %8.2f %10.2f %10.2f %10.2f %10.2f" % (item.cur_edge_id, EdgeSegment.length(item), \ # item.start_x, item.start_y, item.end_x, item.end_y) return result
def read_map(self, path, map_name): start = time.clock() # 1. NODES f = open(path + map_name + ".node", "rb") self.nodes = {} data = f.read() #print type(data[:20]) cur = 0 min_x, min_y = 1000000000, 1000000000 max_x, max_y = -1000000000, -1000000000 while cur < len(data): # node name node_name_len = ord(data[cur]) #print node_name_len node_name = data[cur+1:cur+node_name_len+1] #print node_name # node id (big endian) #print repr(data[cur+node_name_len+2 : cur+node_name_len+10]) node_id = struct.unpack('>Q', data[cur+node_name_len+1 : cur+node_name_len+9])[0] #print node_id # node x (big endian) node_x = struct.unpack('>i', data[cur+node_name_len+9 : cur+node_name_len+13])[0] #print node_x # node y (big endian) node_y = struct.unpack('>i', data[cur+node_name_len+13 : cur+node_name_len+17])[0] #print node_y cur = cur+node_name_len+17 self.nodes[node_id] = Node(node_id, node_x, node_y) self.xy_to_node_id[(node_x, node_y)] = node_id #print node_id, node_x, node_y # if min_x > node_x: min_x = node_x if min_y > node_y: min_y = node_y if max_x < node_x: max_x = node_x if max_y < node_y: max_y = node_y f.close() print "min,max (X,Y)", min_x, min_y, max_x, max_y dx = max_x - min_x dy = max_y - min_y print "dx, dy :", dx, dy self.min_x = min_x self.min_y = min_y self.max_x = max_x self.max_y = max_y self.dx = dx self.dy = dy self.area = self.dx * self.dy print "map.area", self.area # 2. EDGES f = open(path + map_name + ".edge", "rb") self.edges = {} data = f.read() cur = 0 while cur < len(data): # start_node id (big endian) start_node_id = struct.unpack('>Q', data[cur : cur+8])[0] #print start_node_id # end_node id (big endian) end_node_id = struct.unpack('>Q', data[cur+8 : cur+16])[0] #print end_node_id # edge name edge_name_len = ord(data[cur+16]) #print edge_name_len edge_name = data[cur+17:cur+17+edge_name_len] #print "%c" % edge_name # node x (big endian) edge_id = struct.unpack('>Q', data[cur+edge_name_len+17 : cur+edge_name_len+25])[0] #print edge_id # edge_class (big endian) edge_class = struct.unpack('>i', data[cur+edge_name_len+25 : cur+edge_name_len+29])[0] #print edge_class #print start_node_id, end_node_id, edge_name_len, edge_name, edge_id, edge_class #print start_node_id, end_node_id cur = cur + edge_name_len + 29 # CHECK if start_node_id == end_node_id: # print "ERROR (start_node_id == end_node_id) at edge_id =", edge_id continue if (start_node_id, end_node_id) in self.node_pair_to_edge: # print "ERROR (duplicate edge) at edge_id =", edge_id continue self.edges[edge_id] = Edge(edge_id, start_node_id, end_node_id, edge_class) self.total_map_len += get_edge_length(self.nodes[start_node_id], self.nodes[end_node_id]) # try: self.node_to_edges[start_node_id].append(edge_id) except: self.node_to_edges[start_node_id]= [] self.node_to_edges[start_node_id].append(edge_id) # try: self.node_to_edges[end_node_id].append(edge_id) except: self.node_to_edges[end_node_id]= [] self.node_to_edges[end_node_id].append(edge_id) # try: self.adj[start_node_id].append(end_node_id) except: self.adj[start_node_id] = [] self.adj[start_node_id].append(end_node_id) try: self.adj[end_node_id].append(start_node_id) except: self.adj[end_node_id] = [] self.adj[end_node_id].append(start_node_id) # self.node_pair_to_edge[(start_node_id, end_node_id)] = edge_id self.node_pair_to_edge[(end_node_id, start_node_id)] = edge_id f.close() print "total_map_len =", self.total_map_len #timing elapsed = (time.clock() - start) print "Elapsed ", elapsed print "Map.nodes =", len(self.nodes) print "Map.edges =", len(self.edges)
def read_map(self, path, map_name): start = time.clock() # 1. NODES f = open(path + map_name + ".node", "rb") self.nodes = {} data = f.read() #print type(data[:20]) cur = 0 min_x, min_y = 1000000000, 1000000000 max_x, max_y = -1000000000, -1000000000 while cur < len(data): # node name node_name_len = ord(data[cur]) #print node_name_len node_name = data[cur + 1:cur + node_name_len + 1] #print node_name # node id (big endian) #print repr(data[cur+node_name_len+2 : cur+node_name_len+10]) node_id = struct.unpack( '>Q', data[cur + node_name_len + 1:cur + node_name_len + 9])[0] #print node_id # node x (big endian) node_x = struct.unpack( '>i', data[cur + node_name_len + 9:cur + node_name_len + 13])[0] #print node_x # node y (big endian) node_y = struct.unpack( '>i', data[cur + node_name_len + 13:cur + node_name_len + 17])[0] #print node_y cur = cur + node_name_len + 17 self.nodes[node_id] = Node(node_id, node_x, node_y) self.xy_to_node_id[(node_x, node_y)] = node_id #print node_id, node_x, node_y # if min_x > node_x: min_x = node_x if min_y > node_y: min_y = node_y if max_x < node_x: max_x = node_x if max_y < node_y: max_y = node_y f.close() print "min,max (X,Y)", min_x, min_y, max_x, max_y dx = max_x - min_x dy = max_y - min_y self.min_x = min_x self.min_y = min_y self.max_x = max_x self.max_y = max_y self.dx = dx self.dy = dy self.area = self.dx * self.dy print "map.area", self.area # 2. EDGES f = open(path + map_name + ".edge", "rb") self.edges = {} data = f.read() cur = 0 while cur < len(data): # start_node id (big endian) start_node_id = struct.unpack('>Q', data[cur:cur + 8])[0] #print start_node_id # end_node id (big endian) end_node_id = struct.unpack('>Q', data[cur + 8:cur + 16])[0] #print end_node_id # edge name edge_name_len = ord(data[cur + 16]) #print edge_name_len edge_name = data[cur + 17:cur + 17 + edge_name_len] #print "%c" % edge_name # node x (big endian) edge_id = struct.unpack( '>Q', data[cur + edge_name_len + 17:cur + edge_name_len + 25])[0] #print edge_id # edge_class (big endian) edge_class = struct.unpack( '>i', data[cur + edge_name_len + 25:cur + edge_name_len + 29])[0] #print edge_class #print start_node_id, end_node_id, edge_name_len, edge_name, edge_id, edge_class #print start_node_id, end_node_id cur = cur + edge_name_len + 29 self.edges[edge_id] = Edge(edge_id, start_node_id, end_node_id, edge_class) self.total_map_len += get_edge_length(self.nodes[start_node_id], self.nodes[end_node_id]) # try: self.node_to_edges[start_node_id].append(edge_id) except: self.node_to_edges[start_node_id] = [] self.node_to_edges[start_node_id].append(edge_id) # try: self.node_to_edges[end_node_id].append(edge_id) except: self.node_to_edges[end_node_id] = [] self.node_to_edges[end_node_id].append(edge_id) # try: self.adj[start_node_id].append(end_node_id) except: self.adj[start_node_id] = [] self.adj[start_node_id].append(end_node_id) try: self.adj[end_node_id].append(start_node_id) except: self.adj[end_node_id] = [] self.adj[end_node_id].append(start_node_id) # self.node_pair_to_edge[(start_node_id, end_node_id)] = edge_id self.node_pair_to_edge[(end_node_id, start_node_id)] = edge_id f.close() print "total_map_len =", self.total_map_len #timing elapsed = (time.clock() - start) print "Elapsed ", elapsed print len(self.nodes) print len(self.edges) # 3. Interval Trees (X,Y) features_x = [] features_y = [] for edge in self.edges.itervalues(): x1 = min(self.nodes[edge.start_node_id].x, self.nodes[edge.end_node_id].x) x2 = max(self.nodes[edge.start_node_id].x, self.nodes[edge.end_node_id].x) y1 = min(self.nodes[edge.start_node_id].y, self.nodes[edge.end_node_id].y) y2 = max(self.nodes[edge.start_node_id].y, self.nodes[edge.end_node_id].y) features_x.append([x1, x2, edge.edge_id]) features_y.append([y1, y2, edge.edge_id]) self.interval_tree_x = IntervalTree(features_x, 0, 1, self.min_x, self.max_x) self.interval_tree_y = IntervalTree(features_y, 0, 1, self.min_y, self.max_y) print "compute Interval Trees (X,Y): DONE"
def read_map(self, path, map_name): start = time.clock() # 1. NODES f = open(path + map_name + ".node", "rb") self.nodes = {} data = f.read() #print type(data[:20]) cur = 0 min_x, min_y = 1000000000, 1000000000 max_x, max_y = -1000000000, -1000000000 while cur < len(data): # node name node_name_len = ord(data[cur]) #print node_name_len node_name = data[cur+1:cur+node_name_len+1] #print node_name # node id (big endian) #print repr(data[cur+node_name_len+2 : cur+node_name_len+10]) node_id = struct.unpack('>Q', data[cur+node_name_len+1 : cur+node_name_len+9])[0] #print node_id # node x (big endian) node_x = struct.unpack('>i', data[cur+node_name_len+9 : cur+node_name_len+13])[0] #print node_x # node y (big endian) node_y = struct.unpack('>i', data[cur+node_name_len+13 : cur+node_name_len+17])[0] #print node_y cur = cur+node_name_len+17 self.nodes[node_id] = Node(node_id, node_x, node_y) self.xy_to_node_id[(node_x, node_y)] = node_id #print node_id, node_x, node_y # if min_x > node_x: min_x = node_x if min_y > node_y: min_y = node_y if max_x < node_x: max_x = node_x if max_y < node_y: max_y = node_y f.close() print "min,max (X,Y)", min_x, min_y, max_x, max_y dx = max_x - min_x dy = max_y - min_y self.min_x = min_x self.min_y = min_y self.max_x = max_x self.max_y = max_y self.dx = dx self.dy = dy self.area = self.dx * self.dy print "map.area", self.area # 2. EDGES f = open(path + map_name + ".edge", "rb") self.edges = {} data = f.read() cur = 0 while cur < len(data): # start_node id (big endian) start_node_id = struct.unpack('>Q', data[cur : cur+8])[0] #print start_node_id # end_node id (big endian) end_node_id = struct.unpack('>Q', data[cur+8 : cur+16])[0] #print end_node_id # edge name edge_name_len = ord(data[cur+16]) #print edge_name_len edge_name = data[cur+17:cur+17+edge_name_len] #print "%c" % edge_name # node x (big endian) edge_id = struct.unpack('>Q', data[cur+edge_name_len+17 : cur+edge_name_len+25])[0] #print edge_id # edge_class (big endian) edge_class = struct.unpack('>i', data[cur+edge_name_len+25 : cur+edge_name_len+29])[0] #print edge_class #print start_node_id, end_node_id, edge_name_len, edge_name, edge_id, edge_class #print start_node_id, end_node_id cur = cur + edge_name_len + 29 self.edges[edge_id] = Edge(edge_id, start_node_id, end_node_id, edge_class) self.total_map_len += get_edge_length(self.nodes[start_node_id], self.nodes[end_node_id]) # try: self.node_to_edges[start_node_id].append(edge_id) except: self.node_to_edges[start_node_id]= [] self.node_to_edges[start_node_id].append(edge_id) # try: self.node_to_edges[end_node_id].append(edge_id) except: self.node_to_edges[end_node_id]= [] self.node_to_edges[end_node_id].append(edge_id) # try: self.adj[start_node_id].append(end_node_id) except: self.adj[start_node_id] = [] self.adj[start_node_id].append(end_node_id) try: self.adj[end_node_id].append(start_node_id) except: self.adj[end_node_id] = [] self.adj[end_node_id].append(start_node_id) # self.node_pair_to_edge[(start_node_id, end_node_id)] = edge_id self.node_pair_to_edge[(end_node_id, start_node_id)] = edge_id f.close() print "total_map_len =", self.total_map_len #timing elapsed = (time.clock() - start) print "Elapsed ", elapsed print len(self.nodes) print len(self.edges) # 3. Interval Trees (X,Y) features_x = [] features_y = [] for edge in self.edges.itervalues(): x1 = min(self.nodes[edge.start_node_id].x, self.nodes[edge.end_node_id].x) x2 = max(self.nodes[edge.start_node_id].x, self.nodes[edge.end_node_id].x) y1 = min(self.nodes[edge.start_node_id].y, self.nodes[edge.end_node_id].y) y2 = max(self.nodes[edge.start_node_id].y, self.nodes[edge.end_node_id].y) features_x.append([x1, x2, edge.edge_id]) features_y.append([y1, y2, edge.edge_id]) self.interval_tree_x = IntervalTree(features_x, 0, 1, self.min_x, self.max_x) self.interval_tree_y = IntervalTree(features_y, 0, 1, self.min_y, self.max_y) print "compute Interval Trees (X,Y): DONE"