-
Notifications
You must be signed in to change notification settings - Fork 503
Add h3Distance, and internal h3ToIjk and ijkDistance #83
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
58ee5c3
Add h3ToIjk, along with ijkDistance and h3Distance
e9eaec6
Make h3ToIjk and ijkDistance internal
b8fd4e8
Stack alloc in testH3ToIjk
b245905
Add some coverage of invalid mode, resolution cases
53ccd77
Change another case of calloc to stack alloc in tests
5924008
Refactor _getBaseCellDirection to its own function
d1deaa9
`_imax` is now a macro `MAX`
6b7299f
Add h3ToIjk filter application to changelog
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
/* | ||
* 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 stdin/stdout filter that converts from H3 indexes to relative IJK | ||
* coordinates. This is experimental. | ||
* | ||
* usage: `h3ToIjk [origin]` | ||
* | ||
* The program reads H3 indexes from stdin and outputs the corresponding | ||
* IJK coordinates to stdout, until EOF is encountered. The H3 indexes | ||
* should be in integer form. `-1 -1 -1` is printed if the IJK coordinates | ||
* could not be obtained. | ||
* | ||
* `origin` indicates the origin (or anchoring) index for the IJK coordinate | ||
* space. | ||
*/ | ||
|
||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "coordijk.h" | ||
#include "h3Index.h" | ||
#include "h3api.h" | ||
#include "utility.h" | ||
|
||
void doCell(H3Index h, H3Index origin) { | ||
CoordIJK ijk; | ||
if (h3ToIjk(origin, h, &ijk)) { | ||
printf("-1 -1 -1\n"); | ||
} else { | ||
printf("%d %d %d\n", ijk.i, ijk.j, ijk.k); | ||
} | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
// check command line args | ||
if (argc != 2) { | ||
fprintf(stderr, "usage: %s [origin]\n", argv[0]); | ||
exit(1); | ||
} | ||
|
||
H3Index origin; | ||
|
||
if (!sscanf(argv[1], "%" PRIx64, &origin)) | ||
error("origin could not be read"); | ||
|
||
if (!H3_EXPORT(h3IsValid)(origin)) error("origin is invalid"); | ||
|
||
// process the indexes on stdin | ||
char buff[BUFF_SIZE]; | ||
while (1) { | ||
// get an index from stdin | ||
if (!fgets(buff, BUFF_SIZE, stdin)) { | ||
if (feof(stdin)) | ||
break; | ||
else | ||
error("reading H3 index from stdin"); | ||
} | ||
|
||
H3Index h3 = H3_EXPORT(stringToH3)(buff); | ||
doCell(h3, origin); | ||
} | ||
} |
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.
Again, C users would probably prefer macros here and you'll get better performance because C compiler can see what is going on in a macro, but function pointers are opaque. Side note: this is why using the C
qsort
in C++ is slower thanstd::sort
from C++ STL, due to function pointers being worse off than templates.Anyway, here's how I'd do it based on Jansson:
Then users can do this:
Which expands to:
Uh oh!
There was an error while loading. Please reload this page.
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'll change this, but I am not a huge fan of macros. Since this is a test, I don't think performance is that big a concern.
edit: also, this does not permit reusing the code for more than one resolution. That still needs to be a function call or another macro.
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 everyone hates macros until they program in C for a while and realize it's the only way to avoid seriously ugly code. Also, underrated technique: X macro.
Meanwhile, rethinking this API too so that the blocks have matching parentheses.
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.
OK seeing as this is a test I don't really think it's worth the effort. Thought this might lead to a convenience API in general. Never mind my suggestion.