fork(2) download
  1. #include <iostream>
  2. #include <stdint.h>
  3.  
  4. #ifdef FASTLED_AVR
  5. #include <avr/pgmspace.h>
  6. #define USE_PROGMEM
  7. #endif
  8.  
  9. #ifdef USE_PROGMEM
  10. #define FL_PROGMEM PROGMEM
  11. #define P(x) pgm_read_byte_near(p + x)
  12. #else
  13. #define FL_PROGMEM
  14. #define P(x) p[(x)]
  15. #endif
  16.  
  17.  
  18. #define P(x) p[(x)]
  19.  
  20. typedef uint8_t fract8; // ANSI: unsigned short _Fract
  21. typedef uint16_t fract16; // ANSI: unsigned _Fract
  22.  
  23. // scale8: scale one byte by a second one, which is treated as
  24. // the numerator of a fraction whose denominator is 256
  25. // In other words, it computes i * (scale / 256)
  26. // 4 clocks AVR, 2 clocks ARM
  27. uint8_t scale8( uint8_t i, fract8 scale)
  28. {
  29. return static_cast<uint8_t>(((int)i * (int)(scale) ) >> 8);
  30. }
  31.  
  32. uint16_t scale16( uint16_t i, fract16 scale )
  33. {
  34. uint16_t result;
  35. result = ((uint32_t)(i) * (uint32_t)(scale)) / 65536;
  36. return result;
  37. }
  38.  
  39. int16_t lerp15by16( int16_t a, int16_t b, fract16 frac)
  40. {
  41. int16_t result;
  42. if( b > a) {
  43. uint16_t delta = b - a;
  44. uint16_t scaled = scale16( delta, frac);
  45. result = a + scaled;
  46. } else {
  47. uint16_t delta = a - b;
  48. uint16_t scaled = scale16( delta, frac);
  49. result = a - scaled;
  50. }
  51. return result;
  52. }
  53.  
  54. uint8_t scale8_LEAVING_R1_DIRTY( uint8_t i, fract8 scale)
  55. {
  56. return ((int)i * (int)(scale) ) >> 8;
  57. }
  58.  
  59. // 16 bit, fixed point implementation of perlin's Simplex Noise. Coordinates are
  60. // 16.16 fixed point values, 32 bit integers with integral coordinates in the high 16
  61. // bits and fractional in the low 16 bits, and the function takes 1d, 2d, and 3d coordinate
  62. // values. These functions are scaled to return 0-65535
  63. extern uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z);
  64. extern uint16_t inoise16(uint32_t x, uint32_t y);
  65. extern uint16_t inoise16(uint32_t x);
  66.  
  67. // 16 bit raw versions of the noise functions. These values are not scaled/altered and have
  68. // output values roughly in the range (-18k,18k)
  69. extern int16_t inoise16_raw(uint32_t x, uint32_t y, uint32_t z);
  70. extern int16_t inoise16_raw(uint32_t x, uint32_t y);
  71. extern int16_t inoise16_raw(uint32_t x);
  72.  
  73. // 8 bit, fixed point implementation of perlin's Simplex Noise. Coordinates are
  74. // 8.8 fixed point values, 16 bit integers with integral coordinates in the high 8
  75. // bits and fractional in the low 8 bits, and the function takes 1d, 2d, and 3d coordinate
  76. // values. These functions are scaled to return 0-255
  77. extern uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z);
  78. extern uint8_t inoise8(uint16_t x, uint16_t y);
  79. extern uint8_t inoise8(uint16_t x);
  80.  
  81. // 8 bit raw versions of the noise functions. These values are not scaled/altered and have
  82. // output values roughly in the range (-70,70)
  83. extern int8_t inoise8_raw(uint16_t x, uint16_t y, uint16_t z);
  84. extern int8_t inoise8_raw(uint16_t x, uint16_t y);
  85. extern int8_t inoise8_raw(uint16_t x);
  86.  
  87.  
  88. FL_PROGMEM static uint8_t const p[] = { 151,160,137,91,90,15,
  89. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  90. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  91. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  92. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  93. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  94. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  95. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  96. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  97. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  98. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  99. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  100. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,151
  101. };
  102.  
  103. #define FADE(x) scale16(x,x)
  104. #define LERP(a,b,u) lerp15by16(a,b,u)
  105.  
  106. static int16_t grad16(uint8_t hash, int16_t x, int16_t y, int16_t z) {
  107. hash = hash&15;
  108. int16_t u = hash<8?x:y;
  109. int16_t v = hash<4?y:hash==12||hash==14?x:z;
  110. if(hash&1) { u = -u; }
  111. if(hash&2) { v = -v; }
  112.  
  113. return (u+v)>>1;
  114. }
  115.  
  116. static int16_t grad16(uint8_t hash, int16_t x, int16_t y) {
  117. hash = hash & 7;
  118. int16_t u,v;
  119. if(hash < 4) { u = x; v = y; } else { u = y; v = x; }
  120. if(hash&1) { u = -u; }
  121. if(hash&2) { v = -v; }
  122.  
  123. return (u+v)>>1;
  124. }
  125.  
  126. static int16_t grad16(uint8_t hash, int16_t x) {
  127. hash = hash & 15;
  128. int16_t u,v;
  129. if(hash > 8) { u=x;v=x; }
  130. else if(hash < 4) { u=x;v=1; }
  131. else { u=1;v=x; }
  132. if(hash&1) { u = -u; }
  133. if(hash&2) { v = -v; }
  134.  
  135. return (u+v)>>1;
  136. }
  137.  
  138. static int8_t grad8(uint8_t hash, int8_t x, int8_t y, int8_t z) {
  139. hash &= 0xF;
  140. int8_t u = (hash&8)?y:x;
  141. int8_t v = hash<4?y:hash==12||hash==14?x:z;
  142. if(hash&1) { u = -u; }
  143. if(hash&2) { v = -v; }
  144.  
  145. return (u+v)>>1;
  146. }
  147.  
  148. static int8_t grad8(uint8_t hash, int8_t x, int8_t y) {
  149. hash = hash & 7;
  150. int8_t u,v;
  151. if(hash < 4) { u = x; v = y; } else { u = y; v = x; }
  152. if(hash&1) { u = -u; }
  153. if(hash&2) { v = -v; }
  154.  
  155. return (u+v)>>1;
  156. }
  157.  
  158. static int8_t grad8(uint8_t hash, int8_t x) {
  159. hash = hash & 15;
  160. int8_t u,v;
  161. if(hash > 8) { u=x;v=x; }
  162. else if(hash < 4) { u=x;v=1; }
  163. else { u=1;v=x; }
  164. if(hash&1) { u = -u; }
  165. if(hash&2) { v = -v; }
  166.  
  167. return (u+v)>>1;
  168. }
  169.  
  170. static int8_t lerp7by8( int8_t a, int8_t b, fract8 frac)
  171. {
  172. int8_t result;
  173. if( b > a) {
  174. uint8_t delta = b - a;
  175. uint8_t scaled = scale8( delta, frac);
  176. result = a + scaled;
  177. } else {
  178. uint8_t delta = a - b;
  179. uint8_t scaled = scale8( delta, frac);
  180. result = a - scaled;
  181. }
  182. return result;
  183. }
  184.  
  185. int16_t inoise16_raw(uint32_t x, uint32_t y, uint32_t z)
  186. {
  187. // Find the unit cube containing the point
  188. uint8_t X = (x>>16)&0xFF;
  189. uint8_t Y = (y>>16)&0xFF;
  190. uint8_t Z = (z>>16)&0xFF;
  191.  
  192. // Hash cube corner coordinates
  193. uint8_t A = P(X)+Y;
  194. uint8_t AA = P(A)+Z;
  195. uint8_t AB = P(A+1)+Z;
  196. uint8_t B = P(X+1)+Y;
  197. uint8_t BA = P(B) + Z;
  198. uint8_t BB = P(B+1)+Z;
  199.  
  200. // Get the relative position of the point in the cube
  201. uint16_t u = x & 0xFFFF;
  202. uint16_t v = y & 0xFFFF;
  203. uint16_t w = z & 0xFFFF;
  204.  
  205. // Get a signed version of the above for the grad function
  206. int16_t xx = (u >> 1) & 0x7FFF;
  207. int16_t yy = (v >> 1) & 0x7FFF;
  208. int16_t zz = (w >> 1) & 0x7FFF;
  209. uint16_t N = 0x8000L;
  210.  
  211. u = FADE(u); v = FADE(v); w = FADE(w);
  212.  
  213.  
  214. // skip the log fade adjustment for the moment, otherwise here we would
  215. // adjust fade values for u,v,w
  216. int16_t X1 = LERP(grad16(P(AA), xx, yy, zz), grad16(P(BA), xx - N, yy, zz), u);
  217. int16_t X2 = LERP(grad16(P(AB), xx, yy-N, zz), grad16(P(BB), xx - N, yy - N, zz), u);
  218. int16_t X3 = LERP(grad16(P(AA+1), xx, yy, zz-N), grad16(P(BA+1), xx - N, yy, zz-N), u);
  219. int16_t X4 = LERP(grad16(P(AB+1), xx, yy-N, zz-N), grad16(P(BB+1), xx - N, yy - N, zz - N), u);
  220.  
  221. int16_t Y1 = LERP(X1,X2,v);
  222. int16_t Y2 = LERP(X3,X4,v);
  223.  
  224. int16_t ans = LERP(Y1,Y2,w);
  225.  
  226. return ans;
  227. }
  228.  
  229. uint16_t inoise16(uint32_t x, uint32_t y, uint32_t z) {
  230. int32_t ans = inoise16_raw(x,y,z);
  231. ans = ans + 19052L;
  232. uint32_t pan = ans;
  233. return (pan*220L)>>7;
  234. }
  235.  
  236. int16_t inoise16_raw(uint32_t x, uint32_t y)
  237. {
  238. // Find the unit cube containing the point
  239. uint8_t X = x>>16;
  240. uint8_t Y = y>>16;
  241.  
  242. // Hash cube corner coordinates
  243. uint8_t A = P(X)+Y;
  244. uint8_t AA = P(A);
  245. uint8_t AB = P(A+1);
  246. uint8_t B = P(X+1)+Y;
  247. uint8_t BA = P(B);
  248. uint8_t BB = P(B+1);
  249.  
  250. // Get the relative position of the point in the cube
  251. uint16_t u = x & 0xFFFF;
  252. uint16_t v = y & 0xFFFF;
  253.  
  254. // Get a signed version of the above for the grad function
  255. int16_t xx = (u >> 1) & 0x7FFF;
  256. int16_t yy = (v >> 1) & 0x7FFF;
  257. uint16_t N = 0x8000L;
  258.  
  259. u = FADE(u); v = FADE(v);
  260.  
  261. int16_t X1 = LERP(grad16(P(AA), xx, yy), grad16(P(BA), xx - N, yy), u);
  262. int16_t X2 = LERP(grad16(P(AB), xx, yy-N), grad16(P(BB), xx - N, yy - N), u);
  263.  
  264. int16_t ans = LERP(X1,X2,v);
  265.  
  266. return ans;
  267. }
  268.  
  269. uint16_t inoise16(uint32_t x, uint32_t y) {
  270. int32_t ans = inoise16_raw(x,y);
  271. ans = ans + 17308L;
  272. uint32_t pan = ans;
  273. return (pan*242L)>>7;
  274. }
  275.  
  276. int16_t inoise16_raw(uint32_t x)
  277. {
  278. // Find the unit cube containing the point
  279. uint8_t X = x>>16;
  280.  
  281. // Hash cube corner coordinates
  282. uint8_t A = P(X);
  283. uint8_t AA = P(A);
  284. uint8_t B = P(X+1);
  285. uint8_t BA = P(B);
  286.  
  287. // Get the relative position of the point in the cube
  288. uint16_t u = x & 0xFFFF;
  289.  
  290. // Get a signed version of the above for the grad function
  291. int16_t xx = (u >> 1) & 0x7FFF;
  292. uint16_t N = 0x8000L;
  293.  
  294. u = FADE(u);
  295.  
  296. int16_t ans = LERP(grad16(P(AA), xx), grad16(P(BA), xx - N), u);
  297.  
  298. return ans;
  299. }
  300.  
  301. uint16_t inoise16(uint32_t x) {
  302. return ((uint32_t)((int32_t)inoise16_raw(x) + 17308L)) << 1;
  303. }
  304.  
  305. int8_t inoise8_raw(uint16_t x, uint16_t y, uint16_t z)
  306. {
  307. // Find the unit cube containing the point
  308. uint8_t X = x>>8;
  309. uint8_t Y = y>>8;
  310. uint8_t Z = z>>8;
  311.  
  312. // Hash cube corner coordinates
  313. uint8_t A = P(X)+Y;
  314. uint8_t AA = P(A)+Z;
  315. uint8_t AB = P(A+1)+Z;
  316. uint8_t B = P(X+1)+Y;
  317. uint8_t BA = P(B) + Z;
  318. uint8_t BB = P(B+1)+Z;
  319.  
  320. // Get the relative position of the point in the cube
  321. uint8_t u = x;
  322. uint8_t v = y;
  323. uint8_t w = z;
  324.  
  325. // Get a signed version of the above for the grad function
  326. int8_t xx = (x>>1) & 0x7F;
  327. int8_t yy = (y>>1) & 0x7F;
  328. int8_t zz = (z>>1) & 0x7F;
  329. uint8_t N = 0x80;
  330.  
  331. u = scale8_LEAVING_R1_DIRTY(u,u); v = scale8_LEAVING_R1_DIRTY(v,v); w = scale8(w,w);
  332.  
  333. int8_t X1 = lerp7by8(grad8(P(AA), xx, yy, zz), grad8(P(BA), xx - N, yy, zz), u);
  334. int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N, zz), grad8(P(BB), xx - N, yy - N, zz), u);
  335. int8_t X3 = lerp7by8(grad8(P(AA+1), xx, yy, zz-N), grad8(P(BA+1), xx - N, yy, zz-N), u);
  336. int8_t X4 = lerp7by8(grad8(P(AB+1), xx, yy-N, zz-N), grad8(P(BB+1), xx - N, yy - N, zz - N), u);
  337.  
  338. int8_t Y1 = lerp7by8(X1,X2,v);
  339. int8_t Y2 = lerp7by8(X3,X4,v);
  340.  
  341. int8_t ans = lerp7by8(Y1,Y2,w);
  342.  
  343. return ans;
  344. }
  345.  
  346. uint8_t inoise8(uint16_t x, uint16_t y, uint16_t z) {
  347. return scale8(76+(inoise8_raw(x,y,z)),215)<<1;
  348. }
  349.  
  350. int8_t inoise8_raw(uint16_t x, uint16_t y)
  351. {
  352. // Find the unit cube containing the point
  353. uint8_t X = x>>8;
  354. uint8_t Y = y>>8;
  355.  
  356. // Hash cube corner coordinates
  357. uint8_t A = P(X)+Y;
  358. uint8_t AA = P(A);
  359. uint8_t AB = P(A+1);
  360. uint8_t B = P(X+1)+Y;
  361. uint8_t BA = P(B);
  362. uint8_t BB = P(B+1);
  363.  
  364. // Get the relative position of the point in the cube
  365. uint8_t u = x;
  366. uint8_t v = y;
  367.  
  368. // Get a signed version of the above for the grad function
  369. int8_t xx = (x>>1) & 0x7F;
  370. int8_t yy = (y>>1) & 0x7F;
  371. uint8_t N = 0x80;
  372.  
  373. u = scale8_LEAVING_R1_DIRTY(u,u); v = scale8(v,v);
  374.  
  375. int8_t X1 = lerp7by8(grad8(P(AA), xx, yy), grad8(P(BA), xx - N, yy), u);
  376. int8_t X2 = lerp7by8(grad8(P(AB), xx, yy-N), grad8(P(BB), xx - N, yy - N), u);
  377.  
  378. int8_t ans = lerp7by8(X1,X2,v);
  379.  
  380. return ans;
  381. }
  382.  
  383. uint8_t inoise8(uint16_t x, uint16_t y) {
  384. return scale8(69+inoise8_raw(x,y),237)<<1;
  385. }
  386.  
  387. int8_t inoise8_raw(uint16_t x)
  388. {
  389. // Find the unit cube containing the point
  390. uint8_t X = x>>8;
  391.  
  392. // Hash cube corner coordinates
  393. uint8_t A = P(X);
  394. uint8_t AA = P(A);
  395. uint8_t B = P(X+1);
  396. uint8_t BA = P(B);
  397.  
  398. // Get the relative position of the point in the cube
  399. uint8_t u = x;
  400.  
  401. // Get a signed version of the above for the grad function
  402. int8_t xx = (x>>1) & 0x7F;
  403. uint8_t N = 0x80;
  404.  
  405. u = scale8(u,u);
  406.  
  407. int8_t ans = lerp7by8(grad8(P(AA), xx), grad8(P(BA), xx - N), u);
  408.  
  409. return ans;
  410. }
  411.  
  412. uint8_t inoise8(uint16_t x) {
  413. return scale8(69+inoise8_raw(x), 255)<<1;
  414. }
  415.  
  416.  
  417. int main()
  418. {
  419. using namespace std;
  420. for (int i = 1; i < 256*4; ++i) {
  421. int v = 0;
  422. v = inoise8(i);
  423. cout << v << ",";
  424. if (i % 16 == 0) {
  425. cout << "\n";
  426. }
  427. }
  428.  
  429. cout << "\n";
  430. return 0;
  431. }
  432.  
  433.  
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
136,138,138,138,138,140,140,140,140,142,142,142,142,144,144,144,
144,146,146,146,146,148,148,148,148,150,150,150,150,152,152,152,
152,154,154,154,154,156,156,158,158,160,160,160,160,162,162,162,
162,164,164,164,164,166,166,166,166,168,168,168,168,170,170,172,
172,174,174,174,174,176,176,176,176,178,178,178,178,180,180,180,
180,182,182,182,182,184,184,184,184,186,186,186,186,188,188,188,
188,190,190,190,190,192,192,190,192,192,192,192,192,194,194,194,
194,196,196,196,196,198,198,196,196,198,198,198,198,200,200,200,
200,202,202,202,202,204,204,202,202,204,204,204,204,204,204,204,
204,206,206,204,204,206,206,204,204,206,206,204,204,206,206,204,
204,206,206,204,204,206,204,204,204,204,204,204,204,204,204,202,
202,202,202,202,202,202,202,200,200,200,200,198,198,198,198,196,
196,198,196,196,194,196,194,194,192,194,192,190,190,190,190,188,
188,188,188,186,184,186,184,182,182,182,182,180,178,178,178,176,
176,174,174,172,172,170,170,168,166,166,166,164,162,162,162,158,
158,158,156,154,152,152,152,148,148,146,146,142,142,140,140,136,
136,136,136,134,134,134,134,132,132,132,132,130,130,130,130,128,
128,128,128,126,126,126,126,124,124,124,124,122,122,122,122,120,
120,120,120,120,120,120,120,118,118,118,118,116,116,116,116,116,
116,116,116,114,114,114,114,112,112,114,114,112,112,112,112,110,
110,112,112,110,110,110,110,108,108,110,110,108,108,108,108,108,
108,108,108,106,106,106,108,106,106,106,106,106,106,106,106,104,
104,106,106,104,104,104,106,104,104,104,104,104,104,104,104,104,
104,104,104,102,104,104,104,102,104,104,104,102,104,104,104,102,
102,104,104,102,102,104,104,102,102,104,104,102,102,104,104,102,
104,104,104,102,104,104,104,104,104,104,104,104,104,104,106,104,
104,106,106,104,106,106,106,106,106,106,108,106,106,108,108,106,
108,108,108,108,108,108,108,108,108,110,110,108,110,110,110,110,
110,112,112,110,112,112,112,112,112,114,114,112,114,114,116,114,
114,116,116,116,116,116,118,116,118,118,120,118,118,120,120,120,
120,120,122,120,122,122,124,122,124,124,124,124,124,126,126,126,
126,128,128,128,128,130,130,130,130,132,132,132,132,134,134,134,
134,134,134,132,132,132,132,130,130,130,130,128,128,128,128,126,
126,126,126,124,124,124,124,122,122,122,122,120,120,120,120,118,
118,118,118,116,116,116,116,114,114,114,114,112,112,112,112,110,
110,110,110,108,108,108,108,106,106,106,106,104,104,104,104,104,
104,104,104,102,102,102,102,100,100,100,100,98,98,98,98,98,
98,98,98,96,96,96,96,94,94,94,94,94,94,94,94,92,
92,92,92,90,90,92,92,90,90,90,90,88,90,90,90,88,
88,88,88,88,88,88,88,86,86,88,88,86,86,86,86,86,
86,86,86,86,86,86,86,84,84,86,86,84,84,84,86,84,
84,84,86,84,84,84,86,84,84,86,86,84,86,86,86,86,
86,86,86,86,86,86,86,86,86,86,88,86,86,88,88,88,
88,88,88,88,88,90,90,90,90,90,90,90,92,92,92,92,
92,94,94,94,94,94,96,96,96,96,96,96,98,98,98,98,
100,100,100,100,102,102,102,104,104,104,106,106,106,108,108,108,
108,110,110,110,112,112,114,114,114,116,116,116,118,118,120,120,
120,122,122,124,124,126,126,128,128,130,130,132,132,134,134,136,
136,136,136,136,136,136,136,136,136,136,136,136,136,136,136,136,
136,136,136,136,136,136,136,136,136,136,136,136,136,136,136,136,
136,136,136,134,134,134,134,134,134,134,134,134,134,134,134,134,
134,134,134,134,134,132,132,132,132,132,132,132,132,132,132,130,
130,130,130,130,130,130,130,130,130,130,130,130,128,130,128,128,
128,128,128,128,128,128,128,128,128,128,126,126,126,126,126,126,
126,126,126,126,126,126,124,124,124,124,124,124,124,124,124,124,
124,124,124,122,122,122,122,122,122,122,122,122,122,122,122,120,
120,122,120,120,120,120,120,120,120,120,120,120,120,120,120,120,
120,120,120,120,118,120,118,118,118,120,118,118,118,118,118,118,
118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,
118,118,118,118,118,118,118,118,118,120,118,118,118,120,120,118,
118,120,120,120,120,120,120,120,120,120,120,120,120,122,122,122,
122,122,122,122,122,124,122,122,122,124,124,124,124,124,124,124,
124,126,126,126,126,128,128,128,128,128,128,128,128,130,130,130,
130,132,132,132,132,134,134,134,134,136,136,136,136,136,136,