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