-
Notifications
You must be signed in to change notification settings - Fork 472
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 h3ToIj #102
Add h3ToIj #102
Changes from 3 commits
5abcf1e
09a3bce
9e52b63
1204338
60da119
b81299d
d1927f8
62863a4
1170719
ffc1b32
974b3f2
bac0ad1
9ff9d20
a2b720d
d722df2
3671b4e
b982cb6
8cd83bb
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 |
---|---|---|
|
@@ -14,9 +14,10 @@ | |
* limitations under the License. | ||
*/ | ||
/** @file | ||
* @brief tests H3 index to IJK+ grid functions. | ||
* @brief tests H3 index to IJ or IJK+ grid functions, and H3 distance | ||
* function. | ||
* | ||
* usage: `testH3ToIjk` | ||
* usage: `testH3ToIj` | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
@@ -38,8 +39,11 @@ void h3Distance_identity_assertions(H3Index h3) { | |
|
||
t_assert(H3_EXPORT(h3Distance)(h3, h3) == 0, "distance to self is 0"); | ||
|
||
// Test that coordinates are as expected | ||
CoordIJ ij; | ||
t_assert(H3_EXPORT(h3ToIj)(h3, h3, &ij) == 0, "failed to get ij"); | ||
CoordIJK ijk; | ||
t_assert(h3ToIjk(h3, h3, &ijk) == 0, "failed to get ijk"); | ||
ijToIjk(&ij, &ijk); | ||
if (r == 0) { | ||
t_assert(_ijkMatches(&ijk, &UNIT_VECS[0]) == 1, "not at 0,0,0 (res 0)"); | ||
} else if (r == 1) { | ||
|
@@ -57,8 +61,10 @@ void h3Distance_identity_assertions(H3Index h3) { | |
} | ||
|
||
void h3Distance_neighbors_assertions(H3Index h3) { | ||
CoordIJK origin = {0}; | ||
t_assert(h3ToIjk(h3, h3, &origin) == 0, "got ijk for origin"); | ||
CoordIJ origin = {0}; | ||
t_assert(H3_EXPORT(h3ToIj)(h3, h3, &origin) == 0, "got ijk for origin"); | ||
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. Nit: Should say 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. Done |
||
CoordIJK originIjk; | ||
ijToIjk(&origin, &originIjk); | ||
|
||
for (int d = 1; d < 7; d++) { | ||
if (d == 1 && h3IsPentagon(h3)) { | ||
|
@@ -68,8 +74,11 @@ void h3Distance_neighbors_assertions(H3Index h3) { | |
int rotations = 0; | ||
H3Index offset = h3NeighborRotations(h3, d, &rotations); | ||
|
||
CoordIJK ijk = {0}; | ||
t_assert(h3ToIjk(h3, offset, &ijk) == 0, "got ijk for destination"); | ||
CoordIJ ij = {0}; | ||
t_assert(H3_EXPORT(h3ToIj)(h3, offset, &ij) == 0, | ||
"got ijk for destination"); | ||
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. Same nit here on 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. Done |
||
CoordIJK ijk; | ||
ijToIjk(&ij, &ijk); | ||
CoordIJK invertedIjk = {0}; | ||
_neighbor(&invertedIjk, d); | ||
for (int i = 0; i < 3; i++) { | ||
|
@@ -78,7 +87,7 @@ void h3Distance_neighbors_assertions(H3Index h3) { | |
_ijkAdd(&invertedIjk, &ijk, &ijk); | ||
_ijkNormalize(&ijk); | ||
|
||
t_assert(_ijkMatches(&ijk, &origin), "back to origin"); | ||
t_assert(_ijkMatches(&ijk, &originIjk), "back to origin"); | ||
} | ||
} | ||
|
||
|
@@ -109,7 +118,51 @@ void h3Distance_kRing_assertions(H3Index h3) { | |
} | ||
} | ||
|
||
BEGIN_TESTS(h3ToIjk); | ||
BEGIN_TESTS(h3ToIj); | ||
|
||
// Some indexes that represent base cells. Base cells | ||
// are hexagons except for `pent1`. | ||
H3Index bc1 = H3_INIT; | ||
setH3Index(&bc1, 0, 15, 0); | ||
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. Nit: Can you use clearer var names or add comments explaining what these are? |
||
|
||
H3Index bc2 = H3_INIT; | ||
setH3Index(&bc2, 0, 8, 0); | ||
|
||
H3Index bc3 = H3_INIT; | ||
setH3Index(&bc3, 0, 31, 0); | ||
|
||
H3Index pent1 = H3_INIT; | ||
setH3Index(&pent1, 0, 4, 0); | ||
|
||
TEST(ijkToIj_zero) { | ||
CoordIJK ijk = {0}; | ||
CoordIJ ij = {0}; | ||
|
||
ijkToIj(&ijk, &ij); | ||
t_assert(ij.i == 0, "ij.i zero"); | ||
t_assert(ij.j == 0, "ij.j zero"); | ||
|
||
ijToIjk(&ij, &ijk); | ||
t_assert(ijk.i == 0, "ijk.i zero"); | ||
t_assert(ijk.j == 0, "ijk.j zero"); | ||
t_assert(ijk.k == 0, "ijk.k zero"); | ||
} | ||
|
||
TEST(ijkToIj_roundtrip) { | ||
for (Direction dir = CENTER_DIGIT; dir < NUM_DIGITS; dir++) { | ||
CoordIJK ijk = {0}; | ||
_neighbor(&ijk, dir); | ||
|
||
CoordIJ ij = {0}; | ||
ijkToIj(&ijk, &ij); | ||
|
||
CoordIJK recovered = {0}; | ||
ijToIjk(&ij, &recovered); | ||
|
||
t_assert(_ijkMatches(&ijk, &recovered), | ||
"got same ijk coordinates back"); | ||
} | ||
} | ||
|
||
TEST(testIndexDistance) { | ||
H3Index bc = 0; | ||
|
@@ -193,18 +246,6 @@ TEST(h3Distance_kRing) { | |
} | ||
|
||
TEST(h3DistanceBaseCells) { | ||
H3Index bc1 = H3_INIT; | ||
setH3Index(&bc1, 0, 15, 0); | ||
|
||
H3Index bc2 = H3_INIT; | ||
setH3Index(&bc2, 0, 8, 0); | ||
|
||
H3Index bc3 = H3_INIT; | ||
setH3Index(&bc3, 0, 31, 0); | ||
|
||
H3Index pent1 = H3_INIT; | ||
setH3Index(&pent1, 0, 4, 0); | ||
|
||
t_assert(H3_EXPORT(h3Distance)(bc1, pent1) == 1, | ||
"distance to neighbor is 1 (15, 4)"); | ||
t_assert(H3_EXPORT(h3Distance)(bc1, bc2) == 1, | ||
|
@@ -231,4 +272,18 @@ TEST(h3DistanceFailed) { | |
"cannot compare at different resolutions"); | ||
} | ||
|
||
TEST(h3ToIjFailed) { | ||
CoordIJ ij; | ||
|
||
t_assert(H3_EXPORT(h3ToIj)(bc1, bc1, &ij) == 0, "failed to find IJ (1)"); | ||
t_assert(ij.i == 0 && ij.j == 0, "ij correct (1)"); | ||
t_assert(H3_EXPORT(h3ToIj)(bc1, pent1, &ij) == 0, "failed to find IJ (2)"); | ||
t_assert(ij.i == 1 && ij.j == 0, "ij correct (2)"); | ||
t_assert(H3_EXPORT(h3ToIj)(bc1, bc2, &ij) == 0, "failed to find IJ (3)"); | ||
t_assert(ij.i == 0 && ij.j == -1, "ij correct (3)"); | ||
t_assert(H3_EXPORT(h3ToIj)(bc1, bc3, &ij) == 0, "failed to find IJ (4)"); | ||
t_assert(ij.i == -1 && ij.j == 0, "ij correct (4)"); | ||
t_assert(H3_EXPORT(h3ToIj)(pent1, bc3, &ij) != 0, "failed to find IJ (5)"); | ||
} | ||
|
||
END_TESTS(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,16 @@ struct LinkedGeoPolygon { | |
LinkedGeoPolygon *next; | ||
}; | ||
|
||
/** @struct CoordIJ | ||
* @brief IJ hexagon coordinates | ||
* | ||
* Each axis is spaced 120 degrees apart. | ||
*/ | ||
typedef struct { | ||
int i; ///< i component | ||
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. Nit: Do we really need these inline comments? 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. @dfellis they parallel the inline comments on components in |
||
int j; ///< j component | ||
} CoordIJ; | ||
|
||
/** @defgroup geoToH3 geoToH3 | ||
* Functions for geoToH3 | ||
* @{ | ||
|
@@ -449,6 +459,14 @@ void H3_EXPORT(getH3UnidirectionalEdgeBoundary)(H3Index edge, GeoBoundary *gb); | |
int H3_EXPORT(h3Distance)(H3Index origin, H3Index h3); | ||
/** @} */ | ||
|
||
/** @defgroup h3ToIj h3ToIj | ||
* Functions for h3ToIj | ||
* @{ | ||
*/ | ||
/** @brief Returns two dimensional coordinates for the given index */ | ||
int H3_EXPORT(h3ToIj)(H3Index origin, H3Index h3, CoordIJ *out); | ||
/** @} */ | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -776,6 +776,9 @@ int isResClassIII(int res) { return res % 2; } | |
* Coordinates are only comparable if they come from the same | ||
* origin index. | ||
* | ||
* Failure may occur if the index is too far away from the origin | ||
* or if the index is on the other side of a pentagon. | ||
* | ||
* @param origin An anchoring index for the ijk+ coordinate system. | ||
* @param index Index to find the coordinates of | ||
* @param out ijk+ coordinates of the index will be placed here on success | ||
|
@@ -960,6 +963,35 @@ int h3ToIjk(H3Index origin, H3Index h3, CoordIJK* out) { | |
return 0; | ||
} | ||
|
||
/** | ||
* Produces ij coordinates for an index anchored by an origin. | ||
* | ||
* The coordinate space used by this function may have deleted | ||
* regions or warping due to pentagonal distortion. | ||
* | ||
* Coordinates are only comparable if they come from the same | ||
* origin index. | ||
* | ||
* Failure may occur if the index is too far away from the origin | ||
* or if the index is on the other side of a pentagon. | ||
* | ||
* @param origin An anchoring index for the ij coordinate system. | ||
* @param index Index to find the coordinates of | ||
* @param out ij coordinates of the index will be placed here on success | ||
* @return 0 on success, or another value on failure. | ||
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. Can you explain in the doc comment why you might get failure values? |
||
*/ | ||
int H3_EXPORT(h3ToIj)(H3Index origin, H3Index h3, CoordIJ* out) { | ||
CoordIJK ijk; | ||
int failed = h3ToIjk(origin, h3, &ijk); | ||
if (failed) { | ||
return failed; | ||
} | ||
|
||
ijkToIj(&ijk, out); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* Produces the grid distance between the two indexes. | ||
* | ||
|
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.
The comment on this test doesn't make 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.
Clarified that comment.