20namespace ZenitUI::Geometry2D
26 inline constexpr int kRoundedCornerSegments = 12;
27 inline constexpr int kRoundedRingPoints = 4 * (kRoundedCornerSegments + 1);
28 inline constexpr int kCircleMaxSegments = 48;
36 template <
size_t MaxVertices,
size_t MaxIndices>
39 std::array<Vertex2D, MaxVertices> vertices{};
40 std::array<uint32_t, MaxIndices> indices{};
41 uint32_t vertexCount{0};
42 uint32_t indexCount{0};
44 std::span<const Vertex2D> vertexSpan()
const noexcept {
return {vertices.data(), vertexCount}; }
45 std::span<const uint32_t> indexSpan()
const noexcept {
return {indices.data(), indexCount}; }
46 bool empty()
const noexcept {
return indexCount == 0; }
50 vertices[vertexCount++] =
Vertex2D{pos, uv, c};
53 void pushTri(uint32_t i0, uint32_t i1, uint32_t i2)
noexcept
55 indices[indexCount++] = i0;
56 indices[indexCount++] = i1;
57 indices[indexCount++] = i2;
60 void pushQuadIndices(uint32_t tl, uint32_t tr, uint32_t br, uint32_t bl)
noexcept
82 std::array<Vec2, kRoundedRingPoints> outer{};
83 std::array<Vec2, kRoundedRingPoints> inner{};
85 bool isSolidFill{
false};
92 if (thickness <= 0.0f || r.width <= 0.0f || r.height <= 0.0f)
95 const float maxR = std::min(r.width, r.height) * 0.5f;
100 const float t = std::min(thickness, maxR);
101 if (t >= maxR - 0.01f)
103 ring.isSolidFill =
true;
107 const float outerR = std::clamp(radiusPx, 0.0f, maxR);
108 const float innerR = std::max(0.0f, outerR - t);
110 constexpr int segs = kRoundedCornerSegments;
111 constexpr float kHalfPi = 1.5707963267948966f;
112 constexpr float kStep = kHalfPi /
static_cast<float>(segs);
113 constexpr float kStartAngles[4] = {-kHalfPi, 0.0f, kHalfPi, 2.0f * kHalfPi};
115 const Vec2 outerCenters[4] = {
116 {r.x + r.width - outerR, r.y + outerR},
117 {r.x + r.width - outerR, r.y + r.height - outerR},
118 {r.x + outerR, r.y + r.height - outerR},
119 {r.x + outerR, r.y + outerR}};
121 const Vec2 innerCenters[4] = {
122 {r.x + r.width - t - innerR, r.y + t + innerR},
123 {r.x + r.width - t - innerR, r.y + r.height - t - innerR},
124 {r.x + t + innerR, r.y + r.height - t - innerR},
125 {r.x + t + innerR, r.y + t + innerR}};
128 for (
int corner = 0; corner < 4; ++corner)
130 for (
int s = 0; s <= segs; ++s)
132 const float a = kStartAngles[corner] + kStep *
static_cast<float>(s);
133 const float ca = std::cos(a);
134 const float sa = std::sin(a);
135 ring.outer[idx] = {outerCenters[corner].x + ca * outerR,
136 outerCenters[corner].y + sa * outerR};
137 ring.inner[idx] = {innerCenters[corner].x + ca * innerR,
138 innerCenters[corner].y + sa * innerR};
149 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
152 constexpr Vec2 uv{0.5f, 0.5f};
153 m.pushVertex({r.x, r.y}, uv, c);
154 m.pushVertex({r.x + r.width, r.y}, uv, c);
155 m.pushVertex({r.x + r.width, r.y + r.height}, uv, c);
156 m.pushVertex({r.x, r.y + r.height}, uv, c);
157 m.pushQuadIndices(0, 1, 2, 3);
165 if (!t.valid() || t.width <= 0 || t.height <= 0 ||
166 tint.a == 0 || dst.width <= 0.0f || dst.height <= 0.0f)
169 const float invW = 1.0f /
static_cast<float>(t.width);
170 const float invH = 1.0f /
static_cast<float>(t.height);
171 const float u0 = src.x * invW;
172 const float v0 = src.y * invH;
173 const float u1 = (src.x + src.width) * invW;
174 const float v1 = (src.y + src.height) * invH;
176 m.pushVertex({dst.x, dst.y}, {u0, v0}, tint);
177 m.pushVertex({dst.x + dst.width, dst.y}, {u1, v0}, tint);
178 m.pushVertex({dst.x + dst.width, dst.y + dst.height}, {u1, v1}, tint);
179 m.pushVertex({dst.x, dst.y + dst.height}, {u0, v1}, tint);
180 m.pushQuadIndices(0, 1, 2, 3);
188 if (!t.valid() || t.width <= 0 || t.height <= 0 ||
189 tint.a == 0 || dst.width <= 0.0f || dst.height <= 0.0f)
192 const float tw =
static_cast<float>(t.width);
193 const float th =
static_cast<float>(t.height);
195 const float sl = std::clamp(
static_cast<float>(s.left), 0.0f, tw);
196 const float sr = std::clamp(
static_cast<float>(s.right), 0.0f, tw - sl);
197 const float st = std::clamp(
static_cast<float>(s.top), 0.0f, th);
198 const float sb = std::clamp(
static_cast<float>(s.bottom), 0.0f, th - st);
200 float dl = sl, dr = sr;
201 if (dl + dr > dst.width && (dl + dr) > 0.0f)
203 const float k = dst.width / (dl + dr);
207 float dt = st, db = sb;
208 if (dt + db > dst.height && (dt + db) > 0.0f)
210 const float k = dst.height / (dt + db);
215 const float xs[4] = {dst.x, dst.x + dl, dst.x + dst.width - dr, dst.x + dst.width};
216 const float ys[4] = {dst.y, dst.y + dt, dst.y + dst.height - db, dst.y + dst.height};
217 const float us[4] = {0.0f, sl / tw, (tw - sr) / tw, 1.0f};
218 const float vs[4] = {0.0f, st / th, (th - sb) / th, 1.0f};
220 for (
int row = 0; row < 4; ++row)
221 for (
int col = 0; col < 4; ++col)
222 m.pushVertex({xs[col], ys[row]}, {us[col], vs[row]}, tint);
224 for (
int row = 0; row < 3; ++row)
226 if (ys[row + 1] <= ys[row] || vs[row + 1] <= vs[row])
228 for (
int col = 0; col < 3; ++col)
230 if (xs[col + 1] <= xs[col] || us[col + 1] <= us[col])
232 const uint32_t tl =
static_cast<uint32_t
>(row * 4 + col);
233 m.pushQuadIndices(tl, tl + 1, tl + 5, tl + 4);
243 if (c.a == 0 || thickness <= 0.0f || r.width <= 0.0f || r.height <= 0.0f)
246 constexpr Vec2 uv{0.5f, 0.5f};
247 const float maxT = std::min(r.width, r.height) * 0.5f;
248 const float t = std::min(thickness, maxT);
250 if (t >= maxT - 0.01f)
252 m.pushVertex({r.x, r.y}, uv, c);
253 m.pushVertex({r.x + r.width, r.y}, uv, c);
254 m.pushVertex({r.x + r.width, r.y + r.height}, uv, c);
255 m.pushVertex({r.x, r.y + r.height}, uv, c);
256 m.pushQuadIndices(0, 1, 2, 3);
261 m.pushVertex({r.x, r.y}, uv, c);
262 m.pushVertex({r.x + r.width, r.y}, uv, c);
263 m.pushVertex({r.x + r.width, r.y + r.height}, uv, c);
264 m.pushVertex({r.x, r.y + r.height}, uv, c);
267 m.pushVertex({r.x + t, r.y + t}, uv, c);
268 m.pushVertex({r.x + r.width - t, r.y + t}, uv, c);
269 m.pushVertex({r.x + r.width - t, r.y + r.height - t}, uv, c);
270 m.pushVertex({r.x + t, r.y + r.height - t}, uv, c);
272 for (uint32_t k = 0; k < 4; ++k)
274 const uint32_t next = (k + 1) & 3u;
275 m.pushTri(k, 4u + k, 4u + next);
276 m.pushTri(k, 4u + next, next);
285 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
288 constexpr Vec2 uv{0.5f, 0.5f};
289 auto addRect = [&](
float x,
float y,
float w,
float h)
291 if (w <= 0.0f || h <= 0.0f)
293 const uint32_t base = m.vertexCount;
294 m.pushVertex({x, y}, uv, c);
295 m.pushVertex({x + w, y}, uv, c);
296 m.pushVertex({x + w, y + h}, uv, c);
297 m.pushVertex({x, y + h}, uv, c);
298 m.pushQuadIndices(base, base + 1, base + 2, base + 3);
301 if (sides.top > 0.0f)
302 addRect(r.x, r.y, r.width, std::min(sides.top, r.height));
303 if (sides.bottom > 0.0f)
305 const float bh = std::min(sides.bottom, r.height);
306 addRect(r.x, r.y + r.height - bh, r.width, bh);
308 const float innerH = r.height - sides.top - sides.bottom;
311 if (sides.left > 0.0f)
312 addRect(r.x, r.y + sides.top, std::min(sides.left, r.width), innerH);
313 if (sides.right > 0.0f)
315 const float rw = std::min(sides.right, r.width);
316 addRect(r.x + r.width - rw, r.y + sides.top, rw, innerH);
326 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
329 constexpr Vec2 uv{0.5f, 0.5f};
330 const float maxR = std::min(r.width, r.height) * 0.5f;
331 const float rad = std::clamp(radiusPx, 0.0f, maxR);
335 m.pushVertex({r.x, r.y}, uv, c);
336 m.pushVertex({r.x + r.width, r.y}, uv, c);
337 m.pushVertex({r.x + r.width, r.y + r.height}, uv, c);
338 m.pushVertex({r.x, r.y + r.height}, uv, c);
339 m.pushQuadIndices(0, 1, 2, 3);
344 m.pushVertex({r.x + r.width * 0.5f, r.y + r.height * 0.5f}, uv, c);
346 constexpr int segs = kRoundedCornerSegments;
347 constexpr float kHalfPi = 1.5707963267948966f;
348 constexpr float kStep = kHalfPi /
static_cast<float>(segs);
349 constexpr float kStartAngles[4] = {-kHalfPi, 0.0f, kHalfPi, 2.0f * kHalfPi};
351 const Vec2 centers[4] = {
352 {r.x + r.width - rad, r.y + rad},
353 {r.x + r.width - rad, r.y + r.height - rad},
354 {r.x + rad, r.y + r.height - rad},
355 {r.x + rad, r.y + rad}};
357 for (
int corner = 0; corner < 4; ++corner)
359 for (
int s = 0; s <= segs; ++s)
361 const float a = kStartAngles[corner] + kStep *
static_cast<float>(s);
362 m.pushVertex({centers[corner].x + std::cos(a) * rad,
363 centers[corner].y + std::sin(a) * rad},
368 constexpr uint32_t ringPts =
static_cast<uint32_t
>(kRoundedRingPoints);
369 for (uint32_t i = 0; i < ringPts; ++i)
371 const uint32_t cur = 1u + i;
372 const uint32_t nxt = 1u + ((i + 1u) % ringPts);
373 m.pushTri(0u, nxt, cur);
389 if (ring.isSolidFill)
392 std::copy_n(fill.vertices.begin(), fill.vertexCount, m.vertices.begin());
393 std::copy_n(fill.indices.begin(), fill.indexCount, m.indices.begin());
394 m.vertexCount = fill.vertexCount;
395 m.indexCount = fill.indexCount;
399 constexpr Vec2 uv{0.5f, 0.5f};
400 constexpr uint32_t N =
static_cast<uint32_t
>(kRoundedRingPoints);
402 for (uint32_t i = 0; i < N; ++i)
403 m.pushVertex(ring.outer[i], uv, c);
404 for (uint32_t i = 0; i < N; ++i)
405 m.pushVertex(ring.inner[i], uv, c);
407 for (uint32_t i = 0; i < N; ++i)
409 const uint32_t next = (i + 1u) % N;
410 m.pushTri(i, N + next, next);
411 m.pushTri(i, N + i, N + next);
420 if (c.a == 0 || radius <= 0.0f)
423 constexpr Vec2 uv{0.5f, 0.5f};
424 constexpr float kTwoPi = 6.2831853071795865f;
425 const int segs = std::clamp(
static_cast<int>(std::ceil(radius * 0.75f)) + 12,
426 16, kCircleMaxSegments);
427 const float step = kTwoPi /
static_cast<float>(segs);
429 m.pushVertex(center, uv, c);
430 for (
int i = 0; i < segs; ++i)
432 const float a = step *
static_cast<float>(i);
433 m.pushVertex({center.x + std::cos(a) * radius,
434 center.y + std::sin(a) * radius},
438 const uint32_t uSegs =
static_cast<uint32_t
>(segs);
439 for (uint32_t i = 0; i < uSegs; ++i)
441 const uint32_t cur = 1u + i;
442 const uint32_t nxt = 1u + ((i + 1u) % uSegs);
443 m.pushTri(0u, nxt, cur);
Small value types shared by the layout, rendering and input layers.
NineSliceMesh buildNineSlice(TextureHandle t, NineSlice s, Rect dst, Color tint)
Build a 9-slice mesh (up to 9 quads, single indexed draw call).
Definition Geometry2D.hpp:185
RectStrokeMesh buildRectStroke(Rect r, float thickness, Color c)
Build a CSS border-box rectangular stroke strictly inside r without corner overdraw.
Definition Geometry2D.hpp:240
RoundedFillMesh buildRoundedRectFill(Rect r, float radiusPx, Color c)
Build a filled rounded rectangle (fast-paths to a 4-vertex quad when radiusPx <= 0....
Definition Geometry2D.hpp:323
BorderMesh buildBorder(Rect r, EdgeInsets sides, Color c)
Build per-edge rectangular borders in a single indexed draw call.
Definition Geometry2D.hpp:282
CircleMesh buildCircle(Vec2 center, float radius, Color c)
Build a filled circle with adaptive segment count (16..kCircleMaxSegments).
Definition Geometry2D.hpp:417
QuadMesh buildQuad(Rect r, Color c)
Build a solid-color axis-aligned quad.
Definition Geometry2D.hpp:146
RoundedStrokeMesh buildRoundedRectStroke(Rect r, float radiusPx, float thickness, Color c)
Build a CSS border-box rounded stroke mesh strictly inside r.
Definition Geometry2D.hpp:379
RoundedBorderRing buildRoundedBorderRing(Rect r, float radiusPx, float thickness)
Build the inward-extruded rounded ring for IRenderer::strokeRoundedRect.
Definition Geometry2D.hpp:89
QuadMesh buildTexturedQuad(TextureHandle t, Rect src, Rect dst, Color tint)
Build a textured quad mapping pixel sub-rect src onto logical dst.
Definition Geometry2D.hpp:162
8-bit-per-channel RGBA color, non-premultiplied.
Definition CoreTypes.hpp:78
Four-sided edge inset (used for padding, margin, borders).
Definition CoreTypes.hpp:62
Outer and inner vertex loops of a CSS border-box rounded stroke.
Definition Geometry2D.hpp:81
Fixed-capacity stack mesh returned by Geometry2D builders.
Definition Geometry2D.hpp:38
Nine-slice inset values for scalable textures.
Definition CoreTypes.hpp:163
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
Opaque handle to a GPU/software texture owned by the asset provider.
Definition CoreTypes.hpp:127
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25
Interleaved 2D vertex passed to IRenderer::drawGeometry.
Definition CoreTypes.hpp:118