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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) 2017 The Noise-rs Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT
// or http://opensource.org/licenses/MIT>, at your option. All files in the
// project carrying such notice may not be copied, modified, or distributed
// except according to those terms.

use {GenFn2, GenFn3, GenFn4};

use {PermutationTable, math};
use {Point2, Point3, Point4};
use num_traits::Float;

/// A callable struct for applying 2-dimensional [fractional Brownian motion]
/// (http://en.wikipedia.org/wiki/Fractional_Brownian_motion).
///
/// Fractional Brownian motion is a way of combining multiple octaves of a noise
/// function to create a richer and more varied output. It can theoretically be
/// used with any noise function, but it tends to only produce good results with
/// gradient noise functions.
///
/// The struct contains many parameters, which can either be set using the
/// builder methods provided, or by constructing the type manually.
///
/// # Example
///
/// ```rust
/// extern crate noise;
/// extern crate rand;
///
/// use noise::{Brownian2, perlin2};
///
/// # fn main() {
/// let seed = rand::random();
/// let noise = Brownian2::new(perlin2, 4).wavelength(32.0);
/// let val = noise.apply(&seed, &[42.0, 37.0]);
/// # }
/// ```
#[derive(Copy, Clone)]
#[deprecated(since="0.3.0", note="please use `Fbm::new()` instead")]
pub struct Brownian2<T, F: GenFn2<T>> {
    /// The underlying noise function to call.
    pub function: F,
    /// The number of times to call the noise function.
    pub octaves: usize,
    /// The base frequency of the noise
    pub frequency: T,
    /// The rate at which the amplitude of the noise is reduced for each octave.
    pub persistence: T,
    /// The rate at which the frequency of the noise increases for each octave.
    pub lacunarity: T,
}

/// A callable struct for applying 3-dimensional [fractional Brownian motion]
/// (http://en.wikipedia.org/wiki/Fractional_Brownian_motion).
///
/// Fractional Brownian motion is a way of combining multiple octaves of a noise
/// function to create a richer and more varied output. It can theoretically be
/// used with any noise function, but it tends to only produce good results with
/// gradient noise functions.
///
/// The struct contains many parameters, which can either be set using the
/// builder methods provided, or by constructing the type manually.
///
/// # Example
///
/// ```rust
/// extern crate noise;
/// extern crate rand;
///
/// use noise::{Brownian3, perlin3};
///
/// # fn main() {
/// let seed = rand::random();
/// let noise = Brownian3::new(perlin3, 4).wavelength(32.0);
/// let val = noise.apply(&seed, &[42.0, 37.0, 2.0]);
/// # }
/// ```
#[derive(Copy, Clone)]
#[deprecated(since="0.3.0", note="please use `Fbm::new()` instead")]
pub struct Brownian3<T, F: GenFn3<T>> {
    /// The underlying noise function to call.
    pub function: F,
    /// The number of times to call the noise function.
    pub octaves: usize,
    /// The base frequency of the noise
    pub frequency: T,
    /// The rate at which the amplitude of the noise is reduced for each octave.
    pub persistence: T,
    /// The rate at which the frequency of the noise increases for each octave.
    pub lacunarity: T,
}

/// A callable struct for applying 4-dimensional [fractional Brownian motion]
/// (http://en.wikipedia.org/wiki/Fractional_Brownian_motion).
///
/// Fractional Brownian motion is a way of combining multiple octaves of a noise
/// function to create a richer and more varied output. It can theoretically be
/// used with any noise function, but it tends to only produce good results with
/// gradient noise functions.
///
/// The struct contains many parameters, which can either be set using the
/// builder methods provided, or by constructing the type manually.
///
/// # Example
///
/// ```rust
/// extern crate noise;
/// extern crate rand;
///
/// use noise::{Brownian4, perlin4};
///
/// # fn main() {
/// let seed = rand::random();
/// let noise = Brownian4::new(perlin4, 4).wavelength(32.0);
/// let val = noise.apply(&seed, &[42.0, 37.0, 2.0, 3.0]);
/// # }
/// ```
#[derive(Copy, Clone)]
#[deprecated(since="0.3.0", note="please use `Fbm::new()` instead")]
pub struct Brownian4<T, F: GenFn4<T>> {
    /// The underlying noise function to call on each octave.
    pub function: F,
    /// The number of times to call the noise function.
    pub octaves: usize,
    /// The base frequency of the noise
    pub frequency: T,
    /// The rate at which the amplitude of the noise is reduced on each octave.
    pub persistence: T,
    /// The rate at which the frequency of the noise increases on each octave.
    pub lacunarity: T,
}

macro_rules! impl_brownian {
    { $Brownian:ident, $GenFn:ident, $Point:ident, $mapn:path } => {
        impl<T: Float, F: $GenFn<T>> $Brownian<T, F> {
            /// Constructs a new brownian noise function, defaulting to:
            ///
            /// - frequency: `1.0`
            /// - lacunarity: `2.0`
            /// - persistence: `0.5`
            #[inline]
            #[deprecated(since="0.3.0", note="please use `noisemodule::Fbm` instead")]
            pub fn new(function: F, octaves: usize) -> $Brownian<T, F> {
                $Brownian {
                    function: function,
                    octaves: octaves,
                    frequency: math::cast(1.0f32),
                    lacunarity: math::cast(2.0f32),
                    persistence: math::cast(0.5f32),
                }
            }

            /// A builder method that sets underlying noise function to call on
            /// each octave.
            #[inline]
            pub fn function<Q: $GenFn<T>>(self, function: Q) -> $Brownian<T, Q> {
                let $Brownian { octaves, frequency, lacunarity, persistence, .. } = self;
                $Brownian {
                    function: function,
                    octaves: octaves,
                    frequency: frequency,
                    persistence: persistence,
                    lacunarity: lacunarity,
                }
            }

            /// A builder method that sets the number of times to call the noise
            /// function.
            #[inline]
            pub fn octaves(self, octaves: usize) -> $Brownian<T, F> {
                $Brownian { octaves: octaves, ..self }
            }

            /// A builder method that sets the wavelength of the brownian noise.
            /// This is equivalent to `self.frequency(wavelength.recip())`.
            #[inline]
            pub fn wavelength(self, wavelength: T) -> $Brownian<T, F> {
                self.frequency(wavelength.recip())
            }

            /// A builder method that sets the base frequency of the noise.
            #[inline]
            pub fn frequency(self, frequency: T) -> $Brownian<T, F> {
                $Brownian { frequency: frequency, ..self }
            }

            /// A builder method that sets the rate at which the amplitude of
            /// the noise is reduced on each octave.
            #[inline]
            pub fn persistence(self, persistence: T) -> $Brownian<T, F> {
                $Brownian { persistence: persistence, ..self }
            }

            /// A builder method that sets the rate at which the frequency of
            /// the noise increases on each octave.
            #[inline]
            pub fn lacunarity(self, lacunarity: T) -> $Brownian<T, F> {
                $Brownian { lacunarity: lacunarity, ..self }
            }

            /// Apply the Brownian noise function for the supplied permutation table and point.
            #[inline]
            pub fn apply(&self, perm_table: &PermutationTable, point: &$Point<T>) -> T {
                // Fixes weird accumulation at the origin.
                //
                // The chosen offset is the square root of 3. An irrational
                // number is chosen so grid-aligned noise values don't coincide
                // with each other.
                const OFFSET: f64 = 1.73205080756887;

                let mut frequency: T = self.frequency;
                let mut amplitude: T = math::cast(1);
                let mut total_amplitude = T::zero();
                let mut result: T = math::cast(0);
                let mut offset: T = math::cast(OFFSET);
                let point = *point;
                for _ in 0..self.octaves {
                    let scaled_point = $mapn(point, |v| v * frequency + offset);
                    result = result + ((self.function)(perm_table, &scaled_point) * amplitude);
                    total_amplitude = total_amplitude + amplitude;
                    amplitude = amplitude * self.persistence;
                    frequency = frequency * self.lacunarity;
                    offset = offset + math::cast(OFFSET);
                }
                result / total_amplitude
            }
        }
    }
}

impl_brownian! { Brownian2, GenFn2, Point2, math::map2 }
impl_brownian! { Brownian3, GenFn3, Point3, math::map3 }
impl_brownian! { Brownian4, GenFn4, Point4, math::map4 }

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> Fn(&'a PermutationTable, &'b Point2<T>) for Brownian2<T, F>
    where T: Float,
          F: GenFn2<T>,
{
    /// Applies the brownian noise function for the supplied permutation table and point.
    extern "rust-call" fn call(&self,
                               (perm_table, point): (&'a PermutationTable, &'b Point2<T>))
                               -> T {
        self.apply(perm_table, point)
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnMut(&'a PermutationTable, &'b Point2<T>) for Brownian2<T, F>
    where T: Float,
          F: GenFn2<T>,
{
    extern "rust-call" fn call_mut(&mut self, (table, point): (&'a Seed, &'b Point2<T>)) -> T {
        self.call((table, point))
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnOnce(&'a Seed, &'b Point2<T>) for Brownian2<T, F>
    where T: Float,
          F: GenFn2<T>,
{
    type Output = T;
    extern "rust-call" fn call_once(self, (seed, point): (&'a Seed, &'b Point2<T>)) -> T {
        self.call((seed, point))
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> Fn(&'a Seed, &'b Point3<T>) for Brownian3<T, F>
    where T: Float,
          F: GenFn3<T>,
{
    /// Applies the brownian noise function for the supplied seed and point.
    extern "rust-call" fn call(&self, (seed, point): (&'a Seed, &'b Point3<T>)) -> T {
        self.apply(seed, point)
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnMut(&'a Seed, &'b Point3<T>) for Brownian3<T, F>
    where T: Float,
          F: GenFn3<T>,
{
    extern "rust-call" fn call_mut(&mut self, (seed, point): (&'a Seed, &'b Point3<T>)) -> T {
        self.call((seed, point))
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnOnce(&'a Seed, &'b Point3<T>) for Brownian3<T, F>
    where T: Float,
          F: GenFn3<T>,
{
    type Output = T;
    extern "rust-call" fn call_once(self, (seed, point): (&'a Seed, &'b Point3<T>)) -> T {
        self.call((seed, point))
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> Fn(&'a Seed, &'b ::Point4<T>) for Brownian4<T, F>
    where T: Float,
          F: GenFn4<T>,
{
    /// Applies the brownian noise function for the supplied seed and point.
    extern "rust-call" fn call(&self, (seed, point): (&'a Seed, &'b Point4<T>)) -> T {
        self.apply(seed, point)
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnMut(&'a Seed, &'b Point4<T>) for Brownian4<T, F>
    where T: Float,
          F: GenFn4<T>,
{
    extern "rust-call" fn call_mut(&mut self, (seed, point): (&'a Seed, &'b Point4<T>)) -> T {
        self.call((seed, point))
    }
}

#[cfg(rust_unstable)]
impl<'a, 'b, T, F> FnOnce(&'a Seed, &'b Point4<T>) for Brownian4<T, F>
    where T: Float,
          F: GenFn4<T>,
{
    type Output = T;
    extern "rust-call" fn call_once(self, (seed, point): (&'a Seed, &'b Point4<T>)) -> T {
        self.call((seed, point))
    }
}