FastLED 3.10.8
Loading...
Searching...
No Matches

◆ render_value_fp()

FASTLED_FORCE_INLINE fl::i32 fl::render_value_fp ( const render_parameters_fp & p,
const fl::i32 * fade_lut,
const fl::u8 * perm )

Definition at line 54 of file render_value_fp.h.

57 {
58
59 using FP = fl::s16x16;
60 constexpr fl::i32 FP_ONE = static_cast<fl::i32>(1) << FP::FRAC_BITS;
61
62 // sincos32 for angle
63 fl::u32 a24 = radiansToA24_fp(p.angle_raw);
64 SinCos32 sc = sincos32(a24);
65
66 // sincos32 output is in [-2147418112, 2147418112] (Q0.31 range).
67 // To multiply with s16x16 dist, we use:
68 // result_s16x16 = (sc_val * dist_raw) >> 31
69 // This gives us the product in s16x16 format.
70 fl::i32 cos_dist = static_cast<fl::i32>(
71 (static_cast<fl::i64>(sc.cos_val) * p.dist_raw) >> 31);
72 fl::i32 sin_dist = static_cast<fl::i32>(
73 (static_cast<fl::i64>(sc.sin_val) * p.dist_raw) >> 31);
74
75 // newx = (offset_x + center_x - cos*dist) * scale_x
76 // newy = (offset_y + center_y - sin*dist) * scale_y
77 fl::i32 pre_x = p.offset_x_raw + p.center_x_raw - cos_dist;
78 fl::i32 pre_y = p.offset_y_raw + p.center_y_raw - sin_dist;
79
80 fl::i32 nx = static_cast<fl::i32>(
81 (static_cast<fl::i64>(pre_x) * p.scale_x_raw) >> FP::FRAC_BITS);
82 fl::i32 ny = static_cast<fl::i32>(
83 (static_cast<fl::i64>(pre_y) * p.scale_y_raw) >> FP::FRAC_BITS);
84
85 // Compute z coordinate: newz = (offset_z + z) * scale_z
86 fl::i32 nz = static_cast<fl::i32>(
87 (static_cast<fl::i64>(p.offset_z_raw + p.z_raw) * p.scale_z_raw) >> FP::FRAC_BITS);
88
89 // Perlin noise: 2D when z==0, 3D otherwise
90 fl::i32 raw_noise;
91 if (nz == 0) {
92 raw_noise = perlin_s16x16::pnoise2d_raw(nx, ny, fade_lut, perm);
93 } else {
94 raw_noise = perlin_s16x16::pnoise3d_raw(nx, ny, nz, fade_lut, perm);
95 }
96
97 // Clamp to [low_limit, high_limit]
98 if (raw_noise < p.low_limit_raw) raw_noise = p.low_limit_raw;
99 if (raw_noise > p.high_limit_raw) raw_noise = p.high_limit_raw;
100
101 // Map from [low_limit, high_limit] to [0, 255]
102 // map_float(x, low, high, 0, 255) = (x - low) * 255 / (high - low)
103 fl::i32 range = p.high_limit_raw - p.low_limit_raw;
104 fl::i32 shifted = raw_noise - p.low_limit_raw;
105
106 // shifted is in [0, range], we want (shifted * 255) / range
107 // For the common case where low=0, high=1: range = FP_ONE
108 // result = (shifted * 255) >> FRAC_BITS (since shifted is in [0, FP_ONE])
109 // For low=-1, high=1: range = 2*FP_ONE
110 // result = (shifted * 255) / (2*FP_ONE)
111 fl::i32 result;
112 if (range == FP_ONE) {
113 // Optimized path: low_limit=0, high_limit=1
114 result = static_cast<fl::i32>(
115 (static_cast<fl::i64>(shifted) * 255) >> FP::FRAC_BITS);
116 } else if (range == 2 * FP_ONE) {
117 // Optimized path: low_limit=-1, high_limit=1
118 result = static_cast<fl::i32>(
119 (static_cast<fl::i64>(shifted) * 255) >> (FP::FRAC_BITS + 1));
120 } else {
121 // General path
122 result = static_cast<fl::i32>(
123 (static_cast<fl::i64>(shifted) * 255) / range);
124 }
125
126 // Final clamp to [0, 255]
127 if (result < 0) result = 0;
128 if (result > 255) result = 255;
129
130 return result;
131}
static constexpr i32 FP_ONE
FASTLED_FORCE_INLINE fl::u32 radiansToA24_fp(fl::i32 angle_s16x16_raw) FL_NO_EXCEPT
FASTLED_FORCE_INLINE SinCos32 sincos32(u32 angle) FL_NO_EXCEPT
Definition sin32.h:86
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
i32 sin_val
Definition sin32.h:23
i32 cos_val
Definition sin32.h:24
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) 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

References fl::SinCos32::cos_val, FASTLED_FORCE_INLINE, FL_NO_EXCEPT, FP_ONE, fl::perlin_s16x16::pnoise2d_raw(), fl::perlin_s16x16::pnoise3d_raw(), radiansToA24_fp(), fl::SinCos32::sin_val, and sincos32().

Referenced by fl::Center_Field_FP::draw(), fl::Complex_Kaleido_5_FP::draw(), fl::Module_Experiment10_FP::draw(), fl::Module_Experiment2_FP::draw(), fl::Module_Experiment3_FP::draw(), fl::Scaledemo1_FP::draw(), fl::SpiralMatrix4_FP::draw(), and render_value_fp_from_float().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: