fix invalid clip path (#2577)

use fallback if shape has grating issues (too small)
pull/2594/head
Kaalleen 2023-11-10 16:41:36 +01:00 zatwierdzone przez GitHub
rodzic e1a1e887ff
commit 7edebd60f9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 26 dodań i 8 usunięć

Wyświetl plik

@ -10,7 +10,7 @@ import re
import numpy as np
from inkex import Transform
from shapely import geometry as shgeo
from shapely.errors import TopologicalError
from shapely.errors import GEOSException
from shapely.validation import explain_validity, make_valid
from .. import tiles
@ -562,16 +562,20 @@ class FillStitch(EmbroideryElement):
polygons.append(polygon)
if isinstance(polygon, shgeo.MultiPolygon):
polygons.extend(polygon.geoms)
return shgeo.MultiPolygon(polygons)
def _get_clipped_path(self):
if self.node.clip is None:
return self.original_shape
clip_path = get_clip_path(self.node)
# make sure clip path and shape are valid
clip_path = make_valid(clip_path)
shape = make_valid(self.original_shape)
try:
intersection = clip_path.intersection(self.original_shape)
except TopologicalError:
intersection = clip_path.intersection(shape)
except GEOSException:
return self.original_shape
if isinstance(intersection, shgeo.Polygon):

Wyświetl plik

@ -7,6 +7,7 @@ from math import ceil
import shapely.geometry as shgeo
from inkex import Transform
from shapely.errors import GEOSException
from ..i18n import _
from ..marker import get_marker_elements
@ -14,13 +15,12 @@ from ..stitch_plan import StitchGroup
from ..stitches.ripple_stitch import ripple_stitch
from ..stitches.running_stitch import bean_stitch, running_stitch
from ..svg import get_node_transform, parse_length_with_units
from ..svg.clip import get_clip_path
from ..threads import ThreadColor
from ..utils import Point, cache
from ..utils.param import ParamOption
from .element import EmbroideryElement, param
from ..svg.clip import get_clip_path
from .validation import ValidationWarning
from shapely.errors import TopologicalError
class MultipleGuideLineWarning(ValidationWarning):
@ -411,7 +411,7 @@ class Stroke(EmbroideryElement):
line_strings = [shgeo.LineString(path) for path in paths]
try:
intersection = clip_path.intersection(shgeo.MultiLineString(line_strings))
except TopologicalError:
except GEOSException:
return paths
coords = []

Wyświetl plik

@ -25,6 +25,10 @@ from ..utils.smoothing import smooth_path
from ..utils.threading import check_stop_flag
class NoGratingsError(Exception):
pass
class PathEdge(object):
OUTLINE_KEYS = ("outline", "extra", "initial")
SEGMENT_KEY = "segment"
@ -78,6 +82,10 @@ def auto_fill(shape,
return fallback(shape, running_stitch_length, running_stitch_tolerance)
travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath)
if not travel_graph:
return fallback(shape, running_stitch_length, running_stitch_tolerance)
path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point)
result = path_to_stitches(shape, path, travel_graph, fill_stitch_graph, angle, row_spacing,
max_stitch_length, running_stitch_length, running_stitch_tolerance,
@ -314,7 +322,10 @@ def build_travel_graph(fill_stitch_graph, shape, fill_stitch_angle, underpath):
graph.add_nodes_from(fill_stitch_graph.nodes(data=True))
if underpath:
boundary_points, travel_edges = build_travel_edges(shape, fill_stitch_angle)
try:
boundary_points, travel_edges = build_travel_edges(shape, fill_stitch_angle)
except NoGratingsError:
return
# This will ensure that a path traveling inside the shape can reach its
# target on the outline, which will be one of the points added above.
@ -459,6 +470,9 @@ def build_travel_edges(shape, fill_angle):
for ls in mls.geoms
for coord in ls.coords]
if grating1.is_empty or grating2.is_empty:
raise NoGratingsError()
diagonal_edges = ensure_multi_line_string(grating1.symmetric_difference(grating2))
check_stop_flag()