def __init__(self, master, send_q, resp_q, floor_file, walls_file, is_test=True): self.master = master master.title("PathfinderSim Display Window") master.geometry("512x600") # Parse the input files self.floors = OBJModel(floor_file) self.walls = OBJModel(walls_file) self.floors.parse() self.walls.parse() self.walls_AABB = self.walls.model_AABB() # cache this self.floors_AABB = self.floors.model_AABB() # cached self.floor_polys = [] self.floors_seen = [False] * self.floors.get_prim_count() self.floors_id = [] # Intercept the destroy event so we can shutdown gracefully master.bind("<Destroy>", self.on_destroy) self.is_test = is_test if is_test: master.bind('<Left>', lambda x: self.cmd_turn_car(self.car_orn - .1)) master.bind('<Right>', lambda x: self.cmd_turn_car(self.car_orn + .1)) master.bind('<Up>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), 5)))) master.bind('<Down>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), -5)))) self.canvas = Canvas(master, width=512, height=512) self.canvas.pack(fill="both", expand=True) self.canvas.bind('<Configure>', self.on_resize) self.width = self.canvas.winfo_width() self.height = self.canvas.winfo_height() self.car_pos = [100, 100] self.car_orn = 0 self.car_rays = 12 self.ray_dtheta = 2.*math.pi/self.car_rays # Async comms to class in separate thread self.command_q = send_q self.response_q = resp_q self.shutdown_flag = False self.button = Button(master, text="Quit", command=self.shutdown) self.button.pack() self.draw_map() self.car = self.build_car(self.car_pos, 12)
def build_grid(self): scale = self.obj.scale dims = self.obj.dims centre = m2d.mul(compute_centre(self.bound), dims[0]) print("Map Dims:", scale, dims, centre) prim_count = self.obj.get_prim_count() min_dims = m2d.mul(m2d.sub(self.bound[PB][:-1], self.bound[PA][:-1]), dims[0]) print(min_dims) tile_dims = [] ss_offset = [dims[0] / 2, dims[1] / 2] # Screen space offset ss_scale = [1, -1] # Flip y axis on screen # Find minimum dimensions for floor in range(prim_count): if floor % 2 == 1: continue # Skip odd numbered primitives (the other tri in the quad) prim = self.obj.get_prim(floor) if len(prim) != 3: continue A = self.obj.get_position( prim[0])[:-1] # Left Bottom corner, truncate z coord B = self.obj.get_position(prim[1])[:-1] # Right Bottom corner C = self.obj.get_position(prim[2])[:-1] # Left Top corner lb = m2d.sub(m2d.cp_mul([A[X], A[Y]], dims), centre) rt = m2d.sub(m2d.cp_mul([B[X], C[Y]], dims), centre) # Move the polygon into screen-space for direct display by the Display Window slb = m2d.add(m2d.cp_mul(lb, ss_scale), ss_offset) srt = m2d.add(m2d.cp_mul(rt, ss_scale), ss_offset) self.poly_arr.append([slb, srt]) tile_delta = m2d.sub(rt, lb) print(floor, A, B, tile_delta) if tile_delta[X] < min_dims[X]: min_dims[X] = tile_delta[X] if tile_delta[Y] < min_dims[Y]: min_dims[Y] = tile_delta[Y] tile_dims.append(tile_delta) print(min_dims) self.min_dims = min_dims # Compute the greatest common divisor for each axis x_dims = list(map(lambda x: x[0], tile_dims)) y_dims = list(map(lambda x: x[1], tile_dims)) xmin = functools.reduce(lambda x, y: gcd(int(x), int(y)), x_dims) ymin = functools.reduce(lambda x, y: gcd(int(x), int(y)), y_dims) print( "X Axis GCD:", xmin, x_dims) # Seems to be 1 in most cases... will have to be by pixel print("Y Axis GCD:", ymin, y_dims) print("Polygons:", self.poly_arr)
def setup_window(self): from tkinter import Canvas self.master.bind('<Destroy>', self.on_destroy) if self.is_test: self.master.bind('<Left>', lambda x: self.cmd_turn_car(self.car_orn - .1)) self.master.bind('<Right>', lambda x: self.cmd_turn_car(self.car_orn + .1)) self.master.bind('<Up>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), 5)))) self.master.bind('<Down>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), -5)))) self.canvas = Canvas(self.master, width=512, height=512) self.canvas.pack(fill="both", expand=True) self.canvas.bind('<Configure>', self.on_resize) self.width = self.canvas.winfo_width() self.height = self.canvas.winfo_height() return True