This commit is contained in:
Simon Gardling 2025-03-27 14:22:06 -04:00
parent 9881502002
commit 75fab93907
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 10 additions and 65 deletions

View File

@ -1,5 +1,4 @@
use crate::{buffer::Buf, util::wrap}; use crate::{buffer::Buf, util::wrap};
use fastapprox::faster::{cos, sin}; use fastapprox::faster::{cos, sin};
use rand::{seq::SliceRandom, Rng}; use rand::{seq::SliceRandom, Rng};
use std::f32::consts::TAU; use std::f32::consts::TAU;
@ -41,7 +40,6 @@ impl Agent {
} }
// Tick an agent // Tick an agent
#[inline]
pub fn tick( pub fn tick(
&mut self, &mut self,
buf: &Buf, buf: &Buf,
@ -75,7 +73,9 @@ impl Agent {
if (center > left) && (center > right) { if (center > left) && (center > right) {
direction = 0.0; direction = 0.0;
} else if (center < left) && (center < right) { } else if (center < left) && (center < right) {
direction = *[-1.0, 1.0].choose(&mut rng).unwrap(); direction = *[-1.0, 1.0]
.choose(&mut rng)
.expect("unable to choose random direction");
} else if left < right { } else if left < right {
direction = 1.0; direction = 1.0;
} else if right < left { } else if right < left {

View File

@ -1,19 +1,11 @@
use itertools::multizip; use itertools::multizip;
use rayon::prelude::*; use rayon::prelude::*;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Blur { pub struct Blur {
row_buffer: Vec<f32>, row_buffer: Vec<f32>,
} }
impl Clone for Blur {
fn clone(&self) -> Blur {
Blur {
row_buffer: self.row_buffer.clone(),
}
}
}
impl Blur { impl Blur {
pub fn new(width: usize) -> Self { pub fn new(width: usize) -> Self {
Blur { Blur {

View File

@ -23,33 +23,15 @@ impl Display for PopulationConfig {
} }
impl PopulationConfig { impl PopulationConfig {
const SENSOR_ANGLE_MIN: f32 = 0.0;
const SENSOR_ANGLE_MAX: f32 = 120.0;
const SENSOR_DISTANCE_MIN: f32 = 0.0;
const SENSOR_DISTANCE_MAX: f32 = 64.0;
const ROTATION_ANGLE_MIN: f32 = 0.0;
const ROTATION_ANGLE_MAX: f32 = 120.0;
const STEP_DISTANCE_MIN: f32 = 0.2;
const STEP_DISTANCE_MAX: f32 = 2.0;
const DEPOSITION_AMOUNT_MIN: f32 = 5.0;
const DEPOSITION_AMOUNT_MAX: f32 = 5.0;
const DECAY_FACTOR_MIN: f32 = 0.1;
const DECAY_FACTOR_MAX: f32 = 0.1;
// Construct a random configuration. // Construct a random configuration.
pub fn new<R: Rng + ?Sized>(rng: &mut R) -> Self { pub fn new<R: Rng + ?Sized>(rng: &mut R) -> Self {
PopulationConfig { PopulationConfig {
sensor_distance: rng.gen_range(Self::SENSOR_DISTANCE_MIN..=Self::SENSOR_DISTANCE_MAX), sensor_distance: rng.gen_range(0.0..=64.0),
step_distance: rng.gen_range(Self::STEP_DISTANCE_MIN..=Self::STEP_DISTANCE_MAX), step_distance: rng.gen_range(0.2..=2.0),
decay_factor: rng.gen_range(Self::DECAY_FACTOR_MIN..=Self::DECAY_FACTOR_MAX), decay_factor: rng.gen_range(0.1..=0.1),
sensor_angle: rng sensor_angle: rng.gen_range(0.0_f32..=120.0).to_radians(),
.gen_range(Self::SENSOR_ANGLE_MIN..=Self::SENSOR_ANGLE_MAX) rotation_angle: rng.gen_range(0.0_f32..=120.0).to_radians(),
.to_radians(), deposition_amount: rng.gen_range(5.0..=5.0),
rotation_angle: rng
.gen_range(Self::ROTATION_ANGLE_MIN..=Self::ROTATION_ANGLE_MAX)
.to_radians(),
deposition_amount: rng
.gen_range(Self::DEPOSITION_AMOUNT_MIN..=Self::DEPOSITION_AMOUNT_MAX),
} }
} }
} }
@ -103,13 +85,6 @@ impl Grid {
j * self.width + i j * self.width + i
} }
/*
// Get the buffer value at a given position. The implementation effectively treats data as periodic, hence any finite position will produce a value.
pub fn get_buf(&self, x: f32, y: f32) -> f32 {
self.buf.buf[self.index(x, y)]
}
*/
// Add a value to the grid data at a given position. // Add a value to the grid data at a given position.
pub fn deposit(&mut self, x: f32, y: f32) { pub fn deposit(&mut self, x: f32, y: f32) {
let idx = self.index(x, y); let idx = self.index(x, y);
@ -128,7 +103,6 @@ impl Grid {
); );
} }
#[inline]
pub fn tick(&mut self) { pub fn tick(&mut self) {
let (width, height) = (self.width, self.height); let (width, height) = (self.width, self.height);
let PopulationConfig { let PopulationConfig {
@ -153,33 +127,12 @@ impl Grid {
self.deposit_all(); self.deposit_all();
} }
#[inline]
pub fn deposit_all(&mut self) { pub fn deposit_all(&mut self) {
let agent_list = self.agents.clone(); let agent_list = self.agents.clone();
for agent in agent_list.iter() { for agent in agent_list.iter() {
self.deposit(agent.x, agent.y); self.deposit(agent.x, agent.y);
} }
} }
// No longer needed (moved to imgdata.rs)
/*
pub fn quantile(&self, fraction: f32) -> f32 {
let index = if (fraction - 1.0_f32).abs() < f32::EPSILON {
self.data.len() - 1
} else {
(self.data.len() as f32 * fraction) as usize
};
let mut sorted = self.data.clone();
sorted
.as_mut_slice()
.select_nth_unstable_by(index, |a, b| a.partial_cmp(b).unwrap());
sorted[index]
}
pub fn data(&self) -> &[f32] {
&self.data
}
*/
} }
pub fn combine<T>(grids: &mut [Grid], attraction_table: &[T]) pub fn combine<T>(grids: &mut [Grid], attraction_table: &[T])