diff options
Diffstat (limited to 'lib/stitches')
| -rw-r--r-- | lib/stitches/auto_fill.py | 30 | ||||
| -rw-r--r-- | lib/stitches/circular_fill.py | 6 | ||||
| -rw-r--r-- | lib/stitches/guided_fill.py | 10 | ||||
| -rw-r--r-- | lib/stitches/running_stitch.py | 2 |
4 files changed, 29 insertions, 19 deletions
diff --git a/lib/stitches/auto_fill.py b/lib/stitches/auto_fill.py index 03930ddd..50eea18e 100644 --- a/lib/stitches/auto_fill.py +++ b/lib/stitches/auto_fill.py @@ -17,9 +17,11 @@ from shapely.strtree import STRtree from ..debug import debug from ..stitch_plan import Stitch from ..svg import PIXELS_PER_MM +from ..utils.clamp_path import clamp_path_to_polygon from ..utils.geometry import Point as InkstitchPoint, line_string_to_point_list, ensure_multi_line_string from .fill import intersect_region_with_grating, stitch_row from .running_stitch import running_stitch +from ..utils.smoothing import smooth_path from ..utils.threading import check_stop_flag @@ -77,9 +79,9 @@ def auto_fill(shape, travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath) path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point) - result = path_to_stitches(path, travel_graph, fill_stitch_graph, angle, row_spacing, + result = path_to_stitches(shape, path, travel_graph, fill_stitch_graph, angle, row_spacing, max_stitch_length, running_stitch_length, running_stitch_tolerance, - staggers, skip_last) + staggers, skip_last, underpath) return result @@ -350,9 +352,9 @@ def get_segments(graph): def process_travel_edges(graph, fill_stitch_graph, shape, travel_edges): """Weight the interior edges and pre-calculate intersection with fill stitch rows.""" - # Set the weight equal to 5x the edge length, to encourage travel() + # Set the weight equal to 3x the edge length, to encourage travel() # to avoid them. - weight_edges_by_length(graph, 5) + weight_edges_by_length(graph, 3) segments = get_segments(fill_stitch_graph) @@ -618,20 +620,26 @@ def collapse_sequential_outline_edges(path, graph): if not start_of_run: start_of_run = edge[0] - if start_of_run: + if start_of_run and start_of_run != edge[1]: # if we were still in a run, close it off new_path.append(PathEdge((start_of_run, edge[1]), "collapsed")) return new_path -def travel(travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last): +def travel(shape, travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last, underpath): """Create stitches to get from one point on an outline of the shape to another.""" start, end = edge path = networkx.shortest_path(travel_graph, start, end, weight='weight') - path = [Stitch(*p) for p in path] - stitches = running_stitch(path, running_stitch_length, running_stitch_tolerance) + if underpath and path != (start, end): + path = smooth_path(path, 2) + else: + path = [InkstitchPoint.from_tuple(point) for point in path] + path = clamp_path_to_polygon(path, shape) + + points = running_stitch(path, running_stitch_length, running_stitch_tolerance) + stitches = [Stitch(point) for point in points] for stitch in stitches: stitch.add_tag('auto_fill_travel') @@ -653,8 +661,8 @@ def travel(travel_graph, edge, running_stitch_length, running_stitch_tolerance, @debug.time -def path_to_stitches(path, travel_graph, fill_stitch_graph, angle, row_spacing, max_stitch_length, running_stitch_length, running_stitch_tolerance, - staggers, skip_last): +def path_to_stitches(shape, path, travel_graph, fill_stitch_graph, angle, row_spacing, max_stitch_length, running_stitch_length, + running_stitch_tolerance, staggers, skip_last, underpath): path = collapse_sequential_outline_edges(path, fill_stitch_graph) stitches = [] @@ -668,7 +676,7 @@ def path_to_stitches(path, travel_graph, fill_stitch_graph, angle, row_spacing, stitch_row(stitches, edge[0], edge[1], angle, row_spacing, max_stitch_length, staggers, skip_last) travel_graph.remove_edges_from(fill_stitch_graph[edge[0]][edge[1]]['segment'].get('underpath_edges', [])) else: - stitches.extend(travel(travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last)) + stitches.extend(travel(shape, travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last, underpath)) check_stop_flag() diff --git a/lib/stitches/circular_fill.py b/lib/stitches/circular_fill.py index e63f5607..351f4cf4 100644 --- a/lib/stitches/circular_fill.py +++ b/lib/stitches/circular_fill.py @@ -78,7 +78,7 @@ def circular_fill(shape, travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath) path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point) - result = path_to_stitches(path, travel_graph, fill_stitch_graph, running_stitch_length, running_stitch_tolerance, skip_last) + result = path_to_stitches(shape, path, travel_graph, fill_stitch_graph, running_stitch_length, running_stitch_tolerance, skip_last, underpath) result = _apply_bean_stitch_and_repeats(result, repeats, bean_stitch_repeats) return result @@ -117,7 +117,7 @@ def _get_start_end_sequence(outline, start, end): return substring(outline, start_dist, end_dist) -def path_to_stitches(path, travel_graph, fill_stitch_graph, running_stitch_length, running_stitch_tolerance, skip_last): +def path_to_stitches(shape, path, travel_graph, fill_stitch_graph, running_stitch_length, running_stitch_tolerance, skip_last, underpath): path = collapse_sequential_outline_edges(path, fill_stitch_graph) stitches = [] @@ -144,6 +144,6 @@ def path_to_stitches(path, travel_graph, fill_stitch_graph, running_stitch_lengt travel_graph.remove_edges_from(fill_stitch_graph[edge[0]][edge[1]]['segment'].get('underpath_edges', [])) else: - stitches.extend(travel(travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last)) + stitches.extend(travel(shape, travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last, underpath)) return stitches diff --git a/lib/stitches/guided_fill.py b/lib/stitches/guided_fill.py index 7b80c021..e4793838 100644 --- a/lib/stitches/guided_fill.py +++ b/lib/stitches/guided_fill.py @@ -45,7 +45,9 @@ def guided_fill(shape, travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath) path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point) - result = path_to_stitches(path, travel_graph, fill_stitch_graph, max_stitch_length, running_stitch_length, running_stitch_tolerance, skip_last) + result = path_to_stitches(shape, path, travel_graph, fill_stitch_graph, + max_stitch_length, running_stitch_length, running_stitch_tolerance, skip_last, + underpath) return result @@ -59,7 +61,9 @@ def fallback(shape, guideline, row_spacing, max_stitch_length, running_stitch_le num_staggers, skip_last, starting_point, ending_point, underpath) -def path_to_stitches(path, travel_graph, fill_stitch_graph, stitch_length, running_stitch_length, running_stitch_tolerance, skip_last): +def path_to_stitches(shape, path, travel_graph, fill_stitch_graph, + stitch_length, running_stitch_length, running_stitch_tolerance, skip_last, + underpath): path = collapse_sequential_outline_edges(path, fill_stitch_graph) stitches = [] @@ -89,7 +93,7 @@ def path_to_stitches(path, travel_graph, fill_stitch_graph, stitch_length, runni travel_graph.remove_edges_from(fill_stitch_graph[edge[0]][edge[1]]['segment'].get('underpath_edges', [])) else: - stitches.extend(travel(travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last)) + stitches.extend(travel(shape, travel_graph, edge, running_stitch_length, running_stitch_tolerance, skip_last, underpath)) return stitches diff --git a/lib/stitches/running_stitch.py b/lib/stitches/running_stitch.py index 50e3be5f..29e2547d 100644 --- a/lib/stitches/running_stitch.py +++ b/lib/stitches/running_stitch.py @@ -11,7 +11,6 @@ from copy import copy import numpy as np from shapely import geometry as shgeo -from ..debug import debug from ..utils import prng from ..utils.geometry import Point from ..utils.threading import check_stop_flag @@ -248,7 +247,6 @@ def path_to_curves(points: typing.List[Point], min_len: float): return curves -@debug.time def running_stitch(points, stitch_length, tolerance): # Turn a continuous path into a running stitch. stitches = [points[0]] |
