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