From 12924c5ab69784fa865668cd719900e9a80dac5b Mon Sep 17 00:00:00 2001 From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch> Date: Thu, 23 May 2019 22:15:49 +0200 Subject: [PATCH] 1st flatland world --- examples/demo.py | 6 ++++++ flatland/utils/graphics_layer.py | 3 +++ flatland/utils/render_qt.py | 35 ++++++++++++++++++++------------ flatland/utils/rendertools.py | 3 +++ notebooks/Editor2.ipynb | 4 ++-- 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/examples/demo.py b/examples/demo.py index bdb066d5..90ab261b 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -173,3 +173,9 @@ demo_001 = None demo_002 = Demo(Scenario_Generator.load_scenario('./env-data/railway/example_network_002.pkl')) demo_002.run_demo() demo_002 = None + + +demo_flatland_000 = Demo(Scenario_Generator.load_scenario('./env-data/railway/example_flatland_000.pkl')) +demo_flatland_000.renderer.resize() +demo_flatland_000.run_demo(100) +demo_flatland_000 = None diff --git a/flatland/utils/graphics_layer.py b/flatland/utils/graphics_layer.py index 4cfcc64b..527944b7 100644 --- a/flatland/utils/graphics_layer.py +++ b/flatland/utils/graphics_layer.py @@ -68,3 +68,6 @@ class GraphicsLayer(object): def setAgentAt(self, iAgent, row, col, iDirIn, iDirOut): pass + + def resize(self,env): + pass diff --git a/flatland/utils/render_qt.py b/flatland/utils/render_qt.py index 73b8ca77..6413c99a 100644 --- a/flatland/utils/render_qt.py +++ b/flatland/utils/render_qt.py @@ -1,14 +1,15 @@ -from flatland.utils.graphics_qt import QtRenderer -from numpy import array -from flatland.utils.graphics_layer import GraphicsLayer -# from matplotlib import pyplot as plt -import numpy as np import time -from flatland.utils.svg import Track, Zug -from flatland.envs.agent_utils import EnvAgent -from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout +# from matplotlib import pyplot as plt +import numpy as np from PyQt5 import QtSvg +from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout +from numpy import array + +from flatland.envs.agent_utils import EnvAgent +from flatland.utils.graphics_layer import GraphicsLayer +from flatland.utils.graphics_qt import QtRenderer +from flatland.utils.svg import Track, Zug def transform_string_svg(sSVG): @@ -16,6 +17,7 @@ def transform_string_svg(sSVG): bySVG = bytearray(sSVG, encoding='utf-8') return bySVG + def create_QtSvgWidget_from_svg_string(sSVG): svgWidget = QtSvg.QSvgWidget() ret = svgWidget.renderer().load(transform_string_svg(sSVG)) @@ -45,10 +47,10 @@ class QTGL(GraphicsLayer): # use the renderer to scale back to the desired size self.qtr.scale(self.tile_size / self.cell_pixels, self.tile_size / self.cell_pixels) - self.tColBg = (255, 255, 255) # white background + self.tColBg = (255, 255, 255) # white background # self.tColBg = (220, 120, 40) # background color - self.tColRail = (0, 0, 0) # black rails - self.tColGrid = (230,) * 3 # light grey for grid + self.tColRail = (0, 0, 0) # black rails + self.tColGrid = (230,) * 3 # light grey for grid # Draw the background of the in-world cells self.qtr.fillRect(0, 0, self.widthPx, self.heightPx, *self.tColBg) @@ -195,8 +197,8 @@ class QTSVG(GraphicsLayer): # We can only reuse the image if noth new and old are straight and the same: if iDirIn == iDirOut and \ - agentPrev.direction == iDirIn and \ - agentPrev.old_direction == agentPrev.direction: + agentPrev.direction == iDirIn and \ + agentPrev.old_direction == agentPrev.direction: return else: # need to load new image @@ -222,6 +224,13 @@ class QTSVG(GraphicsLayer): def show(self, block=False): self.wMain.update() + def resize(self, env): + screen_resolution = self.app.desktop().screenGeometry() + width, height = screen_resolution.width(), screen_resolution.height() + w = np.ceil(width * 0.8 / env.width) + h = np.ceil(height * 0.8 / env.height) + self.wWinMain.resize(env.width * w, env.height * h) + def main2(): gl = QTGL(10, 10) diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py index b96531c6..4a1827f1 100644 --- a/flatland/utils/rendertools.py +++ b/flatland/utils/rendertools.py @@ -137,6 +137,9 @@ class RenderTool(object): self.new_rail = True + def resize(self): + self.gl.resize(self.env) + def set_new_rail(self): self.new_rail = True diff --git a/notebooks/Editor2.ipynb b/notebooks/Editor2.ipynb index 71b74b79..4ac6e9db 100644 --- a/notebooks/Editor2.ipynb +++ b/notebooks/Editor2.ipynb @@ -114,7 +114,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "31e3248d9a0e4b5da8f2439abd13558d", + "model_id": "ece47ccd72af4638b61e9d93a66e9a57", "version_major": 2, "version_minor": 0 }, @@ -140,7 +140,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "c22754b330ce490383eb05972bc96afe", + "model_id": "86207439e5a94055bb3d837028f195fc", "version_major": 2, "version_minor": 0 }, -- GitLab