From db1132105c731bf3f39975649ff2e6361ebe0303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Mon, 30 Nov 2026 20:28:07 -0500 Subject: [PATCH] fix(labels): keep a label's position and offset upright on a turned map --- crates/render/src/lib.rs | 72 +++++--- crates/render/src/polygon_labels_tests.rs | 2 + .../src/polygon_labels_tests/offsets.rs | 159 ++++++++++++++++++ docs/user-guide/cartography.md | 8 +- 4 files changed, 210 insertions(+), 31 deletions(-) create mode 100644 crates/render/src/polygon_labels_tests/offsets.rs diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 1b4d83be0f..edc8d646ec 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -6264,6 +6264,20 @@ 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); + // The view the frame is shown in, if it isn't the frame's own, and where a place + // in the frame's points is in it. + let shown = match &labels_to { + Labels::Turned(to) | Labels::Tilted(to, _) => Some(*to), + Labels::Own | Labels::Shared => None, + }; + let to_shown = |(x, y): (f32, f32)| match shown { + Some(to) => { + let m = ctx.view.camera.to_map(f64::from(x), f64::from(y), vw, vh); + to.camera.try_to_screen(m, to.width, to.height).map(|(x, y)| (x as f32, y as f32)) + } + None => Some((x, y)), + }; + let (shown_w, shown_h) = shown.map_or((vw, vh), |to| (to.width, to.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| { @@ -6304,7 +6318,13 @@ fn render_frame( ctx.screen(c) } else if offset_reach > 0.0 { let r = offset_reach; - ctx.screen(c).filter(|&(x, y)| x >= -r && y >= -r && x <= vw + r && y <= vh + r) + // Measured in the view shown, where the offset is applied. + ctx.screen(c).filter(|&(x, y)| { + to_shown((x as f32, y as f32)).is_some_and(|(x, y)| { + let (x, y) = (f64::from(x), f64::from(y)); + x >= -r && y >= -r && x <= shown_w + r && y <= shown_h + r + }) + }) } else { None }; @@ -6410,13 +6430,29 @@ fn render_frame( } }); let (bw, bh) = (text::spaced_block_width(&text, csize, face, class.spacing), text::block_height(&text, csize)); - // Where a placement puts it, moved as its class says (points, as the - // text's size), unless along a line. - let place_at = |(x, y): (f32, f32), p: LabelPlacement| { + let from_shown = |(x, y): (f32, f32)| match shown { + Some(to) => to + .camera + .try_to_map(f64::from(x), f64::from(y), to.width, to.height) + .map(|m| ctx.view.to_screen(m)) + .map(|(x, y)| (x as f32, y as f32)), + None => Some((x, y)), + }; + // A place in the view shown, where a placement puts it, moved as its class + // says (points, as the text's size), unless along a line. + let nudged = |(x, y): (f32, f32), p: LabelPlacement| { let (dx, dy) = p.offset(bw, bh, marker, is_point); let (dx, dy) = ((dx + class.offset[0] * 1.33) * label_factor, (dy + class.offset[1] * 1.33) * label_factor); (x + dx, y + dy) }; + // The same from a place in the frame's points: moved in the view shown, so + // right and down are the screen's on a turned or tilted map too (#3508). + // Where the place, or the moved one, has no counterpart (behind a tilted + // view's camera or in its sky), moved in the frame. + let place_at = |at: (f32, f32), p: LabelPlacement| match shown { + Some(_) => to_shown(at).and_then(|there| from_shown(nudged(there, p))).unwrap_or_else(|| nudged(at, p)), + None => nudged(at, p), + }; let place = |p: LabelPlacement| place_at((x as f32, y as f32), p); // Where it goes, each with its repeat's index: by its feature (0), or along // a line at its middle and, repeated, every so far either side. @@ -6771,24 +6807,11 @@ fn render_frame( // 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 = 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), - Labels::Own | Labels::Shared => None, - }; 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) - }; + let in_view = |at: (f32, f32)| to_shown(at).is_some_and(|(x, y)| on_shown((f64::from(x), f64::from(y)))); // Whether its label point is in the view shown. let own_seen = point.is_some() && in_view((x as f32, y as f32)); // Its part's rings as shown, whole and cut to the view: each thinned once. @@ -6876,14 +6899,7 @@ fn render_frame( { // A place in the view shown as one in the frame's points. let slot = |there: Option<(f32, f32)>| { - let here = there.and_then(|(px, py)| match shown { - Some(to) => to - .camera - .try_to_map(f64::from(px), f64::from(py), to.width, to.height) - .map(|m| ctx.view.to_screen(m)) - .map(|(x, y)| (x as f32, y as f32)), - None => Some((px, py)), - }); + let here = there.and_then(from_shown); LabelPlace::from(here.filter(|&at| on_map(at).is_some()).unwrap_or((f32::NAN, f32::NAN))) }; // Which way each goes, across and down. @@ -6907,7 +6923,7 @@ fn render_frame( if w > right - left || h > bottom - top { let along = |way: f32, lo: f32, hi: f32| (lo + hi) / 2.0 + way * (hi - lo) / 2.0; later.extend(ways.iter().map(|&(p, x, y)| { - let there = place_at((along(x, left, right), along(y, top, bottom)), p); + let there = nudged((along(x, left, right), along(y, top, bottom)), p); slot(Some(there).filter(|&(x, y)| on_shown((f64::from(x), f64::from(y))))) })); } else if let Some(((x0, y0), clip)) = search_origin @@ -6924,7 +6940,7 @@ fn render_frame( }; for boxes in [1.0, 2.0] { later.extend(ways.iter().map(|&(_, x, y)| { - let there = place_at((x0 + x * boxes * across, y0 + y * boxes * down), class.placement); + let there = nudged((x0 + x * boxes * across, y0 + y * boxes * down), class.placement); slot(Some(there).filter(|&at| seen(at) && holds(at))) })); } diff --git a/crates/render/src/polygon_labels_tests.rs b/crates/render/src/polygon_labels_tests.rs index 2df5de9162..94c44ae679 100644 --- a/crates/render/src/polygon_labels_tests.rs +++ b/crates/render/src/polygon_labels_tests.rs @@ -5,6 +5,8 @@ use super::*; use rustgis_data::{Dataset, Field, FieldType, Value}; use rustgis_map::{Camera, LabelPlacement}; +mod offsets; + fn c(x: f64, y: f64) -> Coord { Coord::new(x, y) } diff --git a/crates/render/src/polygon_labels_tests/offsets.rs b/crates/render/src/polygon_labels_tests/offsets.rs new file mode 100644 index 0000000000..c1084e4220 --- /dev/null +++ b/crates/render/src/polygon_labels_tests/offsets.rs @@ -0,0 +1,159 @@ +//! A label class's position and offset on a turned or tilted map (#3508): right and down on the +//! screen at every place a label takes, as on a map north up. + +use super::*; + +/// 30 points right, 39.9 pixels. +const RIGHT: [f32; 2] = [30.0, 0.0]; +const MOVED: (f32, f32) = (39.9, 0.0); +/// North up, then each quarter turn. +const TURNS: [f64; 4] = [0.0, 90.0, 180.0, 270.0]; + +fn turned(bearing: f64, pitch: f64) -> View { + let mut v = view(); + (v.camera.bearing, v.camera.pitch) = (bearing, pitch); + v +} + +/// A map with one point in the middle of [`view`], labelled "Lake". +fn point(on: bool) -> (Map, u32) { + point_at(c(0.0, 0.0), on) +} + +/// [`point`] at a longitude and latitude. +fn point_at(p: Coord, on: bool) -> (Map, u32) { + let mut m = Map::new("Map"); + let mut d = Dataset::new("spots", GeometryType::Point, vec![Field::new("name", FieldType::Text)]); + d.push(Some(Geometry::Point(p)), vec![Value::Text("Lake".into())]); + let id = m.add_dataset(d); + if let Some(l) = m.layer_mut(id) { + l.labels.enabled = true; + l.labels.default.other_positions = on; + // Wide, so a label blocking one place leaves the places beside it. + l.labels.default.spacing = WIDE; + } + (m, id) +} + +/// A square 445 points across in [`view`]: room for every place inside, moved or not. +fn big(on: bool) -> (Map, u32) { + map(Geometry::Polygon(vec![vec![square(0.0, 0.0, 0.2)]]), on) +} + +/// How far [`RIGHT`] moves each of the first `count` places a label takes. +fn moves(make: impl Fn() -> (Map, u32), v: &View, count: usize) -> Vec<(f32, f32)> { + let taken = |offset: [f32; 2]| { + let (mut m, id) = make(); + m.layer_mut(id).unwrap().labels.default.offset = offset; + every_place(&mut m, v) + }; + let (still, moved) = (taken([0.0; 2]), taken(RIGHT)); + assert!(still.len() >= count && moved.len() >= count, "{still:?} {moved:?}"); + still.iter().zip(&moved).take(count).map(|(a, b)| (b.0 - a.0, b.1 - a.1)).collect() +} + +fn all_move_right(moves: &[(f32, f32)]) { + assert!(moves.iter().all(|&m| near(m, MOVED)), "{moves:?}"); +} + +#[test] +fn a_points_offset_goes_right_on_the_screen() { + for bearing in TURNS { + all_move_right(&moves(|| point(false), &turned(bearing, 0.0), 1)); + } +} + +#[test] +fn a_points_offset_goes_right_on_a_tilted_map_unshortened() { + all_move_right(&moves(|| point(false), &turned(0.0, 30.0), 1)); +} + +#[test] +fn above_is_above_on_the_screen() { + let v = turned(90.0, 0.0); + let (mut m, id) = point(false); + m.layer_mut(id).unwrap().labels.default.placement = LabelPlacement::Above; + let (x, y) = one(&mut m, &v); + // The point is in the middle of the view. + assert!((x - 400.0).abs() < 1.0 && y < 295.0, "{x} {y}"); +} + +/// A point's own place and the places round it are the same on the screen however the map is +/// turned about it. +#[test] +fn a_turned_points_places_are_those_north_up() { + let taken = |bearing| every_place(&mut point(true).0, &turned(bearing, 0.0)); + let (north, east) = (taken(0.0), taken(90.0)); + assert!(north.len() == east.len() && north.iter().zip(&east).all(|(&a, &b)| near(a, b)), "{north:?} {east:?}"); +} + +/// The first place inside, the label point and the places further inside. +#[test] +fn a_polygons_offset_goes_right_at_every_place_inside() { + all_move_right(&moves(|| big(true), &turned(90.0, 0.0), 6)); +} + +/// The places round a polygon too small for its name, after the two inside. +#[test] +fn a_polygons_offset_goes_right_at_every_place_round_it() { + let make = || islet(small(), true); + all_move_right(&moves(make, &turned(90.0, 0.0), 8)); +} + +#[test] +fn a_polygons_offset_goes_right_with_other_places_off() { + for bearing in TURNS { + all_move_right(&moves(|| big(false), &turned(bearing, 0.0), 1)); + } +} + +/// A label square to a line takes its offset along that line's normal, turning with the map as +/// the label does. +#[test] +fn a_perpendicular_labels_offset_still_turns_with_its_line() { + let place = |offset: [f32; 2]| { + let (mut m, id) = point(false); + let mut d = Dataset::new("coast", GeometryType::Polyline, vec![]); + d.push(Some(Geometry::Line(vec![vec![c(-0.02, -1.0), c(-0.02, 1.0)]])), vec![]); + m.add_dataset(d); + let l = m.layer_mut(id).unwrap(); + l.labels.default.placement = LabelPlacement::Perpendicular; + l.labels.default.square_to = + Some(rustgis_map::SquareTo { layer: "coast".into(), within: 40.0, side: rustgis_map::SquareSide::Away }); + l.labels.default.offset = offset; + one(&mut m, &turned(90.0, 0.0)) + }; + let (still, moved) = (place([0.0; 2]), place(RIGHT)); + // Across a line running north, which on this map runs across the screen. + assert!((moved.0 - still.0).abs() < 1.0 && ((moved.1 - still.1).abs() - MOVED.0).abs() < 1.0, "{still:?} {moved:?}"); +} + +/// A diagonal offset on a map turned half way runs along one axis of the frame the labels are +/// placed in: a point that far outside it still has its label in view. +#[test] +fn a_point_far_out_of_a_turned_view_keeps_its_offset_label() { + let v = turned(45.0, 0.0); + let (mut m, id) = point_at(at(&v, (-99.0, -199.0)), false); + let l = m.layer_mut(id).unwrap(); + l.labels.default.placement = LabelPlacement::Center; + l.labels.default.offset = [150.0, 150.0]; + let placed = one(&mut m, &v); + assert!(near(placed, (100.5, 0.5)), "{placed:?}"); +} + +/// At the far end of a tilted view a point off its side has its label in view as far as its +/// offset reaches on the screen, though that is farther on the ground. +#[test] +fn a_point_off_the_far_side_of_a_tilted_view_keeps_its_offset_label() { + for pitch in [30.0, 45.0, 60.0] { + for y in [10.0, 40.0, 80.0] { + let v = turned(0.0, pitch); + let (mut m, id) = point_at(at(&v, (-150.0, y)), false); + let l = m.layer_mut(id).unwrap(); + l.labels.default.placement = LabelPlacement::Center; + l.labels.default.offset = [150.0, 0.0]; + let p = places(&mut m, &v); + assert!(p.len() == 1 && near(p[0], (49.5, y)), "tilted {pitch}, {y}: {p:?}"); + } + } +} diff --git a/docs/user-guide/cartography.md b/docs/user-guide/cartography.md index 9f551b8dfc..efe41b9680 100644 --- a/docs/user-guide/cartography.md +++ b/docs/user-guide/cartography.md @@ -652,9 +652,11 @@ Turn labels on per layer (Labeling › Labels › **Label**), then: out is kept). - **Offset**: a label moved from where its position puts it, right and down in points (Labeling pane › Position › **Offset right**, **Offset down**, −200 to 200; negative moves it - left or up), growing with the labels at a reference scale. A label along a line isn't moved - (`labels.placement={"layer": "Cities", "position": "aboveRight", "offset": [5.7, -2.8]}`; an - offset can be given without a position). + left or up), growing with the labels at a reference scale. Position and offset are the screen's + on a turned or tilted map too; a label square to a line takes its offset along and across that + direction instead. A label along a line isn't moved + (`labels.placement={"layer": "Cities", "position": "aboveRight", "offset": [5.7, -2.8]}`; + an offset can be given without a position). - **Wrapping**: a label's text broken into lines (Labeling pane › Symbol): **Wrap at** a character (or a few, up to 8), a line break wherever it occurs, left out (a space puts each word on its own line, `/` breaks `Zürich/Winterthur`), and **Wrap length**, the longest line in characters, broken