summaryrefslogtreecommitdiff
path: root/lib/stitches/auto_fill.py
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2019-03-20 22:41:57 -0400
committerLex Neva <github.com@lexneva.name>2019-03-20 22:41:57 -0400
commit97ced89e8793ff608588324304e53427d180ee1e (patch)
tree6ab59c20ceec42b3e5b22ecb7ce7f83a31c10fbf /lib/stitches/auto_fill.py
parent1e5733bbcdb000b27a0569b2b838cc0c04ffb461 (diff)
switch to multigraph to avoid accidentally deleting outline edges
Diffstat (limited to 'lib/stitches/auto_fill.py')
-rw-r--r--lib/stitches/auto_fill.py17
1 files changed, 7 insertions, 10 deletions
diff --git a/lib/stitches/auto_fill.py b/lib/stitches/auto_fill.py
index 8ad9fa16..a7ac49e7 100644
--- a/lib/stitches/auto_fill.py
+++ b/lib/stitches/auto_fill.py
@@ -149,7 +149,7 @@ def build_fill_stitch_graph(shape, angle, row_spacing, end_row_spacing):
# Tag each node with its index and projection.
graph.add_node(node, index=outline_index, projection=outline_projection)
- add_edges_between_outline_nodes(graph, key="outline")
+ add_edges_between_outline_nodes(graph)
for node1, node2, key, data in graph.edges(keys=True, data=True):
if key == "outline":
@@ -168,7 +168,7 @@ def tag_nodes_with_outline_and_projection(graph, shape, nodes):
graph.add_node(node, outline=outline_index, projection=outline_projection)
-def add_edges_between_outline_nodes(graph, key=None):
+def add_edges_between_outline_nodes(graph):
"""Add edges around the outlines of the graph, connecting sequential nodes.
This function assumes that all nodes in the graph are on the outline of the
@@ -188,10 +188,7 @@ def add_edges_between_outline_nodes(graph, key=None):
# add an edge between each successive node
for i, (node1, node2) in enumerate(zip(nodes, nodes[1:] + [nodes[0]])):
data = dict(outline=outline_index, index=i)
- if key:
- graph.add_edge(node1, node2, key=key, **data)
- else:
- graph.add_edge(node1, node2, **data)
+ graph.add_edge(node1, node2, key="outline", **data)
def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath):
@@ -215,7 +212,7 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath):
calculation.
"""
- graph = networkx.Graph()
+ graph = networkx.MultiGraph()
# Add all the nodes from the main graph. This will be all of the endpoints
# of the rows of stitches. Every node will be on the outline of the shape.
@@ -237,13 +234,13 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath):
tag_nodes_with_outline_and_projection(graph, shape, endpoints)
add_edges_between_outline_nodes(graph)
- for start, end in graph.edges:
+ for start, end, key in graph.edges:
p1 = InkstitchPoint(*start)
p2 = InkstitchPoint(*end)
# Set the weight equal to triple the edge length, to encourage travel()
# to avoid them when underpathing is enabled.
- graph.add_edge(start, end, weight=3 * p1.distance(p2))
+ graph[start][end][key]["weight"] = 3 * p1.distance(p2)
if underpath:
segments = []
@@ -260,7 +257,7 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath):
interior_edges = grating1.symmetric_difference(grating2)
for ls in interior_edges.geoms:
p1, p2 = [InkstitchPoint(*coord) for coord in ls.coords]
- edge = (p1.as_tuple(), p2.as_tuple())
+ edge = (p1.as_tuple(), p2.as_tuple(), 'travel')
for segment in rtree.query(ls):
start, end = segment.coords