From 47eb81cb0e5402d790380e224638856e0cee5e63 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 15 Jul 2022 23:22:37 -0400 Subject: first draft of stitch plan caching --- lib/elements/element.py | 54 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 692d8228..5df40475 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -2,15 +2,19 @@ # # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - +import atexit +import os import sys from copy import deepcopy import numpy as np +import appdirs +import diskcache import inkex from inkex import bezier from ..commands import find_commands +from ..debug import debug from ..i18n import _ from ..patterns import apply_patterns from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, @@ -392,21 +396,47 @@ class EmbroideryElement(object): def to_stitch_groups(self, last_patch): raise NotImplementedError("%s must implement to_stitch_groups()" % self.__class__.__name__) - def embroider(self, last_patch): - self.validate() + @classmethod + def _get_stitch_plan_cache(cls): + # one cache, shared by all elements, opened once and closed at program exit + try: + return cls._stitch_plan_cache + except AttributeError: + cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') + cls._stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) + atexit.register(cls._stitch_plan_cache.close) + return cls._stitch_plan_cache + + @debug.time + def _load_cached_stitch_groups(self): + stitch_plan_cache = self._get_stitch_plan_cache() + return stitch_plan_cache.get(self.node.get('id')) + + @debug.time + def _save_cached_stitch_groups(self, stitch_groups): + stitch_plan_cache = self._get_stitch_plan_cache() + stitch_plan_cache[self.node.get('id')] = stitch_groups + + def embroider(self, last_stitch_group): + stitch_groups = self._load_cached_stitch_groups() + + if not stitch_groups: + self.validate() + + stitch_groups = self.to_stitch_groups(last_stitch_group) + apply_patterns(stitch_groups, self.node) - patches = self.to_stitch_groups(last_patch) - apply_patterns(patches, self.node) + for stitch_group in stitch_groups: + stitch_group.tie_modus = self.ties + stitch_group.force_lock_stitches = self.force_lock_stitches - for patch in patches: - patch.tie_modus = self.ties - patch.force_lock_stitches = self.force_lock_stitches + if stitch_groups: + stitch_groups[-1].trim_after = self.has_command("trim") or self.trim_after + stitch_groups[-1].stop_after = self.has_command("stop") or self.stop_after - if patches: - patches[-1].trim_after = self.has_command("trim") or self.trim_after - patches[-1].stop_after = self.has_command("stop") or self.stop_after + self._save_cached_stitch_groups(stitch_groups) - return patches + return stitch_groups def fatal(self, message, point_to_troubleshoot=False): label = self.node.get(INKSCAPE_LABEL) -- cgit v1.3.1 From 0e225277dbb57bdaf850a0c67b4fc988051af800 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 16 Jul 2022 15:44:01 -0400 Subject: move get_stitch_plan_cache() to utils.cache --- lib/elements/element.py | 21 +++------------------ lib/utils/cache.py | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 20 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 5df40475..f285258b 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -2,14 +2,10 @@ # # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. -import atexit -import os import sys from copy import deepcopy import numpy as np -import appdirs -import diskcache import inkex from inkex import bezier @@ -21,6 +17,7 @@ from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, get_node_transform) from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS from ..utils import Point, cache +from ..utils.cache import get_stitch_plan_cache class Param(object): @@ -396,25 +393,13 @@ class EmbroideryElement(object): def to_stitch_groups(self, last_patch): raise NotImplementedError("%s must implement to_stitch_groups()" % self.__class__.__name__) - @classmethod - def _get_stitch_plan_cache(cls): - # one cache, shared by all elements, opened once and closed at program exit - try: - return cls._stitch_plan_cache - except AttributeError: - cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') - cls._stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) - atexit.register(cls._stitch_plan_cache.close) - return cls._stitch_plan_cache - @debug.time def _load_cached_stitch_groups(self): - stitch_plan_cache = self._get_stitch_plan_cache() - return stitch_plan_cache.get(self.node.get('id')) + return get_stitch_plan_cache().get(self.node.get('id')) @debug.time def _save_cached_stitch_groups(self, stitch_groups): - stitch_plan_cache = self._get_stitch_plan_cache() + stitch_plan_cache = get_stitch_plan_cache() stitch_plan_cache[self.node.get('id')] = stitch_groups def embroider(self, last_stitch_group): diff --git a/lib/utils/cache.py b/lib/utils/cache.py index c0313ebe..46d8ec59 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -2,14 +2,32 @@ # # Copyright (c) 2010 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. +import os +import atexit + +import appdirs +import diskcache try: from functools import lru_cache except ImportError: from backports.functools_lru_cache import lru_cache -# simplify use of lru_cache decorator - +# simplify use of lru_cache decorator def cache(*args, **kwargs): return lru_cache(maxsize=None)(*args, **kwargs) + + +__stitch_plan_cache = None + + +def get_stitch_plan_cache(): + global __stitch_plan_cache + + if __stitch_plan_cache is None: + cache_dir = os.path.join(appdirs.user_config_dir('inkstitch'), 'cache', 'stitch_plan') + __stitch_plan_cache = diskcache.Cache(cache_dir, size=1024 * 1024 * 100) + atexit.register(__stitch_plan_cache.close) + + return __stitch_plan_cache -- cgit v1.3.1 From 44af368c795c2c469eb09fee884675db17b7d6d6 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 21 Jul 2022 23:15:19 -0400 Subject: remove unused and broken has_style() method --- lib/elements/element.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index f285258b..f978ce5a 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -190,9 +190,6 @@ class EmbroideryElement(object): style = None return style - def has_style(self, style_name): - return self._get_style_raw(style_name) is not None - @property @cache def stroke_scale(self): -- cgit v1.3.1 From d51feec98d7b4e4b224c34c013da6df059b78005 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 21 Jul 2022 23:16:56 -0400 Subject: cache key generation using params, path, color, and style --- lib/elements/element.py | 23 ++++++++++++++++++++--- lib/utils/cache.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index f978ce5a..746fc8f6 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -17,7 +17,7 @@ from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, get_node_transform) from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS from ..utils import Point, cache -from ..utils.cache import get_stitch_plan_cache +from ..utils.cache import get_stitch_plan_cache, CacheKeyGenerator class Param(object): @@ -392,12 +392,29 @@ class EmbroideryElement(object): @debug.time def _load_cached_stitch_groups(self): - return get_stitch_plan_cache().get(self.node.get('id')) + return get_stitch_plan_cache().get(self._get_cache_key()) @debug.time def _save_cached_stitch_groups(self, stitch_groups): stitch_plan_cache = get_stitch_plan_cache() - stitch_plan_cache[self.node.get('id')] = stitch_groups + stitch_plan_cache[self._get_cache_key()] = stitch_groups + + def get_params_and_values(self): + params = {} + for param in self.get_params(): + params[param.name] = self.get_param(param.name, param.default) + + return params + + @cache + def _get_cache_key(self): + cache_key_generator = CacheKeyGenerator() + cache_key_generator.update(self.__class__.__name__) + cache_key_generator.update(self.get_params_and_values()) + cache_key_generator.update(self.parse_path()) + cache_key_generator.update(list(self._get_specified_style().items())) + # TODO: include commands and patterns that apply to this element + return cache_key_generator.get_cache_key() def embroider(self, last_stitch_group): stitch_groups = self._load_cached_stitch_groups() diff --git a/lib/utils/cache.py b/lib/utils/cache.py index 46d8ec59..767978ca 100644 --- a/lib/utils/cache.py +++ b/lib/utils/cache.py @@ -4,6 +4,8 @@ # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import os import atexit +import hashlib +import pickle import appdirs import diskcache @@ -31,3 +33,38 @@ def get_stitch_plan_cache(): atexit.register(__stitch_plan_cache.close) return __stitch_plan_cache + + +class CacheKeyGenerator(object): + """Generate cache keys given arbitrary data. + + Given arbitrary data, generate short cache key that is extremely likely + to be unique. + + Use example: + + >>> generator = CacheKeyGenerator() + >>> generator.update(b'12345') + >>> generator.update([1, 2, 3, {4, 5, 6}]) + >>> generator.get_cache_key() + """ + + def __init__(self): + # SHA1 is chosen for speed. We don't need cryptography-grade hashing + # for this use case. + self._hasher = hashlib.sha1() + + def update(self, data): + """Provide data to be hashed into a cache key. + + Arguments: + data -- a bytes object or any object that can be pickled + """ + + if not isinstance(data, bytes): + data = pickle.dumps(data) + + self._hasher.update(data) + + def get_cache_key(self): + return self._hasher.hexdigest() -- cgit v1.3.1 From 20e419d79510b5acc80be14c76cd9128ea82afa1 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 23 Jul 2022 11:49:09 -0400 Subject: cache key includes previous stitch --- lib/elements/element.py | 38 ++++++++++++++++++++++++++++++-------- lib/elements/fill_stitch.py | 24 +++++++++++++++--------- 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 746fc8f6..d64b2260 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -391,13 +391,29 @@ class EmbroideryElement(object): raise NotImplementedError("%s must implement to_stitch_groups()" % self.__class__.__name__) @debug.time - def _load_cached_stitch_groups(self): - return get_stitch_plan_cache().get(self._get_cache_key()) + def _load_cached_stitch_groups(self, previous_stitch): + if not self.uses_previous_stitch(): + # we don't care about the previous stitch + previous_stitch = None + + return get_stitch_plan_cache().get(self._get_cache_key(previous_stitch)) + + def uses_previous_stitch(self): + """Returns True if the previous stitch can affect this Element's stitches. + + This function may be overridden in a subclass. + """ + return False @debug.time - def _save_cached_stitch_groups(self, stitch_groups): + def _save_cached_stitch_groups(self, stitch_groups, previous_stitch): stitch_plan_cache = get_stitch_plan_cache() - stitch_plan_cache[self._get_cache_key()] = stitch_groups + stitch_plan_cache[self._get_cache_key(previous_stitch)] = stitch_groups + + if previous_stitch is not None: + # Also store it with None as the previous stitch, so that it can be used next time + # if we don't care about the previous stitch + stitch_plan_cache[self._get_cache_key(None)] = stitch_groups def get_params_and_values(self): params = {} @@ -406,18 +422,24 @@ class EmbroideryElement(object): return params - @cache - def _get_cache_key(self): + def _get_cache_key(self, previous_stitch): cache_key_generator = CacheKeyGenerator() cache_key_generator.update(self.__class__.__name__) cache_key_generator.update(self.get_params_and_values()) cache_key_generator.update(self.parse_path()) cache_key_generator.update(list(self._get_specified_style().items())) + cache_key_generator.update(previous_stitch) + # TODO: include commands and patterns that apply to this element + return cache_key_generator.get_cache_key() def embroider(self, last_stitch_group): - stitch_groups = self._load_cached_stitch_groups() + if last_stitch_group: + previous_stitch = last_stitch_group.stitches[-1] + else: + previous_stitch = None + stitch_groups = self._load_cached_stitch_groups(previous_stitch) if not stitch_groups: self.validate() @@ -433,7 +455,7 @@ class EmbroideryElement(object): stitch_groups[-1].trim_after = self.has_command("trim") or self.trim_after stitch_groups[-1].stop_after = self.has_command("stop") or self.stop_after - self._save_cached_stitch_groups(stitch_groups) + self._save_cached_stitch_groups(stitch_groups, previous_stitch) return stitch_groups diff --git a/lib/elements/fill_stitch.py b/lib/elements/fill_stitch.py index eef8341c..77b4ac7c 100644 --- a/lib/elements/fill_stitch.py +++ b/lib/elements/fill_stitch.py @@ -530,24 +530,30 @@ class FillStitch(EmbroideryElement): def fill_shape(self, shape): return self.shrink_or_grow_shape(shape, self.expand) - def get_starting_point(self, last_patch): + def get_starting_point(self, previous_stitch_group): # If there is a "fill_start" Command, then use that; otherwise pick # the point closest to the end of the last patch. if self.get_command('fill_start'): return self.get_command('fill_start').target_point - elif last_patch: - return last_patch.stitches[-1] + elif previous_stitch_group: + return previous_stitch_group.stitches[-1] else: return None + def uses_previous_stitch(self): + if self.get_command('fill_start'): + return False + else: + return True + def get_ending_point(self): if self.get_command('fill_end'): return self.get_command('fill_end').target_point else: return None - def to_stitch_groups(self, last_patch): # noqa: C901 + def to_stitch_groups(self, previous_stitch_group): # noqa: C901 # backwards compatibility: legacy_fill used to be inkstitch:auto_fill == False if not self.auto_fill or self.fill_method == 3: return self.do_legacy_fill() @@ -556,7 +562,7 @@ class FillStitch(EmbroideryElement): end = self.get_ending_point() for shape in self.shape.geoms: - start = self.get_starting_point(last_patch) + start = self.get_starting_point(previous_stitch_group) try: if self.fill_underlay: underlay_shapes = self.underlay_shape(shape) @@ -567,16 +573,16 @@ class FillStitch(EmbroideryElement): fill_shapes = self.fill_shape(shape) for fill_shape in fill_shapes.geoms: if self.fill_method == 0: - stitch_groups.extend(self.do_auto_fill(fill_shape, last_patch, start, end)) + stitch_groups.extend(self.do_auto_fill(fill_shape, previous_stitch_group, start, end)) if self.fill_method == 1: - stitch_groups.extend(self.do_contour_fill(fill_shape, last_patch, start)) + stitch_groups.extend(self.do_contour_fill(fill_shape, previous_stitch_group, start)) elif self.fill_method == 2: - stitch_groups.extend(self.do_guided_fill(fill_shape, last_patch, start, end)) + stitch_groups.extend(self.do_guided_fill(fill_shape, previous_stitch_group, start, end)) except ExitThread: raise except Exception: self.fatal_fill_error() - last_patch = stitch_groups[-1] + previous_stitch_group = stitch_groups[-1] return stitch_groups -- cgit v1.3.1 From 89441d3d59759301397a6a3666b2f9d695be1ef8 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 23 Jul 2022 11:56:27 -0400 Subject: use commands in cache key --- lib/elements/element.py | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index d64b2260..6dc60f34 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -429,6 +429,7 @@ class EmbroideryElement(object): cache_key_generator.update(self.parse_path()) cache_key_generator.update(list(self._get_specified_style().items())) cache_key_generator.update(previous_stitch) + cache_key_generator.update([(c.command, c.target_point) for c in self.commands]) # TODO: include commands and patterns that apply to this element -- cgit v1.3.1 From a0834e2e7c2f2b6734877a8ee922c01e8c9330ba Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 23 Jul 2022 12:10:37 -0400 Subject: use patterns in cache key --- lib/elements/element.py | 7 ++++++- lib/patterns.py | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 6dc60f34..7164c17c 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -12,7 +12,7 @@ from inkex import bezier from ..commands import find_commands from ..debug import debug from ..i18n import _ -from ..patterns import apply_patterns +from ..patterns import apply_patterns, get_patterns_cache_key_data from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, get_node_transform) from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS @@ -422,6 +422,10 @@ class EmbroideryElement(object): return params + @cache + def _get_patterns_cache_key_data(self): + return get_patterns_cache_key_data(self.node) + def _get_cache_key(self, previous_stitch): cache_key_generator = CacheKeyGenerator() cache_key_generator.update(self.__class__.__name__) @@ -430,6 +434,7 @@ class EmbroideryElement(object): cache_key_generator.update(list(self._get_specified_style().items())) cache_key_generator.update(previous_stitch) cache_key_generator.update([(c.command, c.target_point) for c in self.commands]) + cache_key_generator.update(self._get_patterns_cache_key_data()) # TODO: include commands and patterns that apply to this element diff --git a/lib/patterns.py b/lib/patterns.py index 136b988f..da8c50bf 100644 --- a/lib/patterns.py +++ b/lib/patterns.py @@ -10,6 +10,15 @@ from .stitch_plan import Stitch from .utils import Point +def get_patterns_cache_key_data(node): + patterns = get_marker_elements(node, "pattern") + data = [] + data.extend([fill.wkt for fill in patterns['fill']]) + data.extend([stroke.wkt for stroke in patterns['stroke']]) + + return data + + def apply_patterns(stitch_groups, node): patterns = get_marker_elements(node, "pattern") _apply_fill_patterns(patterns['fill'], stitch_groups) -- cgit v1.3.1 From 2865f4161e4b2e8dc63177ef60a83820ea4f761d Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 4 Aug 2022 21:17:41 -0400 Subject: consistent cache key for Stitch objects --- lib/elements/element.py | 25 ++++++++++++++++++++----- lib/stitch_plan/stitch.py | 30 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 15 deletions(-) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 7164c17c..84a9199b 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -396,7 +396,15 @@ class EmbroideryElement(object): # we don't care about the previous stitch previous_stitch = None - return get_stitch_plan_cache().get(self._get_cache_key(previous_stitch)) + cache_key = self._get_cache_key(previous_stitch) + stitch_groups = get_stitch_plan_cache().get(cache_key) + + if stitch_groups: + debug.log(f"used cache for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") + else: + debug.log(f"did not use cache for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}, key={cache_key}") + + return stitch_groups def uses_previous_stitch(self): """Returns True if the previous stitch can affect this Element's stitches. @@ -408,12 +416,16 @@ class EmbroideryElement(object): @debug.time def _save_cached_stitch_groups(self, stitch_groups, previous_stitch): stitch_plan_cache = get_stitch_plan_cache() - stitch_plan_cache[self._get_cache_key(previous_stitch)] = stitch_groups + cache_key = self._get_cache_key(previous_stitch) + if cache_key not in stitch_plan_cache: + stitch_plan_cache[cache_key] = stitch_groups if previous_stitch is not None: # Also store it with None as the previous stitch, so that it can be used next time # if we don't care about the previous stitch - stitch_plan_cache[self._get_cache_key(None)] = stitch_groups + cache_key = self._get_cache_key(None) + if cache_key not in stitch_plan_cache: + stitch_plan_cache[cache_key] = stitch_groups def get_params_and_values(self): params = {} @@ -436,11 +448,13 @@ class EmbroideryElement(object): cache_key_generator.update([(c.command, c.target_point) for c in self.commands]) cache_key_generator.update(self._get_patterns_cache_key_data()) - # TODO: include commands and patterns that apply to this element + cache_key = cache_key_generator.get_cache_key() + debug.log(f"cache key for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)} {previous_stitch}: {cache_key}") - return cache_key_generator.get_cache_key() + return cache_key def embroider(self, last_stitch_group): + debug.log(f"starting {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") if last_stitch_group: previous_stitch = last_stitch_group.stitches[-1] else: @@ -463,6 +477,7 @@ class EmbroideryElement(object): self._save_cached_stitch_groups(stitch_groups, previous_stitch) + debug.log(f"ending {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") return stitch_groups def fatal(self, message, point_to_troubleshoot=False): diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 67a0fd64..c1553ce5 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -55,16 +55,17 @@ class Stitch(Point): return instance def __repr__(self): - return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, - self.y, - self.color, - "JUMP" if self.jump else " ", - "TRIM" if self.trim else " ", - "STOP" if self.stop else " ", - "TIE MODUS" if self.tie_modus else " ", - "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", - "NO TIES" if self.no_ties else " ", - "COLOR CHANGE" if self.color_change else " ") + return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, + self.y, + self.color, + self.tags, + "JUMP" if self.jump else " ", + "TRIM" if self.trim else " ", + "STOP" if self.stop else " ", + self.tie_modus, + "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", + "NO TIES" if self.no_ties else " ", + "COLOR CHANGE" if self.color_change else " ") def _set(self, attribute, value, base_stitch): # Set an attribute. If the caller passed a Stitch object, use its value, unless @@ -104,3 +105,12 @@ class Stitch(Point): attributes = dict(vars(self)) attributes['tags'] = list(attributes['tags']) return attributes + + def __getstate__(self): + # This is used by pickle. We want to sort the tag list so that the + # pickled representation is stable, since it's used to generate cache + # keys. + state = self.__json__() + state['tags'].sort() + + return state -- cgit v1.3.1 From aa65a2bf3fb747dc89e2d905f1fc45b269b5cab4 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 6 Aug 2022 22:59:00 -0400 Subject: include guides in cache key --- lib/elements/element.py | 6 ++++++ lib/marker.py | 10 ++++++++++ 2 files changed, 16 insertions(+) (limited to 'lib/elements') diff --git a/lib/elements/element.py b/lib/elements/element.py index 84a9199b..5e8bb072 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -12,6 +12,7 @@ from inkex import bezier from ..commands import find_commands from ..debug import debug from ..i18n import _ +from ..marker import get_marker_elements_cache_key_data from ..patterns import apply_patterns, get_patterns_cache_key_data from ..svg import (PIXELS_PER_MM, apply_transforms, convert_length, get_node_transform) @@ -438,6 +439,10 @@ class EmbroideryElement(object): def _get_patterns_cache_key_data(self): return get_patterns_cache_key_data(self.node) + @cache + def _get_guides_cache_key_data(self): + return get_marker_elements_cache_key_data(self.node, "guide-line") + def _get_cache_key(self, previous_stitch): cache_key_generator = CacheKeyGenerator() cache_key_generator.update(self.__class__.__name__) @@ -447,6 +452,7 @@ class EmbroideryElement(object): cache_key_generator.update(previous_stitch) cache_key_generator.update([(c.command, c.target_point) for c in self.commands]) cache_key_generator.update(self._get_patterns_cache_key_data()) + cache_key_generator.update(self._get_guides_cache_key_data()) cache_key = cache_key_generator.get_cache_key() debug.log(f"cache key for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)} {previous_stitch}: {cache_key}") diff --git a/lib/marker.py b/lib/marker.py index 5d4637fe..977475b0 100644 --- a/lib/marker.py +++ b/lib/marker.py @@ -78,6 +78,16 @@ def get_marker_elements(node, marker, get_fills=True, get_strokes=True, get_sati return {'fill': fills, 'stroke': strokes, 'satin': satins} +def get_marker_elements_cache_key_data(node, marker): + marker_elements = get_marker_elements(node, marker, True, True, True) + + marker_elements['fill'] = [shape.wkt for shape in marker_elements['fill']] + marker_elements['stroke'] = [shape.wkt for shape in marker_elements['stroke']] + marker_elements['satin'] = [satin.csp for satin in marker_elements['satin']] + + return marker_elements + + def has_marker(node, marker=list()): if not marker: marker = MARKER -- cgit v1.3.1