qcc_kplugin.geometry ==================== .. py:module:: qcc_kplugin.geometry .. autoapi-nested-parse:: Plain-Python geometry for the GUI tools (points are ``(x, y)`` tuples in µm; no pya, so it is unit-tested). Mirrors what ``quantcore_circuits.route`` does when it builds a route, closely enough for a live preview: port stub → user waypoints → port stub, with Manhattan corners when there are no waypoints (or one oblique straight between ports that face an angle: :func:`oblique_mids`). The real geometry (KQC ``WaveguideComposite``, bends of radius ``r``, meanders) always comes from the rebuild. Attributes ---------- .. autoapisummary:: qcc_kplugin.geometry.ALIGN_TOL qcc_kplugin.geometry.EPS qcc_kplugin.geometry.FACE_TOL_DEG qcc_kplugin.geometry.Point Functions --------- .. autoapisummary:: qcc_kplugin.geometry.add qcc_kplugin.geometry.compatible qcc_kplugin.geometry.constrain qcc_kplugin.geometry.corner_cut qcc_kplugin.geometry.dist qcc_kplugin.geometry.dist_point_polyline qcc_kplugin.geometry.dist_point_segment qcc_kplugin.geometry.dot qcc_kplugin.geometry.elbow qcc_kplugin.geometry.free_end_dir qcc_kplugin.geometry.handle_points qcc_kplugin.geometry.hanger_side qcc_kplugin.geometry.is_axis qcc_kplugin.geometry.mul qcc_kplugin.geometry.nearest qcc_kplugin.geometry.norm qcc_kplugin.geometry.oblique_mids qcc_kplugin.geometry.ortho qcc_kplugin.geometry.ortho_between qcc_kplugin.geometry.parse_grid qcc_kplugin.geometry.radius_from_handle qcc_kplugin.geometry.radius_handle qcc_kplugin.geometry.rounded_points qcc_kplugin.geometry.route_near qcc_kplugin.geometry.route_preview qcc_kplugin.geometry.same_line qcc_kplugin.geometry.segment_near qcc_kplugin.geometry.snap45 qcc_kplugin.geometry.snap_candidate qcc_kplugin.geometry.snap_candidate_rot qcc_kplugin.geometry.snap_grid qcc_kplugin.geometry.snap_rays qcc_kplugin.geometry.sub qcc_kplugin.geometry.tight_segments qcc_kplugin.geometry.turn_angle qcc_kplugin.geometry.unit qcc_kplugin.geometry.waypoint_insert_index Module Contents --------------- .. py:function:: add(a: Point, b: Point) -> Point .. py:function:: compatible(pa: dict, pb: dict, tol: float = 0.001) -> bool Ports that can be snapped together: same CPW (a, b) and facing each other. .. py:function:: constrain(p: Point, grid: float, neighbours=(), shift: bool = False, rays=(), tol: float = 0.0) -> Point The point a click / move at ``p`` stands for: on the grid and, with Shift, constrained by ``neighbours``. One neighbour (drawing the next segment, moving an end node): the segment snaps to 0° / 45° / 90° / …; two neighbours (moving a middle node): horizontal / vertical to both. Aligned coordinates take the neighbour's exact value, which wins over the grid. ``rays`` ((origin, direction) lines: the axes of angled ports) are offered with Shift too: a ray within ``tol`` µm of ``p`` and nearer than the 45° / aligned choice wins. Without Shift the point is only put on the grid: any angle (Ctrl in the tools is the same, spelled out). .. py:function:: corner_cut(prev: Point, p: Point, nxt: Point, r: float) -> float Length of each adjacent straight a bend of radius ``r`` at ``p`` uses (KQC corner model). .. py:function:: dist(a: Point, b: Point) -> float .. py:function:: dist_point_polyline(p: Point, pts: list[Point]) -> tuple[float, int] Distance to a polyline and the 1-based index of the closest segment (segment k joins points k-1 and k). .. py:function:: dist_point_segment(p: Point, a: Point, b: Point) -> float .. py:function:: dot(a: Point, b: Point) -> float .. py:function:: elbow(anchor: Point, anchor_dir: Point | None, p: Point) -> list[Point] The square corner between ``anchor`` and ``p`` (``[]`` when they are aligned): the segment leaves ``anchor`` along ``anchor_dir`` (the previous segment / the port), or along the longer axis when there is none. This is the corner the Connect preview shows, and a click commits it as a node, so the built route matches the preview. .. py:function:: free_end_dir(end: Point, toward: Point) -> float | None ``FreeEnd.dir`` (degrees, pointing from the end into the route) of a free end at ``end`` whose route comes from ``toward``. .. py:function:: handle_points(route: dict) -> tuple[list[tuple[int, Point]], list[tuple[int, Point]]] (waypoint handles [(node_index, pos)], insert handles [(segment, midpoint)]) for a sidecar route. .. py:function:: hanger_side(polyline: list[Point], segment: int, toward: Point) -> str ``"left"`` / ``"right"`` of the route's travel direction on ``segment`` (1-based), the side ``toward`` is on (a hanger coupler sits on the side its resonator comes from). .. py:function:: is_axis(d: Point | None) -> bool True for a horizontal / vertical direction (a port that faces along an axis). .. py:function:: mul(a: Point, s: float) -> Point .. py:function:: nearest(points: dict[str, Point], p: Point, radius: float) -> str | None .. py:function:: norm(a: Point) -> float .. py:function:: oblique_mids(p0: Point, d0: Point | None, p1: Point, d1: Point | None, r: float = 0.0) -> list[Point] | None Corners of the oblique route the router draws between stub ends ``p0`` (arriving along ``d0``) and ``p1`` (leaving along ``d1``) when a port faces an angle, else None (Manhattan, as the router). ``[]`` = one straight (the ports face each other along a line, within 5°); ``[X]`` = one corner where the two ports' axes meet. Candidates whose bends do not fit the radius ``r`` are left out, as in the router. .. py:function:: ortho(anchor: Point, p: Point) -> Point ``p`` moved so the segment from ``anchor`` is horizontal or vertical, whichever is nearer (Shift). .. py:function:: ortho_between(p: Point, neighbours) -> Point ``p`` (a node being moved or inserted) aligned with its neighbours (Shift): each neighbour segment is made horizontal or vertical, the nearer choice first; a second neighbour can only fix the other coordinate, so two generic neighbours give the square corner nearest ``p`` and two neighbours on one line keep ``p`` on it. .. py:function:: parse_grid(text) -> float | None A KLayout editor-grid config value (``"0.5"``, ``"1,1"``, ``""``) → grid step in µm, or None. .. py:function:: radius_from_handle(prev: Point, p: Point, nxt: Point, q: Point) -> float | None The bend radius whose arc middle is at ``q`` projected on the corner's bisector (inverse of :func:`radius_handle`); None on a straight node. .. py:function:: radius_handle(prev: Point, p: Point, nxt: Point, r: float) -> Point | None Curvature handle of the corner at ``p`` bent to radius ``r``: the middle of the arc, on the bisector. .. py:function:: rounded_points(points: list[Point], radii: list[float], segments_per_90: int = 12) -> list[Point] Sharp polyline → polyline with every interior corner ``i`` replaced by an arc of radius ``radii[i]`` (index by point; the ends are ignored). Radii too big for the legs are clipped so the preview stays drawable. .. py:function:: route_near(routes: dict, p: Point, radius: float, exclude=()) -> tuple[str, int] | None The sidecar route whose polyline passes within ``radius`` µm of ``p``: ``(name, segment)`` or None. .. py:function:: route_preview(start: Point, start_dir: Point | None, waypoints: list[Point], end: Point, end_dir: Point | None, stub: float) -> list[Point] Sharp polyline of a route from a port (``start``, outward ``start_dir``) through ``waypoints`` to ``end``. ``end_dir`` is the outward direction of the end port (``None`` while the end is still the mouse position or a free end); ``start_dir`` is None for a route starting at a free end (no stub). .. py:function:: same_line(pa: dict, pb: dict, tol: float = 0.001) -> bool Ports of the same CPW cross-section (a, b), whichever way they face. .. py:function:: segment_near(polyline: list[Point], p: Point, radius: float) -> int | None 1-based index of the polyline segment within ``radius`` of ``p``, or None. .. py:function:: snap45(anchor: Point, p: Point, grid: float = 0.0) -> Point ``p`` moved onto the nearest of the 8 directions from ``anchor`` (0°, 45°, 90°, …; Shift while drawing). Horizontal / vertical keep the anchor's exact coordinate; a diagonal keeps \|dx\| = \|dy\| on the grid. .. py:function:: snap_candidate(moving: dict[str, dict], others: dict[str, dict], delta: Point, radius: float) -> tuple[str, str, Point] | None Best (moving_port, target_port, corrected_delta) when ``moving`` ports are translated by ``delta``. ``moving``/``others`` map port names to sidecar ports ``{"pos", "dir", "a", "b"}``. Only translation is considered while dragging; the rebuild solves the rotation for ``place.snap`` exactly. .. py:function:: snap_candidate_rot(moving: dict[str, dict], others: dict[str, dict], delta: Point, radius: float, origin: Point = (0.0, 0.0), turn: float = 0.0, rotate: bool = True) -> tuple[str, str, Point, float] | None Best ``(moving_port, target_port, corrected_delta, extra_turn)`` while dragging, rotation included. ``moving``/``others`` are sidecar ports ``{"pos", "dir", "a", "b"}``. The held instance is turned by ``turn`` degrees about ``origin`` (the 90° steps of [ and ]) and translated by ``delta``. With ``rotate`` a port snaps to any target port of the same CPW, however it faces: the instance is turned by ``extra_turn`` (degrees, -180..180) more, so the port faces the target exactly (StarQubit pins 72° apart, ConcentricTransmon couplers at 60°), and the port then has to be within ``radius`` of the target *after* that turn. The smallest turn wins ties (a symmetric qubit has four ports that all land on the same spot). Without ``rotate`` only ports that already face the target (within 2.5°) snap, with ``extra_turn`` 0: translation only. ``corrected_delta`` is the translation that puts the port on the target. .. py:function:: snap_grid(p: Point, grid: float) -> Point .. py:function:: snap_rays(p: Point, q: Point, rays, tol: float, grid: float = 0.0) -> Point ``q`` (the point an axis / 45° snap chose for ``p``), or ``p`` put on the nearest of ``rays`` ((origin, direction) lines, e.g. an angled port's own axis) when that ray passes within ``tol`` of ``p`` and nearer than ``q``. The distance along the ray is rounded to the grid. .. py:function:: sub(a: Point, b: Point) -> Point .. py:function:: tight_segments(points: list[Point], radii: list[float]) -> list[int] Segments (1-based) too short for the bends at both their ends (the rebuild would fail there). .. py:function:: turn_angle(prev: Point, p: Point, nxt: Point) -> float .. py:function:: unit(v: Point) -> Point | None .. py:function:: waypoint_insert_index(waypoint_pidx: list[int], segment: int) -> int Index in ``route.nodes`` for a new waypoint on polyline ``segment`` (1-based). .. py:data:: ALIGN_TOL .. py:data:: EPS :value: 1e-06 .. py:data:: FACE_TOL_DEG :value: 2.5 .. py:data:: Point