-
Notifications
You must be signed in to change notification settings - Fork 470
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
Add isValidVertex #420
Add isValidVertex #420
Changes from all 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 |
---|---|---|
|
@@ -192,7 +192,7 @@ H3Index H3_EXPORT(cellToVertex)(H3Index cell, int vertexNum) { | |
// Note that vertex - 1 is the right side, as vertex numbers are CCW | ||
Direction right = directionForVertexNum( | ||
cell, (vertexNum - 1 + cellNumVerts) % cellNumVerts); | ||
// This case should be unreachable; invalid verts fail the left side first | ||
// This case should be unreachable; invalid verts fail the left side first | ||
if (right == INVALID_DIGIT) return H3_NULL; // LCOV_EXCL_LINE | ||
int lRotations = 0; | ||
H3Index rightNeighbor = h3NeighborRotations(cell, right, &lRotations); | ||
|
@@ -269,3 +269,29 @@ void H3_EXPORT(vertexToPoint)(H3Index vertex, GeoCoord* coord) { | |
// Copy from boundary to output coord | ||
*coord = gb.verts[0]; | ||
} | ||
|
||
/** | ||
* Whether the input is a valid H3 vertex | ||
* @param vertex H3 index possibly describing a vertex | ||
* @return Whether the input is valid | ||
*/ | ||
int H3_EXPORT(isValidVertex)(H3Index vertex) { | ||
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. Do vertices have a unique representation? That is, for a given vertex, is it only allowed a single cell "owner"? It doesn't look like this code checks if the vertex has the correct owner. 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. My assumption is this function will assure the vertex is in the canonical form, if we want to support non-canonical vertexes, perhaps we need 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. Hm. That's a good point, though there's no way at present that you can create a non-canonical vertex, so I'm leery about adding public functions addressing a practically unreachable issue. If we wanted this check, I think I'd need to implement I don't have a clear picture for the validation functions on whether we should prioritize speed (because you might need to validate many indexes at once) or exhaustiveness, which in this case would mean an expensive check for an unlikely case. So I guess this brings up two questions:
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 we definitely want uniqueness for the indices (and thus not allow "non-canonical" vertices), and if we have a function called 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 to check validity by recreating the valid vertex and comparing, plus added a test for the incorrect-owner case. Recreating the vertex also covers the invalid vertex number case, so this simplified things nicely. |
||
if (H3_GET_MODE(vertex) != H3_VERTEX_MODE) { | ||
return 0; | ||
} | ||
|
||
int vertexNum = H3_GET_RESERVED_BITS(vertex); | ||
H3Index owner = vertex; | ||
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. Do we need the extra variable 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. That's a good question - I guess it should be fine to modify 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'm actually going to recommend the opposite tack. Getting the That will make this function slower, but since you'd only be validating the vertex if you're not sure about it, and since there are non-canonical representations that could be reached through some bit manipulation, it feels like the only time you'd be calling 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. Oh huh. I hadn't even thought of it, but yeah, the best way to check whether we have the canonical representation is to run this through |
||
H3_SET_MODE(owner, H3_HEXAGON_MODE); | ||
H3_SET_RESERVED_BITS(owner, 0); | ||
|
||
if (!H3_EXPORT(h3IsValid)(owner)) { | ||
return 0; | ||
} | ||
|
||
// The easiest way to ensure that the owner + vertex number is valid, | ||
// and that the vertex is canonical, is to recreate and compare. | ||
H3Index canonical = H3_EXPORT(cellToVertex)(owner, vertexNum); | ||
|
||
return vertex == canonical ? 1 : 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.
Please add a test for a specific valid vertex, to validate the right mode number is being used.
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 do.