Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/render/src/area_labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,36 @@ pub(super) fn screen_rings(ctx: &super::Ctx<'_>, polys: &[Vec<Vec<Coord>>]) -> 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,
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
) -> 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);
let (x, y) = to.camera.try_to_screen(m, to.width, to.height)?;
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
*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);
Expand All @@ -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<Vec<Vec<(f32, f32)>>> {
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::<Vec<_>>()
};
let mut cut: Vec<_> = rings.iter().map(cut).collect();
if cut.first().is_none_or(Vec::is_empty) {
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
return None;
}
cut.retain(|r| !r.is_empty());
Some(cut)
}

#[cfg(test)]
thread_local! {
/// The [`pole`] searches made on this thread.
Expand Down
74 changes: 57 additions & 17 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -6206,15 +6213,22 @@ 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 = || {
any_moved
&& 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.)
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
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;
Expand All @@ -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 };
Expand Down Expand Up @@ -6652,21 +6666,30 @@ 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),
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)
};
// 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) => {
Expand All @@ -6675,30 +6698,47 @@ 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))
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}
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) }
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
})
.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(..)) {
let on_map = |(x, y): (f32, f32)| match &globe {
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));
Expand Down
Loading
Loading