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