summaryrefslogtreecommitdiff
path: root/lib/elements
diff options
context:
space:
mode:
Diffstat (limited to 'lib/elements')
-rw-r--r--lib/elements/element.py110
-rw-r--r--lib/elements/fill_stitch.py24
2 files changed, 109 insertions, 25 deletions
diff --git a/lib/elements/element.py b/lib/elements/element.py
index 692d8228..5e8bb072 100644
--- a/lib/elements/element.py
+++ b/lib/elements/element.py
@@ -2,7 +2,6 @@
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
-
import sys
from copy import deepcopy
import numpy as np
@@ -11,12 +10,15 @@ import inkex
from inkex import bezier
from ..commands import find_commands
+from ..debug import debug
from ..i18n import _
-from ..patterns import apply_patterns
+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)
from ..svg.tags import INKSCAPE_LABEL, INKSTITCH_ATTRIBS
from ..utils import Point, cache
+from ..utils.cache import get_stitch_plan_cache, CacheKeyGenerator
class Param(object):
@@ -189,9 +191,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):
@@ -392,21 +391,100 @@ 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()
+ @debug.time
+ 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
+
+ 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.
+
+ This function may be overridden in a subclass.
+ """
+ return False
+
+ @debug.time
+ def _save_cached_stitch_groups(self, stitch_groups, previous_stitch):
+ stitch_plan_cache = get_stitch_plan_cache()
+ 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
+ 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 = {}
+ for param in self.get_params():
+ params[param.name] = self.get_param(param.name, param.default)
+
+ return params
+
+ @cache
+ 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__)
+ 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)
+ 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}")
+
+ 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:
+ previous_stitch = None
+ stitch_groups = self._load_cached_stitch_groups(previous_stitch)
+
+ 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, previous_stitch)
- return patches
+ debug.log(f"ending {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}")
+ return stitch_groups
def fatal(self, message, point_to_troubleshoot=False):
label = self.node.get(INKSCAPE_LABEL)
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