diff options
| author | Lex Neva <github.com@lexneva.name> | 2022-04-06 08:12:45 -0400 |
|---|---|---|
| committer | Kaalleen <reni@allenka.de> | 2022-05-04 19:18:33 +0200 |
| commit | 920063b324fd59798bc462c644bce8fc543f535b (patch) | |
| tree | e2b9310b51ad417a151f36580d1da1bab611be4a /lib/stitches | |
| parent | f4c47a8e226af11852e2e530c9b4a6a6403bff07 (diff) | |
fix style
Diffstat (limited to 'lib/stitches')
| -rw-r--r-- | lib/stitches/guided_fill.py | 25 | ||||
| -rw-r--r-- | lib/stitches/point_transfer.py | 2 | ||||
| -rw-r--r-- | lib/stitches/sample_linestring.py | 40 | ||||
| -rw-r--r-- | lib/stitches/tangential_fill_stitch_line_creator.py | 169 | ||||
| -rw-r--r-- | lib/stitches/tangential_fill_stitch_pattern_creator.py | 31 |
5 files changed, 145 insertions, 122 deletions
diff --git a/lib/stitches/guided_fill.py b/lib/stitches/guided_fill.py index 51b0618f..fb122fba 100644 --- a/lib/stitches/guided_fill.py +++ b/lib/stitches/guided_fill.py @@ -2,20 +2,17 @@ import networkx from depq import DEPQ from shapely.geometry import GeometryCollection, LineString, MultiLineString from shapely.ops import linemerge, unary_union -from shapely.strtree import STRtree -from ..debug import debug -from ..i18n import _ -from ..stitch_plan import Stitch -from ..svg import PIXELS_PER_MM -from ..utils.geometry import Point as InkstitchPoint from .auto_fill import (add_edges_between_outline_nodes, build_travel_graph, collapse_sequential_outline_edges, fallback, find_stitch_path, graph_is_valid, insert_node, - tag_nodes_with_outline_and_projection, travel, - weight_edges_by_length) + tag_nodes_with_outline_and_projection, travel) from .point_transfer import transfer_points_to_surrounding_graph from .sample_linestring import raster_line_string_with_priority_points +from ..debug import debug +from ..i18n import _ +from ..stitch_plan import Stitch +from ..utils.geometry import Point as InkstitchPoint @debug.time @@ -124,7 +121,15 @@ def build_guided_fill_stitch_graph(shape, guideline, row_spacing, starting_point return graph -def stitch_line(stitches, stitching_direction, geometry, projected_points, max_stitch_length, min_stitch_length, row_spacing, skip_last, offset_by_half): +def stitch_line(stitches, + stitching_direction, + geometry, + projected_points, + max_stitch_length, + min_stitch_length, + row_spacing, + skip_last, + offset_by_half): if stitching_direction == 1: stitched_line, _ = raster_line_string_with_priority_points( geometry, 0.0, geometry.length, max_stitch_length, min_stitch_length, projected_points, abs(row_spacing), offset_by_half, True) @@ -133,7 +138,7 @@ def stitch_line(stitches, stitching_direction, geometry, projected_points, max_s geometry, geometry.length, 0.0, max_stitch_length, min_stitch_length, projected_points, abs(row_spacing), offset_by_half, True) stitches.append(Stitch(*stitched_line[0], tags=('fill_row_start',))) - for i in range(1, len(stitched_line)-1): + for i in range(1, len(stitched_line) - 1): stitches.append(Stitch(*stitched_line[i], tags=('fill_row'))) if not skip_last: diff --git a/lib/stitches/point_transfer.py b/lib/stitches/point_transfer.py index 5506324d..553ffbda 100644 --- a/lib/stitches/point_transfer.py +++ b/lib/stitches/point_transfer.py @@ -1,7 +1,7 @@ import math from collections import namedtuple -from shapely.geometry import LinearRing, LineString, MultiPoint, Point +from shapely.geometry import LineString, LinearRing, MultiPoint, Point from shapely.ops import nearest_points from ..stitches import constants, sample_linestring diff --git a/lib/stitches/sample_linestring.py b/lib/stitches/sample_linestring.py index 838b1792..85592984 100644 --- a/lib/stitches/sample_linestring.py +++ b/lib/stitches/sample_linestring.py @@ -63,8 +63,15 @@ def calculate_line_angles(line): return Angles -def raster_line_string_with_priority_points(line, start_distance, end_distance, maxstitch_distance, minstitch_distance, # noqa: C901 - must_use_points_deque, abs_offset, offset_by_half, replace_forbidden_points): +def raster_line_string_with_priority_points(line, # noqa: C901 + start_distance, + end_distance, + maxstitch_distance, + minstitch_distance, + must_use_points_deque, + abs_offset, + offset_by_half, + replace_forbidden_points): """ Rasters a line between start_distance and end_distance. Input: @@ -94,24 +101,25 @@ def raster_line_string_with_priority_points(line, start_distance, end_distance, if start_distance > end_distance: start_distance, end_distance = line.length - \ - start_distance, line.length-end_distance + start_distance, line.length - end_distance linecoords = linecoords[::-1] for i in range(len(deque_points)): deque_points[i] = (deque_points[i][0], - line.length-deque_points[i][1]) + line.length - deque_points[i][1]) else: # Since points with highest priority (=distance along line) are first (descending sorted) deque_points = deque_points[::-1] # Remove all points from the deque which do not fall in the segment [start_distance; end_distance] - while (len(deque_points) > 0 and deque_points[0][1] <= start_distance+min(maxstitch_distance/20, minstitch_distance, constants.point_spacing_to_be_considered_equal)): + while (len(deque_points) > 0 and + deque_points[0][1] <= start_distance + min(maxstitch_distance / 20, minstitch_distance, constants.point_spacing_to_be_considered_equal)): deque_points.pop(0) - while (len(deque_points) > 0 and deque_points[-1][1] >= end_distance-min(maxstitch_distance/20, minstitch_distance, constants.point_spacing_to_be_considered_equal)): + while (len(deque_points) > 0 and + deque_points[-1][1] >= end_distance - min(maxstitch_distance / 20, minstitch_distance, constants.point_spacing_to_be_considered_equal)): deque_points.pop() - -# Ordering in priority queue: -# (point, LineStringSampling.PointSource), priority) + # Ordering in priority queue: + # (point, LineStringSampling.PointSource), priority) # might be different from line for stitching_direction=-1 aligned_line = LineString(linecoords) path_coords = substring(aligned_line, @@ -121,17 +129,17 @@ def raster_line_string_with_priority_points(line, start_distance, end_distance, # I had the strange situation in which the offset "start_distance" from the line beginning # resulted in a starting point which was already present in aligned_line causing a doubled point. # A double point is not allowed in the following calculations so we need to remove it: - if (abs(path_coords.coords[0][0]-path_coords.coords[1][0]) < constants.eps and - abs(path_coords.coords[0][1]-path_coords.coords[1][1]) < constants.eps): + if (abs(path_coords.coords[0][0] - path_coords.coords[1][0]) < constants.eps and + abs(path_coords.coords[0][1] - path_coords.coords[1][1]) < constants.eps): path_coords.coords = path_coords.coords[1:] - if (abs(path_coords.coords[-1][0]-path_coords.coords[-2][0]) < constants.eps and - abs(path_coords.coords[-1][1]-path_coords.coords[-2][1]) < constants.eps): + if (abs(path_coords.coords[-1][0] - path_coords.coords[-2][0]) < constants.eps and + abs(path_coords.coords[-1][1] - path_coords.coords[-2][1]) < constants.eps): path_coords.coords = path_coords.coords[:-1] angles = calculate_line_angles(path_coords) # For the first and last point we cannot calculate an angle. Set it to above the limit to make it a hard edge - angles[0] = 1.1*constants.limiting_angle - angles[-1] = 1.1*constants.limiting_angle + angles[0] = 1.1 * constants.limiting_angle + angles[-1] = 1.1 * constants.limiting_angle current_distance = 0 last_point = Point(path_coords.coords[0]) @@ -221,7 +229,7 @@ def raster_line_string_with_priority_points(line, start_distance, end_distance, segment_length = merged_point_list[iter][1] - \ merged_point_list[segment_start_index][1] if segment_length < minstitch_distance and merged_point_list[iter][0].point_source != PointSource.HARD_EDGE_INTERNAL: - #We need to create this hard edge exception - otherwise there are some too large deviations posible + # We need to create this hard edge exception - otherwise there are some too large deviations posible iter += 1 continue diff --git a/lib/stitches/tangential_fill_stitch_line_creator.py b/lib/stitches/tangential_fill_stitch_line_creator.py index 01124478..6b141611 100644 --- a/lib/stitches/tangential_fill_stitch_line_creator.py +++ b/lib/stitches/tangential_fill_stitch_line_creator.py @@ -1,13 +1,15 @@ -from shapely.geometry.polygon import LinearRing, LineString -from shapely.geometry import Polygon, MultiLineString -from shapely.ops import polygonize +from enum import IntEnum + +from anytree import AnyNode, LevelOrderGroupIter, PreOrderIter +from depq import DEPQ +from shapely.geometry import MultiLineString, Polygon from shapely.geometry import MultiPolygon -from anytree import AnyNode, PreOrderIter, LevelOrderGroupIter, RenderTree +from shapely.geometry.polygon import LinearRing from shapely.geometry.polygon import orient -from depq import DEPQ -from enum import IntEnum -from ..stitches import tangential_fill_stitch_pattern_creator +from shapely.ops import polygonize + from ..stitches import constants +from ..stitches import tangential_fill_stitch_pattern_creator def offset_linear_ring(ring, offset, resolution, join_style, mitre_limit): @@ -21,81 +23,80 @@ def offset_linear_ring(ring, offset, resolution, join_style, mitre_limit): result_list.append(poly.exterior) return MultiLineString(result_list) - -# """ -# Solves following problem: When shapely offsets a LinearRing the -# start/end point might be handled wrongly since they -# are only treated as LineString. -# (See e.g. https://i.stack.imgur.com/vVh56.png as a problematic example) -# This method checks first whether the start/end point form a problematic -# edge with respect to the offset side. If it is not a problematic -# edge we can use the normal offset_routine. Otherwise we need to -# perform two offsets: -# -offset the ring -# -offset the start/end point + its two neighbors left and right -# Finally both offsets are merged together to get the correct -# offset of a LinearRing -# """ - -#PROBLEM: Did not work in rare cases since it expects the point order be maintained after offsetting the curve -#(e.g. the first point in the offsetted curve shall belong to the first point in the original curve). However, this -#assumption seems to be not always true that is why this code was replaced by the buffer routine. - -# coords = ring.coords[:] -# # check whether edge at index 0 is concave or convex. Only for -# # concave edges we need to spend additional effort -# dx_seg1 = dy_seg1 = 0 -# if coords[0] != coords[-1]: -# dx_seg1 = coords[0][0] - coords[-1][0] -# dy_seg1 = coords[0][1] - coords[-1][1] -# else: -# dx_seg1 = coords[0][0] - coords[-2][0] -# dy_seg1 = coords[0][1] - coords[-2][1] -# dx_seg2 = coords[1][0] - coords[0][0] -# dy_seg2 = coords[1][1] - coords[0][1] -# # use cross product: -# crossvalue = dx_seg1 * dy_seg2 - dy_seg1 * dx_seg2 -# sidesign = 1 -# if side == "left": -# sidesign = -1 - -# # We do not need to take care of the joint n-0 since we -# # offset along a concave edge: -# if sidesign * offset * crossvalue <= 0: -# return ring.parallel_offset(offset, side, resolution, join_style, mitre_limit) - -# # We offset along a convex edge so we offset the joint n-0 separately: -# if coords[0] != coords[-1]: -# coords.append(coords[0]) -# offset_ring1 = ring.parallel_offset( -# offset, side, resolution, join_style, mitre_limit -# ) -# offset_ring2 = LineString((coords[-2], coords[0], coords[1])).parallel_offset( -# offset, side, resolution, join_style, mitre_limit -# ) - -# # Next we need to merge the results: -# if offset_ring1.geom_type == "LineString": -# return LinearRing(offset_ring2.coords[:] + offset_ring1.coords[1:-1]) -# else: -# # We have more than one resulting LineString for offset of -# # the geometry (ring) = offset_ring1. -# # Hence we need to find the LineString which belongs to the -# # offset of element 0 in coords =offset_ring2 -# # in order to add offset_ring2 geometry to it: -# result_list = [] -# thresh = constants.offset_factor_for_adjacent_geometry * abs(offset) -# for offsets in offset_ring1: -# if ( -# abs(offsets.coords[0][0] - coords[0][0]) < thresh -# and abs(offsets.coords[0][1] - coords[0][1]) < thresh -# ): -# result_list.append( -# LinearRing(offset_ring2.coords[:] + offsets.coords[1:-1]) -# ) -# else: -# result_list.append(LinearRing(offsets)) -# return MultiLineString(result_list) + # """ + # Solves following problem: When shapely offsets a LinearRing the + # start/end point might be handled wrongly since they + # are only treated as LineString. + # (See e.g. https://i.stack.imgur.com/vVh56.png as a problematic example) + # This method checks first whether the start/end point form a problematic + # edge with respect to the offset side. If it is not a problematic + # edge we can use the normal offset_routine. Otherwise we need to + # perform two offsets: + # -offset the ring + # -offset the start/end point + its two neighbors left and right + # Finally both offsets are merged together to get the correct + # offset of a LinearRing + # """ + + # PROBLEM: Did not work in rare cases since it expects the point order be maintained after offsetting the curve + # (e.g. the first point in the offsetted curve shall belong to the first point in the original curve). However, this + # assumption seems to be not always true that is why this code was replaced by the buffer routine. + + # coords = ring.coords[:] + # # check whether edge at index 0 is concave or convex. Only for + # # concave edges we need to spend additional effort + # dx_seg1 = dy_seg1 = 0 + # if coords[0] != coords[-1]: + # dx_seg1 = coords[0][0] - coords[-1][0] + # dy_seg1 = coords[0][1] - coords[-1][1] + # else: + # dx_seg1 = coords[0][0] - coords[-2][0] + # dy_seg1 = coords[0][1] - coords[-2][1] + # dx_seg2 = coords[1][0] - coords[0][0] + # dy_seg2 = coords[1][1] - coords[0][1] + # # use cross product: + # crossvalue = dx_seg1 * dy_seg2 - dy_seg1 * dx_seg2 + # sidesign = 1 + # if side == "left": + # sidesign = -1 + + # # We do not need to take care of the joint n-0 since we + # # offset along a concave edge: + # if sidesign * offset * crossvalue <= 0: + # return ring.parallel_offset(offset, side, resolution, join_style, mitre_limit) + + # # We offset along a convex edge so we offset the joint n-0 separately: + # if coords[0] != coords[-1]: + # coords.append(coords[0]) + # offset_ring1 = ring.parallel_offset( + # offset, side, resolution, join_style, mitre_limit + # ) + # offset_ring2 = LineString((coords[-2], coords[0], coords[1])).parallel_offset( + # offset, side, resolution, join_style, mitre_limit + # ) + + # # Next we need to merge the results: + # if offset_ring1.geom_type == "LineString": + # return LinearRing(offset_ring2.coords[:] + offset_ring1.coords[1:-1]) + # else: + # # We have more than one resulting LineString for offset of + # # the geometry (ring) = offset_ring1. + # # Hence we need to find the LineString which belongs to the + # # offset of element 0 in coords =offset_ring2 + # # in order to add offset_ring2 geometry to it: + # result_list = [] + # thresh = constants.offset_factor_for_adjacent_geometry * abs(offset) + # for offsets in offset_ring1: + # if ( + # abs(offsets.coords[0][0] - coords[0][0]) < thresh + # and abs(offsets.coords[0][1] - coords[0][1]) < thresh + # ): + # result_list.append( + # LinearRing(offset_ring2.coords[:] + offsets.coords[1:-1]) + # ) + # else: + # result_list.append(LinearRing(offsets)) + # return MultiLineString(result_list) def take_only_valid_linear_rings(rings): @@ -250,7 +251,7 @@ def offset_poly(poly, offset, join_style, stitch_distance, min_stitch_distance, for j in range(len(current_holes)): inner = offset_linear_ring( current_holes[j].val, - -offset, #take negative offset for holes + -offset, # take negative offset for holes resolution=5, join_style=join_style, mitre_limit=10, @@ -327,7 +328,7 @@ def offset_poly(poly, offset, join_style, stitch_distance, min_stitch_distance, if previous_hole.parent is None: previous_hole.parent = current_poly - #print(RenderTree(root)) + # print(RenderTree(root)) make_tree_uniform_ccw(root) if strategy == StitchingStrategy.CLOSEST_POINT: diff --git a/lib/stitches/tangential_fill_stitch_pattern_creator.py b/lib/stitches/tangential_fill_stitch_pattern_creator.py index 95143bce..13a480e3 100644 --- a/lib/stitches/tangential_fill_stitch_pattern_creator.py +++ b/lib/stitches/tangential_fill_stitch_pattern_creator.py @@ -1,16 +1,18 @@ -from shapely.geometry.polygon import LineString, LinearRing -from shapely.geometry import Point, MultiPoint -from shapely.ops import nearest_points +import math from collections import namedtuple -from depq import DEPQ -import trimesh + import numpy as np -from scipy import spatial -import math +import trimesh from anytree import PreOrderIter -from ..stitches import sample_linestring -from ..stitches import point_transfer +from depq import DEPQ +from scipy import spatial +from shapely.geometry import MultiPoint, Point +from shapely.geometry.polygon import LineString, LinearRing +from shapely.ops import nearest_points + from ..stitches import constants +from ..stitches import point_transfer +from ..stitches import sample_linestring nearest_neighbor_tuple = namedtuple( "nearest_neighbor_tuple", @@ -889,8 +891,15 @@ def connect_raster_tree_spiral( lineseg = LineString([result_coords[-2], result_coords[-1], own_coords[0], own_coords[1]]) else: lineseg = LineString([result_coords[-2], result_coords[-1], own_coords[1]]) - (temp_coords, _) = sample_linestring.raster_line_string_with_priority_points(lineseg, 0, lineseg.length, stitch_distance, min_stitch_distance, - DEPQ(), abs_offset, offset_by_half, False) + (temp_coords, _) = sample_linestring.raster_line_string_with_priority_points(lineseg, + 0, + lineseg.length, + stitch_distance, + min_stitch_distance, + DEPQ(), + abs_offset, + offset_by_half, + False) if len(temp_coords) == 2: # only start and end point of lineseg was needed result_coords.pop() result_coords_origin.pop() |
