FastLED 3.10.8
Loading...
Searching...
No Matches
perlin_q16.cpp.hpp
Go to the documentation of this file.
1#pragma once
2// allow-include-after-namespace
3
4// 2D Perlin noise Q16 implementation
5// Implementation file - included from perlin_q16.h
6
9#include "fl/stl/noexcept.h"
10
12
13namespace fl {
14
16 for (int i = 0; i <= 256; i++) {
17 fl::i32 t = (i * HP_ONE) / 256;
18 fl::i32 t2 = static_cast<fl::i32>((static_cast<fl::i64>(t) * t) >> HP_BITS);
19 fl::i32 t3 = static_cast<fl::i32>((static_cast<fl::i64>(t2) * t) >> HP_BITS);
20 fl::i32 inner = static_cast<fl::i32>((static_cast<fl::i64>(t) * (6 * HP_ONE)) >> HP_BITS);
21 inner -= 15 * HP_ONE;
22 inner = static_cast<fl::i32>((static_cast<fl::i64>(t) * inner) >> HP_BITS);
23 inner += 10 * HP_ONE;
24 table[i] = static_cast<fl::i32>((static_cast<fl::i64>(t3) * inner) >> HP_BITS);
25 }
26}
27
29 const fl::i32 *fade_lut,
30 const fl::u8 *perm) FL_NO_EXCEPT {
32 pnoise2d_raw(fx.raw(), fy.raw(), fade_lut, perm));
33}
34
35fl::i32 perlin_q16::pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw,
36 const fl::i32 *fade_lut,
37 const fl::u8 *perm) FL_NO_EXCEPT {
38 int X, Y;
39 fl::i32 x, y;
40 floor_frac(fx_raw, X, x);
41 floor_frac(fy_raw, Y, y);
42 X &= 255;
43 Y &= 255;
44
45 fl::i32 u = fade(x, fade_lut);
46 fl::i32 v = fade(y, fade_lut);
47
48 int A = perm[X & 255] + Y;
49 int AA = perm[A & 255];
50 int AB = perm[(A + 1) & 255];
51 int B = perm[(X + 1) & 255] + Y;
52 int BA = perm[B & 255];
53 int BB = perm[(B + 1) & 255];
54
55 fl::i32 result = lerp(v,
56 lerp(u, grad(perm[AA & 255], x, y),
57 grad(perm[BA & 255], x - HP_ONE, y)),
58 lerp(u, grad(perm[AB & 255], x, y - HP_ONE),
59 grad(perm[BB & 255], x - HP_ONE, y - HP_ONE)));
60
61 // No shift needed: already in Q16 format, matches s16x16::FRAC_BITS
62 return result;
63}
64
65FASTLED_FORCE_INLINE void perlin_q16::floor_frac(fl::i32 fp16, int &ifloor,
66 fl::i32 &frac16) FL_NO_EXCEPT {
67 ifloor = fp16 >> FP_BITS;
68 frac16 = fp16 & (FP_ONE - 1); // Already Q16, no shift needed
69}
70
71FASTLED_FORCE_INLINE fl::i32 perlin_q16::fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT {
72 fl::u32 idx = static_cast<fl::u32>(t) >> 8; // Q16 → 8-bit index
73 fl::i32 frac = t & 0xFF;
74 fl::i32 a = table[idx];
75 fl::i32 b = table[idx + 1];
76 // Lerp in Q16: frac is 8 bits, expand to 16 for precision
77 return a + static_cast<fl::i32>(
78 (static_cast<fl::i32>(frac << 8) * (b - a)) >> 16);
79}
80
81FASTLED_FORCE_INLINE fl::i32 perlin_q16::lerp(fl::i32 t, fl::i32 a, fl::i32 b) FL_NO_EXCEPT {
82 // All values in Q16, result stays Q16
83 return a + static_cast<fl::i32>(
84 (static_cast<fl::i64>(t) * (b - a)) >> HP_BITS);
85}
86
87FASTLED_FORCE_INLINE fl::i32 perlin_q16::grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT {
88 struct GradCoeff { fl::i8 cx; fl::i8 cy; };
89 constexpr GradCoeff lut[16] = {
90 { 1, 1}, {-1, 1}, { 1, -1}, {-1, -1},
91 { 1, 0}, {-1, 0}, { 1, 0}, {-1, 0},
92 { 0, 1}, { 0, -1}, { 0, 1}, { 0, -1},
93 { 1, 1}, { 0, -1}, {-1, 1}, { 0, -1},
94 };
95 const GradCoeff &g = lut[hash & 15];
96 return g.cx * x + g.cy * y;
97}
98
99} // namespace fl
100
static constexpr FASTLED_FORCE_INLINE s16x16 from_raw(i32 raw) FL_NO_EXCEPT
Definition s16x16.h:56
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FASTLED_FORCE_INLINE
#define FL_OPTIMIZATION_LEVEL_O3_END
unsigned char u8
Definition stdint.h:131
FASTLED_FORCE_INLINE float lerp(float t, float a, float b) FL_NO_EXCEPT
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
FASTLED_FORCE_INLINE float grad(int hash, float x, float y, float z) FL_NO_EXCEPT
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
InputGamut g
Definition rgbw.h:125
FASTLED_FORCE_INLINE float fade(float t) FL_NO_EXCEPT
signed char i8
Definition stdint.h:130
Base definition for an LED controller.
Definition crgb.hpp:179
static constexpr int HP_BITS
Definition perlin_q16.h:16
static FASTLED_FORCE_INLINE void floor_frac(fl::i32 fp16, int &ifloor, fl::i32 &frac16) FL_NO_EXCEPT
static constexpr fl::i32 FP_ONE
Definition perlin_q16.h:34
static FASTLED_FORCE_INLINE fl::i32 grad(int hash, fl::i32 x, fl::i32 y) FL_NO_EXCEPT
static constexpr int FP_BITS
Definition perlin_q16.h:33
static FASTLED_FORCE_INLINE fl::i32 fade(fl::i32 t, const fl::i32 *table) FL_NO_EXCEPT
static FASTLED_FORCE_INLINE fl::i32 lerp(fl::i32 t, fl::i32 a, fl::i32 b) FL_NO_EXCEPT
static fl::i32 pnoise2d_raw(fl::i32 fx_raw, fl::i32 fy_raw, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
static fl::s16x16 pnoise2d(fl::s16x16 fx, fl::s16x16 fy, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
static constexpr fl::i32 HP_ONE
Definition perlin_q16.h:17
static void init_fade_lut(fl::i32 *table) FL_NO_EXCEPT