This commit is contained in:
Simon Gardling
2021-03-29 14:31:23 +00:00
parent 40d0d5632f
commit ef9346c701
2 changed files with 23 additions and 26 deletions

View File

@@ -76,8 +76,7 @@ impl PopulationConfig {
} }
} }
/// A 2D grid with a scalar value per each grid block. Each grid is occupied by a single population, /// A 2D grid with a scalar value per each grid block. Each grid is occupied by a single population, hence we store the population config inside the grid.
/// hence we store the population config inside the grid.
#[derive(Debug)] #[derive(Debug)]
pub struct Grid { pub struct Grid {
pub config: PopulationConfig, pub config: PopulationConfig,
@@ -101,7 +100,6 @@ impl Clone for Grid {
buf: self.buf.clone(), buf: self.buf.clone(),
blur: self.blur.clone(), blur: self.blur.clone(),
} }
// return Grid::new();
} }
} }
@@ -132,8 +130,7 @@ impl Grid {
j * self.width + i j * self.width + i
} }
/// Get the buffer value at a given position. The implementation effectively treats data as /// Get the buffer value at a given position. The implementation effectively treats data as periodic, hence any finite position will produce a value.
/// periodic, hence any finite position will produce a value.
pub fn get_buf(&self, x: f32, y: f32) -> f32 { pub fn get_buf(&self, x: f32, y: f32) -> f32 {
self.buf[self.index(x, y)] self.buf[self.index(x, y)]
} }
@@ -181,8 +178,7 @@ where
let datas: Vec<_> = grids.iter().map(|grid| &grid.data).collect(); let datas: Vec<_> = grids.iter().map(|grid| &grid.data).collect();
let bufs: Vec<_> = grids.iter().map(|grid| &grid.buf).collect(); let bufs: Vec<_> = grids.iter().map(|grid| &grid.buf).collect();
// We mutate grid buffers and read grid data. We use unsafe because we need shared/unique // We mutate grid buffers and read grid data. We use unsafe because we need shared/unique borrows on different fields of the same Grid struct.
// borrows on different fields of the same Grid struct.
bufs.iter().enumerate().for_each(|(i, buf)| unsafe { bufs.iter().enumerate().for_each(|(i, buf)| unsafe {
let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>; let buf_ptr = *buf as *const Vec<f32> as *mut Vec<f32>;
buf_ptr.as_mut().unwrap().fill(0.0); buf_ptr.as_mut().unwrap().fill(0.0);

View File

@@ -155,20 +155,9 @@ impl Model {
} }
} }
fn pick_direction<R: Rng + ?Sized>(center: f32, left: f32, right: f32, rng: &mut R) -> f32 {
if (center > left) && (center > right) {
return 0.0;
} else if (center < left) && (center < right) {
return *[-1.0, 1.0].choose(rng).unwrap();
} else if left < right {
return 1.0;
} else if right < left {
return -1.0;
}
return 0.0;
}
/// Simulates `steps` # of steps /// Simulates `steps` # of steps
#[inline(always)]
pub fn run(&mut self, steps: usize) { pub fn run(&mut self, steps: usize) {
let debug: bool = false; let debug: bool = false;
@@ -189,7 +178,9 @@ impl Model {
// Combine grids // Combine grids
let grids = &mut self.grids; let grids = &mut self.grids;
combine(grids, &self.attraction_table); combine(grids, &self.attraction_table);
let agents_tick_time = Instant::now(); let agents_tick_time = Instant::now();
self.agents.par_iter_mut().for_each(|agent| { self.agents.par_iter_mut().for_each(|agent| {
// let i: usize = agent.i; // let i: usize = agent.i;
@@ -214,15 +205,25 @@ impl Model {
let xr = agent.x + agent_add_sens.cos() * sensor_distance; let xr = agent.x + agent_add_sens.cos() * sensor_distance;
let yr = agent.y + agent_add_sens.sin() * sensor_distance; let yr = agent.y + agent_add_sens.sin() * sensor_distance;
// Sense. We sense from the buffer because this is where we previously combined data // We sense from the buffer because this is where we previously combined data from all the grid.
// from all the grid. let center = grid.get_buf(xc, yc);
let trail_c = grid.get_buf(xc, yc); let left = grid.get_buf(xl, yl);
let trail_l = grid.get_buf(xl, yl); let right = grid.get_buf(xr, yr);
let trail_r = grid.get_buf(xr, yr);
// Rotate and move // Rotate and move logic
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let direction = Model::pick_direction(trail_c, trail_l, trail_r, &mut rng); let mut direction: f32 = 0.0;
if (center > left) && (center > right) {
direction = 0.0;
} else if (center < left) && (center < right) {
direction = *[-1.0, 1.0].choose(&mut rng).unwrap();
} else if left < right {
direction = 1.0;
} else if right < left {
direction = -1.0;
}
agent.rotate_and_move(direction, rotation_angle, step_distance, width, height); agent.rotate_and_move(direction, rotation_angle, step_distance, width, height);
}); });