diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 5cad2c770..9054a1ca1 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -1944,7 +1944,21 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { m.graticule = src.graticule; m.body = src.body.clone(); m.set_projection(rustgis_map::Projection::Globe); - let globe = globe_scale(&m, center, side * 0.92); + // Sized on the equator and centred as near the area as the camera draws a globe that + // small, as the whole globe's frame is (#2337): at the area's own latitude near a pole + // the camera stops at its coarsest resolution, and the disc came out larger than its + // circle, clipped. Further from the pole, the area is still on the disc and outlined. + let globe = globe_scale(&m, Coord::new(center.x, 0.0), side * 0.92); + let (at, whole) = globe_locator_center(&m, center, globe, side); + if at != center { + warnings.push(format!( + "the globe locator is centred at {:.1}° rather than the map frame's {:.1}° of latitude: nearer the pole, it can't draw the globe small enough to fit", + at.y, center.y + )); + } + if !whole { + warnings.push("the globe locator is too small to show the whole globe: its circle clips it".into()); + } // Each polygon simplified to a tenth of a millimetre on the disc (its scale in metres), // so the project keeps a sketch of the layers for it, not a second full copy of them. let tolerance_m = globe * 1e-4; @@ -2031,9 +2045,9 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { } None => s.project.maps.push(m), } - Some((name, globe, FrameShape::Circle)) + Some((name, globe, FrameShape::Circle, at)) } - Locator::Inset => Some((s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle)), + Locator::Inset => Some((s.map().name.clone(), round_scale(scale * fw / side * 4.0), FrameShape::Rectangle, center)), }; // The legend lists what the frame draws: its map synced to the frame's view first, as an // export does, so a streamed layer has its features there. (This changes the session's map, @@ -2704,7 +2718,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { let card = style == Style::Modular && column.is_some(); let (card_r, card_pad) = (3.0 * k, 2.5 * k); let surround_box = |fill: Rgba| if style.cards() { pub_card(fill, clear, 0.0, 2.0 * k) } else { pub_box(fill, clear, 0.0) }; - if let (Some((map, locator_scale, shape)), Some(r)) = (&locator_map, locator_rect) { + if let (Some((map, locator_scale, shape, locator_center)), Some(r)) = (&locator_map, locator_rect) { if let (true, Some(c)) = (card, column) { l.add( "Locator Card", @@ -2717,7 +2731,7 @@ fn publication(s: &mut Session, p: &Value) -> CmdResult { r, ElementKind::MapFrame { map: map.clone(), - center, + center: *locator_center, scale: *locator_scale, shape: shape.clone(), grid: None, @@ -5758,6 +5772,20 @@ fn drawable_globe_center(map: &Map, view: Coord, scale: f64) -> Coord { Coord::new(view.x, fit.copysign(view.y)) } +/// Where a globe locator `side_mm` across at 1:`scale` centres the globe, and whether it shows it +/// whole: as near `area` as the camera draws a globe that small ([`drawable_globe_center`], the +/// equator when it draws it that small nowhere), the disc within the circle and the area on it. +/// A locator too small for the globe even on the equator (under about 20 mm), or one whose nearest +/// centre leaves the area behind the globe's rim, keeps the area in the middle, its circle +/// clipping the globe (#2348). Whether it is whole is measured on the disc drawn, not on the +/// camera's limit: a circle a little over the smallest globe holds it at that limit. +pub(crate) fn globe_locator_center(map: &Map, area: Coord, scale: f64, side_mm: f64) -> (Coord, bool) { + let side = side_mm * rustgis_map::layout::PT_PER_MM; + let at = drawable_globe_center(map, area, scale); + let g = rustgis_map::globe::Globe::new(&rustgis_map::layout::frame_camera(map, at, scale, 0.0, 0.0), side, side); + if 2.0 * g.disc_radius() <= side && g.forward(area).is_some() { (at, true) } else { (area, false) } +} + /// Parses the publication's locator choice with its default: a globe on a sheet, none on a figure /// or where the map is itself the globe seen whole (it would only show it again, smaller). fn publication_locator(p: &Value, sheet: bool, whole_globe: bool) -> Result { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index cc9930508..ededc3d6d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -10347,7 +10347,109 @@ fn a_publication_of_the_whole_globe_fills_its_frame() { let l = s.project.layouts.last().unwrap(); assert!(r["locator"].is_string() && l.elements.iter().any(|e| e.name == "Scale Bar")); let warnings = r["warnings"].to_string(); - assert!(!warnings.contains("the whole globe"), "{warnings}"); + // Neither whole-globe notice (no scale bar, no locator). + assert!(!warnings.contains("the map shows the whole globe"), "{warnings}"); +} + +/// A globe locator moved off a polar area keeps the area on the disc (#2348): at the sizes where +/// the nearest centre the camera draws would leave it behind the rim (a locator just large enough +/// for the globe on the equator), it keeps the area in the middle, clipped, instead. +#[test] +fn a_globe_locator_never_hides_its_area_behind_the_rim() { + use rustgis_geom::Coord; + use rustgis_map::layout::{PT_PER_MM, frame_camera}; + let mut m = rustgis_map::Map::new("Locator"); + m.set_projection(rustgis_map::Projection::Globe); + let (area, side) = (Coord::new(20.0, 89.0), 40.0); + let (mut moved, mut hidden) = (0, 0); + // Steps of 0.2 %: the sizes that would hide it span about 1 % of the scale. + for i in 0..4000 { + let scale = 1e8 * 1.002f64.powi(i); + let (at, whole) = crate::cmd::layout::globe_locator_center(&m, area, scale, side); + let equator = frame_camera(&m, Coord::new(area.x, 0.0), scale, 0.0, 0.0).res < rustgis_map::camera::MAX_RES; + if whole { + let g = rustgis_map::globe::Globe::new(&frame_camera(&m, at, scale, 0.0, 0.0), side * PT_PER_MM, side * PT_PER_MM); + assert!(g.forward(area).is_some(), "1:{scale:.0}: the area behind the rim from {at:?}"); + moved += usize::from(at != area); + } else { + assert_eq!(at, area, "1:{scale:.0}: a clipped locator keeps the area in the middle"); + hidden += usize::from(equator); + } + } + assert!(moved > 0 && hidden > 0, "{moved} moved, {hidden} kept on the area though the equator fits"); +} + +/// A globe locator a little over the smallest globe the camera draws holds it whole at that limit, +/// on the equator, and says nothing is clipped; one under it is clipped (#2348). +#[test] +fn a_globe_locator_at_the_camera_limit_is_whole_when_its_disc_fits() { + use rustgis_geom::Coord; + use rustgis_map::layout::{PT_PER_MM, frame_camera}; + let mut m = rustgis_map::Map::new("Locator"); + m.set_projection(rustgis_map::Projection::Globe); + // A scale past the camera's coarsest everywhere: the smallest globe it draws, on the equator. + let (area, scale) = (Coord::new(20.0, 30.0), 1e11); + let equator = Coord::new(area.x, 0.0); + let smallest = |side: f64| { + let cam = frame_camera(&m, equator, scale, 0.0, 0.0); + assert!(cam.res >= rustgis_map::camera::MAX_RES); + 2.0 * rustgis_map::globe::Globe::new(&cam, side * PT_PER_MM, side * PT_PER_MM).disc_radius() / PT_PER_MM + }; + let over = smallest(19.0) + 0.5; + assert_eq!(crate::cmd::layout::globe_locator_center(&m, area, scale, over), (equator, true), "a {over:.1} mm locator"); + let under = smallest(19.0) - 0.5; + assert_eq!(crate::cmd::layout::globe_locator_center(&m, area, scale, under), (area, false), "a {under:.1} mm locator"); +} + +/// A publication's globe locator near a pole shows the globe whole in its circle (#2337): sized on +/// the equator and centred as near the area as the camera draws it, not at the area's latitude, +/// where the camera stops at its coarsest resolution and the disc came out larger than the circle. +/// On a flat map with an Arctic extent, and on the globe seen whole with a locator asked for. +#[test] +fn a_globe_locator_near_a_pole_fits_its_circle() { + use rustgis_map::layout::{ElementKind, PT_PER_MM, frame_camera}; + let mut s = world(); + let fits = |s: &Session, case: &str| { + let l = s.project.layouts.last().unwrap(); + let locator = l.elements.iter().find(|e| e.name == "Locator").unwrap_or_else(|| panic!("{case}: no locator")); + let ElementKind::MapFrame { map, center, scale, .. } = &locator.kind else { panic!() }; + let m = s.project.maps.iter().find(|m| &m.name == map).unwrap(); + let cam = frame_camera(m, *center, *scale, 0.0, 0.0); + assert!(cam.res < rustgis_map::camera::MAX_RES, "{case}: the camera at its coarsest, 1:{scale} at {center:?}"); + let across = 2.0 * rustgis_map::globe::Globe::new(&cam, locator.w * PT_PER_MM, locator.h * PT_PER_MM).disc_radius() / PT_PER_MM; + assert!(across <= locator.w.min(locator.h), "{case}: a disc {across:.0} mm across in a {:.0} mm circle", locator.w); + }; + let said = |r: &Value, start: &str| r["warnings"].as_array().unwrap().iter().filter_map(Value::as_str).any(|w| w.starts_with(start)); + // The locator centred where the map frame is. + let centred_with_the_map = |s: &Session, case: &str| { + let l = s.project.layouts.last().unwrap(); + let centre = |n: &str| match l.elements.iter().find(|e| e.name == n).map(|e| &e.kind) { + Some(ElementKind::MapFrame { center, .. }) => *center, + _ => panic!("{case}: no {n}"), + }; + assert_eq!(centre("Locator"), centre("Map Frame"), "{case}"); + }; + const MOVED: &str = "the globe locator is centred at"; + const CLIPPED: &str = "the globe locator is too small"; + let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [10.0, 76.0, 30.0, 81.0]})).unwrap(); + fits(&s, "Svalbard on a flat map"); + assert!(said(&r, MOVED) && !said(&r, CLIPPED), "Svalbard: {}", r["warnings"]); + // A figure's locator, too small for the globe even on the equator: clipped wherever it is, it + // keeps the area in the middle and says so. + let r = + s.run("layout.publication", &json!({"profile": "figure-1col", "extent": [10.0, 83.0, 30.0, 84.0], "locator": "globe"})).unwrap(); + assert!(said(&r, CLIPPED) && !said(&r, MOVED), "a figure's locator: {}", r["warnings"]); + centred_with_the_map(&s, "a figure's locator"); + s.run("map.projection", &json!({"type": "globe"})).unwrap(); + s.run("map.goToXY", &json!({"lon": 20.0, "lat": 84.0})).unwrap(); + let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "locator": "globe"})).unwrap(); + fits(&s, "the whole globe at 84° N"); + assert!(said(&r, MOVED) && !said(&r, CLIPPED), "the whole globe: {}", r["warnings"]); + // Away from the poles the locator stays centred on the area, as before, and says nothing. + let r = s.run("layout.publication", &json!({"profile": "a4-portrait", "extent": [-46.8, -11.6, -38.5, -2.2]})).unwrap(); + fits(&s, "north-east Brazil"); + assert!(!said(&r, MOVED) && !said(&r, CLIPPED), "north-east Brazil: {}", r["warnings"]); + centred_with_the_map(&s, "north-east Brazil"); } /// Arrangements of a publication layout's surrounds (#1379): by name, corner by corner, the @@ -51614,7 +51716,8 @@ fn a_globe_locator_says_what_simplifying_did() { .unwrap() .iter() .filter_map(|w| w.as_str()) - .filter(|w| w.starts_with("the globe locator")) + // What simplifying did, not where the locator is centred (#2348). + .filter(|w| w.starts_with("the globe locator, simplifying")) .map(String::from) .collect::>() }; diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 5d044a52c..5f9d17aed 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -411,7 +411,8 @@ fn draw_page( { let ring = frame_outline_lonlat(om, &other.2, other.3, other.4); if let Some(map) = maps.get(mi) { - outline_lonlat(&mut frame_pm, map, &view, &ring, *extent_color, *extent_width as f32); + let from_globe = om.projection.is_globe(); + outline_lonlat(&mut frame_pm, map, &view, &ring, from_globe, *extent_color, *extent_width as f32); } } // Whole pixels, so the clipped image and its outline line up. @@ -877,8 +878,9 @@ fn shape_path(shape: &FrameShape, r: Rect) -> Option { } } -/// The lon/lat outline of what a frame shows. -fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> Vec { +/// The lon/lat outline of what a frame shows, `None` where an edge of a frame on the globe is off +/// the disc: the outline breaks there rather than cutting across the globe. +fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> Vec> { let mut ring = Vec::new(); let n = 16; let edge = |t: f64, side: usize| match side { @@ -887,16 +889,37 @@ fn frame_outline_lonlat(map: &Map, cam: &rustgis_map::Camera, w: f64, h: f64) -> 2 => ((1.0 - t) * w, h), _ => (0.0, (1.0 - t) * h), }; + // A frame on the globe is read through the globe as it draws it (#2348), with a gap where an + // edge runs off the disc: one that shows the globe whole has no edge on it, and no outline. + let globe = map.projection.is_globe().then(|| rustgis_map::globe::Globe::new(cam, w, h)); + // Its longitudes kept continuous, a turn added or taken where they cross ±180°, so on a flat + // frame the outline doesn't jump across the map there. + let mut last: Option = None; + let near = |c: Coord, last: Option| match last { + Some(p) => Coord::new(c.x + 360.0 * ((p - c.x) / 360.0).round(), c.y), + None => c, + }; for side in 0..4 { for i in 0..n { let (sx, sy) = edge(i as f64 / n as f64, side); - if let Some(c) = map.to_lonlat(cam.to_map(sx, sy, w, h)) { - ring.push(c); + match &globe { + Some(g) => { + let c = g.inverse(sx, sy).map(|c| near(c, last)); + if let Some(c) = c { + last = Some(c.x); + } + ring.push(c); + } + None => { + if let Some(c) = map.to_lonlat(cam.to_map(sx, sy, w, h)) { + ring.push(Some(c)); + } + } } } } if let Some(f) = ring.first().copied() { - ring.push(f); + ring.push(f.map(|c| near(c, last))); } ring } @@ -1154,15 +1177,39 @@ fn frame_viewport( } /// Strokes the ring `ring` (longitude, latitude) on a frame's map `width` points wide: another -/// frame's extent, as an extent indicator shows it. -fn outline_lonlat(pm: &mut Canvas, map: &Map, view: &View, ring: &[Coord], color: Rgba, width: f32) { +/// frame's extent, as an extent indicator shows it, broken at each `None`. `from_globe`: the ring +/// is a frame on the globe's, its longitudes run on across ±180° rather than kept in a world. +fn outline_lonlat(pm: &mut Canvas, map: &Map, view: &View, ring: &[Option], from_globe: bool, color: Rgba, width: f32) { let ppp = view.pixels_per_point as f32; + // On a globe, through the globe as the frame draws it, breaking behind its horizon (#2348): + // through the flat camera the outline lands off the area once it is away from the centre. + let globe = map.projection.is_globe().then(|| rustgis_map::globe::Globe::new(&view.camera, view.width, view.height)); + // A frame on the globe's ring on a flat map, which doesn't wrap: the whole ring a turn east or + // west if that brings the middle of its longitudes nearer the frame's centre, as they run on + // from its first point (#2348), which can be on the other side of ±180° from it. One shift for + // all keeps it in one piece. A flat frame's ring is drawn where it is. Not handled: a globe + // frame around a pole, whose ring spans a whole turn, so a stretch of it stays off the world. + let (west, east) = ring.iter().flatten().fold((f64::INFINITY, f64::NEG_INFINITY), |(w, e), c| (w.min(c.x), e.max(c.x))); + let shift = match (from_globe && globe.is_none() && west <= east, map.to_lonlat(view.camera.center)) { + (true, Some(mid)) => 360.0 * ((mid.x - (west + east) / 2.0) / 360.0).round(), + _ => 0.0, + }; let mut pb = PathBuilder::new(); let mut first = true; for c in ring { - let Some(m) = map.to_map(*c) else { continue }; - // A tilted frame's outline breaks where it passes behind the eye. - let Some((x, y)) = view.camera.try_to_screen(m, view.width, view.height) else { + let Some(c) = c else { + first = true; + continue; + }; + let at = match &globe { + Some(g) => g.forward(*c), + None => match map.to_map(Coord::new(c.x + shift, c.y)) { + // A tilted frame's outline breaks where it passes behind the eye. + Some(m) => view.camera.try_to_screen(m, view.width, view.height), + None => continue, + }, + }; + let Some((x, y)) = at else { first = true; continue; }; @@ -5256,6 +5303,241 @@ Elaboração: RustGIS", assert!(t > 0 && w > 3 * t, "a wider outline covers more: {t} at 1 pt, {w} at 6 pt"); } + /// On a globe, the extent indicator is drawn where the globe shows the area, also away from + /// the disc's centre (#2348): through the flat camera, Svalbard 18° north of the centre came out + /// half as far again up, nearly 5 mm off on a 50 mm locator. + #[test] + fn an_extent_indicator_on_a_globe_lands_on_the_area() { + use rustgis_map::layout::{INDICATOR_COLOR, INDICATOR_WIDTH, frame_camera}; + let (area, at, scale) = (Coord::new(20.0, 78.0), Coord::new(20.0, 60.0), 2e8); + let mut globe = Map::new("g"); + globe.set_projection(rustgis_map::Projection::Globe); + let maps = || [Map::new("m"), globe.clone()]; + let frame = |map: &str, center, scale, extent_of| ElementKind::MapFrame { + map: map.into(), + center, + scale, + shape: FrameShape::Rectangle, + grid: None, + extent_of, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + let mut l = Layout::new("Layout", 100.0, 100.0); + let main = l.add("Main", (60.0, 60.0, 30.0, 30.0), frame("m", area, 2e7, None)); + l.add("Locator", (5.0, 5.0, 50.0, 50.0), frame("g", at, scale, Some(main))); + let pm = render(&l, &mut maps(), 96.0, &PageInfo::default()).unwrap(); + // Where the globe draws the area, in pixels on the page. + let side = 50.0 * PT_PER_MM; + let g = rustgis_map::globe::Globe::new(&frame_camera(&globe, at, scale, 0.0, 0.0), side, side); + let (x, y) = g.forward(area).unwrap(); + // At 96 dpi a layout point is a pixel. + let expected = (5.0 * PT_PER_MM + x, 5.0 * PT_PER_MM + y); + // The red pixels' middle, inside the locator. + let px_mm = PT_PER_MM; + let red: Vec<(f64, f64)> = ((5.0 * px_mm) as u32..(55.0 * px_mm) as u32) + .flat_map(|y| ((5.0 * px_mm) as u32..(55.0 * px_mm) as u32).map(move |x| (x, y))) + .filter(|&(x, y)| { + let (r, g, b) = rgb(&pm, x, y); + r > 180 && g < 90 && b < 90 + }) + .map(|(x, y)| (f64::from(x), f64::from(y))) + .collect(); + assert!(!red.is_empty(), "no extent indicator on the globe"); + let n = red.len() as f64; + let mid = (red.iter().map(|p| p.0).sum::() / n, red.iter().map(|p| p.1).sum::() / n); + let off = ((mid.0 - expected.0).powi(2) + (mid.1 - expected.1).powi(2)).sqrt() / px_mm; + assert!(off < 1.5, "the indicator's middle {mid:?} is {off:.1} mm from the area at {expected:?}"); + } + + /// A frame on the globe is outlined by what it shows of the globe (#2348): zoomed in on Iceland, + /// around Iceland on a globe locator; seen whole, with no edge on the disc, not at all (through + /// the flat camera its edges were read as a flat world, a line drawn across the locator). + #[test] + fn a_frame_on_the_globe_is_outlined_by_what_it_shows() { + use rustgis_map::layout::{INDICATOR_COLOR, INDICATOR_WIDTH, frame_camera}; + let mut globe = Map::new("g"); + globe.set_projection(rustgis_map::Projection::Globe); + let frame = |center, scale, extent_of| ElementKind::MapFrame { + map: "g".into(), + center, + scale, + shape: FrameShape::Rectangle, + grid: None, + extent_of, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + let (at, scale) = (Coord::new(-20.0, 55.0), 2e8); + // The red pixels inside the locator (5–55 mm), with the source frame on `center` at 1:`main`. + let red = |center: Coord, main: f64| { + let mut l = Layout::new("Layout", 100.0, 100.0); + // 40 mm across: near the equator, wider than the smallest globe the camera draws there. + let source = l.add("Main", (58.0, 58.0, 40.0, 40.0), frame(center, main, None)); + l.add("Locator", (5.0, 5.0, 50.0, 50.0), frame(at, scale, Some(source))); + let pm = render(&l, &mut [globe.clone()], 96.0, &PageInfo::default()).unwrap(); + let k = PT_PER_MM; + ((5.0 * k) as u32..(55.0 * k) as u32) + .flat_map(|y| ((5.0 * k) as u32..(55.0 * k) as u32).map(move |x| (x, y))) + .filter(|&(x, y)| { + let (r, g, b) = rgb(&pm, x, y); + r > 180 && g < 90 && b < 90 + }) + .map(|(x, y)| (f64::from(x), f64::from(y))) + .collect::>() + }; + let iceland = red(Coord::new(-19.0, 65.0), 2e7); + assert!(!iceland.is_empty(), "no outline of the zoomed-in frame"); + let n = iceland.len() as f64; + let mid = (iceland.iter().map(|p| p.0).sum::() / n, iceland.iter().map(|p| p.1).sum::() / n); + let side = 50.0 * PT_PER_MM; + let g = rustgis_map::globe::Globe::new(&frame_camera(&globe, at, scale, 0.0, 0.0), side, side); + let (x, y) = g.forward(Coord::new(-19.0, 65.0)).unwrap(); + let expected = (5.0 * PT_PER_MM + x, 5.0 * PT_PER_MM + y); + let off = ((mid.0 - expected.0).powi(2) + (mid.1 - expected.1).powi(2)).sqrt() / PT_PER_MM; + assert!(off < 1.5, "the outline's middle {mid:?} is {off:.1} mm from Iceland at {expected:?}"); + let whole = red(Coord::new(-20.0, 10.0), 4e8); + assert!(whole.is_empty(), "the globe seen whole outlined by {} red pixels", whole.len()); + } + + /// A frame on the globe whose sides run off the disc is outlined by its top and bottom edges + /// only, broken where they leave the disc, not joined by a line across the globe (#2348). + #[test] + fn a_frame_partly_off_the_globe_is_outlined_without_a_chord() { + use rustgis_map::layout::{INDICATOR_COLOR, INDICATOR_WIDTH, frame_camera}; + let mut globe = Map::new("g"); + globe.set_projection(rustgis_map::Projection::Globe); + let frame = |center, scale, extent_of| ElementKind::MapFrame { + map: "g".into(), + center, + scale, + shape: FrameShape::Rectangle, + grid: None, + extent_of, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + let center = Coord::new(-20.0, 0.0); + // The source frame, 40 × 20 mm: a scale whose disc is taller than the frame, its sides in + // the sky beside the disc, and its corners too. + let (sw, sh) = (40.0, 20.0); + let disc = |scale: f64| { + let cam = frame_camera(&globe, center, scale, 0.0, 0.0); + rustgis_map::globe::Globe::new(&cam, sw * PT_PER_MM, sh * PT_PER_MM).disc_radius() / PT_PER_MM + }; + let source_scale = (1..400).map(|i| f64::from(i) * 1e6).find(|&s| disc(s) < 0.5 * sw - 3.0).unwrap(); + assert!(disc(source_scale) > 0.5 * sh + 2.0, "a disc {:.1} mm across", 2.0 * disc(source_scale)); + let (at, scale) = (center, 2.0 * source_scale); + let mut l = Layout::new("Layout", 100.0, 100.0); + let source = l.add("Main", (55.0, 70.0, sw, sh), frame(center, source_scale, None)); + l.add("Locator", (5.0, 5.0, 50.0, 50.0), frame(at, scale, Some(source))); + let pm = render(&l, &mut [globe.clone()], 96.0, &PageInfo::default()).unwrap(); + let red = |x: u32, y: u32| { + let (r, g, b) = rgb(&pm, x, y); + r > 180 && g < 90 && b < 90 + }; + let k = PT_PER_MM; + let (x0, x1) = ((5.0 * k) as u32, (55.0 * k) as u32); + let inked = (x0..x1).flat_map(|y| (x0..x1).map(move |x| (x, y))).filter(|&(x, y)| red(x, y)).count(); + assert!(inked > 0, "no outline of the frame"); + // The row through the source frame's centre, between its top and bottom edges: crossed only + // by a chord. + let side = 50.0 * k; + let g = rustgis_map::globe::Globe::new(&frame_camera(&globe, at, scale, 0.0, 0.0), side, side); + let (_, cy) = g.forward(center).unwrap(); + let row = (5.0 * k + cy).round() as u32; + let crossed: Vec = (row - 2..=row + 2).flat_map(|y| (x0..x1).filter(move |&x| red(x, y))).collect(); + assert!(crossed.is_empty(), "the outline crosses the middle of the globe at {crossed:?}"); + } + + /// A frame on the globe across the antimeridian, outlined on a flat frame, stays one small box + /// near the flat frame's centre (#2348): its edge's longitudes kept continuous, not jumping from + /// 179.5° to -179.5° and drawing a line across the flat map, and brought to the flat frame's side + /// of ±180° when they start on the other. + #[test] + fn a_globe_frame_across_the_antimeridian_is_outlined_in_one_piece() { + use rustgis_map::layout::{INDICATOR_COLOR, INDICATOR_WIDTH}; + let mut globe = Map::new("g"); + globe.set_projection(rustgis_map::Projection::Globe); + let frame = |map: &str, center, scale, extent_of| ElementKind::MapFrame { + map: map.into(), + center, + scale, + shape: FrameShape::Rectangle, + grid: None, + extent_of, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + // The globe frame's centre and the flat locator's: on 180°, and a frame on -179° whose + // first point (its top-left corner) is on the +180° side, on a locator on -175°. + for (source_at, locator_at) in [(180.0, 180.0), (-179.0, -175.0)] { + let mut l = Layout::new("Layout", 100.0, 100.0); + // 40 mm at 1:20M, about 800 km across: 4 mm on the locator at 1:200M. + let source = l.add("Main", (58.0, 58.0, 40.0, 40.0), frame("g", Coord::new(source_at, 0.0), 2e7, None)); + l.add("Locator", (5.0, 5.0, 50.0, 50.0), frame("m", Coord::new(locator_at, 0.0), 2e8, Some(source))); + let pm = render(&l, &mut [globe.clone(), Map::new("m")], 96.0, &PageInfo::default()).unwrap(); + let k = PT_PER_MM; + let (x0, x1) = ((5.0 * k) as u32, (55.0 * k) as u32); + let red: Vec<(u32, u32)> = (x0..x1) + .flat_map(|y| (x0..x1).map(move |x| (x, y))) + .filter(|&(x, y)| { + let (r, g, b) = rgb(&pm, x, y); + r > 180 && g < 90 && b < 90 + }) + .collect(); + assert!(!red.is_empty(), "{source_at}° on {locator_at}°: no outline of the frame"); + let middle = 30.0 * k; + let far = + red.iter().filter(|&&(x, y)| (f64::from(x) - middle).abs() > 6.0 * k || (f64::from(y) - middle).abs() > 6.0 * k).count(); + assert_eq!(far, 0, "{source_at}° on {locator_at}°: {far} of {} red pixels more than 6 mm from the middle", red.len()); + } + } + + /// A flat frame wider than the world, outlined on another flat frame, is drawn where it is + /// (#2348): its first point past -180° doesn't move it a turn east, so both its sides show. + #[test] + fn a_flat_frame_wider_than_the_world_is_outlined_where_it_is() { + use rustgis_map::layout::{INDICATOR_COLOR, INDICATOR_WIDTH}; + let frame = |center, scale, extent_of| ElementKind::MapFrame { + map: "m".into(), + center, + scale, + shape: FrameShape::Rectangle, + grid: None, + extent_of, + extent_color: INDICATOR_COLOR, + extent_width: INDICATOR_WIDTH, + bearing: 0.0, + pitch: 0.0, + }; + let at = Coord::new(0.0, 0.0); + let mut l = Layout::new("Layout", 200.0, 120.0); + // 80 mm at 1:529M on the equator: about 380° of longitude, -190° to 190°. + let source = l.add("Main", (5.0, 5.0, 80.0, 20.0), frame(at, 5.29e8, None)); + // 90 mm at 1:593M: about 480°, so both of the source's sides are in it. + l.add("Locator", (100.0, 5.0, 90.0, 60.0), frame(at, 5.93e8, Some(source))); + let pm = render(&l, &mut [Map::new("m")], 96.0, &PageInfo::default()).unwrap(); + let k = PT_PER_MM; + let red = |x: u32, y: u32| { + let (r, g, b) = rgb(&pm, x, y); + r > 180 && g < 90 && b < 90 + }; + let (top, bottom) = ((5.0 * k) as u32, (65.0 * k) as u32); + let middle = 145.0 * k; + let side = |xs: std::ops::Range| xs.flat_map(|x| (top..bottom).map(move |y| (x, y))).filter(|&(x, y)| red(x, y)).count(); + let (west, east) = (side((100.0 * k) as u32..(middle - 10.0 * k) as u32), side((middle + 10.0 * k) as u32..(190.0 * k) as u32)); + assert!(west > 0 && east > 0, "the outline's sides: {west} red pixels west of the middle, {east} east"); + } + fn rgb(pm: &Pixmap, x: u32, y: u32) -> (u8, u8, u8) { pm.pixel(x, y).map(|p| (p.red(), p.green(), p.blue())).unwrap_or((255, 255, 255)) } diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index ba0f0919b..87d6959ba 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -149,7 +149,12 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu layers held in the project. Streamed layers, rasters, points, lines and labels are left out. What simplifying changes or leaves alone there (a vertex past a pole taken to the pole, a polygon left as it was for a missing coordinate, or too intricate to simplify whole) is said - in the layout's warnings; one kept as it was because its ring would collapse is not. + in the layout's warnings; one kept as it was because its ring would collapse is not. The + globe is whole in its circle: near a pole, where it can't be drawn that small at the area's + latitude, it is centred as near the area as fits, the area still outlined on it, and the + warnings say so. A locator too small for the whole globe anywhere (a figure's, under about + 20 mm), or one that would have to move so far that the area went behind the globe's rim, + stays centred on the area, its circle clipping the globe, which the warnings also say. It uses the map's basemap, not its web tiles, in a round frame, the globe nearly filling it and the area outlined in red; `"inset"` shows the surrounding region (about four times the area across) on the map itself, outlined the same way; `"none"` (a figure's