From ae4269a504c2b886bb09ab5bb1dc245d29a9a44b Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Tue, 17 Nov 2026 20:25:23 -0300 Subject: [PATCH 1/3] fix(labels): width check and small rings for curved polygon labels (#2262) Follow-up to #2295, from the review findings confirmed after its last push: - A stretch of the centre line counts as wide enough only when (depth - 1) * cell >= half the text's height: a cell at depth d is d cells from the middle of the nearest outside cell, and the outline runs somewhere between the two. A 600 x 10 rectangle no longer takes 12 pt text on a line at y = 5.625. - thin keeps rings of fewer than 64 points whole: a 15-point hole read one point in five became a triangle of exactly half its area, losing the corners (50, 50) and (550, 50). - The ring area in thin is a shoelace sum over the iterator, no Vec. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/area_labels.rs | 68 ++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/crates/render/src/area_labels.rs b/crates/render/src/area_labels.rs index b8364fad7..a36bfee35 100644 --- a/crates/render/src/area_labels.rs +++ b/crates/render/src/area_labels.rs @@ -22,20 +22,30 @@ use crate::line_labels::{self, Line}; pub(super) const MAX_SIDE: usize = 160; /// About the most outline vertices projected: more are dropped evenly, as finer detail than a cell -/// is lost on the grid anyway. A ring keeps at least three points and its shape, so many small -/// rings (a multipolygon's islands, holes) can keep more than this. +/// is lost on the grid anyway. A small ring is kept whole and a longer one keeps its shape, so many +/// small rings (a multipolygon's islands, holes) can keep more than this. const MAX_VERTICES: usize = 20_000; -/// Every `every`th point of `ring` (at `xy`), but at least three of it, so a small hole or island -/// next to a long outline keeps its place, and fewer dropped while that would lose half its area, -/// so a ring dense along one side is not left a line with no inside (#2295). +/// A ring with fewer points than this is never thinned: a small hole or island keeps every corner. +const KEEP_WHOLE: usize = 64; + +/// Every `every`th point of `ring` (at `xy`), but all of a ring of fewer than [`KEEP_WHOLE`] points +/// and at least three of a longer one, so a small hole or island next to a long outline keeps its +/// place and its corners, and fewer dropped while that would lose half its area, so a ring dense +/// along one side is not left a line with no inside (#2295). fn thin(ring: &[T], every: usize, xy: impl Fn(&T) -> (f64, f64)) -> Vec { + // The area within every `step`th point (the shoelace sum, back to the first point). let area = |step: usize| { - let points: Vec<(f64, f64)> = ring.iter().step_by(step).map(&xy).collect(); - let twice: f64 = points.iter().zip(points.iter().cycle().skip(1)).map(|(a, b)| a.0 * b.1 - b.0 * a.1).sum(); - twice.abs() / 2.0 + let mut points = ring.iter().step_by(step).map(&xy); + let Some(first) = points.next() else { return 0.0 }; + let (mut prev, mut twice) = (first, 0.0); + for p in points.chain(std::iter::once(first)) { + twice += prev.0 * p.1 - p.0 * prev.1; + prev = p; + } + f64::abs(twice) / 2.0 }; - let mut step = every.min(ring.len() / 3).max(1); + let mut step = if ring.len() < KEEP_WHOLE { 1 } else { every.min(ring.len() / 3).max(1) }; if step > 1 { let whole = area(1); while step > 1 && area(step) < whole / 2.0 { @@ -242,8 +252,11 @@ pub(super) fn centre_line(rings: &[Vec<(f32, f32)>], w: f32, h: f32, half_width: let end = cells.len().checked_sub(keep(&cells) + 1)?; cells.reverse(); // Of what is left, the longest stretch where the polygon is wide enough all along: a narrow - // neck between two wide parts (a dumbbell) is never under the text (#2295). - let cells = cells.get(start..=end)?.split(|i| deep(i) * cell < half_width).max_by_key(|run| run.len()).filter(|run| run.len() >= 2)?; + // neck between two wide parts (a dumbbell) is never under the text (#2295). A cell at depth `d` + // is `d` cells from the middle of the nearest outside cell, and the outline runs somewhere + // between the two: only `d - 1` cells of it are sure to be inside. + let wide = |i: &usize| (deep(i) - 1.0) * cell >= half_width; + let cells = cells.get(start..=end)?.split(|i| !wide(i)).max_by_key(|run| run.len()).filter(|run| run.len() >= 2)?; // Cell centres, softened over two cells either side so the grid's steps don't turn the text. let pts: Vec<(f32, f32)> = cells.iter().map(|i| (ox + ((i % nx) as f32 + 0.5) * cell, oy + ((i / nx) as f32 + 0.5) * cell)).collect(); let soft: Vec<(f32, f32)> = (0..pts.len()) @@ -466,6 +479,39 @@ mod tests { assert!(through.iter().any(|p| p.0 < 250.0) && through.iter().any(|p| p.0 > 350.0), "{through:?}"); } + /// A small hole is kept whole when its outer ring is thinned: one point in five of it would be + /// a triangle of half its area, without its lower corners. + #[test] + fn a_small_hole_keeps_its_corners() { + // 100 000 points round a 600 × 100 rectangle, and a 15-point hole across its upper half, + // with a point at each corner. + let n = 25_000; + let mut outer: Vec<(f32, f32)> = (0..n).map(|i| (i as f32 * 600.0 / n as f32, 0.0)).collect(); + outer.extend((0..n).map(|i| (600.0, i as f32 * 100.0 / n as f32))); + outer.extend((0..n).map(|i| (600.0 - i as f32 * 600.0 / n as f32, 100.0))); + outer.extend((0..n).map(|i| (0.0, 100.0 - i as f32 * 100.0 / n as f32))); + let mut hole: Vec<(f32, f32)> = (0..=5).map(|i| (50.0 + i as f32 * 100.0, 10.0)).collect(); + hole.extend((0..9).map(|i| (550.0 - i as f32 * 62.5, 50.0))); + assert_eq!(hole.len(), 15); + let every = every(outer.len() + hole.len()); + let kept = thin(&hole, every, |p| (f64::from(p.0), f64::from(p.1))); + assert!(kept.contains(&(50.0, 50.0)) && kept.contains(&(550.0, 50.0)), "{kept:?}"); + let line = centre_line(&[outer, hole], 1000.0, 1000.0, 4.0).expect("below the hole"); + assert!(line.iter().filter(|p| (100.0..500.0).contains(&p.0)).all(|p| p.1 > 55.0), "{line:?}"); + } + + /// A rectangle 10 points tall is too narrow for text 12 points tall, though its middle row of + /// cells is two cells (7.5 points) deep on the grid: the outline may be nearly a cell nearer. + #[test] + fn a_band_just_too_narrow_gives_no_line() { + let thin_band = vec![(0.0, 0.0), (600.0, 0.0), (600.0, 10.0), (0.0, 10.0)]; + let line = centre_line(std::slice::from_ref(&thin_band), 1000.0, 1000.0, 6.0); + assert!(line.is_none(), "{line:?}"); + // Text thin enough for it gets a line down its middle. + let line = centre_line(&[thin_band], 1000.0, 1000.0, 3.0).expect("down its middle"); + assert!(line.iter().all(|p| (p.1 - 5.0).abs() < 2.0), "{line:?}"); + } + #[test] fn nothing_inside_or_too_narrow_gives_no_line() { assert!(centre_line(&[], 100.0, 100.0, 1.0).is_none()); From 82c694758e80123228ca5a263ea4f001031c5efb Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Wed, 18 Nov 2026 00:51:04 -0300 Subject: [PATCH 2/3] fix(labels): budget small rings kept whole; cover the half-area guard (#2370 review) - Small rings are kept whole only while they add up to at most half of MAX_VERTICES: a multipolygon of thousands of small islands is thinned as before instead of projected point by point each frame. - a_hole_dense_along_one_side_keeps_its_shape now uses a 65-point hole, long enough to be thinned, so the half-area guard is tested again (it fails with the guard turned off). - New test many_small_islands_are_thinned. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/area_labels.rs | 82 +++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/crates/render/src/area_labels.rs b/crates/render/src/area_labels.rs index a36bfee35..67161b114 100644 --- a/crates/render/src/area_labels.rs +++ b/crates/render/src/area_labels.rs @@ -22,18 +22,44 @@ use crate::line_labels::{self, Line}; pub(super) const MAX_SIDE: usize = 160; /// About the most outline vertices projected: more are dropped evenly, as finer detail than a cell -/// is lost on the grid anyway. A small ring is kept whole and a longer one keeps its shape, so many -/// small rings (a multipolygon's islands, holes) can keep more than this. +/// is lost on the grid anyway. Small rings kept whole may add half as many again, and a longer ring +/// keeps at least three points and its shape, so a polygon of many rings can keep a few more. const MAX_VERTICES: usize = 20_000; -/// A ring with fewer points than this is never thinned: a small hole or island keeps every corner. +/// A ring with fewer points than this is not thinned, while such rings add up to at most half of +/// [`MAX_VERTICES`]: a small hole or island keeps every corner. const KEEP_WHOLE: usize = 64; -/// Every `every`th point of `ring` (at `xy`), but all of a ring of fewer than [`KEEP_WHOLE`] points -/// and at least three of a longer one, so a small hole or island next to a long outline keeps its -/// place and its corners, and fewer dropped while that would lose half its area, so a ring dense -/// along one side is not left a line with no inside (#2295). -fn thin(ring: &[T], every: usize, xy: impl Fn(&T) -> (f64, f64)) -> Vec { +/// How a polygon's rings are thinned before they are projected: one point in `every` kept, and +/// rings of fewer than `whole_below` points kept whole. +#[derive(Clone, Copy)] +struct Thinning { + every: usize, + whole_below: usize, +} + +impl Thinning { + /// For rings of `lens` points: one point in as many as keeps about [`MAX_VERTICES`] of them all, + /// and the small rings whole unless there are so many of them (thousands of small islands) that + /// they would take more than half of that. + fn new(lens: impl Iterator) -> Self { + let (mut all, mut small) = (0, 0); + for n in lens { + all += n; + if n < KEEP_WHOLE { + small += n; + } + } + Thinning { every: all.div_ceil(MAX_VERTICES).max(1), whole_below: if small <= MAX_VERTICES / 2 { KEEP_WHOLE } else { 0 } } + } +} + +/// Every `every`th point of `ring` (at `xy`), but all of a small ring (see [`Thinning`]) and at least +/// three of a longer one, so a small hole or island next to a long outline keeps its place and its +/// corners, and fewer dropped while that would lose half its area, so a ring dense along one side is +/// not left a line with no inside (#2295). +fn thin(ring: &[T], how: Thinning, xy: impl Fn(&T) -> (f64, f64)) -> Vec { + let Thinning { every, whole_below } = how; // The area within every `step`th point (the shoelace sum, back to the first point). let area = |step: usize| { let mut points = ring.iter().step_by(step).map(&xy); @@ -45,7 +71,7 @@ fn thin(ring: &[T], every: usize, xy: impl Fn(&T) -> (f64, f64)) -> Vec } f64::abs(twice) / 2.0 }; - let mut step = if ring.len() < KEEP_WHOLE { 1 } else { every.min(ring.len() / 3).max(1) }; + let mut step = if ring.len() < whole_below { 1 } else { every.min(ring.len() / 3).max(1) }; if step > 1 { let whole = area(1); while step > 1 && area(step) < whole / 2.0 { @@ -55,11 +81,6 @@ fn thin(ring: &[T], every: usize, xy: impl Fn(&T) -> (f64, f64)) -> Vec ring.iter().step_by(step).copied().collect() } -/// How many points of `n` outline points [`thin`] keeps one of. -fn every(n: usize) -> usize { - n.div_ceil(MAX_VERTICES).max(1) -} - /// How far the centre line is trimmed at each end, as a share of its distance to the outline there. const TIP: f32 = 2.0; @@ -141,11 +162,11 @@ fn farthest(cost: &[f32], from: usize) -> usize { /// runs into the polygon's tips and where the polygon is narrower than `half_width` either side of /// it. `None` for a polygon with no inside in view, or a centre line of fewer than two points. pub(super) fn centre_line(rings: &[Vec<(f32, f32)>], w: f32, h: f32, half_width: f32) -> Option> { - let every = every(rings.iter().map(Vec::len).sum()); + let how = Thinning::new(rings.iter().map(Vec::len)); let rings: Vec> = rings .iter() .map(|r| { - thin(r, every, |p| (f64::from(p.0), f64::from(p.1))) + thin(r, how, |p| (f64::from(p.0), f64::from(p.1))) .into_iter() .filter(|p| p.0.is_finite() && p.1.is_finite()) .collect::>() @@ -310,11 +331,11 @@ fn curved( }; let near: Vec<&Vec> = polys.iter().filter(|poly| poly.first().is_some_and(near)).flatten().collect(); // Thinned before they are projected. - let every = every(near.iter().map(|r| r.len()).sum()); + let how = Thinning::new(near.iter().map(|r| r.len())); let rings: Vec> = near .into_iter() .map(|ring| { - thin(ring, every, |c| (c.x, c.y)) + thin(ring, how, |c| (c.x, c.y)) .into_iter() .map(|c| ctx.screen(c).map(|(x, y)| (x as f32, y as f32))) .collect::>>() @@ -442,15 +463,20 @@ mod tests { /// would leave it a line with no inside, and the line through it. #[test] fn a_hole_dense_along_one_side_keeps_its_shape() { - // 100 000 points round a 600 × 100 rectangle (one point in five read), and a hole across - // its upper half with eleven points along its top side. + // 100 000 points round a 600 × 100 rectangle (one point in six read), and a hole across + // its upper half, too long to be kept whole, with 61 points along its top side. let n = 25_000; let mut outer: Vec<(f32, f32)> = (0..n).map(|i| (i as f32 * 600.0 / n as f32, 0.0)).collect(); outer.extend((0..n).map(|i| (600.0, i as f32 * 100.0 / n as f32))); outer.extend((0..n).map(|i| (600.0 - i as f32 * 600.0 / n as f32, 100.0))); outer.extend((0..n).map(|i| (0.0, 100.0 - i as f32 * 100.0 / n as f32))); - let mut hole: Vec<(f32, f32)> = (0..=10).map(|i| (50.0 + i as f32 * 50.0, 10.0)).collect(); + let mut hole: Vec<(f32, f32)> = (0..=60).map(|i| (50.0 + i as f32 * 500.0 / 60.0, 10.0)).collect(); hole.extend([(550.0, 50.0), (300.0, 50.0), (50.0, 50.0), (50.0, 10.0)]); + assert!(hole.len() >= KEEP_WHOLE); + // Thinned, but not to its top side alone. + let how = Thinning::new([outer.len(), hole.len()].into_iter()); + let kept = thin(&hole, how, |p| (f64::from(p.0), f64::from(p.1))); + assert!(kept.len() < hole.len() && kept.iter().any(|p| p.1 == 50.0), "{kept:?}"); let line = centre_line(&[outer, hole], 1000.0, 1000.0, 4.0).expect("below the hole"); assert!(line.iter().filter(|p| (100.0..500.0).contains(&p.0)).all(|p| p.1 > 55.0), "{line:?}"); } @@ -493,13 +519,23 @@ mod tests { let mut hole: Vec<(f32, f32)> = (0..=5).map(|i| (50.0 + i as f32 * 100.0, 10.0)).collect(); hole.extend((0..9).map(|i| (550.0 - i as f32 * 62.5, 50.0))); assert_eq!(hole.len(), 15); - let every = every(outer.len() + hole.len()); - let kept = thin(&hole, every, |p| (f64::from(p.0), f64::from(p.1))); + let how = Thinning::new([outer.len(), hole.len()].into_iter()); + let kept = thin(&hole, how, |p| (f64::from(p.0), f64::from(p.1))); assert!(kept.contains(&(50.0, 50.0)) && kept.contains(&(550.0, 50.0)), "{kept:?}"); let line = centre_line(&[outer, hole], 1000.0, 1000.0, 4.0).expect("below the hole"); assert!(line.iter().filter(|p| (100.0..500.0).contains(&p.0)).all(|p| p.1 > 55.0), "{line:?}"); } + /// A few small islands are kept whole; ten thousand of them are thinned, as a long ring is. + #[test] + fn many_small_islands_are_thinned() { + let island: Vec<(f32, f32)> = (0..60).map(|i| i as f32 / 60.0 * std::f32::consts::TAU).map(|t| (t.cos(), t.sin())).collect(); + let xy = |p: &(f32, f32)| (f64::from(p.0), f64::from(p.1)); + assert_eq!(thin(&island, Thinning::new(std::iter::repeat_n(60, 100)), xy).len(), 60); + let kept = thin(&island, Thinning::new(std::iter::repeat_n(60, 10_000)), xy).len(); + assert!((3..60).contains(&kept), "{kept}"); + } + /// A rectangle 10 points tall is too narrow for text 12 points tall, though its middle row of /// cells is two cells (7.5 points) deep on the grid: the outline may be nearly a cell nearer. #[test] From d91cccc5d3fe746f208e2a2a506b8e793feb1149 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Wed, 18 Nov 2026 06:32:19 -0300 Subject: [PATCH 3/3] test(labels): keep the dense-hole test off an exact half-area tie (#2370 review) The every-3rd sample of the hole was a triangle of exactly half its area, so the step depended on f64 rounding. The hole's bottom now bends up to y = 40 in the middle (area 17 500), leaving the sample (10 000) clearly over half. The test still fails with the half-area guard turned off. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/area_labels.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/render/src/area_labels.rs b/crates/render/src/area_labels.rs index 67161b114..c161b76ef 100644 --- a/crates/render/src/area_labels.rs +++ b/crates/render/src/area_labels.rs @@ -464,14 +464,15 @@ mod tests { #[test] fn a_hole_dense_along_one_side_keeps_its_shape() { // 100 000 points round a 600 × 100 rectangle (one point in six read), and a hole across - // its upper half, too long to be kept whole, with 61 points along its top side. + // its upper half, too long to be kept whole, with 61 points along its top side (its bottom + // bent up to y = 40 in the middle, so one point in three keeps clearly over half its area). let n = 25_000; let mut outer: Vec<(f32, f32)> = (0..n).map(|i| (i as f32 * 600.0 / n as f32, 0.0)).collect(); outer.extend((0..n).map(|i| (600.0, i as f32 * 100.0 / n as f32))); outer.extend((0..n).map(|i| (600.0 - i as f32 * 600.0 / n as f32, 100.0))); outer.extend((0..n).map(|i| (0.0, 100.0 - i as f32 * 100.0 / n as f32))); let mut hole: Vec<(f32, f32)> = (0..=60).map(|i| (50.0 + i as f32 * 500.0 / 60.0, 10.0)).collect(); - hole.extend([(550.0, 50.0), (300.0, 50.0), (50.0, 50.0), (50.0, 10.0)]); + hole.extend([(550.0, 50.0), (300.0, 40.0), (50.0, 50.0), (50.0, 10.0)]); assert!(hole.len() >= KEEP_WHOLE); // Thinned, but not to its top side alone. let how = Thinning::new([outer.len(), hole.len()].into_iter());