-
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
Merged
Merged
Add h3ToIj #102
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5abcf1e
Add h3ToIj
09a3bce
Fix whitespace in CMakeLists.txt
9e52b63
Add additional comments about failure cases
1204338
Adjust comments in testH3ToIj.c per review
60da119
Merge branch 'master' into h3-to-ij
b81299d
Rename `h3ToIj` to `experimentalH3ToLocalIj`
d1927f8
Merge branch 'master' into h3-to-ij
62863a4
Add h3ToLocalIj to CHANGELOG
1170719
Cleanup refactoring to localij.h
ffc1b32
Merge branch 'master' into h3-to-ij
974b3f2
Add experimentalLocalIjToH3
bac0ad1
Merge branch 'master' into h3-to-ij
9ff9d20
Fix capitalization of localij.h
a2b720d
Fix undefined handling of deleted base cell
d722df2
Add tests for out of range local IJ coordinates, assert unrotated dir…
3671b4e
Add test for traversing the local IJ space
b982cb6
Adjust comments and test assertion messages based on feedback.
8cd83bb
Remove check for the mode of indexes in h3ToLocalIjk
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2018 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** @file | ||
* @brief tests IJ grid functions and IJK distance functions. | ||
* | ||
* usage: `testCoordIj` | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "algos.h" | ||
#include "baseCells.h" | ||
#include "constants.h" | ||
#include "h3Index.h" | ||
#include "h3api.h" | ||
#include "localij.h" | ||
#include "stackAlloc.h" | ||
#include "test.h" | ||
#include "utility.h" | ||
|
||
SUITE(coordIj) { | ||
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright 2018 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** @file | ||
* @brief tests H3 distance function. | ||
* | ||
* usage: `testH3Distance` | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "algos.h" | ||
#include "baseCells.h" | ||
#include "constants.h" | ||
#include "h3Index.h" | ||
#include "h3api.h" | ||
#include "localij.h" | ||
#include "stackAlloc.h" | ||
#include "test.h" | ||
#include "utility.h" | ||
|
||
static const int MAX_DISTANCES[] = {1, 2, 5, 12, 19, 26}; | ||
|
||
static void h3Distance_identity_assertions(H3Index h3) { | ||
t_assert(H3_EXPORT(h3Distance)(h3, h3) == 0, "distance to self is 0"); | ||
} | ||
|
||
static void h3Distance_kRing_assertions(H3Index h3) { | ||
int r = H3_GET_RESOLUTION(h3); | ||
if (r > 5) { | ||
t_assert(false, "wrong res"); | ||
} | ||
int maxK = MAX_DISTANCES[r]; | ||
|
||
int sz = H3_EXPORT(maxKringSize)(maxK); | ||
STACK_ARRAY_CALLOC(H3Index, neighbors, sz); | ||
STACK_ARRAY_CALLOC(int, distances, sz); | ||
|
||
H3_EXPORT(kRingDistances)(h3, maxK, neighbors, distances); | ||
|
||
for (int i = 0; i < sz; i++) { | ||
if (neighbors[i] == 0) { | ||
continue; | ||
} | ||
|
||
int calculatedDistance = H3_EXPORT(h3Distance)(h3, neighbors[i]); | ||
|
||
// Don't consider indexes where h3Distance reports failure to | ||
// generate | ||
t_assert(calculatedDistance == distances[i] || calculatedDistance == -1, | ||
"kRingDistances matches h3Distance"); | ||
} | ||
} | ||
|
||
SUITE(h3Distance) { | ||
// Some indexes that represent base cells. Base cells | ||
// are hexagons except for `pent1`. | ||
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); | ||
|
||
TEST(testIndexDistance) { | ||
H3Index bc = 0; | ||
setH3Index(&bc, 1, 17, 0); | ||
H3Index p = 0; | ||
setH3Index(&p, 1, 14, 0); | ||
H3Index p2; | ||
setH3Index(&p2, 1, 14, 2); | ||
H3Index p3; | ||
setH3Index(&p3, 1, 14, 3); | ||
H3Index p4; | ||
setH3Index(&p4, 1, 14, 4); | ||
H3Index p5; | ||
setH3Index(&p5, 1, 14, 5); | ||
H3Index p6; | ||
setH3Index(&p6, 1, 14, 6); | ||
|
||
t_assert(H3_EXPORT(h3Distance)(bc, p) == 3, "distance onto pentagon"); | ||
t_assert(H3_EXPORT(h3Distance)(bc, p2) == 2, "distance onto p2"); | ||
t_assert(H3_EXPORT(h3Distance)(bc, p3) == 3, "distance onto p3"); | ||
// TODO works correctly but is rejected due to possible pentagon | ||
// distortion. | ||
// t_assert(H3_EXPORT(h3Distance)(bc, p4) == 3, "distance onto p4"); | ||
// t_assert(H3_EXPORT(h3Distance)(bc, p5) == 4, "distance onto p5"); | ||
t_assert(H3_EXPORT(h3Distance)(bc, p6) == 2, "distance onto p6"); | ||
} | ||
|
||
TEST(testIndexDistance2) { | ||
H3Index origin = 0x820c4ffffffffffL; | ||
// Destination is on the other side of the pentagon | ||
H3Index destination = 0x821ce7fffffffffL; | ||
|
||
// TODO doesn't work because of pentagon distortion. Both should be 5. | ||
t_assert(H3_EXPORT(h3Distance)(destination, origin) == -1, | ||
"distance in res 2 across pentagon"); | ||
t_assert(H3_EXPORT(h3Distance)(origin, destination) == -1, | ||
"distance in res 2 across pentagon (reversed)"); | ||
} | ||
|
||
TEST(h3Distance_identity) { | ||
iterateAllIndexesAtRes(0, h3Distance_identity_assertions); | ||
iterateAllIndexesAtRes(1, h3Distance_identity_assertions); | ||
iterateAllIndexesAtRes(2, h3Distance_identity_assertions); | ||
} | ||
|
||
TEST(h3Distance_kRing) { | ||
iterateAllIndexesAtRes(0, h3Distance_kRing_assertions); | ||
iterateAllIndexesAtRes(1, h3Distance_kRing_assertions); | ||
iterateAllIndexesAtRes(2, h3Distance_kRing_assertions); | ||
// Don't iterate all of res 3, to save time | ||
iterateAllIndexesAtResPartial(3, h3Distance_kRing_assertions, 27); | ||
// These would take too long, even at partial execution | ||
// iterateAllIndexesAtResPartial(4, h3Distance_kRing_assertions, 20); | ||
// iterateAllIndexesAtResPartial(5, h3Distance_kRing_assertions, 20); | ||
} | ||
|
||
TEST(h3DistanceBaseCells) { | ||
t_assert(H3_EXPORT(h3Distance)(bc1, pent1) == 1, | ||
"distance to neighbor is 1 (15, 4)"); | ||
t_assert(H3_EXPORT(h3Distance)(bc1, bc2) == 1, | ||
"distance to neighbor is 1 (15, 8)"); | ||
t_assert(H3_EXPORT(h3Distance)(bc1, bc3) == 1, | ||
"distance to neighbor is 1 (15, 31)"); | ||
t_assert(H3_EXPORT(h3Distance)(pent1, bc3) == -1, | ||
"distance to neighbor is invalid"); | ||
} | ||
|
||
TEST(ijkDistance) { | ||
CoordIJK z = {0, 0, 0}; | ||
CoordIJK i = {1, 0, 0}; | ||
CoordIJK ik = {1, 0, 1}; | ||
CoordIJK ij = {1, 1, 0}; | ||
CoordIJK j2 = {0, 2, 0}; | ||
|
||
t_assert(ijkDistance(&z, &z) == 0, "identity distance 0,0,0"); | ||
t_assert(ijkDistance(&i, &i) == 0, "identity distance 1,0,0"); | ||
t_assert(ijkDistance(&ik, &ik) == 0, "identity distance 1,0,1"); | ||
t_assert(ijkDistance(&ij, &ij) == 0, "identity distance 1,1,0"); | ||
t_assert(ijkDistance(&j2, &j2) == 0, "identity distance 0,2,0"); | ||
|
||
t_assert(ijkDistance(&z, &i) == 1, "0,0,0 to 1,0,0"); | ||
t_assert(ijkDistance(&z, &j2) == 2, "0,0,0 to 0,2,0"); | ||
t_assert(ijkDistance(&z, &ik) == 1, "0,0,0 to 1,0,1"); | ||
t_assert(ijkDistance(&i, &ik) == 1, "1,0,0 to 1,0,1"); | ||
t_assert(ijkDistance(&ik, &j2) == 3, "1,0,1 to 0,2,0"); | ||
t_assert(ijkDistance(&ij, &ik) == 2, "1,0,1 to 1,1,0"); | ||
} | ||
|
||
TEST(h3DistanceFailed) { | ||
H3Index h3 = 0x832830fffffffffL; | ||
H3Index edge = | ||
H3_EXPORT(getH3UnidirectionalEdge(h3, 0x832834fffffffffL)); | ||
H3Index h3res2 = 0x822837fffffffffL; | ||
|
||
t_assert(H3_EXPORT(h3Distance)(edge, h3) == -1, | ||
"edges cannot be origins"); | ||
t_assert(H3_EXPORT(h3Distance)(h3, edge) == -1, | ||
"edges cannot be destinations"); | ||
t_assert(H3_EXPORT(h3Distance)(h3, h3res2) == -1, | ||
"cannot compare at different resolutions"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this supposed to be
h3ToLocalIj
like elsewhere or no?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.
No, as below the IJK functions need to be kept for internal implementation.