M4, #2731: the step beside a segment is shortened until the two sides differ as the loops say they must

From the review of #2732: any fixed step reaches past the other edge of a shape thin
enough. The winding numbers on the two sides of a segment differ by one for each time a
loop goes along it, so a step that crossed another edge shows as the wrong parity and is
quartered, up to twelve times; then the loops decide.
This commit is contained in:
Livio Sidler committed 2026-11-29 13:10:05 +01:00
1 parent 8b509fc155
commit 6d3bd1c88c
2 files changed
+42 -23

No files matched your search

+36 -19
View File
@@ -35,11 +35,9 @@ pub(crate) fn outline(net: &VectorNetwork, props: &StrokeProps, align: StrokeAli
let fill = net.fill_path();
// A segment is placed by what is filled beside its middle, a little way off to either side:
// the loops alone don't say (a loop inside another of the same direction bounds nothing).
let uses = loop_uses(net);
let beside = |rows: &Rows| -> Vec<Place> {
let filled = |p: Point| {
let w = rows.winding(p);
if even_odd { w % 2 != 0 } else { w != 0 }
};
let filled = |w: i32| if even_odd { w % 2 != 0 } else { w != 0 };
(0..net.segments.len())
.map(|i| {
let Some(c) = net.cubic(i) else { return Place::Beyond };
@@ -47,19 +45,35 @@ pub(crate) fn outline(net: &VectorNetwork, props: &StrokeProps, align: StrokeAli
if along.hypot2() == 0.0 {
along = c.p3 - c.p0;
}
// How far off: the least that tells the two sides apart in the numbers the
// point is written in, which is less than anything drawn is thick. (A share of
// the network's size or of the segment's length steps over a thin shape.)
let across = Vec2::new(-along.y, along.x).normalize();
let used = uses.get(i).copied().unwrap_or(0);
// How far off: a small share of the numbers the point is written in to begin
// with, and less until the two sides differ as the loops say they must (by one
// winding for each time the segment is in a loop, so odd or even with them).
// A step that reaches past a thin shape's other edge doesn't, and is shortened.
let length = (c.p1 - c.p0).hypot() + (c.p2 - c.p1).hypot() + (c.p3 - c.p2).hypot();
let off = at.x.abs().max(at.y.abs()).max(length) * PROBE;
let side = Vec2::new(-along.y, along.x).normalize() * off;
if !(side.is_finite() && off > 0.0) {
return if filled(at) { Place::Within } else { Place::Beyond };
let mut off = at.x.abs().max(at.y.abs()).max(length) * PROBE;
for _ in 0..PROBES {
let side = across * off;
if !side.is_finite() || at + side == at {
break;
}
let (one, other) = (rows.winding(at + side), rows.winding(at - side));
if (one - other).rem_euclid(2) as usize == used % 2 {
return match (filled(one), filled(other)) {
(true, true) => Place::Within,
(false, false) => Place::Beyond,
_ => Place::Edge,
};
}
off /= 4.0;
}
match (filled(at + side), filled(at - side)) {
(true, true) => Place::Within,
(false, false) => Place::Beyond,
_ => Place::Edge,
// No step tells: by the loops, as below.
match used {
0 if filled(rows.winding(at)) => Place::Within,
0 => Place::Beyond,
u if u % 2 == 1 => Place::Edge,
_ => Place::Within,
}
})
.collect()
@@ -69,7 +83,7 @@ pub(crate) fn outline(net: &VectorNetwork, props: &StrokeProps, align: StrokeAli
// shared by regions, in none a line that is stroked on its centre and never cut.
let places: Vec<Place> = match Rows::new(&fill, net.segments.len() * 2) {
Some(rows) => beside(&rows),
None => loop_uses(net)
None => uses
.iter()
.map(|u| match u {
0 => Place::Beyond,
@@ -180,11 +194,14 @@ fn trimmed(
})
}
/// How far beside a segment's middle the fill is asked, as a share of the larger of the middle's
/// coordinates and the segment's length: well above what a winding number is worked out to (about
/// 1e-16 of the coordinates), and below what the renderer's own numbers can tell apart (6e-8).
/// How far beside a segment's middle the fill is asked at first, as a share of the larger of the
/// middle's coordinates and the segment's length: well above what a winding number is worked out
/// to (about 1e-16 of the coordinates).
const PROBE: f64 = 1e-7;
/// How often that step is shortened to a quarter, at most, before the loops decide.
const PROBES: usize = 12;
/// Segments looked at, at most, to place a network's segments by the fill beside them.
const MAX_LOOKS: usize = 50_000_000;
+6 -4
View File
@@ -212,15 +212,17 @@ fn rectangles(at: &[(f64, f64, f64, f64)]) -> VectorNetwork {
#[test]
fn thin_and_long_shapes_keep_their_aligned_strokes() {
// Long and far across with another above it, and thin beside a far one: neither the
// network's size nor an edge's length says how thick the shape beside the edge is.
// network's size, nor an edge's length, nor how far from the origin it lies says how thick
// the shape beside the edge is. (x, the rectangle's thickness, the weight.)
let long = rectangles(&[(0.0, 0.0, 2_000_000.0, 10.0), (0.0, 100.0, 2_000_000.0, 10.0)]);
let thin = rectangles(&[(0.0, 0.0, 10.0, 0.004), (2000.0, 0.0, 10.0, 0.004)]);
for (net, weight, x) in [(&long, 4.0, 1_000_000.0), (&thin, 0.002, 5.0)] {
let flat = rectangles(&[(0.0, 0.0, 2_000_000.0, 0.1), (0.0, 100.0, 2_000_000.0, 0.1)]);
let thin = rectangles(&[(0.0, 0.0, 10.0, 0.004), (2_000_000.0, 0.0, 10.0, 0.004)]);
for (net, weight, x) in [(&long, 4.0, 1_000_000.0), (&flat, 0.04, 1_000_000.0), (&thin, 0.002, 5.0), (&thin, 0.002, 2_000_005.0)] {
for align in [StrokeAlign::Inside, StrokeAlign::Outside] {
let props = StrokeProps { weight, align, ..Default::default() };
let area = crate::stroke_outline_of(&net.stroke_path(), &props, true, Some(net));
let (within, beyond) = (painted(&area, x, weight / 4.0), painted(&area, x, -weight / 4.0));
assert_eq!((within, beyond), (align == StrokeAlign::Inside, align == StrokeAlign::Outside), "{align:?}, weight {weight}");
assert_eq!((within, beyond), (align == StrokeAlign::Inside, align == StrokeAlign::Outside), "{align:?}, weight {weight} at {x}");
}
}
}