Files
solidcraft-upstream/crates/ui-egui/src/sketch_drag_ui_tests.rs
T
Iraklis KourtisandClaude Opus 5.5 fe4d171e22 feat(sketch): live drag that keeps its solution, ended by sketch.drag_end (#341)
* feat(sketch): constructive solving ahead of the numeric solver

Part 1 of splitting #251: the sketch crate only. A graph-constructive front
end (Owen 1991; Fudos & Hoffmann 1997) places what it can by ruler-and-compass
loci and cluster decomposition, Levenberg-Marquardt finishes and verifies the
rest, and the rank analysis stays authoritative for degrees of freedom. Solves
can follow a reference (the previous solution), so edits and drags keep their
branch instead of flipping.

New API: solve_with (hold, reference, analysis, budget) and
solve_after_value_edit; solve and solve_holding keep their signatures as
wrappers, so no caller changes here.

When the seed moves an arc's centre, the arc's unconstructed ends start carried
along (the arc translates) instead of left behind for LM to turn; found by the
reference test 20-slot-constrained.

Refs #203

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

* feat(doc): keep a sketch's solved state across value edits

Part 2 of splitting #251 (builds on part 1, the sketch crate):
- Sketch edits solve for the document's values continuing from the stored
  solved state (Document::solve_sketch), so a dimension edit keeps the
  sketch's branch instead of re-solving from scratch.
- A value edit (parameters, a configuration, the units) that would make a
  sketch that solved before fail or turn over is refused with the
  conflicting constraints named; otherwise the solved sketches are written
  back into the document (Model::adopt_solved_sketches), which changes what
  saved designs store.
- Later features are keyed by a sketch's solved geometry (to a 1e-6 mm
  quantum), so setting a value back reuses their earlier results.
- Solved sketches in a model state are shared (Arc) between states.

Refs #203

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

* feat(sketch): live drag that keeps its solution, ended by sketch.drag_end

Part 3 of splitting #251 (builds on part 2):
- sketch.drag follows a long move in short held steps, so the geometry keeps
  its branch, and reports how far it got (reached, at).
- live: true runs one frame of an interactive drag: the sketch is solved
  once and shown, later features keep their last result (marked stale)
  until sketch.drag_end or any command that reads the model evaluates it
  (CommandSpec::live, reads_model, doc_only).
- sketch.drag_end adds the constraint the drag let go on and evaluates the
  model once.
- Undo and redo evaluate a new document once (execute does) instead of
  twice.
- The viewport drives drags through live frames and drag_end; dimension
  text drags start only off sketch geometry.

Refs #203

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 13:45:48 -04:00

108 lines
4.6 KiB
Rust

//! Dragging sketch geometry in the viewport: live frames re-solve the sketch only, letting go
//! brings the model up to date in the drag's one undo step, and the grab stays on the geometry.
use serde_json::{Value, json};
use solvecraft_engine::geom::Vec2;
use crate::scenario::Harness;
fn run(h: &mut Harness, id: &str, p: Value) {
if let Err(e) = h.app.run(id, p.clone()) {
panic!("{id} {p}: {e}");
}
}
fn screen(h: &mut Harness, x: f64, y: f64) -> egui::Pos2 {
let [sx, sy] = h.at(&json!({"sketch": [x, y]})).expect("on screen");
egui::pos2(sx as f32, sy as f32)
}
fn point(h: &Harness, id: &str) -> Vec2 {
let sid = h.app.session.active_sketch.expect("sketch mode");
let st = h.app.session.world_state();
let sk = &st.sketch(sid).expect("the sketch").sketch;
sk.resolve_point(id).and_then(|i| sk.point(i)).unwrap_or_else(|| panic!("no point {id}"))
}
/// Press at `path[0]` and move through the rest, one frame per event.
fn press_and_move(h: &mut Harness, path: &[egui::Pos2]) {
let Some(&a) = path.first() else { return };
h.app.synthetic.push(egui::Event::PointerMoved(a));
h.app.synthetic.push(egui::Event::PointerButton { pos: a, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.app.synthetic.extend(path.iter().skip(1).map(|&p| egui::Event::PointerMoved(p)));
play(h);
}
fn release(h: &mut Harness, at: egui::Pos2) {
h.app.synthetic.push(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
play(h);
}
fn play(h: &mut Harness) {
while !h.app.synthetic.is_empty() {
h.frame();
}
h.frames(2);
}
/// Points from `a` to `b` in `n` equal moves (without `a`).
fn steps(a: egui::Pos2, b: egui::Pos2, n: usize) -> Vec<egui::Pos2> {
(1..=n).map(|k| a + (b - a) * (k as f32 / n as f32)).collect()
}
#[test]
fn a_viewport_drag_solves_the_sketch_live_and_rebuilds_the_model_on_release() {
let mut h = Harness::new();
run(&mut h, "sketch.create", json!({"plane": "XY"}));
run(&mut h, "sketch.rectangle.two_point", json!({"p0": [5, 5], "p1": [25, 20]}));
run(&mut h, "sketch.dimension", json!({"entities": ["l1"], "value": 20}));
run(&mut h, "sketch.dimension", json!({"entities": ["l2"], "value": 15}));
run(&mut h, "sketch.finish", json!({}));
run(&mut h, "solid.extrude", json!({"distance": 5}));
let sketch = h.app.session.doc.features.first().map(|f| f.id).expect("the sketch");
h.app.edit_sketch(sketch);
h.settle();
let undo = h.app.session.undo.len();
let a = screen(&mut h, 5.0, 5.0);
let b = screen(&mut h, -3.0, -6.0);
let mut path = vec![a];
path.extend(steps(a, b, 8));
press_and_move(&mut h, &path);
assert!(h.app.viewport.point_drag.is_some(), "the drag is under way");
assert!(h.app.session.model.is_stale(), "the frames updated the sketch only");
assert!(point(&h, "l1.start").dist(Vec2::new(-3.0, -6.0)) < 0.1, "{:?}", point(&h, "l1.start"));
release(&mut h, b);
assert!(h.app.viewport.point_drag.is_none());
assert!(!h.app.session.model.is_stale(), "letting go evaluated the model");
assert_eq!(h.app.session.undo.len(), undo + 1, "one undo step for the drag");
assert!(point(&h, "l1.start").dist(Vec2::new(-3.0, -6.0)) < 0.1);
run(&mut h, "edit.undo", json!({}));
assert!(point(&h, "l1.start").dist(Vec2::new(5.0, 5.0)) < 1e-9);
}
#[test]
fn a_dragged_line_that_stops_short_stays_under_the_cursor_when_it_can_follow_again() {
let mut h = Harness::new();
run(&mut h, "sketch.create", json!({"plane": "XY"}));
// Two 10 mm links from the origin hold the start of a horizontal 10 mm line: the line can
// slide right until its start is 20 mm from the origin (3 mm).
let (x, y) = (15.0, 75f64.sqrt());
run(&mut h, "sketch.line", json!({"points": [[0, 0], [10, 0], [x, y], [x + 10.0, y]]}));
for l in ["l1", "l2", "l3"] {
run(&mut h, "sketch.dimension", json!({"entities": [l], "value": 10}));
}
run(&mut h, "sketch.constraint.horizontal_vertical", json!({"line": "l3"}));
let grab = screen(&mut h, x + 5.0, y);
let far = screen(&mut h, x + 11.0, y);
let back = screen(&mut h, x + 7.0, y);
let mut path = vec![grab];
path.extend(steps(grab, far, 8));
path.extend(steps(far, back, 4));
press_and_move(&mut h, &path);
release(&mut h, back);
// The cursor went 6 mm right, the line only 3; back at 2 mm right it is under it again.
let start = point(&h, "l3.start");
assert!(start.dist(Vec2::new(x + 2.0, y)) < 0.1, "{start:?}");
assert!(!h.app.session.model.is_stale());
}