Skip to content

Commit 065469a

Browse files
authored
feat: added full support for vertex mode (#71)
1 parent 0a46771 commit 065469a

3 files changed

Lines changed: 110 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ func ExampleLatLngToCell() {
123123
| `directedEdgeToCells` | `DirectedEdge#Cells` |
124124
| `originToDirectedEdges` | `Cell#DirectedEdges` |
125125
| `directedEdgeToBoundary` | `DirectedEdge#Boundary` |
126-
| `cellToVertex` | TODO |
127-
| `cellToVertexes` | TODO |
128-
| `vertexToLatLng` | TODO |
129-
| `isValidVertex` | TODO |
126+
| `cellToVertex` | `CellToVertex` |
127+
| `cellToVertexes` | `cellToVertexes` |
128+
| `vertexToLatLng` | `VertexToLatLng` |
129+
| `isValidVertex` | `IsValidVertex` |
130130
| `gridDistance` | `GridDistance`, `Cell#GridDistance` |
131131
| `gridPathCells` | `GridPath`, `Cell#GridPath` |
132132
| `cellToLocalIj` | `CellToLocalIJ` |

h3.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ const (
6060
base16 = 16
6161
bitSize = 64
6262

63-
numCellEdges = 6
64-
numEdgeCells = 2
63+
numCellEdges = 6
64+
numEdgeCells = 2
65+
numCellVertexes = 6
6566

6667
DegsToRads = math.Pi / 180.0
6768
RadsToDegs = 180.0 / math.Pi
@@ -713,6 +714,31 @@ func LocalIJToCell(origin Cell, ij CoordIJ) Cell {
713714
return Cell(out)
714715
}
715716

717+
func CellToVertex(c Cell, vertexNum int) Cell {
718+
var out C.H3Index
719+
C.cellToVertex(C.H3Index(c), C.int(vertexNum), &out)
720+
721+
return Cell(out)
722+
}
723+
724+
func CellToVertexes(c Cell) []Cell {
725+
out := make([]C.H3Index, numCellVertexes)
726+
C.cellToVertexes(C.H3Index(c), &out[0])
727+
728+
return cellsFromC(out, true, false)
729+
}
730+
731+
func VertexToLatLng(vertex Cell) LatLng {
732+
var out C.LatLng
733+
C.vertexToLatLng(C.H3Index(vertex), &out)
734+
735+
return latLngFromC(out)
736+
}
737+
738+
func IsValidVertex(c Cell) bool {
739+
return C.isValidVertex(C.H3Index(c)) == 1
740+
}
741+
716742
func maxGridDiskSize(k int) int {
717743
return 3*k*(k+1) + 1
718744
}

h3_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ func TestPentagons(t *testing.T) {
609609
t.Parallel()
610610

611611
for _, res := range []int{0, 8, 15} {
612+
res := res
612613
t.Run(fmt.Sprintf("res=%d", res), func(t *testing.T) {
613614
t.Parallel()
614615
pentagons := Pentagons(res)
@@ -621,6 +622,83 @@ func TestPentagons(t *testing.T) {
621622
}
622623
}
623624

625+
func TestCellToVertex(t *testing.T) {
626+
t.Parallel()
627+
628+
testCases := []struct {
629+
cell Cell
630+
expectedVertex Cell
631+
vertexNum int
632+
}{
633+
{cell: validCell, expectedVertex: 0x2050dab63fffffff, vertexNum: 0},
634+
{cell: validCell, expectedVertex: 0, vertexNum: 6}, // vertex num should be between 0 and 5 for hexagonal cells.
635+
}
636+
637+
for i, tc := range testCases {
638+
tc := tc
639+
640+
t.Run(fmt.Sprint(i), func(t *testing.T) {
641+
t.Parallel()
642+
643+
vertex := CellToVertex(tc.cell, tc.vertexNum)
644+
assertEqual(t, tc.expectedVertex, vertex)
645+
})
646+
}
647+
}
648+
649+
func TestCellToVertexes(t *testing.T) {
650+
t.Parallel()
651+
652+
testCases := []struct {
653+
cell Cell
654+
numVertexes int
655+
}{
656+
{cell: validCell, numVertexes: 6},
657+
{cell: pentagonCell, numVertexes: 5},
658+
{cell: -1, numVertexes: 0}, // Invalid cel.
659+
}
660+
661+
for _, tc := range testCases {
662+
tc := tc
663+
t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) {
664+
t.Parallel()
665+
666+
vertexes := CellToVertexes(tc.cell)
667+
assertEqual(t, tc.numVertexes, len(vertexes))
668+
})
669+
}
670+
}
671+
672+
func TestVertexToLatLng(t *testing.T) {
673+
t.Parallel()
674+
675+
testCases := []struct {
676+
vertex Cell
677+
expectedLatLng LatLng
678+
}{
679+
{vertex: CellToVertex(validCell, 0), expectedLatLng: LatLng{Lat: 67.22475, Lng: -168.52301}},
680+
{vertex: -1, expectedLatLng: LatLng{}}, // Invalid vertex.
681+
}
682+
683+
for i, tc := range testCases {
684+
tc := tc
685+
686+
t.Run(fmt.Sprint(i), func(t *testing.T) {
687+
t.Parallel()
688+
689+
latLng := VertexToLatLng(tc.vertex)
690+
assertEqualLatLng(t, tc.expectedLatLng, latLng)
691+
})
692+
}
693+
}
694+
695+
func TestIsValidVertex(t *testing.T) {
696+
t.Parallel()
697+
698+
assertFalse(t, IsValidVertex(0))
699+
assertTrue(t, IsValidVertex(2473183460575936511))
700+
}
701+
624702
func equalEps(expected, actual float64) bool {
625703
return math.Abs(expected-actual) < eps
626704
}

0 commit comments

Comments
 (0)