Files
Gymnasium/gym/envs/classic_control/rendering.py

438 lines
12 KiB
Python
Raw Normal View History

2016-04-27 08:00:58 -07:00
"""
2D rendering framework
"""
import os
import sys
2016-04-27 08:00:58 -07:00
if "Apple" in sys.version:
2021-07-29 02:26:34 +02:00
if "DYLD_FALLBACK_LIBRARY_PATH" in os.environ:
os.environ["DYLD_FALLBACK_LIBRARY_PATH"] += ":/usr/lib"
2016-04-27 08:00:58 -07:00
# (JDS 2016/04/15): avoid bug on Anaconda 2.3.0 / Yosemite
from gym import error
try:
import pyglet
except ImportError as e:
2021-07-29 02:26:34 +02:00
raise ImportError(
"""
Cannot import pyglet.
HINT: you can install pyglet directly via 'pip install pyglet'.
But if you really just want to install all Gym dependencies and not have to think about it,
'pip install -e .[all]' or 'pip install gym[all]' will do it.
2021-07-29 02:26:34 +02:00
"""
)
2016-04-27 08:00:58 -07:00
try:
from pyglet.gl import *
except ImportError as e:
2021-07-29 02:26:34 +02:00
raise ImportError(
"""
2019-07-13 02:39:51 +05:30
Error occurred while running `from pyglet.gl import *`
2021-07-26 21:11:20 +01:00
HINT: make sure you have OpenGL installed. On Ubuntu, you can run 'apt-get install python-opengl'.
If you're running on a server, you may need a virtual frame buffer; something like this should work:
'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'
2021-07-29 02:26:34 +02:00
"""
)
2016-04-27 08:00:58 -07:00
import math
import numpy as np
RAD2DEG = 57.29577951308232
2021-07-29 02:26:34 +02:00
def get_display(spec):
"""Convert a display specification (such as :0) into an actual Display
object.
Pyglet only supports multiple Displays on Linux.
"""
if spec is None:
return pyglet.canvas.get_display()
# returns already available pyglet_display,
# if there is no pyglet display available then it creates one
elif isinstance(spec, str):
return pyglet.canvas.Display(spec)
else:
2021-07-29 12:42:48 -04:00
raise error.Error("Invalid display specification: {}. (Must be a string like :0 or None.)".format(spec))
2021-07-29 02:26:34 +02:00
def get_window(width, height, display, **kwargs):
"""
Will create a pyglet window from the display specification provided.
"""
2021-07-29 02:26:34 +02:00
screen = display.get_screens() # available screens
config = screen[0].get_best_config() # selecting the first screen
context = config.create_context(None) # create GL context
2021-07-29 12:42:48 -04:00
return pyglet.window.Window(width=width, height=height, display=display, config=config, context=context, **kwargs)
2016-04-27 08:00:58 -07:00
class Viewer(object):
def __init__(self, width, height, display=None):
display = get_display(display)
2016-04-27 08:00:58 -07:00
self.width = width
self.height = height
self.window = get_window(width=width, height=height, display=display)
self.window.on_close = self.window_closed_by_user
self.isopen = True
2016-04-27 08:00:58 -07:00
self.geoms = []
self.onetime_geoms = []
self.transform = Transform()
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
def close(self):
if self.isopen and sys.meta_path:
# ^^^ check sys.meta_path to avoid 'ImportError: sys.meta_path is None, Python is likely shutting down'
self.window.close()
self.isopen = False
2016-04-27 08:00:58 -07:00
def window_closed_by_user(self):
self.isopen = False
2016-04-27 08:00:58 -07:00
def set_bounds(self, left, right, bottom, top):
assert right > left and top > bottom
2021-07-29 02:26:34 +02:00
scalex = self.width / (right - left)
scaley = self.height / (top - bottom)
2021-07-29 12:42:48 -04:00
self.transform = Transform(translation=(-left * scalex, -bottom * scaley), scale=(scalex, scaley))
2016-04-27 08:00:58 -07:00
def add_geom(self, geom):
self.geoms.append(geom)
def add_onetime(self, geom):
self.onetime_geoms.append(geom)
def render(self, return_rgb_array=False):
2021-07-29 02:26:34 +02:00
glClearColor(1, 1, 1, 1)
2016-04-27 08:00:58 -07:00
self.window.clear()
self.window.switch_to()
self.window.dispatch_events()
self.transform.enable()
for geom in self.geoms:
geom.render()
for geom in self.onetime_geoms:
geom.render()
self.transform.disable()
arr = None
if return_rgb_array:
buffer = pyglet.image.get_buffer_manager().get_color_buffer()
image_data = buffer.get_image_data()
arr = np.frombuffer(image_data.get_data(), dtype=np.uint8)
# In https://github.com/openai/gym-http-api/issues/2, we
# discovered that someone using Xmonad on Arch was having
# a window of size 598 x 398, though a 600 x 400 window
# was requested. (Guess Xmonad was preserving a pixel for
# the boundary.) So we use the buffer height/width rather
# than the requested one.
arr = arr.reshape(buffer.height, buffer.width, 4)
2021-07-29 02:26:34 +02:00
arr = arr[::-1, :, 0:3]
2016-04-27 08:00:58 -07:00
self.window.flip()
self.onetime_geoms = []
return arr if return_rgb_array else self.isopen
2016-04-27 08:00:58 -07:00
# Convenience
def draw_circle(self, radius=10, res=30, filled=True, **attrs):
geom = make_circle(radius=radius, res=res, filled=filled)
_add_attrs(geom, attrs)
self.add_onetime(geom)
return geom
def draw_polygon(self, v, filled=True, **attrs):
geom = make_polygon(v=v, filled=filled)
_add_attrs(geom, attrs)
self.add_onetime(geom)
return geom
def draw_polyline(self, v, **attrs):
geom = make_polyline(v=v)
_add_attrs(geom, attrs)
self.add_onetime(geom)
return geom
def draw_line(self, start, end, **attrs):
geom = Line(start, end)
_add_attrs(geom, attrs)
self.add_onetime(geom)
return geom
def get_array(self):
self.window.flip()
2021-07-29 12:42:48 -04:00
image_data = pyglet.image.get_buffer_manager().get_color_buffer().get_image_data()
2016-04-27 08:00:58 -07:00
self.window.flip()
2021-07-29 02:26:34 +02:00
arr = np.fromstring(image_data.get_data(), dtype=np.uint8, sep="")
2016-04-27 08:00:58 -07:00
arr = arr.reshape(self.height, self.width, 4)
2021-07-29 02:26:34 +02:00
return arr[::-1, :, 0:3]
2016-04-27 08:00:58 -07:00
def __del__(self):
self.close()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def _add_attrs(geom, attrs):
if "color" in attrs:
geom.set_color(*attrs["color"])
2016-04-27 08:00:58 -07:00
if "linewidth" in attrs:
geom.set_linewidth(attrs["linewidth"])
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Geom(object):
def __init__(self):
2021-07-29 02:26:34 +02:00
self._color = Color((0, 0, 0, 1.0))
2016-04-27 08:00:58 -07:00
self.attrs = [self._color]
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render(self):
for attr in reversed(self.attrs):
attr.enable()
self.render1()
for attr in self.attrs:
attr.disable()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
raise NotImplementedError
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def add_attr(self, attr):
self.attrs.append(attr)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def set_color(self, r, g, b):
self._color.vec4 = (r, g, b, 1)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Attr(object):
def enable(self):
raise NotImplementedError
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def disable(self):
pass
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Transform(Attr):
2021-07-29 02:26:34 +02:00
def __init__(self, translation=(0.0, 0.0), rotation=0.0, scale=(1, 1)):
2016-04-27 08:00:58 -07:00
self.set_translation(*translation)
self.set_rotation(rotation)
self.set_scale(*scale)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def enable(self):
glPushMatrix()
2021-07-29 12:42:48 -04:00
glTranslatef(self.translation[0], self.translation[1], 0) # translate to GL loc ppint
2016-04-27 08:00:58 -07:00
glRotatef(RAD2DEG * self.rotation, 0, 0, 1.0)
glScalef(self.scale[0], self.scale[1], 1)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def disable(self):
glPopMatrix()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def set_translation(self, newx, newy):
self.translation = (float(newx), float(newy))
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def set_rotation(self, new):
self.rotation = float(new)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def set_scale(self, newx, newy):
self.scale = (float(newx), float(newy))
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Color(Attr):
def __init__(self, vec4):
self.vec4 = vec4
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def enable(self):
glColor4f(*self.vec4)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class LineStyle(Attr):
def __init__(self, style):
self.style = style
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def enable(self):
glEnable(GL_LINE_STIPPLE)
glLineStipple(1, self.style)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def disable(self):
glDisable(GL_LINE_STIPPLE)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class LineWidth(Attr):
def __init__(self, stroke):
self.stroke = stroke
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def enable(self):
glLineWidth(self.stroke)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Point(Geom):
def __init__(self):
Geom.__init__(self)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
2021-07-29 02:26:34 +02:00
glBegin(GL_POINTS) # draw point
2016-04-27 08:00:58 -07:00
glVertex3f(0.0, 0.0, 0.0)
glEnd()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class FilledPolygon(Geom):
def __init__(self, v):
Geom.__init__(self)
self.v = v
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
2021-07-29 02:26:34 +02:00
if len(self.v) == 4:
glBegin(GL_QUADS)
elif len(self.v) > 4:
glBegin(GL_POLYGON)
else:
glBegin(GL_TRIANGLES)
2016-04-27 08:00:58 -07:00
for p in self.v:
2021-07-29 02:26:34 +02:00
glVertex3f(p[0], p[1], 0) # draw each vertex
2016-04-27 08:00:58 -07:00
glEnd()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def make_circle(radius=10, res=30, filled=True):
points = []
for i in range(res):
2021-07-29 02:26:34 +02:00
ang = 2 * math.pi * i / res
points.append((math.cos(ang) * radius, math.sin(ang) * radius))
2016-04-27 08:00:58 -07:00
if filled:
return FilledPolygon(points)
else:
return PolyLine(points, True)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def make_polygon(v, filled=True):
2021-07-29 02:26:34 +02:00
if filled:
return FilledPolygon(v)
else:
return PolyLine(v, True)
2016-04-27 08:00:58 -07:00
def make_polyline(v):
return PolyLine(v, False)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def make_capsule(length, width):
2021-07-29 02:26:34 +02:00
l, r, t, b = 0, length, width / 2, -width / 2
box = make_polygon([(l, b), (l, t), (r, t), (r, b)])
circ0 = make_circle(width / 2)
circ1 = make_circle(width / 2)
2016-04-27 08:00:58 -07:00
circ1.add_attr(Transform(translation=(length, 0)))
geom = Compound([box, circ0, circ1])
return geom
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Compound(Geom):
def __init__(self, gs):
Geom.__init__(self)
self.gs = gs
for g in self.gs:
g.attrs = [a for a in g.attrs if not isinstance(a, Color)]
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
for g in self.gs:
g.render()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class PolyLine(Geom):
def __init__(self, v, close):
Geom.__init__(self)
self.v = v
self.close = close
self.linewidth = LineWidth(1)
self.add_attr(self.linewidth)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
glBegin(GL_LINE_LOOP if self.close else GL_LINE_STRIP)
for p in self.v:
2021-07-29 02:26:34 +02:00
glVertex3f(p[0], p[1], 0) # draw each vertex
2016-04-27 08:00:58 -07:00
glEnd()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def set_linewidth(self, x):
self.linewidth.stroke = x
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Line(Geom):
def __init__(self, start=(0.0, 0.0), end=(0.0, 0.0)):
Geom.__init__(self)
self.start = start
self.end = end
self.linewidth = LineWidth(1)
self.add_attr(self.linewidth)
def render1(self):
glBegin(GL_LINES)
glVertex2f(*self.start)
glVertex2f(*self.end)
glEnd()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class Image(Geom):
def __init__(self, fname, width, height):
Geom.__init__(self)
self.set_color(1.0, 1.0, 1.0)
2016-04-27 08:00:58 -07:00
self.width = width
self.height = height
img = pyglet.image.load(fname)
self.img = img
self.flip = False
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def render1(self):
2021-07-29 12:42:48 -04:00
self.img.blit(-self.width / 2, -self.height / 2, width=self.width, height=self.height)
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
# ================================================================
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
class SimpleImageViewer(object):
def __init__(self, display=None, maxwidth=500):
2016-04-27 08:00:58 -07:00
self.window = None
self.isopen = False
self.display = get_display(display)
self.maxwidth = maxwidth
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def imshow(self, arr):
if self.window is None:
height, width, _channels = arr.shape
if width > self.maxwidth:
scale = self.maxwidth / width
width = int(scale * width)
height = int(scale * height)
2021-07-29 02:26:34 +02:00
self.window = get_window(
width=width,
height=height,
display=self.display,
vsync=False,
resizable=True,
)
2016-04-27 08:00:58 -07:00
self.width = width
self.height = height
self.isopen = True
@self.window.event
def on_resize(width, height):
self.width = width
self.height = height
@self.window.event
def on_close():
self.isopen = False
assert len(arr.shape) == 3, "You passed in an image with the wrong number shape"
2021-07-29 12:42:48 -04:00
image = pyglet.image.ImageData(arr.shape[1], arr.shape[0], "RGB", arr.tobytes(), pitch=arr.shape[1] * -3)
2021-07-29 02:26:34 +02:00
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
texture = image.get_texture()
texture.width = self.width
texture.height = self.height
2016-04-27 08:00:58 -07:00
self.window.clear()
self.window.switch_to()
self.window.dispatch_events()
2021-07-29 02:26:34 +02:00
texture.blit(0, 0) # draw
2016-04-27 08:00:58 -07:00
self.window.flip()
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
def close(self):
if self.isopen and sys.meta_path:
# ^^^ check sys.meta_path to avoid 'ImportError: sys.meta_path is None, Python is likely shutting down'
2016-04-27 08:00:58 -07:00
self.window.close()
self.isopen = False
2016-04-27 08:00:58 -07:00
def __del__(self):
self.close()