From 772815b4c0c3328fa4bd4c4897a0938de77e4df9 Mon Sep 17 00:00:00 2001 From: Dahak Builds Date: Sat, 31 Oct 2026 00:45:22 +0000 Subject: [PATCH] Sketch Extend: only an end on a circle counts as held (#9731) (#9773) * claim: #9731 * Sketch Extend: only an end on a circle counts as held (#9731) grow_line's on-circle test was true for every point inside the circle; test distance to the circle instead. Also pin the new end point of a held-end extension onto the curve it stops on, as the free-end path does. * Fix regression test harness usage and held-end setup (#9731) --------- Co-authored-by: Dahak Builds --- crates/engine/src/cmd/sketch_modify.rs | 24 +++++--- crates/engine/src/cmd/sketch_modify_tests.rs | 65 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index 29b4ac42c..d67dd1aa8 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -947,11 +947,19 @@ fn stop_point(sk: &Sketch, ci: usize, q: Vec2) -> Option { } /// Add the line an extension needs, from the growing end to the stop: the sketch's own point -/// when the stop already is one, a new point otherwise. -fn add_line_extension(sk: &mut Sketch, end: usize, q: Vec2, stop: Option) -> Result { +/// when the stop already is one, a new point otherwise. A new point stopped in the middle of a +/// curve is pinned onto it, as the free-end path pins its end, so the extension is attached to +/// the curve it stops on instead of lying loose over it. +fn add_line_extension(sk: &mut Sketch, end: usize, q: Vec2, stop: Option, cutter: Option) -> Result { let b = match stop { Some(p) => p, - None => sk.add_point(q, None)?, + None => { + let p = sk.add_point(q, None)?; + if let Some(cutter) = cutter { + pin(sk, p, cutter)?; + } + p + } }; sk.add_curve(CurveKind::Line { a: end, b }, None)?; Ok(sk.points.get(b).map(|x| x.id.clone()).unwrap_or_default()) @@ -1076,7 +1084,7 @@ fn grow_line(sk: &mut Sketch, ci: usize, aim: LineAim) -> Result { // The end joined to other geometry stays where it is: add a line from it. if !neighbors.is_empty() { - return add_line_extension(sk, end, q, stop); + return add_line_extension(sk, end, q, stop, cutter); } // An end held by a constraint, or lying on a circle or arc, stays where it is too: the @@ -1096,7 +1104,9 @@ fn grow_line(sk: &mut Sketch, ci: usize, aim: LineAim) -> Result { // Geometric check: does the endpoint lie on any circle or arc? let ep = sk.point(end).unwrap_or_default(); sk.curves.iter().any(|curve| match curve.kind { - CurveKind::Circle { c, r } => (ep - sk.point(c).unwrap_or_default()).len().abs() - r < 1e-6, + // On the circle means its distance to the centre equals the radius: a point merely + // *inside* the circle is not held by it. + CurveKind::Circle { c, r } => ((ep - sk.point(c).unwrap_or_default()).len() - r).abs() < TOL, // This is an arc's own start or end: it sits on that arc. CurveKind::Arc { a, b, .. } => ep.dist(sk.point(a).unwrap_or_default()) < 1e-6 || ep.dist(sk.point(b).unwrap_or_default()) < 1e-6, _ => false, @@ -1104,7 +1114,7 @@ fn grow_line(sk: &mut Sketch, ci: usize, aim: LineAim) -> Result { }; if end_is_held { - return add_line_extension(sk, end, q, stop); + return add_line_extension(sk, end, q, stop, cutter); } // A free end moves along instead, if the solver lets it. The attempt is made on a copy, @@ -1134,7 +1144,7 @@ fn grow_line(sk: &mut Sketch, ci: usize, aim: LineAim) -> Result { return Ok(sk.points.get(end).map(|x| x.id.clone()).unwrap_or_default()); } // The solve could not put the end where it belongs: add a new line instead. - add_line_extension(sk, end, q, stop) + add_line_extension(sk, end, q, stop, cutter) } /// The two-curve form of `sketch.extend`: `curves` names exactly two curves, `at` optionally one diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index 20ef7081e..072456841 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -578,6 +578,71 @@ fn exact_extend_takes_one_of_two_stops_at_the_same_distance() { assert!(end.dist(Vec2::new(10.0, 0.0)) < 1e-6, "{end:?}"); } +#[test] +fn extend_does_not_treat_a_line_end_inside_a_circle_as_lying_on_it() { + // #9731: the "lies on a circle" test was true for every end *inside* a circle, so a large + // construction circle around the drawing made Extend treat a free end as held and lay a + // loose new line instead of lengthening the line. Only an end on the circle is held. + let mut s = new_sketch(); + let l1 = ids(&run(&mut s, "sketch.line", json!({"points":[[0,5],[10,5]],"connect":false,"infer":false}))["curves"])[0].clone(); + let l2 = ids(&run(&mut s, "sketch.line", json!({"points":[[20,-10],[20,20]],"connect":false,"infer":false}))["curves"])[0].clone(); + run(&mut s, "sketch.circle.center", json!({"center":[5,5],"radius":60})); + run(&mut s, "sketch.extend", json!({"curve": l1, "at": [9,5]})); + let sk = sketch(&s); + assert_eq!(sk.curves.len(), 3, "the circle around the drawing must not add a line"); + let end = sk.resolve_point(&format!("{l1}.end")).expect("l1's end"); + assert!(sk.point(end).is_some_and(|p| p.dist(Vec2::new(20.0, 5.0)) < 1e-6), "l1 reaches l2: {:?}", sk.point(end)); + let l2i = sk.curve_index(&l2).expect("l2"); + assert!( + sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::PointOnCurve { p, c } if p == end && c == l2i)), + "the extended end is held on l2" + ); + + // The same end deep inside a far larger circle behaves the same way. + let mut s = new_sketch(); + let l1 = ids(&run(&mut s, "sketch.line", json!({"points":[[0,5],[10,5]],"connect":false,"infer":false}))["curves"])[0].clone(); + run(&mut s, "sketch.line", json!({"points":[[20,-10],[20,20]],"connect":false,"infer":false})); + run(&mut s, "sketch.circle.center", json!({"center":[100,100],"radius":200})); + run(&mut s, "sketch.extend", json!({"curve": l1, "at": [9,5]})); + let sk = sketch(&s); + assert_eq!(sk.curves.len(), 3, "an end inside a circle is not held by it"); + let end = sk.resolve_point(&format!("{l1}.end")).expect("l1's end"); + assert!(sk.point(end).is_some_and(|p| p.dist(Vec2::new(20.0, 5.0)) < 1e-6), "{:?}", sk.point(end)); + + // An end that lies ON the circle is still held: Extend adds a new line from it. + let mut s = new_sketch(); + let l1 = ids(&run(&mut s, "sketch.line", json!({"points":[[0,5],[10,5]],"connect":false,"infer":false}))["curves"])[0].clone(); + run(&mut s, "sketch.line", json!({"points":[[20,-10],[20,20]],"connect":false,"infer":false})); + run(&mut s, "sketch.circle.center", json!({"center":[5,5],"radius":5})); + run(&mut s, "sketch.extend", json!({"curve": l1, "at": [9,5]})); + let sk = sketch(&s); + assert_eq!(sk.curves.len(), 4, "an end on the circle stays put and gains a new line"); + let end = sk.resolve_point(&format!("{l1}.end")).expect("l1's end"); + assert!(sk.point(end).is_some_and(|p| p.dist(Vec2::new(10.0, 5.0)) < 1e-6), "l1 stays put: {:?}", sk.point(end)); +} + +#[test] +fn extend_from_a_held_end_is_pinned_to_the_curve_it_stops_on() { + // The end being extended carries geometry (another line meets it there), so the extension is + // a new line. It stops in the middle of a third curve: the new end point is held on that + // curve, as the free-end path holds its end, instead of lying loose over it. + let mut s = new_sketch(); + let both = ids(&run(&mut s, "sketch.line", json!({"points":[[0,0],[10,0],[10,10]],"connect":false,"infer":false}))["curves"]); + let l1 = both[0].clone(); + let wall = ids(&run(&mut s, "sketch.line", json!({"points":[[20,-10],[20,10]],"connect":false,"infer":false}))["curves"])[0].clone(); + run(&mut s, "sketch.extend", json!({"curve": l1, "at": [10,0]})); + let sk = sketch(&s); + assert_eq!(sk.curves.len(), 4, "the held end stays and a new line is added"); + let new_line = sk.curves.last().expect("the new line"); + let CurveKind::Line { b, .. } = new_line.kind else { panic!("the new curve is a line: {new_line:?}") }; + assert!(sk.point(b).is_some_and(|p| p.dist(Vec2::new(20.0, 0.0)) < 1e-6), "the new line stops on the wall: {:?}", sk.point(b)); + let walli = sk.curve_index(&wall).expect("the wall"); + assert!( + sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::PointOnCurve { p, c } if p == b && c == walli)), + "the new end point is held on the curve it stops on" + ); +} + #[test] fn fillet_keeps_expression_corner_when_parameter_changes() { let mut s = new_sketch();