Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Noises.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_NOISES_H
22 #define GF_NOISES_H
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <array>
27 #include <functional>
28 #include <vector>
29 
30 #include "Math.h"
31 #include "Noise.h"
32 #include "Portability.h"
33 #include "Vector.h"
34 
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
41  class Random;
42 
43  /**
44  * @ingroup core
45  * @brief Value 2D noise
46  *
47  * [Value noise](https://en.wikipedia.org/wiki/Value_noise) is a
48  * lattice-based noise based on values.
49  */
50  class GF_API ValueNoise2D : public Noise2D {
51  public:
52  /**
53  * @brief Constructor
54  *
55  * @param random A random engine
56  * @param step A step
57  *
58  * @sa gf::Step
59  */
60  ValueNoise2D(Random& random, Step<double> step);
61 
62  virtual double getValue(double x, double y) override;
63 
64  private:
65  Step<double> m_step;
66  std::array<uint8_t, 256> m_perm;
67  std::array<double, 256> m_values;
68 
69  double at(uint8_t i, uint8_t j) const;
70  };
71 
72 
73  /**
74  * @ingroup core
75  * @brief Gradient 2D noise
76  *
77  * [Gradient noise](https://en.wikipedia.org/wiki/Gradient_noise) is a
78  * lattice-based noise based on gradients.
79  */
80  class GF_API GradientNoise2D : public Noise2D {
81  public:
82  /**
83  * @brief Constructor
84  *
85  * @param random A random engine
86  * @param step A step
87  *
88  * @sa gf::Step
89  */
90  GradientNoise2D(Random& random, Step<double> step);
91 
92  virtual double getValue(double x, double y) override;
93 
94  private:
95  Step<double> m_step;
96  std::array<uint8_t, 256> m_perm;
97  std::array<Vector2d, 256> m_gradients2D;
98 
99  const Vector2d& at(uint8_t i, uint8_t j) const;
100  };
101 
102  /**
103  * @ingroup core
104  * @brief Gradient 3D noise
105  *
106  * [Gradient noise](https://en.wikipedia.org/wiki/Gradient_noise) is a
107  * lattice-based noise based on gradients.
108  */
109  class GF_API GradientNoise3D : public Noise3D {
110  public:
111  /**
112  * @brief Constructor
113  *
114  * @param random A random engine
115  * @param step A step
116  *
117  * @sa gf::Step
118  */
119  GradientNoise3D(Random& random, Step<double> step);
120 
121  virtual double getValue(double x, double y, double z) override;
122 
123  private:
124  Step<double> m_step;
125  std::array<uint8_t, 256> m_perm;
126  std::array<Vector3d, 256> m_gradients3D;
127 
128  const Vector3d& at(uint8_t i, uint8_t j, uint8_t k) const;
129  };
130 
131  /**
132  * @ingroup core
133  * @brief Better gradient 2D noise
134  *
135  * An implementation of the better gradient noise of Kensler et al.,
136  * especially the new hash function and filter kernel. This noise is
137  * slower than gradient noise but gives better results.
138  *
139  * @sa [Better Gradient Noise. A. Kensler, A. Knoll, P. Shirley. 2008](https://www.cs.utah.edu/~aek/research/noise.pdf)
140  */
142  public:
143  /**
144  * @brief Constructor
145  *
146  * @param random A random engine
147  */
148  BetterGradientNoise2D(Random& random);
149 
150  virtual double getValue(double x, double y) override;
151 
152  private:
153  std::array<uint8_t, 256> m_permX;
154  std::array<uint8_t, 256> m_permY;
155  std::array<Vector2d, 256> m_gradients2D;
156 
157  const Vector2d& at(uint8_t i, uint8_t j) const;
158  };
159 
160  /**
161  * @ingroup core
162  * @brief Fractal 2D noise
163  *
164  * Fractal noise is based of fractional Brownian motion (fBm). It consists
165  * in adding several octaves of a basic noise at different amplitudes.
166  *
167  */
168  class GF_API FractalNoise2D : public Noise2D {
169  public:
170  /**
171  * @brief Constructor
172  *
173  * @param noise The basic noise function
174  * @param scale The scale factor
175  * @param octaves The number of octaves
176  * @param lacunarity The factor applied to frequency
177  * @param persistence The factor applied to amplitude
178  * @param dimension The contrast between the layers
179  */
180  FractalNoise2D(Noise2D& noise, double scale, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double
181  dimension = 1.0);
182 
183  virtual double getValue(double x, double y) override;
184 
185  private:
186  Noise2D& m_noise;
187  double m_scale;
188  std::size_t m_octaves;
189  double m_lacunarity;
190  double m_persistence;
191  double m_dimension;
192  };
193 
194 
195  /**
196  * @ingroup core
197  * @brief Fractal 3D noise
198  *
199  * Fractal noise is based of fractional Brownian motion (fBm). It consists
200  * in adding several octaves of a basic noise at different amplitudes.
201  *
202  */
203  class GF_API FractalNoise3D : public Noise3D {
204  public:
205  /**
206  * @brief Constructor
207  *
208  * @param noise The basic noise function
209  * @param scale The scale factor
210  * @param octaves The number of octaves
211  * @param lacunarity The factor applied to frequency
212  * @param persistence The factor applied to amplitude
213  * @param dimension The contrast between the layers
214  */
215  FractalNoise3D(Noise3D& noise, double scale, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0);
216 
217  virtual double getValue(double x, double y, double z) override;
218 
219  private:
220  Noise3D& m_noise;
221  double m_scale;
222  std::size_t m_octaves;
223  double m_lacunarity;
224  double m_persistence;
225  double m_dimension;
226  };
227 
228 
229  /**
230  * @ingroup core
231  * @brief Perlin 2D noise
232  *
233  * [Perlin noise](https://en.wikipedia.org/wiki/Perlin_noise) is the
234  * combination of a fractal noise and a gradient noise.
235  *
236  * @sa gf::GradientNoise2D, gf::FractalNoise2D
237  */
238  class GF_API PerlinNoise2D : public Noise2D {
239  public:
240  /**
241  * @brief Constructor
242  *
243  * @param random A random engine
244  * @param scale The scale factor
245  * @param octaves The number of octaves
246  */
247  PerlinNoise2D(Random& random, double scale, std::size_t octaves = 8);
248 
249  virtual double getValue(double x, double y) override;
250 
251  private:
252  GradientNoise2D m_gradient;
253  FractalNoise2D m_fractal;
254  };
255 
256  /**
257  * @ingroup core
258  * @brief Perlin 3D noise
259  *
260  * [Perlin noise](https://en.wikipedia.org/wiki/Perlin_noise) is the
261  * combination of a fractal noise and a gradient noise.
262  *
263  * @sa gf::GradientNoise3D, gf::FractalNoise3D
264  */
265  class GF_API PerlinNoise3D : public Noise3D {
266  public:
267  /**
268  * @brief Constructor
269  *
270  * @param random A random engine
271  * @param scale The scale factor
272  * @param octaves The number of octaves
273  */
274  PerlinNoise3D(Random& random, double scale, std::size_t octaves = 8);
275 
276  virtual double getValue(double x, double y, double z) override;
277 
278  private:
279  GradientNoise3D m_gradient;
280  FractalNoise3D m_fractal;
281  };
282 
283 
284  /**
285  * @ingroup core
286  * @brief Simplex 2D noise
287  *
288  * [Simplex noise](https://en.wikipedia.org/wiki/Simplex_noise) is a lattice
289  * noise based on gradients put on a simplex.
290  *
291  * This implementation is limited to 2D noise and is *not* submitted to the
292  * patent that covers simplex noise.
293  *
294  * @sa gf::GradientNoise2D
295  */
296  class GF_API SimplexNoise2D : public Noise2D {
297  public:
298  /**
299  * @brief Constructor
300  *
301  * @param random A random engine
302  */
303  SimplexNoise2D(Random& random);
304 
305  virtual double getValue(double x, double y) override;
306 
307  private:
308  std::array<uint8_t, 256> m_perm;
309 
310  const Vector2d& at(uint8_t i, uint8_t j) const;
311  };
312 
313 
314  /**
315  * @ingroup core
316  * @brief OpenSimplex 2D noise
317  *
318  * [OpenSimplex noise](https://en.wikipedia.org/wiki/OpenSimplex_noise) is a lattice
319  * noise very similar to simplex noise.
320  *
321  * @sa gf::SimplexNoise2D
322  */
324  public:
325  /**
326  * @brief Constructor
327  *
328  * @param random A random engine
329  */
330  OpenSimplexNoise2D(Random& random);
331 
332  virtual double getValue(double x, double y) override;
333 
334  private:
335  std::array<uint8_t, 256> m_perm;
336 
337  const Vector2d& at(uint8_t i, uint8_t j) const;
338  };
339 
340  /**
341  * @ingroup core
342  * @brief OpenSimplex3D noise
343  *
344  * [OpenSimplex noise](https://en.wikipedia.org/wiki/OpenSimplex_noise) is a lattice
345  * noise very similar to simplex noise.
346  *
347  * @sa gf::SimplexNoise
348  */
350  public:
351  /**
352  * @brief Constructor
353  *
354  * @param random A random engine
355  */
356  OpenSimplexNoise3D(Random& random);
357 
358  virtual double getValue(double x, double y, double z) override;
359 
360  private:
361  std::array<uint8_t, 256> m_perm;
362 
363  const Vector3d& at(uint8_t i, uint8_t j, uint8_t k) const;
364  };
365 
366  /**
367  * @ingroup core
368  * @brief Wavelet 3D noise
369  *
370  */
371  class GF_API WaveletNoise3D : public Noise3D {
372  public:
373  /**
374  * @brief Constructor
375  *
376  * @param random A random engine
377  * @param n Wavelet tile size
378  */
379  WaveletNoise3D(Random& random, std::ptrdiff_t n = 32);
380 
381  virtual double getValue(double x, double y, double z) override;
382 
383  private:
384  std::ptrdiff_t m_n;
385  std::vector<double> m_data;
386  };
387 
388  /**
389  * @ingroup core
390  * @brief Worley 2D noise
391  *
392  * [Worley noise](https://en.wikipedia.org/wiki/Worley_noise) is a point
393  * based noise. It is also known as Voronoi noise or cellular noise or
394  * simply cell noise.
395  */
396  class GF_API WorleyNoise2D : public Noise2D {
397  public:
398  /**
399  * @brief Constructor
400  *
401  * @param random A random engine
402  * @param count The number of points
403  * @param distance A distance function
404  * @param coeffs The coefficients for the noise
405  */
406  WorleyNoise2D(Random& random, std::size_t count, Distance2<double> distance, std::vector<double> coeffs);
407 
408  virtual double getValue(double x, double y) override;
409 
410  private:
411  std::size_t m_count;
412  Distance2<double> m_distance;
413  std::vector<double> m_coeffs;
414  std::vector<Vector2d> m_cells;
415  };
416 
417 
418  /**
419  * @ingroup core
420  * @brief Multi Fractal 2D noise
421  *
422  */
423  class GF_API Multifractal2D : public Noise2D {
424  public:
425  /**
426  * @brief Constructor
427  *
428  * @param noise The basic noise function
429  * @param scale The scale factor
430  * @param octaves The number of octaves
431  * @param lacunarity The factor applied to frequency
432  * @param persistence The factor applied to amplitude
433  * @param dimension The contrast between the layers
434  */
435  Multifractal2D(Noise2D& noise, double scale, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0);
436 
437  virtual double getValue(double x, double y) override;
438 
439  private:
440  Noise2D& m_noise;
441  double m_scale;
442  std::size_t m_octaves;
443  double m_lacunarity;
444  double m_persistence;
445  double m_dimension;
446  };
447 
448 
449  /**
450  * @ingroup core
451  * @brief Hetero Terrain 2D noise
452  *
453  */
454  class GF_API HeteroTerrain2D : public Noise2D {
455  public:
456  /**
457  * @brief Constructor
458  *
459  * @param noise The basic noise function
460  * @param scale The scale factor
461  * @param offset The offset
462  * @param octaves The number of octaves
463  * @param lacunarity The factor applied to frequency
464  * @param persistence The factor applied to amplitude
465  * @param dimension The contrast between the layers
466  */
467  HeteroTerrain2D(Noise2D& noise, double scale, double offset = 0.0, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0);
468 
469  virtual double getValue(double x, double y) override;
470 
471  private:
472  Noise2D& m_noise;
473  double m_scale;
474  double m_offset;
475  std::size_t m_octaves;
476  double m_lacunarity;
477  double m_persistence;
478  double m_dimension;
479  };
480 
481  /**
482  * @ingroup core
483  * @brief Hybrid Multifractal 2D noise
484  *
485  */
487  public:
488  /**
489  * @brief Constructor
490  *
491  * @param noise The basic noise function
492  * @param scale The scale factor
493  * @param offset The offset
494  * @param octaves The number of octaves
495  * @param lacunarity The factor applied to frequency
496  * @param persistence The factor applied to amplitude
497  * @param dimension The contrast between the layers
498  */
499  HybridMultifractal2D(Noise2D& noise, double scale, double offset = 0.0, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0);
500 
501  virtual double getValue(double x, double y) override;
502 
503  private:
504  Noise2D& m_noise;
505  double m_scale;
506  double m_offset;
507  std::size_t m_octaves;
508  double m_lacunarity;
509  double m_persistence;
510  double m_dimension;
511  };
512 
513  /**
514  * @ingroup core
515  * @brief Ridged Multifractal 2D noise
516  *
517  */
519  public:
520  /**
521  * @brief Constructor
522  *
523  * @param noise The basic noise function
524  * @param scale The scale factor
525  * @param offset The offset
526  * @param gain The gain
527  * @param octaves The number of octaves
528  * @param lacunarity The factor applied to frequency
529  * @param persistence The factor applied to amplitude
530  * @param dimension The contrast between the layers
531  */
532  RidgedMultifractal2D(Noise2D& noise, double scale, double offset = 1.0, double gain = 1.0, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0);
533 
534  virtual double getValue(double x, double y) override;
535 
536  private:
537  Noise2D& m_noise;
538  double m_scale;
539  double m_offset;
540  double m_gain;
541  std::size_t m_octaves;
542  double m_lacunarity;
543  double m_persistence;
544  double m_dimension;
545  };
546 
547 #ifndef DOXYGEN_SHOULD_SKIP_THIS
548 }
549 #endif
550 }
551 
552 #endif // GF_NOISES_H
A random engine.
Definition: Random.h:43
ValueNoise2D(Random &random, Step< double > step)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
RidgedMultifractal2D(Noise2D &noise, double scale, double offset=1.0, double gain=1.0, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
WorleyNoise2D(Random &random, std::size_t count, Distance2< double > distance, std::vector< double > coeffs)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
virtual double getValue(double x, double y) override
Take a 2D noise value.
virtual double getValue(double x, double y) override
Take a 2D noise value.
WaveletNoise3D(Random &random, std::ptrdiff_t n=32)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
Gradient 3D noise.
Definition: Noises.h:109
Gradient 2D noise.
Definition: Noises.h:80
FractalNoise3D(Noise3D &noise, double scale, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
HeteroTerrain2D(Noise2D &noise, double scale, double offset=0.0, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
2D A noise function
Definition: Noise.h:35
3D A noise function
Definition: Noise.h:71
virtual double getValue(double x, double y, double z) override
Take a 3D noise value.
Ridged Multifractal 2D noise.
Definition: Noises.h:518
OpenSimplex3D noise.
Definition: Noises.h:349
GradientNoise2D(Random &random, Step< double > step)
Constructor.
OpenSimplex 2D noise.
Definition: Noises.h:323
virtual double getValue(double x, double y) override
Take a 2D noise value.
virtual double getValue(double x, double y) override
Take a 2D noise value.
Worley 2D noise.
Definition: Noises.h:396
GradientNoise3D(Random &random, Step< double > step)
Constructor.
HybridMultifractal2D(Noise2D &noise, double scale, double offset=0.0, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
OpenSimplexNoise3D(Random &random)
Constructor.
PerlinNoise3D(Random &random, double scale, std::size_t octaves=8)
Constructor.
Definition: Action.h:34
PerlinNoise2D(Random &random, double scale, std::size_t octaves=8)
Constructor.
Perlin 2D noise.
Definition: Noises.h:238
Perlin 3D noise.
Definition: Noises.h:265
virtual double getValue(double x, double y, double z) override
Take a 3D noise value.
virtual double getValue(double x, double y, double z) override
Take a 3D noise value.
FractalNoise2D(Noise2D &noise, double scale, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
virtual double getValue(double x, double y, double z) override
Take a 3D noise value.
Fractal 3D noise.
Definition: Noises.h:203
virtual double getValue(double x, double y) override
Take a 2D noise value.
Fractal 2D noise.
Definition: Noises.h:168
OpenSimplexNoise2D(Random &random)
Constructor.
virtual double getValue(double x, double y) override
Take a 2D noise value.
Wavelet 3D noise.
Definition: Noises.h:371
Value 2D noise.
Definition: Noises.h:50
SimplexNoise2D(Random &random)
Constructor.
Hybrid Multifractal 2D noise.
Definition: Noises.h:486
Simplex 2D noise.
Definition: Noises.h:296
#define GF_API
Definition: Portability.h:35
virtual double getValue(double x, double y) override
Take a 2D noise value.
Multifractal2D(Noise2D &noise, double scale, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
Better gradient 2D noise.
Definition: Noises.h:141
Multi Fractal 2D noise.
Definition: Noises.h:423
virtual double getValue(double x, double y, double z) override
Take a 3D noise value.
BetterGradientNoise2D(Random &random)
Constructor.
Hetero Terrain 2D noise.
Definition: Noises.h:454