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 mln_map::{
5    AnimationOptions, CameraOptions, CameraUpdateMode, ConstrainMode, NorthOrientation, TileLodMode, ViewportMode,
6};
7use mln_util::error::Error;
8use mln_util::geo::{EdgeInsets, LatLng, LatLngBounds};
9use mln_util::geometry::{Geometry, for_each_point};
10
11/// An area for a camera to fit in the map's view.
12#[derive(Clone, Debug, PartialEq)]
13pub enum CameraFitArea {
14    Bounds(LatLngBounds),
15    /// At least one coordinate.
16    Coordinates(Vec<LatLng>),
17    /// A geometry with at least one coordinate, whose points are longitude
18    /// `x` and latitude `y`.
19    Geometry(Geometry<f64>),
20}
21
22/// How a camera fits an area.
23#[derive(Clone, Copy, Debug, Default, PartialEq)]
24pub struct CameraFitOptions {
25    /// The space to leave between the area and each edge of the map. The
26    /// left and right insets add up to less than the map's width, and the top
27    /// and bottom to less than its height.
28    pub padding: EdgeInsets,
29    /// The rotation from north, in degrees. `None` keeps the map's.
30    pub bearing: Option<f64>,
31    /// The tilt toward the horizon, in degrees. `None` keeps the map's.
32    pub pitch: Option<f64>,
33}
34
35/// Moves the camera to fit an area: [`CameraUpdate`](mln_map::CameraUpdate)'s
36/// counterpart for [`Map::fit_camera`](crate::Map::fit_camera).
37#[derive(Clone, Default)]
38pub struct CameraFitUpdate {
39    pub mode: CameraUpdateMode,
40    pub fit: CameraFitOptions,
41    pub animation: AnimationOptions,
42}
43
44/// How the map orients and constrains its view. A setting that is `None`
45/// keeps its value.
46#[derive(Clone, Copy, Debug, Default, PartialEq)]
47pub struct ViewportOptions {
48    pub north_orientation: Option<NorthOrientation>,
49    pub constrain_mode: Option<ConstrainMode>,
50    pub viewport_mode: Option<ViewportMode>,
51    pub frustum_offset: Option<EdgeInsets>,
52}
53
54/// How the map picks the tiles to load, as its snapshot's
55/// [`TileSettings`](mln_map::TileSettings) report it. A setting that is
56/// `None` keeps its value.
57#[derive(Clone, Copy, Debug, Default, PartialEq)]
58pub struct TileOptions {
59    pub prefetch_zoom_delta: Option<u8>,
60    pub lod_min_radius: Option<f64>,
61    pub lod_scale: Option<f64>,
62    /// In degrees.
63    pub lod_pitch_threshold: Option<f64>,
64    pub lod_zoom_shift: Option<f64>,
65    pub lod_mode: Option<TileLodMode>,
66}
67
68/// The camera that fits `area` in `map`'s view. Fails with
69/// [`ErrorKind::InvalidArgument`](mln_util::ErrorKind) for an empty area,
70/// and for padding that leaves no room for it, for which the core would keep
71/// the zoom.
72pub(crate) fn camera_for(
73    map: &mln_map::Map,
74    area: &CameraFitArea,
75    options: &CameraFitOptions,
76) -> Result<CameraOptions, Error> {
77    let size = map.get_transform_state().get_size();
78    let padding = options.padding;
79    if padding.left + padding.right >= f64::from(size.width) || padding.top + padding.bottom >= f64::from(size.height) {
80        return Err(Error::invalid_argument("camera fit padding must be less than the map's width and height"));
81    }
82    let CameraFitOptions { padding, bearing, pitch } = *options;
83    match area {
84        CameraFitArea::Bounds(bounds) => map.camera_for_lat_lng_bounds(bounds, &padding, bearing, pitch),
85        CameraFitArea::Coordinates(coordinates) if coordinates.is_empty() => {
86            Err(Error::invalid_argument("coordinates must not be empty"))
87        }
88        CameraFitArea::Coordinates(coordinates) => map.camera_for_lat_lngs(coordinates, &padding, bearing, pitch),
89        CameraFitArea::Geometry(geometry) => {
90            let mut empty = true;
91            for_each_point(geometry, &mut |_| empty = false);
92            if empty {
93                return Err(Error::invalid_argument("geometry must contain at least one coordinate"));
94            }
95            map.camera_for_geometry(geometry, &padding, bearing, pitch)
96        }
97    }
98}
99
100pub(crate) fn set_viewport(map: &mut mln_map::Map, options: &ViewportOptions) {
101    if let Some(orientation) = options.north_orientation {
102        map.set_north_orientation(orientation);
103    }
104    if let Some(mode) = options.constrain_mode {
105        map.set_constrain_mode(mode);
106    }
107    if let Some(mode) = options.viewport_mode {
108        map.set_viewport_mode(mode);
109    }
110    if let Some(offset) = options.frustum_offset {
111        map.set_frustum_offset(&offset);
112    }
113}
114
115pub(crate) fn set_tile_options(map: &mut mln_map::Map, options: &TileOptions) {
116    if let Some(delta) = options.prefetch_zoom_delta {
117        map.set_prefetch_zoom_delta(delta);
118    }
119    if let Some(radius) = options.lod_min_radius {
120        map.set_tile_lod_min_radius(radius);
121    }
122    if let Some(scale) = options.lod_scale {
123        map.set_tile_lod_scale(scale);
124    }
125    // Degrees don't round-trip through radians exactly, so the threshold that
126    // a snapshot reports converts to another angle: sending it back keeps the
127    // map's, rather than changing it a little.
128    if let Some(threshold) = options.lod_pitch_threshold
129        && threshold != mln_math::rad2deg(map.get_tile_lod_pitch_threshold())
130    {
131        map.set_tile_lod_pitch_threshold(mln_math::deg2rad(threshold));
132    }
133    if let Some(shift) = options.lod_zoom_shift {
134        map.set_tile_lod_zoom_shift(shift);
135    }
136    if let Some(mode) = options.lod_mode {
137        map.set_tile_lod_mode(mode);
138    }
139}