Skip to main content

mln_engine/tile_store/
dem.rs

1//! raster-dem: what a DEM tile holds, and its border. Each DEM tile takes
2//! its border from its eight neighbours' edges, so that hillshading is
3//! seamless across tiles. Whenever a tile's DEM arrives, the store backfills
4//! it and its renderable neighbours both ways, so a DEM that reloads refills
5//! its border and theirs.
6
7use crate::draw::identity::SimpleIdentity;
8use crate::layer::layer_drawing::MadeFrom;
9use crate::tile::OverscaledTileID;
10use crate::tile::dem_data::DEMData;
11use crate::tile_store::contents::Contents;
12use crate::tile_store::raster::Masking;
13
14/// A raster-dem tile's DEM, which the tile backfills from its neighbours,
15/// and the parts of the tile it draws. The layers and the terrain that read
16/// it make their textures and quads of it.
17pub struct DemContents {
18    id: SimpleIdentity,
19    dem: DEMData,
20    pub masking: Masking,
21    /// Counts the backfills of the DEM's border, after which what is made of
22    /// the DEM is made again.
23    generation: u64,
24}
25
26impl DemContents {
27    #[must_use]
28    pub(crate) fn new(dem: DEMData) -> Self {
29        DemContents { id: SimpleIdentity::new(), dem, masking: Masking::default(), generation: 0 }
30    }
31
32    /// The contents' identity, by which the layers and the terrain keep what
33    /// they make of them.
34    #[must_use]
35    pub fn id(&self) -> SimpleIdentity {
36        self.id
37    }
38
39    #[must_use]
40    pub fn dem(&self) -> &DEMData {
41        &self.dem
42    }
43
44    /// The number of times the DEM's border changed.
45    #[must_use]
46    pub fn generation(&self) -> u64 {
47        self.generation
48    }
49
50    /// What a layer's value of a tile of these contents is made from: their
51    /// identity, with the generations of their mask and of their DEM's
52    /// border.
53    #[must_use]
54    pub(crate) fn stamp(&self) -> MadeFrom {
55        (self.id, [self.masking.generation(), self.generation])
56    }
57
58    /// Whether there is a DEM to draw.
59    #[must_use]
60    pub fn has_data(&self) -> bool {
61        self.dem.image().valid()
62    }
63
64    /// Copies the edge of `border`, the tile's neighbour at `offset`, into
65    /// the DEM's border.
66    pub(crate) fn backfill(&mut self, border: &DemContents, offset: Offset) {
67        self.dem.backfill_border(&border.dem, offset.dx, offset.dy);
68        self.generation += 1;
69    }
70}
71
72/// Where a neighbour is, in tiles: `dx` east and `dy` south.
73#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74pub(crate) struct Offset {
75    pub(crate) dx: i8,
76    pub(crate) dy: i8,
77}
78
79impl Offset {
80    fn opposite(self) -> Offset {
81        Offset { dx: -self.dx, dy: -self.dy }
82    }
83}
84
85/// The neighbours of tile `id`, and where each is. A neighbour across the
86/// antimeridian is in the next world copy. Rows above the first and below
87/// the last have no tiles; at zoom 0, the tile's neighbours east and west
88/// are itself, which it has no border from.
89pub(crate) fn neighbors(id: &OverscaledTileID) -> impl Iterator<Item = (Offset, OverscaledTileID)> + '_ {
90    let canonical = id.canonical;
91    let dim = 1i64 << canonical.z;
92    (-1i8..=1).flat_map(move |dy| (-1i8..=1).map(move |dx| Offset { dx, dy })).filter_map(move |offset| {
93        let y = i64::from(canonical.y) + i64::from(offset.dy);
94        if offset == (Offset { dx: 0, dy: 0 }) || !(0..dim).contains(&y) || (offset.dx != 0 && dim == 1) {
95            return None;
96        }
97        let x = i64::from(canonical.x) + i64::from(offset.dx);
98        let wrap = id.wrap + x.div_euclid(dim) as i16;
99        let neighbor =
100            OverscaledTileID::with_zxy(id.overscaled_z, wrap, canonical.z, x.rem_euclid(dim) as u32, y as u32);
101        Some((offset, neighbor))
102    })
103}
104
105/// Backfills the DEM of `tile`, a raster-dem tile's contents, from the edge
106/// of `border`, its neighbour's at `offset`, and `border`'s from `tile`'s, if
107/// `border` is renderable.
108pub(crate) fn backfill_pair(tile: &mut Contents, border: &mut Contents, offset: Offset) {
109    if !border.is_renderable() {
110        return;
111    }
112    let (Contents::Dem(tile), Contents::Dem(border)) = (tile, border) else {
113        return;
114    };
115    tile.backfill(border, offset);
116    border.backfill(tile, offset.opposite());
117}