Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions a5/traversal/global_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,69 @@ def _add_delta_neighbors(
_add_neighbor(ctx, neighbor_triple, orientation, neighbor_origin, neighbor_segment)


def _serialize_res1(origin: Origin, quintant: int) -> int:
"""Serialize a res 1 cell from origin and quintant."""
segment, _ = quintant_to_segment(quintant, origin)
return serialize({'origin': origin, 'segment': segment, 'S': 0, 'resolution': 1})


def _get_res0_neighbors(origin: Origin) -> List[int]:
"""
Get neighbors of a resolution 0 cell (dodecahedron face).
"""
neighbor_set: Set[int] = set()
for q in range(5):
adjacent_face_id, _ = FACE_ADJACENCY[origin.id][q]
neighbor_set.add(serialize({
'origin': origins[adjacent_face_id], 'segment': 0,
'S': 0, 'resolution': 0
}))
return sorted(neighbor_set)


def _get_res1_neighbors(origin: Origin, segment: int, edge_only: bool) -> List[int]:
"""
Get neighbors of a resolution 1 cell (quintant).
"""
quintant, _ = segment_to_quintant(segment, origin)
neighbor_set: Set[int] = set()

# Left and right quintant on the same face (A, B)
left_q = (quintant - 1 + 5) % 5
right_q = (quintant + 1) % 5
neighbor_set.add(_serialize_res1(origin, left_q))
neighbor_set.add(_serialize_res1(origin, right_q))

# Adjacent quintant on adjacent face (C)
adjacent_face_id, adjacent_quintant = FACE_ADJACENCY[origin.id][quintant]
adjacent_origin = origins[adjacent_face_id]
neighbor_set.add(_serialize_res1(adjacent_origin, adjacent_quintant))

if edge_only:
return sorted(neighbor_set)

# Remaining neighbors on face
neighbor_set.add(_serialize_res1(origin, (quintant - 2 + 5) % 5))
neighbor_set.add(_serialize_res1(origin, (quintant + 2) % 5))

# Left & right quintant neighbors of C
neighbor_set.add(_serialize_res1(adjacent_origin, (adjacent_quintant - 1 + 5) % 5))
neighbor_set.add(_serialize_res1(adjacent_origin, (adjacent_quintant + 1) % 5))

# Two neighbors each from adjacent faces of A & B
left_adjacent_face_id, left_adjacent_quintant = FACE_ADJACENCY[origin.id][left_q]
left_adjacent_origin = origins[left_adjacent_face_id]
neighbor_set.add(_serialize_res1(left_adjacent_origin, left_adjacent_quintant))
neighbor_set.add(_serialize_res1(left_adjacent_origin, (left_adjacent_quintant - 1 + 5) % 5))

right_adjacent_face_id, right_adjacent_quintant = FACE_ADJACENCY[origin.id][right_q]
right_adjacent_origin = origins[right_adjacent_face_id]
neighbor_set.add(_serialize_res1(right_adjacent_origin, right_adjacent_quintant))
neighbor_set.add(_serialize_res1(right_adjacent_origin, (right_adjacent_quintant + 1) % 5))

return sorted(neighbor_set)


def get_global_cell_neighbors(cell_id: int, edge_only: bool = False) -> List[int]:
"""
Get all neighbors of a cell across quintant and face boundaries.
Expand All @@ -101,8 +164,10 @@ def get_global_cell_neighbors(cell_id: int, edge_only: bool = False) -> List[int
"""
cell = deserialize(cell_id)
origin, segment, S, resolution = cell['origin'], cell['segment'], cell['S'], cell['resolution']
if resolution < FIRST_HILBERT_RESOLUTION:
return [] # No neighbors for res 0-1
if resolution == 0:
return _get_res0_neighbors(origin)
if resolution == 1:
return _get_res1_neighbors(origin, segment, edge_only)

hilbert_res = resolution - FIRST_HILBERT_RESOLUTION + 1
source_quintant, source_orientation = segment_to_quintant(segment, origin)
Expand Down
2 changes: 1 addition & 1 deletion tests/traversal/fixtures/cap.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
"estimateCellRadius": [
{
"resolution": 0,
"expectedMeters": 7356605.443858088
"expectedMeters": 7356605.443858089
},
{
"resolution": 1,
Expand Down
Loading
Loading