Skip to content

Commit

Permalink
Fast poisson sampling (#178)
Browse files Browse the repository at this point in the history
Co-authored-by: ZeWaka <[email protected]>
  • Loading branch information
FluffyGhoster and ZeWaka authored Jul 5, 2024
1 parent fc3fd8e commit 22a6df5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 1 deletion.
File renamed without changes.
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.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
lazy_static = { version = "1.4", optional = true }
once_cell = { version = "1.19", optional = true }
mysql = { git = "https://github.com/ZeWaka/rust-mysql-simple.git", tag = "v25.0.1", default_features = false, optional = true }
mysql = { git = "https://github.com/ZeWaka/rust-mysql-simple.git", tag = "v25.0.1", default-features = false, optional = true }
dashmap = { version = "5.5", optional = true, features = ["rayon", "serde"] }
zip = { version = "2.1.1", optional = true }
rand = { version = "0.8", optional = true }
Expand All @@ -65,6 +65,7 @@ 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 = ["single_precision"]} # Higher versions have problems with x86 due to 'kiddo'.

[features]
default = [
Expand Down Expand Up @@ -106,6 +107,7 @@ all = [
"hash",
"iconforge",
"pathfinder",
"poissonnoise",
"redis_pubsub",
"redis_reliablequeue",
"unzip",
Expand Down Expand Up @@ -155,6 +157,7 @@ iconforge = [
"twox-hash",
]
pathfinder = ["num-integer", "pathfinding", "serde", "serde_json"]
poissonnoise = ["fast_poisson"]
redis_pubsub = ["flume", "redis", "serde", "serde_json"]
redis_reliablequeue = ["flume", "redis", "serde", "serde_json"]
unzip = ["zip", "jobs"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Additional features are:
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.
* iconforge: A much faster replacement for the spritesheet generation system used by [/tg/station].
* pathfinder: An a* pathfinder used for finding the shortest path in a static node map. Not to be used for a non-static map.
* poissonnoise: A way to generate a 2D poisson disk distribution ('blue noise'), which is relatively uniform.
* redis_pubsub: Library for sending and receiving messages through Redis.
* redis_reliablequeue: Library for using a reliable queue pattern through Redis.
* unzip: Function to download a .zip from a URL and unzip it to a directory.
Expand Down
14 changes: 14 additions & 0 deletions dmsrc/noise.dm
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
#define rustg_noise_get_at_coordinates(seed, x, y) RUSTG_CALL(RUST_G, "noise_get_at_coordinates")(seed, x, y)

/**
* Generates a 2D poisson disk distribution ('blue noise'), which is relatively uniform.
*
* params:
* `seed`: str
* `width`: int, width of the noisemap (see world.maxx)
* `length`: int, height of the noisemap (see world.maxy)
* `radius`: int, distance between points on the noisemap
*
* returns:
* a width*length length string of 1s and 0s representing a 2D poisson sample collapsed into a 1D string
*/
#define rustg_noise_poisson_map(seed, width, length, radius) RUSTG_CALL(RUST_G, "noise_poisson_map")(seed, width, length, radius)
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub mod log;
pub mod noise_gen;
#[cfg(feature = "pathfinder")]
pub mod pathfinder;
#[cfg(feature = "poissonnoise")]
pub mod poissonnoise;
#[cfg(feature = "redis_pubsub")]
pub mod redis_pubsub;
#[cfg(feature = "redis_reliablequeue")]
Expand Down
41 changes: 41 additions & 0 deletions src/poissonnoise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use fast_poisson::Poisson2D;
use std::fmt::Write;

use crate::error::Result;

byond_fn!(fn noise_poisson_map(seed, width, length, radius) {
get_poisson_map(seed, width, length, radius).ok()
});

fn get_poisson_map(
seed_as_str: &str,
width_as_str: &str,
length_as_str: &str,
radius_as_str: &str,
) -> Result<String> {
let width = width_as_str.parse::<f32>()?;
let length = length_as_str.parse::<f32>()?;
let radius = radius_as_str.parse::<f32>()?;
let seed = seed_as_str.parse::<u64>()?;

let points: Vec<[f32; 2]> = Poisson2D::new()
.with_dimensions([width, length], radius)
.with_seed(seed)
.to_vec();

let mut output = String::new();
for y in 0..length as usize {
for x in 0..width as usize {
if points
.iter()
.any(|&point| point[0] as usize == x && point[1] as usize == y)
{
let _ = write!(output, "1");
} else {
let _ = write!(output, "0");
}
}
}

Ok(output)
}

0 comments on commit 22a6df5

Please sign in to comment.