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
// Copyright (c) 2017 The Noise-rs Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT
// or http://opensource.org/licenses/MIT>, at your option. All files in the
// project carrying such notice may not be copied, modified, or distributed
// except according to those terms.

use modules::NoiseModule;
use num_traits::Float;

/// Noise module that applies a scaling factor and a bias to the output value
/// from the source module.
///
/// The module retrieves the output value from the source module, multiplies
/// it with the scaling factor, adds the bias to it, then outputs the value.
pub struct ScaleBias<Source, T> {
    /// Outputs a value.
    pub source: Source,

    /// Scaling factor to apply to the output value from the source module.
    /// The default value is 1.0.
    pub scale: T,

    /// Bias to apply to the scaled output value from the source module.
    /// The default value is 0.0.
    pub bias: T,
}

impl<Source, T> ScaleBias<Source, T>
    where T: Float,
{
    pub fn new(source: Source) -> ScaleBias<Source, T> {
        ScaleBias {
            source: source,
            scale: T::one(),
            bias: T::zero(),
        }
    }

    pub fn set_scale(self, scale: T) -> ScaleBias<Source, T> {
        ScaleBias { scale: scale, ..self }
    }

    pub fn set_bias(self, bias: T) -> ScaleBias<Source, T> {
        ScaleBias { bias: bias, ..self }
    }
}

impl<Source, T, U> NoiseModule<T> for ScaleBias<Source, U>
    where Source: NoiseModule<T, Output = U>,
          T: Copy,
          U: Float,
{
    type Output = U;

    fn get(&self, point: T) -> Self::Output {
        (self.source.get(point)).mul_add(self.scale, self.bias)
    }
}