diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/debug.py | 102 | ||||
| -rw-r--r-- | lib/stitches/auto_fill.py | 10 |
2 files changed, 88 insertions, 24 deletions
diff --git a/lib/debug.py b/lib/debug.py index 05c367f0..13b2f2bc 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -13,23 +13,32 @@ from svg import line_strings_to_path from svg.tags import INKSCAPE_GROUPMODE, INKSCAPE_LABEL +def check_enabled(func): + def decorated(self, *args, **kwargs): + if self.enabled: + func(self, *args, **kwargs) + + return decorated + + class Debug(object): def __init__(self): + self.enabled = False self.last_log_time = None self.current_layer = None + self.group_stack = [] def enable(self): - self.enable_log() - self.enable_debugger() - self.enable_svg() + self.enabled = True + self.init_log() + self.init_debugger() + self.init_svg() - def enable_log(self): - self.log = self._log - self.raw_log = self._raw_log + def init_log(self): self.log_file = open(os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.log"), "w") self.log("Debug logging enabled.") - def enable_debugger(self): + def init_debugger(self): # How to debug Ink/Stitch: # # 1. Install LiClipse (liclipse.com) -- no need to install Eclipse first @@ -58,7 +67,7 @@ class Debug(object): sys.stderr = stderr - def enable_svg(self): + def init_svg(self): self.svg = etree.Element("svg", nsmap=inkex.NSS) atexit.register(self.save_svg) @@ -67,6 +76,7 @@ class Debug(object): with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), "debug.svg"), "w") as debug_svg: tree.write(debug_svg) + @check_enabled def add_layer(self, name="Debug"): layer = etree.Element("g", { INKSCAPE_GROUPMODE: "layer", @@ -75,19 +85,28 @@ class Debug(object): self.svg.append(layer) self.current_layer = layer - def _noop(self, *args, **kwargs): - pass + @check_enabled + def open_group(self, name="Group"): + group = etree.Element("g", { + INKSCAPE_LABEL: name + }) - log = _noop - raw_log = _noop + self.log_svg_element(group) + self.group_stack.append(group) - def _log(self, message, *args): + @check_enabled + def close_group(self): + if self.group_stack: + self.group_stack.pop() + + @check_enabled + def log(self, message, *args): if self.last_log_time: message = "(+%s) %s" % (datetime.now() - self.last_log_time, message) self.raw_log(message, *args) - def _raw_log(self, message, *args): + def raw_log(self, message, *args): now = datetime.now() timestamp = now.isoformat() self.last_log_time = now @@ -97,30 +116,65 @@ class Debug(object): def time(self, func): def decorated(*args, **kwargs): - self.raw_log("entering %s()", func.func_name) - start = time.time() + if self.enabled: + self.raw_log("entering %s()", func.func_name) + start = time.time() + result = func(*args, **kwargs) - end = time.time() - self.raw_log("leaving %s(), duration = %s", func.func_name, round(end - start, 6)) + + if self.enabled: + end = time.time() + self.raw_log("leaving %s(), duration = %s", func.func_name, round(end - start, 6)) + return result return decorated + @check_enabled def log_svg_element(self, element): if self.current_layer is None: self.add_layer() - self.current_layer.append(element) + if self.group_stack: + self.group_stack[-1].append(element) + else: + self.current_layer.append(element) - def log_line_string(self, line_string, color="#000000"): + @check_enabled + def log_line_string(self, line_string, name=None, color=None): """Add a Shapely LineString to the SVG log.""" - self.log_line_strings(self, [line_string]) + self.log_line_strings([line_string], name, color) - def log_line_strings(self, line_strings, color="#000000"): + @check_enabled + def log_line_strings(self, line_strings, name=None, color=None): path = line_strings_to_path(line_strings) - path.set('style', formatStyle({"stroke": color, "stroke-width": "0.3"})) + path.set('style', formatStyle({"stroke": color or "#000000", "stroke-width": "0.3"})) + + if name is not None: + path.set(INKSCAPE_LABEL, name) + self.log_svg_element(path) + @check_enabled + def log_line(self, start, end, name="line", color=None): + self.log_svg_element(etree.Element("path", { + "d": "M%s,%s %s,%s" % (start + end), + "style": formatStyle({"stroke": color or "#000000", "stroke-width": "0.3"}), + INKSCAPE_LABEL: name + })) + + @check_enabled + def log_graph(self, graph, name="Graph", color=None): + self.open_group(name) + + for edge in graph.edges: + self.log_line(edge[0], edge[1], color=color) + + self.close_group() + debug = Debug() -enable = debug.enable + + +def enable(): + debug.enable() diff --git a/lib/stitches/auto_fill.py b/lib/stitches/auto_fill.py index 496ac442..2a48b263 100644 --- a/lib/stitches/auto_fill.py +++ b/lib/stitches/auto_fill.py @@ -130,6 +130,8 @@ def build_fill_stitch_graph(shape, angle, row_spacing, end_row_spacing): path must exist. """ + debug.add_layer("auto-fill fill stitch") + # Convert the shape into a set of parallel line segments. rows_of_segments = intersect_region_with_grating(shape, angle, row_spacing, end_row_spacing) segments = [segment for row in rows_of_segments for segment in row] @@ -160,6 +162,8 @@ def build_fill_stitch_graph(shape, angle, row_spacing, end_row_spacing): if data['index'] % 2 == 0: graph.add_edge(node1, node2, key="extra") + debug.log_graph(graph, "graph") + return graph @@ -228,6 +232,10 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath): grating1 = shgeo.MultiLineString(list(chain(*intersect_region_with_grating(shape, fill_stitch_angle + math.pi / 4, 2 * PIXELS_PER_MM)))) grating2 = shgeo.MultiLineString(list(chain(*intersect_region_with_grating(shape, fill_stitch_angle - math.pi / 4, 2 * PIXELS_PER_MM)))) + debug.add_layer("auto-fill travel") + debug.log_line_strings(grating1, "grating1") + debug.log_line_strings(grating2, "grating2") + # We'll add the endpoints of the crosshatch grating lines too These # will all be on the outline of the shape. This will ensure that a # path traveling inside the shape can reach its target on the outline, @@ -283,6 +291,8 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath): # <bound method STRtree.__del__ of <shapely.strtree.STRtree instance at 0x0D2BFD50>> ignored del rtree + debug.log_graph(graph, "travel graph") + return graph |
