noise_functions/modifiers/
clamp.rs1#[cfg(feature = "nightly-simd")]
2use core::simd::{LaneCount, Simd, SupportedLaneCount};
3
4use crate::{Noise, Sample};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct Clamp<Value, Min, Max> {
12 pub value: Value,
13 pub min: Min,
14 pub max: Max,
15}
16
17impl<Value, Min, Max> Noise for Clamp<Value, Min, Max> {}
18
19impl<const DIM: usize, Value, Min, Max> Sample<DIM> for Clamp<Value, Min, Max>
20where
21 Value: Sample<DIM>,
22 Min: Sample<DIM>,
23 Max: Sample<DIM>,
24{
25 #[inline]
26 fn sample_with_seed(&self, point: [f32; DIM], seed: i32) -> f32 {
27 clamp(self.value.sample_with_seed(point, seed), self.min.sample_with_seed(point, seed), self.max.sample_with_seed(point, seed))
28 }
29}
30
31#[cfg(feature = "nightly-simd")]
32impl<const DIM: usize, const LANES: usize, Value, Min, Max> Sample<DIM, Simd<f32, LANES>> for Clamp<Value, Min, Max>
33where
34 Value: Sample<DIM, Simd<f32, LANES>>,
35 Min: Sample<DIM, Simd<f32, LANES>>,
36 Max: Sample<DIM, Simd<f32, LANES>>,
37 LaneCount<LANES>: SupportedLaneCount,
38{
39 #[inline]
40 fn sample_with_seed(&self, point: Simd<f32, LANES>, seed: i32) -> f32 {
41 clamp(self.value.sample_with_seed(point, seed), self.min.sample_with_seed(point, seed), self.max.sample_with_seed(point, seed))
42 }
43}
44
45#[inline(always)]
46fn clamp(mut value: f32, min: f32, max: f32) -> f32 {
47 if value < min {
48 value = min;
49 }
50
51 if value > max {
52 value = max;
53 }
54
55 value
56}