Optimisation des déplacements

This commit is contained in:
Alex committed 2026-10-23 17:02:48 +02:00
1 parent 99c30068fc
commit 437345c9b7
4 files changed
+229 -9

No files matched your search

+66 -6
View File
@@ -9,9 +9,10 @@ import pytest
import math
from relief_core import (CELL_EDGES, clip_segment, fill_rings, parse_color,
pattern_segments, point_in_rings, polylines_to_d,
rotate_point)
from relief_core import (CELL_EDGES, chain_segments, clip_segment, fill_rings,
order_polylines, parse_color, pattern_segments,
point_in_rings, polylines_to_d, rotate_point,
travel_length)
HERE = os.path.dirname(os.path.abspath(__file__))
SHAPES = os.path.join(HERE, "tests", "data", "shapes.svg")
@@ -168,6 +169,54 @@ def test_rotate_point_clockwise_on_screen():
assert (round(x, 6), round(y, 6)) == (0, 1)
def polyline_segments(polylines):
"""Segments (cles arrondies, sens indifferent) couverts par des polylignes."""
out = []
for line in polylines:
for p, q in zip(line, line[1:]):
out.append(min(key((p, q)), key((q, p))))
return sorted(out)
def test_chain_segments_joins_touching_segments():
# Un « Y » (4 noeuds impairs : 2 traits au minimum) et un trait isole.
y_shape = [((0, 0), (0, -1)), ((0, 0), (1, 1)), ((0, 0), (-1, 1))]
lone = [((5, 5), (6, 5))]
polylines = chain_segments(y_shape + lone)
assert len(polylines) == 3
assert polyline_segments(polylines) == polyline_segments([[p, q] for p, q in y_shape + lone])
def test_chain_segments_closed_loop():
square = [((0, 0), (1, 0)), ((1, 0), (1, 1)), ((1, 1), (0, 1)), ((0, 1), (0, 0))]
polylines = chain_segments(square)
assert len(polylines) == 1 and len(polylines[0]) == 5
assert polylines[0][0] == polylines[0][-1]
def test_order_polylines_nearest_and_reversed():
lines = [[(10, 0), (20, 0)], [(3, 0), (1, 0)], [(30, 0), (21, 0)]]
ordered = order_polylines(lines, start=(0, 0))
assert ordered == [[(1, 0), (3, 0)], [(10, 0), (20, 0)], [(21, 0), (30, 0)]]
assert travel_length(ordered) == pytest.approx(1 + 7 + 1)
def test_order_polylines_rotates_loops():
loop = [(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]
ordered = order_polylines([loop], start=(11, 11))
assert ordered[0][0] == ordered[0][-1] == (10, 10)
assert len(ordered[0]) == 5
def test_fill_order_keeps_lines_and_cuts_travel():
segments = fill_rings(HOLED, 5.0, 17.0, (50, 50))
raw = [[p, q] for p, q in segments]
ordered = order_polylines(chain_segments(segments))
assert polyline_segments(ordered) == polyline_segments(raw)
assert len(ordered) < len(raw) / 2
assert travel_length(ordered) < travel_length(raw) / 4
# --------------------------------------------------------------------------
# Bout en bout (necessite inkex)
# --------------------------------------------------------------------------
@@ -217,6 +266,17 @@ def generated_paths(svg_text):
yield [(nums[k], nums[k + 1]) for k in range(0, len(nums) - 1, 2)]
def generated_subpaths(svg_text):
"""Points (x, y) de chaque sous-chemin (polyligne) du groupe « Relief fill »."""
root = ET.fromstring(svg_text)
for group in root.iter("{http://www.w3.org/2000/svg}g"):
if "Relief fill" in group.attrib.values():
for path in group.iter("{http://www.w3.org/2000/svg}path"):
for sub in re.split(r"[Mm]", path.get("d"))[1:]:
nums = [float(n) for n in re.findall(r"-?\d+\.?\d*", sub)]
yield [(nums[k], nums[k + 1]) for k in range(0, len(nums) - 1, 2)]
@pytest.mark.parametrize("shape,box", [
("rect1", (10, 10, 60, 50)),
("circle1", (78, 8, 122, 52)),
@@ -234,10 +294,10 @@ def test_end_to_end_stays_in_bounding_box(tmp_path, shape, box):
def test_end_to_end_hole_stays_empty(tmp_path):
svg = run_extension(tmp_path, "--id=path2", "--edge=3")
count = 0
for pts in generated_paths(svg):
for k in range(0, len(pts) - 1, 2):
for pts in generated_subpaths(svg):
for p, q in zip(pts, pts[1:]):
count += 1
mid = ((pts[k][0] + pts[k + 1][0]) / 2, (pts[k][1] + pts[k + 1][1]) / 2)
mid = ((p[0] + q[0]) / 2, (p[1] + q[1]) / 2)
assert not (36 < mid[0] < 64 and 81 < mid[1] < 99)
assert count > 20