-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathh3_h3Index.c
More file actions
1484 lines (1338 loc) · 51.4 KB
/
h3_h3Index.c
File metadata and controls
1484 lines (1338 loc) · 51.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2021, 2024 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 h3Index.c
* @brief H3Index utility functions
* (see h3api.h for the main library entry functions)
*/
#include "h3_h3Index.h"
#include <inttypes.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "h3_alloc.h"
#include "h3_baseCells.h"
#include "h3_directedEdge.h"
#include "h3_faceijk.h"
#include "h3_h3Assert.h"
#include "h3_iterators.h"
#include "h3_mathExtensions.h"
#include "h3_vertex.h"
// TODO: https://github.com/uber/h3/issues/984
static const bool isBaseCellPentagonArr[128] = {
[4] = 1, [14] = 1, [24] = 1, [38] = 1, [49] = 1, [58] = 1,
[63] = 1, [72] = 1, [83] = 1, [97] = 1, [107] = 1, [117] = 1};
/** @var H3ErrorDescriptions
* @brief An array of strings describing each of the H3ErrorCodes enum values
*/
static char *H3ErrorDescriptions[] = {
/* E_SUCCESS */ "Success",
/* E_FAILED */
"The operation failed but a more specific error is not available",
/* E_DOMAIN */ "Argument was outside of acceptable range",
/* E_LATLNG_DOMAIN */
"Latitude or longitude arguments were outside of acceptable range",
/* E_RES_DOMAIN */ "Resolution argument was outside of acceptable range",
/* E_CELL_INVALID */ "Cell argument was not valid",
/* E_DIR_EDGE_INVALID */ "Directed edge argument was not valid",
/* E_UNDIR_EDGE_INVALID */ "Undirected edge argument was not valid",
/* E_VERTEX_INVALID */ "Vertex argument was not valid",
/* E_PENTAGON */ "Pentagon distortion was encountered",
/* E_DUPLICATE_INPUT */ "Duplicate input",
/* E_NOT_NEIGHBORS */ "Cell arguments were not neighbors",
/* E_RES_MISMATCH */ "Cell arguments had incompatible resolutions",
/* E_MEMORY_ALLOC */ "Memory allocation failed",
/* E_MEMORY_BOUNDS */ "Bounds of provided memory were insufficient",
/* E_OPTION_INVALID */ "Mode or flags argument was not valid",
/* E_INDEX_INVALID */ "Index argument was not valid",
/* E_BASE_CELL_DOMAIN */ "Base cell number was outside of acceptable range",
/* E_DIGIT_DOMAIN */ "Child digits invalid",
/* E_DELETED_DIGIT */ "Deleted subsequence indicates invalid index"};
/**
* Returns the string describing the H3Error. This string is internally
* allocated and should not be `free`d.
* @param err The H3 error.
* @return The string describing the H3Error
*/
const char *H3_EXPORT(describeH3Error)(H3Error err) {
// err is always non-negative because it is an unsigned integer
if (err < H3_ERROR_END) {
return H3ErrorDescriptions[err];
}
return "Invalid error code";
}
/**
* Returns the H3 resolution of an H3 index.
* @param h The H3 index.
* @return The resolution of the H3 index argument.
*/
int H3_EXPORT(getResolution)(H3Index h) { return H3_GET_RESOLUTION(h); }
/**
* Returns the H3 base cell "number" of an H3 cell (hexagon or pentagon).
*
* Note: Technically works on H3 edges, but will return base cell of the
* origin cell.
*
* @param h The H3 cell.
* @return The base cell "number" of the H3 cell argument.
*/
int H3_EXPORT(getBaseCellNumber)(H3Index h) { return H3_GET_BASE_CELL(h); }
/**
* Returns the index digit at `res`, which starts with 1 for resolution
* 1, up to and including resolution 15.
*
* 0 is not a valid value for `res` because resolution 0 is specified by
* the base cell number, not an indexing digit.
*
* `res` may exceed the actual resolution of the index, in which case
* the actual digit stored in the index is returned. For valid cell indexes
* this will be 7.
*
* @param h The H3 index (e.g. cell).
* @param res Which indexing digit to retrieve, starting with 1.
* @param out Receives the value of the indexing digit.
* @return 0 (E_SUCCESS) on success, or another value otherwise.
*/
H3Error H3_EXPORT(getIndexDigit)(H3Index h, int res, int *out) {
if (res < 1 || res > MAX_H3_RES) {
return E_RES_DOMAIN;
}
*out = H3_GET_INDEX_DIGIT(h, res);
return E_SUCCESS;
}
/**
* Create a cell from its components (resolution, base cell, children digits).
* Only allows for constructing valid H3 cells.
*
* @param res 0--15
* @param baseCellNumber 0--121
* @param digits Array of child digits (0--6) of length `res`.
* NULL allowed for `res=0`.
* @param out Created cell
* @return 0 (E_SUCCESS) on success, otherwise some H3Error
**/
H3Error H3_EXPORT(constructCell)(int res, int baseCellNumber, const int *digits,
H3Index *out) {
if (res < 0 || res > MAX_H3_RES) {
return E_RES_DOMAIN;
}
if (baseCellNumber < 0 || baseCellNumber >= NUM_BASE_CELLS) {
return E_BASE_CELL_DOMAIN;
}
H3Index h = H3_INIT;
H3_SET_MODE(h, H3_CELL_MODE);
H3_SET_RESOLUTION(h, res);
H3_SET_BASE_CELL(h, baseCellNumber);
bool isPentagon = isBaseCellPentagonArr[baseCellNumber];
for (int r = 1; r <= res; r++) {
int d = digits[r - 1];
if (d < CENTER_DIGIT || d >= INVALID_DIGIT) { // (d < 0 || d >= 7)
return E_DIGIT_DOMAIN;
}
if (isPentagon) {
// check for deleted subsequences of pentagons
if (d == CENTER_DIGIT) { // d == 0
// do nothing; still a pentagon
} else if (d == K_AXES_DIGIT) { // d == 1
return E_DELETED_DIGIT;
} else {
isPentagon = false;
}
}
H3_SET_INDEX_DIGIT(h, r, d);
}
*out = h;
return E_SUCCESS;
}
/**
* Converts a string representation of an H3 index into an H3 index.
* @param str The string representation of an H3 index.
* @param out Output: The H3 index corresponding to the string argument
*/
H3Error H3_EXPORT(stringToH3)(const char *str, H3Index *out) {
H3Index h = H3_NULL;
// If failed, h will be unmodified and we should return H3_NULL anyways.
int read = sscanf(str, "%" PRIx64, &h);
if (read != 1) {
return E_FAILED;
}
*out = h;
return E_SUCCESS;
}
/**
* Converts an H3 index into a string representation.
* @param h The H3 index to convert.
* @param str The string representation of the H3 index.
* @param sz Size of the buffer `str`
*/
H3Error H3_EXPORT(h3ToString)(H3Index h, char *str, size_t sz) {
// An unsigned 64 bit integer will be expressed in at most
// 16 digits plus 1 for the null terminator.
if (sz < 17) {
// Buffer is potentially not large enough.
return E_MEMORY_BOUNDS;
}
sprintf(str, "%" PRIx64, h);
return E_SUCCESS;
}
/*
The top 8 bits of any cell should be a specific constant:
- The 1 high bit should be `0`
- The 4 mode bits should be `0001` (H3_CELL_MODE)
- The 3 reserved bits should be `000`
In total, the top 8 bits should be `0_0001_000`
*/
static inline bool _hasGoodTopBits(H3Index h) {
h >>= (64 - 8);
return h == 0b00001000;
}
/* Check that no digit from 1 to `res` is 7 (INVALID_DIGIT).
MHI = 0b100100100100100100100100100100100100100100100;
MLO = MHI >> 2;
| d | d & MHI | ~d | ~d - MLO | d & MHI & (~d - MLO) | result |
|-----|---------|-----|----------|----------------------|---------|
| 000 | 000 | | | 000 | OK |
| 001 | 000 | | | 000 | OK |
| 010 | 000 | | | 000 | OK |
| 011 | 000 | | | 000 | OK |
| 100 | 100 | 011 | 010 | 000 | OK |
| 101 | 100 | 010 | 001 | 000 | OK |
| 110 | 100 | 001 | 000 | 000 | OK |
| 111 | 100 | 000 | 111* | 100 | invalid |
*: carry happened
Note: only care about identifying the *lowest* 7.
Examples with multiple digits:
| d | d & MHI | ~d | ~d - MLO | d & MHI & (~d - MLO) | result |
|---------|---------|---------|----------|----------------------|---------|
| 111.111 | 100.100 | 000.000 | 110.111* | 100.100 | invalid |
| 110.111 | 100.100 | 001.000 | 111.111* | 100.100 | invalid |
| 110.110 | 100.100 | 001.001 | 000.000 | 000.000 | OK |
*: carry happened
In the second example with 110.111, we "misidentify" the 110 as a 7, due
to a carry from the lower bits. But this is OK because we correctly
identify the lowest (only, in this example) 7 just before it.
We only have to worry about carries affecting higher bits in the case of
a 7; all other digits (0--6) don't cause a carry when computing ~d - MLO.
So even though a 7 can affect the results of higher bits, this is OK
because we will always correctly identify the lowest 7.
For further notes, see the discussion here:
https://github.com/uber/h3/pull/496#discussion_r795851046
*/
static inline bool _hasAny7UptoRes(H3Index h, int res) {
const uint64_t MHI = 0b100100100100100100100100100100100100100100100;
const uint64_t MLO = MHI >> 2;
int shift = 3 * (15 - res);
h >>= shift;
h <<= shift;
h = (h & MHI & (~h - MLO));
return h != 0;
}
/* Check that all unused digits after `res` are set to 7 (INVALID_DIGIT).
Bit shift to avoid looping through digits.
*/
static inline bool _hasAll7AfterRes(H3Index h, int res) {
// NOTE: res check is needed because we can't shift by 64
if (res < 15) {
int shift = 19 + 3 * res;
h = ~h;
h <<= shift;
h >>= shift;
return h == 0;
}
return true;
}
/*
Get index of first nonzero bit of an H3Index.
When available, use compiler intrinsics, which should be fast.
If not available, fall back to a loop.
*/
static inline int _firstOneIndex(H3Index h) {
#if defined(__GNUC__) || defined(__clang__)
return 63 - __builtin_clzll(h);
#elif defined(_MSC_VER) && defined(_M_X64) // doesn't work on win32
unsigned long index;
_BitScanReverse64(&index, h);
return (int)index;
#else
// Portable fallback
int pos = 63 - 19;
H3Index m = 1;
while ((h & (m << pos)) == 0) pos--;
return pos;
#endif
}
/*
One final validation just for cells whose base cell (res 0)
is a pentagon.
Pentagon cells start with a sequence of 0's (CENTER_DIGIT's).
The first nonzero digit can't be a 1 (i.e., "deleted subsequence",
PENTAGON_SKIPPED_DIGIT, or K_AXES_DIGIT).
We can check that (in the lower 45 = 15*3 bits) the position of the
first 1 bit isn't divisible by 3.
*/
static inline bool _hasDeletedSubsequence(H3Index h, int base_cell) {
if (isBaseCellPentagonArr[base_cell]) {
h <<= 19;
h >>= 19;
if (h == 0) return false; // all zeros: res 15 pentagon
return _firstOneIndex(h) % 3 == 0;
}
return false;
}
/**
* Returns whether or not an H3 index is a valid cell (hexagon or pentagon).
* @param h The H3 index to validate.
* @return 1 if the H3 index if valid, and 0 if it is not.
*/
int H3_EXPORT(isValidCell)(H3Index h) {
/*
Look for bit patterns that would disqualify an H3Index from
being valid. If identified, exit early.
For reference the H3 index bit layout:
| Region | # bits |
|------------|--------|
| High | 1 |
| Mode | 4 |
| Reserved | 3 |
| Resolution | 4 |
| Base Cell | 7 |
| Digit 1 | 3 |
| Digit 2 | 3 |
| ... | ... |
| Digit 15 | 3 |
Speed benefits come from using bit manipulation instead of loops,
whenever possible.
*/
if (!_hasGoodTopBits(h)) return false;
// No need to check resolution; any 4 bits give a valid resolution.
const int res = H3_GET_RESOLUTION(h);
// Get base cell number and check that it is valid.
const int bc = H3_GET_BASE_CELL(h);
if (bc >= NUM_BASE_CELLS) return false;
if (_hasAny7UptoRes(h, res)) return false;
if (!_hasAll7AfterRes(h, res)) return false;
if (_hasDeletedSubsequence(h, bc)) return false;
// If no disqualifications were identified, the index is a valid H3 cell.
return true;
}
/**
* Returns whether or not an H3 index is valid for any mode (cell, directed
* edge, or vertex).
* @param h The H3 index to validate.
* @return 1 if the H3 index is valid for any supported type, 0 otherwise.
*/
int H3_EXPORT(isValidIndex)(H3Index h) {
return H3_EXPORT(isValidCell)(h) || H3_EXPORT(isValidDirectedEdge)(h) ||
H3_EXPORT(isValidVertex)(h);
}
/**
* Initializes an H3 index.
* @param hp The H3 index to initialize.
* @param res The H3 resolution to initialize the index to.
* @param baseCell The H3 base cell to initialize the index to.
* @param initDigit The H3 digit (0-7) to initialize all of the index digits to.
*/
void setH3Index(H3Index *hp, int res, int baseCell, Direction initDigit) {
H3Index h = H3_INIT;
H3_SET_MODE(h, H3_CELL_MODE);
H3_SET_RESOLUTION(h, res);
H3_SET_BASE_CELL(h, baseCell);
for (int r = 1; r <= res; r++) H3_SET_INDEX_DIGIT(h, r, initDigit);
*hp = h;
}
/**
* cellToParent produces the parent index for a given H3 index
*
* @param h H3Index to find parent of
* @param parentRes The resolution to switch to (parent, grandparent, etc)
* @param out Output: H3Index of the parent
*/
H3Error H3_EXPORT(cellToParent)(H3Index h, int parentRes, H3Index *out) {
int childRes = H3_GET_RESOLUTION(h);
if (parentRes < 0 || parentRes > MAX_H3_RES) {
return E_RES_DOMAIN;
} else if (parentRes > childRes) {
return E_RES_MISMATCH;
} else if (parentRes == childRes) {
*out = h;
return E_SUCCESS;
}
H3Index parentH = H3_SET_RESOLUTION(h, parentRes);
for (int i = parentRes + 1; i <= childRes; i++) {
H3_SET_INDEX_DIGIT(parentH, i, H3_DIGIT_MASK);
}
*out = parentH;
return E_SUCCESS;
}
/**
* Determines whether one resolution is a valid child resolution for a cell.
* Each resolution is considered a valid child resolution of itself.
*
* @param h h3Index parent cell
* @param childRes int resolution of the child
*
* @return The validity of the child resolution
*/
static bool _hasChildAtRes(H3Index h, int childRes) {
int parentRes = H3_GET_RESOLUTION(h);
if (childRes < parentRes || childRes > MAX_H3_RES) {
return false;
}
return true;
}
/**
* cellToChildrenSize returns the exact number of children for a cell at a
* given child resolution.
*
* @param h H3Index to find the number of children of
* @param childRes The child resolution you're interested in
* @param out Output: exact number of children (handles hexagons and
* pentagons correctly)
*/
H3Error H3_EXPORT(cellToChildrenSize)(H3Index h, int childRes, int64_t *out) {
if (!_hasChildAtRes(h, childRes)) return E_RES_DOMAIN;
int n = childRes - H3_GET_RESOLUTION(h);
if (H3_EXPORT(isPentagon)(h)) {
*out = 1 + 5 * (_ipow(7, n) - 1) / 6;
} else {
*out = _ipow(7, n);
}
return E_SUCCESS;
}
/**
* makeDirectChild takes an index and immediately returns the immediate child
* index based on the specified cell number. Bit operations only, could generate
* invalid indexes if not careful (deleted cell under a pentagon).
*
* @param h H3Index to find the direct child of
* @param cellNumber int id of the direct child (0-6)
*
* @return The new H3Index for the child
*/
H3Index makeDirectChild(H3Index h, int cellNumber) {
int childRes = H3_GET_RESOLUTION(h) + 1;
H3Index childH = H3_SET_RESOLUTION(h, childRes);
H3_SET_INDEX_DIGIT(childH, childRes, cellNumber);
return childH;
}
/**
* cellToChildren takes the given hexagon id and generates all of the children
* at the specified resolution storing them into the provided memory pointer.
* It's assumed that cellToChildrenSize was used to determine the allocation.
*
* @param h H3Index to find the children of
* @param childRes int the child level to produce
* @param children H3Index* the memory to store the resulting addresses in
*/
H3Error H3_EXPORT(cellToChildren)(H3Index h, int childRes, H3Index *children) {
int64_t i = 0;
for (IterCellsChildren iter = iterInitParent(h, childRes); iter.h;
iterStepChild(&iter)) {
children[i] = iter.h;
i++;
}
return E_SUCCESS;
}
/**
* Zero out index digits from start to end, inclusive.
* No-op if start > end.
*/
H3Index _zeroIndexDigits(H3Index h, int start, int end) {
if (start > end) return h;
H3Index m = 0;
m = ~m;
m <<= H3_PER_DIGIT_OFFSET * (end - start + 1);
m = ~m;
m <<= H3_PER_DIGIT_OFFSET * (MAX_H3_RES - end);
m = ~m;
return h & m;
}
/**
* cellToCenterChild produces the center child index for a given H3 index at
* the specified resolution
*
* @param h H3Index to find center child of
* @param childRes The resolution to switch to
* @param child H3Index of the center child
* @return 0 (E_SUCCESS) on success
*/
H3Error H3_EXPORT(cellToCenterChild)(H3Index h, int childRes, H3Index *child) {
if (!_hasChildAtRes(h, childRes)) return E_RES_DOMAIN;
h = _zeroIndexDigits(h, H3_GET_RESOLUTION(h) + 1, childRes);
H3_SET_RESOLUTION(h, childRes);
*child = h;
return E_SUCCESS;
}
/**
* compactCells takes a set of hexagons all at the same resolution and
* compresses them by pruning full child branches to the parent level. This is
* also done for all parents recursively to get the minimum number of hex
* addresses that perfectly cover the defined space.
* @param h3Set Set of hexagons
* @param compactedSet The output array of compressed hexagons (preallocated)
* @param numHexes The size of the input and output arrays (possible that no
* contiguous regions exist in the set at all and no compression possible)
* @return an error code on bad input data
*/
H3Error H3_EXPORT(compactCells)(const H3Index *h3Set, H3Index *compactedSet,
const int64_t numHexes) {
if (numHexes == 0) {
return E_SUCCESS;
}
int res = H3_GET_RESOLUTION(h3Set[0]);
if (res == 0) {
// No compaction possible, just copy the set to output
for (int64_t i = 0; i < numHexes; i++) {
compactedSet[i] = h3Set[i];
}
return E_SUCCESS;
}
H3Index *remainingHexes = H3_MEMORY(malloc)(numHexes * sizeof(H3Index));
if (!remainingHexes) {
return E_MEMORY_ALLOC;
}
memcpy(remainingHexes, h3Set, numHexes * sizeof(H3Index));
H3Index *hashSetArray = H3_MEMORY(calloc)(numHexes, sizeof(H3Index));
if (!hashSetArray) {
H3_MEMORY(free)(remainingHexes);
return E_MEMORY_ALLOC;
}
H3Index *compactedSetOffset = compactedSet;
int64_t numRemainingHexes = numHexes;
while (numRemainingHexes) {
res = H3_GET_RESOLUTION(remainingHexes[0]);
int parentRes = res - 1;
// If parentRes is less than zero, we've compacted all the way up to the
// base cells. Time to process the remaining cells.
if (parentRes >= 0) {
// Put the parents of the hexagons into the temp array
// via a hashing mechanism, and use the reserved bits
// to track how many times a parent is duplicated
for (int64_t i = 0; i < numRemainingHexes; i++) {
H3Index currIndex = remainingHexes[i];
// TODO: This case is coverable (reachable by fuzzer)
if (currIndex != 0) {
// If the reserved bits were set by the caller, the
// algorithm below may encounter undefined behavior
// because it expects to have set the reserved bits
// itself.
if (H3_GET_RESERVED_BITS(currIndex) != 0) {
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_CELL_INVALID;
}
H3Index parent;
H3Error parentError =
H3_EXPORT(cellToParent)(currIndex, parentRes, &parent);
// Should never be reachable as a result of the compact
// algorithm. Can happen if cellToParent errors e.g.
// because of incompatible resolutions.
if (parentError) {
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return parentError;
}
// Modulus hash the parent into the temp array
int64_t loc = (int64_t)(parent % numRemainingHexes);
DEFENSEONLY(int64_t loopCount = 0);
while (hashSetArray[loc] != 0) {
if (NEVER(loopCount > numRemainingHexes)) {
// This case should not be possible because at
// most one index is placed into hashSetArray
// per numRemainingHexes.
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_FAILED;
}
H3Index tempIndex =
hashSetArray[loc] & H3_RESERVED_MASK_NEGATIVE;
if (tempIndex == parent) {
int count =
H3_GET_RESERVED_BITS(hashSetArray[loc]) + 1;
int limitCount = 7;
if (H3_EXPORT(isPentagon)(
tempIndex & H3_RESERVED_MASK_NEGATIVE)) {
limitCount--;
}
// One is added to count for this check to match
// one being added to count later in this
// function when checking for all children being
// present.
if (count + 1 > limitCount) {
// Only possible on duplicate input
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_DUPLICATE_INPUT;
}
H3_SET_RESERVED_BITS(parent, count);
hashSetArray[loc] = H3_NULL;
} else {
loc = (loc + 1) % numRemainingHexes;
}
DEFENSEONLY(loopCount++);
}
hashSetArray[loc] = parent;
}
}
}
// Determine which parent hexagons have a complete set
// of children and put them in the compactableHexes array
int64_t compactableCount = 0;
int64_t maxCompactableCount =
numRemainingHexes / 6; // Somehow all pentagons; conservative
if (maxCompactableCount == 0) {
memcpy(compactedSetOffset, remainingHexes,
numRemainingHexes * sizeof(remainingHexes[0]));
break;
}
H3Index *compactableHexes =
H3_MEMORY(calloc)(maxCompactableCount, sizeof(H3Index));
if (!compactableHexes) {
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_MEMORY_ALLOC;
}
for (int64_t i = 0; i < numRemainingHexes; i++) {
if (hashSetArray[i] == 0) continue;
int count = H3_GET_RESERVED_BITS(hashSetArray[i]) + 1;
// Include the deleted direction for pentagons as implicitly "there"
if (H3_EXPORT(isPentagon)(hashSetArray[i] &
H3_RESERVED_MASK_NEGATIVE)) {
// We need this later on, no need to recalculate
H3_SET_RESERVED_BITS(hashSetArray[i], count);
// Increment count after setting the reserved bits,
// since count is already incremented above, so it
// will be the expected value for a complete hexagon.
count++;
}
if (count == 7) {
// Bingo! Full set!
compactableHexes[compactableCount] =
hashSetArray[i] & H3_RESERVED_MASK_NEGATIVE;
compactableCount++;
}
}
// Uncompactable hexes are immediately copied into the
// output compactedSetOffset
int64_t uncompactableCount = 0;
for (int64_t i = 0; i < numRemainingHexes; i++) {
H3Index currIndex = remainingHexes[i];
// TODO: This case is coverable (reachable by fuzzer)
if (currIndex != H3_NULL) {
bool isUncompactable = true;
// Resolution 0 cells always uncompactable, and trying to take
// the res -1 parent of a cell is invalid.
if (parentRes >= 0) {
H3Index parent;
H3Error parentError =
H3_EXPORT(cellToParent)(currIndex, parentRes, &parent);
if (NEVER(parentError)) {
H3_MEMORY(free)(compactableHexes);
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return parentError;
}
// Modulus hash the parent into the temp array
// to determine if this index was included in
// the compactableHexes array
int64_t loc = (int64_t)(parent % numRemainingHexes);
DEFENSEONLY(int64_t loopCount = 0);
do {
if (NEVER(loopCount > numRemainingHexes)) {
// This case should not be possible because at most
// one index is placed into hashSetArray per input
// hexagon.
H3_MEMORY(free)(compactableHexes);
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_FAILED;
}
H3Index tempIndex =
hashSetArray[loc] & H3_RESERVED_MASK_NEGATIVE;
if (tempIndex == parent) {
int count =
H3_GET_RESERVED_BITS(hashSetArray[loc]) + 1;
if (count == 7) {
isUncompactable = false;
}
break;
} else {
loc = (loc + 1) % numRemainingHexes;
}
DEFENSEONLY(loopCount++;)
} while (hashSetArray[loc] != parent);
}
if (isUncompactable) {
compactedSetOffset[uncompactableCount] = remainingHexes[i];
uncompactableCount++;
}
}
}
// Set up for the next loop
memset(hashSetArray, 0, numHexes * sizeof(H3Index));
compactedSetOffset += uncompactableCount;
memcpy(remainingHexes, compactableHexes,
compactableCount * sizeof(H3Index));
numRemainingHexes = compactableCount;
H3_MEMORY(free)(compactableHexes);
}
H3_MEMORY(free)(remainingHexes);
H3_MEMORY(free)(hashSetArray);
return E_SUCCESS;
}
/**
* uncompactCells takes a compressed set of cells and expands back to the
* original set of cells.
*
* Skips elements that are H3_NULL (i.e., 0).
*
* @param compactedSet Set of compacted cells
* @param numCompacted The number of cells in the input compacted set
* @param outSet Output array for decompressed cells (preallocated)
* @param numOut The size of the output array to bound check against
* @param res The H3 resolution to decompress to
* @return An error code if output array is too small or any cell
* is smaller than the output resolution.
*/
H3Error H3_EXPORT(uncompactCells)(const H3Index *compactedSet,
const int64_t numCompacted, H3Index *outSet,
const int64_t numOut, const int res) {
int64_t i = 0;
for (int64_t j = 0; j < numCompacted; j++) {
if (!_hasChildAtRes(compactedSet[j], res)) return E_RES_MISMATCH;
for (IterCellsChildren iter = iterInitParent(compactedSet[j], res);
iter.h; i++, iterStepChild(&iter)) {
if (i >= numOut) return E_MEMORY_BOUNDS; // went too far; abort!
outSet[i] = iter.h;
}
}
return E_SUCCESS;
}
/**
* uncompactCellsSize takes a compacted set of hexagons and provides
* the exact size of the uncompacted set of hexagons.
*
* @param compactedSet Set of hexagons
* @param numCompacted The number of hexes in the input set
* @param res The hexagon resolution to decompress to
* @param out The number of hexagons to allocate memory for
* @returns E_SUCCESS on success, or another value on error
*/
H3Error H3_EXPORT(uncompactCellsSize)(const H3Index *compactedSet,
const int64_t numCompacted, const int res,
int64_t *out) {
int64_t numOut = 0;
for (int64_t i = 0; i < numCompacted; i++) {
if (compactedSet[i] == H3_NULL) continue;
int64_t childrenSize;
H3Error childrenError =
H3_EXPORT(cellToChildrenSize)(compactedSet[i], res, &childrenSize);
if (childrenError) {
// The parent res does not contain `res`.
return E_RES_MISMATCH;
}
numOut += childrenSize;
}
*out = numOut;
return E_SUCCESS;
}
/**
* isResClassIII takes a hexagon ID and determines if it is in a
* Class III resolution (rotated versus the icosahedron and subject
* to shape distortion adding extra points on icosahedron edges, making
* them not true hexagons).
* @param h The H3Index to check.
* @return Returns 1 if the hexagon is class III, otherwise 0.
*/
int H3_EXPORT(isResClassIII)(H3Index h) { return H3_GET_RESOLUTION(h) % 2; }
/**
* isPentagon takes an H3Index and determines if it is actually a pentagon.
* @param h The H3Index to check.
* @return Returns 1 if it is a pentagon, otherwise 0.
*/
int H3_EXPORT(isPentagon)(H3Index h) {
return _isBaseCellPentagon(H3_GET_BASE_CELL(h)) &&
!_h3LeadingNonZeroDigit(h);
}
/**
* Returns the highest resolution non-zero digit in an H3Index.
* @param h The H3Index.
* @return The highest resolution non-zero digit in the H3Index.
*/
Direction _h3LeadingNonZeroDigit(H3Index h) {
for (int r = 1; r <= H3_GET_RESOLUTION(h); r++)
if (H3_GET_INDEX_DIGIT(h, r)) return H3_GET_INDEX_DIGIT(h, r);
// if we're here it's all 0's
return CENTER_DIGIT;
}
/**
* Rotate an H3Index 60 degrees counter-clockwise about a pentagonal center.
* @param h The H3Index.
*/
H3Index _h3RotatePent60ccw(H3Index h) {
// rotate in place; skips any leading 1 digits (k-axis)
int foundFirstNonZeroDigit = 0;
for (int r = 1, res = H3_GET_RESOLUTION(h); r <= res; r++) {
// rotate this digit
H3_SET_INDEX_DIGIT(h, r, _rotate60ccw(H3_GET_INDEX_DIGIT(h, r)));
// look for the first non-zero digit so we
// can adjust for deleted k-axes sequence
// if necessary
if (!foundFirstNonZeroDigit && H3_GET_INDEX_DIGIT(h, r) != 0) {
foundFirstNonZeroDigit = 1;
// adjust for deleted k-axes sequence
if (_h3LeadingNonZeroDigit(h) == K_AXES_DIGIT)
h = _h3Rotate60ccw(h);
}
}
return h;
}
/**
* Rotate an H3Index 60 degrees clockwise about a pentagonal center.
* @param h The H3Index.
*/
H3Index _h3RotatePent60cw(H3Index h) {
// rotate in place; skips any leading 1 digits (k-axis)
int foundFirstNonZeroDigit = 0;
for (int r = 1, res = H3_GET_RESOLUTION(h); r <= res; r++) {
// rotate this digit
H3_SET_INDEX_DIGIT(h, r, _rotate60cw(H3_GET_INDEX_DIGIT(h, r)));
// look for the first non-zero digit so we
// can adjust for deleted k-axes sequence
// if necessary
if (!foundFirstNonZeroDigit && H3_GET_INDEX_DIGIT(h, r) != 0) {
foundFirstNonZeroDigit = 1;
// adjust for deleted k-axes sequence
if (_h3LeadingNonZeroDigit(h) == K_AXES_DIGIT) h = _h3Rotate60cw(h);
}
}
return h;
}
/**
* Rotate an H3Index 60 degrees counter-clockwise.
* @param h The H3Index.
*/
H3Index _h3Rotate60ccw(H3Index h) {
for (int r = 1, res = H3_GET_RESOLUTION(h); r <= res; r++) {
Direction oldDigit = H3_GET_INDEX_DIGIT(h, r);
H3_SET_INDEX_DIGIT(h, r, _rotate60ccw(oldDigit));
}
return h;
}
/**
* Rotate an H3Index 60 degrees clockwise.
* @param h The H3Index.
*/
H3Index _h3Rotate60cw(H3Index h) {
for (int r = 1, res = H3_GET_RESOLUTION(h); r <= res; r++) {
H3_SET_INDEX_DIGIT(h, r, _rotate60cw(H3_GET_INDEX_DIGIT(h, r)));
}
return h;
}
/**
* Convert an FaceIJK address to the corresponding H3Index.
* @param fijk The FaceIJK address.
* @param res The cell resolution.
* @return The encoded H3Index (or H3_NULL on failure).
*/
H3Index _faceIjkToH3(const FaceIJK *fijk, int res) {
// initialize the index
H3Index h = H3_INIT;
H3_SET_MODE(h, H3_CELL_MODE);
H3_SET_RESOLUTION(h, res);
// check for res 0/base cell
if (res == 0) {
if (fijk->coord.i > MAX_FACE_COORD || fijk->coord.j > MAX_FACE_COORD ||
fijk->coord.k > MAX_FACE_COORD) {
// out of range input
return H3_NULL;
}
H3_SET_BASE_CELL(h, _faceIjkToBaseCell(fijk));
return h;
}
// we need to find the correct base cell FaceIJK for this H3 index;
// start with the passed in face and resolution res ijk coordinates
// in that face's coordinate system
FaceIJK fijkBC = *fijk;
// build the H3Index from finest res up
// adjust r for the fact that the res 0 base cell offsets the indexing
// digits
CoordIJK *ijk = &fijkBC.coord;
for (int r = res - 1; r >= 0; r--) {
CoordIJK lastIJK = *ijk;
CoordIJK lastCenter;
if (isResolutionClassIII(r + 1)) {
// rotate ccw
_upAp7(ijk);
lastCenter = *ijk;
_downAp7(&lastCenter);
} else {
// rotate cw
_upAp7r(ijk);
lastCenter = *ijk;
_downAp7r(&lastCenter);
}
CoordIJK diff;
_ijkSub(&lastIJK, &lastCenter, &diff);
_ijkNormalize(&diff);
H3_SET_INDEX_DIGIT(h, r + 1, _unitIjkToDigit(&diff));
}
// fijkBC should now hold the IJK of the base cell in the
// coordinate system of the current face
if (fijkBC.coord.i > MAX_FACE_COORD || fijkBC.coord.j > MAX_FACE_COORD ||
fijkBC.coord.k > MAX_FACE_COORD) {
// out of range input
return H3_NULL;
}
// lookup the correct base cell