FastLED 3.10.10
Loading...
Searching...
No Matches

◆ polySortBand()

void fl::gfx::detail::polySortBand ( fl::u8 * idx,
fl::i64 * keys,
const PolyEdge * edges,
int nc,
fl::i32 mid )
inline

Sort a band's active edges (idx[0..nc)) by the committed implementation's midpoint key: the FLOORED Q16.16 crossing at the band midpoint, key_e = floor((2*dy_e*x_e + B_e*mid) / (2*dy_e)), mid = a + b with a stable insertion sort (equal keys keep edge-index order).

The key is computed in i64 exactly as the committed code did, including its wrap of 2*dy*x for steep single-row edges (|2*dy*x| can exceed 2^63: |x| up to ~2^47, dy < 2^16 there) — the order that wrap produces is part of the committed output, so the wrap is reproduced bit-for-bit via unsigned arithmetic (same bits, no UB). Sorting by the exact rational x instead would change span pairing whenever two midpoints share a floored key (or the old key wrapped) and is NOT byte-identical. Cost: one i64 floor division per active edge per band (the same the committed code paid); the per-pixel kernel needs no division.

Definition at line 375 of file primitives.h.

376 {
377 for (int e = 0; e < nc; ++e) {
378 const PolyEdge &ed = edges[idx[e]];
379 // (2*dy) fits in i32; (2*dy)*x and the sum wrap mod 2^64 as in the
380 // committed i64 expression. B*mid < 2^47 never wraps.
381 const fl::u64 num = (fl::u64)(2 * (fl::i64)ed.dy) * (fl::u64)ed.x
382 + (fl::u64)(static_cast<fl::i64>(ed.B) * (fl::i64)mid);
383 keys[e] = detail::floorDivI64(static_cast<fl::i64>(num),
384 (fl::i64)2 * ed.dy);
385 }
386 for (int e = 1; e < nc; ++e) {
387 const fl::i64 kv = keys[e];
388 const fl::u8 iv = idx[e];
389 int m = e - 1;
390 while (m >= 0 && keys[m] > kv) {
391 keys[m + 1] = keys[m];
392 idx[m + 1] = idx[m];
393 --m;
394 }
395 keys[m + 1] = kv;
396 idx[m + 1] = iv;
397 }
398}
unsigned char u8
Definition s16x16x4.h:132
fl::i64 floorDivI64(fl::i64 a, fl::i64 b) FL_NO_EXCEPT
Floor-divide a by positive b (rounds toward -inf).
Definition primitives.h:270
Per-edge scanline state for drawPolygon (even-odd fill, band model).
Definition primitives.h:323
fl::i64 i64
Definition s16x16x4.h:222
fl::u64 u64
Definition s16x16x4.h:221

References fl::gfx::detail::PolyEdge::B, fl::gfx::detail::PolyEdge::dy, fl::FL_NO_EXCEPT, floorDivI64(), polySortBand(), and fl::gfx::detail::PolyEdge::x.

Referenced by drawPolygonCore(), and polySortBand().

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