2222from ..utils .threading import check_stop_flag
2323from .auto_fill import (add_edges_between_outline_nodes ,
2424 build_fill_stitch_graph , fallback , find_stitch_path ,
25- graph_make_valid , process_travel_edges ,
25+ graph_make_valid , process_travel_edges , collapse_sequential_outline_edges ,
2626 tag_nodes_with_outline_and_projection )
2727from .circular_fill import _apply_bean_stitch_and_repeats
2828
@@ -48,21 +48,22 @@ def cross_stitch(fill, shape, starting_point, ending_point):
4848 boxes = []
4949 scaled_boxes = []
5050 center_points = []
51+ travel_edges = []
5152 y = adapted_miny
5253 while y <= adapted_maxy :
5354 x = adapted_minx
5455 while x <= adapted_maxx :
5556 box = translate (square , x , y )
5657 if shape .contains (box ):
57- boxes , scaled_boxes , center_points , crosses_lr , crosses_rl = add_cross (
58- box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes
58+ travel_edges , boxes , scaled_boxes , center_points , crosses_lr , crosses_rl , = add_cross (
59+ box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes , travel_edges
5960 )
6061 elif shape .intersects (box ):
6162 intersection = box .intersection (shape )
6263 intersection_area = intersection .area
6364 if intersection_area / full_square_area * 100 > fill .cross_coverage :
64- boxes , scaled_boxes , center_points , crosses_lr , crosses_rl = add_cross (
65- box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes
65+ travel_edges , boxes , scaled_boxes , center_points , crosses_lr , crosses_rl , = add_cross (
66+ box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes , travel_edges
6667 )
6768 x += square_size
6869 y += square_size
@@ -88,24 +89,29 @@ def cross_stitch(fill, shape, starting_point, ending_point):
8889
8990 nodes = get_line_endpoints (rl )
9091 nodes .extend (get_line_endpoints (lr ))
92+ nodes .extend (get_line_endpoints (v ))
9193
9294 check_stop_flag ()
9395
94- starting_point , ending_point = get_start_and_end (fill .max_stitch_length , starting_point , ending_point , lr , rl )
96+ starting_point , ending_point = get_start_and_end (
97+ fill .max_stitch_length , starting_point , ending_point , MultiLineString (crosses_lr ), MultiLineString (crosses_rl )
98+ )
99+
100+ travel_edges = list (ensure_multi_line_string (line_merge (MultiLineString (travel_edges ))).geoms )
95101
96102 stitches = _lines_to_stitches (
97- lr , crosses_rl , outline , stitch_length , fill .bean_stitch_repeats ,
103+ lr , travel_edges , outline , stitch_length , fill .bean_stitch_repeats ,
98104 starting_point , ending_point , nodes , center_points , clamp
99105 )
100106 starting_point = InkstitchPoint (* stitches [- 1 ])
101107 stitches .extend (
102108 _lines_to_stitches (
103- rl , crosses_rl , outline , stitch_length , fill .bean_stitch_repeats ,
109+ rl , travel_edges , outline , stitch_length , fill .bean_stitch_repeats ,
104110 starting_point , ending_point , nodes , center_points , clamp
105111 )
106112 )
107113
108- return stitches
114+ return [ stitches ]
109115
110116
111117def cross_stitch_multiple (outline , fill , starting_point , ending_point ):
@@ -121,7 +127,7 @@ def cross_stitch_multiple(outline, fill, starting_point, ending_point):
121127 else :
122128 end = ending_point
123129 stitches .extend (cross_stitch (fill , polygon , starting_point , end ))
124- starting_point = InkstitchPoint (* stitches [- 1 ])
130+ starting_point = InkstitchPoint (* stitches [- 1 ][ - 1 ] )
125131 return stitches
126132
127133
@@ -141,16 +147,27 @@ def get_line_endpoints(multilinestring):
141147 return nodes
142148
143149
144- def add_cross (box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes ):
150+ def add_cross (box , scaled_boxes , center_points , crosses_lr , crosses_rl , boxes , travel_edges ):
145151 minx , miny , maxx , maxy = box .bounds
146- center_points .append (box .centroid )
152+ center = box .centroid
153+ center_points .append (center )
147154 crosses_lr .append (LineString ([(minx , miny ), (maxx , maxy )]))
148155 crosses_rl .append (LineString ([(maxx , miny ), (minx , maxy )]))
156+
157+ travel_edges .append (LineString ([(minx , miny ), center ]))
158+ travel_edges .append (LineString ([(maxx , miny ), center ]))
159+ travel_edges .append (LineString ([(maxx , maxy ), center ]))
160+ travel_edges .append (LineString ([(minx , maxy ), center ]))
161+ travel_edges .append (LineString ([(minx , miny ), (maxx , miny )]))
162+ travel_edges .append (LineString ([(minx , miny ), (minx , maxy )]))
163+ travel_edges .append (LineString ([(maxx , maxy ), (maxx , miny )]))
164+ travel_edges .append (LineString ([(maxx , maxy ), (minx , maxy )]))
165+
149166 boxes .append (box )
150167 # scaling the outline allows us to connect otherwise unconnected boxes
151168 box = scale (box , xfact = 1.000000000000001 , yfact = 1.000000000000001 )
152169 scaled_boxes .append (box )
153- return boxes , scaled_boxes , center_points , crosses_lr , crosses_rl
170+ return travel_edges , boxes , scaled_boxes , center_points , crosses_lr , crosses_rl
154171
155172
156173def _lines_to_stitches (
@@ -199,7 +216,7 @@ def collapse_travel_edges(result):
199216
200217
201218def path_to_stitches (shape , path , travel_graph , fill_stitch_graph , stitch_length , center_points , clamp ):
202- # path = collapse_sequential_outline_edges(path, fill_stitch_graph)
219+ path = collapse_sequential_outline_edges (path , fill_stitch_graph )
203220
204221 stitches = []
205222 if not path [0 ].is_segment ():
@@ -255,7 +272,7 @@ def travel(shape, travel_graph, edge, center_points, stitch_length, clamp=True):
255272 pass
256273 else :
257274 line = LineString ([last_point , point ])
258- if line .length < stitch_length / 2 + 2 :
275+ if line .length < stitch_length / 2 :
259276 stitches .append (Stitch (point , tags = ["auto_fill_travel" ]))
260277 last_point = point
261278 continue
@@ -268,7 +285,7 @@ def travel(shape, travel_graph, edge, center_points, stitch_length, clamp=True):
268285 # snap to avoid problems in edge collapsing later on
269286 center_point = snap (center_point , center_points , tolerance = 0.01 )
270287 stitches .append (Stitch (center_point , tags = ["auto_fill_travel" ]))
271- stitches .append (Stitch (* point , tags = ["auto_fill_travel" ]))
288+ stitches .append (Stitch (* point , tags = ["auto_fill_travel" ]))
272289 last_point = point
273290
274291 return stitches
0 commit comments