guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
shape_mesh.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
24
25#include "guillaume/theme.hpp"
26
27#include <algorithm>
28#include <cmath>
29#include <cstdint>
30
31namespace guillaume::systems::shape
32{
33 utility::graphic::PositionF rotatePositionByQuaternion(
34 const utility::graphic::PositionF &position,
35 const utility::graphic::OrientationF &orientation)
36 {
37 const auto normalizedOrientation = orientation.normalized();
38 const float qx = normalizedOrientation.x;
39 const float qy = normalizedOrientation.y;
40 const float qz = normalizedOrientation.z;
41 const float qw = normalizedOrientation.w;
42
43 const float positionX = position[0];
44 const float positionY = position[1];
45 const float positionZ = position[2];
46
47 const float crossX = (qy * positionZ) - (qz * positionY);
48 const float crossY = (qz * positionX) - (qx * positionZ);
49 const float crossZ = (qx * positionY) - (qy * positionX);
50
51 const float tX = 2.0f * crossX;
52 const float tY = 2.0f * crossY;
53 const float tZ = 2.0f * crossZ;
54
55 const float crossTX = (qy * tZ) - (qz * tY);
56 const float crossTY = (qz * tX) - (qx * tZ);
57 const float crossTZ = (qx * tY) - (qy * tX);
58
59 return utility::graphic::PositionF(positionX + (qw * tX) + crossTX,
60 positionY + (qw * tY) + crossTY,
61 positionZ + (qw * tZ) + crossTZ);
62 }
63
64 std::vector<utility::graphic::PositionF> transformToWorldVertices(
65 const std::vector<utility::math::Vector2F> &localVertices,
66 const utility::graphic::PositionF &center,
67 const utility::graphic::OrientationF &orientation)
68 {
69 std::vector<utility::graphic::PositionF> worldVertices;
70 worldVertices.reserve(localVertices.size());
71 for (const auto &localVertex: localVertices) {
72 const utility::graphic::PositionF localPosition(
73 localVertex[0], localVertex[1], 0.0f);
74 const auto rotatedPosition =
75 rotatePositionByQuaternion(localPosition, orientation);
76 worldVertices.push_back(utility::graphic::PositionF(
77 center[0] + rotatedPosition[0], center[1] + rotatedPosition[1],
78 center[2] + rotatedPosition[2]));
79 }
80 return worldVertices;
81 }
82
83 utility::graphic::VertexF
84 createVertex(const utility::graphic::PositionF &position,
85 const utility::graphic::Color32Bit &color)
86 {
87 utility::graphic::VertexF vertex;
88 vertex.setPosition(position);
89 vertex.setColor(opaqueColor(color));
90 return vertex;
91 }
92
93 bool isInvisible(const utility::graphic::Color32Bit &color)
94 {
95 return color.getAlpha() == 0;
96 }
97
98 utility::graphic::Color32Bit
99 opaqueColor(const utility::graphic::Color32Bit &color)
100 {
101 if (color.getAlpha() == 255 || color.getAlpha() == 0) {
102 return color;
103 }
104 // The engine draws shapes without blending, so a translucent color
105 // would come out at full strength. Composite it over the surface it
106 // is normally drawn on instead.
107 const auto surface = getActiveColor(SchemeColorRole::Surface);
108 auto blended = color.blendOver(surface);
109 return utility::graphic::Color32Bit(
110 blended.getRed(), blended.getGreen(), blended.getBlue(), 255);
111 }
112
114 utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center,
115 const std::vector<utility::graphic::PositionF> &outline,
116 const utility::graphic::Color32Bit &color)
117 {
118 if (isInvisible(color)) {
119 return;
120 }
121
122 mesh.addVertex(createVertex(center, color));
123
124 for (const auto &outlineVertex: outline) {
125 mesh.addVertex(createVertex(outlineVertex, color));
126 }
127
128 if (outline.size() >= 2) {
129 mesh.addIndex(0);
130 mesh.addIndex(1);
131 mesh.addIndex(2);
132
133 for (size_t i = 2; i < outline.size(); ++i) {
134 mesh.addIndex(0);
135 mesh.addIndex(static_cast<uint32_t>(i));
136 mesh.addIndex(static_cast<uint32_t>(i + 1));
137 }
138
139 mesh.addIndex(0);
140 mesh.addIndex(static_cast<uint32_t>(outline.size()));
141 mesh.addIndex(1);
142 } else if (outline.size() == 1) {
143 mesh.addIndex(0);
144 mesh.addIndex(1);
145 mesh.addIndex(1);
146 }
147 }
148
149 void buildRingMesh(utility::graphic::Mesh &mesh,
150 const utility::graphic::PositionF &center,
151 const utility::graphic::OrientationF &orientation,
152 float outerHalfWidth, float outerHalfHeight,
153 float thickness, float startAngle, float sweepAngle,
154 int segments, bool closed,
155 const utility::graphic::Color32Bit &color)
156 {
157 if (isInvisible(color)) {
158 return;
159 }
160
161 if (segments <= 0) {
162 segments = 1;
163 }
164
165 const float outerWidth = std::abs(outerHalfWidth);
166 const float outerHeight = std::abs(outerHalfHeight);
167 const float innerWidth =
168 std::max(0.0f, outerWidth - std::abs(thickness));
169 const float innerHeight =
170 std::max(0.0f, outerHeight - std::abs(thickness));
171
172 const int sampleCount = closed ? segments : segments + 1;
173 const float step = sweepAngle / static_cast<float>(segments);
174
175 std::vector<utility::math::Vector2F> localVertices;
176 localVertices.reserve(static_cast<std::size_t>(sampleCount) * 2u);
177
178 for (int i = 0; i < sampleCount; ++i) {
179 const float angle = startAngle + step * static_cast<float>(i);
180 const float cos = std::cos(angle);
181 const float sin = std::sin(angle);
182 localVertices.push_back(utility::math::Vector2F(
183 { outerWidth * cos, outerHeight * sin }));
184 localVertices.push_back(utility::math::Vector2F(
185 { innerWidth * cos, innerHeight * sin }));
186 }
187
188 for (const auto &worldVertex:
189 transformToWorldVertices(localVertices, center, orientation)) {
190 mesh.addVertex(createVertex(worldVertex, color));
191 }
192
193 for (int i = 0; i < segments; ++i) {
194 const int nextIndex = closed ? (i + 1) % sampleCount : i + 1;
195 const uint32_t outerA = static_cast<uint32_t>(2 * i);
196 const uint32_t innerA = static_cast<uint32_t>(2 * i + 1);
197 const uint32_t outerB = static_cast<uint32_t>(2 * nextIndex);
198 const uint32_t innerB = static_cast<uint32_t>(2 * nextIndex + 1);
199
200 mesh.addIndex(outerA);
201 mesh.addIndex(innerA);
202 mesh.addIndex(outerB);
203
204 mesh.addIndex(innerA);
205 mesh.addIndex(innerB);
206 mesh.addIndex(outerB);
207 }
208 }
209
210 void buildLineMesh(utility::graphic::Mesh &mesh,
211 const utility::graphic::PositionF &center,
212 const utility::graphic::OrientationF &orientation,
213 float halfLength, float halfThickness, bool dashed,
214 float dashLength, float gapLength,
215 const utility::graphic::Color32Bit &color)
216 {
217 if (isInvisible(color)) {
218 return;
219 }
220
221 const float length = std::abs(halfLength);
222 const float half = std::abs(halfThickness);
223
224 std::vector<utility::math::Vector2F> localVertices;
225
226 auto appendQuad = [&localVertices, half](float start, float end) {
227 localVertices.push_back(utility::math::Vector2F({ start, -half }));
228 localVertices.push_back(utility::math::Vector2F({ end, -half }));
229 localVertices.push_back(utility::math::Vector2F({ end, half }));
230 localVertices.push_back(utility::math::Vector2F({ start, half }));
231 };
232
233 if (!dashed || dashLength <= 0.0f) {
234 appendQuad(-length, length);
235 } else {
236 const float period = dashLength + std::max(0.0f, gapLength);
237 float cursor = -length;
238
239 while (cursor < length) {
240 const float dashEnd = std::min(cursor + dashLength, length);
241 appendQuad(cursor, dashEnd);
242 if (period <= 0.0f) {
243 break;
244 }
245 cursor += period;
246 }
247 }
248
249 for (const auto &worldVertex:
250 transformToWorldVertices(localVertices, center, orientation)) {
251 mesh.addVertex(createVertex(worldVertex, color));
252 }
253
254 const uint32_t quadCount =
255 static_cast<uint32_t>(localVertices.size() / 4u);
256 for (uint32_t i = 0; i < quadCount; ++i) {
257 const uint32_t base = i * 4u;
258 mesh.addIndex(base + 0u);
259 mesh.addIndex(base + 1u);
260 mesh.addIndex(base + 2u);
261
262 mesh.addIndex(base + 0u);
263 mesh.addIndex(base + 2u);
264 mesh.addIndex(base + 3u);
265 }
266 }
267
269 utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center,
270 const utility::graphic::OrientationF &orientation,
271 const float halfWidth, const float halfHeight, const float radius,
272 const float width, int arcSegments,
273 const utility::graphic::Color32Bit &color)
274 {
275 if (isInvisible(color)) {
276 return;
277 }
278
279 const float pi = std::acos(-1.0f);
280 if (arcSegments <= 0) {
281 arcSegments = 1;
282 }
283
284 const float stroke = (std::min)({ width, halfWidth, halfHeight });
285 const float outerRadius =
286 std::max(0.0f, (std::min)({ radius, halfWidth, halfHeight }));
287 const float innerHalfWidth = halfWidth - stroke;
288 const float innerHalfHeight = halfHeight - stroke;
289 const float innerRadius = std::max(0.0f, outerRadius - stroke);
290
291 // Both loops always sample every corner arc (a zero radius collapses
292 // an arc onto its corner), so outer and inner vertices pair up 1:1.
293 const auto buildLoop = [&](float hw, float hh, float r) {
294 std::vector<utility::math::Vector2F> loop;
295 loop.reserve(static_cast<std::size_t>(4 * (arcSegments + 1)));
296 const auto appendArc = [&](float cx, float cy, float start,
297 float end) {
298 for (int i = 0; i <= arcSegments; ++i) {
299 const float t =
300 static_cast<float>(i) / static_cast<float>(arcSegments);
301 const float angle = start + (end - start) * t;
302 loop.push_back(
303 utility::math::Vector2F({ cx + std::cos(angle) * r,
304 cy + std::sin(angle) * r }));
305 }
306 };
307 appendArc(hw - r, -hh + r, -pi / 2.0f, 0.0f);
308 appendArc(hw - r, hh - r, 0.0f, pi / 2.0f);
309 appendArc(-hw + r, hh - r, pi / 2.0f, pi);
310 appendArc(-hw + r, -hh + r, pi, 3.0f * pi / 2.0f);
311 return transformToWorldVertices(loop, center, orientation);
312 };
313
314 const auto outer = buildLoop(halfWidth, halfHeight, outerRadius);
315 const auto inner =
316 buildLoop(innerHalfWidth, innerHalfHeight, innerRadius);
317
318 for (std::size_t i = 0; i < outer.size(); ++i) {
319 mesh.addVertex(createVertex(outer[i], color));
320 mesh.addVertex(createVertex(inner[i], color));
321 }
322
323 const auto count = static_cast<std::uint32_t>(outer.size());
324 for (std::uint32_t i = 0; i < count; ++i) {
325 const std::uint32_t next = (i + 1) % count;
326 const std::uint32_t outerIndex = i * 2;
327 const std::uint32_t innerIndex = i * 2 + 1;
328 const std::uint32_t outerNext = next * 2;
329 const std::uint32_t innerNext = next * 2 + 1;
330 mesh.addIndex(outerIndex);
331 mesh.addIndex(innerIndex);
332 mesh.addIndex(outerNext);
333 mesh.addIndex(outerNext);
334 mesh.addIndex(innerIndex);
335 mesh.addIndex(innerNext);
336 }
337 }
338
339} // namespace guillaume::systems::shape
utility::graphic::Color32Bit getActiveColor(SchemeColorRole role)
Get the color the active scheme gives to a role.
Mesh builders shared by the shape render systems.
void buildRoundedStrokeMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation, float halfWidth, float halfHeight, float radius, float width, int arcSegments, const utility::graphic::Color32Bit &color)
Build the border of a rounded rectangle as a band of constant width.
void buildLineMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation, float halfLength, float halfThickness, bool dashed, float dashLength, float gapLength, const utility::graphic::Color32Bit &color)
Build a straight stroke along X, solid or dashed.
utility::graphic::VertexF createVertex(const utility::graphic::PositionF &position, const utility::graphic::Color32Bit &color)
Make a mesh vertex from a position and a color.
void buildRingMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation, float outerHalfWidth, float outerHalfHeight, float thickness, float startAngle, float sweepAngle, int segments, bool closed, const utility::graphic::Color32Bit &color)
Build a ring, or a part of one, as a strip of triangles.
bool isInvisible(const utility::graphic::Color32Bit &color)
Whether a color draws nothing.
void buildTriangleFanVertices(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const std::vector< utility::graphic::PositionF > &outline, const utility::graphic::Color32Bit &color)
Fill a closed outline with a fan of triangles around its center.
utility::graphic::PositionF rotatePositionByQuaternion(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation)
Rotate a 3D point around the origin by a quaternion.
utility::graphic::Color32Bit opaqueColor(const utility::graphic::Color32Bit &color)
Turn a translucent color into the opaque color it looks like.
std::vector< utility::graphic::PositionF > transformToWorldVertices(const std::vector< utility::math::Vector2F > &localVertices, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation)
Move 2D points of a shape's plane into world space.