13namespace lampda::helixmask {
16constexpr double kPi = 3.141592653589793;
17constexpr double kSqrt3 = 1.7320508075688772;
19constexpr double cx_abs(
double x) {
return x < 0.0 ? -x : x; }
20constexpr double cx_floor(
double x)
22 const long long i =
static_cast<long long>(x);
23 return static_cast<double>(x < static_cast<double>(i) ? i - 1 : i);
25constexpr double cx_round(
double x) {
return cx_floor(x + 0.5); }
26constexpr double cx_frac(
double x) {
return x - cx_floor(x); }
27constexpr double cx_max(
double a,
double b) {
return a > b ? a : b; }
28constexpr double cx_min(
double a,
double b) {
return a < b ? a : b; }
29constexpr double cx_sqrt(
double x)
33 double r = x < 1.0 ? 1.0 : x;
34 for (
int i = 0; i < 40; ++i)
35 r = 0.5 * (r + x / r);
44 double led_spacing_mm;
45 std::size_t led_count;
47 constexpr double circumference()
const {
return 2.0 * kPi * radius_mm; }
48 constexpr double turn_length()
const
50 return cx_sqrt(circumference() * circumference() + pitch_mm * pitch_mm);
52 constexpr double leds_per_turn()
const {
return turn_length() / led_spacing_mm; }
53 constexpr double height()
const
55 return static_cast<double>(led_count) * led_spacing_mm / turn_length() * pitch_mm;
57 constexpr double area_per_led()
const {
return circumference() * pitch_mm / leds_per_turn(); }
66 double resolution = 0.004;
67 double density_w = 2.0;
68 double stripe_w = 5.0;
69 double invert_above = 0.5;
71 double clump_radius = 0.75;
72 double ring_min = 2.0;
73 double ring_max = 5.0;
74 double height_cap = 0.4;
77constexpr std::size_t kMaxLevels = 16;
78constexpr std::size_t kMaxOffsets = 1536;
79constexpr int kBins = 12;
84constexpr double lattice_badness(
double mu,
const Geometry& g)
86 const double N = g.leds_per_turn(), C = g.circumference();
87 const double turns = mu / N;
88 double b1x = cx_frac(turns) * C, b1y = -g.pitch_mm * turns;
89 double b2x = C, b2y = 0.0;
90 for (
int it = 0; it < 64; ++it)
92 if (b1x * b1x + b1y * b1y > b2x * b2x + b2y * b2y)
94 const double tx = b1x, ty = b1y;
100 const double n1 = b1x * b1x + b1y * b1y;
103 const double k = cx_round((b1x * b2x + b1y * b2y) / n1);
109 const double a2 = b1x * b1x + b1y * b1y, c2n = b2x * b2x + b2y * b2y;
110 const double d = cx_abs(b1x * b2x + b1y * b2y);
111 const double cell2 = a2 * c2n - d * d;
114 const double c2 = a2 + c2n - 2.0 * d;
115 const double cell = cx_sqrt(cell2);
116 const double R2 = a2 * c2n * c2 / (4.0 * cell2);
117 const double R02 = 2.0 * cell / (3.0 * kSqrt3);
118 return cx_sqrt(R2 / R02);
132 std::array<Item, kMaxOffsets> item {};
133 std::array<int, kBins> count {};
134 std::size_t size = 0;
139constexpr OffsetList make_offsets(
140 const Geometry& g,
double alpha_target,
double ring_min,
double ring_max,
double height_cap)
143 const double C = g.circumference(), Lt = g.turn_length();
144 const double sp = cx_sqrt(2.0 * g.area_per_led() / (alpha_target * kSqrt3));
145 const double rmin = ring_min * sp;
146 const double rmax = cx_min(ring_max * sp, height_cap * g.height());
149 const double ex = g.led_spacing_mm * C / Lt;
150 const double ez = g.led_spacing_mm * g.pitch_mm / Lt;
151 const std::size_t kmax =
static_cast<std::size_t
>(rmax / ez) + 1;
152 const std::size_t kend = kmax < g.led_count - 1 ? kmax : g.led_count - 1;
153 const std::size_t stride = 1 + (3 * kend) / kMaxOffsets;
154 for (std::size_t k = 1; k <= kend; k += stride)
156 const double vy =
static_cast<double>(k) * ez;
157 const double vx0 = cx_frac(
static_cast<double>(k) * ex / C) * C;
158 for (
int j = -1; j <= 1; ++j)
160 const double vx = vx0 + j * C;
161 const double r2 = vx * vx + vy * vy;
162 if (r2 < rmin * rmin || r2 > rmax * rmax || o.size >= kMaxOffsets)
164 const double s = vx / (cx_abs(vx) + vy);
165 int b =
static_cast<int>((1.0 - s) * 0.5 * kBins);
166 b = b < 0 ? 0 : (b >= kBins ? kBins - 1 : b);
167 o.item[o.size] = {
static_cast<uint16_t
>(k),
static_cast<uint8_t
>(b)};
169 ++o.count[
static_cast<std::size_t
>(b)];
176constexpr double stripe_score(
double alpha,
const OffsetList& o)
178 const double chance = alpha * alpha + (1.0 - alpha) * (1.0 - alpha);
179 std::array<double, kBins> acc {};
180 for (std::size_t i = 0; i < o.size; ++i)
182 const double t = cx_frac(
static_cast<double>(o.item[i].k) * alpha);
183 const double p11 = cx_max(0.0, alpha - t) + cx_max(0.0, alpha - (1.0 - t));
184 acc[o.item[i].bin] += (1.0 - 2.0 * alpha + 2.0 * p11) - chance;
187 for (
int b = 0; b < kBins; ++b)
189 const std::size_t u =
static_cast<std::size_t
>(b);
191 best = cx_max(best, acc[u] / o.count[u]);
198constexpr double clump_score(
double alpha,
const OffsetList& o)
201 for (std::size_t i = 0; i < o.size; ++i)
203 const double t = cx_frac(
static_cast<double>(o.item[i].k) * alpha);
204 sum += cx_max(0.0, alpha - t) + cx_max(0.0, alpha - (1.0 - t));
206 const double frac = 2.0 * sum / alpha;
207 return frac > 1.0 ? 1.0 : frac;
213 std::array<double, kMaxLevels> mu {};
214 std::array<uint32_t, kMaxLevels> A {};
215 std::array<bool, kMaxLevels> invert {};
216 std::array<double, kMaxLevels> lit {};
217 std::array<double, kMaxLevels> angle {};
218 std::array<double, kMaxLevels> quality {};
219 std::array<double, kMaxLevels> stripes {};
220 std::array<double, kMaxLevels> clumps {};
227 t.max_level = p.max_level;
228 const double N = g.leds_per_turn();
232 for (
int L = 1; L <= p.max_level && L < static_cast<int>(kMaxLevels); ++L)
235 const bool inv = f > p.invert_above;
236 const double alpha_t = inv ? 1.0 - f : f;
237 const double mu_t = 1.0 / alpha_t;
238 const OffsetList offs = make_offsets(g, alpha_t, p.ring_min, p.ring_max, p.height_cap);
239 const OffsetList close_offs = make_offsets(g, alpha_t, 0.0, p.clump_radius, 1.0);
240 double best = 1e18, bestmu = mu_t, bq = 0.0, bs = 0.0, bc = 0.0;
241 const double lo = mu_t * (1.0 - p.window), hi = mu_t * (1.0 + p.window);
242 for (
double mu = lo < 1.05 ? 1.05 : lo; mu <= hi; mu += mu_t * p.resolution)
244 const double q = lattice_badness(mu, g);
245 const double s = stripe_score(1.0 / mu, offs);
246 const double c = clump_score(1.0 / mu, close_offs);
247 const double pen = q * (1.0 + p.density_w * cx_abs(mu / mu_t - 1.0)) + p.stripe_w * s + p.clump_w * c;
257 const std::size_t l =
static_cast<std::size_t
>(L);
259 t.A[l] =
static_cast<uint32_t
>(cx_round(4294967296.0 / bestmu));
261 t.lit[l] = inv ? 1.0 - 1.0 / bestmu : 1.0 / bestmu;
262 t.angle[l] = cx_frac(bestmu / N) * 360.0;
271constexpr bool led_on(
const StepTable& t, std::size_t index,
int level)
273 if (level <= 0 || level > t.max_level)
275 const std::size_t L =
static_cast<std::size_t
>(level);
276 const uint32_t A = t.A[L];
277 const bool lattice =
static_cast<uint32_t
>(
static_cast<uint32_t
>(index) * A) < A;
278 return t.invert[L] ? !lattice : lattice;
Definition: helix_mask.hpp:41
Definition: helix_mask.hpp:128
Definition: helix_mask.hpp:126
Definition: helix_mask.hpp:62
Definition: helix_mask.hpp:212