Skip to main content

mln_runtime/
camera.rs

1//! The options of the camera commands that the core map takes as separate
2//! calls: fitting an area, the viewport and the tile settings.
3
4use geo_types::Geometry;
5use mln_map::{
6    AnimationOptions, CameraOptions, CameraUpdateMode, ConstrainMode, NorthOrientation, TileLodMode, ViewportMode,
7};
8use mln_util::error::Error;
9use mln_util::geo::{EdgeInsets, LatLng, LatLngBounds};
10use mln_util::geometry::for_each_point;
11
12/// An area for a camera to fit in the map's view.
13#[derive(Clone, Debug, PartialEq)]
14pub enum CameraFitArea {
15    Bounds(LatLngBounds),
16    /// At least one coordinate.
17    Coordinates(Vec<LatLng>),
18    /// A geometry with at least one coordinate, whose points are longitude
19    /// `x` and latitude `y`.
20    Geometry(Geometry<f64>),
21}
22
23/// How a camera fits an area.
24#[derive(Clone, Copy, Debug, Default, PartialEq)]
25pub struct CameraFitOptions {
26    /// The space to leave between the area and each edge of the map. The
27    /// left and right insets add up to less than the map's width, and the top
28    /// and bottom to less than its height.
29    pub padding: EdgeInsets,
30    /// The rotation from north, in degrees. `None` keeps the map's.
31    pub bearing: Option<f64>,
32    /// The tilt toward the horizon, in degrees. `None` keeps the map's.
33    pub pitch: Option<f64>,
34}
35
36/// Moves the camera to fit an area: [`CameraUpdate`](mln_map::CameraUpdate)'s
37/// counterpart for [`Map::fit_camera`](crate::Map::fit_camera).
38#[derive(Clone, Copy, Debug, Default, PartialEq)]
39pub struct CameraFitUpdate {
40    pub mode: CameraUpdateMode,
41    pub fit: CameraFitOptions,
42    pub animation: AnimationOptions,
43}
44
45/// How the map orients and constrains its view. A setting that is `None`
46/// keeps its value.
47#[derive(Clone, Copy, Debug, Default, PartialEq)]
48pub struct ViewportOptions {
49    pub north_orientation: Option<NorthOrientation>,
50    pub constrain_mode: Option<ConstrainMode>,
51    pub viewport_mode: Option<ViewportMode>,
52    pub frustum_offset: Option<EdgeInsets>,
53}
54
55/// How the map picks the tiles to load, as its snapshot's
56/// `TileSettings` report it. A setting that is
57/// `None` keeps its value.
58#[derive(Clone, Copy, Debug, Default, PartialEq)]
59pub struct TileOptions {
60    pub prefetch_zoom_delta: Option<u8>,
61    pub lod_min_radius: Option<f64>,
62    pub lod_scale: Option<f64>,
63    /// In degrees.
64    pub lod_pitch_threshold: Option<f64>,
65    pub lod_zoom_shift: Option<f64>,
66    pub lod_mode: Option<TileLodMode>,
67}
68
69/// The camera that fits `area` in `map`'s view. Fails with
70/// [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an empty area,
71/// and for padding that leaves no room for it, for which the core would keep
72/// the zoom.
73pub(crate) fn camera_for(
74    map: &mln_map::Map,
75    area: &CameraFitArea,
76    options: &CameraFitOptions,
77) -> Result<CameraOptions, Error> {
78    let size = map.transform_state().view().size();
79    let padding = options.padding;
80    if padding.left + padding.right >= f64::from(size.width) || padding.top + padding.bottom >= f64::from(size.height) {
81        return Err(Error::invalid_argument("camera fit padding must be less than the map's width and height"));
82    }
83    let CameraFitOptions { padding, bearing, pitch } = *options;
84    match area {
85        CameraFitArea::Bounds(bounds) => map.camera_for_lat_lng_bounds(bounds, &padding, bearing, pitch),
86        CameraFitArea::Coordinates(coordinates) if coordinates.is_empty() => {
87            Err(Error::invalid_argument("coordinates must not be empty"))
88        }
89        CameraFitArea::Coordinates(coordinates) => map.camera_for_lat_lngs(coordinates, &padding, bearing, pitch),
90        CameraFitArea::Geometry(geometry) => {
91            let mut empty = true;
92            for_each_point(geometry, &mut |_| empty = false);
93            if empty {
94                return Err(Error::invalid_argument("geometry must contain at least one coordinate"));
95            }
96            map.camera_for_geometry(geometry, &padding, bearing, pitch)
97        }
98    }
99}
100
101pub(crate) fn set_viewport(map: &mut mln_map::Map, options: &ViewportOptions) {
102    let mut viewport = map.snapshot().view.viewport;
103    viewport.north_orientation = options.north_orientation.unwrap_or(viewport.north_orientation);
104    viewport.constrain_mode = options.constrain_mode.unwrap_or(viewport.constrain_mode);
105    viewport.viewport_mode = options.viewport_mode.unwrap_or(viewport.viewport_mode);
106    viewport.frustum_offset = options.frustum_offset.unwrap_or(viewport.frustum_offset);
107    map.set_viewport(&viewport);
108}
109
110pub(crate) fn set_tile_options(map: &mut mln_map::Map, options: &TileOptions) {
111    let mut tile = map.snapshot().tile;
112    tile.prefetch_zoom_delta = options.prefetch_zoom_delta.unwrap_or(tile.prefetch_zoom_delta);
113    tile.lod_min_radius = options.lod_min_radius.unwrap_or(tile.lod_min_radius);
114    tile.lod_scale = options.lod_scale.unwrap_or(tile.lod_scale);
115    tile.lod_pitch_threshold = options.lod_pitch_threshold.unwrap_or(tile.lod_pitch_threshold);
116    tile.lod_zoom_shift = options.lod_zoom_shift.unwrap_or(tile.lod_zoom_shift);
117    tile.lod_mode = options.lod_mode.unwrap_or(tile.lod_mode);
118    map.set_tile_settings(&tile);
119}