FastLED 3.10.8
Loading...
Searching...
No Matches
noise_woryley.cpp.hpp
Go to the documentation of this file.
2#include "fl/stl/limits.h"
3#include "fl/stl/noexcept.h"
4
5namespace fl {
6namespace {
7
8constexpr i32 Q15_ONE = 32768; // 1.0 in Q15
9// constexpr i32 Q15_HALF = Q15_ONE / 2;
10
11// Helper: multiply two Q15 numbers (result in Q15)
12// i32 q15_mul(i32 a, i32 b) {
13// return (i32)(((i64)a * b) >> 15);
14// }
15
16// Helper: absolute difference
17i32 q15_abs(i32 a) FL_NO_EXCEPT { return a < 0 ? -a : a; }
18
19// Pseudo-random hash based on grid coordinates
20u16 hash(i32 x, i32 y) FL_NO_EXCEPT {
21 u32 n = (u32)(x * 374761393 + y * 668265263);
22 n = (n ^ (n >> 13)) * 1274126177;
23 return (u16)((n ^ (n >> 16)) & 0xFFFF);
24}
25
26// Get fractional feature point inside a grid cell
27void feature_point(i32 gx, i32 gy, i32 &fx, i32 &fy) FL_NO_EXCEPT {
28 u16 h = hash(gx, gy);
29 fx = (h & 0xFF) * 128; // scale to Q15 (0–32767)
30 fy = ((h >> 8) & 0xFF) * 128;
31}
32} // namespace
33
34// Compute 2D Worley noise at (x, y) in Q15
36 i32 cell_x = x >> 15;
37 i32 cell_y = y >> 15;
38
39 // Use (max)() to prevent macro expansion by Arduino.h's max macro
40 i32 min_dist = (fl::numeric_limits<i32>::max)();
41
42 // Check surrounding 9 cells
43 for (int dy = -1; dy <= 1; ++dy) {
44 for (int dx = -1; dx <= 1; ++dx) {
45 i32 gx = cell_x + dx;
46 i32 gy = cell_y + dy;
47
48 i32 fx, fy;
49 feature_point(gx, gy, fx, fy);
50
51 i32 feature_x = (gx << 15) + fx;
52 i32 feature_y = (gy << 15) + fy;
53
54 i32 dx_q15 = x - feature_x;
55 i32 dy_q15 = y - feature_y;
56
57 // Approximate distance using Manhattan (faster) or Euclidean
58 // (costlier)
59 i32 dist =
60 q15_abs(dx_q15) + q15_abs(dy_q15); // Manhattan distance
61
62 if (dist < min_dist)
63 min_dist = dist;
64 }
65 }
66
67 // Normalize: maximum possible distance is roughly 2*Q15_ONE
68 return (min_dist << 15) / (2 * Q15_ONE);
69}
70
71} // namespace fl
void feature_point(i32 gx, i32 gy, i32 &fx, i32 &fy) FL_NO_EXCEPT
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
i32 worley_noise_2d_q15(i32 x, i32 y) FL_NO_EXCEPT
Base definition for an LED controller.
static constexpr T max() FL_NO_EXCEPT
Definition limits.h:108