mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! One view of a map: the camera, size and projection that a map snapshot or a
//! rendered frame shows.

use mln_style::crs::WorldMapping;
use mln_style::error::Error;
use mln_style::geo::{LatLng, WrapMode};
use mln_style::size::Size;
use mln_style::types::ProjectionDefinition;

use crate::camera::camera::CameraOptions;
use crate::camera::camera_limits::{CameraLimits, MAX_FIELD_OF_VIEW, MIN_FIELD_OF_VIEW};
use crate::camera::constants;
use crate::camera::mode::ConstrainMode;
use crate::camera::projection_mode::{AxonometricMode, ProjectionMode};
use crate::camera::transform_state::TransformState;
use crate::camera::view::{ViewParams, center_offset, zoom_scale};
use crate::geo::{EdgeInsets, NorthOrientation};

/// A camera with every member set: the camera that a map has, where
/// [`CameraOptions`] is a change to one.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Camera {
    /// The coordinate at the center. It keeps the copy of the world that the
    /// camera is in, so its longitude can be outside -180 to 180.
    pub center: LatLng,
    /// The altitude of the center, in meters above sea level.
    pub center_altitude: f64,
    /// Insets from the map's edges. The camera centers within the rest.
    pub padding: EdgeInsets,
    pub zoom: f64,
    /// The rotation from north, in degrees.
    pub bearing: f64,
    /// The tilt toward the horizon, in degrees. 0 looks straight down.
    pub pitch: f64,
    /// The roll, in degrees.
    pub roll: f64,
    /// The vertical field of view, in degrees.
    pub fov: f64,
}

impl From<Camera> for CameraOptions {
    /// Options that set every member of the camera, and no anchor.
    fn from(camera: Camera) -> Self {
        CameraOptions {
            center: Some(camera.center),
            center_altitude: Some(camera.center_altitude),
            padding: Some(camera.padding),
            anchor: None,
            zoom: Some(camera.zoom),
            bearing: Some(camera.bearing),
            pitch: Some(camera.pitch),
            roll: Some(camera.roll),
            fov: Some(camera.fov),
        }
    }
}

/// A size in logical pixels, and the physical pixels in each logical pixel.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LogicalExtent {
    pub size: Size,
    pub scale_factor: f64,
}

impl LogicalExtent {
    #[must_use]
    pub fn new(size: Size, scale_factor: f64) -> LogicalExtent {
        LogicalExtent { size, scale_factor }
    }

    /// The size in physical pixels of a target with this extent: each
    /// dimension times the scale factor, rounded to the nearest pixel, so a
    /// dimension under half a pixel has none. `None` for an empty size, a
    /// scale factor that isn't finite and positive, or a dimension too large
    /// for `u32`.
    ///
    /// A host that fits its surface or view to the map's target sizes it by
    /// this, so that the two agree to the pixel. Rounding keeps a product a
    /// hair short of a whole number, as with a scale factor rounded to `f32`
    /// (5/3 as 1.6666666), at that number.
    #[must_use]
    pub fn physical_size(&self) -> Option<Size> {
        if self.size.is_empty() || !self.scale_factor.is_finite() || self.scale_factor <= 0.0 {
            return None;
        }
        let physical = |logical: u32| {
            let pixels = (f64::from(logical) * self.scale_factor).round();
            (pixels <= f64::from(u32::MAX)).then_some(pixels as u32)
        };
        Some(Size::new(physical(self.size.width)?, physical(self.size.height)?))
    }
}

/// How a map orients and constrains its view.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Viewport {
    pub north_orientation: NorthOrientation,
    pub constrain_mode: ConstrainMode,
    /// Insets that move the vanishing point off the center of the map.
    pub frustum_offset: EdgeInsets,
    /// Whether the center stands on the terrain, at its height under the
    /// center, once that has loaded. The ground decides the altitude, so the
    /// camera ignores the altitude it is given, such as the style's
    /// `centerAltitude`: at rest the center stands on the ground, and a
    /// command eases the altitude onto the ground where it leaves the center.
    /// When this is off, every camera keeps the altitude it is given. Over
    /// terrain, a command's eye stays above the ground either way. Without
    /// terrain it changes nothing. On by default.
    pub center_clamped_to_ground: bool,
    /// Whether drags and zooms around a point on the globe turn the bearing,
    /// so the planet keeps its orientation, and a drag can carry the camera
    /// over a pole. Off by default: they keep the bearing, as they do on
    /// Mercator. It changes nothing while the map draws Mercator.
    pub globe_free_bearing: bool,
}

impl Default for Viewport {
    fn default() -> Self {
        Viewport {
            north_orientation: NorthOrientation::default(),
            constrain_mode: ConstrainMode::default(),
            frustum_offset: EdgeInsets::default(),
            center_clamped_to_ground: true,
            globe_free_bearing: false,
        }
    }
}

/// What a view draws the map in: a built-in projection, or a registered one.
#[derive(Clone, Debug, PartialEq)]
pub enum ViewProjection {
    /// Mercator, the globe, or the transition between them: the style's
    /// projection at the camera's zoom. A view from a map never names
    /// `globe`, which [`MapView::to_state`] resolves at the camera's zoom.
    BuiltIn(ProjectionDefinition),
    /// A registered projection, which draws flat, with how it maps lng/lat to
    /// its world square ([`View::world`](crate::camera::View::world)).
    Registered(WorldMapping),
}

impl ViewProjection {
    /// How lng/lat maps to the world square that the view draws in:
    /// Mercator's for a built-in projection.
    #[must_use]
    pub fn world(&self) -> &WorldMapping {
        match self {
            ViewProjection::BuiltIn(_) => &mln_style::crs::MERCATOR,
            ViewProjection::Registered(world) => world,
        }
    }

    /// 0 on a flat map, 1 on the globe, and in between during the
    /// transition between them.
    #[must_use]
    pub(crate) fn transition(&self) -> f64 {
        match self {
            ViewProjection::BuiltIn(definition) => definition.transition_state(),
            ViewProjection::Registered(_) => 0.0,
        }
    }
}

impl From<ProjectionDefinition> for ViewProjection {
    fn from(definition: ProjectionDefinition) -> Self {
        ViewProjection::BuiltIn(definition)
    }
}

/// One view of a map: its camera, size, and projection.
#[derive(Clone, Debug, PartialEq)]
pub struct MapView {
    pub camera: Camera,
    pub extent: LogicalExtent,
    /// Sets every member when it comes from a map. A member that a view leaves
    /// unset takes the default of [`View`](crate::camera::View).
    pub projection_mode: ProjectionMode,
    pub viewport: Viewport,
    /// What the view draws the map in.
    pub projection: ViewProjection,
}

impl MapView {
    /// A transform state that shows exactly this view, to convert coordinates
    /// for it.
    ///
    /// The state takes the camera as it is: it keeps the center's copy of the
    /// world, and has no bounds and the widest zoom and pitch limits, so no
    /// constraint of the map that the view came from moves the camera.
    ///
    /// `Err` for a view that no transform can show: a camera member that isn't
    /// finite, a center that the projection has no place for, a zoom, pitch or
    /// field of view outside the limits of every map, a padding or frustum
    /// offset below zero, an empty size, a scale factor that isn't positive,
    /// or a skew that isn't finite.
    pub fn to_state(&self) -> Result<TransformState, Error> {
        self.validate()?;
        let camera = &self.camera;
        let viewport = &self.viewport;
        let projection = match &self.projection {
            ViewProjection::BuiltIn(definition) => ViewProjection::BuiltIn(definition.at_zoom(camera.zoom)),
            registered @ ViewProjection::Registered(_) => registered.clone(),
        };
        let world = projection.world();
        let scale = zoom_scale(camera.zoom);
        let Some((x, y)) = center_offset(world, &camera.center, scale) else {
            return Err(Error::invalid_argument(format!(
                "the projection \"{}\" has no place for the view's center",
                world.name()
            )));
        };
        let params = ViewParams {
            size: self.extent.size,
            frustum_offset: viewport.frustum_offset,
            north_orientation: viewport.north_orientation,
            constrain_mode: viewport.constrain_mode,
            center_clamped_to_ground: viewport.center_clamped_to_ground,
            globe_free_bearing: viewport.globe_free_bearing,
            projection,
            padding: camera.padding,
            x,
            y,
            center_altitude: camera.center_altitude,
            scale,
            angle: -mln_style::math::deg2rad(camera.bearing),
            pitch: mln_style::math::deg2rad(camera.pitch),
            roll: mln_style::math::deg2rad(camera.roll),
            fov: mln_style::math::deg2rad(camera.fov),
            x_skew: self.projection_mode.x_skew.unwrap_or(0.0),
            y_skew: self.projection_mode.y_skew.unwrap_or(1.0),
            axonometric: self.projection_mode.axonometric.is_some_and(AxonometricMode::as_bool),
            center_wrap: WrapMode::Unwrapped,
            terrain_min_elevation: None,
        };
        Ok(TransformState::from_parts(params, CameraLimits::widest()))
    }

    /// The view that this describes, to draw it or convert coordinates for
    /// it. `Err`: as [`to_state`](Self::to_state).
    #[cfg(test)]
    pub(crate) fn to_view(&self) -> Result<crate::camera::view::View, Error> {
        Ok(self.to_state()?.view().clone())
    }

    fn validate(&self) -> Result<(), Error> {
        let camera = &self.camera;
        let finite = [camera.center_altitude, camera.zoom, camera.bearing, camera.pitch, camera.roll, camera.fov];
        if !finite.iter().all(|value| value.is_finite()) {
            return Err(Error::invalid_argument("view camera members must be finite"));
        }
        let globe = match &self.projection {
            ViewProjection::BuiltIn(definition) => {
                if !definition.transition.is_finite() {
                    return Err(Error::invalid_argument("view projection transition must be finite"));
                }
                definition.at_zoom(camera.zoom).transition_state() > 0.0
            }
            ViewProjection::Registered(_) => false,
        };
        let limits = TransformState::default();
        // The globe lets the zoom fall below the minimum toward the poles.
        let min_zoom = limits.min_zoom_at_latitude_with(camera.center.latitude(), globe);
        if !(min_zoom..=constants::MAX_ZOOM).contains(&camera.zoom)
            || !(mln_style::math::rad2deg(constants::PITCH_MIN)..=mln_style::math::rad2deg(constants::PITCH_MAX))
                .contains(&camera.pitch)
            || !(mln_style::math::rad2deg(MIN_FIELD_OF_VIEW)..=mln_style::math::rad2deg(MAX_FIELD_OF_VIEW))
                .contains(&camera.fov)
        {
            return Err(Error::invalid_argument(
                "view camera zoom, pitch and field of view must be within the limits of a map",
            ));
        }
        if !valid_insets(&camera.padding) || !valid_insets(&self.viewport.frustum_offset) {
            return Err(Error::invalid_argument("view padding and frustum offset must be finite and at least 0"));
        }
        let extent = &self.extent;
        if extent.size.is_empty() || !extent.scale_factor.is_finite() || extent.scale_factor <= 0.0 {
            return Err(Error::invalid_argument("view extent dimensions and scale factor must be positive"));
        }
        let mode = &self.projection_mode;
        if !mode.x_skew.is_none_or(f64::is_finite) || !mode.y_skew.is_none_or(f64::is_finite) {
            return Err(Error::invalid_argument("view projection skews must be finite"));
        }
        Ok(())
    }
}

fn valid_insets(insets: &EdgeInsets) -> bool {
    [insets.top, insets.left, insets.bottom, insets.right].iter().all(|value| value.is_finite() && *value >= 0.0)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn physical_size_rounds_to_the_nearest_pixel() {
        let physical =
            |width, height, scale_factor| LogicalExtent::new(Size::new(width, height), scale_factor).physical_size();
        // Browsers report 5/3 rounded to f32, at which 870 and 615 come a hair
        // short of whole pixels.
        let five_thirds = f64::from(5.0_f32 / 3.0);
        assert_eq!(physical(870, 615, five_thirds), Some(Size::new(1450, 1025)));
        assert_eq!(physical(871, 616, five_thirds), Some(Size::new(1452, 1027)));
        assert_eq!(physical(410, 731, 2.625), Some(Size::new(1076, 1919)));
        assert_eq!(physical(1, 3, 0.25), Some(Size::new(0, 1)));
        assert_eq!(physical(u32::MAX, 1, 1.0), Some(Size::new(u32::MAX, 1)));
        assert_eq!(physical(0x8000_0000, 1, 2.0), None);
        assert_eq!(physical(0, 1, 1.0), None);
        assert_eq!(physical(1, 1, f64::NAN), None);
        assert_eq!(physical(1, 1, 0.0), None);
    }
}