diff options
| author | Lex Neva <github.com@lexneva.name> | 2023-02-05 22:33:38 -0500 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2023-02-16 22:56:16 -0500 |
| commit | f6c2aa89197c82cedfb31465fc4b2acffaeb8f25 (patch) | |
| tree | 1123050a6ba7428dbcc8b72bd074161bdb3e014f /lib/stitches | |
| parent | 62b7c0080c90d0cf44ee173db7d1f237e8df538e (diff) | |
add more check_stop_flag() calls
Diffstat (limited to 'lib/stitches')
| -rw-r--r-- | lib/stitches/auto_run.py | 9 | ||||
| -rw-r--r-- | lib/stitches/auto_satin.py | 3 | ||||
| -rw-r--r-- | lib/stitches/contour_fill.py | 10 | ||||
| -rw-r--r-- | lib/stitches/fill.py | 2 | ||||
| -rw-r--r-- | lib/stitches/guided_fill.py | 5 | ||||
| -rw-r--r-- | lib/stitches/ripple_stitch.py | 11 | ||||
| -rw-r--r-- | lib/stitches/running_stitch.py | 3 | ||||
| -rw-r--r-- | lib/stitches/utils/autoroute.py | 8 |
8 files changed, 51 insertions, 0 deletions
diff --git a/lib/stitches/auto_run.py b/lib/stitches/auto_run.py index ff26eb16..82c7fc7e 100644 --- a/lib/stitches/auto_run.py +++ b/lib/stitches/auto_run.py @@ -21,6 +21,7 @@ from .utils.autoroute import (add_elements_to_group, add_jumps, get_starting_and_ending_nodes, preserve_original_groups, remove_original_elements) +from ..utils.threading import check_stop_flag class LineSegments: @@ -59,9 +60,13 @@ class LineSegments: self._lines.append(line) self._elements.append(element) + check_stop_flag() + def _get_intersection_points(self): for i, line1 in enumerate(self._lines): for j in range(i + 1, len(self._lines)): + check_stop_flag() + line2 = self._lines[j] distance = line1.distance(line2) if distance > 50: @@ -169,6 +174,8 @@ def build_graph(elements, preserve_order, break_up): # any direction, so we add the edge in the opposite direction too. graph.add_edge(str(end), str(start), element=element) + check_stop_flag() + return graph @@ -199,6 +206,8 @@ def path_to_elements(graph, path, trim): # noqa: C901 just_trimmed = False el = None for start, end, direction in path: + check_stop_flag() + element = graph[start][end].get('element') start_coord = graph.nodes[start]['point'] end_coord = graph.nodes[end]['point'] diff --git a/lib/stitches/auto_satin.py b/lib/stitches/auto_satin.py index 6d792b4e..228f1325 100644 --- a/lib/stitches/auto_satin.py +++ b/lib/stitches/auto_satin.py @@ -24,6 +24,7 @@ from .utils.autoroute import (add_elements_to_group, add_jumps, get_starting_and_ending_nodes, preserve_original_groups, remove_original_elements) +from ..utils.threading import check_stop_flag class SatinSegment(object): @@ -366,6 +367,8 @@ def build_graph(elements, preserve_order=False): # best spots for us. for element in elements: + check_stop_flag() + segments = [] if isinstance(element, Stroke): segments.append(RunningStitch(element)) diff --git a/lib/stitches/contour_fill.py b/lib/stitches/contour_fill.py index 86a55406..885a7e6c 100644 --- a/lib/stitches/contour_fill.py +++ b/lib/stitches/contour_fill.py @@ -15,6 +15,7 @@ from ..utils import DotDict from ..utils.geometry import (cut, ensure_geometry_collection, ensure_multi_polygon, reverse_line_string, roll_linear_ring) +from ..utils.threading import check_stop_flag from .running_stitch import running_stitch @@ -132,6 +133,8 @@ def offset_polygon(polygon, offset, join_style, clockwise): active_holes[0].append(hole_node) while len(active_polygons) > 0: + check_stop_flag() + current_poly = active_polygons.pop() current_holes = active_holes.pop() @@ -325,6 +328,7 @@ def _find_path_inner_to_outer(tree, node, offset, starting_point, avoid_self_cro Return value: LineString -- the stitching path """ + check_stop_flag() current_node = tree.nodes[node] current_ring = current_node.val @@ -473,6 +477,8 @@ def _check_and_prepare_tree_for_valid_spiral(tree): """ def process_node(node): + check_stop_flag() + children = set(tree[node]) if len(children) == 0: @@ -526,6 +532,8 @@ def _get_spiral_rings(tree): node = 'root' while True: + check_stop_flag() + rings.append(tree.nodes[node].val) children = tree[node] @@ -556,6 +564,8 @@ def _make_spiral(rings, stitch_length, starting_point): path = [] for ring1, ring2 in zip(rings[:-1], rings[1:]): + check_stop_flag() + spiral_part = _interpolate_linear_rings(ring1, ring2, stitch_length, starting_point) path.extend(spiral_part.coords) diff --git a/lib/stitches/fill.py b/lib/stitches/fill.py index b3bb0bb7..3bd7761d 100644 --- a/lib/stitches/fill.py +++ b/lib/stitches/fill.py @@ -140,6 +140,8 @@ def intersect_region_with_grating(shape, angle, row_spacing, end_row_spacing=Non current_row_y = start rows = [] while current_row_y < end: + check_stop_flag() + p0 = center + normal * current_row_y + direction * half_length p1 = center + normal * current_row_y - direction * half_length endpoints = [p0.as_tuple(), p1.as_tuple()] diff --git a/lib/stitches/guided_fill.py b/lib/stitches/guided_fill.py index a9906bfb..4c441b32 100644 --- a/lib/stitches/guided_fill.py +++ b/lib/stitches/guided_fill.py @@ -15,6 +15,7 @@ from ..utils.geometry import (ensure_geometry_collection, from .auto_fill import (auto_fill, build_fill_stitch_graph, build_travel_graph, collapse_sequential_outline_edges, find_stitch_path, graph_is_valid, travel) +from ..utils.threading import check_stop_flag def guided_fill(shape, @@ -68,6 +69,8 @@ def path_to_stitches(path, travel_graph, fill_stitch_graph, stitch_length, runni stitches.append(Stitch(*path[0].nodes[0])) for edge in path: + check_stop_flag() + if edge.is_segment(): current_edge = fill_stitch_graph[edge[0]][edge[-1]]['segment'] path_geometry = current_edge['geometry'] @@ -233,6 +236,8 @@ def intersect_region_with_grating_guideline(shape, line, row_spacing, num_stagge offset_line = None rows = [] while True: + check_stop_flag() + if strategy == 0: translate_amount = translate_direction * row * row_spacing offset_line = translate(line, xoff=translate_amount.x, yoff=translate_amount.y) diff --git a/lib/stitches/ripple_stitch.py b/lib/stitches/ripple_stitch.py index a66eff74..33054119 100644 --- a/lib/stitches/ripple_stitch.py +++ b/lib/stitches/ripple_stitch.py @@ -9,6 +9,7 @@ from .running_stitch import running_stitch from ..elements import SatinColumn from ..utils import Point as InkstitchPoint from ..utils.geometry import line_string_to_point_list +from ..utils.threading import check_stop_flag def ripple_stitch(stroke): @@ -80,6 +81,8 @@ def _get_satin_ripple_helper_lines(stroke): helper_lines = [] for point0, point1 in rail_pairs: + check_stop_flag() + helper_lines.append([]) helper_line = LineString((point0, point1)) for step in steps: @@ -95,6 +98,8 @@ def _converge_helper_line_points(helper_lines, point_edge=False): num_lines = len(helper_lines) steps = _get_steps(num_lines) for i, line in enumerate(helper_lines): + check_stop_flag() + points = [] for j in range(len(line) - 1): if point_edge and j % 2 == 1: @@ -133,6 +138,8 @@ def _target_point_helper_lines(stroke, outline): target = stroke.get_ripple_target() steps = _get_steps(stroke.get_line_count(), exponent=stroke.exponent, flip=stroke.flip_exponent) for i, point in enumerate(outline.coords): + check_stop_flag() + line = LineString([point, target]) for step in steps: @@ -193,6 +200,8 @@ def _generate_guided_helper_lines(stroke, outline, max_distance, guide_line): previous_guide_point = None for i in range(stroke.get_line_count()): + check_stop_flag() + guide_point = InkstitchPoint.from_shapely_point(guide_line.interpolate(outline_steps[i], normalized=True)) translation = guide_point - start_point scaling = scale_steps[i] @@ -232,6 +241,8 @@ def _generate_satin_guide_helper_lines(stroke, outline, guide_line): # add scaled and rotated outlines along the satin column guide line for i, (point0, point1) in enumerate(zip(*rail_points)): + check_stop_flag() + guide_center = (point0 + point1) / 2 translation = guide_center - outline_center if stroke.rotate_ripples: diff --git a/lib/stitches/running_stitch.py b/lib/stitches/running_stitch.py index c1c2d99c..8ba53498 100644 --- a/lib/stitches/running_stitch.py +++ b/lib/stitches/running_stitch.py @@ -12,6 +12,7 @@ import numpy as np from shapely import geometry as shgeo from ..utils import prng from ..utils.geometry import Point +from ..utils.threading import check_stop_flag """ Utility functions to produce running stitches. """ @@ -196,6 +197,8 @@ def stitch_curve_evenly(points: typing.Sequence[Point], stitch_length: float, to last = points[0] stitches = [] while i is not None and i < len(points): + check_stop_flag() + d = last.distance(points[i]) + distLeft[i] if d == 0: return stitches diff --git a/lib/stitches/utils/autoroute.py b/lib/stitches/utils/autoroute.py index 5acb1400..3ada4299 100644 --- a/lib/stitches/utils/autoroute.py +++ b/lib/stitches/utils/autoroute.py @@ -13,6 +13,7 @@ import inkex from ...svg import get_correction_transform from ...svg.tags import INKSCAPE_LABEL +from ...utils.threading import check_stop_flag def find_path(graph, starting_node, ending_node): @@ -31,6 +32,8 @@ def find_path(graph, starting_node, ending_node): # "underpathing". path = nx.shortest_path(graph, starting_node, ending_node) + check_stop_flag() + # Copy the graph so that we can remove the edges as we visit them. # This also converts the directed graph into an undirected graph in the # case that "preserve_order" is set. @@ -40,6 +43,8 @@ def find_path(graph, starting_node, ending_node): final_path = [] prev = None for node in path: + check_stop_flag() + if prev is not None: final_path.append((prev, node)) prev = node @@ -85,6 +90,8 @@ def add_jumps(graph, elements, preserve_order): # will enforce stitching the elements in order. for element1, element2 in zip(elements[:-1], elements[1:]): + check_stop_flag() + potential_edges = [] nodes1 = get_nodes_on_element(graph, element1) @@ -106,6 +113,7 @@ def add_jumps(graph, elements, preserve_order): # a weight, which we'll set as the length of the jump stitch. The # algorithm will minimize the total length of jump stitches added. for jump in nx.k_edge_augmentation(graph, 1, avail=list(possible_jumps(graph))): + check_stop_flag() graph.add_edge(*jump, jump=True) return graph |
