const CELL_SIZE_DMD: i64 = 1_000_000;
const LON_CELLS: usize = 3600;
const LAT_CELLS: usize = 1800;
const NUM_CELLS: usize = LON_CELLS * LAT_CELLS;
const LON_MAX_CELL: i64 = 3599;
const LAT_MAX_CELL: i64 = 1799;
const LON_OFFSET_DMD: i64 = 1_800_000_000;
const LAT_OFFSET_DMD: i64 = 900_000_000;
const GRID_MAX_INDEX_BYTES: u64 = 256 * 1024 * 1024;
const GRID_MAX_PAIRS: u64 = GRID_MAX_INDEX_BYTES / 4;
pub(crate) struct RegionGrid {
cell_starts: Vec<u32>,
region_indices: Vec<u32>,
}
#[inline]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn cell_lon(lon: i32) -> usize {
let raw = (i64::from(lon) + LON_OFFSET_DMD) / CELL_SIZE_DMD;
raw.clamp(0, LON_MAX_CELL) as usize
}
#[inline]
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn cell_lat(lat: i32) -> usize {
let raw = (i64::from(lat) + LAT_OFFSET_DMD) / CELL_SIZE_DMD;
raw.clamp(0, LAT_MAX_CELL) as usize
}
#[inline]
fn cell_of(lat: i32, lon: i32) -> usize {
cell_lat(lat) * LON_CELLS + cell_lon(lon)
}
#[inline]
fn coverage_is_within_budget(total_pairs: u64) -> bool {
total_pairs <= GRID_MAX_PAIRS && total_pairs <= u64::from(u32::MAX)
}
#[cfg(feature = "test-hooks")]
pub mod test_hooks {
use std::sync::atomic::{AtomicBool, Ordering};
pub static FORCE_LINEAR: AtomicBool = AtomicBool::new(false);
pub fn reset() {
FORCE_LINEAR.store(false, Ordering::Relaxed);
}
}
#[cfg(feature = "test-hooks")]
#[inline]
fn force_linear() -> bool {
test_hooks::FORCE_LINEAR.load(std::sync::atomic::Ordering::Relaxed)
}
#[cfg(not(feature = "test-hooks"))]
#[inline]
fn force_linear() -> bool {
false
}
impl RegionGrid {
pub(crate) fn build(region_bboxes: &[(i32, i32, i32, i32)]) -> Option<Self> {
if force_linear() {
return None;
}
let mut total_pairs = 0_u64;
for &(min_lat, max_lat, min_lon, max_lon) in region_bboxes {
let min_col = cell_lon(min_lon);
let max_col = cell_lon(max_lon);
let min_row = cell_lat(min_lat);
let max_row = cell_lat(max_lat);
if min_col > max_col || min_row > max_row {
return None;
}
let width = (max_col - min_col + 1) as u64;
let height = (max_row - min_row + 1) as u64;
total_pairs = total_pairs.checked_add(width * height)?;
if !coverage_is_within_budget(total_pairs) {
return None;
}
}
let mut cell_starts = vec![0_u32; NUM_CELLS + 1];
for &(min_lat, max_lat, min_lon, max_lon) in region_bboxes {
for row in cell_lat(min_lat)..=cell_lat(max_lat) {
let row_start = row * LON_CELLS;
for col in cell_lon(min_lon)..=cell_lon(max_lon) {
cell_starts[row_start + col + 1] += 1;
}
}
}
for cell in 1..=NUM_CELLS {
cell_starts[cell] += cell_starts[cell - 1];
}
let mut region_indices = vec![0_u32; usize::try_from(total_pairs).ok()?];
let mut cursor = cell_starts.clone();
for (region, &(min_lat, max_lat, min_lon, max_lon)) in region_bboxes.iter().enumerate() {
let region = u32::try_from(region).ok()?;
for row in cell_lat(min_lat)..=cell_lat(max_lat) {
let row_start = row * LON_CELLS;
for col in cell_lon(min_lon)..=cell_lon(max_lon) {
let cell = row_start + col;
let offset = cursor[cell] as usize;
region_indices[offset] = region;
cursor[cell] += 1;
}
}
}
Some(Self {
cell_starts,
region_indices,
})
}
#[inline]
pub(crate) fn candidates(&self, lat: i32, lon: i32) -> &[u32] {
let cell = cell_of(lat, lon);
let start = self.cell_starts[cell] as usize;
let end = self.cell_starts[cell + 1] as usize;
&self.region_indices[start..end]
}
}
#[cfg(test)]
mod tests {
use super::*;
fn xs(state: &mut u64) -> u64 {
*state ^= *state << 13;
*state ^= *state >> 7;
*state ^= *state << 17;
*state
}
#[test]
fn cell_index_math() {
assert_eq!(cell_of(0, 0), 3_241_800);
assert_eq!(cell_of(123_456, 654_321), 3_241_800);
assert_eq!(cell_of(-900_000_000, -1_800_000_000), 0);
assert_eq!(cell_of(900_000_000, 1_800_000_000), 6_479_999);
assert_eq!(cell_of(-899_000_000, -1_799_000_000), 3_601);
assert_eq!(cell_lon(500_000_000), 2300);
assert_eq!(cell_lon(499_999_999), 2299);
}
#[test]
fn region_max_lon_boundary_is_a_candidate() {
let region = (10_000_000, 30_000_000, 100_000_000, 500_000_000);
let grid = RegionGrid::build(&[region]).expect("single region within budget");
assert!(grid.candidates(20_000_000, 500_000_000).contains(&0));
}
#[test]
fn out_of_domain_clamps() {
assert_eq!(cell_lon(2_000_000_000), LON_CELLS - 1);
assert_eq!(cell_lat(1_000_000_000), LAT_CELLS - 1);
assert_eq!(cell_lon(-2_000_000_000), 0);
assert_eq!(cell_lat(-1_000_000_000), 0);
}
#[test]
fn rasterize_exact_cell_set() {
let bbox = (10_000_000, 30_000_000, -20_000_000, 40_000_000);
let grid = RegionGrid::build(&[bbox]).expect("small grid coverage");
for row in 0..LAT_CELLS {
for col in 0..LON_CELLS {
let expected = (cell_lat(bbox.0)..=cell_lat(bbox.1)).contains(&row)
&& (cell_lon(bbox.2)..=cell_lon(bbox.3)).contains(&col);
let found = grid.cell_starts[row * LON_CELLS + col]
!= grid.cell_starts[row * LON_CELLS + col + 1];
assert_eq!(found, expected);
}
}
}
#[test]
fn rasterize_full_width_antimeridian() {
let grid = RegionGrid::build(&[(0, 0, -1_800_000_000, 1_800_000_000)])
.expect("one row is within budget");
let row = cell_lat(0);
for col in 0..LON_CELLS {
assert_eq!(
grid.region_indices[grid.cell_starts[row * LON_CELLS + col] as usize],
0
);
}
assert!(grid.candidates(100_000_000, 0).is_empty());
}
#[test]
fn coverage_budget_falls_back() {
let world = (-900_000_000, 900_000_000, -1_800_000_000, 1_800_000_000);
assert!(RegionGrid::build(&[world; 16]).is_none());
let small = (10_000_000, 20_000_000, 10_000_000, 20_000_000);
assert!(RegionGrid::build(&[small; 16]).is_some());
assert!(coverage_is_within_budget(16 * 121));
assert!(coverage_is_within_budget(GRID_MAX_PAIRS));
assert!(!coverage_is_within_budget(GRID_MAX_PAIRS + 1));
assert!(!coverage_is_within_budget(u64::from(u32::MAX) + 1));
}
#[allow(
clippy::cast_sign_loss,
clippy::cast_possible_wrap,
clippy::cast_possible_truncation
)]
fn rand_range(state: &mut u64, lo: i64, hi: i64) -> i32 {
let span = (hi - lo + 1) as u64;
(lo + (xs(state) % span) as i64) as i32
}
#[allow(clippy::cast_possible_truncation)]
fn fixture_regions(seed: u64) -> Vec<(i32, i32, i32, i32)> {
let mut s = seed;
let mut r = Vec::with_capacity(24);
for _ in 0..6 {
let min_lon = rand_range(&mut s, -1_790_000_000, 1_780_000_000);
let min_lat = rand_range(&mut s, -890_000_000, 880_000_000);
r.push((min_lat, min_lat + 10_000_000, min_lon, min_lon + 10_000_000));
}
for _ in 0..6 {
let min_lon = rand_range(&mut s, -1_800_000_000, 1_500_000_000);
let min_lat = rand_range(&mut s, -900_000_000, 600_000_000);
r.push((
min_lat,
min_lat + 300_000_000,
min_lon,
min_lon + 300_000_000,
));
}
for k in 0..6_i64 {
let w = 600_000_000_i64;
let min_lon = -1_800_000_000 + k * w;
r.push((
-400_000_000,
400_000_000,
min_lon as i32,
(min_lon + w - 1) as i32,
));
}
for k in 0..3_i64 {
let c = 100_000_000 + k * 20_000_000;
r.push((
(c - 150_000_000) as i32,
(c + 150_000_000) as i32,
(c - 150_000_000) as i32,
(c + 150_000_000) as i32,
));
}
r.push((800_000_000, 900_000_000, -200_000_000, 200_000_000));
r.push((100_000_000, 120_000_000, -1_800_000_000, 1_800_000_000));
r.push((-100_000_000, 100_000_000, 1_700_000_000, 2_000_000_000));
r
}
#[allow(clippy::cast_possible_truncation)]
#[test]
fn superset_matches_linear() {
for &seed in &[1_u64, 2, 3, 5, 8, 13, 21, 34] {
let bboxes = fixture_regions(seed);
let grid = RegionGrid::build(&bboxes).expect("fixture is within budget");
let mut ps = seed ^ 0x1234_5678_9ABC_DEF0;
for _ in 0..5_000 {
let lat = rand_range(&mut ps, -1_000_000_000, 1_000_000_000);
let lon = rand_range(&mut ps, -2_000_000_000, 2_000_000_000);
let linear: Vec<u32> = bboxes
.iter()
.enumerate()
.filter_map(|(i, b)| {
(lat >= b.0 && lat <= b.1 && lon >= b.2 && lon <= b.3).then_some(i as u32)
})
.collect();
let candidates = grid.candidates(lat, lon);
let pruned: Vec<u32> = candidates
.iter()
.copied()
.filter(|&i| {
let b = bboxes[i as usize];
lat >= b.0 && lat <= b.1 && lon >= b.2 && lon <= b.3
})
.collect();
assert_eq!(pruned, linear, "seed {seed} point ({lat},{lon})");
for i in &linear {
assert!(
candidates.contains(i),
"seed {seed} point ({lat},{lon}) missing candidate {i}"
);
}
}
}
}
}