noise_functions/modifiers/
ridged.rs

1#[cfg(feature = "nightly-simd")]
2use core::simd::{LaneCount, Simd, SupportedLaneCount};
3
4use crate::{math::abs, Noise, Sample};
5
6/// Modifies a noise to create a peak at 0.
7///
8/// This outputs values is in the [-1, 1] range.
9///
10/// **Note:** This modifier assumes `self` returns values in the [-1, 1] range.
11pub struct Ridged<Noise> {
12    pub noise: Noise,
13}
14
15impl<N> Noise for Ridged<N> {}
16
17impl<Noise, const DIM: usize> Sample<DIM> for Ridged<Noise>
18where
19    Noise: Sample<DIM>,
20{
21    #[inline]
22    fn sample_with_seed(&self, point: [f32; DIM], seed: i32) -> f32 {
23        apply(self.noise.sample_with_seed(point, seed))
24    }
25}
26
27#[cfg(feature = "nightly-simd")]
28impl<Noise, const DIM: usize, const LANES: usize> Sample<DIM, Simd<f32, LANES>> for Ridged<Noise>
29where
30    Noise: Sample<DIM, Simd<f32, LANES>>,
31    LaneCount<LANES>: SupportedLaneCount,
32{
33    #[inline]
34    fn sample_with_seed(&self, point: Simd<f32, LANES>, seed: i32) -> f32 {
35        apply(self.noise.sample_with_seed(point, seed))
36    }
37}
38
39#[inline]
40fn apply(value: f32) -> f32 {
41    abs(value) * -2.0 + 1.0
42}