fix: stroke arcs and splines analytically — opaque core, angle-independent weight

LICE_Arc never reaches opacity and ThickFLine's width is minor-axis. One
distance-to-polyline coverage mask, blended once, replaces both.
This commit is contained in:
2026-08-01 13:18:21 -04:00
parent ae9019465e
commit 2e09776342
16 changed files with 1001 additions and 72 deletions
+178
View File
@@ -0,0 +1,178 @@
#include "core/ui/stroke_aa.h"
#include <algorithm>
#include <cmath>
namespace reasampler::ui {
namespace {
inline float clamp01(float v) { return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v); }
} // namespace
void StrokeCanvas::reset(const Rect& bounds) {
bounds_ = bounds;
if (bounds_.empty()) {
bounds_ = Rect{};
return;
}
const std::size_t area =
static_cast<std::size_t>(bounds_.width) * static_cast<std::size_t>(bounds_.height);
if (coverage_.size() < area) coverage_.resize(area);
rowLo_.assign(static_cast<std::size_t>(bounds_.height), bounds_.width);
rowHi_.assign(static_cast<std::size_t>(bounds_.height), 0);
}
float StrokeCanvas::coverageAt(int x, int y) const {
if (!contains(bounds_, x, y)) return 0.0f;
const int rel = x - bounds_.x;
if (rel < rowLo(y) || rel >= rowHi(y)) return 0.0f;
return rowData(y)[rel];
}
// Grows a row's valid span to cover [x0, x1) (canvas-relative), zero-filling only the cells that
// become valid. Cost is bounded by the growth, so a whole stroke stays O(ink).
void StrokeCanvas::extendRow(int y, int x0, int x1) {
const std::size_t r = static_cast<std::size_t>(y - bounds_.y);
float* row = coverage_.data() + r * static_cast<std::size_t>(bounds_.width);
int lo = rowLo_[r];
int hi = rowHi_[r];
if (hi <= lo) {
std::fill(row + x0, row + x1, 0.0f);
rowLo_[r] = x0;
rowHi_[r] = x1;
return;
}
// A stroke can revisit a row far from where it left it (an arc touches most rows on both
// sides of the circle), so the gap between the old span and the new one must be zeroed too.
if (x0 < lo) {
std::fill(row + x0, row + lo, 0.0f);
rowLo_[r] = x0;
}
if (x1 > hi) {
std::fill(row + hi, row + x1, 0.0f);
rowHi_[r] = x1;
}
}
void StrokeCanvas::addPiece(float ax, float ay, float bx, float by, float halfWidth) {
const float reach = halfWidth + 0.5f; // beyond this the coverage is 0
const float dx = bx - ax;
const float dy = by - ay;
const float len2 = dx * dx + dy * dy;
const float invLen2 = len2 > 0.0f ? 1.0f / len2 : 0.0f;
int x0 = static_cast<int>(std::floor((std::min)(ax, bx) - reach));
int x1 = static_cast<int>(std::ceil((std::max)(ax, bx) + reach)) + 1;
int y0 = static_cast<int>(std::floor((std::min)(ay, by) - reach));
int y1 = static_cast<int>(std::ceil((std::max)(ay, by) + reach)) + 1;
x0 = (std::max)(x0, bounds_.x);
y0 = (std::max)(y0, bounds_.y);
x1 = (std::min)(x1, bounds_.right());
y1 = (std::min)(y1, bounds_.bottom());
if (x0 >= x1 || y0 >= y1) return;
const int relX0 = x0 - bounds_.x;
const int relX1 = x1 - bounds_.x;
for (int y = y0; y < y1; ++y) {
extendRow(y, relX0, relX1);
float* row = coverage_.data() + static_cast<std::size_t>(y - bounds_.y) *
static_cast<std::size_t>(bounds_.width);
const float pyc = static_cast<float>(y) + 0.5f;
const float qy = pyc - ay;
for (int x = x0; x < x1; ++x) {
const float qx = static_cast<float>(x) + 0.5f - ax;
const float t = clamp01((qx * dx + qy * dy) * invLen2);
const float ex = qx - dx * t;
const float ey = qy - dy * t;
const float cov = clamp01(reach - std::sqrt(ex * ex + ey * ey));
float& dst = row[x - bounds_.x];
if (cov > dst) dst = cov;
}
}
}
void StrokeCanvas::addSegment(float ax, float ay, float bx, float by, float halfWidth) {
if (bounds_.empty() || halfWidth <= 0.0f) return;
const float dx = bx - ax;
const float dy = by - ay;
const float len2 = dx * dx + dy * dy;
if (len2 <= kMaxPieceLen * kMaxPieceLen) {
addPiece(ax, ay, bx, by, halfWidth);
return;
}
const int pieces = static_cast<int>(std::sqrt(len2) / kMaxPieceLen) + 1;
float px = ax;
float py = ay;
for (int i = 1; i <= pieces; ++i) {
const float t = static_cast<float>(i) / static_cast<float>(pieces);
const float qx = ax + dx * t;
const float qy = ay + dy * t;
addPiece(px, py, qx, qy, halfWidth);
px = qx;
py = qy;
}
}
Rect strokeBounds(const StrokePoint* pts, std::size_t count, float halfWidth, const Rect& clip) {
if (pts == nullptr || count == 0 || halfWidth <= 0.0f || clip.empty()) return Rect{};
float minX = pts[0].x, maxX = pts[0].x, minY = pts[0].y, maxY = pts[0].y;
for (std::size_t i = 1; i < count; ++i) {
minX = (std::min)(minX, pts[i].x);
maxX = (std::max)(maxX, pts[i].x);
minY = (std::min)(minY, pts[i].y);
maxY = (std::max)(maxY, pts[i].y);
}
const float reach = halfWidth + 0.5f;
const int x0 = (std::max)(clip.x, static_cast<int>(std::floor(minX - reach)));
const int y0 = (std::max)(clip.y, static_cast<int>(std::floor(minY - reach)));
const int x1 = (std::min)(clip.right(), static_cast<int>(std::ceil(maxX + reach)) + 1);
const int y1 = (std::min)(clip.bottom(), static_cast<int>(std::ceil(maxY + reach)) + 1);
if (x0 >= x1 || y0 >= y1) return Rect{};
return Rect::ltrb(x0, y0, x1, y1);
}
void strokePolyline(StrokeCanvas& canvas, const StrokePoint* pts, std::size_t count,
float halfWidth, const Rect& clip) {
canvas.reset(strokeBounds(pts, count, halfWidth, clip));
if (canvas.bounds().empty()) return;
if (count == 1) {
canvas.addSegment(pts[0].x, pts[0].y, pts[0].x, pts[0].y, halfWidth);
return;
}
for (std::size_t i = 1; i < count; ++i) {
canvas.addSegment(pts[i - 1].x, pts[i - 1].y, pts[i].x, pts[i].y, halfWidth);
}
}
void appendArc(std::vector<StrokePoint>& out, float cx, float cy, float radius, float startRad,
float endRad, float flatnessPx) {
if (radius <= 0.0f) {
out.push_back(StrokePoint{cx, cy});
return;
}
if (!(flatnessPx > 0.0f)) flatnessPx = kArcFlatnessPx;
// Chord sagitta: r*(1 - cos(step/2)) <= flatness. A flatness at or past the diameter admits
// the whole sweep in one chord, which is what keeps `maxStep` strictly positive.
const float cosHalf = (std::max)(-1.0f, 1.0f - flatnessPx / radius);
const float maxStep = 2.0f * std::acos(cosHalf);
const float sweep = endRad - startRad;
int segments = 1;
if (maxStep > 0.0f) {
const float wanted = std::ceil(std::fabs(sweep) / maxStep);
segments = wanted >= static_cast<float>(kMaxArcSegments)
? kMaxArcSegments
: (std::max)(1, static_cast<int>(wanted));
} else {
// A radius large enough that flatness/radius underflows the cosine's resolution. The cap
// is the termination guarantee, not a quality choice.
segments = kMaxArcSegments;
}
out.reserve(out.size() + static_cast<std::size_t>(segments) + 1);
for (int i = 0; i <= segments; ++i) {
const float a = startRad + sweep * (static_cast<float>(i) / static_cast<float>(segments));
out.push_back(StrokePoint{cx + radius * std::sin(a), cy - radius * std::cos(a)});
}
}
} // namespace reasampler::ui