2023-02-18 22:44:42 +00:00
|
|
|
from itertools import combinations
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
import networkx as nx
|
2023-04-02 04:14:57 +00:00
|
|
|
from shapely.geometry import LineString, MultiPoint, Point
|
2023-01-16 19:27:06 +00:00
|
|
|
from shapely.ops import nearest_points
|
|
|
|
|
|
|
|
from .. import tiles
|
2024-05-11 06:14:40 +00:00
|
|
|
from ..debug.debug import debug
|
2023-03-09 17:57:55 +00:00
|
|
|
from ..i18n import _
|
2023-02-18 22:44:42 +00:00
|
|
|
from ..utils.clamp_path import clamp_path_to_polygon
|
2023-03-09 17:57:55 +00:00
|
|
|
from ..utils.geometry import Point as InkStitchPoint
|
|
|
|
from ..utils.geometry import ensure_geometry_collection
|
2023-01-16 19:27:06 +00:00
|
|
|
from ..utils.list import poprandom
|
2023-01-18 02:44:23 +00:00
|
|
|
from ..utils.prng import iter_uniform_floats
|
2023-02-18 22:44:42 +00:00
|
|
|
from ..utils.smoothing import smooth_path
|
2023-02-06 03:58:17 +00:00
|
|
|
from ..utils.threading import check_stop_flag
|
2024-05-05 17:55:33 +00:00
|
|
|
from .running_stitch import bean_stitch, even_running_stitch, zigzag_stitch
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
|
2023-04-02 18:16:16 +00:00
|
|
|
def meander_fill(fill, shape, original_shape, shape_index, starting_point, ending_point):
|
2023-01-18 02:44:23 +00:00
|
|
|
debug.log(f"meander pattern: {fill.meander_pattern}")
|
2023-01-16 19:27:06 +00:00
|
|
|
tile = get_tile(fill.meander_pattern)
|
|
|
|
if not tile:
|
|
|
|
return []
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
debug.log(f"tile name: {tile.name}")
|
2023-01-16 19:27:06 +00:00
|
|
|
|
2023-01-29 03:09:34 +00:00
|
|
|
debug.log_line_strings(lambda: ensure_geometry_collection(shape.boundary).geoms, 'Meander shape')
|
2023-04-04 02:59:02 +00:00
|
|
|
graph = tile.to_graph(shape, fill.meander_scale, fill.meander_angle)
|
2023-03-09 17:57:55 +00:00
|
|
|
|
|
|
|
if not graph:
|
2023-09-07 17:25:47 +00:00
|
|
|
fill.fatal(_('Could not build graph for meander stitching. Try to enlarge your shape or '
|
|
|
|
'scale your meander pattern down.'))
|
2023-03-09 17:57:55 +00:00
|
|
|
|
2023-01-29 03:09:34 +00:00
|
|
|
debug.log_graph(graph, 'Meander graph')
|
2023-02-18 22:44:42 +00:00
|
|
|
ensure_connected(graph)
|
2023-01-18 02:44:23 +00:00
|
|
|
start, end = find_starting_and_ending_nodes(graph, shape, starting_point, ending_point)
|
|
|
|
rng = iter_uniform_floats(fill.random_seed, 'meander-fill', shape_index)
|
|
|
|
|
2023-04-02 18:16:16 +00:00
|
|
|
return post_process(generate_meander_path(graph, start, end, rng), shape, original_shape, fill)
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
|
2023-02-19 03:24:58 +00:00
|
|
|
def get_tile(tile_id):
|
|
|
|
all_tiles = {tile.id: tile for tile in tiles.all_tiles()}
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
try:
|
2023-02-19 03:24:58 +00:00
|
|
|
return all_tiles.get(tile_id, all_tiles.popitem()[1])
|
2023-01-16 19:27:06 +00:00
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-02-18 22:44:42 +00:00
|
|
|
def ensure_connected(graph):
|
|
|
|
"""If graph is unconnected, add edges to make it connected."""
|
|
|
|
|
|
|
|
# TODO: combine this with possible_jumps() in lib/stitches/utils/autoroute.py
|
|
|
|
possible_connections = []
|
|
|
|
for component1, component2 in combinations(nx.connected_components(graph), 2):
|
|
|
|
points1 = MultiPoint([Point(node) for node in component1])
|
|
|
|
points2 = MultiPoint([Point(node) for node in component2])
|
|
|
|
|
|
|
|
start_point, end_point = nearest_points(points1, points2)
|
|
|
|
possible_connections.append(((start_point.x, start_point.y), (end_point.x, end_point.y), start_point.distance(end_point)))
|
|
|
|
|
|
|
|
if possible_connections:
|
|
|
|
for start, end in nx.k_edge_augmentation(graph, 1, avail=possible_connections):
|
|
|
|
check_stop_flag()
|
|
|
|
graph.add_edge(start, end)
|
|
|
|
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
def find_starting_and_ending_nodes(graph, shape, starting_point, ending_point):
|
|
|
|
if starting_point is None:
|
|
|
|
starting_point = shape.exterior.coords[0]
|
|
|
|
starting_point = Point(starting_point)
|
|
|
|
|
|
|
|
if ending_point is None:
|
2023-03-07 17:13:57 +00:00
|
|
|
# pick a spot on the opposite side of the shape
|
|
|
|
projection = (shape.exterior.project(starting_point, normalized=True) + 0.5) % 1.0
|
|
|
|
ending_point = shape.exterior.interpolate(projection, normalized=True)
|
2023-01-18 02:44:23 +00:00
|
|
|
else:
|
|
|
|
ending_point = Point(ending_point)
|
|
|
|
|
2023-01-16 19:27:06 +00:00
|
|
|
all_points = MultiPoint(list(graph))
|
|
|
|
|
|
|
|
starting_node = nearest_points(starting_point, all_points)[1].coords[0]
|
|
|
|
ending_node = nearest_points(ending_point, all_points)[1].coords[0]
|
|
|
|
|
|
|
|
if starting_node == ending_node:
|
|
|
|
# We need a path to start with, so pick a new ending node
|
|
|
|
all_points = all_points.difference(Point(starting_node))
|
|
|
|
ending_node = nearest_points(ending_point, all_points)[1].coords[0]
|
|
|
|
|
|
|
|
return starting_node, ending_node
|
|
|
|
|
|
|
|
|
|
|
|
def find_initial_path(graph, start, end):
|
|
|
|
# We need some path to start with. We could use
|
|
|
|
# nx.all_simple_paths(graph, start, end) and choose the first one.
|
|
|
|
# However, that tends to pick a really "orderly" path. Shortest
|
|
|
|
# path looks more random.
|
2023-01-29 02:35:24 +00:00
|
|
|
|
|
|
|
# TODO: handle if this can't find a path
|
2023-01-16 19:27:06 +00:00
|
|
|
return nx.shortest_path(graph, start, end)
|
|
|
|
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
@debug.time
|
|
|
|
def generate_meander_path(graph, start, end, rng):
|
2023-01-16 19:27:06 +00:00
|
|
|
path = find_initial_path(graph, start, end)
|
|
|
|
path_edges = list(zip(path[:-1], path[1:]))
|
|
|
|
graph.remove_edges_from(path_edges)
|
|
|
|
graph_nodes = set(graph) - set(path)
|
|
|
|
|
|
|
|
edges_to_consider = list(path_edges)
|
|
|
|
meander_path = path_edges
|
|
|
|
while edges_to_consider:
|
|
|
|
while edges_to_consider:
|
2023-02-06 03:58:17 +00:00
|
|
|
check_stop_flag()
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
edge = poprandom(edges_to_consider, rng)
|
2023-01-16 19:27:06 +00:00
|
|
|
edges_to_consider.extend(replace_edge(meander_path, edge, graph, graph_nodes))
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
edge_pairs = list(zip(meander_path[:-1], meander_path[1:]))
|
2023-01-16 19:27:06 +00:00
|
|
|
while edge_pairs:
|
2023-02-06 03:58:17 +00:00
|
|
|
check_stop_flag()
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
edge1, edge2 = poprandom(edge_pairs, rng)
|
2023-04-02 04:14:57 +00:00
|
|
|
new_edges = replace_edge_pair(meander_path, edge1, edge2, graph, graph_nodes)
|
|
|
|
if new_edges:
|
|
|
|
edges_to_consider.extend(new_edges)
|
|
|
|
break
|
|
|
|
|
|
|
|
debug.log_graph(graph, "remaining graph", "#FF0000")
|
|
|
|
points = path_to_points(meander_path)
|
|
|
|
debug.log_line_string(LineString(points), "meander path", "#00FF00")
|
2023-01-16 19:27:06 +00:00
|
|
|
|
2023-04-02 04:14:57 +00:00
|
|
|
return points
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def replace_edge(path, edge, graph, graph_nodes):
|
|
|
|
subgraph = graph.subgraph(graph_nodes | set(edge))
|
|
|
|
new_path = None
|
|
|
|
for new_path in nx.all_simple_edge_paths(subgraph, edge[0], edge[1], 7):
|
|
|
|
if len(new_path) > 1:
|
|
|
|
break
|
|
|
|
if new_path is None or len(new_path) == 1:
|
|
|
|
return []
|
|
|
|
i = path.index(edge)
|
|
|
|
path[i:i + 1] = new_path
|
|
|
|
graph.remove_edges_from(new_path)
|
2023-01-18 02:44:23 +00:00
|
|
|
# do I need to remove the last one too?
|
2023-01-16 19:27:06 +00:00
|
|
|
graph_nodes.difference_update(start for start, end in new_path)
|
2023-01-18 02:44:23 +00:00
|
|
|
# debug.log(f"found new path of length {len(new_path)} at position {i}")
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
return new_path
|
|
|
|
|
|
|
|
|
|
|
|
def replace_edge_pair(path, edge1, edge2, graph, graph_nodes):
|
|
|
|
subgraph = graph.subgraph(graph_nodes | {edge1[0], edge2[1]})
|
|
|
|
new_path = None
|
|
|
|
for new_path in nx.all_simple_edge_paths(subgraph, edge1[0], edge2[1], 10):
|
|
|
|
if len(new_path) > 2:
|
|
|
|
break
|
|
|
|
if new_path is None or len(new_path) <= 2:
|
|
|
|
return []
|
|
|
|
i = path.index(edge1)
|
|
|
|
path[i:i + 2] = new_path
|
|
|
|
graph.remove_edges_from(new_path)
|
2023-01-18 02:44:23 +00:00
|
|
|
# do I need to remove the last one too?
|
2023-01-16 19:27:06 +00:00
|
|
|
graph_nodes.difference_update(start for start, end in new_path)
|
2023-01-18 02:44:23 +00:00
|
|
|
# debug.log(f"found new pair path of length {len(new_path)} at position {i}")
|
2023-01-16 19:27:06 +00:00
|
|
|
|
|
|
|
return new_path
|
2023-01-18 02:44:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@debug.time
|
2023-04-02 18:16:16 +00:00
|
|
|
def post_process(points, shape, original_shape, fill):
|
2023-01-18 02:44:23 +00:00
|
|
|
debug.log(f"smoothness: {fill.smoothness}")
|
|
|
|
# debug.log_line_string(LineString(points), "pre-smoothed", "#FF0000")
|
|
|
|
smoothed_points = smooth_path(points, fill.smoothness)
|
|
|
|
smoothed_points = [InkStitchPoint.from_tuple(point) for point in smoothed_points]
|
|
|
|
|
2024-01-28 07:48:44 +00:00
|
|
|
if fill.zigzag_spacing > 0:
|
2024-05-05 17:55:33 +00:00
|
|
|
stitches = even_running_stitch(smoothed_points, fill.zigzag_spacing / 2, fill.running_stitch_tolerance)
|
2024-01-28 07:48:44 +00:00
|
|
|
stitches = zigzag_stitch(stitches, fill.zigzag_spacing, fill.zigzag_width, 0)
|
|
|
|
else:
|
2024-05-05 17:55:33 +00:00
|
|
|
stitches = even_running_stitch(smoothed_points, fill.running_stitch_length, fill.running_stitch_tolerance)
|
2023-04-02 18:16:16 +00:00
|
|
|
|
|
|
|
if fill.clip:
|
|
|
|
stitches = clamp_path_to_polygon(stitches, original_shape)
|
2023-01-18 02:44:23 +00:00
|
|
|
|
2023-04-24 20:52:31 +00:00
|
|
|
if fill.bean_stitch_repeats:
|
|
|
|
stitches = bean_stitch(stitches, fill.bean_stitch_repeats)
|
|
|
|
|
|
|
|
if fill.repeats:
|
|
|
|
for i in range(1, fill.repeats):
|
|
|
|
if i % 2 == 1:
|
|
|
|
# reverse every other pass
|
|
|
|
stitches.extend(stitches[::-1])
|
|
|
|
else:
|
|
|
|
stitches.extend(stitches)
|
|
|
|
|
2023-01-18 02:44:23 +00:00
|
|
|
return stitches
|
|
|
|
|
|
|
|
|
|
|
|
def path_to_points(path):
|
|
|
|
points = [start for start, end in path]
|
|
|
|
if path:
|
|
|
|
points.append(path[-1][1])
|
|
|
|
|
|
|
|
return points
|