rust/hwphysics/src/grid.rs
author alfadur
Fri, 28 Jan 2022 02:33:44 +0300
changeset 15851 d5e6c8c92d87
parent 15850 44b49f255e31
child 16010 5ba4d3a0c3eb
permissions -rw-r--r--
add direct access to gear data
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     1
use crate::{
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     2
    collision::{fppoint_round, CircleBounds, DetectedCollisions},
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     3
    common::GearId,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     4
};
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     5
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     6
use fpnum::FPPoint;
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
     7
use integral_geometry::{GridIndex, Point, PotSize};
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     8
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
     9
struct GridBin {
15849
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    10
    refs: Vec<GearId>,
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    11
    entries: Vec<CircleBounds>,
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    12
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    13
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    14
impl GridBin {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    15
    fn new() -> Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    16
        Self {
15849
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    17
            refs: vec![],
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    18
            entries: vec![],
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    19
        }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    20
    }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    21
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    22
    fn add(&mut self, gear_id: GearId, bounds: &CircleBounds) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    23
        self.refs.push(gear_id);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    24
        self.entries.push(*bounds);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    25
    }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    26
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    27
    fn remove(&mut self, gear_id: GearId) -> bool {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    28
        if let Some(pos) = self.refs.iter().position(|id| *id == gear_id) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    29
            self.refs.swap_remove(pos);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    30
            self.entries.swap_remove(pos);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    31
            true
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    32
        } else {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    33
            false
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    34
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    35
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    36
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    37
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    38
const GRID_BIN_SIZE: usize = 128;
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    39
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    40
pub struct Grid {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    41
    bins: Vec<GridBin>,
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    42
    space_size: PotSize,
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    43
    bins_count: PotSize,
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    44
    index: GridIndex,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    45
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    46
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    47
impl Grid {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    48
    pub fn new(size: PotSize) -> Self {
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    49
        let bins_count =
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    50
            PotSize::new(size.width() / GRID_BIN_SIZE, size.height() / GRID_BIN_SIZE).unwrap();
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    51
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    52
        Self {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    53
            bins: (0..bins_count.area()).map(|_| GridBin::new()).collect(),
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    54
            space_size: size,
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    55
            bins_count,
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    56
            index: PotSize::square(GRID_BIN_SIZE).unwrap().to_grid_index(),
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    57
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    58
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    59
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    60
    fn linear_bin_index(&self, index: Point) -> usize {
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    61
        self.bins_count
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    62
            .linear_index(index.x as usize, index.y as usize)
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    63
    }
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    64
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    65
    fn bin_index(&self, position: &FPPoint) -> Point {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    66
        self.index.map(fppoint_round(position))
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    67
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    68
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    69
    fn get_bin(&mut self, index: Point) -> &mut GridBin {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    70
        let index = self.linear_bin_index(index);
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    71
        &mut self.bins[index]
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    72
    }
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    73
15779
c7332c7f64cd remove crash from collision
alfadur
parents: 15778
diff changeset
    74
    fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    75
        let index = self.linear_bin_index(index);
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
    76
        self.bins.get_mut(index)
15779
c7332c7f64cd remove crash from collision
alfadur
parents: 15778
diff changeset
    77
    }
c7332c7f64cd remove crash from collision
alfadur
parents: 15778
diff changeset
    78
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    79
    fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    80
        self.get_bin(self.bin_index(position))
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    81
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    82
15849
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    83
    pub fn insert(&mut self, gear_id: GearId, bounds: &CircleBounds) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    84
        self.lookup_bin(&bounds.center).add(gear_id, bounds);
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    85
    }
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    86
15849
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    87
    fn remove_all(&mut self, gear_id: GearId) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    88
        for bin in &mut self.bins {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    89
            if bin.remove(gear_id) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    90
                break;
15779
c7332c7f64cd remove crash from collision
alfadur
parents: 15778
diff changeset
    91
            }
15282
501dfa1c8deb update collision grid with position changes
alfadur
parents: 15141
diff changeset
    92
        }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    93
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
    94
15849
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    95
    pub fn remove(&mut self, gear_id: GearId, bounds: Option<&CircleBounds>) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    96
        if let Some(bounds) = bounds {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    97
            if !self.lookup_bin(&bounds.center).remove(gear_id) {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    98
                self.remove_all(gear_id);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
    99
            }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
   100
        } else {
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
   101
            self.remove_all(gear_id);
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
   102
        }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
   103
    }
64b0a5cead86 use grid only for static gears
alfadur
parents: 15779
diff changeset
   104
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   105
    pub fn check_collisions(&self, collisions: &mut DetectedCollisions) {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   106
        for bin in &self.bins {
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 15849
diff changeset
   107
            for (index, bounds) in bin.entries.iter().enumerate() {}
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   108
        }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   109
    }
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14737
diff changeset
   110
}