15141
|
1 |
use crate::{
|
|
2 |
collision::{fppoint_round, CircleBounds, DetectedCollisions},
|
|
3 |
common::GearId,
|
|
4 |
};
|
|
5 |
|
|
6 |
use fpnum::FPPoint;
|
15850
|
7 |
use integral_geometry::{GridIndex, Point, PotSize};
|
15141
|
8 |
|
|
9 |
struct GridBin {
|
15849
|
10 |
refs: Vec<GearId>,
|
|
11 |
entries: Vec<CircleBounds>,
|
15141
|
12 |
}
|
|
13 |
|
|
14 |
impl GridBin {
|
|
15 |
fn new() -> Self {
|
|
16 |
Self {
|
15849
|
17 |
refs: vec![],
|
|
18 |
entries: vec![],
|
|
19 |
}
|
|
20 |
}
|
|
21 |
|
|
22 |
fn add(&mut self, gear_id: GearId, bounds: &CircleBounds) {
|
|
23 |
self.refs.push(gear_id);
|
|
24 |
self.entries.push(*bounds);
|
|
25 |
}
|
|
26 |
|
|
27 |
fn remove(&mut self, gear_id: GearId) -> bool {
|
|
28 |
if let Some(pos) = self.refs.iter().position(|id| *id == gear_id) {
|
|
29 |
self.refs.swap_remove(pos);
|
|
30 |
self.entries.swap_remove(pos);
|
|
31 |
true
|
|
32 |
} else {
|
|
33 |
false
|
15141
|
34 |
}
|
|
35 |
}
|
|
36 |
}
|
|
37 |
|
|
38 |
const GRID_BIN_SIZE: usize = 128;
|
|
39 |
|
|
40 |
pub struct Grid {
|
|
41 |
bins: Vec<GridBin>,
|
15850
|
42 |
space_size: PotSize,
|
|
43 |
bins_count: PotSize,
|
15141
|
44 |
index: GridIndex,
|
|
45 |
}
|
|
46 |
|
|
47 |
impl Grid {
|
15850
|
48 |
pub fn new(size: PotSize) -> Self {
|
|
49 |
let bins_count =
|
|
50 |
PotSize::new(size.width() / GRID_BIN_SIZE, size.height() / GRID_BIN_SIZE).unwrap();
|
15141
|
51 |
|
|
52 |
Self {
|
|
53 |
bins: (0..bins_count.area()).map(|_| GridBin::new()).collect(),
|
|
54 |
space_size: size,
|
|
55 |
bins_count,
|
15850
|
56 |
index: PotSize::square(GRID_BIN_SIZE).unwrap().to_grid_index(),
|
15141
|
57 |
}
|
|
58 |
}
|
|
59 |
|
15850
|
60 |
fn linear_bin_index(&self, index: Point) -> usize {
|
|
61 |
self.bins_count
|
|
62 |
.linear_index(index.x as usize, index.y as usize)
|
|
63 |
}
|
|
64 |
|
15141
|
65 |
fn bin_index(&self, position: &FPPoint) -> Point {
|
|
66 |
self.index.map(fppoint_round(position))
|
|
67 |
}
|
|
68 |
|
15282
|
69 |
fn get_bin(&mut self, index: Point) -> &mut GridBin {
|
15850
|
70 |
let index = self.linear_bin_index(index);
|
|
71 |
&mut self.bins[index]
|
15282
|
72 |
}
|
|
73 |
|
15779
|
74 |
fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
|
15850
|
75 |
let index = self.linear_bin_index(index);
|
|
76 |
self.bins.get_mut(index)
|
15779
|
77 |
}
|
|
78 |
|
15141
|
79 |
fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
|
15282
|
80 |
self.get_bin(self.bin_index(position))
|
15141
|
81 |
}
|
|
82 |
|
15849
|
83 |
pub fn insert(&mut self, gear_id: GearId, bounds: &CircleBounds) {
|
|
84 |
self.lookup_bin(&bounds.center).add(gear_id, bounds);
|
15282
|
85 |
}
|
|
86 |
|
15849
|
87 |
fn remove_all(&mut self, gear_id: GearId) {
|
|
88 |
for bin in &mut self.bins {
|
|
89 |
if bin.remove(gear_id) {
|
|
90 |
break;
|
15779
|
91 |
}
|
15282
|
92 |
}
|
15141
|
93 |
}
|
|
94 |
|
15849
|
95 |
pub fn remove(&mut self, gear_id: GearId, bounds: Option<&CircleBounds>) {
|
|
96 |
if let Some(bounds) = bounds {
|
|
97 |
if !self.lookup_bin(&bounds.center).remove(gear_id) {
|
|
98 |
self.remove_all(gear_id);
|
|
99 |
}
|
|
100 |
} else {
|
|
101 |
self.remove_all(gear_id);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
15141
|
105 |
pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
|
|
106 |
for bin in &self.bins {
|
15850
|
107 |
for (index, bounds) in bin.entries.iter().enumerate() {}
|
15141
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|