main
John Lauer
Publish input-bound R11 prototype bundle and completed CLI clearance review
7007e93
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
--- a/src/collide.rs
+++ b/src/collide.rs
@@ -13,6 +13,24 @@
use crate::num::round;
use crate::stack::Bx;
+
+/// Exact XY distance from a segment to an axis-aligned obstacle footprint.
+/// A large cooler must be selected by its near edge, not its remote center.
+fn segment_box_distance(a: [f64;2], b: [f64;2], bx: &Bx) -> f64 {
+ let d=[b[0]-a[0],b[1]-a[1]];let mut enter:f64=0.0;let mut exit:f64=1.0;
+ let mut intersects=true;
+ for i in 0..2 {
+ if d[i].abs()<1e-12 {if a[i]<bx.min[i] || a[i]>bx.max[i] {intersects=false;}}
+ else {let t0=(bx.min[i]-a[i])/d[i];let t1=(bx.max[i]-a[i])/d[i];enter=enter.max(t0.min(t1));exit=exit.min(t0.max(t1));}
+ }
+ if intersects && enter<=exit {return 0.0;}
+ let point_box=|p:[f64;2]| ((bx.min[0]-p[0]).max(0.0).max(p[0]-bx.max[0])).hypot((bx.min[1]-p[1]).max(0.0).max(p[1]-bx.max[1]));
+ let mut best=point_box(a).min(point_box(b));let l2=d[0]*d[0]+d[1]*d[1];
+ for x in [bx.min[0],bx.max[0]] {for y in [bx.min[1],bx.max[1]] {
+ let u=if l2>0.0 {(((x-a[0])*d[0]+(y-a[1])*d[1])/l2).clamp(0.0,1.0)} else {0.0};
+ best=best.min((x-a[0]-u*d[0]).hypot(y-a[1]-u*d[1]));
+ }}best
+}
pub struct Body { pub name: String, pub anchor: &'static str, pub pts: Vec<[f64; 3]>, pub hull: Bx }
pub struct Obstacle { pub name: String, pub kind: &'static str, pub bx: Bx }
@@ -342,13 +360,35 @@
inp.length = wc["probe"]["ground"]["tether_mm"].as_f64().unwrap_or(g.tether_mm);
// the tallest parts nearest the line from the pin to the tip, as many as the solver takes
let (ax, ay, bx_, by_) = (gx, gy, tip.0, tip.1);
- let seg_d = |x: f64, y: f64| { let (dx, dy) = (bx_ - ax, by_ - ay); let l2 = dx * dx + dy * dy; let u = if l2 > 0.0 { (((x - ax) * dx + (y - ay) * dy) / l2).clamp(0.0, 1.0) } else { 0.0 }; (x - ax - u * dx).hypot(y - ay - u * dy) };
let mut near: Vec<&&Obstacle> = parts.iter().filter(|o| o.bx.max[2] > surf + 0.6).collect();
- near.sort_by(|a, b| seg_d((a.bx.min[0] + a.bx.max[0]) / 2.0, (a.bx.min[1] + a.bx.max[1]) / 2.0).total_cmp(&seg_d((b.bx.min[0] + b.bx.max[0]) / 2.0, (b.bx.min[1] + b.bx.max[1]) / 2.0)));
+ near.sort_by(|a,b| segment_box_distance([ax,ay],[bx_,by_],&a.bx).total_cmp(&segment_box_distance([ax,ay],[bx_,by_],&b.bx)).then_with(|| b.bx.max[2].total_cmp(&a.bx.max[2])));
for o in near.iter().take(16usize.saturating_sub(inp.boxes.len())) {
inp.boxes.push([o.bx.min[0] - tip.0, o.bx.min[1] - tip.1, o.bx.min[2] - tip.2, o.bx.max[0] - tip.0, o.bx.max[1] - tip.1, o.bx.max[2] - tip.2]);
}
- let out = tether::solve(&inp, None).0;
+ let base_boxes=inp.boxes[..tether::v2::boxes().len()].to_vec();
+ let mut out = tether::solve(&inp, None).0;
+ // A slack wire bows away from the endpoint segment. Add every encountered
+ // obstacle to the active set, then solve again; acceptance still checks all parts.
+ let mut active: Vec<usize>=Vec::new();
+ for _ in 0..4 {
+ let mut found=false;
+ for (idx,o) in parts.iter().enumerate() {
+ if out.points.iter().skip(2).any(|q| {
+ let ds=(0..3).map(|i| {let v=[q.x,q.y,q.z][i];(v-v.clamp(o.bx.min[i],o.bx.max[i])).powi(2)}).sum::<f64>().sqrt()-wr;
+ ds < -0.1
+ }) && !active.contains(&idx) {active.push(idx);found=true;}
+ }
+ if !found {break;}
+ inp.boxes=base_boxes.clone();
+ let capacity=16usize.saturating_sub(inp.boxes.len());
+ let mut chosen:Vec<&Obstacle>=active.iter().map(|&i| parts[i]).take(capacity).collect();
+ for o in &near {
+ if chosen.len()>=capacity {break;}
+ if !chosen.iter().any(|v| std::ptr::eq(*v,**o)) {chosen.push(**o);}
+ }
+ for o in chosen {inp.boxes.push([o.bx.min[0]-tip.0,o.bx.min[1]-tip.1,o.bx.min[2]-tip.2,o.bx.max[0]-tip.0,o.bx.max[1]-tip.1,o.bx.max[2]-tip.2]);}
+ out=tether::solve(&inp,None).0;
+ }
solves += 1;
worst_clear = worst_clear.min(out.min_clearance);
let (test, text) = replay.event_at(t);
@@ -357,7 +397,7 @@
let c: Vec<f64> = (0..3).map(|i| [q.x, q.y, q.z][i].min(o.bx.max[i]).max(o.bx.min[i])).collect();
let dist = ((q.x - c[0]).powi(2) + (q.y - c[1]).powi(2) + (q.z - c[2]).powi(2)).sqrt() - wr;
if dist < -0.1 && snags.iter().all(|s| s["part"] != json!(o.name) || s["test"] != json!(test)) {
- snags.push(json!({"t": round(t, 2), "part": o.name, "depth_mm": round(-dist, 2), "node": k, "test": test, "during": text,
+ snags.push(json!({"t": round(t, 2), "part": o.name, "depth_mm": round(-dist, 2), "node": k, "position_mm": [q.x,q.y,q.z], "obstacle_bounds_mm": {"min":o.bx.min,"max":o.bx.max}, "test": test, "during": text,
"fix": format!("the ground wire runs through {}: move the probe's approach, the ground contact, or {} (board flow)", o.name, o.name)}));
} else if dist < 0.15 {
let e = rests.entry(o.name.clone()).or_insert((t, t)); e.1 = t;
@@ -471,3 +511,19 @@
assert!(validate_slab_outline(&posed[0],&json!({}),(240.0,464.0,0.135,90.0)).is_err());
}
}
+
+#[cfg(test)] mod tether_selection_tests {
+ use super::*;
+ #[test] fn large_obstacle_edge_beats_small_remote_center() {
+ let sink=Bx{min:[-20.0,1.0,0.0],max:[100.0,40.0,40.0]};
+ let pad=Bx{min:[4.0,3.0,0.0],max:[6.0,4.0,1.0]};
+ assert_eq!(segment_box_distance([0.0,0.0],[10.0,0.0],&sink),1.0);
+ assert!(segment_box_distance([0.0,0.0],[10.0,0.0],&sink)<segment_box_distance([0.0,0.0],[10.0,0.0],&pad));
+ }
+ #[test] fn crossing_inside_degenerate_and_corner_segments() {
+ let b=Bx{min:[0.0,0.0,0.0],max:[2.0,2.0,3.0]};
+ assert_eq!(segment_box_distance([-1.0,1.0],[3.0,1.0],&b),0.0);
+ assert_eq!(segment_box_distance([1.0,1.0],[1.0,1.0],&b),0.0);
+ assert_eq!(segment_box_distance([-2.0,-2.0],[-1.0,-1.0],&b),2.0_f64.sqrt());
+ }
+}