diff options
| author | Kaalleen <36401965+kaalleen@users.noreply.github.com> | 2024-03-26 07:10:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-26 07:10:40 +0100 |
| commit | 8e70f3d2feaab8a14f775a5ef84002bdab9688f0 (patch) | |
| tree | f34d46ead8eb8d15317dc98de4113ccaf8acd0e9 /lib/stitch_plan | |
| parent | ea394f6d3b7de3bc0818b6a9921f64e8bcbc4fbf (diff) | |
Add object based min stitch length (#2792)
* add object based min stitch length (overwrites global)
* add object based minimum jump stitch (overwrites global)
* rename patches to stitch_groups
Diffstat (limited to 'lib/stitch_plan')
| -rw-r--r-- | lib/stitch_plan/color_block.py | 4 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch.py | 42 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_group.py | 21 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_plan.py | 24 |
4 files changed, 70 insertions, 21 deletions
diff --git a/lib/stitch_plan/color_block.py b/lib/stitch_plan/color_block.py index a0f9d43f..3cec826d 100644 --- a/lib/stitch_plan/color_block.py +++ b/lib/stitch_plan/color_block.py @@ -112,6 +112,7 @@ class ColorBlock(object): if min_stitch_len is None: min_stitch_len = 0.1 + min_stitch_len *= PIXELS_PER_MM stitches = [self.stitches[0]] for stitch in self.stitches[1:]: @@ -123,7 +124,8 @@ class ColorBlock(object): pass else: length = (stitch - stitches[-1]).length() - if length <= min_stitch_len * PIXELS_PER_MM: + min_length = stitch.min_stitch_length or min_stitch_len + if length <= min_length: # duplicate stitch, skip this one continue diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 8ad699c7..ffa944ae 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -11,7 +11,17 @@ from ..utils.geometry import Point class Stitch(Point): """A stitch is a Point with extra information telling how to sew it.""" - def __init__(self, x, y=None, color=None, jump=False, stop=False, trim=False, color_change=False, tags=None): + def __init__( + self, + x, y=None, + color=None, + jump=False, + stop=False, + trim=False, + color_change=False, + min_stitch_length=None, + tags=None + ): # DANGER: if you add new attributes, you MUST also set their default # values in __new__() below. Otherwise, cached stitch plans can be # loaded and create objects without those properties defined, because @@ -37,6 +47,7 @@ class Stitch(Point): self._set('trim', trim, base_stitch) self._set('stop', stop, base_stitch) self._set('color_change', color_change, base_stitch) + self._set('min_stitch_length', min_stitch_length, base_stitch) self.tags = set() self.add_tags(tags or []) @@ -52,13 +63,16 @@ class Stitch(Point): return instance def __repr__(self): - return "Stitch(%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 " ", - "COLOR CHANGE" if self.color_change else " ") + return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s)" % ( + self.x, + self.y, + self.color, + self.min_stitch_length, + "JUMP" if self.jump else " ", + "TRIM" if self.trim else " ", + "STOP" if self.stop 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 @@ -95,7 +109,17 @@ class Stitch(Point): return tag in self.tags def copy(self): - return Stitch(self.x, self.y, self.color, self.jump, self.stop, self.trim, self.color_change, self.tags) + return Stitch( + self.x, + self.y, + self.color, + self.jump, + self.stop, + self.trim, + self.color_change, + self.min_stitch_length, + self.tags + ) def offset(self, offset: Point): out = self.copy() diff --git a/lib/stitch_plan/stitch_group.py b/lib/stitch_plan/stitch_group.py index c85fc5f5..0730101c 100644 --- a/lib/stitch_plan/stitch_group.py +++ b/lib/stitch_plan/stitch_group.py @@ -17,8 +17,17 @@ class StitchGroup: between them by the stitch plan generation code. """ - def __init__(self, color=None, stitches=None, trim_after=False, stop_after=False, - lock_stitches=(None, None), force_lock_stitches=False, tags=None): + def __init__( + self, + color=None, + stitches=None, + min_jump_stitch_length=False, + trim_after=False, + stop_after=False, + lock_stitches=(None, None), + force_lock_stitches=False, + tags=None + ): # DANGER: if you add new attributes, you MUST also set their default # values in __new__() below. Otherwise, cached stitch plans can be # loaded and create objects without those properties defined, because @@ -29,6 +38,7 @@ class StitchGroup: self.stop_after = stop_after self.lock_stitches = lock_stitches self.force_lock_stitches = force_lock_stitches + self.min_jump_stitch_length = min_jump_stitch_length self.stitches = [] if stitches: @@ -55,9 +65,13 @@ class StitchGroup: raise TypeError("StitchGroup can only be added to another StitchGroup") def __len__(self): - # This method allows `len(patch)` and `if patch: + # This method allows `len(stitch_group)` and `if stitch_group: return len(self.stitches) + def set_minimum_stitch_length(self, min_stitch_length): + for stitch in self.stitches: + stitch.min_stitch_length = min_stitch_length + def add_stitches(self, stitches, tags=None): for stitch in stitches: self.add_stitch(stitch, tags=tags) @@ -66,7 +80,6 @@ class StitchGroup: if not isinstance(stitch, Stitch): # probably a Point stitch = Stitch(stitch, tags=tags) - self.stitches.append(stitch) def reverse(self): diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 8067749a..f6e3f0de 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -59,13 +59,23 @@ def stitch_groups_to_stitch_plan(stitch_groups, collapse_len=None, min_stitch_le # make a new block of our color color_block = stitch_plan.new_color_block(color=stitch_group.color) else: - if (len(color_block) and not need_tie_in and - ((stitch_group.stitches[0] - color_block.stitches[-1]).length() > collapse_len or - previous_stitch_group.force_lock_stitches)): - lock_stitches = previous_stitch_group.get_lock_stitches("end", disable_ties) - if lock_stitches: - color_block.add_stitches(stitches=lock_stitches) - need_tie_in = True + add_lock = False + if len(color_block) and not need_tie_in: + distance_to_previous_stitch = (stitch_group.stitches[0] - color_block.stitches[-1]).length() + if previous_stitch_group.force_lock_stitches: + add_lock = True + elif previous_stitch_group.min_jump_stitch_length: + # object based minimum jump stitch length overrides the global collapse_len setting + if distance_to_previous_stitch > previous_stitch_group.min_jump_stitch_length: + add_lock = True + elif distance_to_previous_stitch > collapse_len: + add_lock = True + + if add_lock: + lock_stitches = previous_stitch_group.get_lock_stitches("end", disable_ties) + need_tie_in = True + if lock_stitches: + color_block.add_stitches(stitches=lock_stitches) if need_tie_in is True: lock_stitches = stitch_group.get_lock_stitches("start", disable_ties) |
