Repository navigation
Expand file tree
/
Copy pathgrid.go
More file actions
750 lines (623 loc) · 21.7 KB
/
Copy pathgrid.go
File metadata and controls
750 lines (623 loc) · 21.7 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
/*
* Copyright 2026 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.
*/
package h3
import (
"errors"
)
// nextRingDirection is the direction stepped to move from one ring to the next
// larger ring before tracing it.
const nextRingDirection = iAxesDigit
var (
// directions is the ordered set of the six neighbor directions used when
// tracing a ring counterclockwise, one side of the ring per entry.
directions = [6]int{
jAxesDigit, jkAxesDigit, kAxesDigit, ikAxesDigit, iAxesDigit, ijAxesDigit,
}
// newDigitII maps current digit and direction to the new digit on a Class II grid.
newDigitII = [7][7]int{
{centerDigit, kAxesDigit, jAxesDigit, jkAxesDigit, iAxesDigit,
ikAxesDigit, ijAxesDigit},
{kAxesDigit, iAxesDigit, jkAxesDigit, ijAxesDigit, ikAxesDigit,
jAxesDigit, centerDigit},
{jAxesDigit, jkAxesDigit, kAxesDigit, iAxesDigit, ijAxesDigit,
centerDigit, ikAxesDigit},
{jkAxesDigit, ijAxesDigit, iAxesDigit, ikAxesDigit, centerDigit,
kAxesDigit, jAxesDigit},
{iAxesDigit, ikAxesDigit, ijAxesDigit, centerDigit, jAxesDigit,
jkAxesDigit, kAxesDigit},
{ikAxesDigit, jAxesDigit, centerDigit, kAxesDigit, jkAxesDigit,
ijAxesDigit, iAxesDigit},
{ijAxesDigit, centerDigit, ikAxesDigit, jAxesDigit, kAxesDigit,
iAxesDigit, jkAxesDigit}}
// newAdjustmentII maps current digit and direction to the coarser ap7 move on a Class II grid.
newAdjustmentII = [7][7]int{
{centerDigit, centerDigit, centerDigit, centerDigit, centerDigit,
centerDigit, centerDigit},
{centerDigit, kAxesDigit, centerDigit, kAxesDigit, centerDigit,
ikAxesDigit, centerDigit},
{centerDigit, centerDigit, jAxesDigit, jkAxesDigit, centerDigit,
centerDigit, jAxesDigit},
{centerDigit, kAxesDigit, jkAxesDigit, jkAxesDigit, centerDigit,
centerDigit, centerDigit},
{centerDigit, centerDigit, centerDigit, centerDigit, iAxesDigit,
iAxesDigit, ijAxesDigit},
{centerDigit, ikAxesDigit, centerDigit, centerDigit, iAxesDigit,
ikAxesDigit, centerDigit},
{centerDigit, centerDigit, jAxesDigit, centerDigit, ijAxesDigit,
centerDigit, ijAxesDigit}}
// newDigitIII maps current digit and direction to the new digit on a Class III grid.
newDigitIII = [7][7]int{
{centerDigit, kAxesDigit, jAxesDigit, jkAxesDigit, iAxesDigit,
ikAxesDigit, ijAxesDigit},
{kAxesDigit, jAxesDigit, jkAxesDigit, iAxesDigit, ikAxesDigit,
ijAxesDigit, centerDigit},
{jAxesDigit, jkAxesDigit, iAxesDigit, ikAxesDigit, ijAxesDigit,
centerDigit, kAxesDigit},
{jkAxesDigit, iAxesDigit, ikAxesDigit, ijAxesDigit, centerDigit,
kAxesDigit, jAxesDigit},
{iAxesDigit, ikAxesDigit, ijAxesDigit, centerDigit, kAxesDigit,
jAxesDigit, jkAxesDigit},
{ikAxesDigit, ijAxesDigit, centerDigit, kAxesDigit, jAxesDigit,
jkAxesDigit, iAxesDigit},
{ijAxesDigit, centerDigit, kAxesDigit, jAxesDigit, jkAxesDigit,
iAxesDigit, ikAxesDigit}}
// newAdjustmentIII maps current digit and direction to the coarser ap7 move on a Class III grid.
newAdjustmentIII = [7][7]int{
{centerDigit, centerDigit, centerDigit, centerDigit, centerDigit,
centerDigit, centerDigit},
{centerDigit, kAxesDigit, centerDigit, jkAxesDigit, centerDigit,
kAxesDigit, centerDigit},
{centerDigit, centerDigit, jAxesDigit, jAxesDigit, centerDigit,
centerDigit, ijAxesDigit},
{centerDigit, jkAxesDigit, jAxesDigit, jkAxesDigit, centerDigit,
centerDigit, centerDigit},
{centerDigit, centerDigit, centerDigit, centerDigit, iAxesDigit,
ikAxesDigit, iAxesDigit},
{centerDigit, kAxesDigit, centerDigit, centerDigit, ikAxesDigit,
ikAxesDigit, centerDigit},
{centerDigit, centerDigit, ijAxesDigit, centerDigit, iAxesDigit,
centerDigit, ijAxesDigit}}
)
// isBaseCellPolarPentagon reports whether a base cell is one of the two polar
// pentagons, which have all-i neighbors and therefore distort differently.
func isBaseCellPolarPentagon(baseCell int) bool {
return baseCell == 4 || baseCell == 117
}
// baseCellIsCwOffset reports whether testFace is one of the clockwise-offset
// faces of a pentagon base cell.
func baseCellIsCwOffset(baseCell, testFace int) bool {
offsets, ok := baseCellCWOffsetPent[baseCell]
return ok && (offsets[0] == testFace || offsets[1] == testFace)
}
// neighborRotations returns the cell adjacent to c in direction dir, applying
// rotations counterclockwise 60° turns to dir first. It also returns the number
// of rotations a caller should carry into the next step, which changes whenever
// the move crosses an icosahedron face edge or pentagon distortion. It fails
// with ErrPentagon when the move enters a pentagon's deleted k subsequence in an
// undefined way.
func (c Cell) neighborRotations(dir, rotations int) (Cell, int, error) {
current := c
if dir < centerDigit || dir >= numDigits {
return 0, 0, ErrFailed
}
rotations %= 6
for range rotations {
dir = rotate60ccw(dir)
}
newRotations := 0
oldBaseCell := current.BaseCellNumber()
if oldBaseCell < 0 || oldBaseCell >= NumBaseCells {
return 0, 0, ErrCellInvalid
}
oldLeadingDigit := current.leadingNonZeroDigit()
res := current.Resolution() - 1
for {
if res == -1 {
current = current.setBaseCell(baseCellNeighbors[oldBaseCell][dir])
newRotations = baseCellNeighbor60CCWRots[oldBaseCell][dir]
if current.BaseCellNumber() == invalidBaseCell {
// Adjust for the deleted k vertex at the base cell level. This
// edge actually borders a different neighbor.
current = current.setBaseCell(baseCellNeighbors[oldBaseCell][ikAxesDigit])
newRotations = baseCellNeighbor60CCWRots[oldBaseCell][ikAxesDigit]
// Perform the adjustment for the k-subsequence we're skipping.
current = current.rotate60ccw()
rotations++
}
break
}
oldDigit := indexDigit(current, res+1)
if oldDigit == numDigits {
return 0, 0, ErrCellInvalid
}
var nextDir int
if isResClassIII(res + 1) {
current = current.setIndexDigit(res+1, newDigitII[oldDigit][dir])
nextDir = newAdjustmentII[oldDigit][dir]
} else {
current = current.setIndexDigit(res+1, newDigitIII[oldDigit][dir])
nextDir = newAdjustmentIII[oldDigit][dir]
}
if nextDir == centerDigit {
break
}
dir = nextDir
res--
}
newBaseCell := current.BaseCellNumber()
if isBaseCellPentagon[newBaseCell] {
alreadyAdjustedKSubsequence := false
// Force rotation out of the missing k-axes sub-sequence.
if current.leadingNonZeroDigit() == kAxesDigit {
if oldBaseCell != newBaseCell {
// We traversed into the deleted k subsequence of a different
// pentagon base cell. The shared edge is always that base cell's
// clockwise-offset face, so the rotation is clockwise.
if baseCellIsCwOffset(newBaseCell, baseCellHomeFijk[oldBaseCell].face) {
current = current.rotate60cw()
}
alreadyAdjustedKSubsequence = true
} else {
// We traversed into the deleted k subsequence from within the
// same pentagon base cell.
switch oldLeadingDigit {
case centerDigit:
// Undefined: the k direction is deleted from here.
return 0, 0, ErrPentagon
case jkAxesDigit:
current = current.rotate60ccw()
rotations++
case ikAxesDigit:
current = current.rotate60cw()
rotations += 5
default:
return 0, 0, ErrFailed
}
}
}
for range newRotations {
current = current.rotatePent60ccw()
}
// Account for differing orientation of the base cells.
if oldBaseCell != newBaseCell {
if isBaseCellPolarPentagon(newBaseCell) {
// Polar base cells behave differently because they have all i
// neighbors.
if oldBaseCell != 118 && oldBaseCell != 8 &&
current.leadingNonZeroDigit() != jkAxesDigit {
rotations++
}
} else if current.leadingNonZeroDigit() == ikAxesDigit && !alreadyAdjustedKSubsequence {
// Account for distortion introduced to the 5 neighbor by the
// deleted k subsequence.
rotations++
}
}
} else if newRotations != 0 {
current = current.rotate60ccwBy(newRotations)
}
rotations = (rotations + newRotations) % 6
return current, rotations, nil
}
// maxGridDiskSize returns the maximum number of cells in a grid disk of radius
// k: the origin plus six cells per ring. It sizes the output buffer up front so
// the traversal never has to grow it.
func maxGridDiskSize(k int) int {
return 3*k*(k+1) + 1
}
// ringSize returns the number of cells in the hollow ring at grid distance d: 1
// at the origin, 6*d otherwise.
func ringSize(d int) int {
if d == 0 {
return 1
}
return 6 * d
}
// gridDiskUnsafeInto appends the grid disk of radius k around c to dst, the
// origin first and then each ring outward, using the fast spiral traversal. It
// fails with ErrPentagon on pentagon distortion, having appended a partial
// result the caller is expected to discard. The caller sizes dst's capacity.
func (c Cell) gridDiskUnsafeInto(k int, dst []Cell) ([]Cell, error) {
if k < 0 {
return dst, ErrDomain
}
cursor := c
dst = append(dst, cursor)
if cursor.IsPentagon() {
return dst, ErrPentagon
}
rotations := 0
for ring := 1; ring <= k; ring++ {
// Step outward to the start of the next ring before tracing it.
next, rot, err := cursor.neighborRotations(nextRingDirection, rotations)
if err != nil {
return dst, err
}
cursor, rotations = next, rot
if cursor.IsPentagon() {
return dst, ErrPentagon
}
for direction := 0; direction < 6; direction++ {
for i := 0; i < ring; i++ {
next, rot, err := cursor.neighborRotations(directions[direction], rotations)
if err != nil {
return dst, err
}
cursor, rotations = next, rot
dst = append(dst, cursor)
if cursor.IsPentagon() {
return dst, ErrPentagon
}
}
}
}
return dst, nil
}
// gridDiskInto appends the grid disk of radius k around c to dst, trying the
// fast spiral first and falling back to the safe breadth-first traversal on
// pentagon distortion. The caller sizes dst's capacity.
func (c Cell) gridDiskInto(k int, dst []Cell) ([]Cell, error) {
start := len(dst)
dst, err := c.gridDiskUnsafeInto(k, dst)
if err == nil {
return dst, nil
}
// Discard the partial spiral and refill from the safe traversal.
dst = dst[:start]
rings, err := c.GridDiskDistancesSafe(k)
if err != nil {
return dst, err
}
for _, ring := range rings {
dst = append(dst, ring...)
}
return dst, nil
}
// GridDisk returns the cells within grid distance k of the origin cell. The
// origin is included. Output ordering is not significant. It falls back to the
// safe traversal when the fast one meets pentagon distortion.
func (c Cell) GridDisk(k int) ([]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
return c.gridDiskInto(k, make([]Cell, 0, maxGridDiskSize(k)))
}
// GridDisk returns the cells within grid distance k of the origin cell.
func GridDisk(origin Cell, k int) ([]Cell, error) {
return origin.GridDisk(k)
}
// GridDiskUnsafe returns the cells within grid distance k of the origin cell,
// the origin first and then each ring outward, using the fast spiral
// traversal. Unlike GridDisk, which falls back to the safe traversal, it fails
// with ErrPentagon when the spiral meets a pentagon or pentagon distortion.
func (c Cell) GridDiskUnsafe(k int) ([]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
out, err := c.gridDiskUnsafeInto(k, make([]Cell, 0, maxGridDiskSize(k)))
if err != nil {
return nil, err
}
return out, nil
}
// GridDiskUnsafe returns the cells within grid distance k of the origin cell,
// using the fast spiral traversal.
func GridDiskUnsafe(origin Cell, k int) ([]Cell, error) {
return origin.GridDiskUnsafe(k)
}
// GridDisksUnsafe returns, for each origin, the cells within grid distance k of
// that origin. The outer slice matches the order of origins; inner ordering is
// not significant. It fails if any disk encounters pentagon distortion. Every
// disk shares one backing buffer sized for the worst case, so the result costs
// two allocations regardless of the number of origins.
func GridDisksUnsafe(origins []Cell, k int) ([][]Cell, error) {
if len(origins) == 0 {
return nil, nil
}
if k < 0 {
return nil, ErrDomain
}
out := make([][]Cell, len(origins))
buf := make([]Cell, 0, len(origins)*maxGridDiskSize(k))
for i, origin := range origins {
start := len(buf)
var err error
buf, err = origin.gridDiskUnsafeInto(k, buf)
if err != nil {
return nil, err
}
out[i] = buf[start:len(buf):len(buf)]
}
return out, nil
}
// GridDiskDistances returns the cells within grid distance k of the origin,
// grouped by ring: index 0 is the origin, index d holds the cells at grid
// distance d. It optimistically tries the fast traversal, falling back to the
// safe one on pentagon distortion.
func (c Cell) GridDiskDistances(k int) ([][]Cell, error) {
rings, err := c.GridDiskDistancesUnsafe(k)
if err != nil {
return c.GridDiskDistancesSafe(k)
}
return rings, nil
}
// GridDiskDistances returns the cells within grid distance k of the origin,
// grouped by ring.
func GridDiskDistances(origin Cell, k int) ([][]Cell, error) {
return origin.GridDiskDistances(k)
}
// GridDiskDistancesUnsafe returns the cells within grid distance k of the
// origin, grouped by ring, using a fast spiral traversal. It fails with
// ErrPentagon if the spiral meets a pentagon or pentagon distortion.
func (c Cell) GridDiskDistancesUnsafe(k int) ([][]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
buf, err := c.gridDiskUnsafeInto(k, make([]Cell, 0, maxGridDiskSize(k)))
if err != nil {
return nil, err
}
// The spiral fills buf ring by ring, so each ring is a contiguous window
// into the one backing array.
rings := make([][]Cell, k+1)
offset := 0
for d := 0; d <= k; d++ {
size := ringSize(d)
rings[d] = buf[offset : offset+size : offset+size]
offset += size
}
return rings, nil
}
// GridDiskDistancesUnsafe returns the cells within grid distance k of the
// origin, grouped by ring, using a fast spiral traversal.
func GridDiskDistancesUnsafe(origin Cell, k int) ([][]Cell, error) {
return origin.GridDiskDistancesUnsafe(k)
}
// GridDiskDistancesSafe returns the cells within grid distance k of the origin,
// grouped by ring, using a breadth-first traversal that tolerates pentagon
// distortion. It is slower than the unsafe variant but always correct.
func (c Cell) GridDiskDistancesSafe(k int) ([][]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
rings := make([][]Cell, k+1)
// Give every ring an exact-capacity window into one shared buffer so the
// breadth-first appends below never reallocate.
buf := make([]Cell, maxGridDiskSize(k))
offset := 0
for d := 0; d <= k; d++ {
size := ringSize(d)
rings[d] = buf[offset : offset : offset+size]
offset += size
}
// Each completed ring is the queue for the next one, so no separate queue
// is needed. Membership uses an open-addressing hash set sized at twice
// the disk so probing stays short; the zero Cell is the empty-slot marker.
seen := make([]Cell, 2*len(buf))
seen[hashSlot(c, len(seen))] = c
rings[0] = append(rings[0], c)
for dist := 0; dist < k; dist++ {
for _, head := range rings[dist] {
for _, dir := range directions {
neighbor, _, err := head.neighborRotations(dir, 0)
if err != nil {
if errors.Is(err, ErrPentagon) {
continue
}
return nil, err
}
slot := hashSlot(neighbor, len(seen))
for seen[slot] != 0 && seen[slot] != neighbor {
slot++
if slot == len(seen) {
slot = 0
}
}
if seen[slot] == neighbor {
continue
}
seen[slot] = neighbor
rings[dist+1] = append(rings[dist+1], neighbor)
}
}
}
return rings, nil
}
// GridDiskDistancesSafe returns the cells within grid distance k of the origin,
// grouped by ring, using a breadth-first traversal that tolerates pentagon
// distortion.
func GridDiskDistancesSafe(origin Cell, k int) ([][]Cell, error) {
return origin.GridDiskDistancesSafe(k)
}
// GridRingUnsafe returns the hollow ring of cells at exactly grid distance k
// from the origin, using a fast single-loop traversal. k of 0 returns just the
// origin. It fails with ErrPentagon if the ring meets a pentagon or pentagon
// distortion.
func (c Cell) GridRingUnsafe(k int) ([]Cell, error) {
if k < 0 {
return nil, ErrDomain
}
if k == 0 {
return []Cell{c}, nil
}
cursor := c
rotations := 0
if cursor.IsPentagon() {
return nil, ErrPentagon
}
for range k {
next, rot, err := cursor.neighborRotations(nextRingDirection, rotations)
if err != nil {
return nil, err
}
cursor, rotations = next, rot
if cursor.IsPentagon() {
return nil, ErrPentagon
}
}
lastIndex := cursor
out := make([]Cell, 0, ringSize(k))
out = append(out, cursor)
for direction := 0; direction < 6; direction++ {
for pos := 0; pos < k; pos++ {
next, rot, err := cursor.neighborRotations(directions[direction], rotations)
if err != nil {
return nil, err
}
cursor, rotations = next, rot
// Skip the very last index; it equals the first one already added.
// We still traverse to it for the pentagon-distortion check below.
if pos != k-1 || direction != 5 {
out = append(out, cursor)
if cursor.IsPentagon() {
return nil, ErrPentagon
}
}
}
}
// If the loop didn't return to its start, pentagon distortion occurred.
if lastIndex != cursor {
return nil, ErrPentagon
}
return out, nil
}
// GridRingUnsafe returns the hollow ring of cells at exactly grid distance k
// from the origin, using a fast single-loop traversal.
func GridRingUnsafe(origin Cell, k int) ([]Cell, error) {
return origin.GridRingUnsafe(k)
}
// GridRing returns the hollow ring of cells at exactly grid distance k from the
// origin. It tries the fast traversal first and falls back to the safe disk
// traversal when pentagon distortion is encountered.
func (c Cell) GridRing(k int) ([]Cell, error) {
out, err := c.GridRingUnsafe(k)
if err == nil {
return out, nil
}
if k < 0 {
return nil, ErrDomain
}
rings, err := c.GridDiskDistancesSafe(k)
if err != nil {
return nil, err
}
return rings[k], nil
}
// GridRing returns the hollow ring of cells at exactly grid distance k from the
// origin.
func GridRing(origin Cell, k int) ([]Cell, error) {
return origin.GridRing(k)
}
// IsNeighbor reports whether c and other are adjacent cells at the same
// resolution.
func (c Cell) IsNeighbor(other Cell) (bool, error) {
if modeOf(c) != cellMode || modeOf(other) != cellMode {
return false, ErrCellInvalid
}
// A cell is never its own neighbor.
if c == other {
return false, nil
}
if c.Resolution() != other.Resolution() {
return false, ErrResolutionMismatch
}
// Cells that share a parent are very likely neighbors; a digit lookup is a
// cheap way to decide many cases before the general traversal.
parentRes := c.Resolution() - 1
if parentRes > 0 {
originParent, originErr := c.Parent(parentRes)
destParent, destErr := other.Parent(parentRes)
if originErr == nil && destErr == nil && originParent == destParent {
originResDigit := indexDigit(c, parentRes+1)
destResDigit := indexDigit(other, parentRes+1)
if originResDigit == centerDigit || destResDigit == centerDigit {
return true, nil
}
if originResDigit >= numDigits {
return false, ErrCellInvalid
}
if (originResDigit == kAxesDigit || destResDigit == kAxesDigit) &&
originParent.IsPentagon() {
// Real neighbors across a pentagon's deleted subsequence fail
// this optimized check but are accepted by the disk check below.
return false, ErrCellInvalid
}
// neighborDigits maps an indexing digit to the two adjacent digits
// (clockwise, counterclockwise) of the cells that share this parent
// and neighbor the origin.
neighborDigits := [7][2]int{
{centerDigit, centerDigit},
{jkAxesDigit, ikAxesDigit},
{ijAxesDigit, jkAxesDigit},
{jAxesDigit, kAxesDigit},
{ikAxesDigit, ijAxesDigit},
{kAxesDigit, iAxesDigit},
{iAxesDigit, jAxesDigit},
}
//nolint:gosec // originResDigit is in [1,6] here (center and out-of-range digits are handled above), so this fixed-size lookup stays in bounds.
adjacent := neighborDigits[originResDigit]
if adjacent[0] == destResDigit || adjacent[1] == destResDigit {
return true, nil
}
}
}
// Otherwise determine the relationship the hard way.
ring, err := c.GridDisk(1)
if err != nil {
return false, err
}
for _, cell := range ring {
if cell == other {
return true, nil
}
}
return false, nil
}
// directionForNeighbor returns the digit direction from origin to destination,
// the reverse of stepping a neighbor. It returns numDigits when the cells are
// not neighbors. It checks each neighbor in turn, skipping the center (which is
// the origin) and the deleted k axis for pentagons.
func (c Cell) directionForNeighbor(destination Cell) int {
firstDir := kAxesDigit
if c.IsPentagon() {
firstDir = jAxesDigit
}
for dir := firstDir; dir < numDigits; dir++ {
neighbor, _, err := c.neighborRotations(dir, 0)
if err == nil && neighbor == destination {
return dir
}
}
return numDigits
}
// baseCellToCCWrot60 returns the number of 60° counterclockwise rotations for a
// base cell's coordinate system on the given face, or invalidRotations if the
// base cell does not appear on that face.
func baseCellToCCWrot60(baseCell, face int) int {
if face < 0 || face >= NumIcosaFaces {
return invalidRotations
}
for i := range 3 {
for j := range 3 {
for k := range 3 {
if faceIjkBaseCells[face][i][j][k].baseCell == baseCell {
return faceIjkBaseCells[face][i][j][k].ccwRot60
}
}
}
}
return invalidRotations
}