use approx::assert_abs_diff_eq;
use geo_types::{Geometry, GeometryCollection, LineString, MultiPoint, Point, Polygon};
use mln_style::error::ErrorKind;
use mln_style::geo::{LatLng, LatLngBounds};
use mln_style::size::Size;
use crate::camera::{BoundOptions, 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() };
super::test_map(file_source, options)
}
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))
}
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:?}");
}
#[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);
}
}
#[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);
}
#[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()));
}
#[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);
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());
}
#[test]
fn a_fit_takes_the_pitch_within_the_maps_limits() {
let run_loop = Queue::<()>::new();
let mut map = map(&run_loop);
for ((min, max), asked, limit) in [((0.0, 0.0), 45.0, 0.0), ((20.0, 60.0), 0.0, 20.0)] {
map.set_bounds(&BoundOptions { min_pitch: Some(min), max_pitch: Some(max), ..Default::default() }).unwrap();
let fit = |pitch| CameraFitOptions { pitch: Some(pitch), ..CameraFitOptions::default() };
let camera = map.camera_for_lat_lng_bounds(&route(), &fit(asked)).unwrap();
assert_eq!(camera.pitch, Some(limit));
assert_eq!(camera, map.camera_for_lat_lng_bounds(&route(), &fit(limit)).unwrap());
}
}
#[test]
fn a_geometry_fits_as_its_points() {
let run_loop = Queue::<()>::new();
let map = map(&run_loop);
let points = [ll(40.7, 74.0), ll(15.7, 73.5), ll(30.8, 83.4)];
let line: LineString<f64> = points.iter().map(|point| (point.longitude(), point.latitude())).collect();
let geometry = Geometry::GeometryCollection(GeometryCollection(vec![
Geometry::Point(line.0[0].into()),
Geometry::Polygon(Polygon::new(LineString(vec![line.0[1], line.0[2], line.0[1]]), vec![])),
Geometry::GeometryCollection(GeometryCollection(Vec::new())),
]));
let options = CameraFitOptions { bearing: Some(23.0), pitch: Some(20.0), ..CameraFitOptions::default() };
assert_eq!(
map.camera_for_lat_lngs(&points, &options).unwrap(),
map.camera_for_geometry(&geometry, &options).unwrap()
);
let past_the_pole = Geometry::MultiPoint(MultiPoint(vec![Point::new(0.0, 0.0), Point::new(0.0, 91.0)]));
assert_eq!(ErrorKind::InvalidArgument, map.camera_for_geometry(&past_the_pole, &options).unwrap_err().kind);
}
#[test]
fn the_globe_gives_the_area_a_camera_shows() {
let cases: [[f64; 9]; 9] = [
[0.0, 0.0, 1.0, 0.0, 0.0, -79.917_960, -79.917_960, 79.917_960, 79.917_960],
[30.0, 20.0, 1.5, 30.0, 30.0, -56.805_976, -56.895_025, 94.307_727, 73.433_065],
[0.0, 75.0, 1.0, 0.0, 0.0, -180.0, 32.709_225, 180.0, 90.0],
[0.0, 75.0, 2.5, 0.0, 0.0, -54.890_623, 64.728_839, 54.890_623, 83.474_994],
[10.0, 10.0, 3.0, 0.0, 0.0, -25.902_645, -19.587_774, 45.902_645, 36.437_840],
[170.0, -10.0, 2.0, 45.0, 40.0, 90.751_539, -71.205_726, 267.178_009, 42.321_226],
[0.0, 80.0, 3.0, 180.0, 75.0, -27.136_825, 69.572_707, 27.136_825, 86.227_340],
[0.0, 84.5, 5.0, 180.0, 84.0, -9.769_091, 81.424_541, 9.769_091, 85.708_481],
[0.0, 85.0, 6.0, 180.0, 85.0, -7.124_132, 82.925_903, 7.124_132, 85.578_001],
];
let run_loop = Queue::<()>::new();
let mut map = map(&run_loop);
map.set_bounds(&BoundOptions { max_pitch: Some(85.0), ..Default::default() }).unwrap();
map.load_style_json(r#"{"version":8,"projection":{"type":"vertical-perspective"},"sources":{},"layers":[]}"#);
map.process_events();
for [lng, lat, zoom, bearing, pitch, west, south, east, north] in cases {
let camera = CameraOptions {
center: Some(ll(lat, lng)),
zoom: Some(zoom),
bearing: Some(bearing),
pitch: Some(pitch),
..Default::default()
};
let area = map.lat_lng_bounds_for_camera_unwrapped(&camera).unwrap();
assert_abs_diff_eq!(west, area.west(), epsilon = 1e-5);
assert_abs_diff_eq!(south, area.south(), epsilon = 1e-5);
assert_abs_diff_eq!(east, area.east(), epsilon = 1e-5);
assert_abs_diff_eq!(north, area.north(), epsilon = 1e-5);
}
}