proper doc comments
This commit is contained in:
parent
50e85dec90
commit
eee266979c
@ -4,7 +4,7 @@ use rand::{seq::SliceRandom, Rng};
|
|||||||
use std::f32::consts::TAU;
|
use std::f32::consts::TAU;
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
// A single Physarum agent. The x and y positions are continuous, hence we use floating point numbers instead of integers.
|
/// A single Physarum agent. The x and y positions are continuous, hence we use floating point numbers instead of integers.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
@ -21,7 +21,7 @@ impl Display for Agent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Agent {
|
impl Agent {
|
||||||
// Construct a new agent with random parameters.
|
/// Construct a new agent with random parameters.
|
||||||
pub fn new<R: Rng + ?Sized>(
|
pub fn new<R: Rng + ?Sized>(
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
@ -39,7 +39,7 @@ impl Agent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tick an agent
|
/// Tick an agent
|
||||||
pub fn tick(
|
pub fn tick(
|
||||||
&mut self,
|
&mut self,
|
||||||
buf: &Buf,
|
buf: &Buf,
|
||||||
|
|||||||
10
src/blur.rs
10
src/blur.rs
@ -13,7 +13,7 @@ impl Blur {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blur an image with 2 box filter passes. The result will be written to the src slice, while the buf slice is used as a scratch space.
|
/// Blur an image with 2 box filter passes. The result will be written to the src slice, while the buf slice is used as a scratch space.
|
||||||
pub fn run(
|
pub fn run(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: &mut [f32],
|
src: &mut [f32],
|
||||||
@ -28,7 +28,7 @@ impl Blur {
|
|||||||
self.box_blur(src, buf, width, height, boxes[1], decay);
|
self.box_blur(src, buf, width, height, boxes[1], decay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass.
|
/// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass.
|
||||||
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] {
|
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] {
|
||||||
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt();
|
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt();
|
||||||
let mut w = w_ideal as usize;
|
let mut w = w_ideal as usize;
|
||||||
@ -44,7 +44,7 @@ impl Blur {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space.
|
/// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space.
|
||||||
fn box_blur(
|
fn box_blur(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: &mut [f32],
|
src: &mut [f32],
|
||||||
@ -58,7 +58,7 @@ impl Blur {
|
|||||||
self.box_blur_v(buf, src, width, height, radius, decay);
|
self.box_blur_v(buf, src, width, height, radius, decay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform one pass of the 1D box filter of the given radius along x axis.
|
/// Perform one pass of the 1D box filter of the given radius along x axis.
|
||||||
fn box_blur_h(&mut self, src: &[f32], dst: &mut [f32], width: usize, radius: usize) {
|
fn box_blur_h(&mut self, src: &[f32], dst: &mut [f32], width: usize, radius: usize) {
|
||||||
let weight = 1.0 / (2 * radius + 1) as f32;
|
let weight = 1.0 / (2 * radius + 1) as f32;
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ impl Blur {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform one pass of the 1D box filter of the given radius along y axis. Applies the decay factor to the destination buffer.
|
/// Perform one pass of the 1D box filter of the given radius along y axis. Applies the decay factor to the destination buffer.
|
||||||
fn box_blur_v(
|
fn box_blur_v(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: &[f32],
|
src: &[f32],
|
||||||
|
|||||||
@ -14,12 +14,12 @@ impl Buf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate x and y and return a corresponding index into the data slice.
|
/// Truncate x and y and return a corresponding index into the data slice.
|
||||||
const fn index(&self, x: f32, y: f32) -> usize {
|
const fn index(&self, x: f32, y: f32) -> usize {
|
||||||
crate::util::index(self.width, self.height, x, y)
|
crate::util::index(self.width, self.height, x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the buffer value at a given position. The implementation effectively treats data as periodic, hence any finite position will produce a value.
|
/// 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 {
|
pub fn get_buf(&self, x: f32, y: f32) -> f32 {
|
||||||
self.buf[self.index(x, y)]
|
self.buf[self.index(x, y)]
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/grid.rs
12
src/grid.rs
@ -4,7 +4,7 @@ use rand::{distributions::Uniform, Rng};
|
|||||||
use rayon::{iter::ParallelIterator, prelude::*};
|
use rayon::{iter::ParallelIterator, prelude::*};
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
// A population configuration.
|
/// A population configuration.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct PopulationConfig {
|
pub struct PopulationConfig {
|
||||||
pub sensor_distance: f32,
|
pub sensor_distance: f32,
|
||||||
@ -23,7 +23,7 @@ impl Display for PopulationConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PopulationConfig {
|
impl PopulationConfig {
|
||||||
// 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(0.0..=64.0),
|
sensor_distance: rng.gen_range(0.0..=64.0),
|
||||||
@ -53,7 +53,7 @@ pub struct Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Grid {
|
impl Grid {
|
||||||
// Create a new grid filled with random floats in the [0.0..1.0) range.
|
/// Create a new grid filled with random floats in the [0.0..1.0) range.
|
||||||
pub fn new<R: Rng + ?Sized>(
|
pub fn new<R: Rng + ?Sized>(
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
@ -74,18 +74,18 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate x and y and return a corresponding index into the data slice.
|
/// Truncate x and y and return a corresponding index into the data slice.
|
||||||
const fn index(&self, x: f32, y: f32) -> usize {
|
const fn index(&self, x: f32, y: f32) -> usize {
|
||||||
crate::util::index(self.width, self.height, x, y)
|
crate::util::index(self.width, self.height, 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);
|
||||||
self.data[idx] += self.config.deposition_amount;
|
self.data[idx] += self.config.deposition_amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diffuse grid data and apply a decay multiplier.
|
/// Diffuse grid data and apply a decay multiplier.
|
||||||
pub fn diffuse(&mut self, radius: usize) {
|
pub fn diffuse(&mut self, radius: usize) {
|
||||||
self.blur.run(
|
self.blur.run(
|
||||||
&mut self.data,
|
&mut self.data,
|
||||||
|
|||||||
@ -12,7 +12,7 @@ pub struct ThinGridData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ThinGridData {
|
impl ThinGridData {
|
||||||
// Convert Grid to ThinGridData
|
/// Convert Grid to ThinGridData
|
||||||
pub fn new_from_grid(in_grid: &Grid) -> Self {
|
pub fn new_from_grid(in_grid: &Grid) -> Self {
|
||||||
ThinGridData {
|
ThinGridData {
|
||||||
width: in_grid.width,
|
width: in_grid.width,
|
||||||
@ -22,13 +22,10 @@ impl ThinGridData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_grid_vec(in_grids: &[Grid]) -> Vec<Self> {
|
pub fn new_from_grid_vec(in_grids: &[Grid]) -> Vec<Self> {
|
||||||
in_grids
|
in_grids.iter().map(Self::new_from_grid).collect()
|
||||||
.iter()
|
|
||||||
.map(Self::new_from_grid)
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// from grid.rs (needed in image gen)
|
/// from grid.rs (needed in image gen)
|
||||||
pub fn quantile(&self, fraction: f32) -> f32 {
|
pub fn quantile(&self, fraction: f32) -> f32 {
|
||||||
let index = if (fraction - 1.0_f32).abs() < f32::EPSILON {
|
let index = if (fraction - 1.0_f32).abs() < f32::EPSILON {
|
||||||
self.data.len() - 1
|
self.data.len() - 1
|
||||||
|
|||||||
15
src/model.rs
15
src/model.rs
@ -10,21 +10,21 @@ use rand_distr::{Distribution, Normal};
|
|||||||
use rayon::{iter::ParallelIterator, prelude::*};
|
use rayon::{iter::ParallelIterator, prelude::*};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
// Top-level simulation class.
|
/// Top-level simulation class.
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
// per-population grid (one for each population)
|
/// per-population grid (one for each population)
|
||||||
population_grids: Vec<Grid>,
|
population_grids: Vec<Grid>,
|
||||||
|
|
||||||
// Attraction table governs interaction across populations
|
/// Attraction table governs interaction across populations
|
||||||
attraction_table: Vec<Vec<f32>>,
|
attraction_table: Vec<Vec<f32>>,
|
||||||
|
|
||||||
// Global grid diffusivity.
|
/// Global grid diffusivity.
|
||||||
diffusivity: usize,
|
diffusivity: usize,
|
||||||
|
|
||||||
// Current model iteration.
|
/// Current model iteration.
|
||||||
iteration: usize,
|
iteration: usize,
|
||||||
|
|
||||||
// Color palette
|
/// Color palette
|
||||||
palette: Palette,
|
palette: Palette,
|
||||||
|
|
||||||
time_per_agent_list: Vec<f64>,
|
time_per_agent_list: Vec<f64>,
|
||||||
@ -44,7 +44,7 @@ impl Model {
|
|||||||
println!("Attraction table: {:#?}", self.attraction_table);
|
println!("Attraction table: {:#?}", self.attraction_table);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct a new model with random initial conditions and random configuration.
|
/// Construct a new model with random initial conditions and random configuration.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
@ -132,7 +132,6 @@ impl Model {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accessors for rendering
|
|
||||||
pub fn population_grids(&self) -> &[Grid] {
|
pub fn population_grids(&self) -> &[Grid] {
|
||||||
&self.population_grids
|
&self.population_grids
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ pub fn wrap(x: f32, max: f32) -> f32 {
|
|||||||
x - max * ((x > max) as i32 as f32 - (x < 0.0_f32) as i32 as f32)
|
x - max * ((x > max) as i32 as f32 - (x < 0.0_f32) as i32 as f32)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate x and y and return a corresponding index into the data slice.
|
/// Truncate x and y and return a corresponding index into the data slice.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
|
pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
|
||||||
// x/y can come in negative, hence we shift them by width/height.
|
// x/y can come in negative, hence we shift them by width/height.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user