noise_functions/modifiers/
clamp.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
#[cfg(feature = "nightly-simd")]
use core::simd::{LaneCount, Simd, SupportedLaneCount};

use crate::{Noise, Sample};

/// 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)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Clamp<Value, Min, Max> {
    pub value: Value,
    pub min: Min,
    pub max: Max,
}

impl<Value, Min, Max> Noise for Clamp<Value, Min, Max> {}

impl<const DIM: usize, Value, Min, Max> Sample<DIM> for Clamp<Value, Min, Max>
where
    Value: Sample<DIM>,
    Min: Sample<DIM>,
    Max: Sample<DIM>,
{
    #[inline]
    fn sample_with_seed(&self, point: [f32; DIM], seed: i32) -> f32 {
        clamp(self.value.sample_with_seed(point, seed), self.min.sample_with_seed(point, seed), self.max.sample_with_seed(point, seed))
    }
}

#[cfg(feature = "nightly-simd")]
impl<const DIM: usize, const LANES: usize, Value, Min, Max> Sample<DIM, Simd<f32, LANES>> for Clamp<Value, Min, Max>
where
    Value: Sample<DIM, Simd<f32, LANES>>,
    Min: Sample<DIM, Simd<f32, LANES>>,
    Max: Sample<DIM, Simd<f32, LANES>>,
    LaneCount<LANES>: SupportedLaneCount,
{
    #[inline]
    fn sample_with_seed(&self, point: Simd<f32, LANES>, seed: i32) -> f32 {
        clamp(self.value.sample_with_seed(point, seed), self.min.sample_with_seed(point, seed), self.max.sample_with_seed(point, seed))
    }
}

#[inline(always)]
fn clamp(mut value: f32, min: f32, max: f32) -> f32 {
    if value < min {
        value = min;
    }

    if value > max {
        value = max;
    }

    value
}