From 479dd2753526590e86d6696df475560f8eba9f68 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 20:42:08 -0800 Subject: [PATCH 1/2] Dimension tool: a line at a slight angle or past the distance limit is not a level A linear dimension line placed near another dimension's line lands on its level only when that line is parallel. Before, the check compared the two directions only, so a long line at a slight angle passed it: with its first end within the snapping tolerance, the new line landed on a level the other line had already left, and the guide ran at an angle between the lines. A line is now parallel when it moves off its level by no more than 0.000001 pt across the span of both lines. A level that would put the line more than 1,000,000 pt from its points is passed over. Before, the tool took it, the preview failed validation and was not sent, and a click committed the earlier preview position. The next level within reach is taken instead, or the line goes where the pointer is. The level guide also needs Smart Guides on when it is drawn, so turning Smart Guides off before the next pointer move hides it. The nonparallel case in the placement test now puts the pointer within reach of the first line's end, so it fails without the parallel check, and the guide test expects exactly one guide. --- crates/engine/tests/it/dimensions.rs | 16 ++++--- crates/tools/src/dimension.rs | 72 ++++++++++++++++++++++++++-- docs/dimensions.md | 6 ++- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/crates/engine/tests/it/dimensions.rs b/crates/engine/tests/it/dimensions.rs index 30f3ce2665..dc88673521 100644 --- a/crates/engine/tests/it/dimensions.rs +++ b/crates/engine/tests/it/dimensions.rs @@ -2077,18 +2077,19 @@ fn a_dimension_line_placed_beside_another_lands_on_its_level() { run(&mut s, "object.lock", json!({})); assert_eq!(level_next(&mut s, plain, 172.5), (pt(200.0, 172.5), pt(300.0, 172.5))); // A dimension of the right edge has a vertical line, which is not parallel to the first: the - // pointer 2.5 points below the first line's level places it where the pointer is. + // pointer 2.5 points right of the first line's left end, within reach across the vertical + // line, places it where the pointer is. let (mut s, _) = level_start(plain); linear_click(&mut s, plain, 300.0, 200.0); linear_click(&mut s, plain, 300.0, 300.0); - linear_click(&mut s, plain, 330.0, 172.5); + linear_click(&mut s, plain, 102.5, 172.5); let id = s.doc().unwrap().selection.objects[0].0; - assert_eq!(recipe(&s, id).line_ends(), (pt(330.0, 200.0), pt(330.0, 300.0))); + assert_eq!(recipe(&s, id).line_ends(), (pt(102.5, 200.0), pt(102.5, 300.0))); } /// While a dimension is placed with its line on the level of another, one alignment guide runs -/// along the level across both lines. Off the level, after a dropped press and with Alignment -/// Guides off, no guide shows. +/// along the level across both lines. Off the level, after a dropped press and with Smart Guides +/// or Alignment Guides off, no guide shows. #[test] fn a_dimension_line_on_another_level_shows_one_guide_across_both() { let plain = ViewInfo::default(); @@ -2097,7 +2098,10 @@ fn a_dimension_line_on_another_level_shows_one_guide_across_both() { linear_click(&mut s, plain, 200.0, 200.0); linear_click(&mut s, plain, 300.0, 200.0); let guides = guide_lines(&mut s, plain, 250.0, 172.5); - assert!(guides.iter().any(|(a, b)| near(*a, pt(100.0, 170.0)) && near(*b, pt(300.0, 170.0))), "{guides:?}"); + assert!(matches!(guides.as_slice(), [(a, b)] if near(*a, pt(100.0, 170.0)) && near(*b, pt(300.0, 170.0))), "{guides:?}"); + // Smart Guides turned off before the next pointer move: the guide goes. + let off = ViewInfo { smart_guides: false, ..plain }; + assert!(s.overlays(off).iter().all(|o| !matches!(o, vectorcraft_engine::tools::Overlay::Line { .. })), "{:?}", s.overlays(off)); assert!(guide_lines(&mut s, plain, 250.0, 180.0).is_empty(), "no guide off the level"); // A press on the level that the app drops cancels the preview, and its guide goes with it. assert!(!guide_lines(&mut s, plain, 250.0, 172.5).is_empty()); diff --git a/crates/tools/src/dimension.rs b/crates/tools/src/dimension.rs index a2a27e0510..5dbe1fd6ef 100644 --- a/crates/tools/src/dimension.rs +++ b/crates/tools/src/dimension.rs @@ -530,7 +530,10 @@ fn dimension_lines(doc: &vectorcraft_doc::Document, preview: (u64, [Point; 2])) /// Where the line of the linear dimension `d`, placed through `p`, lands on the level of one of /// the `levels` that runs parallel to it, within `reach` of `p` across the line: the point to /// place `d` through, and the ends of that other line. The nearest level wins, then the line -/// nearest `p` along it (none nearer than a line that `p` lies across). +/// nearest `p` along it (none nearer than a line that `p` lies across). A line runs parallel when +/// it moves off its level by no more than 1e-6 pt across the span that both lines cover, so a line +/// at a slight angle to `d` is not a level however near its first end lies. A level that would put +/// the line past the limits a dimension allows is passed over. fn line_level(levels: &[DimensionLine], d: &Dimension, p: Point, reach: f64) -> Option<(Point, (Point, Point))> { if d.mode != DimensionMode::Linear { return None; @@ -538,14 +541,20 @@ fn line_level(levels: &[DimensionLine], d: &Dimension, p: Point, reach: f64) -> let (u, normal) = (d.direction(), d.normal()); let mut best: Option<(f64, f64, f64, (Point, Point))> = None; for &(ends, direction) in levels { - if u.cross(direction).abs() > 1e-6 { + let span = [ends.1, d.a, d.b].iter().map(|q| (*q - ends.0).dot(u).abs()).fold(1.0, f64::max); + let drift = u.cross(direction).abs() * span; + if !drift.is_finite() || drift > 1e-6 { continue; } let shift = (ends.0 - p).dot(normal); let (s0, s1) = ((ends.0 - p).dot(u), (ends.1 - p).dot(u)); let along = if s0 * s1 <= 0.0 { 0.0 } else { s0.abs().min(s1.abs()) }; if shift.is_finite() && along.is_finite() && shift.abs() <= reach && best.is_none_or(|(s, a, _, _)| (shift.abs(), along) < (s, a)) { - best = Some((shift.abs(), along, shift, ends)); + let mut placed = d.clone(); + placed.place(p + normal * shift); + if placed.validate().is_ok() { + best = Some((shift.abs(), along, shift, ends)); + } } } best.map(|(_, _, shift, ends)| (p + normal * shift, ends)) @@ -1123,8 +1132,9 @@ impl Tool for DimensionTool { out.extend(self.snap.guides().iter().cloned()); } // A dimension line placed on the level of another: one alignment guide across both lines, - // unless Alignment Guides are off. - if cx.alignment_guides + // unless Smart Guides or Alignment Guides are off. + if cx.smart_guides + && cx.alignment_guides && let (Some(d), Some((c, e))) = (&self.pending, self.level_guide) { let (a, b) = d.line_ends(); @@ -1189,6 +1199,58 @@ mod tests { } } + /// A line at a slight angle is not a level, even where its first end lies within reach across + /// the line: it moves off the level by 9 pt across the span. A line exactly parallel is. + #[test] + fn a_line_at_a_slight_angle_is_not_a_level() { + let pt = Point::new; + let d = + DimensionTool::default().recipe(DimensionMode::Linear, pt(10_000_050.0, 200.0), pt(10_000_150.0, 200.0), pt(10_000_100.0, 200.0), None); + let (from, to) = (pt(100.0, 170.0), pt(10_000_100.0, 179.0)); + let slanted = ((from, to), (to - from) / (to - from).hypot()); + assert!(line_level(&[slanted], &d, pt(10_000_100.0, 168.0), 4.0).is_none()); + let parallel = ((from, pt(10_000_100.0, 170.0)), Vec2::new(1.0, 0.0)); + let (at, _) = line_level(&[parallel], &d, pt(10_000_100.0, 168.0), 4.0).expect("on the level"); + assert!(at.distance(pt(10_000_100.0, 170.0)) < 1e-9, "{at:?}"); + } + + /// A level that would put the line past the largest distance a dimension allows is passed + /// over: the preview goes where the pointer is, with no level guide, or onto a level further + /// off within reach that keeps the line within the limit. + #[test] + fn a_level_past_the_distance_limit_is_passed_over() { + use vectorcraft_doc::{Node, NodeKind}; + let pt = Point::new; + let (mut d, _) = doc_with_rect(); + let l = d.layers[0].id; + let add_level = |d: &mut vectorcraft_doc::Document, y: f64| { + let id = d.alloc_id(); + let mut drawn = Node::new(id, NodeKind::Group { children: vec![], clip: false }); + drawn.extras_mut().dimension = + Some(Box::new(DimensionTool::default().recipe(DimensionMode::Linear, pt(300.0, y), pt(380.0, y), pt(340.0, y), None))); + d.insert(Some(l), usize::MAX, drawn).unwrap(); + }; + let (s, p) = (Selection::default(), paint()); + let ev = |kind, x: f64, y: f64| PointerEvent::new(kind, x, y); + // The points are at y = 400, so a line at y = 1,000,401 is 1,000,001 pt from them. + add_level(&mut d, 1_000_401.0); + for (also, offset) in [(None, 999_999.0), (Some(1_000_396.0), 999_996.0)] { + if let Some(y) = also { + add_level(&mut d, y); + } + let c = cx(&d, &s, &p); + let mut t = DimensionTool::default(); + for (x, y) in [(400.0, 400.0), (450.0, 400.0)] { + t.pointer(&c, &ev(PointerKind::Down, x, y)); + t.pointer(&c, &ev(PointerKind::Up, x, y)); + } + let acts = t.pointer(&c, &ev(PointerKind::Move, 420.0, 1_000_399.0)); + let Some(Action::Preview(_, params)) = acts.last() else { panic!("{acts:?}") }; + assert_eq!(params["offset"], json!(offset)); + assert_eq!(t.level_guide.is_some(), also.is_some()); + } + } + /// The lines of the dimensions in the document are read only for a linear dimension. They are /// read with Smart Guides off too, so a line placed after Smart Guides are turned on lines up. #[test] diff --git a/docs/dimensions.md b/docs/dimensions.md index bf6cf78653..75388109fd 100644 --- a/docs/dimensions.md +++ b/docs/dimensions.md @@ -78,8 +78,10 @@ The **Dimension Tool** is in the Modify section of the toolbar. Its strip offers tolerance of the level of a parallel line of a linear dimension already drawn has its line on that level. The other dimension, and every layer and group it is in, must be visible and unlocked. The tool reads at most 20,000 visible, unlocked layers, groups and objects, from the - frontmost back, so in a larger document a dimension past them is not a level. This is - VectorCraft's own rule. With + frontmost back, so in a larger document a dimension past them is not a level. A line that + moves off its level by more than 0.000001 pt across the span of both lines is not parallel. A + level that would put the line more than 1,000,000 pt from its points is passed over, and the + line goes where the pointer is. This is VectorCraft's own rule. With Smart Guides and Alignment Guides on, a guide runs along the level across both lines while the dimension is placed. - **Angular:** hover a corner where two straight segments meet and drag outwards. Or click two From 17517e93a5c72678773a3b0ecf00937edce874ff Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 21:27:59 -0800 Subject: [PATCH 2/2] Dimension tool: a level is checked across the whole span of both lines The span a parallel line is checked across now runs from the lowest to the highest point of both lines along the dimension. It no longer depends on which end of the other line comes first, and it has no one-point floor, so a short line that stays within 0.000001 pt of its level is a level. --- crates/tools/src/dimension.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/tools/src/dimension.rs b/crates/tools/src/dimension.rs index 5dbe1fd6ef..4e76ede6b2 100644 --- a/crates/tools/src/dimension.rs +++ b/crates/tools/src/dimension.rs @@ -541,7 +541,8 @@ fn line_level(levels: &[DimensionLine], d: &Dimension, p: Point, reach: f64) -> let (u, normal) = (d.direction(), d.normal()); let mut best: Option<(f64, f64, f64, (Point, Point))> = None; for &(ends, direction) in levels { - let span = [ends.1, d.a, d.b].iter().map(|q| (*q - ends.0).dot(u).abs()).fold(1.0, f64::max); + let along_u = [ends.0, ends.1, d.a, d.b].map(|q| (q - ends.0).dot(u)); + let span = along_u.iter().fold(f64::NEG_INFINITY, |m, s| m.max(*s)) - along_u.iter().fold(f64::INFINITY, |m, s| m.min(*s)); let drift = u.cross(direction).abs() * span; if !drift.is_finite() || drift > 1e-6 { continue; @@ -1214,6 +1215,27 @@ mod tests { assert!(at.distance(pt(10_000_100.0, 170.0)) < 1e-9, "{at:?}"); } + /// The span a level is checked across runs from the lowest to the highest point of both lines + /// along the dimension, whichever end of the other line comes first, and has no lower bound. + #[test] + fn a_level_is_checked_across_the_whole_span_of_both_lines() { + let pt = Point::new; + let level = |from: Point, to: Point| ((from, to), (to - from) / (to - from).hypot()); + // 0.00000075 pt off across 100 pt drifts 0.0000015 pt across the 200 pt both lines cover. + let d = DimensionTool::default().recipe(DimensionMode::Linear, pt(-100.0, 200.0), pt(100.0, 200.0), pt(0.0, 200.0), None); + let (a, b) = (pt(0.0, 170.0), pt(100.0, 170.000_000_75)); + for slanted in [level(a, b), level(b, a)] { + assert!(line_level(&[slanted], &d, pt(0.0, 168.0), 4.0).is_none(), "{slanted:?}"); + } + // 0.0000005 pt off across the 0.1 pt both lines cover is within the tolerance. + let d = DimensionTool::default().recipe(DimensionMode::Linear, pt(0.0, 200.0), pt(0.1, 200.0), pt(0.05, 200.0), None); + let (a, b) = (pt(0.0, 170.0), pt(0.1, 170.000_000_5)); + for short in [level(a, b), level(b, a)] { + let (at, _) = line_level(&[short], &d, pt(0.05, 168.0), 4.0).expect("on the level"); + assert!((at.y - 170.0).abs() < 1e-6, "{at:?}"); + } + } + /// A level that would put the line past the largest distance a dimension allows is passed /// over: the preview goes where the pointer is, with no level guide, or onto a level further /// off within reach that keeps the line within the limit.