Skip to main content

mln_style/
subdivision_granularity.rs

1/// How finely tile geometry is split so the vertex shader can bend it onto
2/// the globe. Derived from MapLibre GL JS's subdivision granularity settings.
3#[derive(Clone, Copy, Debug, PartialEq, Eq, mln_wire::Wire)]
4pub struct SubdivisionGranularityExpression {
5    base_zoom_granularity: u32,
6    min_granularity: u32,
7    surface_granularity: u32,
8}
9
10impl SubdivisionGranularityExpression {
11    #[must_use]
12    pub const fn new(base_zoom_granularity: u32, min_granularity: u32) -> Self {
13        debug_assert!(min_granularity <= base_zoom_granularity || base_zoom_granularity == 0);
14        SubdivisionGranularityExpression { base_zoom_granularity, min_granularity, surface_granularity: 0 }
15    }
16
17    /// Sets the granularity at zoom 0 of a second rule, which halves every
18    /// second zoom level instead of every level.
19    #[must_use]
20    pub const fn with_surface_granularity(self, surface_granularity: u32) -> Self {
21        SubdivisionGranularityExpression { surface_granularity, ..self }
22    }
23
24    /// The base granularity halves with every zoom level, down to the
25    /// minimum and never below 1. The surface granularity halves every
26    /// second zoom level: a chord's distance from the sphere falls with the
27    /// square of its length, so this keeps that distance about the same in
28    /// pixels at every zoom level. A tile gets the finer of the two.
29    #[must_use]
30    pub fn granularity_for_zoom_level(&self, zoom: u8) -> u32 {
31        let base = self.base_zoom_granularity.checked_shr(u32::from(zoom)).unwrap_or(0);
32        let surface = self.surface_granularity.checked_shr(u32::from(zoom / 2)).unwrap_or(0);
33        base.max(surface).max(self.min_granularity).max(1)
34    }
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq, mln_wire::Wire)]
38pub struct SubdivisionGranularitySetting {
39    pub fill: SubdivisionGranularityExpression,
40    pub line: SubdivisionGranularityExpression,
41    pub tile: SubdivisionGranularityExpression,
42    pub stencil: SubdivisionGranularityExpression,
43    /// Circle vertices per axis: 1, 3, 5 or 7.
44    pub(crate) circle: u8,
45}
46
47impl Default for SubdivisionGranularitySetting {
48    fn default() -> Self {
49        Self::none()
50    }
51}
52
53impl SubdivisionGranularitySetting {
54    #[must_use]
55    pub const fn none() -> Self {
56        SubdivisionGranularitySetting {
57            fill: SubdivisionGranularityExpression::new(0, 0),
58            line: SubdivisionGranularityExpression::new(0, 0),
59            tile: SubdivisionGranularityExpression::new(0, 0),
60            stencil: SubdivisionGranularityExpression::new(0, 0),
61            circle: 1,
62        }
63    }
64
65    /// The granularities that the globe draws with.
66    #[must_use]
67    pub const fn globe() -> Self {
68        SubdivisionGranularitySetting {
69            fill: SubdivisionGranularityExpression::new(128, 2).with_surface_granularity(32),
70            line: SubdivisionGranularityExpression::new(512, 0),
71            tile: SubdivisionGranularityExpression::new(128, 32),
72            stencil: SubdivisionGranularityExpression::new(128, 1).with_surface_granularity(32),
73            circle: 3,
74        }
75    }
76}