diff --git a/crates/sketch/src/link_side_tests.rs b/crates/sketch/src/link_side_tests.rs index bbb15a108..428e381eb 100644 --- a/crates/sketch/src/link_side_tests.rs +++ b/crates/sketch/src/link_side_tests.rs @@ -172,3 +172,58 @@ fn a_line_tangent_to_a_linked_circle_keeps_its_side_when_the_circle_moves_far() let mut sk = stored.clone(); assert!(solve_after_link_move(&mut sk, &stored, &stored.driving_values(), Analysis::Full).is_none()); } + +/// A point held on a linked rail 3 mm from another point on it (a distance between the two): +/// the same distance back past the other point solves every constraint too, and only the side +/// it was drawn on tells them apart. The other point is held on the rail itself, or lies on it +/// as the mirror image of a point on it across a line square to it. The mirror image counts as +/// a flip, and the rail walked far (steeper, higher, the other way up) keeps the point on its side. +#[test] +fn a_point_a_distance_along_a_linked_line_keeps_its_side() { + let slant = |y0: f64, y1: f64| vec![LinkGeom::Line(Vec2::new(0.0, y0), Vec2::new(80.0, y1))]; + for mirrored_on in [false, true] { + let mut sk = Sketch::new(); + let link = sk.add_link(LinkKind::Project, source(), &slant(10.0, 50.0)).unwrap(); + let rail = sk.link_curves(&link)[0]; + let a = sk.add_point(Vec2::new(20.0, 20.0), None).unwrap(); + let h = sk.add_point(Vec2::new(23.0, 21.5), None).unwrap(); + if mirrored_on { + // `a` mirrors `b` (on the rail, x 0..) across a line square to the rail at x 10. + let b = sk.add_point(Vec2::new(0.5, 10.0), None).unwrap(); + let mh = sk.add_line(Vec2::new(10.0, 15.0), Vec2::new(5.5, 24.0), None, None, None).unwrap(); + let (m, _) = sk.curves[mh].kind.ends().unwrap(); + sk.add_constraint(ConstraintKind::PointOnCurve { p: b, c: rail }, None).unwrap(); + sk.add_constraint(ConstraintKind::PointOnCurve { p: m, c: rail }, None).unwrap(); + sk.add_constraint(ConstraintKind::Perpendicular { a: mh, b: rail }, None).unwrap(); + sk.add_constraint(ConstraintKind::Length { l: mh, value: 10.0 }, None).unwrap(); + sk.add_constraint(ConstraintKind::DistanceX { p: 0, q: m, value: 10.0 }, None).unwrap(); + sk.add_constraint(ConstraintKind::DistanceX { p: 0, q: b, value: 0.5 }, None).unwrap(); + sk.add_constraint(ConstraintKind::Symmetric { p: b, q: a, l: mh }, None).unwrap(); + } else { + sk.add_constraint(ConstraintKind::PointOnCurve { p: a, c: rail }, None).unwrap(); + sk.add_constraint(ConstraintKind::DistanceX { p: 0, q: a, value: 20.0 }, None).unwrap(); + } + sk.add_constraint(ConstraintKind::PointOnCurve { p: h, c: rail }, None).unwrap(); + let span = sk.add_constraint(ConstraintKind::Distance { p: a, q: h, value: 3.0 }, None).unwrap(); + assert!(solve(&mut sk).ok(), "{mirrored_on}"); + let ahead = |sk: &Sketch| match sk.curves[rail].kind { + CurveKind::Line { a: s, b: e } => (sk.points[e].pos - sk.points[s].pos).dot(sk.points[h].pos - sk.points[a].pos), + _ => f64::NAN, + }; + assert!(ahead(&sk) > 0.0 && (sk.points[h].pos.dist(sk.points[a].pos) - 3.0).abs() < 1e-9, "{mirrored_on}"); + let stored = sk.clone(); + // The same 3 mm back past `a`: solved as it is, and told apart only by the side. + let mut back = stored.clone(); + back.points[h].pos = back.points[a].pos * 2.0 - back.points[h].pos; + assert!(solve(&mut back).ok() && ahead(&back) < 0.0, "{mirrored_on}"); + assert!(flips_from_stored(&back, &stored).contains(&span), "{mirrored_on}: {:?}", flips_from_stored(&back, &stored)); + // Walked there, `h` stays ahead of `a` along the rail. + for (y0, y1) in [(300.0, 260.0), (-40.0, 400.0), (2000.0, 1990.0)] { + let mut sk = stored.clone(); + sk.update_link(&link, &slant(y0, y1)).unwrap(); + let report = solve_after_link_move(&mut sk, &stored, &stored.driving_values(), Analysis::Full); + assert!(report.is_some_and(|r| r.keeps_orientation()), "{mirrored_on} {y0} {y1}"); + assert!(ahead(&sk) > 0.0 && flips_from_stored(&sk, &stored).is_empty(), "{mirrored_on} {y0} {y1}: {}", ahead(&sk)); + } + } +} diff --git a/crates/sketch/src/solver.rs b/crates/sketch/src/solver.rs index c16ecaf85..df858ea42 100644 --- a/crates/sketch/src/solver.rs +++ b/crates/sketch/src/solver.rs @@ -1990,6 +1990,10 @@ enum Orient { OfLine(usize, usize, f64), /// Which of two nested circles or arcs encloses the other. RadiusOrder(usize, usize), + /// Which way along line `l` the point `q` is from `p`: a `Distance` between two points that + /// both lie on `l` (its ends, points on it, or points coincident with either; one of them may + /// lie on it by what else holds it). + Along(usize, usize, usize, f64), } /// An orientation's signed offset in a state (a corner's distance from the opposite side, an @@ -2023,6 +2027,11 @@ fn offset(s: &State, o: Orient) -> Option<(f64, f64, f64)> { // its size; rounding still accounts for the radii and the arc coordinates. (ra - rb, (ra - rb).abs(), ra.max(rb).max(coords(&[ca, cb]))) } + Orient::Along(p, q, l, size) => { + let (a, b) = s.line(l); + let (pp, pq) = (s.p(p), s.p(q)); + ((b - a).normalized()?.dot(pq - pp), size, coords(&[pp, pq, a, b])) + } }) } @@ -2154,13 +2163,17 @@ fn orients(s: &State, sub: &[(Block, Vec)]) -> Vec<(Orient, Vec)> let mut out = Vec::new(); let mut joins = Vec::new(); let mut edges = Vec::new(); + let mut spans: Vec<(usize, usize, f64, usize)> = Vec::new(); for (b, _) in sub { // A tangency at a point the two curves share (or a line's end held on the round) is a // block of its own; its round keeps its side of the line all the same. let (Block::User(ci, kind) | Block::TangentAt(ci, kind, _)) = b else { continue }; match **kind { ConstraintKind::Coincident { p, q } => joins.push((p, q)), - ConstraintKind::Distance { p, q, .. } => edges.push((p, q, *ci)), + ConstraintKind::Distance { p, q, value } => { + edges.push((p, q, *ci)); + spans.push((p, q, value, *ci)); + } ConstraintKind::Length { l, .. } => edges.extend(line(l).map(|(p, q)| (p, q, *ci))), ConstraintKind::DistanceX { p, q, value } if free(p) || free(q) => out.push((Orient::SideX(p, q, value), vec![*ci])), ConstraintKind::DistanceY { p, q, value } if free(p) || free(q) => out.push((Orient::SideY(p, q, value), vec![*ci])), @@ -2191,6 +2204,57 @@ fn orients(s: &State, sub: &[(Block, Vec)]) -> Vec<(Orient, Vec)> _ => {} } } + // A distance between two points on one line leaves which way along it open: the other + // solution is the same distance back past `p`. A point is on a line as one of its ends, as a + // point held on it, or coincident with either, read from the whole sketch (the line may be + // fixed, solved apart from these points). + let ends = |l: usize| line(l).map(|(a, b)| [a, b]).unwrap_or([usize::MAX; 2]); + let mut lines_at: BTreeMap> = BTreeMap::new(); + let mut together: Vec<(usize, usize)> = Vec::new(); + if !spans.is_empty() { + for (l, c) in s.sk.curves.iter().enumerate() { + if let CurveKind::Line { a, b } = c.kind { + lines_at.entry(a).or_default().push(l); + lines_at.entry(b).or_default().push(l); + } + } + for c in &s.sk.constraints { + match c.kind { + ConstraintKind::PointOnCurve { p, c } if line(c).is_some() => lines_at.entry(p).or_default().push(c), + ConstraintKind::Coincident { p, q } => together.push((p, q)), + _ => {} + } + } + } + let lines_of = |p: usize| -> Vec { + let joined = together.iter().filter_map(|&(x, y)| { + if x == p { + Some(y) + } else if y == p { + Some(x) + } else { + None + } + }); + std::iter::once(p).chain(joined).flat_map(|x| lines_at.get(&x).into_iter().flatten().copied()).collect() + }; + // One of the two may lie on the line by what else holds it (the mirror image of a point on + // it, across a line square to it): where it is now, as exactly as a solve puts it. + let lies_on = |x: usize, l: usize| { + let ((a, b), px) = (s.line(l), s.p(x)); + let reach = 1e-7 * crate::loci::mag(a).max(crate::loci::mag(b)).max(crate::loci::mag(px)).max(1.0); + (b - a).normalized().is_some_and(|d| d.cross(px - a).abs() <= reach) + }; + for &(p, q, value, ci) in &spans { + if !(free(p) || free(q)) { + continue; + } + let (lp, lq) = (lines_of(p), lines_of(q)); + let both = |l: &usize| (lp.contains(l) || lies_on(p, *l)) && (lq.contains(l) || lies_on(q, *l)); + if let Some(l) = lp.iter().chain(&lq).copied().find(|l| both(l) && !(ends(*l).contains(&p) && ends(*l).contains(&q))) { + out.push((Orient::Along(p, q, l, value), vec![ci])); + } + } // Union-find over the points these constraints name (not the whole sketch). let mut local: BTreeMap = BTreeMap::new(); for (p, q) in joins.iter().copied().chain(edges.iter().map(|&(p, q, _)| (p, q))) {