mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 19:41:36 -05:00
Merge pull request #10811 from dotCooCoo/fix/comb-line-reversal
Curvature comb: the line through the tips breaks where a straight segment turns back
This commit is contained in:
+1
-1
@@ -124,7 +124,7 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
|
||||
## Progress log
|
||||
|
||||
- 2026-12-03: View › Show Curvature Comb (`view.curvatureComb`) draws the curvature comb of the selected paths and compound paths: teeth along the normal as long as the curvature, and a line through their tips (#10659). Headline numbers unchanged.
|
||||
- 2026-12-03: View › Show Curvature Comb (`view.curvatureComb`) draws the curvature comb of the selected paths and compound paths: teeth along the normal as long as the curvature, and a line through their tips that starts again where the path turns back on itself at a cusp, a straight segment included (#10659). Headline numbers unchanged.
|
||||
|
||||
- 2026-12-02: The desktop Print dialog looks up installed printers in the background. Named printers become selectable after the lookup returns and the dialog applies the list. PDF File is available at once. Physical printing waits for the list to be applied, and Done can save settings while the lookup runs. The dialog shows and announces discovery and disables physical Print until the list is applied.
|
||||
|
||||
|
||||
@@ -236,6 +236,16 @@ pub struct CombTooth {
|
||||
/// The most teeth a curvature comb draws in all.
|
||||
pub const MAX_COMB_TEETH: usize = 20_000;
|
||||
|
||||
/// Does the direction `next` head back against the direction `previous`? A right angle
|
||||
/// whose dot product rounds a little below zero does not count.
|
||||
fn turns_back(previous: Vec2, next: Vec2) -> bool {
|
||||
let unit = |v: Vec2| {
|
||||
let v = v / v.x.abs().max(v.y.abs());
|
||||
v / v.hypot()
|
||||
};
|
||||
unit(previous).dot(unit(next)) < -1e-9
|
||||
}
|
||||
|
||||
/// The curvature comb of `sp`: `samples` teeth on each segment (at least 2), evenly spaced in `t`
|
||||
/// from 0 to 1, each `scale` times the signed curvature long ([`CombTooth`]). A straight segment
|
||||
/// gives teeth of no length, its ends included. A point of a curved segment where it has no
|
||||
@@ -243,6 +253,8 @@ pub const MAX_COMB_TEETH: usize = 20_000;
|
||||
/// with no length, a segment whose points are not finite, or a `scale` that is not finite. When
|
||||
/// `samples` teeth on every segment would pass `max_teeth`, every segment gets 2 teeth, and when
|
||||
/// that would still pass it, only every k-th segment gets teeth, spread along the whole subpath.
|
||||
/// The line through the tips starts again wherever the path turns back on itself, at a straight
|
||||
/// segment as at a curve.
|
||||
pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize) -> Vec<CombTooth> {
|
||||
let n = sp.segment_count();
|
||||
if n == 0 || !scale.is_finite() || max_teeth < 2 {
|
||||
@@ -279,8 +291,10 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
if line {
|
||||
// A line stored with its handles on its anchors has no speed at its ends, but a
|
||||
// direction and no curvature all along.
|
||||
// A line that turns back on the segment before it is a cusp at their anchor.
|
||||
let at = c.eval(t);
|
||||
previous_direction = None;
|
||||
start |= previous_direction.is_some_and(|previous| turns_back(previous, chord));
|
||||
previous_direction = Some(chord);
|
||||
if !at.is_finite() {
|
||||
start = true;
|
||||
continue;
|
||||
@@ -298,7 +312,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
}
|
||||
let k = v.cross(a) / (speed * speed * speed);
|
||||
let direction = v / speed;
|
||||
start |= previous_direction.is_some_and(|previous| previous.dot(direction) < 0.0);
|
||||
start |= previous_direction.is_some_and(|previous| turns_back(previous, direction));
|
||||
previous_direction = Some(direction);
|
||||
let normal = Vec2::new(-direction.y, direction.x);
|
||||
let at = c.eval(t);
|
||||
@@ -333,8 +347,15 @@ pub fn comb_closes(sp: &SubPath, teeth: &[CombTooth]) -> bool {
|
||||
if !(last.segment + 1..n).chain(0..first.segment).all(no_length) {
|
||||
return false;
|
||||
}
|
||||
match (neighbor(sp, 0, false), neighbor(sp, n.saturating_sub(1), true)) {
|
||||
(Some((into, _)), Some((out, _))) => end_direction(into).dot(start_direction(out)) >= 0.0,
|
||||
match ((0..n).rev().find(|&i| !no_length(i)), (0..n).find(|&i| !no_length(i))) {
|
||||
(Some(into), Some(out)) => {
|
||||
let (a, b) = (sp.segment(into), sp.segment(out));
|
||||
// A straight segment's chord, as `curvature_comb` uses it, when the chord has a length.
|
||||
let chord = |i: usize, c: CubicBez| Some(c.p3 - c.p0).filter(|v| sp.segment_is_line(i) && v.hypot() > 0.0);
|
||||
let a = chord(into, a).unwrap_or_else(|| end_direction(a));
|
||||
let b = chord(out, b).unwrap_or_else(|| start_direction(b));
|
||||
!turns_back(a, b)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -415,6 +436,22 @@ mod tests {
|
||||
assert!(!comb_closes(&sp, &teeth), "an open subpath does not close");
|
||||
}
|
||||
|
||||
/// A curve that ends heading down, a straight segment back up from its end, and a curve that
|
||||
/// starts heading down again: both anchors are cusps, so the line through the tips starts again
|
||||
/// at each of them.
|
||||
#[test]
|
||||
fn the_comb_breaks_where_a_straight_segment_turns_back() {
|
||||
let a0 = Anchor { h_out: Point::new(0.0, -50.0), ..Anchor::corner(Point::new(0.0, 0.0)) };
|
||||
let a1 = Anchor { h_in: Point::new(100.0, -50.0), ..Anchor::corner(Point::new(100.0, 0.0)) };
|
||||
let a2 = Anchor { h_out: Point::new(100.0, -50.0), ..Anchor::corner(Point::new(100.0, -100.0)) };
|
||||
let a3 = Anchor { h_in: Point::new(200.0, -50.0), ..Anchor::corner(Point::new(200.0, -100.0)) };
|
||||
let sp = SubPath::new(vec![a0, a1, a2, a3], false);
|
||||
assert!(sp.segment_is_line(1));
|
||||
let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.len(), 9);
|
||||
assert_eq!(teeth.iter().map(|t| t.start).collect::<Vec<_>>(), [true, false, false, true, false, false, true, false, false]);
|
||||
}
|
||||
|
||||
/// A closed comb closes at its first anchor when the curve goes on smoothly there, even with a
|
||||
/// cusp elsewhere, and stays open when the first anchor is the cusp.
|
||||
#[test]
|
||||
@@ -436,6 +473,52 @@ mod tests {
|
||||
assert!(comb_closes(sp, &curvature_comb(sp, 5, 1000.0, MAX_COMB_TEETH)));
|
||||
}
|
||||
|
||||
/// A straight segment uses its chord at the seam even when a tiny handle points the other way.
|
||||
#[test]
|
||||
fn the_comb_stays_open_where_a_straight_segment_turns_back_at_the_seam() {
|
||||
let a0 = Anchor { h_in: Point::new(-1e-12, 0.0), h_out: Point::new(50.0, 0.0), ..Anchor::corner(Point::ORIGIN) };
|
||||
let a1 = Anchor { h_in: Point::new(100.0, 50.0), ..Anchor::corner(Point::new(100.0, 0.0)) };
|
||||
let mut sp = SubPath::new(vec![a0, a1], true);
|
||||
for line in [1, 0] {
|
||||
assert!(sp.segment_is_line(line));
|
||||
let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.len(), 6);
|
||||
assert!(teeth[0].at.distance(teeth[5].at) < 1e-9, "the teeth meet at the seam");
|
||||
assert!(!comb_closes(&sp, &teeth), "the chord reverses direction at the seam: {line}");
|
||||
sp.reverse();
|
||||
sp.anchors.rotate_right(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// A segment whose anchors sit on one point and whose handle is too short to count still has
|
||||
/// a direction at the seam, so a reversal there keeps the comb open.
|
||||
#[test]
|
||||
fn the_comb_stays_open_at_a_seam_segment_with_coincident_anchors() {
|
||||
let a0 = Anchor::corner(Point::ORIGIN);
|
||||
let a1 = Anchor::corner(Point::new(100.0, 0.0));
|
||||
let a2 = Anchor { h_out: Point::new(1e-12, 0.0), ..Anchor::corner(Point::ORIGIN) };
|
||||
let sp = SubPath::new(vec![a0, a1, a2], true);
|
||||
assert!(sp.segment_is_line(2));
|
||||
let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH);
|
||||
let (first, last) = (teeth.first().map(|t| t.at), teeth.last().map(|t| t.at));
|
||||
assert!(first.zip(last).is_some_and(|(f, l)| f.distance(l) < 1e-9), "the teeth meet at the seam: {teeth:?}");
|
||||
assert!(!comb_closes(&sp, &teeth), "the closing segment heads back against the first one");
|
||||
}
|
||||
|
||||
/// Large finite line directions still break the comb at a reversal inside the path and at its seam.
|
||||
#[test]
|
||||
fn the_comb_breaks_where_large_straight_segments_turn_back() {
|
||||
let sp = SubPath::polyline(&[Point::ORIGIN, Point::new(1e155, 0.0), Point::ORIGIN], false);
|
||||
let teeth = curvature_comb(&sp, 2, 1.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.len(), 4);
|
||||
assert!(teeth.iter().all(|t| t.at.is_finite() && t.tip.is_finite()), "{teeth:?}");
|
||||
assert_eq!(teeth.iter().map(|t| t.start).collect::<Vec<_>>(), [true, false, true, false]);
|
||||
let closed = SubPath::polyline(&[Point::ORIGIN, Point::new(1e155, 0.0)], true);
|
||||
let teeth = curvature_comb(&closed, 2, 1.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.len(), 4);
|
||||
assert!(!comb_closes(&closed, &teeth), "the closing chord reverses direction at the first anchor");
|
||||
}
|
||||
|
||||
/// A segment with no length and a handle drawn back onto its anchor do not panic and give no
|
||||
/// tooth where the curve has no direction; a scale that is not finite gives no comb.
|
||||
#[test]
|
||||
|
||||
@@ -271,7 +271,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"view.curvatureComb",
|
||||
"Show Curvature Comb",
|
||||
"",
|
||||
"{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on",
|
||||
"{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color that starts again where the path turns back on itself at a cusp, a straight segment included; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on",
|
||||
),
|
||||
(
|
||||
"type.hiddenCharacters",
|
||||
|
||||
@@ -272,7 +272,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[
|
||||
"view.curvatureComb",
|
||||
"Show Curvature Comb",
|
||||
"",
|
||||
"{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on",
|
||||
"{} toggle drawing the curvature comb of the selected paths and compound paths while edges show: 16 teeth per segment along the normal, on the outer side of the bend, each as long as the curvature there (a radius of 100 screen points gives a 20-point tooth at any zoom), with a line through their tips in the layer color that starts again where the path turns back on itself at a cusp, a straight segment included; about 20,000 teeth at most, fewer per segment on a large selection; a view setting, not saved in the document and not an undo step → true when on",
|
||||
),
|
||||
(
|
||||
"type.hiddenCharacters",
|
||||
|
||||
+2
-1
@@ -9717,7 +9717,8 @@ origin), when `t` is not from 0 to 1, when `id` is a guide, and when the path, s
|
||||
`view.curvatureComb` (View › Show Curvature Comb, or Hide Curvature Comb while it is on) turns on the curvature comb
|
||||
of the selected paths, and of each path in a selected compound path, while edges show. Each segment gets 16 evenly
|
||||
spaced teeth along the normal, on the outer side of the bend, each as long as the curvature at that point, and a line
|
||||
joins the teeth's tips, in the path's layer color. A straight segment has teeth of no length, and a point of a curve
|
||||
joins the teeth's tips, in the path's layer color. The line starts again where the path turns back on itself at a
|
||||
cusp, a straight segment included. A straight segment has teeth of no length, and a point of a curve
|
||||
where it has no direction (a handle on its anchor) has no tooth. The length is VectorCraft's own value: a bend with a
|
||||
radius of 100 screen points draws 20-point teeth at any zoom. The comb draws about 20,000 teeth at most, shared among
|
||||
the selected paths by their number of segments: a larger selection gets 2 teeth per segment, then teeth on every k-th
|
||||
|
||||
Reference in New Issue
Block a user