Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Commit

Permalink
Add shim for Rust noise library, exposing 2d Perlin noise functions (t…
Browse files Browse the repository at this point in the history
…gstation#28)

Provides an interface to get 2d Perlin noise generated by the Rust [noise] library.

rustg_noise_get_at_coordinates(seed, x, y) takes an integer seed and floating-point coordinates, and returns a floating-point value in the range [0,1].

[noise]: https://docs.rs/noise/0.6.0/noise/
  • Loading branch information
4dplanner authored May 6, 2020
1 parent 4120da8 commit 552dff4
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 4 deletions.
131 changes: 131 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hex = { version = "0.3", optional = true }
percent-encoding = { version = "1.0", optional = true }
png = { version = "0.11.0", optional = true }
git2 = { version = "0.7.1", optional = true, default-features = false }
noise = { version = "0.6.0", optional = true}
reqwest = { version = "0.9", optional = true, default-features = false, features = ["rustls-tls"] }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ cargo build --release --features dmi,file,log,url,http
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.
* **log** (default): Faster log output.
* url: Faster replacements for `url_encode` and `url_decode`.
* 2d-noise: 2d perlin noise.
* http: HTTP client to allow `GET`, `POST`, `PUT`, `PATCH`, `DELETE` and `HEAD`.

## Installing
Expand Down
9 changes: 8 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ fn main() {
"#).unwrap();
}

// module: noise
if enabled!("NOISE") {
write!(f, r#"
#define rustg_noise_get_at_coordinates(seed, x, y) call(RUST_G, "noise_get_at_coordinates")(seed, x, y)
"#).unwrap()
}

// module: file
if enabled!("FILE") {
write!(f, r#"
Expand Down Expand Up @@ -102,5 +109,5 @@ fn main() {
#define rustg_http_request_async(method, url, body, headers) call(RUST_G, "http_request_async")(method, url, body, headers)
#define rustg_http_check_request(req_id) call(RUST_G, "http_check_request")(req_id)
"#).unwrap();
}
}
}
11 changes: 8 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use std::result;
use std::str::Utf8Error;
use std::num::ParseIntError;
use std::num::{ParseIntError, ParseFloatError};

#[cfg(feature="png")]
use png::{DecodingError, EncodingError};
Expand All @@ -26,9 +26,10 @@ pub enum Error {
#[cfg(feature="png")]
#[fail(display = "{}", _0)]
ImageEncoding(#[cause] EncodingError),
#[cfg(feature="png")]
#[fail(display = "{}", _0)]
ParseIntError(#[cause] ParseIntError),
#[fail(display = "{}", _0)]
ParseFloatError(#[cause] ParseFloatError),
#[cfg(feature="png")]
#[fail(display = "Invalid png data.")]
InvalidPngDataError,
Expand Down Expand Up @@ -66,13 +67,17 @@ impl From<EncodingError> for Error {
}
}

#[cfg(feature="png")]
impl From<ParseIntError> for Error {
fn from(error: ParseIntError) -> Error {
Error::ParseIntError(error)
}
}

impl From<ParseFloatError> for Error {
fn from(error: ParseFloatError) -> Error {
Error::ParseFloatError(error)
}
}
#[cfg(feature="http")]
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Error {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extern crate hex;
extern crate percent_encoding;
#[cfg(feature="png")]
extern crate png;
#[cfg(feature="noise")]
extern crate noise;
#[cfg(feature="http")]
extern crate reqwest;
#[cfg(feature="http")]
Expand Down Expand Up @@ -42,5 +44,7 @@ pub mod hash;
pub mod log;
#[cfg(feature="url")]
pub mod url;
#[cfg(feature="2d-noise")]
pub mod noise_gen;
#[cfg(feature="http")]
pub mod http;
36 changes: 36 additions & 0 deletions src/noise_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use noise::{NoiseFn, Perlin, Seedable};
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::Entry;

use crate::error::Result;

thread_local! {
static GENERATORS: RefCell<HashMap<String, Perlin>> = RefCell::new(HashMap::new());
}

byond_fn! { noise_get_at_coordinates(seed, x, y) {
get_at_coordinates(seed, x, y).ok()
} }

//note that this will be 0 at integer x & y, scaling is left up to the caller
fn get_at_coordinates(seed_as_str: &str, x_as_str: &str, y_as_str: &str) -> Result<String> {
let x = x_as_str.parse::<f64>()?;
let y = y_as_str.parse::<f64>()?;
GENERATORS.with(|cell| {
let mut generators = cell.borrow_mut();
let mut entry = generators.entry(seed_as_str.to_string());
let generator = match entry {
Entry::Occupied(ref mut occ) => occ.get_mut(),
Entry::Vacant(vac) => {
let seed = seed_as_str.parse::<u32>()?;
let perlin = Perlin::new().set_seed(seed);
vac.insert(perlin)
}
};
//perlin noise produces a result in [-sqrt(0.5), sqrt(0.5)] which we scale to [0, 1] for simplicity
let unscaled = generator.get([x, y]);
let scaled_from_0_to_1 = (unscaled * 2.0_f64.sqrt() + 1.0) / 2.0;
Ok(scaled_from_0_to_1.to_string())
})
}

0 comments on commit 552dff4

Please sign in to comment.