def __init__(self, road_map, body): self.major_road_view = RoadView(road_map.major_road) self.minor_road_views = [ RoadView(minor_road) for minor_road in road_map.minor_roads ] if road_map.minor_roads is not None else list() if self.minor_road_views: self.clear_intersections = rendering.Compound([ rendering.make_polyline([ tuple(bounding_box.front_left), tuple(bounding_box.front_right) ]) for bounding_box in road_map.intersection_bounding_boxes ]) self.clear_intersections.set_color(*RGB.WHITE.value) self.intersection_markings = rendering.Compound([ rendering.make_polyline([ tuple(bounding_box.rear_left), tuple(bounding_box.rear_right) ]) for bounding_box in road_map.inbound_intersection_bounding_boxes ]) self.intersection_markings.add_attr( mods.FactoredLineStyle(0x0F0F, 2)) self.obstacle_view = None if road_map.obstacle is not None: self.obstacle_view = ObstacleView(road_map.obstacle, body)
def __init__(self, pelican_crossing, ego, **kwargs): super().__init__(**kwargs) coordinates = pelican_crossing.bounding_box() self.area = rendering.make_polygon(list(coordinates)) self.area.set_color(*RGB.WHITE.value) self.markings = rendering.Compound([ rendering.make_polyline([ tuple(pelican_crossing.outbound_intersection_bounding_box. rear_left), tuple(pelican_crossing.outbound_intersection_bounding_box. rear_right) ]), rendering.make_polyline([ tuple(pelican_crossing.inbound_intersection_bounding_box. front_left), tuple(pelican_crossing.inbound_intersection_bounding_box. front_right) ]), rendering.make_polyline([ tuple(pelican_crossing.static_bounding_box.rear_left), tuple(pelican_crossing.static_bounding_box.front_left) ]), rendering.make_polyline([ tuple(pelican_crossing.static_bounding_box.rear_right), tuple(pelican_crossing.static_bounding_box.front_right) ]) ]) offset_rear_right = Point( coordinates.rear_right.x + (pelican_crossing.constants.width * 0.15), coordinates.rear_right.y) offset_rear_left = Point( coordinates.rear_left.x + (pelican_crossing.constants.width * 0.15), coordinates.rear_left.y) offset_front_right = Point( coordinates.front_right.x - (pelican_crossing.constants.width * 0.15), coordinates.front_right.y) offset_front_left = Point( coordinates.front_left.x - (pelican_crossing.constants.width * 0.15), coordinates.front_left.y) self.inner = rendering.Compound([ rendering.make_polyline( [tuple(offset_rear_right), tuple(offset_rear_left)]), rendering.make_polyline( [tuple(offset_front_right), tuple(offset_front_left)]) ]) self.inner.add_attr(mods.FactoredLineStyle(0x0F0F, 1)) self.outbound_traffic_light_view = TrafficLightView( pelican_crossing.outbound_traffic_light, ego) self.inbound_traffic_light_view = TrafficLightView( pelican_crossing.inbound_traffic_light, ego)
def _make_door_open( # pylint: disable=unused-argument door: Door, ) -> rendering.Geom: pad = 0.8 geoms_frame_background = [ rendering.make_polygon( [(-1.0, -1.0), (-1.0, 1.0), (-pad, 1.0), (-pad, -1.0)], filled=True, ), rendering.make_polygon( [(pad, -1.0), (pad, 1.0), (1.0, 1.0), (1.0, -1.0)], filled=True, ), rendering.make_polygon( [(-1.0, -1.0), (-1.0, -pad), (1.0, -pad), (1.0, -1.0)], filled=True, ), rendering.make_polygon( [(-1.0, pad), (-1.0, 1.0), (1.0, 1.0), (1.0, pad)], filled=True, ), ] geom_frame_background = rendering.Compound(geoms_frame_background) geom_frame_background.set_color(*colormap[door.color]) geom_frame = rendering.make_polygon( [(-pad, -pad), (-pad, pad), (pad, pad), (pad, -pad)], filled=False, ) return Group([geom_frame_background, geom_frame])
def render(self, mode='human'): if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(500, 500) self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2) surface = rendering.Line(start=(-1.2, -0.05), end=(1.2, -0.05)) self.viewer.add_geom(surface) bob = rendering.make_circle(0.15, filled=True) bob.set_color(.8, .3, .2) attributes = rendering.Transform(translation=(0.0, 1.0)) bob.add_attr(attributes) rod = rendering.FilledPolygon([(-0.025, 0), (-0.025, 1.0 - 0.15), (0.025, 1.0 - 0.15), (0.025, 0)]) rod.set_color(0.2, 0.2, 0.7) pendulum = rendering.Compound([bob, rod]) pendulum.set_color(0.4, 0.5, 1) translate = rendering.Transform(translation=(0.0, -0.05)) pendulum.add_attr(translate) self.pole_transform = rendering.Transform() pendulum.add_attr(self.pole_transform) self.viewer.add_geom(pendulum) axle_fill = rendering.make_circle(radius=.1, res=30, filled=True) axle_fill.set_color(1, 1, 1) axle = rendering.make_circle(radius=0.1, res=30, filled=False) semi = rendering.Transform(translation=(0.0, -0.05)) axle_fill.add_attr(semi) axle.add_attr(semi) axle.set_color(0, 0, 0) self.viewer.add_geom(axle_fill) self.viewer.add_geom(axle) pivot = rendering.make_circle(0.02, filled=True) self.viewer.add_geom(pivot) hide = rendering.FilledPolygon([(-2.2, -0.07), (-2.2, -2.2), (2.2, -2.2), (2.2, -0.07)]) hide.set_color(1, 1, 1) self.viewer.add_geom(hide) fname = path.join(path.dirname(__file__), "clockwise.png") self.img = rendering.Image(fname, 0.5, 0.5) self.imgtrans = rendering.Transform() self.img.add_attr(self.imgtrans) self.viewer.add_onetime(self.img) self.pole_transform.set_rotation(self.state[0]) if self.action != 0: self.imgtrans.scale = (-self.action / 8, np.abs(self.action) / 8) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def __init__(self, road): self.area = rendering.make_polygon(list(road.static_bounding_box)) self.area.set_color(*RGB.WHITE.value) coordinates = road.bounding_box() self.edge_markings = rendering.Compound([ rendering.make_polyline( [tuple(coordinates.rear_left), tuple(coordinates.front_left)]), rendering.make_polyline([ tuple(coordinates.rear_right), tuple(coordinates.front_right) ]) ]) outbound_coordinates = road.outbound.bounding_box() self.centre_markings = rendering.make_polyline([ tuple(outbound_coordinates.rear_right), tuple(outbound_coordinates.front_right) ]) self.centre_markings.add_attr(mods.FactoredLineStyle(0x00FF, 2)) lane_lines = list() for lane in road.outbound.lanes[:-1] + road.inbound.lanes[:-1]: lane_coordinates = lane.bounding_box() lane_line = rendering.make_polyline([ tuple(lane_coordinates.rear_right), tuple(lane_coordinates.front_right) ]) lane_lines.append(lane_line) self.lane_markings = rendering.Compound(lane_lines) self.lane_markings.add_attr(mods.FactoredLineStyle(0x00FF, 2)) self.pelican_crossing_view = None self.bus_stop_views = list() for direction in [road.outbound, road.inbound]: if direction.bus_stop is not None: markings = rendering.make_polygon(list( direction.bus_stop.static_bounding_box), filled=False) self.bus_stop_views.append(markings)
def render(mode='human'): from gym.envs.classic_control import rendering viewer = rendering.Viewer(500, 500) viewer.set_bounds(-2.2, 2.2, -2.2, 2.2) surface = rendering.Line(start=(-1.2, -0.05), end=(1.2, -0.05)) viewer.add_geom(surface) hide = rendering.FilledPolygon([(-2.2, -0.05), (-2.2,-2.2), (2.2,-2.2), (2.2,0.05)]) hide.set_color(0.4,0.5,.9) #viewer.add_geom(hide) bob = rendering.make_circle(0.15,filled=True) bob.set_color(.8,.3,.2) attributes = rendering.Transform(translation=(0.0,1.0), rotation=np.pi/6) bob.add_attr(attributes) #viewer.add_geom(bob) #viewer.draw_circle(0.5,30,filled=True).set_color(0.1,0.4,.5) #viewer.draw_line(0,3) support = rendering.FilledPolygon([(0.15, 0.05), (0.15,-0.05), (-0.15, -0.05), (-0.15, 0.05)]) support.set_color(0.7,0.7,0.7) #viewer.add_geom(support) rod = rendering.FilledPolygon([(-0.025,0), (-0.025,1.0-0.15), (0.025,1.0 - 0.15), (0.025,0)]) rod.set_color(0.2, 0.2, 0.7) pendulum = rendering.Compound([bob, rod]) pendulum.set_color(0.4, 0.5, 1) translate = rendering.Transform(translation=(0.0,-0.05)) pendulum.add_attr(translate) viewer.add_geom(pendulum) axle_fill = rendering.make_circle(radius=.1,res=30,filled=True) axle_fill.set_color(1,1,1) axle = rendering.make_circle(radius=0.1, res=30, filled=False) semi = rendering.Transform(translation=(0.0, -0.05)) axle_fill.add_attr(semi) axle.add_attr(semi) axle.set_color(0, 0, 0) viewer.add_geom(axle_fill) viewer.add_geom(axle) pivot = rendering.make_circle(0.02, filled=True) viewer.add_geom(pivot) hide = rendering.FilledPolygon([(-2.2, -0.07), (-2.2, -2.2), (2.2, -2.2), (2.2, -0.07)]) hide.set_color(1, 1, 1) viewer.add_geom(hide) return viewer.render(return_rgb_array=mode == 'rgb_array')
def draw_filled_donut(Viewer, rout=10, rin=5, res=30, **attrs): points = [] for i in range(res+1): ang = 2*np.pi*i / res points.append((np.cos(ang)*rout, np.sin(ang)*rout)) for i in range(res+1): ang = 2*np.pi*i / res points.append((np.cos(ang)*rin, np.sin(ang)*rin)) geom = [] for i in range(res): geom.append(rendering.FilledPolygon([points[j] for j in [i,i+1,i+res+2,i+res+1]])) geom = rendering.Compound(geom) rendering._add_attrs(geom, attrs) Viewer.add_onetime(geom) return geom
def make_ant(length=1): l = length/4 leg1 = rendering.PolyLine([(l/2,0),(l/2,l),(-l*.5,3*l),(l/2,l)],True) leg2 = rendering.PolyLine([(l/2,0),(l/2,-l),(-l*1.5,-3*l),(l/2,-l)],True) leg3 = rendering.PolyLine([(l*1.5,0),(l*1.5,l/2),(l,2.5*l),(l*1.5,l/2)],True) leg4 = rendering.PolyLine([(l*1.5,0),(l*1.5,-l/2),(l*2,-2.5*l),(l*1.5,-l/2)],True) leg5 = rendering.PolyLine([(l*2.5,0),(l*2.5,l/2),(l*2.5,2*l),(l*2.5,l/2)],True) leg6 = rendering.PolyLine([(l*2.5,0),(l*2.5,-l/2),(l*3.5,-2*l),(l*2.5,-l/2)],True) circ0 = make_ellipse(2*l, l/1.5) circ1 = make_ellipse(l, l/2) circ1.add_attr(rendering.Transform(translation=(l+l/2, 0))) circ2 = make_ellipse(l, l) circ2.add_attr(rendering.Transform(translation=(2*l+l/2, 0))) geom = rendering.Compound([leg1, leg2, leg3, leg4, leg5, leg6, circ0, circ1, circ2]) geom.add_attr(rendering.Transform(translation=(-3*l, 0))) geom.add_attr(Flip(flipx=True)) return geom
def make_ant1(): # Ant using polygons ant_color = (0.5, 0.4, 0.9) body_radius = 7 # thorax (middle) body = rendering.make_circle(radius=body_radius, res=20, filled=True) # head head_pos = rendering.Transform(translation=(body_radius, 0)) head = rendering.make_circle(radius=body_radius * 0.9, res=20, filled=True) head.add_attr(head_pos) # abdomen (back) parts = [body] ant = rendering.Compound(parts) ant.set_color(*ant_color) return ant
def make_arrow(start, end): translation = start rotation = arctan2(end[1] - start[1], end[0] - start[0]) l = np.linalg.norm(np.array(end) - np.array(start)) line = rendering.make_polyline(((0, 0), (l, 0))) line.set_linewidth(2) line.add_attr(rendering.Transform(translation, rotation)) x = l / 6 * cos(pi / 6) y = l / 6 * sin(pi / 6) line_u = rendering.make_polyline(((l, 0), (l - x, y))) line_d = rendering.make_polyline(((l, 0), (l - x, -y))) line_u.set_linewidth(2) line_d.set_linewidth(2) line_u.add_attr(rendering.Transform(translation, rotation)) line_d.add_attr(rendering.Transform(translation, rotation)) geom = rendering.Compound([line, line_u, line_d]) return geom
def render(self, mode='human'): screen_width = 600 screen_height = 400 world_width = self.x_threshold * 2 scale = screen_width / world_width carty = 100 # TOP OF CART polewidth = 10.0 polelen = scale * (2 * self.length) cartwidth = 100.0 cartheight = 30.0 if self.viewer is None: from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) axleoffset = cartheight / 4.0 l, r, t, b = -cartwidth / 2, cartwidth / 2, cartheight / 2, -cartheight / 2 box = rendering.make_polygon([(l, b), (l, t), (r, t), (r, b)]) circ0 = rendering.make_circle(cartheight / 2) circ1 = rendering.make_circle(cartheight / 2) circ0.add_attr(rendering.Transform(translation=(cartwidth / 2, 0))) circ1.add_attr(rendering.Transform(translation=(-cartwidth / 2, 0))) cart = rendering.Compound([box, circ0, circ1]) #cart = rendering.make_capsule(cartwidth, cartheight) self.carttrans = rendering.Transform() cart.set_color(.38, .83, .68) cart.add_attr(self.carttrans) self.viewer.add_geom(cart) l, r, t, b = -polewidth / 2, polewidth / 2, polelen - polewidth / 2, -polewidth / 2 pole = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) pole.set_color(1, .87, .45) self.poletrans = rendering.Transform(translation=(0, axleoffset)) pole.add_attr(self.poletrans) pole.add_attr(self.carttrans) self.viewer.add_geom(pole) self.axle = rendering.make_circle(polewidth / 2) self.axle.add_attr(self.poletrans) self.axle.add_attr(self.carttrans) self.axle.set_color(.53, .13, .47) self.viewer.add_geom(self.axle) self.track = rendering.Line((0, carty), (screen_width, carty)) self.track.set_color(0, 0, 0) self.viewer.add_geom(self.track) self._pole_geom = pole if self.state is None: return None # Edit the pole polygon vertex pole = self._pole_geom l, r, t, b = -polewidth / 2, polewidth / 2, polelen - polewidth / 2, -polewidth / 2 pole.v = [(l, b), (l, t), (r, t), (r, b)] x = self.state cartx = x[0] * scale + screen_width / 2.0 # MIDDLE OF CART self.carttrans.set_translation(cartx, carty) self.poletrans.set_rotation(-x[2]) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def _render_old(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return screen_width = 600 screen_height = 400 view_wide = 15 view_tall = (screen_height / float(screen_width)) * view_wide if self.viewer is None: # set up viewer from gym.envs.classic_control import rendering self.viewer = rendering.Viewer(screen_width, screen_height) # draw terrain self.terrain_polyline = rendering.make_polyline( zip(self.terrain.x, self.terrain.z)) self.terrain_polyline.set_linewidth(2) self.viewer.add_geom(self.terrain_polyline) # draw quad l, r, t, b = -ARM_LENGTH, ARM_LENGTH, ARM_LENGTH / 8, -ARM_LENGTH / 8 quad_polygon = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]) self.quadtrans = rendering.Transform() # draw arrows to represent motor thrust arrow_xy = [(0, 0), (0, 1), (-0.2, 0.8), (0, 1), (0.2, 0.8)] m0_arrow = rendering.make_polyline(arrow_xy) m0_arrow.set_color(0, 0.8, 0.4) self.m0_trans = rendering.Transform() m0_arrow.add_attr(self.m0_trans) m1_arrow = rendering.make_polyline(arrow_xy) m1_arrow.set_color(0, 0.8, 0.4) self.m1_trans = rendering.Transform() m1_arrow.add_attr(self.m1_trans) quad_group = rendering.Compound([quad_polygon, m0_arrow, m1_arrow]) quad_group.add_attr(self.quadtrans) self.viewer.add_geom(quad_group) # note: lidar rendering is done in "immediate mode" below # terrain has been re-randomized, update if self.terrain_dirty_flag: self.terrain_polyline.v = zip(self.terrain.x, self.terrain.z) self.terrain_dirty_flag = False # draw quad pos = self.dynamics.pos self.quadtrans.set_translation(pos[0], pos[1]) self.quadtrans.set_rotation(self.dynamics.th) thrusts = self.last_motors + GRAV / 2 self.m0_trans.set_scale(thrusts[0] / 9.8, thrusts[0] / 9.8) self.m0_trans.set_translation(-ARM_LENGTH, 0) self.m1_trans.set_scale(thrusts[1] / 9.8, thrusts[1] / 9.8) self.m1_trans.set_translation(ARM_LENGTH, 0) self.viewer.set_bounds(-view_wide / 2 + pos[0], view_wide / 2 + pos[0], -view_tall / 2 + pos[1], view_tall / 2 + pos[1]) # draw lidar rays for pt, hit, dist in self.last_lidar: if hit: self.viewer.draw_line(pos, hit, color=(1, 0, 0)) else: self.viewer.draw_line(pos, pt, color=(.5, .5, .5)) return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def make_markers(points, radius=3): return rendering.Compound( [mods.make_circle(*point, 3) for point in points])
def make_lights(self, position1, position2, state): return rendering.Compound([ mods.make_circle(*position1, self.scale[state]), mods.make_circle(*position2, self.scale[state]) ])
def make_key(key: Key) -> rendering.Geom: # pylint: disable=unused-argument # OUTLINE lw = 4 geom_bow_outline = rendering.make_circle(radius=0.4, res=6, filled=False) geom_bow_outline.add_attr(rendering.Transform(translation=(-0.3, 0.0))) geom_bow_outline.set_linewidth(lw) geom_blade_outline = make_capsule(0.6, 0.2, filled=False) for geom in geom_blade_outline.geoms: geom.set_linewidth(lw) geom_bit1_outline = make_capsule(0.3, 0.1, filled=False) geom_bit1_outline.add_attr( rendering.Transform(translation=(0.4, 0.0), rotation=math.pi / 2)) for geom in geom_bit1_outline.geoms: geom.set_linewidth(lw) geom_bit2_outline = make_capsule(0.3, 0.1, filled=False) geom_bit2_outline.add_attr( rendering.Transform(translation=(0.5, 0.0), rotation=math.pi / 2)) for geom in geom_bit2_outline.geoms: geom.set_linewidth(lw) geom_bit3_outline = make_capsule(0.3, 0.1, filled=False) geom_bit3_outline.add_attr( rendering.Transform(translation=(0.6, 0.0), rotation=math.pi / 2)) for geom in geom_bit3_outline.geoms: geom.set_linewidth(lw) geom_outline = Group([ geom_bow_outline, geom_blade_outline, geom_bit1_outline, geom_bit2_outline, geom_bit3_outline, ]) # BODY geom_bow = rendering.make_circle(radius=0.4, res=6, filled=True) geom_bow.add_attr(rendering.Transform(translation=(-0.3, 0.0))) geom_blade = rendering.make_capsule(0.6, 0.2) geom_bit1 = rendering.make_capsule(0.3, 0.1) geom_bit1.add_attr( rendering.Transform(translation=(0.4, 0.0), rotation=math.pi / 2)) geom_bit2 = rendering.make_capsule(0.3, 0.1) geom_bit2.add_attr( rendering.Transform(translation=(0.5, 0.0), rotation=math.pi / 2)) geom_bit3 = rendering.make_capsule(0.3, 0.1) geom_bit3.add_attr( rendering.Transform(translation=(0.6, 0.0), rotation=math.pi / 2)) geom = rendering.Compound( [geom_bow, geom_blade, geom_bit1, geom_bit2, geom_bit3]) geom.set_color(*colormap[key.color]) return Group([geom_outline, geom])