2021-06-30 12:05:13 +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 shapely import geometry as shgeo
|
|
|
|
|
2022-05-18 14:02:07 +00:00
|
|
|
from .marker import get_marker_elements
|
2021-08-07 22:38:57 +00:00
|
|
|
from .stitch_plan import Stitch
|
2021-06-30 12:05:13 +00:00
|
|
|
from .utils import Point
|
|
|
|
|
|
|
|
|
2022-07-23 16:10:37 +00:00
|
|
|
def get_patterns_cache_key_data(node):
|
|
|
|
patterns = get_marker_elements(node, "pattern")
|
|
|
|
data = []
|
|
|
|
data.extend([fill.wkt for fill in patterns['fill']])
|
|
|
|
data.extend([stroke.wkt for stroke in patterns['stroke']])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-07-23 16:01:46 +00:00
|
|
|
def apply_patterns(stitch_groups, node):
|
2022-05-18 14:02:07 +00:00
|
|
|
patterns = get_marker_elements(node, "pattern")
|
2022-07-23 16:01:46 +00:00
|
|
|
_apply_fill_patterns(patterns['fill'], stitch_groups)
|
|
|
|
_apply_stroke_patterns(patterns['stroke'], stitch_groups)
|
2021-06-30 12:05:13 +00:00
|
|
|
|
2021-07-07 18:07:04 +00:00
|
|
|
|
2022-07-23 16:01:46 +00:00
|
|
|
def _apply_stroke_patterns(patterns, stitch_groups):
|
2021-07-07 18:07:04 +00:00
|
|
|
for pattern in patterns:
|
2022-07-23 16:01:46 +00:00
|
|
|
for stitch_group in stitch_groups:
|
|
|
|
stitch_group_points = []
|
|
|
|
for i, stitch in enumerate(stitch_group.stitches):
|
|
|
|
stitch_group_points.append(stitch)
|
|
|
|
if i == len(stitch_group.stitches) - 1:
|
2021-07-07 18:07:04 +00:00
|
|
|
continue
|
2022-07-23 16:01:46 +00:00
|
|
|
intersection_points = _get_pattern_points(stitch, stitch_group.stitches[i + 1], pattern)
|
2021-07-07 18:07:04 +00:00
|
|
|
for point in intersection_points:
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(Stitch(point, tags=('pattern_point',)))
|
|
|
|
stitch_group.stitches = stitch_group_points
|
2021-07-07 18:07:04 +00:00
|
|
|
|
|
|
|
|
2022-07-23 16:01:46 +00:00
|
|
|
def _apply_fill_patterns(patterns, stitch_groups):
|
2021-07-07 18:07:04 +00:00
|
|
|
for pattern in patterns:
|
2022-07-23 16:01:46 +00:00
|
|
|
for stitch_group in stitch_groups:
|
|
|
|
stitch_group_points = []
|
|
|
|
for i, stitch in enumerate(stitch_group.stitches):
|
2021-07-07 18:07:04 +00:00
|
|
|
if not shgeo.Point(stitch).within(pattern):
|
2021-08-08 19:51:29 +00:00
|
|
|
# keep points outside the fill pattern
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
|
|
|
elif i - 1 < 0 or i >= len(stitch_group.stitches) - 1:
|
2021-08-07 22:38:57 +00:00
|
|
|
# keep start and end points
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
2021-08-07 22:38:57 +00:00
|
|
|
elif stitch.has_tag('fill_row_start') or stitch.has_tag('fill_row_end'):
|
|
|
|
# keep points if they are the start or end of a fill stitch row
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
2021-08-15 21:24:59 +00:00
|
|
|
elif stitch.has_tag('auto_fill') and not stitch.has_tag('auto_fill_top'):
|
|
|
|
# keep auto-fill underlay
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
2021-08-15 21:24:59 +00:00
|
|
|
elif stitch.has_tag('auto_fill_travel'):
|
|
|
|
# keep travel stitches (underpath or travel around the border)
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
2021-08-16 14:30:22 +00:00
|
|
|
elif stitch.has_tag('satin_column') and not stitch.has_tag('satin_split_stitch'):
|
|
|
|
# keep satin column stitches unless they are split stitches
|
2022-07-23 16:01:46 +00:00
|
|
|
stitch_group_points.append(stitch)
|
|
|
|
stitch_group.stitches = stitch_group_points
|
2021-06-30 12:05:13 +00:00
|
|
|
|
|
|
|
|
2021-07-07 18:07:04 +00:00
|
|
|
def _get_pattern_points(first, second, pattern):
|
2021-06-30 12:05:13 +00:00
|
|
|
points = []
|
2021-07-07 18:07:04 +00:00
|
|
|
intersection = shgeo.LineString([first, second]).intersection(pattern)
|
|
|
|
if isinstance(intersection, shgeo.Point):
|
|
|
|
points.append(Point(intersection.x, intersection.y))
|
|
|
|
if isinstance(intersection, shgeo.MultiPoint):
|
2022-05-18 14:02:07 +00:00
|
|
|
for point in intersection.geoms:
|
2021-07-07 18:07:04 +00:00
|
|
|
points.append(Point(point.x, point.y))
|
|
|
|
# sort points after their distance to first
|
2021-06-30 12:05:13 +00:00
|
|
|
points.sort(key=lambda point: point.distance(first))
|
|
|
|
return points
|