From 20e419d79510b5acc80be14c76cd9128ea82afa1 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 23 Jul 2022 11:49:09 -0400 Subject: [PATCH] cache key includes previous stitch --- lib/elements/element.py | 38 +++++++++++++++++++++++++++++-------- lib/elements/fill_stitch.py | 24 ++++++++++++++--------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/elements/element.py b/lib/elements/element.py index 746fc8f62..d64b22602 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 eef8341c9..77b4ac7cf 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