libnoise/core/sources/
value.rs

1use super::functional::{self, constants::PERMUTATION_TABLE_SIZE};
2use crate::core::{
3    generator::{Generator, Generator1D, Generator2D, Generator3D, Generator4D},
4    utils::ptable::{PermutationTable, Seed},
5};
6
7/// A generator which produces n-dimensional value noise.
8///
9/// For details, see the documentation of [`value()`]. Typically, this struct is not meant
10/// to be used directly. Instead, [`value()`] implemented by [`Source`], should be used to
11/// create a value noise generator.
12///
13/// # Direct usage of this struct
14///
15/// Direct instantiation of this struct:
16///
17/// ```
18/// # use libnoise::{Value, Generator};
19/// let generator = Value::new(42);
20/// let value = generator.sample([0.2, 0.5]);
21/// ```
22///
23/// [`value()`]: crate::Source::value
24/// [`Source`]: crate::Source
25#[derive(Clone, Debug)]
26pub struct Value<const D: usize> {
27    permutation_table: PermutationTable,
28}
29
30impl Generator1D for Value<1> {}
31impl Generator2D for Value<2> {}
32impl Generator3D for Value<3> {}
33impl Generator4D for Value<4> {}
34
35impl<const D: usize> Value<D> {
36    /// Create a new value noise generator.
37    #[inline]
38    pub fn new(seed: impl Seed) -> Self {
39        let permutation_table = PermutationTable::new(seed, PERMUTATION_TABLE_SIZE, true);
40        Self { permutation_table }
41    }
42}
43
44impl Generator<1> for Value<1> {
45    #[inline]
46    fn sample(&self, point: [f64; 1]) -> f64 {
47        functional::value::noise1d(&self.permutation_table, point)
48    }
49}
50
51impl Generator<2> for Value<2> {
52    #[inline]
53    fn sample(&self, point: [f64; 2]) -> f64 {
54        functional::value::noise2d(&self.permutation_table, point)
55    }
56}
57
58impl Generator<3> for Value<3> {
59    #[inline]
60    fn sample(&self, point: [f64; 3]) -> f64 {
61        functional::value::noise3d(&self.permutation_table, point)
62    }
63}
64
65impl Generator<4> for Value<4> {
66    #[inline]
67    fn sample(&self, point: [f64; 4]) -> f64 {
68        functional::value::noise4d(&self.permutation_table, point)
69    }
70}