From d6d1e00b357debc50850cfd899b8733eade7cf92 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 31 Mar 2021 13:04:35 -0400 Subject: [PATCH] things --- Notes.md | 2 +- src/lib.rs | 2 +- src/main.rs | 4 +- src/math.rs | 1 + src/model.rs | 106 +++++++++++++++++++++++++-------------------------- 5 files changed, 56 insertions(+), 59 deletions(-) diff --git a/Notes.md b/Notes.md index ea61c1f..9e39ec6 100644 --- a/Notes.md +++ b/Notes.md @@ -1,4 +1,4 @@ -## sin and cos optimizations: +## sin and cos implementations: ### Setup/Info: - measured in ms/agent ticked - 2048 iterations diff --git a/src/lib.rs b/src/lib.rs index 9b2bb82..9b05422 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,4 @@ mod imgdata; // for storing image data mod math; pub mod model; mod palette; -mod util; // for math things +mod util; // for math things \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 32c5b97..dd3cff5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ fn main() { let n_iterations = 2048; // Size of grid and pictures - // let (width, height) = (256, 256); - let (width, height) = (1024, 1024); + let (width, height) = (256, 256); + // let (width, height) = (1024, 1024); // # of agents let n_particles = 1 << 20; diff --git a/src/math.rs b/src/math.rs index 90e4e76..dde2b75 100644 --- a/src/math.rs +++ b/src/math.rs @@ -37,3 +37,4 @@ pub fn cos(mut x: f32) -> f32 { pub fn sin(x: f32) -> f32 { return cos(x - std::f32::consts::FRAC_PI_2); } + diff --git a/src/model.rs b/src/model.rs index ea70859..4c0d57d 100644 --- a/src/model.rs +++ b/src/model.rs @@ -34,60 +34,6 @@ impl Agent { i: i, } } - - #[inline] - pub fn tick(&mut self, grid: &Grid) { - let (width, height) = (grid.width, grid.height); - let PopulationConfig { - sensor_distance, - sensor_angle, - rotation_angle, - step_distance, - .. - } = grid.config; - - let xc = self.x + fastapprox::faster::cos(self.angle) * sensor_distance; - let yc = self.y + fastapprox::faster::sin(self.angle) * sensor_distance; - - let agent_add_sens = self.angle + sensor_angle; - let agent_sub_sens = self.angle - sensor_angle; - - let xl = self.x + fastapprox::faster::cos(agent_sub_sens) * sensor_distance; - let yl = self.y + fastapprox::faster::sin(agent_sub_sens) * sensor_distance; - let xr = self.x + fastapprox::faster::cos(agent_add_sens) * sensor_distance; - let yr = self.y + fastapprox::faster::sin(agent_add_sens) * sensor_distance; - - // We sense from the buffer because this is where we previously combined data from all the grid. - let center = grid.get_buf(xc, yc); - let left = grid.get_buf(xl, yl); - let right = grid.get_buf(xr, yr); - - // Rotate and move logic - let mut rng = rand::thread_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; - } - - let delta_angle = rotation_angle * direction; - - self.angle = wrap(self.angle + delta_angle, TAU); - self.x = wrap( - self.x + step_distance * fastapprox::faster::cos(self.angle), - width as f32, - ); - self.y = wrap( - self.y + step_distance * fastapprox::faster::sin(self.angle), - height as f32, - ); - } } impl Clone for Agent { @@ -223,7 +169,57 @@ impl Model { // Tick agents self.agents.par_iter_mut().for_each(|agent| { - agent.tick(&grids[agent.population_id]); + let grid = &grids[agent.population_id]; + let (width, height) = (grid.width, grid.height); + let PopulationConfig { + sensor_distance, + sensor_angle, + rotation_angle, + step_distance, + .. + } = grid.config; + + let xc = agent.x + fastapprox::faster::cos(agent.angle) * sensor_distance; + let yc = agent.y + fastapprox::faster::sin(agent.angle) * sensor_distance; + + let agent_add_sens = agent.angle + sensor_angle; + let agent_sub_sens = agent.angle - sensor_angle; + + let xl = agent.x + fastapprox::faster::cos(agent_sub_sens) * sensor_distance; + let yl = agent.y + fastapprox::faster::sin(agent_sub_sens) * sensor_distance; + let xr = agent.x + fastapprox::faster::cos(agent_add_sens) * sensor_distance; + let yr = agent.y + fastapprox::faster::sin(agent_add_sens) * sensor_distance; + + // We sense from the buffer because this is where we previously combined data from all the grid. + let center = grid.get_buf(xc, yc); + let left = grid.get_buf(xl, yl); + let right = grid.get_buf(xr, yr); + + // Rotate and move logic + let mut rng = rand::thread_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; + } + + let delta_angle = rotation_angle * direction; + + agent.angle = wrap(agent.angle + delta_angle, TAU); + agent.x = wrap( + agent.x + step_distance * fastapprox::faster::cos(agent.angle), + width as f32, + ); + agent.y = wrap( + agent.y + step_distance * fastapprox::faster::sin(agent.angle), + height as f32, + ); }); // Deposit // TODO - Make this parallel