oxiphysics-collision 0.1.2

Collision detection algorithms 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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

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

use super::functions::{aabb3_overlaps, aabb3_point_dist_sq, axis_is_sorted};

/// An endpoint on one axis used by the Sweep-and-Prune broadphase.
#[derive(Debug, Clone)]
pub struct SapEndpoint {
    /// The coordinate value of this endpoint.
    pub value: f64,
    /// Which object this endpoint belongs to.
    pub object_id: u64,
    /// `true` for the minimum (left) endpoint, `false` for the maximum (right).
    pub is_min: bool,
}
/// Extended SAP that tracks statistics.
pub struct StatTrackingSap {
    /// Inner SAP structure.
    pub inner: IncrementalSap,
    /// Statistics from the last query.
    pub stats: SapStats,
}
impl StatTrackingSap {
    /// Create a new statistics-tracking SAP.
    pub fn new() -> Self {
        Self {
            inner: IncrementalSap::new(),
            stats: SapStats::default(),
        }
    }
    /// Insert a body.
    pub fn insert(&mut self, id: u32, aabb: Aabb3) {
        self.inner.insert(id, aabb);
    }
    /// Remove a body.
    pub fn remove(&mut self, id: u32) {
        self.inner.remove(id);
    }
    /// Update a body's AABB.
    pub fn update(&mut self, id: u32, aabb: Aabb3) {
        self.inner.update(id, aabb);
    }
    /// Query all overlapping pairs, recording statistics.
    pub fn query_pairs(&mut self) -> Vec<(u32, u32)> {
        let n_endpoints = self.inner.endpoints_x.len();
        let pairs = self.inner.query_pairs();
        self.stats = SapStats {
            pair_count: pairs.len(),
            sweep_count: n_endpoints,
            body_count: self.inner.body_count(),
        };
        pairs
    }
}
/// An axis-aligned bounding box stored inside the SAP structure.
#[derive(Debug, Clone)]
pub struct SapObject {
    /// Unique identifier for this object.
    pub id: u64,
    /// Minimum corner `[x, y, z]`.
    pub min: [f64; 3],
    /// Maximum corner `[x, y, z]`.
    pub max: [f64; 3],
}
/// Result of a single broadphase step.
#[derive(Debug, Clone)]
pub struct BroadphaseResult {
    /// All currently overlapping pairs.
    pub pairs: Vec<(u32, u32)>,
    /// New pairs this frame (begin events).
    pub new_pairs: Vec<(u32, u32)>,
    /// Pairs that ended this frame (end events).
    pub lost_pairs: Vec<(u32, u32)>,
    /// Statistics for this step.
    pub stats: SapStats,
}
/// An endpoint on one axis used by the incremental multi-axis SAP.
#[derive(Debug, Clone)]
pub struct SapEndpointU32 {
    /// The coordinate value of this endpoint.
    pub value: f64,
    /// Which body this endpoint belongs to.
    pub body_id: u32,
    /// `true` for the minimum (left) endpoint, `false` for the maximum (right).
    pub is_min: bool,
}
/// Uniform-grid broadphase that hashes objects into fixed-size cells.
pub struct GridBroadphase {
    /// Side length of each cubic cell.
    pub cell_size: f64,
    /// Map from cell coordinate to the list of object ids in that cell.
    pub cells: HashMap<(i32, i32, i32), Vec<u64>>,
}
impl GridBroadphase {
    /// Create a new grid with the given cell size.
    pub fn new(cell_size: f64) -> Self {
        Self {
            cell_size,
            cells: HashMap::new(),
        }
    }
    /// Insert an object into every grid cell that its AABB overlaps.
    pub fn insert(&mut self, id: u64, min: [f64; 3], max: [f64; 3]) {
        let min_cx = self.cell_coord(min[0]);
        let min_cy = self.cell_coord(min[1]);
        let min_cz = self.cell_coord(min[2]);
        let max_cx = self.cell_coord(max[0]);
        let max_cy = self.cell_coord(max[1]);
        let max_cz = self.cell_coord(max[2]);
        for cx in min_cx..=max_cx {
            for cy in min_cy..=max_cy {
                for cz in min_cz..=max_cz {
                    self.cells.entry((cx, cy, cz)).or_default().push(id);
                }
            }
        }
    }
    /// Convert a world-space coordinate to its cell index.
    #[inline]
    pub fn cell_coord(&self, pos: f64) -> i32 {
        (pos / self.cell_size).floor() as i32
    }
    /// Return all object-id pairs that share at least one cell (conservative).
    pub fn query_potential_pairs(&self) -> Vec<(u64, u64)> {
        let mut pairs: Vec<(u64, u64)> = Vec::new();
        for ids in self.cells.values() {
            for i in 0..ids.len() {
                for j in (i + 1)..ids.len() {
                    let (lo, hi) = if ids[i] < ids[j] {
                        (ids[i], ids[j])
                    } else {
                        (ids[j], ids[i])
                    };
                    pairs.push((lo, hi));
                }
            }
        }
        pairs.sort_unstable();
        pairs.dedup();
        pairs
    }
    /// Remove all objects from all cells.
    pub fn clear(&mut self) {
        self.cells.clear();
    }
}
impl GridBroadphase {
    /// Return all object ids in the cell that contains world-space point `p`.
    pub fn query_point(&self, p: [f64; 3]) -> Vec<u64> {
        let key = (
            self.cell_coord(p[0]),
            self.cell_coord(p[1]),
            self.cell_coord(p[2]),
        );
        self.cells.get(&key).cloned().unwrap_or_default()
    }
    /// Return total number of object-cell entries (not unique objects).
    pub fn total_entries(&self) -> usize {
        self.cells.values().map(|v| v.len()).sum()
    }
    /// Return the number of occupied cells.
    pub fn occupied_cells(&self) -> usize {
        self.cells.len()
    }
    /// Remove a specific object id from all cells it occupies.
    ///
    /// Scans all cells — `O(cells * max_per_cell)`.  For high-density scenes
    /// prefer `clear` + bulk re-insert.
    pub fn remove(&mut self, id: u64) {
        for ids in self.cells.values_mut() {
            ids.retain(|&x| x != id);
        }
        self.cells.retain(|_, v| !v.is_empty());
    }
    /// Return a conservative over-approximation of all objects that could
    /// overlap `aabb` by checking every cell the AABB touches.
    pub fn query_aabb(&self, min: [f64; 3], max: [f64; 3]) -> Vec<u64> {
        let min_cx = self.cell_coord(min[0]);
        let min_cy = self.cell_coord(min[1]);
        let min_cz = self.cell_coord(min[2]);
        let max_cx = self.cell_coord(max[0]);
        let max_cy = self.cell_coord(max[1]);
        let max_cz = self.cell_coord(max[2]);
        let mut result = Vec::new();
        for cx in min_cx..=max_cx {
            for cy in min_cy..=max_cy {
                for cz in min_cz..=max_cz {
                    if let Some(ids) = self.cells.get(&(cx, cy, cz)) {
                        for &id in ids {
                            if !result.contains(&id) {
                                result.push(id);
                            }
                        }
                    }
                }
            }
        }
        result.sort_unstable();
        result
    }
}
/// A sorted endpoint array for one axis used by the incremental SAP.
///
/// Maintains a sorted list of `SapEndpointU32` entries and provides
/// insertion-sort for small moves.
#[derive(Debug, Clone, Default)]
pub struct SapAxis {
    /// Sorted endpoints on this axis.
    pub endpoints: Vec<SapEndpointU32>,
}
impl SapAxis {
    /// Create an empty axis.
    pub fn new() -> Self {
        Self {
            endpoints: Vec::new(),
        }
    }
    /// Insert min and max endpoints for `body_id` with the given values.
    pub fn insert(&mut self, body_id: u32, min_val: f64, max_val: f64) {
        self.endpoints.push(SapEndpointU32 {
            value: min_val,
            body_id,
            is_min: true,
        });
        self.endpoints.push(SapEndpointU32 {
            value: max_val,
            body_id,
            is_min: false,
        });
        let n = self.endpoints.len();
        for start in [n - 2, n - 1] {
            let mut i = start;
            while i > 0 && self.endpoints[i - 1].value > self.endpoints[i].value {
                self.endpoints.swap(i - 1, i);
                i -= 1;
            }
        }
    }
    /// Remove all endpoints belonging to `body_id`.
    pub fn remove(&mut self, body_id: u32) {
        self.endpoints.retain(|e| e.body_id != body_id);
    }
    /// Update the endpoints for `body_id` using insertion sort (efficient for small moves).
    pub fn update(&mut self, body_id: u32, min_val: f64, max_val: f64) {
        for ep in self.endpoints.iter_mut() {
            if ep.body_id == body_id {
                ep.value = if ep.is_min { min_val } else { max_val };
            }
        }
        let n = self.endpoints.len();
        for i in 1..n {
            let mut j = i;
            while j > 0 && self.endpoints[j - 1].value > self.endpoints[j].value {
                self.endpoints.swap(j - 1, j);
                j -= 1;
            }
        }
    }
    /// Sweep the sorted endpoints and return overlapping pairs on this axis.
    pub fn overlapping_pairs(&self) -> HashSet<(u32, u32)> {
        let mut pairs = HashSet::new();
        let mut active: Vec<u32> = Vec::new();
        for ep in &self.endpoints {
            if ep.is_min {
                for &aid in &active {
                    let key = if ep.body_id < aid {
                        (ep.body_id, aid)
                    } else {
                        (aid, ep.body_id)
                    };
                    pairs.insert(key);
                }
                active.push(ep.body_id);
            } else {
                active.retain(|&id| id != ep.body_id);
            }
        }
        pairs
    }
}
impl SapAxis {
    /// Return `true` if the endpoint list is sorted.
    pub fn is_sorted(&self) -> bool {
        axis_is_sorted(&self.endpoints)
    }
    /// Return the number of distinct bodies tracked on this axis.
    pub fn body_count(&self) -> usize {
        self.endpoints.iter().filter(|e| e.is_min).count()
    }
    /// Return the minimum value across all endpoints.
    pub fn min_value(&self) -> Option<f64> {
        self.endpoints.first().map(|e| e.value)
    }
    /// Return the maximum value across all endpoints.
    pub fn max_value(&self) -> Option<f64> {
        self.endpoints.last().map(|e| e.value)
    }
    /// Return all body ids currently tracked on this axis.
    pub fn tracked_bodies(&self) -> Vec<u32> {
        let mut ids: Vec<u32> = self
            .endpoints
            .iter()
            .filter(|e| e.is_min)
            .map(|e| e.body_id)
            .collect();
        ids.sort_unstable();
        ids.dedup();
        ids
    }
}
/// A snapshot of an `IncrementalSap` that can be restored cheaply.
#[derive(Debug, Clone)]
pub struct SapSnapshot {
    pub(super) aabbs: HashMap<u32, Aabb3>,
}
impl SapSnapshot {
    /// Capture the current AABB map from `sap`.
    pub fn capture(sap: &IncrementalSap) -> Self {
        Self {
            aabbs: sap.aabbs.clone(),
        }
    }
    /// Restore `sap` to the state recorded in this snapshot.
    ///
    /// Bodies added after the snapshot are removed; bodies removed after the
    /// snapshot are re-added; changed AABBs are reverted.
    pub fn restore(self, sap: &mut IncrementalSap) {
        let current_ids: Vec<u32> = sap.aabbs.keys().copied().collect();
        for id in &current_ids {
            if !self.aabbs.contains_key(id) {
                sap.remove(*id);
            }
        }
        for (id, aabb) in self.aabbs {
            if sap.aabbs.contains_key(&id) {
                sap.update(id, aabb);
            } else {
                sap.insert(id, aabb);
            }
        }
    }
}
/// An overlap event: a pair has just started or stopped overlapping.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OverlapEvent {
    /// Bodies `(a, b)` began overlapping this frame.
    Begin(u32, u32),
    /// Bodies `(a, b)` stopped overlapping this frame.
    End(u32, u32),
}
/// A simple AABB for the incremental SAP (raw f64 arrays).
#[derive(Debug, Clone)]
pub struct Aabb3 {
    /// Minimum corner `[x, y, z]`.
    pub min: [f64; 3],
    /// Maximum corner `[x, y, z]`.
    pub max: [f64; 3],
}
/// Statistics collected during a SAP broadphase query.
#[derive(Debug, Clone, Default)]
pub struct SapStats {
    /// Number of overlapping pairs found in the last query.
    pub pair_count: usize,
    /// Number of endpoint comparisons made during the last sweep.
    pub sweep_count: usize,
    /// Number of active bodies tracked.
    pub body_count: usize,
}
/// SAP that tracks pair-begin / pair-end events across frames.
///
/// Call `step_pairs` each frame; it returns only the *delta* — new overlaps
/// and vanished overlaps — rather than the full pair list.
pub struct EventDrivenSap {
    pub(super) inner: IncrementalSap,
    /// Pairs that were overlapping in the *previous* frame.
    pub(super) prev_pairs: HashSet<(u32, u32)>,
}
impl EventDrivenSap {
    /// Create a new event-driven SAP.
    pub fn new() -> Self {
        Self {
            inner: IncrementalSap::new(),
            prev_pairs: HashSet::new(),
        }
    }
    /// Insert a body.
    pub fn insert(&mut self, id: u32, aabb: Aabb3) {
        self.inner.insert(id, aabb);
    }
    /// Remove a body.  Any pairs it was part of will appear as `End` events
    /// on the next `step_pairs` call.
    pub fn remove(&mut self, id: u32) {
        self.inner.remove(id);
    }
    /// Update a body's AABB.
    pub fn update(&mut self, id: u32, aabb: Aabb3) {
        self.inner.update(id, aabb);
    }
    /// Compute overlap events for the current frame.
    ///
    /// Returns `(events, current_pairs)` where `events` lists only the
    /// begin/end transitions relative to the previous frame.
    pub fn step_pairs(&mut self) -> (Vec<OverlapEvent>, Vec<(u32, u32)>) {
        let current_vec = self.inner.query_pairs();
        let current: HashSet<(u32, u32)> = current_vec.iter().copied().collect();
        let mut events = Vec::new();
        for &pair in &current {
            if !self.prev_pairs.contains(&pair) {
                events.push(OverlapEvent::Begin(pair.0, pair.1));
            }
        }
        for &pair in &self.prev_pairs {
            if !current.contains(&pair) {
                events.push(OverlapEvent::End(pair.0, pair.1));
            }
        }
        self.prev_pairs = current;
        events.sort_by_key(|e| match e {
            OverlapEvent::Begin(a, b) | OverlapEvent::End(a, b) => (*a, *b),
        });
        (events, current_vec)
    }
    /// Number of currently tracked bodies.
    pub fn body_count(&self) -> usize {
        self.inner.body_count()
    }
}
/// Sweep-and-Prune broadphase that operates on the X axis and filters on Y/Z.
pub struct SweepAndPrune {
    /// All registered objects.
    pub objects: Vec<SapObject>,
    /// Sorted endpoints on the X axis.
    pub sorted_x: Vec<SapEndpoint>,
}
impl SweepAndPrune {
    /// Create an empty `SweepAndPrune` instance.
    pub fn new() -> Self {
        Self {
            objects: Vec::new(),
            sorted_x: Vec::new(),
        }
    }
    /// Add an object with the given AABB.
    pub fn add_object(&mut self, id: u64, min: [f64; 3], max: [f64; 3]) {
        self.objects.push(SapObject { id, min, max });
        self.sorted_x.push(SapEndpoint {
            value: min[0],
            object_id: id,
            is_min: true,
        });
        self.sorted_x.push(SapEndpoint {
            value: max[0],
            object_id: id,
            is_min: false,
        });
        self.sort_axis();
    }
    /// Remove the object with the given `id`.
    pub fn remove_object(&mut self, id: u64) {
        self.objects.retain(|o| o.id != id);
        self.sorted_x.retain(|e| e.object_id != id);
    }
    /// Update (replace) the AABB for an existing object.
    pub fn update_object(&mut self, id: u64, min: [f64; 3], max: [f64; 3]) {
        if let Some(obj) = self.objects.iter_mut().find(|o| o.id == id) {
            obj.min = min;
            obj.max = max;
        }
        for ep in self.sorted_x.iter_mut().filter(|e| e.object_id == id) {
            ep.value = if ep.is_min { min[0] } else { max[0] };
        }
        self.sort_axis();
    }
    /// Sort the X-axis endpoint list by value.
    pub fn sort_axis(&mut self) {
        self.sorted_x.sort_by(|a, b| {
            a.value
                .partial_cmp(&b.value)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
    }
    /// Return all overlapping pairs.
    ///
    /// Sweeps the sorted X endpoints to collect candidate pairs that overlap on
    /// X, then filters each candidate by checking Y and Z overlap as well.
    /// The returned list is deduplicated and has `(min_id, max_id)` ordering.
    pub fn query_overlapping_pairs(&mut self) -> Vec<(u64, u64)> {
        self.sort_axis();
        let obj_map: HashMap<u64, &SapObject> = self.objects.iter().map(|o| (o.id, o)).collect();
        let mut pairs: Vec<(u64, u64)> = Vec::new();
        let mut active: Vec<u64> = Vec::new();
        for ep in &self.sorted_x {
            if ep.is_min {
                if let Some(obj_a) = obj_map.get(&ep.object_id) {
                    for &active_id in &active {
                        if let Some(obj_b) = obj_map.get(&active_id)
                            && Self::overlaps_on_axis(
                                obj_a.min[1],
                                obj_a.max[1],
                                obj_b.min[1],
                                obj_b.max[1],
                            )
                            && Self::overlaps_on_axis(
                                obj_a.min[2],
                                obj_a.max[2],
                                obj_b.min[2],
                                obj_b.max[2],
                            )
                        {
                            let (lo, hi) = if ep.object_id < active_id {
                                (ep.object_id, active_id)
                            } else {
                                (active_id, ep.object_id)
                            };
                            pairs.push((lo, hi));
                        }
                    }
                }
                active.push(ep.object_id);
            } else {
                active.retain(|&id| id != ep.object_id);
            }
        }
        pairs.sort_unstable();
        pairs.dedup();
        pairs
    }
    /// Test whether two intervals `[min_a, max_a]` and `[min_b, max_b]` overlap.
    #[inline]
    pub fn overlaps_on_axis(min_a: f64, max_a: f64, min_b: f64, max_b: f64) -> bool {
        min_a <= max_b && min_b <= max_a
    }
    /// Return the number of registered objects.
    pub fn object_count(&self) -> usize {
        self.objects.len()
    }
}
impl SweepAndPrune {
    /// Batch-insert multiple objects at once, sorting the axis list only once.
    ///
    /// More efficient than calling `add_object` repeatedly when adding many
    /// objects at the same time.
    pub fn add_object_batch(&mut self, objects: &[(u64, [f64; 3], [f64; 3])]) {
        for &(id, min, max) in objects {
            self.objects.push(SapObject { id, min, max });
            self.sorted_x.push(SapEndpoint {
                value: min[0],
                object_id: id,
                is_min: true,
            });
            self.sorted_x.push(SapEndpoint {
                value: max[0],
                object_id: id,
                is_min: false,
            });
        }
        self.sort_axis();
    }
    /// Compute the variance of AABB centres along each axis.
    ///
    /// Returns `[var_x, var_y, var_z]`.  The axis with the highest variance is
    /// the best choice for the SAP sweep direction (maximises early-out
    /// opportunities).
    pub fn compute_axis_variance(&self) -> [f64; 3] {
        let n = self.objects.len();
        if n == 0 {
            return [0.0; 3];
        }
        let mut sum = [0.0_f64; 3];
        let mut sum_sq = [0.0_f64; 3];
        for obj in &self.objects {
            for ax in 0..3 {
                let c = (obj.min[ax] + obj.max[ax]) * 0.5;
                sum[ax] += c;
                sum_sq[ax] += c * c;
            }
        }
        let nf = n as f64;
        let mut var = [0.0_f64; 3];
        for ax in 0..3 {
            let mean = sum[ax] / nf;
            var[ax] = (sum_sq[ax] / nf) - mean * mean;
        }
        var
    }
    /// Reorder the SAP to sweep along the axis with the highest variance.
    ///
    /// Updates `self.axis` (if it were a stored field; here we rebuild
    /// `sorted_x` to reflect the chosen axis) and re-sorts the endpoint list.
    ///
    /// Returns the chosen axis index (`0`=X, `1`=Y, `2`=Z).
    pub fn reorder_axes(&mut self) -> usize {
        let var = self.compute_axis_variance();
        let best_axis = if var[0] >= var[1] && var[0] >= var[2] {
            0
        } else if var[1] >= var[2] {
            1
        } else {
            2
        };
        self.sorted_x.clear();
        for obj in &self.objects {
            self.sorted_x.push(SapEndpoint {
                value: obj.min[best_axis],
                object_id: obj.id,
                is_min: true,
            });
            self.sorted_x.push(SapEndpoint {
                value: obj.max[best_axis],
                object_id: obj.id,
                is_min: false,
            });
        }
        self.sort_axis();
        best_axis
    }
}
/// Incremental multi-axis Sweep-and-Prune broadphase.
///
/// Maintains sorted endpoint lists on X, Y, and Z axes and intersects
/// overlap sets from all three to produce the final pair set.
pub struct IncrementalSap {
    /// Sorted endpoints on the X axis.
    pub endpoints_x: Vec<SapEndpointU32>,
    /// Sorted endpoints on the Y axis.
    pub endpoints_y: Vec<SapEndpointU32>,
    /// Sorted endpoints on the Z axis.
    pub endpoints_z: Vec<SapEndpointU32>,
    /// Currently tracked AABBs keyed by body id.
    pub aabbs: HashMap<u32, Aabb3>,
    /// Active overlapping pairs from the last query.
    pub active_pairs: HashSet<(u32, u32)>,
}
impl IncrementalSap {
    /// Create an empty incremental SAP.
    pub fn new() -> Self {
        Self {
            endpoints_x: Vec::new(),
            endpoints_y: Vec::new(),
            endpoints_z: Vec::new(),
            aabbs: HashMap::new(),
            active_pairs: HashSet::new(),
        }
    }
    /// Insert a body with the given AABB.
    pub fn insert(&mut self, body_id: u32, aabb: Aabb3) {
        self.endpoints_x.push(SapEndpointU32 {
            value: aabb.min[0],
            body_id,
            is_min: true,
        });
        self.endpoints_x.push(SapEndpointU32 {
            value: aabb.max[0],
            body_id,
            is_min: false,
        });
        self.endpoints_y.push(SapEndpointU32 {
            value: aabb.min[1],
            body_id,
            is_min: true,
        });
        self.endpoints_y.push(SapEndpointU32 {
            value: aabb.max[1],
            body_id,
            is_min: false,
        });
        self.endpoints_z.push(SapEndpointU32 {
            value: aabb.min[2],
            body_id,
            is_min: true,
        });
        self.endpoints_z.push(SapEndpointU32 {
            value: aabb.max[2],
            body_id,
            is_min: false,
        });
        self.aabbs.insert(body_id, aabb);
    }
    /// Remove a body from the SAP.
    pub fn remove(&mut self, body_id: u32) {
        self.endpoints_x.retain(|e| e.body_id != body_id);
        self.endpoints_y.retain(|e| e.body_id != body_id);
        self.endpoints_z.retain(|e| e.body_id != body_id);
        self.aabbs.remove(&body_id);
        self.active_pairs
            .retain(|&(a, b)| a != body_id && b != body_id);
    }
    /// Update the AABB for an existing body.
    pub fn update(&mut self, body_id: u32, new_aabb: Aabb3) {
        for ep in self.endpoints_x.iter_mut().filter(|e| e.body_id == body_id) {
            ep.value = if ep.is_min {
                new_aabb.min[0]
            } else {
                new_aabb.max[0]
            };
        }
        for ep in self.endpoints_y.iter_mut().filter(|e| e.body_id == body_id) {
            ep.value = if ep.is_min {
                new_aabb.min[1]
            } else {
                new_aabb.max[1]
            };
        }
        for ep in self.endpoints_z.iter_mut().filter(|e| e.body_id == body_id) {
            ep.value = if ep.is_min {
                new_aabb.min[2]
            } else {
                new_aabb.max[2]
            };
        }
        self.aabbs.insert(body_id, new_aabb);
    }
    /// Batch update multiple bodies at once, then re-sort once.
    pub fn batch_update(&mut self, updates: &[(u32, Aabb3)]) {
        for (body_id, new_aabb) in updates {
            for ep in self
                .endpoints_x
                .iter_mut()
                .filter(|e| e.body_id == *body_id)
            {
                ep.value = if ep.is_min {
                    new_aabb.min[0]
                } else {
                    new_aabb.max[0]
                };
            }
            for ep in self
                .endpoints_y
                .iter_mut()
                .filter(|e| e.body_id == *body_id)
            {
                ep.value = if ep.is_min {
                    new_aabb.min[1]
                } else {
                    new_aabb.max[1]
                };
            }
            for ep in self
                .endpoints_z
                .iter_mut()
                .filter(|e| e.body_id == *body_id)
            {
                ep.value = if ep.is_min {
                    new_aabb.min[2]
                } else {
                    new_aabb.max[2]
                };
            }
            self.aabbs.insert(*body_id, new_aabb.clone());
        }
    }
    /// Query all overlapping pairs by intersecting results from all three axes.
    pub fn query_pairs(&mut self) -> Vec<(u32, u32)> {
        let pairs_x = Self::sort_and_sweep_axis(&mut self.endpoints_x);
        let pairs_y = Self::sort_and_sweep_axis(&mut self.endpoints_y);
        let pairs_z = Self::sort_and_sweep_axis(&mut self.endpoints_z);
        let result: HashSet<(u32, u32)> = pairs_x
            .intersection(&pairs_y)
            .copied()
            .collect::<HashSet<_>>()
            .intersection(&pairs_z)
            .copied()
            .collect();
        self.active_pairs = result.clone();
        let mut pairs: Vec<(u32, u32)> = result.into_iter().collect();
        pairs.sort_unstable();
        pairs
    }
    /// Sort endpoints on one axis and sweep to find overlapping pairs.
    pub fn sort_and_sweep_axis(endpoints: &mut [SapEndpointU32]) -> HashSet<(u32, u32)> {
        endpoints.sort_by(|a, b| {
            a.value
                .partial_cmp(&b.value)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut pairs = HashSet::new();
        let mut active: Vec<u32> = Vec::new();
        for ep in endpoints.iter() {
            if ep.is_min {
                for &aid in &active {
                    let (lo, hi) = if ep.body_id < aid {
                        (ep.body_id, aid)
                    } else {
                        (aid, ep.body_id)
                    };
                    pairs.insert((lo, hi));
                }
                active.push(ep.body_id);
            } else {
                active.retain(|&id| id != ep.body_id);
            }
        }
        pairs
    }
    /// Return the number of tracked bodies.
    pub fn body_count(&self) -> usize {
        self.aabbs.len()
    }
    /// Return the current set of active pairs (from last query).
    pub fn current_pairs(&self) -> &HashSet<(u32, u32)> {
        &self.active_pairs
    }
    /// Insert a new AABB for `id` and update the active pair set.
    pub fn insert_aabb(&mut self, id: u32, min: [f64; 3], max: [f64; 3]) {
        self.insert(id, Aabb3 { min, max });
        self.query_pairs();
    }
    /// Remove the AABB for `id` and clear its affected pairs.
    pub fn remove_aabb(&mut self, id: u32) {
        self.remove(id);
        self.active_pairs.retain(|&(a, b)| a != id && b != id);
    }
    /// Update the AABB for `id` using incremental re-sort (insertion sort for small moves).
    pub fn update_aabb(&mut self, id: u32, min: [f64; 3], max: [f64; 3]) {
        self.update(id, Aabb3 { min, max });
        self.query_pairs();
    }
    /// Return the current overlapping pair set.
    pub fn active_pairs(&self) -> &HashSet<(u32, u32)> {
        &self.active_pairs
    }
    /// Compute broadphase pairs between two disjoint sets using 3-axis SAP.
    ///
    /// Only pairs `(a, b)` where `a ∈ set_a` and `b ∈ set_b` are returned.
    pub fn bipartite_pairs(&self, set_a: &[u32], set_b: &[u32]) -> Vec<(u32, u32)> {
        let set_a_hs: HashSet<u32> = set_a.iter().copied().collect();
        let set_b_hs: HashSet<u32> = set_b.iter().copied().collect();
        let mut temp = IncrementalSap::new();
        for &id in set_a.iter().chain(set_b.iter()) {
            if let Some(aabb) = self.aabbs.get(&id) {
                temp.insert(id, aabb.clone());
            }
        }
        let all_pairs = temp.query_pairs();
        all_pairs
            .into_iter()
            .filter(|&(a, b)| {
                (set_a_hs.contains(&a) && set_b_hs.contains(&b))
                    || (set_a_hs.contains(&b) && set_b_hs.contains(&a))
            })
            .collect()
    }
}
impl IncrementalSap {
    /// Compute the variance of AABB centres on each axis, returning
    /// `[var_x, var_y, var_z]`.  The axis with highest variance is the best
    /// sweep direction.
    pub fn compute_axis_variance(&self) -> [f64; 3] {
        let n = self.aabbs.len();
        if n == 0 {
            return [0.0; 3];
        }
        let mut sum = [0.0_f64; 3];
        let mut sum_sq = [0.0_f64; 3];
        for aabb in self.aabbs.values() {
            for ax in 0..3 {
                let c = (aabb.min[ax] + aabb.max[ax]) * 0.5;
                sum[ax] += c;
                sum_sq[ax] += c * c;
            }
        }
        let nf = n as f64;
        let mut var = [0.0_f64; 3];
        for ax in 0..3 {
            let mean = sum[ax] / nf;
            var[ax] = (sum_sq[ax] / nf) - mean * mean;
        }
        var
    }
    /// Reorder all three endpoint arrays to sweep along the axis with the
    /// highest AABB-centre variance, then re-sort.
    ///
    /// Returns the chosen axis index (`0`=X, `1`=Y, `2`=Z).
    pub fn reorder_axes(&mut self) -> usize {
        let var = self.compute_axis_variance();
        let best = if var[0] >= var[1] && var[0] >= var[2] {
            0
        } else if var[1] >= var[2] {
            1
        } else {
            2
        };
        self.endpoints_x.clear();
        for (id, aabb) in &self.aabbs {
            self.endpoints_x.push(SapEndpointU32 {
                value: aabb.min[best],
                body_id: *id,
                is_min: true,
            });
            self.endpoints_x.push(SapEndpointU32 {
                value: aabb.max[best],
                body_id: *id,
                is_min: false,
            });
        }
        self.endpoints_x.sort_by(|a, b| {
            a.value
                .partial_cmp(&b.value)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        best
    }
}
impl IncrementalSap {
    /// Return `true` if any body in the SAP overlaps `aabb`.
    pub fn any_overlap(&self, aabb: &Aabb3) -> bool {
        for stored in self.aabbs.values() {
            if aabb3_overlaps(stored, aabb) {
                return true;
            }
        }
        false
    }
    /// Return the ids of all bodies whose AABB overlaps `aabb`.
    pub fn query_aabb(&self, aabb: &Aabb3) -> Vec<u32> {
        self.aabbs
            .iter()
            .filter_map(|(&id, stored)| {
                if aabb3_overlaps(stored, aabb) {
                    Some(id)
                } else {
                    None
                }
            })
            .collect()
    }
    /// Return the ids of all bodies within squared distance `radius_sq` of point `p`.
    pub fn query_sphere_sq(&self, p: [f64; 3], radius_sq: f64) -> Vec<u32> {
        self.aabbs
            .iter()
            .filter_map(|(&id, aabb)| {
                if aabb3_point_dist_sq(aabb, p) <= radius_sq {
                    Some(id)
                } else {
                    None
                }
            })
            .collect()
    }
    /// Remove all bodies and reset to empty.
    pub fn clear(&mut self) {
        self.endpoints_x.clear();
        self.endpoints_y.clear();
        self.endpoints_z.clear();
        self.aabbs.clear();
        self.active_pairs.clear();
    }
    /// Return the AABB for `id`, or `None` if not present.
    pub fn get_aabb(&self, id: u32) -> Option<&Aabb3> {
        self.aabbs.get(&id)
    }
    /// Return an iterator over all tracked body ids.
    pub fn body_ids(&self) -> impl Iterator<Item = u32> + '_ {
        self.aabbs.keys().copied()
    }
    /// Batch-insert all bodies from `other` into `self`.
    ///
    /// Bodies already in `self` are overwritten (their AABBs updated).
    pub fn merge_from(&mut self, other: &IncrementalSap) {
        for (&id, aabb) in &other.aabbs {
            if self.aabbs.contains_key(&id) {
                self.update(id, aabb.clone());
            } else {
                self.insert(id, aabb.clone());
            }
        }
    }
    /// Return `true` if `id` is currently tracked.
    pub fn contains(&self, id: u32) -> bool {
        self.aabbs.contains_key(&id)
    }
}
/// A multi-phase SAP that integrates event tracking and statistics.
pub struct MultiPhaseSap {
    pub(super) event_sap: EventDrivenSap,
    pub(super) stat_sap: StatTrackingSap,
}
impl MultiPhaseSap {
    /// Create a new multi-phase SAP.
    pub fn new() -> Self {
        Self {
            event_sap: EventDrivenSap::new(),
            stat_sap: StatTrackingSap::new(),
        }
    }
    /// Insert a body into both SAP structures.
    pub fn insert(&mut self, id: u32, aabb: Aabb3) {
        self.event_sap.insert(id, aabb.clone());
        self.stat_sap.insert(id, aabb);
    }
    /// Remove a body from both SAP structures.
    pub fn remove(&mut self, id: u32) {
        self.event_sap.remove(id);
        self.stat_sap.remove(id);
    }
    /// Update a body's AABB in both SAP structures.
    pub fn update(&mut self, id: u32, aabb: Aabb3) {
        self.event_sap.update(id, aabb.clone());
        self.stat_sap.update(id, aabb);
    }
    /// Perform a full broadphase step and return the combined result.
    pub fn step(&mut self) -> BroadphaseResult {
        let pairs = self.stat_sap.query_pairs();
        let stats = self.stat_sap.stats.clone();
        let (events, _) = self.event_sap.step_pairs();
        let mut new_pairs = Vec::new();
        let mut lost_pairs = Vec::new();
        for ev in events {
            match ev {
                OverlapEvent::Begin(a, b) => new_pairs.push((a, b)),
                OverlapEvent::End(a, b) => lost_pairs.push((a, b)),
            }
        }
        BroadphaseResult {
            pairs,
            new_pairs,
            lost_pairs,
            stats,
        }
    }
}