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