From 2cd4963d09ef78dd25a7401cc47a69474d7fa952 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sun, 15 Jul 2018 22:53:18 -0400 Subject: adjust stitch plan code for pyembroidery --- lib/stitch_plan/stitch_plan.py | 94 +++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 38 deletions(-) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 742916f0..1a466295 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -16,64 +16,40 @@ def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): """ stitch_plan = StitchPlan() - color_block = stitch_plan.new_color_block() - need_trim = False + if not patches: + return stitch_plan + + color_block = stitch_plan.new_color_block(color=patches[0].color) + for patch in patches: if not patch.stitches: continue - if not color_block.has_color(): - # set the color for the first color block - color_block.color = patch.color - - if color_block.color == patch.color: - if need_trim: - process_trim(color_block, patch.stitches[0]) - need_trim = False - - # add a jump stitch between patches if the distance is more - # than the collapse length - if color_block.last_stitch: - if (patch.stitches[0] - color_block.last_stitch).length() > collapse_len: - color_block.add_stitch(patch.stitches[0].x, patch.stitches[0].y, jump=True) - - else: + if color_block.color != patch.color or color_block.stop_after: # add a color change (only if we didn't just do a "STOP after") - if not color_block.last_stitch.color_change: - stitch = color_block.last_stitch.copy() - stitch.color_change = True - color_block.add_stitch(stitch) + if not color_block.stop_after: + color_block.add_stitch(color_change=True) - color_block = stitch_plan.new_color_block() - color_block.color = patch.color + color_block = stitch_plan.new_color_block(color=patch.color) color_block.filter_duplicate_stitches() color_block.add_stitches(patch.stitches, no_ties=patch.stitch_as_is) if patch.trim_after: - # a trim needs to be followed by a jump to the next stitch, so - # we'll process it when we start the next patch - need_trim = True + color_block.add_stitch(trim=True) if patch.stop_after: - process_stop(color_block) + process_stop(stitch_plan) + + # process_stop() may have split the block into two + color_block = stitch_plan.last_color_block - add_jumps(stitch_plan) add_ties(stitch_plan) return stitch_plan -def add_jumps(stitch_plan): - """Add a JUMP stitch at the start of each color block.""" - - for color_block in stitch_plan: - stitch = color_block.stitches[0].copy() - stitch.jump = True - color_block.stitches.insert(0, stitch) - - class StitchPlan(object): """Holds a set of color blocks, each containing stitches.""" @@ -85,6 +61,9 @@ class StitchPlan(object): self.color_blocks.append(color_block) return color_block + def add_color_block(self, color_block): + self.color_blocks.append(color_block) + def __iter__(self): return iter(self.color_blocks) @@ -99,6 +78,10 @@ class StitchPlan(object): """Number of unique colors in the stitch plan.""" return len({block.color for block in self}) + @property + def num_color_blocks(self): + return len(self.color_blocks) + @property def num_stops(self): return sum(block.num_stops for block in self) @@ -137,6 +120,13 @@ class StitchPlan(object): dimensions = self.dimensions return (dimensions[0] / PIXELS_PER_MM, dimensions[1] / PIXELS_PER_MM) + @property + def last_color_block(self): + if self.color_blocks: + return self.color_blocks[-1] + else: + return None + class ColorBlock(object): """Holds a set of stitches, all with the same thread color.""" @@ -148,6 +138,9 @@ class ColorBlock(object): def __iter__(self): return iter(self.stitches) + def __len__(self): + return len(self.stitches) + def __repr__(self): return "ColorBlock(%s, %s)" % (self.color, self.stitches) @@ -191,6 +184,13 @@ class ColorBlock(object): return sum(1 for stitch in self if stitch.trim) + @property + def stop_after(self): + if self.last_stitch is not None: + return self.last_stitch.stop + else: + return False + def filter_duplicate_stitches(self): if not self.stitches: return @@ -212,11 +212,21 @@ class ColorBlock(object): self.stitches = stitches def add_stitch(self, *args, **kwargs): + if not args: + # They're adding a command, e.g. `color_block.add_stitch(stop=True)``. + # Use the position from the last stitch. + if self.last_stitch: + args = (self.last_stitch.x, self.last_stitch.y) + else: + raise ValueError("internal error: can't add a command to an empty stitch block") + if isinstance(args[0], Stitch): self.stitches.append(args[0]) elif isinstance(args[0], Point): self.stitches.append(Stitch(args[0].x, args[0].y, *args[1:], **kwargs)) else: + if not args and self.last_stitch: + args = (self.last_stitch.x, self.last_stitch.y) self.stitches.append(Stitch(*args, **kwargs)) def add_stitches(self, stitches, *args, **kwargs): @@ -237,3 +247,11 @@ class ColorBlock(object): maxy = max(stitch.y for stitch in self) return minx, miny, maxx, maxy + + def split_at(self, index): + """Split this color block into two at the specified stitch index""" + + new_color_block = ColorBlock(self.color, self.stitches[index:]) + del self.stitches[index:] + + return new_color_block -- cgit v1.2.3 From 754bf54897e309fa21fa61bc7a626cde71a00f97 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sun, 15 Jul 2018 23:01:52 -0400 Subject: fix gap caused by splitting block --- lib/stitch_plan/stitch_plan.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 1a466295..5d847ad2 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -1,3 +1,5 @@ +from copy import copy + from .stitch import Stitch from .stop import process_stop from .trim import process_trim @@ -254,4 +256,11 @@ class ColorBlock(object): new_color_block = ColorBlock(self.color, self.stitches[index:]) del self.stitches[index:] + # If we're splitting in the middle of a run of stitches, we don't + # want a gap to appear in the preview and the PDF printout, so + # add an extra stitch to bridge the gap. Technically this will + # result in a double needle penetration but it's no big deal. + if not self.last_stitch.trim: + self.add_stitch(copy(new_color_block.stitches[0])) + return new_color_block -- cgit v1.2.3 From b1912157579299212131b86f0b7267d7d91df047 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 16 Jul 2018 19:38:42 -0400 Subject: tidy up code --- lib/stitch_plan/stitch_plan.py | 9 --------- 1 file changed, 9 deletions(-) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 5d847ad2..1a466295 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -1,5 +1,3 @@ -from copy import copy - from .stitch import Stitch from .stop import process_stop from .trim import process_trim @@ -256,11 +254,4 @@ class ColorBlock(object): new_color_block = ColorBlock(self.color, self.stitches[index:]) del self.stitches[index:] - # If we're splitting in the middle of a run of stitches, we don't - # want a gap to appear in the preview and the PDF printout, so - # add an extra stitch to bridge the gap. Technically this will - # result in a double needle penetration but it's no big deal. - if not self.last_stitch.trim: - self.add_stitch(copy(new_color_block.stitches[0])) - return new_color_block -- cgit v1.2.3 From 5ce8df77a07b11b902792d299d4cb89b6951ccd8 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 20 Jul 2018 21:41:28 -0400 Subject: remove incorrect stop logic --- lib/stitch_plan/stitch_plan.py | 47 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 1a466295..0fa87d71 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -1,6 +1,4 @@ from .stitch import Stitch -from .stop import process_stop -from .trim import process_trim from .ties import add_ties from ..svg import PIXELS_PER_MM from ..utils.geometry import Point @@ -26,26 +24,29 @@ def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): if not patch.stitches: continue - if color_block.color != patch.color or color_block.stop_after: - # add a color change (only if we didn't just do a "STOP after") - if not color_block.stop_after: + if color_block.color != patch.color: + if len(color_block) == 0: + # We just processed a stop, which created a new color block. + # We'll just claim this new block as ours: + color_block.color = patch.color + else: + # end the previous block with a color change color_block.add_stitch(color_change=True) - color_block = stitch_plan.new_color_block(color=patch.color) + # make a new block of our color + color_block = stitch_plan.new_color_block(color=patch.color) - color_block.filter_duplicate_stitches() color_block.add_stitches(patch.stitches, no_ties=patch.stitch_as_is) if patch.trim_after: color_block.add_stitch(trim=True) if patch.stop_after: - process_stop(stitch_plan) - - # process_stop() may have split the block into two - color_block = stitch_plan.last_color_block + color_block.add_stitch(stop=True) + color_block = stitch_plan.new_color_block(color_block.color) - add_ties(stitch_plan) + stitch_plan.filter_duplicate_stitches() + stitch_plan.add_ties() return stitch_plan @@ -64,6 +65,14 @@ class StitchPlan(object): def add_color_block(self, color_block): self.color_blocks.append(color_block) + def filter_duplicate_stitches(self): + for color_block in self: + color_block.filter_duplicate_stitches() + + def add_ties(self): + # see ties.py + add_ties(self) + def __iter__(self): return iter(self.color_blocks) @@ -198,12 +207,12 @@ class ColorBlock(object): stitches = [self.stitches[0]] for stitch in self.stitches[1:]: - if stitches[-1].jump or stitch.stop or stitch.trim: - # Don't consider jumps, stops, or trims as candidates for filtering + if stitches[-1].jump or stitch.stop or stitch.trim or stitch.color_change: + # Don't consider jumps, stops, color changes, or trims as candidates for filtering pass else: l = (stitch - stitches[-1]).length() - if l <= 0.1: + if l <= 0.1 * PIXELS_PER_MM: # duplicate stitch, skip this one continue @@ -247,11 +256,3 @@ class ColorBlock(object): maxy = max(stitch.y for stitch in self) return minx, miny, maxx, maxy - - def split_at(self, index): - """Split this color block into two at the specified stitch index""" - - new_color_block = ColorBlock(self.color, self.stitches[index:]) - del self.stitches[index:] - - return new_color_block -- cgit v1.2.3 From e0cecd6fa444e3b7ea2391ce4f2953ae9fd5f3fa Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 23 Jul 2018 20:15:49 -0400 Subject: fix a couple crashes --- lib/stitch_plan/stitch_plan.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index 0fa87d71..a7cd60e8 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -45,6 +45,10 @@ def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): color_block.add_stitch(stop=True) color_block = stitch_plan.new_color_block(color_block.color) + if len(color_block) == 0: + # last block ended in a stop, so now we have an empty block + del stitch_plan.color_blocks[-1] + stitch_plan.filter_duplicate_stitches() stitch_plan.add_ties() -- cgit v1.2.3 From 1bd7aa110a1b30a6c44f4d792b3c817e10234c07 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 23 Jul 2018 20:22:53 -0400 Subject: change '# stops' in block to be 'stop after?' --- lib/stitch_plan/stitch_plan.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'lib/stitch_plan/stitch_plan.py') diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py index a7cd60e8..682ea09f 100644 --- a/lib/stitch_plan/stitch_plan.py +++ b/lib/stitch_plan/stitch_plan.py @@ -97,7 +97,7 @@ class StitchPlan(object): @property def num_stops(self): - return sum(block.num_stops for block in self) + return sum(1 for block in self if block.stop_after) @property def num_trims(self): @@ -185,12 +185,6 @@ class ColorBlock(object): """Number of stitches in this color block.""" return len(self.stitches) - @property - def num_stops(self): - """Number of pauses in this color block.""" - - return sum(1 for stitch in self if stitch.stop) - @property def num_trims(self): """Number of trims in this color block.""" -- cgit v1.2.3