mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
View: Curvature Comb breaks at a cusp on an anchor
The direction of the last curved tooth now carries from one segment to the next, so a cusp where two curved segments meet starts a new line through the tips. It is cleared wherever a tooth is left out, on a line and between strided segments. A closed comb now closes at its first anchor when the curve goes on there without turning back (comb_closes), instead of only when the comb has no break at all, so a cusp elsewhere does not open the join.
This commit is contained in:
1 parent
9803e7438d
commit
80e8c74d55
2 files changed
+67
-6
No files matched your search
@@ -250,11 +250,18 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
let stride = (per * n).div_ceil(max_teeth).max(1);
|
||||
let mut teeth = Vec::with_capacity((per * n / stride).min(max_teeth));
|
||||
let mut start = true;
|
||||
// The direction of the last curved tooth, carried across segments so that a cusp at an
|
||||
// anchor also starts a new line through the tips.
|
||||
let mut previous_direction: Option<Vec2> = None;
|
||||
for seg in (0..n).step_by(stride) {
|
||||
start |= stride > 1;
|
||||
if stride > 1 {
|
||||
start = true;
|
||||
previous_direction = None;
|
||||
}
|
||||
let c = sp.segment(seg);
|
||||
if ![c.p0, c.p1, c.p2, c.p3].iter().all(|p| p.is_finite()) {
|
||||
start = true;
|
||||
previous_direction = None;
|
||||
continue;
|
||||
}
|
||||
let (d1, d2) = (c.deriv(), c.deriv().deriv());
|
||||
@@ -262,7 +269,6 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
let size = (c.p1 - c.p0).hypot() + (c.p2 - c.p1).hypot() + (c.p3 - c.p2).hypot();
|
||||
let chord = c.p3 - c.p0;
|
||||
let line = sp.segment_is_line(seg) && chord.hypot() > 0.0;
|
||||
let mut previous_direction: Option<Vec2> = None;
|
||||
for i in 0..per {
|
||||
if teeth.len() >= max_teeth {
|
||||
return teeth;
|
||||
@@ -272,6 +278,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
// A line stored with its handles on its anchors has no speed at its ends, but a
|
||||
// direction and no curvature all along.
|
||||
let at = c.eval(t);
|
||||
previous_direction = None;
|
||||
if !at.is_finite() {
|
||||
start = true;
|
||||
continue;
|
||||
@@ -284,6 +291,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
let speed = v.hypot();
|
||||
if speed <= CUSP_SHARE * size || !speed.is_finite() {
|
||||
start = true;
|
||||
previous_direction = None;
|
||||
continue;
|
||||
}
|
||||
let k = v.cross(a) / (speed * speed * speed);
|
||||
@@ -295,6 +303,7 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
let tip = at - normal * (k * scale);
|
||||
if !at.is_finite() || !tip.is_finite() {
|
||||
start = true;
|
||||
previous_direction = None;
|
||||
continue;
|
||||
}
|
||||
teeth.push(CombTooth { at, tip, start });
|
||||
@@ -304,6 +313,23 @@ pub fn curvature_comb(sp: &SubPath, samples: usize, scale: f64, max_teeth: usize
|
||||
teeth
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn comb_closes(sp: &SubPath, teeth: &[CombTooth]) -> bool {
|
||||
let (Some(first), Some(last)) = (teeth.first(), teeth.last()) else {
|
||||
return false;
|
||||
};
|
||||
if !sp.closed || teeth.len() < 2 || first.at.distance(last.at) >= 1e-9 {
|
||||
return false;
|
||||
}
|
||||
let n = sp.segment_count();
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -363,6 +389,42 @@ mod tests {
|
||||
assert!((after / before - 2.0).abs() < 1e-9, "{before} {after}");
|
||||
assert!((before - 10.0).abs() < 0.3, "{before}");
|
||||
assert!(teeth[2].at.distance(teeth[3].at) < 1e-9, "both teeth start on the shared anchor");
|
||||
assert_eq!(teeth.iter().filter(|t| t.start).count(), 1, "a smooth anchor keeps one unbroken comb");
|
||||
}
|
||||
|
||||
/// Two segments that meet at a cusp on their shared anchor: the first segment ends heading
|
||||
/// down and the second starts heading up, so the line through the tips starts again there.
|
||||
#[test]
|
||||
fn the_comb_breaks_at_a_cusp_on_an_anchor() {
|
||||
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), h_out: Point::new(100.0, -25.0), ..Anchor::corner(Point::new(100.0, 0.0)) };
|
||||
let a2 = Anchor { h_in: Point::new(200.0, -25.0), ..Anchor::corner(Point::new(200.0, 0.0)) };
|
||||
let sp = SubPath::new(vec![a0, a1, a2], false);
|
||||
let teeth = curvature_comb(&sp, 3, 1000.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.len(), 6);
|
||||
assert_eq!(teeth.iter().map(|t| t.start).collect::<Vec<_>>(), [true, false, false, true, false, false]);
|
||||
assert!(!comb_closes(&sp, &teeth), "an open subpath does not close");
|
||||
}
|
||||
|
||||
/// 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]
|
||||
fn the_comb_closes_unless_its_first_anchor_is_a_cusp() {
|
||||
let closed = |h_in: Point| {
|
||||
let a0 = Anchor { h_in, 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), h_out: Point::new(100.0, -25.0), ..Anchor::corner(Point::new(100.0, 0.0)) };
|
||||
let a2 = Anchor { h_in: Point::new(200.0, -25.0), h_out: Point::new(200.0, 50.0), ..Anchor::corner(Point::new(200.0, 0.0)) };
|
||||
SubPath::new(vec![a0, a1, a2], true)
|
||||
};
|
||||
let smooth = closed(Point::new(0.0, 50.0));
|
||||
let teeth = curvature_comb(&smooth, 3, 1000.0, MAX_COMB_TEETH);
|
||||
assert_eq!(teeth.iter().filter(|t| t.start).count(), 2, "the cusp at the second anchor breaks the comb");
|
||||
assert!(comb_closes(&smooth, &teeth));
|
||||
let cusp = closed(Point::new(0.0, -50.0));
|
||||
assert!(!comb_closes(&cusp, &curvature_comb(&cusp, 3, 1000.0, MAX_COMB_TEETH)));
|
||||
let circle = shapes::ellipse(Rect::new(0.0, 0.0, 100.0, 100.0));
|
||||
let sp = &circle.subpaths[0];
|
||||
assert!(comb_closes(sp, &curvature_comb(sp, 5, 1000.0, MAX_COMB_TEETH)));
|
||||
}
|
||||
|
||||
/// A segment with no length and a handle drawn back onto its anchor do not panic and give no
|
||||
|
||||
@@ -848,7 +848,7 @@ const COMB_SCALE: f64 = 2000.0;
|
||||
/// [`vectorcraft_geom::curvature::MAX_COMB_TEETH`] teeth in all, shared among the subpaths by their
|
||||
/// number of segments, so a large selection gets fewer teeth everywhere.
|
||||
pub(in crate::canvas) fn curvature_comb_overlays(app: &VectorcraftApp, zoom: f64) -> Vec<Overlay> {
|
||||
use vectorcraft_geom::curvature::{MAX_COMB_TEETH, curvature_comb};
|
||||
use vectorcraft_geom::curvature::{MAX_COMB_TEETH, comb_closes, curvature_comb};
|
||||
let Some(st) = app.session.active() else { return vec![] };
|
||||
if !(zoom.is_finite() && zoom > 0.0) {
|
||||
return vec![];
|
||||
@@ -886,9 +886,8 @@ pub(in crate::canvas) fn curvature_comb_overlays(app: &VectorcraftApp, zoom: f64
|
||||
}
|
||||
}
|
||||
// A closed subpath combed all the way round ends where it starts: the line closes there.
|
||||
let unbroken = teeth.iter().filter(|t| t.start).count() == 1;
|
||||
if let (true, true, Some(first), Some(last)) = (sp.closed, unbroken, teeth.first(), teeth.last())
|
||||
&& first.at.distance(last.at) < 1e-9
|
||||
if let Some(first) = teeth.first()
|
||||
&& comb_closes(sp, &teeth)
|
||||
{
|
||||
envelope.line_to(first.tip);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user