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

◆ drawPolygonCore()

template<typename PixelT, typename Coord, bool Overwrite, int MAXN, int CHUNK>
void fl::gfx::detail::drawPolygonCore ( Canvas< PixelT > & canvas,
const PixelT & color,
fl::span< const PolyPoint< Coord > > pts )
inline

Definition at line 1326 of file primitives.h.

1327 {
1328 FL_STATIC_ASSERT(MAXN >= 3 && MAXN <= 256, "drawPolygon: MAXN must be in [3, 256] (edge indices are u8)");
1329 FL_STATIC_ASSERT(CHUNK > 0 && (CHUNK & (CHUNK - 1)) == 0, "drawPolygon: CHUNK must be a power of two");
1330 const fl::i32 n = static_cast<fl::i32>(pts.size());
1331 if (n < 3) return;
1332 PixelT* pixels = canvas.pixels;
1333 int width = canvas.width;
1334 int height = canvas.height;
1335 if (width <= 0 || height <= 0) return;
1336
1337 const int nv = (n < MAXN) ? n : MAXN;
1338
1339 // Convert vertices to Q16.16 (clamped to ±8191 px — i32-safe)
1340 fl::i32 vx[MAXN];
1341 fl::i32 vy[MAXN];
1342 for (int i = 0; i < nv; ++i) {
1343 vx[i] = detail::toQ16Clamped(pts[i].x);
1344 vy[i] = detail::toQ16Clamped(pts[i].y);
1345 }
1346
1347 // Build per-edge scanline state. Two i64 floor divisions per edge
1348 // (anchor crossing + step) are the only divisions in this function.
1349 PolyEdge edges[MAXN];
1350 int nedges = 0;
1351 int ymin = height, ymax = -1;
1352 for (int i = 0; i < nv; ++i) {
1353 const int j = (i + 1 == nv) ? 0 : (i + 1);
1354 fl::i32 dy = vy[j] - vy[i];
1355 if (dy == 0) continue; // horizontal edge: ignored (even-odd convention)
1356 // Orient edge top-to-bottom: B measured from the top vertex
1357 fl::i32 ytop, xtop, B;
1358 if (dy > 0) { ytop = vy[i]; xtop = vx[i]; B = vx[j] - vx[i]; }
1359 else { ytop = vy[j]; xtop = vx[j]; B = vx[i] - vx[j]; }
1360 const fl::i32 dy2 = (dy > 0) ? dy : -dy; // span height, > 0
1361 const fl::i32 ybot = ytop + dy2;
1362
1363 // Active pixel rows: the row containing ytop through the row
1364 // containing ybot (floor/ceil rule; rows are half-open so a vertex
1365 // on a row boundary belongs to exactly one edge's row range).
1366 int py0 = ytop >> 16;
1367 int py1 = (ybot - 1) >> 16;
1368 if (py0 < 0) py0 = 0;
1369 if (py1 >= height) py1 = height - 1;
1370 if (py0 > py1) continue; // edge off-canvas vertically
1371
1372 PolyEdge& ed = edges[nedges++];
1373 ed.py0 = py0;
1374 ed.py1 = py1;
1375 ed.dy = dy2;
1376 ed.B = B;
1377 ed.ytop = ytop;
1378 ed.ybot = ybot;
1379
1380 // Exact crossing at py0's row center (one i64 floor division):
1381 // x = xtop + floor(w0 * B / dy2), w0 = (py0<<16) + 32768 - ytop
1382 const fl::i64 w0 = (static_cast<fl::i64>(py0) << 16) + 32768 - ytop;
1383 const fl::i64 prod = w0 * B;
1384 const fl::i64 q = detail::floorDivI64(prod, dy2);
1385 ed.x = xtop + q;
1386 // 65536*B = s*dy2 + rem, rem in [0, dy2). s is the per-row x
1387 // advance (used by the carry on multi-row edges only) and, with
1388 // rem, yields the row's x half-span hw = ceil(32768*|B|/dy2)
1389 // without a second division:
1390 // B >= 0: 2*hw*dy2 = s*dy2 + rem ->
1391 // hw = (s>>1) + (s&1) + (s even && rem > 0)
1392 // B < 0: -s = ceil(65536*|B|/dy2) -> hw = ceil(-s/2)
1393 const fl::i64 p2 = static_cast<fl::i64>(65536) * B;
1394 const fl::i64 s = detail::floorDivI64(p2, dy2);
1395 const fl::i32 rem = static_cast<fl::i32>(p2 - s * dy2);
1396 ed.hw = (B >= 0)
1397 ? ((s >> 1) + (s & 1) + ((s & 1) == 0 && rem > 0 ? 1 : 0))
1398 : (((-s) >> 1) + ((-s) & 1));
1399 if (dy2 >= 65536) {
1400 ed.step = s;
1401 ed.rem = rem;
1402 ed.t = static_cast<fl::i32>(prod - q * dy2); // in [0, dy2)
1403 } else {
1404 // Span < 1 px: may still span two canvas rows (a row-boundary
1405 // crossing), but the carry stays a no-op (step = rem = t = 0):
1406 // x keeps its py0-row value and the per-row extent (x +/- hw)
1407 // boxes the line the kernel integrates — see PolyEdge.
1408 ed.step = 0;
1409 ed.rem = 0;
1410 ed.t = 0;
1411 }
1412 if (ed.py0 < ymin) ymin = ed.py0;
1413 if (ed.py1 > ymax) ymax = ed.py1;
1414 }
1415 if (nedges == 0) return;
1416
1417 // Scan rows
1418 fl::i32 ev[MAXN + 2]; // band boundaries: row ends + vertex y's in row
1419 fl::u8 idx[MAXN]; // a band's active edges, sorted by midpoint key
1420 fl::i64 keys[MAXN]; // per-edge midpoint sort keys (see polySortBand)
1421 int lx0a[MAXN]; // per-row per-edge column extents (left, right),
1422 int lx1a[MAXN]; // clamped to [-1, width] (see polyColClamped)
1423 fl::i32 accb[CHUNK]; // per-row CHUNK-px coverage accumulator (T units)
1424
1425 for (int py = ymin; py <= ymax; ++py) {
1426 const fl::i32 yc = (static_cast<fl::i32>(py) << 16) + 32768;
1427
1428 // Band boundaries: the row ends plus every vertex strictly inside
1429 // the row (a vertex exactly on a row boundary coincides with a band
1430 // edge — the floor/ceil row rule already splits the edges there).
1431 int nev = 0;
1432 ev[nev++] = -32768;
1433 ev[nev++] = 32768;
1434 for (int i = 0; i < nv; ++i) {
1435 if ((vy[i] >> 16) == py && (vy[i] & 0xFFFF) != 0)
1436 ev[nev++] = vy[i] - yc;
1437 }
1438 // Insertion sort (nev <= MAXN + 2)
1439 for (int a = 1; a < nev; ++a) {
1440 const fl::i32 v = ev[a];
1441 int b = a - 1;
1442 while (b >= 0 && ev[b] > v) { ev[b + 1] = ev[b]; --b; }
1443 ev[b + 1] = v;
1444 }
1445
1446 // Per-row column extents of each active edge (x is at the row
1447 // center): the line stays within x +/- hw over the row, so px
1448 // outside [lx0, lx1] is a full/zero shortcut for polyLineT28.
1449 // floor(v / 65536) == v >> 16 — shifts, not divisions. The loop
1450 // also yields the row extent actually touched (xmin/xmax): a span
1451 // pair's range is [min(L.lx0, R.lx0), max(L.lx1, R.lx1)] — it
1452 // can reach BETWEEN two off-canvas edges (e.g. a polygon covering
1453 // the whole canvas), so the bounds are the min lx0 / max lx1 over
1454 // ALL row-active edges, clamped once. Pairs partition the edges
1455 // (odd count: the unpaired last edge only widens the range), so
1456 // this superset can only add no-op chunks that the fill loop
1457 // skips; the exact-x ordering of polySortBand only affects span
1458 // pairing.
1459 int minLx0 = 0, maxLx1 = 0;
1460 int nactive = 0;
1461 for (int e = 0; e < nedges; ++e) {
1462 const PolyEdge& ed = edges[e];
1463 if (py < ed.py0 || py > ed.py1) continue;
1464 lx0a[e] = detail::polyColClamped(ed.x - ed.hw, width);
1465 lx1a[e] = detail::polyColClamped(ed.x + ed.hw, width);
1466 if (nactive++ == 0) { minLx0 = lx0a[e]; maxLx1 = lx1a[e]; }
1467 else {
1468 if (lx0a[e] < minLx0) minLx0 = lx0a[e];
1469 if (lx1a[e] > maxLx1) maxLx1 = lx1a[e];
1470 }
1471 }
1472 int xmin = minLx0, xmax = maxLx1;
1473 if (nactive == 0) { xmin = width; xmax = -1; }
1474 else {
1475 if (xmin < 0) xmin = 0;
1476 if (xmax >= width) xmax = width - 1;
1477 }
1478
1479 // Per-band active-edge list and its midpoint order (polySortBand)
1480 // depend only on the row and the band bounds a, b — never on the
1481 // column chunk. Compute them once per row (bidx/bnc/bfull) and
1482 // reuse for every chunk: the committed code rebuilt and re-sorted
1483 // each band for every chunk (one i64 sort key per active edge per
1484 // chunk — pure redundancy for CHUNK == 1, where there is one chunk
1485 // per pixel column). Rows whose extent is empty (off-canvas
1486 // polygon) skip the prepass entirely: nothing would be filled.
1487 fl::u8 bidx[MAXN + 1][MAXN];
1488 fl::u8 bnc[MAXN + 1];
1489 fl::i32 bfull[MAXN + 1];
1490 if (xmin <= xmax) {
1491 for (int k = 0; k + 1 < nev; ++k) {
1492 const fl::i32 a = ev[k], b = ev[k + 1];
1493 int nc = 0;
1494 if (a < b) {
1495 for (int e = 0; e < nedges; ++e) {
1496 const PolyEdge& ed = edges[e];
1497 if (py >= ed.py0 && py <= ed.py1 &&
1498 ed.ytop < yc + b && ed.ybot > yc + a)
1499 idx[nc++] = (fl::u8)e;
1500 }
1501 if (nc >= 2) polySortBand(idx, keys, edges, nc, a + b);
1502 // Full-pixel coverage over this band: (b - a) *
1503 // kPolyOne >> 16 == (b - a) << 12 exactly (0 < b - a
1504 // < 2^16: both bounds lie inside the row).
1505 bfull[k] = (b - a) << 12;
1506 }
1507 bnc[k] = (fl::u8)nc;
1508 for (int e = 0; e < nc; ++e) bidx[k][e] = idx[e];
1509 }
1510 }
1511
1512 // Fill CHUNK-px chunks: accumulate coverage over the bands, then
1513 // quantize once per pixel.
1514 // An empty row extent (xmin > xmax, e.g. every active edge right of
1515 // the canvas) skipped the prepass, so bnc/bidx are uninitialized:
1516 // start past xmax so the chunk loop never reads them.
1517 const int csFirst = (xmin <= xmax) ? (xmin & ~(CHUNK - 1)) : xmax + 1;
1518 for (int cs = csFirst; cs <= xmax; cs += CHUNK) {
1519 for (int i = 0; i < CHUNK; ++i) accb[i] = 0;
1520 const int ce = (cs + CHUNK <= xmax + 1) ? cs + CHUNK : xmax + 1;
1521 for (int k = 0; k + 1 < nev; ++k) {
1522 const int nc = bnc[k];
1523 if (nc < 2) continue;
1524 const fl::i32 a = ev[k], b = ev[k + 1];
1525 // Full-pixel coverage over this band (row prepass).
1526 const fl::i64 full = (fl::i64)bfull[k];
1527 for (int p = 0; p + 1 < nc; p += 2) {
1528 const PolyEdge& L = edges[bidx[k][p]];
1529 const PolyEdge& R = edges[bidx[k][p + 1]];
1530 const int Ll0 = lx0a[bidx[k][p]], Ll1 = lx1a[bidx[k][p]];
1531 const int Rl0 = lx0a[bidx[k][p + 1]], Rl1 = lx1a[bidx[k][p + 1]];
1532 int pl = (Ll0 < Rl0) ? Ll0 : Rl0;
1533 if (pl < cs) pl = cs;
1534 int ph = (Ll1 > Rl1) ? Ll1 : Rl1;
1535 if (ph >= ce) ph = ce - 1;
1536 if (pl > ph) continue;
1537 fl::i64 uL = (fl::i64)L.dy * (L.x - ((fl::i64)pl << 16));
1538 fl::i64 uR = (fl::i64)R.dy * (R.x - ((fl::i64)pl << 16));
1539 const fl::i64 suL = -((fl::i64)L.dy << 16);
1540 const fl::i64 suR = -((fl::i64)R.dy << 16);
1541 for (int px = pl; px <= ph; ++px) {
1542 fl::i32 TL = (px > Ll1) ? 0
1543 : (px < Ll0) ? (fl::i32)full
1544 : polyLineT28(L.B, L.dy, uL, a, b);
1545 fl::i32 TR = (px > Rl1) ? 0
1546 : (px < Rl0) ? (fl::i32)full
1547 : polyLineT28(R.B, R.dy, uR, a, b);
1548 const fl::i32 d = TR - TL;
1549 if (d > 0) accb[px - cs] += d;
1550 uL += suL;
1551 uR += suR;
1552 }
1553 }
1554 }
1555 PixelT* row = pixels + py * width + cs;
1556 for (int i = 0; i < ce - cs; ++i) {
1557 const fl::i32 v = accb[i];
1558 if (v <= 0) continue;
1559 const fl::u8 br = (v >= kPolyOne)
1560 ? 255
1561 : static_cast<fl::u8>(((fl::u64)v * 255u
1562 + (fl::u64)(kPolyOne >> 1)) >> 28);
1563 if (br == 0) continue;
1564 PixelT c = color;
1565 c.nscale8(br);
1566 if (Overwrite) row[i] = c;
1567 else row[i] += c;
1568 }
1569 }
1570
1571 // Carry edge crossings to the next row's center (exact; t + rem
1572 // < 2*dy < 2^31, so the signed compare is safe)
1573 for (int e = 0; e < nedges; ++e) {
1574 PolyEdge& ed = edges[e];
1575 if (py >= ed.py0 && py <= ed.py1) {
1576 ed.x += ed.step;
1577 ed.t += ed.rem;
1578 if (ed.t >= ed.dy) {
1579 ed.t -= ed.dy;
1580 ++ed.x;
1581 }
1582 }
1583 }
1584 }
1585}
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
constexpr fl::size size() const FL_NO_EXCEPT
Definition span.h:483
unsigned char u8
Definition s16x16x4.h:132
int polyColClamped(fl::i64 v, int width) FL_NO_EXCEPT
Pixel column floor(v / 65536) of a row-extent bound v = x +/- hw, clamped to [-1, width].
Definition primitives.h:291
fl::i32 toQ16Clamped(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16, clamped to ±8191 px (NaN -> 0).
Definition primitives.h:265
fl::i32 polyLineT28(fl::i32 B, fl::i32 D, fl::i64 u, fl::i32 ta, fl::i32 tb) FL_NO_EXCEPT
Exact left-of-line coverage of one pixel for one polygon edge.
Definition primitives.h:541
void polySortBand(fl::u8 *idx, fl::i64 *keys, const PolyEdge *edges, int nc, fl::i32 mid) FL_NO_EXCEPT
Sort a band's active edges (idx[0..nc)) by the committed implementation's midpoint key: the FLOORED Q...
Definition primitives.h:375
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
static const fl::i32 kPolyOne
Full-pixel coverage in T units (see polyLineT28).
Definition primitives.h:340
Per-edge scanline state for drawPolygon (even-odd fill, band model).
Definition primitives.h:323
unsigned char u8
Definition stdint.h:132
u8 u8 height
Definition blur.h:186
fl::i64 i64
Definition s16x16x4.h:222
u8 width
Definition blur.h:186
fl::u64 u64
Definition s16x16x4.h:221
#define FL_STATIC_ASSERT(...)

References fl::B, fl::gfx::detail::PolyEdge::B, canvas, fl::gfx::detail::PolyEdge::dy, fl::FL_NO_EXCEPT, FL_STATIC_ASSERT, floorDivI64(), fl::height, fl::gfx::detail::PolyEdge::hw, kPolyOne, polyColClamped(), polyLineT28(), polySortBand(), fl::gfx::detail::PolyEdge::py0, fl::gfx::detail::PolyEdge::py1, fl::gfx::detail::PolyEdge::rem, fl::gfx::detail::PolyEdge::step, fl::gfx::detail::PolyEdge::t, toQ16Clamped(), fl::width, fl::gfx::detail::PolyEdge::x, fl::x, fl::y, fl::gfx::detail::PolyEdge::ybot, and fl::gfx::detail::PolyEdge::ytop.

Referenced by fl::gfx::drawPolygon().

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