diff options
Diffstat (limited to 'lib/stitch_plan')
| -rw-r--r-- | lib/stitch_plan/__init__.py | 2 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch.py | 15 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch_plan.py | 228 | ||||
| -rw-r--r-- | lib/stitch_plan/stop.py | 27 | ||||
| -rw-r--r-- | lib/stitch_plan/ties.py | 53 | ||||
| -rw-r--r-- | lib/stitch_plan/trim.py | 23 |
6 files changed, 348 insertions, 0 deletions
diff --git a/lib/stitch_plan/__init__.py b/lib/stitch_plan/__init__.py new file mode 100644 index 00000000..791a5f20 --- /dev/null +++ b/lib/stitch_plan/__init__.py @@ -0,0 +1,2 @@ +from stitch_plan import patches_to_stitch_plan, StitchPlan, ColorBlock +from .stitch import Stitch diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py new file mode 100644 index 00000000..6a8579c2 --- /dev/null +++ b/lib/stitch_plan/stitch.py @@ -0,0 +1,15 @@ +from ..utils.geometry import Point + + +class Stitch(Point): + def __init__(self, x, y, color=None, jump=False, stop=False, trim=False, no_ties=False): + self.x = x + self.y = y + self.color = color + self.jump = jump + self.trim = trim + self.stop = stop + self.no_ties = no_ties + + 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 " ", "NO TIES" if self.no_ties else " ") diff --git a/lib/stitch_plan/stitch_plan.py b/lib/stitch_plan/stitch_plan.py new file mode 100644 index 00000000..570a7645 --- /dev/null +++ b/lib/stitch_plan/stitch_plan.py @@ -0,0 +1,228 @@ +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 +from ..threads import ThreadColor + + +def patches_to_stitch_plan(patches, collapse_len=3.0 * PIXELS_PER_MM): + """Convert a collection of inkstitch.element.Patch objects to a StitchPlan. + + * applies instructions embedded in the Patch such as trim_after and stop_after + * adds tie-ins and tie-offs + * adds jump-stitches between patches if necessary + """ + + stitch_plan = StitchPlan() + color_block = stitch_plan.new_color_block() + + need_trim = False + for patch in patches: + if not patch.stitches: + continue + + if need_trim: + process_trim(color_block, patch.stitches[0]) + need_trim = False + + 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: + # 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: + # add a color change + color_block.add_stitch(color_block.last_stitch.x, color_block.last_stitch.y, stop=True) + color_block = stitch_plan.new_color_block() + 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 + + if patch.stop_after: + process_stop(color_block) + + add_ties(stitch_plan) + + return stitch_plan + + +class StitchPlan(object): + """Holds a set of color blocks, each containing stitches.""" + + def __init__(self): + self.color_blocks = [] + + def new_color_block(self, *args, **kwargs): + color_block = ColorBlock(*args, **kwargs) + self.color_blocks.append(color_block) + return color_block + + def __iter__(self): + return iter(self.color_blocks) + + def __len__(self): + return len(self.color_blocks) + + def __repr__(self): + return "StitchPlan(%s)" % ", ".join(repr(cb) for cb in self.color_blocks) + + @property + def num_colors(self): + """Number of unique colors in the stitch plan.""" + return len({block.color for block in self}) + + @property + def num_stops(self): + return sum(block.num_stops for block in self) + + @property + def num_trims(self): + return sum(block.num_trims for block in self) + + @property + def num_stitches(self): + return sum(block.num_stitches for block in self) + + @property + def bounding_box(self): + color_block_bounding_boxes = [cb.bounding_box for cb in self] + minx = min(bb[0] for bb in color_block_bounding_boxes) + miny = min(bb[1] for bb in color_block_bounding_boxes) + maxx = max(bb[2] for bb in color_block_bounding_boxes) + maxy = max(bb[3] for bb in color_block_bounding_boxes) + + return minx, miny, maxx, maxy + + @property + def dimensions(self): + minx, miny, maxx, maxy = self.bounding_box + return (maxx - minx, maxy - miny) + + @property + def extents(self): + minx, miny, maxx, maxy = self.bounding_box + + return max(-minx, maxx), max(-miny, maxy) + + @property + def dimensions_mm(self): + dimensions = self.dimensions + return (dimensions[0] / PIXELS_PER_MM, dimensions[1] / PIXELS_PER_MM) + + +class ColorBlock(object): + """Holds a set of stitches, all with the same thread color.""" + + def __init__(self, color=None, stitches=None): + self.color = color + self.stitches = stitches or [] + + def __iter__(self): + return iter(self.stitches) + + def __repr__(self): + return "ColorBlock(%s, %s)" % (self.color, self.stitches) + + def has_color(self): + return self._color is not None + + @property + def color(self): + return self._color + + @color.setter + def color(self, value): + if isinstance(value, ThreadColor): + self._color = value + elif value is None: + self._color = None + else: + self._color = ThreadColor(value) + + @property + def last_stitch(self): + if self.stitches: + return self.stitches[-1] + else: + return None + + @property + def num_stitches(self): + """Number of stitches in this color block.""" + return len(self.stitches) + + @property + def num_stops(self): + """Number of pauses in this color block.""" + + # Stops are encoded using two STOP stitches each. See the comment in + # stop.py for an explanation. + + return sum(1 for stitch in self if stitch.stop) / 2 + + @property + def num_trims(self): + """Number of trims in this color block.""" + + return sum(1 for stitch in self if stitch.trim) + + def filter_duplicate_stitches(self): + if not self.stitches: + return + + 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 + pass + else: + l = (stitch - stitches[-1]).length() + if l <= 0.1: + # duplicate stitch, skip this one + continue + + stitches.append(stitch) + + self.stitches = stitches + + def add_stitch(self, *args, **kwargs): + 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: + self.stitches.append(Stitch(*args, **kwargs)) + + def add_stitches(self, stitches, *args, **kwargs): + for stitch in stitches: + if isinstance(stitch, (Stitch, Point)): + self.add_stitch(stitch, *args, **kwargs) + else: + self.add_stitch(*(list(stitch) + args), **kwargs) + + def replace_stitches(self, stitches): + self.stitches = stitches + + @property + def bounding_box(self): + minx = min(stitch.x for stitch in self) + miny = min(stitch.y for stitch in self) + maxx = max(stitch.x for stitch in self) + maxy = max(stitch.y for stitch in self) + + return minx, miny, maxx, maxy diff --git a/lib/stitch_plan/stop.py b/lib/stitch_plan/stop.py new file mode 100644 index 00000000..c5e9f7e4 --- /dev/null +++ b/lib/stitch_plan/stop.py @@ -0,0 +1,27 @@ +def process_stop(color_block): + """Handle the "stop after" checkbox. + + The user wants the machine to pause after this patch. This can + be useful for applique and similar on multi-needle machines that + normally would not stop between colors. + + On such machines, the user assigns needles to the colors in the + design before starting stitching. C01, C02, etc are normal + needles, but C00 is special. For a block of stitches assigned + to C00, the machine will continue sewing with the last color it + had and pause after it completes the C00 block. + + That means we need to introduce an artificial color change + shortly before the current stitch so that the user can set that + to C00. We'll go back 3 stitches and do that: + """ + + if len(color_block.stitches) >= 3: + color_block.stitches[-3].stop = True + + # and also add a color change on this stitch, completing the C00 + # block: + + color_block.stitches[-1].stop = True + + # reference for the above: https://github.com/lexelby/inkstitch/pull/29#issuecomment-359175447 diff --git a/lib/stitch_plan/ties.py b/lib/stitch_plan/ties.py new file mode 100644 index 00000000..f9c5b721 --- /dev/null +++ b/lib/stitch_plan/ties.py @@ -0,0 +1,53 @@ +from copy import deepcopy + +from .stitch import Stitch +from ..utils import cut_path +from ..stitches import running_stitch + + +def add_tie(stitches, tie_path): + if stitches[-1].no_ties: + # It's from a manual stitch block, so don't add tie stitches. The user + # will add them if they want them. + return + + tie_path = cut_path(tie_path, 0.6) + tie_stitches = running_stitch(tie_path, 0.3) + tie_stitches = [Stitch(stitch.x, stitch.y) for stitch in tie_stitches] + + stitches.extend(deepcopy(tie_stitches[1:])) + stitches.extend(deepcopy(list(reversed(tie_stitches))[1:])) + + +def add_tie_off(stitches): + add_tie(stitches, list(reversed(stitches))) + + +def add_tie_in(stitches, upcoming_stitches): + add_tie(stitches, upcoming_stitches) + + +def add_ties(stitch_plan): + """Add tie-off before and after trims, jumps, and color changes.""" + + for color_block in stitch_plan: + need_tie_in = True + new_stitches = [] + for i, stitch in enumerate(color_block.stitches): + is_special = stitch.trim or stitch.jump or stitch.stop + + if is_special and not need_tie_in: + add_tie_off(new_stitches) + new_stitches.append(stitch) + need_tie_in = True + elif need_tie_in and not is_special: + new_stitches.append(stitch) + add_tie_in(new_stitches, upcoming_stitches=color_block.stitches[i:]) + need_tie_in = False + else: + new_stitches.append(stitch) + + if not need_tie_in: + add_tie_off(new_stitches) + + color_block.replace_stitches(new_stitches) diff --git a/lib/stitch_plan/trim.py b/lib/stitch_plan/trim.py new file mode 100644 index 00000000..f692a179 --- /dev/null +++ b/lib/stitch_plan/trim.py @@ -0,0 +1,23 @@ +def process_trim(color_block, next_stitch): + """Handle the "trim after" checkbox. + + DST (and maybe other formats?) has no actual TRIM instruction. + Instead, 3 sequential JUMPs cause the machine to trim the thread. + + To support both DST and other formats, we'll add a TRIM and two + JUMPs. The TRIM will be converted to a JUMP by libembroidery + if saving to DST, resulting in the 3-jump sequence. + """ + + delta = next_stitch - color_block.last_stitch + delta = delta * (1/4.0) + + pos = color_block.last_stitch + + for i in xrange(3): + pos += delta + color_block.add_stitch(pos.x, pos.y, jump=True) + + # first one should be TRIM instead of JUMP + color_block.stitches[-3].jump = False + color_block.stitches[-3].trim = True |
