Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added full support for vertex mode #71

Merged
merged 11 commits into from
Aug 26, 2024
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ func ExampleLatLngToCell() {
| `directedEdgeToCells` | `DirectedEdge#Cells` |
| `originToDirectedEdges` | `Cell#DirectedEdges` |
| `directedEdgeToBoundary` | `DirectedEdge#Boundary` |
| `cellToVertex` | TODO |
| `cellToVertexes` | TODO |
| `vertexToLatLng` | TODO |
| `isValidVertex` | TODO |
| `cellToVertex` | `CellToVertex` |
| `cellToVertexes` | `cellToVertexes` |
| `vertexToLatLng` | `VertexToLatLng` |
| `isValidVertex` | `IsValidVertex` |
| `gridDistance` | `GridDistance`, `Cell#GridDistance` |
| `gridPathCells` | `GridPath`, `Cell#GridPath` |
| `cellToLocalIj` | `CellToLocalIJ` |
Expand Down
30 changes: 28 additions & 2 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ const (
base16 = 16
bitSize = 64

numCellEdges = 6
numEdgeCells = 2
numCellEdges = 6
numEdgeCells = 2
numCellVertexes = 6

DegsToRads = math.Pi / 180.0
RadsToDegs = 180.0 / math.Pi
Expand Down Expand Up @@ -713,6 +714,31 @@ func LocalIJToCell(origin Cell, ij CoordIJ) Cell {
return Cell(out)
}

func CellToVertex(c Cell, vertexNum int) Cell {
var out C.H3Index
C.cellToVertex(C.H3Index(c), C.int(vertexNum), &out)

return Cell(out)
}

func CellToVertexes(c Cell) []Cell {
out := make([]C.H3Index, numCellVertexes)
C.cellToVertexes(C.H3Index(c), &out[0])

return cellsFromC(out, true, false)
}

func VertexToLatLng(vertex Cell) LatLng {
var out C.LatLng
C.vertexToLatLng(C.H3Index(vertex), &out)

return latLngFromC(out)
}

func IsValidVertex(c Cell) bool {
return C.isValidVertex(C.H3Index(c)) == 1
}

func maxGridDiskSize(k int) int {
return 3*k*(k+1) + 1
}
Expand Down
78 changes: 78 additions & 0 deletions h3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ func TestPentagons(t *testing.T) {
t.Parallel()

for _, res := range []int{0, 8, 15} {
res := res
jogly marked this conversation as resolved.
Show resolved Hide resolved
t.Run(fmt.Sprintf("res=%d", res), func(t *testing.T) {
t.Parallel()
pentagons := Pentagons(res)
Expand All @@ -621,6 +622,83 @@ func TestPentagons(t *testing.T) {
}
}

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

testCases := []struct {
cell Cell
expectedVertex Cell
vertexNum int
}{
{cell: validCell, expectedVertex: 0x2050dab63fffffff, vertexNum: 0},
{cell: validCell, expectedVertex: 0, vertexNum: 6}, // vertex num should be between 0 and 5 for hexagonal cells.
}

for i, tc := range testCases {
tc := tc

t.Run(fmt.Sprint(i), func(t *testing.T) {
t.Parallel()

vertex := CellToVertex(tc.cell, tc.vertexNum)
assertEqual(t, tc.expectedVertex, vertex)
})
}
}

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

testCases := []struct {
cell Cell
numVertexes int
}{
{cell: validCell, numVertexes: 6},
{cell: pentagonCell, numVertexes: 5},
{cell: -1, numVertexes: 0}, // Invalid cel.
}

for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) {
t.Parallel()

vertexes := CellToVertexes(tc.cell)
assertEqual(t, tc.numVertexes, len(vertexes))
})
}
}

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

testCases := []struct {
vertex Cell
expectedLatLng LatLng
}{
{vertex: CellToVertex(validCell, 0), expectedLatLng: LatLng{Lat: 67.22475, Lng: -168.52301}},
{vertex: -1, expectedLatLng: LatLng{}}, // Invalid vertex.
}

for i, tc := range testCases {
tc := tc

t.Run(fmt.Sprint(i), func(t *testing.T) {
t.Parallel()

latLng := VertexToLatLng(tc.vertex)
assertEqualLatLng(t, tc.expectedLatLng, latLng)
})
}
}

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

assertFalse(t, IsValidVertex(0))
assertTrue(t, IsValidVertex(2473183460575936511))
}

func equalEps(expected, actual float64) bool {
return math.Abs(expected-actual) < eps
}
Expand Down
Loading