15120
|
1 |
use crate::{
|
|
2 |
collision::{fppoint_round, CircleBounds, DetectedCollisions},
|
|
3 |
common::GearId,
|
|
4 |
};
|
|
5 |
|
|
6 |
use fpnum::FPPoint;
|
15828
|
7 |
use integral_geometry::{GridIndex, Point, PotSize};
|
15120
|
8 |
|
|
9 |
struct GridBin {
|
15827
|
10 |
refs: Vec<GearId>,
|
|
11 |
entries: Vec<CircleBounds>,
|
15120
|
12 |
}
|
|
13 |
|
|
14 |
impl GridBin {
|
|
15 |
fn new() -> Self {
|
|
16 |
Self {
|
15827
|
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
|
15120
|
34 |
}
|
|
35 |
}
|
|
36 |
}
|
|
37 |
|
|
38 |
const GRID_BIN_SIZE: usize = 128;
|
|
39 |
|
|
40 |
pub struct Grid {
|
|
41 |
bins: Vec<GridBin>,
|
15828
|
42 |
space_size: PotSize,
|
|
43 |
bins_count: PotSize,
|
15120
|
44 |
index: GridIndex,
|
|
45 |
}
|
|
46 |
|
|
47 |
impl Grid {
|
15828
|
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();
|
15120
|
51 |
|
|
52 |
Self {
|
|
53 |
bins: (0..bins_count.area()).map(|_| GridBin::new()).collect(),
|
|
54 |
space_size: size,
|
|
55 |
bins_count,
|
15828
|
56 |
index: PotSize::square(GRID_BIN_SIZE).unwrap().to_grid_index(),
|
15120
|
57 |
}
|
|
58 |
}
|
|
59 |
|
15828
|
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 |
|
15120
|
65 |
fn bin_index(&self, position: &FPPoint) -> Point {
|
|
66 |
self.index.map(fppoint_round(position))
|
|
67 |
}
|
|
68 |
|
15261
|
69 |
fn get_bin(&mut self, index: Point) -> &mut GridBin {
|
15828
|
70 |
let index = self.linear_bin_index(index);
|
|
71 |
&mut self.bins[index]
|
15261
|
72 |
}
|
|
73 |
|
15757
|
74 |
fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
|
15828
|
75 |
let index = self.linear_bin_index(index);
|
|
76 |
self.bins.get_mut(index)
|
15757
|
77 |
}
|
|
78 |
|
15120
|
79 |
fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
|
15261
|
80 |
self.get_bin(self.bin_index(position))
|
15120
|
81 |
}
|
|
82 |
|
15827
|
83 |
pub fn insert(&mut self, gear_id: GearId, bounds: &CircleBounds) {
|
|
84 |
self.lookup_bin(&bounds.center).add(gear_id, bounds);
|
15261
|
85 |
}
|
|
86 |
|
15827
|
87 |
fn remove_all(&mut self, gear_id: GearId) {
|
|
88 |
for bin in &mut self.bins {
|
|
89 |
if bin.remove(gear_id) {
|
|
90 |
break;
|
15757
|
91 |
}
|
15261
|
92 |
}
|
15120
|
93 |
}
|
|
94 |
|
15827
|
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 |
|
15120
|
105 |
pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
|
|
106 |
for bin in &self.bins {
|
15828
|
107 |
for (index, bounds) in bin.entries.iter().enumerate() {}
|
15120
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|