def region2svg(regionFile): """ Converts region file to svg This is from the deprecated regions file with slight changes for proper calculation of the size of the regions map """ fout= regionFile.replace(".regions", ".svg") rfi = regions.RegionFileInterface() rfi.readFile(regionFile) polyList = [] for region in rfi.regions: if region.name != "boundary": points = [(pt.x,-pt.y) for pt in region.getPoints()] poly = Polygon.Polygon(points) polyList.append(poly) else: width=region.size.width height=region.size.height #use boundary size for image size Polygon.IO.writeSVG(fout, polyList,width=width,height=height, fill_color=("(255,255,255)",)) # works better than width=width,height=height #Polygon.IO.writeSVG(fout, polyList, fill_color=("(255,255,255)",)) # works better than width=width,height=height return fout #return the file name
def loadSimpleSpec(self,text="", regionList=[], sensors=[], actuators=[], customs=[], adj=[], outputfile=""): """ Load a simple spec given by the arguments without reading from a spec file For Slurp region, sensors, actuators, customs are lists of strings representing props adj is a list of tuples [(region1,region2),...] """ if outputfile == "": logging.error("Need to specify output filename") return self.proj.compile_options['decompose'] = False self.proj.project_root = os.path.abspath(os.path.dirname(os.path.expanduser(outputfile))) self.proj.project_basename, ext = os.path.splitext(os.path.basename(outputfile)) self.proj.specText=text # construct a list of region objects with given names self.proj.rfi = regions.RegionFileInterface() for rname in regionList: self.proj.rfi.regions.append(regions.Region(name=rname)) self.proj.enabled_sensors = sensors self.proj.enabled_actuators = actuators self.proj.all_customs = customs # construct adjacency matrix self.proj.rfi.transitions= [[[] for j in range(len(self.proj.rfi.regions))] for i in range(len(self.proj.rfi.regions))] for tran in adj: idx0 = self.proj.rfi.indexOfRegionWithName(tran[0]) idx1 = self.proj.rfi.indexOfRegionWithName(tran[1]) self.proj.rfi.transitions[idx0][idx1] = [(0,0)] # fake trans face self.proj.rfi.transitions[idx1][idx0] = [(0,0)]
def loadRegionFile(self, decomposed=False): """ Returns a Region File Interface object corresponding to the regions file referenced in the spec file """ #### Load in the region file if decomposed: regf_name = self.getFilenamePrefix() + "_decomposed.regions" else: try: regf_name = os.path.join( self.project_root, self.spec_data['SETTINGS']['RegionFile'][0]) except (IndexError, KeyError): logging.warning("Region file undefined") return None logging.info("Loading region file %s..." % regf_name) rfi = regions.RegionFileInterface() if not rfi.readFile(regf_name): if not self.silent: logging.error("Could not load region file %s!" % regf_name) if decomposed: logging.error( "Are you sure you compiled your specification?") return None logging.info("Found definitions for %d regions." % len(rfi.regions)) return rfi
def gen_world_regions(region_dict, to_remove_faces, output_file): region_obj_list = [] map_polygon = Polygon.Polygon() region_obj_list, map_polygon = create_regions(region_dict, map_polygon=map_polygon) # add boundary obstacles_and_boundary_regions = make_boundary_and_obstacles( region_obj_list) interface = regions.RegionFileInterface(regions=region_obj_list + obstacles_and_boundary_regions) # here we also want to remove the lanes in the middle #offset_faces = get_offset_faces(to_remove_faces) #interface.recalcAdjacency(offset_faces) interface.recalcAdjacency() interface.writeFile(output_file) return region_obj_list
def loadRegionFile(self, filename): self.proj.rfi = regions.RegionFileInterface() self.proj.rfi.readFile(filename) self.Bind(wx.EVT_SIZE, self.onResize, self) self.onResize()
ltlmop_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..") sys.path.append(os.path.join(ltlmop_root, "lib")) # load in the LTLMoP library import regions # load in topo file print "Loading '{}'...".format(sys.argv[1]) with open(sys.argv[1]) as f: data = f.read() data = json.loads(data) # create LTLMoP regions rfi = regions.RegionFileInterface() for rn in data["region_names"]: newRegion = regions.Region(name=rn) rfi.regions.append(newRegion) # force topology (we avoid LTLMoP's automatic coincident edge detection) rfi.transitions = [[[] for j in range(len(rfi.regions))] for i in range(len(rfi.regions))] for path in data["adjacencies"]: r1 = rfi.indexOfRegionWithName(path[0]) r2 = rfi.indexOfRegionWithName(path[1]) fake_face = [frozenset((regions.Point(1, 2), regions.Point(3, 4)))] rfi.transitions[r1][r2] = fake_face rfi.transitions[r2][r1] = fake_face
(p, t) = os.path.split(p) if p == "": print "I have no idea where I am; this is ridiculous" sys.exit(1) sys.path.append(os.path.join(p, "src", "lib")) import regions # Check that we have been passed an input file if len(sys.argv) != 2: print "Usage: {} [input_region_file]".format(sys.argv[0]) sys.exit(-1) # Load in map file print "Loading '{}'...".format(sys.argv[1]) mapfile = regions.RegionFileInterface() mapfile.readFile(sys.argv[1]) # Make all regions relative to (0,0), so they effectively store absolute coordinates for region in mapfile.regions: region.pointArray = map(lambda pt: region.position+pt, region.pointArray) region.position = regions.Point(0,0) # Calculate transformation matrix # Flip over y-axis, scale up to make width be 1000, and topleft be (100,100) leftMargin, topMargin, rightExtent, downExtent = mapfile.getBoundingBox() scale_factor = 1000.0/rightExtent offset = regions.Point(100,100) - regions.Point(leftMargin, -(topMargin+downExtent))*scale_factor T = matrix([[scale_factor, 0, offset.x], [0, -scale_factor, offset.y], [0, 0, 1]])
def region2svg(regionFile, png_file): """ Converts region file to svg This is from the deprecated regions file with slight changes for proper calculation of the size of the regions map """ rfi = regions.RegionFileInterface() rfi.readFile(regionFile) fout_list = [] two_region_list = itertools.combinations(rfi.regions, 2) for (region_1, region_2) in two_region_list: if not (region_1.name == 'boundary' or region_2.name == 'boundary' \ or region_1.isObstacle or region_2.isObstacle) \ and rfi.transitions[rfi.regions.index(region_1)][rfi.regions.index(region_2)]: #adjacent region print region_1, region_2 fout= png_file.replace(".png","_"+region_1.name+"_"+region_2.name+".svg") # combine two regions next to each other region_1_poly = Polygon.Polygon([(pt.x,-pt.y) for pt in region_1.getPoints()]) region_2_poly = Polygon.Polygon([(pt.x,-pt.y) for pt in region_2.getPoints()]) two_region_poly = region_1_poly + region_2_poly polyList = [two_region_poly] width = 0 height = 0 for region in rfi.regions: width = max(width, *[pt.x for pt in region.getPoints()]) height = max(height, *[pt.y for pt in region.getPoints()]) if region.name != "boundary" and region.name != region_1.name and region.name != region_2.name: points = [(pt.x,-pt.y) for pt in region.getPoints()] poly = Polygon.Polygon(points) polyList.append(poly) bound_zero = Polygon.Polygon([(0,0),(1, 0), (1, -1), (0, -1)]) bound_width = Polygon.Polygon([(width,0),(width-1, 0), (width-1, -1), (width, -1)]) bound_height = Polygon.Polygon([(0,-height),(-1, -height), (-1, -height+1), (0, -height+1)]) bound_all = Polygon.Polygon([(width,-height),(width-1, -height), (width-1, -height+1), (width, -height+1)]) polyList.extend([bound_zero, bound_width, bound_height, bound_all]) #use boundary size for image size Polygon.IO.writeSVG(fout, polyList, width=width,height=height, fill_color=("(255,255,255)",)) fout_list.append(fout) #return the file name # original only polyList = [] fout= png_file.replace(".png", ".svg") width = 0 height = 0 for region in rfi.regions: width = max(width, *[pt.x for pt in region.getPoints()]) height = max(height, *[pt.y for pt in region.getPoints()]) if region.name != "boundary": points = [(pt.x,-pt.y) for pt in region.getPoints()] poly = Polygon.Polygon(points) polyList.append(poly) bound_zero = Polygon.Polygon([(0,0),(1, 0), (1, -1), (0, -1)]) bound_width = Polygon.Polygon([(width,0),(width-1, 0), (width-1, -1), (width, -1)]) bound_height = Polygon.Polygon([(0,-height),(-1, -height), (-1, -height+1), (0, -height+1)]) bound_all = Polygon.Polygon([(width,-height),(width-1, -height), (width-1, -height+1), (width, -height+1)]) polyList.extend([bound_zero, bound_width, bound_height, bound_all]) #use boundary size for image size Polygon.IO.writeSVG(fout, polyList, width=width,height=height, fill_color=("(255,255,255)",)) fout_list.append(fout) #return the file name return fout_list