/src/h3/src/h3lib/lib/coordijk.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2018, 2020-2023 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.c |
17 | | * @brief Hex IJK coordinate systems functions including conversions to/from |
18 | | * lat/lng. |
19 | | */ |
20 | | |
21 | | #include "coordijk.h" |
22 | | |
23 | | #include <math.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | #include "constants.h" |
29 | | #include "h3Assert.h" |
30 | | #include "latLng.h" |
31 | | #include "mathExtensions.h" |
32 | | |
33 | 0 | #define INT32_MAX_3 (INT32_MAX / 3) |
34 | | |
35 | | /** |
36 | | * Sets an IJK coordinate to the specified component values. |
37 | | * |
38 | | * @param ijk The IJK coordinate to set. |
39 | | * @param i The desired i component value. |
40 | | * @param j The desired j component value. |
41 | | * @param k The desired k component value. |
42 | | */ |
43 | 35 | void _setIJK(CoordIJK *ijk, int i, int j, int k) { |
44 | 35 | ijk->i = i; |
45 | 35 | ijk->j = j; |
46 | 35 | ijk->k = k; |
47 | 35 | } |
48 | | |
49 | | /** |
50 | | * Determine the containing hex in ijk+ coordinates for a 2D cartesian |
51 | | * coordinate vector (from DGGRID). |
52 | | * |
53 | | * @param v The 2D cartesian coordinate vector. |
54 | | * @param h The ijk+ coordinates of the containing hex. |
55 | | */ |
56 | 0 | void _hex2dToCoordIJK(const Vec2d *v, CoordIJK *h) { |
57 | 0 | double a1, a2; |
58 | 0 | double x1, x2; |
59 | 0 | int m1, m2; |
60 | 0 | double r1, r2; |
61 | | |
62 | | // quantize into the ij system and then normalize |
63 | 0 | h->k = 0; |
64 | |
|
65 | 0 | a1 = fabsl(v->x); |
66 | 0 | a2 = fabsl(v->y); |
67 | | |
68 | | // first do a reverse conversion |
69 | 0 | x2 = a2 * M_RSIN60; |
70 | 0 | x1 = a1 + x2 / 2.0; |
71 | | |
72 | | // check if we have the center of a hex |
73 | 0 | m1 = (int)x1; |
74 | 0 | m2 = (int)x2; |
75 | | |
76 | | // otherwise round correctly |
77 | 0 | r1 = x1 - m1; |
78 | 0 | r2 = x2 - m2; |
79 | |
|
80 | 0 | if (r1 < 0.5) { |
81 | 0 | if (r1 < 1.0 / 3.0) { |
82 | 0 | if (r2 < (1.0 + r1) / 2.0) { |
83 | 0 | h->i = m1; |
84 | 0 | h->j = m2; |
85 | 0 | } else { |
86 | 0 | h->i = m1; |
87 | 0 | h->j = m2 + 1; |
88 | 0 | } |
89 | 0 | } else { |
90 | 0 | if (r2 < (1.0 - r1)) { |
91 | 0 | h->j = m2; |
92 | 0 | } else { |
93 | 0 | h->j = m2 + 1; |
94 | 0 | } |
95 | |
|
96 | 0 | if ((1.0 - r1) <= r2 && r2 < (2.0 * r1)) { |
97 | 0 | h->i = m1 + 1; |
98 | 0 | } else { |
99 | 0 | h->i = m1; |
100 | 0 | } |
101 | 0 | } |
102 | 0 | } else { |
103 | 0 | if (r1 < 2.0 / 3.0) { |
104 | 0 | if (r2 < (1.0 - r1)) { |
105 | 0 | h->j = m2; |
106 | 0 | } else { |
107 | 0 | h->j = m2 + 1; |
108 | 0 | } |
109 | |
|
110 | 0 | if ((2.0 * r1 - 1.0) < r2 && r2 < (1.0 - r1)) { |
111 | 0 | h->i = m1; |
112 | 0 | } else { |
113 | 0 | h->i = m1 + 1; |
114 | 0 | } |
115 | 0 | } else { |
116 | 0 | if (r2 < (r1 / 2.0)) { |
117 | 0 | h->i = m1 + 1; |
118 | 0 | h->j = m2; |
119 | 0 | } else { |
120 | 0 | h->i = m1 + 1; |
121 | 0 | h->j = m2 + 1; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | | // now fold across the axes if necessary |
127 | |
|
128 | 0 | if (v->x < 0.0) { |
129 | 0 | if ((h->j % 2) == 0) // even |
130 | 0 | { |
131 | 0 | long long int axisi = h->j / 2; |
132 | 0 | long long int diff = h->i - axisi; |
133 | 0 | h->i = (int)(h->i - 2.0 * diff); |
134 | 0 | } else { |
135 | 0 | long long int axisi = (h->j + 1) / 2; |
136 | 0 | long long int diff = h->i - axisi; |
137 | 0 | h->i = (int)(h->i - (2.0 * diff + 1)); |
138 | 0 | } |
139 | 0 | } |
140 | |
|
141 | 0 | if (v->y < 0.0) { |
142 | 0 | h->i = h->i - (2 * h->j + 1) / 2; |
143 | 0 | h->j = -1 * h->j; |
144 | 0 | } |
145 | |
|
146 | 0 | _ijkNormalize(h); |
147 | 0 | } |
148 | | |
149 | | /** |
150 | | * Find the center point in 2D cartesian coordinates of a hex. |
151 | | * |
152 | | * @param h The ijk coordinates of the hex. |
153 | | * @param v The 2D cartesian coordinates of the hex center point. |
154 | | */ |
155 | 0 | void _ijkToHex2d(const CoordIJK *h, Vec2d *v) { |
156 | 0 | int i = h->i - h->k; |
157 | 0 | int j = h->j - h->k; |
158 | |
|
159 | 0 | v->x = i - 0.5 * j; |
160 | 0 | v->y = j * M_SQRT3_2; |
161 | 0 | } |
162 | | |
163 | | /** |
164 | | * Returns whether or not two ijk coordinates contain exactly the same |
165 | | * component values. |
166 | | * |
167 | | * @param c1 The first set of ijk coordinates. |
168 | | * @param c2 The second set of ijk coordinates. |
169 | | * @return 1 if the two addresses match, 0 if they do not. |
170 | | */ |
171 | 0 | int _ijkMatches(const CoordIJK *c1, const CoordIJK *c2) { |
172 | 0 | return (c1->i == c2->i && c1->j == c2->j && c1->k == c2->k); |
173 | 0 | } |
174 | | |
175 | | /** |
176 | | * Add two ijk coordinates. |
177 | | * |
178 | | * @param h1 The first set of ijk coordinates. |
179 | | * @param h2 The second set of ijk coordinates. |
180 | | * @param sum The sum of the two sets of ijk coordinates. |
181 | | */ |
182 | 17.6k | void _ijkAdd(const CoordIJK *h1, const CoordIJK *h2, CoordIJK *sum) { |
183 | 17.6k | sum->i = h1->i + h2->i; |
184 | 17.6k | sum->j = h1->j + h2->j; |
185 | 17.6k | sum->k = h1->k + h2->k; |
186 | 17.6k | } |
187 | | |
188 | | /** |
189 | | * Subtract two ijk coordinates. |
190 | | * |
191 | | * @param h1 The first set of ijk coordinates. |
192 | | * @param h2 The second set of ijk coordinates. |
193 | | * @param diff The difference of the two sets of ijk coordinates (h1 - h2). |
194 | | */ |
195 | 35 | void _ijkSub(const CoordIJK *h1, const CoordIJK *h2, CoordIJK *diff) { |
196 | 35 | diff->i = h1->i - h2->i; |
197 | 35 | diff->j = h1->j - h2->j; |
198 | 35 | diff->k = h1->k - h2->k; |
199 | 35 | } |
200 | | |
201 | | /** |
202 | | * Uniformly scale ijk coordinates by a scalar. Works in place. |
203 | | * |
204 | | * @param c The ijk coordinates to scale. |
205 | | * @param factor The scaling factor. |
206 | | */ |
207 | 19.7k | void _ijkScale(CoordIJK *c, int factor) { |
208 | 19.7k | c->i *= factor; |
209 | 19.7k | c->j *= factor; |
210 | 19.7k | c->k *= factor; |
211 | 19.7k | } |
212 | | |
213 | | /** |
214 | | * Returns true if _ijkNormalize with the given input could have a signed |
215 | | * integer overflow. Assumes k is set to 0. |
216 | | */ |
217 | 0 | bool _ijkNormalizeCouldOverflow(const CoordIJK *ijk) { |
218 | | // Check for the possibility of overflow |
219 | 0 | int max, min; |
220 | 0 | if (ijk->i > ijk->j) { |
221 | 0 | max = ijk->i; |
222 | 0 | min = ijk->j; |
223 | 0 | } else { |
224 | 0 | max = ijk->j; |
225 | 0 | min = ijk->i; |
226 | 0 | } |
227 | 0 | if (min < 0) { |
228 | | // Only if the min is less than 0 will the resulting number be larger |
229 | | // than max. If min is positive, then max is also positive, and a |
230 | | // positive signed integer minus another positive signed integer will |
231 | | // not overflow. |
232 | 0 | if (ADD_INT32S_OVERFLOWS(max, min)) { |
233 | | // max + min would overflow |
234 | 0 | return true; |
235 | 0 | } |
236 | 0 | if (SUB_INT32S_OVERFLOWS(0, min)) { |
237 | | // 0 - INT32_MIN would overflow |
238 | 0 | return true; |
239 | 0 | } |
240 | 0 | if (SUB_INT32S_OVERFLOWS(max, min)) { |
241 | | // max - min would overflow |
242 | 0 | return true; |
243 | 0 | } |
244 | 0 | } |
245 | 0 | return false; |
246 | 0 | } |
247 | | |
248 | | /** |
249 | | * Normalizes ijk coordinates by setting the components to the smallest possible |
250 | | * values. Works in place. |
251 | | * |
252 | | * This function does not protect against signed integer overflow. The caller |
253 | | * must ensure that none of (i - j), (i - k), (j - i), (j - k), (k - i), (k - j) |
254 | | * will overflow. This function may be changed in the future to make that check |
255 | | * itself and return an error code. |
256 | | * |
257 | | * @param c The ijk coordinates to normalize. |
258 | | */ |
259 | 11.4k | void _ijkNormalize(CoordIJK *c) { |
260 | | // remove any negative values |
261 | 11.4k | if (c->i < 0) { |
262 | 107 | c->j -= c->i; |
263 | 107 | c->k -= c->i; |
264 | 107 | c->i = 0; |
265 | 107 | } |
266 | | |
267 | 11.4k | if (c->j < 0) { |
268 | 56 | c->i -= c->j; |
269 | 56 | c->k -= c->j; |
270 | 56 | c->j = 0; |
271 | 56 | } |
272 | | |
273 | 11.4k | if (c->k < 0) { |
274 | 0 | c->i -= c->k; |
275 | 0 | c->j -= c->k; |
276 | 0 | c->k = 0; |
277 | 0 | } |
278 | | |
279 | | // remove the min value if needed |
280 | 11.4k | int min = c->i; |
281 | 11.4k | if (c->j < min) min = c->j; |
282 | 11.4k | if (c->k < min) min = c->k; |
283 | 11.4k | if (min > 0) { |
284 | 8.66k | c->i -= min; |
285 | 8.66k | c->j -= min; |
286 | 8.66k | c->k -= min; |
287 | 8.66k | } |
288 | 11.4k | } |
289 | | |
290 | | /** |
291 | | * Determines the H3 digit corresponding to a unit vector or the zero vector |
292 | | * in ijk coordinates. |
293 | | * |
294 | | * @param ijk The ijk coordinates; must be a unit vector or zero vector. |
295 | | * @return The H3 digit (0-6) corresponding to the ijk unit vector, zero vector, |
296 | | * or INVALID_DIGIT (7) on failure. |
297 | | */ |
298 | 0 | Direction _unitIjkToDigit(const CoordIJK *ijk) { |
299 | 0 | CoordIJK c = *ijk; |
300 | 0 | _ijkNormalize(&c); |
301 | |
|
302 | 0 | Direction digit = INVALID_DIGIT; |
303 | 0 | for (Direction i = CENTER_DIGIT; i < NUM_DIGITS; i++) { |
304 | 0 | if (_ijkMatches(&c, &UNIT_VECS[i])) { |
305 | 0 | digit = i; |
306 | 0 | break; |
307 | 0 | } |
308 | 0 | } |
309 | |
|
310 | 0 | return digit; |
311 | 0 | } |
312 | | |
313 | | /** |
314 | | * Returns non-zero if _upAp7 with the given input could have a signed integer |
315 | | * overflow. |
316 | | * |
317 | | * Assumes ijk is IJK+ coordinates (no negative numbers). |
318 | | */ |
319 | 0 | H3Error _upAp7Checked(CoordIJK *ijk) { |
320 | | // Doesn't need to be checked because i, j, and k must all be non-negative |
321 | 0 | int i = ijk->i - ijk->k; |
322 | 0 | int j = ijk->j - ijk->k; |
323 | | |
324 | | // <0 is checked because the input must all be non-negative, but some |
325 | | // negative inputs are used in unit tests to exercise the below. |
326 | 0 | if (i >= INT32_MAX_3 || j >= INT32_MAX_3 || i < 0 || j < 0) { |
327 | 0 | if (ADD_INT32S_OVERFLOWS(i, i)) { |
328 | 0 | return E_FAILED; |
329 | 0 | } |
330 | 0 | int i2 = i + i; |
331 | 0 | if (ADD_INT32S_OVERFLOWS(i2, i)) { |
332 | 0 | return E_FAILED; |
333 | 0 | } |
334 | 0 | int i3 = i2 + i; |
335 | 0 | if (ADD_INT32S_OVERFLOWS(j, j)) { |
336 | 0 | return E_FAILED; |
337 | 0 | } |
338 | 0 | int j2 = j + j; |
339 | |
|
340 | 0 | if (SUB_INT32S_OVERFLOWS(i3, j)) { |
341 | 0 | return E_FAILED; |
342 | 0 | } |
343 | 0 | if (ADD_INT32S_OVERFLOWS(i, j2)) { |
344 | 0 | return E_FAILED; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | ijk->i = (int)lround(((i * 3) - j) * M_ONESEVENTH); |
349 | 0 | ijk->j = (int)lround((i + (j * 2)) * M_ONESEVENTH); |
350 | 0 | ijk->k = 0; |
351 | | |
352 | | // Expected not to be reachable, because max + min or max - min would need |
353 | | // to overflow. |
354 | 0 | if (NEVER(_ijkNormalizeCouldOverflow(ijk))) { |
355 | 0 | return E_FAILED; |
356 | 0 | } |
357 | 0 | _ijkNormalize(ijk); |
358 | 0 | return E_SUCCESS; |
359 | 0 | } |
360 | | |
361 | | /** |
362 | | * Returns non-zero if _upAp7r with the given input could have a signed integer |
363 | | * overflow. |
364 | | * |
365 | | * Assumes ijk is IJK+ coordinates (no negative numbers). |
366 | | */ |
367 | 0 | H3Error _upAp7rChecked(CoordIJK *ijk) { |
368 | | // Doesn't need to be checked because i, j, and k must all be non-negative |
369 | 0 | int i = ijk->i - ijk->k; |
370 | 0 | int j = ijk->j - ijk->k; |
371 | | |
372 | | // <0 is checked because the input must all be non-negative, but some |
373 | | // negative inputs are used in unit tests to exercise the below. |
374 | 0 | if (i >= INT32_MAX_3 || j >= INT32_MAX_3 || i < 0 || j < 0) { |
375 | 0 | if (ADD_INT32S_OVERFLOWS(i, i)) { |
376 | 0 | return E_FAILED; |
377 | 0 | } |
378 | 0 | int i2 = i + i; |
379 | 0 | if (ADD_INT32S_OVERFLOWS(j, j)) { |
380 | 0 | return E_FAILED; |
381 | 0 | } |
382 | 0 | int j2 = j + j; |
383 | 0 | if (ADD_INT32S_OVERFLOWS(j2, j)) { |
384 | 0 | return E_FAILED; |
385 | 0 | } |
386 | 0 | int j3 = j2 + j; |
387 | |
|
388 | 0 | if (ADD_INT32S_OVERFLOWS(i2, j)) { |
389 | 0 | return E_FAILED; |
390 | 0 | } |
391 | 0 | if (SUB_INT32S_OVERFLOWS(j3, i)) { |
392 | 0 | return E_FAILED; |
393 | 0 | } |
394 | 0 | } |
395 | | |
396 | 0 | ijk->i = (int)lround(((i * 2) + j) * M_ONESEVENTH); |
397 | 0 | ijk->j = (int)lround(((j * 3) - i) * M_ONESEVENTH); |
398 | 0 | ijk->k = 0; |
399 | | |
400 | | // Expected not to be reachable, because max + min or max - min would need |
401 | | // to overflow. |
402 | 0 | if (NEVER(_ijkNormalizeCouldOverflow(ijk))) { |
403 | 0 | return E_FAILED; |
404 | 0 | } |
405 | 0 | _ijkNormalize(ijk); |
406 | 0 | return E_SUCCESS; |
407 | 0 | } |
408 | | |
409 | | /** |
410 | | * Find the normalized ijk coordinates of the indexing parent of a cell in a |
411 | | * counter-clockwise aperture 7 grid. Works in place. |
412 | | * |
413 | | * @param ijk The ijk coordinates. |
414 | | */ |
415 | 0 | void _upAp7(CoordIJK *ijk) { |
416 | | // convert to CoordIJ |
417 | 0 | int i = ijk->i - ijk->k; |
418 | 0 | int j = ijk->j - ijk->k; |
419 | |
|
420 | 0 | ijk->i = (int)lround((3 * i - j) * M_ONESEVENTH); |
421 | 0 | ijk->j = (int)lround((i + 2 * j) * M_ONESEVENTH); |
422 | 0 | ijk->k = 0; |
423 | 0 | _ijkNormalize(ijk); |
424 | 0 | } |
425 | | |
426 | | /** |
427 | | * Find the normalized ijk coordinates of the indexing parent of a cell in a |
428 | | * clockwise aperture 7 grid. Works in place. |
429 | | * |
430 | | * @param ijk The ijk coordinates. |
431 | | */ |
432 | 223 | void _upAp7r(CoordIJK *ijk) { |
433 | | // convert to CoordIJ |
434 | 223 | int i = ijk->i - ijk->k; |
435 | 223 | int j = ijk->j - ijk->k; |
436 | | |
437 | 223 | ijk->i = (int)lround((2 * i + j) * M_ONESEVENTH); |
438 | 223 | ijk->j = (int)lround((3 * j - i) * M_ONESEVENTH); |
439 | 223 | ijk->k = 0; |
440 | 223 | _ijkNormalize(ijk); |
441 | 223 | } |
442 | | |
443 | | /** |
444 | | * Find the normalized ijk coordinates of the hex centered on the indicated |
445 | | * hex at the next finer aperture 7 counter-clockwise resolution. Works in |
446 | | * place. |
447 | | * |
448 | | * @param ijk The ijk coordinates. |
449 | | */ |
450 | 1.90k | void _downAp7(CoordIJK *ijk) { |
451 | | // res r unit vectors in res r+1 |
452 | 1.90k | CoordIJK iVec = {3, 0, 1}; |
453 | 1.90k | CoordIJK jVec = {1, 3, 0}; |
454 | 1.90k | CoordIJK kVec = {0, 1, 3}; |
455 | | |
456 | 1.90k | _ijkScale(&iVec, ijk->i); |
457 | 1.90k | _ijkScale(&jVec, ijk->j); |
458 | 1.90k | _ijkScale(&kVec, ijk->k); |
459 | | |
460 | 1.90k | _ijkAdd(&iVec, &jVec, ijk); |
461 | 1.90k | _ijkAdd(ijk, &kVec, ijk); |
462 | | |
463 | 1.90k | _ijkNormalize(ijk); |
464 | 1.90k | } |
465 | | |
466 | | /** |
467 | | * Find the normalized ijk coordinates of the hex centered on the indicated |
468 | | * hex at the next finer aperture 7 clockwise resolution. Works in place. |
469 | | * |
470 | | * @param ijk The ijk coordinates. |
471 | | */ |
472 | 2.18k | void _downAp7r(CoordIJK *ijk) { |
473 | | // res r unit vectors in res r+1 |
474 | 2.18k | CoordIJK iVec = {3, 1, 0}; |
475 | 2.18k | CoordIJK jVec = {0, 3, 1}; |
476 | 2.18k | CoordIJK kVec = {1, 0, 3}; |
477 | | |
478 | 2.18k | _ijkScale(&iVec, ijk->i); |
479 | 2.18k | _ijkScale(&jVec, ijk->j); |
480 | 2.18k | _ijkScale(&kVec, ijk->k); |
481 | | |
482 | 2.18k | _ijkAdd(&iVec, &jVec, ijk); |
483 | 2.18k | _ijkAdd(ijk, &kVec, ijk); |
484 | | |
485 | 2.18k | _ijkNormalize(ijk); |
486 | 2.18k | } |
487 | | |
488 | | /** |
489 | | * Find the normalized ijk coordinates of the hex in the specified digit |
490 | | * direction from the specified ijk coordinates. Works in place. |
491 | | * |
492 | | * @param ijk The ijk coordinates. |
493 | | * @param digit The digit direction from the original ijk coordinates. |
494 | | */ |
495 | 3.52k | void _neighbor(CoordIJK *ijk, Direction digit) { |
496 | 3.52k | if (digit > CENTER_DIGIT && digit < NUM_DIGITS) { |
497 | 2.23k | _ijkAdd(ijk, &UNIT_VECS[digit], ijk); |
498 | 2.23k | _ijkNormalize(ijk); |
499 | 2.23k | } |
500 | 3.52k | } |
501 | | |
502 | | /** |
503 | | * Rotates ijk coordinates 60 degrees counter-clockwise. Works in place. |
504 | | * |
505 | | * @param ijk The ijk coordinates. |
506 | | */ |
507 | 1.60k | void _ijkRotate60ccw(CoordIJK *ijk) { |
508 | | // unit vector rotations |
509 | 1.60k | CoordIJK iVec = {1, 1, 0}; |
510 | 1.60k | CoordIJK jVec = {0, 1, 1}; |
511 | 1.60k | CoordIJK kVec = {1, 0, 1}; |
512 | | |
513 | 1.60k | _ijkScale(&iVec, ijk->i); |
514 | 1.60k | _ijkScale(&jVec, ijk->j); |
515 | 1.60k | _ijkScale(&kVec, ijk->k); |
516 | | |
517 | 1.60k | _ijkAdd(&iVec, &jVec, ijk); |
518 | 1.60k | _ijkAdd(ijk, &kVec, ijk); |
519 | | |
520 | 1.60k | _ijkNormalize(ijk); |
521 | 1.60k | } |
522 | | |
523 | | /** |
524 | | * Rotates ijk coordinates 60 degrees clockwise. Works in place. |
525 | | * |
526 | | * @param ijk The ijk coordinates. |
527 | | */ |
528 | 35 | void _ijkRotate60cw(CoordIJK *ijk) { |
529 | | // unit vector rotations |
530 | 35 | CoordIJK iVec = {1, 0, 1}; |
531 | 35 | CoordIJK jVec = {1, 1, 0}; |
532 | 35 | CoordIJK kVec = {0, 1, 1}; |
533 | | |
534 | 35 | _ijkScale(&iVec, ijk->i); |
535 | 35 | _ijkScale(&jVec, ijk->j); |
536 | 35 | _ijkScale(&kVec, ijk->k); |
537 | | |
538 | 35 | _ijkAdd(&iVec, &jVec, ijk); |
539 | 35 | _ijkAdd(ijk, &kVec, ijk); |
540 | | |
541 | 35 | _ijkNormalize(ijk); |
542 | 35 | } |
543 | | |
544 | | /** |
545 | | * Rotates indexing digit 60 degrees counter-clockwise. Returns result. |
546 | | * |
547 | | * @param digit Indexing digit (between 1 and 6 inclusive) |
548 | | */ |
549 | 0 | Direction _rotate60ccw(Direction digit) { |
550 | 0 | switch (digit) { |
551 | 0 | case K_AXES_DIGIT: |
552 | 0 | return IK_AXES_DIGIT; |
553 | 0 | case IK_AXES_DIGIT: |
554 | 0 | return I_AXES_DIGIT; |
555 | 0 | case I_AXES_DIGIT: |
556 | 0 | return IJ_AXES_DIGIT; |
557 | 0 | case IJ_AXES_DIGIT: |
558 | 0 | return J_AXES_DIGIT; |
559 | 0 | case J_AXES_DIGIT: |
560 | 0 | return JK_AXES_DIGIT; |
561 | 0 | case JK_AXES_DIGIT: |
562 | 0 | return K_AXES_DIGIT; |
563 | 0 | default: |
564 | 0 | return digit; |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | | /** |
569 | | * Rotates indexing digit 60 degrees clockwise. Returns result. |
570 | | * |
571 | | * @param digit Indexing digit (between 1 and 6 inclusive) |
572 | | */ |
573 | 329 | Direction _rotate60cw(Direction digit) { |
574 | 329 | switch (digit) { |
575 | 21 | case K_AXES_DIGIT: |
576 | 21 | return JK_AXES_DIGIT; |
577 | 37 | case JK_AXES_DIGIT: |
578 | 37 | return J_AXES_DIGIT; |
579 | 27 | case J_AXES_DIGIT: |
580 | 27 | return IJ_AXES_DIGIT; |
581 | 31 | case IJ_AXES_DIGIT: |
582 | 31 | return I_AXES_DIGIT; |
583 | 18 | case I_AXES_DIGIT: |
584 | 18 | return IK_AXES_DIGIT; |
585 | 69 | case IK_AXES_DIGIT: |
586 | 69 | return K_AXES_DIGIT; |
587 | 126 | default: |
588 | 126 | return digit; |
589 | 329 | } |
590 | 329 | } |
591 | | |
592 | | /** |
593 | | * Find the normalized ijk coordinates of the hex centered on the indicated |
594 | | * hex at the next finer aperture 3 counter-clockwise resolution. Works in |
595 | | * place. |
596 | | * |
597 | | * @param ijk The ijk coordinates. |
598 | | */ |
599 | 344 | void _downAp3(CoordIJK *ijk) { |
600 | | // res r unit vectors in res r+1 |
601 | 344 | CoordIJK iVec = {2, 0, 1}; |
602 | 344 | CoordIJK jVec = {1, 2, 0}; |
603 | 344 | CoordIJK kVec = {0, 1, 2}; |
604 | | |
605 | 344 | _ijkScale(&iVec, ijk->i); |
606 | 344 | _ijkScale(&jVec, ijk->j); |
607 | 344 | _ijkScale(&kVec, ijk->k); |
608 | | |
609 | 344 | _ijkAdd(&iVec, &jVec, ijk); |
610 | 344 | _ijkAdd(ijk, &kVec, ijk); |
611 | | |
612 | 344 | _ijkNormalize(ijk); |
613 | 344 | } |
614 | | |
615 | | /** |
616 | | * Find the normalized ijk coordinates of the hex centered on the indicated |
617 | | * hex at the next finer aperture 3 clockwise resolution. Works in place. |
618 | | * |
619 | | * @param ijk The ijk coordinates. |
620 | | */ |
621 | 344 | void _downAp3r(CoordIJK *ijk) { |
622 | | // res r unit vectors in res r+1 |
623 | 344 | CoordIJK iVec = {2, 1, 0}; |
624 | 344 | CoordIJK jVec = {0, 2, 1}; |
625 | 344 | CoordIJK kVec = {1, 0, 2}; |
626 | | |
627 | 344 | _ijkScale(&iVec, ijk->i); |
628 | 344 | _ijkScale(&jVec, ijk->j); |
629 | 344 | _ijkScale(&kVec, ijk->k); |
630 | | |
631 | 344 | _ijkAdd(&iVec, &jVec, ijk); |
632 | 344 | _ijkAdd(ijk, &kVec, ijk); |
633 | | |
634 | 344 | _ijkNormalize(ijk); |
635 | 344 | } |
636 | | |
637 | | /** |
638 | | * Finds the distance between the two coordinates. Returns result. |
639 | | * |
640 | | * @param c1 The first set of ijk coordinates. |
641 | | * @param c2 The second set of ijk coordinates. |
642 | | */ |
643 | 0 | int ijkDistance(const CoordIJK *c1, const CoordIJK *c2) { |
644 | 0 | CoordIJK diff; |
645 | 0 | _ijkSub(c1, c2, &diff); |
646 | 0 | _ijkNormalize(&diff); |
647 | 0 | CoordIJK absDiff = {abs(diff.i), abs(diff.j), abs(diff.k)}; |
648 | 0 | return MAX(absDiff.i, MAX(absDiff.j, absDiff.k)); |
649 | 0 | } |
650 | | |
651 | | /** |
652 | | * Transforms coordinates from the IJK+ coordinate system to the IJ coordinate |
653 | | * system. |
654 | | * |
655 | | * @param ijk The input IJK+ coordinates |
656 | | * @param ij The output IJ coordinates |
657 | | */ |
658 | 0 | void ijkToIj(const CoordIJK *ijk, CoordIJ *ij) { |
659 | 0 | ij->i = ijk->i - ijk->k; |
660 | 0 | ij->j = ijk->j - ijk->k; |
661 | 0 | } |
662 | | |
663 | | /** |
664 | | * Transforms coordinates from the IJ coordinate system to the IJK+ coordinate |
665 | | * system. |
666 | | * |
667 | | * @param ij The input IJ coordinates |
668 | | * @param ijk The output IJK+ coordinates |
669 | | * @returns E_SUCCESS on success, E_FAILED if signed integer overflow would have |
670 | | * occurred. |
671 | | */ |
672 | 0 | H3Error ijToIjk(const CoordIJ *ij, CoordIJK *ijk) { |
673 | 0 | ijk->i = ij->i; |
674 | 0 | ijk->j = ij->j; |
675 | 0 | ijk->k = 0; |
676 | |
|
677 | 0 | if (_ijkNormalizeCouldOverflow(ijk)) { |
678 | 0 | return E_FAILED; |
679 | 0 | } |
680 | | |
681 | 0 | _ijkNormalize(ijk); |
682 | 0 | return E_SUCCESS; |
683 | 0 | } |
684 | | |
685 | | /** |
686 | | * Convert IJK coordinates to cube coordinates, in place |
687 | | * @param ijk Coordinate to convert |
688 | | */ |
689 | 0 | void ijkToCube(CoordIJK *ijk) { |
690 | 0 | ijk->i = -ijk->i + ijk->k; |
691 | 0 | ijk->j = ijk->j - ijk->k; |
692 | 0 | ijk->k = -ijk->i - ijk->j; |
693 | 0 | } |
694 | | |
695 | | /** |
696 | | * Convert cube coordinates to IJK coordinates, in place |
697 | | * @param ijk Coordinate to convert |
698 | | */ |
699 | 0 | void cubeToIjk(CoordIJK *ijk) { |
700 | 0 | ijk->i = -ijk->i; |
701 | 0 | ijk->k = 0; |
702 | 0 | _ijkNormalize(ijk); |
703 | 0 | } |