| 1 | /* |
| 2 | * Copyright 2016-2018 Uber Technologies, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | /** @file coordijk.h |
| 17 | * @brief Header file for CoordIJK functions including conversion from lat/lon |
| 18 | * |
| 19 | * References two Vec2d cartesian coordinate systems: |
| 20 | * |
| 21 | * 1. gnomonic: face-centered polyhedral gnomonic projection space with |
| 22 | * traditional scaling and x-axes aligned with the face Class II |
| 23 | * i-axes. |
| 24 | * |
| 25 | * 2. hex2d: local face-centered coordinate system scaled a specific H3 grid |
| 26 | * resolution unit length and with x-axes aligned with the local |
| 27 | * i-axes |
| 28 | */ |
| 29 | |
| 30 | #ifndef COORDIJK_H |
| 31 | #define COORDIJK_H |
| 32 | |
| 33 | #include "geoCoord.h" |
| 34 | #include "h3api.h" |
| 35 | #include "vec2d.h" |
| 36 | |
| 37 | /** @struct CoordIJK |
| 38 | * @brief IJK hexagon coordinates |
| 39 | * |
| 40 | * Each axis is spaced 120 degrees apart. |
| 41 | */ |
| 42 | typedef struct { |
| 43 | int i; ///< i component |
| 44 | int j; ///< j component |
| 45 | int k; ///< k component |
| 46 | } CoordIJK; |
| 47 | |
| 48 | /** @brief CoordIJK unit vectors corresponding to the 7 H3 digits. |
| 49 | */ |
| 50 | static const CoordIJK UNIT_VECS[] = { |
| 51 | {0, 0, 0}, // direction 0 |
| 52 | {0, 0, 1}, // direction 1 |
| 53 | {0, 1, 0}, // direction 2 |
| 54 | {0, 1, 1}, // direction 3 |
| 55 | {1, 0, 0}, // direction 4 |
| 56 | {1, 0, 1}, // direction 5 |
| 57 | {1, 1, 0} // direction 6 |
| 58 | }; |
| 59 | |
| 60 | /** @brief H3 digit representing ijk+ axes direction. |
| 61 | * Values will be within the lowest 3 bits of an integer. |
| 62 | */ |
| 63 | typedef enum { |
| 64 | /** H3 digit in center */ |
| 65 | CENTER_DIGIT = 0, |
| 66 | /** H3 digit in k-axes direction */ |
| 67 | K_AXES_DIGIT = 1, |
| 68 | /** H3 digit in j-axes direction */ |
| 69 | J_AXES_DIGIT = 2, |
| 70 | /** H3 digit in j == k direction */ |
| 71 | JK_AXES_DIGIT = J_AXES_DIGIT | K_AXES_DIGIT, /* 3 */ |
| 72 | /** H3 digit in i-axes direction */ |
| 73 | I_AXES_DIGIT = 4, |
| 74 | /** H3 digit in i == k direction */ |
| 75 | IK_AXES_DIGIT = I_AXES_DIGIT | K_AXES_DIGIT, /* 5 */ |
| 76 | /** H3 digit in i == j direction */ |
| 77 | IJ_AXES_DIGIT = I_AXES_DIGIT | J_AXES_DIGIT, /* 6 */ |
| 78 | /** H3 digit in the invalid direction */ |
| 79 | INVALID_DIGIT = 7, |
| 80 | /** Valid digits will be less than this value. Same value as INVALID_DIGIT. |
| 81 | */ |
| 82 | NUM_DIGITS = INVALID_DIGIT |
| 83 | } Direction; |
| 84 | |
| 85 | // Internal functions |
| 86 | |
| 87 | void _setIJK(CoordIJK* ijk, int i, int j, int k); |
| 88 | void _hex2dToCoordIJK(const Vec2d* v, CoordIJK* h); |
| 89 | void _ijkToHex2d(const CoordIJK* h, Vec2d* v); |
| 90 | int _ijkMatches(const CoordIJK* c1, const CoordIJK* c2); |
| 91 | void _ijkAdd(const CoordIJK* h1, const CoordIJK* h2, CoordIJK* sum); |
| 92 | void _ijkSub(const CoordIJK* h1, const CoordIJK* h2, CoordIJK* diff); |
| 93 | void _ijkScale(CoordIJK* c, int factor); |
| 94 | void _ijkNormalize(CoordIJK* c); |
| 95 | Direction _unitIjkToDigit(const CoordIJK* ijk); |
| 96 | void _upAp7(CoordIJK* ijk); |
| 97 | void _upAp7r(CoordIJK* ijk); |
| 98 | void _downAp7(CoordIJK* ijk); |
| 99 | void _downAp7r(CoordIJK* ijk); |
| 100 | void _downAp3(CoordIJK* ijk); |
| 101 | void _downAp3r(CoordIJK* ijk); |
| 102 | void _neighbor(CoordIJK* ijk, Direction digit); |
| 103 | void _ijkRotate60ccw(CoordIJK* ijk); |
| 104 | void _ijkRotate60cw(CoordIJK* ijk); |
| 105 | Direction _rotate60ccw(Direction digit); |
| 106 | Direction _rotate60cw(Direction digit); |
| 107 | int ijkDistance(const CoordIJK* a, const CoordIJK* b); |
| 108 | void ijkToIj(const CoordIJK* ijk, CoordIJ* ij); |
| 109 | void ijToIjk(const CoordIJ* ij, CoordIJK* ijk); |
| 110 | void ijkToCube(CoordIJK* ijk); |
| 111 | void cubeToIjk(CoordIJK* ijk); |
| 112 | |
| 113 | #endif |
| 114 | |