summaryrefslogtreecommitdiff
path: root/lib/stitches/auto_fill.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/stitches/auto_fill.py')
-rw-r--r--lib/stitches/auto_fill.py33
1 files changed, 27 insertions, 6 deletions
diff --git a/lib/stitches/auto_fill.py b/lib/stitches/auto_fill.py
index ebb1fb6f..e90093ce 100644
--- a/lib/stitches/auto_fill.py
+++ b/lib/stitches/auto_fill.py
@@ -7,6 +7,7 @@
import math
from itertools import chain, groupby
+from typing import Iterator
import networkx
from shapely import geometry as shgeo
@@ -53,11 +54,15 @@ class PathEdge(object):
def __eq__(self, other):
return self._sorted_nodes == other._sorted_nodes and self.key == other.key
+ def __iter__(self) -> Iterator:
+ for i in range(2):
+ yield self[i]
+
def is_outline(self):
- return self.key in self.OUTLINE_KEYS
+ return self.key.startswith(self.OUTLINE_KEYS)
def is_segment(self):
- return self.key == self.SEGMENT_KEY
+ return self.key.startswith(self.SEGMENT_KEY)
@debug.time
@@ -87,7 +92,7 @@ def auto_fill(shape,
return fallback(shape, running_stitch_length, running_stitch_tolerance)
# ensure graph is eulerian
- fill_stitch_graph = graph_make_valid(fill_stitch_graph)
+ graph_make_valid(fill_stitch_graph)
travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath)
@@ -298,8 +303,24 @@ def add_edges_between_outline_nodes(graph, duplicate_every_other=False):
def graph_make_valid(graph):
if not networkx.is_eulerian(graph):
- return networkx.eulerize(graph)
- return graph
+ newgraph = networkx.eulerize(graph)
+ for start, end, key, data in newgraph.edges(keys=True, data=True):
+ if isinstance(key, int):
+ # make valid duplicated edges, we cannot use the very same key
+ # again, but the automatic naming will not apply to the autofill algorithm
+ graph_edges = graph[start][end]
+ if 'segment' in graph_edges.keys():
+ data = graph_edges['segment']
+ graph.add_edge(start, end, key=f'segment-{key}', **data)
+ elif 'outline' in graph_edges.keys():
+ data = graph_edges['outline']
+ graph.add_edge(start, end, key='outline-{key}', **data)
+ elif 'extra' in graph_edges.keys():
+ data = graph_edges['extra']
+ graph.add_edge(start, end, key='extra-{key}', **data)
+ elif 'initial' in graph_edges.keys():
+ data = graph_edges['initial']
+ graph.add_edge(start, end, key='initial-{key}', **data)
def fallback(shape, running_stitch_length, running_stitch_tolerance):
@@ -380,7 +401,7 @@ def weight_edges_by_length(graph, multiplier=1):
def get_segments(graph):
segments = []
for start, end, key, data in graph.edges(keys=True, data=True):
- if key == 'segment':
+ if key.startswith('segment'):
segments.append(data["geometry"])
return segments