5788a83203e3ba08230728676bbc56592ab8c9f0
[oweals/minetest.git] / src / noise.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <math.h>
22 #include "noise.h"
23 #include <iostream>
24 #include "debug.h"
25 #include "util/numeric.h"
26
27 #define NOISE_MAGIC_X    1619
28 #define NOISE_MAGIC_Y    31337
29 #define NOISE_MAGIC_Z    52591
30 #define NOISE_MAGIC_SEED 1013
31
32 float cos_lookup[16] = {
33         1.0,  0.9238,  0.7071,  0.3826, 0, -0.3826, -0.7071, -0.9238,
34         1.0, -0.9238, -0.7071, -0.3826, 0,  0.3826,  0.7071,  0.9238
35 };
36
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40
41 //noise poly:  p(n) = 60493n^3 + 19990303n + 137612589
42 float noise2d(int x, int y, int seed) {
43         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
44                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
45         n = (n >> 13) ^ n;
46         n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
47         return 1.f - (float)n / 0x40000000;
48 }
49
50
51 float noise3d(int x, int y, int z, int seed) {
52         int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
53                         + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
54         n = (n >> 13) ^ n;
55         n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
56         return 1.f - (float)n / 0x40000000;
57 }
58
59
60 float dotProduct(float vx, float vy, float wx, float wy) {
61         return vx * wx + vy * wy;
62 }
63
64
65 inline float linearInterpolation(float v0, float v1, float t) {
66     return v0 + (v1 - v0) * t;
67 }
68
69
70 float biLinearInterpolation(float v00, float v10,
71                                                         float v01, float v11,
72                                                         float x, float y) {
73     float tx = easeCurve(x);
74     float ty = easeCurve(y);
75     float u = linearInterpolation(v00, v10, tx);
76     float v = linearInterpolation(v01, v11, tx);
77     return linearInterpolation(u, v, ty);
78 }
79
80
81 float biLinearInterpolationNoEase(float x0y0, float x1y0,
82                                                                   float x0y1, float x1y1,
83                                                                   float x, float y) {
84     float u = linearInterpolation(x0y0, x1y0, x);
85     float v = linearInterpolation(x0y1, x1y1, x);
86     return linearInterpolation(u, v, y);
87 }
88
89
90 float triLinearInterpolation(
91                 float v000, float v100, float v010, float v110,
92                 float v001, float v101, float v011, float v111,
93                 float x, float y, float z) {
94         float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
95         float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
96         return linearInterpolation(u, v, z);
97 }
98
99
100 #if 0
101 float triLinearInterpolation(
102                 float v000, float v100, float v010, float v110,
103                 float v001, float v101, float v011, float v111,
104                 float x, float y, float z)
105 {
106         /*float tx = easeCurve(x);
107         float ty = easeCurve(y);
108         float tz = easeCurve(z);*/
109         float tx = x;
110         float ty = y;
111         float tz = z;
112         return(
113                 v000 * (1 - tx) * (1 - ty) * (1 - tz) +
114                 v100 * tx * (1 - ty) * (1 - tz) +
115                 v010 * (1 - tx) * ty * (1 - tz) +
116                 v110 * tx * ty * (1 - tz) +
117                 v001 * (1 - tx) * (1 - ty) * tz +
118                 v101 * tx * (1 - ty) * tz +
119                 v011 * (1 - tx) * ty * tz +
120                 v111 * tx * ty * tz
121         );
122 }
123 #endif
124
125
126 #if 0
127 float noise2d_gradient(float x, float y, int seed)
128 {
129         // Calculate the integer coordinates
130         int x0 = (x > 0.0 ? (int)x : (int)x - 1);
131         int y0 = (y > 0.0 ? (int)y : (int)y - 1);
132         // Calculate the remaining part of the coordinates
133         float xl = x - (float)x0;
134         float yl = y - (float)y0;
135         // Calculate random cosine lookup table indices for the integer corners.
136         // They are looked up as unit vector gradients from the lookup table.
137         int n00 = (int)((noise2d(x0, y0, seed)+1)*8);
138         int n10 = (int)((noise2d(x0+1, y0, seed)+1)*8);
139         int n01 = (int)((noise2d(x0, y0+1, seed)+1)*8);
140         int n11 = (int)((noise2d(x0+1, y0+1, seed)+1)*8);
141         // Make a dot product for the gradients and the positions, to get the values
142         float s = dotProduct(cos_lookup[n00], cos_lookup[(n00+12)%16], xl, yl);
143         float u = dotProduct(-cos_lookup[n10], cos_lookup[(n10+12)%16], 1.-xl, yl);
144         float v = dotProduct(cos_lookup[n01], -cos_lookup[(n01+12)%16], xl, 1.-yl);
145         float w = dotProduct(-cos_lookup[n11], -cos_lookup[(n11+12)%16], 1.-xl, 1.-yl);
146         // Interpolate between the values
147         return biLinearInterpolation(s,u,v,w,xl,yl);
148 }
149 #endif
150
151
152 float noise2d_gradient(float x, float y, int seed)
153 {
154         // Calculate the integer coordinates
155         int x0 = myfloor(x);
156         int y0 = myfloor(y);
157         // Calculate the remaining part of the coordinates
158         float xl = x - (float)x0;
159         float yl = y - (float)y0;
160         // Get values for corners of square
161         float v00 = noise2d(x0, y0, seed);
162         float v10 = noise2d(x0+1, y0, seed);
163         float v01 = noise2d(x0, y0+1, seed);
164         float v11 = noise2d(x0+1, y0+1, seed);
165         // Interpolate
166         return biLinearInterpolation(v00,v10,v01,v11,xl,yl);
167 }
168
169
170 float noise3d_gradient(float x, float y, float z, int seed)
171 {
172         // Calculate the integer coordinates
173         int x0 = myfloor(x);
174         int y0 = myfloor(y);
175         int z0 = myfloor(z);
176         // Calculate the remaining part of the coordinates
177         float xl = x - (float)x0;
178         float yl = y - (float)y0;
179         float zl = z - (float)z0;
180         // Get values for corners of cube
181         float v000 = noise3d(x0,     y0,     z0,     seed);
182         float v100 = noise3d(x0 + 1, y0,     z0,     seed);
183         float v010 = noise3d(x0,     y0 + 1, z0,     seed);
184         float v110 = noise3d(x0 + 1, y0 + 1, z0,     seed);
185         float v001 = noise3d(x0,     y0,     z0 + 1, seed);
186         float v101 = noise3d(x0 + 1, y0,     z0 + 1, seed);
187         float v011 = noise3d(x0,     y0 + 1, z0 + 1, seed);
188         float v111 = noise3d(x0 + 1, y0 + 1, z0 + 1, seed);
189         // Interpolate
190         return triLinearInterpolation(v000, v100, v010, v110,
191                                                                   v001, v101, v011, v111,
192                                                                   xl, yl, zl);
193 }
194
195
196 float noise2d_perlin(float x, float y, int seed,
197                 int octaves, float persistence)
198 {
199         float a = 0;
200         float f = 1.0;
201         float g = 1.0;
202         for (int i = 0; i < octaves; i++)
203         {
204                 a += g * noise2d_gradient(x * f, y * f, seed + i);
205                 f *= 2.0;
206                 g *= persistence;
207         }
208         return a;
209 }
210
211
212 float noise2d_perlin_abs(float x, float y, int seed,
213                 int octaves, float persistence)
214 {
215         float a = 0;
216         float f = 1.0;
217         float g = 1.0;
218         for (int i = 0; i < octaves; i++)
219         {
220                 a += g * fabs(noise2d_gradient(x * f, y * f, seed + i));
221                 f *= 2.0;
222                 g *= persistence;
223         }
224         return a;
225 }
226
227
228 float noise3d_perlin(float x, float y, float z, int seed,
229                 int octaves, float persistence)
230 {
231         float a = 0;
232         float f = 1.0;
233         float g = 1.0;
234         for (int i = 0; i < octaves; i++)
235         {
236                 a += g * noise3d_gradient(x * f, y * f, z * f, seed + i);
237                 f *= 2.0;
238                 g *= persistence;
239         }
240         return a;
241 }
242
243
244 float noise3d_perlin_abs(float x, float y, float z, int seed,
245                 int octaves, float persistence)
246 {
247         float a = 0;
248         float f = 1.0;
249         float g = 1.0;
250         for (int i = 0; i < octaves; i++)
251         {
252                 a += g * fabs(noise3d_gradient(x * f, y * f, z * f, seed + i));
253                 f *= 2.0;
254                 g *= persistence;
255         }
256         return a;
257 }
258
259
260 // -1->0, 0->1, 1->0
261 float contour(float v)
262 {
263         v = fabs(v);
264         if(v >= 1.0)
265                 return 0.0;
266         return (1.0-v);
267 }
268
269
270 ///////////////////////// [ New perlin stuff ] ////////////////////////////
271
272
273 Noise::Noise(NoiseParams *np, int seed, int sx, int sy) {
274         init(np, seed, sx, sy, 1);
275 }
276
277
278 Noise::Noise(NoiseParams *np, int seed, int sx, int sy, int sz) {
279         init(np, seed, sx, sy, sz);
280 }
281
282
283 void Noise::init(NoiseParams *np, int seed, int sx, int sy, int sz) {
284         this->np   = np;
285         this->seed = seed;
286         this->sx   = sx;
287         this->sy   = sy;
288         this->sz   = sz;
289
290         this->noisebuf = NULL;
291         resizeNoiseBuf(sz > 1);
292
293         this->buf    = new float[sx * sy * sz];
294         this->result = new float[sx * sy * sz];
295 }
296
297
298 Noise::~Noise() {
299         delete[] buf;
300         delete[] result;
301         delete[] noisebuf;
302 }
303
304
305 void Noise::setSize(int sx, int sy) {
306         setSize(sx, sy, 1);
307 }
308
309
310 void Noise::setSize(int sx, int sy, int sz) {
311         this->sx = sx;
312         this->sy = sy;
313         this->sz = sz;
314
315         this->noisebuf = NULL;
316         resizeNoiseBuf(sz > 1);
317
318         delete[] buf;
319         delete[] result;
320         this->buf    = new float[sx * sy * sz];
321         this->result = new float[sx * sy * sz];
322 }
323
324
325 void Noise::setSpreadFactor(v3f spread) {
326         this->np->spread = spread;
327
328         resizeNoiseBuf(sz > 1);
329 }
330
331
332 void Noise::setOctaves(int octaves) {
333         this->np->octaves = octaves;
334
335         resizeNoiseBuf(sz > 1);
336 }
337
338
339 void Noise::resizeNoiseBuf(bool is3d) {
340         int nlx, nly, nlz;
341         float ofactor;
342
343         //maximum possible spread value factor
344         ofactor = (float)(1 << (np->octaves - 1));
345
346         //noise lattice point count
347         //(int)(sz * spread * ofactor) is # of lattice points crossed due to length
348         // + 2 for the two initial endpoints
349         // + 1 for potentially crossing a boundary due to offset
350         nlx = (int)(sx * ofactor / np->spread.X) + 3;
351         nly = (int)(sy * ofactor / np->spread.Y) + 3;
352         nlz = is3d ? (int)(sz * ofactor / np->spread.Z) + 3 : 1;
353
354         if (noisebuf)
355                 delete[] noisebuf;
356         noisebuf = new float[nlx * nly * nlz];
357 }
358
359
360 /*
361  * NB:  This algorithm is not optimal in terms of space complexity.  The entire
362  * integer lattice of noise points could be done as 2 lines instead, and for 3D,
363  * 2 lines + 2 planes.
364  * However, this would require the noise calls to be interposed with the
365  * interpolation loops, which may trash the icache, leading to lower overall
366  * performance.
367  * Another optimization that could save half as many noise calls is to carry over
368  * values from the previous noise lattice as midpoints in the new lattice for the
369  * next octave.
370  */
371 #define idx(x, y) ((y) * nlx + (x))
372 void Noise::gradientMap2D(float x, float y, float step_x, float step_y, int seed) {
373         float v00, v01, v10, v11, u, v, orig_u;
374         int index, i, j, x0, y0, noisex, noisey;
375         int nlx, nly;
376
377         x0 = floor(x);
378         y0 = floor(y);
379         u = x - (float)x0;
380         v = y - (float)y0;
381         orig_u = u;
382
383         //calculate noise point lattice
384         nlx = (int)(u + sx * step_x) + 2;
385         nly = (int)(v + sy * step_y) + 2;
386         index = 0;
387         for (j = 0; j != nly; j++)
388                 for (i = 0; i != nlx; i++)
389                         noisebuf[index++] = noise2d(x0 + i, y0 + j, seed);
390
391         //calculate interpolations
392         index  = 0;
393         noisey = 0;
394         for (j = 0; j != sy; j++) {
395                 v00 = noisebuf[idx(0, noisey)];
396                 v10 = noisebuf[idx(1, noisey)];
397                 v01 = noisebuf[idx(0, noisey + 1)];
398                 v11 = noisebuf[idx(1, noisey + 1)];
399
400                 u = orig_u;
401                 noisex = 0;
402                 for (i = 0; i != sx; i++) {
403                         buf[index++] = biLinearInterpolation(v00, v10, v01, v11, u, v);
404                         u += step_x;
405                         if (u >= 1.0) {
406                                 u -= 1.0;
407                                 noisex++;
408                                 v00 = v10;
409                                 v01 = v11;
410                                 v10 = noisebuf[idx(noisex + 1, noisey)];
411                                 v11 = noisebuf[idx(noisex + 1, noisey + 1)];
412                         }
413                 }
414
415                 v += step_y;
416                 if (v >= 1.0) {
417                         v -= 1.0;
418                         noisey++;
419                 }
420         }
421 }
422 #undef idx
423
424
425 #define idx(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
426 void Noise::gradientMap3D(float x, float y, float z,
427                                                   float step_x, float step_y, float step_z,
428                                                   int seed) {
429         float v000, v010, v100, v110;
430         float v001, v011, v101, v111;
431         float u, v, w, orig_u, orig_v;
432         int index, i, j, k, x0, y0, z0, noisex, noisey, noisez;
433         int nlx, nly, nlz;
434
435         x0 = floor(x);
436         y0 = floor(y);
437         z0 = floor(z);
438         u = x - (float)x0;
439         v = y - (float)y0;
440         w = z - (float)z0;
441         orig_u = u;
442         orig_v = v;
443
444         //calculate noise point lattice
445         nlx = (int)(u + sx * step_x) + 2;
446         nly = (int)(v + sy * step_y) + 2;
447         nlz = (int)(w + sz * step_z) + 2;
448         index = 0;
449         for (k = 0; k != nlz; k++)
450                 for (j = 0; j != nly; j++)
451                         for (i = 0; i != nlx; i++)
452                                 noisebuf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
453
454         //calculate interpolations
455         index  = 0;
456         noisey = 0;
457         noisez = 0;
458         for (k = 0; k != sz; k++) {
459                 v = orig_v;
460                 noisey = 0;
461                 for (j = 0; j != sy; j++) {
462                         v000 = noisebuf[idx(0, noisey,     noisez)];
463                         v100 = noisebuf[idx(1, noisey,     noisez)];
464                         v010 = noisebuf[idx(0, noisey + 1, noisez)];
465                         v110 = noisebuf[idx(1, noisey + 1, noisez)];
466                         v001 = noisebuf[idx(0, noisey,     noisez + 1)];
467                         v101 = noisebuf[idx(1, noisey,     noisez + 1)];
468                         v011 = noisebuf[idx(0, noisey + 1, noisez + 1)];
469                         v111 = noisebuf[idx(1, noisey + 1, noisez + 1)];
470
471                         u = orig_u;
472                         noisex = 0;
473                         for (i = 0; i != sx; i++) {
474                                 buf[index++] = triLinearInterpolation(
475                                                                         v000, v100, v010, v110,
476                                                                         v001, v101, v011, v111,
477                                                                         u, v, w);
478                                 u += step_x;
479                                 if (u >= 1.0) {
480                                         u -= 1.0;
481                                         noisex++;
482                                         v000 = v100;
483                                         v010 = v110;
484                                         v100 = noisebuf[idx(noisex + 1, noisey,     noisez)];
485                                         v110 = noisebuf[idx(noisex + 1, noisey + 1, noisez)];
486                                         v001 = v101;
487                                         v011 = v111;
488                                         v101 = noisebuf[idx(noisex + 1, noisey,     noisez + 1)];
489                                         v111 = noisebuf[idx(noisex + 1, noisey + 1, noisez + 1)];
490                                 }
491                         }
492
493                         v += step_y;
494                         if (v >= 1.0) {
495                                 v -= 1.0;
496                                 noisey++;
497                         }
498                 }
499
500                 w += step_z;
501                 if (w >= 1.0) {
502                         w -= 1.0;
503                         noisez++;
504                 }
505         }
506 }
507 #undef idx
508
509
510 float *Noise::perlinMap2D(float x, float y) {
511         float f = 1.0, g = 1.0;
512         int i, j, index, oct;
513
514         x /= np->spread.X;
515         y /= np->spread.Y;
516
517         memset(result, 0, sizeof(float) * sx * sy);
518
519         for (oct = 0; oct < np->octaves; oct++) {
520                 gradientMap2D(x * f, y * f,
521                         f / np->spread.X, f / np->spread.Y,
522                         seed + np->seed + oct);
523
524                 index = 0;
525                 for (j = 0; j != sy; j++) {
526                         for (i = 0; i != sx; i++) {
527                                 result[index] += g * buf[index];
528                                 index++;
529                         }
530                 }
531
532                 f *= 2.0;
533                 g *= np->persist;
534         }
535
536         return result;
537 }
538
539
540 float *Noise::perlinMap2DModulated(float x, float y, float *persist_map) {
541         float f = 1.0;
542         int i, j, index, oct;
543
544         x /= np->spread.X;
545         y /= np->spread.Y;
546
547         memset(result, 0, sizeof(float) * sx * sy);
548         
549         float *g = new float[sx * sy];
550         for (index = 0; index != sx * sy; index++)
551                 g[index] = 1.0;
552
553         for (oct = 0; oct < np->octaves; oct++) {
554                 gradientMap2D(x * f, y * f,
555                         f / np->spread.X, f / np->spread.Y,
556                         seed + np->seed + oct);
557
558                 index = 0;
559                 for (j = 0; j != sy; j++) {
560                         for (i = 0; i != sx; i++) {
561                                 result[index] += g[index] * buf[index];
562                                 g[index] *= persist_map[index];
563                                 index++;
564                         }
565                 }
566
567                 f *= 2.0;
568         }
569         
570         delete[] g;
571         return result;
572 }
573
574
575 float *Noise::perlinMap3D(float x, float y, float z) {
576         float f = 1.0, g = 1.0;
577         int i, j, k, index, oct;
578
579         x /= np->spread.X;
580         y /= np->spread.Y;
581         z /= np->spread.Z;
582
583         memset(result, 0, sizeof(float) * sx * sy * sz);
584
585         for (oct = 0; oct < np->octaves; oct++) {
586                 gradientMap3D(x * f, y * f, z * f,
587                         f / np->spread.X, f / np->spread.Y, f / np->spread.Z,
588                         seed + np->seed + oct);
589
590                 index = 0;
591                 for (k = 0; k != sz; k++) {
592                         for (j = 0; j != sy; j++) {
593                                 for (i = 0; i != sx; i++) {
594                                         result[index] += g * buf[index];
595                                         index++;
596                                 }
597                         }
598                 }
599
600                 f *= 2.0;
601                 g *= np->persist;
602         }
603
604         return result;
605 }
606
607
608 void Noise::transformNoiseMap() {
609         int i = 0;
610         for (int z = 0; z != sz; z++) {
611                 for (int y = 0; y != sy; y++) {
612                         for (int x = 0; x != sx; x++) {
613                                 result[i] = result[i] * np->scale + np->offset;
614                                 i++;
615                         }
616                 }
617         }
618 }