diff options
| author | Louis <louis@dnge.de> | 2026-11-11 10:32:51 +0100 |
|---|---|---|
| committer | Louis <louis@dnge.de> | 2026-11-11 10:32:51 +0100 |
| commit | a59e8e99243a895197a30d6cd6dd8d744dd606de (patch) | |
| tree | 8910cc7476ff1317932257dfc8497a0f9ffc29f7 /tools/router/src/placement.rs | |
| parent | 6f2bbe1f94e4321c329f92ec3420f6d32fd7b26d (diff) | |
| download | hw-toolchain-a59e8e99243a895197a30d6cd6dd8d744dd606de.tar.gz | |
The sequential Python/C router (pcbroute.py, pipeline.py, astar.c) is
gone. The Rust crate takes its place at tools/router/, next to the KiCad
I/O scripts it uses (dump_board.py, write_board.py, plot_routes.py).
knowledge/tooling/router.md is now the Rust router's reference; the
small Python venv for plots and 3D checks moves to tools/.venv
(tools/requirements.txt).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/router/src/placement.rs')
| -rw-r--r-- | tools/router/src/placement.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tools/router/src/placement.rs b/tools/router/src/placement.rs new file mode 100644 index 0000000..9409742 --- /dev/null +++ b/tools/router/src/placement.rs @@ -0,0 +1,99 @@ +//! Move footprints of a board.json to a placer result without KiCad. +//! +//! Placement files are ref -> [x, y, rotation in degrees], at the top level or under +//! "placement" (tools/placer). KiCad's y axis points down and its angles turn counter-clockwise +//! on screen, so a footprint-local point (u, v) lies at +//! (x + u cos a + v sin a, y - u sin a + v cos a) for orientation a; moving from pose +//! (x0, y0, a0) to (x1, y1, a1) is that rotation by a1 - a0 about the old origin, then the shift. +//! +//! Pads, the footprint's own copper and its rule areas (via `keepout_refs`) move with it. +//! `tracks` and `vias` stay where they are: the old copper belongs to the old placement. +//! Footprints on the bottom side are not handled. + +use crate::io::BoardJson; +use anyhow::{bail, Context, Result}; +use std::collections::BTreeMap; + +/// Reads a placement file. +pub fn load(path: &str) -> Result<BTreeMap<String, [f64; 3]>> { + let s = std::fs::read_to_string(path).with_context(|| path.to_string())?; + let v: serde_json::Value = serde_json::from_str(&s).with_context(|| path.to_string())?; + let v = v.get("placement").cloned().unwrap_or(v); + serde_json::from_value(v).with_context(|| format!("{path}: expected ref -> [x, y, rot]")) +} + +/// Rigid transform: rotate by an angle (KiCad sense) about (ox, oy), then shift to (nx, ny). +struct Pose { + ox: f64, + oy: f64, + nx: f64, + ny: f64, + c: f64, + s: f64, +} + +impl Pose { + fn new(from: (f64, f64, f64), to: [f64; 3]) -> Pose { + let a = (to[2] - from.2).rem_euclid(360.0); + // exact values at right angles, so the common rotations add no noise + let (s, c) = match a { + 0.0 => (0.0, 1.0), + 90.0 => (1.0, 0.0), + 180.0 => (0.0, -1.0), + 270.0 => (-1.0, 0.0), + _ => a.to_radians().sin_cos(), + }; + Pose { ox: from.0, oy: from.1, nx: to[0], ny: to[1], c, s } + } + + fn pt(&self, x: f64, y: f64) -> (f64, f64) { + let (u, v) = (x - self.ox, y - self.oy); + (self.nx + u * self.c + v * self.s, self.ny - u * self.s + v * self.c) + } + + fn poly(&self, p: &mut [[f64; 2]]) { + for q in p { + let (x, y) = self.pt(q[0], q[1]); + *q = [x, y]; + } + } +} + +/// Moves each listed footprint; errors on unknown references and on bottom-side parts. +pub fn apply(d: &mut BoardJson, placement: &BTreeMap<String, [f64; 3]>) -> Result<()> { + let mut poses = BTreeMap::new(); + for (r, to) in placement { + let f = d.fps.get_mut(r.as_str()).with_context(|| format!("{r} is not on the board"))?; + poses.insert(r.as_str(), Pose::new((f.x, f.y, f.rot), *to)); + (f.x, f.y, f.rot) = (to[0], to[1], to[2]); + } + for p in &mut d.pads { + let Some(pose) = poses.get(p.refdes.as_str()) else { continue }; + if !p.npth && p.f.is_none() && p.b.is_some() { + bail!("{} has a bottom-side pad; flipped footprints are not supported", p.refdes); + } + (p.x, p.y) = pose.pt(p.x, p.y); + for poly in [&mut p.f, &mut p.b].into_iter().flatten() { + pose.poly(poly); + } + } + for c in &mut d.fpcopper { + if let Some(pose) = poses.get(c.refdes.as_str()) { + pose.poly(&mut c.poly); + } + } + if d.keepout_refs.is_empty() { + if !d.keepouts.is_empty() { + eprintln!("warning: board.json has no keepout_refs (old dump): footprint keep-outs are not moved"); + } + } else if d.keepout_refs.len() != d.keepouts.len() { + bail!("keepout_refs and keepouts differ in length"); + } else { + for (k, r) in d.keepouts.iter_mut().zip(&d.keepout_refs) { + if let Some(pose) = poses.get(r.as_str()) { + pose.poly(k); + } + } + } + Ok(()) +} |
