xs_h3 0.2.0

Excerion Sun's Rust implementation of Uber's H3 geospatial indexing library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
// src/coords/ijk.rs

use crate::constants::{EPSILON, M_ONESEVENTH, M_ONETHIRD, M_RSIN60, M_SQRT3_2, M_SQRT7};
use crate::math::extensions::_ipow; // For _downAp* later, if needed here or used elsewhere
use crate::types::{CoordIJ, CoordIJK, Direction};
use crate::Vec2d;
use std::f64; // For lround if we implement it, or f64::round
use std::i32; // For i32::MAX/MIN for overflow checks

/// IJK unit vectors corresponding to the 7 H3 digits (0-6).
#[rustfmt::skip] // Keep the C-style formatting for easy comparison
pub(crate) static UNIT_VECS: [CoordIJK; 7] = [
    CoordIJK { i: 0, j: 0, k: 0 },  // CENTER_DIGIT
    CoordIJK { i: 0, j: 0, k: 1 },  // K_AXES_DIGIT
    CoordIJK { i: 0, j: 1, k: 0 },  // J_AXES_DIGIT
    CoordIJK { i: 0, j: 1, k: 1 },  // JK_AXES_DIGIT (J_AXES_DIGIT | K_AXES_DIGIT)
    CoordIJK { i: 1, j: 0, k: 0 },  // I_AXES_DIGIT
    CoordIJK { i: 1, j: 0, k: 1 },  // IK_AXES_DIGIT (I_AXES_DIGIT | K_AXES_DIGIT)
    CoordIJK { i: 1, j: 1, k: 0 },  // IJ_AXES_DIGIT (I_AXES_DIGIT | J_AXES_DIGIT)
];

/// Sets an IJK coordinate to the specified component values.
#[inline]
pub(crate) fn _set_ijk(ijk: &mut CoordIJK, i: i32, j: i32, k: i32) {
  ijk.i = i;
  ijk.j = j;
  ijk.k = k;
}

/// Returns whether or not two IJK coordinates contain exactly the same
/// component values.
#[inline]
#[must_use]
pub(crate) fn _ijk_matches(c1: &CoordIJK, c2: &CoordIJK) -> bool {
  c1.i == c2.i && c1.j == c2.j && c1.k == c2.k
}

/// Add two IJK coordinates.
#[inline]
pub(crate) fn _ijk_add(h1: &CoordIJK, h2: &CoordIJK, sum: &mut CoordIJK) {
  sum.i = h1.i.saturating_add(h2.i);
  sum.j = h1.j.saturating_add(h2.j);
  sum.k = h1.k.saturating_add(h2.k);
}

/// Subtract two IJK coordinates. (h1 - h2)
#[inline]
pub(crate) fn _ijk_sub(h1: &CoordIJK, h2: &CoordIJK, diff: &mut CoordIJK) {
  diff.i = h1.i.saturating_sub(h2.i);
  diff.j = h1.j.saturating_sub(h2.j);
  diff.k = h1.k.saturating_sub(h2.k);
}

/// Uniformly scale IJK coordinates by a scalar. Works in place.
#[inline]
pub(crate) fn _ijk_scale(c: &mut CoordIJK, factor: i32) {
  c.i = c.i.saturating_mul(factor);
  c.j = c.j.saturating_mul(factor);
  c.k = c.k.saturating_mul(factor);
}

// Helper for _ijk_normalize_could_overflow and _ijk_normalize
// Equivalent to C's ADD_INT32S_OVERFLOWS
#[inline]
fn _add_i32s_overflows(a: i32, b: i32) -> bool {
  // From C: `if (a > 0) return INT32_MAX - a < b; else return INT32_MIN - a > b;`
  // Rust's checked_add returns None on overflow.
  a.checked_add(b).is_none()
}

// Helper for _ijk_normalize_could_overflow and _ijk_normalize
// Equivalent to C's SUB_INT32S_OVERFLOWS
#[inline]
fn _sub_i32s_overflows(a: i32, b: i32) -> bool {
  // From C: `if (a >= 0) return INT32_MIN + a >= b; else return INT32_MAX + a + 1 < b;`
  // Rust's checked_sub returns None on overflow.
  a.checked_sub(b).is_none()
}

/// Returns true if `_ijk_normalize` with the given input could have a signed
/// integer overflow. Assumes k is set to 0 before this call.
/// (This check is specific to the C implementation's _ijkNormalize logic).
#[inline]
#[must_use]
pub(crate) fn _ijk_normalize_could_overflow(ijk: &CoordIJK) -> bool {
  // Pre-condition from C code: ijk.k must be 0 for this check to be meaningful
  // in the context of _ijkNormalize, which sets k=0 before these subtractions.
  // The C macro versions are:
  // #define ADD_INT32S_OVERFLOWS(a,b) ( (a > 0) ? (INT32_MAX - a < b) : (INT32_MIN - a > b) )
  // #define SUB_INT32S_OVERFLOWS(a,b) ( (a >= 0) ? (INT32_MIN + a >= b) : (INT32_MAX + a + 1 < b) )

  // The C version's _ijkNormalize does this after potentially modifying i, j, k:
  // if (c->i < 0) { c->j -= c->i; c->k -= c->i; c->i = 0; }
  // This means `c->j` could become `c->j_orig - c->i_orig`.
  // The critical subtractions that _could_ overflow inside _ijkNormalize's logic are
  // when adjusting components based on other negative components.
  // E.g., `c->j -= c->i;` if `c->i` is very negative and `c->j` is positive.

  // Let's analyze the C _ijkNormalize carefully:
  // 1. if (c->i < 0) { c->j -= c->i; c->k -= c->i; c->i = 0; }
  //    Overflow if c->j - c->i overflows. `c->i` is negative. So `c->j + abs(c->i)`
  //    Could overflow if c->j is large positive and c->i is large negative.
  //    The check in C is `ADD_INT32S_OVERFLOWS(c->j, -c->i)` which is `c->j + (-c->i)`
  if ijk.i < 0 && _add_i32s_overflows(ijk.j, -ijk.i) {
    return true;
  }
  //    And `c->k -= c->i;` (k is initially 0). `0 - c->i`
  if ijk.i < 0 && _add_i32s_overflows(0, -ijk.i) {
    return true;
  } // Effectively -c->i

  // 2. if (c->j < 0) { c->i -= c->j; c->k -= c->j; c->j = 0; } (uses potentially modified c->i)
  //    Test with original values for simplicity of this pre-check.
  //    If ijk.j < 0 and ijk.i - ijk.j overflows:
  if ijk.j < 0 && _add_i32s_overflows(ijk.i, -ijk.j) {
    return true;
  }
  //    If ijk.j < 0 and 0 - ijk.j overflows (if k was 0, after i adjustment):
  if ijk.j < 0 && _add_i32s_overflows(0, -ijk.j) {
    return true;
  } // Effectively -ijk.j

  // 3. if (c->k < 0) { c->i -= c->k; c->j -= c->k; c->k = 0; } (uses potentially modified c->i, c->j)
  //    Test with original values for simplicity.
  //    If ijk.k < 0 (which it is, as 0 initially) and ijk.i - ijk.k overflows:
  //    This step is applied to k which might have become negative. Let's assume k is already 0 here.
  //    The C code version of this check is complex because it's trying to predict
  //    overflow within the _ijkNormalize steps.
  //    The C's `_ijkNormalizeCouldOverflow` seems to simplify this by checking original i,j (k=0):
  //    int max_val, min_val; if (ijk->i > ijk->j) { max_val = ijk->i; min_val = ijk->j; } else { ... }
  //    if (min_val < 0) {
  //        if (ADD_INT32S_OVERFLOWS(max_val, min_val)) return true; // max_val + min_val (not sub)
  //        if (SUB_INT32S_OVERFLOWS(0, min_val)) return true;       // 0 - min_val
  //        if (SUB_INT32S_OVERFLOWS(max_val, min_val)) return true; // max_val - min_val
  //    }

  let (max_val, min_val) = if ijk.i > ijk.j { (ijk.i, ijk.j) } else { (ijk.j, ijk.i) };
  if min_val < 0 {
    if _add_i32s_overflows(max_val, min_val) {
      return true;
    }
    if _sub_i32s_overflows(0, min_val) {
      return true;
    } // equivalent to -min_val if min_val is INT32_MIN
    if _sub_i32s_overflows(max_val, min_val) {
      return true;
    }
  }

  false
}

/// Normalizes IJK coordinates by setting the components to the smallest possible
/// values. Works in place.
///
/// This function does not protect against signed integer overflow if called directly
/// with arbitrary inputs; however, H3 library functions that call this internally
/// are responsible for input validation.
/// `_ijk_normalize_could_overflow` can be used as a pre-check.
#[inline]
pub fn _ijk_normalize(c: &mut CoordIJK) {
  // Simplify by finding the smallest component and subtracting it from all components
  // This makes one component 0 and the others non-negative.
  if c.i < 0 {
    c.j = c.j.saturating_sub(c.i);
    c.k = c.k.saturating_sub(c.i);
    c.i = 0;
  }

  if c.j < 0 {
    c.i = c.i.saturating_sub(c.j);
    c.k = c.k.saturating_sub(c.j);
    c.j = 0;
  }

  if c.k < 0 {
    c.i = c.i.saturating_sub(c.k);
    c.j = c.j.saturating_sub(c.k);
    c.k = 0;
  }

  // Remove the min value if needed
  let min_val = c.i.min(c.j).min(c.k);
  if min_val > 0 {
    c.i -= min_val;
    c.j -= min_val;
    c.k -= min_val;
  }
}

/// Determines the H3 digit corresponding to a unit vector or the zero vector
/// in IJK coordinates.
///
/// # Arguments
/// * `ijk` - The IJK coordinates; must be a unit vector or zero vector after normalization.
///
/// # Returns
/// The H3 `Direction` (0-6) corresponding to the IJK unit vector,
/// or `Direction::InvalidDigit` (7) on failure/if not a unit vector.
#[inline]
#[must_use]
pub(crate) fn _unit_ijk_to_digit(ijk: &CoordIJK) -> Direction {
  let mut c = *ijk; // Make a mutable copy
  _ijk_normalize(&mut c);

  // Corresponds to H3 C library's for loop check against UNIT_VECS
  if _ijk_matches(&c, &UNIT_VECS[0]) {
    Direction::Center
  } else if _ijk_matches(&c, &UNIT_VECS[1]) {
    Direction::KAxes
  } else if _ijk_matches(&c, &UNIT_VECS[2]) {
    Direction::JAxes
  } else if _ijk_matches(&c, &UNIT_VECS[3]) {
    Direction::JkAxes
  } else if _ijk_matches(&c, &UNIT_VECS[4]) {
    Direction::IAxes
  } else if _ijk_matches(&c, &UNIT_VECS[5]) {
    Direction::IkAxes
  } else if _ijk_matches(&c, &UNIT_VECS[6]) {
    Direction::IjAxes
  } else {
    Direction::InvalidDigit
  }
}

/// Find the normalized IJK coordinates of the hex in the specified digit
/// direction from the specified IJK coordinates. Works in place.
///
/// # Arguments
/// * `ijk` - The IJK coordinates. Modified in place.
/// * `digit` - The H3 `Direction` from the original IJK coordinates.
#[inline]
pub(crate) fn _neighbor(ijk: &mut CoordIJK, digit: Direction) {
  if digit != Direction::Center && digit != Direction::InvalidDigit {
    let temp_h2 = UNIT_VECS[digit as usize]; // temp_h2 is a copy from static
    let mut result = CoordIJK::default(); // Temporary variable for the sum
    _ijk_add(ijk, &temp_h2, &mut result); // Calculate sum into the temporary
    *ijk = result; // Assign the result back
    _ijk_normalize(ijk);
  }
}

/// Determine the containing hex in IJK+ coordinates for a 2D Cartesian
/// coordinate vector (from DGGRID).
///
/// # Arguments
/// * `v` - The 2D Cartesian coordinate vector.
/// * `h` - Output: The IJK+ coordinates of the containing hex.
#[inline]
#[inline]
pub(crate) fn _hex2d_to_coord_ijk(v: &Vec2d, h: &mut CoordIJK) {
  h.k = 0;

  let a1 = v.x.abs();
  let a2 = v.y.abs();

  let x2 = a2 * M_RSIN60;
  let x1 = a1 + x2 / 2.0;

  // Add H3_EPSILON to m1 and m2 before casting to int, to nudge floating
  // point values that are extremely close to an integer boundary.
  let m1 = (x1 + EPSILON) as i32;
  let m2 = (x2 + EPSILON) as i32;

  let r1 = x1 - m1 as f64;
  let r2 = x2 - m2 as f64;

  // Constants for region checks, matching C's direct usage of fractions
  const ONE_THIRD: f64 = 1.0 / 3.0; // Replaces FMOD_APPROX_R1 usages if they were different
  const TWO_THIRDS: f64 = 2.0 / 3.0; // Replaces FMOD_APPROX_R2 usages if they were different

  // Restructured to precisely match the C code's conditional blocks for r1, r2.
  if r1 < 0.5 {
    if r1 < ONE_THIRD {
      // Region I
      if r2 < (1.0 + r1) / 2.0 {
        h.i = m1;
        h.j = m2;
      } else {
        h.i = m1;
        h.j = m2 + 1;
      }
    } else {
      // Region II (1/3 <= r1 < 0.5)
      if r2 < (1.0 - r1) {
        h.j = m2;
      } else {
        h.j = m2 + 1;
      }
      // Common h.i logic for Region II based on C structure
      if ((1.0 - r1) <= r2) && (r2 < (2.0 * r1)) {
        h.i = m1 + 1;
      } else {
        h.i = m1;
      }
    }
  } else {
    // r1 >= 0.5
    if r1 < TWO_THIRDS {
      // Region III (0.5 <= r1 < 2/3)
      if r2 < (2.0 * r1 - 1.0) {
        h.j = m2 + 1;
      } else {
        h.j = m2;
      }
      // Common h.i logic for Region III based on C structure
      if ((2.0 * r1 - 1.0) < r2) && (r2 < (1.0 - r1)) {
        h.i = m1;
      } else {
        h.i = m1 + 1;
      }
    } else {
      // Region IV (r1 >= 2/3)
      h.i = m1 + 1;
      if r2 < (r1 / 2.0) {
        // Corrected this condition based on C
        h.j = m2;
      } else {
        h.j = m2 + 1;
      }
    }
  }

  // Folding logic (remains the same as it matched C)
  if v.x < 0.0 {
    if (h.j % 2) == 0 {
      // even j
      let axisi = h.j / 2;
      let diff = h.i - axisi;
      h.i -= 2 * diff; // Equivalent to h.i = h.i - 2 * diff
    } else {
      // odd j
      let axisi = (h.j + 1) / 2;
      let diff = h.i - axisi;
      h.i -= 2 * diff + 1; // Equivalent to h.i = h.i - (2 * diff + 1)
    }
  }
  if v.y < 0.0 {
    // In C: h.i = h.i - (2 * h.j + 1) / 2;
    // This integer division is equivalent to:
    // if (h.j < 0) { h.i -= (h.j - ( (h.j % 2 == 0) ? 0: 1) ) ; } else { h.i -= h.j; } NO this is too complex
    // Standard C integer division `(2*N+1)/2` is `N` for `N>=0`, and `N` for `N<0` IF `2N+1` is not odd negative.
    // Let's be explicit:
    let term = 2 * h.j + 1;
    // Integer division in Rust rounds towards zero.
    // C's division also rounds towards zero since C99.
    h.i -= term / 2;
    h.j = -h.j;
  }
  _ijk_normalize(h);
}

/// Find the center point in 2D Cartesian coordinates of a hex.
///
/// # Arguments
/// * `h` - The IJK coordinates of the hex.
/// * `v` - Output: The 2D Cartesian coordinates of the hex center point.
#[inline]
pub(crate) fn _ijk_to_hex2d(h: &CoordIJK, v: &mut Vec2d) {
  let i = h.i - h.k;
  let j = h.j - h.k;
  v.x = i as f64 - 0.5 * j as f64;
  v.y = j as f64 * M_SQRT3_2;
  // println!(
  //   "--- _ijk_to_hex2d --- Input IJK: {{i:{},j:{},k:{}}} -> Axial (i-k, j-k): ({},{}) -> v2d: {{x:{:.4}, y:{:.4}}}",
  //   h.i, h.j, h.k, i, j, v.x, v.y
  // );
}

// Helper for lround, C's lround might not be available or behave identically
// Rust's f64::round() rounds to nearest, ties to even.
// C99 lround rounds to nearest, ties away from zero.
// For H3, precise tie-breaking might be important if the C code relied on it.
// Given the usage, standard f64::round() is likely acceptable.
// If specific lround behavior is needed, we'd implement it.
// For now, let's assume f64::round() is okay for the Aperture functions.
fn h3_lround(val: f64) -> i32 {
  if val > 0.0 {
    (val + 0.5).floor() as i32
  } else {
    // val <= 0.0
    (val - 0.5).ceil() as i32
  }
}

fn lround_c99_style(val: f64) -> i32 {
  if val > 0.0 {
    (val + 0.5).floor() as i32
  } else if val < 0.0 {
    (val - 0.5).ceil() as i32
  } else {
    // val == 0.0 or -0.0
    0
  }
}

/// Find the normalized IJK coordinates of the indexing parent of a cell in a
/// counter-clockwise aperture 7 grid. Works in place.
/// (Corresponds to Class III resolution.)
///
/// # Arguments
/// * `ijk` - The IJK coordinates. Modified in place.
/// # Returns
/// An `H3Error` if an integer overflow occurs.
// In C, these were `_upAp7Checked` and `_upAp7rChecked` when returning H3Error.
// The non-checked versions didn't return error.
// Let's make them all checked for Rust.
pub(crate) fn _up_ap7_checked(ijk: &mut CoordIJK) -> Result<(), crate::types::H3Error> {
  // convert to CoordIJ (local, non-normalized)
  // These subtractions can't overflow if ijk components are within i32 range,
  // as the result will also be within i32 range.
  let i_ax = ijk.i - ijk.k;
  let j_ax = ijk.j - ijk.k;

  // check for int overflow before multiplying.
  // Max value for i_ax or j_ax can be 2*MAX_H3_RES_COORD_VAL approx.
  // 3 * i_ax, 2 * j_ax
  let term1_i = i_ax.checked_mul(3).ok_or(crate::types::H3Error::Failed)?;
  let new_i_num = term1_i.checked_sub(j_ax).ok_or(crate::types::H3Error::Failed)?;

  let term1_j = j_ax.checked_mul(2).ok_or(crate::types::H3Error::Failed)?;
  let new_j_num = i_ax.checked_add(term1_j).ok_or(crate::types::H3Error::Failed)?;

  ijk.i = lround_c99_style(new_i_num as f64 * M_ONESEVENTH);
  ijk.j = lround_c99_style(new_j_num as f64 * M_ONESEVENTH);
  ijk.k = 0;

  // Normalization involves subtractions that could overflow if intermediate i,j are pathological.
  // The C code had `_ijkNormalizeCouldOverflow` for this.
  // Our `_ijk_normalize` uses saturating arithmetic, so it won't panic,
  // but the result might differ from C's wrapping if C relied on specific wrapping.
  // Given H3's design, saturation or error is likely safer than wrapping.
  if _ijk_normalize_could_overflow(ijk) { // This check is based on the new i, j before normalize
     // It's hard to predict overflow *within* _ijk_normalize precisely without re-doing its logic.
     // If _ijk_normalize itself used checked_ops, it could return Result.
     // For now, _ijk_normalize uses saturating_ops.
  }
  _ijk_normalize(ijk);
  Ok(())
}

/// Find the normalized IJK coordinates of the indexing parent of a cell in a
/// clockwise aperture 7 grid. Works in place.
/// (Corresponds to Class II resolution.)
///
/// # Arguments
/// * `ijk` - The IJK coordinates. Modified in place.
/// # Returns
/// An `H3Error` if an integer overflow occurs.
pub(crate) fn _up_ap7r_checked(ijk: &mut CoordIJK) -> Result<(), crate::types::H3Error> {
  let i_ax = ijk.i - ijk.k;
  let j_ax = ijk.j - ijk.k;

  let term1_i = i_ax.checked_mul(2).ok_or(crate::types::H3Error::Failed)?;
  let new_i_num = term1_i.checked_add(j_ax).ok_or(crate::types::H3Error::Failed)?;

  let term1_j = j_ax.checked_mul(3).ok_or(crate::types::H3Error::Failed)?;
  let new_j_num = term1_j.checked_sub(i_ax).ok_or(crate::types::H3Error::Failed)?;

  ijk.i = lround_c99_style(new_i_num as f64 * M_ONESEVENTH);
  ijk.j = lround_c99_style(new_j_num as f64 * M_ONESEVENTH);
  ijk.k = 0;

  _ijk_normalize(ijk);
  Ok(())
}

// Non-checked versions (panic on overflow in debug, wrap in release, like C default)
// These are closer to the original C _upAp7 and _upAp7r signatures.
// For safety, it's better to use the checked versions internally if performance allows.

/// Find the normalized IJK coordinates of the indexing parent of a cell in a
/// counter-clockwise aperture 7 grid. Works in place. (Class III)
#[inline]
pub(crate) fn _up_ap7(ijk: &mut CoordIJK) {
  let i = ijk.i - ijk.k;
  let j = ijk.j - ijk.k;

  ijk.i = lround_c99_style((3 * i - j) as f64 * M_ONESEVENTH);
  ijk.j = lround_c99_style((i + 2 * j) as f64 * M_ONESEVENTH);
  ijk.k = 0;
  _ijk_normalize(ijk);
}

/// Find the normalized IJK coordinates of the indexing parent of a cell in a
/// clockwise aperture 7 grid. Works in place. (Class II)
#[inline]
pub(crate) fn _up_ap7r(ijk: &mut CoordIJK) {
  let i = ijk.i - ijk.k;
  let j = ijk.j - ijk.k;

  ijk.i = lround_c99_style((2 * i + j) as f64 * M_ONESEVENTH);
  ijk.j = lround_c99_style((3 * j - i) as f64 * M_ONESEVENTH);
  ijk.k = 0;
  _ijk_normalize(ijk);
}

/// Find the normalized IJK coordinates of the hex centered on the indicated
/// hex at the next finer aperture 7 counter-clockwise resolution. Works in place. (Class III)
#[inline]
pub(crate) fn _down_ap7(ijk: &mut CoordIJK) {
  // res r unit vectors in res r+1
  // Components are scaled based on the original ijk values.
  let i_vec = CoordIJK { i: 3, j: 0, k: 1 };
  let j_vec = CoordIJK { i: 1, j: 3, k: 0 };
  let k_vec = CoordIJK { i: 0, j: 1, k: 3 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk); // Store final result in ijk

  _ijk_normalize(ijk);
}

/// Find the normalized IJK coordinates of the hex centered on the indicated
/// hex at the next finer aperture 7 clockwise resolution. Works in place. (Class II)
#[inline]
pub(crate) fn _down_ap7r(ijk: &mut CoordIJK) {
  // res r unit vectors in res r+1
  let i_vec = CoordIJK { i: 3, j: 1, k: 0 };
  let j_vec = CoordIJK { i: 0, j: 3, k: 1 };
  let k_vec = CoordIJK { i: 1, j: 0, k: 3 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk);

  _ijk_normalize(ijk);
}

/// Find the normalized IJK coordinates of the hex centered on the indicated
/// hex at the next finer aperture 3 counter-clockwise resolution. Works in place.
#[inline]
pub(crate) fn _down_ap3(ijk: &mut CoordIJK) {
  // res r unit vectors in res r+1
  let i_vec = CoordIJK { i: 2, j: 0, k: 1 };
  let j_vec = CoordIJK { i: 1, j: 2, k: 0 };
  let k_vec = CoordIJK { i: 0, j: 1, k: 2 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk);

  _ijk_normalize(ijk);
}

/// Find the normalized IJK coordinates of the hex centered on the indicated
/// hex at the next finer aperture 3 clockwise resolution. Works in place.
#[inline]
pub(crate) fn _down_ap3r(ijk: &mut CoordIJK) {
  // res r unit vectors in res r+1
  let i_vec = CoordIJK { i: 2, j: 1, k: 0 };
  let j_vec = CoordIJK { i: 0, j: 2, k: 1 };
  let k_vec = CoordIJK { i: 1, j: 0, k: 2 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk);

  _ijk_normalize(ijk);
}

/// Rotates IJK coordinates 60 degrees counter-clockwise. Works in place.
#[inline]
pub(crate) fn _ijk_rotate60_ccw(ijk: &mut CoordIJK) {
  // unit vector rotations
  let i_vec = CoordIJK { i: 1, j: 1, k: 0 };
  let j_vec = CoordIJK { i: 0, j: 1, k: 1 };
  let k_vec = CoordIJK { i: 1, j: 0, k: 1 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk);

  _ijk_normalize(ijk);
}

/// Rotates IJK coordinates 60 degrees clockwise. Works in place.
#[inline]
pub(crate) fn _ijk_rotate60_cw(ijk: &mut CoordIJK) {
  // unit vector rotations
  let i_vec = CoordIJK { i: 1, j: 0, k: 1 };
  let j_vec = CoordIJK { i: 1, j: 1, k: 0 };
  let k_vec = CoordIJK { i: 0, j: 1, k: 1 };

  let mut temp_i = i_vec;
  _ijk_scale(&mut temp_i, ijk.i);
  let mut temp_j = j_vec;
  _ijk_scale(&mut temp_j, ijk.j);
  let mut temp_k = k_vec;
  _ijk_scale(&mut temp_k, ijk.k);

  let mut sum1 = CoordIJK::default();
  _ijk_add(&temp_i, &temp_j, &mut sum1);
  _ijk_add(&sum1, &temp_k, ijk);

  _ijk_normalize(ijk);
}

/// Rotates an H3 digit 60 degrees counter-clockwise.
#[inline]
#[must_use]
pub(crate) fn _rotate60_ccw(digit: Direction) -> Direction {
  use Direction::*;
  match digit {
    KAxes => IkAxes,
    IkAxes => IAxes,
    IAxes => IjAxes,
    IjAxes => JAxes,
    JAxes => JkAxes,
    JkAxes => KAxes,
    _ => digit, // Center or Invalid
  }
}

/// Rotates an H3 digit 60 degrees clockwise.
#[inline]
#[must_use]
pub(crate) fn _rotate60_cw(digit: Direction) -> Direction {
  use Direction::*;
  match digit {
    KAxes => JkAxes,
    JkAxes => JAxes,
    JAxes => IjAxes,
    IjAxes => IAxes,
    IAxes => IkAxes,
    IkAxes => KAxes,
    _ => digit, // Center or Invalid
  }
}

/// Finds the grid distance between the two IJK+ coordinates.
#[inline]
#[must_use]
pub(crate) fn ijk_distance(c1: &CoordIJK, c2: &CoordIJK) -> i32 {
  let mut diff = CoordIJK::default();
  _ijk_sub(c1, c2, &mut diff);
  _ijk_normalize(&mut diff); // Normalize to IJK "axial" coordinates.

  // The distance is the maximum component of the normalized difference.
  // Or, equivalently, (abs(i) + abs(j) + abs(k)) / 2, assuming sum i+j+k=0 for axial.
  // H3 C uses MAX(abs(i), abs(j), abs(k)).
  diff.i.abs().max(diff.j.abs()).max(diff.k.abs())
}

/// Transforms coordinates from the IJK+ coordinate system to the IJ coordinate
/// system. IJ coordinates are chosen relative to a given origin.
///
/// # Arguments
/// * `ijk` - The input IJK+ coordinates.
/// * `ij`  - Output: The IJ coordinates.
#[inline]
pub(crate) fn ijk_to_ij(ijk: &CoordIJK, ij: &mut CoordIJ) {
  // Note: This simple conversion assumes that IJK is already normalized
  // such that its k component can be ignored or is 0.
  // If ijk is an arbitrary H3 cell's local IJK, it might need
  // normalization first relative to its origin's IJK (which is (0,0,0)).
  // However, the C code appears to do `i = ijk->i - ijk->k; j = ijk->j - ijk->k;`
  // which is a standard way to convert from Cube/Axial (where i+j+k=0) to a 2-axis system.
  // Our _ijk_normalize ensures one component is 0. If k=0, then i=i, j=j.
  // If i=0, then new_i = 0 - k = -k; new_j = j - k.
  // If j=0, then new_i = i - k; new_j = 0 - k = -k.
  // The C code comment says "convert to IJ" where k is 0.
  // This implies normalized IJK where k has become 0 if it wasn't already minimal.
  ij.i = ijk.i - ijk.k;
  ij.j = ijk.j - ijk.k;
}

/// Transforms coordinates from the IJ coordinate system to the IJK+ coordinate system.
///
/// # Arguments
/// * `ij`  - The input IJ coordinates.
/// * `ijk` - Output: The IJK+ coordinates.
/// # Returns
/// An `H3Error` if an integer overflow would have occurred during normalization.
// Note: C's ijToIjk also has an error return for overflow, but only if a define is set.
// We make it always checked.
pub(crate) fn ij_to_ijk(ij: &CoordIJ, ijk: &mut CoordIJK) -> Result<(), crate::types::H3Error> {
  ijk.i = ij.i;
  ijk.j = ij.j;
  ijk.k = 0; // In IJK+, k is initially 0 before normalization.

  if _ijk_normalize_could_overflow(ijk) {
    // If we detect a potential overflow before normalization, return error.
    // This matches a pre-check pattern seen in some H3 C functions,
    // though not explicitly in the C ijToIjk which seems to assume inputs
    // don't cause overflow in its _ijkNormalize.
    // Our _ijk_normalize itself uses saturating arithmetic making it safe,
    // but if the *intent* of C was to error on such large pre-normalized values,
    // this check helps. For H3, typically inputs that would cause this are invalid.
    return Err(crate::types::H3Error::Failed); // Or a more specific error if available.
  }

  _ijk_normalize(ijk);
  Ok(())
}

/// Convert IJK coordinates to cube coordinates, in place.
/// Cube coordinates have the property that `i + j + k = 0`.
pub fn ijk_to_cube(ijk: &mut CoordIJK) {
  // axial coords = IJK+.i/j minus k
  let ia = ijk.i - ijk.k;
  let ja = ijk.j - ijk.k;
  // cube k = –(ia+ja)
  let ka = -ia - ja;
  ijk.i = ia;
  ijk.j = ja;
  ijk.k = ka;
}

/// Convert cube coordinates to IJK coordinates, in place.
/// Assumes input `ijk` holds cube coordinates where `i+j+k = 0`.
/// Output `ijk` will be H3's normalized IJK+ (minimal non-negative components).
/// Reverse the above: cube→IJK+ by negating i, zero'ing k, then normalizing
pub fn cube_to_ijk(ijk: &mut CoordIJK) {
  ijk.i = ijk.i.saturating_neg();
  // ijk.j remains as-is
  ijk.k = 0;
  _ijk_normalize(ijk);
}

#[cfg(test)]
mod tests {
  use crate::Vec2d;

  use super::*;

  #[test]
  fn test_set_ijk() {
    let mut ijk = CoordIJK::default();
    _set_ijk(&mut ijk, 1, 2, 3);
    assert_eq!(ijk, CoordIJK { i: 1, j: 2, k: 3 });
  }

  #[test]
  fn test_ijk_matches() {
    let c1 = CoordIJK { i: 1, j: 2, k: 3 };
    let c2 = CoordIJK { i: 1, j: 2, k: 3 };
    let c3 = CoordIJK { i: 4, j: 2, k: 3 };
    assert!(_ijk_matches(&c1, &c2));
    assert!(!_ijk_matches(&c1, &c3));
  }

  #[test]
  fn test_ijk_add() {
    let h1 = CoordIJK { i: 1, j: 2, k: -3 };
    let h2 = CoordIJK { i: 4, j: -5, k: 6 };
    let mut sum = CoordIJK::default();
    _ijk_add(&h1, &h2, &mut sum);
    assert_eq!(sum, CoordIJK { i: 5, j: -3, k: 3 });

    // Test saturation
    let h_max = CoordIJK {
      i: i32::MAX,
      j: 0,
      k: 0,
    };
    let h_one = CoordIJK { i: 1, j: 0, k: 0 };
    _ijk_add(&h_max, &h_one, &mut sum);
    assert_eq!(
      sum,
      CoordIJK {
        i: i32::MAX,
        j: 0,
        k: 0
      }
    );
  }

  #[test]
  fn test_ijk_sub() {
    let h1 = CoordIJK { i: 1, j: 2, k: -3 };
    let h2 = CoordIJK { i: 4, j: -5, k: 6 };
    let mut diff = CoordIJK::default();
    _ijk_sub(&h1, &h2, &mut diff);
    assert_eq!(diff, CoordIJK { i: -3, j: 7, k: -9 });

    // Test saturation
    let h_min = CoordIJK {
      i: i32::MIN,
      j: 0,
      k: 0,
    };
    let h_one = CoordIJK { i: 1, j: 0, k: 0 };
    _ijk_sub(&h_min, &h_one, &mut diff);
    assert_eq!(
      diff,
      CoordIJK {
        i: i32::MIN,
        j: 0,
        k: 0
      }
    );
  }

  #[test]
  fn test_ijk_scale() {
    let mut c = CoordIJK { i: 1, j: -2, k: 3 };
    _ijk_scale(&mut c, 2);
    assert_eq!(c, CoordIJK { i: 2, j: -4, k: 6 });
    _ijk_scale(&mut c, -1);
    assert_eq!(c, CoordIJK { i: -2, j: 4, k: -6 });
    _ijk_scale(&mut c, 0);
    assert_eq!(c, CoordIJK { i: 0, j: 0, k: 0 });

    // Test saturation
    let mut c_max = CoordIJK {
      i: i32::MAX / 2 + 1,
      j: 0,
      k: 0,
    };
    _ijk_scale(&mut c_max, 2);
    assert_eq!(
      c_max,
      CoordIJK {
        i: i32::MAX,
        j: 0,
        k: 0
      }
    );
  }

  #[test]
  fn test_ijk_normalize() {
    let mut c = CoordIJK { i: 0, j: 0, k: 0 };
    _ijk_normalize(&mut c);
    assert_eq!(c, CoordIJK { i: 0, j: 0, k: 0 }, "0,0,0 normalizes to 0,0,0");

    _set_ijk(&mut c, 2, 3, 4); // sum is 9
    _ijk_normalize(&mut c);
    assert_eq!(c, CoordIJK { i: 0, j: 1, k: 2 }, "positive components");

    _set_ijk(&mut c, -2, -3, -4); // sum is -9
    _ijk_normalize(&mut c);
    assert_eq!(c, CoordIJK { i: 2, j: 1, k: 0 }, "negative components");

    _set_ijk(&mut c, 2, -1, 0); // sum is 1
    _ijk_normalize(&mut c);
    assert_eq!(c, CoordIJK { i: 3, j: 0, k: 1 }, "mixed components (1)");

    _set_ijk(&mut c, 10, 20, 5); // sum = 35, min = 5
    _ijk_normalize(&mut c);
    assert_eq!(c, CoordIJK { i: 5, j: 15, k: 0 }, "remove min value");
  }

  #[test]
  fn test_ijk_normalize_saturating_behavior() {
    // Test cases that would overflow with standard arithmetic but should saturate
    let mut c = CoordIJK {
      i: i32::MIN,
      j: 1,
      k: 1,
    };
    _ijk_normalize(&mut c); // Expects i=0, j = 1-MIN, k = 1-MIN (both saturate to MAX)
    assert_eq!(
      c,
      CoordIJK {
        i: 0,
        j: i32::MAX,
        k: i32::MAX
      }
    );

    c = CoordIJK {
      i: 1,
      j: i32::MIN,
      k: 1,
    };
    _ijk_normalize(&mut c); // Expects i = 1-MIN (MAX), j = 0, k = 1-MIN (MAX)
    assert_eq!(
      c,
      CoordIJK {
        i: i32::MAX,
        j: 0,
        k: i32::MAX
      }
    );

    c = CoordIJK {
      i: 1,
      j: 1,
      k: i32::MIN,
    };
    _ijk_normalize(&mut c); // Expects i = 1-MIN (MAX), j = 1-MIN (MAX), k = 0
    assert_eq!(
      c,
      CoordIJK {
        i: i32::MAX,
        j: i32::MAX,
        k: 0
      }
    );

    c = CoordIJK {
      i: -10,
      j: i32::MIN, // -2147483648
      k: -20,
    };
    _ijk_normalize(&mut c);
    // <<< MODIFIED START >>>
    // The algorithm correctly produces these values. The previous expectation of i32::MAX was incorrect.
    assert_eq!(
      c,
      CoordIJK {
        i: 2147483638, // 0 - (i32::MIN - (-10))
        j: 0,
        k: 2147483628 // -20 - (i32::MIN - (-10) - (-10)) -> simplified: -10 - (i32::MIN - (-10))
                      // Trace: i=0, j=i32::MIN+10, k=-10
                      // then:  i=0-(i32::MIN+10), j=0, k=-10-(i32::MIN+10)
                      //       i = 2147483638, j=0, k = -10 + 2147483638 = 2147483628
      },
      "Normalization of {{i:-10, j:MIN, k:-20}} failed" // Added context to message
    );
    // <<< MODIFIED END >>>
  }

  // src/coords/ijk.rs
  // ...
  #[inline]
  #[must_use]
  pub(crate) fn _ijk_normalize_could_overflow(ijk: &CoordIJK) -> bool {
    // This function's C counterpart pre-checks for potential overflow in _ijkNormalize's C logic.
    // Since our _ijk_normalize uses saturating arithmetic, it's inherently safe from panics.
    // However, to match the C function's *predictive* behavior (which assumed wrapping arithmetic):

    let neg_i = ijk.i.checked_neg();
    let neg_j = ijk.j.checked_neg();
    // k is assumed 0 for input to this C check usually, but let's be general
    let neg_k = ijk.k.checked_neg();

    // Check step 1: if (c->i < 0) { c->j -= c->i; c->k -= c->i; c->i = 0; }
    if ijk.i < 0 {
      if neg_i.is_none() {
        return true;
      } // -i would overflow
      if _add_i32s_overflows(ijk.j, neg_i.unwrap()) {
        return true;
      }
      if _add_i32s_overflows(ijk.k, neg_i.unwrap()) {
        return true;
      } // In C, k is often 0 at this point
    }
    // Check step 2: if (c->j < 0) { c->i -= c->j; c->k -= c->j; c->j = 0; }
    if ijk.j < 0 {
      if neg_j.is_none() {
        return true;
      } // -j would overflow
      if _add_i32s_overflows(ijk.i, neg_j.unwrap()) {
        return true;
      }
      if _add_i32s_overflows(ijk.k, neg_j.unwrap()) {
        return true;
      }
    }
    // Check step 3: if (c->k < 0) { c->i -= c->k; c->j -= c->k; c->k = 0; }
    if ijk.k < 0 {
      if neg_k.is_none() {
        return true;
      } // -k would overflow
      if _add_i32s_overflows(ijk.i, neg_k.unwrap()) {
        return true;
      }
      if _add_i32s_overflows(ijk.j, neg_k.unwrap()) {
        return true;
      }
    }

    // The C `_ijkNormalizeCouldOverflow` also had a section with max_val/min_val.
    // This seemed to be a more general check.
    // Let's assume the above checks based on _ijkNormalize's steps are sufficient to catch
    // the same conditions C's more complex macro-based pre-check would.
    // The C version of this check with max_val/min_val for `k=0` simplifies to:
    // `if (min_val < 0)` where min_val is min(i,j)
    //   `ADD_INT32S_OVERFLOWS(max_val, min_val)` => max_val + min_val overflows
    //   `SUB_INT32S_OVERFLOWS(0, min_val)` => 0 - min_val overflows (same as -min_val)
    //   `SUB_INT32S_OVERFLOWS(max_val, min_val)` => max_val - min_val overflows
    if ijk.k == 0 {
      // If k=0 as is often assumed for this C check's input
      let (max_c, min_c) = if ijk.i > ijk.j { (ijk.i, ijk.j) } else { (ijk.j, ijk.i) };
      if min_c < 0 {
        if _add_i32s_overflows(max_c, min_c) {
          return true;
        }
        if min_c == i32::MIN && 0_i32.checked_sub(min_c).is_none() {
          return true;
        }
        // Special case for -MIN
        else if min_c != i32::MIN && _sub_i32s_overflows(0, min_c) {
          return true;
        }
        if _sub_i32s_overflows(max_c, min_c) {
          return true;
        }
      }
    }

    false
  }

  #[test]
  fn test_unit_ijk_to_digit() {
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 0, j: 0, k: 0 }),
      Direction::Center,
      "Center"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 0, j: 0, k: 1 }),
      Direction::KAxes,
      "K"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 0, j: 1, k: 0 }),
      Direction::JAxes,
      "J"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 0, j: 1, k: 1 }),
      Direction::JkAxes,
      "JK"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 1, j: 0, k: 0 }),
      Direction::IAxes,
      "I"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 1, j: 0, k: 1 }),
      Direction::IkAxes,
      "IK"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 1, j: 1, k: 0 }),
      Direction::IjAxes,
      "IJ"
    );

    // Test non-unit vectors after normalization
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 2, j: 2, k: 2 }),
      Direction::Center,
      "Unnormalized Center"
    ); // normalizes to 0,0,0
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 1, j: 1, k: 2 }),
      Direction::KAxes,
      "Unnormalized K"
    ); // normalizes to 0,0,1

    // Test invalid
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 2, j: 0, k: 0 }),
      Direction::InvalidDigit,
      "Not a unit vector (I=2)"
    );
    assert_eq!(
      _unit_ijk_to_digit(&CoordIJK { i: 1, j: 2, k: 3 }),
      Direction::InvalidDigit,
      "Not a unit vector (complex)"
    );
  }

  #[test]
  fn test_neighbor() {
    let mut ijk = CoordIJK { i: 0, j: 0, k: 0 };
    let initial_ijk = ijk;

    _neighbor(&mut ijk, Direction::Center);
    assert!(_ijk_matches(&ijk, &initial_ijk), "Center neighbor is self");

    _neighbor(&mut ijk, Direction::IAxes);
    assert!(
      _ijk_matches(&ijk, &UNIT_VECS[Direction::IAxes as usize]),
      "I neighbor as expected"
    );

    // Reset for next test
    ijk = initial_ijk;
    _neighbor(&mut ijk, Direction::InvalidDigit); // Should do nothing
    assert!(_ijk_matches(&ijk, &initial_ijk), "Invalid neighbor is self");

    // Test from non-origin
    _set_ijk(&mut ijk, 1, 1, 1); // Normalizes to 0,0,0
    _ijk_normalize(&mut ijk);
    _neighbor(&mut ijk, Direction::JAxes);
    assert!(
      _ijk_matches(&ijk, &UNIT_VECS[Direction::JAxes as usize]),
      "J neighbor from normalized (1,1,1)"
    );
  }

  // Test _hex2dToCoordIJK (ported from C testFaceIjk.c _hex2dToCoordIJK_coordinates_test)
  fn assert_hex2d_to_coord_ijk(v: Vec2d, expected_h: CoordIJK, msg: &str) {
    let mut h = CoordIJK::default();
    _hex2d_to_coord_ijk(&v, &mut h);
    assert!(
      _ijk_matches(&h, &expected_h),
      "{} - Expected ({},{},{}), Got ({},{},{})",
      msg,
      expected_h.i,
      expected_h.j,
      expected_h.k,
      h.i,
      h.j,
      h.k
    );
  }

  #[test]
  fn test_hex2d_to_coord_ijk() {
    assert_hex2d_to_coord_ijk(Vec2d { x: 0.0, y: 0.0 }, CoordIJK { i: 0, j: 0, k: 0 }, "origin");
    assert_hex2d_to_coord_ijk(Vec2d { x: 1.0, y: 0.0 }, CoordIJK { i: 1, j: 0, k: 0 }, "i axis");
    assert_hex2d_to_coord_ijk(Vec2d { x: -0.5, y: M_SQRT3_2 }, CoordIJK { i: 0, j: 1, k: 0 }, "j axis");
    assert_hex2d_to_coord_ijk(
      Vec2d { x: -0.5, y: -M_SQRT3_2 },
      CoordIJK { i: 0, j: 0, k: 1 },
      "k axis",
    );
    assert_hex2d_to_coord_ijk(Vec2d { x: 0.1, y: 0.1 }, CoordIJK { i: 0, j: 0, k: 0 }, "eps case");
    assert_hex2d_to_coord_ijk(Vec2d { x: -0.1, y: -0.1 }, CoordIJK { i: 0, j: 0, k: 0 }, "eps case N");
    assert_hex2d_to_coord_ijk(Vec2d { x: 0.1, y: -0.1 }, CoordIJK { i: 0, j: 0, k: 0 }, "eps case E");
    assert_hex2d_to_coord_ijk(Vec2d { x: -0.1, y: 0.1 }, CoordIJK { i: 0, j: 0, k: 0 }, "eps case W");

    assert_hex2d_to_coord_ijk(
      Vec2d { x: 0.5, y: M_SQRT3_2 },
      CoordIJK { i: 1, j: 1, k: 0 },
      "IJ axis (was 'on edge' with wrong expectation)",
    );
    assert_hex2d_to_coord_ijk(
      Vec2d { x: 0.4, y: M_SQRT3_2 },
      CoordIJK { i: 1, j: 1, k: 0 }, // Corrected expectation
      "pert on edge1 (closer to IJ than J)",
    );
    assert_hex2d_to_coord_ijk(
      Vec2d { x: 0.6, y: M_SQRT3_2 },
      CoordIJK { i: 1, j: 1, k: 0 },
      "pert on edge2",
    );
    assert_hex2d_to_coord_ijk(
      Vec2d { x: 1.0, y: M_SQRT3_2 },
      CoordIJK { i: 2, j: 2, k: 0 },
      "Shared vertex",
    );
  }

  // Test _ijkToHex2d (ported from C testFaceIjk.c _ijkToHex2d_roundtrip_test)
  fn assert_ijk_to_hex2d_roundtrip(h: CoordIJK, msg: &str) {
    let mut v = Vec2d::default();
    _ijk_to_hex2d(&h, &mut v);
    let mut h2 = CoordIJK::default();
    _hex2d_to_coord_ijk(&v, &mut h2);
    assert!(_ijk_matches(&h, &h2), "{} - roundtrip", msg);
  }

  #[test]
  fn test_ijk_to_hex2d_roundtrip() {
    assert_ijk_to_hex2d_roundtrip(CoordIJK { i: 0, j: 0, k: 0 }, "origin");
    for dir_enum_val in 1..=6 {
      // K_AXES_DIGIT to IJ_AXES_DIGIT
      let dir = unsafe { std::mem::transmute::<u8, Direction>(dir_enum_val as u8) };
      let unit_vec = UNIT_VECS[dir as usize];
      assert_ijk_to_hex2d_roundtrip(unit_vec, &format!("unit vec {:?}", dir));
    }
    assert_ijk_to_hex2d_roundtrip(CoordIJK { i: 2, j: 0, k: 1 }, "arbitrary"); // Normalized to (1,0,0)
  }

  #[test]
  fn test_up_ap7_checked() {
    let mut ijk = CoordIJK { i: 0, j: 0, k: 0 };
    assert!(_up_ap7_checked(&mut ijk).is_ok());
    assert_eq!(ijk, CoordIJK { i: 0, j: 0, k: 0 });

    // Example from C test (testH3IndexInternal.c#faceIjkToH3ExtremeCoordinates)
    // Where fijk.coord = {18,0,0} for res 2, upAp7r is called for class II (res 2)
    // and upAp7 is called for class III (res 1).
    // Let's test a similar progression. For _up_ap7 (class III):
    // Start with a fine res coord, e.g., equivalent to res 1 digit K (0,0,1) scaled up.
    // UNIT_VECS[1] = {0,0,1}. Let's scale it as if it's child of a res 0 cell at res 1.
    // This would mean its parent at res 0 is (0,0,0) after _up_ap7.
    let mut c = UNIT_VECS[Direction::KAxes as usize]; // (0,0,1)
    assert!(_up_ap7_checked(&mut c).is_ok());
    assert_eq!(c, CoordIJK { i: 0, j: 0, k: 0 }, "K axis upAp7");

    let mut c2 = UNIT_VECS[Direction::IAxes as usize]; // (1,0,0)
    assert!(_up_ap7_checked(&mut c2).is_ok());
    assert_eq!(c2, CoordIJK { i: 0, j: 0, k: 0 }, "I axis upAp7");

    // Test overflow scenarios. These are hard to hit with valid H3 cell coordinates
    // as they are typically small after normalization per resolution.
    // Using large raw numbers to test overflow checks.
    let mut large_i = CoordIJK {
      i: i32::MAX,
      j: 0,
      k: 0,
    };
    assert!(
      _up_ap7_checked(&mut large_i).is_err(),
      "Large i for up_ap7 should overflow"
    );

    let mut large_j = CoordIJK {
      i: 0,
      j: i32::MAX,
      k: 0,
    };
    assert!(
      _up_ap7_checked(&mut large_j).is_err(),
      "Large j for up_ap7 should overflow"
    );
  }

  #[test]
  fn test_up_ap7r_checked() {
    let mut ijk = CoordIJK { i: 0, j: 0, k: 0 };
    assert!(_up_ap7r_checked(&mut ijk).is_ok());
    assert_eq!(ijk, CoordIJK { i: 0, j: 0, k: 0 });

    let mut c = UNIT_VECS[Direction::KAxes as usize]; // (0,0,1)
    assert!(_up_ap7r_checked(&mut c).is_ok());
    assert_eq!(c, CoordIJK { i: 0, j: 0, k: 0 }, "K axis upAp7r");

    // Test overflow
    let mut large_i = CoordIJK {
      i: i32::MAX,
      j: 0,
      k: 0,
    };
    assert!(
      _up_ap7r_checked(&mut large_i).is_err(),
      "Large i for up_ap7r should overflow"
    );
  }

  fn test_down_aperture(
    ap_func: fn(&mut CoordIJK),
    ap_r_func: fn(&mut CoordIJK),
    expected_ijs: &[(i32, i32, i32)], // Expected IJK for Center, I, J, K after down-aperture
    label: &str,
  ) {
    // Test Center
    let mut center = CoordIJK { i: 0, j: 0, k: 0 };
    ap_func(&mut center);
    assert_eq!(
      center,
      CoordIJK {
        i: expected_ijs[0].0,
        j: expected_ijs[0].1,
        k: expected_ijs[0].2
      },
      "{}: Center",
      label
    );

    let mut center_r = CoordIJK { i: 0, j: 0, k: 0 };
    ap_r_func(&mut center_r);
    assert_eq!(
      center_r,
      CoordIJK {
        i: expected_ijs[0].0,
        j: expected_ijs[0].1,
        k: expected_ijs[0].2
      },
      "{}: Center (reversed)",
      label
    ); // Reversed should be same for center

    // Test I-axis (CoordIJK {1,0,0})
    let mut i_axis = UNIT_VECS[Direction::IAxes as usize];
    ap_func(&mut i_axis);
    assert_eq!(
      i_axis,
      CoordIJK {
        i: expected_ijs[1].0,
        j: expected_ijs[1].1,
        k: expected_ijs[1].2
      },
      "{}: I-axis",
      label
    );

    // Test J-axis (CoordIJK {0,1,0})
    let mut j_axis = UNIT_VECS[Direction::JAxes as usize];
    ap_func(&mut j_axis);
    assert_eq!(
      j_axis,
      CoordIJK {
        i: expected_ijs[2].0,
        j: expected_ijs[2].1,
        k: expected_ijs[2].2
      },
      "{}: J-axis",
      label
    );

    // Test K-axis (CoordIJK {0,0,1})
    let mut k_axis = UNIT_VECS[Direction::KAxes as usize];
    ap_func(&mut k_axis);
    assert_eq!(
      k_axis,
      CoordIJK {
        i: expected_ijs[3].0,
        j: expected_ijs[3].1,
        k: expected_ijs[3].2
      },
      "{}: K-axis",
      label
    );
  }

  #[test]
  fn test_down_ap7() {
    // Expected from C's _downAp7:
    // I: (3,0,1), J: (1,3,0), K: (0,1,3)
    // Center is always (0,0,0) after normalization
    let expected = [(0, 0, 0), (3, 0, 1), (1, 3, 0), (0, 1, 3)];
    test_down_aperture(_down_ap7, _down_ap7r, &expected, "downAp7");
  }

  #[test]
  fn test_down_ap3() {
    // Expected from C's _downAp3:
    // I: (2,0,1), J: (1,2,0), K: (0,1,2)
    let expected = [(0, 0, 0), (2, 0, 1), (1, 2, 0), (0, 1, 2)];
    test_down_aperture(_down_ap3, _down_ap3r, &expected, "downAp3");
  }

  #[test]
  fn test_ijk_rotate60() {
    let mut c = CoordIJK { i: 1, j: 0, k: 0 }; // I vector
    _ijk_rotate60_ccw(&mut c); // Should be IJ {1,1,0}
    assert_eq!(c, CoordIJK { i: 1, j: 1, k: 0 }, "I ccw is IJ");
    _ijk_rotate60_ccw(&mut c); // Should be J {0,1,0}
    assert_eq!(c, CoordIJK { i: 0, j: 1, k: 0 }, "IJ ccw is J");
    _ijk_rotate60_ccw(&mut c); // Should be JK {0,1,1}
    assert_eq!(c, CoordIJK { i: 0, j: 1, k: 1 }, "J ccw is JK");
    _ijk_rotate60_ccw(&mut c); // Should be K {0,0,1}
    assert_eq!(c, CoordIJK { i: 0, j: 0, k: 1 }, "JK ccw is K");
    _ijk_rotate60_ccw(&mut c); // Should be IK {1,0,1}
    assert_eq!(c, CoordIJK { i: 1, j: 0, k: 1 }, "K ccw is IK");
    _ijk_rotate60_ccw(&mut c); // Should be I {1,0,0} again
    assert_eq!(c, CoordIJK { i: 1, j: 0, k: 0 }, "IK ccw is I");

    // Now clockwise
    _ijk_rotate60_cw(&mut c); // Should be IK {1,0,1}
    assert_eq!(c, CoordIJK { i: 1, j: 0, k: 1 }, "I cw is IK");
  }

  #[test]
  fn test_rotate_digit() {
    assert_eq!(_rotate60_ccw(Direction::KAxes), Direction::IkAxes);
    assert_eq!(_rotate60_cw(Direction::KAxes), Direction::JkAxes);
    assert_eq!(_rotate60_ccw(Direction::Center), Direction::Center); // No change
    assert_eq!(_rotate60_cw(Direction::InvalidDigit), Direction::InvalidDigit); // No change
  }

  #[test]
  fn test_ijk_distance() {
    let z = CoordIJK { i: 0, j: 0, k: 0 };
    let i = CoordIJK { i: 1, j: 0, k: 0 };
    let ik = CoordIJK { i: 1, j: 0, k: 1 }; // normalize -> {1,0,1} -> {0, -1, 0} after sub? No, normalize {1,0,1} is still {1,0,1}
                                            // IJK axial coords. {1,0,1} is not normalized in H3 sense (sum of components !=0)
                                            // _ijk_normalize({1,0,1}) = {0,0,0} because min is 0, then i=1-0,j=0-0,k=1-0. No, min is 0.
                                            // _ijk_normalize({1,0,1}) = {1,0,1} because all non-negative, min is 0.
                                            // The inputs to ijkDistance are H3 IJK+ (normalized).
                                            // So, ik is an H3 IJK+.
    let ij = CoordIJK { i: 1, j: 1, k: 0 };
    let j2 = CoordIJK { i: 0, j: 2, k: 0 };

    assert_eq!(ijk_distance(&z, &z), 0, "identity distance 0,0,0");
    assert_eq!(ijk_distance(&i, &i), 0, "identity distance 1,0,0");
    assert_eq!(ijk_distance(&ik, &ik), 0, "identity distance 1,0,1");
    assert_eq!(ijk_distance(&ij, &ij), 0, "identity distance 1,1,0");
    assert_eq!(ijk_distance(&j2, &j2), 0, "identity distance 0,2,0");

    assert_eq!(ijk_distance(&z, &i), 1, "0,0,0 to 1,0,0");
    assert_eq!(ijk_distance(&z, &j2), 2, "0,0,0 to 0,2,0");
    assert_eq!(ijk_distance(&z, &ik), 1, "0,0,0 to 1,0,1"); // diff {1,0,1}, max_abs=1
    assert_eq!(ijk_distance(&i, &ik), 1, "1,0,0 to 1,0,1"); // diff {0,0,1}, max_abs=1
    assert_eq!(ijk_distance(&ik, &j2), 3, "1,0,1 to 0,2,0"); // diff {1,-2,1}, max_abs=2? No, normalize {1,-2,1}
                                                             // {1,-2,1} -> {1+2, 0, 1+2} = {3,0,3} -> {0, -3, 0}
                                                             // _ijk_sub(ik,j2) = {1,0,1} - {0,2,0} = {1,-2,1}
                                                             // _ijk_normalize({1,-2,1}):
                                                             //  i=1, j=-2, k=1
                                                             //  j<0: i=1-(-2)=3. k=1-(-2)=3. j=0. -> {3,0,3}
                                                             //  min=0. -> {3,0,3}
                                                             //  abs components are 3,0,3. Max is 3.
    assert_eq!(ijk_distance(&ij, &ik), 2, "1,1,0 to 1,0,1"); // diff {0,1,-1}
                                                             // _ijk_normalize({0,1,-1}):
                                                             //  i=0, j=1, k=-1
                                                             //  k<0: i=0-(-1)=1. j=1-(-1)=2. k=0. -> {1,2,0}
                                                             //  min=0. -> {1,2,0}
                                                             //  abs components are 1,2,0. Max is 2.
  }

  #[test]
  fn test_ijk_to_ij_and_back() {
    let mut orig_ijk = CoordIJK { i: 1, j: 2, k: 0 }; // Already H3 IJK+ normalized
    _ijk_normalize(&mut orig_ijk); // ensure it's minimal non-negative
    let mut ij = CoordIJ::default();
    let mut new_ijk = CoordIJK::default();

    ijk_to_ij(&orig_ijk, &mut ij);
    let res = ij_to_ijk(&ij, &mut new_ijk);
    assert!(res.is_ok());
    assert!(
      _ijk_matches(&orig_ijk, &new_ijk),
      "Roundtrip IJK->IJ->IJK should match for {:?}",
      orig_ijk
    );

    // Test with k being non-zero before normalization (but still i+j+k=0 for axial representation)
    let axial_k_non_zero = CoordIJK { i: 1, j: -3, k: 2 }; // Sum is 0
                                                           // H3 IJK+ needs one component to be 0 and others non-negative.
                                                           // We need to convert this axial to H3 IJK+ first to test fairly.
    let mut h3_ijk_from_axial = axial_k_non_zero;
    _ijk_normalize(&mut h3_ijk_from_axial); // {1, -3, 2} -> {1 - (-3), 0, 2 - (-3)} = {4,0,5} -> {0, -4, 1} (this logic is faulty)
                                            // _ijk_normalize of {1,-3,2}:
                                            // j<0 (-3): i_new = 1-(-3)=4. k_new = 2-(-3)=5. j_new=0. -> {4,0,5}
                                            // min=0. Stays {4,0,5}. This is a valid H3 IJK+.

    ijk_to_ij(&h3_ijk_from_axial, &mut ij); // ij.i = 4-5 = -1. ij.j = 0-5 = -5.
    let res2 = ij_to_ijk(&ij, &mut new_ijk);
    assert!(res2.is_ok());
    assert!(
      _ijk_matches(&h3_ijk_from_axial, &new_ijk),
      "Roundtrip with initial non-zero k (after H3 norm) for {:?}",
      h3_ijk_from_axial
    );
  }

  #[test]
  fn test_ijk_cube_transformations() {
    let mut h3_ijk_normalized = CoordIJK { i: 1, j: 2, k: 0 }; // Start with H3 normalized

    let mut cube_coords = h3_ijk_normalized;
    ijk_to_cube(&mut cube_coords);
    let expected_cube = CoordIJK { i: 1, j: 2, k: -3 };
    assert!(
      _ijk_matches(&cube_coords, &expected_cube),
      "ijkToCube transform failed. Expected {:?}, got {:?}",
      expected_cube,
      cube_coords
    );
    assert_eq!(
      cube_coords.i + cube_coords.j + cube_coords.k,
      0,
      "Cube coords should sum to 0"
    );

    let mut roundtrip_h3_ijk = cube_coords; // Start with the cube_coords we just got
    cube_to_ijk(&mut roundtrip_h3_ijk);
    // What does cubeToIjk({1,2,-3}) produce?
    // i = -1
    // j = 2
    // k = 0
    // normalize {-1,2,0} -> {0 - (-1), 2 - (-1), 0 - (-1)} if i<0,j<0,k<0 applied (this is wrong)
    // normalize {-1,2,0}:
    //   i<0: j=2-(-1)=3. k=0-(-1)=1. i=0. -> {0,3,1}
    //   min=0. -> {0,3,1}
    let expected_h3_ijk_after_roundtrip = CoordIJK { i: 0, j: 3, k: 1 };
    assert!(
      _ijk_matches(&roundtrip_h3_ijk, &expected_h3_ijk_after_roundtrip),
      "cubeToIjk transform failed for specific cube. Expected {:?}, got {:?}",
      expected_h3_ijk_after_roundtrip,
      roundtrip_h3_ijk
    );

    // This shows it's not an identity roundtrip from original h3_ijk_normalized
    assert!(!_ijk_matches(&h3_ijk_normalized, &roundtrip_h3_ijk));
  }
}