13using V = std::array<double, 3>;
14V
add(V
a, V
b) {
return {
a[0] +
b[0],
a[1] +
b[1],
a[2] +
b[2]}; }
15V
sub(V
a, V
b) {
return {
a[0] -
b[0],
a[1] -
b[1],
a[2] -
b[2]}; }
16V
mul(V
a,
double s) {
return {
a[0] *
s,
a[1] *
s,
a[2] *
s}; }
17double dot(V
a, V
b) {
return a[0] *
b[0] +
a[1] *
b[1] +
a[2] *
b[2]; }
18V
cross(V
a, V
b) {
return {
a[1] *
b[2] -
a[2] *
b[1],
a[2] *
b[0] -
a[0] *
b[2],
a[0] *
b[1] -
a[1] *
b[0]}; }
22 return std::all_of(
a.begin(),
a.end(), [](
double v) { return std::isfinite(v) && std::abs(v) <= 1e6; });
27void triangle(MeshBuild&
mesh, V
a, V
b, V
c,
double u0,
double u1,
double u2) {
29 const auto base =
static_cast<uint32_t
>(
mesh.getVertexCount());
31 const double us[]{u0, u1, u2};
32 for (
int i = 0; i < 3; ++i)
33 mesh.addVertex(
static_cast<float>(
points[i][0]),
static_cast<float>(
points[i][1]),
34 static_cast<float>(
points[i][2]),
static_cast<float>(
n[0]),
static_cast<float>(
n[1]),
35 static_cast<float>(
n[2]),
static_cast<float>(us[i]), i == 0 ? 0.f : 1.f);
36 mesh.addTriangle(base, base + 1, base + 2);
38void quad(MeshBuild&
m, V
a, V
b, V
c, V
d,
double u,
double v) {
42double flowingRatio(
double t) {
return 0.08 + 0.92 * std::sin(
t * std::numbers::pi * 0.5); }
46PathSample samplePath(V
p,
const std::vector<V>&
path,
const std::vector<double>& arc) {
47 double best = std::numeric_limits<double>::max(),
result = 1.;
48 for (std::size_t i = 0; i + 1 <
path.size(); ++i) {
50 const double t = std::clamp(
dot(
sub(
p,
path[i]), delta) /
dot(delta, delta), 0., 1.);
52 const double squared =
dot(difference, difference);
55 result = (arc[i] + (arc[i + 1] - arc[i]) *
t) / arc.back();
58 const double z =
p[2] -
path.front()[2];
63Result<MeshBuild> flatArrow(
const std::vector<V>&
path,
const std::vector<V>&
normals, V
tip,
double shaft,
double head,
66 std::vector<double> arc;
67 arc.reserve(
path.size());
69 for (std::size_t i = 1; i <
path.size(); ++i) arc.push_back(arc.back() +
length(
sub(
path[i],
path[i - 1])));
70 const auto taper = [&](
double t) {
return flowing ? flowingRatio(
t) : 1.; };
71 const auto progress = [&](V
p) {
return samplePath(
p,
path, arc).progress; };
72 std::vector<V> outline;
73 outline.reserve(2 *
path.size() + 3);
74 for (std::size_t i = 0; i <
path.size(); ++i)
77 outline.push_back(
tip);
79 for (std::size_t i =
path.size(); i-- > 0;)
82 for (std::size_t i = 0; outline.size() > 3 && i < outline.size();) {
83 const std::size_t
n = outline.size();
84 if (std::abs(
turn(outline[(i +
n - 1) %
n], outline[i], outline[(i + 1) %
n])) < 1e-12) {
85 outline.erase(outline.begin() +
static_cast<std::ptrdiff_t
>(i));
95 const double span = std::min(shaft * 0.7,
radius * 10.);
96 for (V neck : necks) {
97 bool shortened =
true;
98 while (shortened && outline.size() > 7) {
100 const auto found = std::find(outline.begin(), outline.end(), neck);
101 const std::size_t i =
static_cast<std::size_t
>(
found - outline.begin());
102 for (std::size_t j : {(i + outline.size() - 1) % outline.size(), (i + 1) % outline.size()}) {
103 if (
length(
sub(outline[j], neck)) < span && outline[j] != necks[0] && outline[j] != necks[1] &&
105 outline.erase(outline.begin() +
static_cast<std::ptrdiff_t
>(j));
113 std::vector<V> contour;
114 for (std::size_t i = 0; i < outline.size(); ++i) {
115 const V
p = outline[(i + outline.size() - 1) % outline.size()],
v = outline[i],
116 q = outline[(i + 1) % outline.size()];
117 const V incoming =
sub(
p,
v), outgoing =
sub(
q,
v);
118 const double cosine =
dot(
unit(incoming),
unit(outgoing));
119 const double desiredCut = 2. *
radius * (flowing &&
v !=
tip ? 3. : 1.) *
120 std::sqrt(std::max(0., (1. + cosine) * 0.5)) / std::max(1e-8, (1. - cosine) * 0.5);
121 const double cut = std::min({desiredCut,
length(incoming) * 0.45,
length(outgoing) * 0.45});
125 contour.push_back(
v);
128 for (
int j = 0; j <=
steps; ++j) {
129 const double t =
static_cast<double>(j) /
steps;
133 const std::size_t
count = contour.size();
134 std::vector<V> insetDirections;
135 for (std::size_t i = 0; i <
count; ++i) {
138 const V n0{-prev[1], prev[0], 0.}, n1{-
next[1],
next[0], 0.};
139 const double denominator = 1. +
dot(n0, n1);
141 insetDirections.push_back(
mul(
add(n0, n1), 1. / denominator));
143 std::vector<double>
radii, thicknesses;
144 for (V
point : contour) {
147 thicknesses.push_back(
h);
150 std::vector<std::vector<V>> rings;
151 const auto appendRing = [&](
double insetFactor,
double sign,
double riseFactor) {
153 for (std::size_t i = 0; i <
count; ++i) {
154 V
v =
add(contour[i],
mul(insetDirections[i],
radii[i] * insetFactor));
155 v[2] += sign * thicknesses[i] * 0.5 +
radii[i] * riseFactor;
158 rings.push_back(std::move(ring));
160 for (
int j = 0; j <=
steps; ++j) {
161 const double angle = std::numbers::pi * 0.5 * j /
steps;
162 appendRing(1. - std::sin(
angle), -1., 1. - std::cos(
angle));
164 appendRing(0., 1., -1.);
165 for (
int j = 1; j <=
steps; ++j) {
166 const double angle = std::numbers::pi * 0.5 * j /
steps;
167 appendRing(1. - std::cos(
angle), 1., std::sin(
angle) - 1.);
170 for (
const auto& ring : rings) {
171 for (std::size_t i = 0; i <
count; ++i)
172 for (std::size_t j = i + 2; j <
count; ++j) {
173 if (i == 0 && j + 1 ==
count)
continue;
174 const V
a = ring[i],
b = ring[(i + 1) %
count],
c = ring[j],
d = ring[(j + 1) %
count];
177 std::to_string(i) +
", " + std::to_string(j)));
181 mesh.setActiveGroup(
"bevel");
182 for (std::size_t
r = 0;
r + 1 < rings.size(); ++
r)
183 for (std::size_t i = 0; i <
count; ++i) {
184 const std::size_t j = (i + 1) %
count;
185 quad(
mesh, rings[
r][i], rings[
r][j], rings[
r + 1][j], rings[
r + 1][i], 0., 1.);
187 mesh.setActiveGroup(
"face");
188 std::vector<std::size_t> remaining;
189 remaining.reserve(
count);
190 for (std::size_t i = 0; i <
count; ++i) remaining.push_back(i);
191 const auto&
top = rings.back();
192 const auto&
bottom = rings.front();
193 while (remaining.size() > 2) {
194 bool clipped =
false;
195 for (std::size_t i = 0; i < remaining.size(); ++i) {
196 const auto a = remaining[(i + remaining.size() - 1) % remaining.size()],
b = remaining[i],
197 c = remaining[(i + 1) % remaining.size()];
200 for (
auto k : remaining) {
201 if (k ==
a || k ==
b || k ==
c)
continue;
211 remaining.erase(remaining.begin() +
static_cast<std::ptrdiff_t
>(i));
225MeshBuild shapeFlatSurface(
const MeshBuild&
source, V base, V
axis,
double length,
double centerZ,
double scale,
226 const std::vector<V>&
path,
bool flowing,
double shaftWidth,
double headWidth) {
230 std::vector<double> arc{0.};
231 for (std::size_t i = 1; i <
path.size(); ++i)
233 std::vector<std::pair<V, double>>
cuts;
243 const V transverse{-
axis[1],
axis[0], 0.};
244 for (
int i = 1; i < 10; ++i)
cuts.emplace_back(transverse,
dot(base, transverse) + headWidth * (i / 10. - 0.5));
247 for (
int i = 0; i <= 4; ++i)
cuts.emplace_back(
axis,
dot(base,
axis) +
length * (-0.25 + 0.25 * i / 4.));
248 for (
int i = 0; i <
source.getIndexCount(); i += 3) {
254 std::vector<std::vector<HeadVertex>> polygons(1);
255 for (
int j = 0; j < 3; ++j) {
261 for (
const auto& [clipAxis, plane] :
cuts) {
262 const auto projection = [&](V
point) {
return dot(
point, clipAxis); };
263 const auto intersection = [&](HeadVertex
a, HeadVertex
b) {
264 if (
b.position <
a.position) std::swap(
a,
b);
265 const double t = (plane - projection(
a.position)) / (projection(
b.position) - projection(
a.position));
268 return HeadVertex{
point,
a.u + (
b.u -
a.u) *
t,
a.v + (
b.v -
a.v) *
t};
270 std::vector<std::vector<HeadVertex>>
next;
271 for (
auto polygon : polygons) {
272 const double tolerance =
length * 1e-6;
273 for (
auto& vertex : polygon) {
274 const double delta = projection(vertex.position) - plane;
275 if (std::abs(delta) <= tolerance) {
276 vertex.position =
sub(vertex.position,
mul(clipAxis, delta));
281 for (
const auto& vertex : polygon) {
285 if (
minimum >= plane - tolerance ||
maximum <= plane + tolerance) {
286 next.push_back(polygon);
289 std::vector<HeadVertex> low, high;
290 for (std::size_t k = 0; k < polygon.size(); ++k) {
291 const auto a = polygon[k],
b = polygon[(k + 1) % polygon.size()];
292 double da = projection(
a.position) - plane, db = projection(
b.position) - plane;
293 if (std::abs(da) <= tolerance) da = 0.;
294 if (std::abs(db) <= tolerance) db = 0.;
295 if (da <= 0.) low.push_back(
a);
296 if (da >= 0.) high.push_back(
a);
297 if ((da < 0. && db > 0.) || (da > 0. && db < 0.)) {
298 const auto cut = intersection(
a,
b);
303 if (low.size() >= 3)
next.push_back(std::move(low));
304 if (high.size() >= 3)
next.push_back(std::move(high));
306 polygons = std::move(next);
308 for (
auto& polygon : polygons) {
309 for (
auto& vertex : polygon) {
310 const double t = std::clamp((
distance(vertex.position) /
length + 0.25) * 4., 0., 1.);
311 const double weight =
t *
t * (3. - 2. *
t);
312 double taper = 1., crown = 1.;
314 const auto sample = samplePath(vertex.position,
path, arc);
316 const double headDistance =
distance(vertex.position);
317 const double headHalfWidth = headWidth * 0.5 * std::clamp(1. - headDistance /
length, 0., 1.);
325 centerZ + (vertex.position[2] - centerZ) *
taper * crown * (1. + (
scale - 1.) *
weight);
326 vertex.position[2] =
static_cast<float>(vertex.position[2]);
328 for (std::size_t j = 1; j + 1 < polygon.size(); ++j) {
329 const HeadVertex tri[]{polygon[0], polygon[j], polygon[j + 1]};
330 const V normalVector =
332 if (
dot(normalVector, normalVector) < 1e-32)
continue;
333 const V
n =
unit(normalVector);
334 const auto first =
static_cast<uint32_t
>(
output.getVertexCount());
335 for (
const auto& vertex : tri)
337 static_cast<float>(vertex.
position[2]), static_cast<float>(
n[0]),
338 static_cast<float>(
n[1]), static_cast<float>(
n[2]), static_cast<float>(vertex.
u),
339 static_cast<float>(vertex.
v));
349Result<MeshBuild> pointFlatTip(
const MeshBuild&
source, V base, V
axis, V
tip,
double headLength) {
350 const double start = headLength * 0.55;
351 const double tolerance = headLength * 1e-6;
354 for (
int i = 0; i <
source.getVertexCount(); ++i)
356 if (front <= start + tolerance || front > headLength + tolerance)
359 for (
int i = 0; i <
source.getVertexCount(); ++i) {
362 if (
d >= front - tolerance)
365 const double t = std::clamp((
d -
start) / (front -
start), 0., 1.);
366 const double forward =
d + (headLength - front) *
t *
t * (3. - 2. *
t);
376 for (
int i = 0; i <
source.getIndexCount(); i += 3) {
380 if (
a ==
b ||
b ==
c ||
c ==
a)
continue;
390 const auto first =
static_cast<uint32_t
>(
mesh.getVertexCount());
393 mesh.addVertex(
static_cast<float>(
p[0]),
static_cast<float>(
p[1]),
static_cast<float>(
p[2]),
394 static_cast<float>(
normal[0]),
static_cast<float>(
normal[1]),
static_cast<float>(
normal[2]),
404 const V
a{ax, ay, az},
b{bx, by, bz};
418 planar_ = mode !=
"3d";
419 flat_ = mode ==
"flat";
423 if (!
valid({shaftWidth, headWidth, headLength}) || shaftWidth < 1e-5f || headWidth <= shaftWidth ||
426 shaftWidth_ = shaftWidth;
427 headWidth_ = headWidth;
428 headLength_ = headLength;
433 flowing_ = profile ==
"flowing";
438 headThicknessScale_ =
scale;
456 const V
chord = sub(b_, a_);
459 if (std::abs(dot(bend_,
unit(
chord))) >= chordLength * 0.499 || (planar_ && (a_[2] != b_[2] || bend_[2] != 0.)))
461 const V control = add(mul(add(a_, b_), 0.5), bend_);
462 const auto sampleCapacity =
static_cast<std::size_t
>(segments_) + 2;
464 points.reserve(sampleCapacity);
466 std::vector<double> arc;
467 arc.reserve(sampleCapacity);
469 for (
int i = 1; i <= segments_; ++i) {
470 const double t =
static_cast<double>(i) / segments_;
471 const V
p = i == segments_
473 : add(add(mul(a_, (1. -
t) * (1. -
t)), mul(control, 2. *
t * (1. -
t))), mul(b_,
t *
t));
477 const double total = arc.back();
480 std::size_t cut =
points.size() - 2;
481 while (cut > 0 &&
length(sub(
points[cut], b_)) < headLength_) --cut;
483 for (
int i = 0; i < 48; ++i) {
484 const V
mid = mul(add(lo, hi), 0.5);
490 const V base = mul(add(lo, hi), 0.5);
496 arc.push_back(arc.back() + last);
499 const V headAxis =
unit(sub(b_, base));
500 if (flat_ && flowing_) {
502 const double neckSpan = std::min(headLength_ * 0.5,
length(sub(base,
points.front())) * 0.5);
505 const double t = std::clamp(1. -
length(
offset) / neckSpan, 0., 1.);
506 const double weight =
t *
t * (3. - 2. *
t);
507 const V aligned = add(base, mul(headAxis, dot(
offset, headAxis)));
511 std::vector<V> tangents,
normals;
512 tangents.reserve(
points.size());
514 for (std::size_t i = 0; i <
points.size(); ++i) {
517 tangents.push_back(
t);
521 const V
reference = i == 0 ? (std::abs(
t[2]) < 0.9 ? V{0., 0., 1.} : V{0., 1., 0.}) :
normals.back();
528 const int sides = planar_ ? 2 : sides_;
529 const auto ringPoint = [&](std::size_t i,
int j,
double radius) {
531 const double angle = 2. * std::numbers::pi * j /
sides;
538 flatArrow(
points,
normals, b_, shaftWidth_, headWidth_, thickness_, bevelRadius_, bevelSegments_, flowing_);
541 if (headThicknessScale_ != 1. || flowing_)
542 mesh = shapeFlatSurface(
mesh, base, headAxis, headLength_, a_[2], headThicknessScale_,
points, flowing_,
543 shaftWidth_, headWidth_);
545 mesh.setActiveGroup(
"shaft");
546 for (std::size_t i = 0; i + 1 <
points.size(); ++i)
547 for (
int j = 0; j < (planar_ ? 1 :
sides); ++j) {
548 const int k = (j + 1) %
sides;
549 quad(
mesh, ringPoint(i, j, shaftWidth_ / 2.), ringPoint(i, k, shaftWidth_ / 2.),
550 ringPoint(i + 1, k, shaftWidth_ / 2.), ringPoint(i + 1, j, shaftWidth_ / 2.), arc[i] / total,
553 const std::size_t
end =
points.size() - 1;
554 mesh.setActiveGroup(
"head");
556 triangle(
mesh, ringPoint(
end, 1, headWidth_ / 2.), b_, ringPoint(
end, 0, headWidth_ / 2.),
557 arc.back() / total, 1., arc.back() / total);
559 for (
int j = 0; j <
sides; ++j) {
560 const int k = (j + 1) %
sides;
561 triangle(
mesh, ringPoint(
end, j, headWidth_ / 2.), ringPoint(
end, k, headWidth_ / 2.), b_,
562 arc.back() / total, arc.back() / total, 1.);
563 quad(
mesh, ringPoint(
end, j, shaftWidth_ / 2.), ringPoint(
end, k, shaftWidth_ / 2.),
564 ringPoint(
end, k, headWidth_ / 2.), ringPoint(
end, j, headWidth_ / 2.), arc.back() / total,
566 triangle(
mesh,
points[0], ringPoint(0, k, shaftWidth_ / 2.), ringPoint(0, j, shaftWidth_ / 2.), 0., 0.,
572 auto pointed = pointFlatTip(
mesh, base, headAxis, b_, headLength_);
574 mesh = std::move(pointed).takeValue();
577 for (
int i = 0; i <
mesh.getIndexCount(); i += 3) {
579 for (
int j = 0; j < 3; ++j) {
583 if (
length(cross(sub(
p[1],
p[0]), sub(
p[2],
p[0]))) < 1e-16)
586 if (flat_ && flowing_) {
588 std::map<V, V> accumulated;
589 for (
int i = 0; i <
mesh.getIndexCount(); i += 3) {
591 for (
int j = 0; j < 3; ++j) {
592 const int k =
mesh.getIndex(i + j);
593 p[j] = {
mesh.getPositionX(k),
mesh.getPositionY(k),
mesh.getPositionZ(k)};
596 for (
int j = 0; j < 3; ++j) {
597 const V
a =
unit(sub(
p[(j + 1) % 3],
p[j])),
b =
unit(sub(
p[(j + 2) % 3],
p[j]));
598 const double angle = std::acos(std::clamp(dot(
a,
b), -1., 1.));
599 accumulated[
p[j]] = add(accumulated[
p[j]], mul(
normal,
angle));
602 for (
int i = 0; i <
mesh.getVertexCount(); ++i) {
604 const V sum = accumulated.at(
point);
607 const V average =
unit(sum);
608 const V face{
mesh.getNormalX(i),
mesh.getNormalY(i),
mesh.getNormalZ(i)};
610 const V
normal = dot(average, face) >= 0.2588190451 ? average : face;
611 for (
int j = 0; j < 3; ++j)
612 mesh.normals()[
static_cast<std::size_t
>(i) * 3 + j] =
static_cast<float>(
normal[j]);
AuthorityStoreHandleRef reference
std::array< double, 10 > q
std::vector< std::uint32_t > indices
std::vector< float > normals
std::vector< float > positions
std::array< float, 3 > position
std::array< float, 3 > scale
std::shared_ptr< const std::vector< glm::vec2 > > points
RoadLaneDirection direction
AbilityActivationGroup group
std::map< Cell, int > best
TacticalUnit::TurnResources turn
std::array< float, 4 > radii
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
Result< void > setResolution(int curveSegments, int radialSides)
Set tessellation budgets; failure preserves state.
Result< void > setBevel(float thickness, float radius, int segments)
Configure the flat mode's thin extrusion and rounded rim.
Result< MeshBuild > buildMeshResult() const
Build a new owning mesh without changing this builder or any previously built mesh.
Result< void > setFlatProfile(std::string_view profile)
Select flat geometry's uniform outline or tapered, softly rounded flowing profile.
Result< void > setBend(float x, float y, float z)
Set quadratic Bezier control offset from the A/B midpoint; zero gives a straight line.
Result< void > setEndpoints(float ax, float ay, float az, float bx, float by, float bz)
Set finite world-space endpoints; failure preserves previous endpoints.
Result< void > setSize(float shaftWidth, float headWidth, float headLength)
Set positive full shaft width, full head width and head length, each at most 1e6.
Result< void > setHeadThicknessScale(float scale)
Set the flat arrow head's thickness relative to its shaft, with a short shoulder ramp.
Result< void > setMode(std::string_view mode)
Choose "2d" (XY ribbon), "3d" (closed tube/cone), or "flat" (beveled XY slab).
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
eve::Diagnostic Diagnostic
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
ForceVector add(ForceVector a, ForceVector b)
double dot(ForceVector a, ForceVector b)
Vec3 mul(Vec3 a, float scale)
double sample(const Heightmap &map, double u, double v)
Sample.
Result< int > invalid(std::string message)
Invalid.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.