use crate::tiles::neighbourhood::RgbaField;
const NODATA_CEILING_METERS: f32 = -20000.0;
fn decode_terrarium(texel: [u8; 4]) -> f32 {
let [r, g, b, _a] = texel;
(f32::mul_add(f32::from(r), 256.0, f32::from(g)) + f32::from(b) / 256.0) + f32::from(i16::MIN)
}
#[derive(Debug, Clone, PartialEq)]
pub struct HeightGrid {
values: Vec<f32>,
width: usize,
height: usize,
}
impl HeightGrid {
#[must_use]
pub fn from_field(field: &RgbaField, margin: u8) -> Self {
let margin = usize::from(margin);
let tile_size = field.tile_size();
let side = tile_size + 2 * margin;
let start = tile_size - margin;
let mut values = Vec::with_capacity(side * side);
for y in 0..side {
for x in 0..side {
values.push(decode_terrarium(field.texel(start + x, start + y)));
}
}
Self {
values,
width: side,
height: side,
}
}
#[must_use]
pub fn from_values(values: Vec<f32>, width: usize, height: usize) -> Self {
assert_eq!(
values.len(),
width * height,
"a height grid must hold width * height samples"
);
Self {
values,
width,
height,
}
}
#[must_use]
pub fn values(&self) -> &[f32] {
&self.values
}
#[must_use]
pub const fn dimensions(&self) -> (usize, usize) {
(self.width, self.height)
}
#[must_use]
pub fn get_thresholds(&self, interval: f32) -> Vec<f32> {
let Some((min_elevation, max_elevation)) = self.min_max() else {
return vec![];
};
let start_threshold = ((min_elevation / interval).floor() + 1.0) * interval;
let mut thresholds = Vec::new();
let mut current_threshold = start_threshold;
while current_threshold <= max_elevation {
thresholds.push(current_threshold);
current_threshold += interval;
}
thresholds
}
fn min_max(&self) -> Option<(f32, f32)> {
let mut min = f32::MAX;
let mut max = f32::MIN;
let mut found_one = false;
for &value in &self.values {
if value.is_finite() && value > NODATA_CEILING_METERS {
min = min.min(value);
max = max.max(value);
found_one = true;
}
}
found_one.then_some((min, max))
}
}
#[cfg(test)]
mod tests {
use approx::assert_relative_eq;
use super::*;
use crate::tiles::neighbourhood::DEFAULT_TILE_SIZE;
fn grid(values: &[f32]) -> HeightGrid {
HeightGrid::from_values(values.to_vec(), values.len(), 1)
}
const FLOOR: f32 = -32768.0;
const NEAR_FLOOR: f32 = -32768.0 + 1.0 / 256.0;
#[test]
fn nodata_pixel_does_not_stretch_the_threshold_range() {
let poisoned = grid(&[FLOOR, 120.0, 350.0]);
assert_eq!(poisoned.get_thresholds(100.0), vec![200.0, 300.0]);
assert_eq!(
grid(&[120.0, 350.0]).get_thresholds(100.0),
poisoned.get_thresholds(100.0)
);
}
#[test]
fn near_floor_pixel_is_nodata_too() {
assert_eq!(
grid(&[NEAR_FLOOR, 120.0, 350.0]).get_thresholds(100.0),
vec![200.0, 300.0]
);
}
#[test]
fn deepest_real_elevation_is_kept() {
assert_eq!(
grid(&[-10935.0, 0.0]).get_thresholds(5000.0),
vec![-10000.0, -5000.0, 0.0]
);
}
#[test]
fn a_threshold_at_or_below_the_minimum_is_dropped() {
assert_eq!(
grid(&[120.0, 350.0]).get_thresholds(100.0),
vec![200.0, 300.0]
);
assert_eq!(grid(&[200.0, 350.0]).get_thresholds(100.0), vec![300.0]);
}
#[test]
fn all_nodata_grid_yields_no_thresholds() {
assert_eq!(
grid(&[FLOOR, NEAR_FLOOR]).get_thresholds(100.0),
[] as [f32; 0]
);
}
#[test]
fn blank_pixel_decodes_to_the_terrarium_floor() {
assert_relative_eq!(decode_terrarium([0, 0, 0, 0]), FLOOR);
}
#[test]
fn terrarium_decode_matches_the_reference_formula() {
assert_relative_eq!(decode_terrarium([128, 0, 0, 255]), 0.0);
assert_relative_eq!(decode_terrarium([128, 100, 128, 255]), 100.5);
}
#[test]
fn from_field_crops_to_the_centre_plus_margin() {
for tile_size in [DEFAULT_TILE_SIZE, 2 * DEFAULT_TILE_SIZE] {
let field = RgbaField::uniform([128, 0, 0, 255], tile_size);
for margin in [0u8, 1, 32] {
let grid = HeightGrid::from_field(&field, margin);
let expected = tile_size + 2 * usize::from(margin);
assert_eq!(
grid.dimensions(),
(expected, expected),
"tile size {tile_size}, margin {margin}"
);
assert_eq!(
grid.values().len(),
expected * expected,
"tile size {tile_size}, margin {margin}"
);
}
}
}
}