Files
vectorcraft/crates/render/src/tests/thincoverage.rs
T
dotCooCoo 45de39a138 Render: a thin stroke takes the corners of its own subpath
A corner in one subpath gave the stroke's join to the lines of a curve
in another subpath that met at the same point, so a miter could be
drawn there instead of a round join. The corners are now kept per
subpath and each subpath of the outline reads only its own.

Test: a curve's outline is the same with and without a second subpath
that has a corner at the curve's start.

(cherry picked from commit f1ef1a4e39)
2026-12-01 08:56:09 +01:00

398 lines
20 KiB
Rust

use super::*;
use crate::stroke::outline as raster_stroke_outline;
use vectorcraft_doc::{LineCap, LineJoin, StrokeLayer};
/// How many lines a fill of `path` takes once flattened at `tol`.
fn lines_at(path: &BezPath, tol: f64) -> usize {
let mut n = 0;
vectorcraft_geom::flatten(path.iter(), tol, |el| n += usize::from(matches!(el, kurbo::PathEl::LineTo(_) | kurbo::PathEl::ClosePath)));
n
}
/// Whether `path` holds lines only.
fn lines_only(path: &BezPath) -> bool {
path.elements().iter().all(|el| !matches!(el, kurbo::PathEl::QuadTo(..) | kurbo::PathEl::CurveTo(..)))
}
/// A thin curved stroke's outline takes at most twice the lines the stroke's plain outline takes
/// at the raster tolerance (#7257). 200 cubic curves, each about 100 device pixels across.
#[test]
fn thin_curved_strokes_take_at_most_twice_the_lines_of_their_plain_outline() {
let mut seed: u64 = 7;
let mut r = |lo: f64, hi: f64| {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
lo + (hi - lo) * ((seed >> 11) as f64 / (1u64 << 53) as f64)
};
let mut paths = Vec::new();
for _ in 0..200 {
let (x, y) = (r(20.0, 980.0), r(20.0, 980.0));
let mut q = [kurbo::Point::ZERO; 3];
for p in &mut q {
*p = kurbo::Point::new((x + r(-30.0, 30.0)) * 1.71, (y + r(-30.0, 30.0)) * 1.71);
}
let mut path = BezPath::new();
path.move_to((x * 1.71, y * 1.71));
path.curve_to(q[0], q[1], q[2]);
paths.push(path);
}
let tol = 0.25;
for width in [0.5, 0.75, 0.99] {
let st = StrokeLayer::new(Paint::solid(Color::BLACK), width);
let (mut plain, mut thin) = (0, 0);
for path in &paths {
plain += lines_at(&crate::effects::stroke::line_outline(path, &st, width, tol), tol);
let outline = raster_stroke_outline(path, &st, width, tol);
assert!(lines_only(&outline), "width {width}: lines only");
thin += lines_at(&outline, tol);
}
assert!(thin <= plain * 2, "width {width}: {thin} lines, plain outline {plain}");
}
}
/// A thin stroke drawn as lines keeps its caps, its joins and the hole of a closed path.
#[test]
fn thin_strokes_drawn_as_lines_keep_caps_joins_and_holes() {
let (w, tol) = (0.5, 0.25);
// A quarter circle from (100, 100) heading right, then a right-angle corner at (200, 200).
let mut open = BezPath::new();
open.move_to((100.0, 100.0));
open.curve_to((155.23, 100.0), (200.0, 144.77), (200.0, 200.0));
open.line_to((100.0, 200.0));
for cap in [LineCap::Butt, LineCap::Round, LineCap::Square] {
for join in [LineJoin::Miter, LineJoin::Round, LineJoin::Bevel] {
let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
st.cap = cap;
st.join = join;
let o = raster_stroke_outline(&open, &st, w, tol);
assert!(lines_only(&o), "{cap:?} {join:?}: lines only");
let inside = |x: f64, y: f64| o.winding(kurbo::Point::new(x, y)) != 0;
assert_eq!(inside(100.0 - 0.4 * w, 100.0), cap != LineCap::Butt, "{cap:?} {join:?}: start cap");
assert!(!inside(100.0 - 0.6 * w, 100.0), "{cap:?} {join:?}: past the start cap");
assert_eq!(inside(100.0 - 0.45 * w, 100.0 + 0.45 * w), cap == LineCap::Square, "{cap:?} {join:?}: cap corner");
assert!(
inside(200.0 + 0.4 * w, 199.0) && inside(200.0 - 0.4 * w, 199.0) && !inside(200.0 + 0.6 * w, 199.0),
"{cap:?} {join:?}: curve end"
);
assert_eq!(inside(200.0 + 0.3 * w, 200.0 + 0.3 * w), join != LineJoin::Bevel, "{cap:?} {join:?}: corner");
assert_eq!(inside(200.0 + 0.45 * w, 200.0 + 0.45 * w), join == LineJoin::Miter, "{cap:?} {join:?}: miter tip");
assert!(inside(150.0, 200.0 + 0.4 * w) && !inside(150.0, 200.0 + 0.6 * w), "{cap:?} {join:?}: line");
}
}
let ring = kurbo::Circle::new((300.0, 300.0), 50.0).to_path(0.01);
let st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
let o = raster_stroke_outline(&ring, &st, w, tol);
assert!(lines_only(&o), "closed: lines only");
for k in 0..36 {
let a = f64::from(k) * 10f64.to_radians();
let at = |r: f64| o.winding(kurbo::Point::new(300.0 + r * a.cos(), 300.0 + r * a.sin()));
assert!(at(50.0 - 0.4 * w) != 0 && at(50.0 + 0.4 * w) != 0, "ring band at {k}");
assert!(at(50.0 - 0.6 * w) == 0 && at(50.0 + 0.6 * w) == 0, "ring edges at {k}");
}
assert_eq!(o.winding(kurbo::Point::new(300.0, 300.0)), 0, "the hole");
}
/// A thin curve that runs out and back along almost the same line ends where the curve turns
/// back. The curve goes from (0, 0) to x = 75 and back; its outline reaches no further than half
/// the stroke's width past that.
#[test]
fn thin_strokes_drawn_as_lines_stop_where_a_curve_turns_back() {
let (w, tol, e) = (0.5, 0.25, 1e-9);
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.curve_to((100.0, e / 3.0), (100.0, 2.0 * e / 3.0), (0.0, e));
let st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
let o = raster_stroke_outline(&path, &st, w, tol);
let inside = |x: f64| o.winding(kurbo::Point::new(x, 0.0)) != 0;
assert!(inside(10.0) && inside(70.0), "the band");
let far = o.bounding_box().x1;
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:?}");
}
/// A path with a bend at (10, 0) and then six smooth anchors `gap` apart, each with handles
/// `handle` and 1.7 times `handle` long, scaled by `zoom`, and its points flattened within 0.001.
fn close_smooth_anchors(gap: f64, handle: f64, zoom: f64) -> (BezPath, Vec<kurbo::Point>) {
let path = close_smooth_path(gap, handle, zoom);
let mut line = Vec::new();
vectorcraft_geom::flatten(path.iter(), 1e-3, |el| {
if let kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) = el {
line.push(p);
}
});
(path, line)
}
/// How far `q` is from the nearest point of `path`.
fn from_curve(path: &BezPath, q: kurbo::Point) -> f64 {
use kurbo::ParamCurveNearest;
path.segments().map(|s| s.nearest(q, 1e-9).distance_sq.sqrt()).fold(f64::INFINITY, f64::min)
}
/// The path of [`close_smooth_anchors`].
fn close_smooth_path(gap: f64, handle: f64, zoom: f64) -> BezPath {
use vectorcraft_geom::path::{Anchor, AnchorKind, PathData, SubPath};
let dir = kurbo::Vec2::new(0.8, 0.6);
let mut anchors = vec![
Anchor::corner(kurbo::Point::new(0.0, 0.0)),
Anchor::with_handles(kurbo::Point::new(10.0, 0.0), kurbo::Point::new(10.0, 0.0), kurbo::Point::new(10.0, 5.0)),
];
let mut x = 10.0;
for i in 0..6 {
x += gap;
let p = kurbo::Point::new(x, 1.0 + if i % 2 == 0 { 0.3 } else { -0.2 } * gap);
anchors.push(Anchor { p, h_in: p - dir * handle, h_out: p + dir * handle * 1.7, kind: AnchorKind::Smooth });
}
anchors.push(Anchor::smooth(kurbo::Point::new(x + 20.0, 0.0), kurbo::Point::new(x + 25.0, -4.0)));
kurbo::Affine::scale(zoom) * PathData::single(SubPath::new(anchors, false)).to_bezpath()
}
/// A thin stroke along smooth anchors that sit close together after a bend follows the curve: its
/// outline has no point farther than a stroke's width from the curve, and covers the curve. The
/// lines such a curve is drawn with are short beside the stroke and turn sharply, so offsetting
/// them drew the stroke's miter joins as spikes.
#[test]
fn thin_strokes_follow_smooth_anchors_close_together() {
for gap in [0.1, 1.0] {
let (path, line) = close_smooth_anchors(gap, 1.0, 1.0);
for w in [0.6, 0.9] {
let st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
let o = raster_stroke_outline(&path, &st, w, 0.25);
for el in o.elements() {
if let kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) = *el {
let d = from_curve(&path, p);
assert!(d <= w, "gap {gap}, width {w}: outline point {p:?} is {d} from the curve");
}
}
for p in line.iter().step_by(7) {
assert!(o.winding(*p) != 0, "gap {gap}, width {w}: the curve at {p:?} is not covered");
}
}
}
}
/// A thin stroke's outline stays within the stroke's width of its curve where the lines the curve is
/// drawn with turn sharply, as near a cusp: they meet with a round join, not the stroke's miter.
/// 200 cubic curves, each about 100 device pixels across.
#[test]
fn thin_strokes_meet_round_where_a_curves_lines_turn_sharply() {
let mut seed: u64 = 7;
let mut r = |lo: f64, hi: f64| {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
lo + (hi - lo) * ((seed >> 11) as f64 / (1u64 << 53) as f64)
};
let w = 0.5;
let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
st.join = LineJoin::Miter;
st.miter_limit = 10.0;
for _ in 0..200 {
let (x, y) = (r(20.0, 980.0), r(20.0, 980.0));
let mut q = [kurbo::Point::ZERO; 3];
for p in &mut q {
*p = kurbo::Point::new((x + r(-30.0, 30.0)) * 1.71, (y + r(-30.0, 30.0)) * 1.71);
}
let mut path = BezPath::new();
path.move_to((x * 1.71, y * 1.71));
path.curve_to(q[0], q[1], q[2]);
let o = raster_stroke_outline(&path, &st, w, 0.25);
for el in o.elements() {
if let kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) = *el {
let d = from_curve(&path, p);
assert!(d <= w, "outline point {p:?} is {d} from the curve {path:?}");
}
}
}
}
/// A thin stroke along smooth anchors close together, with handles long beside the gaps between
/// them, covers what the curve's own outline covers: at every point of a grid more than 0.25 pixels
/// nearer or farther than half the stroke's width from the curve, both outlines agree. The miter
/// joins drawn where the curve's lines turned sharply covered points well outside the stroke.
#[test]
fn thin_strokes_along_long_handles_cover_the_curve_outline() {
let tol = 0.25;
for (gap, w) in [(1.0, 0.6), (1.0, 0.9), (0.1, 0.6)] {
let path = close_smooth_path(gap, 10.0, 4.0);
let st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
let o = raster_stroke_outline(&path, &st, w, tol);
let fine = (tol / 8.0).min(w / 32.0);
let mut want = BezPath::new();
vectorcraft_geom::flatten(crate::effects::stroke::line_outline(&path, &st, w, fine).iter(), fine, |el| want.push(el));
let bb = want.bounding_box().inflate(w, w);
for i in 0..90 {
for j in 0..90 {
let q = kurbo::Point::new(bb.x0 + bb.width() * f64::from(i) / 90.0, bb.y0 + bb.height() * f64::from(j) / 90.0);
let d = from_curve(&path, q);
if (d - w / 2.0).abs() > 0.25 {
assert_eq!(o.winding(q) != 0, want.winding(q) != 0, "gap {gap}, width {w}: at {q:?}, {d} from the curve");
}
}
}
}
}
/// A corner of one subpath does not change the join where the lines of another subpath's curve
/// meet at the same point: the first subpath's outline is the same with or without the second.
#[test]
fn thin_strokes_take_corners_from_their_own_subpath() {
let w = 0.5;
let mut st = StrokeLayer::new(Paint::solid(Color::BLACK), w);
st.join = LineJoin::Miter;
st.miter_limit = 10.0;
let mut alone = BezPath::new();
alone.move_to((-1.0, 0.0));
alone.line_to((0.0, 0.0));
alone.curve_to((0.000001, 0.0), (-1.0, 0.5), (-2.0, 1.0));
let mut both = alone.clone();
both.move_to((-10.0, -10.0));
both.line_to((0.0, 0.0));
both.line_to((-10.0, 10.0));
let one = raster_stroke_outline(&alone, &st, w, 0.25);
let two = raster_stroke_outline(&both, &st, w, 0.25);
assert_eq!(two.elements().get(..one.elements().len()), Some(one.elements()), "the second subpath changed the first one's outline");
}
/// 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]
fn fractional_curved_strokes_retain_coverage_between_notches() {
for rounded in [false, true] {
for (weight, rotation, phase, opacity) in
[(1.0 / 3.0, 0.0, 0.0, 1.0), (0.5, 30.0, 0.25, 1.0), (0.75, 75.0, 0.5, 0.5), (1.0, 135.0, 0.75, 1.0)]
{
let mut d = Document::new(1000.0, 1000.0);
let mut path = BezPath::new();
let mut radius = 450.0;
let mut previous = 0.0;
path.move_to((950.0, 500.0));
let mut arcs = Vec::new();
let point = |r: f64, a: f64| vectorcraft_geom::Point::new(500.0 + r * a.cos(), 500.0 + r * a.sin());
for degrees in [17.0_f64, 64.0, 119.0, 147.0, 201.0, 242.0, 313.0, 360.0] {
let a = degrees.to_radians();
let k = 4.0 / 3.0 * ((a - previous) / 4.0).tan();
let start = point(radius, previous);
let end = point(radius, a);
path.curve_to(
start + kurbo::Vec2::new(-previous.sin(), previous.cos()) * (k * radius),
end + kurbo::Vec2::new(a.sin(), -a.cos()) * (k * radius),
end,
);
arcs.push((radius, previous + 0.06, a - 0.06));
radius = if radius == 450.0 { 438.0 } else { 450.0 };
path.line_to(point(radius, a));
previous = a;
}
path.close_path();
// Match a parser's f32 coordinate round-trip and exact closed endpoint as well as the
// original f64 construction. A near-coincident final anchor must not hide this case.
let q = |p: kurbo::Point| kurbo::Point::new(f64::from(p.x as f32), f64::from(p.y as f32));
let path = if rounded {
BezPath::from_vec(
path.iter()
.map(|el| match el {
kurbo::PathEl::MoveTo(p) => kurbo::PathEl::MoveTo(q(p)),
kurbo::PathEl::LineTo(p) => kurbo::PathEl::LineTo(q(p)),
kurbo::PathEl::QuadTo(a, b) => kurbo::PathEl::QuadTo(q(a), q(b)),
kurbo::PathEl::CurveTo(a, b, c) => kurbo::PathEl::CurveTo(q(a), q(b), q(c)),
kurbo::PathEl::ClosePath => kurbo::PathEl::ClosePath,
})
.collect(),
)
} else {
path
};
{
use kurbo::{ParamCurve, ParamCurveDeriv};
let st = StrokeLayer::new(Paint::solid(Color::BLACK), weight);
let outline = raster_stroke_outline(&path, &st, weight.max(0.5), 0.25);
for segment in path.segments() {
if let kurbo::PathSeg::Cubic(c) = segment {
for step in 1..20 {
let t = step as f64 / 20.0;
let p = c.eval(t);
let v = c.deriv().eval(t).to_vec2().normalize();
let n = kurbo::Vec2::new(-v.y, v.x);
for side in [-1.0, 1.0] {
assert_ne!(outline.winding(p + n * (side * weight.max(0.5) * 0.4)), 0, "missing geometric band, rounded={rounded}");
assert_eq!(outline.winding(p + n * (side * weight.max(0.5) * 0.6)), 0, "outside geometric band, rounded={rounded}");
}
}
}
}
}
let mut node = Node::path(
d.alloc_id(),
vectorcraft_geom::PathData::from_bezpath(&path),
Appearance::basic(Paint::None, Paint::solid(Color::BLACK), weight),
);
node.opacity = opacity;
d.insert(Some(d.layers[0].id), 0, node).unwrap();
let view =
Affine::translate((500.0 + phase, 500.0 + phase)) * Affine::rotate(f64::to_radians(rotation)) * Affine::translate((-500.0, -500.0));
let image =
Renderer::new().render(&d, 1000, 1000, view, &RenderOptions { background: Some([255; 4]), artboards: false, ..Default::default() });
// Collect only the measured radial bands once, in the original row-major order.
// Each bin then adds the same pixels in the same order without rescanning the image.
let mut ring = Vec::new();
for y in 0..1000 {
for x in 0..1000 {
let dx = x as f64 + 0.5 - 500.0 - phase;
let dy = y as f64 + 0.5 - 500.0 - phase;
let distance = dx.hypot(dy);
if !arcs.iter().any(|(r, _, _)| (distance - r).abs() <= 3.0) {
continue;
}
let angle = (dy.atan2(dx) - f64::to_radians(rotation)).rem_euclid(std::f64::consts::TAU);
ring.push((distance, angle, f64::from(255 - image.pixel(x, y)[0]) / 255.0));
}
}
for (r, begin, end) in arcs {
// 3 bins per arc avoid averaging away a washed-out section. The expected ink area
// is independently r * angle * displayed weight, with the existing half-pixel floor.
for bin in 0..3 {
let lo = begin + (end - begin) * bin as f64 / 3.0;
let hi = begin + (end - begin) * (bin + 1) as f64 / 3.0;
let mut ink = 0.0;
for &(distance, angle, value) in &ring {
if (distance - r).abs() > 3.0 {
continue;
}
if angle >= lo && angle < hi {
ink += value;
}
}
let expected = r * (hi - lo) * weight.max(0.5) * f64::from(opacity);
assert!(
(ink / expected - 1.0).abs() < 0.15,
"weight {weight}, rotation {rotation}, phase {phase}, opacity {opacity}, arc {r} {lo}..{hi}: coverage {}",
ink / expected
);
}
}
}
}
}