I am using H3 spatial index implementaion h3o. I want to encode a local cell pattern to draw it around a given index.
The first problem is that I am inevitably going to fight pentagons, but I don't care about warping the pattern around them. So handling of pentagons is reduced to preventing panics.
The second problem is that as I understand it is impossible to fix the order in which cells are walked during calls of grid_disk_distances_safe
. Thus I can't encode the pattern by enumerating neighboring cells.
I think that there is no information in the index that can help me either. Index encodes the path from the base cell to desired one through layers and does not contain spatial information about neighbors' order.
How to encode the pattern of cells to reproduce it around a given index?
The solution and related discussion is here. The code for transition function is as follows.
fn transpose_pattern(src: CellIndex,
dst: CellIndex,
pattern: &[CellIndex]) -> Vec<CellIndex>
{
// Compute translation offset.
let src_coord = src.to_local_ij(src).expect("src coord");
let dst_coord = dst.to_local_ij(dst).expect("dst coord");
let i_offset = dst_coord.i() - src_coord.i();
let j_offset = dst_coord.j() - src_coord.j();
// Transpose the pattern from src to dst.
pattern
.iter()
.copied()
.map(|cell| {
// Compute the local IJ coordinate wrt original center cell.
let src_ij = cell.to_local_ij(src).expect("local IJ");
// Apply translation and re-anchor at destination center cell.
let dst_ij = LocalIJ::new_unchecked(dst, src_ij.i() + i_offset, src_ij.j() + j_offset);
// Convert back to cell index.
CellIndex::try_from(dst_ij).expect("dst cell")
})
.collect::<Vec<_>>()
}
All credit for the provided solution goes to grim7reaper.