Skip to content

Commit

Permalink
Simplify Elevation type
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Nov 24, 2023
1 parent 9f78d55 commit c3fcd17
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 49 deletions.
13 changes: 6 additions & 7 deletions hexit/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ impl Combine {
for _sample_n in 0..n_samples {
let raw_cell = rdr.read_u64::<LE>()?;
let cell = hextree::Cell::from_raw(raw_cell)?;
let elevation = rdr.read_i16::<LE>()?;
hextree.insert(cell, Elevation::Plain(elevation));
let raw_elevation = rdr.read_i16::<LE>()?;
let elevation = Elevation::new(raw_elevation);
hextree.insert(cell, elevation);
pb.inc(1);
}
assert!(
Expand All @@ -67,11 +68,9 @@ impl Combine {
7_usize.pow(self.source_resolution as u32 - self.target_resolution as u32);
for (cell, elev) in hextree.iter() {
match elev {
Elevation::Intermediate(intermediate)
if cell.res() == self.target_resolution as u8 =>
{
assert_eq!(intermediate.n, max_child_cnt);
let reduction = intermediate.reduce();
elevation if cell.res() == self.target_resolution as u8 => {
assert_eq!(elevation.n, max_child_cnt);
let reduction = elevation.reduce();
reduced_hextree.insert(cell, reduction);
}
_ => {}
Expand Down
68 changes: 26 additions & 42 deletions hexit/src/elevation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,49 @@ impl ReducedElevation {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IntermediateElevation {
pub struct Elevation {
pub min: i16,
pub sum: i32,
pub max: i16,
pub n: usize,
}

impl IntermediateElevation {
impl Elevation {
pub fn new(raw: i16) -> Elevation {
Elevation {
min: raw,
sum: i32::from(raw),
max: raw,
n: 1,
}
}

pub fn reduce(&self) -> ReducedElevation {
let min = self.min;
let avg = i16::try_from(self.sum / i32::try_from(self.n).unwrap()).unwrap();
let max = self.max;
assert_ne!(min, i16::MIN);
assert_ne!(min, i16::MAX);
assert_ne!(max, i16::MIN);
assert_ne!(max, i16::MAX);
assert!(min <= avg && avg <= max);
ReducedElevation { min, avg, max }
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Elevation {
Plain(i16),
Intermediate(IntermediateElevation),
}

impl Elevation {
pub fn concat(source_resolution: u8, this_resolution: u8, items: &[Self]) -> Self {
let mut new_min = i16::MAX;
let mut new_sum: i32 = 0;
let mut new_max = i16::MIN;
let mut new_n = 0_usize;
pub fn concat(items: &[Self]) -> Self {
let mut min = i16::MAX;
let mut sum: i32 = 0;
let mut max = i16::MIN;
let mut n = 0_usize;
for item in items {
match item {
Elevation::Plain(elev) => {
let n = 7_usize.pow(u32::from(source_resolution - this_resolution - 1));
assert_ne!(n, 0);
let sum = i32::from(*elev) * i32::try_from(n).unwrap();
new_sum += sum;
new_min = i16::min(new_min, *elev);
new_max = i16::max(new_max, *elev);
new_n += n;
}

Elevation::Intermediate(IntermediateElevation { min, sum, max, n }) => {
new_sum += *sum;
new_min = i16::min(new_min, *min);
new_max = i16::max(new_max, *max);
new_n += n;
}
}
sum += item.sum;
min = i16::min(min, item.min);
max = i16::max(max, item.max);
n += item.n;
}
Elevation::Intermediate(IntermediateElevation {
min: new_min,
sum: new_sum,
max: new_max,
n: new_n,
})
Elevation { min, sum, max, n }
}
}

Expand All @@ -93,11 +81,7 @@ impl Compactor<Elevation> for ReductionCompactor {
} else if let [Some(v0), Some(v1), Some(v2), Some(v3), Some(v4), Some(v5), Some(v6)] =
children
{
Some(Elevation::concat(
self.source_resolution,
cell.res(),
&[*v0, *v1, *v2, *v3, *v4, *v5, *v6],
))
Some(Elevation::concat(&[*v0, *v1, *v2, *v3, *v4, *v5, *v6]))
} else {
None
}
Expand Down

0 comments on commit c3fcd17

Please sign in to comment.