rust/hwphysics/src/grid.rs
changeset 16113 36862a9ec59b
parent 16010 5ba4d3a0c3eb
equal deleted inserted replaced
16112:004258297037 16113:36862a9ec59b
    33             false
    33             false
    34         }
    34         }
    35     }
    35     }
    36 }
    36 }
    37 
    37 
    38 const GRID_BIN_SIZE: usize = 128;
    38 const GRID_BIN_SIZE: u32 = 128;
    39 
    39 
    40 pub struct Grid {
    40 pub struct Grid {
    41     bins: Vec<GridBin>,
    41     bins: Vec<GridBin>,
    42     space_size: PotSize,
    42     space_size: PotSize,
    43     bins_count: PotSize,
    43     bins_count: PotSize,
    55             bins_count,
    55             bins_count,
    56             index: PotSize::square(GRID_BIN_SIZE).unwrap().to_grid_index(),
    56             index: PotSize::square(GRID_BIN_SIZE).unwrap().to_grid_index(),
    57         }
    57         }
    58     }
    58     }
    59 
    59 
    60     fn linear_bin_index(&self, index: Point) -> usize {
    60     fn linear_bin_index(&self, index: Point) -> u32 {
    61         self.bins_count
    61         self.bins_count
    62             .linear_index(index.x as usize, index.y as usize)
    62             .linear_index(index.x as u32, index.y as u32)
    63     }
    63     }
    64 
    64 
    65     fn bin_index(&self, position: &FPPoint) -> Point {
    65     fn bin_index(&self, position: &FPPoint) -> Point {
    66         self.index.map(Point::from_fppoint(position))
    66         self.index.map(Point::from_fppoint(position))
    67     }
    67     }
    68 
    68 
    69     fn get_bin(&mut self, index: Point) -> &mut GridBin {
    69     fn get_bin(&mut self, index: Point) -> &mut GridBin {
    70         let index = self.linear_bin_index(index);
    70         let index = self.linear_bin_index(index);
    71         &mut self.bins[index]
    71         &mut self.bins[index as usize]
    72     }
    72     }
    73 
    73 
    74     fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
    74     fn try_get_bin(&mut self, index: Point) -> Option<&mut GridBin> {
    75         let index = self.linear_bin_index(index);
    75         let index = self.linear_bin_index(index);
    76         self.bins.get_mut(index)
    76         self.bins.get_mut(index as usize)
    77     }
    77     }
    78 
    78 
    79     fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
    79     fn lookup_bin(&mut self, position: &FPPoint) -> &mut GridBin {
    80         self.get_bin(self.bin_index(position))
    80         self.get_bin(self.bin_index(position))
    81     }
    81     }