diff --git a/crates/render/src/area_labels.rs b/crates/render/src/area_labels.rs index 5439a6e892..0c58bd3d34 100644 --- a/crates/render/src/area_labels.rs +++ b/crates/render/src/area_labels.rs @@ -350,16 +350,26 @@ pub(super) fn screen_rings(ctx: &super::Ctx<'_>, polys: &[Vec>]) -> O /// frame's own), so the search is made there. The place is given in the frame's points, then in /// that view's. `None` for no polygon, one with no area near the view or one with a place behind /// the globe or the camera. +/// +/// With `clip` (#3276), the rings are first cut to that view, so the place is in the polygon's +/// part in view and the view's edges count as its own: a name that fits in that part is whole on +/// the screen. pub(super) fn pole( ctx: &super::Ctx<'_>, g: &rustgis_data::Geometry, aspect: f32, shown: Option<&super::View>, + clip: bool, ) -> Option<((f32, f32), (f32, f32))> { #[cfg(test)] SEARCHES.with(|n| n.set(n.get() + 1)); let mut rings = screen_rings(ctx, std::slice::from_ref(g.label_part()?))?; let here = ctx.view; + // Cut to the frame before a tilted view is projected to: a vertex far outside the frame may be + // behind its camera. + if clip { + rings = clipped(&rings, here)?; + } if let Some(to) = shown { for p in rings.iter_mut().flatten() { let m = here.camera.to_map(f64::from(p.0), f64::from(p.1), here.width, here.height); @@ -367,6 +377,9 @@ pub(super) fn pole( *p = (x as f32, y as f32); } } + if let Some(to) = shown.filter(|_| clip) { + rings = clipped(&rings, to)?; + } // A text of no measurable shape counts as square; a very long or tall one as 20 to 1. let aspect = if aspect.is_finite() && aspect > 0.0 { aspect.clamp(0.05, 20.0) } else { 1.0 }; rings.iter_mut().flatten().for_each(|p| p.0 /= aspect); @@ -378,6 +391,22 @@ pub(super) fn pole( Some(((x as f32, y as f32), there)) } +/// The parts of `rings` (an outer ring first) inside `view`'s box, in its points; `None` with none +/// of the outer ring in it, whatever is left of the holes. +fn clipped(rings: &[Vec<(f32, f32)>], view: &super::View) -> Option>> { + let screen = Extent { min_x: 0.0, min_y: 0.0, max_x: view.width, max_y: view.height }; + let cut = |ring: &Vec<(f32, f32)>| { + let ring: Vec<(f64, f64)> = ring.iter().map(|&(x, y)| (f64::from(x), f64::from(y))).collect(); + super::clip_ring(&ring, &screen).into_iter().map(|(x, y)| (x as f32, y as f32)).collect::>() + }; + let mut cut: Vec<_> = rings.iter().map(cut).collect(); + if cut.first().is_none_or(Vec::is_empty) { + return None; + } + cut.retain(|r| !r.is_empty()); + Some(cut) +} + #[cfg(test)] thread_local! { /// The [`pole`] searches made on this thread. diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 7a090bb8d7..4d6279fc49 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6178,6 +6178,13 @@ fn render_frame( .fold(0.0, f32::max); let offset_reach = f64::from(offset_reach.max(locked_reach)); let (vw, vh) = (ctx.view.width, ctx.view.height); + // Whether a class's polygon labels go to a place inside (#3268): in view too + // when their label point isn't (#3276). + let goes_inside = |class: &rustgis_map::LabelClass, g: &Geometry| { + class.other_positions && class.places_inside_polygons() && matches!(g, Geometry::Polygon(_)) + }; + let any_inside = + labels.in_order().zip(&label_fields).any(|(c, f)| f.is_some() && c.other_positions && c.places_inside_polygons()); // Labels moved by hand (the Move Label tool): any class's, its feature known by // its id. Where one was put is in view or not whatever its feature's place. let any_moved = labels.in_order().zip(&label_fields).any(|(c, f)| f.is_some() && !c.moved.is_empty()); @@ -6206,7 +6213,8 @@ fn render_frame( } else { None }; - let line_in_view = || globe.is_some() || ctx.cull.intersects(&extents.get(row).copied().unwrap_or_else(|| g.extent())); + let extent_in_view = + || globe.is_some() || ctx.cull.intersects(&extents.get(row).copied().unwrap_or_else(|| g.extent())); let fid = layer.dataset.features.get(row).map(|f| f.id); // (Its class's clause read only for a feature with a label moved.) let moved_here = || { @@ -6214,7 +6222,13 @@ fn render_frame( && fid.is_some_and(|id| labels.in_order().any(|c| c.moved_to(id).is_some())) && classes.classes_of(ds_ptr, row).any(|(_, c)| moved_to(c, fid).is_some()) }; - if point.is_none() && !(any_along && matches!(g, Geometry::Line(_)) && line_in_view()) && !moved_here() { + // (A polygon with its label point behind the globe is behind it too.) + let reaches_view = || { + ((any_along && matches!(g, Geometry::Line(_))) + || (any_inside && globe.is_none() && matches!(g, Geometry::Polygon(_)))) + && extent_in_view() + }; + if point.is_none() && !reaches_view() && !moved_here() { continue; } let is_point = g.kind() == GeometryType::Point; @@ -6241,12 +6255,12 @@ fn render_frame( } // Moved by hand: one label, upright, where it was put. let moved = if any_moved { moved_to(class, fid) } else { None }; - if point.is_none() && moved.is_none() && !along_line(class.placement, g) { + if point.is_none() && moved.is_none() && !along_line(class.placement, g) && !goes_inside(class, g) { continue; } // Its feature's place, for a callout's line back to it: off the screen too // when the label was moved into view. - let anchor = point.or_else(|| moved.and_then(|_| ctx.screen(c))); + let mut anchor = point.or_else(|| moved.and_then(|_| ctx.screen(c))); // (Along a line, the label is put on its stretch in view below.) let (x, y) = point.unwrap_or_default(); let Some(fi) = label_fields.get(ci).copied().flatten() else { continue }; @@ -6652,10 +6666,7 @@ fn render_frame( // A polygon's label trying other places (#3268) goes first where its // shape fits best inside (a pole of inaccessibility), then here. A curved // or guided one has its own course, which this is only what's left of. - let tries = class.other_positions - && moved.is_none() - && class.places_inside_polygons() - && matches!(g, Geometry::Polygon(_)); + let tries = moved.is_none() && goes_inside(class, g); // The view the frame is shown in, where that isn't its own. let shown = match &labels_to { Labels::Turned(to) | Labels::Tilted(to, _) => Some(*to), @@ -6663,10 +6674,22 @@ fn render_frame( }; let key = (lid, label_id(row), ci, 0); let locked_at = lock.as_ref().and_then(|k| k.get(&key)).map(|won| won.place); + // Whether a place in the frame's points is in the view shown. + let (shown_w, shown_h) = shown.map_or((vw, vh), |to| (to.width, to.height)); + let on_shown = |(x, y): (f64, f64)| (0.0..=shown_w).contains(&x) && (0.0..=shown_h).contains(&y); + let in_view = |(x, y): (f32, f32)| { + let (x, y) = (f64::from(x), f64::from(y)); + let there = match shown { + Some(to) => to.camera.try_to_screen(ctx.view.camera.to_map(x, y, vw, vh), shown_w, shown_h), + None => Some((x, y)), + }; + there.is_some_and(on_shown) + }; // Where it goes first, when that isn't its label point: locked there, // the map place it was locked at (not what a search finds now, as along // a flat stretch many places are as good); not locked, the pole, when a - // search finds one in view. Locked at the place after, neither. + // search finds one in view, or with its label point out of view too, the + // pole of its part in view (#3276). Locked at the place after, neither. let first = match locked_at { _ if !tries => None, Some(0) => { @@ -6675,16 +6698,30 @@ fn render_frame( None => flat.forward(anchor).map(|p| view.to_screen(p)), }); let Some((x, y)) = target else { continue }; - Some((x as f32, y as f32)) + Some((x as f32, y as f32)).filter(|&at| point.is_some() || in_view(at)) } Some(_) => None, - None => area_labels::pole(ctx, g, bw / bh, shown) - .filter(|&(_, (px, py))| { - let (w, h) = shown.map_or((vw, vh), |to| (to.width, to.height)); - (0.0..=w).contains(&f64::from(px)) && (0.0..=h).contains(&f64::from(py)) + None => area_labels::pole(ctx, g, bw / bh, shown, false) + .filter(|&(_, (px, py))| on_shown((f64::from(px), f64::from(py)))) + .or_else(|| { + let own_seen = point.is_some() && in_view((x as f32, y as f32)); + if own_seen { None } else { area_labels::pole(ctx, g, bw / bh, shown, true) } }) .map(|(here, _)| here), }; + // Its label point, where that is a place for it: not one out of view + // when it has a place in view. + let own = point.is_some() && (first.is_none() || locked_at.is_some() || in_view((x as f32, y as f32))); + if tries && !own && first.is_none() { + continue; + } + // A callout's line goes back to the place inside when the label point + // isn't in view. + if let Some((fx, fy)) = first + && !(point.is_some() && in_view((x as f32, y as f32))) + { + anchor = Some((f64::from(fx), f64::from(fy))); + } // The first place and the label point on the map, while the lock is // taken (never from a tilted view), for it to keep. if tries && display.locked && lock.is_none() && !matches!(labels_to, Labels::Tilted(..)) { @@ -6692,13 +6729,16 @@ fn render_frame( Some(globe) => globe.inverse(f64::from(x), f64::from(y)), None => flat.inverse(view.camera.to_map(f64::from(x), f64::from(y), view.width, view.height)), }; - let own = (x as f32, y as f32); - let anchors = vec![on_map(first.unwrap_or(own)), on_map(own)]; + let label_point = (x as f32, y as f32); + let mut anchors = vec![on_map(first.unwrap_or(label_point))]; + if own { + anchors.push(on_map(label_point)); + } line_places = Some(LinePlaces { anchors, ..Default::default() }); } // The label point is the place after the first, so a lock that took it // there finds it. - if tries { + if tries && own { line_alternatives.push(LabelPlace { x: lx, y: ly, angle: turned, glyphs: Vec::new() }); } let (px, py) = first.map_or((lx, ly), |first| place_at(first, class.placement)); diff --git a/crates/render/src/polygon_labels_tests.rs b/crates/render/src/polygon_labels_tests.rs index 150a77fb2d..2276ed8e42 100644 --- a/crates/render/src/polygon_labels_tests.rs +++ b/crates/render/src/polygon_labels_tests.rs @@ -378,3 +378,208 @@ fn locking_leaves_a_label_whose_pole_is_just_out_of_view() { assert!(near(one(&mut m, &v), today(&v, &g)), "as before the lock"); assert!(near(one(&mut m, &v), today(&v, &g)), "and locked"); } + +/// [`view`] zoomed in twice, its bottom edge 12 points under the place `p`. +fn just_under(p: Coord) -> View { + let mut v = view(); + v.camera.res = 50.0; + v.camera.center.y = lonlat_to_merc(p).y + 288.0 * v.camera.res; + v +} + +/// A square of 2 by 2 degrees: its label point and its pole are at (0, 0). +fn big() -> Vec> { + vec![square(0.0, 0.0, 1.0)] +} + +/// A view of [`big`]'s east side, its label point well out of view. +fn east() -> View { + view_at(0.9, 0.0) +} + +/// Whether the "Lake" label at `p` has its whole box in view and inside `rings` (#3276). +fn box_in_view_and_polygon(m: &mut Map, v: &View, rings: &[Vec]) { + let placed = render(m, v).unwrap().labels; + let lake: Vec<_> = placed.iter().filter(|l| l.text == "Lake").collect(); + assert_eq!(lake.len(), 1, "turned {}, tilted {}", v.camera.bearing, v.camera.pitch); + let p = lake[0]; + let face = text::Face { bold: false, italic: false, serif: false, family: None }; + let (hw, hh) = (text::spaced_block_width("Lake", p.size, face, 0.0) / 2.0, text::block_height("Lake", p.size) / 2.0); + for (x, y) in [(p.x - hw, p.y - hh), (p.x + hw, p.y - hh), (p.x - hw, p.y + hh), (p.x + hw, p.y + hh)] { + assert!((0.0..=800.0).contains(&x) && (0.0..=600.0).contains(&y), "out of view: {:?}", (p.x, p.y)); + assert!(depth(rings, at(v, (x, y))) > 0.0, "out of the polygon: {:?}", (p.x, p.y)); + } +} + +#[test] +fn with_its_label_point_out_of_view_a_polygon_is_labelled_in_its_visible_part() { + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + box_in_view_and_polygon(&mut m, &east(), &big()); + // Off, as without the setting: no label. + assert!(places(&mut map(Geometry::Polygon(vec![big()]), false).0, &east()).is_empty()); +} + +#[test] +fn a_view_wholly_inside_a_polygon_labels_it_near_its_middle() { + let rings = vec![square(0.0, 0.0, 2.0)]; + let (mut m, _) = map(Geometry::Polygon(vec![rings]), true); + let (x, y) = one(&mut m, &view_at(0.5, 0.5)); + assert!((x - 400.0).abs() < 10.0 && (150.0..=450.0).contains(&y), "{:?}", (x, y)); +} + +#[test] +fn a_visible_parts_label_keeps_out_of_a_hole() { + let rings = vec![square(0.0, 0.0, 1.0), square(0.82, 0.0, 0.1)]; + let (mut m, _) = map(Geometry::Polygon(vec![rings.clone()]), true); + box_in_view_and_polygon(&mut m, &view_at(0.8, 0.0), &rings); +} + +#[test] +fn a_polygon_out_of_view_is_not_searched() { + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + area_labels::SEARCHES.with(|n| n.set(0)); + assert!(places(&mut m, &view_at(5.0, 0.0)).is_empty()); + area_labels::SEARCHES.with(|n| assert_eq!(n.get(), 0)); +} + +#[test] +fn a_sliver_is_labelled_in_its_visible_part_and_no_area_is_not() { + // Three points of the polygon's east side in view. + let mut v = view(); + v.camera.center.x = lonlat_to_merc(c(1.0, 0.0)).x + 397.0 * v.camera.res; + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + // Too thin for the name, which then shows in part, its middle inside the sliver. + let (x, y) = one(&mut m, &v); + assert!((0.0..=3.0).contains(&x) && (0.0..=600.0).contains(&y), "{:?}", (x, y)); + let flat = Geometry::Polygon(vec![vec![ring(&[(-1.0, 0.0), (0.0, 0.0), (1.0, 0.0)])]]); + let (mut m, _) = map(flat, true); + assert!(places(&mut m, &east()).is_empty()); +} + +#[test] +fn a_lock_keeps_a_visible_parts_label_where_it_was() { + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + let v = east(); + m.label_display.locked = true; + let before = at(&v, one(&mut m, &v)); + let moved = view_at(0.87, 0.03); + let after = at(&moved, one(&mut m, &moved)); + assert!((after.x - before.x).abs() < 2e-3 && (after.y - before.y).abs() < 2e-3, "{before:?} {after:?}"); +} + +#[test] +fn a_lock_inside_holds_with_the_label_point_out_of_view() { + let g = Geometry::Polygon(vec![u()]); + let (mut m, _) = map(g.clone(), true); + let v = view(); + m.label_display.locked = true; + let before = at(&v, one(&mut m, &v)); + let low = just_under(before); + assert!(today(&low, &g).1 > 640.0, "the label point is out of view, past the cull margin: {:?}", today(&low, &g)); + let after = at(&low, one(&mut m, &low)); + assert!((after.x - before.x).abs() < 2e-3 && (after.y - before.y).abs() < 2e-3, "{before:?} {after:?}"); + // Its place out of view too: no label. + assert!(places(&mut m, &view_at(0.0, -1.0)).is_empty()); +} + +#[test] +fn a_lock_on_the_label_point_hides_with_it_out_of_view() { + let (mut m, _) = map(Geometry::Polygon(vec![u()]), true); + let v = view(); + let pole = one(&mut m, &v); + block(&mut m, &v, &[pole]); + m.label_display.locked = true; + one(&mut m, &v); + // The label point, at the south edge, below the view; the pole's place in it. + let low = just_under(at(&v, pole)); + assert!(today(&low, &Geometry::Polygon(vec![u()])).1 > 640.0); + assert!(places(&mut m, &low).is_empty()); +} + +/// A label point just past the view's edge, within the margin features are still drawn in, is out +/// of view too. +#[test] +fn a_label_point_just_past_the_edge_labels_the_visible_part() { + let mut v = view(); + v.camera.center.x = lonlat_to_merc(c(0.0, 0.0)).x + 410.0 * v.camera.res; + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + box_in_view_and_polygon(&mut m, &v, &big()); +} + +#[test] +fn turned_and_tilted_views_label_the_visible_part() { + for (bearing, pitch) in [(90.0, 0.0), (0.0, 30.0)] { + let mut v = east(); + (v.camera.bearing, v.camera.pitch) = (bearing, pitch); + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + box_in_view_and_polygon(&mut m, &v, &big()); + } +} + +#[test] +fn a_visible_parts_label_with_its_place_taken_is_left_out() { + let (mut m, _) = map(Geometry::Polygon(vec![big()]), true); + let v = east(); + let here = one(&mut m, &v); + block(&mut m, &v, &[here]); + m.label_display.show_unplaced = true; + let r = render(&mut m, &v).unwrap(); + assert!(r.labels.iter().all(|l| l.text != "Lake")); + let unplaced: Vec<_> = r.unplaced.iter().filter(|l| l.text == "Lake").map(|l| (l.x, l.y)).collect(); + assert_eq!(unplaced.len(), 1); + assert!(near(unplaced[0], here), "{unplaced:?} {here:?}"); +} + +/// A callout moved off a crowded place draws its line back to that place, with the label point far +/// out of view or just past the edge, and with Lock on. +#[test] +fn a_visible_parts_callout_points_back_to_its_place() { + let mut past_edge = view(); + past_edge.camera.center.x = lonlat_to_merc(c(0.0, 0.0)).x + 410.0 * past_edge.camera.res; + for v in [east(), past_edge] { + let (mut m, id) = map(Geometry::Polygon(vec![big()]), true); + m.layer_mut(id).unwrap().labels.default.callout = Some(60.0); + let here = one(&mut m, &v); + block(&mut m, &v, &[here]); + for locked in [false, true] { + m.label_display.locked = locked; + // Twice: the frame that takes the lock, then one replaying it. + for frame in 0..2 { + let r = render(&mut m, &v).unwrap(); + let l = r.labels.iter().find(|l| l.text == "Lake").unwrap(); + let [ax, ay] = l.collide.callout_anchor.expect("a line back"); + assert!(near((l.x + ax, l.y + ay), here), "locked {locked}, frame {frame}: {:?} {here:?}", (l.x + ax, l.y + ay)); + } + } + } +} + +#[test] +fn a_curved_label_stays_out_with_its_label_point_out_of_view() { + let (mut m, id) = map(Geometry::Polygon(vec![big()]), true); + m.layer_mut(id).unwrap().labels.default.placement = LabelPlacement::Curved; + assert!(places(&mut m, &east()).is_empty()); +} + +/// On the globe, a polygon behind it has no part in view to search. +#[test] +fn a_polygon_behind_the_globe_is_not_searched() { + let (mut m, _) = map(Geometry::Polygon(vec![vec![square(180.0 - 1.0, 0.0, 0.5)]]), true); + m.projection = rustgis_map::Projection::Globe; + area_labels::SEARCHES.with(|n| n.set(0)); + assert!(places(&mut m, &view()).is_empty()); + area_labels::SEARCHES.with(|n| assert_eq!(n.get(), 0)); +} + +/// A polygon far larger than a tilted view reaches behind the camera: the part in view is still +/// found. +#[test] +fn a_tilted_view_labels_a_polygon_reaching_behind_the_camera() { + let huge = vec![square(0.0, 0.0, 20.0)]; + for pitch in [30.0, 60.0] { + let mut v = view_at(19.9, 0.0); + v.camera.pitch = pitch; + let (mut m, _) = map(Geometry::Polygon(vec![huge.clone()]), true); + box_in_view_and_polygon(&mut m, &v, &huge); + } +} diff --git a/docs/user-guide/cartography.md b/docs/user-guide/cartography.md index bff2bf87ac..2847d34ecb 100644 --- a/docs/user-guide/cartography.md +++ b/docs/user-guide/cartography.md @@ -784,10 +784,14 @@ Turn labels on per layer (Labeling › Labels › **Label**), then: tall as the text), so a name sits in the body of a lake or bay rather than against a shore. Where another label takes that place, it goes where it goes without this setting, and is left out if that is taken too (Labeling pane › Position › **Try other places inside the polygon**, for polygon - layers). With that point out of view, the label stays where it goes without this. On a polygon - more than 64 times longer than wide on screen, once narrowed to the text's shape, only its first - 64 widths are searched. Curved labels and labels along a guide don't use it: the box is hidden for - them, the command refuses to turn it on, and a setting already on has no effect. Off by default, + layers). With that point out of view, the label stays where it goes without this. With both out of + view, the label goes where its shape fits best inside the part of the polygon in view, the view's + edges counting as the polygon's own: it follows that part as the map moves (Lock holds it), and is + left out if another label takes its place. On a multipolygon this is the part its label is for; + other parts in view don't take it. On a polygon more than 64 times longer than wide on screen, + once narrowed to the text's shape, only its first 64 widths are searched. Curved labels and + labels along a guide don't use it: the box is hidden for them, the command refuses to turn it + on, and a setting already on has no effect. Off by default, as round points; the class's position and offset apply from each of the two places (Above sets the name above that point), and Lock keeps the place each label took (`labels.placement={"layer": "Lakes", "otherPositions": true}`).