qcc_kplugin.geometry

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: oblique_mids()). The real geometry (KQC WaveguideComposite, bends of radius r, meanders) always comes from the rebuild.

Attributes

Functions

add(a, b) → Point

compatible(pa, pb[, tol]) → bool

Ports that can be snapped together: same CPW (a, b) and facing each other.

constrain(p, grid[, neighbours, shift, rays, tol]) → Point

The point a click / move at p stands for: on the grid and, with Shift, constrained by neighbours.

corner_cut(prev, p, nxt, r) → float

Length of each adjacent straight a bend of radius r at p uses (KQC corner model).

dist(a, b) → float

dist_point_polyline(p, pts) → tuple[float, int]

Distance to a polyline and the 1-based index of the closest segment (segment k joins points k-1 and k).

dist_point_segment(p, a, b) → float

dot(a, b) → float

elbow(anchor, anchor_dir, p) → list[Point]

The square corner between anchor and p ([] when they are aligned): the segment leaves anchor

free_end_dir(end, toward) → float | None

FreeEnd.dir (degrees, pointing from the end into the route) of a free end at end whose route comes

handle_points(route) → tuple[list[tuple[int, Point]], list[tuple[int, Point]]]

(waypoint handles [(node_index, pos)], insert handles [(segment, midpoint)]) for a sidecar route.

hanger_side(polyline, segment, toward) → str

"left" / "right" of the route's travel direction on segment (1-based), the side toward is on

is_axis(d) → bool

True for a horizontal / vertical direction (a port that faces along an axis).

mul(a, s) → Point

nearest(points, p, radius) → str | None

norm(a) → float

oblique_mids(p0, d0, p1, d1[, r]) → list[Point] | None

Corners of the oblique route the router draws between stub ends p0 (arriving along d0) and p1

ortho(anchor, p) → Point

p moved so the segment from anchor is horizontal or vertical, whichever is nearer (Shift).

ortho_between(p, neighbours) → Point

p (a node being moved or inserted) aligned with its neighbours (Shift): each neighbour segment is made

parse_grid(text) → float | None

A KLayout editor-grid config value ("0.5", "1,1", "") → grid step in µm, or None.

radius_from_handle(prev, p, nxt, q) → float | None

The bend radius whose arc middle is at q projected on the corner's bisector (inverse of

radius_handle(prev, p, nxt, r) → Point | None

Curvature handle of the corner at p bent to radius r: the middle of the arc, on the bisector.

rounded_points(points, radii[, segments_per_90]) → list[Point]

Sharp polyline → polyline with every interior corner i replaced by an arc of radius radii[i] (index

route_near(routes, p, radius[, exclude]) → tuple[str, int] | None

The sidecar route whose polyline passes within radius µm of p: (name, segment) or None.

route_preview(start, start_dir, waypoints, end, ...) → list[Point]

Sharp polyline of a route from a port (start, outward start_dir) through waypoints to end.

same_line(pa, pb[, tol]) → bool

Ports of the same CPW cross-section (a, b), whichever way they face.

segment_near(polyline, p, radius) → int | None

1-based index of the polyline segment within radius of p, or None.

snap45(anchor, p[, grid]) → Point

p moved onto the nearest of the 8 directions from anchor (0°, 45°, 90°, …; Shift while drawing).

snap_candidate(moving, others, delta, radius) → tuple[str, str, Point] | None

Best (moving_port, target_port, corrected_delta) when moving ports are translated by delta.

snap_candidate_rot(moving, others, delta, radius[, ...]) → tuple[str, str, Point, float] | None

Best (moving_port, target_port, corrected_delta, extra_turn) while dragging, rotation included.

snap_grid(p, grid) → Point

snap_rays(p, q, rays, tol[, grid]) → Point

q (the point an axis / 45° snap chose for p), or p put on the nearest of rays ((origin,

sub(a, b) → Point

tight_segments(points, radii) → list[int]

Segments (1-based) too short for the bends at both their ends (the rebuild would fail there).

turn_angle(prev, p, nxt) → float

unit(v) → Point | None

waypoint_insert_index(waypoint_pidx, segment) → int

Index in route.nodes for a new waypoint on polyline segment (1-based).

Module Contents

qcc_kplugin.geometry.add(a: Point, b: Point) → Point
qcc_kplugin.geometry.compatible(pa: dict, pb: dict, tol: float = 0.001) → bool

Ports that can be snapped together: same CPW (a, b) and facing each other.

qcc_kplugin.geometry.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).

qcc_kplugin.geometry.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).

qcc_kplugin.geometry.dist(a: Point, b: Point) → float
qcc_kplugin.geometry.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).

qcc_kplugin.geometry.dist_point_segment(p: Point, a: Point, b: Point) → float
qcc_kplugin.geometry.dot(a: Point, b: Point) → float
qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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).

qcc_kplugin.geometry.is_axis(d: Point | None) → bool

True for a horizontal / vertical direction (a port that faces along an axis).

qcc_kplugin.geometry.mul(a: Point, s: float) → Point
qcc_kplugin.geometry.nearest(points: dict[str, Point], p: Point, radius: float) → str | None
qcc_kplugin.geometry.norm(a: Point) → float
qcc_kplugin.geometry.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.

qcc_kplugin.geometry.ortho(anchor: Point, p: Point) → Point

p moved so the segment from anchor is horizontal or vertical, whichever is nearer (Shift).

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.parse_grid(text) → float | None

A KLayout editor-grid config value ("0.5", "1,1", "") → grid step in µm, or None.

qcc_kplugin.geometry.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 radius_handle()); None on a straight node.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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).

qcc_kplugin.geometry.same_line(pa: dict, pb: dict, tol: float = 0.001) → bool

Ports of the same CPW cross-section (a, b), whichever way they face.

qcc_kplugin.geometry.segment_near(polyline: list[Point], p: Point, radius: float) → int | None

1-based index of the polyline segment within radius of p, or None.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.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.

qcc_kplugin.geometry.snap_grid(p: Point, grid: float) → Point
qcc_kplugin.geometry.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.

qcc_kplugin.geometry.sub(a: Point, b: Point) → Point
qcc_kplugin.geometry.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).

qcc_kplugin.geometry.turn_angle(prev: Point, p: Point, nxt: Point) → float
qcc_kplugin.geometry.unit(v: Point) → Point | None
qcc_kplugin.geometry.waypoint_insert_index(waypoint_pidx: list[int], segment: int) → int

Index in route.nodes for a new waypoint on polyline segment (1-based).

qcc_kplugin.geometry.ALIGN_TOL
qcc_kplugin.geometry.EPS = 1e-06
qcc_kplugin.geometry.FACE_TOL_DEG = 2.5
qcc_kplugin.geometry.Point