From e22c250e6ab71bbbcd8f02e29a3c652ce9413834 Mon Sep 17 00:00:00 2001 From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch> Date: Tue, 25 Jun 2019 13:24:59 +0200 Subject: [PATCH] performance opt. speedup --- flatland/utils/graphics_pil.py | 39 +++++++++++++++++++++++++--------- notebooks/Scene_Editor.ipynb | 2 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py index 8d10312b..ed48654c 100644 --- a/flatland/utils/graphics_pil.py +++ b/flatland/utils/graphics_pil.py @@ -79,19 +79,38 @@ class PILGL(GraphicsLayer): self.window_root = None self.window_open = False self.firstFrame = True + self.old_background_image = (None, None, None) self.create_layers() 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.ceil(np.sqrt(self.width ** 2.0 + self.height ** 2.0))) - 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) - self.background_grid[x][y] = distance + x = self.old_background_image + rebuild = False + if x[0] is None: + rebuild = True + else: + if len(x[0]) != len(dTargets): + rebuild = True + else: + if x[0] != dTargets: + rebuild = True + if x[1] != self.width: + rebuild = True + if x[2] != self.height: + rebuild = True + + if rebuild: + 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.ceil(np.sqrt(self.width ** 2.0 + self.height ** 2.0))) + 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) + self.background_grid[x][y] = distance + + self.old_background_image = (dTargets, self.width, self.height) def rgb_s2i(self, sRGB): """ convert a hex RGB string like 0091ea to 3-tuple of ints """ diff --git a/notebooks/Scene_Editor.ipynb b/notebooks/Scene_Editor.ipynb index 75abead1..df314171 100644 --- a/notebooks/Scene_Editor.ipynb +++ b/notebooks/Scene_Editor.ipynb @@ -70,7 +70,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "b9ba3ece379e463184ad5217c1f4c289", + "model_id": "9d466178f2f04a33b5589de583a565b7", "version_major": 2, "version_minor": 0 }, -- GitLab