oxiphysics-geometry 0.1.2

Geometric shape types for the OxiPhysics engine
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
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Spatial hashing, loose octree, k-nearest neighbours, ray grid traversal,
//! and spatial statistics.

use std::collections::{BinaryHeap, HashMap};

/// Type alias for the 3-D integer cell key used in spatial hashes.
type CellKey = (i32, i32, i32);

// ─── Helper math ─────────────────────────────────────────────────────────────

fn dist_sq(a: [f64; 3], b: [f64; 3]) -> f64 {
    (a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2) + (a[2] - b[2]).powi(2)
}

// ─── SpatialHash3D ────────────────────────────────────────────────────────────

/// A uniform-grid spatial hash that maps 3-D positions to arbitrary data.
///
/// Items are bucketed by their cell coordinate
/// `(floor(x/cell_size), floor(y/cell_size), floor(z/cell_size))`.
pub struct SpatialHash3D<T: Clone> {
    /// Side length of each cubic cell.
    pub cell_size: f64,
    cells: HashMap<CellKey, Vec<(usize, T)>>,
    /// Total number of items currently stored.
    pub n_items: usize,
}

impl<T: Clone> SpatialHash3D<T> {
    /// Creates a new empty spatial hash with the given cell size.
    pub fn new(cell_size: f64) -> Self {
        Self {
            cell_size,
            cells: HashMap::new(),
            n_items: 0,
        }
    }

    /// Maps a single coordinate to its cell index via `floor(x / cell_size)`.
    pub fn cell_coord(&self, x: f64) -> i32 {
        (x / self.cell_size).floor() as i32
    }

    /// Returns the 3-D cell key for a world-space position.
    pub fn cell_key(&self, pos: [f64; 3]) -> (i32, i32, i32) {
        (
            self.cell_coord(pos[0]),
            self.cell_coord(pos[1]),
            self.cell_coord(pos[2]),
        )
    }

    /// Inserts `(id, data)` at `pos` and returns the cell key it was placed in.
    pub fn insert(&mut self, id: usize, pos: [f64; 3], data: T) -> (i32, i32, i32) {
        let key = self.cell_key(pos);
        self.cells.entry(key).or_default().push((id, data));
        self.n_items += 1;
        key
    }

    /// Removes the entry with the given `id` from the cell that contains `pos`.
    pub fn remove(&mut self, id: usize, pos: [f64; 3]) {
        let key = self.cell_key(pos);
        if let Some(bucket) = self.cells.get_mut(&key) {
            let before = bucket.len();
            bucket.retain(|(item_id, _)| *item_id != id);
            let removed = before - bucket.len();
            self.n_items = self.n_items.saturating_sub(removed);
            if bucket.is_empty() {
                self.cells.remove(&key);
            }
        }
    }

    /// Returns all items within the cells overlapping a sphere of `radius`
    /// centred at `center`. Note: without stored positions, exact distance
    /// filtering is not possible; use `SpatialHashPos3D` for exact queries.
    pub fn query_radius(&self, center: [f64; 3], radius: f64) -> Vec<(usize, &T)> {
        let r_cells = (radius / self.cell_size).ceil() as i32;
        let cx = self.cell_coord(center[0]);
        let cy = self.cell_coord(center[1]);
        let cz = self.cell_coord(center[2]);

        let mut result = Vec::new();
        for dx in -r_cells..=r_cells {
            for dy in -r_cells..=r_cells {
                for dz in -r_cells..=r_cells {
                    let key = (cx + dx, cy + dy, cz + dz);
                    if let Some(bucket) = self.cells.get(&key) {
                        for (id, data) in bucket {
                            result.push((*id, data));
                        }
                    }
                }
            }
        }
        result
    }

    /// Returns all items stored in the given cell key.
    pub fn query_cell(&self, key: (i32, i32, i32)) -> Vec<(usize, &T)> {
        match self.cells.get(&key) {
            Some(bucket) => bucket.iter().map(|(id, data)| (*id, data)).collect(),
            None => Vec::new(),
        }
    }

    /// Removes all items.
    pub fn clear(&mut self) {
        self.cells.clear();
        self.n_items = 0;
    }

    /// Returns the total number of items stored.
    pub fn len(&self) -> usize {
        self.n_items
    }

    /// Returns `true` if no items are stored.
    pub fn is_empty(&self) -> bool {
        self.n_items == 0
    }

    /// Returns the number of non-empty cells.
    pub fn n_cells(&self) -> usize {
        self.cells.len()
    }
}

// ─── SpatialHashPos3D ─────────────────────────────────────────────────────────

/// A uniform-grid spatial hash with position storage for exact radius queries.
pub struct SpatialHashPos3D<T: Clone> {
    /// Side length of each cubic cell.
    pub cell_size: f64,
    cells: HashMap<CellKey, Vec<(usize, [f64; 3], T)>>,
    /// Total number of items currently stored.
    pub n_items: usize,
}

impl<T: Clone> SpatialHashPos3D<T> {
    /// Creates a new empty hash with the given cell size.
    pub fn new(cell_size: f64) -> Self {
        Self {
            cell_size,
            cells: HashMap::new(),
            n_items: 0,
        }
    }

    /// Maps a coordinate to its cell index.
    pub fn cell_coord(&self, x: f64) -> i32 {
        (x / self.cell_size).floor() as i32
    }

    /// Returns the 3-D cell key for a world-space position.
    pub fn cell_key(&self, pos: [f64; 3]) -> (i32, i32, i32) {
        (
            self.cell_coord(pos[0]),
            self.cell_coord(pos[1]),
            self.cell_coord(pos[2]),
        )
    }

    /// Inserts item and returns its cell key.
    pub fn insert(&mut self, id: usize, pos: [f64; 3], data: T) -> (i32, i32, i32) {
        let key = self.cell_key(pos);
        self.cells.entry(key).or_default().push((id, pos, data));
        self.n_items += 1;
        key
    }

    /// Removes the entry with the given id from the cell containing pos.
    pub fn remove(&mut self, id: usize, pos: [f64; 3]) {
        let key = self.cell_key(pos);
        if let Some(bucket) = self.cells.get_mut(&key) {
            let before = bucket.len();
            bucket.retain(|(item_id, _, _)| *item_id != id);
            let removed = before - bucket.len();
            self.n_items = self.n_items.saturating_sub(removed);
            if bucket.is_empty() {
                self.cells.remove(&key);
            }
        }
    }

    /// Returns all items within a sphere, with exact distance filtering.
    pub fn query_radius(&self, center: [f64; 3], radius: f64) -> Vec<(usize, &T)> {
        let r_cells = (radius / self.cell_size).ceil() as i32;
        let cx = self.cell_coord(center[0]);
        let cy = self.cell_coord(center[1]);
        let cz = self.cell_coord(center[2]);
        let r2 = radius * radius;
        let mut result = Vec::new();

        for dx in -r_cells..=r_cells {
            for dy in -r_cells..=r_cells {
                for dz in -r_cells..=r_cells {
                    let key = (cx + dx, cy + dy, cz + dz);
                    if let Some(bucket) = self.cells.get(&key) {
                        for (id, pos, data) in bucket {
                            let d2 = dist_sq(*pos, center);
                            if d2 <= r2 {
                                result.push((*id, data));
                            }
                        }
                    }
                }
            }
        }
        result
    }

    /// Returns all items within an axis-aligned box defined by `min` and `max`.
    pub fn query_aabb(&self, min: [f64; 3], max: [f64; 3]) -> Vec<(usize, &T)> {
        let cmin = [
            self.cell_coord(min[0]),
            self.cell_coord(min[1]),
            self.cell_coord(min[2]),
        ];
        let cmax = [
            self.cell_coord(max[0]),
            self.cell_coord(max[1]),
            self.cell_coord(max[2]),
        ];
        let mut result = Vec::new();
        for cx in cmin[0]..=cmax[0] {
            for cy in cmin[1]..=cmax[1] {
                for cz in cmin[2]..=cmax[2] {
                    if let Some(bucket) = self.cells.get(&(cx, cy, cz)) {
                        for (id, pos, data) in bucket {
                            if pos[0] >= min[0]
                                && pos[0] <= max[0]
                                && pos[1] >= min[1]
                                && pos[1] <= max[1]
                                && pos[2] >= min[2]
                                && pos[2] <= max[2]
                            {
                                result.push((*id, data));
                            }
                        }
                    }
                }
            }
        }
        result
    }

    /// Find the k nearest neighbours of `center`.
    ///
    /// Uses an expanding search radius starting at cell_size and doubling
    /// until at least k candidates are found, then filters to the true k
    /// nearest.
    pub fn k_nearest(&self, center: [f64; 3], k: usize) -> Vec<(usize, f64, &T)> {
        if k == 0 || self.n_items == 0 {
            return Vec::new();
        }

        // Collect all items with their distances
        let mut all: Vec<(usize, f64, &T)> = Vec::new();
        for bucket in self.cells.values() {
            for (id, pos, data) in bucket {
                let d2 = dist_sq(*pos, center);
                all.push((*id, d2, data));
            }
        }
        all.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        all.truncate(k);
        // Convert d2 to distance
        all.iter()
            .map(|(id, d2, data)| (*id, d2.sqrt(), *data))
            .collect()
    }

    /// Returns all items stored in the given cell.
    pub fn query_cell(&self, key: (i32, i32, i32)) -> Vec<(usize, &T)> {
        match self.cells.get(&key) {
            Some(bucket) => bucket.iter().map(|(id, _, data)| (*id, data)).collect(),
            None => Vec::new(),
        }
    }

    /// Removes all items.
    pub fn clear(&mut self) {
        self.cells.clear();
        self.n_items = 0;
    }

    /// Returns the total number of items stored.
    pub fn len(&self) -> usize {
        self.n_items
    }

    /// Returns `true` if no items are stored.
    pub fn is_empty(&self) -> bool {
        self.n_items == 0
    }

    /// Returns the number of non-empty cells.
    pub fn n_cells(&self) -> usize {
        self.cells.len()
    }

    /// Compute statistics about the spatial hash distribution.
    pub fn statistics(&self) -> SpatialHashStats {
        let n_cells = self.cells.len();
        if n_cells == 0 {
            return SpatialHashStats {
                n_items: 0,
                n_occupied_cells: 0,
                min_bucket_size: 0,
                max_bucket_size: 0,
                avg_bucket_size: 0.0,
                load_factor: 0.0,
            };
        }
        let sizes: Vec<usize> = self.cells.values().map(|b| b.len()).collect();
        let min_b = *sizes.iter().min().unwrap_or(&0);
        let max_b = *sizes.iter().max().unwrap_or(&0);
        let sum: usize = sizes.iter().sum();
        SpatialHashStats {
            n_items: self.n_items,
            n_occupied_cells: n_cells,
            min_bucket_size: min_b,
            max_bucket_size: max_b,
            avg_bucket_size: sum as f64 / n_cells as f64,
            load_factor: sum as f64 / n_cells as f64,
        }
    }

    /// Update the position of an item (remove from old cell, insert into new).
    pub fn update_position(&mut self, id: usize, old_pos: [f64; 3], new_pos: [f64; 3], data: T) {
        self.remove(id, old_pos);
        self.insert(id, new_pos, data);
    }
}

/// Statistics about a spatial hash's distribution.
#[derive(Debug, Clone)]
pub struct SpatialHashStats {
    /// Total items stored.
    pub n_items: usize,
    /// Number of occupied cells.
    pub n_occupied_cells: usize,
    /// Smallest bucket.
    pub min_bucket_size: usize,
    /// Largest bucket.
    pub max_bucket_size: usize,
    /// Average items per occupied cell.
    pub avg_bucket_size: f64,
    /// Load factor (same as avg_bucket_size).
    pub load_factor: f64,
}

// ─── Ray traversal through uniform grid (3D DDA) ────────────────────────────

/// Result of a ray traversal step through the grid.
#[derive(Debug, Clone, Copy)]
pub struct RayGridStep {
    /// The cell key visited.
    pub cell: (i32, i32, i32),
    /// Parameter t at entry to this cell.
    pub t_enter: f64,
    /// Parameter t at exit from this cell.
    pub t_exit: f64,
}

/// Traverse a ray through a uniform grid using 3D DDA (Amanatides-Woo).
///
/// Returns the sequence of cells visited by the ray from `origin` in
/// `direction`, up to `max_t`.
pub fn ray_traverse_grid(
    origin: [f64; 3],
    direction: [f64; 3],
    cell_size: f64,
    max_t: f64,
    max_steps: usize,
) -> Vec<RayGridStep> {
    let mut result = Vec::new();
    if cell_size <= 0.0 {
        return result;
    }

    // Normalize direction
    let dir_len =
        (direction[0] * direction[0] + direction[1] * direction[1] + direction[2] * direction[2])
            .sqrt();
    if dir_len < 1e-15 {
        return result;
    }
    let dir = [
        direction[0] / dir_len,
        direction[1] / dir_len,
        direction[2] / dir_len,
    ];

    // Current cell
    let mut cell = [
        (origin[0] / cell_size).floor() as i32,
        (origin[1] / cell_size).floor() as i32,
        (origin[2] / cell_size).floor() as i32,
    ];

    // Step direction (+1 or -1) and t_delta (how much t to cross one cell)
    let mut step = [0i32; 3];
    let mut t_max = [0.0f64; 3]; // t at which ray crosses next cell boundary
    let mut t_delta = [f64::INFINITY; 3];

    for i in 0..3 {
        if dir[i] > 0.0 {
            step[i] = 1;
            let next_boundary = (cell[i] as f64 + 1.0) * cell_size;
            t_max[i] = (next_boundary - origin[i]) / dir[i];
            t_delta[i] = cell_size / dir[i];
        } else if dir[i] < 0.0 {
            step[i] = -1;
            let next_boundary = cell[i] as f64 * cell_size;
            t_max[i] = (next_boundary - origin[i]) / dir[i];
            t_delta[i] = cell_size / (-dir[i]);
        } else {
            step[i] = 0;
            t_max[i] = f64::INFINITY;
            t_delta[i] = f64::INFINITY;
        }
    }

    let mut t = 0.0;
    for _ in 0..max_steps {
        // Find the axis with smallest t_max
        let min_t = t_max[0].min(t_max[1]).min(t_max[2]);
        let t_exit = min_t.min(max_t);

        result.push(RayGridStep {
            cell: (cell[0], cell[1], cell[2]),
            t_enter: t,
            t_exit,
        });

        if min_t >= max_t {
            break;
        }

        // Advance along the axis with smallest t_max
        if t_max[0] <= t_max[1] && t_max[0] <= t_max[2] {
            cell[0] += step[0];
            t = t_max[0];
            t_max[0] += t_delta[0];
        } else if t_max[1] <= t_max[2] {
            cell[1] += step[1];
            t = t_max[1];
            t_max[1] += t_delta[1];
        } else {
            cell[2] += step[2];
            t = t_max[2];
            t_max[2] += t_delta[2];
        }
    }

    result
}

// ─── LooseOctree ──────────────────────────────────────────────────────────────

/// A loose octree node that stores items at every level.
pub struct LooseOctree<T: Clone> {
    /// World-space centre of this node.
    pub center: [f64; 3],
    /// Half-size of the *tight* cell (the loose cell is `2 * half_size`).
    pub half_size: f64,
    /// Remaining subdivision depth (0 = leaf).
    pub depth: u32,
    /// Items stored at this node (not yet pushed to children).
    pub items: Vec<(usize, [f64; 3], T)>,
    /// Eight children, present after the first subdivision.
    pub children: Option<Box<[LooseOctree<T>; 8]>>,
}

impl<T: Clone> LooseOctree<T> {
    /// Creates a new root node.
    pub fn new(center: [f64; 3], half_size: f64, max_depth: u32) -> Self {
        Self {
            center,
            half_size,
            depth: max_depth,
            items: Vec::new(),
            children: None,
        }
    }

    /// Returns the octant index (0..7) for `pos` relative to `self.center`.
    pub fn child_index(&self, pos: [f64; 3]) -> usize {
        let mut idx = 0usize;
        if pos[0] >= self.center[0] {
            idx |= 1;
        }
        if pos[1] >= self.center[1] {
            idx |= 2;
        }
        if pos[2] >= self.center[2] {
            idx |= 4;
        }
        idx
    }

    /// Builds child centres for octant `i`.
    fn child_center(&self, i: usize) -> [f64; 3] {
        let q = self.half_size * 0.5;
        [
            self.center[0] + if i & 1 != 0 { q } else { -q },
            self.center[1] + if i & 2 != 0 { q } else { -q },
            self.center[2] + if i & 4 != 0 { q } else { -q },
        ]
    }

    /// Subdivides this node, creating eight children.
    fn subdivide(&mut self) {
        if self.children.is_some() {
            return;
        }
        let child_depth = self.depth.saturating_sub(1);
        let child_half = self.half_size * 0.5;
        let children = std::array::from_fn(|i| {
            LooseOctree::new(self.child_center(i), child_half, child_depth)
        });
        self.children = Some(Box::new(children));
    }

    /// Inserts `(id, pos, data)` into the tree.
    pub fn insert(&mut self, id: usize, pos: [f64; 3], data: T, max_items: usize) {
        self.items.push((id, pos, data.clone()));

        if self.items.len() > max_items && self.depth > 0 {
            self.subdivide();
            let items: Vec<_> = self.items.drain(..).collect();
            for (iid, ipos, idata) in items {
                let ci = self.child_index(ipos);
                if let Some(children) = self.children.as_mut() {
                    children[ci].insert(iid, ipos, idata, max_items);
                }
            }
        }
    }

    /// Returns all items whose stored position is within `radius` of `center`.
    pub fn query_sphere(&self, center: [f64; 3], radius: f64) -> Vec<(usize, &T)> {
        let loose = self.half_size * 2.0;
        let mut closest2 = 0.0f64;
        for (c_i, sc_i) in center.iter().zip(self.center.iter()) {
            let lo = sc_i - loose;
            let hi = sc_i + loose;
            if c_i < &lo {
                closest2 += (c_i - lo).powi(2);
            } else if c_i > &hi {
                closest2 += (c_i - hi).powi(2);
            }
        }
        if closest2 > radius * radius {
            return Vec::new();
        }

        let r2 = radius * radius;
        let mut result = Vec::new();

        for (id, pos, data) in &self.items {
            let d2 = dist_sq(*pos, center);
            if d2 <= r2 {
                result.push((*id, data));
            }
        }

        if let Some(children) = &self.children {
            for child in children.iter() {
                result.extend(child.query_sphere(center, radius));
            }
        }

        result
    }

    /// Returns all items within an axis-aligned box.
    pub fn query_aabb(&self, min: [f64; 3], max: [f64; 3]) -> Vec<(usize, &T)> {
        // Quick rejection: does loose node box overlap query box?
        let loose = self.half_size * 2.0;
        for i in 0..3 {
            if self.center[i] - loose > max[i] || self.center[i] + loose < min[i] {
                return Vec::new();
            }
        }

        let mut result = Vec::new();
        for (id, pos, data) in &self.items {
            if pos[0] >= min[0]
                && pos[0] <= max[0]
                && pos[1] >= min[1]
                && pos[1] <= max[1]
                && pos[2] >= min[2]
                && pos[2] <= max[2]
            {
                result.push((*id, data));
            }
        }

        if let Some(children) = &self.children {
            for child in children.iter() {
                result.extend(child.query_aabb(min, max));
            }
        }

        result
    }

    /// Counts all items stored in this node and all descendants.
    pub fn item_count(&self) -> usize {
        let mut count = self.items.len();
        if let Some(children) = &self.children {
            for child in children.iter() {
                count += child.item_count();
            }
        }
        count
    }

    /// Returns the maximum depth reached in the tree.
    pub fn max_depth_reached(&self) -> u32 {
        if let Some(children) = &self.children {
            let child_max = children
                .iter()
                .map(|c| c.max_depth_reached())
                .max()
                .unwrap_or(0);
            child_max + 1
        } else {
            0
        }
    }

    /// Count the total number of nodes in the tree.
    pub fn node_count(&self) -> usize {
        let mut count = 1;
        if let Some(children) = &self.children {
            for child in children.iter() {
                count += child.node_count();
            }
        }
        count
    }

    /// K-nearest neighbours search via the octree.
    pub fn k_nearest(&self, center: [f64; 3], k: usize) -> Vec<(usize, f64, &T)> {
        // Use a max-heap to keep track of the k closest
        let mut heap: BinaryHeap<KnnEntry<&T>> = BinaryHeap::new();
        self.knn_recurse(center, k, &mut heap);

        let mut result: Vec<(usize, f64, &T)> =
            heap.into_iter().map(|e| (e.id, e.dist, e.data)).collect();
        result.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        result
    }

    fn knn_recurse<'a>(
        &'a self,
        center: [f64; 3],
        k: usize,
        heap: &mut BinaryHeap<KnnEntry<&'a T>>,
    ) {
        // Check items at this node
        for (id, pos, data) in &self.items {
            let d = dist_sq(*pos, center).sqrt();
            if heap.len() < k {
                heap.push(KnnEntry {
                    dist: d,
                    id: *id,
                    data,
                });
            } else if let Some(top) = heap.peek()
                && d < top.dist
            {
                heap.pop();
                heap.push(KnnEntry {
                    dist: d,
                    id: *id,
                    data,
                });
            }
        }

        // Recurse into children that could contain closer points
        if let Some(children) = &self.children {
            for child in children.iter() {
                // Minimum distance from center to child's loose box
                let loose = child.half_size * 2.0;
                let mut min_d2 = 0.0f64;
                for (c_i, cc_i) in center.iter().zip(child.center.iter()) {
                    let lo = cc_i - loose;
                    let hi = cc_i + loose;
                    if c_i < &lo {
                        min_d2 += (c_i - lo).powi(2);
                    } else if c_i > &hi {
                        min_d2 += (c_i - hi).powi(2);
                    }
                }
                let min_d = min_d2.sqrt();
                let should_search =
                    heap.len() < k || heap.peek().is_some_and(|top| min_d < top.dist);
                if should_search {
                    child.knn_recurse(center, k, heap);
                }
            }
        }
    }
}

/// Internal entry for KNN max-heap (ordered by distance descending).
struct KnnEntry<D> {
    dist: f64,
    id: usize,
    data: D,
}

impl<D> PartialEq for KnnEntry<D> {
    fn eq(&self, other: &Self) -> bool {
        self.dist == other.dist
    }
}

impl<D> Eq for KnnEntry<D> {}

impl<D> PartialOrd for KnnEntry<D> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<D> Ord for KnnEntry<D> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.dist
            .partial_cmp(&other.dist)
            .unwrap_or(std::cmp::Ordering::Equal)
    }
}

// ─── Parallel Spatial Hash ────────────────────────────────────────────────────

/// A parallel-build spatial hash.
///
/// All entries are inserted in one batch call, making the build trivially
/// parallelisable (simulated here sequentially).  Queries are identical to
/// `SpatialHashPos3D`.
pub struct ParallelSpatialHash<T: Clone> {
    inner: SpatialHashPos3D<T>,
}

impl<T: Clone> ParallelSpatialHash<T> {
    /// Build from a slice of `(id, position, data)` tuples.
    pub fn build(entries: &[(usize, [f64; 3], T)], cell_size: f64) -> Self {
        let mut inner = SpatialHashPos3D::new(cell_size);
        for (id, pos, data) in entries {
            inner.insert(*id, *pos, data.clone());
        }
        Self { inner }
    }

    /// Query all items within `radius` of `center`.
    pub fn query_radius(&self, center: [f64; 3], radius: f64) -> Vec<(usize, &T)> {
        self.inner.query_radius(center, radius)
    }

    /// Query all items within an AABB.
    pub fn query_aabb(&self, min: [f64; 3], max: [f64; 3]) -> Vec<(usize, &T)> {
        self.inner.query_aabb(min, max)
    }

    /// Number of items.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Whether the hash is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Number of occupied cells.
    pub fn n_cells(&self) -> usize {
        self.inner.n_cells()
    }
}

// ─── Spatial Hash Merging ─────────────────────────────────────────────────────

/// Merge two `SpatialHashPos3D` instances (same cell size) into a new one.
///
/// IDs are *not* re-numbered; callers must ensure they are disjoint.
pub fn merge_spatial_hashes<T: Clone>(
    a: &SpatialHashPos3D<T>,
    b: &SpatialHashPos3D<T>,
) -> SpatialHashPos3D<T> {
    assert!(
        (a.cell_size - b.cell_size).abs() < 1e-12,
        "merge requires identical cell sizes"
    );
    let mut merged = SpatialHashPos3D::new(a.cell_size);

    // Copy all entries from a
    for (key, bucket) in &a.cells {
        for (id, pos, data) in bucket {
            // Insert directly into merged map
            merged
                .cells
                .entry(*key)
                .or_default()
                .push((*id, *pos, data.clone()));
            merged.n_items += 1;
        }
    }

    // Copy all entries from b
    for (key, bucket) in &b.cells {
        for (id, pos, data) in bucket {
            merged
                .cells
                .entry(*key)
                .or_default()
                .push((*id, *pos, data.clone()));
            merged.n_items += 1;
        }
    }

    merged
}

// ─── Persistent Spatial Hash ──────────────────────────────────────────────────

/// A spatial hash that persists across frames.
///
/// Tracks the last known position of each object so that `insert_or_update`
/// correctly removes the object from its old cell before re-inserting.
pub struct PersistentSpatialHash<T: Clone> {
    /// Underlying hash with positions.
    hash: SpatialHashPos3D<T>,
    /// Tracks last position of each object: `id → last_position`.
    last_positions: HashMap<usize, [f64; 3]>,
    /// Current frame number.
    frame: u64,
}

impl<T: Clone> PersistentSpatialHash<T> {
    /// Create a new persistent hash with the given cell size.
    pub fn new(cell_size: f64) -> Self {
        Self {
            hash: SpatialHashPos3D::new(cell_size),
            last_positions: HashMap::new(),
            frame: 0,
        }
    }

    /// Insert a new object or update an existing one.
    ///
    /// If the object already exists, it is first removed from its old cell.
    pub fn insert_or_update(&mut self, id: usize, pos: [f64; 3], data: T) {
        if let Some(&old_pos) = self.last_positions.get(&id) {
            self.hash.remove(id, old_pos);
        }
        self.hash.insert(id, pos, data);
        self.last_positions.insert(id, pos);
    }

    /// Remove an object.
    pub fn remove(&mut self, id: usize) {
        if let Some(&pos) = self.last_positions.get(&id) {
            self.hash.remove(id, pos);
            self.last_positions.remove(&id);
        }
    }

    /// Query all items within `radius` of `center`.
    pub fn query_radius(&self, center: [f64; 3], radius: f64) -> Vec<(usize, &T)> {
        self.hash.query_radius(center, radius)
    }

    /// Query all items in an AABB.
    pub fn query_aabb(&self, min: [f64; 3], max: [f64; 3]) -> Vec<(usize, &T)> {
        self.hash.query_aabb(min, max)
    }

    /// Number of objects.
    pub fn len(&self) -> usize {
        self.hash.len()
    }

    /// Whether the hash is empty.
    pub fn is_empty(&self) -> bool {
        self.hash.is_empty()
    }

    /// Advance the frame counter (call once per simulation step).
    pub fn advance_frame(&mut self) {
        self.frame += 1;
    }

    /// Current frame number.
    pub fn current_frame(&self) -> u64 {
        self.frame
    }

    /// Compute statistics.
    pub fn statistics(&self) -> SpatialHashStats {
        self.hash.statistics()
    }
}

// ─── GPU-Friendly Spatial Hash Layout ────────────────────────────────────────

/// A spatial hash with an interleaved, flat-array layout for GPU upload.
///
/// Data is stored as sorted arrays of cell keys and per-item records,
/// matching the layout expected by GPU compute shaders.
pub struct GpuSpatialHashLayout<T: Clone> {
    cell_size: f32,
    /// Sorted cell keys.
    cell_keys: Vec<(i32, i32, i32)>,
    /// Cell start/end indices into `items`.
    cell_offsets: Vec<(usize, usize)>,
    /// Items sorted by cell key: (id, position, data).
    items: Vec<(usize, [f32; 3], T)>,
}

impl<T: Clone> GpuSpatialHashLayout<T> {
    /// Build the GPU-friendly layout from a slice of `(id, position, data)`.
    pub fn build(entries: &[(usize, [f32; 3], T)], cell_size: f32) -> Self {
        // Bucket entries by cell key
        let mut buckets: HashMap<CellKey, Vec<(usize, [f32; 3], T)>> = HashMap::new();
        for (id, pos, data) in entries {
            let key = (
                (pos[0] / cell_size).floor() as i32,
                (pos[1] / cell_size).floor() as i32,
                (pos[2] / cell_size).floor() as i32,
            );
            buckets
                .entry(key)
                .or_default()
                .push((*id, *pos, data.clone()));
        }

        // Sort cell keys for cache-friendly GPU traversal
        let mut cell_keys: Vec<(i32, i32, i32)> = buckets.keys().copied().collect();
        cell_keys.sort_unstable();

        let mut items = Vec::new();
        let mut cell_offsets = Vec::new();

        for &key in &cell_keys {
            let start = items.len();
            for entry in &buckets[&key] {
                items.push(entry.clone());
            }
            cell_offsets.push((start, items.len()));
        }

        Self {
            cell_size,
            cell_keys,
            cell_offsets,
            items,
        }
    }

    /// Total number of items.
    pub fn n_items(&self) -> usize {
        self.items.len()
    }

    /// Number of non-empty cells.
    pub fn n_cells(&self) -> usize {
        self.cell_keys.len()
    }

    /// Query items in a specific cell.
    pub fn query_cell_flat(&self, cx: i32, cy: i32, cz: i32) -> Vec<(usize, &T)> {
        let key = (cx, cy, cz);
        if let Ok(pos) = self.cell_keys.binary_search(&key) {
            let (start, end) = self.cell_offsets[pos];
            self.items[start..end]
                .iter()
                .map(|(id, _, data)| (*id, data))
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Export all items as a flat `f32` buffer for GPU upload.
    ///
    /// Format per item: `[x, y, z, cell_x_f32, cell_y_f32, cell_z_f32]`
    pub fn to_flat_f32_buffer(&self) -> Vec<f32> {
        let mut buf = Vec::with_capacity(self.items.len() * 6);
        for (item_idx, (_, pos, _)) in self.items.iter().enumerate() {
            // Find which cell this item belongs to
            let key = self
                .cell_keys
                .iter()
                .enumerate()
                .find(|(ci, _)| {
                    let (start, end) = self.cell_offsets[*ci];
                    item_idx >= start && item_idx < end
                })
                .map(|(_, &k)| k)
                .unwrap_or((0, 0, 0));
            buf.push(pos[0]);
            buf.push(pos[1]);
            buf.push(pos[2]);
            buf.push(key.0 as f32);
            buf.push(key.1 as f32);
            buf.push(key.2 as f32);
        }
        buf
    }

    /// Cell size used for this layout.
    pub fn cell_size(&self) -> f32 {
        self.cell_size
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── SpatialHash3D ──────────────────────────────────────────────────────

    #[test]
    fn spatial_hash_basic_insert_query() {
        let mut sh: SpatialHash3D<&str> = SpatialHash3D::new(1.0);
        sh.insert(0, [0.5, 0.5, 0.5], "a");
        assert_eq!(sh.len(), 1);
        assert_eq!(sh.n_cells(), 1);
        let items = sh.query_cell((0, 0, 0));
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].0, 0);
    }

    #[test]
    fn spatial_hash_remove() {
        let mut sh: SpatialHash3D<i32> = SpatialHash3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], 42);
        sh.insert(1, [0.5, 0.0, 0.0], 7);
        sh.remove(0, [0.0, 0.0, 0.0]);
        assert_eq!(sh.len(), 1);
    }

    #[test]
    fn spatial_hash_cell_key() {
        let sh: SpatialHash3D<()> = SpatialHash3D::new(2.0);
        assert_eq!(sh.cell_key([0.5, 0.5, 0.5]), (0, 0, 0));
        assert_eq!(sh.cell_key([2.5, 4.5, -0.5]), (1, 2, -1));
    }

    // ── SpatialHashPos3D ───────────────────────────────────────────────────

    #[test]
    fn spatial_hash_insert_query_radius() {
        let mut sh: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], "origin");
        sh.insert(1, [0.5, 0.0, 0.0], "near");
        sh.insert(2, [10.0, 0.0, 0.0], "far");

        let results = sh.query_radius([0.0, 0.0, 0.0], 1.0);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0), "origin should be found");
        assert!(ids.contains(&1), "near should be found");
        assert!(!ids.contains(&2), "far should NOT be found");
    }

    #[test]
    fn spatial_hash_clear_len_zero() {
        let mut sh: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], 42);
        sh.insert(1, [1.0, 1.0, 1.0], 7);
        assert_eq!(sh.len(), 2);
        sh.clear();
        assert_eq!(sh.len(), 0);
        assert!(sh.is_empty());
    }

    #[test]
    fn spatial_hash_query_radius_excludes_outside() {
        let mut sh: SpatialHashPos3D<u32> = SpatialHashPos3D::new(2.0);
        let radius = 1.5_f64;
        sh.insert(0, [0.0, 0.0, 0.0], 0);
        sh.insert(1, [radius + 0.1, 0.0, 0.0], 1);
        sh.insert(2, [radius - 0.1, 0.0, 0.0], 2);

        let results = sh.query_radius([0.0, 0.0, 0.0], radius);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0));
        assert!(ids.contains(&2));
        assert!(
            !ids.contains(&1),
            "item just outside radius must be excluded"
        );
    }

    #[test]
    fn spatial_hash_query_aabb() {
        let mut sh: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.5, 0.5, 0.5], "inside");
        sh.insert(1, [1.5, 0.5, 0.5], "inside2");
        sh.insert(2, [5.0, 5.0, 5.0], "outside");

        let results = sh.query_aabb([0.0, 0.0, 0.0], [2.0, 1.0, 1.0]);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0));
        assert!(ids.contains(&1));
        assert!(!ids.contains(&2));
    }

    #[test]
    fn spatial_hash_k_nearest() {
        let mut sh: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], "a");
        sh.insert(1, [1.0, 0.0, 0.0], "b");
        sh.insert(2, [2.0, 0.0, 0.0], "c");
        sh.insert(3, [10.0, 0.0, 0.0], "d");

        let nearest = sh.k_nearest([0.0, 0.0, 0.0], 2);
        assert_eq!(nearest.len(), 2);
        assert_eq!(nearest[0].0, 0, "closest should be item 0");
        assert_eq!(nearest[1].0, 1, "second closest should be item 1");
    }

    #[test]
    fn spatial_hash_k_nearest_more_than_items() {
        let mut sh: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], 1);

        let nearest = sh.k_nearest([0.0, 0.0, 0.0], 5);
        assert_eq!(nearest.len(), 1, "should return all items when k > n_items");
    }

    #[test]
    fn spatial_hash_statistics() {
        let mut sh: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], 1);
        sh.insert(1, [0.5, 0.0, 0.0], 2);
        sh.insert(2, [5.0, 5.0, 5.0], 3);

        let stats = sh.statistics();
        assert_eq!(stats.n_items, 3);
        assert_eq!(stats.n_occupied_cells, 2);
        assert_eq!(stats.max_bucket_size, 2);
        assert_eq!(stats.min_bucket_size, 1);
    }

    #[test]
    fn spatial_hash_statistics_empty() {
        let sh: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        let stats = sh.statistics();
        assert_eq!(stats.n_items, 0);
        assert_eq!(stats.n_occupied_cells, 0);
    }

    #[test]
    fn spatial_hash_update_position() {
        let mut sh: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        sh.insert(0, [0.0, 0.0, 0.0], "a");
        sh.update_position(0, [0.0, 0.0, 0.0], [5.0, 5.0, 5.0], "a");
        assert_eq!(sh.len(), 1);

        // Should no longer be at origin
        let at_origin = sh.query_radius([0.0, 0.0, 0.0], 0.5);
        assert!(at_origin.is_empty(), "item should no longer be at origin");

        // Should be at new position
        let at_new = sh.query_radius([5.0, 5.0, 5.0], 0.5);
        assert_eq!(at_new.len(), 1);
    }

    // ── Ray traversal ──────────────────────────────────────────────────────

    #[test]
    fn ray_traverse_along_x_axis() {
        let steps = ray_traverse_grid([0.5, 0.5, 0.5], [1.0, 0.0, 0.0], 1.0, 3.0, 100);
        assert!(!steps.is_empty());
        // Should visit cells (0,0,0), (1,0,0), (2,0,0), (3,0,0)
        let cells: Vec<(i32, i32, i32)> = steps.iter().map(|s| s.cell).collect();
        assert!(cells.contains(&(0, 0, 0)));
        assert!(cells.contains(&(1, 0, 0)));
        assert!(cells.contains(&(2, 0, 0)));
    }

    #[test]
    fn ray_traverse_diagonal() {
        let steps = ray_traverse_grid([0.5, 0.5, 0.5], [1.0, 1.0, 0.0], 1.0, 3.0, 100);
        assert!(!steps.is_empty());
        // First cell should be (0,0,0)
        assert_eq!(steps[0].cell, (0, 0, 0));
    }

    #[test]
    fn ray_traverse_zero_direction() {
        let steps = ray_traverse_grid([0.0, 0.0, 0.0], [0.0, 0.0, 0.0], 1.0, 10.0, 100);
        assert!(steps.is_empty(), "zero direction should produce no steps");
    }

    #[test]
    fn ray_traverse_negative_direction() {
        let steps = ray_traverse_grid([2.5, 0.5, 0.5], [-1.0, 0.0, 0.0], 1.0, 3.0, 100);
        assert!(!steps.is_empty());
        let cells: Vec<(i32, i32, i32)> = steps.iter().map(|s| s.cell).collect();
        assert!(cells.contains(&(2, 0, 0)));
        assert!(cells.contains(&(1, 0, 0)));
        assert!(cells.contains(&(0, 0, 0)));
    }

    #[test]
    fn ray_traverse_max_steps_limit() {
        let steps = ray_traverse_grid([0.5, 0.5, 0.5], [1.0, 0.0, 0.0], 1.0, 1000.0, 5);
        assert!(steps.len() <= 5, "should respect max_steps limit");
    }

    // ── LooseOctree ────────────────────────────────────────────────────────

    #[test]
    fn loose_octree_insert_query_sphere() {
        let mut tree: LooseOctree<&str> = LooseOctree::new([0.0; 3], 16.0, 4);
        tree.insert(0, [1.0, 0.0, 0.0], "a", 2);
        tree.insert(1, [0.0, 1.0, 0.0], "b", 2);
        tree.insert(2, [100.0, 0.0, 0.0], "far", 2);

        let results = tree.query_sphere([0.0, 0.0, 0.0], 5.0);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0), "item 0 should be found");
        assert!(ids.contains(&1), "item 1 should be found");
        assert!(!ids.contains(&2), "far item must not appear");
    }

    #[test]
    fn loose_octree_item_count() {
        let mut tree: LooseOctree<i32> = LooseOctree::new([0.0; 3], 32.0, 3);
        for i in 0..5_usize {
            tree.insert(i, [i as f64, 0.0, 0.0], i as i32, 2);
        }
        assert!(
            tree.item_count() >= 5,
            "item_count should be at least 5, got {}",
            tree.item_count()
        );
    }

    #[test]
    fn loose_octree_query_aabb() {
        let mut tree: LooseOctree<&str> = LooseOctree::new([0.0; 3], 16.0, 4);
        tree.insert(0, [1.0, 1.0, 1.0], "inside", 2);
        tree.insert(1, [-1.0, -1.0, -1.0], "inside2", 2);
        tree.insert(2, [50.0, 50.0, 50.0], "outside", 2);

        let results = tree.query_aabb([-2.0, -2.0, -2.0], [2.0, 2.0, 2.0]);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0));
        assert!(ids.contains(&1));
        assert!(!ids.contains(&2));
    }

    #[test]
    fn loose_octree_k_nearest() {
        let mut tree: LooseOctree<&str> = LooseOctree::new([0.0; 3], 32.0, 4);
        tree.insert(0, [0.0, 0.0, 0.0], "origin", 2);
        tree.insert(1, [1.0, 0.0, 0.0], "near", 2);
        tree.insert(2, [5.0, 0.0, 0.0], "mid", 2);
        tree.insert(3, [100.0, 0.0, 0.0], "far", 2);

        let nearest = tree.k_nearest([0.0, 0.0, 0.0], 2);
        assert_eq!(nearest.len(), 2);
        assert_eq!(nearest[0].0, 0, "closest should be item 0");
        assert_eq!(nearest[1].0, 1, "second closest should be item 1");
    }

    #[test]
    fn loose_octree_max_depth() {
        let mut tree: LooseOctree<i32> = LooseOctree::new([0.0; 3], 32.0, 4);
        // Insert enough items to trigger subdivision
        for i in 0..10_usize {
            tree.insert(i, [i as f64 * 0.1, 0.0, 0.0], i as i32, 2);
        }
        let depth = tree.max_depth_reached();
        assert!(depth > 0, "should have subdivided at least once");
    }

    #[test]
    fn loose_octree_node_count() {
        let tree: LooseOctree<i32> = LooseOctree::new([0.0; 3], 32.0, 4);
        assert_eq!(tree.node_count(), 1, "empty tree has one root node");
    }

    #[test]
    fn loose_octree_node_count_after_subdivision() {
        let mut tree: LooseOctree<i32> = LooseOctree::new([0.0; 3], 32.0, 4);
        for i in 0..10_usize {
            tree.insert(i, [i as f64, 0.0, 0.0], i as i32, 2);
        }
        assert!(tree.node_count() > 1, "should have created child nodes");
    }

    // ── Parallel spatial hash tests ────────────────────────────────────

    #[test]
    fn parallel_spatial_hash_build_query() {
        let entries: Vec<(usize, [f64; 3], i32)> = (0..8)
            .map(|i| (i, [i as f64, 0.0, 0.0], i as i32))
            .collect();
        let sh = ParallelSpatialHash::build(&entries, 2.0);
        let results = sh.query_radius([0.0, 0.0, 0.0], 2.5);
        let ids: Vec<usize> = results.iter().map(|(id, _)| *id).collect();
        assert!(ids.contains(&0));
        assert!(ids.contains(&1));
        assert!(ids.contains(&2));
    }

    #[test]
    fn parallel_spatial_hash_len() {
        let entries: Vec<(usize, [f64; 3], &str)> =
            vec![(0, [0.0, 0.0, 0.0], "a"), (1, [1.0, 0.0, 0.0], "b")];
        let sh = ParallelSpatialHash::build(&entries, 1.0);
        assert_eq!(sh.len(), 2);
        assert!(!sh.is_empty());
    }

    #[test]
    fn parallel_spatial_hash_empty() {
        let entries: Vec<(usize, [f64; 3], i32)> = Vec::new();
        let sh = ParallelSpatialHash::build(&entries, 1.0);
        assert!(sh.is_empty());
        assert_eq!(sh.len(), 0);
    }

    // ── Spatial hash merging tests ─────────────────────────────────────

    #[test]
    fn spatial_hash_merge_basic() {
        let mut a: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        a.insert(0, [0.0, 0.0, 0.0], 10);
        a.insert(1, [1.0, 0.0, 0.0], 20);

        let mut b: SpatialHashPos3D<i32> = SpatialHashPos3D::new(1.0);
        b.insert(2, [5.0, 0.0, 0.0], 30);
        b.insert(3, [6.0, 0.0, 0.0], 40);

        let merged = merge_spatial_hashes(&a, &b);
        assert_eq!(merged.len(), 4);
    }

    #[test]
    fn spatial_hash_merge_query_all() {
        let mut a: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        a.insert(0, [0.0, 0.0, 0.0], "a");

        let mut b: SpatialHashPos3D<&str> = SpatialHashPos3D::new(1.0);
        b.insert(1, [10.0, 0.0, 0.0], "b");

        let merged = merge_spatial_hashes(&a, &b);
        let r0 = merged.query_radius([0.0, 0.0, 0.0], 0.5);
        let r1 = merged.query_radius([10.0, 0.0, 0.0], 0.5);
        assert_eq!(r0.len(), 1);
        assert_eq!(r1.len(), 1);
    }

    // ── Persistent spatial hash tests ─────────────────────────────────

    #[test]
    fn persistent_hash_update_positions() {
        let mut ph: PersistentSpatialHash<i32> = PersistentSpatialHash::new(1.0);
        ph.insert_or_update(0, [0.0, 0.0, 0.0], 100);
        ph.insert_or_update(1, [5.0, 0.0, 0.0], 200);

        // Query frame 0
        let r = ph.query_radius([0.0, 0.0, 0.0], 0.5);
        assert_eq!(r.len(), 1);
        assert_eq!(r[0].0, 0);

        // Move item 0 to new position
        ph.insert_or_update(0, [5.5, 0.0, 0.0], 100);

        let r2 = ph.query_radius([0.0, 0.0, 0.0], 0.5);
        assert!(r2.is_empty(), "item 0 should have moved");
    }

    #[test]
    fn persistent_hash_remove() {
        let mut ph: PersistentSpatialHash<i32> = PersistentSpatialHash::new(1.0);
        ph.insert_or_update(0, [0.0, 0.0, 0.0], 1);
        ph.insert_or_update(1, [0.5, 0.0, 0.0], 2);
        ph.remove(0);
        assert_eq!(ph.len(), 1);
    }

    #[test]
    fn persistent_hash_frame_advance() {
        let mut ph: PersistentSpatialHash<i32> = PersistentSpatialHash::new(1.0);
        ph.insert_or_update(0, [0.0, 0.0, 0.0], 42);
        ph.advance_frame();
        assert_eq!(ph.current_frame(), 1);
        // Data persists across frames
        let r = ph.query_radius([0.0, 0.0, 0.0], 0.5);
        assert_eq!(r.len(), 1);
    }

    // ── GPU-friendly layout tests ──────────────────────────────────────

    #[test]
    fn gpu_layout_build_basic() {
        let entries: Vec<(usize, [f32; 3], u32)> = vec![
            (0, [0.0, 0.0, 0.0], 10),
            (1, [1.0, 0.0, 0.0], 20),
            (2, [2.0, 0.0, 0.0], 30),
        ];
        let layout = GpuSpatialHashLayout::build(&entries, 1.0);
        assert_eq!(layout.n_items(), 3);
    }

    #[test]
    fn gpu_layout_query_cell() {
        let entries: Vec<(usize, [f32; 3], u32)> = vec![
            (0, [0.5, 0.0, 0.0], 1),
            (1, [0.7, 0.0, 0.0], 2),
            (2, [5.0, 0.0, 0.0], 3),
        ];
        let layout = GpuSpatialHashLayout::build(&entries, 1.0);
        let cell_items = layout.query_cell_flat(0, 0, 0);
        assert_eq!(cell_items.len(), 2, "two items in cell (0,0,0)");
    }

    #[test]
    fn gpu_layout_flat_buffer_format() {
        let entries: Vec<(usize, [f32; 3], u32)> = vec![(0, [1.0, 2.0, 3.0], 99)];
        let layout = GpuSpatialHashLayout::build(&entries, 1.0);
        let buf = layout.to_flat_f32_buffer();
        // Format: [x, y, z, cell_x as f32, cell_y as f32, cell_z as f32]
        assert!(!buf.is_empty());
        assert_eq!(buf[0], 1.0_f32);
        assert_eq!(buf[1], 2.0_f32);
        assert_eq!(buf[2], 3.0_f32);
    }
}