-
Notifications
You must be signed in to change notification settings - Fork 482
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
Fix for edge cases with bbox overlap and out-of-bounds polygons #817
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -50,6 +50,11 @@ static LatLng invalidVerts[] = {{INFINITY, INFINITY}, {-INFINITY, -INFINITY}}; | |||||
static GeoLoop invalidGeoLoop = {.numVerts = 2, .verts = invalidVerts}; | ||||||
static GeoPolygon invalidGeoPolygon; | ||||||
|
||||||
static LatLng outOfBoundsVert[] = {{-2000, -2000}}; | ||||||
static GeoLoop outOfBoundsVertGeoLoop = {.numVerts = 1, | ||||||
.verts = outOfBoundsVert}; | ||||||
static GeoPolygon outOfBoundsVertGeoPolygon; | ||||||
|
||||||
static LatLng invalid2Verts[] = {{NAN, NAN}, {-NAN, -NAN}}; | ||||||
static GeoLoop invalid2GeoLoop = {.numVerts = 2, .verts = invalid2Verts}; | ||||||
static GeoPolygon invalid2GeoPolygon; | ||||||
|
@@ -173,6 +178,9 @@ SUITE(polygonToCells) { | |||||
invalid2GeoPolygon.geoloop = invalid2GeoLoop; | ||||||
invalid2GeoPolygon.numHoles = 0; | ||||||
|
||||||
outOfBoundsVertGeoPolygon.geoloop = outOfBoundsVertGeoLoop; | ||||||
outOfBoundsVertGeoPolygon.numHoles = 0; | ||||||
|
||||||
nullGeoPolygon.geoloop = nullGeoLoop; | ||||||
nullGeoPolygon.numHoles = 0; | ||||||
|
||||||
|
@@ -226,6 +234,21 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCells_OverlappingBBox) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nitpicking |
||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&sfGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, &numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&sfGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 1416, | ||||||
"got expected polygonToCells size (overlapping bbox mode)"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsHole) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
|
@@ -272,6 +295,22 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsHoleOverlappingBBox) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&holeGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, &numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&holeGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert( | ||||||
actualNumIndexes == 1403, | ||||||
"got expected polygonToCells size (hole, overlapping bbox mode)"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsHoleParentIssue) { | ||||||
// This checks a specific issue where the bounding box of the parent | ||||||
// cell fully contains the hole. | ||||||
|
@@ -398,6 +437,22 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsContainsPolygon_OverlappingBBox) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nitpicking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We weren't consistent here - normalized all to |
||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&sfGeoPolygon, 4, CONTAINMENT_OVERLAPPING_BBOX, &numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&sfGeoPolygon, 4, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 5, | ||||||
"got expected polygonToCells size (overlapping bbox mode)"); | ||||||
t_assert(hexagons[0] == 0x8428309ffffffff, "got expected hexagon"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsExact) { | ||||||
LatLng somewhere = {1, 2}; | ||||||
H3Index origin; | ||||||
|
@@ -440,6 +495,15 @@ SUITE(polygonToCells) { | |||||
// TODO: CONTAINMENT_OVERLAPPING yields 7 cells, presumably due to FPE | ||||||
// in the various cell boundaries | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&someHexagon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
// Overlapping BBox is very rough, so we get a couple of overlaps from | ||||||
// non-neighboring cells | ||||||
t_assert(actualNumIndexes == 9, | ||||||
"got expected polygonToCells size for overlapping bbox " | ||||||
"containment"); | ||||||
|
||||||
free(hexagons); | ||||||
free(verts); | ||||||
} | ||||||
|
@@ -643,48 +707,104 @@ SUITE(polygonToCells) { | |||||
} | ||||||
|
||||||
TEST(polygonToCellsPointPolygon) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_CENTER, &numHexagons)); | ||||||
t_assert(numHexagons == 1, "got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_CENTER, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 0, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
for (int res = 0; res < MAX_H3_RES; res++) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_CENTER, &numHexagons)); | ||||||
t_assert(numHexagons >= 1 && numHexagons <= 5, | ||||||
"got expected estimated size"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated these assertions to expect a reasonable range rather than a specific value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 5 the actual max that could be contained? I guess it doesn't really matter as long as it's equal or above the actual number that could be contained. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the max I saw in tests was 4, but 5 seemed reasonable 🤷 |
||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_CENTER, hexagons)); | ||||||
int64_t actualNumIndexes = | ||||||
countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 0, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsPointPolygon_full) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_FULL, &numHexagons)); | ||||||
t_assert(numHexagons == 1, "got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
for (int res = 0; res < MAX_H3_RES; res++) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_FULL, &numHexagons)); | ||||||
t_assert(numHexagons >= 1 && numHexagons <= 5, | ||||||
"got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_FULL, hexagons)); | ||||||
int64_t actualNumIndexes = | ||||||
countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 0, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
} | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_FULL, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
TEST(polygonToCellsPointPolygon_overlapping) { | ||||||
for (int res = 0; res < MAX_H3_RES; res++) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_OVERLAPPING, &numHexagons)); | ||||||
t_assert(numHexagons >= 1 && numHexagons <= 5, | ||||||
"got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_OVERLAPPING, hexagons)); | ||||||
int64_t actualNumIndexes = | ||||||
countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 1, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
} | ||||||
|
||||||
t_assert(actualNumIndexes == 0, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
TEST(polygonToCellsPointPolygon_overlappingBBox) { | ||||||
for (int res = 0; res < MAX_H3_RES; res++) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_OVERLAPPING_BBOX, | ||||||
&numHexagons)); | ||||||
t_assert(numHexagons >= 1 && numHexagons <= 5, | ||||||
"got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, res, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = | ||||||
countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes >= 1 && actualNumIndexes <= 5, | ||||||
"got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsPointPolygon_overlapping) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_OVERLAPPING, &numHexagons)); | ||||||
t_assert(numHexagons == 1, "got expected estimated size"); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
TEST(polygonToCellsOutOfBoundsPolygon) { | ||||||
for (int res = 0; res < MAX_H3_RES; res++) { | ||||||
for (uint32_t flags = 0; flags < CONTAINMENT_INVALID; flags++) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&outOfBoundsVertGeoPolygon, res, flags, &numHexagons)); | ||||||
t_assert(numHexagons == 0, "got expected estimated size"); | ||||||
// Note: We're allocating more memory than the estimate to test | ||||||
// for out-of-bounds writes here | ||||||
H3Index *hexagons = calloc(10, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointGeoPolygon, 9, CONTAINMENT_OVERLAPPING, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&outOfBoundsVertGeoPolygon, res, flags, hexagons)); | ||||||
int64_t actualNumIndexes = | ||||||
countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 1, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
t_assert(actualNumIndexes == 0, | ||||||
"got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsLinePolygon) { | ||||||
|
@@ -729,6 +849,20 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsLinePolygon_overlappingBBox) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&lineGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, &numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&lineGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
t_assert(actualNumIndexes == 21, "got expected polygonToCells size"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsNullHole) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
|
@@ -777,6 +911,23 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsNullHole_overlappingBBox) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&nullHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, | ||||||
&numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&nullHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
// Same as without the hole | ||||||
t_assert(actualNumIndexes == 1416, | ||||||
"got expected polygonToCells size (null hole)"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsPointHole) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
|
@@ -825,6 +976,23 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsPointHole_overlappingBBox) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&pointHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, | ||||||
&numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&pointHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
// Same as without the hole | ||||||
t_assert(actualNumIndexes == 1416, | ||||||
"got expected polygonToCells size (point hole)"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsLineHole) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
|
@@ -873,6 +1041,23 @@ SUITE(polygonToCells) { | |||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(polygonToCellsLineHole_overlappingBBox) { | ||||||
int64_t numHexagons; | ||||||
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSizeExperimental)( | ||||||
&lineHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, | ||||||
&numHexagons)); | ||||||
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index)); | ||||||
|
||||||
t_assertSuccess(H3_EXPORT(polygonToCellsExperimental)( | ||||||
&lineHoleGeoPolygon, 9, CONTAINMENT_OVERLAPPING_BBOX, hexagons)); | ||||||
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons); | ||||||
|
||||||
// Same as without the hole | ||||||
t_assert(actualNumIndexes == 1416, | ||||||
"got expected polygonToCells size (line hole)"); | ||||||
free(hexagons); | ||||||
} | ||||||
|
||||||
TEST(invalidFlags) { | ||||||
int64_t numHexagons; | ||||||
for (uint32_t flags = CONTAINMENT_INVALID; flags <= 32; flags++) { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test covered a block that AFAIK is now unreachable.