noise_functions/
noise.rs

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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#[cfg(feature = "nightly-simd")]
use core::simd::{f32x2, f32x4};

use crate::{
    modifiers::{
        Abs, Add, AddSeed, Ceil, Clamp, Div, Fbm, Floor, Frequency, Lerp, Map, Max, Min, Mul, MulSeed, Neg, Pow, Rem, Ridged, Round, Seeded, Sqrt, Sub, Tileable, TranslateX, TranslateXy,
        TranslateXyz, TranslateXyzw, TriangleWave,
    },
    Sample, ValueOrNoise,
};

/// Provides utility methods for noise types.
pub trait Noise {
    /// Samples the noise in 2D.
    fn sample2<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<2>,
        Point: Into<[f32; 2]>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Samples the noise in 3D.
    fn sample3<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<3>,
        Point: Into<[f32; 3]>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Samples the noise in 3D.
    fn sample4<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<4>,
        Point: Into<[f32; 4]>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Samples the noise in 2D.
    #[cfg(feature = "nightly-simd")]
    fn sample2a<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<2, f32x2>,
        Point: Into<f32x2>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Samples the noise in 3D.
    #[cfg(feature = "nightly-simd")]
    fn sample3a<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<3, f32x4>,
        Point: Into<f32x4>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Samples the noise in 4D.
    #[cfg(feature = "nightly-simd")]
    fn sample4a<Point>(&self, point: Point) -> f32
    where
        Self: Sized + Sample<4, f32x4>,
        Point: Into<f32x4>,
    {
        self.sample_with_seed(point.into(), 0)
    }

    /// Overwrites the seed to be sampled with.
    ///
    /// For the sake of composition it can be better to use [`add_seed`](Self::add_seed) instead.
    #[inline(always)]
    fn seed(self, seed: i32) -> Seeded<Self>
    where
        Self: Sized,
    {
        Seeded { noise: self, seed }
    }

    /// Adds `value` to the seed.
    #[inline(always)]
    fn add_seed(self, value: i32) -> AddSeed<Self>
    where
        Self: Sized,
    {
        AddSeed { noise: self, value }
    }

    /// Multiplies the seed by `value`.
    #[inline(always)]
    fn mul_seed(self, value: i32) -> MulSeed<Self>
    where
        Self: Sized,
    {
        MulSeed { noise: self, value }
    }

    /// Modifies a noise with a frequency multiplier.
    ///
    /// This multiplies the point by the provided `frequency` before sampling.
    #[inline(always)]
    fn frequency<F>(self, frequency: F) -> Frequency<Self, F::Noise>
    where
        Self: Sized,
        F: ValueOrNoise,
    {
        Frequency {
            noise: self,
            frequency: frequency.into_noise(),
        }
    }

    /// Creates a fractal from this noise with the provided `octaves`, `gain`
    /// and `lacunarity`.
    ///
    /// The seed, with which the fractal is sampled with, will be incremented
    /// after each octave.
    // TODO: explain the parameters
    #[inline(always)]
    fn fbm(self, octaves: u32, gain: f32, lacunarity: f32) -> Fbm<Self>
    where
        Self: Sized,
    {
        Fbm::new(self, octaves, gain, lacunarity)
    }

    /// Modifies a noise to create a peak at 0.
    ///
    /// This outputs values is in the [-1, 1] range.
    ///
    /// **Note:** This modifier assumes `self` returns values in the [-1, 1] range.
    #[inline(always)]
    fn ridged(self) -> Ridged<Self>
    where
        Self: Sized,
    {
        Ridged { noise: self }
    }

    /// Applies a triangle wave to the output of a noise function.
    ///
    /// This outputs values is in the [-1, 1] range.
    ///
    /// **Note:** This modifier assumes `self` returns values in the [-1, 1] range.
    #[inline(always)]
    fn triangle_wave<F>(self, frequency: F) -> TriangleWave<Self, F::Noise>
    where
        Self: Sized,
        F: ValueOrNoise,
    {
        TriangleWave {
            noise: self,
            frequency: frequency.into_noise(),
        }
    }

    /// Creates a tileable 2D noise from a 4D noise.
    ///
    /// The parameters `width` and `height` describe the size of the repeating tile.
    #[inline(always)]
    fn tileable(self, width: f32, height: f32) -> Tileable<Self>
    where
        Self: Sized,
    {
        Tileable::new(self, width, height)
    }

    /// Translates the point before it is used to sample `self`.
    fn translate_x<X>(self, x: X) -> TranslateX<Self, X::Noise>
    where
        Self: Sized,
        X: ValueOrNoise,
    {
        TranslateX { noise: self, x: x.into_noise() }
    }

    /// Translates the point before it is used to sample `self`.
    fn translate_xy<X, Y>(self, x: X, y: Y) -> TranslateXy<Self, X::Noise, Y::Noise>
    where
        Self: Sized,
        X: ValueOrNoise,
        Y: ValueOrNoise,
    {
        TranslateXy {
            noise: self,
            x: x.into_noise(),
            y: y.into_noise(),
        }
    }

    /// Translates the point before it is used to sample `self`.
    fn translate_xyz<X, Y, Z>(self, x: X, y: Y, z: Z) -> TranslateXyz<Self, X::Noise, Y::Noise, Z::Noise>
    where
        Self: Sized,
        X: ValueOrNoise,
        Y: ValueOrNoise,
        Z: ValueOrNoise,
    {
        TranslateXyz {
            noise: self,
            x: x.into_noise(),
            y: y.into_noise(),
            z: z.into_noise(),
        }
    }

    /// Translates the point before it is used to sample `self`.
    fn translate_xyzw<X, Y, Z, W>(self, x: X, y: Y, z: Z, w: W) -> TranslateXyzw<Self, X::Noise, Y::Noise, Z::Noise, W::Noise>
    where
        Self: Sized,
        X: ValueOrNoise,
        Y: ValueOrNoise,
        Z: ValueOrNoise,
        W: ValueOrNoise,
    {
        TranslateXyzw {
            noise: self,
            x: x.into_noise(),
            y: y.into_noise(),
            z: z.into_noise(),
            w: w.into_noise(),
        }
    }

    /// Adds the output values.
    fn add<Rhs>(self, rhs: Rhs) -> Add<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Add { lhs: self, rhs: rhs.into_noise() }
    }

    /// Subtracts one output value from the other.
    fn sub<Rhs>(self, rhs: Rhs) -> Sub<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Sub { lhs: self, rhs: rhs.into_noise() }
    }

    /// Multiplies the output values.
    fn mul<Rhs>(self, rhs: Rhs) -> Mul<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Mul { lhs: self, rhs: rhs.into_noise() }
    }

    /// Divides one output value by the other.
    fn div<Rhs>(self, rhs: Rhs) -> Div<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Div { lhs: self, rhs: rhs.into_noise() }
    }

    /// Calculates the remainder from dividing one output value by the other.
    fn rem<Rhs>(self, rhs: Rhs) -> Rem<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Rem { lhs: self, rhs: rhs.into_noise() }
    }

    /// Computes the minimum of the two output values.
    fn min<Rhs>(self, rhs: Rhs) -> Min<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Min { lhs: self, rhs: rhs.into_noise() }
    }

    /// Computes the maximum of the two output values.
    fn max<Rhs>(self, rhs: Rhs) -> Max<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Max { lhs: self, rhs: rhs.into_noise() }
    }

    /// Returns `max` if `value` is greater than `max` and `min` if `value` is less than `min`.
    /// Otherwise this will return `value`.
    ///
    /// Unlike [`f32::clamp`], this modifier won't panic if `!(min <= max)`.
    fn clamp<Min, Max>(self, min: Min, max: Max) -> Clamp<Self, Min::Noise, Max::Noise>
    where
        Self: Sized,
        Min: ValueOrNoise,
        Max: ValueOrNoise,
    {
        Clamp {
            value: self,
            min: min.into_noise(),
            max: max.into_noise(),
        }
    }

    /// Linearly interpolates between `self` and `b`.
    #[doc(alias = "mix")]
    #[doc(alias = "blend")]
    fn lerp<B, T>(self, b: B, t: T) -> Lerp<Self, B::Noise, T::Noise>
    where
        Self: Sized,
        B: ValueOrNoise,
        T: ValueOrNoise,
    {
        Lerp {
            a: self,
            b: b.into_noise(),
            t: t.into_noise(),
        }
    }

    /// Raises the output value to a power.
    fn pow<Rhs>(self, rhs: Rhs) -> Pow<Self, Rhs::Noise>
    where
        Self: Sized,
        Rhs: ValueOrNoise,
    {
        Pow { lhs: self, rhs: rhs.into_noise() }
    }

    /// Performs negation on the output value.
    fn neg(self) -> Neg<Self>
    where
        Self: Sized,
    {
        Neg { noise: self }
    }

    /// Computes the absolute value of the output value.
    fn abs(self) -> Abs<Self>
    where
        Self: Sized,
    {
        Abs { noise: self }
    }

    /// Returns the square root of a number.
    ///
    /// Returns NaN if `self` is a negative number other than `-0.0`.
    fn sqrt(self) -> Sqrt<Self>
    where
        Self: Sized,
    {
        Sqrt { noise: self }
    }

    /// Computes the largest integer less than or equal to the output value.
    fn floor(self) -> Floor<Self>
    where
        Self: Sized,
    {
        Floor { noise: self }
    }

    /// Computes the smallest integer greater than or equal to self.
    fn ceil(self) -> Ceil<Self>
    where
        Self: Sized,
    {
        Ceil { noise: self }
    }

    /// Computes the nearest integer to the output value.
    /// If a value is half-way between two integers, round away from 0.0.
    fn round(self) -> Round<Self>
    where
        Self: Sized,
    {
        Round { noise: self }
    }

    /// Maps the output value.
    #[inline(always)]
    fn map<F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: Fn(f32) -> f32,
    {
        Map { noise: self, f }
    }

    /// Returns the `Noise` by reference.
    ///
    /// This reference also implements `Noise`.
    fn by_ref(&self) -> &Self
    where
        Self: Sized,
    {
        self
    }
}

impl<N: Noise> Noise for &N {}

#[cfg(feature = "alloc")]
impl<N: Noise + ?Sized> Noise for alloc::boxed::Box<N> {}