From b9b6c26fbc2e7841a0a793e21edf10cfd4b08014 Mon Sep 17 00:00:00 2001
From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch>
Date: Thu, 13 Jun 2019 10:57:34 +0200
Subject: [PATCH] Rendering "nicified"

---
 flatland/utils/graphics_pil.py  | 65 ++++++++++++++++++++-------------
 notebooks/Scene_Editor.ipynb    |  2 +-
 svg/Background_Light_green.svg  | 57 +++++++++++++++++++++++++++++
 svg/Background_city.svg         | 57 +++++++++++++++++++++++++++++
 svg/Background_rail.svg         | 57 +++++++++++++++++++++++++++++
 svg/Background_white_filter.svg | 57 +++++++++++++++++++++++++++++
 6 files changed, 268 insertions(+), 27 deletions(-)
 create mode 100644 svg/Background_Light_green.svg
 create mode 100644 svg/Background_city.svg
 create mode 100644 svg/Background_rail.svg
 create mode 100644 svg/Background_white_filter.svg

diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index f8c96f1..192d6f3 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -79,20 +79,18 @@ class PILGL(GraphicsLayer):
         self.firstFrame = True
         self.create_layers()
 
-
-    def build_background_map(self,dTargets):
+    def build_background_map(self, dTargets):
         self.background_grid = np.zeros(shape=(self.width, self.height))
         for x in range(self.width):
             for y in range(self.height):
-                distance = int(np.floor(np.sqrt(self.width*2.0 + self.height)))
+                distance = int(np.floor(np.sqrt(self.width * 2.0 + self.height)))
                 for rc in dTargets:
                     r = rc[1]
                     c = rc[0]
-                    d = int(np.floor(np.sqrt((x-r)**2 + (y-c)**2)))
-                    distance = min(d,distance)
+                    d = int(np.floor(np.sqrt((x - r) ** 2 + (y - c) ** 2)))
+                    distance = min(d, distance)
                 self.background_grid[x][y] = distance
 
-
     def rgb_s2i(self, sRGB):
         """ convert a hex RGB string like 0091ea to 3-tuple of ints """
         return tuple(int(sRGB[iRGB * 2:iRGB * 2 + 2], 16) for iRGB in [0, 1, 2])
@@ -236,7 +234,6 @@ class PILSVG(PILGL):
         self.lwAgents = []
         self.agents_prev = []
 
-
         self.loadBuildingSVGs()
         self.loadScenerySVGs()
         self.loadRailSVGs()
@@ -273,12 +270,18 @@ class PILSVG(PILGL):
             pil_img = Image.open(fIn)
             return pil_img
 
-
-
     def loadBuildingSVGs(self):
         dBuildingFiles = [
             "Buildings/Bank.svg",
             "Buildings/Bar.svg",
+            "Buildings/Wohnhaus.svg",
+            "Buildings/Hochhaus.svg",
+            "Buildings/Hotel.svg",
+            "Buildings/Office.svg",
+            "Buildings/Polizei.svg",
+            "Buildings/Post.svg",
+            "Buildings/Supermarkt.svg",
+            "Buildings/Tankstelle.svg",
             "Buildings/Fabrik_A.svg",
             "Buildings/Fabrik_B.svg",
             "Buildings/Fabrik_C.svg",
@@ -288,18 +291,14 @@ class PILSVG(PILGL):
             "Buildings/Fabrik_G.svg",
             "Buildings/Fabrik_H.svg",
             "Buildings/Fabrik_I.svg",
-            "Buildings/Hochhaus.svg",
-            "Buildings/Hotel.svg",
-            "Buildings/Office.svg",
-            "Buildings/Polizei.svg",
-            "Buildings/Post.svg",
-            "Buildings/Supermarkt.svg",
-            "Buildings/Tankstelle.svg",
-            "Buildings/Wohnhaus.svg"]
+        ]
+
+        imgBg = self.pilFromSvgFile('svg', "Background_city.svg")
 
         self.dBuildings = []
         for sFile in dBuildingFiles:
-            img = self.pilFromSvgFile('svg',sFile)
+            img = self.pilFromSvgFile('svg', sFile)
+            img = Image.alpha_composite(imgBg, img)
             self.dBuildings.append(img)
 
     def loadScenerySVGs(self):
@@ -316,16 +315,20 @@ class PILSVG(PILGL):
             "Scenery/Bergwelt_A_Teil_2_mitte.svg",
             "Scenery/Bergwelt_A_Teil_3_rechts.svg",
         ]
+
+        imgBg = self.pilFromSvgFile('svg', "Background_Light_green.svg")
+
         self.dScenery = []
         for sFile in dSceneryFiles:
-            img = self.pilFromSvgFile('svg',sFile)
+            img = self.pilFromSvgFile('svg', sFile)
+            img = Image.alpha_composite(imgBg, img)
             self.dScenery.append(img)
 
     def loadRailSVGs(self):
         """ Load the rail SVG images, apply rotations, and store as PIL images.
         """
         dRailFiles = {
-            "": "Background_#91D1DD.svg",
+            "": "Background_Light_green.svg",
             "WE": "Gleis_Deadend.svg",
             "WW EE NN SS": "Gleis_Diamond_Crossing.svg",
             "WW EE": "Gleis_horizontal.svg",
@@ -355,16 +358,18 @@ class PILSVG(PILGL):
             "NN SS": "Bahnhof_#d50000_Gleis_vertikal.svg"}
 
         # Dict of rail cell images indexed by binary transitions
-        self.dPilRail = self.loadSVGs(dRailFiles, rotate=True)
+        self.dPilRail = self.loadSVGs(dRailFiles, rotate=True, backgroundImage="Background_rail.svg",
+                                      whitefilter="Background_white_filter.svg")
 
         # Load the target files (which have rails and transitions of their own)
         # They are indexed by (binTrans, iAgent), ie a tuple of the binary transition and the agent index
-        dPilRail2 = self.loadSVGs(dTargetFiles, rotate=False, agent_colors=self.ltAgentColors)
+        dPilRail2 = self.loadSVGs(dTargetFiles, rotate=False, agent_colors=self.ltAgentColors, backgroundImage="Background_rail.svg",
+                                  whitefilter="Background_white_filter.svg")
         # Merge them with the regular rails.
         # https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
         self.dPilRail = {**self.dPilRail, **dPilRail2}
 
-    def loadSVGs(self, dDirFile, rotate=False, agent_colors=False):
+    def loadSVGs(self, dDirFile, rotate=False, agent_colors=False, backgroundImage=None, whitefilter=None):
         dPil = {}
 
         transitions = RailEnvTransitions()
@@ -387,6 +392,14 @@ class PILSVG(PILGL):
 
             pilRail = self.pilFromSvgFile('svg', sFile)
 
+            if backgroundImage is not None:
+                imgBg = self.pilFromSvgFile('svg', backgroundImage)
+                pilRail = Image.alpha_composite(imgBg, pilRail)
+
+            if whitefilter is not None:
+                imgBg = self.pilFromSvgFile('svg', whitefilter)
+                pilRail = Image.alpha_composite(pilRail, imgBg)
+
             if rotate:
                 # For rotations, we also store the base image
                 dPil[binTrans] = pilRail
@@ -412,13 +425,13 @@ class PILSVG(PILGL):
             if binTrans in self.dPilRail:
                 pilTrack = self.dPilRail[binTrans]
 
-                if binTrans == 0 :
+                if binTrans == 0:
                     if self.background_grid[col][row] < 4:
                         a = int(self.background_grid[col][row])
                         a = a % len(self.dBuildings)
                         pilTrack = self.dBuildings[a]
-                    else:
-                        a = int(self.background_grid[col][row]) - 4
+                    elif self.background_grid[col][row] > 5:
+                        a = int(self.background_grid[col][row]) - 5
                         a = a % len(self.dScenery)
                         pilTrack = self.dScenery[a]
 
diff --git a/notebooks/Scene_Editor.ipynb b/notebooks/Scene_Editor.ipynb
index 7a13a49..c06bbc5 100644
--- a/notebooks/Scene_Editor.ipynb
+++ b/notebooks/Scene_Editor.ipynb
@@ -100,7 +100,7 @@
     {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "2460d8e561a3407d8fe2a6f28773bc04",
+       "model_id": "d97b62cfb71442548d19f76225810450",
        "version_major": 2,
        "version_minor": 0
       },
diff --git a/svg/Background_Light_green.svg b/svg/Background_Light_green.svg
new file mode 100644
index 0000000..613995a
--- /dev/null
+++ b/svg/Background_Light_green.svg
@@ -0,0 +1,57 @@
+<?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="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_#DEBDA0 - Kopie.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="1137"
+        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
+        style="fill:#eef4d7"
+        id="rect13"
+        width="242.03391"
+        height="238.98306"
+        x="3.2327943e-08"
+        y="-1.1314779e-07"/></svg>
diff --git a/svg/Background_city.svg b/svg/Background_city.svg
new file mode 100644
index 0000000..ba5d2ce
--- /dev/null
+++ b/svg/Background_city.svg
@@ -0,0 +1,57 @@
+<?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="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_Light_gray.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="1137"
+        id="namedview7"
+        showgrid="false"
+        inkscape:zoom="0.98333333"
+        inkscape:cx="-102.20339"
+        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
+        style="fill:#fdf7fa;fill-opacity:1"
+        id="rect13"
+        width="242.03391"
+        height="238.98306"
+        x="3.2327943e-08"
+        y="-1.1314779e-07"/></svg>
diff --git a/svg/Background_rail.svg b/svg/Background_rail.svg
new file mode 100644
index 0000000..e1d7066
--- /dev/null
+++ b/svg/Background_rail.svg
@@ -0,0 +1,57 @@
+<?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="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_rail.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="1137"
+        id="namedview7"
+        showgrid="false"
+        inkscape:zoom="0.98333333"
+        inkscape:cx="-362.54237"
+        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
+        style="fill:#fbf6f7;fill-opacity:1"
+        id="rect13"
+        width="242.03391"
+        height="238.98306"
+        x="3.2327943e-08"
+        y="-1.1314779e-07"/></svg>
diff --git a/svg/Background_white_filter.svg b/svg/Background_white_filter.svg
new file mode 100644
index 0000000..895a97a
--- /dev/null
+++ b/svg/Background_white_filter.svg
@@ -0,0 +1,57 @@
+<?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="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_filter.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="1137"
+        id="namedview7"
+        showgrid="false"
+        inkscape:zoom="0.98333333"
+        inkscape:cx="-324.40678"
+        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
+        style="fill:#ffffff;fill-opacity:0.50196081"
+        id="rect13"
+        width="242.03391"
+        height="238.98306"
+        x="3.2327943e-08"
+        y="-1.1314779e-07"/></svg>
-- 
GitLab