1 // billow.h
2 //
3 // Copyright (C) 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.billow;
23 
24 private {
25     import noise.mod.modulebase;
26 }
27 /// @addtogroup libnoise
28 /// @{
29 
30 /// @addtogroup modules
31 /// @{
32 
33 /// @addtogroup generatormodules
34 /// @{
35 
36 /// Default frequency for the noise::module::Billow noise module.
37 const double DEFAULT_BILLOW_FREQUENCY = 1.0;
38 
39 /// Default lacunarity for the the noise::module::Billow noise module.
40 const double DEFAULT_BILLOW_LACUNARITY = 2.0;
41 
42 /// Default number of octaves for the the noise::module::Billow noise
43 /// module.
44 const int DEFAULT_BILLOW_OCTAVE_COUNT = 6;
45 
46 /// Default persistence value for the the noise::module::Billow noise
47 /// module.
48 const double DEFAULT_BILLOW_PERSISTENCE = 0.5;
49 
50 /// Default noise quality for the the noise::module::Billow noise module.
51 const NoiseQuality DEFAULT_BILLOW_QUALITY = NoiseQuality.QUALITY_STD;
52 
53 /// Default noise seed for the the noise::module::Billow noise module.
54 const int DEFAULT_BILLOW_SEED = 0;
55 
56 /// Maximum number of octaves for the the noise::module::Billow noise
57 /// module.
58 const int BILLOW_MAX_OCTAVE = 30;
59 
60 /// Noise module that outputs three-dimensional "billowy" noise.
61 ///
62 /// @image html modulebillow.png
63 ///
64 /// This noise module generates "billowy" noise suitable for clouds and
65 /// rocks.
66 ///
67 /// This noise module is nearly identical to noise::module::Perlin except
68 /// this noise module modifies each octave with an absolute-value
69 /// function.  See the documentation of noise::module::Perlin for more
70 /// information.
71 class Billow : Mod
72 {
73 
74   public:
75 
76     /// Constructor.
77     ///
78     /// The default frequency is set to
79     /// noise::module::DEFAULT_BILLOW_FREQUENCY.
80     ///
81     /// The default lacunarity is set to
82     /// noise::module::DEFAULT_BILLOW_LACUNARITY.
83     ///
84     /// The default number of octaves is set to
85     /// noise::module::DEFAULT_BILLOW_OCTAVE_COUNT.
86     ///
87     /// The default persistence value is set to
88     /// noise::module::DEFAULT_BILLOW_PERSISTENCE.
89     ///
90     /// The default seed value is set to
91     /// noise::module::DEFAULT_BILLOW_SEED.
92     this () 
93     {
94         super(this.GetSourceModCount());
95         m_frequency = DEFAULT_BILLOW_FREQUENCY;
96         m_lacunarity = DEFAULT_BILLOW_LACUNARITY;
97         m_noiseQuality = DEFAULT_BILLOW_QUALITY;
98         m_octaveCount = DEFAULT_BILLOW_OCTAVE_COUNT;
99         m_persistence = DEFAULT_BILLOW_PERSISTENCE;
100         m_seed = DEFAULT_BILLOW_SEED;
101     }
102 
103     /// Returns the frequency of the first octave.
104     ///
105     /// @returns The frequency of the first octave.
106     double GetFrequency () const
107     {
108       return m_frequency;
109     }
110 
111     /// Returns the lacunarity of the billowy noise.
112     ///
113     /// @returns The lacunarity of the billowy noise.
114     /// 
115     /// The lacunarity is the frequency multiplier between successive
116     /// octaves.
117     double GetLacunarity () const
118     {
119       return m_lacunarity;
120     }
121 
122     /// Returns the quality of the billowy noise.
123     ///
124     /// @returns The quality of the billowy noise.
125     ///
126     /// See noise::NoiseQuality for definitions of the various
127     /// coherent-noise qualities.
128     NoiseQuality GetNoiseQuality () const
129     {
130       return m_noiseQuality;
131     }
132 
133     /// Returns the number of octaves that generate the billowy noise.
134     ///
135     /// @returns The number of octaves that generate the billowy noise.
136     ///
137     /// The number of octaves controls the amount of detail in the billowy
138     /// noise.
139     int GetOctaveCount () const
140     {
141       return m_octaveCount;
142     }
143 
144     /// Returns the persistence value of the billowy noise.
145     ///
146     /// @returns The persistence value of the billowy noise.
147     ///
148     /// The persistence value controls the roughness of the billowy noise.
149     double GetPersistence () const
150     {
151       return m_persistence;
152     }
153 
154     /// Returns the seed value used by the billowy-noise function.
155     ///
156     /// @returns The seed value.
157     int GetSeed () const
158     {
159       return m_seed;
160     }
161 
162     override int GetSourceModCount () const
163     {
164       return 0;
165     }
166 
167     override double GetValue (double x, double y, double z) const
168     {
169       double value = 0.0;
170       double signal = 0.0;
171       double curPersistence = 1.0;
172       double nx, ny, nz;
173       int seed;
174 
175       x *= m_frequency;
176       y *= m_frequency;
177       z *= m_frequency;
178 
179       for (int curOctave = 0; curOctave < m_octaveCount; curOctave++) {
180 
181         // Make sure that these floating-point values have the same range as a 32-
182         // bit integer so that we can pass them to the coherent-noise functions.
183         nx = MakeInt32Range (x);
184         ny = MakeInt32Range (y);
185         nz = MakeInt32Range (z);
186 
187         // Get the coherent-noise value from the input value and add it to the
188         // final result.
189         seed = (m_seed + curOctave) & 0xffffffff;
190         signal = GradientCoherentNoise3D (nx, ny, nz, seed, m_noiseQuality);
191         signal = 2.0 * fabs (signal) - 1.0;
192         value += signal * curPersistence;
193 
194         // Prepare the next octave.
195         x *= m_lacunarity;
196         y *= m_lacunarity;
197         z *= m_lacunarity;
198         curPersistence *= m_persistence;
199       }
200       value += 0.5;
201 
202       return value;
203     }
204 
205     /// Sets the frequency of the first octave.
206     ///
207     /// @param frequency The frequency of the first octave.
208     void SetFrequency (double frequency)
209     {
210       m_frequency = frequency;
211     }
212 
213     /// Sets the lacunarity of the billowy noise.
214     ///
215     /// @param lacunarity The lacunarity of the billowy noise.
216     /// 
217     /// The lacunarity is the frequency multiplier between successive
218     /// octaves.
219     ///
220     /// For best results, set the lacunarity to a number between 1.5 and
221     /// 3.5.
222     void SetLacunarity (double lacunarity)
223     {
224       m_lacunarity = lacunarity;
225     }
226 
227     /// Sets the quality of the billowy noise.
228     ///
229     /// @param noiseQuality The quality of the billowy noise.
230     ///
231     /// See noise::NoiseQuality for definitions of the various
232     /// coherent-noise qualities.
233     void SetNoiseQuality (NoiseQuality noiseQuality)
234     {
235       m_noiseQuality = noiseQuality;
236     }
237 
238     /// Sets the number of octaves that generate the billowy noise.
239     ///
240     /// @param octaveCount The number of octaves that generate the billowy
241     /// noise.
242     ///
243     /// @pre The number of octaves ranges from 1 to
244     /// noise::module::BILLOW_MAX_OCTAVE.
245     ///
246     /// @throw new noise::ExceptionInvalidParam An invalid parameter was
247     /// specified; see the preconditions for more information.
248     ///
249     /// The number of octaves controls the amount of detail in the billowy
250     /// noise.
251     ///
252     /// The larger the number of octaves, the more time required to
253     /// calculate the billowy-noise value.
254     void SetOctaveCount (int octaveCount)
255     {
256       if (octaveCount < 1 || octaveCount > BILLOW_MAX_OCTAVE) {
257         throw new ExceptionInvalidParam ();
258       }
259       m_octaveCount = octaveCount;
260     }
261 
262     /// Sets the persistence value of the billowy noise.
263     ///
264     /// @param persistence The persistence value of the billowy noise.
265     ///
266     /// The persistence value controls the roughness of the billowy noise.
267     ///
268     /// For best results, set the persistence value to a number between
269     /// 0.0 and 1.0.
270     void SetPersistence (double persistence)
271     {
272       m_persistence = persistence;
273     }
274 
275     /// Sets the seed value used by the billowy-noise function.
276     ///
277     /// @param seed The seed value.
278     void SetSeed (int seed)
279     {
280       m_seed = seed;
281     }
282 
283   protected:
284 
285     /// Frequency of the first octave.
286     double m_frequency;
287 
288     /// Frequency multiplier between successive octaves.
289     double m_lacunarity;
290 
291     /// Quality of the billowy noise.
292     NoiseQuality m_noiseQuality;
293 
294     /// Total number of octaves that generate the billowy noise.
295     int m_octaveCount;
296 
297     /// Persistence value of the billowy noise.
298     double m_persistence;
299 
300     /// Seed value used by the billowy-noise function.
301     int m_seed;
302 
303 }
304 
305 /// @}
306 
307 /// @}
308 
309 /// @}