Sketch: preview slot outlines before committing (#2535) (#2537)

* claim: #2535

* claim: #2535

* Sketch: preview all slot sides and caps while choosing width

Fix missing temporary slot geometry for all five tools and paint every preview curve with a shared sampling budget. Preserve the arc slot construction guide. Add geometry, painting, invalid-input and interactive typed-width regression coverage.

* Sketch: honor typed slot widths below the pointer minimum

---------

Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
takuto-NA
2026-10-20 05:13:51 +09:00
committed by GitHub
parent 28d67fb577
commit 77c3b1e765
4 changed files with 306 additions and 19 deletions
@@ -86,7 +86,7 @@ fn pixel_preview_temporary_curves_match_committed_geometry_and_sampling() {
];
for (command, points, hover, sweep, rho, params) in cases {
let tool = tool(command, &points);
let preview = preview_curve(&tool, Vec2::new(hover[0], hover[1]), sweep, rho).unwrap();
let preview = preview_curve(&tool, Vec2::new(hover[0], hover[1]), sweep, rho, None).unwrap();
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
h.app.run(command, params).unwrap();
@@ -129,7 +129,7 @@ fn pixel_preview_temporary_curves_match_committed_geometry_and_sampling() {
#[test]
fn pixel_preview_fully_offscreen_circle_is_clipped_without_false_connections() {
let curve = preview_curve(&tool("sketch.circle.center", &[[0., 0.]]), Vec2::X, None, None).unwrap();
let curve = preview_curve(&tool("sketch.circle.center", &[[0., 0.]]), Vec2::X, None, None, None).unwrap();
let proj = projection(1000.0);
// The viewport half diagonal is about 832px: a 1000px circle never enters it.
assert!((proj.rect.width() * 0.5).hypot(proj.rect.height() * 0.5) < 1000.0);
@@ -138,10 +138,10 @@ fn pixel_preview_fully_offscreen_circle_is_clipped_without_false_connections() {
#[test]
fn pixel_preview_rejects_degenerate_and_unbounded_input_without_changing_document() {
assert!(preview_curve(&tool("sketch.circle.center", &[[0., 0.]]), Vec2::new(f64::NAN, 0.), None, None).is_none());
assert!(preview_curve(&tool("sketch.arc.three_point", &[[0., 0.], [1., 0.]]), Vec2::new(2., 0.), None, None).is_none());
assert!(preview_curve(&tool("sketch.circle.center", &[[0., 0.]]), Vec2::new(f64::NAN, 0.), None, None, None).is_none());
assert!(preview_curve(&tool("sketch.arc.three_point", &[[0., 0.], [1., 0.]]), Vec2::new(2., 0.), None, None, None).is_none());
let points = vec![[0., 0.]; 500];
assert!(preview_curve(&tool("sketch.spline.fit_point", &points), Vec2::X, None, None).is_none());
assert!(preview_curve(&tool("sketch.spline.fit_point", &points), Vec2::X, None, None, None).is_none());
}
#[test]
@@ -173,3 +173,136 @@ fn polygon_preview_rejects_invalid_or_unbounded_input() {
let t = tool("sketch.polygon.inscribed", &[[f64::INFINITY, 0.0]]);
assert!(polygon_outline(&t, Vec2::X, 6.0).is_none());
}
#[test]
fn slot_preview_matches_every_committed_side_and_cap() {
let cases = [
("sketch.slot.center_to_center", vec![[0., 0.], [40., 0.]], [20., 8.], json!({"p0":[0,0],"p1":[40,0],"width":16})),
// Free pointer input retains the final click's minimum width.
("sketch.slot.center_to_center", vec![[0., 0.], [40., 0.]], [20., 0.], json!({"p0":[0,0],"p1":[40,0],"width":0.001})),
("sketch.slot.overall", vec![[0., 0.], [40., 0.]], [20., -8.], json!({"p0":[0,0],"p1":[40,0],"width":16})),
("sketch.slot.center_point", vec![[0., 0.], [20., 0.]], [10., 8.], json!({"center":[0,0],"end":[20,0],"width":16})),
("sketch.slot.arc_center", vec![[0., 0.], [20., 0.], [0., 20.]], [0., 24.], json!({"center":[0,0],"start":[20,0],"end":[0,20],"width":8})),
(
"sketch.slot.arc_three_point",
vec![[20., 0.], [0., 20.], [-20., 0.]],
[-20., 4.],
json!({"start":[20,0],"through":[0,20],"end":[-20,0],"width":8}),
),
// A sloping, reversed axis and the opposite side of a three-point centre arc.
("sketch.slot.center_to_center", vec![[30., 40.], [0., 0.]], [8., -6.], json!({"p0":[30,40],"p1":[0,0],"width":20})),
(
"sketch.slot.arc_three_point",
vec![[-20., 0.], [0., 20.], [20., 0.]],
[20., -4.],
json!({"start":[-20,0],"through":[0,20],"end":[20,0],"width":8}),
),
];
for (command, points, hover, params) in cases {
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
let preview = preview_curve(&tool(command, &points), Vec2::new(hover[0], hover[1]), None, None, None)
.unwrap_or_else(|| panic!("{command}: no slot outline before the width click"));
let ctx = egui::Context::default();
let proj = projection(2.0);
let output = ctx.run_ui(egui::RawInput { screen_rect: Some(proj.rect), ..Default::default() }, |ui| {
super::paint_preview_curve(preview.clone(), &Plane::XY, ui.painter(), &proj, egui::Stroke::new(1.5, egui::Color32::WHITE), 0);
});
assert_eq!(
output.shapes.iter().filter(|shape| matches!(shape.shape, egui::Shape::Path(_))).count(),
4,
"{command}: paint all four outline curves"
);
h.app.run(command, params).unwrap();
let state = h.app.session.world_state();
let committed = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;
assert_eq!(preview.curves.len(), committed.curves.len(), "{command}");
for index in 0..committed.curves.len() {
let a = preview.exact_segs(index).unwrap_or_else(|| preview.segs(index));
let b = committed.exact_segs(index).unwrap_or_else(|| committed.segs(index));
assert_eq!(a.len(), b.len(), "{command}, curve {index}");
assert_eq!(preview.curves[index].construction, committed.curves[index].construction);
for (a, b) in a.iter().zip(&b) {
for i in 0..=20 {
assert!(a.point_at(f64::from(i) / 20.0).dist(b.point_at(f64::from(i) / 20.0)) < 1e-7, "{command}, curve {index}");
}
}
}
}
}
#[test]
fn typed_small_slot_width_preview_matches_the_final_click() {
for (command, length) in [("sketch.slot.center_to_center", 40.0), ("sketch.slot.overall", 40.0), ("sketch.slot.overall", 0.0002)] {
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
let mut t = tool(command, &[[0., 0.], [length, 0.]]);
t.dims = crate::sketch_dims::boxes_for(command, 2);
t.dims[0].value = "0.0002 / 2".into();
t.dims[0].locked = true;
t.hover = Some((Vec2::new(length * 0.5, 8.), None));
crate::sketch_dims::validate(&h.app, &t).unwrap();
// Zoom into the first cap: the accepted typed width is smaller than the pointer's
// minimum width, and must still produce the same visible outline as the final click.
let ctx = egui::Context::default();
let proj = projection(2_000_000.0);
let input = || egui::RawInput { screen_rect: Some(proj.rect), ..Default::default() };
let output = ctx.run_ui(input(), |ui| super::preview(&h.app, &t, ui.painter(), &proj));
let paths = |output: egui::FullOutput| {
output
.shapes
.into_iter()
.filter_map(|s| match s.shape {
egui::Shape::Path(path) => Some(path.points),
_ => None,
})
.collect::<Vec<_>>()
};
let preview = paths(output);
assert!(h.app.session.world_state().sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch.curves.is_empty());
let click = crate::sketch_dims::placed(&h.app, &t, t.hover.as_ref().unwrap());
h.app.tool = Some(t);
super::place(&mut h.app, click);
let state = h.app.session.world_state();
let committed = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;
assert_eq!(committed.curves.len(), 4, "{command}: final click commits the typed width");
let solidcraft_engine::geom::Seg2::Arc { radius, .. } = committed.segs(1)[0] else { panic!("missing slot cap") };
assert!((radius - 0.00005).abs() < 1e-10, "{command}: the typed width determines the cap radius");
let output = ctx.run_ui(input(), |ui| {
super::paint_preview_curve(committed.clone(), &Plane::XY, ui.painter(), &proj, egui::Stroke::new(1.5, egui::Color32::WHITE), 0);
});
let expected = paths(output);
assert!(!expected.is_empty(), "{command}: the zoomed cap must be visible");
assert_eq!(preview.len(), expected.len(), "{command}: preview and committed paths");
for (preview, expected) in preview.iter().zip(&expected) {
assert_eq!(preview.len(), expected.len(), "{command}: sampled path vertices");
for (preview, expected) in preview.iter().zip(expected) {
assert!(preview.distance(*expected) < 0.1, "{command}: preview {preview:?}, committed {expected:?}");
}
}
}
}
#[test]
fn slot_preview_rejects_invalid_extents() {
let cases = [
("sketch.slot.center_to_center", vec![[0., 0.], [0., 0.]], [0., 8.]),
("sketch.slot.overall", vec![[0., 0.], [10., 0.]], [5., 5.]),
("sketch.slot.overall", vec![[0., 0.], [10., 0.]], [5., 8.]),
("sketch.slot.arc_center", vec![[0., 0.], [20., 0.], [0., 20.]], [0., 40.]),
("sketch.slot.arc_center", vec![[0., 0.], [0., 20.], [0., 0.]], [0., 24.]),
("sketch.slot.arc_three_point", vec![[0., 0.], [10., 0.], [20., 0.]], [20., 4.]),
];
for (command, points, hover) in cases {
assert!(preview_curve(&tool(command, &points), Vec2::new(hover[0], hover[1]), None, None, None).is_none(), "{command}");
}
let t = tool("sketch.slot.center_to_center", &[[0., 0.], [40., 0.]]);
for hover in [Vec2::new(f64::NAN, 0.), Vec2::new(0., f64::INFINITY), Vec2::new(f64::MAX, f64::MAX)] {
assert!(preview_curve(&t, hover, None, None, None).is_none());
}
for width in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::MAX] {
assert!(preview_curve(&t, Vec2::new(20., 8.), None, None, Some(width)).is_none());
}
}
+1 -1
View File
@@ -1033,7 +1033,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) {
}
}
if let Some((h, _)) = t.hover.as_ref()
&& let Some(curve) = crate::tools::preview_curve(t, *h, None, crate::sketch_dims::locked_value(app, t, "Rho"))
&& let Some(curve) = crate::tools::preview_curve(t, *h, None, crate::sketch_dims::locked_value(app, t, "Rho"), None)
{
crate::tools::paint_preview_curve(curve, &ss.plane, painter, proj, stroke, crate::sketch_palette::mode(app));
}
+85 -13
View File
@@ -410,8 +410,10 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result<Vec<String>, String> {
"sketch.polygon.edge" => json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "sides": sides}),
"sketch.slot.center_to_center" | "sketch.slot.overall" => {
let dir = (p[1].0 - p[0].0).normalized().unwrap_or(Vec2::X);
let w = 2.0 * dir.cross(p[2].0 - p[0].0).abs();
json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "width": w.max(1e-3)})
// Seed the creator with the typed width too: an Overall slot places its cap
// centres from this width before the driving dimension is applied.
let width = crate::sketch_dims::locked_value(app, t, "Width").unwrap_or_else(|| (2.0 * dir.cross(p[2].0 - p[0].0).abs()).max(1e-3));
json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "width": width})
}
"sketch.point" => json!({"point": xy(&p[0])}),
_ => return Err(format!("drawing {} is not supported", t.cmd)),
@@ -572,9 +574,13 @@ pub(crate) fn paint_preview_curve(
let line = matches!(curve.kind, solidcraft_engine::sketch::CurveKind::Line { .. });
curve.centerline = mode == 2 && line;
curve.infinite = mode == 3 && line;
curve.construction = mode != 0 && !curve.centerline;
curve.construction |= mode != 0 && !curve.centerline;
}
paint_curve(&sketch, 0, plane, painter, proj, stroke, &mut solidcraft_engine::render::SamplingBudget::new(4096, 65_536));
let mut budget = solidcraft_engine::render::SamplingBudget::new(4096, 65_536);
for index in 0..sketch.curves.len() {
paint_curve(&sketch, index, plane, painter, proj, stroke, &mut budget);
}
crate::scenario::publish_count("tool_preview_curves", sketch.curves.len() as f64);
}
pub(crate) fn paint_sketch_path(points: &[solidcraft_engine::geom::Vec3], mode: u8, painter: &egui::Painter, proj: &Proj, stroke: Stroke) {
@@ -610,7 +616,13 @@ pub(crate) fn paint_curve(
/// Temporary analytic geometry uses the same exact span extraction as committed curves.
/// It never solves constraints or modifies the document.
pub(crate) fn preview_curve(t: &Tool, hover: Vec2, sweep: Option<f64>, rho: Option<f64>) -> Option<solidcraft_engine::sketch::Sketch> {
pub(crate) fn preview_curve(
t: &Tool,
hover: Vec2,
sweep: Option<f64>,
rho: Option<f64>,
slot_width: Option<f64>,
) -> Option<solidcraft_engine::sketch::Sketch> {
use solidcraft_engine::sketch::{CurveKind, Sketch};
if !hover.is_finite() || t.pts.len() >= solidcraft_engine::sketch::MAX_SPLINE_POINTS || t.pts.iter().any(|p| !p.0.is_finite()) {
return None;
@@ -633,13 +645,7 @@ pub(crate) fn preview_curve(t: &Tool, hover: Vec2, sweep: Option<f64>, rho: Opti
sk.add_line(*m * 2.0 - hover, hover, None, None, None).ok()?;
}
("sketch.circle.three_point" | "sketch.arc.three_point", [a, b]) => {
let ab = *b - *a;
let ah = hover - *a;
let d = 2.0 * ab.cross(ah);
if !d.is_finite() || d.abs() < 1e-12 {
return None;
}
let centre = *a + Vec2::new(ah.y * ab.len2() - ab.y * ah.len2(), ab.x * ah.len2() - ah.x * ab.len2()) / d;
let centre = preview_circumcentre(*a, *b, hover)?;
if t.cmd == "sketch.circle.three_point" {
sk.add_circle(centre, centre.dist(*a), None, None).ok()?;
} else {
@@ -664,6 +670,55 @@ pub(crate) fn preview_curve(t: &Tool, hover: Vec2, sweep: Option<f64>, rho: Opti
curve.reversed = sweep < 0.0;
}
}
("sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point", [a, b]) => {
let dir = (*b - *a).normalized()?;
// Typed widths can be smaller than the final click's pointer minimum.
let width = slot_width.unwrap_or_else(|| (2.0 * dir.cross(hover - *a).abs()).max(1e-3));
if !width.is_finite() || width <= 1e-9 {
return None;
}
let radius = width * 0.5;
let (c0, c1) = match t.cmd {
"sketch.slot.overall" if a.dist(*b) > width => (*a + dir * radius, *b - dir * radius),
"sketch.slot.overall" => return None,
"sketch.slot.center_point" => (*a * 2.0 - *b, *b),
_ => (*a, *b),
};
let n = dir.perp() * radius;
sk.add_line(c0 - n, c1 - n, None, None, None).ok()?;
sk.add_arc(c1, c1 - n, c1 + n, [None; 3], None).ok()?;
sk.add_line(c1 + n, c0 + n, None, None, None).ok()?;
sk.add_arc(c0, c0 + n, c0 - n, [None; 3], None).ok()?;
}
("sketch.slot.arc_center" | "sketch.slot.arc_three_point", [a, b, c]) => {
let (centre, start, end, radius, width) = if t.cmd == "sketch.slot.arc_center" {
(*a, *b, *c, a.dist(*b), (2.0 * (hover.dist(*a) - a.dist(*b)).abs()).max(1e-3))
} else {
let centre = preview_circumcentre(*a, *c, *b)?;
let span = |p: Vec2| ((p - centre).angle() - (*a - centre).angle()).rem_euclid(std::f64::consts::TAU);
let (start, end) = if span(*b) < span(*c) { (*a, *c) } else { (*c, *a) };
(centre, start, end, centre.dist(*a), (2.0 * hover.dist(*c)).max(1e-3))
};
let t0 = (start - centre).angle();
let sweep = ((end - centre).angle() - t0).rem_euclid(std::f64::consts::TAU);
let half = width * 0.5;
if !radius.is_finite()
|| !half.is_finite()
|| half >= radius
|| end.dist(centre) < 1e-9
|| !(sweep > 1e-6 && sweep < std::f64::consts::TAU - 1e-6)
{
return None;
}
let at = |angle: f64, r: f64| centre + Vec2::from_angle(angle) * r;
let t1 = t0 + sweep;
sk.add_arc(centre, at(t0, radius + half), at(t1, radius + half), [None; 3], None).ok()?;
sk.add_arc(centre, at(t0, radius - half), at(t1, radius - half), [None; 3], None).ok()?;
sk.add_arc(at(t1, radius), at(t1, radius + half), at(t1, radius - half), [None; 3], None).ok()?;
sk.add_arc(at(t0, radius), at(t0, radius - half), at(t0, radius + half), [None; 3], None).ok()?;
let guide = sk.add_arc(centre, at(t0, radius), at(t1, radius), [None; 3], None).ok()?;
sk.curves.get_mut(guide)?.construction = true;
}
("sketch.ellipse", [c, m]) => {
let u = (*m - *c).normalized()?;
let d = hover - *c;
@@ -712,6 +767,17 @@ pub(crate) fn preview_curve(t: &Tool, hover: Vec2, sweep: Option<f64>, rho: Opti
Some(sk)
}
fn preview_circumcentre(a: Vec2, b: Vec2, c: Vec2) -> Option<Vec2> {
let ab = b - a;
let ac = c - a;
let d = 2.0 * ab.cross(ac);
if !d.is_finite() || d.abs() < 1e-12 {
return None;
}
let centre = a + Vec2::new(ac.y * ab.len2() - ab.y * ac.len2(), ab.x * ac.len2() - ac.x * ab.len2()) / d;
centre.is_finite().then_some(centre)
}
/// The polygon's actual straight edges, in the same orientation as the eventual command.
/// Keep preview work bounded even while a side-count expression is being edited.
pub(crate) fn polygon_outline(t: &Tool, hover: Vec2, sides: f64) -> Option<Vec<Vec2>> {
@@ -779,7 +845,13 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) {
}
}
if let Some(curve) = preview_curve(t, h, crate::sketch_dims::locked_value(app, t, "Sweep"), crate::sketch_dims::locked_value(app, t, "Rho")) {
if let Some(curve) = preview_curve(
t,
h,
crate::sketch_dims::locked_value(app, t, "Sweep"),
crate::sketch_dims::locked_value(app, t, "Rho"),
crate::sketch_dims::locked_value(app, t, "Width"),
) {
paint_preview_curve(curve, &ss.plane, painter, proj, stroke, crate::sketch_palette::mode(app));
return;
}
@@ -0,0 +1,82 @@
[
{"note":"#2535: show the complete slot outline while choosing its width, without adding document curves until the final click."},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"call":"ui.view","params":{"view":"top"}},
{"start":"sketch.slot.center_to_center"},
{"click":{"sketch":[0,0]}},
{"click":{"sketch":[40,0]}},
{"move":{"sketch":[20,8]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"sketch_curves":{"own":0},"errors":0}},
{"text":"16"},
{"key":"Tab"},
{"move":{"sketch":[20,20]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"errors":0}},
{"click":{"sketch":[20,20]}},
{"expect":{"sketch_curves":{"own":4},"param":{"d1":16},"errors":0}},
{"key":"Escape"},
{"start":"sketch.slot.overall"},
{"click":{"sketch":[0,-30]}},
{"click":{"sketch":[40,-30]}},
{"move":{"sketch":[20,-38]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"errors":0}},
{"click":{"sketch":[20,-38]}},
{"expect":{"sketch_curves":{"own":8},"errors":0}},
{"key":"Escape"},
{"start":"sketch.slot.center_point"},
{"click":{"sketch":[0,30]}},
{"click":{"sketch":[20,30]}},
{"move":{"sketch":[10,38]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"errors":0}},
{"click":{"sketch":[10,38]}},
{"expect":{"sketch_curves":{"own":12},"errors":0}},
{"key":"Escape"},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"call":"ui.view","params":{"view":"top"}},
{"start":"sketch.slot.arc_center"},
{"click":{"sketch":[0,0]}},
{"click":{"sketch":[20,0]}},
{"click":{"sketch":[0,20]}},
{"move":{"sketch":[0,24]}},
{"expect":{"count":{"name":"tool_preview_curves","value":5},"errors":0}},
{"click":{"sketch":[0,24]}},
{"expect":{"sketch_curves":{"own":5},"errors":0}},
{"key":"Escape"},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"call":"ui.view","params":{"view":"top"}},
{"start":"sketch.slot.arc_three_point"},
{"click":{"sketch":[20,0]}},
{"click":{"sketch":[0,20]}},
{"click":{"sketch":[-20,0]}},
{"move":{"sketch":[-20,4]}},
{"expect":{"count":{"name":"tool_preview_curves","value":5},"errors":0}},
{"click":{"sketch":[-20,4]}},
{"expect":{"sketch_curves":{"own":5},"errors":0}},
{"key":"Escape"},
{"note":"Typed widths below the pointer minimum remain valid for both dimensioned slot tools."},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"call":"ui.view","params":{"view":"top"}},
{"start":"sketch.slot.center_to_center"},
{"click":{"sketch":[0,0]}},
{"click":{"sketch":[40,0]}},
{"move":{"sketch":[20,8]}},
{"text":"0.0002 / 2"},
{"key":"Tab"},
{"move":{"sketch":[20,20]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"sketch_curves":{"own":0},"errors":0}},
{"click":{"sketch":[20,20]}},
{"expect":{"sketch_curves":{"own":4},"param":{"d2":0.0001},"errors":0}},
{"key":"Escape"},
{"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}},
{"call":"ui.view","params":{"view":"top"}},
{"start":"sketch.slot.overall"},
{"click":{"sketch":[0,0]}},
{"click":{"sketch":[40,0]}},
{"move":{"sketch":[20,8]}},
{"text":"0.0001"},
{"key":"Tab"},
{"move":{"sketch":[20,20]}},
{"expect":{"count":{"name":"tool_preview_curves","value":4},"sketch_curves":{"own":0},"errors":0}},
{"click":{"sketch":[20,20]}},
{"expect":{"sketch_curves":{"own":4},"param":{"d3":0.0001},"errors":0}},
{"key":"Escape"}
]