Minetest  5.4.0
noise.h
Go to the documentation of this file.
1 /*
2  * Minetest
3  * Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
4  * Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are
8  * permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice, this list of
10  * conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #pragma once
27 
28 #include "irr_v3d.h"
29 #include "exceptions.h"
30 #include "util/string.h"
31 
32 #if defined(RANDOM_MIN)
33 #undef RANDOM_MIN
34 #endif
35 #if defined(RANDOM_MAX)
36 #undef RANDOM_MAX
37 #endif
38 
40 
41 // Note: this class is not polymorphic so that its high level of
42 // optimizability may be preserved in the common use case
43 class PseudoRandom {
44 public:
45  const static u32 RANDOM_RANGE = 32767;
46 
47  inline PseudoRandom(int seed=0):
48  m_next(seed)
49  {
50  }
51 
52  inline void seed(int seed)
53  {
54  m_next = seed;
55  }
56 
57  inline int next()
58  {
59  m_next = m_next * 1103515245 + 12345;
60  return (unsigned)(m_next / 65536) % (RANDOM_RANGE + 1);
61  }
62 
63  inline int range(int min, int max)
64  {
65  if (max < min)
66  throw PrngException("Invalid range (max < min)");
67  /*
68  Here, we ensure the range is not too large relative to RANDOM_MAX,
69  as otherwise the effects of bias would become noticable. Unlike
70  PcgRandom, we cannot modify this RNG's range as it would change the
71  output of this RNG for reverse compatibility.
72  */
73  if ((u32)(max - min) > (RANDOM_RANGE + 1) / 10)
74  throw PrngException("Range too large");
75 
76  return (next() % (max - min + 1)) + min;
77  }
78 
79 private:
80  int m_next;
81 };
82 
83 class PcgRandom {
84 public:
85  const static s32 RANDOM_MIN = -0x7fffffff - 1;
86  const static s32 RANDOM_MAX = 0x7fffffff;
87  const static u32 RANDOM_RANGE = 0xffffffff;
88 
89  PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL);
90  void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL);
91  u32 next();
92  u32 range(u32 bound);
93  s32 range(s32 min, s32 max);
94  void bytes(void *out, size_t len);
95  s32 randNormalDist(s32 min, s32 max, int num_trials=6);
96 
97 private:
98  u64 m_state;
99  u64 m_inc;
100 };
101 
102 #define NOISE_FLAG_DEFAULTS 0x01
103 #define NOISE_FLAG_EASED 0x02
104 #define NOISE_FLAG_ABSVALUE 0x04
105 
107 #define NOISE_FLAG_POINTBUFFER 0x08
108 #define NOISE_FLAG_SIMPLEX 0x10
109 
110 struct NoiseParams {
111  float offset = 0.0f;
112  float scale = 1.0f;
113  v3f spread = v3f(250, 250, 250);
114  s32 seed = 12345;
115  u16 octaves = 3;
116  float persist = 0.6f;
117  float lacunarity = 2.0f;
119 
120  NoiseParams() = default;
121 
122  NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_,
123  u16 octaves_, float persist_, float lacunarity_,
124  u32 flags_=NOISE_FLAG_DEFAULTS)
125  {
126  offset = offset_;
127  scale = scale_;
128  spread = spread_;
129  seed = seed_;
130  octaves = octaves_;
131  persist = persist_;
132  lacunarity = lacunarity_;
133  flags = flags_;
134  }
135 };
136 
137 class Noise {
138 public:
140  s32 seed;
141  u32 sx;
142  u32 sy;
143  u32 sz;
144  float *noise_buf = nullptr;
145  float *gradient_buf = nullptr;
146  float *persist_buf = nullptr;
147  float *result = nullptr;
148 
149  Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
150  ~Noise();
151 
152  void setSize(u32 sx, u32 sy, u32 sz=1);
153  void setSpreadFactor(v3f spread);
154  void setOctaves(int octaves);
155 
156  void gradientMap2D(
157  float x, float y,
158  float step_x, float step_y,
159  s32 seed);
160  void gradientMap3D(
161  float x, float y, float z,
162  float step_x, float step_y, float step_z,
163  s32 seed);
164 
165  float *perlinMap2D(float x, float y, float *persistence_map=NULL);
166  float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL);
167 
168  inline float *perlinMap2D_PO(float x, float xoff, float y, float yoff,
169  float *persistence_map=NULL)
170  {
171  return perlinMap2D(
172  x + xoff * np.spread.X,
173  y + yoff * np.spread.Y,
174  persistence_map);
175  }
176 
177  inline float *perlinMap3D_PO(float x, float xoff, float y, float yoff,
178  float z, float zoff, float *persistence_map=NULL)
179  {
180  return perlinMap3D(
181  x + xoff * np.spread.X,
182  y + yoff * np.spread.Y,
183  z + zoff * np.spread.Z,
184  persistence_map);
185  }
186 
187 private:
188  void allocBuffers();
189  void resizeNoiseBuf(bool is3d);
190  void updateResults(float g, float *gmap, const float *persistence_map,
191  size_t bufsize);
192 
193 };
194 
195 float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
196 float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
197 
198 inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
199  float y, float yoff, s32 seed)
200 {
201  return NoisePerlin2D(np,
202  x + xoff * np->spread.X,
203  y + yoff * np->spread.Y,
204  seed);
205 }
206 
207 inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff,
208  float y, float yoff, float z, float zoff, s32 seed)
209 {
210  return NoisePerlin3D(np,
211  x + xoff * np->spread.X,
212  y + yoff * np->spread.Y,
213  z + zoff * np->spread.Z,
214  seed);
215 }
216 
217 // Return value: -1 ... 1
218 float noise2d(int x, int y, s32 seed);
219 float noise3d(int x, int y, int z, s32 seed);
220 
221 float noise2d_gradient(float x, float y, s32 seed, bool eased=true);
222 float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false);
223 
224 float noise2d_perlin(float x, float y, s32 seed,
225  int octaves, float persistence, bool eased=true);
226 
227 float noise2d_perlin_abs(float x, float y, s32 seed,
228  int octaves, float persistence, bool eased=true);
229 
230 float noise3d_perlin(float x, float y, float z, s32 seed,
231  int octaves, float persistence, bool eased=false);
232 
233 float noise3d_perlin_abs(float x, float y, float z, s32 seed,
234  int octaves, float persistence, bool eased=false);
235 
236 inline float easeCurve(float t)
237 {
238  return t * t * t * (t * (6.f * t - 15.f) + 10.f);
239 }
240 
241 float contour(float v);
Definition: noise.h:137
u32 sz
Definition: noise.h:143
float * perlinMap3D(float x, float y, float z, float *persistence_map=NULL)
Definition: noise.cpp:746
void setSize(u32 sx, u32 sy, u32 sz=1)
Definition: noise.cpp:473
s32 seed
Definition: noise.h:140
float * perlinMap2D(float x, float y, float *persistence_map=NULL)
Definition: noise.cpp:709
float * result
Definition: noise.h:147
float * persist_buf
Definition: noise.h:146
void gradientMap3D(float x, float y, float z, float step_x, float step_y, float step_z, s32 seed)
Definition: noise.cpp:618
float * perlinMap2D_PO(float x, float xoff, float y, float yoff, float *persistence_map=NULL)
Definition: noise.h:168
Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1)
Definition: noise.cpp:425
float * gradient_buf
Definition: noise.h:145
void setSpreadFactor(v3f spread)
Definition: noise.cpp:483
u32 sx
Definition: noise.h:141
~Noise()
Definition: noise.cpp:437
void setOctaves(int octaves)
Definition: noise.cpp:491
NoiseParams np
Definition: noise.h:139
float * perlinMap3D_PO(float x, float xoff, float y, float yoff, float z, float zoff, float *persistence_map=NULL)
Definition: noise.h:177
void gradientMap2D(float x, float y, float step_x, float step_y, s32 seed)
Definition: noise.cpp:554
float * noise_buf
Definition: noise.h:144
u32 sy
Definition: noise.h:142
void allocBuffers()
Definition: noise.cpp:446
void updateResults(float g, float *gmap, const float *persistence_map, size_t bufsize)
Definition: noise.cpp:784
void resizeNoiseBuf(bool is3d)
Definition: noise.cpp:499
Definition: noise.h:83
u64 m_state
Definition: noise.h:98
s32 randNormalDist(s32 min, s32 max, int num_trials=6)
Definition: noise.cpp:155
void seed(u64 state, u64 seq=0xda3e39cb94b95bdbULL)
Definition: noise.cpp:65
static const u32 RANDOM_RANGE
Definition: noise.h:87
u32 range(u32 bound)
Definition: noise.cpp:86
static const s32 RANDOM_MIN
Definition: noise.h:85
u32 next()
Definition: noise.cpp:75
PcgRandom(u64 state=0x853c49e6748fea9bULL, u64 seq=0xda3e39cb94b95bdbULL)
Definition: noise.cpp:60
void bytes(void *out, size_t len)
Definition: noise.cpp:135
u64 m_inc
Definition: noise.h:99
static const s32 RANDOM_MAX
Definition: noise.h:86
Definition: exceptions.h:90
Definition: noise.h:43
int range(int min, int max)
Definition: noise.h:63
int next()
Definition: noise.h:57
PseudoRandom(int seed=0)
Definition: noise.h:47
int m_next
Definition: noise.h:80
void seed(int seed)
Definition: noise.h:52
static const u32 RANDOM_RANGE
Definition: noise.h:45
core::vector3df v3f
Definition: irr_v3d.h:26
float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff, float y, float yoff, s32 seed)
Definition: noise.h:198
float contour(float v)
Definition: noise.cpp:360
float noise3d_perlin(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false)
Definition: noise.cpp:330
FlagDesc flagdesc_noiseparams[]
Definition: noise.cpp:49
float noise2d(int x, int y, s32 seed)
Definition: noise.cpp:165
float noise3d(int x, int y, int z, s32 seed)
Definition: noise.cpp:175
float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff, float y, float yoff, float z, float zoff, s32 seed)
Definition: noise.h:207
float noise2d_perlin_abs(float x, float y, s32 seed, int octaves, float persistence, bool eased=true)
Definition: noise.cpp:315
#define NOISE_FLAG_DEFAULTS
Definition: noise.h:102
float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false)
Definition: noise.cpp:265
float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
Definition: noise.cpp:372
float noise2d_gradient(float x, float y, s32 seed, bool eased=true)
Definition: noise.cpp:244
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
Definition: noise.cpp:398
float noise2d_perlin(float x, float y, s32 seed, int octaves, float persistence, bool eased=true)
Definition: noise.cpp:299
float easeCurve(float t)
Definition: noise.h:236
float noise3d_perlin_abs(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false)
Definition: noise.cpp:345
static std::random_device seed
Definition: serverenvironment.cpp:392
Definition: string.h:62
Definition: noise.h:110
NoiseParams()=default
v3f spread
Definition: noise.h:113
float persist
Definition: noise.h:116
float offset
Definition: noise.h:111
u16 octaves
Definition: noise.h:115
u32 flags
Definition: noise.h:118
float scale
Definition: noise.h:112
NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_, u16 octaves_, float persist_, float lacunarity_, u32 flags_=NOISE_FLAG_DEFAULTS)
Definition: noise.h:122
float lacunarity
Definition: noise.h:117
s32 seed
Definition: noise.h:114