mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
View: Curvature Comb does not close across segments a capped comb skips
When a long subpath has more segments than the tooth budget, only every k-th segment gets teeth. comb_closes compared only the first and last tooth positions, so a last sampled segment that ended on the first anchor joined its tip to the first tooth across the skipped closing segment. Each tooth now records its segment, and the comb closes only when the segments between the last tooth and the first across the seam have no length.
This commit is contained in:
1 parent
80e8c74d55
commit
c618b9b22c
1 file changed
+42
-5
@@ -229,6 +229,8 @@ pub struct CombTooth {
|
||||
/// first after the curve turns back on itself (a cusp between two teeth): a line through the
|
||||
/// tips starts again here.
|
||||
pub start: bool,
|
||||
/// The index of the segment the tooth sits on.
|
||||
pub segment: usize,
|
||||
}
|
||||
|
||||
/// The most teeth a curvature comb draws in all.
|
||||
@@ -283,7 +285,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
start = true;
|
||||
continue;
|
||||
}
|
||||
teeth.push(CombTooth { at, tip: at, start });
|
||||
teeth.push(CombTooth { at, tip: at, start, segment: seg });
|
||||
start = false;
|
||||
continue;
|
||||
}
|
||||
@@ -306,7 +308,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
previous_direction = None;
|
||||
continue;
|
||||
}
|
||||
teeth.push(CombTooth { at, tip, start });
|
||||
teeth.push(CombTooth { at, tip, start, segment: seg });
|
||||
start = false;
|
||||
}
|
||||
}
|
||||
@@ -314,16 +316,23 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
}
|
||||
|
||||
/// Does the line through the tips of `teeth`, the comb of `sp` ([`curvature_comb`]), close where
|
||||
/// it starts: `sp` is closed, its first and last teeth sit on the same point, and the curve does
|
||||
/// not turn back on itself at its first anchor.
|
||||
/// it starts: `sp` is closed, only segments with no length lie between its last tooth and its
|
||||
/// first across the seam, the two sit on the same point, and the curve does not turn back on itself at its
|
||||
/// first anchor. A comb that skips segments to keep to its budget does not close across the ones
|
||||
/// it skipped at the end.
|
||||
pub fn comb_closes(sp: &SubPath, teeth: &[CombTooth]) -> bool {
|
||||
let (Some(first), Some(last)) = (teeth.first(), teeth.last()) else {
|
||||
return false;
|
||||
};
|
||||
let n = sp.segment_count();
|
||||
if !sp.closed || teeth.len() < 2 || first.at.distance(last.at) >= 1e-9 {
|
||||
return false;
|
||||
}
|
||||
let n = sp.segment_count();
|
||||
// Only segments with no length may lie between the last tooth and the first across the seam.
|
||||
let no_length = |i: usize| start_direction(sp.segment(i)) == Vec2::ZERO;
|
||||
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,
|
||||
_ => false,
|
||||
@@ -509,6 +518,34 @@ mod tests {
|
||||
assert_eq!(few.len(), 60_000, "too many for 16 on each, so 2 on each");
|
||||
}
|
||||
|
||||
/// A capped comb that skips the last segment does not close, even when the last segment it
|
||||
/// samples ends on the first anchor: the line through the tips would cross the skipped loop.
|
||||
#[test]
|
||||
fn a_capped_comb_does_not_close_across_a_skipped_segment() {
|
||||
let sp = SubPath::new(
|
||||
vec![
|
||||
Anchor::with_handles(Point::new(0.0, 0.0), Point::new(-50.0, -50.0), Point::new(50.0, 50.0)),
|
||||
Anchor::with_handles(Point::new(100.0, 0.0), Point::new(80.0, 40.0), Point::new(120.0, 20.0)),
|
||||
Anchor::with_handles(Point::new(100.0, 100.0), Point::new(140.0, 80.0), Point::new(60.0, 140.0)),
|
||||
Anchor::with_handles(Point::new(0.0, 0.0), Point::new(30.0, 120.0), Point::new(-60.0, 60.0)),
|
||||
],
|
||||
true,
|
||||
);
|
||||
let teeth = curvature_comb(&sp, 16, 1000.0, 4);
|
||||
let (first, last) = (&teeth[0], &teeth[teeth.len() - 1]);
|
||||
assert!(last.segment < 3, "the closing segment is skipped: {teeth:?}");
|
||||
assert!(first.at.distance(last.at) < 1e-9, "the last sampled tooth sits on the first anchor");
|
||||
assert!(!comb_closes(&sp, &teeth), "the comb does not close across the skipped last segment");
|
||||
assert!(comb_closes(&sp, &curvature_comb(&sp, 16, 1000.0, MAX_COMB_TEETH)), "the full comb closes");
|
||||
let mut anchors: Vec<Anchor> = sp.anchors.iter().copied().collect();
|
||||
anchors[0].h_in = anchors[0].p;
|
||||
anchors.insert(0, Anchor { h_in: Point::new(-50.0, -50.0), ..Anchor::corner(Point::new(0.0, 0.0)) });
|
||||
let seam = SubPath::new(anchors, true);
|
||||
let full = curvature_comb(&seam, 16, 1000.0, MAX_COMB_TEETH);
|
||||
assert_eq!(full.first().map(|t| t.segment), Some(1), "the segment of no length at the seam has no teeth");
|
||||
assert!(comb_closes(&seam, &full), "the comb closes across a segment of no length");
|
||||
}
|
||||
|
||||
/// Every segment of a circle drawn by [`shapes::ellipse`] is the usual cubic of a quarter
|
||||
/// turn, and the circle at any point of it is the circle itself: radius 100, center (150, 150).
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user