From 2e433fcd0d1afadd8ab786dd30d7d4546bdee43d Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 11 Nov 2026 10:08:41 +0100 Subject: hw-toolchain: placer, routers and KiCad helpers for agent-driven hardware work Rust placer (simulated annealing) and negotiated-congestion router for 2-layer KiCad boards, pnr.sh to place, route, DRC-check and install candidates, Python KiCad helpers, repo conventions (AGENTS.md) and the tool documentation under knowledge/. Konnect (KiCad MCP, AGPL) as a submodule. Co-Authored-By: Claude Opus 5.5 --- tools/placer/src/geom.rs | 174 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tools/placer/src/geom.rs (limited to 'tools/placer/src/geom.rs') diff --git a/tools/placer/src/geom.rs b/tools/placer/src/geom.rs new file mode 100644 index 0000000..3514340 --- /dev/null +++ b/tools/placer/src/geom.rs @@ -0,0 +1,174 @@ +//! Axis-aligned rectangles, 90 degree turns, segment crossings and the MST. + +pub type Pt = (f64, f64); + +#[derive(Clone, Copy, Debug, Default)] +pub struct Rect { + pub x0: f64, + pub y0: f64, + pub x1: f64, + pub y1: f64, +} + +impl Rect { + pub fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Rect { + Rect { x0: x0.min(x1), y0: y0.min(y1), x1: x0.max(x1), y1: y0.max(y1) } + } + pub fn around(c: Pt, hw: f64, hh: f64) -> Rect { + Rect::new(c.0 - hw, c.1 - hh, c.0 + hw, c.1 + hh) + } + pub fn area(&self) -> f64 { + (self.x1 - self.x0) * (self.y1 - self.y0) + } + /// Overlap area with `o`. + pub fn inter(&self, o: &Rect) -> f64 { + let w = self.x1.min(o.x1) - self.x0.max(o.x0); + let h = self.y1.min(o.y1) - self.y0.max(o.y0); + if w > 0.0 && h > 0.0 { + w * h + } else { + 0.0 + } + } + pub fn hits(&self, o: &Rect) -> bool { + self.x0 < o.x1 && o.x0 < self.x1 && self.y0 < o.y1 && o.y0 < self.y1 + } + /// Area of `self` that lies outside `b`. + pub fn outside(&self, b: &Rect) -> f64 { + self.area() - self.inter(b) + } + pub fn grow(&self, d: f64) -> Rect { + Rect { x0: self.x0 - d, y0: self.y0 - d, x1: self.x1 + d, y1: self.y1 + d } + } + pub fn union(&self, o: &Rect) -> Rect { + Rect { x0: self.x0.min(o.x0), y0: self.y0.min(o.y0), x1: self.x1.max(o.x1), y1: self.y1.max(o.y1) } + } + /// The rectangle of a footprint-local rectangle on a part at `at`, turned by `k` x 90 degrees. + pub fn place(&self, at: Pt, k: u8) -> Rect { + let a = turn((self.x0, self.y0), k); + let b = turn((self.x1, self.y1), k); + Rect::new(a.0 + at.0, a.1 + at.1, b.0 + at.0, b.1 + at.1) + } +} + +/// Turn a footprint-local vector by k x 90 degrees as KiCad does: positive angles turn +/// counter-clockwise on screen, where y points down. +pub fn turn(p: Pt, k: u8) -> Pt { + match k & 3 { + 0 => p, + 1 => (p.1, -p.0), + 2 => (-p.0, -p.1), + _ => (-p.1, p.0), + } +} + +pub fn dist(a: Pt, b: Pt) -> f64 { + ((a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)).sqrt() +} + +/// Proper crossing of segments ab and cd; touching or shared ends do not count. +pub fn crosses(a: Pt, b: Pt, c: Pt, d: Pt) -> bool { + fn orient(p: Pt, q: Pt, r: Pt) -> f64 { + (q.0 - p.0) * (r.1 - p.1) - (q.1 - p.1) * (r.0 - p.0) + } + let (o1, o2) = (orient(a, b, c), orient(a, b, d)); + if o1 * o2 >= 0.0 { + return false; + } + let (o3, o4) = (orient(c, d, a), orient(c, d, b)); + o3 * o4 < 0.0 +} + +/// Segment ab passes through rectangle r (Liang-Barsky clipping). +pub fn seg_hits(a: Pt, b: Pt, r: &Rect) -> bool { + let (dx, dy) = (b.0 - a.0, b.1 - a.1); + let (mut t0, mut t1) = (0.0f64, 1.0f64); + for (p, q) in [(-dx, a.0 - r.x0), (dx, r.x1 - a.0), (-dy, a.1 - r.y0), (dy, r.y1 - a.1)] { + if p.abs() < 1e-12 { + if q < 0.0 { + return false; + } + } else { + let t = q / p; + if p < 0.0 { + t0 = t0.max(t); + } else { + t1 = t1.min(t); + } + if t0 > t1 { + return false; + } + } + } + true +} + +/// Prim's MST over the points: (i, j) pairs. +pub fn mst(pts: &[Pt]) -> Vec<(usize, usize)> { + let n = pts.len(); + let mut out = Vec::with_capacity(n.saturating_sub(1)); + if n < 2 { + return out; + } + let mut inside = vec![false; n]; + let mut best = vec![f64::INFINITY; n]; + let mut link = vec![usize::MAX; n]; + best[0] = 0.0; + for _ in 0..n { + let mut i = usize::MAX; + for k in 0..n { + if !inside[k] && (i == usize::MAX || best[k] < best[i]) { + i = k; + } + } + inside[i] = true; + if link[i] != usize::MAX { + out.push((link[i], i)); + } + for k in 0..n { + if !inside[k] { + let d = dist(pts[i], pts[k]); + if d < best[k] { + best[k] = d; + link[k] = i; + } + } + } + } + out +} + +/// Cover a polygon with rectangles: one horizontal slab per pair of vertex heights, each +/// cut into the x intervals that lie inside. Exact for rectilinear outlines, a slight +/// overestimate for slanted edges and arcs (each slab takes the wider of its two ends). +pub fn poly_rects(poly: &[[f64; 2]]) -> Vec { + let mut ys: Vec = poly.iter().map(|p| p[1]).collect(); + ys.sort_by(|a, b| a.partial_cmp(b).unwrap()); + ys.dedup_by(|a, b| (*a - *b).abs() < 1e-4); + let xs_at = |y: f64| { + let mut xs = Vec::new(); + for i in 0..poly.len() { + let (a, b) = (poly[i], poly[(i + 1) % poly.len()]); + if (a[1] <= y && b[1] > y) || (b[1] <= y && a[1] > y) { + xs.push(a[0] + (y - a[1]) / (b[1] - a[1]) * (b[0] - a[0])); + } + } + xs.sort_by(|a, b| a.partial_cmp(b).unwrap()); + xs + }; + let mut out = Vec::new(); + for w in ys.windows(2) { + let (y0, y1) = (w[0], w[1]); + let eps = (y1 - y0) * 1e-3; + let (lo, hi) = (xs_at(y0 + eps), xs_at(y1 - eps)); + if lo.len() != hi.len() { + continue; + } + for k in (0..lo.len()).step_by(2) { + if k + 1 < lo.len() { + out.push(Rect::new(lo[k].min(hi[k]), y0, lo[k + 1].max(hi[k + 1]), y1)); + } + } + } + out +} -- cgit v1.3