noise.cpp 19 KB

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