Skip to content

Commit

Permalink
Update Perlin to work purely off vectors
Browse files Browse the repository at this point in the history
This had a minimal impact on the benchmarks. It's superior to the old usage of vectors because it stays as vectors the entire time, instead of being passed in as arrays and converting back and forth repeatedly.
  • Loading branch information
Cifram authored and Razaekel committed Apr 10, 2023
1 parent 9036912 commit 88d0665
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
6 changes: 3 additions & 3 deletions examples/perlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod utils;
fn main() {
let hasher = PermutationTable::new(0);
utils::write_example_to_file(
&PlaneMapBuilder::new_fn(|point| perlin_2d(point, &hasher))
&PlaneMapBuilder::new_fn(|point| perlin_2d(point.into(), &hasher))
.set_size(1024, 1024)
.set_x_bounds(-5.0, 5.0)
.set_y_bounds(-5.0, 5.0)
Expand All @@ -22,7 +22,7 @@ fn main() {
);

utils::write_example_to_file(
&PlaneMapBuilder::new_fn(|point| perlin_3d(point, &hasher))
&PlaneMapBuilder::new_fn(|point| perlin_3d(point.into(), &hasher))
.set_size(1024, 1024)
.set_x_bounds(-5.0, 5.0)
.set_y_bounds(-5.0, 5.0)
Expand All @@ -31,7 +31,7 @@ fn main() {
);

utils::write_example_to_file(
&PlaneMapBuilder::new_fn(|point| perlin_4d(point, &hasher))
&PlaneMapBuilder::new_fn(|point| perlin_4d(point.into(), &hasher))
.set_size(1024, 1024)
.set_x_bounds(-5.0, 5.0)
.set_y_bounds(-5.0, 5.0)
Expand Down
11 changes: 3 additions & 8 deletions src/core/perlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
}

#[inline(always)]
pub fn perlin_2d<NH>(point: [f64; 2], hasher: &NH) -> f64
pub fn perlin_2d<NH>(point: Vector2<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
Expand All @@ -61,8 +61,6 @@ where
// 1/(sqrt(N)/2), N=2 -> 2/sqrt(2)
const SCALE_FACTOR: f64 = 2.0 / f64::consts::SQRT_2;

let point = Vector2::from(point);

let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();

Expand Down Expand Up @@ -101,7 +99,7 @@ where
}

#[inline(always)]
pub fn perlin_3d<NH>(point: [f64; 3], hasher: &NH) -> f64
pub fn perlin_3d<NH>(point: Vector3<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
Expand All @@ -115,7 +113,6 @@ where
// 2/sqrt(3) = 1.1547005383792515290182975610039149112952035025402537520372046529
const SCALE_FACTOR: f64 = 1.154_700_538_379_251_5;

let point = Vector3::from(point);
let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();

Expand Down Expand Up @@ -174,7 +171,7 @@ where
}

#[inline(always)]
pub fn perlin_4d<NH>(point: [f64; 4], hasher: &NH) -> f64
pub fn perlin_4d<NH>(point: Vector4<f64>, hasher: &NH) -> f64
where
NH: NoiseHasher + ?Sized,
{
Expand All @@ -183,8 +180,6 @@ where
// range of (-1, 1).
const SCALE_FACTOR: f64 = 1.0; // 1/(sqrt(N)/2), N=4 -> 2/sqrt(4) -> 2/2 -> 1

let point = Vector4::from(point);

let corner = point.floor_to_isize();
let distance = point - corner.numcast().unwrap();

Expand Down

0 comments on commit 88d0665

Please sign in to comment.