Skip to main content

mln_engine/tile_store/
tile_lod.rs

1use mln_style::math::deg2rad;
2
3/// The pitch at which Mercator's horizon reaches the top of the screen, in
4/// degrees.
5pub(crate) const MAX_MERCATOR_HORIZON_ANGLE_DEGREES: f64 = 89.25;
6
7const ASSUMED_MAX_FEATURE_HEIGHT_METERS: f64 = 500.0;
8const TILE_CULLING_HORIZON_ONSET_DEGREES: f64 = 15.0;
9
10fn integral_of_cos_x_by_p(p: f64, x1: f64, x2: f64) -> f64 {
11    const NUM_POINTS: i32 = 10;
12    let mut sum = 0.0;
13    let dx = (x2 - x1) / f64::from(NUM_POINTS);
14    for i in 0..NUM_POINTS {
15        let x = x1 + (f64::from(i) + 0.5) / f64::from(NUM_POINTS) * (x2 - x1);
16        sum += dx * x.cos().powf(p);
17    }
18    sum
19}
20
21/// The zoom each tile is loaded at when the view is pitched: finer for tiles
22/// nearer the camera, coarser toward the horizon, under a total tile budget.
23/// GL JS `createCalculateTileZoomFunction`, which GL JS applies to both
24/// projections (the Mercator cover uses it whenever terrain is present or the
25/// pitch passes 78.5 - fov/2 degrees).
26#[derive(Clone, Copy, Debug)]
27pub(crate) struct TileZoomFunction {
28    camera_vertical_fov: f64,
29    pitch_tile_loading_behavior: f64,
30    tile_count_pitch0: f64,
31    tile_count_max_min_ratio: f64,
32}
33
34impl TileZoomFunction {
35    pub(crate) const DEFAULT_MAX_ZOOM_LEVELS_ON_SCREEN: f64 = 9.314;
36    pub(crate) const DEFAULT_TILE_COUNT_MAX_MIN_RATIO: f64 = 3.0;
37
38    /// The terms that depend on the field of view alone are computed once
39    /// here, for a whole cover.
40    #[must_use]
41    pub(crate) fn new(
42        camera_vertical_fov_degrees: f64,
43        max_zoom_levels_on_screen: f64,
44        tile_count_max_min_ratio: f64,
45    ) -> Self {
46        let pitch_tile_loading_behavior = 2.0
47            * ((max_zoom_levels_on_screen - 1.0)
48                / (deg2rad(MAX_MERCATOR_HORIZON_ANGLE_DEGREES - camera_vertical_fov_degrees).cos()
49                    / deg2rad(MAX_MERCATOR_HORIZON_ANGLE_DEGREES).cos())
50                .log2()
51                - 1.0);
52        TileZoomFunction {
53            camera_vertical_fov: camera_vertical_fov_degrees,
54            pitch_tile_loading_behavior,
55            tile_count_pitch0: 2.0
56                * integral_of_cos_x_by_p(
57                    pitch_tile_loading_behavior - 1.0,
58                    0.0,
59                    deg2rad(camera_vertical_fov_degrees / 2.0),
60                ),
61            tile_count_max_min_ratio,
62        }
63    }
64
65    /// The zoom to load a tile at. Distances share one unit (GL JS uses
66    /// Mercator units at zoom 0). Returns an unrounded zoom.
67    #[must_use]
68    pub(crate) fn zoom_for(
69        &self,
70        requested_center_zoom: f64,
71        distance_to_tile_2d: f64,
72        distance_to_tile_z: f64,
73        distance_to_center_3d: f64,
74    ) -> f64 {
75        let center_pitch = f64::min(1.0, distance_to_tile_z / distance_to_center_3d).acos();
76        let highest_pitch = f64::min(
77            deg2rad(MAX_MERCATOR_HORIZON_ANGLE_DEGREES),
78            center_pitch + deg2rad(self.camera_vertical_fov / 2.0),
79        );
80        let lowest_pitch = f64::min(highest_pitch, center_pitch - deg2rad(self.camera_vertical_fov / 2.0));
81        let tile_count = integral_of_cos_x_by_p(self.pitch_tile_loading_behavior - 1.0, lowest_pitch, highest_pitch);
82        let this_tile_pitch = distance_to_tile_2d.atan2(distance_to_tile_z);
83        let distance_to_tile_3d = distance_to_tile_2d.hypot(distance_to_tile_z);
84        let mut desired = requested_center_zoom;
85        desired += (distance_to_center_3d
86            / distance_to_tile_3d
87            / f64::max(0.5, deg2rad(self.camera_vertical_fov / 2.0).cos()))
88        .log2();
89        desired += self.pitch_tile_loading_behavior * this_tile_pitch.cos().log2() / 2.0;
90        desired -= f64::max(1.0, tile_count / self.tile_count_pitch0 / self.tile_count_max_min_ratio).log2() / 2.0;
91        desired
92    }
93}
94
95/// Near the bottom of a steeply pitched view, tiles are culled with room for
96/// the tallest feature they might hold, so buildings do not vanish before
97/// their tile leaves the screen. GL JS `getElevationForTileCulling`:
98/// `center_elevation` is the terrain height under the map center and
99/// `max_content_elevation` the tallest content the style is known to hold;
100/// both are zero without terrain.
101#[must_use]
102pub(crate) fn elevation_for_tile_culling(
103    pitch_degrees: f64,
104    fov_degrees: f64,
105    center_elevation: f64,
106    max_content_elevation: f64,
107) -> f64 {
108    let bottom_edge_degrees_above_horizontal = MAX_MERCATOR_HORIZON_ANGLE_DEGREES - pitch_degrees - fov_degrees / 2.0;
109    let proximity = (TILE_CULLING_HORIZON_ONSET_DEGREES - bottom_edge_degrees_above_horizontal)
110        / TILE_CULLING_HORIZON_ONSET_DEGREES;
111    let proximity_to_horizon = proximity.clamp(0.0, 1.0);
112    center_elevation + proximity_to_horizon * f64::max(ASSUMED_MAX_FEATURE_HEIGHT_METERS, max_content_elevation)
113}
114
115#[cfg(test)]
116mod tests {
117    use super::TileZoomFunction;
118
119    #[test]
120    fn level_camera_over_containing_tile_does_not_return_nan() {
121        let zoom = TileZoomFunction::new(
122            36.87,
123            TileZoomFunction::DEFAULT_MAX_ZOOM_LEVELS_ON_SCREEN,
124            TileZoomFunction::DEFAULT_TILE_COUNT_MAX_MIN_RATIO,
125        );
126        // At pitch = 90°, camera_coord.z == center_coord.z (distance_z == 0.0)
127        // on platforms whose trig leaves no residual, and the root tile contains
128        // the camera (distance_to_tile_2d == 0.0).
129        let desired = zoom.zoom_for(13.0, 0.0, 0.0, 1e-4);
130        assert!(!desired.is_nan() && desired >= 13.0, "desired zoom: {desired}");
131    }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
135#[repr(u8)]
136pub enum TileLodMode {
137    /// Default `TileLOD` algorithm
138    #[default]
139    Default,
140    /// Distance-based `TileLOD` algorithm
141    Distance,
142}