1 // perlin.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.perlin;
23 
24 private {
25     import noise.mod.modulebase;
26 }
27 
28 /// @addtogroup libnoise
29 /// @{
30 
31 /// @addtogroup modules
32 /// @{
33 
34 /// @addtogroup generatormodules
35 /// @{
36 
37 /// Default frequency for the module::Perlin noise module.
38 const double DEFAULT_PERLIN_FREQUENCY = 1.0;
39 
40 /// Default lacunarity for the module::Perlin noise module.
41 const double DEFAULT_PERLIN_LACUNARITY = 2.0;
42 
43 /// Default number of octaves for the module::Perlin noise module.
44 const int DEFAULT_PERLIN_OCTAVE_COUNT = 6;
45 
46 /// Default persistence value for the module::Perlin noise module.
47 const double DEFAULT_PERLIN_PERSISTENCE = 0.5;
48 
49 /// Default noise quality for the module::Perlin noise module.
50 const NoiseQuality DEFAULT_PERLIN_QUALITY = NoiseQuality.QUALITY_STD;
51 
52 /// Default noise seed for the module::Perlin noise module.
53 const int DEFAULT_PERLIN_SEED = 0;
54 
55 /// Maximum number of octaves for the module::Perlin noise module.
56 const int PERLIN_MAX_OCTAVE = 30;
57 
58 /// Noise module that outputs 3-dimensional Perlin noise.
59 ///
60 /// @image html moduleperlin.png
61 ///
62 /// Perlin noise is the sum of several coherent-noise functions of
63 /// ever-increasing frequencies and ever-decreasing amplitudes.
64 ///
65 /// An important property of Perlin noise is that a small change in the
66 /// input value will produce a small change in the output value, while a
67 /// large change in the input value will produce a random change in the
68 /// output value.
69 ///
70 /// This noise module outputs Perlin-noise values that usually range from
71 /// -1.0 to +1.0, but there are no guarantees that all output values will
72 /// exist within that range.
73 ///
74 /// For a better description of Perlin noise, see the links in the
75 /// <i>References and Acknowledgments</i> section.
76 ///
77 /// This noise module does not require any source modules.
78 ///
79 /// <b>Octaves</b>
80 ///
81 /// The number of octaves control the <i>amount of detail</i> of the
82 /// Perlin noise.  Adding more octaves increases the detail of the Perlin
83 /// noise, but with the drawback of increasing the calculation time.
84 ///
85 /// An octave is one of the coherent-noise functions in a series of
86 /// coherent-noise functions that are added together to form Perlin
87 /// noise.
88 ///
89 /// An application may specify the frequency of the first octave by
90 /// calling the SetFrequency() method.
91 ///
92 /// An application may specify the number of octaves that generate Perlin
93 /// noise by calling the SetOctaveCount() method.
94 ///
95 /// These coherent-noise functions are called octaves because each octave
96 /// has, by default, double the frequency of the previous octave.  Musical
97 /// tones have this property as well; a musical C tone that is one octave
98 /// higher than the previous C tone has double its frequency.
99 ///
100 /// <b>Frequency</b>
101 ///
102 /// An application may specify the frequency of the first octave by
103 /// calling the SetFrequency() method.
104 ///
105 /// <b>Persistence</b>
106 ///
107 /// The persistence value controls the <i>roughness</i> of the Perlin
108 /// noise.  Larger values produce rougher noise.
109 ///
110 /// The persistence value determines how quickly the amplitudes diminish
111 /// for successive octaves.  The amplitude of the first octave is 1.0.
112 /// The amplitude of each subsequent octave is equal to the product of the
113 /// previous octave's amplitude and the persistence value.  So a
114 /// persistence value of 0.5 sets the amplitude of the first octave to
115 /// 1.0; the second, 0.5; the third, 0.25; etc.
116 ///
117 /// An application may specify the persistence value by calling the
118 /// SetPersistence() method.
119 ///
120 /// <b>Lacunarity</b>
121 ///
122 /// The lacunarity specifies the frequency multipler between successive
123 /// octaves.
124 ///
125 /// The effect of modifying the lacunarity is subtle; you may need to play
126 /// with the lacunarity value to determine the effects.  For best results,
127 /// set the lacunarity to a number between 1.5 and 3.5.
128 ///
129 /// <b>References &amp; acknowledgments</b>
130 ///
131 /// <a href=http://www.noisemachine.com/talk1/>The Noise Machine</a> -
132 /// From the master, Ken Perlin himself.  This page contains a
133 /// presentation that describes Perlin noise and some of its variants.
134 /// He won an Oscar for creating the Perlin noise algorithm!
135 ///
136 /// <a
137 /// href=http://freespace.virgin.net/hugo.elias/models/m_perlin.htm>
138 /// Perlin Noise</a> - Hugo Elias's webpage contains a very good
139 /// description of Perlin noise and describes its many applications.  This
140 /// page gave me the inspiration to create libnoise in the first place.
141 /// Now that I know how to generate Perlin noise, I will never again use
142 /// cheesy subdivision algorithms to create terrain (unless I absolutely
143 /// need the speed.)
144 ///
145 /// <a
146 /// href=http://www.robo-murito.net/code/perlin-noise-math-faq.html>The
147 /// Perlin noise math FAQ</a> - A good page that describes Perlin noise in
148 /// plain English with only a minor amount of math.  During development of
149 /// libnoise, I noticed that my coherent-noise function generated terrain
150 /// with some "regularity" to the terrain features.  This page describes a
151 /// better coherent-noise function called <i>gradient noise</i>.  This
152 /// version of module::Perlin uses gradient coherent noise to
153 /// generate Perlin noise.
154 class Perlin : Mod
155 {
156 
157   public:
158 
159     /// Constructor.
160     ///
161     /// The default frequency is set to
162     /// module::DEFAULT_PERLIN_FREQUENCY.
163     ///
164     /// The default lacunarity is set to
165     /// module::DEFAULT_PERLIN_LACUNARITY.
166     ///
167     /// The default number of octaves is set to
168     /// module::DEFAULT_PERLIN_OCTAVE_COUNT.
169     ///
170     /// The default persistence value is set to
171     /// module::DEFAULT_PERLIN_PERSISTENCE.
172     ///
173     /// The default seed value is set to
174     /// module::DEFAULT_PERLIN_SEED.
175     this()
176     {
177         super(this.GetSourceModCount());
178         m_frequency = DEFAULT_PERLIN_FREQUENCY;
179         m_lacunarity = DEFAULT_PERLIN_LACUNARITY;
180         m_noiseQuality = DEFAULT_PERLIN_QUALITY;
181         m_octaveCount = DEFAULT_PERLIN_OCTAVE_COUNT;
182         m_persistence = DEFAULT_PERLIN_PERSISTENCE;
183         m_seed = DEFAULT_PERLIN_SEED;
184     }
185 
186     /// Returns the frequency of the first octave.
187     ///
188     /// @returns The frequency of the first octave.
189     double GetFrequency () const
190     {
191       return m_frequency;
192     }
193 
194     /// Returns the lacunarity of the Perlin noise.
195     ///
196     /// @returns The lacunarity of the Perlin noise.
197     /// 
198     /// The lacunarity is the frequency multiplier between successive
199     /// octaves.
200     double GetLacunarity () const
201     {
202       return m_lacunarity;
203     }
204 
205     /// Returns the quality of the Perlin noise.
206     ///
207     /// @returns The quality of the Perlin noise.
208     ///
209     /// See NoiseQuality for definitions of the various
210     /// coherent-noise qualities.
211     NoiseQuality GetNoiseQuality () const
212     {
213       return m_noiseQuality;
214     }
215 
216     /// Returns the number of octaves that generate the Perlin noise.
217     ///
218     /// @returns The number of octaves that generate the Perlin noise.
219     ///
220     /// The number of octaves controls the amount of detail in the Perlin
221     /// noise.
222     int GetOctaveCount () const
223     {
224       return m_octaveCount;
225     }
226 
227     /// Returns the persistence value of the Perlin noise.
228     ///
229     /// @returns The persistence value of the Perlin noise.
230     ///
231     /// The persistence value controls the roughness of the Perlin noise.
232     double GetPersistence () const
233     {
234       return m_persistence;
235     }
236 
237     /// Returns the seed value used by the Perlin-noise function.
238     ///
239     /// @returns The seed value.
240     int GetSeed () const
241     {
242       return m_seed;
243     }
244 
245     override int GetSourceModCount () const
246     {
247       return 0;
248     }
249 
250     override double GetValue (double x, double y, double z) const
251     {
252       double value = 0.0;
253       double signal = 0.0;
254       double curPersistence = 1.0;
255       double nx, ny, nz;
256       int seed;
257 
258       x *= m_frequency;
259       y *= m_frequency;
260       z *= m_frequency;
261 
262       for (int curOctave = 0; curOctave < m_octaveCount; curOctave++) {
263 
264         // Make sure that these floating-point values have the same range as a 32-
265         // bit integer so that we can pass them to the coherent-noise functions.
266         nx = MakeInt32Range (x);
267         ny = MakeInt32Range (y);
268         nz = MakeInt32Range (z);
269 
270         // Get the coherent-noise value from the input value and add it to the
271         // final result.
272         seed = (m_seed + curOctave) & 0xffffffff;
273         signal = GradientCoherentNoise3D (nx, ny, nz, seed, m_noiseQuality);
274         value += signal * curPersistence;
275 
276         // Prepare the next octave.
277         x *= m_lacunarity;
278         y *= m_lacunarity;
279         z *= m_lacunarity;
280         curPersistence *= m_persistence;
281       }
282 
283       return value;
284     }
285 
286     /// Sets the frequency of the first octave.
287     ///
288     /// @param frequency The frequency of the first octave.
289     void SetFrequency (double frequency)
290     {
291       m_frequency = frequency;
292     }
293 
294     /// Sets the lacunarity of the Perlin noise.
295     ///
296     /// @param lacunarity The lacunarity of the Perlin noise.
297     /// 
298     /// The lacunarity is the frequency multiplier between successive
299     /// octaves.
300     ///
301     /// For best results, set the lacunarity to a number between 1.5 and
302     /// 3.5.
303     void SetLacunarity (double lacunarity)
304     {
305       m_lacunarity = lacunarity;
306     }
307 
308     /// Sets the quality of the Perlin noise.
309     ///
310     /// @param noiseQuality The quality of the Perlin noise.
311     ///
312     /// See NoiseQuality for definitions of the various
313     /// coherent-noise qualities.
314     void SetNoiseQuality (NoiseQuality noiseQuality)
315     {
316       m_noiseQuality = noiseQuality;
317     }
318 
319     /// Sets the number of octaves that generate the Perlin noise.
320     ///
321     /// @param octaveCount The number of octaves that generate the Perlin
322     /// noise.
323     ///
324     /// @pre The number of octaves ranges from 1 to
325     /// module::PERLIN_MAX_OCTAVE.
326     ///
327     /// @throw new ExceptionInvalidParam An invalid parameter was
328     /// specified; see the preconditions for more information.
329     ///
330     /// The number of octaves controls the amount of detail in the Perlin
331     /// noise.
332     ///
333     /// The larger the number of octaves, the more time required to
334     /// calculate the Perlin-noise value.
335     void SetOctaveCount (int octaveCount)
336     {
337       if (octaveCount < 1 || octaveCount > PERLIN_MAX_OCTAVE) {
338         throw new ExceptionInvalidParam ();
339       }
340       m_octaveCount = octaveCount;
341     }
342 
343     /// Sets the persistence value of the Perlin noise.
344     ///
345     /// @param persistence The persistence value of the Perlin noise.
346     ///
347     /// The persistence value controls the roughness of the Perlin noise.
348     ///
349     /// For best results, set the persistence to a number between 0.0 and
350     /// 1.0.
351     void SetPersistence (double persistence)
352     {
353       m_persistence = persistence;
354     }
355 
356     /// Sets the seed value used by the Perlin-noise function.
357     ///
358     /// @param seed The seed value.
359     void SetSeed (int seed)
360     {
361       m_seed = seed;
362     }
363 
364   protected:
365 
366     /// Frequency of the first octave.
367     double m_frequency;
368 
369     /// Frequency multiplier between successive octaves.
370     double m_lacunarity;
371 
372     /// Quality of the Perlin noise.
373     NoiseQuality m_noiseQuality;
374 
375     /// Total number of octaves that generate the Perlin noise.
376     int m_octaveCount;
377 
378     /// Persistence of the Perlin noise.
379     double m_persistence;
380 
381     /// Seed value used by the Perlin-noise function.
382     int m_seed;
383 
384 }
385 
386 /// @}
387 
388 /// @}
389 
390 /// @}