1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::noise_fns::{Fbm, MultiFractal, NoiseFn, Seedable};

/// Noise function that randomly displaces the input value before returning the
/// output value from the source function.
///
/// _ is the pseudo-random displacement of the input value. The
/// get() method randomly displaces the coordinates of the input value before
/// retrieving the output value from the source function. To control the
/// turbulence, an application can modify its frequency, its power, and its
/// roughness.
#[derive(Clone, Debug)]
pub struct Turbulence<Source, F>
where
    F: Default + Seedable,
{
    /// Source function that outputs a value.
    pub source: Source,

    /// Frequency value for the Turbulence function.
    pub frequency: f64,

    /// Controls the strength of the turbulence by affecting how much each
    /// point is moved.
    pub power: f64,

    /// Affects the roughness of the turbulence. Higher values are rougher.
    pub roughness: usize,

    seed: u32,
    x_distort_function: Fbm<F>,
    y_distort_function: Fbm<F>,
    z_distort_function: Fbm<F>,
    u_distort_function: Fbm<F>,
}

impl<Source, F> Turbulence<Source, F>
where
    F: Default + Seedable,
{
    pub const DEFAULT_SEED: u32 = 0;
    pub const DEFAULT_FREQUENCY: f64 = 1.0;
    pub const DEFAULT_POWER: f64 = 1.0;
    pub const DEFAULT_ROUGHNESS: usize = 3;

    pub fn new(source: Source) -> Self {
        Self {
            source,
            seed: Self::DEFAULT_SEED,
            frequency: Self::DEFAULT_FREQUENCY,
            power: Self::DEFAULT_POWER,
            roughness: Self::DEFAULT_ROUGHNESS,
            x_distort_function: Fbm::default()
                .set_seed(Self::DEFAULT_SEED)
                .set_octaves(Self::DEFAULT_ROUGHNESS)
                .set_frequency(Self::DEFAULT_FREQUENCY),
            y_distort_function: Fbm::default()
                .set_seed(Self::DEFAULT_SEED + 1)
                .set_octaves(Self::DEFAULT_ROUGHNESS)
                .set_frequency(Self::DEFAULT_FREQUENCY),
            z_distort_function: Fbm::default()
                .set_seed(Self::DEFAULT_SEED + 2)
                .set_octaves(Self::DEFAULT_ROUGHNESS)
                .set_frequency(Self::DEFAULT_FREQUENCY),
            u_distort_function: Fbm::default()
                .set_seed(Self::DEFAULT_SEED + 3)
                .set_octaves(Self::DEFAULT_ROUGHNESS)
                .set_frequency(Self::DEFAULT_FREQUENCY),
        }
    }

    pub fn set_frequency(self, frequency: f64) -> Self {
        Self {
            frequency,
            x_distort_function: self.x_distort_function.set_frequency(frequency),
            y_distort_function: self.y_distort_function.set_frequency(frequency),
            z_distort_function: self.z_distort_function.set_frequency(frequency),
            u_distort_function: self.u_distort_function.set_frequency(frequency),
            ..self
        }
    }

    pub fn set_power(self, power: f64) -> Self {
        Self { power, ..self }
    }

    pub fn set_roughness(self, roughness: usize) -> Self {
        Self {
            roughness,
            x_distort_function: self.x_distort_function.set_octaves(roughness),
            y_distort_function: self.y_distort_function.set_octaves(roughness),
            z_distort_function: self.z_distort_function.set_octaves(roughness),
            u_distort_function: self.u_distort_function.set_octaves(roughness),
            ..self
        }
    }
}

impl<Source, F> Seedable for Turbulence<Source, F>
where
    F: Default + Seedable,
{
    fn set_seed(self, seed: u32) -> Self {
        Self {
            seed,
            x_distort_function: self.x_distort_function.set_seed(seed),
            y_distort_function: self.y_distort_function.set_seed(seed + 1),
            z_distort_function: self.z_distort_function.set_seed(seed + 2),
            u_distort_function: self.u_distort_function.set_seed(seed + 3),
            ..self
        }
    }

    fn seed(&self) -> u32 {
        self.seed
    }
}

impl<Source, F> NoiseFn<f64, 2> for Turbulence<Source, F>
where
    Source: NoiseFn<f64, 2>,
    F: Default + Seedable + NoiseFn<f64, 2>,
{
    fn get(&self, point: [f64; 2]) -> f64 {
        // First, create offsets based on the input values to keep the sampled
        // points from being near a integer boundary. This is a result of
        // using perlin noise, which returns zero at integer boundaries.
        let x0 = point[0] + 12414.0 / 65536.0;
        let y0 = point[1] + 65124.0 / 65536.0;

        let x1 = point[0] + 26519.0 / 65536.0;
        let y1 = point[1] + 18128.0 / 65536.0;

        let x_distort = point[0] + (self.x_distort_function.get([x0, y0]) * self.power);
        let y_distort = point[1] + (self.y_distort_function.get([x1, y1]) * self.power);

        self.source.get([x_distort, y_distort])
    }
}

impl<Source, F> NoiseFn<f64, 3> for Turbulence<Source, F>
where
    Source: NoiseFn<f64, 3>,
    F: Default + Seedable + NoiseFn<f64, 3>,
{
    fn get(&self, point: [f64; 3]) -> f64 {
        // First, create offsets based on the input values to keep the sampled
        // points from being near a integer boundary. This is a result of
        // using perlin noise, which returns zero at integer boundaries.
        let x0 = point[0] + 12414.0 / 65536.0;
        let y0 = point[1] + 65124.0 / 65536.0;
        let z0 = point[2] + 31337.0 / 65536.0;

        let x1 = point[0] + 26519.0 / 65536.0;
        let y1 = point[1] + 18128.0 / 65536.0;
        let z1 = point[2] + 60943.0 / 65536.0;

        let x2 = point[0] + 53820.0 / 65536.0;
        let y2 = point[1] + 11213.0 / 65536.0;
        let z2 = point[2] + 44845.0 / 65536.0;

        let x_distort = point[0] + (self.x_distort_function.get([x0, y0, z0]) * self.power);
        let y_distort = point[1] + (self.y_distort_function.get([x1, y1, z1]) * self.power);
        let z_distort = point[2] + (self.z_distort_function.get([x2, y2, z2]) * self.power);

        self.source.get([x_distort, y_distort, z_distort])
    }
}

impl<Source, F> NoiseFn<f64, 4> for Turbulence<Source, F>
where
    Source: NoiseFn<f64, 4>,
    F: Default + Seedable + NoiseFn<f64, 4>,
{
    fn get(&self, point: [f64; 4]) -> f64 {
        // First, create offsets based on the input values to keep the sampled
        // points from being near a integer boundary. This is a result of
        // using perlin noise, which returns zero at integer boundaries.
        let x0 = point[0] + 12414.0 / 65536.0;
        let y0 = point[1] + 65124.0 / 65536.0;
        let z0 = point[2] + 31337.0 / 65536.0;
        let u0 = point[3] + 57948.0 / 65536.0;

        let x1 = point[0] + 26519.0 / 65536.0;
        let y1 = point[1] + 18128.0 / 65536.0;
        let z1 = point[2] + 60943.0 / 65536.0;
        let u1 = point[3] + 48513.0 / 65536.0;

        let x2 = point[0] + 53820.0 / 65536.0;
        let y2 = point[1] + 11213.0 / 65536.0;
        let z2 = point[2] + 44845.0 / 65536.0;
        let u2 = point[3] + 39357.0 / 65536.0;

        let x3 = point[0] + 18128.0 / 65536.0;
        let y3 = point[1] + 44845.0 / 65536.0;
        let z3 = point[2] + 12414.0 / 65536.0;
        let u3 = point[3] + 60943.0 / 65536.0;

        let x_distort = point[0] + (self.x_distort_function.get([x0, y0, z0, u0]) * self.power);
        let y_distort = point[1] + (self.y_distort_function.get([x1, y1, z1, u1]) * self.power);
        let z_distort = point[2] + (self.z_distort_function.get([x2, y2, z2, u2]) * self.power);
        let u_distort = point[3] + (self.u_distort_function.get([x3, y3, z3, u3]) * self.power);

        self.source
            .get([x_distort, y_distort, z_distort, u_distort])
    }
}