rely on deriving traits instead
This commit is contained in:
parent
f1b9f9ad30
commit
735a820799
30
src/agent.rs
30
src/agent.rs
@ -6,7 +6,7 @@ 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)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
pub y: f32,
|
pub y: f32,
|
||||||
@ -17,11 +17,7 @@ pub struct Agent {
|
|||||||
|
|
||||||
impl Display for Agent {
|
impl Display for Agent {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
write!(f, "{:?}", self)
|
||||||
f,
|
|
||||||
"{{\n(x,y): ({},{})\nangle: {}\npopulation id: {}\ni: {}}}",
|
|
||||||
self.x, self.y, self.angle, self.population_id, self.i,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,25 +89,3 @@ impl Agent {
|
|||||||
self.y = wrap(self.y + step_distance * sin(self.angle), height as f32);
|
self.y = wrap(self.y + step_distance * sin(self.angle), height as f32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Agent {
|
|
||||||
fn clone(&self) -> Agent {
|
|
||||||
Agent {
|
|
||||||
x: self.x,
|
|
||||||
y: self.y,
|
|
||||||
angle: self.angle,
|
|
||||||
population_id: self.population_id,
|
|
||||||
i: self.i,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Agent {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.x == other.x
|
|
||||||
&& self.y == other.y
|
|
||||||
&& self.angle == other.angle
|
|
||||||
&& self.population_id == other.population_id
|
|
||||||
&& self.i == other.i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,22 +1,12 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Buf {
|
pub struct Buf {
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
pub height: usize,
|
pub height: usize,
|
||||||
pub buf: Vec<f32>,
|
pub buf: Vec<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Buf {
|
|
||||||
fn clone(&self) -> Buf {
|
|
||||||
Buf {
|
|
||||||
width: self.width,
|
|
||||||
height: self.height,
|
|
||||||
buf: self.buf.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Buf {
|
impl Buf {
|
||||||
pub fn new(width: usize, height: usize, buf: Vec<f32>) -> Self {
|
pub const fn new(width: usize, height: usize, buf: Vec<f32>) -> Self {
|
||||||
Buf { width, height, buf }
|
Buf { width, height, buf }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
42
src/grid.rs
42
src/grid.rs
@ -5,7 +5,7 @@ use rayon::{iter::ParallelIterator, prelude::*};
|
|||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
// A population configuration.
|
// A population configuration.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct PopulationConfig {
|
pub struct PopulationConfig {
|
||||||
pub sensor_distance: f32,
|
pub sensor_distance: f32,
|
||||||
pub step_distance: f32,
|
pub step_distance: f32,
|
||||||
@ -16,31 +16,9 @@ pub struct PopulationConfig {
|
|||||||
deposition_amount: f32,
|
deposition_amount: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for PopulationConfig {
|
|
||||||
fn clone(&self) -> PopulationConfig {
|
|
||||||
PopulationConfig {
|
|
||||||
sensor_distance: self.sensor_distance,
|
|
||||||
step_distance: self.step_distance,
|
|
||||||
sensor_angle: self.sensor_angle,
|
|
||||||
rotation_angle: self.rotation_angle,
|
|
||||||
decay_factor: self.decay_factor,
|
|
||||||
deposition_amount: self.deposition_amount,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for PopulationConfig {
|
impl Display for PopulationConfig {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
write!(f, "{:?}", self)
|
||||||
f,
|
|
||||||
"{{\nSensor Distance: {},\nStep Distance: {},\nSensor Angle: {},\nRotation Angle: {},\nDecay Factor: {},\nDeposition Amount: {},\n}}",
|
|
||||||
self.sensor_distance,
|
|
||||||
self.step_distance,
|
|
||||||
self.sensor_angle,
|
|
||||||
self.rotation_angle,
|
|
||||||
self.decay_factor,
|
|
||||||
self.deposition_amount
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +55,7 @@ impl PopulationConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Grid {
|
pub struct Grid {
|
||||||
pub config: PopulationConfig,
|
pub config: PopulationConfig,
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
@ -92,20 +70,6 @@ pub struct Grid {
|
|||||||
pub agents: Vec<Agent>,
|
pub agents: Vec<Agent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Grid {
|
|
||||||
fn clone(&self) -> Grid {
|
|
||||||
Grid {
|
|
||||||
config: self.config.clone(),
|
|
||||||
width: self.width,
|
|
||||||
height: self.height,
|
|
||||||
data: self.data.clone(),
|
|
||||||
buf: self.buf.clone(),
|
|
||||||
blur: self.blur.clone(),
|
|
||||||
agents: self.agents.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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>(
|
||||||
|
|||||||
@ -2,23 +2,14 @@ use crate::{grid::Grid, palette::Palette};
|
|||||||
|
|
||||||
use itertools::multizip;
|
use itertools::multizip;
|
||||||
|
|
||||||
// Stores data that is located in grids that is used for image generation
|
/// Stores data that is located in grids that is used for image generation
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct ThinGridData {
|
pub struct ThinGridData {
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
pub height: usize,
|
pub height: usize,
|
||||||
pub data: Vec<f32>,
|
pub data: Vec<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for ThinGridData {
|
|
||||||
fn clone(&self) -> ThinGridData {
|
|
||||||
ThinGridData {
|
|
||||||
width: self.width,
|
|
||||||
height: self.height,
|
|
||||||
data: self.data.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
@ -69,23 +60,14 @@ impl ThinGridData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Class for storing data that will be used to create images
|
/// Class for storing data that will be used to create images
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct ImgData {
|
pub struct ImgData {
|
||||||
pub grids: Vec<ThinGridData>,
|
pub grids: Vec<ThinGridData>,
|
||||||
pub palette: Palette,
|
pub palette: Palette,
|
||||||
pub iteration: i32,
|
pub iteration: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for ImgData {
|
|
||||||
fn clone(&self) -> ImgData {
|
|
||||||
ImgData {
|
|
||||||
grids: self.grids.clone(),
|
|
||||||
palette: self.palette,
|
|
||||||
iteration: self.iteration,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ImgData {
|
impl ImgData {
|
||||||
pub fn new(in_grids: Vec<ThinGridData>, in_palette: Palette, in_iteration: i32) -> Self {
|
pub fn new(in_grids: Vec<ThinGridData>, in_palette: Palette, in_iteration: i32) -> Self {
|
||||||
ImgData {
|
ImgData {
|
||||||
@ -105,7 +87,6 @@ impl ImgData {
|
|||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn save_to_image(&self) {
|
pub fn save_to_image(&self) {
|
||||||
let (width, height) = (self.grids[0].width, self.grids[0].height);
|
let (width, height) = (self.grids[0].width, self.grids[0].height);
|
||||||
let mut img = image::RgbImage::new(width as u32, height as u32);
|
let mut img = image::RgbImage::new(width as u32, height as u32);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user