noise.cpp 18 KB

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