Stash a maybe working mess

This commit is contained in:
Joshua Coles 2023-03-08 15:08:54 +00:00
parent c707a20a75
commit eceb067add
15 changed files with 60436 additions and 48 deletions

170
Cargo.lock generated
View File

@ -1931,6 +1931,20 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "kd-tree"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f54287107c30b23cf7a32e394c68b9472220a090c897d916dfdf467caef88864"
dependencies = [
"nalgebra 0.31.4",
"num-traits",
"ordered-float",
"paste",
"pdqselect",
"typenum",
]
[[package]] [[package]]
name = "khronos-egl" name = "khronos-egl"
version = "4.1.0" version = "4.1.0"
@ -1942,6 +1956,15 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "kiddo"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ced2e69cfc5f22f86ccc9ce4ecff9f19917f3083a4bac0f402bdab034d73f1"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "kqueue" name = "kqueue"
version = "1.0.7" version = "1.0.7"
@ -2063,6 +2086,15 @@ dependencies = [
"regex-automata", "regex-automata",
] ]
[[package]]
name = "matrixmultiply"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add85d4dd35074e6fedc608f8c8f513a3548619a9024b751949ef0e8e45a4d84"
dependencies = [
"rawpointer",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -2141,6 +2173,60 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "nalgebra"
version = "0.31.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20bd243ab3dbb395b39ee730402d2e5405e448c75133ec49cc977762c4cba3d1"
dependencies = [
"approx",
"matrixmultiply",
"nalgebra-macros 0.1.0",
"num-complex",
"num-rational",
"num-traits",
"simba 0.7.3",
"typenum",
]
[[package]]
name = "nalgebra"
version = "0.32.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d68d47bba83f9e2006d117a9a33af1524e655516b8919caac694427a6fb1e511"
dependencies = [
"approx",
"matrixmultiply",
"nalgebra-macros 0.2.0",
"num-complex",
"num-rational",
"num-traits",
"simba 0.8.0",
"typenum",
]
[[package]]
name = "nalgebra-macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "nalgebra-macros"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d232c68884c0c99810a5a4d333ef7e47689cfd0edc85efc9e54e1e6bf5212766"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "nd_array" name = "nd_array"
version = "0.1.0" version = "0.1.0"
@ -2295,6 +2381,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num-complex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.3.3" version = "0.3.3"
@ -2414,6 +2509,15 @@ version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "ordered-float"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d84eb1409416d254e4a9c8fa56cc24701755025b458f0fcd8e59e1f5f40c23bf"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "6.4.1" version = "6.4.1"
@ -2464,6 +2568,18 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "paste"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79"
[[package]]
name = "pdqselect"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7778906d9321dd56cde1d1ffa69a73e59dcf5fda6d366f62727adf2bd4193aee"
[[package]] [[package]]
name = "peeking_take_while" name = "peeking_take_while"
version = "0.1.2" version = "0.1.2"
@ -2643,6 +2759,12 @@ dependencies = [
"cty", "cty",
] ]
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]] [[package]]
name = "rectangle-pack" name = "rectangle-pack"
version = "0.4.2" version = "0.4.2"
@ -2719,6 +2841,9 @@ dependencies = [
"cbindgen", "cbindgen",
"clap 4.1.8", "clap 4.1.8",
"csv", "csv",
"kd-tree",
"kiddo",
"nalgebra 0.32.2",
"nd_array", "nd_array",
"num-integer", "num-integer",
"rand", "rand",
@ -2761,6 +2886,15 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "safe_arch"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "794821e4ccb0d9f979512f9c1973480123f9bd62a90d74ab0f9426fcf8f4a529"
dependencies = [
"bytemuck",
]
[[package]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.6" version = "1.0.6"
@ -2858,6 +2992,32 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]]
name = "simba"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f3fd720c48c53cace224ae62bef1bbff363a70c68c4802a78b5cc6159618176"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
"wide",
]
[[package]]
name = "simba"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50582927ed6f77e4ac020c057f37a268fc6aebc29225050365aacbb9deeeddc4"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
"wide",
]
[[package]] [[package]]
name = "slab" name = "slab"
version = "0.4.8" version = "0.4.8"
@ -3415,6 +3575,16 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "wide"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b689b6c49d6549434bf944e6b0f39238cf63693cb7a147e9d887507fffa3b223"
dependencies = [
"bytemuck",
"safe_arch",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -39,6 +39,9 @@ rand = { version = "0.8.5", features = ["default", "small_rng"] }
csv = "1.1" csv = "1.1"
serde = { version = "1.0.152", features = ["derive"] } serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93" serde_json = "1.0.93"
kd-tree = { version = "0.5.1", features = ["nalgebra"] }
nalgebra = "0.32.2"
kiddo = "0.2.5"
[build-dependencies] [build-dependencies]
cbindgen = "0.24.3" cbindgen = "0.24.3"

10002
balls.json Normal file

File diff suppressed because it is too large Load Diff

50002
out.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,12 @@
use std::fs::File; use std::fs::File;
use std::path::Path; use std::path::Path;
use bevy::render::render_resource::AsBindGroupShaderType;
use kd_tree::KdTree;
use rand::rngs::SmallRng; use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
use crate::system::model::DLASystem; use crate::system::model::DLASystem;
use crate::system::{Position, Storage}; 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::grid::{Pos2D, VectorStorage};
use crate::system::spaces::hexagonal::HexPosition; use crate::system::spaces::hexagonal::HexPosition;
use crate::system::spaces::nd::{NDPosition, NDVectorStorage}; 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::sticker::{ProbabilisticSticking, SimpleSticking, Sticker};
use crate::system::walker::{LocalRandomWalker, Walker}; 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 { while sys.running {
sys.update(); 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) let mut wtr = csv::Writer::from_path(csv_path)
.expect("Failed to open file"); .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(); .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"); let file = File::create(output_path).expect("Failed to open file");
serde_json::to_writer(file, &sys.history) serde_json::to_writer(file, &sys.history)
@ -60,29 +63,79 @@ 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( DLASystem::new(
SmallRng::seed_from_u64(seed), SmallRng::seed_from_u64(seed),
VectorStorage::new(1600), VectorStorage::new(grid_size),
LocalRandomWalker, LocalRandomWalker,
UniformSpawner, UniformSpawner,
ProbabilisticSticking { stick_probability }, ProbabilisticSticking { stick_probability },
max_particles, max_particles,
) )
} }
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( DLASystem::new(
SmallRng::seed_from_u64(seed), SmallRng::seed_from_u64(seed),
NDVectorStorage::new(1600), NDVectorStorage::new(grid_size),
LocalRandomWalker, LocalRandomWalker,
UniformSpawner, UniformSpawner,
ProbabilisticSticking { stick_probability }, ProbabilisticSticking { stick_probability },
max_particles, max_particles,
) )
} }
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( DLASystem::new(
SmallRng::seed_from_u64(seed), SmallRng::seed_from_u64(seed),
VectorStorage::new(1600), VectorStorage::new(1600),

View File

@ -1,20 +1,20 @@
#![feature(array_zip)] #![feature(array_zip)]
#![feature(generic_const_exprs)] #![feature(generic_const_exprs)]
use std::fs::File;
use std::path::PathBuf; use std::path::PathBuf;
mod system; mod system;
mod example_systems; mod example_systems;
use clap::Parser; 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)] #[derive(clap::ValueEnum, Clone, Debug)]
enum PreConfiguredSystem { enum PreConfiguredSystem {
Grid2, Grid2,
Grid3, Grid3,
Hex Hex,
Balls,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -22,6 +22,9 @@ struct Cli {
#[arg(value_enum, long, default_value_t = PreConfiguredSystem::Grid2)] #[arg(value_enum, long, default_value_t = PreConfiguredSystem::Grid2)]
mode: PreConfiguredSystem, mode: PreConfiguredSystem,
#[arg(long, default_value_t = 1600)]
grid_size: u32,
#[arg(short, long)] #[arg(short, long)]
max_frames: Option<usize>, max_frames: Option<usize>,
@ -40,6 +43,7 @@ fn main() {
PreConfiguredSystem::Grid2 => { PreConfiguredSystem::Grid2 => {
let mut sys = stick_probability( let mut sys = stick_probability(
cli.seed, cli.seed,
cli.grid_size,
cli.max_particles, cli.max_particles,
cli.stick_probability, cli.stick_probability,
); );
@ -51,6 +55,7 @@ fn main() {
PreConfiguredSystem::Grid3 => { PreConfiguredSystem::Grid3 => {
let mut sys = three_dimensional( let mut sys = three_dimensional(
cli.seed, cli.seed,
cli.grid_size,
cli.max_particles, cli.max_particles,
cli.stick_probability, cli.stick_probability,
); );
@ -63,12 +68,24 @@ fn main() {
PreConfiguredSystem::Hex => { PreConfiguredSystem::Hex => {
let mut sys = three_dimensional( let mut sys = three_dimensional(
cli.seed, cli.seed,
cli.grid_size,
cli.max_particles, cli.max_particles,
cli.stick_probability, cli.stick_probability,
); );
drive_system(&mut sys, cli.max_frames); 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); write_json(&mut sys, &cli.output);
} }
} }

View File

@ -24,6 +24,6 @@ pub trait GriddedPosition: Position {
} }
pub trait Storage<P: 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); fn deposit(&mut self, position: &P);
} }

View File

@ -15,7 +15,7 @@ pub struct HistoryLine<P: Position> {
pub position: P, 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, rng: R,
/* /*
@ -51,7 +51,7 @@ pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawn
cluster_radius: f32, 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 { pub fn new(rng: R, space: S, walker: W, spawner: Sp, sticker: St, max_particles: usize) -> Self {
let mut sys = DLASystem { let mut sys = DLASystem {
rng, 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 { if distance > self.kill_circle {
self.active_particle = None; 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) { if self.sticker.should_stick(&mut self.rng, &self.space, &next_position) {
self.deposit(&next_position); self.deposit(&next_position);
self.active_particle = None; 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) { fn spawn_particle(&mut self) {
let position = self.spawner.spawn(&mut self.rng, self.add_circle); 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); self.active_particle = Some(position);
} }
} }

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)
}
}

View File

@ -45,13 +45,19 @@ impl GriddedPosition for Pos2D {
fn linear_index(&self, grid_size: u32) -> usize { fn linear_index(&self, grid_size: u32) -> usize {
let grid_size = grid_size as i32; let grid_size = grid_size as i32;
assert!(self.x <= grid_size && -(grid_size) <= self.x); assert!(self.x < grid_size && -(grid_size) < self.x);
assert!(self.y <= grid_size && -(grid_size) <= self.y); assert!(self.y < grid_size && -(grid_size) < self.y);
let x = (self.x + (grid_size) / 2) as usize; let x = (self.x + (grid_size) / 2) as usize;
let y = (self.y + (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 { 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)]; return self.backing[position.linear_index(self.grid_size)];
} }
@ -94,7 +100,7 @@ impl Storage<Pos2D> for VectorStorage {
} }
impl Storage<HexPosition> 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)]; return self.backing[position.linear_index(self.grid_size)];
} }

View File

@ -1,3 +1,5 @@
pub mod grid; pub mod grid;
pub mod hexagonal; pub mod hexagonal;
pub mod nd; pub mod nd;
pub mod continuous;
pub mod ns;

View File

@ -18,7 +18,7 @@ impl<const DIM: usize> NDVectorStorage<DIM> {
} }
impl<const DIM: usize> Storage<NDPosition<DIM>> for 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)]; return self.backing[position.linear_index(self.grid_size)];
} }

20
src/system/spaces/ns.rs Normal file
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,
}

View File

@ -1,6 +1,7 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use rand::Rng; use rand::Rng;
use crate::system::Position; use crate::system::Position;
use crate::system::spaces::continuous::P3;
use crate::system::spaces::grid::Pos2D; use crate::system::spaces::grid::Pos2D;
use crate::system::spaces::hexagonal::HexPosition; use crate::system::spaces::hexagonal::HexPosition;
use crate::system::spaces::nd::NDPosition; 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. // Does not work due to const generics issue, for now specialise for each dimension needed.
// pub struct NDUniformSpawner<const DIM: usize>; // pub struct NDUniformSpawner<const DIM: usize>;
// //

View File

@ -1,8 +1,8 @@
use rand::Rng; use rand::Rng;
use crate::system::{GriddedPosition, Position, Storage}; use crate::system::{GriddedPosition, Position, Storage};
pub trait Sticker<P: Position> { pub trait Sticker<P: Position, S: Storage<P>> {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool; fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool;
} }
pub struct SimpleSticking; pub struct SimpleSticking;
@ -11,19 +11,19 @@ pub struct ProbabilisticSticking {
pub stick_probability: f32 pub stick_probability: f32
} }
impl<P: GriddedPosition> Sticker<P> for SimpleSticking { impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for SimpleSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool { fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS) (0..P::NEIGHBOURS)
.map(|n| position.neighbour(n)) .map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour)) .any(|neighbour| space.is_occupied(&neighbour))
} }
} }
impl<P: GriddedPosition> Sticker<P> for ProbabilisticSticking { impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for ProbabilisticSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool { fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS) (0..P::NEIGHBOURS)
.map(|n| position.neighbour(n)) .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)
} }
} }