34 const utility::graphic::PositionF &position,
35 const utility::graphic::OrientationF &orientation)
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;
43 const float positionX = position[0];
44 const float positionY = position[1];
45 const float positionZ = position[2];
47 const float crossX = (qy * positionZ) - (qz * positionY);
48 const float crossY = (qz * positionX) - (qx * positionZ);
49 const float crossZ = (qx * positionY) - (qy * positionX);
51 const float tX = 2.0f * crossX;
52 const float tY = 2.0f * crossY;
53 const float tZ = 2.0f * crossZ;
55 const float crossTX = (qy * tZ) - (qz * tY);
56 const float crossTY = (qz * tX) - (qx * tZ);
57 const float crossTZ = (qx * tY) - (qy * tX);
59 return utility::graphic::PositionF(positionX + (qw * tX) + crossTX,
60 positionY + (qw * tY) + crossTY,
61 positionZ + (qw * tZ) + crossTZ);
65 const std::vector<utility::math::Vector2F> &localVertices,
66 const utility::graphic::PositionF ¢er,
67 const utility::graphic::OrientationF &orientation)
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 =
76 worldVertices.push_back(utility::graphic::PositionF(
77 center[0] + rotatedPosition[0], center[1] + rotatedPosition[1],
78 center[2] + rotatedPosition[2]));
101 if (color.getAlpha() == 255 || color.getAlpha() == 0) {
108 auto blended = color.blendOver(surface);
109 return utility::graphic::Color32Bit(
110 blended.getRed(), blended.getGreen(), blended.getBlue(), 255);
114 utility::graphic::Mesh &mesh,
const utility::graphic::PositionF ¢er,
115 const std::vector<utility::graphic::PositionF> &outline,
116 const utility::graphic::Color32Bit &color)
124 for (
const auto &outlineVertex: outline) {
128 if (outline.size() >= 2) {
133 for (
size_t i = 2; i < outline.size(); ++i) {
135 mesh.addIndex(
static_cast<uint32_t
>(i));
136 mesh.addIndex(
static_cast<uint32_t
>(i + 1));
140 mesh.addIndex(
static_cast<uint32_t
>(outline.size()));
142 }
else if (outline.size() == 1) {
150 const utility::graphic::PositionF ¢er,
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)
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));
172 const int sampleCount = closed ? segments : segments + 1;
173 const float step = sweepAngle /
static_cast<float>(segments);
175 std::vector<utility::math::Vector2F> localVertices;
176 localVertices.reserve(
static_cast<std::size_t
>(sampleCount) * 2u);
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 }));
188 for (
const auto &worldVertex:
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);
200 mesh.addIndex(outerA);
201 mesh.addIndex(innerA);
202 mesh.addIndex(outerB);
204 mesh.addIndex(innerA);
205 mesh.addIndex(innerB);
206 mesh.addIndex(outerB);
211 const utility::graphic::PositionF ¢er,
212 const utility::graphic::OrientationF &orientation,
213 float halfLength,
float halfThickness,
bool dashed,
214 float dashLength,
float gapLength,
215 const utility::graphic::Color32Bit &color)
221 const float length = std::abs(halfLength);
222 const float half = std::abs(halfThickness);
224 std::vector<utility::math::Vector2F> localVertices;
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 }));
233 if (!dashed || dashLength <= 0.0f) {
234 appendQuad(-length, length);
236 const float period = dashLength + std::max(0.0f, gapLength);
237 float cursor = -length;
239 while (cursor < length) {
240 const float dashEnd = std::min(cursor + dashLength, length);
241 appendQuad(cursor, dashEnd);
242 if (period <= 0.0f) {
249 for (
const auto &worldVertex:
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);
262 mesh.addIndex(base + 0u);
263 mesh.addIndex(base + 2u);
264 mesh.addIndex(base + 3u);
269 utility::graphic::Mesh &mesh,
const utility::graphic::PositionF ¢er,
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)
279 const float pi = std::acos(-1.0f);
280 if (arcSegments <= 0) {
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);
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,
298 for (
int i = 0; i <= arcSegments; ++i) {
300 static_cast<float>(i) /
static_cast<float>(arcSegments);
301 const float angle = start + (end - start) * t;
303 utility::math::Vector2F({ cx + std::cos(angle) * r,
304 cy + std::sin(angle) * r }));
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);
314 const auto outer = buildLoop(halfWidth, halfHeight, outerRadius);
316 buildLoop(innerHalfWidth, innerHalfHeight, innerRadius);
318 for (std::size_t i = 0; i < outer.size(); ++i) {
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);
void buildRoundedStrokeMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF ¢er, 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 ¢er, 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.
void buildRingMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF ¢er, 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.
void buildTriangleFanVertices(utility::graphic::Mesh &mesh, const utility::graphic::PositionF ¢er, const std::vector< utility::graphic::PositionF > &outline, const utility::graphic::Color32Bit &color)
Fill a closed outline with a fan of triangles around its center.