use crate::frame::FlatFrame;
use std::collections::HashMap;
pub fn running_average(frame_id: usize, num_scans: usize, window: &[&FlatFrame]) -> FlatFrame {
let n = window.len().max(1) as u64;
let mut acc: HashMap<u64, u64> = HashMap::new();
for f in window {
for i in 0..f.len() {
let key = ((f.scan[i] as u64) << 32) | f.tof[i] as u64;
*acc.entry(key).or_insert(0) += f.intensity[i] as u64;
}
}
let mut scan = Vec::with_capacity(acc.len());
let mut tof = Vec::with_capacity(acc.len());
let mut intensity = Vec::with_capacity(acc.len());
for (key, sum) in acc {
let avg = ((sum + n / 2) / n).min(u32::MAX as u64) as u32;
if avg == 0 {
continue;
}
scan.push((key >> 32) as u32);
tof.push((key & 0xFFFF_FFFF) as u32);
intensity.push(avg);
}
FlatFrame {
frame_id,
num_scans,
scan,
tof,
intensity,
}
}