-
Notifications
You must be signed in to change notification settings - Fork 503
Fix bounding box bug for polygons crossing the antimeridian #130
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
Conversation
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.
Nice stuff. A few nits but nothing major.
@@ -88,9 +88,16 @@ TEST(transmeridian) { | |||
{-0.4, M_PI - 0.1}}; | |||
const Geofence geofence = {.numVerts = 4, .verts = verts}; | |||
const BBox expected = {0.4, -0.4, -M_PI + 0.1, M_PI - 0.1}; | |||
const GeoCoord inside = {-0.1, M_PI}; | |||
const GeoCoord insideOnMeridian = {-0.1, M_PI}; |
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.
IDK how you all feel about this idea, but I find it would be easier to read this code if it used designated initializers:
const GeoCoord insideOnMeridian = { .lat = -0.1, .lon = M_PI }
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.
Yeah, we've been really inconsistent on this in tests, but I think that's probably the best/most legible option for most structs. GeoCoord
might be the exception, because a) we instantiate a lot of them, and b) it's really equivalent to a lat, lon
tuple, so instantiating it with the same syntax as a double[]
array makes sense.
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.
I get that. Just a warning about designated initializers: it's not 100% portable, but should be okay on most platforms.
src/apps/testapps/testPolyfill.c
Outdated
static int countActualHexagons(H3Index* hexagons, int numHexagons) { | ||
int actualNumHexagons = 0; | ||
for (int i = 0; i < numHexagons; i++) { | ||
if (hexagons[i] != 0) { |
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.
I have been wondering where in the code we rely on zero-initialization. There are reasons to avoid zero-initialization if possible. Is this a case when the caller relies on the fact that hexagons
contains only valid hexagons or zeros?
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.
Yes. In algorithms where we return a list of hexagons and cannot say ahead of time how many we need, we require the caller to pass in a zero-filled array of the max possible size and then only fill H3 addresses for the real hexagons. We could theoretically fill in index order and then add a NULL as a stop signal afterward, but internally this isn't as efficient because we often need to use the array as a set, which we do via modulo arithmetic on the index, resulting in arbitrary index positions.
src/apps/testapps/testPolyfill.c
Outdated
{0.05, M_PI - 0.2}, {-0.1, M_PI - 0.00001}, | ||
{-0.1, -M_PI + 0.00001}, {-0.05, -M_PI + 0.2}}; | ||
|
||
Geofence geofence; |
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.
Why not Geofence geofence = { verts, 6 };
?
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.
Will update using designated initializers.
src/apps/testapps/testPolyfill.c
Outdated
|
||
GeoPolygon polygon; | ||
polygon.geofence = geofence; | ||
polygon.numHoles = 0; |
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.
Same question.
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.
ditto.
|
||
int actualNumHexagons = countActualHexagons(hexagons, numHexagons); | ||
|
||
t_assert(actualNumHexagons == 1204, |
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.
If we keep t_assert
as immediately exiting program, this is fine. Changing that to another behavior might result in a memory leak if we don't hit line 260.
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 would be true in a lot of places. I'm assuming the current behavior is fine in this test, otherwise we'd have the same problem all over. If t_assert
doesn't exit the program, I'd assume it doesn't exit the function either, so we'd still hit L260.
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.
Ya I have a feeling that's true. OK for now, assertions will remain fatal.
src/apps/testapps/testPolygon.c
Outdated
{0.05, M_PI - 0.2}, {-0.1, M_PI - 0.1}, | ||
{-0.1, -M_PI + 0.1}, {-0.05, -M_PI + 0.2}}; | ||
|
||
Geofence geofence; |
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.
Same question.
src/h3lib/include/polygonAlgos.h
Outdated
|
||
bbox->south = DBL_MAX; | ||
bbox->west = DBL_MAX; | ||
bbox->north = -1.0 * DBL_MAX; | ||
bbox->east = -1.0 * DBL_MAX; | ||
double minPosLon = DBL_MAX; | ||
double maxNegLon = -1.0 * DBL_MAX; |
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.
For any case of -1.0 * DBL_MAX
I would highly suggest changing to -DBL_MIN
-DBL_MAX
. I'm sure this is what happens in most compilers anyway, but unary minus avoids theoretical multiplication.
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.
Do you mean -DBL_MAX
? Will update.
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.
Ya my mistake. Thanks.
// Save the min positive and max negative longitude for | ||
// use in the transmeridian case | ||
if (lon > 0 && lon < minPosLon) minPosLon = lon; | ||
if (lon < 0 && lon > maxNegLon) maxNegLon = lon; |
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.
Nice.
33f9a11
to
71ca03e
Compare
Fix bounding box bug for polygons crossing the antimeridian
This fixes a previously unknown bug calculating the bounding box for most polygons crossing the antimeridian. Bounding boxes were using the max longitude as west and the min longitude as east, which only considers the vertices closest to the antimeridian, instead of using the min positive longitude as west and the max negative longitude as east (vertices furthest from the antimeridian).
My tests failed to catch this because my test polygons were all orthogonal rectangles 😞.