noise.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. #include <cmath>
  26. #include "noise.h"
  27. #include <iostream>
  28. #include <cstring> // memset
  29. #include "debug.h"
  30. #include "util/numeric.h"
  31. #include "util/string.h"
  32. #include "exceptions.h"
  33. #define NOISE_MAGIC_X 1619
  34. #define NOISE_MAGIC_Y 31337
  35. #define NOISE_MAGIC_Z 52591
  36. #define NOISE_MAGIC_SEED 1013
  37. typedef float (*Interp2dFxn)(
  38. float v00, float v10, float v01, float v11,
  39. float x, float y);
  40. typedef float (*Interp3dFxn)(
  41. float v000, float v100, float v010, float v110,
  42. float v001, float v101, float v011, float v111,
  43. float x, float y, float z);
  44. float cos_lookup[16] = {
  45. 1.0f, 0.9238f, 0.7071f, 0.3826f, .0f, -0.3826f, -0.7071f, -0.9238f,
  46. 1.0f, -0.9238f, -0.7071f, -0.3826f, .0f, 0.3826f, 0.7071f, 0.9238f
  47. };
  48. FlagDesc flagdesc_noiseparams[] = {
  49. {"defaults", NOISE_FLAG_DEFAULTS},
  50. {"eased", NOISE_FLAG_EASED},
  51. {"absvalue", NOISE_FLAG_ABSVALUE},
  52. {"pointbuffer", NOISE_FLAG_POINTBUFFER},
  53. {"simplex", NOISE_FLAG_SIMPLEX},
  54. {NULL, 0}
  55. };
  56. ///////////////////////////////////////////////////////////////////////////////
  57. PcgRandom::PcgRandom(u64 state, u64 seq)
  58. {
  59. seed(state, seq);
  60. }
  61. void PcgRandom::seed(u64 state, u64 seq)
  62. {
  63. m_state = 0U;
  64. m_inc = (seq << 1u) | 1u;
  65. next();
  66. m_state += state;
  67. next();
  68. }
  69. u32 PcgRandom::next()
  70. {
  71. u64 oldstate = m_state;
  72. m_state = oldstate * 6364136223846793005ULL + m_inc;
  73. u32 xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
  74. u32 rot = oldstate >> 59u;
  75. return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
  76. }
  77. u32 PcgRandom::range(u32 bound)
  78. {
  79. // If the bound is 0, we cover the whole RNG's range
  80. if (bound == 0)
  81. return next();
  82. /*
  83. This is an optimization of the expression:
  84. 0x100000000ull % bound
  85. since 64-bit modulo operations typically much slower than 32.
  86. */
  87. u32 threshold = -bound % bound;
  88. u32 r;
  89. /*
  90. If the bound is not a multiple of the RNG's range, it may cause bias,
  91. e.g. a RNG has a range from 0 to 3 and we take want a number 0 to 2.
  92. Using rand() % 3, the number 0 would be twice as likely to appear.
  93. With a very large RNG range, the effect becomes less prevalent but
  94. still present.
  95. This can be solved by modifying the range of the RNG to become a
  96. multiple of bound by dropping values above the a threshold.
  97. In our example, threshold == 4 % 3 == 1, so reject values < 1
  98. (that is, 0), thus making the range == 3 with no bias.
  99. This loop may look dangerous, but will always terminate due to the
  100. RNG's property of uniformity.
  101. */
  102. while ((r = next()) < threshold)
  103. ;
  104. return r % bound;
  105. }
  106. s32 PcgRandom::range(s32 min, s32 max)
  107. {
  108. if (max < min)
  109. throw PrngException("Invalid range (max < min)");
  110. // We have to cast to s64 because otherwise this could overflow,
  111. // and signed overflow is undefined behavior.
  112. u32 bound = (s64)max - (s64)min + 1;
  113. return range(bound) + min;
  114. }
  115. void PcgRandom::bytes(void *out, size_t len)
  116. {
  117. u8 *outb = (u8 *)out;
  118. int bytes_left = 0;
  119. u32 r;
  120. while (len--) {
  121. if (bytes_left == 0) {
  122. bytes_left = sizeof(u32);
  123. r = next();
  124. }
  125. *outb = r & 0xFF;
  126. outb++;
  127. bytes_left--;
  128. r >>= CHAR_BIT;
  129. }
  130. }
  131. s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials)
  132. {
  133. s32 accum = 0;
  134. for (int i = 0; i != num_trials; i++)
  135. accum += range(min, max);
  136. return myround((float)accum / num_trials);
  137. }
  138. ///////////////////////////////////////////////////////////////////////////////
  139. float noise2d(int x, int y, s32 seed)
  140. {
  141. unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y
  142. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  143. n = (n >> 13) ^ n;
  144. n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
  145. return 1.f - (float)(int)n / 0x40000000;
  146. }
  147. float noise3d(int x, int y, int z, s32 seed)
  148. {
  149. unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z
  150. + NOISE_MAGIC_SEED * seed) & 0x7fffffff;
  151. n = (n >> 13) ^ n;
  152. n = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
  153. return 1.f - (float)(int)n / 0x40000000;
  154. }
  155. inline float dotProduct(float vx, float vy, float wx, float wy)
  156. {
  157. return vx * wx + vy * wy;
  158. }
  159. inline float linearInterpolation(float v0, float v1, float t)
  160. {
  161. return v0 + (v1 - v0) * t;
  162. }
  163. inline float biLinearInterpolation(
  164. float v00, float v10,
  165. float v01, float v11,
  166. float x, float y)
  167. {
  168. float tx = easeCurve(x);
  169. float ty = easeCurve(y);
  170. float u = linearInterpolation(v00, v10, tx);
  171. float v = linearInterpolation(v01, v11, tx);
  172. return linearInterpolation(u, v, ty);
  173. }
  174. inline float biLinearInterpolationNoEase(
  175. float v00, float v10,
  176. float v01, float v11,
  177. float x, float y)
  178. {
  179. float u = linearInterpolation(v00, v10, x);
  180. float v = linearInterpolation(v01, v11, x);
  181. return linearInterpolation(u, v, y);
  182. }
  183. float triLinearInterpolation(
  184. float v000, float v100, float v010, float v110,
  185. float v001, float v101, float v011, float v111,
  186. float x, float y, float z)
  187. {
  188. float tx = easeCurve(x);
  189. float ty = easeCurve(y);
  190. float tz = easeCurve(z);
  191. float u = biLinearInterpolationNoEase(v000, v100, v010, v110, tx, ty);
  192. float v = biLinearInterpolationNoEase(v001, v101, v011, v111, tx, ty);
  193. return linearInterpolation(u, v, tz);
  194. }
  195. float triLinearInterpolationNoEase(
  196. float v000, float v100, float v010, float v110,
  197. float v001, float v101, float v011, float v111,
  198. float x, float y, float z)
  199. {
  200. float u = biLinearInterpolationNoEase(v000, v100, v010, v110, x, y);
  201. float v = biLinearInterpolationNoEase(v001, v101, v011, v111, x, y);
  202. return linearInterpolation(u, v, z);
  203. }
  204. float noise2d_gradient(float x, float y, s32 seed, bool eased)
  205. {
  206. // Calculate the integer coordinates
  207. int x0 = myfloor(x);
  208. int y0 = myfloor(y);
  209. // Calculate the remaining part of the coordinates
  210. float xl = x - (float)x0;
  211. float yl = y - (float)y0;
  212. // Get values for corners of square
  213. float v00 = noise2d(x0, y0, seed);
  214. float v10 = noise2d(x0+1, y0, seed);
  215. float v01 = noise2d(x0, y0+1, seed);
  216. float v11 = noise2d(x0+1, y0+1, seed);
  217. // Interpolate
  218. if (eased)
  219. return biLinearInterpolation(v00, v10, v01, v11, xl, yl);
  220. return biLinearInterpolationNoEase(v00, v10, v01, v11, xl, yl);
  221. }
  222. float noise3d_gradient(float x, float y, float z, s32 seed, bool eased)
  223. {
  224. // Calculate the integer coordinates
  225. int x0 = myfloor(x);
  226. int y0 = myfloor(y);
  227. int z0 = myfloor(z);
  228. // Calculate the remaining part of the coordinates
  229. float xl = x - (float)x0;
  230. float yl = y - (float)y0;
  231. float zl = z - (float)z0;
  232. // Get values for corners of cube
  233. float v000 = noise3d(x0, y0, z0, seed);
  234. float v100 = noise3d(x0 + 1, y0, z0, seed);
  235. float v010 = noise3d(x0, y0 + 1, z0, seed);
  236. float v110 = noise3d(x0 + 1, y0 + 1, z0, seed);
  237. float v001 = noise3d(x0, y0, z0 + 1, seed);
  238. float v101 = noise3d(x0 + 1, y0, z0 + 1, seed);
  239. float v011 = noise3d(x0, y0 + 1, z0 + 1, seed);
  240. float v111 = noise3d(x0 + 1, y0 + 1, z0 + 1, seed);
  241. // Interpolate
  242. if (eased) {
  243. return triLinearInterpolation(
  244. v000, v100, v010, v110,
  245. v001, v101, v011, v111,
  246. xl, yl, zl);
  247. }
  248. return triLinearInterpolationNoEase(
  249. v000, v100, v010, v110,
  250. v001, v101, v011, v111,
  251. xl, yl, zl);
  252. }
  253. float noise2d_perlin(float x, float y, s32 seed,
  254. int octaves, float persistence, bool eased)
  255. {
  256. float a = 0;
  257. float f = 1.0;
  258. float g = 1.0;
  259. for (int i = 0; i < octaves; i++)
  260. {
  261. a += g * noise2d_gradient(x * f, y * f, seed + i, eased);
  262. f *= 2.0;
  263. g *= persistence;
  264. }
  265. return a;
  266. }
  267. float noise2d_perlin_abs(float x, float y, s32 seed,
  268. int octaves, float persistence, bool eased)
  269. {
  270. float a = 0;
  271. float f = 1.0;
  272. float g = 1.0;
  273. for (int i = 0; i < octaves; i++) {
  274. a += g * std::fabs(noise2d_gradient(x * f, y * f, seed + i, eased));
  275. f *= 2.0;
  276. g *= persistence;
  277. }
  278. return a;
  279. }
  280. float noise3d_perlin(float x, float y, float z, s32 seed,
  281. int octaves, float persistence, bool eased)
  282. {
  283. float a = 0;
  284. float f = 1.0;
  285. float g = 1.0;
  286. for (int i = 0; i < octaves; i++) {
  287. a += g * noise3d_gradient(x * f, y * f, z * f, seed + i, eased);
  288. f *= 2.0;
  289. g *= persistence;
  290. }
  291. return a;
  292. }
  293. float noise3d_perlin_abs(float x, float y, float z, s32 seed,
  294. int octaves, float persistence, bool eased)
  295. {
  296. float a = 0;
  297. float f = 1.0;
  298. float g = 1.0;
  299. for (int i = 0; i < octaves; i++) {
  300. a += g * std::fabs(noise3d_gradient(x * f, y * f, z * f, seed + i, eased));
  301. f *= 2.0;
  302. g *= persistence;
  303. }
  304. return a;
  305. }
  306. float contour(float v)
  307. {
  308. v = std::fabs(v);
  309. if (v >= 1.0)
  310. return 0.0;
  311. return (1.0 - v);
  312. }
  313. ///////////////////////// [ New noise ] ////////////////////////////
  314. float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
  315. {
  316. float a = 0;
  317. float f = 1.0;
  318. float g = 1.0;
  319. x /= np->spread.X;
  320. y /= np->spread.Y;
  321. seed += np->seed;
  322. for (size_t i = 0; i < np->octaves; i++) {
  323. float noiseval = noise2d_gradient(x * f, y * f, seed + i,
  324. np->flags & (NOISE_FLAG_DEFAULTS | NOISE_FLAG_EASED));
  325. if (np->flags & NOISE_FLAG_ABSVALUE)
  326. noiseval = std::fabs(noiseval);
  327. a += g * noiseval;
  328. f *= np->lacunarity;
  329. g *= np->persist;
  330. }
  331. return np->offset + a * np->scale;
  332. }
  333. float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
  334. {
  335. float a = 0;
  336. float f = 1.0;
  337. float g = 1.0;
  338. x /= np->spread.X;
  339. y /= np->spread.Y;
  340. z /= np->spread.Z;
  341. seed += np->seed;
  342. for (size_t i = 0; i < np->octaves; i++) {
  343. float noiseval = noise3d_gradient(x * f, y * f, z * f, seed + i,
  344. np->flags & NOISE_FLAG_EASED);
  345. if (np->flags & NOISE_FLAG_ABSVALUE)
  346. noiseval = std::fabs(noiseval);
  347. a += g * noiseval;
  348. f *= np->lacunarity;
  349. g *= np->persist;
  350. }
  351. return np->offset + a * np->scale;
  352. }
  353. Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
  354. {
  355. memcpy(&np, np_, sizeof(np));
  356. this->seed = seed;
  357. this->sx = sx;
  358. this->sy = sy;
  359. this->sz = sz;
  360. allocBuffers();
  361. }
  362. Noise::~Noise()
  363. {
  364. delete[] gradient_buf;
  365. delete[] persist_buf;
  366. delete[] noise_buf;
  367. delete[] result;
  368. }
  369. void Noise::allocBuffers()
  370. {
  371. if (sx < 1)
  372. sx = 1;
  373. if (sy < 1)
  374. sy = 1;
  375. if (sz < 1)
  376. sz = 1;
  377. this->noise_buf = NULL;
  378. resizeNoiseBuf(sz > 1);
  379. delete[] gradient_buf;
  380. delete[] persist_buf;
  381. delete[] result;
  382. try {
  383. size_t bufsize = sx * sy * sz;
  384. this->persist_buf = NULL;
  385. this->gradient_buf = new float[bufsize];
  386. this->result = new float[bufsize];
  387. } catch (std::bad_alloc &e) {
  388. throw InvalidNoiseParamsException();
  389. }
  390. }
  391. void Noise::setSize(u32 sx, u32 sy, u32 sz)
  392. {
  393. this->sx = sx;
  394. this->sy = sy;
  395. this->sz = sz;
  396. allocBuffers();
  397. }
  398. void Noise::setSpreadFactor(v3f spread)
  399. {
  400. this->np.spread = spread;
  401. resizeNoiseBuf(sz > 1);
  402. }
  403. void Noise::setOctaves(int octaves)
  404. {
  405. this->np.octaves = octaves;
  406. resizeNoiseBuf(sz > 1);
  407. }
  408. void Noise::resizeNoiseBuf(bool is3d)
  409. {
  410. //maximum possible spread value factor
  411. float ofactor = (np.lacunarity > 1.0) ?
  412. pow(np.lacunarity, np.octaves - 1) :
  413. np.lacunarity;
  414. // noise lattice point count
  415. // (int)(sz * spread * ofactor) is # of lattice points crossed due to length
  416. float num_noise_points_x = sx * ofactor / np.spread.X;
  417. float num_noise_points_y = sy * ofactor / np.spread.Y;
  418. float num_noise_points_z = sz * ofactor / np.spread.Z;
  419. // protect against obviously invalid parameters
  420. if (num_noise_points_x > 1000000000.f ||
  421. num_noise_points_y > 1000000000.f ||
  422. num_noise_points_z > 1000000000.f)
  423. throw InvalidNoiseParamsException();
  424. // + 2 for the two initial endpoints
  425. // + 1 for potentially crossing a boundary due to offset
  426. size_t nlx = (size_t)std::ceil(num_noise_points_x) + 3;
  427. size_t nly = (size_t)std::ceil(num_noise_points_y) + 3;
  428. size_t nlz = is3d ? (size_t)std::ceil(num_noise_points_z) + 3 : 1;
  429. delete[] noise_buf;
  430. try {
  431. noise_buf = new float[nlx * nly * nlz];
  432. } catch (std::bad_alloc &e) {
  433. throw InvalidNoiseParamsException();
  434. }
  435. }
  436. /*
  437. * NB: This algorithm is not optimal in terms of space complexity. The entire
  438. * integer lattice of noise points could be done as 2 lines instead, and for 3D,
  439. * 2 lines + 2 planes.
  440. * However, this would require the noise calls to be interposed with the
  441. * interpolation loops, which may trash the icache, leading to lower overall
  442. * performance.
  443. * Another optimization that could save half as many noise calls is to carry over
  444. * values from the previous noise lattice as midpoints in the new lattice for the
  445. * next octave.
  446. */
  447. #define idx(x, y) ((y) * nlx + (x))
  448. void Noise::gradientMap2D(
  449. float x, float y,
  450. float step_x, float step_y,
  451. s32 seed)
  452. {
  453. float v00, v01, v10, v11, u, v, orig_u;
  454. u32 index, i, j, noisex, noisey;
  455. u32 nlx, nly;
  456. s32 x0, y0;
  457. bool eased = np.flags & (NOISE_FLAG_DEFAULTS | NOISE_FLAG_EASED);
  458. Interp2dFxn interpolate = eased ?
  459. biLinearInterpolation : biLinearInterpolationNoEase;
  460. x0 = std::floor(x);
  461. y0 = std::floor(y);
  462. u = x - (float)x0;
  463. v = y - (float)y0;
  464. orig_u = u;
  465. //calculate noise point lattice
  466. nlx = (u32)(u + sx * step_x) + 2;
  467. nly = (u32)(v + sy * step_y) + 2;
  468. index = 0;
  469. for (j = 0; j != nly; j++)
  470. for (i = 0; i != nlx; i++)
  471. noise_buf[index++] = noise2d(x0 + i, y0 + j, seed);
  472. //calculate interpolations
  473. index = 0;
  474. noisey = 0;
  475. for (j = 0; j != sy; j++) {
  476. v00 = noise_buf[idx(0, noisey)];
  477. v10 = noise_buf[idx(1, noisey)];
  478. v01 = noise_buf[idx(0, noisey + 1)];
  479. v11 = noise_buf[idx(1, noisey + 1)];
  480. u = orig_u;
  481. noisex = 0;
  482. for (i = 0; i != sx; i++) {
  483. gradient_buf[index++] = interpolate(v00, v10, v01, v11, u, v);
  484. u += step_x;
  485. if (u >= 1.0) {
  486. u -= 1.0;
  487. noisex++;
  488. v00 = v10;
  489. v01 = v11;
  490. v10 = noise_buf[idx(noisex + 1, noisey)];
  491. v11 = noise_buf[idx(noisex + 1, noisey + 1)];
  492. }
  493. }
  494. v += step_y;
  495. if (v >= 1.0) {
  496. v -= 1.0;
  497. noisey++;
  498. }
  499. }
  500. }
  501. #undef idx
  502. #define idx(x, y, z) ((z) * nly * nlx + (y) * nlx + (x))
  503. void Noise::gradientMap3D(
  504. float x, float y, float z,
  505. float step_x, float step_y, float step_z,
  506. s32 seed)
  507. {
  508. float v000, v010, v100, v110;
  509. float v001, v011, v101, v111;
  510. float u, v, w, orig_u, orig_v;
  511. u32 index, i, j, k, noisex, noisey, noisez;
  512. u32 nlx, nly, nlz;
  513. s32 x0, y0, z0;
  514. Interp3dFxn interpolate = (np.flags & NOISE_FLAG_EASED) ?
  515. triLinearInterpolation : triLinearInterpolationNoEase;
  516. x0 = std::floor(x);
  517. y0 = std::floor(y);
  518. z0 = std::floor(z);
  519. u = x - (float)x0;
  520. v = y - (float)y0;
  521. w = z - (float)z0;
  522. orig_u = u;
  523. orig_v = v;
  524. //calculate noise point lattice
  525. nlx = (u32)(u + sx * step_x) + 2;
  526. nly = (u32)(v + sy * step_y) + 2;
  527. nlz = (u32)(w + sz * step_z) + 2;
  528. index = 0;
  529. for (k = 0; k != nlz; k++)
  530. for (j = 0; j != nly; j++)
  531. for (i = 0; i != nlx; i++)
  532. noise_buf[index++] = noise3d(x0 + i, y0 + j, z0 + k, seed);
  533. //calculate interpolations
  534. index = 0;
  535. noisey = 0;
  536. noisez = 0;
  537. for (k = 0; k != sz; k++) {
  538. v = orig_v;
  539. noisey = 0;
  540. for (j = 0; j != sy; j++) {
  541. v000 = noise_buf[idx(0, noisey, noisez)];
  542. v100 = noise_buf[idx(1, noisey, noisez)];
  543. v010 = noise_buf[idx(0, noisey + 1, noisez)];
  544. v110 = noise_buf[idx(1, noisey + 1, noisez)];
  545. v001 = noise_buf[idx(0, noisey, noisez + 1)];
  546. v101 = noise_buf[idx(1, noisey, noisez + 1)];
  547. v011 = noise_buf[idx(0, noisey + 1, noisez + 1)];
  548. v111 = noise_buf[idx(1, noisey + 1, noisez + 1)];
  549. u = orig_u;
  550. noisex = 0;
  551. for (i = 0; i != sx; i++) {
  552. gradient_buf[index++] = interpolate(
  553. v000, v100, v010, v110,
  554. v001, v101, v011, v111,
  555. u, v, w);
  556. u += step_x;
  557. if (u >= 1.0) {
  558. u -= 1.0;
  559. noisex++;
  560. v000 = v100;
  561. v010 = v110;
  562. v100 = noise_buf[idx(noisex + 1, noisey, noisez)];
  563. v110 = noise_buf[idx(noisex + 1, noisey + 1, noisez)];
  564. v001 = v101;
  565. v011 = v111;
  566. v101 = noise_buf[idx(noisex + 1, noisey, noisez + 1)];
  567. v111 = noise_buf[idx(noisex + 1, noisey + 1, noisez + 1)];
  568. }
  569. }
  570. v += step_y;
  571. if (v >= 1.0) {
  572. v -= 1.0;
  573. noisey++;
  574. }
  575. }
  576. w += step_z;
  577. if (w >= 1.0) {
  578. w -= 1.0;
  579. noisez++;
  580. }
  581. }
  582. }
  583. #undef idx
  584. float *Noise::perlinMap2D(float x, float y, float *persistence_map)
  585. {
  586. float f = 1.0, g = 1.0;
  587. size_t bufsize = sx * sy;
  588. x /= np.spread.X;
  589. y /= np.spread.Y;
  590. memset(result, 0, sizeof(float) * bufsize);
  591. if (persistence_map) {
  592. if (!persist_buf)
  593. persist_buf = new float[bufsize];
  594. for (size_t i = 0; i != bufsize; i++)
  595. persist_buf[i] = 1.0;
  596. }
  597. for (size_t oct = 0; oct < np.octaves; oct++) {
  598. gradientMap2D(x * f, y * f,
  599. f / np.spread.X, f / np.spread.Y,
  600. seed + np.seed + oct);
  601. updateResults(g, persist_buf, persistence_map, bufsize);
  602. f *= np.lacunarity;
  603. g *= np.persist;
  604. }
  605. if (std::fabs(np.offset - 0.f) > 0.00001 || std::fabs(np.scale - 1.f) > 0.00001) {
  606. for (size_t i = 0; i != bufsize; i++)
  607. result[i] = result[i] * np.scale + np.offset;
  608. }
  609. return result;
  610. }
  611. float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map)
  612. {
  613. float f = 1.0, g = 1.0;
  614. size_t bufsize = sx * sy * sz;
  615. x /= np.spread.X;
  616. y /= np.spread.Y;
  617. z /= np.spread.Z;
  618. memset(result, 0, sizeof(float) * bufsize);
  619. if (persistence_map) {
  620. if (!persist_buf)
  621. persist_buf = new float[bufsize];
  622. for (size_t i = 0; i != bufsize; i++)
  623. persist_buf[i] = 1.0;
  624. }
  625. for (size_t oct = 0; oct < np.octaves; oct++) {
  626. gradientMap3D(x * f, y * f, z * f,
  627. f / np.spread.X, f / np.spread.Y, f / np.spread.Z,
  628. seed + np.seed + oct);
  629. updateResults(g, persist_buf, persistence_map, bufsize);
  630. f *= np.lacunarity;
  631. g *= np.persist;
  632. }
  633. if (std::fabs(np.offset - 0.f) > 0.00001 || std::fabs(np.scale - 1.f) > 0.00001) {
  634. for (size_t i = 0; i != bufsize; i++)
  635. result[i] = result[i] * np.scale + np.offset;
  636. }
  637. return result;
  638. }
  639. void Noise::updateResults(float g, float *gmap,
  640. const float *persistence_map, size_t bufsize)
  641. {
  642. // This looks very ugly, but it is 50-70% faster than having
  643. // conditional statements inside the loop
  644. if (np.flags & NOISE_FLAG_ABSVALUE) {
  645. if (persistence_map) {
  646. for (size_t i = 0; i != bufsize; i++) {
  647. result[i] += gmap[i] * std::fabs(gradient_buf[i]);
  648. gmap[i] *= persistence_map[i];
  649. }
  650. } else {
  651. for (size_t i = 0; i != bufsize; i++)
  652. result[i] += g * std::fabs(gradient_buf[i]);
  653. }
  654. } else {
  655. if (persistence_map) {
  656. for (size_t i = 0; i != bufsize; i++) {
  657. result[i] += gmap[i] * gradient_buf[i];
  658. gmap[i] *= persistence_map[i];
  659. }
  660. } else {
  661. for (size_t i = 0; i != bufsize; i++)
  662. result[i] += g * gradient_buf[i];
  663. }
  664. }
  665. }