Skip to content

Commit 4642da5

Browse files
committed
underpathing
1 parent 6dbd1dd commit 4642da5

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

lib/elements/fill_stitch.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from ..utils.param import ParamOption
3131
from .element import EmbroideryElement, param
3232
from .validation import ValidationError, ValidationWarning
33-
import sys
3433

3534

3635
class SmallShapeWarning(ValidationWarning):
@@ -1160,6 +1159,7 @@ def do_meander_fill(self, shape, original_shape, i, starting_point, ending_point
11601159
def do_cross_stitch(self, previous_stitch_group, start, end):
11611160
fill_shapes = ensure_multi_polygon(make_valid(self.fill_shape(self.shape)))
11621161
fill_shapes = list(fill_shapes.geoms)
1162+
11631163
if start:
11641164
fill_shapes.sort(key=lambda shape: shape.distance(shgeo.Point(start)))
11651165
else:
@@ -1168,21 +1168,22 @@ def do_cross_stitch(self, previous_stitch_group, start, end):
11681168

11691169
stitch_groups = []
11701170
for i, shape in enumerate(fill_shapes):
1171-
print(shape, file=sys.stderr)
11721171
start = self.get_starting_point(previous_stitch_group)
11731172
if i < len(fill_shapes) - 1:
11741173
end = nearest_points(shape, fill_shapes[i+1])[0].coords
11751174
else:
11761175
end = final_end
1177-
stitch_group = StitchGroup(
1178-
color=self.color,
1179-
tags=("cross_stitch"),
1180-
stitches=cross_stitch(self, shape, start, end),
1181-
force_lock_stitches=self.force_lock_stitches,
1182-
lock_stitches=self.lock_stitches
1183-
)
1184-
previous_stitch_group = stitch_group
1185-
stitch_groups.append(stitch_group)
1176+
stitch_lists = cross_stitch(self, shape, start, end)
1177+
for stitch_list in stitch_lists:
1178+
stitch_group = StitchGroup(
1179+
color=self.color,
1180+
tags=("cross_stitch"),
1181+
stitches=stitch_list,
1182+
force_lock_stitches=self.force_lock_stitches,
1183+
lock_stitches=self.lock_stitches
1184+
)
1185+
previous_stitch_group = stitch_group
1186+
stitch_groups.append(stitch_group)
11861187
return stitch_groups
11871188

11881189
def do_circular_fill(self, shape, starting_point, ending_point):

lib/stitches/cross_stitch.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ..utils.threading import check_stop_flag
2323
from .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)
2727
from .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

111117
def 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

156173
def _lines_to_stitches(
@@ -199,7 +216,7 @@ def collapse_travel_edges(result):
199216

200217

201218
def 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

Comments
 (0)