8000 Switch `int` digit to an enum, `Direction` by isaacbrodsky · Pull Request #77 · uber/h3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Switch int digit to an enum, Direction #77

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 5 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The public API of this library consists of the functions declared in file
[h3api.h](./src/h3lib/include/h3api.h).

## [Unreleased]
### Added
- Added Direction enum, replacing int and defined constants (#77)

## [3.0.7] - 2018-06-08
### Added
Expand Down
12 changes: 6 additions & 6 deletions src/apps/miscapps/generateBaseCellNeighbors.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 Uber Technologies, Inc.
* Copyright 2016-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.
Expand Down Expand Up @@ -153,12 +153,12 @@ static void generate() {
}
// direction from the pentagon towards the neighboring
// base cell
int dir = _unitIjkToDigit(&ijk);
Direction dir = _unitIjkToDigit(&ijk);

// the direction was detected as being the i direction,
// but this can't be because i is deleted from the
// pentagon. We need to choose a different direction.
if (dir == 1) {
if (dir == K_AXES_DIGIT) {
// 4 and 117 are 'polar' type pentagons, which have
// some different behavior.
if (i == 4 || i == 117) {
Expand All @@ -175,15 +175,15 @@ static void generate() {
if (i == 4 || i == 117) {
// 'polar' type pentagon with all faces pointing
// towards i
if (dir == 5) {
if (dir == IK_AXES_DIGIT) {
rotAdj = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these rotAdj assignments get moved to constants as well?

} else if (dir == 6) {
} else if (dir == IJ_AXES_DIGIT) {
rotAdj = 4;
}
} else {
// the deleted k subsequence causes 4 and 5 to
// 'warp', need to adjust for that.
if (dir == 4 || dir == 5) {
if (dir == I_AXES_DIGIT || dir == IK_AXES_DIGIT) {
rotAdj = dir;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apps/testapps/testKRing.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ TEST(cwOffsetPent) {
// Only direction 2 needs to be checked, because that is the only
// direction where we can move from digit 2 to digit 1, and into the
// deleted k subsequence.
t_assert(_getBaseCellNeighbor(neighbor, 2) != pentagon ||
t_assert(_getBaseCellNeighbor(neighbor, J_AXES_DIGIT) != pentagon ||
_baseCellIsCwOffset(pentagon, neighborFace),
"cwOffsetPent is reachable");
}
Expand Down
2 changes: 1 addition & 1 deletion src/h3lib/include/algos.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "vertexGraph.h"

// neighbor along the ijk coordinate system of the current face, rotated
H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations);
H3Index h3NeighborRotations(H3Index origin, Direction dir, int* rotations);

// k-ring implementation
void _kRingInternal(H3Index origin, int k, H3Index* out, int* distances,
Expand Down
4 changes: 2 additions & 2 deletions src/h3lib/include/baseCells.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 Uber Technologies, Inc.
* Copyright 2016-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.
Expand Down Expand Up @@ -51,6 +51,6 @@ int _faceIjkToBaseCell(const FaceIJK* h);
int _faceIjkToBaseCellCCWrot60(const FaceIJK* h);
void _baseCellToFaceIjk(int baseCell, FaceIJK* h);
bool _baseCellIsCwOffset(int baseCell, int testFace);
int _getBaseCellNeighbor(int baseCell, int dir);
int _getBaseCellNeighbor(int baseCell, Direction dir);

#endif
48 changes: 28 additions & 20 deletions src/h3lib/include/coordijk.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,30 @@ static const CoordIJK UNIT_VECS[] = {
{1, 1, 0} // direction 6
};

// H3 digit representing ijk+ axes direction
// see also unitVecs in coordijk.c
/** H3 digit in center */
#define CENTER_DIGIT 0
/** H3 digit in k-axes direction */
#define K_AXES_DIGIT 1
/** H3 digit in j-axes direction */
#define J_AXES_DIGIT 2
/** H3 digit in j == k direction */
#define JK_AXES_DIGIT (K_AXES_DIGIT | J_AXES_DIGIT) /* 3 */
/** H3 digit in i-axes direction */
#define I_AXES_DIGIT 4
/** H3 digit in i == k direction */
#define IK_AXES_DIGIT (I_AXES_DIGIT | K_AXES_DIGIT) /* 5 */
/** H3 digit in i == j direction */
#define IJ_AXES_DIGIT (I_AXES_DIGIT | J_AXES_DIGIT) /* 6 */
/** @brief H3 digit representing ijk+ axes direction.
* Values will be within the lowest 3 bits of an integer.
*/
typedef enum {
/** H3 digit in center */
CENTER_DIGIT = 0,
/** H3 digit in k-axes direction */
K_AXES_DIGIT = 1,
/** H3 digit in j-axes direction */
J_AXES_DIGIT = 2,
/** H3 digit in j == k direction */
JK_AXES_DIGIT = J_AXES_DIGIT | K_AXES_DIGIT, /* 3 */
/** H3 digit in i-axes direction */
I_AXES_DIGIT = 4,
/** H3 digit in i == k direction */
IK_AXES_DIGIT = I_AXES_DIGIT | K_AXES_DIGIT, /* 5 */
/** H3 digit in i == j direction */
IJ_AXES_DIGIT = I_AXES_DIGIT | J_AXES_DIGIT, /* 6 */
/** H3 digit in the invalid direction */
INVALID_DIGIT = 7,
/** Valid digits will be less than this value. Same value as INVALID 9E81 _DIGIT.
*/
NUM_DIGITS = INVALID_DIGIT
} Direction;

// Internal functions

Expand All @@ -81,17 +89,17 @@ void _ijkAdd(const CoordIJK* h1, const CoordIJK* h2, CoordIJK* sum);
void _ijkSub(const CoordIJK* h1, const CoordIJK* h2, CoordIJK* diff);
void _ijkScale(CoordIJK* c, int factor);
void _ijkNormalize(CoordIJK* c);
int _unitIjkToDigit(const CoordIJK* ijk);
Direction _unitIjkToDigit(const CoordIJK* ijk);
void _upAp7(CoordIJK* ijk);
void _upAp7r(CoordIJK* ijk);
void _downAp7(CoordIJK* ijk);
void _downAp7r(CoordIJK* ijk);
void _downAp3(CoordIJK* ijk);
void _downAp3r(CoordIJK* ijk);
void _neighbor(CoordIJK* ijk, int digit);
void _neighbor(CoordIJK* ijk, Direction digit);
void _ijkRotate60ccw(CoordIJK* ijk);
void _ijkRotate60cw(CoordIJK* ijk);
int _rotate60ccw(int digit);
int _rotate60cw(int digit);
Direction _rotate60ccw(Direction digit);
Direction _rotate60cw(Direction digit);

#endif
12 changes: 6 additions & 6 deletions src/h3lib/include/h3Index.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 Uber Technologies, Inc.
* Copyright 2016-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.
Expand Down Expand Up @@ -115,9 +115,9 @@
/**
* Gets the resolution res integer digit (0-7) of h3.
*/
#define H3_GET_INDEX_DIGIT(h3, res) \
((int)((((h3) >> ((MAX_H3_RES - (res)) * H3_PER_DIGIT_OFFSET)) & \
H3_DIGIT_MASK)))
#define H3_GET_INDEX_DIGIT(h3, res) \
((Direction)((((h3) >> ((MAX_H3_RES - (res)) * H3_PER_DIGIT_OFFSET)) & \
H3_DIGIT_MASK)))

/**
* Sets a value in the reserved space. Setting to non-zero may produce invalid
Expand Down Expand Up @@ -147,13 +147,13 @@
*/
#define H3_INVALID_INDEX 0

void setH3Index(H3Index* h, int res, int baseCell, int initDigit);
void setH3Index(H3Index* h, int res, int baseCell, Direction initDigit);
int isResClassIII(int res);

// Internal functions

H3Index _faceIjkToH3(const FaceIJK* fijk, int res);
int _h3LeadingNonZeroDigit(H3Index h);
Direction _h3LeadingNonZeroDigit(H3Index h);
H3Index _h3RotatePent60ccw(H3Index h);
H3Index _h3Rotate60ccw(H3Index h);
H3Index _h3Rotate60cw(H3Index h);
Expand Down
135 changes: 97 additions & 38 deletions src/h3lib/lib/algos.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,98 @@
* \\2/
* </pre>
*/
static const int DIRECTIONS[6] = {J_AXES_DIGIT, JK_AXES_DIGIT, K_AXES_DIGIT,
IK_AXES_DIGIT, I_AXES_DIGIT, IJ_AXES_DIGIT};
static const Direction DIRECTIONS[6] = {J_AXES_DIGIT, JK_AXES_DIGIT,
K_AXES_DIGIT, IK_AXES_DIGIT,
I_AXES_DIGIT, IJ_AXES_DIGIT};

/**
* Direction used for traversing to the next outward hexagonal ring.
*/
static const int NEXT_RING_DIRECTION = I_AXES_DIGIT;
static const Direction NEXT_RING_DIRECTION = I_AXES_DIGIT;

/**
* New digit when traversing along class II grids.
*
* Current digit -> direction -> new digit.
*/
static const Direction NEW_DIGIT_II[7][7] = {
{CENTER_DIGIT, K_AXES_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT, I_AXES_DIGIT,
IK_AXES_DIGIT, IJ_AXES_DIGIT},
{K_AXES_DIGIT, I_AXES_DIGIT, JK_AXES_DIGIT, IJ_AXES_DIGIT, IK_AXES_DIGIT,
J_AXES_DIGIT, CENTER_DIGIT},
{J_AXES_DIGIT, JK_AXES_DIGIT, K_AXES_DIGIT, I_AXES_DIGIT, IJ_AXES_DIGIT,
CENTER_DIGIT, IK_AXES_DIGIT},
{JK_AXES_DIGIT, IJ_AXES_DIGIT, I_AXES_DIGIT, IK_AXES_DIGIT, CENTER_DIGIT,
K_AXES_DIGIT, J_AXES_DIGIT},
{I_AXES_DIGIT, IK_AXES_DIGIT, IJ_AXES_DIGIT, CENTER_DIGIT, J_AXES_DIGIT,
JK_AXES_DIGIT, K_AXES_DIGIT},
{IK_AXES_DIGIT, J_AXES_DIGIT, CENTER_DIGIT, K_AXES_DIGIT, JK_AXES_DIGIT,
IJ_AXES_DIGIT, I_AXES_DIGIT},
{IJ_AXES_DIGIT, CENTER_DIGIT, IK_AXES_DIGIT, J_AXES_DIGIT, K_AXES_DIGIT,
I_AXES_DIGIT, JK_AXES_DIGIT}};

/**
* New traversal direction when traversing along class II grids.
*
* Current digit -> direction -> new ap7 move (at coarser level).
*/
static const Direction NEW_ADJUSTMENT_II[7][7] = {
{CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, K_AXES_DIGIT, CENTER_DIGIT, K_AXES_DIGIT, CENTER_DIGIT,
IK_AXES_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, J_AXES_DIGIT},
{CENTER_DIGIT, K_AXES_DIGIT, JK_AXES_DIGIT, JK_AXES_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, I_AXES_DIGIT,
I_AXES_DIGIT, IJ_AXES_DIGIT},
{CENTER_DIGIT, IK_AXES_DIGIT, CENTER_DIGIT, CENTER_DIGIT, I_AXES_DIGIT,
IK_AXES_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, J_AXES_DIGIT, CENTER_DIGIT, IJ_AXES_DIGIT,
CENTER_DIGIT, IJ_AXES_DIGIT}};

/**
* New traversal direction when traversing along class III grids.
*
* Current digit -> direction -> new ap7 move (at coarser level).
*/
static const Direction NEW_DIGIT_III[7][7] = {
{CENTER_DIGIT, K_AXES_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT, I_AXES_DIGIT,
IK_AXES_DIGIT, IJ_AXES_DIGIT},
{K_AXES_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT, I_AXES_DIGIT, IK_AXES_DIGIT,
IJ_AXES_DIGIT, CENTER_DIGIT},
{J_AXES_DIGIT, JK_AXES_DIGIT, I_AXES_DIGIT, IK_AXES_DIGIT, IJ_AXES_DIGIT,
CENTER_DIGIT, K_AXES_DIGIT},
{JK_AXES_DIGIT, I_AXES_DIGIT, IK_AXES_DIGIT, IJ_AXES_DIGIT, CENTER_DIGIT,
K_AXES_DIGIT, J_AXES_DIGIT},
{I_AXES_DIGIT, IK_AXES_DIGIT, IJ_AXES_DIGIT, CENTER_DIGIT, K_AXES_DIGIT,
J_AXES_DIGIT, JK_AXES_DIGIT},
{IK_AXES_DIGIT, IJ_AXES_DIGIT, CENTER_DIGIT, K_AXES_DIGIT, J_AXES_DIGIT,
JK_AXES_DIGIT, I_AXES_DIGIT},
{IJ_AXES_DIGIT, CENTER_DIGIT, K_AXES_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT,
I_AXES_DIGIT, IK_AXES_DIGIT}};

/**
* New traversal direction when traversing along class III grids.
*
* Current digit -> direction -> new ap7 move (at coarser level).
*/
static const Direction NEW_ADJUSTMENT_III[7][7] = {
{CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, K_AXES_DIGIT, CENTER_DIGIT, JK_AXES_DIGIT, CENTER_DIGIT,
K_AXES_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, J_AXES_DIGIT, J_AXES_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, IJ_AXES_DIGIT},
{CENTER_DIGIT, JK_AXES_DIGIT, J_AXES_DIGIT, JK_AXES_DIGIT, CENTER_DIGIT,
CENTER_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, CENTER_DIGIT, I_AXES_DIGIT,
IK_AXES_DIGIT, I_AXES_DIGIT},
{CENTER_DIGIT, K_AXES_DIGIT, CENTER_DIGIT, CENTER_DIGIT, IK_AXES_DIGIT,
IK_AXES_DIGIT, CENTER_DIGIT},
{CENTER_DIGIT, CENTER_DIGIT, IJ_AXES_DIGIT, CENTER_DIGIT, I_AXES_DIGIT,
CENTER_DIGIT, IJ_AXES_DIGIT}};

/**
* Maximum number of indices that result from the kRing algorithm with the given
Expand Down Expand Up @@ -118,7 +203,7 @@ void H3_EXPORT(kRingDistances)(H3Index origin, int k, H3Index* out,
// Fast algo failed, fall back to slower, correct algo
// and also wipe out array because contents untrustworthy
for (int i = 0; i < maxIdx; i++) {
out[i] = 0;
out[i] = H3_INVALID_INDEX;
distances[i] = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required, but it's worth noting that 0 is actually a valid value in distances - maybe worth defining INVALID_DISTANCE = -1 or similar?

}
_kRingInternal(origin, k, out, distances, maxIdx, 0);
Expand Down Expand Up @@ -186,7 +271,7 @@ void _kRingInternal(H3Index origin, int k, H3Index* out, int* distances,
* @return H3Index of the specified neighbor or 0 if deleted k-subsequence
* distortion is encountered.
*/
H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {
H3Index h3NeighborRotations(H3Index origin, Direction dir, int* rotations) {
H3Index out = origin;

for (int i = 0; i < *rotations; i++) {
Expand All @@ -195,7 +280,7 @@ H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {

int newRotations = 0;
int oldBaseCell = H3_GET_BASE_CELL(out);
int oldLeadingDigit = _h3LeadingNonZeroDigit(out);
Direction oldLeadingDigit = _h3LeadingNonZeroDigit(out);

// Adjust the indexing digits and, if needed, the base cell.
int r = H3_GET_RESOLUTION(out) - 1;
Expand All @@ -220,34 +305,8 @@ H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {

break;
} else {
// generated by hand
// Current digit -> direction -> new digit
const int NEW_DIGIT_II[7][7] = {
{0, 1, 2, 3, 4, 5, 6}, {1, 4, 3, 6, 5, 2, 0},
{2, 3, 1, 4, 6, 0, 5}, {3, 6, 4, 5, 0, 1, 2},
{4, 5, 6, 0, 2, 3, 1}, {5, 2, 0, 1, 3, 6, 4},
{6, 0, 5, 2, 1, 4, 3}};
// Current digit -> direction -> new ap7 move
const int NEW_ADJUSTMENT_II[7][7] = {
{0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 5, 0},
{0, 0, 2, 3, 0, 0, 2}, {0, 1, 3, 3, 0, 0, 0},
{0, 0, 0, 0, 4, 4, 6}, {0, 5, 0, 0, 4, 5, 0},
{0, 0, 2, 0, 6, 0, 6}};
// Current digit -> direction -> new digit
const int NEW_DIGIT_III[7][7] = {
{0, 1, 2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6, 0},
{2, 3, 4, 5, 6, 0, 1}, {3, 4, 5, 6, 0, 1, 2},
{4, 5, 6, 0, 1, 2, 3}, {5, 6, 0, 1, 2, 3, 4},
{6, 0, 1, 2, 3, 4, 5}};
// Current digit -> direction -> new ap7 move
const int NEW_ADJUSTMENT_III[7][7] = {
{0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 3, 0, 1, 0},
{0, 0, 2, 2, 0, 0, 6}, {0, 3, 2, 3, 0, 0, 0},
{0, 0, 0, 0, 4, 5, 4}, {0, 1, 0, 0, 5, 5, 0},
{0, 0, 6, 0, 4, 0, 6}};

int oldDigit = H3_GET_INDEX_DIGIT(out, r + 1);
int nextDir;
Direction oldDigit = H3_GET_INDEX_DIGIT(out, r + 1);
Direction nextDir;
if (isResClassIII(r + 1)) {
H3_SET_INDEX_DIGIT(out, r + 1, NEW_DIGIT_II[oldDigit][dir]);
nextDir = NEW_ADJUSTMENT_II[oldDigit][dir];
Expand All @@ -256,7 +315,7 @@ H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {
nextDir = NEW_ADJUSTMENT_III[oldDigit][dir];
}

if (nextDir != 0) {
if (nextDir != CENTER_DIGIT) {
dir = nextDir;
r--;
} else {
Expand Down Expand Up @@ -294,7 +353,7 @@ H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {
// base cell.
if (oldLeadingDigit == CENTER_DIGIT) {
// Undefined: the k direction is deleted from here
return 0;
return H3_INVALID_INDEX;
} else if (oldLeadingDigit == JK_AXES_DIGIT) {
// Rotate out of the deleted k subsequence
// We also need an additional change to the direction we're
Expand All @@ -309,7 +368,7 @@ H3Index h3NeighborRotations(H3Index origin, int dir, int* rotations) {
*rotations = *rotations + 5;
} else {
// Should never occur
return 0; // LCOV_EXCL_LINE
return H3_INVALID_INDEX; // LCOV_EXCL_LINE
}
}
}
Expand Down Expand Up @@ -760,7 +819,7 @@ void H3_EXPORT(polyfill)(const GeoPolygon* geoPolygon, int res, H3Index* out) {
hexCenter.lon = constrainLng(hexCenter.lon);
// And remove from list if not
if (!_pointInPolyContains(geoPolygon, bboxes, &hexCenter)) {
out[i] = 0;
out[i] = H3_INVALID_INDEX;
}
}
}
Expand Down
Loading
0