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
use modules::NoiseModule;
use num_traits::Float;
pub struct Clamp<Source, T> {
pub source: Source,
pub lower_bound: T,
pub upper_bound: T,
}
impl<Source, T> Clamp<Source, T>
where T: Float,
{
pub fn new(source: Source) -> Clamp<Source, T> {
Clamp {
source: source,
lower_bound: -T::one(),
upper_bound: T::one(),
}
}
pub fn set_lower_bound(self, lower_bound: T) -> Clamp<Source, T> {
Clamp { lower_bound: lower_bound, ..self }
}
pub fn set_upper_bound(self, upper_bound: T) -> Clamp<Source, T> {
Clamp { upper_bound: upper_bound, ..self }
}
pub fn set_bounds(self, lower_bound: T, upper_bound: T) -> Clamp<Source, T> {
Clamp {
lower_bound: lower_bound,
upper_bound: upper_bound,
..self
}
}
}
impl<Source, T, U> NoiseModule<T> for Clamp<Source, U>
where Source: NoiseModule<T, Output = U>,
T: Copy,
U: Float,
{
type Output = U;
fn get(&self, point: T) -> Self::Output {
let value = self.source.get(point);
match () {
_ if value < self.lower_bound => self.lower_bound,
_ if value > self.upper_bound => self.upper_bound,
_ => value,
}
}
}