fix: a dimension from linked geometry keeps its side when the source moves past it (#614)

A point dimensioned from linked geometry (projected, included or intersected) keeps its side when a parameter moves the link's source past it: the solve reads sides and turns from the stored state instead of from beside the moved line, both on recompute and when editing the sketch.

Co-authored-by: NOWN-DEV <257771410+NOWN-DEV@users.noreply.github.com>
This commit is contained in:
NOWN-DEV authored and GitHub committed 2026-10-13 05:15:15 -04:00
1 parent bf962a9989
commit 24974e6f37
13 files changed
+392 -27

No files matched your search

+7 -8
View File
@@ -94,11 +94,13 @@ impl Placement {
/// Place the points the constraints pin down; `given[i]` marks the points that are constants
/// for this solve. Branches are chosen to match the orientation of `reference` (positions
/// before an interactive move) where it covers a point, of the sketch's current positions
/// otherwise; a reference longer than the point list is ignored. `on_curve` holds the sketch's
/// point-on-curve pairs ([`crate::solver::points_on_curves`]); the decomposition works within
/// what is left of `budget`, and charges it.
pub(crate) fn seed(sk: &Sketch, given: &[bool], reference: Option<&[Vec2]>, on_curve: &[(usize, usize)], budget: &mut Budget) -> Seed {
/// before an interactive move, or the stored ones after linked geometry moved) where it covers
/// a point, of the sketch's current positions otherwise; a reference longer than the point list
/// is ignored. `edits`: the solve applies edits; a drag step solves against its reference and
/// moves only held points. `on_curve` holds the sketch's point-on-curve pairs
/// ([`crate::solver::points_on_curves`]); the decomposition works within what is left of
/// `budget`, and charges it.
pub(crate) fn seed(sk: &Sketch, given: &[bool], reference: Option<&[Vec2]>, edits: bool, on_curve: &[(usize, usize)], budget: &mut Budget) -> Seed {
let n = sk.points.len();
let reference = reference.filter(|r| r.len() <= n);
let before = |i: usize| match reference.and_then(|r| r.get(i)) {
@@ -139,9 +141,6 @@ pub(crate) fn seed(sk: &Sketch, given: &[bool], reference: Option<&[Vec2]>, on_c
if !switch::decompose_on() {
return out;
}
// A drag step solves against its reference and moves only held points; any other solve
// applies edits.
let edits = reference.is_none();
let mut clusters = Decomposition::new(&reading, &prop.anchors, edits, budget.left());
if !clusters.built() {
return out;
+4 -4
View File
@@ -693,7 +693,7 @@ fn hostile_lines_and_rounds_neither_panic_nor_change_the_report() {
let mut c = base();
case(&mut c.sk);
let sk = edited(&c);
let s = seed(&sk, &given(&sk), Some(&[v(f64::NAN, 0.0)]), &points_on_curves(&sk), &mut Budget::full());
let s = seed(&sk, &given(&sk), Some(&[v(f64::NAN, 0.0)]), false, &points_on_curves(&sk), &mut Budget::full());
assert!(s.pos.iter().flatten().all(|p| p.is_finite()), "case {i}: {s:?}");
assert!(s.radius.iter().flatten().all(|r| r.is_finite() && *r > 0.0), "case {i}: {s:?}");
let mut t = c.sk.clone();
@@ -751,7 +751,7 @@ fn a_dimensioned_radius_is_seeded_with_nothing_given() {
let mut sk = Sketch::new();
let circle = sk.add_circle(v(10.0, 5.0), 10.0, None, None).unwrap();
push(&mut sk, ConstraintKind::Diameter { c: circle, value: 30.0 });
let s = seed(&sk, &vec![false; sk.points.len()], None, &[], &mut Budget::full());
let s = seed(&sk, &vec![false; sk.points.len()], None, true, &[], &mut Budget::full());
assert_eq!(s.radius[circle], Some(15.0));
}
@@ -773,10 +773,10 @@ fn the_touch_point_comes_from_the_point_on_curve_index() {
push(&mut sk, ConstraintKind::Tangent { a: l, b: circle });
push(&mut sk, ConstraintKind::PointOnCurve { p: b, c: circle });
let on_curve = crate::solver::points_on_curves(&sk);
let with = seed(&sk, &given(&sk), None, &on_curve, &mut Budget::full());
let with = seed(&sk, &given(&sk), None, true, &on_curve, &mut Budget::full());
assert!(with.placed_by[c].is_some_and(Placement::anchors), "{with:?}");
assert!(with.pos[c].unwrap().dist(v(40.0, 12.0) + d.perp() * 8.0) < 1e-9, "{with:?}");
let without = seed(&sk, &given(&sk), None, &[], &mut Budget::full());
let without = seed(&sk, &given(&sk), None, true, &[], &mut Budget::full());
assert!(!without.placed_by[c].is_some_and(Placement::anchors), "{without:?}");
}
+2 -2
View File
@@ -528,7 +528,7 @@ fn hostile_input_neither_panics_nor_changes_the_report() {
case(&mut sk, a, b, l);
let mut mask = given(&sk);
mask.push(true);
let s = seed(&sk, &mask, Some(&[v(f64::NAN, 0.0)]), &points_on_curves(&sk), &mut Budget::full());
let s = seed(&sk, &mask, Some(&[v(f64::NAN, 0.0)]), false, &points_on_curves(&sk), &mut Budget::full());
assert!(s.pos.iter().flatten().all(|p| p.is_finite()), "case {i}: {s:?}");
let n = sk.points.len();
for reference in [None, Some(vec![Vec2::ZERO; 1]), Some(vec![v(f64::NAN, f64::INFINITY); n]), Some(vec![Vec2::ZERO; n + 3])] {
@@ -902,7 +902,7 @@ fn given_points_are_never_placed() {
push(&mut sk, ConstraintKind::Distance { p: a, q: d, value: 36.0 });
let mask = given(&sk);
assert!(mask[g]);
let out = seed(&sk, &mask, None, &points_on_curves(&sk), &mut Budget::full());
let out = seed(&sk, &mask, None, true, &points_on_curves(&sk), &mut Budget::full());
assert_eq!((out.pos[g], out.placed_by[g]), (None, None), "{out:?}");
assert_eq!(out.placed_by[d], None, "nothing is built on it: {out:?}");
}
+2 -1
View File
@@ -55,7 +55,8 @@ pub use profiles::{
};
pub use shape::{Shape, intersections};
pub use solver::{
Analysis, DrivingValues, Hold, SolveReport, SolveStatus, measure_dimension, solve, solve_after_value_edit, solve_holding, solve_with,
Analysis, DrivingValues, Hold, SolveReport, SolveStatus, measure_dimension, solve, solve_after_edit, solve_after_value_edit, solve_holding,
solve_with,
};
pub use text::{MAX_TEXT_CHARS, text_geometry, text_geometry_in};
pub use wire::{MAX_FIT_POINTS, MAX_WIRE_POINTS, Wire, WireSpace, fit_polyline};
+19
View File
@@ -852,6 +852,21 @@ impl Sketch {
Ok(true)
}
/// Where this sketch's points were in `stored`, the same sketch before its links were
/// moved to where their sources are now ([`Sketch::update_link`]): point by point, as
/// [`crate::solve_after_edit`] takes them. Points are matched by id (a rebuilt link has
/// new ones); a point `stored` does not have is where it is now. `None` when none moved.
pub fn positions_in(&self, stored: &Sketch) -> Option<Vec<Vec2>> {
let aligned = self.points.len() == stored.points.len() && self.points.iter().zip(&stored.points).all(|(p, q)| p.id == q.id);
let was: Vec<Vec2> = if aligned {
stored.points.iter().map(|p| p.pos).collect()
} else {
let by_id: std::collections::BTreeMap<&str, Vec2> = stored.points.iter().map(|p| (p.id.as_str(), p.pos)).collect();
self.points.iter().map(|p| by_id.get(p.id.as_str()).copied().unwrap_or(p.pos)).collect()
};
self.points.iter().zip(&was).any(|(p, w)| p.pos != *w).then_some(was)
}
fn drop_link_entities(&mut self, id: &str) {
let curves = self.link_curves(id);
let keep = self.links.clone();
@@ -921,3 +936,7 @@ impl Sketch {
#[cfg(test)]
#[path = "link_identity_tests.rs"]
mod identity_tests;
#[cfg(test)]
#[path = "link_side_tests.rs"]
mod side_tests;
+122
View File
@@ -0,0 +1,122 @@
//! A dimension from a link keeps its side when the link moves: the solve after the links are
//! refreshed reads sides and turns where the points were stored ([`Sketch::positions_in`]),
//! not beside a line that has already moved past them.
use super::*;
use crate::{ConstraintKind, solve, solve_after_edit};
fn source() -> LinkSource {
LinkSource::SketchCurve { sketch: 1, curve: "rail".into() }
}
fn rail(height: f64) -> Vec<LinkGeom> {
vec![LinkGeom::Line(Vec2::new(0.0, height), Vec2::new(80.0, height))]
}
/// What the evaluation does with a stored sketch whose link's source moved: the link goes
/// where the source is, then the sketch is solved from its stored state. `kept`: the solve is
/// given the stored positions.
fn moved(stored: &Sketch, link: &str, geom: &[LinkGeom], kept: bool) -> Sketch {
let mut sk = stored.clone();
assert!(!sk.update_link(link, geom).unwrap(), "the link keeps its points");
let before = if kept { sk.positions_in(stored) } else { None };
assert!(solve_after_edit(&mut sk, &stored.driving_values(), before.as_deref()).keeps_orientation());
sk
}
fn near(at: Vec2, x: f64, y: f64) -> bool {
at.dist(Vec2::new(x, y)) < 1e-9
}
#[test]
fn a_point_dimensioned_from_a_linked_line_keeps_its_side_when_the_line_moves_past() {
let mut sk = Sketch::new();
let link = sk.add_link(LinkKind::Project, source(), &rail(50.0)).unwrap();
let l = sk.link_curves(&link)[0];
let p = sk.add_point(Vec2::new(40.0, 45.0), None).unwrap();
sk.add_constraint(ConstraintKind::DistanceX { p: 0, q: p, value: 40.0 }, None).unwrap();
sk.add_constraint(ConstraintKind::PointLineDistance { p, l, value: 5.0 }, None).unwrap();
assert!(solve(&mut sk).ok());
// Past the point (20, 6, 44), away from it (400) and not as far as it (48).
for height in [20.0, 6.0, 44.0, 400.0, 48.0] {
let at = moved(&sk, &link, &rail(height), true).points[p].pos;
assert!(near(at, 40.0, height - 5.0), "{height}: {at:?}");
}
// Read where the line is now, the stored point is above it: the solution on that side.
let at = moved(&sk, &link, &rail(20.0), false).points[p].pos;
assert!(near(at, 40.0, 25.0), "{at:?}");
}
#[test]
fn a_dimension_along_an_axis_keeps_its_side_of_a_linked_point_that_moves_past() {
for upright in [true, false] {
let end = |h: f64| vec![LinkGeom::Point(if upright { Vec2::new(80.0, h) } else { Vec2::new(h, 80.0) })];
let mut sk = Sketch::new();
let link = sk.add_link(LinkKind::Project, LinkSource::SketchPoint { sketch: 1, point: "end".into() }, &end(50.0)).unwrap();
let q = sk.link_points(&link)[0];
let p = sk.add_point(if upright { Vec2::new(40.0, 45.0) } else { Vec2::new(45.0, 40.0) }, None).unwrap();
if upright {
sk.add_constraint(ConstraintKind::DistanceX { p: 0, q: p, value: 40.0 }, None).unwrap();
sk.add_constraint(ConstraintKind::DistanceY { p: q, q: p, value: 5.0 }, None).unwrap();
} else {
sk.add_constraint(ConstraintKind::DistanceY { p: 0, q: p, value: 40.0 }, None).unwrap();
sk.add_constraint(ConstraintKind::DistanceX { p: q, q: p, value: 5.0 }, None).unwrap();
}
assert!(solve(&mut sk).ok());
for height in [20.0, 6.0, 400.0] {
let at = moved(&sk, &link, &end(height), true).points[p].pos;
let (along, across) = if upright { (at.y, at.x) } else { (at.x, at.y) };
assert!((along - (height - 5.0)).abs() < 1e-9 && (across - 40.0).abs() < 1e-9, "upright {upright}, {height}: {at:?}");
}
let at = moved(&sk, &link, &end(20.0), false).points[p].pos;
assert!(((if upright { at.y } else { at.x }) - 25.0).abs() < 1e-9, "upright {upright}: {at:?}");
}
}
/// A corner 50 from both ends of a linked line: below the line, or its mirror image above.
#[test]
fn a_corner_dimensioned_from_two_linked_points_keeps_its_turn_when_they_move_past() {
let mut sk = Sketch::new();
let link = sk.add_link(LinkKind::Project, source(), &rail(50.0)).unwrap();
let ends = sk.link_points(&link);
let p = sk.add_point(Vec2::new(40.0, 20.0), None).unwrap();
for end in ends {
sk.add_constraint(ConstraintKind::Distance { p: end, q: p, value: 50.0 }, None).unwrap();
}
assert!(solve(&mut sk).ok());
assert!(near(sk.points[p].pos, 40.0, 20.0));
for height in [10.0, -200.0, 300.0] {
let at = moved(&sk, &link, &rail(height), true).points[p].pos;
assert!(near(at, 40.0, height - 30.0), "{height}: {at:?}");
}
let at = moved(&sk, &link, &rail(10.0), false).points[p].pos;
assert!(near(at, 40.0, 40.0), "{at:?}");
}
#[test]
fn stored_positions_are_matched_by_point_id() {
let mut stored = Sketch::new();
let link = stored.add_link(LinkKind::Project, source(), &rail(50.0)).unwrap();
let p = stored.add_point(Vec2::new(40.0, 45.0), Some("p")).unwrap();
assert_eq!(stored.positions_in(&stored), None, "nothing moved");
let mut sk = stored.clone();
sk.update_link(&link, &rail(20.0)).unwrap();
let was: Vec<Vec2> = stored.points.iter().map(|q| q.pos).collect();
assert_eq!(sk.positions_in(&stored), Some(was));
// A link of another shape is built anew: its points are new, and the points after it
// change places in the list.
let mut sk = stored.clone();
let bent = [LinkGeom::Line(Vec2::new(0.0, 20.0), Vec2::new(40.0, 30.0)), LinkGeom::Line(Vec2::new(40.0, 30.0), Vec2::new(80.0, 20.0))];
assert!(sk.update_link(&link, &bent).unwrap());
let now = sk.point_index("p").unwrap();
assert_ne!(now, p);
assert_eq!(sk.positions_in(&stored), None, "the new points have no stored place and `p` has not moved");
sk.points[now].pos = Vec2::new(60.0, 45.0);
let was = sk.positions_in(&stored).unwrap();
assert_eq!(was[now], Vec2::new(40.0, 45.0));
for i in sk.link_points(&link) {
assert_eq!(was[i], sk.points[i].pos);
}
}
+17 -2
View File
@@ -1275,8 +1275,19 @@ pub type DrivingValues = std::collections::BTreeMap<String, f64>;
/// again by stepping its changed values from old to new (see `continue_values`). Where no
/// value changed a solve behaves as [`solve`].
pub fn solve_after_value_edit(sk: &mut Sketch, old: &DrivingValues) -> SolveReport {
solve_after_edit(sk, old, None)
}
/// [`solve_after_value_edit`] of a sketch whose linked geometry may have moved as well: once
/// its links are refreshed ([`Sketch::update_link`]), the linked points are where their sources
/// are now and everything else is still the stored solved state. `stored` has every point's
/// position in that state ([`Sketch::positions_in`]; `None` when no link moved), and the sides
/// and turns the solve keeps are read there, as [`solve_with`] reads them from the positions
/// before an interactive move. Read where the links are now, a point dimensioned from a
/// projected line is on the far side of it as soon as the source moves the line past the point.
pub fn solve_after_edit(sk: &mut Sketch, old: &DrivingValues, stored: Option<&[Vec2]>) -> SolveReport {
let old_values: Vec<Option<f64>> = sk.constraints.iter().map(|c| old.get(&c.id).copied()).collect();
solve_from(sk, &Hold::default(), None, Analysis::Full, Some(&old_values), &mut Budget::full())
solve_from(sk, &Hold::default(), stored, Analysis::Full, Some(&old_values), &mut Budget::full())
}
impl Sketch {
@@ -1736,7 +1747,11 @@ fn solve_inner(
let mut constructed = vec![false; lay.n];
if crate::construct::switch::seed_on() {
let given: Vec<bool> = lay.pvar.iter().map(Option::is_none).collect();
let seeded = crate::construct::seed(sk, &given, reference, &on_curve, budget);
// A drag step solves against its reference and moves only held points. Any other solve
// applies edits: also the one after linked geometry moved, whose reference is the
// stored state.
let edits = reference.is_none() || old_values.is_some();
let seeded = crate::construct::seed(sk, &given, reference, edits, &on_curve, budget);
for (i, (at, how)) in seeded.pos.iter().zip(&seeded.placed_by).enumerate() {
if let (Some(at), Some(how), Some(v)) = (at, how, lay.pvar.get(i).copied().flatten())
&& how.anchors()
+1 -1
View File
@@ -39,7 +39,7 @@ pub(crate) fn given(sk: &Sketch) -> Vec<bool> {
/// The constructive seed of a plain solve.
pub(crate) fn seed_of(sk: &Sketch) -> crate::construct::Seed {
crate::construct::seed(sk, &given(sk), None, &crate::solver::points_on_curves(sk), &mut crate::decompose::Budget::full())
crate::construct::seed(sk, &given(sk), None, true, &crate::solver::points_on_curves(sk), &mut crate::decompose::Budget::full())
}
/// Rough rectangle from four lines sharing corner points.