1 // clamp.h
2 //
3 // Copyright (C) 2003, 2004 Jason Bevins
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or (at
8 // your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 // License (COPYING.txt) for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // The developer's email is jlbezigvins@gmzigail.com (for great email, take
20 // off every 'zig'.)
21 //
22 module noise.mod.clamp;
23 
24 private {
25     import noise.mod.modulebase;
26 }
27 
28 /// @addtogroup libnoise
29 /// @{
30 
31 /// @addtogroup modules
32 /// @{
33 
34 /// @addtogroup modifiermodules
35 /// @{
36 
37 /// Default lower bound of the clamping range for the module::Clamp
38 /// noise module.
39 const double DEFAULT_CLAMP_LOWER_BOUND = -1.0;
40 
41 /// Default upper bound of the clamping range for the module::Clamp
42 /// noise module.
43 const double DEFAULT_CLAMP_UPPER_BOUND = 1.0;
44 
45 /// Noise module that clamps the output value from a source module to a
46 /// range of values.
47 ///
48 /// @image html moduleclamp.png
49 ///
50 /// The range of values in which to clamp the output value is called the
51 /// <i>clamping range</i>.
52 ///
53 /// If the output value from the source module is less than the lower
54 /// bound of the clamping range, this noise module clamps that value to
55 /// the lower bound.  If the output value from the source module is
56 /// greater than the upper bound of the clamping range, this noise module
57 /// clamps that value to the upper bound.
58 ///
59 /// To specify the upper and lower bounds of the clamping range, call the
60 /// SetBounds() method.
61 ///
62 /// This noise module requires one source module.
63 class Clamp : Mod
64 {
65 
66   public:
67 
68     /// Constructor.
69     ///
70     /// The default lower bound of the clamping range is set to
71     /// module::DEFAULT_CLAMP_LOWER_BOUND.
72     ///
73     /// The default upper bound of the clamping range is set to
74     /// module::DEFAULT_CLAMP_UPPER_BOUND.
75     this()
76     {
77         super(this.GetSourceModCount());
78         m_lowerBound = DEFAULT_CLAMP_LOWER_BOUND;
79         m_upperBound = DEFAULT_CLAMP_UPPER_BOUND;
80     }
81 
82     /// Returns the lower bound of the clamping range.
83     ///
84     /// @returns The lower bound.
85     ///
86     /// If the output value from the source module is less than the lower
87     /// bound of the clamping range, this noise module clamps that value
88     /// to the lower bound.
89     double GetLowerBound() const
90     {
91       return m_lowerBound;
92     }
93 
94     override int GetSourceModCount() const
95     {
96       return 1;
97     }
98 
99     /// Returns the upper bound of the clamping range.
100     ///
101     /// @returns The upper bound.
102     ///
103     /// If the output value from the source module is greater than the
104     /// upper bound of the clamping range, this noise module clamps that
105     /// value to the upper bound.
106     double GetUpperBound() const
107     {
108       return m_upperBound;
109     }
110 
111     override double GetValue(double x, double y, double z) const
112     {
113       assert (m_pSourceMod[0] !is null);
114 
115       double value = m_pSourceMod[0].GetValue (x, y, z);
116       if (value < m_lowerBound) {
117         return m_lowerBound;
118       } else if (value > m_upperBound) {
119         return m_upperBound;
120       } else {
121         return value;
122       }
123     }
124 
125     /// Sets the lower and upper bounds of the clamping range.
126     ///
127     /// @param lowerBound The lower bound.
128     /// @param upperBound The upper bound.
129     ///
130     /// @pre The lower bound must be less than or equal to the
131     /// upper bound.
132     ///
133     /// @throw new ExceptionInvalidParam An invalid parameter was
134     /// specified; see the preconditions for more information.
135     ///
136     /// If the output value from the source module is less than the lower
137     /// bound of the clamping range, this noise module clamps that value
138     /// to the lower bound.  If the output value from the source module
139     /// is greater than the upper bound of the clamping range, this noise
140     /// module clamps that value to the upper bound.
141     void SetBounds(double lowerBound, double upperBound)
142     {
143       assert (lowerBound < upperBound);
144 
145       m_lowerBound = lowerBound;
146       m_upperBound = upperBound;
147     }
148 
149   protected:
150 
151     /// Lower bound of the clamping range.
152     double m_lowerBound;
153 
154     /// Upper bound of the clamping range.
155     double m_upperBound;
156 
157 };
158 
159 /// @}
160 
161 /// @}
162 
163 /// @}