mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Render: curve lines keep both turns of a curve that turns back twice
A curve drawn as lines took its line ends from the curve's bending alone. Where a curve runs out and back twice along almost one line, its two turns hold almost no bending, so a thin stroke drew it with one step in two halves: each half held a whole turn, the two halves' gaps canceled, and the band ran straight from start to end without either turn. A cubic from (0, 0) out to x = 28.87 and back to x = -28.87 drew only around x = 0. Each step is now checked against its curve: where the curve lies more than twice the chord allowance from the step's line, to either side or past its ends, the step count doubles, up to the curve's equal steps. Wide strokes draw their long curves the same way and get the same check.
This commit is contained in:
1 parent
6e3c328646
commit
a9aa1f405b
2 files changed
+113
-20
No files matched your search
+88
-20
@@ -345,13 +345,19 @@ fn end_tangent(c: &CubicBez) -> Option<Vec2> {
|
||||
/// How many pieces of a curve [`params`] measures its bending on.
|
||||
const SAMPLES: usize = 32;
|
||||
|
||||
/// A chord whose curve lies more than this many times `chord` from it ([`off_chord`]) is drawn with
|
||||
/// more steps ([`params`]).
|
||||
const FIT: f64 = 2.0;
|
||||
|
||||
/// The parameters curve `c` has its line ends at, and whether its first and its last step are
|
||||
/// halves. Each step holds an equal share of the curve's bending (the integral of the square root
|
||||
/// of its curvature along it), and there are as many as keep each chord within about `chord` of the
|
||||
/// curve, at most `most` (its equal steps, [`steps`]). The steps are equal when the bending can't
|
||||
/// be measured (a cusp, or a point that isn't finite) or is 0. The first or last step is halved
|
||||
/// when that end stays on the curve (`pin_start`, `pin_end`); a curve of one step is then drawn
|
||||
/// in two halves.
|
||||
/// curve, at most `most` (its equal steps, [`steps`]). When the curve between two line ends lies
|
||||
/// more than [`FIT`] times `chord` from their chord ([`off_chord`]), as where a curve turns back
|
||||
/// within a step, the count doubles, up to `most`; when `most` such steps still don't fit, the
|
||||
/// steps are equal. The steps are also equal when the bending can't be measured (a cusp, or a point
|
||||
/// that isn't finite) or is 0. The first or last step is halved when that end stays on the curve
|
||||
/// (`pin_start`, `pin_end`); a curve of one step is then drawn in two halves.
|
||||
fn params(c: &CubicBez, most: usize, chord: f64, pin_start: bool, pin_end: bool) -> (Vec<f64>, (bool, bool)) {
|
||||
let (d1, d2) = (c.deriv(), c.deriv().deriv());
|
||||
let bend = |u: f64| {
|
||||
@@ -367,33 +373,95 @@ fn params(c: &CubicBez, most: usize, chord: f64, pin_start: bool, pin_end: bool)
|
||||
}
|
||||
let total = sums[SAMPLES];
|
||||
let most = most.max(1);
|
||||
let n = if total.is_finite() && total > 0.0 { ((total / SAMPLES as f64 / (8.0 * chord).sqrt()).ceil() as usize).clamp(1, most) } else { most };
|
||||
// The parameter at which a share `s` (0 to 1) of the bending lies before it.
|
||||
let at = |s: f64| -> f64 {
|
||||
if !(total.is_finite() && total > 0.0) {
|
||||
return s;
|
||||
}
|
||||
let want = s * total;
|
||||
let j = sums.partition_point(|&x| x <= want).clamp(1, SAMPLES);
|
||||
let (lo, hi) = (sums[j - 1], sums[j]);
|
||||
let part = if hi > lo { ((want - lo) / (hi - lo)).clamp(0.0, 1.0) } else { 0.0 };
|
||||
(j as f64 - 1.0 + part) / SAMPLES as f64
|
||||
};
|
||||
if n == 1 && (pin_start || pin_end) {
|
||||
return (vec![0.0, at(0.5), 1.0], (true, true));
|
||||
// The line ends of `n` steps, each holding a share `step(s)` of the curve.
|
||||
let cut = |n: usize, step: &dyn Fn(f64) -> f64| -> (Vec<f64>, (bool, bool)) {
|
||||
if n == 1 && (pin_start || pin_end) {
|
||||
return (vec![0.0, step(0.5), 1.0], (true, true));
|
||||
}
|
||||
let h = 1.0 / n as f64;
|
||||
let mut t = Vec::with_capacity(n + 3);
|
||||
t.push(0.0);
|
||||
if pin_start {
|
||||
t.push(step(h / 2.0));
|
||||
}
|
||||
t.extend((1..n).map(|k| step(k as f64 * h)));
|
||||
if pin_end {
|
||||
t.push(step(1.0 - h / 2.0));
|
||||
}
|
||||
t.push(1.0);
|
||||
(t, (pin_start, pin_end))
|
||||
};
|
||||
if total.is_finite() && total > 0.0 {
|
||||
let mut n = ((total / SAMPLES as f64 / (8.0 * chord).sqrt()).ceil() as usize).clamp(1, most);
|
||||
loop {
|
||||
let fit = cut(n, &at);
|
||||
if fit.0.windows(2).all(|w| off_chord(c, w[0], w[1]) <= FIT * chord) {
|
||||
return fit;
|
||||
}
|
||||
if n >= most {
|
||||
break;
|
||||
}
|
||||
n = n.saturating_mul(2).min(most);
|
||||
}
|
||||
}
|
||||
let h = 1.0 / n as f64;
|
||||
let mut t = Vec::with_capacity(n + 3);
|
||||
t.push(0.0);
|
||||
if pin_start {
|
||||
t.push(at(h / 2.0));
|
||||
cut(most, &|s| s)
|
||||
}
|
||||
|
||||
/// How far curve `c` between parameters `a` and `b` lies from the chord between its points there:
|
||||
/// the farthest it lies to either side of the chord's line, or before the chord's start or past its
|
||||
/// end along it. For a chord of no length, the farthest the curve's control points between `a` and
|
||||
/// `b` lie from it.
|
||||
fn off_chord(c: &CubicBez, a: f64, b: f64) -> f64 {
|
||||
let (p, q) = (c.eval(a), c.eval(b));
|
||||
let along = q - p;
|
||||
let len = along.hypot();
|
||||
if !(len > 0.0 && len.is_finite()) {
|
||||
let s = c.subsegment(a..b);
|
||||
return [s.p1, s.p2, s.p3].iter().map(|x| (*x - p).hypot()).fold(0.0, f64::max);
|
||||
}
|
||||
t.extend((1..n).map(|k| at(k as f64 * h)));
|
||||
if pin_end {
|
||||
t.push(at(1.0 - h / 2.0));
|
||||
let u = along / len;
|
||||
// The derivative, t² k2 + t k1 + k0.
|
||||
let d = c.deriv();
|
||||
let (k2, k1, k0) = (d.p0.to_vec2() - d.p1.to_vec2() * 2.0 + d.p2.to_vec2(), (d.p1 - d.p0) * 2.0, d.p0.to_vec2());
|
||||
let mut far: f64 = 0.0;
|
||||
// Where the curve moves along the chord's line or across it, its distance across it or its
|
||||
// position along it is largest or smallest.
|
||||
for (a2, a1, a0) in [(k2.cross(u), k1.cross(u), k0.cross(u)), (k2.dot(u), k1.dot(u), k0.dot(u))] {
|
||||
for t in roots(a2, a1, a0) {
|
||||
if t > a && t < b {
|
||||
let v = c.eval(t) - p;
|
||||
let ahead = v.dot(u);
|
||||
far = far.max(v.cross(u).abs()).max(-ahead).max(ahead - len);
|
||||
}
|
||||
}
|
||||
}
|
||||
t.push(1.0);
|
||||
(t, (pin_start, pin_end))
|
||||
far
|
||||
}
|
||||
|
||||
/// The real roots of `a2 t² + a1 t + a0`, with NaN in place of a root that doesn't exist (none when
|
||||
/// every coefficient is 0).
|
||||
fn roots(a2: f64, a1: f64, a0: f64) -> [f64; 2] {
|
||||
if a2 == 0.0 {
|
||||
return [-a0 / a1, f64::NAN];
|
||||
}
|
||||
let disc = a1 * a1 - 4.0 * a2 * a0;
|
||||
if disc < 0.0 {
|
||||
return [f64::NAN; 2];
|
||||
}
|
||||
// The root that adds two numbers of the same sign, and the other from their product.
|
||||
let r = -(a1 + a1.signum() * disc.sqrt()) / 2.0;
|
||||
if r == 0.0 {
|
||||
return [0.0, f64::NAN];
|
||||
}
|
||||
[r / a2, a0 / r]
|
||||
}
|
||||
|
||||
/// How far the curve `c` lies from the chord between parameters `a` and `b`, at its parameter
|
||||
|
||||
@@ -108,6 +108,31 @@ fn thin_strokes_drawn_as_lines_stop_where_a_curve_turns_back() {
|
||||
assert!(far <= 75.0 + w / 2.0 + 1e-6, "the outline reaches x = {far}");
|
||||
}
|
||||
|
||||
/// A thin curve that turns back twice keeps both of its turns. The curve runs from (0, 0) out to
|
||||
/// x = 28.87, back past its start to x = -28.87, and back to (0, 0.001). Its outline covers every
|
||||
/// point of the curve more than a third of the stroke's width from a turn, reaches both turns to
|
||||
/// within that, and reaches no further than two thirds of the stroke's width past either turn.
|
||||
#[test]
|
||||
fn thin_strokes_drawn_as_lines_keep_both_turns_of_a_curve() {
|
||||
let (w, tol, e) = (0.5, 0.25, 1e-3);
|
||||
let c = kurbo::CubicBez::new((0.0, 0.0), (100.0, e / 3.0), (-100.0, 2.0 * e / 3.0), (0.0, e));
|
||||
let mut path = BezPath::new();
|
||||
path.move_to(c.p0);
|
||||
path.curve_to(c.p1, c.p2, c.p3);
|
||||
let st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
|
||||
let o = raster_stroke_outline(&path, &st, w, tol);
|
||||
assert!(lines_only(&o), "lines only");
|
||||
let turn = kurbo::ParamCurve::eval(&c, (3.0 - 3f64.sqrt()) / 6.0).x;
|
||||
let near = w / 3.0;
|
||||
for k in 1..200 {
|
||||
let p = kurbo::ParamCurve::eval(&c, f64::from(k) / 200.0);
|
||||
assert!(p.x.abs() > turn - near || o.winding(p) != 0, "the curve at {p:?}");
|
||||
}
|
||||
let (bounds, past) = (o.bounding_box(), w * 2.0 / 3.0);
|
||||
assert!(bounds.x1 >= turn - near && bounds.x0 <= -turn + near, "the outline reaches both turns: {bounds:?}");
|
||||
assert!(bounds.x1 <= turn + past && bounds.x0 >= -turn - past, "the outline spans {bounds:?}");
|
||||
}
|
||||
|
||||
/// Independently integrate coverage in radial/angle bins of an original notched ring. Checking
|
||||
/// only the outline geometry misses raster flattening that cancels its two nearby contours.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user