FastLED 3.9.15
Loading...
Searching...
No Matches
simplex.cpp
Go to the documentation of this file.
1
3
4#define FASTLED_INTERNAL
5#include "FastLED.h"
6
7// This file implements simplex noise, which is an improved Perlin noise. This
8// implementation is a fixed-point version that avoids all uses of floating
9// point while still being compatible with the floating point version.
10
11// Original author: Stefan Gustavson, converted to Go by Lars Pensjö, converted
12// to fixed-point Go and then to C++ by Ayke van Laethem.
13// https://github.com/larspensjo/Go-simplex-noise/blob/master/simplexnoise/simplexnoise.go
14// https://github.com/aykevl/ledsgo/blob/master/noise.go
15//
16// The code in this file has been placed in the public domain. You can do
17// whatever you want with it. Attribution is appreciated but not required.
18
19// Notation:
20// Every fixed-point calculation has a line comment saying how many bits in the
21// given integer are used for the fractional part. For example:
22//
23// uint32_t n = a + b; // .12
24//
25// means the result of this operation has the floating point 12 bits from the
26// right. Specifically, there are 20 integer bits and 12 fractional bits. It
27// can be converted to a floating point using:
28//
29// double nf = (double)n / (1 << 12);
30
32
33namespace simplex_detail {
34
35#define SIMPLEX_P(x) FL_PGM_READ_BYTE_NEAR(simplex_detail::p + (x))
36
37// Permutation table. This is just a random jumble of all numbers.
38// This needs to be exactly the same for all instances on all platforms,
39// so it's easiest to just keep it as static explicit data.
40FL_PROGMEM static uint8_t const p[] = {
41 151, 160, 137, 91, 90, 15,
42 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
43 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
44 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
45 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
46 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
47 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
48 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
49 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
50 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
51 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
52 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
53 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
54};
55
56// A lookup table to traverse the simplex around a given point in 4D.
57// Details can be found where this table is used, in the 4D noise method.
58// TODO: This should not be required, backport it from Bill's GLSL code!
59static uint8_t const simplex[64][4] = {
60 {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0},
61 {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0},
62 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
63 {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0},
64 {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0},
65 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
66 {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0},
67 {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0},
68};
69
70} // namespace simplex_detail
71
72// hash is 0..0xff, x is 0.12 fixed point
73// returns *.12 fixed-point value
74static int32_t grad(uint8_t hash, int32_t x) {
75 uint8_t h = hash & 15;
76 int32_t grad = 1 + (h&7); // Gradient value 1.0, 2.0, ..., 8.0
77 if ((h&8) != 0) {
78 grad = -grad; // Set a random sign for the gradient
79 }
80 return grad * x; // Multiply the gradient with the distance (integer * 0.12 = *.12)
81}
82
83static int32_t grad(uint8_t hash, int32_t x, int32_t y) {
84 uint8_t h = hash & 7; // Convert low 3 bits of hash code
85 int32_t u = h < 4 ? x : y; // into 8 simple gradient directions,
86 int32_t v = h < 4 ? y : x; // and compute the dot product with (x,y).
87 return ((h&1) != 0 ? -u : u) + ((h&2) != 0 ? -2*v : 2*v);
88}
89
90static int32_t grad(uint8_t hash, int32_t x, int32_t y, int32_t z) {
91 int32_t h = hash & 15; // Convert low 4 bits of hash code into 12 simple
92 int32_t u = h < 8 ? x : y; // gradient directions, and compute dot product.
93 int32_t v = h < 4 ? y : (h == 12 || h == 14 ? x : z); // Fix repeats at h = 12 to 15
94 return ((h&1) != 0 ? -u : u) + ((h&2) != 0 ? -v : v);
95}
96
97static int32_t grad(uint8_t hash, int32_t x, int32_t y, int32_t z, int32_t t) {
98 uint8_t h = hash & 31; // Convert low 5 bits of hash code into 32 simple
99 int32_t u = h < 24 ? x : y; // gradient directions, and compute dot product.
100 int32_t v = h < 16 ? y : z;
101 int32_t w = h < 8 ? z : t;
102 return ((h&1) != 0 ? -u : u) + ((h&2) != 0 ? -v : v) + ((h&4) != 0 ? -w : w);
103}
104
105// 1D simplex noise.
106uint16_t snoise16(uint32_t x) {
107 uint32_t i0 = x >> 12;
108 uint32_t i1 = i0 + 1;
109 int32_t x0 = x & 0xfff; // .12
110 int32_t x1 = x0 - 0x1000; // .12
111
112 int32_t t0 = 0x8000 - ((x0*x0)>>9); // .15
113 t0 = (t0 * t0) >> 15; // .15
114 t0 = (t0 * t0) >> 15; // .15
115 int32_t n0 = (t0 * grad(SIMPLEX_P(i0&0xff), x0)) >> 12; // .15 * .12 = .15
116
117 int32_t t1 = 0x8000 - ((x1*x1)>>9); // .15
118 t1 = (t1 * t1) >> 15; // .15
119 t1 = (t1 * t1) >> 15; // .15
120 int32_t n1 = (t1 * grad(SIMPLEX_P(i1&0xff), x1)) >> 12; // .15 * .12 = .15
121
122 int32_t n = n0 + n1; // .15
123 n += 2503; // .15: fix offset, adjust to +0.03
124 n = (n * 26694) >> 16; // .15: fix scale to fit in [-1,1]
125 return uint16_t(n) + 0x8000;
126}
127
128// 2D simplex noise.
129uint16_t snoise16(uint32_t x, uint32_t y) {
130 const uint64_t F2 = 1572067135; // .32: F2 = 0.5*(sqrt(3.0)-1.0)
131 const uint64_t G2 = 907633384; // .32: G2 = (3.0-Math.sqrt(3.0))/6.0
132
133 // Skew the input space to determine which simplex cell we're in
134 uint32_t s = (((uint64_t)x + (uint64_t)y) * F2) >> 32; // (.12 + .12) * .32 = .12: Hairy factor for 2D
135 uint32_t i = ((x>>1) + (s>>1)) >> 11; // .0
136 uint32_t j = ((y>>1) + (s>>1)) >> 11; // .0
137
138 uint64_t t = ((uint64_t)i + (uint64_t)j) * G2; // .32
139 uint64_t X0 = ((uint64_t)i<<32) - t; // .32: Unskew the cell origin back to (x,y) space
140 uint64_t Y0 = ((uint64_t)j<<32) - t; // .32
141 int32_t x0 = ((uint64_t)x<<2) - (X0>>18); // .14: The x,y distances from the cell origin
142 int32_t y0 = ((uint64_t)y<<2) - (Y0>>18); // .14
143
144 // For the 2D case, the simplex shape is an equilateral triangle.
145 // Determine which simplex we are in.
146 uint32_t i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
147 if (x0 > y0) {
148 i1 = 1;
149 j1 = 0; // lower triangle, XY order: (0,0)->(1,0)->(1,1)
150 } else {
151 i1 = 0;
152 j1 = 1;
153 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
154
155 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
156 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
157 // c = (3-sqrt(3))/6
158
159 int32_t x1 = x0 - ((int32_t)i1<<14) + (int32_t)(G2>>18); // .14: Offsets for middle corner in (x,y) unskewed coords
160 int32_t y1 = y0 - ((int32_t)j1<<14) + (int32_t)(G2>>18); // .14
161 int32_t x2 = x0 - (1 << 14) + ((int32_t)(2*G2)>>18); // .14: Offsets for last corner in (x,y) unskewed coords
162 int32_t y2 = y0 - (1 << 14) + ((int32_t)(2*G2)>>18); // .14
163
164 int32_t n0 = 0, n1 = 0, n2 = 0; // Noise contributions from the three corners
165
166 // Calculate the contribution from the three corners
167 int32_t t0 = (((int32_t)1 << 27) - x0*x0 - y0*y0) >> 12; // .16
168 if (t0 > 0) {
169 t0 = (t0 * t0) >> 16; // .16
170 t0 = (t0 * t0) >> 16; // .16
171 n0 = t0 * grad(SIMPLEX_P((i+(uint32_t)(SIMPLEX_P(j&0xff)))&0xff), x0, y0); // .16 * .14 = .30
172 }
173
174 int32_t t1 = (((int32_t)1 << 27) - x1*x1 - y1*y1) >> 12; // .16
175 if (t1 > 0) {
176 t1 = (t1 * t1) >> 16; // .16
177 t1 = (t1 * t1) >> 16; // .16
178 n1 = t1 * grad(SIMPLEX_P((i+i1+(uint32_t)(SIMPLEX_P((j+j1)&0xff)))&0xff), x1, y1); // .16 * .14 = .30
179 }
180
181 int32_t t2 = (((int32_t)1 << 27) - x2*x2 - y2*y2) >> 12; // .16
182 if (t2 > 0) {
183 t2 = (t2 * t2) >> 16; // .16
184 t2 = (t2 * t2) >> 16; // .16
185 n2 = t2 * grad(SIMPLEX_P((i+1+(uint32_t)(SIMPLEX_P((j+1)&0xff)))&0xff), x2, y2); // .16 * .14 = .30
186 }
187
188 // Add contributions from each corner to get the final noise value.
189 // The result is scaled to return values in the interval [-1,1].
190 int32_t n = n0 + n1 + n2; // .30
191 n = ((n >> 8) * 23163) >> 16; // fix scale to fit exactly in an int16
192 return (uint16_t)n + 0x8000;
193}
194
195// 3D simplex noise.
196uint16_t snoise16(uint32_t x, uint32_t y, uint32_t z) {
197 // Simple skewing factors for the 3D case
198 const uint64_t F3 = 1431655764; // .32: 0.333333333
199 const uint64_t G3 = 715827884; // .32: 0.166666667
200
201 // Skew the input space to determine which simplex cell we're in
202 uint32_t s = (((uint64_t)x + (uint64_t)y + (uint64_t)z) * F3) >> 32; // .12 + .32 = .12: Very nice and simple skew factor for 3D
203 uint32_t i = ((x>>1) + (s>>1)) >> 11; // .0
204 uint32_t j = ((y>>1) + (s>>1)) >> 11; // .0
205 uint32_t k = ((z>>1) + (s>>1)) >> 11; // .0
206
207 uint64_t t = ((uint64_t)i + (uint64_t)j + (uint64_t)k) * G3; // .32
208 uint64_t X0 = ((uint64_t)i<<32) - t; // .32: Unskew the cell origin back to (x,y) space
209 uint64_t Y0 = ((uint64_t)j<<32) - t; // .32
210 uint64_t Z0 = ((uint64_t)k<<32) - t; // .32
211 int32_t x0 = ((uint64_t)x<<2) - (X0>>18); // .14: The x,y distances from the cell origin
212 int32_t y0 = ((uint64_t)y<<2) - (Y0>>18); // .14
213 int32_t z0 = ((uint64_t)z<<2) - (Z0>>18); // .14
214
215 // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
216 // Determine which simplex we are in.
217 uint32_t i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
218 uint32_t i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
219
220 // This code would benefit from a backport from the GLSL version!
221 if (x0 >= y0) {
222 if (y0 >= z0) {
223 i1 = 1;
224 j1 = 0;
225 k1 = 0;
226 i2 = 1;
227 j2 = 1;
228 k2 = 0; // X Y Z order
229 } else if (x0 >= z0) {
230 i1 = 1;
231 j1 = 0;
232 k1 = 0;
233 i2 = 1;
234 j2 = 0;
235 k2 = 1; // X Z Y order
236 } else {
237 i1 = 0;
238 j1 = 0;
239 k1 = 1;
240 i2 = 1;
241 j2 = 0;
242 k2 = 1; // Z X Y order
243 }
244 } else { // x0<y0
245 if (y0 < z0) {
246 i1 = 0;
247 j1 = 0;
248 k1 = 1;
249 i2 = 0;
250 j2 = 1;
251 k2 = 1; // Z Y X order
252 } else if (x0 < z0) {
253 i1 = 0;
254 j1 = 1;
255 k1 = 0;
256 i2 = 0;
257 j2 = 1;
258 k2 = 1; // Y Z X order
259 } else {
260 i1 = 0;
261 j1 = 1;
262 k1 = 0;
263 i2 = 1;
264 j2 = 1;
265 k2 = 0; // Y X Z order
266 }
267 }
268
269 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
270 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
271 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
272 // c = 1/6.
273
274 int32_t x1 = x0 - ((int32_t)i1<<14) + ((int32_t)(G3>>18)); // .14: Offsets for second corner in (x,y,z) coords
275 int32_t y1 = y0 - ((int32_t)j1<<14) + ((int32_t)(G3>>18)); // .14
276 int32_t z1 = z0 - ((int32_t)k1<<14) + ((int32_t)(G3>>18)); // .14
277 int32_t x2 = x0 - ((int32_t)i2<<14) + ((int32_t)(2*G3)>>18); // .14: Offsets for third corner in (x,y,z) coords
278 int32_t y2 = y0 - ((int32_t)j2<<14) + ((int32_t)(2*G3)>>18); // .14
279 int32_t z2 = z0 - ((int32_t)k2<<14) + ((int32_t)(2*G3)>>18); // .14
280 int32_t x3 = x0 - (1 << 14) + (int32_t)((3*G3)>>18); // .14: Offsets for last corner in (x,y,z) coords
281 int32_t y3 = y0 - (1 << 14) + (int32_t)((3*G3)>>18); // .14
282 int32_t z3 = z0 - (1 << 14) + (int32_t)((3*G3)>>18); // .14
283
284 // Calculate the contribution from the four corners
285 int32_t n0 = 0, n1 = 0, n2 = 0, n3 = 0; // .30
286 const int32_t fix0_6 = 161061274; // .28: 0.6
287
288 int32_t t0 = (fix0_6 - x0*x0 - y0*y0 - z0*z0) >> 12; // .16
289 if (t0 > 0) {
290 t0 = (t0 * t0) >> 16; // .16
291 t0 = (t0 * t0) >> 16; // .16
292 // .16 * .14 = .30
293 n0 = t0 * grad(SIMPLEX_P((i+(uint32_t)SIMPLEX_P((j+(uint32_t)SIMPLEX_P(k&0xff))&0xff))&0xff), x0, y0, z0);
294 }
295
296 int32_t t1 = (fix0_6 - x1*x1 - y1*y1 - z1*z1) >> 12; // .16
297 if (t1 > 0) {
298 t1 = (t1 * t1) >> 16; // .16
299 t1 = (t1 * t1) >> 16; // .16
300 // .16 * .14 = .30
301 n1 = t1 * grad(SIMPLEX_P((i+i1+(uint32_t)SIMPLEX_P((j+j1+(uint32_t)SIMPLEX_P((k+k1)&0xff))&0xff))&0xff), x1, y1, z1);
302 }
303
304 int32_t t2 = (fix0_6 - x2*x2 - y2*y2 - z2*z2) >> 12; // .16
305 if (t2 > 0) {
306 t2 = (t2 * t2) >> 16; // .16
307 t2 = (t2 * t2) >> 16; // .16
308 // .16 * .14 = .30
309 n2 = t2 * grad(SIMPLEX_P((i+i2+(uint32_t)SIMPLEX_P((j+j2+(uint32_t)SIMPLEX_P((k+k2)&0xff))&0xff))&0xff), x2, y2, z2);
310 }
311
312 int32_t t3 = (fix0_6 - x3*x3 - y3*y3 - z3*z3) >> 12; // .16
313 if (t3 > 0) {
314 t3 = (t3 * t3) >> 16; // .16
315 t3 = (t3 * t3) >> 16; // .16
316 // .16 * .14 = .30
317 n3 = t3 * grad(SIMPLEX_P((i+1+(uint32_t)SIMPLEX_P((j+1+(uint32_t)SIMPLEX_P((k+1)&0xff))&0xff))&0xff), x3, y3, z3);
318 }
319
320 // Add contributions from each corner to get the final noise value.
321 // The result is scaled to stay just inside [-1,1]
322 int32_t n = n0 + n1 + n2 + n3; // .30
323 n = ((n >> 8) * 16748) >> 16 ; // fix scale to fit exactly in an int16
324 return (uint16_t)n + 0x8000;
325}
326
327// 4D simplex noise.
328uint16_t snoise16(uint32_t x, uint32_t y, uint32_t z, uint32_t w) {
329 // The skewing and unskewing factors are hairy again for the 4D case
330 const uint64_t F4 = 331804471; // .30: (Math.sqrt(5.0)-1.0)/4.0 = 0.30901699437494745
331 const uint64_t G4 = 593549882; // .32: (5.0-Math.sqrt(5.0))/20.0 = 0.1381966011250105
332
333 // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're
334 // in.
335 uint32_t s = (((uint64_t)x + (uint64_t)y + (uint64_t)z + (uint64_t)w) * F4) >> 32; // .12 + .30 = .10: Factor for 4D skewing.
336 uint32_t i = ((x>>2) + s) >> 10; // .0
337 uint32_t j = ((y>>2) + s) >> 10; // .0
338 uint32_t k = ((z>>2) + s) >> 10; // .0
339 uint32_t l = ((w>>2) + s) >> 10; // .0
340
341 uint64_t t = (((uint64_t)i + (uint64_t)j + (uint64_t)k + (uint64_t)l) * G4) >> 18; // .14
342 uint64_t X0 = ((uint64_t)i<<14) - t; // .14: Unskew the cell origin back to (x,y,z,w) space
343 uint64_t Y0 = ((uint64_t)j<<14) - t; // .14
344 uint64_t Z0 = ((uint64_t)k<<14) - t; // .14
345 uint64_t W0 = ((uint64_t)l<<14) - t; // .14
346 int32_t x0 = ((uint64_t)x<<2) - X0; // .14: The x,y,z,w distances from the cell origin
347 int32_t y0 = ((uint64_t)y<<2) - Y0; // .14
348 int32_t z0 = ((uint64_t)z<<2) - Z0; // .14
349 int32_t w0 = ((uint64_t)w<<2) - W0; // .14
350
351 // For the 4D case, the simplex is a 4D shape I won't even try to describe.
352 // To find out which of the 24 possible simplices we're in, we need to
353 // determine the magnitude ordering of x0, y0, z0 and w0.
354 // The method below is a good way of finding the ordering of x,y,z,w and
355 // then find the correct traversal order for the simplex we’re in.
356 // First, six pair-wise comparisons are performed between each possible pair
357 // of the four coordinates, and the results are used to add up binary bits
358 // for an integer index.
359 int c = 0;
360 if (x0 > y0) {
361 c += 32;
362 }
363 if (x0 > z0) {
364 c += 16;
365 }
366 if (y0 > z0) {
367 c += 8;
368 }
369 if (x0 > w0) {
370 c += 4;
371 }
372 if (y0 > w0) {
373 c += 2;
374 }
375 if (z0 > w0) {
376 c += 1;
377 }
378
379 // simplex_detail::simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
380 // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
381 // impossible. Only the 24 indices which have non-zero entries make any sense.
382 // We use a thresholding to set the coordinates in turn from the largest magnitude.
383 // The number 3 in the "simplex" array is at the position of the largest coordinate.
384 // The integer offsets for the second simplex corner
385 uint32_t i1 = simplex_detail::simplex[c][0] >= 3 ? 1 : 0;
386 uint32_t j1 = simplex_detail::simplex[c][1] >= 3 ? 1 : 0;
387 uint32_t k1 = simplex_detail::simplex[c][2] >= 3 ? 1 : 0;
388 uint32_t l1 = simplex_detail::simplex[c][3] >= 3 ? 1 : 0;
389 // The number 2 in the "simplex" array is at the second largest coordinate.
390 // The integer offsets for the third simplex corner
391 uint32_t i2 = simplex_detail::simplex[c][0] >= 2 ? 1 : 0;
392 uint32_t j2 = simplex_detail::simplex[c][1] >= 2 ? 1 : 0;
393 uint32_t k2 = simplex_detail::simplex[c][2] >= 2 ? 1 : 0;
394 uint32_t l2 = simplex_detail::simplex[c][3] >= 2 ? 1 : 0;
395 // The number 1 in the "simplex" array is at the second smallest coordinate.
396 // The integer offsets for the fourth simplex corner
397 uint32_t i3 = simplex_detail::simplex[c][0] >= 1 ? 1 : 0;
398 uint32_t j3 = simplex_detail::simplex[c][1] >= 1 ? 1 : 0;
399 uint32_t k3 = simplex_detail::simplex[c][2] >= 1 ? 1 : 0;
400 uint32_t l3 = simplex_detail::simplex[c][3] >= 1 ? 1 : 0;
401 // The fifth corner has all coordinate offsets = 1, so no need to look that up.
402
403 int32_t x1 = x0 - ((int32_t)i1<<14) + (int32_t)(G4>>18); // .14: Offsets for second corner in (x,y,z,w) coords
404 int32_t y1 = y0 - ((int32_t)j1<<14) + (int32_t)(G4>>18);
405 int32_t z1 = z0 - ((int32_t)k1<<14) + (int32_t)(G4>>18);
406 int32_t w1 = w0 - ((int32_t)l1<<14) + (int32_t)(G4>>18);
407 int32_t x2 = x0 - ((int32_t)i2<<14) + (int32_t)(2*G4>>18); // .14: Offsets for third corner in (x,y,z,w) coords
408 int32_t y2 = y0 - ((int32_t)j2<<14) + (int32_t)(2*G4>>18);
409 int32_t z2 = z0 - ((int32_t)k2<<14) + (int32_t)(2*G4>>18);
410 int32_t w2 = w0 - ((int32_t)l2<<14) + (int32_t)(2*G4>>18);
411 int32_t x3 = x0 - ((int32_t)i3<<14) + (int32_t)(3*G4>>18); // .14: Offsets for fourth corner in (x,y,z,w) coords
412 int32_t y3 = y0 - ((int32_t)j3<<14) + (int32_t)(3*G4>>18);
413 int32_t z3 = z0 - ((int32_t)k3<<14) + (int32_t)(3*G4>>18);
414 int32_t w3 = w0 - ((int32_t)l3<<14) + (int32_t)(3*G4>>18);
415 int32_t x4 = x0 - (1 << 14) + (int32_t)(4*G4>>18); // .14: Offsets for last corner in (x,y,z,w) coords
416 int32_t y4 = y0 - (1 << 14) + (int32_t)(4*G4>>18);
417 int32_t z4 = z0 - (1 << 14) + (int32_t)(4*G4>>18);
418 int32_t w4 = w0 - (1 << 14) + (int32_t)(4*G4>>18);
419
420 int32_t n0 = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0; // Noise contributions from the five corners
421 const int32_t fix0_6 = 161061274; // .28: 0.6
422
423 // Calculate the contribution from the five corners
424 int32_t t0 = (fix0_6 - x0*x0 - y0*y0 - z0*z0 - w0*w0) >> 12; // .16
425 if (t0 > 0) {
426 t0 = (t0 * t0) >> 16;
427 t0 = (t0 * t0) >> 16;
428 // .16 * .14 = .30
429 n0 = t0 * grad(SIMPLEX_P((i+(uint32_t)(SIMPLEX_P((j+(uint32_t)(SIMPLEX_P((k+(uint32_t)(SIMPLEX_P(l&0xff)))&0xff)))&0xff)))&0xff), x0, y0, z0, w0);
430 }
431
432 int32_t t1 = (fix0_6 - x1*x1 - y1*y1 - z1*z1 - w1*w1) >> 12; // .16
433 if (t1 > 0) {
434 t1 = (t1 * t1) >> 16;
435 t1 = (t1 * t1) >> 16;
436 // .16 * .14 = .30
437 n1 = t1 * grad(SIMPLEX_P((i+i1+(uint32_t)(SIMPLEX_P((j+j1+(uint32_t)(SIMPLEX_P((k+k1+(uint32_t)(SIMPLEX_P((l+l1)&0xff)))&0xff)))&0xff)))&0xff), x1, y1, z1, w1);
438 }
439
440 int32_t t2 = (fix0_6 - x2*x2 - y2*y2 - z2*z2 - w2*w2) >> 12; // .16
441 if (t2 > 0) {
442 t2 = (t2 * t2) >> 16;
443 t2 = (t2 * t2) >> 16;
444 // .16 * .14 = .30
445 n2 = t2 * grad(SIMPLEX_P((i+i2+(uint32_t)(SIMPLEX_P((j+j2+(uint32_t)(SIMPLEX_P((k+k2+(uint32_t)(SIMPLEX_P((l+l2)&0xff)))&0xff)))&0xff)))&0xff), x2, y2, z2, w2);
446 }
447
448 int32_t t3 = (fix0_6 - x3*x3 - y3*y3 - z3*z3 - w3*w3) >> 12; // .16
449 if (t3 > 0) {
450 t3 = (t3 * t3) >> 16;
451 t3 = (t3 * t3) >> 16;
452 // .16 * .14 = .30
453 n3 = t3 * grad(SIMPLEX_P((i+i3+(uint32_t)(SIMPLEX_P((j+j3+(uint32_t)(SIMPLEX_P((k+k3+(uint32_t)(SIMPLEX_P((l+l3)&0xff)))&0xff)))&0xff)))&0xff), x3, y3, z3, w3);
454 }
455
456 int32_t t4 = (fix0_6 - x4*x4 - y4*y4 - z4*z4 - w4*w4) >> 12; // .16
457 if (t4 > 0) {
458 t4 = (t4 * t4) >> 16;
459 t4 = (t4 * t4) >> 16;
460 // .16 * .14 = .30
461 n4 = t4 * grad(SIMPLEX_P((i+1+(uint32_t)(SIMPLEX_P((j+1+(uint32_t)(SIMPLEX_P((k+1+(uint32_t)(SIMPLEX_P((l+1)&0xff)))&0xff)))&0xff)))&0xff), x4, y4, z4, w4);
462 }
463
464 int32_t n = n0 + n1 + n2 + n3 + n4; // .30
465 n = ((n >> 8) * 13832) >> 16; // fix scale
466 return uint16_t(n) + 0x8000;
467}
468
int y
Definition simple.h:93
int x
Definition simple.h:92
central include file for FastLED, defines the CFastLED class/object
uint32_t z[NUM_LAYERS]
Definition Fire2023.h:94
@ W0
White is first.
Definition eorder.h:27
static uint32_t t
Definition Luminova.h:54
#define FL_PROGMEM
PROGMEM keyword for storage.
uint16_t snoise16(uint32_t x)
32 bit, fixed point implementation of simplex noise functions.
Definition simplex.cpp:106
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
#define FASTLED_NAMESPACE_BEGIN
Definition namespace.h:22
static uint8_t const p[]
Definition simplex.cpp:40
static uint8_t const simplex[64][4]
Definition simplex.cpp:59
static int32_t grad(uint8_t hash, int32_t x)
Definition simplex.cpp:74
#define SIMPLEX_P(x)
Definition simplex.cpp:35