mln-rs-engine 0.0.23

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The stacks of draped layers: the runs of the frame's layers that drape,
//! each drawn into its own drapes and shown by its own surface draw.

use smallvec::SmallVec;

use super::SurfaceLight;

/// How a frame layer that drapes takes part in its stack.
#[derive(Clone, Copy, Debug, PartialEq)]
#[expect(clippy::large_enum_variant, reason = "one a frame layer, which the stacks take once; a box would allocate it")]
pub(crate) enum Draped {
    /// It draws its colors over the layers under it.
    Plain,
    /// It blends with the layers under it otherwise than by covering them
    /// (a layer blend other than `normal`), so they must be in its drapes.
    Blends,
    /// Its shading can come from the surface, with the frame's `light`: a
    /// hillshade lit from the viewport, whose drapes then hold its slopes,
    /// the same at every bearing.
    Lit(SurfaceLight),
}

/// A frame's stacks of draped layers: each a run of consecutive frame layers
/// that drape, ended by a frame layer that doesn't. A layer that the surface
/// can light ([`Draped::Lit`]) has a stack to itself, which the surface lights,
/// unless a layer above it in its run blends with what is under it, whose
/// drapes then hold its colors. Stacks are numbered bottom up from 0.
#[derive(Default)]
pub(crate) struct DrapeStacks {
    /// The draped layers' style indexes, stack after stack, each stack in
    /// drawing order.
    layers: SmallVec<[u32; 16]>,
    /// Where each stack's layers end in `layers`, and the light the surface
    /// shades the stack's drapes with, if it does.
    ends: SmallVec<[(usize, Option<SurfaceLight>); 4]>,
}

impl DrapeStacks {
    /// The stacks of `layers`, the frame's layers in drawing order, each
    /// with how it drapes, or `None` if it doesn't.
    pub(crate) fn of(layers: impl IntoIterator<Item = (u32, Option<Draped>)>) -> DrapeStacks {
        let mut stacks = DrapeStacks::default();
        let mut run: SmallVec<[(u32, Draped); 16]> = SmallVec::new();
        for (index, draped) in layers {
            match draped {
                Some(draped) => run.push((index, draped)),
                None => stacks.end_run(&mut run),
            }
        }
        stacks.end_run(&mut run);
        stacks
    }

    /// Adds the stacks of `run`, a run of draped layers, and empties it.
    fn end_run(&mut self, run: &mut SmallVec<[(u32, Draped); 16]>) {
        // The layers under the last that blends are in its drapes.
        let blended = run.iter().rposition(|(_, draped)| *draped == Draped::Blends).map_or(0, |last| last + 1);
        for (at, (index, draped)) in run.drain(..).enumerate() {
            match draped {
                Draped::Lit(light) if at >= blended => {
                    self.end_stack(None);
                    self.layers.push(index);
                    self.end_stack(Some(light));
                }
                _ => self.layers.push(index),
            }
        }
        self.end_stack(None);
    }

    /// Ends the stack in progress, if it has layers, which the surface
    /// shades with `light`.
    fn end_stack(&mut self, light: Option<SurfaceLight>) {
        if self.ends.last().map_or(0, |&(end, _)| end) < self.layers.len() {
            self.ends.push((self.layers.len(), light));
        }
    }

    /// The number of stacks.
    pub(crate) fn len(&self) -> usize {
        self.ends.len()
    }

    /// Whether there is no stack: no frame layer drapes.
    pub(crate) fn is_empty(&self) -> bool {
        self.ends.is_empty()
    }

    /// The style indexes of the layers of `stack`, in drawing order.
    pub(crate) fn layers(&self, stack: usize) -> &[u32] {
        let start = stack.checked_sub(1).map_or(0, |previous| self.ends[previous].0);
        &self.layers[start..self.ends[stack].0]
    }

    /// The light that the surface shades the drapes of `stack` with, whose
    /// one layer draws its slopes there; `None` for a stack whose drapes
    /// hold its layers' colors.
    pub(crate) fn light(&self, stack: usize) -> Option<&SurfaceLight> {
        self.ends[stack].1.as_ref()
    }

    /// The style index of each stack's first layer, where its surface draws,
    /// bottom up.
    pub(crate) fn starts(&self) -> impl Iterator<Item = u32> + '_ {
        (0..self.len()).map(|stack| self.layers(stack)[0])
    }
}

#[cfg(test)]
mod tests {
    use super::{DrapeStacks, Draped, SurfaceLight};

    const PLAIN: Option<Draped> = Some(Draped::Plain);
    const BLENDS: Option<Draped> = Some(Draped::Blends);
    const STANDS: Option<Draped> = None;

    /// The stacks of `layers`, each a list of style indexes, and whether the
    /// surface lights each.
    fn stacks_of(layers: &[(u32, Option<Draped>)]) -> (Vec<Vec<u32>>, Vec<bool>) {
        let stacks = DrapeStacks::of(layers.iter().copied());
        assert_eq!(layers.iter().any(|&(_, draped)| draped.is_some()), !stacks.is_empty());
        let split: Vec<Vec<u32>> = (0..stacks.len()).map(|stack| stacks.layers(stack).to_vec()).collect();
        let starts: Vec<u32> = stacks.starts().collect();
        assert_eq!(split.iter().map(|layers| layers[0]).collect::<Vec<_>>(), starts);
        let lit = (0..stacks.len()).map(|stack| stacks.light(stack).is_some()).collect();
        (split, lit)
    }

    /// Consecutive layers that drape are one stack, which a layer that
    /// stands splits; layers that stand at either end start or end none. The
    /// frame's layers skip those it doesn't draw, which split no stack.
    #[test]
    fn layers_that_stand_split_the_draped_layers_into_stacks() {
        type Case = (&'static [(u32, bool)], &'static [&'static [u32]]);
        let cases: [Case; 8] = [
            (&[(0, true), (1, true), (2, true)], &[&[0, 1, 2]]),
            (&[(0, true), (1, true), (2, false), (3, true)], &[&[0, 1], &[3]]),
            (&[(0, false), (1, true), (2, true)], &[&[1, 2]]),
            (&[(0, true), (1, true), (2, false), (3, false)], &[&[0, 1]]),
            (&[(0, true), (1, false), (2, false), (3, true)], &[&[0], &[3]]),
            (&[(2, true), (5, true)], &[&[2, 5]]),
            (&[(0, false), (1, false)], &[]),
            (&[], &[]),
        ];
        for (layers, expected) in cases {
            let layers: Vec<_> = layers.iter().map(|&(index, drapes)| (index, PLAIN.filter(|_| drapes))).collect();
            let (split, lit) = stacks_of(&layers);
            assert_eq!(expected, split, "{layers:?}");
            assert!(lit.iter().all(|lit| !lit), "{layers:?}");
        }
    }

    /// A layer that the surface lights has a stack to itself, between the
    /// layers under and over it, but a layer that blends over a lit one
    /// blends over its colors, in one stack with it, which the surface
    /// doesn't light. A blend under a lit layer, or past a layer that
    /// stands, doesn't keep it in its stack.
    #[test]
    fn a_lit_layer_stands_alone_but_for_a_blend_over_it() {
        type Case = (Vec<(u32, Option<Draped>)>, &'static [&'static [u32]], &'static [bool]);
        let lit = || Some(Draped::Lit(SurfaceLight::default()));
        let cases: [Case; 4] = [
            (vec![(0, PLAIN), (1, lit()), (2, PLAIN), (3, PLAIN)], &[&[0], &[1], &[2, 3]], &[false, true, false]),
            (vec![(0, lit()), (1, lit()), (2, STANDS), (3, lit())], &[&[0], &[1], &[3]], &[true, true, true]),
            (vec![(0, PLAIN), (1, lit()), (2, PLAIN), (3, BLENDS), (4, lit())], &[&[0, 1, 2, 3], &[4]], &[false, true]),
            (vec![(0, BLENDS), (1, lit()), (2, STANDS), (3, BLENDS)], &[&[0], &[1], &[3]], &[false, true, false]),
        ];
        for (layers, stacks, lit) in cases {
            assert_eq!((stacks.iter().map(|stack| stack.to_vec()).collect(), lit.to_vec()), stacks_of(&layers));
        }
    }
}