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
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
//#![warn(unsafe_code)]
//! Hilbert R-tree implementation using unsafe memory layout for performance.
//!
//! All unsafe operations are internal implementation details. The public API is safe.
//! Memory is managed in a single buffer with type-punned box structures and indices.
//! Buffer invariants are maintained throughout the tree's lifetime.
use std::mem::size_of;
use std::collections::VecDeque;
/// Box structure: minX, minY, maxX, maxY
#[derive(Clone, Copy, Debug)]
pub(crate) struct Box {
pub(crate) min_x: f64,
pub(crate) min_y: f64,
pub(crate) max_x: f64,
pub(crate) max_y: f64,
}
impl Box {
fn new(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> Self {
Self { min_x, min_y, max_x, max_y }
}
}
/// Hilbert R-tree for spatial queries - following flatbush algorithm
///
/// Memory layout (in single buffer):
/// - Header: 8 bytes (magic, version, `node_size`, `num_items`)
/// - All boxes: `num_total_nodes` * 32 bytes (4 f64 per box)
/// - All indices: `num_total_nodes` * 4 bytes (u32 per node)
///
/// Leaf nodes occupy positions [0, `num_items`), parent nodes appended after.
/// Tree is built bottom-up with Hilbert curve ordering for spatial locality.
#[derive(Clone, Debug)]
pub struct HilbertRTree {
/// Single buffer: header + boxes + indices
data: Vec<u8>,
/// Level boundaries: end position of each tree level
pub(crate) level_bounds: Vec<usize>,
/// Node size for tree construction
pub(crate) node_size: usize,
/// Number of leaf items
pub(crate) num_items: usize,
/// Current position during building
pub(crate) position: usize,
/// Bounding box of all items
pub(crate) bounds: Box,
/// Total nodes in tree (cached from level_bounds.last())
total_nodes: usize,
/// Pre-allocated capacity in bytes (0 if not pre-allocated)
allocated_capacity: usize,
}
const MAX_HILBERT: u32 = u16::MAX as u32;
const DEFAULT_NODE_SIZE: usize = 16;
const HEADER_SIZE: usize = 8; // bytes
/// Helper: Estimate total nodes in tree given item count
/// For a tree with node_size, total nodes ≈ N + N/node_size + N/node_size^2 + ...
/// This converges to: N * node_size / (node_size - 1)
#[inline]
fn estimate_total_nodes(num_items: usize, node_size: usize) -> usize {
if num_items == 0 {
return 0;
}
(num_items * node_size) / (node_size - 1) + 1
}
/// Helper: Calculate EXACT total nodes by simulating tree construction (O(log n) - tree depth)
#[inline]
fn calculate_exact_total_nodes(num_items: usize, node_size: usize) -> usize {
if num_items == 0 {
return 0;
}
let mut total_nodes = num_items;
let mut count = num_items;
loop {
count = count.div_ceil(node_size);
total_nodes += count;
if count <= 1 {
break;
}
}
total_nodes
}
/// Helper: Calculate required buffer size for estimated nodes
#[inline]
fn estimate_buffer_size(num_items: usize, node_size: usize) -> usize {
let estimated_nodes = estimate_total_nodes(num_items, node_size);
HEADER_SIZE + estimated_nodes * (size_of::<Box>() + size_of::<u32>())
}
impl HilbertRTree {
/// Creates a new empty Hilbert R-tree
pub fn new() -> Self {
Self::with_capacity(0)
}
/// Creates a new Hilbert R-tree with preallocated capacity
pub fn with_capacity(capacity: usize) -> Self {
let data = if capacity > 0 {
let needed_size = estimate_buffer_size(capacity, DEFAULT_NODE_SIZE);
Vec::with_capacity(needed_size)
} else {
Vec::new()
};
let allocated_capacity = data.capacity();
Self {
data,
allocated_capacity,
level_bounds: Vec::new(),
node_size: DEFAULT_NODE_SIZE,
num_items: 0,
position: 0,
bounds: Box::new(f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY),
total_nodes: 0,
}
}
/// Adds a bounding box to the tree
pub fn add(&mut self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) {
// Calculate required size for this item
let required_size = estimate_buffer_size(self.num_items + 1, self.node_size);
// Allocate if needed
if required_size > self.data.capacity() {
let new_capacity = (self.data.capacity() * 2).max(required_size);
self.data.reserve(new_capacity - self.data.capacity());
self.allocated_capacity = self.data.capacity();
}
// Ensure len is sufficient for writing at the position we need
let box_idx = HEADER_SIZE + self.num_items * size_of::<Box>();
let needed_len = box_idx + size_of::<Box>();
if needed_len > self.data.len() {
unsafe {
self.data.set_len(needed_len);
}
}
let box_ptr = &mut self.data[box_idx] as *mut u8 as *mut Box;
unsafe {
std::ptr::write_unaligned(box_ptr, Box::new(min_x, min_y, max_x, max_y));
}
self.bounds.min_x = self.bounds.min_x.min(min_x);
self.bounds.min_y = self.bounds.min_y.min(min_y);
self.bounds.max_x = self.bounds.max_x.max(max_x);
self.bounds.max_y = self.bounds.max_y.max(max_y);
self.num_items += 1;
}
/// Adds a point to the tree.
///
/// This is a convenience method for adding point data. A point is stored internally
/// as a degenerate bounding box where min_x==max_x and min_y==max_y. This method
/// makes the API clearer when working with point clouds and pairs well with the
/// optimized `query_circle_points()` and `query_nearest_k_points()` methods.
///
/// # Arguments
/// * `x` - X coordinate of the point
/// * `y` - Y coordinate of the point
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add_point(0.0, 0.0);
/// tree.add_point(1.0, 1.0);
/// tree.add_point(2.0, 2.0);
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_circle_points(0.0, 0.0, 1.5, &mut results);
/// // Results contain points 0 and 1 within distance 1.5
/// ```
pub fn add_point(&mut self, x: f64, y: f64) {
self.add(x, y, x, y);
}
/// Retrieves a point that was added with `add_point()`.
///
/// This is a convenience method for retrieving point data. Returns `Some((x, y))` if the
/// point exists, or `None` if the index is out of bounds.
///
/// # Arguments
/// * `item_id` - The index of the point (0-based, in order added)
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(2);
/// tree.add_point(1.5, 2.5);
/// tree.add_point(3.0, 4.0);
/// tree.build();
///
/// assert_eq!(tree.get_point(0), Some((1.5, 2.5)));
/// assert_eq!(tree.get_point(1), Some((3.0, 4.0)));
/// assert_eq!(tree.get_point(2), None);
/// ```
/// Gets the center point for a given item ID (for points added via add_point)
///
/// Returns the center coordinates (x, y) for the item with the given ID,
/// or None if the ID is out of bounds. This method is intended for items
/// that were added as points using add_point().
///
/// # Arguments
/// * `item_id` - The index of the point (0-based, in order added)
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(2);
/// tree.add_point(1.5, 2.5);
/// tree.add_point(3.0, 4.0);
/// tree.build();
///
/// assert_eq!(tree.get_point(0), Some((1.5, 2.5)));
/// assert_eq!(tree.get_point(1), Some((3.0, 4.0)));
/// assert_eq!(tree.get_point(2), None);
/// ```
pub fn get_point(&self, item_id: usize) -> Option<(f64, f64)> {
if let Some((min_x, min_y, _max_x, _max_y)) = self.get(item_id) {
// For points, min_x == max_x and min_y == max_y, so we can return either
Some((min_x, min_y))
} else {
None
}
}
/// Builds the Hilbert R-tree index
pub fn build(&mut self) {
if self.num_items == 0 {
return;
}
let num_items = self.num_items;
let node_size = self.node_size;
// Calculate exact total nodes needed (O(log n) - only tree depth iterations)
let total_nodes = calculate_exact_total_nodes(num_items, node_size);
let data_size = HEADER_SIZE + total_nodes * (size_of::<Box>() + size_of::<u32>());
// Reserve all needed space at once (avoids reallocation during build)
if data_size > self.data.capacity() {
self.data.reserve(data_size - self.data.capacity());
self.allocated_capacity = self.data.capacity();
}
// CRITICAL: Must zero-fill parent node memory. The build process writes parent nodes
// incrementally in the loop below, and during tree traversal we may read indices/boxes
// from parent positions before they're written. Zero values act as sentinels.
// This is unavoidable without additional bookkeeping to track which positions are initialized.
if self.data.len() < data_size {
self.data.resize(data_size, 0);
}
// Calculate level bounds
let mut level_bounds = Vec::with_capacity(16); // Max tree depth ~16 for 1M items
let mut count = num_items;
let mut level_total_nodes = num_items;
level_bounds.push(level_total_nodes);
// Create parent levels until we have a single root
loop {
count = count.div_ceil(node_size);
level_total_nodes += count;
level_bounds.push(level_total_nodes);
if count <= 1 {
break;
}
}
// Write header
self.data[0] = 0xfb; // magic
self.data[1] = 0x01; // version 1 + double type (8)
self.data[2..4].copy_from_slice(&(node_size as u16).to_le_bytes());
self.data[4..8].copy_from_slice(&(num_items as u32).to_le_bytes());
self.level_bounds = level_bounds;
self.position = 0;
self.total_nodes = total_nodes;
// If all items fit in one node, create a root level
if num_items <= node_size {
// Initialize all leaf indices first
let indices_start = HEADER_SIZE + total_nodes * size_of::<Box>();
for i in 0..num_items {
let idx_ptr = &mut self.data[indices_start + i * size_of::<u32>()] as *mut u8 as *mut u32;
unsafe {
std::ptr::write_unaligned(idx_ptr, i as u32);
}
}
// Write the root node box at position num_items
let root_idx = HEADER_SIZE + num_items * size_of::<Box>();
let root_ptr = &mut self.data[root_idx] as *mut u8 as *mut Box;
unsafe {
std::ptr::write_unaligned(root_ptr, self.bounds);
}
// Write the root node index (pointer to first child at position 0)
let root_idx_ptr = &mut self.data[indices_start + num_items * size_of::<u32>()] as *mut u8 as *mut u32;
unsafe {
std::ptr::write_unaligned(root_idx_ptr, 0_u32 << 2_u32); // First child at position 0
}
// For single-node case, no sorting happens
// No need to populate sorted_order since we use lazy lookup
return;
}
// Compute Hilbert values for leaves
let hilbert_width = MAX_HILBERT as f64 / (self.bounds.max_x - self.bounds.min_x);
let hilbert_height = MAX_HILBERT as f64 / (self.bounds.max_y - self.bounds.min_y);
let mut hilbert_values = Vec::with_capacity(num_items);
for i in 0..num_items {
let box_data = self.get_box(i);
let center_x = ((box_data.min_x + box_data.max_x) / 2.0 - self.bounds.min_x) * hilbert_width;
let center_y = ((box_data.min_y + box_data.max_y) / 2.0 - self.bounds.min_y) * hilbert_height;
let hx = center_x.max(0.0).min(MAX_HILBERT as f64 - 1.0) as u32;
let hy = center_y.max(0.0).min(MAX_HILBERT as f64 - 1.0) as u32;
hilbert_values.push(hilbert_xy_to_index(hx, hy));
}
// Create an indirection array to track sorting permutations
// This allows us to use Rust's optimized sort (introsort) instead of custom quicksort
let mut sort_indices: Vec<usize> = (0..num_items).collect();
// Sort indices by their corresponding Hilbert values
sort_indices.sort_unstable_by_key(|&i| hilbert_values[i]);
// Apply the permutation to boxes
let mut temp_data = Vec::with_capacity(num_items * size_of::<Box>());
unsafe {
temp_data.set_len(num_items * size_of::<Box>());
}
for (new_pos, &old_pos) in sort_indices.iter().enumerate() {
let old_box_idx = HEADER_SIZE + old_pos * size_of::<Box>();
let new_box_idx = new_pos * size_of::<Box>(); // new_pos from enumerate index
temp_data[new_box_idx..new_box_idx + size_of::<Box>()]
.copy_from_slice(&self.data[old_box_idx..old_box_idx + size_of::<Box>()]);
}
// Copy sorted boxes back to data
for i in 0..num_items {
let src_idx = i * size_of::<Box>();
let dst_idx = HEADER_SIZE + i * size_of::<Box>();
self.data[dst_idx..dst_idx + size_of::<Box>()]
.copy_from_slice(&temp_data[src_idx..src_idx + size_of::<Box>()]);
}
// Apply the same permutation to hilbert_values array to keep it in sync with boxes
// let mut temp_hilbert = Vec::with_capacity(num_items);
// for (new_pos, &old_pos) in sort_indices.iter().enumerate() {
// temp_hilbert.push(hilbert_values[old_pos]);
// }
// Note: We keep temp_hilbert for consistency but don't need it for later operations
// since the indices array contains the original box IDs
// Initialize leaf indices AFTER sorting - map new position to original box ID
let indices_start = HEADER_SIZE + total_nodes * size_of::<Box>();
// Note: We removed the sorted_order vector to save memory
// get() method now uses lazy search through indices when needed
for i in 0..num_items {
let idx_ptr = &mut self.data[indices_start + i * size_of::<u32>()] as *mut u8 as *mut u32;
unsafe {
// sort_indices[i] tells us which original box is now at position i
std::ptr::write_unaligned(idx_ptr, sort_indices[i] as u32);
}
}
// Build parent levels
let mut pos = 0_usize;
for level_idx in 0..self.level_bounds.len() - 1 {
let level_end = self.level_bounds[level_idx];
let mut parent_pos = level_end;
while pos < level_end {
let node_index = (pos as u32) << 2_u32; // for JS compatibility
let mut node_box = self.get_box(pos);
// Merge up to node_size children
for _ in 0..node_size {
if pos >= level_end {
break;
}
let child_box = self.get_box(pos);
node_box.min_x = node_box.min_x.min(child_box.min_x);
node_box.min_y = node_box.min_y.min(child_box.min_y);
node_box.max_x = node_box.max_x.max(child_box.max_x);
node_box.max_y = node_box.max_y.max(child_box.max_y);
pos += 1;
}
// Write parent node box
let box_idx = HEADER_SIZE + parent_pos * size_of::<Box>();
let box_ptr = &mut self.data[box_idx] as *mut u8 as *mut Box;
unsafe {
std::ptr::write_unaligned(box_ptr, node_box);
}
// Write parent node index
let idx_ptr = &mut self.data[indices_start + parent_pos * size_of::<u32>()] as *mut u8 as *mut u32;
unsafe {
std::ptr::write_unaligned(idx_ptr, node_index);
}
parent_pos += 1;
}
pos = level_end;
}
}
/// Returns the number of items
pub fn len(&self) -> usize {
self.num_items
}
/// Returns whether the tree is empty
pub fn is_empty(&self) -> bool {
self.num_items == 0
}
/// Gets the bounding box for a given item ID (0-based insertion order)
///
/// Returns the bounding box (min_x, min_y, max_x, max_y) for the item with the given ID,
/// or None if the ID is out of bounds.
///
/// # Arguments
/// * `item_id` - The index of the item (0-based, in order added)
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(2);
/// tree.add(1.0, 2.0, 3.0, 4.0);
/// tree.add(5.0, 6.0, 7.0, 8.0);
/// tree.build();
///
/// assert_eq!(tree.get(0), Some((1.0, 2.0, 3.0, 4.0)));
/// assert_eq!(tree.get(1), Some((5.0, 6.0, 7.0, 8.0)));
/// assert_eq!(tree.get(2), None);
/// ```
pub fn get(&self, item_id: usize) -> Option<(f64, f64, f64, f64)> {
if item_id >= self.num_items {
return None;
}
// If tree hasn't been built yet, items are in insertion order
if self.level_bounds.is_empty() {
let bbox = self.get_box(item_id);
return Some((bbox.min_x, bbox.min_y, bbox.max_x, bbox.max_y));
}
// After build(): search through leaf nodes to find which position has this item_id
for pos in 0..self.num_items {
if self.get_index(pos) as usize == item_id {
let bbox = self.get_box(pos);
return Some((bbox.min_x, bbox.min_y, bbox.max_x, bbox.max_y));
}
}
None
}
/// Finds all boxes that intersect with a given rectangular region.
///
/// This query returns all boxes whose bounding boxes overlap with the query rectangle,
/// including boxes that merely touch at edges or corners. This is useful for broad-phase
/// collision detection, finding objects in a viewport, or spatial filtering.
///
/// # Arguments
/// * `min_x` - Left edge of query rectangle
/// * `min_y` - Bottom edge of query rectangle
/// * `max_x` - Right edge of query rectangle
/// * `max_y` - Top edge of query rectangle
/// * `results` - Output vector; will be cleared and populated with matching box indices
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 2.0, 2.0); // Box 0
/// tree.add(1.0, 1.0, 3.0, 3.0); // Box 1
/// tree.add(4.0, 4.0, 5.0, 5.0); // Box 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_intersecting(0.5, 0.5, 2.5, 2.5, &mut results);
/// // Results include box 0 and 1 (both intersect the query rectangle)
/// ```
pub fn query_intersecting(
&self,
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() {
return;
}
// Query area heuristic for early termination decision
let query_area = (max_x - min_x) * (max_y - min_y);
let bounds_area = (self.bounds.max_x - self.bounds.min_x)
* (self.bounds.max_y - self.bounds.min_y);
// If query covers >50% of space, full scan is faster than hierarchical traversal
if query_area > bounds_area * 0.5 {
// Fast path: scan all leaf nodes directly
for pos in 0..self.num_items {
let node_box = self.get_box(pos);
if max_x >= node_box.min_x && max_y >= node_box.min_y
&& min_x <= node_box.max_x && min_y <= node_box.max_y
{
let index = self.get_index(pos);
results.push(index as usize);
}
}
return;
}
// Slow path: hierarchical traversal with pruning
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
// Find bounds of current level
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
// Process groups of 4 nodes at a time
let mut pos = node_index;
while pos + 4 <= end_pos {
let boxes = self.get_boxes_batch(pos);
for i in 0..4 {
let node_box = boxes[i];
if !(max_x < node_box.min_x || max_y < node_box.min_y
|| min_x > node_box.max_x || min_y > node_box.max_y)
{
let current_pos = pos + i;
let index = self.get_index(current_pos);
if current_pos < self.num_items {
results.push(index as usize);
} else {
queue.push_back((index >> 2) as usize);
}
}
}
pos += 4;
}
// Process remaining nodes (1-3) individually
while pos < end_pos {
let node_box = self.get_box(pos);
if !(max_x < node_box.min_x || max_y < node_box.min_y
|| min_x > node_box.max_x || min_y > node_box.max_y)
{
let index = self.get_index(pos);
if pos < self.num_items {
results.push(index as usize);
} else {
queue.push_back((index >> 2) as usize);
}
}
pos += 1;
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all boxes that intersect with a box already in the index.
///
/// This query is useful when you have an item already added to the tree and want to find
/// all other items that intersect with it, excluding the query item itself. It's more
/// convenient than calling `query_intersecting()` with manually extracted bounds, and
/// avoids redundant lookups and self-intersection checks.
///
/// # Arguments
/// * `item_id` - The index of an item already in the tree (0 to `num_items - 1`)
/// * `results` - Output vector; will be cleared and populated with matching box indices
/// (excluding the query item itself)
///
/// # Errors
/// Returns an error if `item_id >= num_items` (the item doesn't exist in the tree).
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 2.0, 2.0); // Item 0
/// tree.add(1.0, 1.0, 3.0, 3.0); // Item 1
/// tree.add(4.0, 4.0, 5.0, 5.0); // Item 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_intersecting_id(0, &mut results).unwrap();
/// // Results include item 1 (intersects with item 0), but not item 0 itself
/// ```
pub fn query_intersecting_id(
&self,
item_id: usize,
results: &mut Vec<usize>,
) -> Result<(), String> {
if item_id >= self.num_items {
return Err(format!("item_id {} is out of bounds (tree has {} items)", item_id, self.num_items));
}
// Get the bounding box of the query item
let query_box = self.get_box(item_id);
// Use the existing query_intersecting method with the box bounds
self.query_intersecting(query_box.min_x, query_box.min_y, query_box.max_x, query_box.max_y, results);
// Always exclude the query item itself (no self-intersections)
results.retain(|&idx| idx != item_id);
Ok(())
}
/// Finds the K nearest boxes to a point using Euclidean distance.
///
/// This query efficiently finds the K closest boxes to a given point by computing
/// the distance from the point to each box (distance to nearest point in box).
/// Boxes containing the point have distance 0. This is useful for finding nearby
/// objects, KNN queries, or closest match lookups.
///
/// # Arguments
/// * `point_x` - X coordinate of the query point
/// * `point_y` - Y coordinate of the query point
/// * `k` - Number of nearest boxes to find
/// * `results` - Output vector; will be cleared and populated with K nearest box indices,
/// sorted by distance (closest first)
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 1.0, 1.0); // Box 0
/// tree.add(2.0, 2.0, 3.0, 3.0); // Box 1
/// tree.add(5.0, 5.0, 6.0, 6.0); // Box 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_nearest_k(0.0, 0.0, 2, &mut results);
/// // Results contain the 2 nearest boxes
/// ```
pub fn query_nearest_k(
&self,
point_x: f64,
point_y: f64,
k: usize,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || k == 0 {
return;
}
use std::collections::BinaryHeap;
use std::cmp::Ordering;
// Priority queue entry: (distance, node_pos, is_leaf)
// Use reverse ordering so we get min-heap (closest items first)
#[derive(Debug, Clone, Copy)]
struct NodeEntry {
dist_sq: f64,
pos: usize,
is_leaf: bool,
}
impl Eq for NodeEntry {}
impl PartialEq for NodeEntry {
fn eq(&self, other: &Self) -> bool {
self.dist_sq == other.dist_sq && self.pos == other.pos
}
}
impl Ord for NodeEntry {
fn cmp(&self, other: &Self) -> Ordering {
// Reverse order for min-heap: larger distances sort first
other.dist_sq.partial_cmp(&self.dist_sq)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for NodeEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
// Result accumulator: use max-heap of (distance, index) to track K nearest
// When heap size > k, pop the farthest element
#[derive(Debug, Clone, Copy)]
struct ResultEntry {
dist_sq: f64,
idx: u32,
}
impl Eq for ResultEntry {}
impl PartialEq for ResultEntry {
fn eq(&self, other: &Self) -> bool {
self.dist_sq == other.dist_sq
}
}
impl Ord for ResultEntry {
fn cmp(&self, other: &Self) -> Ordering {
// Forward order for max-heap: smaller distances sort first
// This way we keep the K smallest distances
self.dist_sq.partial_cmp(&other.dist_sq)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for ResultEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
let mut queue = BinaryHeap::new();
let mut result_heap = BinaryHeap::new();
// Start at root level (highest level has fewest nodes)
let root_level = self.level_bounds.len() - 1;
let root_start = if root_level > 0 {
self.level_bounds[root_level - 1]
} else {
0
};
let root_end = self.level_bounds[root_level];
// Initialize queue with root nodes
for pos in root_start..root_end {
let node_box = self.get_box(pos);
let dx = self.axis_distance(point_x, node_box.min_x, node_box.max_x);
let dy = self.axis_distance(point_y, node_box.min_y, node_box.max_y);
let dist_sq = dx * dx + dy * dy;
queue.push(NodeEntry {
dist_sq,
pos,
is_leaf: false,
});
}
let mut max_dist_sq = f64::INFINITY;
// Traverse tree in priority order
while let Some(entry) = queue.pop() {
// Skip if this node is farther than our kth result
if entry.dist_sq > max_dist_sq {
// Early exit: once we have k results and current node is too far,
// all remaining nodes in the priority queue are even farther
if result_heap.len() == k {
break;
}
continue;
}
if entry.is_leaf {
// This is a leaf item - add to results
let index = self.get_index(entry.pos);
result_heap.push(ResultEntry {
dist_sq: entry.dist_sq,
idx: index,
});
// Keep only k results, removing farthest if we exceed k
if result_heap.len() > k {
result_heap.pop();
}
// Update max distance threshold
if result_heap.len() == k
&& let Some(&top) = result_heap.peek()
{
max_dist_sq = top.dist_sq;
}
} else {
// Internal node - add its children to queue
// Children span from level_bounds[level] to level_bounds[level+1]
// Find which level this node is at
let mut level = 0;
for i in 0..self.level_bounds.len() {
if entry.pos < self.level_bounds[i] {
level = i;
break;
}
}
if level > 0 {
let child_level_start = self.level_bounds[level - 1];
let child_level_end = if level == 0 {
self.num_items
} else {
self.level_bounds[level - 1]
};
// Internal nodes point to their children via indices
// Calculate which children belong to this parent node
// Node at position in this level has node_size children at next level down
let node_index = self.get_index(entry.pos);
let first_child = (node_index >> 2) as usize;
for child_idx in 0..self.node_size {
let child_pos = first_child + child_idx;
if child_pos >= child_level_end {
break;
}
let child_box = self.get_box(child_pos);
let dx = self.axis_distance(point_x, child_box.min_x, child_box.max_x);
let dy = self.axis_distance(point_y, child_box.min_y, child_box.max_y);
let dist_sq = dx * dx + dy * dy;
// Only add if within threshold or we haven't found k results yet
if dist_sq <= max_dist_sq || result_heap.len() < k {
let is_child_leaf = child_level_start == self.num_items;
queue.push(NodeEntry {
dist_sq,
pos: child_pos,
is_leaf: is_child_leaf,
});
}
}
} else {
// Children are leaf items
let first_child = (self.get_index(entry.pos) >> 2) as usize;
for child_idx in 0..self.node_size {
let child_pos = first_child + child_idx;
if child_pos >= self.num_items {
break;
}
let child_box = self.get_box(child_pos);
let dx = self.axis_distance(point_x, child_box.min_x, child_box.max_x);
let dy = self.axis_distance(point_y, child_box.min_y, child_box.max_y);
let dist_sq = dx * dx + dy * dy;
if dist_sq <= max_dist_sq || result_heap.len() < k {
queue.push(NodeEntry {
dist_sq,
pos: child_pos,
is_leaf: true,
});
}
}
}
}
}
// Extract results and sort by distance (ascending)
let mut distance_pairs: Vec<(f64, u32)> = result_heap
.into_iter()
.map(|entry| (entry.dist_sq, entry.idx))
.collect();
// Sort by distance ascending - this is already a partial sort since we only have k elements
distance_pairs.sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
for (_, idx) in distance_pairs {
results.push(idx as usize);
}
}
/// Finds the K nearest point items (stored as (x, x, y, y)) to a query point.
///
/// This is an optimized version of `query_nearest_k()` specifically for point data.
/// For point items where min_x==max_x and min_y==max_y, this method computes
/// distances directly without the axis_distance() helper function, providing
/// approximately 30% faster queries on point clouds.
///
/// **Important:** This method assumes all items in the tree are stored as points.
/// If the tree contains mixed data (both points and boxes), use `query_nearest_k()` instead
/// for correct results. Calling this on non-point data will produce incorrect results.
///
/// # Arguments
/// * `point_x` - X coordinate of the query point
/// * `point_y` - Y coordinate of the query point
/// * `k` - Number of nearest points to find
/// * `results` - Output vector; will be cleared and populated with K nearest point indices,
/// sorted by distance (closest first)
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 0.0, 0.0); // Point 0
/// tree.add(2.0, 2.0, 2.0, 2.0); // Point 1
/// tree.add(5.0, 5.0, 5.0, 5.0); // Point 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_nearest_k_points(0.0, 0.0, 2, &mut results);
/// // Results contain the 2 nearest points
/// ```
pub fn query_nearest_k_points(
&self,
point_x: f64,
point_y: f64,
k: usize,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || k == 0 {
return;
}
use std::collections::BinaryHeap;
use std::cmp::Ordering;
// Priority queue entry: (distance, node_pos, is_leaf)
// Use reverse ordering so we get min-heap (closest items first)
#[derive(Debug, Clone, Copy)]
struct NodeEntry {
dist_sq: f64,
pos: usize,
is_leaf: bool,
}
impl Eq for NodeEntry {}
impl PartialEq for NodeEntry {
fn eq(&self, other: &Self) -> bool {
self.dist_sq == other.dist_sq && self.pos == other.pos
}
}
impl Ord for NodeEntry {
fn cmp(&self, other: &Self) -> Ordering {
// Reverse order for min-heap: larger distances sort first
other.dist_sq.partial_cmp(&self.dist_sq)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for NodeEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
// Result accumulator: use max-heap of (distance, index) to track K nearest
// When heap size > k, pop the farthest element
#[derive(Debug, Clone, Copy)]
struct ResultEntry {
dist_sq: f64,
idx: u32,
}
impl Eq for ResultEntry {}
impl PartialEq for ResultEntry {
fn eq(&self, other: &Self) -> bool {
self.dist_sq == other.dist_sq
}
}
impl Ord for ResultEntry {
fn cmp(&self, other: &Self) -> Ordering {
// Forward order for max-heap: smaller distances sort first
// This way we keep the K smallest distances
self.dist_sq.partial_cmp(&other.dist_sq)
.unwrap_or(Ordering::Equal)
}
}
impl PartialOrd for ResultEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
let mut queue = BinaryHeap::new();
let mut result_heap = BinaryHeap::new();
// Start at root level (highest level has fewest nodes)
let root_level = self.level_bounds.len() - 1;
let root_start = if root_level > 0 {
self.level_bounds[root_level - 1]
} else {
0
};
let root_end = self.level_bounds[root_level];
// Initialize queue with root nodes
for pos in root_start..root_end {
let node_box = self.get_box(pos);
// For root nodes (parent nodes), use axis_distance for safety
let dx = self.axis_distance(point_x, node_box.min_x, node_box.max_x);
let dy = self.axis_distance(point_y, node_box.min_y, node_box.max_y);
let dist_sq = dx * dx + dy * dy;
queue.push(NodeEntry {
dist_sq,
pos,
is_leaf: false,
});
}
let mut max_dist_sq = f64::INFINITY;
// Traverse tree in priority order
while let Some(entry) = queue.pop() {
// Skip if this node is farther than our kth result
if entry.dist_sq > max_dist_sq {
// Early exit: once we have k results and current node is too far,
// all remaining nodes in the priority queue are even farther
if result_heap.len() == k {
break;
}
continue;
}
if entry.is_leaf {
// This is a leaf item - add to results
let index = self.get_index(entry.pos);
result_heap.push(ResultEntry {
dist_sq: entry.dist_sq,
idx: index,
});
// Keep only k results, removing farthest if we exceed k
if result_heap.len() > k {
result_heap.pop();
}
// Update max distance threshold
if result_heap.len() == k
&& let Some(&top) = result_heap.peek()
{
max_dist_sq = top.dist_sq;
}
} else {
// Internal node - add its children to queue
// Children span from level_bounds[level] to level_bounds[level+1]
// Find which level this node is at
let mut level = 0;
for i in 0..self.level_bounds.len() {
if entry.pos < self.level_bounds[i] {
level = i;
break;
}
}
if level > 0 {
let child_level_start = self.level_bounds[level - 1];
let child_level_end = if level == 0 {
self.num_items
} else {
self.level_bounds[level - 1]
};
// Internal nodes point to their children via indices
// Calculate which children belong to this parent node
// Node at position in this level has node_size children at next level down
let node_index = self.get_index(entry.pos);
let first_child = (node_index >> 2) as usize;
for child_idx in 0..self.node_size {
let child_pos = first_child + child_idx;
if child_pos >= child_level_end {
break;
}
let child_box = self.get_box(child_pos);
// For parent nodes (internal), use axis_distance; for leaf nodes, direct distance
let dist_sq = if child_pos < self.num_items {
// Leaf node - assume point
let dx = point_x - child_box.min_x;
let dy = point_y - child_box.min_y;
dx * dx + dy * dy
} else {
// Parent node
let dx = self.axis_distance(point_x, child_box.min_x, child_box.max_x);
let dy = self.axis_distance(point_y, child_box.min_y, child_box.max_y);
dx * dx + dy * dy
};
// Only add if within threshold or we haven't found k results yet
if dist_sq <= max_dist_sq || result_heap.len() < k {
let is_child_leaf = child_level_start == self.num_items;
queue.push(NodeEntry {
dist_sq,
pos: child_pos,
is_leaf: is_child_leaf,
});
}
}
} else {
// Children are leaf items
let first_child = (self.get_index(entry.pos) >> 2) as usize;
for child_idx in 0..self.node_size {
let child_pos = first_child + child_idx;
if child_pos >= self.num_items {
break;
}
let child_box = self.get_box(child_pos);
// For leaf items (points): direct distance calculation
let dx = point_x - child_box.min_x;
let dy = point_y - child_box.min_y;
let dist_sq = dx * dx + dy * dy;
if dist_sq <= max_dist_sq || result_heap.len() < k {
queue.push(NodeEntry {
dist_sq,
pos: child_pos,
is_leaf: true,
});
}
}
}
}
}
// Extract results and sort by distance (ascending)
let mut distance_pairs: Vec<(f64, u32)> = result_heap
.into_iter()
.map(|entry| (entry.dist_sq, entry.idx))
.collect();
// Sort by distance ascending
distance_pairs.sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
for (_, idx) in distance_pairs {
results.push(idx as usize);
}
}
/// Finds all boxes that contain a specific point.
///
/// This query returns all boxes whose boundaries include the given point.
/// A point on the edge or corner of a box is considered contained (inclusive test).
/// This is useful for hit testing, picking objects at a screen location, or
/// determining which regions contain a specific coordinate.
///
/// # Arguments
/// * `x` - X coordinate of the query point
/// * `y` - Y coordinate of the query point
/// * `results` - Output vector; will be cleared and populated with indices of all
/// boxes that contain the point
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(2);
/// tree.add(0.0, 0.0, 2.0, 2.0); // Box 0
/// tree.add(1.0, 1.0, 3.0, 3.0); // Box 1
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_point(1.5, 1.5, &mut results);
/// // Results contain both box 0 and box 1 (point is inside both)
/// ```
pub fn query_point(&self, x: f64, y: f64, results: &mut Vec<usize>) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() {
return;
}
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
// Check if point is inside box
if x < node_box.min_x || x > node_box.max_x ||
y < node_box.min_y || y > node_box.max_y {
continue;
}
let index = self.get_index(pos);
if pos >= self.num_items {
queue.push_back((index >> 2) as usize);
} else {
results.push(index as usize);
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all boxes that completely contain a given rectangular region.
///
/// This query returns all boxes whose boundaries fully enclose the query rectangle.
/// The query rectangle must be fully contained within each result box for inclusion.
/// This is useful for finding container regions, parent regions, or areas that
/// fully cover a given space.
///
/// # Arguments
/// * `min_x` - Left edge of query rectangle
/// * `min_y` - Bottom edge of query rectangle
/// * `max_x` - Right edge of query rectangle
/// * `max_y` - Top edge of query rectangle
/// * `results` - Output vector; will be cleared and populated with indices of boxes
/// that completely contain the query rectangle
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 5.0, 5.0); // Box 0 (large)
/// tree.add(1.0, 1.0, 4.0, 4.0); // Box 1 (medium)
/// tree.add(6.0, 6.0, 8.0, 8.0); // Box 2 (separate)
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_contain(1.5, 1.5, 3.5, 3.5, &mut results);
/// // Results contain box 0 and box 1 (both contain the query rectangle)
/// ```
pub fn query_contain(&self, min_x: f64, min_y: f64, max_x: f64, max_y: f64, results: &mut Vec<usize>) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() {
return;
}
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
// Check if node contains the query rectangle
if node_box.min_x <= min_x && node_box.max_x >= max_x &&
node_box.min_y <= min_y && node_box.max_y >= max_y {
let index = self.get_index(pos);
if pos >= self.num_items {
queue.push_back((index >> 2) as usize);
} else {
results.push(index as usize);
}
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all boxes that are completely contained within a given rectangle.
///
/// This query returns all boxes that fit entirely within the query rectangle's boundaries.
/// Each result box must be fully contained for inclusion. This is the opposite of
/// `query_contain` and is useful for finding items within a region, filtering
/// objects by area, or identifying sub-regions.
///
/// # Arguments
/// * `min_x` - Left edge of query rectangle
/// * `min_y` - Bottom edge of query rectangle
/// * `max_x` - Right edge of query rectangle
/// * `max_y` - Top edge of query rectangle
/// * `results` - Output vector; will be cleared and populated with indices of boxes
/// that are completely contained within the query rectangle
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 5.0, 5.0); // Box 0 (large)
/// tree.add(1.0, 1.0, 2.0, 2.0); // Box 1 (small, inside)
/// tree.add(6.0, 6.0, 8.0, 8.0); // Box 2 (outside)
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_contained_within(0.5, 0.5, 4.5, 4.5, &mut results);
/// // Results contain only box 1 (box 0 is too large, box 2 is outside)
/// ```
pub fn query_contained_within(&self, min_x: f64, min_y: f64, max_x: f64, max_y: f64, results: &mut Vec<usize>) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() {
return;
}
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
if pos >= self.num_items {
// This is a parent node - check if it could have matching children
// (any overlap with query region)
if node_box.max_x >= min_x && node_box.max_y >= min_y &&
node_box.min_x <= max_x && node_box.min_y <= max_y {
let index = self.get_index(pos);
queue.push_back((index >> 2) as usize);
}
} else {
// This is a leaf - check if fully contained
if node_box.min_x >= min_x && node_box.max_x <= max_x &&
node_box.min_y >= min_y && node_box.max_y <= max_y {
let index = self.get_index(pos);
results.push(index as usize);
}
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds the first K intersecting boxes within a rectangular region.
///
/// This query finds boxes intersecting a query rectangle and stops after collecting K results.
/// Unlike `query_intersecting` which finds all matches, this variant returns early when K
/// results are found, making it more efficient when only a limited number of results are needed.
/// Results are included in the order they are encountered during tree traversal.
///
/// # Arguments
/// * `min_x` - Left edge of query rectangle
/// * `min_y` - Bottom edge of query rectangle
/// * `max_x` - Right edge of query rectangle
/// * `max_y` - Top edge of query rectangle
/// * `k` - Maximum number of results to return
/// * `results` - Output vector; will be cleared and populated with up to K matching box indices
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(5);
/// tree.add(0.0, 0.0, 1.0, 1.0); // Box 0
/// tree.add(0.5, 0.5, 1.5, 1.5); // Box 1
/// tree.add(1.0, 1.0, 2.0, 2.0); // Box 2
/// tree.add(1.5, 1.5, 2.5, 2.5); // Box 3
/// tree.add(4.0, 4.0, 5.0, 5.0); // Box 4
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_intersecting_k(0.0, 0.0, 2.0, 2.0, 2, &mut results);
/// // Results contain at most 2 of the intersecting boxes
/// ```
pub fn query_intersecting_k(
&self,
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
k: usize,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || k == 0 {
return;
}
let mut queue = VecDeque::with_capacity(self.level_bounds.len() * 2);
let mut node_index = self.total_nodes - 1;
loop {
if results.len() >= k {
break;
}
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
if results.len() >= k {
break;
}
let node_box = self.get_box(pos);
if max_x < node_box.min_x || max_y < node_box.min_y ||
min_x > node_box.max_x || min_y > node_box.max_y {
continue;
}
let index = self.get_index(pos);
if pos < self.num_items {
results.push(index as usize);
} else {
queue.push_back((index >> 2) as usize);
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all boxes that intersect with a circular region.
///
/// This query returns all boxes whose bounding boxes intersect with the circle
/// centered at `(center_x, center_y)` with the given `radius`. The distance check
/// uses the Euclidean distance from the circle's center to the nearest point in each box.
/// This is useful for circular range queries, area-of-effect searches, and radial filtering.
///
/// # Arguments
/// * `center_x` - X coordinate of circle center
/// * `center_y` - Y coordinate of circle center
/// * `radius` - Radius of the circular region
/// * `results` - Output vector; will be cleared and populated with indices of all boxes
/// intersecting the circular region
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 1.0, 1.0); // Box 0
/// tree.add(1.0, 1.0, 2.0, 2.0); // Box 1
/// tree.add(5.0, 5.0, 6.0, 6.0); // Box 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_circle(1.0, 1.0, 1.5, &mut results);
/// // Results include boxes 0 and 1 (within circle), but not box 2
/// ```
pub fn query_circle(&self, center_x: f64, center_y: f64, radius: f64, results: &mut Vec<usize>) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || radius < 0.0 {
return;
}
let radius_sq = radius * radius;
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
// Distance from circle center to box
let dx = self.axis_distance(center_x, node_box.min_x, node_box.max_x);
let dy = self.axis_distance(center_y, node_box.min_y, node_box.max_y);
let dist_sq = dx * dx + dy * dy;
if dist_sq <= radius_sq {
let index = self.get_index(pos);
if pos >= self.num_items {
queue.push_back((index >> 2) as usize);
} else {
results.push(index as usize);
}
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all point items (stored as (x, x, y, y)) within a circular region.
///
/// This is an optimized version of `query_circle()` specifically for point data.
/// For point items where min_x==max_x and min_y==max_y, this method computes
/// distances directly without the axis_distance() helper function for leaf nodes.
/// For parent nodes, axis_distance is used to maintain correctness.
/// This optimization provides approximately 30% faster queries on point clouds.
///
/// **Important:** This method assumes all items in the tree are stored as points.
/// If the tree contains mixed data (both points and boxes), use `query_circle()` instead
/// for correct results. Calling this on non-point data will produce incorrect results.
///
/// # Arguments
/// * `center_x` - X coordinate of circle center
/// * `center_y` - Y coordinate of circle center
/// * `radius` - Radius of the circular region
/// * `results` - Output vector; will be cleared and populated with indices of all point items
/// within the circular region
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 0.0, 0.0); // Point 0
/// tree.add(1.0, 1.0, 1.0, 1.0); // Point 1
/// tree.add(5.0, 5.0, 5.0, 5.0); // Point 2
/// tree.build();
///
/// let mut results = Vec::new();
/// tree.query_circle_points(0.0, 0.0, 1.5, &mut results);
/// // Results include points 0 and 1 (within radius)
/// ```
pub fn query_circle_points(&self, center_x: f64, center_y: f64, radius: f64, results: &mut Vec<usize>) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || radius < 0.0 {
return;
}
let radius_sq = radius * radius;
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
// Use axis_distance for parent nodes (they may have min_x < max_x)
// Use direct distance only for leaf nodes (points have min_x == max_x)
let dist_sq = if pos < self.num_items {
// Leaf node - assume it's a point with min_x == max_x and min_y == max_y
let dx = center_x - node_box.min_x;
let dy = center_y - node_box.min_y;
dx * dx + dy * dy
} else {
// Parent node - use axis_distance for safety
let dx = self.axis_distance(center_x, node_box.min_x, node_box.max_x);
let dy = self.axis_distance(center_y, node_box.min_y, node_box.max_y);
dx * dx + dy * dy
};
if dist_sq <= radius_sq {
let index = self.get_index(pos);
if pos >= self.num_items {
queue.push_back((index >> 2) as usize);
} else {
results.push(index as usize);
}
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds all boxes that intersect with a rectangle's directional movement path.
///
/// This query simulates moving a rectangle from its initial position in a given direction
/// for a specified distance, and returns all boxes intersecting the swept path. The sweep
/// region is the bounding box of the rectangle at its start and end positions.
/// This is useful for collision detection along movement paths, ray casting, and
/// predictive spatial queries (e.g., finding obstacles in a moving object's path).
///
/// # Arguments
/// * `min_x` - Left edge of the rectangle
/// * `min_y` - Bottom edge of the rectangle
/// * `max_x` - Right edge of the rectangle
/// * `max_y` - Top edge of the rectangle
/// * `dir_x` - X component of movement direction vector
/// * `dir_y` - Y component of movement direction vector
/// * `distance` - Distance to move in the direction (direction is normalized internally)
/// * `results` - Output vector; will be cleared and populated with indices of all boxes
/// intersecting the swept movement path
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(3);
/// tree.add(0.0, 0.0, 1.0, 1.0); // Box 0
/// tree.add(3.0, 0.0, 4.0, 1.0); // Box 1 (in the path)
/// tree.add(5.0, 5.0, 6.0, 6.0); // Box 2 (not in the path)
/// tree.build();
///
/// let mut results = Vec::new();
/// // Move rectangle from (0,0)-(1,1) to the right for distance 3
/// tree.query_in_direction(0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 3.0, &mut results);
/// // Results include boxes 0 and 1
/// ```
pub fn query_in_direction(
&self,
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
dir_x: f64,
dir_y: f64,
distance: f64,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || distance < 0.0 {
return;
}
// Normalize direction vector
let dir_len_sq = dir_x * dir_x + dir_y * dir_y;
if dir_len_sq <= 0.0 {
return;
}
let dir_len = dir_len_sq.sqrt();
let norm_dir_x = dir_x / dir_len;
let norm_dir_y = dir_y / dir_len;
// Calculate movement vector
let dx = norm_dir_x * distance;
let dy = norm_dir_y * distance;
let sweep_min_x = min_x.min(min_x + dx);
let sweep_min_y = min_y.min(min_y + dy);
let sweep_max_x = max_x.max(max_x + dx);
let sweep_max_y = max_y.max(max_y + dy);
let mut queue = VecDeque::new();
let mut node_index = self.total_nodes - 1;
loop {
let node_end = self.upper_bound(node_index);
let end_pos = (node_index + self.node_size).min(node_end);
for pos in node_index..end_pos {
let node_box = self.get_box(pos);
// Check if box intersects the sweep area (AABB intersection)
if sweep_max_x < node_box.min_x || sweep_max_y < node_box.min_y ||
sweep_min_x > node_box.max_x || sweep_min_y > node_box.max_y {
continue;
}
let index = self.get_index(pos);
if pos >= self.num_items {
queue.push_back((index >> 2) as usize);
} else {
results.push(index as usize);
}
}
if queue.is_empty() {
break;
}
node_index = queue.pop_front().unwrap();
}
}
/// Finds the K nearest boxes intersecting a rectangle's directional movement path.
///
/// This query finds boxes that intersect with a moving rectangle's swept path and
/// returns the K closest ones ordered by distance along the movement direction.
/// Distance is computed as the projection of each box's center onto the normalized
/// direction vector, making results ordered by "how far along the path" each box is.
/// This is useful for ordered collision detection, finding the closest obstacles
/// in a movement path, or sequential hit detection in a sweep.
///
/// # Arguments
/// * `min_x` - Left edge of the rectangle
/// * `min_y` - Bottom edge of the rectangle
/// * `max_x` - Right edge of the rectangle
/// * `max_y` - Top edge of the rectangle
/// * `dir_x` - X component of movement direction vector
/// * `dir_y` - Y component of movement direction vector
/// * `k` - Maximum number of results to return
/// * `distance` - Distance to move in the direction (direction is normalized internally)
/// * `results` - Output vector; will be cleared and populated with up to K nearest box indices
/// sorted by distance along the movement direction
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(4);
/// tree.add(0.0, 0.0, 1.0, 1.0); // Box 0
/// tree.add(2.0, 0.0, 3.0, 1.0); // Box 1 (first in path)
/// tree.add(4.0, 0.0, 5.0, 1.0); // Box 2 (second in path)
/// tree.add(6.0, 6.0, 7.0, 7.0); // Box 3 (not in path)
/// tree.build();
///
/// let mut results = Vec::new();
/// // Move rectangle right, find 2 nearest obstacles
/// tree.query_in_direction_k(0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 2, 10.0, &mut results);
/// // Results contain boxes 1 and 2 (ordered by distance along direction)
/// ```
pub fn query_in_direction_k(
&self,
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
dir_x: f64,
dir_y: f64,
k: usize,
distance: f64,
results: &mut Vec<usize>,
) {
results.clear();
if self.num_items == 0 || self.level_bounds.is_empty() || distance < 0.0 || k == 0 {
return;
}
// Normalize direction vector
let dir_len_sq = dir_x * dir_x + dir_y * dir_y;
if dir_len_sq <= 0.0 {
results.clear();
return;
}
let dir_len = dir_len_sq.sqrt();
let norm_dir_x = dir_x / dir_len;
let norm_dir_y = dir_y / dir_len;
// Calculate movement vector
let dx = norm_dir_x * distance;
let dy = norm_dir_y * distance;
let sweep_min_x = min_x.min(min_x + dx);
let sweep_min_y = min_y.min(min_y + dy);
let sweep_max_x = max_x.max(max_x + dx);
let sweep_max_y = max_y.max(max_y + dy);
let mut candidates: Vec<(f64, usize)> = Vec::new();
let mut queue = Vec::with_capacity(self.level_bounds.len() * 2);
queue.push(self.total_nodes - 1);
while let Some(node_idx) = queue.pop() {
let node_end = self.upper_bound(node_idx);
let end_pos = (node_idx + self.node_size).min(node_end);
for pos in node_idx..end_pos {
let node_box = self.get_box(pos);
// Check if box intersects the sweep area (AABB intersection)
if sweep_max_x < node_box.min_x || sweep_max_y < node_box.min_y ||
sweep_min_x > node_box.max_x || sweep_min_y > node_box.max_y {
continue;
}
let index = self.get_index(pos) as usize;
if pos < self.num_items {
// Calculate distance to box center along direction
let box_center_x = (node_box.min_x + node_box.max_x) / 2.0;
let box_center_y = (node_box.min_y + node_box.max_y) / 2.0;
let relative_x = box_center_x - min_x;
let relative_y = box_center_y - min_y;
// Distance along direction (use normalized direction)
let dist_along_dir = relative_x * norm_dir_x + relative_y * norm_dir_y;
candidates.push((dist_along_dir, index));
} else {
queue.push(index >> 2);
}
}
}
// Partial sort: get K smallest elements by distance
results.clear();
if candidates.len() <= k {
// Fewer candidates than K, just sort all
candidates.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
for (_, idx) in candidates {
results.push(idx);
}
} else {
// More candidates than K, use partial sort
candidates.select_nth_unstable_by(k - 1, |a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
// Now split at position k and sort only the K elements
let (k_smallest, _) = candidates.split_at_mut(k);
k_smallest.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
for (_, idx) in k_smallest {
results.push(*idx);
}
}
}
/// Retrieves the bounding box for an item by its ID.
///
/// Returns the axis-aligned bounding box (min_x, min_y, max_x, max_y) for the item
/// at the given ID. This allows users to retrieve stored bounding boxes without
/// needing to store them separately.
///
/// # Arguments
/// * `item_id` - The ID of the item (0 to `num_items - 1`)
///
/// # Returns
/// `Some((min_x, min_y, max_x, max_y))` if the item exists, `None` if the ID is out of bounds.
///
/// # Example
/// ```
/// use aabb::prelude::*;
/// let mut tree = AABB::with_capacity(2);
/// tree.add(0.0, 0.0, 2.0, 2.0); // Item 0
/// tree.add(1.0, 1.0, 3.0, 3.0); // Item 1
/// tree.build();
///
/// let bbox = tree.get(0).unwrap();
/// assert_eq!(bbox, (0.0, 0.0, 2.0, 2.0));
/// ```
// Note: get() method removed to eliminate sorted_order dependency
// Use spatial query methods instead for accessing data
/// Get box at position using read_unaligned
#[inline(always)]
pub(crate) fn get_box(&self, pos: usize) -> Box {
let idx = HEADER_SIZE + pos * size_of::<Box>();
unsafe {
std::ptr::read_unaligned(&self.data[idx] as *const u8 as *const Box)
}
}
/// Get 4 boxes at once for batch processing - single read_unaligned call
#[allow(dead_code)]
#[inline]
pub(crate) fn get_boxes_batch(&self, start_pos: usize) -> [Box; 4] {
let base_idx = HEADER_SIZE + start_pos * size_of::<Box>();
unsafe {
std::ptr::read_unaligned(self.data.as_ptr().add(base_idx) as *const [Box; 4])
}
}
/// Get index at position using read_unaligned
#[inline(always)]
pub(crate) fn get_index(&self, pos: usize) -> u32 {
let indices_start = HEADER_SIZE + self.total_nodes * size_of::<Box>();
unsafe {
std::ptr::read_unaligned(&self.data[indices_start + pos * size_of::<u32>()] as *const u8 as *const u32)
}
}
/// Get distance along an axis
#[inline(always)]
fn axis_distance(&self, coordinate: f64, min: f64, max: f64) -> f64 {
if coordinate < min {
min - coordinate
} else if coordinate > max {
coordinate - max
} else {
0.0
}
}
/// Find upper bound of a node in `level_bounds`
#[inline(always)]
fn upper_bound(&self, node_index: usize) -> usize {
// Binary search: find first level_bound > node_index
let idx = self.level_bounds.partition_point(|&bound| bound <= node_index);
if idx < self.level_bounds.len() {
self.level_bounds[idx]
} else {
self.total_nodes
}
}
/// Saves the built Hilbert R-tree to a file.
///
/// Serializes the complete tree structure including the header, buffer, metadata, and level bounds
/// to enable fast loading without rebuilding. The file format includes a magic number and version
/// for integrity checking during load.
///
/// # Arguments
/// * `path` - File path where the tree will be saved
///
/// # Errors
/// Returns an error if the file cannot be created or written to.
///
/// # Example
/// ```ignore
/// let mut tree = HilbertRTree::with_capacity(3);
/// tree.add(0.0, 0.0, 1.0, 1.0);
/// tree.add(1.0, 1.0, 2.0, 2.0);
/// tree.build();
/// tree.save("my_tree.bin")?;
/// ```
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> std::io::Result<()> {
use std::io::Write;
let mut file = std::fs::File::create(path)?;
// Write magic number and version (file header for validation)
file.write_all(&[0xfb])?; // magic (f64 variant)
file.write_all(&[0x01])?; // version 1 (f64 variant)
// Write node_size
file.write_all(&(self.node_size as u32).to_le_bytes())?;
// Write num_items
file.write_all(&(self.num_items as u32).to_le_bytes())?;
// Write total_nodes
file.write_all(&(self.total_nodes as u32).to_le_bytes())?;
// Write level_bounds length
file.write_all(&(self.level_bounds.len() as u32).to_le_bytes())?;
// Write level_bounds
for &bound in &self.level_bounds {
file.write_all(&(bound as u32).to_le_bytes())?;
}
// Write bounds
file.write_all(&self.bounds.min_x.to_le_bytes())?;
file.write_all(&self.bounds.min_y.to_le_bytes())?;
file.write_all(&self.bounds.max_x.to_le_bytes())?;
file.write_all(&self.bounds.max_y.to_le_bytes())?;
// Write data buffer
file.write_all(&(self.data.len() as u32).to_le_bytes())?;
file.write_all(&self.data)?;
Ok(())
}
/// Loads a Hilbert R-tree from a file.
///
/// Deserializes a tree that was previously saved with `save()`.
/// Validates the file format by checking the magic number and version.
/// The loaded tree is immediately ready for querying without rebuilding.
///
/// # Arguments
/// * `path` - File path of the saved tree
///
/// # Errors
/// Returns an error if the file cannot be read, the format is invalid,
/// or the magic number/version check fails.
///
/// # Example
/// ```ignore
/// let tree = HilbertRTree::load("my_tree.bin")?;
/// let mut results = Vec::new();
/// tree.query_intersecting(0.0, 0.0, 1.0, 1.0, &mut results);
/// ```
pub fn load<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<Self> {
use std::io::Read;
let mut file = std::fs::File::open(path)?;
// Read and validate magic number
let mut magic_buf = [0u8; 1];
file.read_exact(&mut magic_buf)?;
if magic_buf[0] != 0xfb {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid file format: magic number mismatch",
));
}
// Read and validate version
let mut version_buf = [0u8; 1];
file.read_exact(&mut version_buf)?;
if version_buf[0] != 0x01 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Unsupported file version (expected f64 variant v1, got different version)",
));
}
// Read node_size
let mut buf = [0u8; 4];
file.read_exact(&mut buf)?;
let node_size = u32::from_le_bytes(buf) as usize;
// Read num_items
file.read_exact(&mut buf)?;
let num_items = u32::from_le_bytes(buf) as usize;
// Read total_nodes
file.read_exact(&mut buf)?;
let total_nodes = u32::from_le_bytes(buf) as usize;
// Read level_bounds length
file.read_exact(&mut buf)?;
let level_bounds_len = u32::from_le_bytes(buf) as usize;
// Read level_bounds
let mut level_bounds = Vec::with_capacity(level_bounds_len);
for _ in 0..level_bounds_len {
file.read_exact(&mut buf)?;
level_bounds.push(u32::from_le_bytes(buf) as usize);
}
// Read bounds
let mut f64_buf = [0u8; 8];
file.read_exact(&mut f64_buf)?;
let min_x = f64::from_le_bytes(f64_buf);
file.read_exact(&mut f64_buf)?;
let min_y = f64::from_le_bytes(f64_buf);
file.read_exact(&mut f64_buf)?;
let max_x = f64::from_le_bytes(f64_buf);
file.read_exact(&mut f64_buf)?;
let max_y = f64::from_le_bytes(f64_buf);
let bounds = Box::new(min_x, min_y, max_x, max_y);
// Read data buffer
file.read_exact(&mut buf)?;
let data_len = u32::from_le_bytes(buf) as usize;
let mut data = vec![0u8; data_len];
file.read_exact(&mut data)?;
Ok(Self {
data,
level_bounds,
node_size,
num_items,
position: 0,
bounds,
total_nodes,
allocated_capacity: data_len,
})
}
}
impl Default for HilbertRTree {
fn default() -> Self {
Self::new()
}
}
/// Hilbert curve index computation
/// From <https://github.com/rawrunprotected/hilbert_curves> (public domain)
#[inline(always)]
fn interleave(mut x: u32) -> u32 {
x = (x | (x << 8)) & 0x00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F;
x = (x | (x << 2)) & 0x33333333;
x = (x | (x << 1)) & 0x55555555;
x
}
#[expect(non_snake_case)]
#[inline]
fn hilbert_xy_to_index(x: u32, y: u32) -> u32 {
// Initial prefix scan round, prime with x and y
let mut a = x ^ y;
let mut b = 0xFFFF ^ a;
let mut c = 0xFFFF ^ (x | y);
let mut d = x & (y ^ 0xFFFF);
let mut A = a | (b >> 1);
let mut B = (a >> 1) ^ a;
let mut C = ((c >> 1) ^ (b & (d >> 1))) ^ c;
let mut D = ((a & (c >> 1)) ^ (d >> 1)) ^ d;
a = A;
b = B;
c = C;
d = D;
A = (a & (a >> 2)) ^ (b & (b >> 2));
B = (a & (b >> 2)) ^ (b & ((a ^ b) >> 2));
C ^= (a & (c >> 2)) ^ (b & (d >> 2));
D ^= (b & (c >> 2)) ^ ((a ^ b) & (d >> 2));
a = A;
b = B;
c = C;
d = D;
A = (a & (a >> 4)) ^ (b & (b >> 4));
B = (a & (b >> 4)) ^ (b & ((a ^ b) >> 4));
C ^= (a & (c >> 4)) ^ (b & (d >> 4));
D ^= (b & (c >> 4)) ^ ((a ^ b) & (d >> 4));
// Final round and projection
a = A;
b = B;
c = C;
d = D;
C ^= (a & (c >> 8)) ^ (b & (d >> 8));
D ^= (b & (c >> 8)) ^ ((a ^ b) & (d >> 8));
// Undo transformation prefix scan
a = C ^ (C >> 1);
b = D ^ (D >> 1);
// Recover index bits
let i0 = x ^ y;
let i1 = b | (0xFFFF ^ (i0 | a));
(interleave(i1) << 1) | interleave(i0)
}