Simplex

Struct Simplex 

Source
pub struct Simplex {
    pub seed: Vec<usize>,
    /* private fields */
}
Expand description

Hold the proper permutation tables and methods for generating 2D and 3D noise.

It is intended for you to get a Simplex through Simplex::new() since that creates the necessary permutation tables needed to generate noise.

Noise generated by Simplex is random every time.

  • seed - Seed that will be used by Simplex to generate it’s permutation table

Fields§

§seed: Vec<usize>

Implementations§

Source§

impl Simplex

Source

pub fn new() -> Simplex

Return a new Simplex with a new random permutation table

Necessary to generate the proper permutation tables (GRAD3) used by noise_2d() and noise_3d.

§Examples
use fuss::Simplex;
 
let sn = Simplex::new();
Source

pub fn from_seed(seed: Vec<usize>) -> Simplex

Seed the random number generator with a specific seed

A seed is just a vector of usizes that will be passed into StdRng::from_seed as a slice.

§Examples
use fuss::Simplex;
 
let mut sn = Simplex::from_seed(vec![1, 2, 3]);
let mut other_sn = Simplex::from_seed(vec![1, 2, 3]);
 
assert_eq!(other_sn.noise_2d(1.0, 14.2), sn.noise_2d(1.0, 14.2));
assert_eq!(other_sn.noise_3d(1.0, 14.2, -5.4), sn.noise_3d(1.0, 14.2, -5.4));
 
sn = Simplex::from_seed(vec![4, 5, 6]);
let mut other_sn = Simplex::from_seed(vec![1, 2, 3]);
assert!(other_sn.noise_2d(1.0, 14.2) != sn.noise_2d(1.0, 14.2));
assert!(other_sn.noise_3d(1.0, 14.2, -5.4) != sn.noise_3d(1.0, 14.2, -5.4));
Source

pub fn sum_octave_2d( &self, num_iterations: isize, xin: f32, yin: f32, persistence: f32, scale: f32, ) -> f32

Smooth the output from noise_2d based on fractal Brownian motion.

Returns an f32 in [-1, 1]

§Examples
use fuss::Simplex;
 
let sn = Simplex::new();
 
let mut luminance = Vec::<Vec<f32>>::new();
for x in 0..100 {
  luminance.push(Vec::<f32>::new());
  for y in 0..100 {
    luminance[x as usize].push(sn.sum_octave_2d(16, x as f32, y as f32, 0.5, 0.008));
  }
}
Source

pub fn sum_octave_3d( &self, num_iterations: isize, xin: f32, yin: f32, zin: f32, persistence: f32, scale: f32, ) -> f32

Smooth the output from noise_3d based on fractal Brownian motion.

Returns an f32 in [-1, 1]

§Examples
use fuss::Simplex;
 
let sn = Simplex::new();
 
let mut luminance = Vec::<Vec<Vec<f32>>>::new();
for x in 0..10 {
  luminance.push(Vec::<Vec<f32>>::new());
  for y in 0..10 {
    luminance[x as usize].push(Vec::<f32>::new());
    for z in 0..10 {
      luminance[x as usize][y as usize].push(sn.sum_octave_3d(16, x as f32, y as f32, z as f32, 0.5, 0.008));
    }
  }
}
Source

pub fn noise_2d(&self, xin: f32, yin: f32) -> f32

Generate 2D simplex noise for a specific point

Returns an f32 in [-1, 1].

§Examples
use fuss::Simplex;
 
let sn = Simplex::from_seed(vec![5, 3, 2, 1, 1]);
println!("{}", sn.noise_2d(50.1912, 30.50102));
 
// Simplex will return the same thing for the same points
assert_eq!(sn.noise_2d(1.5, -0.5), sn.noise_2d(1.5, -0.5));
 
let other_sn = Simplex::from_seed(vec![0, 1, 2, 3, 4, 5]);
 
// However each `Simplex` has it's own set of permutations, therefore
// each one is different. If you want consistency, try the `from_seed()` method.
assert!(sn.noise_2d(1.5, -0.5) != other_sn.noise_2d(1.5, -0.5));
Source

pub fn noise_3d(&self, xin: f32, yin: f32, zin: f32) -> f32

Generate 3D simplex noise for a specific point

Returns an f32 in [-1, 1].

§Examples
use fuss::Simplex;
 
let sn = Simplex::new();
println!("{}", sn.noise_2d(50.1912, 30.50102));
 
// Simplex will return the same thing for the same points
assert_eq!(sn.noise_3d(1.5, -0.5, 2.1), sn.noise_3d(1.5, -0.5, 2.1));
 
let other_sn = Simplex::new();
 
// However each `Simplex` has it's own set of permutations, therefore
// each one is different. If you want consistency, try the `from_seed()` method.
assert!(sn.noise_3d(1.5, -0.5, 2.1) != other_sn.noise_3d(1.5, -0.5, 2.1));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.