mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Curvature comb: the line through the tips breaks where a straight segment turns back
A straight segment cleared the direction the comb carries from segment to segment, so a curve that ended heading one way and a line that went back the other way, or a line followed by a curve that turned back on it, kept one line through the tips across the cusp. A straight segment now carries its chord's direction, and the comb starts a new line wherever the next direction heads back against the last one. The test for turning back now ignores a dot product that rounds a little below zero at a right angle, at interior anchors and at the closed-path seam alike.
This commit is contained in:
1 parent
f29fe10c32
commit
57efe8105e
2 files changed
+72
-5
No files matched your search
@@ -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,13 @@ 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));
|
||||
let a = if sp.segment_is_line(into) { a.p3 - a.p0 } else { end_direction(a) };
|
||||
let b = if sp.segment_is_line(out) { b.p3 - b.p0 } else { start_direction(b) };
|
||||
!turns_back(a, b)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -415,6 +434,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 +471,37 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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]
|
||||
|
||||
+2
-1
@@ -9639,7 +9639,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