"""Freeze reusable editor artwork; no observations or scene coordinates.""" import ast import hashlib import importlib.util import json import re from pathlib import Path import numpy as np ROOT = Path(__file__).resolve().parents[1] SOURCE = ROOT / "simulation/urban-plan-research" def main(): templates = [] def add(key, label, polygons, source): templates.append(dict(id=key, label=label, polygons=polygons, source=source)) # Same fixed short-profile vertices as replay/standard_arrow_templates.py. straight = [ (0, -0.075), (2.95, -0.075), (2.95, -0.375), (5, 0), (2.95, 0.375), (2.95, 0.075), (0, 0.075), ] turn = [ (y, -(x - 0.075)) for x, y in [ (0, 0), (0.15, 0), (0.15, 2.60), (0.75, 3.2), (0.75, 2.5), (1.05, 3.7), (0.75, 5), (0.75, 4.2), (0, 3.45), ] ] def closed(r): q = [[float(x), float(y)] for x, y in r] return q + [q[0][:]] if q[0] != q[-1] else q add( "linear_marking", "Линейная разметка", [[closed([(-1.5, -0.075), (1.5, -0.075), (1.5, 0.075), (-1.5, 0.075)])]], dict(artist="NODE.DC", license="project-authored", adaptation=("Editable starter rectangle; dimensions are adjustable, " "not a standard claim"), dimensions_m=[3, 0.15]), ) ref = dict( path="simulation/urban-plan-research/replay/standard_arrow_templates.py", sha256=hashlib.sha256( (SOURCE / "replay/standard_arrow_templates.py").read_bytes() ).hexdigest(), profile="short", ) for key, label, r in [ ("straight_arrow", "Стрелка прямо", straight), ("right_arrow", "Поворот направо", turn), ("left_arrow", "Поворот налево", [(x, -y) for x, y in turn]), ]: add(key, label, [[closed(r)]], ref) # Operator placement presets, not a claim of road-standard compliance. add( "letter_A", "Буква А", [ [ closed([(0, -0.7), (4, 0), (0, 0.7), (0, 0.5), (1.1, 0.3), (1.1, -0.3), (0, -0.5)]), closed([(1.4, -0.24), (3.3, 0), (1.4, 0.24)]), ] ], dict(kind="editor-artwork", dimensions_m=[4, 1.4], compliance="not-assessed"), ) add( "zebra", "Зебра", [ [closed([(x, -2), (x + 0.5, -2), (x + 0.5, 2), (x, 2)])] for x in (-2.75, -1.75, -0.75, 0.25, 1.25, 2.25) ], dict(kind="editor-artwork", dimensions_m=[5.5, 4], compliance="not-assessed"), ) # Reuse attributed bicycle SVG and its existing curve sampling exactly. artwork = {} exec((SOURCE / "catalog_pass/bicycle_artwork.py").read_text(), artwork) node = next( n for n in ast.parse((SOURCE / "catalog_pass/catalog_templates.py").read_text()).body if isinstance(n, ast.FunctionDef) and n.name == "svg_rings" ) scope = dict(np=np, re=re) exec(compile(ast.Module(body=[node], type_ignores=[]), "svg_rings", "exec"), scope) rings = scope["svg_rings"](artwork["PATH"]) xy = np.concatenate(rings) lo = xy.min(0) span = xy.max(0) - lo rings = [ closed(np.c_[((r - lo) / span)[:, 1] * -2.5 + 1.25, ((r - lo) / span)[:, 0] * -1.1 + 0.55]) for r in rings ] add( "bicycle", "Велосипед", [rings], dict( artist="Major Alexey", license="CC BY-SA 4.0", url="https://commons.wikimedia.org/wiki/File:1.23.3_Russian_road_marking.svg", source_sha256=artwork["SOURCE_SHA256"], adaptation="curve sampling and metric scaling", ), ) # Keep the old id immutable for replay; new placements use the detector's source artwork. next(t for t in templates if t["id"] == "letter_A")["hidden"] = True from shapely.geometry import LineString from shapely.ops import unary_union letter_source = SOURCE / "systematic/paint_templates.py" letter_node = next( n for n in ast.parse(letter_source.read_text()).body if isinstance(n, ast.FunctionDef) and n.name == "template" ) letter_scope = dict(LineString=LineString, unary_union=unary_union) exec( compile(ast.Module(body=[letter_node], type_ignores=[]), "letter_template", "exec"), letter_scope, ) def polygons(q): parts = [q] if q.geom_type == "Polygon" else list(q.geoms) return [ [closed(part.exterior.coords), *[closed(h.coords) for h in part.interiors]] for part in parts ] add( "letter_A_v2", "Буква А", polygons(letter_scope["template"]("letter_A", 4.0, 1.4, 0.18)), dict( path="simulation/urban-plan-research/systematic/paint_templates.py", sha256=hashlib.sha256(letter_source.read_bytes()).hexdigest(), dimensions_m=[4.0, 1.4], stroke_m=0.18, compliance="not-assessed", ), ) # Historical ids must replay their exact old geometry. Road artwork is a new id. next(t for t in templates if t["id"] == "letter_A_v2")["hidden"] = True add( "letter_A_v3", "Буква А", [[closed([(0, -.7), (3, -.125), (3, .125), (0, .7), (0, .45), (.4, .375), (.4, -.375), (0, -.45)]), closed([(1, -.25), (2.2, 0), (1, .25)])]], dict(kind="road-marking-artwork", standard="GOST R 51256-2018", figure="B.11", marking="1.23.1", dimensions_m=[3, 1.4], top_width_m=.25, foot_width_m=.25, crossbar_bottom_m=.4, crossbar_height_m=.6, url="https://www.mos.ru/upload/documents/files/1106/GOSTR51256-2018TehnicheskiesredstvaorganizaciidorojnogodvijeniyaR_.pdf", adaptation=("Dimensioned silhouette; undimensioned inner slopes traced from figure. " "Longitudinal axis +X."), compliance="artwork-reference-not-survey-certification"), ) spec = importlib.util.spec_from_file_location( "standard_arrows", SOURCE / "replay/standard_arrow_templates.py" ) arrows = importlib.util.module_from_spec(spec) spec.loader.exec_module(arrows) for key, label in [ ("straight_right_arrow", "Прямо и направо"), ("straight_left_arrow", "Прямо и налево"), ]: add(key, label, polygons(arrows.preset(key, "short")), ref) bicycle = next(t for t in templates if t["id"] == "bicycle") bicycle["label"] = "Велосипед · вытянутый" add( "bicycle_compact", "Велосипед · компактный", [ [[[x * 1.4 / 2.5, y] for x, y in ring] for ring in polygon] for polygon in bicycle["polygons"] ], { **bicycle["source"], "dimensions_m": [1.4, 1.1], "adaptation": "compact metric placement variant; same attributed artwork", }, ) # Immutable B.6 merger presets; repeated Bezier junctions are not vertices. for key, label in [("merge_left_arrow", "Смещение налево"), ("merge_right_arrow", "Смещение направо")]: q = arrows.preset(key, "short") ring = [] for point in q.exterior.coords: if not ring or tuple(point) != tuple(ring[-1]): ring.append(point) add(key, label, [[closed(ring)]], {**ref, "figure": "B.6"}) # Attributed sign artwork, converted to longitudinal road paint. This # metric placement preset is a visual reconstruction, not certification. import xml.etree.ElementTree as ET from shapely.geometry import Polygon svg = SOURCE / "catalog_pass/children_warning.svg" elements = ET.fromstring(svg.read_text()).findall("{http://www.w3.org/2000/svg}path") parts = [] for element in elements: style = element.get("style", "") if "fill:#ff0000" not in style and "fill:#000000" not in style: continue rings = scope["svg_rings"](element.attrib["d"]) q = Polygon(rings[0], rings[1:]) assert q.is_valid parts.append(q) # Source sign tip is up. +X is forward, +Y is left from the driver. def clean_road_ring(points): clean = [] for point in points: if not clean or np.linalg.norm(np.array(point) - clean[-1]) > 1e-8: clean.append(list(point)) if np.linalg.norm(np.array(clean[0]) - clean[-1]) < 1e-8: clean.pop() return closed(clean) def road_ring(ring): return clean_road_ring([(1.5 - (y - 10) / 714.423 * 3, (417.058 - x) / 814.116 * 1.5) for x, y in ring]) add("warning_children", "Предупреждение: дети", [[road_ring(q.exterior.coords), *[road_ring(h.coords) for h in q.interiors]] for q in parts], dict(artist="Юкатан; Nikolaev ec06ffa5", license="CC BY-SA 3.0", license_url="https://creativecommons.org/licenses/by-sa/3.0/", url="https://commons.wikimedia.org/wiki/File:RU_road_sign_1.23.svg", source_sha256=hashlib.sha256(svg.read_bytes()).hexdigest(), dimensions_m=[3, 1.5], marking="1.24.1", sign="1.23", adaptation="White road-paint silhouette; curve sampling; longitudinal stretch", compliance="visual-placement-preset-not-survey-certification")) # Existing ids are immutable replay contracts. New artwork needs a new id. frozen_path = ROOT / "src/k1link/simulation/ai_polygon/marking-templates.json" if frozen_path.exists(): frozen = {t["id"]: t for t in json.loads(frozen_path.read_text())["templates"]} templates = [frozen.get(t["id"], t) for t in templates] (ROOT / "src/k1link/simulation/ai_polygon/marking-templates.json").write_text( json.dumps( dict(version="semantic-marking-artwork/v1", templates=templates), ensure_ascii=False, separators=(",", ":"), ) + "\n" ) if __name__ == "__main__": main()