diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rs index ee1492a3e..af1653bf0 100644 --- a/crates/doc/src/sheet.rs +++ b/crates/doc/src/sheet.rs @@ -2955,22 +2955,22 @@ impl SheetBody { Err(why) => notes.push(format!("no corner relief at {}: {why}", self.shown(k.c))), } } - // The reliefs at the two ends of one edge must not overlap: the one planned later - // is left out, with a note. + // Two reliefs must not overlap. Two corners that share an edge never come here: + // each relief reaches exactly `r` along that edge, and `plan_relief` refuses + // unless `2r` fits in it. Two corners that do not share one can: the square + // relief at a corner of 120° reaches `2r` from it, along the bisector. Two that + // would overlap are left out together, each with a note naming the other, so + // that which of them is cut does not depend on the order the corners come in. let mut kept: Vec = Vec::with_capacity(planned.len()); - for p in planned { - let mut clash = None; - for q in &kept { - if convex_overlap(&q.cut, &p.cut) > 1e-9 { - clash = Some(q.corner.c); - break; - } - } + for (i, p) in planned.iter().enumerate() { + let clash = planned.iter().enumerate().find(|(j, q)| *j != i && convex_overlap(&q.cut, &p.cut) > 1e-9); match clash { - Some(at) => { - notes.push(format!("no corner relief at {}: it would overlap the relief at {}", self.shown(p.corner.c), self.shown(at))) - } - None => kept.push(p), + Some((_, q)) => notes.push(format!( + "no corner relief at {}: it would overlap the relief at {}", + self.shown(p.corner.c), + self.shown(q.corner.c) + )), + None => kept.push(p.clone()), } } for p in &kept { @@ -5593,6 +5593,54 @@ mod tests { c + bisector * (s0 / (alpha / 2.0).sin()) } + /// Two corner reliefs that would overlap are both left out. The two corners do not share an + /// edge: they are the 120° ones opposite each other in a rhombus, and the square relief at + /// each reaches 2r from it along the bisector, so the two reach each other. + #[test] + fn two_reliefs_that_would_overlap_are_both_left_out() { + // A rhombus of 120° and 60° corners, with a flange on each of its four edges. The two + // 120° corners are 12 mm apart in the trimmed base; a square relief 8 mm across reaches + // 4 mm along the edge it starts on and 8 mm along the bisector, so the two meet. + let l = 12.0 + 4.0 * 3.0f64.sqrt(); + let u = Vec2::from_angle(std::f64::consts::FRAC_PI_3); + let pts = [Vec2::new(0.0, 0.0), Vec2::new(l / 2.0, l * u.y), Vec2::new(l, 0.0), Vec2::new(l / 2.0, -l * u.y)]; + let base = || Region2 { outer: Loop2::polygon(&pts).ccw(), holes: vec![] }; + let picks = [ + Vec3::new(l / 4.0, l * u.y / 2.0, 2.0), + Vec3::new(l * 0.75, l * u.y / 2.0, 2.0), + Vec3::new(l * 0.75, -l * u.y / 2.0, 2.0), + Vec3::new(l / 4.0, -l * u.y / 2.0, 2.0), + ]; + let flange = |i: usize| EdgeFlange { pick: picks[i], height: 38.6, angle: 90f64.to_radians() }; + // The round relief reaches r from its corner and the two are 12 mm apart: it fits at both. + for shape in [ReliefShape::Round, ReliefShape::Rectangular] { + let what = format!("{shape:?} at two corners opposite each other"); + let rv = RuleValues { corner_relief_shape: shape, corner_relief: 8.0, ..alu() }; + // The same four flanges, made in another order: the corners that keep their relief + // must not depend on it. + for order in [[0, 1, 2, 3], [3, 2, 1, 0]] { + let each: Vec = order.iter().map(|&i| flange(i)).collect(); + let (s, notes) = flanges_each(base(), &rv, &each); + if shape == ReliefShape::Round { + assert!(notes.is_empty(), "{what}: {order:?}: {notes:?}"); + // A round at each of the four corners. + assert_eq!(s.base.outer.segs.len(), 8, "{what}: {order:?}: {:?}", s.base.outer.segs); + continue; + } + // The two reliefs that would overlap are both left out, each with a note naming + // the other. + assert_eq!(notes.len(), 2, "{what}: {order:?}: {notes:?}"); + for n in ¬es { + assert!(n.starts_with("no corner relief at (") && n.contains("it would overlap the relief at ("), "{what}: {notes:?}"); + } + // The two kites at the 60° corners are cut, the ones at the 120° corners are + // not: the base keeps four of its edges and gains the two kites. + assert_eq!(s.base.outer.segs.len(), 8, "{what}: {order:?}: {:?}", s.base.outer.segs); + holds_the_flat_material(&s, &what); + } + } + } + /// The corner reliefs at the corners that are not square: a 60° and a 120° one, each of the /// four shapes. The relief is cut `size / 2` from the corner along both edges, the bends stop /// there, and the flat pattern keeps one clean loop. diff --git a/docs/book/src/guide/sheet-metal.md b/docs/book/src/guide/sheet-metal.md index 60374d983..813927f0b 100644 --- a/docs/book/src/guide/sheet-metal.md +++ b/docs/book/src/guide/sheet-metal.md @@ -57,9 +57,9 @@ square the round follows the angle of the base and the square becomes a kite, it square to the edges. Changing the shape in a rule changes the parts made with that rule. A relief that doesn't fit (under 0.01 mm across, leaving nothing of a bend, or reaching as far from its corner as another edge of the base is) is left out, and the flange feature reports a -warning saying where. The reliefs at the corners of one edge are cut together, so a corner's -relief does not depend on the order the corners are cut in; two of them that would overlap -leave the second one out, with a warning. +warning saying where. The reliefs at all the corners are cut together, so a corner's relief +does not depend on the order the corners are cut in; two of them that would overlap are both +left out, each with a warning saying which the other is. ```json {"command":"sheet.manage_rules","params":{"name":"Sheet 2 mm","thickness":"2 mm","k_factor":0.5,