/src/h3/src/h3lib/lib/directedEdge.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2017-2018, 2020-2021 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 directedEdge.c |
17 | | * @brief DirectedEdge functions for manipulating directed edge indexes. |
18 | | */ |
19 | | |
20 | | #include <inttypes.h> |
21 | | #include <stdbool.h> |
22 | | |
23 | | #include "algos.h" |
24 | | #include "constants.h" |
25 | | #include "coordijk.h" |
26 | | #include "h3Assert.h" |
27 | | #include "h3Index.h" |
28 | | #include "latLng.h" |
29 | | #include "vertex.h" |
30 | | |
31 | | /** |
32 | | * Returns whether or not the provided H3Indexes are neighbors. |
33 | | * @param origin The origin H3 index. |
34 | | * @param destination The destination H3 index. |
35 | | * @param out Set to 1 if the indexes are neighbors, 0 otherwise |
36 | | * @return Error code if the origin or destination are invalid or incomparable. |
37 | | */ |
38 | | H3Error H3_EXPORT(areNeighborCells)(H3Index origin, H3Index destination, |
39 | 0 | int *out) { |
40 | | // Make sure they're hexagon indexes |
41 | 0 | if (H3_GET_MODE(origin) != H3_CELL_MODE || |
42 | 0 | H3_GET_MODE(destination) != H3_CELL_MODE) { |
43 | 0 | return E_CELL_INVALID; |
44 | 0 | } |
45 | | |
46 | | // Hexagons cannot be neighbors with themselves |
47 | 0 | if (origin == destination) { |
48 | 0 | *out = 0; |
49 | 0 | return E_SUCCESS; |
50 | 0 | } |
51 | | |
52 | | // Only hexagons in the same resolution can be neighbors |
53 | 0 | if (H3_GET_RESOLUTION(origin) != H3_GET_RESOLUTION(destination)) { |
54 | 0 | return E_RES_MISMATCH; |
55 | 0 | } |
56 | | |
57 | | // H3 Indexes that share the same parent are very likely to be neighbors |
58 | | // Child 0 is neighbor with all of its parent's 'offspring', the other |
59 | | // children are neighbors with 3 of the 7 children. So a simple comparison |
60 | | // of origin and destination parents and then a lookup table of the children |
61 | | // is a super-cheap way to possibly determine they are neighbors. |
62 | 0 | int parentRes = H3_GET_RESOLUTION(origin) - 1; |
63 | 0 | if (parentRes > 0) { |
64 | | // TODO: Return error codes here |
65 | 0 | H3Index originParent; |
66 | 0 | H3_EXPORT(cellToParent)(origin, parentRes, &originParent); |
67 | 0 | H3Index destinationParent; |
68 | 0 | H3_EXPORT(cellToParent)(destination, parentRes, &destinationParent); |
69 | 0 | if (originParent == destinationParent) { |
70 | 0 | Direction originResDigit = |
71 | 0 | H3_GET_INDEX_DIGIT(origin, parentRes + 1); |
72 | 0 | Direction destinationResDigit = |
73 | 0 | H3_GET_INDEX_DIGIT(destination, parentRes + 1); |
74 | 0 | if (originResDigit >= INVALID_DIGIT || |
75 | 0 | destinationResDigit >= INVALID_DIGIT) { |
76 | | // Prevent indexing off the end of the array below |
77 | 0 | return E_CELL_INVALID; |
78 | 0 | } |
79 | 0 | if ((originResDigit == K_AXES_DIGIT || |
80 | 0 | destinationResDigit == K_AXES_DIGIT) && |
81 | 0 | H3_EXPORT(isPentagon)(originParent)) { |
82 | | // If these are invalid cells, fail rather than incorrectly |
83 | | // reporting neighbors. For pentagon cells that are actually |
84 | | // neighbors across the deleted subsequence, they will fail the |
85 | | // optimized check below, but they will be accepted by the |
86 | | // gridDisk check below that. |
87 | 0 | return E_CELL_INVALID; |
88 | 0 | } |
89 | 0 | if (originResDigit == CENTER_DIGIT || |
90 | 0 | destinationResDigit == CENTER_DIGIT) { |
91 | 0 | *out = 1; |
92 | 0 | return E_SUCCESS; |
93 | 0 | } |
94 | | // These sets are the relevant neighbors in the clockwise |
95 | | // and counter-clockwise |
96 | 0 | const Direction neighborSetClockwise[] = { |
97 | 0 | CENTER_DIGIT, JK_AXES_DIGIT, IJ_AXES_DIGIT, J_AXES_DIGIT, |
98 | 0 | IK_AXES_DIGIT, K_AXES_DIGIT, I_AXES_DIGIT}; |
99 | 0 | const Direction neighborSetCounterclockwise[] = { |
100 | 0 | CENTER_DIGIT, IK_AXES_DIGIT, JK_AXES_DIGIT, K_AXES_DIGIT, |
101 | 0 | IJ_AXES_DIGIT, I_AXES_DIGIT, J_AXES_DIGIT}; |
102 | 0 | if (neighborSetClockwise[originResDigit] == destinationResDigit || |
103 | 0 | neighborSetCounterclockwise[originResDigit] == |
104 | 0 | destinationResDigit) { |
105 | 0 | *out = 1; |
106 | 0 | return E_SUCCESS; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | // Otherwise, we have to determine the neighbor relationship the "hard" way. |
112 | 0 | H3Index neighborRing[7] = {0}; |
113 | 0 | H3_EXPORT(gridDisk)(origin, 1, neighborRing); |
114 | 0 | for (int i = 0; i < 7; i++) { |
115 | 0 | if (neighborRing[i] == destination) { |
116 | 0 | *out = 1; |
117 | 0 | return E_SUCCESS; |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | // Made it here, they definitely aren't neighbors |
122 | 0 | *out = 0; |
123 | 0 | return E_SUCCESS; |
124 | 0 | } |
125 | | |
126 | | /** |
127 | | * Returns a directed edge H3 index based on the provided origin and |
128 | | * destination |
129 | | * @param origin The origin H3 hexagon index |
130 | | * @param destination The destination H3 hexagon index |
131 | | * @param out Output: The directed edge H3Index. |
132 | | */ |
133 | | H3Error H3_EXPORT(cellsToDirectedEdge)(H3Index origin, H3Index destination, |
134 | 0 | H3Index *out) { |
135 | | // Determine the IJK direction from the origin to the destination |
136 | 0 | Direction direction = directionForNeighbor(origin, destination); |
137 | | |
138 | | // The direction will be invalid if the cells are not neighbors |
139 | 0 | if (direction == INVALID_DIGIT) { |
140 | 0 | return E_NOT_NEIGHBORS; |
141 | 0 | } |
142 | | |
143 | | // Create the edge index for the neighbor direction |
144 | 0 | H3Index output = origin; |
145 | 0 | H3_SET_MODE(output, H3_DIRECTEDEDGE_MODE); |
146 | 0 | H3_SET_RESERVED_BITS(output, direction); |
147 | |
|
148 | 0 | *out = output; |
149 | 0 | return E_SUCCESS; |
150 | 0 | } |
151 | | |
152 | | /** |
153 | | * Returns the origin hexagon from the directed edge H3Index |
154 | | * @param edge The edge H3 index |
155 | | * @param out Output: The origin H3 hexagon index |
156 | | */ |
157 | 0 | H3Error H3_EXPORT(getDirectedEdgeOrigin)(H3Index edge, H3Index *out) { |
158 | 0 | if (H3_GET_MODE(edge) != H3_DIRECTEDEDGE_MODE) { |
159 | 0 | return E_DIR_EDGE_INVALID; |
160 | 0 | } |
161 | 0 | H3Index origin = edge; |
162 | 0 | H3_SET_MODE(origin, H3_CELL_MODE); |
163 | 0 | H3_SET_RESERVED_BITS(origin, 0); |
164 | 0 | *out = origin; |
165 | 0 | return E_SUCCESS; |
166 | 0 | } |
167 | | |
168 | | /** |
169 | | * Returns the destination hexagon from the directed edge H3Index |
170 | | * @param edge The edge H3 index |
171 | | * @param out Output: The destination H3 hexagon index |
172 | | */ |
173 | 0 | H3Error H3_EXPORT(getDirectedEdgeDestination)(H3Index edge, H3Index *out) { |
174 | 0 | Direction direction = H3_GET_RESERVED_BITS(edge); |
175 | 0 | int rotations = 0; |
176 | 0 | H3Index origin; |
177 | | // Note: This call is also checking for H3_DIRECTEDEDGE_MODE |
178 | 0 | H3Error originResult = H3_EXPORT(getDirectedEdgeOrigin)(edge, &origin); |
179 | 0 | if (originResult) { |
180 | 0 | return originResult; |
181 | 0 | } |
182 | 0 | return h3NeighborRotations(origin, direction, &rotations, out); |
183 | 0 | } |
184 | | |
185 | | /** |
186 | | * Determines if the provided H3Index is a valid directed edge index |
187 | | * @param edge The directed edge H3Index |
188 | | * @return 1 if it is a directed edge H3Index, otherwise 0. |
189 | | */ |
190 | 0 | int H3_EXPORT(isValidDirectedEdge)(H3Index edge) { |
191 | 0 | Direction neighborDirection = H3_GET_RESERVED_BITS(edge); |
192 | 0 | if (neighborDirection <= CENTER_DIGIT || neighborDirection >= NUM_DIGITS) { |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | 0 | H3Index origin; |
197 | | // Note: This call is also checking for H3_DIRECTEDEDGE_MODE |
198 | 0 | H3Error originResult = H3_EXPORT(getDirectedEdgeOrigin)(edge, &origin); |
199 | 0 | if (originResult) { |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | if (H3_EXPORT(isPentagon)(origin) && neighborDirection == K_AXES_DIGIT) { |
203 | 0 | return 0; |
204 | 0 | } |
205 | | |
206 | 0 | return H3_EXPORT(isValidCell)(origin); |
207 | 0 | } |
208 | | |
209 | | /** |
210 | | * Returns the origin, destination pair of hexagon IDs for the given edge ID |
211 | | * @param edge The directed edge H3Index |
212 | | * @param originDestination Pointer to memory to store origin and destination |
213 | | * IDs |
214 | | */ |
215 | | H3Error H3_EXPORT(directedEdgeToCells)(H3Index edge, |
216 | 0 | H3Index *originDestination) { |
217 | 0 | H3Error originResult = |
218 | 0 | H3_EXPORT(getDirectedEdgeOrigin)(edge, &originDestination[0]); |
219 | 0 | if (originResult) { |
220 | 0 | return originResult; |
221 | 0 | } |
222 | 0 | H3Error destinationResult = |
223 | 0 | H3_EXPORT(getDirectedEdgeDestination)(edge, &originDestination[1]); |
224 | 0 | if (destinationResult) { |
225 | 0 | return destinationResult; |
226 | 0 | } |
227 | 0 | return E_SUCCESS; |
228 | 0 | } |
229 | | |
230 | | /** |
231 | | * Provides all of the directed edges from the current H3Index. |
232 | | * @param origin The origin hexagon H3Index to find edges for. |
233 | | * @param edges The memory to store all of the edges inside. |
234 | | */ |
235 | 0 | H3Error H3_EXPORT(originToDirectedEdges)(H3Index origin, H3Index *edges) { |
236 | | // Determine if the origin is a pentagon and special treatment needed. |
237 | 0 | int isPent = H3_EXPORT(isPentagon)(origin); |
238 | | |
239 | | // This is actually quite simple. Just modify the bits of the origin |
240 | | // slightly for each direction, except the 'k' direction in pentagons, |
241 | | // which is zeroed. |
242 | 0 | for (int i = 0; i < 6; i++) { |
243 | 0 | if (isPent && i == 0) { |
244 | 0 | edges[i] = H3_NULL; |
245 | 0 | } else { |
246 | 0 | edges[i] = origin; |
247 | 0 | H3_SET_MODE(edges[i], H3_DIRECTEDEDGE_MODE); |
248 | 0 | H3_SET_RESERVED_BITS(edges[i], i + 1); |
249 | 0 | } |
250 | 0 | } |
251 | 0 | return E_SUCCESS; |
252 | 0 | } |
253 | | |
254 | | /** |
255 | | * Provides the coordinates defining the directed edge. |
256 | | * @param edge The directed edge H3Index |
257 | | * @param cb The cellboundary object to store the edge coordinates. |
258 | | */ |
259 | 0 | H3Error H3_EXPORT(directedEdgeToBoundary)(H3Index edge, CellBoundary *cb) { |
260 | | // Get the origin and neighbor direction from the edge |
261 | 0 | Direction direction = H3_GET_RESERVED_BITS(edge); |
262 | 0 | H3Index origin; |
263 | 0 | H3Error originResult = H3_EXPORT(getDirectedEdgeOrigin)(edge, &origin); |
264 | 0 | if (originResult) { |
265 | 0 | return originResult; |
266 | 0 | } |
267 | | |
268 | | // Get the start vertex for the edge |
269 | 0 | int startVertex = vertexNumForDirection(origin, direction); |
270 | 0 | if (startVertex == INVALID_VERTEX_NUM) { |
271 | | // This is not actually an edge (i.e. no valid direction), |
272 | | // so return no vertices. |
273 | 0 | cb->numVerts = 0; |
274 | 0 | return E_DIR_EDGE_INVALID; |
275 | 0 | } |
276 | | |
277 | | // Get the geo boundary for the appropriate vertexes of the origin. Note |
278 | | // that while there are always 2 topological vertexes per edge, the |
279 | | // resulting edge boundary may have an additional distortion vertex if it |
280 | | // crosses an edge of the icosahedron. |
281 | 0 | FaceIJK fijk; |
282 | 0 | H3Error fijkResult = _h3ToFaceIjk(origin, &fijk); |
283 | 0 | if (NEVER(fijkResult)) { |
284 | 0 | return fijkResult; |
285 | 0 | } |
286 | 0 | int res = H3_GET_RESOLUTION(origin); |
287 | 0 | int isPent = H3_EXPORT(isPentagon)(origin); |
288 | |
|
289 | 0 | if (isPent) { |
290 | 0 | _faceIjkPentToCellBoundary(&fijk, res, startVertex, 2, cb); |
291 | 0 | } else { |
292 | 0 | _faceIjkToCellBoundary(&fijk, res, startVertex, 2, cb); |
293 | 0 | } |
294 | 0 | return E_SUCCESS; |
295 | 0 | } |
296 | | |
297 | | /** |
298 | | * Returns the directed edge with origin and destination cells reversed. |
299 | | * @param edge The H3 directed edge index |
300 | | * @param out Output: The reversed directed edge index |
301 | | */ |
302 | 0 | H3Error H3_EXPORT(reverseDirectedEdge)(H3Index edge, H3Index *out) { |
303 | 0 | H3Index origin, destination; |
304 | 0 | H3Error err; |
305 | |
|
306 | 0 | err = H3_EXPORT(getDirectedEdgeOrigin)(edge, &origin); |
307 | 0 | if (err) return err; |
308 | 0 | err = H3_EXPORT(getDirectedEdgeDestination)(edge, &destination); |
309 | 0 | if (err) return err; |
310 | 0 | err = H3_EXPORT(cellsToDirectedEdge)(destination, origin, out); |
311 | 0 | if (err) return err; |
312 | | |
313 | 0 | return E_SUCCESS; |
314 | 0 | } |