noise/noise_fns/generators/
simplex.rs1use crate::{
2 core::simplex::*,
3 noise_fns::{NoiseFn, Seedable},
4 permutationtable::PermutationTable,
5};
6
7#[derive(Clone, Copy, Debug)]
10pub struct Simplex {
11 seed: u32,
12 hasher: PermutationTable,
13}
14
15impl Simplex {
16 pub const DEFAULT_SEED: u32 = 0;
17
18 pub fn new(seed: u32) -> Self {
19 Simplex {
20 seed,
21 hasher: PermutationTable::new(seed),
22 }
23 }
24}
25
26impl Default for Simplex {
27 fn default() -> Self {
28 Self::new(Self::DEFAULT_SEED)
29 }
30}
31
32impl Seedable for Simplex {
33 fn set_seed(self, seed: u32) -> Self {
35 if self.seed == seed {
37 return self;
38 }
39
40 Simplex {
42 seed,
43 hasher: PermutationTable::new(seed),
44 }
45 }
46
47 fn seed(&self) -> u32 {
48 self.seed
49 }
50}
51
52impl NoiseFn<f64, 2> for Simplex {
54 fn get(&self, point: [f64; 2]) -> f64 {
55 let (result, _) = simplex_2d(point.into(), &self.hasher);
56
57 result
58 }
59}
60
61impl NoiseFn<f64, 3> for Simplex {
63 fn get(&self, point: [f64; 3]) -> f64 {
64 let (result, _) = simplex_3d(point.into(), &self.hasher);
65
66 result
67 }
68}
69
70impl NoiseFn<f64, 4> for Simplex {
72 fn get(&self, point: [f64; 4]) -> f64 {
73 let (result, _) = simplex_4d(point.into(), &self.hasher);
74
75 result
76 }
77}