Stash a maybe working mess

This commit is contained in:
2023-03-08 15:08:54 +00:00
parent c707a20a75
commit eceb067add
15 changed files with 60418 additions and 30 deletions
+61 -8
View File
@@ -1,9 +1,12 @@
use std::fs::File;
use std::path::Path;
use bevy::render::render_resource::AsBindGroupShaderType;
use kd_tree::KdTree;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use crate::system::model::DLASystem;
use crate::system::{Position, Storage};
use crate::system::spaces::continuous::{ContinuousSticker, ContinuousStorage, ContinuousWalker, P3};
use crate::system::spaces::grid::{Pos2D, VectorStorage};
use crate::system::spaces::hexagonal::HexPosition;
use crate::system::spaces::nd::{NDPosition, NDVectorStorage};
@@ -11,7 +14,7 @@ use crate::system::spawner::{Spawner, UniformSpawner};
use crate::system::sticker::{ProbabilisticSticking, SimpleSticking, Sticker};
use crate::system::walker::{LocalRandomWalker, Walker};
pub fn drive_system<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>>(sys: &mut DLASystem<R, P, S, W, Sp, St>, max_frames: Option<usize>) {
pub fn drive_system<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &mut DLASystem<R, P, S, W, Sp, St>, max_frames: Option<usize>) {
while sys.running {
sys.update();
@@ -25,7 +28,7 @@ pub fn drive_system<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawne
}
}
pub fn write_csv<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>>(sys: &DLASystem<R, P, S, W, Sp, St>, csv_path: &Path) {
pub fn write_csv<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &DLASystem<R, P, S, W, Sp, St>, csv_path: &Path) {
let mut wtr = csv::Writer::from_path(csv_path)
.expect("Failed to open file");
@@ -42,7 +45,7 @@ pub fn write_csv<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P
.unwrap();
}
pub fn write_json<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>>(sys: &DLASystem<R, P, S, W, Sp, St>, output_path: &Path) {
pub fn write_json<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &DLASystem<R, P, S, W, Sp, St>, output_path: &Path) {
let file = File::create(output_path).expect("Failed to open file");
serde_json::to_writer(file, &sys.history)
@@ -60,10 +63,10 @@ pub fn initial_config(seed: u64, max_particles: usize) -> DLASystem<SmallRng, Po
)
}
pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Pos2D, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
pub fn stick_probability(seed: u64, grid_size: u32, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Pos2D, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
VectorStorage::new(1600),
VectorStorage::new(grid_size),
LocalRandomWalker,
UniformSpawner,
ProbabilisticSticking { stick_probability },
@@ -71,10 +74,10 @@ pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32
)
}
pub fn three_dimensional(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, NDPosition<3>, NDVectorStorage<3>, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
pub fn three_dimensional(seed: u64, grid_size: u32, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, NDPosition<3>, NDVectorStorage<3>, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
NDVectorStorage::new(1600),
NDVectorStorage::new(grid_size),
LocalRandomWalker,
UniformSpawner,
ProbabilisticSticking { stick_probability },
@@ -82,7 +85,57 @@ pub fn three_dimensional(seed: u64, max_particles: usize, stick_probability: f32
)
}
pub fn hex_grid(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, HexPosition, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
pub fn cont(seed: u64, max_particles: usize) -> DLASystem<SmallRng, P3, ContinuousStorage, ContinuousWalker, UniformSpawner, ContinuousSticker> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
ContinuousStorage { inner: kiddo::KdTree::new() },
ContinuousWalker { walk_step: 1f32 },
UniformSpawner,
ContinuousSticker { range_sq: 4f32 },
max_particles,
)
}
pub fn hex_grid(seed: u64, max_particles: usize, grid_size: u32, stick_probability: f32) -> DLASystem<SmallRng, HexPosition, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
VectorStorage::new(grid_size),
LocalRandomWalker,
UniformSpawner,
ProbabilisticSticking { stick_probability },
max_particles,
)
}
struct LoggerSticker {
inner: SimpleSticking
}
impl<S: Storage<Pos2D>> Sticker<Pos2D, S> for LoggerSticker {
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &Pos2D) -> bool {
if self.inner.should_stick(rng, space, position) {
println!("{:?}", position);
}
false
}
}
struct ReadOnlyVectorStorage {
inner: VectorStorage,
}
impl Storage<Pos2D> for ReadOnlyVectorStorage {
fn is_occupied(&self, position: &Pos2D) -> bool {
self.inner.is_occupied(position)
}
fn deposit(&mut self, position: &Pos2D) {
eprintln!("Write ignore for space at {position:?}");
}
}
pub fn aenguses_thing(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, HexPosition, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
VectorStorage::new(1600),
+20 -3
View File
@@ -1,20 +1,20 @@
#![feature(array_zip)]
#![feature(generic_const_exprs)]
use std::fs::File;
use std::path::PathBuf;
mod system;
mod example_systems;
use clap::Parser;
use crate::example_systems::{drive_system, stick_probability, three_dimensional, write_csv, write_json};
use crate::example_systems::{cont, drive_system, stick_probability, three_dimensional, write_csv, write_json};
#[derive(clap::ValueEnum, Clone, Debug)]
enum PreConfiguredSystem {
Grid2,
Grid3,
Hex
Hex,
Balls,
}
#[derive(Parser, Debug)]
@@ -22,6 +22,9 @@ struct Cli {
#[arg(value_enum, long, default_value_t = PreConfiguredSystem::Grid2)]
mode: PreConfiguredSystem,
#[arg(long, default_value_t = 1600)]
grid_size: u32,
#[arg(short, long)]
max_frames: Option<usize>,
@@ -40,6 +43,7 @@ fn main() {
PreConfiguredSystem::Grid2 => {
let mut sys = stick_probability(
cli.seed,
cli.grid_size,
cli.max_particles,
cli.stick_probability,
);
@@ -51,6 +55,7 @@ fn main() {
PreConfiguredSystem::Grid3 => {
let mut sys = three_dimensional(
cli.seed,
cli.grid_size,
cli.max_particles,
cli.stick_probability,
);
@@ -63,12 +68,24 @@ fn main() {
PreConfiguredSystem::Hex => {
let mut sys = three_dimensional(
cli.seed,
cli.grid_size,
cli.max_particles,
cli.stick_probability,
);
drive_system(&mut sys, cli.max_frames);
write_json(&mut sys, &cli.output);
},
PreConfiguredSystem::Balls => {
let mut sys = cont(
cli.seed,
cli.max_particles,
);
drive_system(&mut sys, cli.max_frames);
write_json(&mut sys, &cli.output);
}
}
+1 -1
View File
@@ -24,6 +24,6 @@ pub trait GriddedPosition: Position {
}
pub trait Storage<P: Position> {
fn at(&self, position: &P) -> bool;
fn is_occupied(&self, position: &P) -> bool;
fn deposit(&mut self, position: &P);
}
+4 -4
View File
@@ -15,7 +15,7 @@ pub struct HistoryLine<P: Position> {
pub position: P,
}
pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>> {
pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>> {
rng: R,
/*
@@ -51,7 +51,7 @@ pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawn
cluster_radius: f32,
}
impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>> DLASystem<R, P, S, W, Sp, St> {
impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>> DLASystem<R, P, S, W, Sp, St> {
pub fn new(rng: R, space: S, walker: W, spawner: Sp, sticker: St, max_particles: usize) -> Self {
let mut sys = DLASystem {
rng,
@@ -103,7 +103,7 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
if distance > self.kill_circle {
self.active_particle = None;
} else if !self.space.at(&next_position) {
} else if !self.space.is_occupied(&next_position) {
if self.sticker.should_stick(&mut self.rng, &self.space, &next_position) {
self.deposit(&next_position);
self.active_particle = None;
@@ -117,7 +117,7 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
fn spawn_particle(&mut self) {
let position = self.spawner.spawn(&mut self.rng, self.add_circle);
if !self.space.at(&position) {
if !self.space.is_occupied(&position) {
self.active_particle = Some(position);
}
}
+106
View File
@@ -0,0 +1,106 @@
use std::f32::consts::PI;
use std::ops::Add;
use bevy::utils::tracing::Instrument;
use kiddo::distance::squared_euclidean;
use kiddo::ErrorKind;
use nalgebra::{DimAdd, Point3};
use rand::Rng;
use serde::Serialize;
use crate::system::sticker::Sticker;
use crate::system::{Position, Storage};
use crate::system::walker::Walker;
#[derive(Serialize, Debug, Clone)]
pub struct P3 {
x: f32,
y: f32,
z: f32,
}
impl P3 {
fn as_arr(&self) -> [f32; 3] {
[self.x, self.y, self.z]
}
pub fn random_with_radius<R: Rng>(rng: &mut R, radius: f32) -> P3 {
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
let phi = rng.gen_range(0f32..1.0) * 2.0 * PI;
let (x, y, z) = (
radius * theta.sin() * phi.cos(),
radius * theta.sin() * phi.sin(),
radius * theta.cos()
);
P3 { x, y, z}
}
}
impl Add for P3 {
type Output = P3;
fn add(self, rhs: Self) -> Self::Output {
P3 {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
const BALL_RADIUS_SQ: f32 = 1.0;
pub struct ContinuousStorage {
pub inner: kiddo::KdTree<f32, (), 3>,
}
impl Position for P3 {
const DIM: usize = 3;
fn zero() -> Self {
P3 { x: 0f32, y: 0f32, z: 0f32 }
}
fn abs(&self) -> f32 {
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).powf(0.5)
}
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
P3 { x: cartesian[0], y: cartesian[1], z: cartesian[3] }
}
}
impl Storage<P3> for ContinuousStorage {
fn is_occupied(&self, position: &P3) -> bool {
let (dist_sq, _) = self.inner.nearest_one(&position.as_arr(), &squared_euclidean).unwrap();
// Is the distance of this point to the next one less than twice the ball radius
dist_sq < 2.0 * BALL_RADIUS_SQ
}
fn deposit(&mut self, position: &P3) {
self.inner.add(&position.as_arr(), ()).expect("Failed to write to space")
}
}
pub struct ContinuousSticker {
/// INVARIANT: THIS SHOULD BE GREATER THAN THE BALL_RADIUS_SQ value
pub range_sq: f32,
}
impl Sticker<P3, ContinuousStorage> for ContinuousSticker {
fn should_stick<R: Rng>(&self, _rng: &mut R, space: &ContinuousStorage, position: &P3) -> bool {
let (a, _) = space.inner.nearest_one(&position.as_arr(), &squared_euclidean).unwrap();
a < self.range_sq
}
}
pub struct ContinuousWalker {
pub walk_step: f32
}
impl Walker<P3> for ContinuousWalker {
fn walk<R: Rng>(&self, rng: &mut R, position: &P3) -> P3 {
position.clone() + P3::random_with_radius(rng, self.walk_step)
}
}
+11 -5
View File
@@ -45,13 +45,19 @@ impl GriddedPosition for Pos2D {
fn linear_index(&self, grid_size: u32) -> usize {
let grid_size = grid_size as i32;
assert!(self.x <= grid_size && -(grid_size) <= self.x);
assert!(self.y <= grid_size && -(grid_size) <= self.y);
assert!(self.x < grid_size && -(grid_size) < self.x);
assert!(self.y < grid_size && -(grid_size) < self.y);
let x = (self.x + (grid_size) / 2) as usize;
let y = (self.y + (grid_size) / 2) as usize;
return grid_size as usize * y + x;
let linear_index = grid_size as usize * y + x;
if linear_index >= (grid_size * grid_size) as usize {
eprintln!("AHHH SOMETHING WENT WRONG {:?} gives {}", self, linear_index);
}
return linear_index;
}
}
@@ -84,7 +90,7 @@ impl VectorStorage {
}
impl Storage<Pos2D> for VectorStorage {
fn at(&self, position: &Pos2D) -> bool {
fn is_occupied(&self, position: &Pos2D) -> bool {
return self.backing[position.linear_index(self.grid_size)];
}
@@ -94,7 +100,7 @@ impl Storage<Pos2D> for VectorStorage {
}
impl Storage<HexPosition> for VectorStorage {
fn at(&self, position: &HexPosition) -> bool {
fn is_occupied(&self, position: &HexPosition) -> bool {
return self.backing[position.linear_index(self.grid_size)];
}
+2
View File
@@ -1,3 +1,5 @@
pub mod grid;
pub mod hexagonal;
pub mod nd;
pub mod continuous;
pub mod ns;
+1 -1
View File
@@ -18,7 +18,7 @@ impl<const DIM: usize> NDVectorStorage<DIM> {
}
impl<const DIM: usize> Storage<NDPosition<DIM>> for NDVectorStorage<DIM> {
fn at(&self, position: &NDPosition<DIM>) -> bool {
fn is_occupied(&self, position: &NDPosition<DIM>) -> bool {
return self.backing[position.linear_index(self.grid_size)];
}
+20
View File
@@ -0,0 +1,20 @@
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Grid2D {
pub x: i32,
pub y: i32,
}
impl Add for Grid2D {
type Output = Grid2D;
fn add(self, rhs: Self) -> Self::Output {
Pos2D { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Grid3D {
pub x: i32,
pub y: i32,
pub z: i32,
}
+7
View File
@@ -1,6 +1,7 @@
use std::f32::consts::PI;
use rand::Rng;
use crate::system::Position;
use crate::system::spaces::continuous::P3;
use crate::system::spaces::grid::Pos2D;
use crate::system::spaces::hexagonal::HexPosition;
use crate::system::spaces::nd::NDPosition;
@@ -44,6 +45,12 @@ impl Spawner<NDPosition<3>> for UniformSpawner {
}
}
impl Spawner<P3> for UniformSpawner {
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> P3 {
P3::random_with_radius(rng, radius)
}
}
// Does not work due to const generics issue, for now specialise for each dimension needed.
// pub struct NDUniformSpawner<const DIM: usize>;
//
+8 -8
View File
@@ -1,8 +1,8 @@
use rand::Rng;
use crate::system::{GriddedPosition, Position, Storage};
pub trait Sticker<P: Position> {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool;
pub trait Sticker<P: Position, S: Storage<P>> {
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool;
}
pub struct SimpleSticking;
@@ -11,19 +11,19 @@ pub struct ProbabilisticSticking {
pub stick_probability: f32
}
impl<P: GriddedPosition> Sticker<P> for SimpleSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for SimpleSticking {
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour))
.any(|neighbour| space.is_occupied(&neighbour))
}
}
impl<P: GriddedPosition> Sticker<P> for ProbabilisticSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for ProbabilisticSticking {
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour) && rng.gen_range(0.0f32..=1.0) < self.stick_probability)
.any(|neighbour| space.is_occupied(&neighbour) && rng.gen_range(0.0f32..=1.0) < self.stick_probability)
}
}