mln-rs-engine 0.0.20

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
//! `Map::camera_for_lat_lngs`: which padding a fit keeps, and how its margin
//! and maximum zoom frame the area.

use approx::assert_abs_diff_eq;
use mln_style::geo::{LatLng, LatLngBounds};
use mln_style::size::Size;

use crate::camera::{CameraFitOptions, CameraOptions};
use crate::exec::queue::Queue;
use crate::geo::{EdgeInsets, ScreenCoordinate};
use crate::map::{Map, MapOptions};
use crate::storage::stub_file_source::{ResponseType, StubFileSource};

fn map(run_loop: &Queue<()>) -> Map {
    let file_source = StubFileSource::on_queue(run_loop, ResponseType::Asynchronous);
    let options = MapOptions { size: Size::new(512, 512), ..Default::default() };
    Map::with_file_source(file_source, options, crate::tests::registry(), &crate::map::tests::pool()).expect("map")
}

fn ll(lat: f64, lon: f64) -> LatLng {
    LatLng::new(lat, lon).expect("LatLng")
}

fn insets(top: f64, left: f64, bottom: f64, right: f64) -> EdgeInsets {
    EdgeInsets::new(top, left, bottom, right)
}

fn route() -> LatLngBounds {
    LatLngBounds::hull(ll(35.0, -125.0), ll(39.0, -115.0))
}

/// Asserts that `map`, moved to `camera`, shows `bounds` inside the view inset
/// by `frame`, touching at least one of its edges.
fn assert_framed(map: &mut Map, camera: &CameraOptions, bounds: &LatLngBounds, frame: &EdgeInsets) {
    map.jump_to(camera);
    let corners = [bounds.northwest(), bounds.northeast(), bounds.southeast(), bounds.southwest()];
    let pixels = corners.map(|corner| map.pixel_for_lat_lng(&corner));
    let (left, top) = (frame.left(), frame.top());
    let (right, bottom) = (512.0 - frame.right(), 512.0 - frame.bottom());
    let inside = |pixel: &ScreenCoordinate| {
        (left - 1e-6..=right + 1e-6).contains(&pixel.x) && (top - 1e-6..=bottom + 1e-6).contains(&pixel.y)
    };
    assert!(pixels.iter().all(inside), "{pixels:?} outside {frame:?}");
    let touches = |value: f64, edge: f64| (value - edge).abs() < 1e-6;
    let touching = pixels.iter().any(|pixel| {
        touches(pixel.x, left) || touches(pixel.x, right) || touches(pixel.y, top) || touches(pixel.y, bottom)
    });
    assert!(touching, "{pixels:?} touch no edge of {frame:?}");
}

/// A bottom sheet's 200 pixels of padding stay the camera's, and the area
/// fits inside them and the margin.
#[test]
fn a_fit_keeps_the_padding_and_frames_the_area_inside_it_and_the_margin() {
    let run_loop = Queue::<()>::new();
    let mut map = map(&run_loop);
    let sheet = insets(200.0, 0.0, 0.0, 0.0);
    map.jump_to(&CameraOptions { padding: Some(sheet), ..Default::default() });

    for margin in [insets(20.0, 20.0, 20.0, 20.0), insets(0.0, 80.0, 30.0, 10.0)] {
        let options = CameraFitOptions { margin, ..CameraFitOptions::default() };
        let camera = map.camera_for_lat_lng_bounds(&route(), &options).unwrap();
        assert_eq!(camera.padding, Some(sheet), "{margin:?}");
        let frame = insets(sheet.top + margin.top, margin.left, margin.bottom, margin.right);
        assert_framed(&mut map, &camera, &route(), &frame);
    }
}

/// Padding in the options becomes the camera's, and the area fits inside it,
/// whatever padding the map had.
#[test]
fn a_fit_with_padding_gives_the_camera_that_padding() {
    let run_loop = Queue::<()>::new();
    let mut map = map(&run_loop);
    map.jump_to(&CameraOptions { padding: Some(insets(200.0, 0.0, 0.0, 0.0)), ..Default::default() });

    let padding = insets(20.0, 30.0, 40.0, 50.0);
    let options = CameraFitOptions { padding: Some(padding), ..CameraFitOptions::default() };
    let camera = map.camera_for_lat_lng_bounds(&route(), &options).unwrap();
    assert_eq!(camera.padding, Some(padding));
    assert_framed(&mut map, &camera, &route(), &padding);
}

/// The fitted camera sets every field that decides what it shows, so that
/// moving to it later shows the same view.
#[test]
fn a_fit_sets_the_bearing_and_pitch_it_fits_at() {
    let run_loop = Queue::<()>::new();
    let mut map = map(&run_loop);
    map.jump_to(&CameraOptions { bearing: Some(30.0), pitch: Some(10.0), ..Default::default() });

    let camera = map.camera_for_lat_lng_bounds(&route(), &CameraFitOptions::default()).unwrap();
    assert_abs_diff_eq!(camera.bearing.unwrap(), 30.0, epsilon = 1e-9);
    assert_abs_diff_eq!(camera.pitch.unwrap(), 10.0, epsilon = 1e-9);
    assert_eq!(camera.padding, Some(EdgeInsets::default()));
}

/// One point has no extent to fill the view with, so its fit stops at the
/// maximum zoom of the options, or else of the map.
#[test]
fn a_fit_of_one_point_stops_at_the_maximum_zoom() {
    let run_loop = Queue::<()>::new();
    let map = map(&run_loop);
    let point = [ll(37.0, -120.0)];

    let unbounded = map.camera_for_lat_lngs(&point, &CameraFitOptions::default()).unwrap();
    assert_eq!(unbounded.zoom, Some(map.transform_state().limits().max_zoom));

    let options = CameraFitOptions { max_zoom: Some(15.0), ..CameraFitOptions::default() };
    let capped = map.camera_for_lat_lngs(&point, &options).unwrap();
    assert_eq!(capped.zoom, Some(15.0));
    let center = capped.center.unwrap();
    assert_abs_diff_eq!(center.latitude(), 37.0, epsilon = 1e-9);
    assert_abs_diff_eq!(center.longitude(), -120.0, epsilon = 1e-9);

    // The map's limits still apply.
    let below = CameraFitOptions { max_zoom: Some(-5.0), ..CameraFitOptions::default() };
    let zoom = map.camera_for_lat_lngs(&point, &below).unwrap().zoom.unwrap();
    assert_eq!(zoom, map.transform_state().min_zoom());
}