ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Geometry2D.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include "CoreTypes.hpp"
13
14#include <algorithm>
15#include <array>
16#include <cmath>
17#include <cstdint>
18#include <span>
19
20namespace ZenitUI::Geometry2D
21{
25
26 inline constexpr int kRoundedCornerSegments = 12;
27 inline constexpr int kRoundedRingPoints = 4 * (kRoundedCornerSegments + 1);
28 inline constexpr int kCircleMaxSegments = 48;
29
36 template <size_t MaxVertices, size_t MaxIndices>
38 {
39 std::array<Vertex2D, MaxVertices> vertices{};
40 std::array<uint32_t, MaxIndices> indices{};
41 uint32_t vertexCount{0};
42 uint32_t indexCount{0};
43
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; }
47
48 void pushVertex(Vec2 pos, Vec2 uv, Color c) noexcept
49 {
50 vertices[vertexCount++] = Vertex2D{pos, uv, c};
51 }
52
53 void pushTri(uint32_t i0, uint32_t i1, uint32_t i2) noexcept
54 {
55 indices[indexCount++] = i0;
56 indices[indexCount++] = i1;
57 indices[indexCount++] = i2;
58 }
59
60 void pushQuadIndices(uint32_t tl, uint32_t tr, uint32_t br, uint32_t bl) noexcept
61 {
62 // CCW in NDC (Y-down screen space)
63 pushTri(tl, bl, br);
64 pushTri(tl, br, tr);
65 }
66 };
67
68 using QuadMesh = StaticMesh<4, 6>;
69 using RectStrokeMesh = StaticMesh<8, 24>;
70 using BorderMesh = StaticMesh<16, 24>;
71 using NineSliceMesh = StaticMesh<16, 54>;
75
81 {
82 std::array<Vec2, kRoundedRingPoints> outer{};
83 std::array<Vec2, kRoundedRingPoints> inner{};
84 bool empty{true};
85 bool isSolidFill{false};
86 };
87
89 inline RoundedBorderRing buildRoundedBorderRing(Rect r, float radiusPx, float thickness)
90 {
92 if (thickness <= 0.0f || r.width <= 0.0f || r.height <= 0.0f)
93 return ring;
94
95 const float maxR = std::min(r.width, r.height) * 0.5f;
96 if (maxR <= 0.0f)
97 return ring;
98
99 ring.empty = false;
100 const float t = std::min(thickness, maxR);
101 if (t >= maxR - 0.01f)
102 {
103 ring.isSolidFill = true;
104 return ring;
105 }
106
107 const float outerR = std::clamp(radiusPx, 0.0f, maxR);
108 const float innerR = std::max(0.0f, outerR - t);
109
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};
114
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}};
120
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}};
126
127 int idx = 0;
128 for (int corner = 0; corner < 4; ++corner)
129 {
130 for (int s = 0; s <= segs; ++s)
131 {
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};
139 ++idx;
140 }
141 }
142 return ring;
143 }
144
146 inline QuadMesh buildQuad(Rect r, Color c)
147 {
148 QuadMesh m;
149 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
150 return m;
151
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);
158 return m;
159 }
160
162 inline QuadMesh buildTexturedQuad(TextureHandle t, Rect src, Rect dst, Color tint)
163 {
164 QuadMesh m;
165 if (!t.valid() || t.width <= 0 || t.height <= 0 ||
166 tint.a == 0 || dst.width <= 0.0f || dst.height <= 0.0f)
167 return m;
168
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;
175
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);
181 return m;
182 }
183
185 inline NineSliceMesh buildNineSlice(TextureHandle t, NineSlice s, Rect dst, Color tint)
186 {
187 NineSliceMesh m;
188 if (!t.valid() || t.width <= 0 || t.height <= 0 ||
189 tint.a == 0 || dst.width <= 0.0f || dst.height <= 0.0f)
190 return m;
191
192 const float tw = static_cast<float>(t.width);
193 const float th = static_cast<float>(t.height);
194
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);
199
200 float dl = sl, dr = sr;
201 if (dl + dr > dst.width && (dl + dr) > 0.0f)
202 {
203 const float k = dst.width / (dl + dr);
204 dl *= k;
205 dr *= k;
206 }
207 float dt = st, db = sb;
208 if (dt + db > dst.height && (dt + db) > 0.0f)
209 {
210 const float k = dst.height / (dt + db);
211 dt *= k;
212 db *= k;
213 }
214
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};
219
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);
223
224 for (int row = 0; row < 3; ++row)
225 {
226 if (ys[row + 1] <= ys[row] || vs[row + 1] <= vs[row])
227 continue;
228 for (int col = 0; col < 3; ++col)
229 {
230 if (xs[col + 1] <= xs[col] || us[col + 1] <= us[col])
231 continue;
232 const uint32_t tl = static_cast<uint32_t>(row * 4 + col);
233 m.pushQuadIndices(tl, tl + 1, tl + 5, tl + 4);
234 }
235 }
236 return m;
237 }
238
240 inline RectStrokeMesh buildRectStroke(Rect r, float thickness, Color c)
241 {
242 RectStrokeMesh m;
243 if (c.a == 0 || thickness <= 0.0f || r.width <= 0.0f || r.height <= 0.0f)
244 return m;
245
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);
249
250 if (t >= maxT - 0.01f)
251 {
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);
257 return m;
258 }
259
260 // Outer loop: 0=TL, 1=TR, 2=BR, 3=BL
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);
265
266 // Inner loop: 4=iTL, 5=iTR, 6=iBR, 7=iBL
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);
271
272 for (uint32_t k = 0; k < 4; ++k)
273 {
274 const uint32_t next = (k + 1) & 3u;
275 m.pushTri(k, 4u + k, 4u + next);
276 m.pushTri(k, 4u + next, next);
277 }
278 return m;
279 }
280
282 inline BorderMesh buildBorder(Rect r, EdgeInsets sides, Color c)
283 {
284 BorderMesh m;
285 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
286 return m;
287
288 constexpr Vec2 uv{0.5f, 0.5f};
289 auto addRect = [&](float x, float y, float w, float h)
290 {
291 if (w <= 0.0f || h <= 0.0f)
292 return;
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);
299 };
300
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)
304 {
305 const float bh = std::min(sides.bottom, r.height);
306 addRect(r.x, r.y + r.height - bh, r.width, bh);
307 }
308 const float innerH = r.height - sides.top - sides.bottom;
309 if (innerH > 0.0f)
310 {
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)
314 {
315 const float rw = std::min(sides.right, r.width);
316 addRect(r.x + r.width - rw, r.y + sides.top, rw, innerH);
317 }
318 }
319 return m;
320 }
321
323 inline RoundedFillMesh buildRoundedRectFill(Rect r, float radiusPx, Color c)
324 {
325 RoundedFillMesh m;
326 if (c.a == 0 || r.width <= 0.0f || r.height <= 0.0f)
327 return m;
328
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);
332
333 if (rad <= 0.01f)
334 {
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);
340 return m;
341 }
342
343 // Vertex 0: center of fan
344 m.pushVertex({r.x + r.width * 0.5f, r.y + r.height * 0.5f}, uv, c);
345
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};
350
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}};
356
357 for (int corner = 0; corner < 4; ++corner)
358 {
359 for (int s = 0; s <= segs; ++s)
360 {
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},
364 uv, c);
365 }
366 }
367
368 constexpr uint32_t ringPts = static_cast<uint32_t>(kRoundedRingPoints);
369 for (uint32_t i = 0; i < ringPts; ++i)
370 {
371 const uint32_t cur = 1u + i;
372 const uint32_t nxt = 1u + ((i + 1u) % ringPts);
373 m.pushTri(0u, nxt, cur);
374 }
375 return m;
376 }
377
379 inline RoundedStrokeMesh buildRoundedRectStroke(Rect r, float radiusPx, float thickness, Color c)
380 {
381 RoundedStrokeMesh m;
382 if (c.a == 0)
383 return m;
384
385 const auto ring = buildRoundedBorderRing(r, radiusPx, thickness);
386 if (ring.empty)
387 return m;
388
389 if (ring.isSolidFill)
390 {
391 const auto fill = buildRoundedRectFill(r, radiusPx, c);
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;
396 return m;
397 }
398
399 constexpr Vec2 uv{0.5f, 0.5f};
400 constexpr uint32_t N = static_cast<uint32_t>(kRoundedRingPoints);
401
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);
406
407 for (uint32_t i = 0; i < N; ++i)
408 {
409 const uint32_t next = (i + 1u) % N;
410 m.pushTri(i, N + next, next);
411 m.pushTri(i, N + i, N + next);
412 }
413 return m;
414 }
415
417 inline CircleMesh buildCircle(Vec2 center, float radius, Color c)
418 {
419 CircleMesh m;
420 if (c.a == 0 || radius <= 0.0f)
421 return m;
422
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);
428
429 m.pushVertex(center, uv, c);
430 for (int i = 0; i < segs; ++i)
431 {
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},
435 uv, c);
436 }
437
438 const uint32_t uSegs = static_cast<uint32_t>(segs);
439 for (uint32_t i = 0; i < uSegs; ++i)
440 {
441 const uint32_t cur = 1u + i;
442 const uint32_t nxt = 1u + ((i + 1u) % uSegs);
443 m.pushTri(0u, nxt, cur);
444 }
445 return m;
446 }
447
449} // namespace ZenitUI::Geometry2D
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