* Extend: join a stop that is already another curve's end
sketch.extend refused a target that coincided with another curve's end
("the extension target coincides with existing geometry") instead of
extending onto that point. The stop is now joined: a new line ends on the
existing point, and a free end grows onto it held by a Coincident. The
preview shows that stop too, so the line it draws is the line the click adds.
A stop at the end of a spline or conic joins that end the same way, and
extending an infinite line now refuses instead of moving one of its two
points.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
* Extend: close an arc, take an equal stop, preview with the command's search
An arc with nothing on its path now grows all the way to its own far end and
closes into a whole circle instead of reporting that there is nothing to
extend to.
Two curves stopping the extension equally far are both on the way, so the first
stop is taken instead of refusing to choose between them.
The line preview and the command now share one stop search (`line_aim`), so the
preview can no longer draw a line the click refuses.
---------
Co-authored-by: n0tfr3shm1lk <n0tfr3shm1lk@ukr.net>
Co-authored-by: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -98,6 +98,22 @@ fn trim_and_extend_use_the_whole_line_but_never_trim_it() {
|
||||
assert!(sk.point(b).unwrap().dist(Vec2::new(25.0, 10.0)) < 1e-6, "{:?}", sk.point(b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extend_refuses_it_instead_of_moving_it() {
|
||||
// An infinite line has no end to grow from: moving one of its two points would move the whole
|
||||
// line, so the command says why and leaves it alone.
|
||||
let mut s = new_sketch();
|
||||
run(&mut s, "sketch.line", json!({"points": [[25, 50], [25, 60]], "ids": ["inf"]}));
|
||||
run(&mut s, "sketch.infinite", json!({"curves": ["inf"]}));
|
||||
let before = s.doc.to_json();
|
||||
let e = s.execute("sketch.extend", &json!({"curve": "inf", "at": [25, 50]})).unwrap_err().to_string();
|
||||
assert!(e.contains("infinite"), "{e}");
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
let sk = sketch(&s);
|
||||
let ci = sk.curve_index("inf").expect("the infinite line");
|
||||
assert!(crate::cmd::extend_preview(&sk, ci, Vec2::new(25.0, 50.0)).is_none(), "no preview to offer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fillets_and_chamfers_refuse_it_and_mirrors_copy_it() {
|
||||
let mut s = new_sketch();
|
||||
|
||||
@@ -70,7 +70,7 @@ pub static COMMANDS: &[CommandSpec] = &[
|
||||
.at("SKETCH", "MODIFY")
|
||||
.icon("extend")
|
||||
.enabled(in_sketch)
|
||||
.params("curve: id, at: [x,y] near the end to extend (to the nearest curve)"),
|
||||
.params("curve: id, at: [x,y] near the end to extend (a line runs on to the nearest curve, or the previewed reach when nothing is in the way; an arc grows along its circle and closes into a whole circle when nothing stops it)"),
|
||||
CommandSpec::new("sketch.break", "Break", break_cmd)
|
||||
.at("SKETCH", "MODIFY")
|
||||
.icon("sketch_break")
|
||||
@@ -182,10 +182,7 @@ fn cut_points(sk: &Sketch, ci: usize) -> Vec<(f64, Vec2, usize)> {
|
||||
fn pin(sk: &mut Sketch, p: usize, cutter: usize) -> Result<()> {
|
||||
let at = sk.point(p).unwrap_or_default();
|
||||
// An end point of the cutter there: coincident with it; otherwise on the curve.
|
||||
let ends: Vec<usize> = match sk.curves.get(cutter).map(|c| c.kind.clone()) {
|
||||
Some(CurveKind::Line { a, b }) | Some(CurveKind::Arc { a, b, .. }) => vec![a, b],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let ends: Vec<usize> = sk.curves.get(cutter).and_then(|c| c.kind.ends()).map(|(a, b)| vec![a, b]).unwrap_or_default();
|
||||
if let Some(e) = ends.into_iter().find(|e| sk.point(*e).is_some_and(|q| q.dist(at) < TOL * 10.0)) {
|
||||
if e != p {
|
||||
add_c(sk, ConstraintKind::Coincident { p, q: e })?;
|
||||
@@ -606,208 +603,99 @@ fn break_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// of nothing.
|
||||
const EXTEND_REACH: f64 = 50.0;
|
||||
|
||||
/// Preview the extend operation without modifying the sketch.
|
||||
/// Returns the hit point that would be reached if the user extends the curve,
|
||||
/// or None if extension is not possible (blocked by collinear curves).
|
||||
pub fn extend_preview(sk: &solidcraft_sketch::Sketch, ci: usize, hover_pos: Vec2) -> Option<Vec2> {
|
||||
use solidcraft_sketch::{CurveKind, Shape};
|
||||
/// One end of a line growing along its own support, and what stops it.
|
||||
///
|
||||
/// A line extends past either of its ends, running on along its own straight support until the
|
||||
/// first geometry in the way. `sketch.extend` and its preview both take this aim, so the preview
|
||||
/// draws the line the click adds, never one the command refuses.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct LineAim {
|
||||
/// The end that grows.
|
||||
end: usize,
|
||||
/// The point the extension stops at: the first geometry on the way, or the reach past the end
|
||||
/// when nothing is in the way.
|
||||
hit: Vec2,
|
||||
/// The curve that stops it there, and the stop as the sketch's own point when the sketch
|
||||
/// already has one there.
|
||||
cutter: Option<usize>,
|
||||
stop: Option<usize>,
|
||||
}
|
||||
|
||||
let curve = sk.curves.get(ci)?;
|
||||
let (a, b) = match &curve.kind {
|
||||
CurveKind::Line { a: pa, b: pb } => (*pa, *pb),
|
||||
CurveKind::Arc { a: pa, b: pb, .. } => (*pa, *pb),
|
||||
_ => return None,
|
||||
};
|
||||
let pa = sk.point(a)?;
|
||||
let pb = sk.point(b)?;
|
||||
let at_b = hover_pos.dist(pb) <= hover_pos.dist(pa);
|
||||
let end = if at_b { b } else { a };
|
||||
let end_pt = sk.point(end)?;
|
||||
let other = if at_b { pa } else { pb };
|
||||
/// The first geometry a line meets when it grows past the end of `ci` nearest `at`, or the reach
|
||||
/// past that end when nothing is in the way. Two curves stopping it equally far are one stop: the
|
||||
/// first of them is taken.
|
||||
fn line_aim(sk: &Sketch, ci: usize, at: Vec2) -> Result<Option<LineAim>> {
|
||||
let cmd = "sketch.extend";
|
||||
// An infinite line has no end to grow: moving either of its points moves the whole line.
|
||||
let Some(curve) = sk.curves.get(ci).filter(|c| !c.infinite) else { return Ok(None) };
|
||||
let CurveKind::Line { a: pa, b: pb } = curve.kind else { return Ok(None) };
|
||||
let sh = sk.shape(ci).ok_or_else(|| bad(cmd, "curve"))?;
|
||||
let (qa, qb) = (sk.point(pa).unwrap_or_default(), sk.point(pb).unwrap_or_default());
|
||||
let at_b = at.dist(qb) <= at.dist(qa);
|
||||
let (end, end_pt) = if at_b { (pb, qb) } else { (pa, qa) };
|
||||
let dir = (end_pt - if at_b { qa } else { qb }).normalized().ok_or_else(|| bad(cmd, "cannot determine extension direction"))?;
|
||||
|
||||
// Get the shape (line or arc) of the curve to extend
|
||||
let sh = sk.shape(ci)?;
|
||||
let unbounded = match sh {
|
||||
Shape::Line { .. } => sh,
|
||||
Shape::Round { c, r, start, .. } => Shape::Round { c, r, start, sweep: std::f64::consts::TAU },
|
||||
};
|
||||
|
||||
// Quick check for collinearity blocking by neighbors
|
||||
for other_curve in sk.curves.iter() {
|
||||
if let CurveKind::Line { a: na, b: nb } = &other_curve.kind
|
||||
&& (*na == end || *nb == end)
|
||||
{
|
||||
let other_idx = if *na == end { *nb } else { *na };
|
||||
if let Some(other_pos) = sk.point(other_idx) {
|
||||
let dir = (end_pt - other).normalized()?;
|
||||
let v = other_pos - end_pt;
|
||||
// Collinear and in the same direction: this blocks extension.
|
||||
if v.cross(dir).abs() < 1e-6 && v.dot(dir) > 1e-6 {
|
||||
return None;
|
||||
}
|
||||
// A neighbour running on the same way as the extension has it nowhere to go.
|
||||
for (oi, c) in sk.curves.iter().enumerate() {
|
||||
if oi == ci || !uses(c, end) {
|
||||
continue;
|
||||
}
|
||||
// Arcs do not block a line: the line runs past the point they share.
|
||||
if let CurveKind::Line { a: na, b: nb } = c.kind {
|
||||
let other_idx = if na == end { nb } else { na };
|
||||
let v = sk.point(other_idx).unwrap_or_default() - end_pt;
|
||||
if v.cross(dir).abs() < 1e-6 && v.dot(dir) > 1e-6 {
|
||||
return Err(bad(cmd, "that end is connected to other geometry"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now search for actual hit points using the same algorithm as extend
|
||||
let mut best_gap = f64::MAX;
|
||||
let mut best_hit: Option<Vec2> = None;
|
||||
let mut budget = 500_000usize; // Reduced budget for preview
|
||||
let tol_preview = 1e-7;
|
||||
|
||||
// Get the extension direction
|
||||
let dir = (end_pt - other).normalized().unwrap_or_default();
|
||||
|
||||
for (oi, other_curve) in sk.curves.iter().enumerate() {
|
||||
// Candidate stops on the line's unbounded support beyond the chosen end.
|
||||
let length = qa.dist(qb);
|
||||
let mut best: Option<(f64, Vec2, usize)> = None;
|
||||
let mut budget = 1_000_000usize;
|
||||
let mut segment_count = 0usize;
|
||||
for (oi, other) in sk.curves.iter().enumerate() {
|
||||
if oi == ci {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip curves that share the endpoint we're extending from
|
||||
let shares_endpoint = match &other_curve.kind {
|
||||
CurveKind::Line { a, b } => *a == end || *b == end,
|
||||
CurveKind::Arc { a, b, .. } => *a == end || *b == end,
|
||||
_ => false,
|
||||
};
|
||||
if shares_endpoint {
|
||||
// Check for collinearity blocking (already checked above, but re-check here)
|
||||
if let CurveKind::Line { a: na, b: nb } = &other_curve.kind {
|
||||
let other_idx = if *na == end { *nb } else { *na };
|
||||
if let Some(other_pos) = sk.point(other_idx) {
|
||||
let v = other_pos - end_pt;
|
||||
let cross = v.cross(dir);
|
||||
let dot = v.dot(dir);
|
||||
if cross.abs() < 1e-6 && dot > 1e-6 {
|
||||
// Collinear and in same direction - blocks extension
|
||||
continue; // Skip this curve for hit finding
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut pieces = sk.exact_segs(oi).unwrap_or_else(|| sk.segs(oi));
|
||||
// An infinite line (#1359) is a boundary along its whole line.
|
||||
if other.infinite {
|
||||
pieces = pieces.iter().map(|s| support::endless(s, sh.point(sh.param(at)))).collect();
|
||||
}
|
||||
|
||||
let pieces = sk.exact_segs(oi).unwrap_or_else(|| sk.segs(oi));
|
||||
|
||||
// For infinite lines, extend segments
|
||||
let pieces_to_check: Vec<_> =
|
||||
if other_curve.infinite { pieces.iter().map(|s| support::endless(s, end_pt)).collect() } else { pieces.to_vec() };
|
||||
|
||||
for piece in pieces_to_check {
|
||||
// Try to get hits - hits() will return empty vec or error for unsupported types
|
||||
if let Ok(hits) = support::hits(&unbounded, &piece, &mut budget) {
|
||||
for q in hits {
|
||||
// Check if hit is beyond the current end
|
||||
let t = sh.param(q);
|
||||
let gap = match sh {
|
||||
Shape::Line { a, b } => {
|
||||
let l = a.dist(b);
|
||||
if at_b { (t - 1.0) * l } else { -t * l }
|
||||
}
|
||||
Shape::Round { r, .. } => {
|
||||
let (_, t1) = sh.range();
|
||||
if t <= t1 + tol_preview / r.max(1e-12) || t >= TAU - tol_preview / r.max(1e-12) {
|
||||
continue;
|
||||
}
|
||||
if at_b { (t - t1) * r } else { (TAU - t) * r }
|
||||
}
|
||||
};
|
||||
// Only consider hits that are further than tolerance
|
||||
if gap > tol_preview * 10.0 && gap < best_gap {
|
||||
// CRITICAL CHECK: Verify no other curve lies between end_pt and q along the extension direction
|
||||
let mut blocked = false;
|
||||
for (oi2, _check_curve) in sk.curves.iter().enumerate() {
|
||||
if oi2 == ci {
|
||||
continue;
|
||||
}
|
||||
if oi2 == oi {
|
||||
continue;
|
||||
} // Skip the curve we just found hit on
|
||||
|
||||
// Check if any segment of this curve lies between end_pt and q
|
||||
let segs = sk.exact_segs(oi2).unwrap_or_else(|| sk.segs(oi2));
|
||||
for seg in segs {
|
||||
// Get the endpoints of this segment
|
||||
let (sa, sb) = match seg {
|
||||
solidcraft_geom::Seg2::Line { a: sa, b: sb } => (sa, sb),
|
||||
solidcraft_geom::Seg2::Arc { center, radius, start, sweep } => {
|
||||
// For arcs, check the start and end points
|
||||
let angle = start + sweep;
|
||||
(
|
||||
center + Vec2::new(radius * start.cos(), radius * start.sin()),
|
||||
center + Vec2::new(radius * angle.cos(), radius * angle.sin()),
|
||||
)
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
// Check if either endpoint lies between end_pt and q along the extension direction
|
||||
let vec_to_sa = sa - end_pt;
|
||||
let proj_sa = vec_to_sa.dot(dir);
|
||||
if proj_sa > tol_preview && proj_sa < gap - tol_preview {
|
||||
// Point sa projects onto the extension path between start and hit
|
||||
// Check if it's close to the line (within tolerance)
|
||||
let perp_dist = (sa - end_pt).cross(dir).abs();
|
||||
if perp_dist < tol_preview * 10.0 {
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let vec_to_sb = sb - end_pt;
|
||||
let proj_sb = vec_to_sb.dot(dir);
|
||||
if proj_sb > tol_preview && proj_sb < gap - tol_preview {
|
||||
let perp_dist = (sb - end_pt).cross(dir).abs();
|
||||
if perp_dist < tol_preview * 10.0 {
|
||||
blocked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if blocked {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if blocked {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !blocked {
|
||||
best_gap = gap;
|
||||
best_hit = Some(q);
|
||||
}
|
||||
}
|
||||
segment_count += pieces.len();
|
||||
if segment_count > 16_384 {
|
||||
return Err(bad(cmd, "too many exact curve pieces to extend safely"));
|
||||
}
|
||||
for piece in &pieces {
|
||||
for q in support::hits(&sh, piece, &mut budget)? {
|
||||
let t = sh.param(q);
|
||||
let travel = if at_b { (t - 1.0) * length } else { -t * length };
|
||||
if travel <= TOL * 10.0 {
|
||||
continue;
|
||||
}
|
||||
// Equally far stops are both on the way: the first one found is the stop.
|
||||
if best.is_none_or(|x| travel < x.0 - TOL) {
|
||||
best = Some((travel, q, oi));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CRITICAL CHECK 3: Verify no other curve endpoint coincides with the hit point.
|
||||
// If another curve already ends at this location, we should not create a new overlapping line.
|
||||
if let Some(hit) = best_hit {
|
||||
for (oi2, _other_curve) in sk.curves.iter().enumerate() {
|
||||
if oi2 == ci {
|
||||
continue;
|
||||
}
|
||||
let (hit, cutter, stop) = match best {
|
||||
Some((_, q, oi)) => (q, Some(oi), stop_point(sk, ci, q)),
|
||||
None => (end_pt + dir * EXTEND_REACH, None, None),
|
||||
};
|
||||
Ok(Some(LineAim { end, hit, cutter, stop }))
|
||||
}
|
||||
|
||||
// Get endpoints of the other curve
|
||||
let ends = match &sk.curves.get(oi2).map(|c| &c.kind) {
|
||||
Some(CurveKind::Line { a, b }) => [a, b],
|
||||
Some(CurveKind::Arc { a, b, .. }) => [a, b],
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
for &end_idx in &ends {
|
||||
if let Some(pt) = sk.point(*end_idx) {
|
||||
// If an existing point is at the hit location (within tolerance), block extension.
|
||||
if pt.dist(hit) < tol_preview * 10.0 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
best_hit.or(Some(end_pt + dir * EXTEND_REACH))
|
||||
/// Preview the extend operation without modifying the sketch: the point the line past the end
|
||||
/// nearest `hover_pos` reaches, from the same search `sketch.extend` makes. `None` when the curve
|
||||
/// is not a line to extend, when it is an infinite line, or when the extension has nowhere to go
|
||||
/// (the end is connected to a line running on from it).
|
||||
pub fn extend_preview(sk: &solidcraft_sketch::Sketch, ci: usize, hover_pos: Vec2) -> Option<Vec2> {
|
||||
line_aim(sk, ci, hover_pos).ok().flatten().map(|aim| aim.hit)
|
||||
}
|
||||
|
||||
/// One end of an arc growing along the arc's own circle, as a new arc, and what stops it.
|
||||
@@ -843,10 +731,11 @@ impl ArcAim {
|
||||
}
|
||||
}
|
||||
|
||||
/// The first geometry an arc meets when it grows from the end of `ci` nearest `at`, or `None`
|
||||
/// when nothing is in the way. Both ends grow: past `b` counter-clockwise, past `a` clockwise,
|
||||
/// and only as far as the nearest curve on that path stops them. Two curves stopping it at the
|
||||
/// same distance is an error, not a guess.
|
||||
/// The first geometry an arc meets when it grows from the end of `ci` nearest `at`. Both ends
|
||||
/// grow: past `b` counter-clockwise, past `a` clockwise, and only as far as the nearest curve on
|
||||
/// that path stops them. With nothing in the way the extension runs on to the arc's own far end,
|
||||
/// closing the arc into a full circle. Two curves stopping it equally far are one stop: the first
|
||||
/// is taken.
|
||||
fn arc_aim(sk: &Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<Option<ArcAim>> {
|
||||
let Some(CurveKind::Arc { a, b, .. }) = sk.curves.get(ci).map(|c| c.kind.clone()) else { return Ok(None) };
|
||||
let Some(Shape::Round { c: centre, r, start, sweep }) = sk.shape(ci) else { return Ok(None) };
|
||||
@@ -867,7 +756,6 @@ fn arc_aim(sk: &Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<Option<ArcAim>
|
||||
let far_tol = TOL * 10.0 / r;
|
||||
let circle = Shape::Round { c: centre, r, start: 0.0, sweep: TAU };
|
||||
let mut best: Option<(f64, Vec2, usize)> = None;
|
||||
let mut ambiguous = false;
|
||||
let mut budget = 1_000_000usize;
|
||||
let mut segment_count = 0usize;
|
||||
for (oi, curve) in sk.curves.iter().enumerate() {
|
||||
@@ -895,7 +783,7 @@ fn arc_aim(sk: &Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<Option<ArcAim>
|
||||
continue;
|
||||
}
|
||||
let entry = centre + Vec2::from_angle(entry) * r;
|
||||
keep_stop(&mut best, &mut ambiguous, travel, entry, oi, angle_tol);
|
||||
keep_stop(&mut best, travel, entry, oi, angle_tol);
|
||||
continue;
|
||||
}
|
||||
let mut pieces = sk.exact_segs(oi).unwrap_or_else(|| sk.segs(oi));
|
||||
@@ -915,24 +803,22 @@ fn arc_aim(sk: &Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<Option<ArcAim>
|
||||
if travel <= angle_tol || travel > reach + far_tol {
|
||||
continue;
|
||||
}
|
||||
keep_stop(&mut best, &mut ambiguous, travel, q, oi, angle_tol);
|
||||
keep_stop(&mut best, travel, q, oi, angle_tol);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ambiguous {
|
||||
return Err(bad(cmd, "multiple curves stop the extension at the same distance"));
|
||||
}
|
||||
Ok(best.map(|(travel, hit, cutter)| ArcAim { end, far, at_b, centre, radius: r, end_angle, travel, hit, cutter }))
|
||||
// Nothing stops the arc on its way round: it grows all the way to its own far end, closing
|
||||
// the gap between its ends into a full circle.
|
||||
let far_pt = if at_b { qa } else { qb };
|
||||
let (travel, hit, cutter) = best.unwrap_or((reach, far_pt, ci));
|
||||
Ok(Some(ArcAim { end, far, at_b, centre, radius: r, end_angle, travel, hit, cutter }))
|
||||
}
|
||||
|
||||
/// Keep the stop the nearest of the candidates, and remember it when another curve stops the
|
||||
/// extension at the same distance somewhere else; the same point from several curves is one stop.
|
||||
fn keep_stop(best: &mut Option<(f64, Vec2, usize)>, ambiguous: &mut bool, travel: f64, q: Vec2, oi: usize, angle_tol: f64) {
|
||||
/// Keep the stop the nearest of the candidates. Two curves stopping the extension equally far are
|
||||
/// both on the way, so the first one found is taken instead of refusing to choose.
|
||||
fn keep_stop(best: &mut Option<(f64, Vec2, usize)>, travel: f64, q: Vec2, oi: usize, angle_tol: f64) {
|
||||
if best.is_none_or(|x| travel < x.0 - angle_tol) {
|
||||
*best = Some((travel, q, oi));
|
||||
*ambiguous = false;
|
||||
} else if best.is_some_and(|x| (travel - x.0).abs() <= angle_tol && x.1.dist(q) > TOL) {
|
||||
*ambiguous = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -948,11 +834,8 @@ fn arc_stop(sk: &Sketch, ci: usize, aim: &ArcAim) -> Option<usize> {
|
||||
return Some(aim.far);
|
||||
}
|
||||
sk.curves.iter().enumerate().filter(|(i, _)| *i != ci).find_map(|(_, c)| {
|
||||
let ends = match c.kind {
|
||||
CurveKind::Line { a, b } | CurveKind::Arc { a, b, .. } => [a, b],
|
||||
_ => return None,
|
||||
};
|
||||
ends.into_iter().find(|p| at_hit(*p))
|
||||
let (a, b) = c.kind.ends()?;
|
||||
[a, b].into_iter().find(|p| at_hit(*p))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -975,18 +858,27 @@ fn add_arc_extension(sk: &mut Sketch, ci: usize, aim: &ArcAim, stop: Option<usiz
|
||||
Ok(sk.points.get(stop).map(|x| x.id.clone()).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// Whether `q` already is another curve's end: extending there would double geometry that is
|
||||
/// there, so extend refuses instead of adding an overlapping curve.
|
||||
fn ends_at_curve_end(sk: &Sketch, ci: usize, q: Vec2) -> bool {
|
||||
sk.curves.iter().enumerate().filter(|(i, _)| *i != ci).any(|(_, c)| {
|
||||
let ends = match &c.kind {
|
||||
CurveKind::Line { a, b } | CurveKind::Arc { a, b, .. } => [*a, *b],
|
||||
_ => return false,
|
||||
};
|
||||
ends.iter().any(|e| sk.point(*e).is_some_and(|pt| pt.dist(q) < TOL * 10.0))
|
||||
/// The point the extension stops at when the sketch already has one there: an end of another
|
||||
/// curve at the hit. Joining that point keeps the extension attached to the geometry that is
|
||||
/// there instead of laying a second point over it. `None` when the stop needs a new point.
|
||||
fn stop_point(sk: &Sketch, ci: usize, q: Vec2) -> Option<usize> {
|
||||
sk.curves.iter().enumerate().filter(|(i, _)| *i != ci).find_map(|(_, c)| {
|
||||
let (a, b) = c.kind.ends()?;
|
||||
[a, b].into_iter().find(|p| sk.point(*p).is_some_and(|pt| pt.dist(q) <= TOL * 10.0))
|
||||
})
|
||||
}
|
||||
|
||||
/// Add the line an extension needs, from the growing end to the stop: the sketch's own point
|
||||
/// when the stop already is one, a new point otherwise.
|
||||
fn add_line_extension(sk: &mut Sketch, end: usize, q: Vec2, stop: Option<usize>) -> Result<String> {
|
||||
let b = match stop {
|
||||
Some(p) => p,
|
||||
None => sk.add_point(q, None)?,
|
||||
};
|
||||
sk.add_curve(CurveKind::Line { a: end, b }, None)?;
|
||||
Ok(sk.points.get(b).map(|x| x.id.clone()).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// Whether the growing end of an arc already carries geometry: another curve starts there, or a
|
||||
/// constraint holds the point. Such an end keeps its place, so the extension has to be new
|
||||
/// geometry.
|
||||
@@ -1011,7 +903,7 @@ fn arc_end_held(sk: &Sketch, ci: usize, end: usize) -> bool {
|
||||
fn extend_arc(sk: &mut Sketch, ci: usize, at: Vec2) -> Result<String> {
|
||||
let cmd = "sketch.extend";
|
||||
let Some(aim) = arc_aim(sk, ci, at, cmd)? else {
|
||||
return Err(bad(cmd, "nothing to extend to"));
|
||||
return Err(bad(cmd, "an arc that is already a whole circle has no end to extend"));
|
||||
};
|
||||
let stop = arc_stop(sk, ci, &aim);
|
||||
// The arc's own far end is where the arc begins again: moving the end onto it would leave a
|
||||
@@ -1036,14 +928,21 @@ fn extend_arc(sk: &mut Sketch, ci: usize, at: Vec2) -> Result<String> {
|
||||
}
|
||||
return add_arc_extension(sk, ci, &aim, stop);
|
||||
}
|
||||
// Held to what stopped it, the end stays on the geometry it reached.
|
||||
pin(sk, aim.end, aim.cutter)?;
|
||||
// Held to the point that is already there or to what else stopped it, the end stays on the
|
||||
// geometry it reached.
|
||||
if let Some(stop) = stop {
|
||||
if stop != aim.end {
|
||||
add_c(sk, ConstraintKind::Coincident { p: aim.end, q: stop })?;
|
||||
}
|
||||
} else {
|
||||
pin(sk, aim.end, aim.cutter)?;
|
||||
}
|
||||
Ok(sk.points.get(aim.end).map(|x| x.id.clone()).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// The arc `sketch.extend` would add from the end of `ci` nearest `hover_pos`, for the viewport to
|
||||
/// show before the click: the new arc as points, the growing end first. `None` when extending
|
||||
/// there is not possible (nothing on the way stops it, or stopping it is ambiguous).
|
||||
/// show before the click: the new arc as points, the growing end first. `None` when the arc cannot
|
||||
/// grow (it is already a whole circle).
|
||||
pub fn extend_arc_preview(sk: &solidcraft_sketch::Sketch, ci: usize, hover_pos: Vec2) -> Option<Vec<Vec2>> {
|
||||
let aim = arc_aim(sk, ci, hover_pos, "sketch.extend").ok().flatten()?;
|
||||
Some(aim.points())
|
||||
@@ -1059,105 +958,29 @@ fn extend(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let ci = curve_arg(sk, p, cmd)?;
|
||||
refuse_offset_ends(sk, ci, "extend", cmd)?;
|
||||
// An infinite line already reaches across the view: moving one of its two points would
|
||||
// move the whole line instead of extending it.
|
||||
if sk.curves.get(ci).is_some_and(|c| c.infinite) {
|
||||
return Err(bad(cmd, "an infinite line has no end to extend: make it a normal line first"));
|
||||
}
|
||||
// An arc grows along its own circle, either way round: in place when its end is free, as
|
||||
// a new arc when the end already carries geometry.
|
||||
if matches!(sk.curves.get(ci).map(|c| &c.kind), Some(CurveKind::Arc { .. })) {
|
||||
return extend_arc(sk, ci, at);
|
||||
}
|
||||
let sh = sk.shape(ci).ok_or_else(|| bad(cmd, "curve"))?;
|
||||
let Some(CurveKind::Line { a: pa, b: pb }) = sk.curves.get(ci).map(|c| c.kind.clone()) else {
|
||||
// The line grows past one end, out to the first geometry on the way, or the reach the
|
||||
// preview draws when nothing is in the way. A stop where the sketch already has a point
|
||||
// (another curve's end) joins that point instead of doubling it.
|
||||
let Some(aim) = line_aim(sk, ci, at)? else {
|
||||
return Err(bad(cmd, "only lines and arcs can be extended"));
|
||||
};
|
||||
let (qa, qb) = (sk.point(pa).unwrap_or_default(), sk.point(pb).unwrap_or_default());
|
||||
let at_b = at.dist(qb) <= at.dist(qa);
|
||||
let end = if at_b { pb } else { pa };
|
||||
let end = aim.end;
|
||||
let (q, cutter, stop) = (aim.hit, aim.cutter, aim.stop);
|
||||
let neighbors: Vec<_> = sk.curves.iter().enumerate().filter(|(i, c)| *i != ci && uses(c, end)).collect();
|
||||
|
||||
// Fixed ends are allowed: an extension that cannot move an end adds a new line from it,
|
||||
// so the fixed point itself never moves (a diagonal ending at the origin can be
|
||||
// continued through it).
|
||||
let extension_direction = {
|
||||
let p_end = sk.point(end).unwrap_or_default();
|
||||
let p_other = if at_b { qa } else { qb };
|
||||
(p_end - p_other).normalized()
|
||||
}
|
||||
.ok_or_else(|| bad(cmd, "cannot determine extension direction"))?;
|
||||
|
||||
// A neighbour running on the same way as the extension has it nowhere to go.
|
||||
for (_, c) in &neighbors {
|
||||
// Arcs do not block a line: the line runs past the point they share.
|
||||
if matches!(c.kind, CurveKind::Arc { .. }) {
|
||||
continue;
|
||||
}
|
||||
if let CurveKind::Line { a: na, b: nb } = c.kind {
|
||||
let other_idx = if na == end { nb } else { na };
|
||||
let other_pos = sk.point(other_idx).unwrap_or_default();
|
||||
// From the shared point to the neighbour's other end.
|
||||
let v = other_pos - sk.point(end).unwrap_or_default();
|
||||
if v.cross(extension_direction).abs() < 1e-6 && v.dot(extension_direction) > 1e-6 {
|
||||
return Err(bad(cmd, "that end is connected to other geometry"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Candidate stops on the line's unbounded support beyond the chosen end.
|
||||
let length = qa.dist(qb);
|
||||
let mut best: Option<(f64, Vec2, usize)> = None;
|
||||
let mut ambiguous = false;
|
||||
let mut budget = 1_000_000usize;
|
||||
let mut segment_count = 0usize;
|
||||
for (oi, curve) in sk.curves.iter().enumerate() {
|
||||
if oi == ci {
|
||||
continue;
|
||||
}
|
||||
let mut pieces = sk.exact_segs(oi).unwrap_or_else(|| sk.segs(oi));
|
||||
// An infinite line (#1359) is a boundary along its whole line.
|
||||
if curve.infinite {
|
||||
pieces = pieces.iter().map(|s| support::endless(s, sh.point(sh.param(at)))).collect();
|
||||
}
|
||||
segment_count += pieces.len();
|
||||
if segment_count > 16_384 {
|
||||
return Err(bad(cmd, "too many exact curve pieces to extend safely"));
|
||||
}
|
||||
for piece in &pieces {
|
||||
for q in support::hits(&sh, piece, &mut budget)? {
|
||||
let t = sh.param(q);
|
||||
let gap = if at_b { (t - 1.0) * length } else { -t * length };
|
||||
if gap <= TOL * 10.0 {
|
||||
continue;
|
||||
}
|
||||
if best.is_none_or(|x| gap < x.0 - TOL) {
|
||||
best = Some((gap, q, oi));
|
||||
ambiguous = false;
|
||||
} else if best.is_some_and(|x| (gap - x.0).abs() <= TOL && (x.2 != oi || x.1.dist(q) > TOL)) {
|
||||
ambiguous = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ambiguous {
|
||||
return Err(bad(cmd, "multiple curves stop the extension at the same distance"));
|
||||
}
|
||||
|
||||
// Nothing in the way stops the extension: it runs as far as the preview draws, so the
|
||||
// click adds the line the user saw rather than reporting that there is nothing to reach.
|
||||
let (q, cutter) = match best {
|
||||
Some((_, q, oi)) => {
|
||||
// A target that is already another curve's end would double that geometry.
|
||||
if ends_at_curve_end(sk, ci, q) {
|
||||
return Err(bad(cmd, "the extension target coincides with existing geometry"));
|
||||
}
|
||||
(q, Some(oi))
|
||||
}
|
||||
None => (sk.point(end).unwrap_or_default() + extension_direction * EXTEND_REACH, None),
|
||||
};
|
||||
|
||||
// The end joined to other geometry stays where it is: add a line from it.
|
||||
if !neighbors.is_empty() {
|
||||
let np = sk.add_point(q, None)?;
|
||||
sk.add_curve(CurveKind::Line { a: end, b: np }, None)?;
|
||||
return Ok(sk.points.get(np).map(|x| x.id.clone()).unwrap_or_default());
|
||||
return add_line_extension(sk, end, q, stop);
|
||||
}
|
||||
|
||||
// An end held by a constraint, or lying on a circle or arc, stays where it is too: the
|
||||
@@ -1185,32 +1008,37 @@ fn extend(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
|
||||
if end_is_held {
|
||||
let np = sk.add_point(q, None)?;
|
||||
sk.add_curve(CurveKind::Line { a: end, b: np }, None)?;
|
||||
return Ok(sk.points.get(np).map(|x| x.id.clone()).unwrap_or_default());
|
||||
return add_line_extension(sk, end, q, stop);
|
||||
}
|
||||
|
||||
// A free end moves along instead, if the solver lets it.
|
||||
if let Some(pt) = sk.points.get_mut(end) {
|
||||
// A free end moves along instead, if the solver lets it. The attempt is made on a copy,
|
||||
// so an end the solver cannot put where it belongs leaves the sketch as it was for the
|
||||
// new line below.
|
||||
let mut attempt = sk.clone();
|
||||
if let Some(pt) = attempt.points.get_mut(end) {
|
||||
pt.pos = q;
|
||||
}
|
||||
// Held to what stopped it, an end stays where the extension put it; an end with nothing
|
||||
// to hold it is simply placed there.
|
||||
if let Some(cutter) = cutter {
|
||||
pin(sk, end, cutter)?;
|
||||
// Held to the point that is already there or to what else stopped it, an end stays where
|
||||
// the extension put it; an end with nothing to hold it is simply placed there.
|
||||
if let Some(stop) = stop {
|
||||
if stop != end {
|
||||
add_c(&mut attempt, ConstraintKind::Coincident { p: end, q: stop })?;
|
||||
}
|
||||
} else if let Some(cutter) = cutter {
|
||||
pin(&mut attempt, end, cutter)?;
|
||||
}
|
||||
let before = sk.points.iter().map(|p| p.pos).collect::<Vec<_>>();
|
||||
let hold = solidcraft_sketch::Hold { points: (0..sk.points.len()).filter(|i| *i != end).collect(), radii: (0..sk.curves.len()).collect() };
|
||||
let moved = solidcraft_sketch::solve_with(sk, &hold, None, solidcraft_sketch::Analysis::StatusOnly, None).ok()
|
||||
&& sk.point(end).is_some_and(|point| point.dist(q) <= TOL)
|
||||
&& sk.points.iter().enumerate().all(|(i, point)| i == end || point.pos.dist(before[i]) <= TOL);
|
||||
let hold =
|
||||
solidcraft_sketch::Hold { points: (0..attempt.points.len()).filter(|i| *i != end).collect(), radii: (0..attempt.curves.len()).collect() };
|
||||
let moved = solidcraft_sketch::solve_with(&mut attempt, &hold, None, solidcraft_sketch::Analysis::StatusOnly, None).ok()
|
||||
&& attempt.point(end).is_some_and(|point| point.dist(q) <= TOL)
|
||||
&& attempt.points.iter().enumerate().all(|(i, point)| i == end || point.pos.dist(before[i]) <= TOL);
|
||||
if moved {
|
||||
*sk = attempt;
|
||||
return Ok(sk.points.get(end).map(|x| x.id.clone()).unwrap_or_default());
|
||||
}
|
||||
// The solve could not put the end where it belongs: add a new line instead.
|
||||
let np = sk.add_point(q, None)?;
|
||||
sk.add_curve(CurveKind::Line { a: end, b: np }, None)?;
|
||||
Ok(sk.points.get(np).map(|x| x.id.clone()).unwrap_or_default())
|
||||
add_line_extension(sk, end, q, stop)
|
||||
})?;
|
||||
Ok(json!({"moved": out, "sketch": info}))
|
||||
}
|
||||
|
||||
@@ -211,17 +211,47 @@ fn exact_extend_ellipse_preserves_support_target_and_history() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_extend_arc_rejects_interior_hit_atomically() {
|
||||
fn exact_extend_arc_closes_round_a_cutter_inside_its_own_sweep() {
|
||||
let mut s = new_sketch();
|
||||
let arc = ids(&run(&mut s, "sketch.arc.center_point", json!({"center":[0,0],"start":[10,0],"end":[0,10],"connect":false}))["curves"])[0].clone();
|
||||
let x = 50.0_f64.sqrt();
|
||||
// A cutter crossing the circle at 45°: inside the arc's own quarter, so it is not on the path
|
||||
// the extension takes round the other way.
|
||||
let cutter = ids(&run(&mut s, "sketch.line", json!({"points":[[x,5],[x,9]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
run(&mut s, "sketch.constraint.fix", json!({"entity":cutter}));
|
||||
let before = s.doc.to_json();
|
||||
let history = s.undo.len();
|
||||
assert!(s.execute("sketch.extend", &json!({"curve":arc,"at":[10,0]})).is_err());
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
assert_eq!(s.undo.len(), history);
|
||||
let before = sketch(&s);
|
||||
run(&mut s, "sketch.extend", json!({"curve":arc,"at":[10,0]}));
|
||||
let sk = sketch(&s);
|
||||
let ci = sk.curve_index(&arc).expect("the arc");
|
||||
// Nothing stops the extension, so the arc keeps its quarter...
|
||||
assert!(matches!(sk.shape(ci), Some(solidcraft_sketch::Shape::Round { sweep, .. }) if (sweep - std::f64::consts::FRAC_PI_2).abs() < 1e-9));
|
||||
// ...and a new arc runs from its far end back round to close the circle.
|
||||
assert_eq!(sk.curves.len(), before.curves.len() + 1);
|
||||
let sh = sk.shape(sk.curves.len() - 1).expect("the closing arc");
|
||||
assert!(matches!(sh, solidcraft_sketch::Shape::Round { sweep, .. } if (sweep - 3.0 * std::f64::consts::FRAC_PI_2).abs() < 1e-9), "{sh:?}");
|
||||
assert!(sh.dist(Vec2::new(10.0, 0.0)) < 1e-6 && sh.dist(Vec2::new(0.0, 10.0)) < 1e-6, "{sh:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arc_extend_with_nothing_in_the_way_closes_into_a_full_circle() {
|
||||
let mut s = new_sketch();
|
||||
let arc = ids(&run(&mut s, "sketch.arc.center_point", json!({"center":[0,0],"start":[10,0],"end":[0,10],"connect":false}))["curves"])[0].clone();
|
||||
let before = sketch(&s);
|
||||
let ci = before.curve_index(&arc).expect("the arc");
|
||||
assert!(profiles(&s).is_empty(), "an arc alone bounds nothing");
|
||||
// The preview shows the whole circle: the closing arc from the growing end to the far end.
|
||||
let preview = super::extend_arc_preview(&before, ci, Vec2::new(10.0, 0.0)).expect("a closing preview");
|
||||
assert!(preview.first().is_some_and(|p| p.dist(Vec2::new(10.0, 0.0)) < 1e-6), "the preview starts at the growing end: {preview:?}");
|
||||
assert!(preview.last().is_some_and(|p| p.dist(Vec2::new(0.0, 10.0)) < 1e-6), "and ends on the arc's far end: {preview:?}");
|
||||
|
||||
run(&mut s, "sketch.extend", json!({"curve": arc, "at": [10, 0]}));
|
||||
|
||||
let sk = sketch(&s);
|
||||
assert_eq!(sk.curves.len(), before.curves.len() + 1, "a new arc closes the circle");
|
||||
assert_eq!(sk.points.len(), before.points.len(), "it joins the arc's own far end instead of laying a second point over it");
|
||||
let area = profiles(&s);
|
||||
assert_eq!(area.len(), 1, "the two arcs bound one disc: {area:?}");
|
||||
assert!((area[0] - std::f64::consts::PI * 100.0).abs() < 1e-3, "{area:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -396,17 +426,105 @@ fn extend_with_nothing_in_the_way_adds_the_line_the_preview_shows() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_extend_refuses_ambiguous_stop_without_history_change() {
|
||||
fn line_extend_joins_a_stop_that_is_another_curves_end() {
|
||||
// The end being extended already carries geometry (a horizontal line meets it there), so the
|
||||
// extension is a new line. The first stop on the way is the end of another line: the new line
|
||||
// joins that point instead of refusing or laying a second point over it.
|
||||
let mut s = new_sketch();
|
||||
run(&mut s, "sketch.line", json!({"points":[[0,0],[5,0]],"connect":false,"infer":false}));
|
||||
let vertical = ids(&run(&mut s, "sketch.line", json!({"points":[[0,0],[0,45],[15,45]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
let top = ids(&run(&mut s, "sketch.line", json!({"points":[[0,47],[10,47]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
let before = sketch(&s);
|
||||
let stop = before.resolve_point(&format!("{top}.start")).expect("the top line's start");
|
||||
assert!(before.point(stop).is_some_and(|p| p.dist(Vec2::new(0.0, 47.0)) < 1e-9));
|
||||
let (curves, points) = (before.curves.len(), before.points.len());
|
||||
let ci = before.curve_index(&vertical).expect("the vertical line");
|
||||
let preview = super::extend_preview(&before, ci, Vec2::new(0.0, 45.0));
|
||||
|
||||
run(&mut s, "sketch.extend", json!({"curve": vertical, "at": [0,45]}));
|
||||
|
||||
assert!(preview.is_some_and(|q| q.dist(Vec2::new(0.0, 47.0)) < 1e-6), "the preview shows the line to the stop: {preview:?}");
|
||||
|
||||
let sk = sketch(&s);
|
||||
assert_eq!(sk.curves.len(), curves + 1, "the extension is a new line");
|
||||
assert_eq!(sk.points.len(), points, "the extension joins the point that is there");
|
||||
assert!(matches!(sk.curves[sk.curves.len() - 1].kind, CurveKind::Line { b, .. } if b == stop), "the new line ends on the existing point");
|
||||
|
||||
// A free end moves onto the point that is there, and is held to it instead of doubling it.
|
||||
let mut s = new_sketch();
|
||||
let vertical = ids(&run(&mut s, "sketch.line", json!({"points":[[0,0],[0,45]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
let top = ids(&run(&mut s, "sketch.line", json!({"points":[[0,47],[10,47]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
let before = sketch(&s);
|
||||
let stop = before.resolve_point(&format!("{top}.start")).expect("the top line's start");
|
||||
run(&mut s, "sketch.extend", json!({"curve": vertical, "at": [0,45]}));
|
||||
let sk = sketch(&s);
|
||||
assert_eq!((sk.curves.len(), sk.points.len()), (before.curves.len(), before.points.len()), "the line itself reaches the point");
|
||||
let end = sk.resolve_point(&format!("{vertical}.end")).expect("the line's end");
|
||||
assert!(sk.point(end).is_some_and(|p| p.dist(Vec2::new(0.0, 47.0)) < 1e-9), "{end}");
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Coincident { p, q } if (p, q) == (end, stop) || (p, q) == (stop, end))),
|
||||
"the end is held to the point that was already there"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extend_joins_the_end_of_a_freeform_curve() {
|
||||
// A line whose end is free grows onto a spline's start instead of laying a second point over
|
||||
// the spline's own end.
|
||||
let mut s = new_sketch();
|
||||
let line = ids(&run(&mut s, "sketch.line", json!({"points":[[0,0],[5,0]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
let spline = ids(&run(&mut s, "sketch.spline.fit_point", json!({"points":[[10,0],[15,5],[20,0]],"connect":false}))["curves"])[0].clone();
|
||||
let before = sketch(&s);
|
||||
let stop = before.resolve_point(&format!("{spline}.start")).expect("the spline's start");
|
||||
run(&mut s, "sketch.extend", json!({"curve": line, "at": [5,0]}));
|
||||
let sk = sketch(&s);
|
||||
assert_eq!((sk.curves.len(), sk.points.len()), (before.curves.len(), before.points.len()));
|
||||
let end = sk.resolve_point(&format!("{line}.end")).expect("the line's end");
|
||||
assert!(
|
||||
sk.point(end).zip(sk.point(stop)).is_some_and(|(a, b)| a.dist(b) < 1e-9),
|
||||
"the line ends on the spline's own point: {:?} / {:?}",
|
||||
sk.point(end),
|
||||
sk.point(stop)
|
||||
);
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Coincident { p, q } if (p, q) == (end, stop) || (p, q) == (stop, end))),
|
||||
"the end is held to the spline's point"
|
||||
);
|
||||
|
||||
// An arc whose end is free grows onto a conic's start the same way.
|
||||
let mut s = new_sketch();
|
||||
let arc = ids(&run(&mut s, "sketch.arc.center_point", json!({"center":[0,0],"start":[10,0],"end":[0,10],"connect":false}))["curves"])[0].clone();
|
||||
let conic =
|
||||
ids(&run(&mut s, "sketch.conic", json!({"start":[0,-10],"end":[-20,-10],"apex":[-10,-20],"rho":0.5,"connect":false}))["curves"])[0].clone();
|
||||
let before = sketch(&s);
|
||||
let stop = before.resolve_point(&format!("{conic}.start")).expect("the conic's start");
|
||||
run(&mut s, "sketch.extend", json!({"curve": arc, "at": [10,0]}));
|
||||
let sk = sketch(&s);
|
||||
assert_eq!((sk.curves.len(), sk.points.len()), (before.curves.len(), before.points.len()));
|
||||
let end = sk.resolve_point(&format!("{arc}.start")).expect("the arc's start");
|
||||
assert!(sk.point(end).is_some_and(|p| p.dist(Vec2::new(0.0, -10.0)) < 1e-6), "{end}");
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Coincident { p, q } if (p, q) == (end, stop) || (p, q) == (stop, end))),
|
||||
"the arc ends on the conic's own point"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_extend_takes_one_of_two_stops_at_the_same_distance() {
|
||||
// A vertical line and a diagonal cross the line's support at the same point: two curves stop
|
||||
// the extension equally far out. Either stop is the same point, so the extension takes it
|
||||
// instead of refusing to choose, and the preview shows the line the command adds.
|
||||
let mut s = new_sketch();
|
||||
let line = ids(&run(&mut s, "sketch.line", json!({"points":[[0,0],[5,0]],"connect":false,"infer":false}))["curves"])[0].clone();
|
||||
run(&mut s, "sketch.line", json!({"points":[[10,-5],[10,5]],"connect":false,"infer":false}));
|
||||
run(&mut s, "sketch.line", json!({"points":[[8,-2],[12,2]],"connect":false,"infer":false}));
|
||||
let before = s.doc.to_json();
|
||||
let history = s.undo.len();
|
||||
let error = s.execute("sketch.extend", &json!({"curve":"l1","at":[5,0]})).unwrap_err();
|
||||
assert!(error.to_string().contains("multiple curves"), "{error}");
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
assert_eq!(s.undo.len(), history);
|
||||
let before = sketch(&s);
|
||||
let ci = before.curve_index(&line).expect("the line");
|
||||
let preview = super::extend_preview(&before, ci, Vec2::new(5.0, 0.0)).expect("a preview");
|
||||
assert!(preview.dist(Vec2::new(10.0, 0.0)) < 1e-9, "{preview:?}");
|
||||
run(&mut s, "sketch.extend", json!({"curve":line,"at":[5,0]}));
|
||||
let sk = sketch(&s);
|
||||
let end = sk.point(sk.resolve_point(&format!("{line}.end")).expect("the line's end")).expect("a point");
|
||||
assert!(end.dist(Vec2::new(10.0, 0.0)) < 1e-6, "{end:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user