Files
solidcraft/crates/sketch/src/link_side_tests.rs
T
NOWN-DEVandClaude Opus 5.5 0063954286 Sketch: a sketch whose links moved far is walked there and keeps the stored sides and turns (#6420)
* Test: a label on a path follows its source far

A rectangle of construction lines placed on three points projected from another sketch, with a
centred text on its far side: moved 270 away by the parameters that place the source, its solve
from the stored sketch failed ("the sketch constraints conflict").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PsiqyXWBFGTCX9J51bhysc

* Sketch: a sketch whose links moved far is walked there and keeps the stored sides and turns

Problem: a change upstream moves what a sketch projects (or the points its parameters place)
while its own values stay. The solve from the stored state then either failed ("the sketch
constraints conflict") or landed on the mirror image of a side or turn and reported it solved,
because a solve after a link move checks no orientation when no value changed: a line tangent to
a moving circle hopped a diameter to the circle's other side. And a sketch moved so was stored
only when its own values changed, so every later evaluation solved it from its first stored state
again.

Solution:
- flips_from_stored: the orientations of the stored sketch (unsigned offsets, distance
  triangles, and a line's side of a circle it is tangent to) that a solved sketch turned over,
  each read where the stored sketch has its points.
- solve_after_link_move: the new values solved where the links were stored, then the moved
  points walked to where they are now (predictor-corrector: each step starts from the last two
  solutions carried along their secant; steps halve on a failure or a flip and grow while they
  solve).
- Document::solve_sketch: when a linked or fixed point moved and the jump fails or flips, the
  walk; a flip the walk cannot avoid either is reported as one.
- Model::edited_sketches: a sketch whose linked points a value edit moved is stored as solved, so
  the next evaluation starts where this one ended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PsiqyXWBFGTCX9J51bhysc

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 01:55:17 +00:00

175 lines
8.6 KiB
Rust

//! 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::{Analysis, ConstraintKind, flips_from_stored, solve, solve_after_edit, solve_after_link_move};
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);
}
}
/// A 40 mm horizontal line tangent to a linked circle (r 3) above it, its left end 20 mm left
/// of the centre; the stored sketch with the circle where it was (`y`).
fn tangent_on_circle(y: f64) -> (Sketch, String, usize) {
let circle = |y: f64| vec![LinkGeom::Circle(Vec2::new(0.0, y), 3.0)];
let mut sk = Sketch::new();
let link = sk.add_link(LinkKind::Project, LinkSource::SketchCurve { sketch: 1, curve: "bend".into() }, &circle(y)).unwrap();
let c = sk.link_curves(&link)[0];
let centre = sk.link_points(&link)[0];
let l = sk.add_line(Vec2::new(-20.0, y + 3.0), Vec2::new(20.0, y + 3.0), None, None, None).unwrap();
let (a, _) = sk.curves[l].kind.ends().unwrap();
sk.add_constraint(ConstraintKind::Horizontal { l }, None).unwrap();
sk.add_constraint(ConstraintKind::Tangent { a: l, b: c }, None).unwrap();
sk.add_constraint(ConstraintKind::DistanceX { p: a, q: centre, value: 20.0 }, None).unwrap();
sk.add_constraint(ConstraintKind::Length { l, value: 40.0 }, None).unwrap();
assert!(solve(&mut sk).ok());
(sk, link, l)
}
#[test]
fn a_line_tangent_to_a_linked_circle_keeps_its_side_when_the_circle_moves_far() {
let (stored, link, l) = tangent_on_circle(0.0);
let height = |sk: &Sketch| match sk.curves[l].kind {
CurveKind::Line { a, .. } => sk.points[a].pos.y,
_ => f64::NAN,
};
assert!((height(&stored) - 3.0).abs() < 1e-9);
let circle = |y: f64| vec![LinkGeom::Circle(Vec2::new(0.0, y), 3.0)];
// Moved 100 mm up, the circle passes the line. Its other tangent (y 97, a diameter below)
// solves every constraint too: only the side it left tells it from the right one.
let mut mirrored = stored.clone();
mirrored.update_link(&link, &circle(100.0)).unwrap();
for p in &mut mirrored.points {
if p.link.is_none() && p.id != "origin" {
p.pos.y += 94.0;
}
}
assert!((height(&mirrored) - 97.0).abs() < 1e-9);
assert!(solve(&mut mirrored).ok() && (height(&mirrored) - 97.0).abs() < 1e-9);
assert!(!flips_from_stored(&mirrored, &stored).is_empty());
// Walked there, the line stays above the circle, in either direction and far.
for y in [100.0, -100.0, 2000.0] {
let mut sk = stored.clone();
sk.update_link(&link, &circle(y)).unwrap();
let report = solve_after_link_move(&mut sk, &stored, &stored.driving_values(), Analysis::Full);
assert!(report.is_some_and(|r| r.keeps_orientation()), "{y}");
assert!((height(&sk) - (y + 3.0)).abs() < 1e-9 && flips_from_stored(&sk, &stored).is_empty(), "{y}: {}", height(&sk));
}
// Nothing moved: nothing to walk.
let mut sk = stored.clone();
assert!(solve_after_link_move(&mut sk, &stored, &stored.driving_values(), Analysis::Full).is_none());
}