Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast poisson sampling #178

Merged
merged 8 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ num-integer = { version = "0.1.46", optional = true }
dmi = { version = "0.3.5", optional = true }
tracy_full = { version = "1.8.0", optional = true }
ammonia = { version = "4.0.0", optional = true }
fast_poisson = { version = "0.5.2", optional = true }

[features]
default = [
"acreplace",
"batchnoise",
"fast_poisson_sample",
"cellularnoise",
"dmi",
"file",
Expand All @@ -89,6 +91,7 @@ default = [
all = [
"acreplace",
"batchnoise",
"fast_poisson_sample",
"cellularnoise",
"dmi",
"file",
Expand Down Expand Up @@ -117,6 +120,7 @@ acreplace = ["aho-corasick"]
batchnoise = ["dbpnoise"]
cellularnoise = ["rand", "rayon"]
dmi = ["png", "image", "dep:dmi"]
fast_poisson_sample = ["fast_poisson"]
file = []
git = ["gix", "chrono"]
http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
Expand Down
1 change: 1 addition & 0 deletions dmsrc/noise.dm
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define rustg_noise_get_at_coordinates(seed, x, y) RUSTG_CALL(RUST_G, "noise_get_at_coordinates")(seed, x, y)
#define rustg_noise_poisson_sample(seed, x, y, r) RUSTG_CALL(RUST_G, "generate_poisson_sample")(seed, x, y, r)
ZeWaka marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions src/noise_gen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use fast_poisson::Poisson2D;
use noise::{NoiseFn, Perlin};
use std::{
cell::RefCell,
collections::hash_map::{Entry, HashMap},
fmt::Write,
};

use crate::error::Result;
Expand Down Expand Up @@ -36,3 +38,45 @@ fn get_at_coordinates(seed_as_str: &str, x_as_str: &str, y_as_str: &str) -> Resu
Ok(clamped.to_string())
})
}

byond_fn!(fn generate_poisson_sample(seed, x, y, r) {
get_poisson_sample(seed, x, y, r).ok()
});

fn get_poisson_sample(
seed_as_str: &str,
x_as_str: &str,
y_as_str: &str,
radius_as_str: &str,
) -> Result<String> {
let x = x_as_str.parse::<f64>()?;
let y = y_as_str.parse::<f64>()?;
let r = radius_as_str.parse::<f64>()?;
let seed = seed_as_str.parse::<u64>()?;

let points = Poisson2D::new()
.with_dimensions([x, y], r)
.with_seed(seed)
.iter();
let mut pointmap = vec![vec![0usize; y as usize]; x as usize];
let mut output = String::new();

// we're just gonna truncate these to the nearest point
for p in points {
let point_x = p[0] as usize;
let point_y = p[1] as usize;
pointmap[point_x][point_y] = 1;
}

for row in pointmap {
for cell in row {
if cell > 0 {
let _ = write!(output, "1");
} else {
let _ = write!(output, "0");
}
}
}

Ok(output)
}