A line drawn across a circle only snapped its end onto the curve, so a diameter could miss the centre at any angle. Near the centre the line now locks onto the radial line: on the rim it stays on the circle and the centre is kept on the line, and on the centre point the end lands there.
This commit is contained in:
committed by
GitHub
parent
9b8fa3d357
commit
43fa25458d
@@ -1,7 +1,8 @@
|
||||
//! Drawing-time inference for the Line tool: while the next point is placed the
|
||||
//! cursor snaps to directions and places taken from the existing geometry (parallel or
|
||||
//! perpendicular to a line, on the extension of a line, tangent to a circle or arc). A dashed
|
||||
//! guide and a label show it; placing the point adds the matching constraint.
|
||||
//! perpendicular to a line, on the extension of a line, tangent to a circle or arc, or through
|
||||
//! the centre of a circle or arc). A dashed guide and a label show it; placing the point adds
|
||||
//! the matching constraint.
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
@@ -20,6 +21,8 @@ pub enum Kind {
|
||||
Perpendicular,
|
||||
Extension,
|
||||
Tangent,
|
||||
/// The new line passes through the centre of a circle or arc.
|
||||
Center,
|
||||
}
|
||||
|
||||
/// A snap taken from existing geometry: what, to which curve, where the point goes, how far the
|
||||
@@ -31,6 +34,9 @@ pub struct Inference {
|
||||
pub at: Vec2,
|
||||
pub dist: f64,
|
||||
pub guide: (Vec2, Vec2),
|
||||
/// Point argument for the new endpoint: `"<id>.center"` when it lands on the centre, or
|
||||
/// `"on:<id>:x,y"` when it lands on the circle. None: a free point on the radial line.
|
||||
pub arg: Option<String>,
|
||||
}
|
||||
|
||||
impl Inference {
|
||||
@@ -40,6 +46,7 @@ impl Inference {
|
||||
Kind::Perpendicular => "\u{22a5}",
|
||||
Kind::Extension => "Ext",
|
||||
Kind::Tangent => "T",
|
||||
Kind::Center => "Ctr",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,13 +97,67 @@ pub fn tangent(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference>
|
||||
let on_arc = sk.shape(i).is_none_or(|sh| sh.dist(t) < 1e-6);
|
||||
let e = t.dist(lp);
|
||||
if on_arc && e < tol && best.as_ref().is_none_or(|b| e < b.dist) {
|
||||
best = Some(Inference { kind: Kind::Tangent, curve: c.id.clone(), at: t, dist: e, guide: (prev, t) });
|
||||
best = Some(Inference { kind: Kind::Tangent, curve: c.id.clone(), at: t, dist: e, guide: (prev, t), arg: None });
|
||||
}
|
||||
}
|
||||
}
|
||||
keep(best)
|
||||
}
|
||||
|
||||
/// The line from `prev` passes near the centre of a circle or arc (within `tol`).
|
||||
///
|
||||
/// The point lands on that radial line, so the segment contains the centre. On the circle it
|
||||
/// lands on the rim (a diameter, or a radius when the other end is already the centre's side).
|
||||
/// On the centre point itself it lands on that point. A chord that misses the centre, or that
|
||||
/// stops before reaching it, is left alone.
|
||||
pub fn through_center(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference> {
|
||||
let mut best: Option<Inference> = None;
|
||||
for (i, c) in sk.curves.iter().enumerate() {
|
||||
if !matches!(c.kind, CurveKind::Circle { .. } | CurveKind::Arc { .. }) {
|
||||
continue;
|
||||
}
|
||||
let (Some(o), Some(r)) = (sk.center(i), sk.radius(i)) else { continue };
|
||||
if r <= 1e-9 {
|
||||
continue;
|
||||
}
|
||||
// Already standing on the centre: every direction goes through it.
|
||||
let Some(dir) = (o - prev).normalized() else { continue };
|
||||
let off = (lp - prev).cross(dir).abs();
|
||||
if off > tol {
|
||||
continue;
|
||||
}
|
||||
let along = (lp - prev).dot(dir);
|
||||
if along < tol {
|
||||
continue;
|
||||
}
|
||||
let d = prev.dist(o);
|
||||
let past = along + tol >= d;
|
||||
let far = prev + dir * (d + r);
|
||||
let on_arc = sk.shape(i).is_none_or(|sh| sh.dist(far) < 1e-6);
|
||||
let near_curve = sk.shape(i).is_some_and(|sh| sh.dist(lp) < tol * 0.8);
|
||||
let rim = near_curve && on_arc && past && far.dist(lp) <= tol;
|
||||
let at_centre = lp.dist(o) <= tol;
|
||||
let (at, arg) = if rim && (!at_centre || far.dist(lp) <= lp.dist(o)) {
|
||||
(far, Some(format!("on:{}:{},{}", c.id, far.x, far.y)))
|
||||
} else if at_centre {
|
||||
(o, Some(format!("{}.center", c.id)))
|
||||
} else if past {
|
||||
let at = prev + dir * along;
|
||||
if at.dist(o) <= tol { (o, Some(format!("{}.center", c.id))) } else { (at, None) }
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
if at.dist(prev) < 1e-6 {
|
||||
continue;
|
||||
}
|
||||
let dist = at.dist(lp);
|
||||
if dist <= tol && best.as_ref().is_none_or(|b| dist < b.dist) {
|
||||
best = Some(Inference { kind: Kind::Center, curve: c.id.clone(), at, dist, guide: (prev, at), arg });
|
||||
}
|
||||
}
|
||||
keep(best)
|
||||
}
|
||||
|
||||
/// Directions and places from lines: parallel or perpendicular to a line (from `prev`), or on
|
||||
/// the extension of a line beyond its ends.
|
||||
pub fn lines(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference> {
|
||||
@@ -133,6 +194,7 @@ pub fn lines(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference> {
|
||||
at,
|
||||
dist: e,
|
||||
guide: (prev - d * (t.signum() * tol * 6.0), at + d * (t.signum() * tol * 6.0)),
|
||||
arg: None,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -144,7 +206,7 @@ pub fn lines(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference> {
|
||||
let e = at.dist(lp);
|
||||
if e < tol * 0.8 && !touches {
|
||||
let from = if s < 0.0 { a } else { b };
|
||||
offer(Inference { kind: Kind::Extension, curve: c.id.clone(), at, dist: e * 0.9, guide: (from, at) });
|
||||
offer(Inference { kind: Kind::Extension, curve: c.id.clone(), at, dist: e * 0.9, guide: (from, at), arg: None });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,11 +218,16 @@ pub fn lines(sk: &Sketch, prev: Vec2, lp: Vec2, tol: f64) -> Option<Inference> {
|
||||
pub fn commit(app: &mut SolveApp, line: &str, end: Vec2) {
|
||||
let Some(i) = current().filter(|i| i.at.dist(end) < 1e-9) else { return };
|
||||
clear();
|
||||
// The endpoint is the circle's centre: the line already ends on that point.
|
||||
if matches!(i.kind, Kind::Center) && i.arg.as_deref().is_some_and(|a| a.ends_with(".center")) {
|
||||
return;
|
||||
}
|
||||
let (cmd, mut params) = match i.kind {
|
||||
Kind::Parallel => ("sketch.constraint.parallel", json!({"a": i.curve, "b": line})),
|
||||
Kind::Perpendicular => ("sketch.constraint.perpendicular", json!({"a": i.curve, "b": line})),
|
||||
Kind::Tangent => ("sketch.constraint.tangent", json!({"a": i.curve, "b": line})),
|
||||
Kind::Extension => ("sketch.constraint.coincident", json!({"a": format!("{line}.end"), "b": i.curve})),
|
||||
Kind::Center => ("sketch.constraint.coincident", json!({"a": format!("{}.center", i.curve), "b": line})),
|
||||
};
|
||||
// An inference only adds what the sketch does not already have: one that is already implied
|
||||
// (redundant) or would conflict is simply not kept.
|
||||
@@ -225,4 +292,85 @@ mod tests {
|
||||
assert!(i.at.dist(t) < 1e-9);
|
||||
assert!((i.at - Vec2::new(30.0, 0.0)).dot(i.at - prev).abs() < 1e-9);
|
||||
}
|
||||
|
||||
fn circle() -> Sketch {
|
||||
let mut s = Sketch::new();
|
||||
let _ = s.add_circle(Vec2::ZERO, 10.0, None, Some("c1"));
|
||||
s
|
||||
}
|
||||
|
||||
/// `o` lies on the segment from `prev` to `at`.
|
||||
fn through(prev: Vec2, at: Vec2, o: Vec2) -> bool {
|
||||
let d = at - prev;
|
||||
let len2 = d.len2();
|
||||
if len2 < 1e-18 {
|
||||
return false;
|
||||
}
|
||||
let t = (o - prev).dot(d) / len2;
|
||||
(0.0..=1.0).contains(&t) && (prev + d * t).dist(o) < 1e-8
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_near_diameter_lands_on_the_circle_through_its_centre() {
|
||||
let s = circle();
|
||||
// Start on the left of the circle; the cursor is on the right, a fraction off the diameter.
|
||||
let i = through_center(&s, Vec2::new(-10.0, 0.0), Vec2::new(10.0, 0.3), 0.5).expect("diameter");
|
||||
assert_eq!(i.kind, Kind::Center);
|
||||
assert!(i.at.dist(Vec2::new(10.0, 0.0)) < 1e-9, "{:?}", i.at);
|
||||
assert!(through(Vec2::new(-10.0, 0.0), i.at, Vec2::ZERO));
|
||||
assert_eq!(i.arg.as_deref(), Some("on:c1:10,0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chord_that_misses_the_centre_stays_free() {
|
||||
let s = circle();
|
||||
assert!(through_center(&s, Vec2::new(-10.0, 0.0), Vec2::new(10.0, 4.0), 0.5).is_none());
|
||||
// Stopped short of the centre: still a free point on the near side.
|
||||
assert!(through_center(&s, Vec2::new(-30.0, 0.0), Vec2::new(-15.0, 0.1), 0.5).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_endpoint_snaps_onto_the_centre_point() {
|
||||
let s = circle();
|
||||
let i = through_center(&s, Vec2::new(20.0, 0.0), Vec2::new(0.2, 0.1), 0.5).expect("centre");
|
||||
assert!(i.at.dist(Vec2::ZERO) < 1e-9);
|
||||
assert_eq!(i.arg.as_deref(), Some("c1.center"));
|
||||
// Already standing on the centre: nothing further to snap.
|
||||
assert!(through_center(&s, Vec2::ZERO, Vec2::new(10.0, 0.2), 0.5).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_past_the_centre_locks_onto_the_radial_line() {
|
||||
let s = circle();
|
||||
let i = through_center(&s, Vec2::new(-20.0, 0.0), Vec2::new(5.0, 0.2), 0.5).expect("radial");
|
||||
assert!(i.at.dist(Vec2::new(5.0, 0.0)) < 1e-9, "{:?}", i.at);
|
||||
assert!(i.arg.is_none());
|
||||
assert!(through(Vec2::new(-20.0, 0.0), i.at, Vec2::ZERO));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_arc_only_accepts_a_rim_point_that_lies_on_it() {
|
||||
let mut s = Sketch::new();
|
||||
// Quarter arc in the first quadrant.
|
||||
let _ = s.add_arc(Vec2::ZERO, Vec2::new(10.0, 0.0), Vec2::new(0.0, 10.0), [None; 3], Some("a1"));
|
||||
let on = through_center(&s, Vec2::new(-20.0, 0.0), Vec2::new(10.1, 0.2), 0.5).expect("on the arc");
|
||||
assert!(on.at.dist(Vec2::new(10.0, 0.0)) < 1e-9, "{:?}", on.at);
|
||||
assert!(on.arg.as_deref().unwrap().starts_with("on:a1:"));
|
||||
// The opposite point, (-10, 0), is not on this arc, so the line still locks through the
|
||||
// centre but does not claim a point on the arc.
|
||||
let off = through_center(&s, Vec2::new(20.0, 0.0), Vec2::new(-10.0, 0.2), 0.5).expect("through, off the arc");
|
||||
assert!(off.at.dist(Vec2::new(-10.0, 0.0)) < 1e-9, "{:?}", off.at);
|
||||
assert!(off.arg.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_construction_circle_still_snaps_and_the_nearer_centre_wins() {
|
||||
let mut s = circle();
|
||||
s.curves[0].construction = true;
|
||||
let i = through_center(&s, Vec2::new(-10.0, 0.0), Vec2::new(9.8, 0.2), 0.5).expect("construction");
|
||||
assert!(i.at.dist(Vec2::new(10.0, 0.0)) < 1e-9);
|
||||
let _ = s.add_circle(Vec2::new(0.0, 40.0), 10.0, None, Some("c2"));
|
||||
let i = through_center(&s, Vec2::new(-20.0, 0.0), Vec2::new(10.0, 0.2), 0.5).expect("nearer");
|
||||
assert_eq!(i.curve, "c1");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,20 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
|
||||
/// sketch geometry stays there, and one on a curve slides along it to where the axis crosses.
|
||||
pub fn placed(app: &SolveApp, t: &Tool, p: &(Vec2, Option<String>)) -> (Vec2, Option<String>) {
|
||||
let line = t.cmd == "sketch.line";
|
||||
if t.dims.iter().any(|b| b.locked) || (line && p.1.is_none()) {
|
||||
// A typed length or angle wins over every snap.
|
||||
if t.dims.iter().any(|b| b.locked) {
|
||||
return (effective(app, t, p.0), None);
|
||||
}
|
||||
// A line snapped through a circle's centre stays on that radial line. The axis snap would
|
||||
// pull a near-horizontal diameter off the centre.
|
||||
if line
|
||||
&& let Some(i) = crate::inference::current()
|
||||
&& i.kind == crate::inference::Kind::Center
|
||||
&& i.at.dist(p.0) < 1e-9
|
||||
{
|
||||
return p.clone();
|
||||
}
|
||||
if line && p.1.is_none() {
|
||||
return (effective(app, t, p.0), None);
|
||||
}
|
||||
if line && let Some(q) = on_curve_axis(app, t, p) {
|
||||
|
||||
@@ -411,7 +411,8 @@ pub fn refine_snap(
|
||||
}
|
||||
None => PREV.with(Cell::get),
|
||||
};
|
||||
// Line tool: the tangent point of a circle or arc (before plain "on the curve").
|
||||
// Line tool: the tangent point of a circle or arc, then a line through its centre
|
||||
// (before plain "on the curve", which would leave a chord free to miss the centre).
|
||||
let line_tool = app.tool.as_ref().is_none_or(|t| t.cmd == "sketch.line");
|
||||
if !mid_only
|
||||
&& best.is_none()
|
||||
@@ -421,6 +422,15 @@ pub fn refine_snap(
|
||||
{
|
||||
best = Some((i.dist, i.label(), i.at));
|
||||
}
|
||||
if !mid_only
|
||||
&& best.is_none()
|
||||
&& line_tool
|
||||
&& let Some(prev) = prev
|
||||
&& let Some(i) = crate::inference::through_center(sk, prev, lp, tol)
|
||||
{
|
||||
best = Some((i.dist, i.label(), i.at));
|
||||
mid_of = i.arg;
|
||||
}
|
||||
// On a curve (coincident with it).
|
||||
if !mid_only && best.is_none() {
|
||||
for (i, c) in sk.curves.iter().enumerate() {
|
||||
@@ -462,6 +472,8 @@ pub fn refine_snap(
|
||||
let arg = match l {
|
||||
"Mid" => mid_of.map(|m| format!("mid:{m}")),
|
||||
"On" => mid_of.map(|m| format!("on:{m}")),
|
||||
// Already a point argument (`<id>.center` or `on:<id>:x,y`), or none for a free point.
|
||||
"Ctr" => mid_of,
|
||||
_ => None,
|
||||
};
|
||||
(p, arg)
|
||||
|
||||
Reference in New Issue
Block a user