From 6c0301dc30242783554fda6e1f6f513264b2b644 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Mon, 20 Dec 2021 12:21:47 +0100 Subject: [PATCH] optimized outline detection (3x faster), added possibility to disable lines per object --- ahb_cmd_render.py | 57 ++++++++++++++++++++++++++------------- ahb_cmd_set_part_stage.py | 2 ++ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/ahb_cmd_render.py b/ahb_cmd_render.py index 081a498..a1dcf80 100644 --- a/ahb_cmd_render.py +++ b/ahb_cmd_render.py @@ -18,10 +18,13 @@ class AHB_Render: for obj in doc.Objects: if obj.TypeId == 'Part::Feature': if 'AssemblyHandbook_Stage' in obj.PropertiesList: + if 'AssemblyHandbook_RenderLines' in obj.PropertiesList and not obj.AssemblyHandbook_RenderLines: + obj.ViewObject.LineColor = background_color + else: + obj.ViewObject.LineColor = line_color obj.ViewObject.DisplayMode = 'Flat Lines' obj.ViewObject.ShapeMaterial.AmbientColor = (0.0, 0.0, 0.0, 0.0) obj.ViewObject.ShapeMaterial.EmissiveColor = background_color - obj.ViewObject.LineColor = line_color def set_render_outlines(self): doc = App.activeDocument() @@ -49,7 +52,7 @@ class AHB_Render: b = step def Activated(self): - resolution = (512, 512) + resolution = (1024, 1024) self.set_render_lines() Gui.ActiveDocument.ActiveView.saveImage("/home/youen/dev_linux/vhelio-render/vhelio.png", resolution[0], resolution[1], "#ffffff") @@ -62,25 +65,41 @@ class AHB_Render: lines = Image.open("/home/youen/dev_linux/vhelio-render/vhelio.png") lines = lines.convert("L") shapes = Image.open("/home/youen/dev_linux/vhelio-render/vhelio-shapes.png") + + cacheWidth = 8 + cacheHalfWidth = int(cacheWidth/2) + cache = [] + for y in range(0, 4): + cacheLine = [] + for x in range(0, cacheWidth + 2): + cacheLine.append((0.0,0.0,0.0)) + cache.append(cacheLine) + startTime = time.perf_counter() - for xb in range(1, lines.size[0]-1): + for xb in range(1, lines.size[0]-cacheHalfWidth, cacheHalfWidth): for yb in range(1, lines.size[1]-1): - outline_color = 1.0 - for x in range(xb*2, xb*2+2): - for y in range(yb*2, yb*2+2): - c = shapes.getpixel((x, y)) - border = False - if c != shapes.getpixel((x - 1, y - 1)): border = True - if c != shapes.getpixel((x, y - 1)): border = True - if c != shapes.getpixel((x + 1, y - 1)): border = True - if c != shapes.getpixel((x - 1, y)): border = True - if c != shapes.getpixel((x, y)): border = True - if c != shapes.getpixel((x + 1, y)): border = True - if c != shapes.getpixel((x - 1, y + 1)): border = True - if c != shapes.getpixel((x, y + 1)): border = True - if c != shapes.getpixel((x + 1, y + 1)): border = True - if border: outline_color = outline_color - 0.25 - lines.putpixel((xb, yb), min(lines.getpixel((xb, yb)), int(outline_color * 255.0))) + for y in range(0, 4): + cacheLine = cache[y] + for x in range(0, cacheWidth + 2): + cacheLine[x] = shapes.getpixel((xb*2+x-1, yb*2+y-1)) + + for xa in range(0, cacheHalfWidth): + outline_color = 1.0 + for xaa in range(0, 2): + x = xa*2 + xaa + for y in range(0, 2): + c = cache[y+1][x+1] + border = False + if c != cache[y][x]: border = True + if c != cache[y][x+1]: border = True + if c != cache[y][x+2]: border = True + if c != cache[y+1][x]: border = True + if c != cache[y+1][x+2]: border = True + if c != cache[y+2][x]: border = True + if c != cache[y+2][x+1]: border = True + if c != cache[y+2][x+2]: border = True + if border: outline_color = outline_color - 0.25 + lines.putpixel((xb+xa, yb), min(lines.getpixel((xb+xa, yb)), int(outline_color * 255.0))) endTime = time.perf_counter() print("Outline detection: " + str(round((endTime - startTime)*1000)/1000) + "s") lines.save("/home/youen/dev_linux/vhelio-render/vhelio-final.png") diff --git a/ahb_cmd_set_part_stage.py b/ahb_cmd_set_part_stage.py index 12fec02..8edf8f4 100644 --- a/ahb_cmd_set_part_stage.py +++ b/ahb_cmd_set_part_stage.py @@ -25,6 +25,8 @@ class AHB_SetPartStage: if obj is not None and obj.TypeId == 'Part::Feature': if not "AssemblyHandbook_Stage" in obj.PropertiesList: obj.addProperty("App::PropertyInteger", "AssemblyHandbook_Stage", "AssemblyHandbook") + if not "AssemblyHandbook_RenderLines" in obj.PropertiesList: + obj.addProperty("App::PropertyBool", "AssemblyHandbook_RenderLines", "AssemblyHandbook") if obj.AssemblyHandbook_Stage != stageId: obj.AssemblyHandbook_Stage = stageId workbench.context.onPartStageChanged(obj)