Skip to content

Commit

Permalink
added cellToChildPos and childPosToCell (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
akhenakh authored Mar 23, 2023
1 parent ff107fd commit a6e5063
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package h3
#include <h3_h3Index.h>
*/
import "C"

import (
"errors"
"fmt"
Expand Down Expand Up @@ -604,6 +605,38 @@ func UncompactCells(in []Cell, resolution int) []Cell {
return cellsFromC(cout, false, true)
}

// ChildPosToCell returns the child of cell a at a given position within an ordered list of all
// children at the specified resolution.
func ChildPosToCell(position int, a Cell, resolution int) Cell {
var out C.H3Index

C.childPosToCell(C.int64_t(position), C.H3Index(a), C.int(resolution), &out)

return Cell(out)
}

// ChildPosToCell returns the child cell at a given position within an ordered list of all
// children at the specified resolution.
func (c Cell) ChildPosToCell(position int, resolution int) Cell {
return ChildPosToCell(position, c, resolution)
}

// CellToChildPos returns the position of the cell a within an ordered list of all children of the cell's parent
// at the specified resolution.
func CellToChildPos(a Cell, resolution int) int {
var out C.int64_t

C.cellToChildPos(C.H3Index(a), C.int(resolution), &out)

return int(out)
}

// ChildPos returns the position of the cell within an ordered list of all children of the cell's parent
// at the specified resolution.
func (c Cell) ChildPos(resolution int) int {
return CellToChildPos(c, resolution)
}

func GridDistance(a, b Cell) int {
var out C.int64_t
C.gridDistance(C.H3Index(a), C.H3Index(b), &out)
Expand Down
19 changes: 19 additions & 0 deletions h3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ func TestUncompactCells(t *testing.T) {
assertCellIn(t, validCell, out)
}

func TestChildPosToCell(t *testing.T) {
t.Parallel()

children := validCell.Children(6)

assertEqual(t, children[0], validCell.ChildPosToCell(0, 6))
assertEqual(t, children[0], ChildPosToCell(0, validCell, 6))
}

func TestChildPos(t *testing.T) {
t.Parallel()

children := validCell.Children(7)

assertEqual(t, 32, children[32].ChildPos(validCell.Resolution()))
assertEqual(t, 32, CellToChildPos(children[32], validCell.Resolution()))
}

func TestIsResClassIII(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -767,6 +785,7 @@ func assertFalse(t *testing.T, b bool) {
t.Helper()
assertEqual(t, false, b)
}

func assertTrue(t *testing.T, b bool) {
t.Helper()
assertEqual(t, true, b)
Expand Down

0 comments on commit a6e5063

Please sign in to comment.