From cbf9e1e68723d44c907d87d1a71480851611059e Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Tue, 1 Oct 2019 18:37:08 +0200
Subject: [PATCH] rendering

---
 examples/flatland_2_0_example.py |  7 +++-
 flatland/envs/rail_env.py        |  6 ++--
 flatland/utils/graphics_layer.py |  2 +-
 flatland/utils/graphics_pil.py   | 10 ++++--
 flatland/utils/rendertools.py    |  8 +++--
 svg/Background_white.svg         | 55 ++++++++++++++++++++++++++++++++
 6 files changed, 77 insertions(+), 11 deletions(-)
 create mode 100644 svg/Background_white.svg

diff --git a/examples/flatland_2_0_example.py b/examples/flatland_2_0_example.py
index 09c59918..3b24b3d5 100644
--- a/examples/flatland_2_0_example.py
+++ b/examples/flatland_2_0_example.py
@@ -39,7 +39,12 @@ env = RailEnv(width=50,
               schedule_generator=sparse_schedule_generator(),
               number_of_agents=20,
               stochastic_data=stochastic_data,  # Malfunction data generator
-              obs_builder_object=GlobalObsForRailEnv())
+              obs_builder_object=GlobalObsForRailEnv(),
+              remove_agents_at_target=True
+              )
+
+RailEnv.DEPOT_POSITION = lambda agent, agent_handle : (agent_handle % env.height,0)
+
 
 env_renderer = RenderTool(env, gl="PILSVG",
                           agent_render_variant=AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX,
diff --git a/flatland/envs/rail_env.py b/flatland/envs/rail_env.py
index b64716f5..8105d1eb 100644
--- a/flatland/envs/rail_env.py
+++ b/flatland/envs/rail_env.py
@@ -111,7 +111,7 @@ class RailEnv(Environment):
 
     # Where the agent will be placed to when the reach their target destination
     # (Remove the agents to free the cell)
-    DEPOT_POSITION = (-10, -10)
+    DEPOT_POSITION = lambda agent, agent_handle : (-10, -10)
 
     def __init__(self,
                  width,
@@ -514,8 +514,8 @@ class RailEnv(Environment):
                 agent.moving = False
 
                 if self.remove_agents_at_target:
-                    agent.position = RailEnv.DEPOT_POSITION
-                    agent.target = RailEnv.DEPOT_POSITION
+                    agent.position = RailEnv.DEPOT_POSITION(agent,i_agent)
+                    agent.target = RailEnv.DEPOT_POSITION(agent,i_agent)
             else:
                 self.rewards_dict[i_agent] += self.step_penalty * agent.speed_data['speed']
         else:
diff --git a/flatland/utils/graphics_layer.py b/flatland/utils/graphics_layer.py
index 11a9addc..5f0ce53a 100644
--- a/flatland/utils/graphics_layer.py
+++ b/flatland/utils/graphics_layer.py
@@ -73,7 +73,7 @@ class GraphicsLayer(object):
         """
         pass
 
-    def set_agent_at(self, iAgent, row, col, iDirIn, iDirOut, isSelected=False):
+    def set_agent_at(self, iAgent, row, col, iDirIn, iDirOut, isSelected=False,rail_grid=None,show_debug=False):
         pass
 
     def set_cell_occupied(self, iAgent, row, col):
diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index a2fc845c..971e1927 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -368,6 +368,8 @@ class PILSVG(PILGL):
 
         img_back_ground = self.pil_from_svg_file('svg', "Background_Light_green.svg")
 
+        self.scenery_background_white = self.pil_from_svg_file('svg', "Background_white.svg")
+
         self.scenery = []
         for file in scenery_files:
             img = self.pil_from_svg_file('svg', file)
@@ -392,7 +394,6 @@ class PILSVG(PILGL):
             img = Image.alpha_composite(img_back_ground, img)
             self.scenery_water.append(img)
 
-
     def load_rail(self):
         """ Load the rail SVG images, apply rotations, and store as PIL images.
         """
@@ -630,7 +631,8 @@ class PILSVG(PILGL):
                 for color_idx, pil_zug_3 in enumerate(pils):
                     self.pil_zug[(in_direction_2, out_direction_2, color_idx)] = pils[color_idx]
 
-    def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected, show_debug=False):
+    def set_agent_at(self, agent_idx, row, col, in_direction, out_direction, is_selected,
+                     rail_grid=None, show_debug=False):
         delta_dir = (out_direction - in_direction) % 4
         color_idx = agent_idx % self.n_agent_colors
         # when flipping direction at a dead end, use the "out_direction" direction.
@@ -638,12 +640,14 @@ class PILSVG(PILGL):
             in_direction = out_direction
         pil_zug = self.pil_zug[(in_direction % 4, out_direction % 4, color_idx)]
         self.draw_image_row_col(pil_zug, (row, col), layer=PILGL.AGENT_LAYER)
+        if rail_grid is not None: 
+            if rail_grid[row,col] == 0.0:
+                self.draw_image_row_col(self.scenery_background_white, (row, col), layer=PILGL.RAIL_LAYER)
 
         if is_selected:
             bg_svg = self.pil_from_svg_file("svg", "Selected_Agent.svg")
             self.clear_layer(PILGL.SELECTED_AGENT_LAYER, 0)
             self.draw_image_row_col(bg_svg, (row, col), layer=PILGL.SELECTED_AGENT_LAYER)
-
         if show_debug:
             self.text_rowcol((row + 0.2, col + 0.2,), str(agent_idx))
 
diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py
index 42203368..be6ffb79 100644
--- a/flatland/utils/rendertools.py
+++ b/flatland/utils/rendertools.py
@@ -556,7 +556,7 @@ class RenderTool(object):
                 if self.agent_render_variant == AgentRenderVariant.ONE_STEP_BEHIND_AND_BOX:
                     self.gl.set_cell_occupied(agent_idx, *(agent.position))
                 self.gl.set_agent_at(agent_idx, *position, old_direction, direction,
-                                     selected_agent == agent_idx, show_debug=self.show_debug)
+                                     selected_agent == agent_idx, rail_grid=env.rail.grid, show_debug=self.show_debug)
             else:
                 position = agent.position
                 direction = agent.direction
@@ -568,12 +568,14 @@ class RenderTool(object):
 
                         # set_agent_at uses the agent index for the color
                         self.gl.set_agent_at(agent_idx, *position, agent.direction, direction,
-                                             selected_agent == agent_idx, show_debug=self.show_debug)
+                                             selected_agent == agent_idx, rail_grid=env.rail.grid,
+                                             show_debug=self.show_debug)
 
                 # set_agent_at uses the agent index for the color
                 if self.agent_render_variant == AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX:
                     self.gl.set_cell_occupied(agent_idx, *(agent.position))
-                self.gl.set_agent_at(agent_idx, *position, agent.direction, direction, selected_agent == agent_idx)
+                self.gl.set_agent_at(agent_idx, *position, agent.direction, direction, selected_agent == agent_idx,
+                                     rail_grid=env.rail.grid)
 
         if show_observations:
             self.render_observation(range(env.get_num_agents()), env.dev_obs_dict)
diff --git a/svg/Background_white.svg b/svg/Background_white.svg
new file mode 100644
index 00000000..5c72b282
--- /dev/null
+++ b/svg/Background_white.svg
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Generator: Adobe Illustrator 23.0.3, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   version="1.1"
+   id="Ebene_1"
+   x="0px"
+   y="0px"
+   viewBox="0 0 240 240"
+   style="enable-background:new 0 0 240 240;"
+   xml:space="preserve"
+   sodipodi:docname="Background_white.svg"
+   inkscape:version="0.92.4 (5da689c313, 2019-01-14)"><metadata
+   id="metadata11"><rdf:RDF><cc:Work
+       rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
+         rdf:resource="http://purl.org/dc/dcmitype/StillImage" /></cc:Work></rdf:RDF></metadata><defs
+   id="defs9" /><sodipodi:namedview
+   pagecolor="#ffffff"
+   bordercolor="#666666"
+   borderopacity="1"
+   objecttolerance="10"
+   gridtolerance="10"
+   guidetolerance="10"
+   inkscape:pageopacity="0"
+   inkscape:pageshadow="2"
+   inkscape:window-width="1920"
+   inkscape:window-height="1017"
+   id="namedview7"
+   showgrid="false"
+   inkscape:zoom="0.98333333"
+   inkscape:cx="120"
+   inkscape:cy="120"
+   inkscape:window-x="-8"
+   inkscape:window-y="-8"
+   inkscape:window-maximized="1"
+   inkscape:current-layer="Ebene_1" />
+<style
+   type="text/css"
+   id="style2">
+	.st0{fill:#DEBDA0;}
+</style>
+<rect
+   class="st0"
+   width="240"
+   height="240"
+   id="rect4"
+   style="fill:#f9f9f9" />
+</svg>
\ No newline at end of file
-- 
GitLab