Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiberducci committed Sep 14, 2023
1 parent f9e8c05 commit 93ba5d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
6 changes: 4 additions & 2 deletions gym/f110_gym/envs/f110_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def __init__(self, config: dict = None, render_mode=None, **kwargs):
# stateful observations for rendering
self.render_obs = None
self.render_mode = render_mode
self.renderer, self.render_spec = make_renderer(track=self.track, agent_ids=self.agent_ids, render_mode=render_mode)
self.renderer, self.render_spec = make_renderer(
track=self.track, agent_ids=self.agent_ids, render_mode=render_mode
)
self.metadata["render_fps"] = self.render_spec.render_fps

def __del__(self):
Expand Down Expand Up @@ -470,4 +472,4 @@ def close(self):
"""
if self.renderer is not None:
self.renderer.close()
super().close()
super().close()
19 changes: 16 additions & 3 deletions gym/f110_gym/envs/rendering/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@
from f110_gym.envs.track import Track


def make_renderer(track: Track, agent_ids: List[str], render_mode: str = None) -> Tuple[EnvRenderer, RenderSpec]:
def make_renderer(
track: Track, agent_ids: List[str], render_mode: str = None
) -> Tuple[EnvRenderer, RenderSpec]:
from f110_gym.envs.rendering.rendering_pygame import PygameEnvRenderer

cfg_file = pathlib.Path(__file__).parent.absolute() / "rendering.yaml"
render_spec = RenderSpec.from_yaml(cfg_file)

if render_mode in ["human_fast"]:
cfg_file = pathlib.Path(__file__).parent.absolute() / "rendering_fast.yaml"
render_spec = RenderSpec.from_yaml(cfg_file)
renderer = PygameEnvRenderer(track=track, agent_ids=agent_ids, render_spec=render_spec, render_mode=render_mode)
renderer = PygameEnvRenderer(
track=track,
agent_ids=agent_ids,
render_spec=render_spec,
render_mode=render_mode,
)
elif render_mode in ["human", "rgb_array"]:
renderer = PygameEnvRenderer(track=track, agent_ids=agent_ids, render_spec=render_spec, render_mode=render_mode)
renderer = PygameEnvRenderer(
track=track,
agent_ids=agent_ids,
render_spec=render_spec,
render_mode=render_mode,
)
else:
renderer = None
return renderer, render_spec
19 changes: 11 additions & 8 deletions gym/f110_gym/envs/rendering/rendering_pygame.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def render(self, time: float, display: pygame.Surface):


class Info:
def __init__(self, window_shape=(1000,1000)):
def __init__(self, window_shape=(1000, 1000)):
font_size = int(32 * window_shape[0] / 1000)
self.font = pygame.font.SysFont("Arial", font_size)
self.text = self.font.render("", True, (125, 125, 125))
Expand Down Expand Up @@ -130,7 +130,6 @@ def render(self, display):
front_right = (vertices[1] * lam + vertices[2] * (1 - lam)).astype(int)
arrow_length = self.steering_arrow_len / self.resolution


for mid_point in [front_left, front_right]:
end_point = mid_point + 0.5 * arrow_length * np.array(
[np.cos(self.steering_angle), np.sin(self.steering_angle)]
Expand Down Expand Up @@ -319,10 +318,10 @@ def render_points(
pygame.draw.circle(self.map_canvas, color, point, size)

def render_lines(
self,
points: Union[List, np.ndarray],
color: Tuple[int, int, int] = (0, 0, 255),
size: int = 1,
self,
points: Union[List, np.ndarray],
color: Tuple[int, int, int] = (0, 0, 255),
size: int = 1,
):
"""
Render a sequence of lines segments.
Expand All @@ -337,7 +336,9 @@ def render_lines(
points = ((points - origin[:2]) / resolution).astype(int)
size = math.ceil(size / self.ppu)

pygame.draw.lines(self.map_canvas, color, closed=False, points=points, width=size)
pygame.draw.lines(
self.map_canvas, color, closed=False, points=points, width=size
)

def render_closed_lines(
self,
Expand All @@ -358,7 +359,9 @@ def render_closed_lines(
points = ((points - origin[:2]) / resolution).astype(int)
size = math.ceil(size / self.ppu)

pygame.draw.lines(self.map_canvas, color, closed=True, points=points, width=size)
pygame.draw.lines(
self.map_canvas, color, closed=True, points=points, width=size
)

def close(self):
if self.render_mode == "human" or self.render_mode == "human_fast":
Expand Down
9 changes: 3 additions & 6 deletions gym/f110_gym/envs/rendering/rendering_pyglet.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class PygletEnvRenderer(pyglet.window.Window, EnvRenderer):
A window class inherited from pyglet.window.Window, handles the camera/projection interaction, resizing window, and rendering the environment
"""


def __init__(self, track: Track, render_spec: RenderSpec):
"""
Class constructor
Expand All @@ -69,9 +68,7 @@ def __init__(self, track: Track, render_spec: RenderSpec):
"""
conf = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True)
width, height = render_spec.window_size, render_spec.window_size
super().__init__(
width, height, config=conf, resizable=True, vsync=False
)
super().__init__(width, height, config=conf, resizable=True, vsync=False)

# gl init
glClearColor(9 / 255, 32 / 255, 87 / 255, 1.0)
Expand Down Expand Up @@ -360,11 +357,11 @@ def update(self, state):

self.score_label.text = (
"Lap Time: {laptime:.2f}, Ego Lap Count: {count:.0f}".format(
laptime=state["lap_times"][0], count=state["lap_counts"][state["ego_idx"]]
laptime=state["lap_times"][0],
count=state["lap_counts"][state["ego_idx"]],
)
)


def render_map(self):
pass

Expand Down
6 changes: 2 additions & 4 deletions gym/f110_gym/test/benchmark_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def pretty_print(dict: dict, col_width=15):
print("|" + "-" * col_width, end="")
print("|")

class BenchmarkRenderer:

class BenchmarkRenderer:
@staticmethod
def _make_env(config={}, render_mode=None) -> F110Env:
import gymnasium as gym
Expand Down Expand Up @@ -95,7 +95,6 @@ def benchmark_single_agent_rendering(self):

pretty_print(results)


def benchmark_n_agents_human_rendering(self):
"""
This is meant to benchmark the human rendering mode, for increasing nr of agents.
Expand Down Expand Up @@ -135,7 +134,6 @@ def benchmark_n_agents_human_rendering(self):

pretty_print(results)


def benchmark_callbacks_human_rendering(self):
import time

Expand Down Expand Up @@ -216,7 +214,7 @@ def render_position(self, e):
pretty_print(results)


if __name__ == '__main__':
if __name__ == "__main__":
benchmark = BenchmarkRenderer()

benchmark.benchmark_single_agent_rendering()
Expand Down

0 comments on commit 93ba5d8

Please sign in to comment.