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