inkstitch/lib/stitch_plan/stitch_plan.py

169 wiersze
5.1 KiB
Python
Czysty Zwykły widok Historia

2021-03-12 04:17:19 +00:00
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
from .ties import add_ties
2021-08-07 15:21:13 +00:00
from .color_block import ColorBlock
from ..svg import PIXELS_PER_MM
2021-05-15 07:57:43 +00:00
def patches_to_stitch_plan(patches, collapse_len=None, disable_ties=False): # noqa: C901
2021-08-07 14:57:53 +00:00
"""Convert a collection of inkstitch.element.StitchGroup objects to a StitchPlan.
2021-08-07 14:57:53 +00:00
* applies instructions embedded in the StitchGroup such as trim_after and stop_after
* adds tie-ins and tie-offs
* adds jump-stitches between patches if necessary
"""
if collapse_len is None:
collapse_len = 3.0
collapse_len = collapse_len * PIXELS_PER_MM
stitch_plan = StitchPlan()
color_block = stitch_plan.new_color_block(color=patches[0].color)
for patch in patches:
if not patch.stitches:
continue
2018-07-21 01:41:28 +00:00
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)
2018-07-21 01:41:28 +00:00
# make a new block of our color
color_block = stitch_plan.new_color_block(color=patch.color)
# always start a color with a JUMP to the first stitch position
color_block.add_stitch(patch.stitches[0], jump=True)
else:
if len(color_block) and (patch.stitches[0] - color_block.stitches[-1]).length() > collapse_len:
color_block.add_stitch(patch.stitches[0], jump=True)
color_block.add_stitches(stitches=patch.stitches, tie_modus=patch.tie_modus, no_ties=patch.stitch_as_is)
if patch.trim_after:
color_block.add_stitch(trim=True)
if patch.stop_after:
2018-07-21 01:41:28 +00:00
color_block.add_stitch(stop=True)
color_block = stitch_plan.new_color_block(color_block.color)
2018-07-24 00:15:49 +00:00
if len(color_block) == 0:
# last block ended in a stop, so now we have an empty block
del stitch_plan.color_blocks[-1]
2018-07-21 01:41:28 +00:00
stitch_plan.filter_duplicate_stitches()
if not disable_ties:
stitch_plan.add_ties()
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
2019-07-10 00:39:07 +00:00
def delete_empty_color_blocks(self):
color_blocks = []
for color_block in self.color_blocks:
if len(color_block) > 0:
color_blocks.append(color_block)
2019-07-10 06:08:39 +00:00
self.color_blocks = color_blocks
2019-07-07 14:25:21 +00:00
def add_color_block(self, color_block):
self.color_blocks.append(color_block)
2018-07-21 01:41:28 +00:00
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)
def __len__(self):
return len(self.color_blocks)
2018-04-07 02:09:26 +00:00
def __repr__(self):
return "StitchPlan(%s)" % ", ".join(repr(cb) for cb in self.color_blocks)
2020-04-28 16:34:05 +00:00
def __json__(self):
return dict(color_blocks=self.color_blocks,
num_stops=self.num_stops,
num_trims=self.num_trims,
num_stitches=self.num_stitches,
bounding_box=self.bounding_box
)
@property
def num_colors(self):
"""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(1 for block in self if block.stop_after)
@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)
@property
def last_color_block(self):
if self.color_blocks:
return self.color_blocks[-1]
else:
return None