FastLED 3.9.15
Loading...
Searching...
No Matches
perlin_s16x16.cpp.hpp
Go to the documentation of this file.
1#pragma once
2// allow-include-after-namespace
3
4// 2D Perlin noise implementation using s16x16 fixed-point arithmetic
5// Implementation file - included from perlin_s16x16.h
6
9
11
12namespace fl {
13
14void perlin_s16x16::init_fade_lut(fl::i32 *table) {
15 for (int i = 0; i <= 256; i++) {
16 fl::i64 t = static_cast<fl::i64>(i) * (HP_ONE / 256);
17 fl::i64 t2 = (t * t) >> HP_BITS;
18 fl::i64 t3 = (t2 * t) >> HP_BITS;
19 fl::i64 inner = (t * (6LL * HP_ONE)) >> HP_BITS;
20 inner -= 15LL * HP_ONE;
21 inner = (t * inner) >> HP_BITS;
22 inner += 10LL * HP_ONE;
23 table[i] = static_cast<fl::i32>((t3 * inner) >> HP_BITS);
24 }
25}
26
28 const fl::i32 *fade_lut,
29 const fl::u8 *perm) {
31 pnoise2d_raw(fx.raw(), fy.raw(), fade_lut, perm));
32}
33
34fl::i32 perlin_s16x16::pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
35 const fl::i32 *fade_lut,
36 const fl::u8 *perm) {
37 int X, Y;
38 fl::i32 x, y;
39 floor_frac(fx_raw, X, x);
40 floor_frac(fy_raw, Y, y);
41 X &= 255;
42 Y &= 255;
43
44 fl::i32 u = fade(x, fade_lut);
45 fl::i32 v = fade(y, fade_lut);
46
47 int A = perm[X & 255] + Y;
48 int AA = perm[A & 255];
49 int AB = perm[(A + 1) & 255];
50 int B = perm[(X + 1) & 255] + Y;
51 int BA = perm[B & 255];
52 int BB = perm[(B + 1) & 255];
53
54 fl::i32 result = lerp(v,
55 lerp(u, grad(perm[AA & 255], x, y),
56 grad(perm[BA & 255], x - HP_ONE, y)),
57 lerp(u, grad(perm[AB & 255], x, y - HP_ONE),
58 grad(perm[BB & 255], x - HP_ONE, y - HP_ONE)));
59
61}
62
63 void perlin_s16x16::floor_frac(fl::i32 fp16, int &ifloor,
64 fl::i32 &frac24) {
65 ifloor = fp16 >> FP_BITS;
66 frac24 = (fp16 & (FP_ONE - 1)) << (HP_BITS - FP_BITS);
67}
68
69 fl::i32 perlin_s16x16::fade(fl::i32 t, const fl::i32 *table) {
70 fl::u32 idx = static_cast<fl::u32>(t) >> 16;
71 fl::i32 frac = t & 0xFFFF;
72 fl::i32 a = table[idx];
73 fl::i32 b = table[idx + 1];
74 return a + static_cast<fl::i32>(
75 (static_cast<fl::i64>(frac) * (b - a)) >> 16);
76}
77
78 fl::i32 perlin_s16x16::lerp(fl::i32 t, fl::i32 a, fl::i32 b) {
79 return a + static_cast<fl::i32>(
80 (static_cast<fl::i64>(t) * (b - a)) >> HP_BITS);
81}
82
83 fl::i32 perlin_s16x16::grad(int hash, fl::i32 x, fl::i32 y) {
84 struct GradCoeff { fl::i8 cx; fl::i8 cy; };
85 constexpr GradCoeff lut[16] = {
86 { 1, 1}, {-1, 1}, { 1, -1}, {-1, -1},
87 { 1, 0}, {-1, 0}, { 1, 0}, {-1, 0},
88 { 0, 1}, { 0, -1}, { 0, 1}, { 0, -1},
89 { 1, 1}, { 0, -1}, {-1, 1}, { 0, -1},
90 };
91 const GradCoeff &g = lut[hash & 15];
92 return g.cx * x + g.cy * y;
93}
94
96 const fl::i32 *fade_lut,
97 const fl::u8 *perm) {
99 pnoise3d_raw(fx.raw(), fy.raw(), fz.raw(), fade_lut, perm));
100}
101
102fl::i32 perlin_s16x16::pnoise3d_raw(fl::i32 fx_raw, fl::i32 fy_raw, fl::i32 fz_raw,
103 const fl::i32 *fade_lut,
104 const fl::u8 *perm) {
105 int X, Y, Z;
106 fl::i32 x, y, z;
107 floor_frac(fx_raw, X, x);
108 floor_frac(fy_raw, Y, y);
109 floor_frac(fz_raw, Z, z);
110 X &= 255;
111 Y &= 255;
112 Z &= 255;
113
114 fl::i32 u = fade(x, fade_lut);
115 fl::i32 v = fade(y, fade_lut);
116 fl::i32 w = fade(z, fade_lut);
117
118 int A = perm[X & 255] + Y;
119 int AA = perm[A & 255] + Z;
120 int AB = perm[(A + 1) & 255] + Z;
121 int B = perm[(X + 1) & 255] + Y;
122 int BA = perm[B & 255] + Z;
123 int BB = perm[(B + 1) & 255] + Z;
124
125 fl::i32 result = lerp(w,
126 lerp(v,
127 lerp(u, grad3d(perm[AA & 255], x, y, z),
128 grad3d(perm[BA & 255], x - HP_ONE, y, z)),
129 lerp(u, grad3d(perm[AB & 255], x, y - HP_ONE, z),
130 grad3d(perm[BB & 255], x - HP_ONE, y - HP_ONE, z))),
131 lerp(v,
132 lerp(u, grad3d(perm[(AA + 1) & 255], x, y, z - HP_ONE),
133 grad3d(perm[(BA + 1) & 255], x - HP_ONE, y, z - HP_ONE)),
134 lerp(u, grad3d(perm[(AB + 1) & 255], x, y - HP_ONE, z - HP_ONE),
135 grad3d(perm[(BB + 1) & 255], x - HP_ONE, y - HP_ONE, z - HP_ONE))));
136
138}
139
140fl::i32 perlin_s16x16::grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z) {
141 // Matches float grad(hash, x, y, z) from perlin_float.h:
142 // h = hash & 15
143 // u = h < 8 ? x : y
144 // v = h < 4 ? y : (h == 12 || h == 14 ? x : z)
145 // return ((h&1)==0 ? u : -u) + ((h&2)==0 ? v : -v)
146 int h = hash & 15;
147 fl::i32 u = h < 8 ? x : y;
148 fl::i32 v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
149 return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
150}
151
152} // namespace fl
153
uint32_t z[NUM_LAYERS]
Definition Fire2023.h:93
static constexpr int FRAC_BITS
Definition s16x16.h:22
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NOEXCEPT
Definition s16x16.h:54
constexpr i32 raw() const FL_NOEXCEPT
Definition s16x16.h:60
unsigned char u8
Definition s16x16x4.h:132
signed char i8
Definition s16x16x4.h:131
FL_DISABLE_WARNING_PUSH unsigned char * B
fl::i64 i64
Definition s16x16x4.h:222
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FL_OPTIMIZATION_LEVEL_O3_END
static fl::i32 fade(fl::i32 t, const fl::i32 *table)
static constexpr fl::i32 HP_ONE
static fl::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm)
static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw, const fl::i32 *fade_lut, const fl::u8 *perm)
static fl::i32 pnoise3d_raw(fl::i32 fx_raw, fl::i32 fy_raw, fl::i32 fz_raw, const fl::i32 *fade_lut, const fl::u8 *perm)
static void floor_frac(fl::i32 fp16, int &ifloor, fl::i32 &frac24)
static fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b)
static constexpr int HP_BITS
static fl::s16x16 pnoise3d(fl::s16x16 fx, fl::s16x16 fy, fl::s16x16 fz, const fl::i32 *fade_lut, const fl::u8 *perm)
static void init_fade_lut(fl::i32 *table)
static constexpr int FP_BITS
static constexpr fl::i32 FP_ONE
static fl::i32 grad3d(int hash, fl::i32 x, fl::i32 y, fl::i32 z)
static fl::i32 grad(int hash, fl::i32 x, fl::i32 y)