载入中...
搜索中...
未找到
RoadBake.cpp
浏览该文件的文档.
4
5#include "common/Diagnostic.h"
9
10#include <algorithm>
11#include <array>
12#include <cmath>
13#include <functional>
14#include <limits>
15#include <unordered_map>
16#include <unordered_set>
17#include <utility>
18#include <vector>
19
20namespace eve::procgen::road {
21using namespace detail;
22namespace detail {
23
24
25V3 operator+(V3 a, V3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
26V3 operator-(V3 a, V3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
27V3 operator*(V3 a, float s) { return {a.x * s, a.y * s, a.z * s}; }
28float dot(V3 a, V3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
29V3 cross(V3 a, V3 b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
30float length(V3 a) { return std::sqrt(dot(a, a)); }
32 const float l = length(a);
33 return l > 1e-8f ? a * (1.f / l) : V3{0.f, 1.f, 0.f};
34}
35
36
38 return static_cast<std::size_t>(mesh.getVertexCount()) + static_cast<std::size_t>(mesh.getIndexCount()) >
39 options.maximumMeshElements;
40}
41
42float junctionTrimDistance(float pathLength, float requestedRadius) {
43 const float maximum = std::max(0.f, pathLength * 0.5f - 0.5f);
44 return std::min(std::max(0.f, requestedRadius), maximum);
45}
46
47int circularArcSegments(float radius, float angle, float maximumError) {
48 if (radius <= maximumError) return 4;
49 const float halfStep = std::acos(std::clamp(1.f - maximumError / radius, -1.f, 1.f));
50 if (!std::isfinite(halfStep) || halfStep <= 1e-5f) return 64;
51 return std::clamp(static_cast<int>(std::ceil(angle / (2.f * halfStep))), 4, 64);
52}
53
54int quadraticSegments(V3 start, V3 control, V3 end, float angle, float maximumError) {
55 const float secondDifference = length(start - control * 2.f + end);
56 const int errorSegments =
57 std::max(1, static_cast<int>(std::ceil(std::sqrt(secondDifference / (8.f * maximumError)))));
58 const int angleSegments = 3 + static_cast<int>(angle * 3.f);
59 return std::clamp(std::max(errorSegments, angleSegments), 4, 64);
60}
61
64 auto kind = path.setKindResult("catmullRom");
65 if (!kind.ok()) return Result<SplinePath>::failure(kind.status());
66 path.setClosed(false);
67 for (const auto& p : edge.controlPoints) {
68 SplinePoint sp;
69 sp.alignStraightStart = p.alignStraightStart;
70 sp.alignStraightEnd = p.alignStraightEnd;
71 sp.circularFromEnd = p.circularFromEnd;
72 sp.spanControl = static_cast<eve::procgen::SplineSpanControl>(p.pathControl);
73 sp.spanStrength = p.splineStrength;
74 sp.x = p.x;
75 sp.y = p.y;
76 sp.z = p.z;
77 sp.scaleX = p.widthScale;
78 sp.scaleXInterpolation = edge.style.widthInterpolation == RoadWidthInterpolation::CatmullRom
81 sp.rollDegrees = p.rollDegrees;
82 sp.rollPivotSide = (-p.rollCenterOffset) * 2.f * asphaltHalfWidth(edge);
83 sp.worldUpFrame = edge.style.frameOrientation == RoadFrameOrientation::WorldUp;
84 sp.rollInterpolation = edge.style.rollInterpolation == RoadRollInterpolation::SmoothStep
87 auto added = path.addPointResult(sp);
88 if (!added.ok()) return Result<SplinePath>::failure(added.status());
89 }
90 return Result<SplinePath>::success(std::move(path));
91}
92
94 if (edge.controlPoints.size() < 2) return {1.f, 0.f, 0.f};
95 const auto& adjacent =
96 edge.from == node.id ? edge.controlPoints[1] : edge.controlPoints[edge.controlPoints.size() - 2];
97 return normalize(V3{adjacent.x - node.x, 0.f, adjacent.z - node.z});
98}
99
101 float half = 0.5f * edge.style.laneWidth * static_cast<float>(edge.lanesForward + edge.lanesBackward);
102 if (!edge.style.crossSection.empty()) {
103 half = 0.f;
104 const auto& points = edge.style.crossSection;
105 const std::size_t count = edge.style.crossSectionClosed ? points.size() : points.size() - 1;
106 for (std::size_t i = 0; i < count; ++i)
108 half = std::max({half, std::fabs(points[i].side), std::fabs(points[(i + 1) % points.size()].side)});
109 }
110 return half;
111}
112
114float scaledProfileSide(float side, float asphaltHalf, float scale) {
115 if (scale == 1.f) return side;
116 const float magnitude = std::fabs(side);
117 if (magnitude <= asphaltHalf) return side * scale;
118 return std::copysign(asphaltHalf * scale + magnitude - asphaltHalf, side);
119}
120
122 float result = 0.f;
123 if (profile.explicitMaterials) {
124 const std::size_t count = profile.closed ? profile.points.size() : profile.points.size() - 1;
125 for (std::size_t i = 0; i < count; ++i)
126 if (profile.points[i].material == RoadMaterial::Asphalt)
127 result = std::max({result, std::fabs(profile.points[i].side),
128 std::fabs(profile.points[(i + 1) % profile.points.size()].side)});
129 } else
130 for (const auto& point : profile.points)
131 if (point.material == RoadMaterial::Asphalt) result = std::max(result, std::fabs(point.side));
132 return result;
133}
134
136 float scale = 0.f;
137 float minimum = 16.f;
138 for (const auto& point : edge.controlPoints) {
139 scale = std::max(scale, point.widthScale);
140 minimum = std::min(minimum, point.widthScale);
141 }
142 if (edge.style.widthInterpolation == RoadWidthInterpolation::CatmullRom) scale += .125f * (scale - minimum);
143 return asphaltHalfWidth(edge) * scale;
144}
145
146float junctionSocketDistance(const RoadNetwork& network, const RoadNode& node, const RoadEdge& edge, float pathLength) {
147 if (!node.connection.ports.empty()) return connectionSocketDistance(node, edge, pathLength);
148 float required = node.junctionRadius;
151
152 // Only the immediate clockwise/counter-clockwise arms bound this mouth.
153 // Letting every incident arm participate makes dense junctions grow with
154 // unrelated opposite/diagonal roads and creates overlapping aprons.
155 const RoadEdge* clockwise = nullptr;
156 const RoadEdge* counterClockwise = nullptr;
157 float clockwiseAngle = std::numeric_limits<float>::max();
158 float counterClockwiseAngle = std::numeric_limits<float>::max();
159 for (const auto& other : network.edges()) {
160 if (other.id == edge.id || (other.from != node.id && other.to != node.id)) continue;
161 const V3 otherDirection = authoredOutwardDirection(node, other);
162 const float cosine = std::clamp(dot(direction, otherDirection), -1.f, 1.f);
163 const float signedAngle =
164 std::atan2(direction.x * otherDirection.z - direction.z * otherDirection.x, cosine);
165 const float angle = std::fabs(signedAngle);
166 if (signedAngle < 0.f && angle < clockwiseAngle) {
167 clockwise = &other;
168 clockwiseAngle = angle;
169 } else if (signedAngle >= 0.f && angle < counterClockwiseAngle) {
170 counterClockwise = &other;
171 counterClockwiseAngle = angle;
172 }
173 }
174
175 auto constrainTo = [&](const RoadEdge* other) {
176 if (other == nullptr) return;
177 const V3 otherDirection = authoredOutwardDirection(node, *other);
178 const float cosine = std::clamp(dot(direction, otherDirection), -1.f, 1.f);
179 const float sine = std::sqrt(std::max(0.f, 1.f - cosine * cosine));
180 // Collinear continuations constrain no corner; near-parallel duplicate
181 // arms are rejected by the network validator before reaching bake.
182 if (sine < 0.08f) return;
183 const float tangentDistance = (asphaltHalfWidth(*other) + halfWidth * std::fabs(cosine)) / sine;
184 required = std::max(required, tangentDistance + 0.35f);
185 };
186 constrainTo(clockwise);
187 if (counterClockwise != clockwise) constrainTo(counterClockwise);
188 return junctionTrimDistance(pathLength, required);
189}
190
191
192float planeHeight(const JunctionPlane& plane, float x, float z) {
193 return plane.origin.y + plane.gradeX * (x - plane.origin.x) + plane.gradeZ * (z - plane.origin.z);
194}
195
198 JunctionPlane plane{{node.x, node.y, node.z}};
199 float xx = 0.f, xz = 0.f, zz = 0.f, xy = 0.f, zy = 0.f;
200 for (const auto& edge : network.edges()) {
201 if (edge.from != node.id && edge.to != node.id) continue;
202 auto spline = edgeToSpline(edge);
203 if (!spline.ok()) continue;
204 auto pathLength = spline.value().lengthResult(16);
205 if (!pathLength.ok()) continue;
206 const float trim = junctionSocketDistance(network, node, edge, pathLength.value());
207 const float d = edge.from == node.id ? trim : pathLength.value() - trim;
208 auto frame = spline.value().travelFrameResult(d, "clamp", 16);
209 if (!frame.ok()) continue;
210 const float dx = frame.value().sample.x - node.x;
211 const float dz = frame.value().sample.z - node.z;
212 const float dy = frame.value().sample.y - node.y;
213 const float weight =
214 std::max(1.f, edge.style.laneWidth * static_cast<float>(edge.lanesForward + edge.lanesBackward));
215 xx += weight * dx * dx;
216 xz += weight * dx * dz;
217 zz += weight * dz * dz;
218 xy += weight * dx * dy;
219 zy += weight * dz * dy;
220 }
221 const float determinant = xx * zz - xz * xz;
222 if (std::fabs(determinant) > 1e-5f) {
223 plane.gradeX = (xy * zz - zy * xz) / determinant;
224 plane.gradeZ = (zy * xx - xy * xz) / determinant;
225 }
226 // A road junction is a local construction plane, not a terrain patch. Limit
227 // the fitted grade so a single malformed arm cannot turn it into a wall.
228 const float grade = std::sqrt(plane.gradeX * plane.gradeX + plane.gradeZ * plane.gradeZ);
229 if (grade > 0.25f) {
230 plane.gradeX *= 0.25f / grade;
231 plane.gradeZ *= 0.25f / grade;
232 }
233 plane.up = normalize(V3{-plane.gradeX, 1.f, -plane.gradeZ});
234 return plane;
235}
236
238 if (!node.connection.ports.empty()) return validateConnectionSeams(network, node);
239 for (const auto& edge : network.edges()) {
240 if ((edge.from != node.id && edge.to != node.id) || edge.style.crossSection.empty()) continue;
241 if (!edge.style.crossSectionClosed && edge.style.crossSection.size() == 2u &&
242 std::fabs(edge.style.crossSection[0].up) < 1e-4f && std::fabs(edge.style.crossSection[1].up) < 1e-4f &&
243 std::fabs(edge.style.crossSection[0].side + edge.style.crossSection[1].side) < 1e-4f)
244 continue;
245 auto standard = edge.style;
246 standard.crossSection.clear();
247 standard.crossSectionClosed = false;
248 auto canonical = makeRoadProfile(standard, edge.lanesForward, edge.lanesBackward);
249 if (!canonical.ok()) return Result<void>::failure(canonical.status());
250 auto samePoint = [](const auto& a, const auto& b) {
251 return std::fabs(a.side - b.side) < 1e-4f && std::fabs(a.up - b.up) < 1e-4f;
252 };
253 for (const auto& point : edge.style.crossSection)
254 if (!std::any_of(canonical.value().points.begin(), canonical.value().points.end(),
255 [&](const auto& p) { return samePoint(point, p); }))
256 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
257 "nonstandard sections require a compatible custom connection asset", "junction");
258 for (const auto& point : canonical.value().points)
259 if (!std::any_of(edge.style.crossSection.begin(), edge.style.crossSection.end(),
260 [&](const auto& p) { return samePoint(point, p); }))
261 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
262 "custom profile does not match the procedural junction section", "junction");
263 }
264 std::vector<const RoadEdge*> incident;
265 std::vector<V3> directions;
266 for (const auto& edge : network.edges()) {
267 if (edge.from != node.id && edge.to != node.id) continue;
268 incident.push_back(&edge);
270 }
271 for (std::size_t first = 0; directions.size() >= 3u && first < directions.size(); ++first)
272 for (std::size_t second = first + 1; second < directions.size(); ++second)
273 if (dot(directions[first], directions[second]) > 0.9999905f)
274 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
275 "junction contains two arms with indistinguishable outgoing directions",
276 "junction");
277
278 const JunctionPlane plane = fitJunctionPlane(network, node);
279 if (incident.size() < 4u) return Result<void>::success();
280 for (const RoadEdge* edge : incident) {
281 auto spline = edgeToSpline(*edge);
282 if (!spline.ok()) return Result<void>::failure(spline.status());
283 auto pathLength = spline.value().lengthResult(24);
284 if (!pathLength.ok()) return Result<void>::failure(pathLength.status());
285 const float trim = junctionSocketDistance(network, node, *edge, pathLength.value());
286 const float distance = edge->from == node.id ? trim : pathLength.value() - trim;
287 auto frame = spline.value().travelFrameResult(distance, "clamp", 24);
288 if (!frame.ok()) return Result<void>::failure(frame.status());
289 const float expected = planeHeight(plane, frame.value().sample.x, frame.value().sample.z);
290 const float tolerance = std::max(0.35f, asphaltHalfWidth(*edge) * 0.15f);
291 if (std::fabs(frame.value().sample.y - expected) > tolerance)
292 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
293 "junction arm sockets cannot share one bounded construction plane", "junction");
294 }
295 return Result<void>::success();
296}
297
299 weight = std::clamp(weight, 0.f, 1.f);
300 const float targetY = planeHeight(plane, frame.sample.x, frame.sample.z);
301 frame.sample.y += (targetY - frame.sample.y) * weight;
302
303 const V3 oldForward = normalize(V3{frame.forwardX, frame.forwardY, frame.forwardZ});
304 const V3 oldSide = normalize(V3{frame.sideX, frame.sideY, frame.sideZ});
305 V3 targetForward =
306 normalize(V3{oldForward.x, plane.gradeX * oldForward.x + plane.gradeZ * oldForward.z, oldForward.z});
307 V3 targetSide = normalize(cross(plane.up, targetForward));
308 if (dot(targetSide, oldSide) < 0.f) targetSide = targetSide * -1.f;
309 V3 targetUp = normalize(cross(targetForward, targetSide));
310 if (targetUp.y < 0.f) targetUp = targetUp * -1.f;
311
312 const V3 forward = normalize(oldForward * (1.f - weight) + targetForward * weight);
313 const V3 side = normalize(oldSide * (1.f - weight) + targetSide * weight);
314 V3 up = normalize(cross(forward, side));
315 if (dot(up, targetUp) < 0.f) up = up * -1.f;
316 frame.forwardX = forward.x;
317 frame.forwardY = forward.y;
318 frame.forwardZ = forward.z;
319 frame.sideX = side.x;
320 frame.sideY = side.y;
321 frame.sideZ = side.z;
322 frame.upX = up.x;
323 frame.upY = up.y;
324 frame.upZ = up.z;
325}
326
327float signedAreaXZ(const std::vector<V3>& polygon) {
328 float area = 0.f;
329 for (std::size_t i = 0; i < polygon.size(); ++i) {
330 const V3& a = polygon[i];
331 const V3& b = polygon[(i + 1) % polygon.size()];
332 area += a.x * b.z - b.x * a.z;
333 }
334 return area * 0.5f;
335}
336
337float crossXZ(V3 a, V3 b, V3 c) { return (b.x - a.x) * (c.z - a.z) - (b.z - a.z) * (c.x - a.x); }
338
339bool isPointInTriangleXZ(V3 p, V3 a, V3 b, V3 c, float orientation) {
340 constexpr float epsilon = 1e-5f;
341 // Boundary points on a sampled curve must not veto the ear; only a point
342 // strictly inside the candidate triangle makes that diagonal invalid.
343 return crossXZ(a, b, p) * orientation > epsilon && crossXZ(b, c, p) * orientation > epsilon &&
344 crossXZ(c, a, p) * orientation > epsilon;
345}
346
348TriangulationStatus triangulateBoundary(const std::vector<V3>& polygon,
349 std::vector<std::array<std::size_t, 3>>& triangles,
350 BoundaryPointPolicy boundaryPolicy) {
351 if (polygon.size() < 3) return TriangulationStatus::Degenerate;
352 const float area = signedAreaXZ(polygon);
353 if (std::fabs(area) < 1e-5f) return TriangulationStatus::Degenerate;
354 const float orientation = area > 0.f ? 1.f : -1.f;
355 std::vector<std::size_t> remaining(polygon.size());
356 for (std::size_t i = 0; i < remaining.size(); ++i) remaining[i] = i;
357 triangles.clear();
358 triangles.reserve(polygon.size() - 2);
359 std::size_t guard = polygon.size() * polygon.size();
360 while (remaining.size() > 3 && guard-- > 0) {
361 bool clipped = false;
362 for (std::size_t i = 0; i < remaining.size(); ++i) {
363 const std::size_t previous = remaining[(i + remaining.size() - 1) % remaining.size()];
364 const std::size_t current = remaining[i];
365 const std::size_t next = remaining[(i + 1) % remaining.size()];
366 if (crossXZ(polygon[previous], polygon[current], polygon[next]) * orientation <= 1e-5f) continue;
367 bool contains = false;
368 for (const std::size_t candidate : remaining) {
369 if (candidate == previous || candidate == current || candidate == next) continue;
370 const bool inclusive =
371 boundaryPolicy == BoundaryPointPolicy::IncludeBoundary &&
372 crossXZ(polygon[previous], polygon[current], polygon[candidate]) * orientation >= -1e-5f &&
373 crossXZ(polygon[current], polygon[next], polygon[candidate]) * orientation >= -1e-5f &&
374 crossXZ(polygon[next], polygon[previous], polygon[candidate]) * orientation >= -1e-5f;
375 if (inclusive || isPointInTriangleXZ(polygon[candidate], polygon[previous], polygon[current],
376 polygon[next], orientation)) {
377 contains = true;
378 break;
379 }
380 }
381 if (contains) continue;
382 triangles.push_back({previous, current, next});
383 remaining.erase(remaining.begin() + static_cast<std::ptrdiff_t>(i));
384 clipped = true;
385 break;
386 }
387 if (!clipped) return TriangulationStatus::Degenerate;
388 }
389 if (remaining.size() == 3) triangles.push_back({remaining[0], remaining[1], remaining[2]});
390 return triangles.size() + 2 == polygon.size() ? TriangulationStatus::Complete : TriangulationStatus::Degenerate;
391}
392
393float laneCenterOffset(const RoadStyle& style, int lanesForward, int lanesBackward, int laneIndex,
395 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(lanesForward + lanesBackward);
396 if (lanesBackward <= 0 || direction == RoadLaneDirection::Backward)
397 return -asphaltHalf + style.laneWidth * (static_cast<float>(laneIndex) + 0.5f);
398 const float opposingBoundary = -asphaltHalf + style.laneWidth * static_cast<float>(lanesBackward);
399 return opposingBoundary + style.laneWidth * (static_cast<float>(laneIndex) + 0.5f);
400}
401
402void appendBox(MeshBuild& mesh, V3 center, V3 side, V3 up, V3 forward, float hx, float hy, float hz,
404 mesh.setActiveGroup(roadMaterialGroup(material));
405 const V3 corners[8] = {
406 center + side * -hx + up * -hy + forward * -hz, center + side * hx + up * -hy + forward * -hz,
407 center + side * hx + up * hy + forward * -hz, center + side * -hx + up * hy + forward * -hz,
408 center + side * -hx + up * -hy + forward * hz, center + side * hx + up * -hy + forward * hz,
409 center + side * hx + up * hy + forward * hz, center + side * -hx + up * hy + forward * hz,
410 };
411 const int faces[6][4] = {{0, 1, 2, 3}, {4, 7, 6, 5}, {0, 4, 5, 1}, {3, 2, 6, 7}, {0, 3, 7, 4}, {1, 5, 6, 2}};
412 const V3 normals[6] = {forward * -1.f, forward, up * -1.f, up, side * -1.f, side};
413 for (int f = 0; f < 6; ++f) {
414 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
415 for (int c = 0; c < 4; ++c) {
416 const V3& p = corners[faces[f][c]];
417 mesh.addVertex(p.x, p.y, p.z, normals[f].x, normals[f].y, normals[f].z, static_cast<float>(c & 1),
418 static_cast<float>((c >> 1) & 1));
419 }
420 mesh.addTriangle(base, base + 1, base + 2);
421 mesh.addTriangle(base, base + 2, base + 3);
422 }
423}
424
425void appendStripQuad(MeshBuild& mesh, V3 a, V3 b, V3 c, V3 d, V3 normal, float u0, float u1, float v0, float v1,
427 mesh.setActiveGroup(roadMaterialGroup(material));
428 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
429 mesh.addVertex(a.x, a.y, a.z, normal.x, normal.y, normal.z, u0, v0);
430 mesh.addVertex(b.x, b.y, b.z, normal.x, normal.y, normal.z, u1, v0);
431 mesh.addVertex(c.x, c.y, c.z, normal.x, normal.y, normal.z, u1, v1);
432 mesh.addVertex(d.x, d.y, d.z, normal.x, normal.y, normal.z, u0, v1);
434 const V3 firstAreaNormal = across ? cross(b - a, d - a) : cross(b - a, c - a);
435 const V3 secondAreaNormal = across ? cross(c - b, d - b) : cross(c - a, d - a);
436 if (dot(firstAreaNormal, firstAreaNormal) > 1e-12f) mesh.addTriangle(base, base + 1, base + (across ? 3 : 2));
437 if (dot(secondAreaNormal, secondAreaNormal) > 1e-12f) mesh.addTriangle(base + (across ? 1 : 0), base + 2, base + 3);
438}
439
441void appendOrientedQuad(MeshBuild& mesh, V3 a, V3 b, V3 c, V3 d, V3 normal, float u0, float u1, float v0, float v1,
443 const V3 n = normalize(normal);
444 // Use both constituent triangles so a deliberately collapsed edge still
445 // gets the winding of its non-degenerate half.
446 const V3 geometricNormal = cross(b - a, c - a) + cross(c - a, d - a);
447 if (dot(geometricNormal, geometricNormal) <= 1e-12f) return;
448 if (dot(geometricNormal, n) < 0.f) {
449 appendStripQuad(mesh, a, d, c, b, n, u0, u1, v1, v0, material, diagonal);
450 } else {
451 appendStripQuad(mesh, a, b, c, d, n, u0, u1, v0, v1, material, diagonal);
452 }
453}
454
456 const V3 n = normalize(normal);
457 const V3 areaNormal = cross(b - a, c - a);
458 if (dot(areaNormal, areaNormal) <= 1e-12f) return;
459 const float uvScale = 1.f / std::max(uvMeters, 0.1f);
460 mesh.setActiveGroup(roadMaterialGroup(material));
461 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
462 mesh.addVertex(a.x, a.y, a.z, n.x, n.y, n.z, a.x * uvScale, a.z * uvScale);
463 mesh.addVertex(b.x, b.y, b.z, n.x, n.y, n.z, b.x * uvScale, b.z * uvScale);
464 mesh.addVertex(c.x, c.y, c.z, n.x, n.y, n.z, c.x * uvScale, c.z * uvScale);
465 if (dot(areaNormal, n) < 0.f)
466 mesh.addTriangle(base, base + 2, base + 1);
467 else
468 mesh.addTriangle(base, base + 1, base + 2);
469}
470
471Result<void> addDeckAndPiers(MeshBuild& mesh, const std::vector<SplineFrameSample>& frames, const RoadStyle& style,
472 float halfWidth, float asphaltHalf, bool includePiers, std::size_t budget) {
473 if (frames.size() < 2) return Result<void>::success();
474 double travel = 0.;
475 for (std::size_t i = 1; i < frames.size(); ++i)
476 travel += length(V3{frames[i].sample.x - frames[i - 1].sample.x, frames[i].sample.y - frames[i - 1].sample.y,
477 frames[i].sample.z - frames[i - 1].sample.z});
478 const double piers = includePiers && style.pierSpacing > 1e-3f ? std::ceil(travel / style.pierSpacing) : 0.;
479 const auto used = static_cast<std::size_t>(mesh.getVertexCount()) + static_cast<std::size_t>(mesh.getIndexCount());
480 if (!std::isfinite(piers) || piers > static_cast<double>(budget / 60u))
481 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "bridge piers exceed the mesh/work budget",
482 "mesh");
483 const std::size_t needed = (frames.size() - 1) * 10u + static_cast<std::size_t>(piers) * 60u;
484 if (used > budget || needed > budget - used)
485 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "bridge deck exceeds mesh budget", "mesh");
486 // Deck underside strip.
487 for (int ring = 0; ring < static_cast<int>(frames.size()) - 1; ++ring) {
488 const auto& f0 = frames[static_cast<std::size_t>(ring)];
489 const auto& f1 = frames[static_cast<std::size_t>(ring + 1)];
490 const V3 o0{f0.sample.x, f0.sample.y, f0.sample.z};
491 const V3 o1{f1.sample.x, f1.sample.y, f1.sample.z};
492 const V3 s0{f0.sideX, f0.sideY, f0.sideZ};
493 const V3 s1{f1.sideX, f1.sideY, f1.sideZ};
494 const V3 u0{f0.upX, f0.upY, f0.upZ};
495 const V3 u1{f1.upX, f1.upY, f1.upZ};
496 const V3 a = o0 + s0 * -scaledProfileSide(halfWidth, asphaltHalf, f0.sample.scaleX) + u0 * -style.deckThickness;
497 const V3 b = o0 + s0 * scaledProfileSide(halfWidth, asphaltHalf, f0.sample.scaleX) + u0 * -style.deckThickness;
498 const V3 c = o1 + s1 * scaledProfileSide(halfWidth, asphaltHalf, f1.sample.scaleX) + u1 * -style.deckThickness;
499 const V3 d = o1 + s1 * -scaledProfileSide(halfWidth, asphaltHalf, f1.sample.scaleX) + u1 * -style.deckThickness;
500 appendStripQuad(mesh, a, b, c, d, normalize((u0 + u1) * -0.5f), 0.f, 1.f, 0.f, 1.f, RoadMaterial::Deck);
501 }
502
503 if (!includePiers || style.pierSpacing <= 1e-3f) return Result<void>::success();
504
505 float traveled = 0.f;
506 float nextPier = style.pierSpacing * 0.5f;
507 for (std::size_t i = 1; i < frames.size(); ++i) {
508 const auto& a = frames[i - 1];
509 const auto& b = frames[i];
510 const V3 pa{a.sample.x, a.sample.y, a.sample.z};
511 const V3 pb{b.sample.x, b.sample.y, b.sample.z};
512 const float seg = length(pb - pa);
513 if (seg <= 1e-5f) continue;
514 while (nextPier <= traveled + seg + 1e-4f) {
515 const float t = std::clamp((nextPier - traveled) / seg, 0.f, 1.f);
516 const V3 pos = pa + (pb - pa) * t;
517 if (pos.y > style.pierClearance) {
518 // World-up pier: never reuse Frenet side (can flip / tilt and skew the box).
519 V3 fwdFlat{pb.x - pa.x, 0.f, pb.z - pa.z};
520 const float fwdLen = length(fwdFlat);
521 fwdFlat = fwdLen > 1e-5f ? fwdFlat * (1.f / fwdLen) : V3{1.f, 0.f, 0.f};
522 const V3 up{0.f, 1.f, 0.f};
523 const V3 side = normalize(cross(up, fwdFlat));
524 const float pierH = pos.y - style.deckThickness;
525 if (pierH > 0.2f) {
526 const V3 center{pos.x, pierH * 0.5f, pos.z};
527 appendBox(mesh, center, side, up, fwdFlat, style.pierWidth * 0.5f, pierH * 0.5f,
528 style.pierDepth * 0.5f, RoadMaterial::Pier);
529 }
530 }
531 nextPier += style.pierSpacing;
532 if (nextPier > 1.0e7f) break;
533 }
534 traveled += seg;
535 }
536 return Result<void>::success();
537}
538
539Result<void> addLaneMarkings(MeshBuild& mesh, const std::vector<SplineFrameSample>& frames, const RoadEdge& edge,
540 float pathLength, const RoadBakeOptions& options) {
541 if (frames.size() < 2) return Result<void>::success();
542 const auto& style = edge.style;
543 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(edge.lanesForward + edge.lanesBackward);
544 auto paintLine = [&](float lateral, bool dashed, RoadMaterial material) -> Result<void> {
545 if (dashed && style.dashLength <= 1e-5f) return Result<void>::success();
546 const bool continuous = !dashed || style.dashGap <= 1e-5f;
547 // Retain the authored phase at the trim, then measure the emitted polyline.
548 double traveled = static_cast<double>(frames.front().sample.normalizedDistance) * pathLength;
549 for (std::size_t i = 1; i < frames.size(); ++i) {
550 const auto& a = frames[i - 1];
551 const auto& b = frames[i];
552 const V3 pa{a.sample.x, a.sample.y, a.sample.z};
553 const V3 pb{b.sample.x, b.sample.y, b.sample.z};
554 const V3 sa{a.sideX, a.sideY, a.sideZ};
555 const V3 sb{b.sideX, b.sideY, b.sideZ};
556 const V3 ua{a.upX, a.upY, a.upZ};
557 const V3 ub{b.upX, b.upY, b.upZ};
558 const float seg = length(pb - pa);
559 if (seg <= 1e-5f) continue;
560 const double startDistance = traveled;
561 const double distanceSpan = seg;
562 traveled += distanceSpan;
563 auto emitSpan = [&](float t0, float t1) -> Result<void> {
564 constexpr std::size_t quadElements = 10u;
565 const std::size_t currentElements =
566 static_cast<std::size_t>(mesh.getVertexCount()) + static_cast<std::size_t>(mesh.getIndexCount());
567 if (currentElements > options.maximumMeshElements ||
568 options.maximumMeshElements - currentElements < quadElements)
569 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
570 "road markings exceed the mesh element budget", "mesh");
571 const float hw = style.markingWidth * 0.5f;
572 const V3 p0 = pa + (pb - pa) * t0;
573 const V3 p1 = pa + (pb - pa) * t1;
574 if (length(p1 - p0) <= 1e-5f) return Result<void>::success();
575 const V3 s0 = normalize(sa + (sb - sa) * t0);
576 const V3 s1 = normalize(sa + (sb - sa) * t1);
577 const V3 u0 = normalize(ua + (ub - ua) * t0);
578 const V3 u1 = normalize(ua + (ub - ua) * t1);
579 const float lateral0 = lateral * std::lerp(a.sample.scaleX, b.sample.scaleX, t0);
580 const float lateral1 = lateral * std::lerp(a.sample.scaleX, b.sample.scaleX, t1);
581 const V3 a0 =
582 p0 + s0 * (lateral0 - hw) +
583 u0 * (roadSurfaceHeight(style, lateral - hw / std::lerp(a.sample.scaleX, b.sample.scaleX, t0)) +
584 0.035f);
585 const V3 a1 =
586 p0 + s0 * (lateral0 + hw) +
587 u0 * (roadSurfaceHeight(style, lateral + hw / std::lerp(a.sample.scaleX, b.sample.scaleX, t0)) +
588 0.035f);
589 const V3 b1 =
590 p1 + s1 * (lateral1 + hw) +
591 u1 * (roadSurfaceHeight(style, lateral + hw / std::lerp(a.sample.scaleX, b.sample.scaleX, t1)) +
592 0.035f);
593 const V3 b0 =
594 p1 + s1 * (lateral1 - hw) +
595 u1 * (roadSurfaceHeight(style, lateral - hw / std::lerp(a.sample.scaleX, b.sample.scaleX, t1)) +
596 0.035f);
597 appendStripQuad(mesh, a0, b0, b1, a1, normalize(u0 + u1), 0.f, 1.f, 0.f, 1.f, material);
598 return Result<void>::success();
599 };
600 if (continuous) {
601 auto emitted = emitSpan(0.f, 1.f);
602 if (!emitted.ok()) return emitted;
603 continue;
604 }
605 const double cycle = static_cast<double>(style.dashLength) + static_cast<double>(style.dashGap);
606 double local = 0.0;
607 while (local < distanceSpan - 1e-7) {
608 const double phase = std::fmod(startDistance + local, cycle);
609 const bool draw = phase < static_cast<double>(style.dashLength);
610 const double boundary = draw ? static_cast<double>(style.dashLength) - phase : cycle - phase;
611 const double next = std::min(distanceSpan, local + boundary);
612 if (!(next > local))
613 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
614 "road marking period is too small to advance", "style.dashLength");
615 if (draw) {
616 auto emitted = emitSpan(static_cast<float>(local / distanceSpan),
617 static_cast<float>(next / distanceSpan));
618 if (!emitted.ok()) return emitted;
619 }
620 local = next;
621 }
622 }
623 return Result<void>::success();
624 };
625
626 auto leftEdge = paintLine(-asphaltHalf + style.markingWidth, false, RoadMaterial::Marking);
627 if (!leftEdge.ok()) return leftEdge;
628 auto rightEdge = paintLine(asphaltHalf - style.markingWidth, false, RoadMaterial::Marking);
629 if (!rightEdge.ok()) return rightEdge;
630 const float divider = -asphaltHalf + style.laneWidth * static_cast<float>(edge.lanesBackward);
631 for (int lane = 1; lane < edge.lanesForward; ++lane) {
632 const float lateral = (edge.lanesBackward > 0 ? divider : -asphaltHalf) +
633 style.laneWidth * static_cast<float>(lane);
634 auto line = paintLine(lateral, true, RoadMaterial::Marking);
635 if (!line.ok()) return line;
636 }
637 for (int lane = 1; lane < edge.lanesBackward; ++lane) {
638 const float lateral = -asphaltHalf + style.laneWidth * static_cast<float>(lane);
639 auto line = paintLine(lateral, true, RoadMaterial::Marking);
640 if (!line.ok()) return line;
641 }
642 if (edge.lanesForward > 0 && edge.lanesBackward > 0) {
643 const float offset = std::max(0.08f, style.markingWidth);
644 auto first = paintLine(divider - offset, false, RoadMaterial::MarkingYellow);
645 if (!first.ok()) return first;
646 auto second = paintLine(divider + offset, false, RoadMaterial::MarkingYellow);
647 if (!second.ok()) return second;
648 }
649 return Result<void>::success();
650}
651
653 const V3 side = normalize(cross(up, forward));
654 const V3 tip = pos + forward * size + up * 0.05f;
655 const V3 left = pos - forward * size * 0.35f + side * (size * 0.5f) + up * 0.05f;
656 const V3 right = pos - forward * size * 0.35f - side * (size * 0.5f) + up * 0.05f;
658 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
659 mesh.addVertex(tip.x, tip.y, tip.z, up.x, up.y, up.z, 0.5f, 1.f);
660 mesh.addVertex(left.x, left.y, left.z, up.x, up.y, up.z, 0.f, 0.f);
661 mesh.addVertex(right.x, right.y, right.z, up.x, up.y, up.z, 1.f, 0.f);
662 mesh.addTriangle(base, base + 1, base + 2);
663 // Back face so arrows stay visible under top-down interchange cameras.
664 const auto back = static_cast<std::uint32_t>(mesh.getVertexCount());
665 const V3 nd = up * -1.f;
666 mesh.addVertex(tip.x, tip.y, tip.z, nd.x, nd.y, nd.z, 0.5f, 1.f);
667 mesh.addVertex(right.x, right.y, right.z, nd.x, nd.y, nd.z, 1.f, 0.f);
668 mesh.addVertex(left.x, left.y, left.z, nd.x, nd.y, nd.z, 0.f, 0.f);
669 mesh.addTriangle(back, back + 1, back + 2);
670}
671
672
673} // namespace detail
674
675namespace detail {
676
678
680 float pathLength) {
681 return junctionSocketDistance(network, node, edge, pathLength);
682}
683
684} // namespace detail
685
686namespace {
687
688struct MaterialColor {
689 float r, g, b, a;
690};
691
692MaterialColor colorForGroup(const std::string& name) {
693 if (name == "asphalt" || name == "asphalt.bridge" || name == "asphalt.tunnel" || name == "asphalt.tunnel.portal")
694 return {0.28f, 0.28f, 0.30f, 1.f};
695 if (name == "curb") return {0.78f, 0.78f, 0.76f, 1.f};
696 if (name == "sidewalk") return {0.86f, 0.86f, 0.84f, 1.f};
697 if (name == "deck") return {0.48f, 0.48f, 0.46f, 1.f};
698 if (name == "pier") return {0.70f, 0.68f, 0.64f, 1.f};
699 if (name == "marking") return {0.96f, 0.96f, 0.94f, 1.f};
700 if (name == "markingYellow") return {0.96f, 0.80f, 0.10f, 1.f};
701 if (name == "nav") return {0.10f, 0.95f, 1.f, 1.f};
702 return {0.55f, 0.55f, 0.55f, 1.f};
703}
704
705Result<void> paintGroupVertexColors(MeshBuild& mesh) {
706 const int verts = mesh.getVertexCount();
707 if (verts <= 0) return Result<void>::success();
708 std::vector<float> colors(static_cast<std::size_t>(verts) * 4u, 1.f);
709 const int triCount = mesh.getIndexCount() / 3;
710 for (int t = 0; t < triCount; ++t) {
711 const int group = mesh.getTriangleGroup(t);
712 const MaterialColor c =
713 group >= 0 ? colorForGroup(mesh.getGroupName(group)) : MaterialColor{0.5f, 0.5f, 0.5f, 1.f};
714 for (int k = 0; k < 3; ++k) {
715 const int vi = mesh.getIndex(t * 3 + k);
716 if (vi < 0 || vi >= verts) continue;
717 const auto base = static_cast<std::size_t>(vi) * 4u;
718 colors[base + 0] = c.r;
719 colors[base + 1] = c.g;
720 colors[base + 2] = c.b;
721 colors[base + 3] = c.a;
722 }
723 }
724 return mesh.setVertexColors(std::move(colors));
725}
726
727} // namespace
728
730 if (options.maximumSamplingEvaluations == 0 || options.maximumSamplingEvaluations > 4u * 1024u * 1024u)
731 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument, "sampling work budget must be in [1,4194304]",
732 "options.maximumSamplingEvaluations");
733 if (!std::isfinite(options.samplingScale) || options.samplingScale < 1.f || options.samplingScale > 64.f)
734 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument, "samplingScale must be in [1,64]",
735 "options.samplingScale");
736 auto topology = network.validate();
737 if (!topology.ok()) return Result<RoadBakeResult>::failure(topology.status());
738 constexpr int maximumSamples = 4096;
739 if (options.pathSegmentsPerEdge < 2 || options.turnSamples < 2 ||
740 options.pathSegmentsPerEdge > maximumSamples || options.turnSamples > maximumSamples)
741 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument,
742 "road sample counts must be in [2,4096]", "options");
743 if (options.includeNavigation &&
744 (!std::isfinite(options.navRibbonHalfWidth) || options.navRibbonHalfWidth <= 0.f ||
745 !std::isfinite(options.arrowSpacing) || options.arrowSpacing <= 0.f))
746 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument,
747 "road navigation dimensions must be finite and positive", "options");
748 if (!std::isfinite(options.junctionChordError) || options.junctionChordError <= 0.f ||
749 options.junctionChordError > 1.f)
750 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument,
751 "road junction chord error must be finite and in (0,1] metres", "options");
752 if (options.maximumMeshElements == 0u)
753 return bakeFail<RoadBakeResult>(DiagnosticCode::InvalidArgument,
754 "road mesh element budget must be positive", "options");
755 if (network.edgeCount() == 0)
756 return bakeFail<RoadBakeResult>(DiagnosticCode::PreconditionViolation, "road network has no edges");
757
759 std::unordered_set<std::uint32_t> activeJunctionNodes;
760 std::unordered_set<std::uint32_t> terminalNodes;
761 std::unordered_map<std::uint32_t, std::vector<const RoadEdge*>> incidentEdges;
762 incidentEdges.reserve(network.nodeCount());
763 for (const auto& edge : network.edges()) {
764 incidentEdges[edge.from].push_back(&edge);
765 incidentEdges[edge.to].push_back(&edge);
766 }
767 for (const auto& node : network.nodes()) {
768 const auto found = incidentEdges.find(node.id);
769 if (found != incidentEdges.end() && found->second.size() == 1) terminalNodes.insert(node.id);
770 }
771 if (options.includeJunctions) {
772 for (const auto& node : network.nodes()) {
773 const auto found = incidentEdges.find(node.id);
774 if (found == incidentEdges.end() || (found->second.size() < 2 && node.connection.ports.empty())) continue;
775 activeJunctionNodes.insert(node.id);
776 auto solvable = validateJunctionGeometry(network, node);
777 if (!solvable.ok()) return Result<RoadBakeResult>::failure(solvable.status());
778 }
779 }
780 for (const auto& edge : network.edges()) {
781 auto spline = edgeToSpline(edge);
782 if (!spline.ok()) return Result<RoadBakeResult>::failure(spline.status());
783 auto pathLength = spline.value().lengthResult(16);
784 if (!pathLength.ok()) return Result<RoadBakeResult>::failure(pathLength.status());
785 const auto from = network.nodeResult(edge.from);
786 const auto to = network.nodeResult(edge.to);
787 if (!from.ok() || !to.ok())
788 return bakeFail<RoadBakeResult>(DiagnosticCode::InvariantViolation,
789 "edge endpoint disappeared during junction preflight", "edge");
790 const float start = activeJunctionNodes.contains(edge.from)
791 ? junctionSocketDistance(network, from.value(), edge, pathLength.value())
792 : 0.f;
793 const float end = activeJunctionNodes.contains(edge.to)
794 ? junctionSocketDistance(network, to.value(), edge, pathLength.value())
795 : 0.f;
796 if ((!from.value().connection.ports.empty() || !to.value().connection.ports.empty()) &&
797 start + end >= pathLength.value() - .01f)
798 return bakeFail<RoadBakeResult>(DiagnosticCode::PreconditionViolation,
799 "custom sockets consume the complete road edge", "edge");
800 const int laneCount = edge.lanesForward + edge.lanesBackward;
801 if (laneCount >= 6 && pathLength.value() - start - end <= 1.001f)
802 return bakeFail<RoadBakeResult>(DiagnosticCode::PreconditionViolation,
803 "junction socket trims consume the complete road edge", "edge");
804 }
805 RoadFrameCache frameCache;
806 std::vector<RoadGeometrySourceSpan> geometrySources;
807 auto recordSource = [&](RoadGeometrySourceKind kind, std::uint32_t id, int first) {
808 const int end = result.mesh.getIndexCount() / 3;
809 if (end > first)
810 geometrySources.push_back(
811 {kind, id, static_cast<std::uint32_t>(first), static_cast<std::uint32_t>(end - first)});
812 };
813 for (const auto& edge : network.edges()) {
814 const int first = result.mesh.getIndexCount() / 3;
815 auto baked = bakeEdgeGeometry(result.mesh, result.overlay, network, edge, options, activeJunctionNodes,
816 terminalNodes, frameCache);
817 if (!baked.ok()) return Result<RoadBakeResult>::failure(baked.status());
820 return bakeFail<RoadBakeResult>(DiagnosticCode::PreconditionViolation,
821 "road edges exceed the mesh element budget", "mesh");
822 }
823 if (options.includeJunctions) {
824 for (const auto& node : network.nodes()) {
825 if (!activeJunctionNodes.contains(node.id)) continue;
826 const int first = result.mesh.getIndexCount() / 3;
827 auto junction = bakeJunction(result.mesh, network, node, options);
828 if (!junction.ok()) return Result<RoadBakeResult>::failure(junction.status());
831 return bakeFail<RoadBakeResult>(DiagnosticCode::PreconditionViolation,
832 "road junctions exceed the mesh element budget", "mesh");
833 }
834 }
835 const int derivedFirst = result.mesh.getIndexCount() / 3;
836 auto continued = bakeFacilityContinuations(result.mesh, network, options, activeJunctionNodes);
837 if (!continued.ok()) return Result<RoadBakeResult>::failure(continued.status());
838 if (options.includeNavigation) {
839 auto turns = bakeTurnOverlays(result.mesh, result.overlay, network, options, activeJunctionNodes);
840 if (!turns.ok()) return Result<RoadBakeResult>::failure(turns.status());
841 }
842 if (options.includePlacements) {
843 auto placements = bakePlacementPoints(result.placements, network, options, activeJunctionNodes, frameCache);
844 if (!placements.ok()) return Result<RoadBakeResult>::failure(placements.status());
845 }
846
847 auto painted = paintGroupVertexColors(result.mesh);
848 if (!painted.ok()) return Result<RoadBakeResult>::failure(painted.status());
849 recordSource(RoadGeometrySourceKind::NetworkDerived, 0, derivedFirst);
850 auto sealed = sealRoadGeometrySources(result.mesh, geometrySources);
851 if (!sealed.ok()) return Result<RoadBakeResult>::failure(sealed.status());
852
853 result.mesh.setMeta("generator", "procgen.road");
854 result.mesh.setMeta("schema", "eve.procgen.roadNetwork");
855 result.mesh.setMeta("schemaVersion", "1");
856 result.mesh.setMeta("edges", std::to_string(network.edgeCount()));
857 result.mesh.setMeta("nodes", std::to_string(network.nodeCount()));
858 result.mesh.setMeta("laneLinks", std::to_string(network.laneLinkCount()));
859 if (result.mesh.getVertexCount() < 3)
860 return bakeFail<RoadBakeResult>(DiagnosticCode::InvariantViolation, "bake produced an empty mesh");
861 return Result<RoadBakeResult>::success(std::move(result));
862}
863
864} // namespace eve::procgen::road
double z
double x
Duration start
int tip
Definition AnimSmr.cpp:121
double lateral
const std::string & s
std::vector< std::uint32_t > verts
Definition Builder.cpp:27
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
std::string name
Definition ECS.cpp:154
float maximum[3]
float minimum[3]
tensor::Graph g
Definition GpuGraph.cpp:7
float u
Definition Grass.cpp:233
float area
Definition Grass.cpp:62
glm::vec3 n
Definition Grass.cpp:63
double r
std::vector< float > normals
HexVec3 across
float halfWidth
HexVec3 up
HexVec3 left
HexVec3 right
std::int32_t second
std::int32_t c
std::int32_t first
float length
std::string local
size_t offset
std::array< float, 3 > scale
bool required
std::string to
std::string from
std::string id
Poco::JSON::Object::Ptr result
uint64_t used
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
std::vector< TriangleRef > triangles
MeshVfxBatchedDraw draw
Texture * normal
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
OceanMeshSettings mesh
ssq::Object frame
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
std::shared_ptr< const OceanSprayFrame > previous
size_t directions
std::array< uint32_t, 16 > material
Topology topology
bool diagonal
float f
std::string path
Definition PlayHost.cpp:111
std::shared_ptr< const std::vector< glm::vec2 > > points
float d
int detail
float t
const RoadNode * node
RoadLaneDirection direction
SplinePath spline
bool found
Value::Array corners
unsigned line
size_t end
std::string kind
double current
float dy
Definition Shadow.cpp:151
const SquirrelValueOptions & options
int count
AbilityActivationGroup group
float phase
CommandLogBoundary boundary
Anchor rule, see above.
float size
Definition TreeMesh.cpp:158
double weight
int turns
V forward
glm::vec3 point
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning multi-segment 3D spline with deterministic bounded sampling.
Definition SplinePath.h:118
Result< void > setKindResult(std::string_view kind)
Select linear, catmullRom, quadraticBezier, or cubic bezier interpolation.
Owning directed road graph with lane connectivity.
Definition RoadNetwork.h:19
int nodeCount() const noexcept
Node count.
int edgeCount() const noexcept
Edge count.
const std::vector< RoadEdge > & edges() const noexcept
Edges.
Result< void > validate() const
Validate graph, endpoint and lane-link invariants without mutation.
int laneLinkCount() const noexcept
Lane link count.
Result< RoadNode > nodeResult(std::uint32_t id) const
Return an owning node snapshot. @cost Copies the node-owned custom mesh and port sections; authoring ...
const std::vector< RoadNode > & nodes() const noexcept
Nodes.
void appendBox(MeshBuild &mesh, V3 center, V3 side, V3 up, V3 forward, float hx, float hy, float hz, RoadMaterial material)
Module-private road bake implementation helper.
Definition RoadBake.cpp:402
float laneCenterOffset(const RoadStyle &style, int lanesForward, int lanesBackward, int laneIndex, RoadLaneDirection direction)
Module-private road bake implementation helper.
Definition RoadBake.cpp:393
V3 operator+(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:25
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
float asphaltHalfWidth(const RoadEdge &edge)
Module-private road bake implementation helper.
Definition RoadBake.cpp:100
float scaledProfileSide(float side, float asphaltHalf, float scale)
Module-private transverse width transform preserving fixed-width shoulders.
Definition RoadBake.cpp:114
float profileAsphaltHalfWidth(const RoadProfile &profile)
Module-private driving-surface half-width from a generated profile.
Definition RoadBake.cpp:121
QuadDiagonal
Module-private road bake implementation helper.
V3 operator-(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:26
float signedAreaXZ(const std::vector< V3 > &polygon)
Module-private road bake implementation helper.
Definition RoadBake.cpp:327
float junctionTrimDistance(float pathLength, float requestedRadius)
Module-private road bake implementation helper.
Definition RoadBake.cpp:42
Result< void > bakeJunction(MeshBuild &mesh, const RoadNetwork &network, const RoadNode &node, const RoadBakeOptions &options)
Module-private road bake implementation helper.
int quadraticSegments(V3 start, V3 control, V3 end, float angle, float maximumError)
Module-private road bake implementation helper.
Definition RoadBake.cpp:54
Result< void > bakeTurnOverlays(MeshBuild &mesh, RoadOverlay &overlay, const RoadNetwork &network, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeJunctionNodes)
Module-private road bake implementation helper.
Result< void > bakeFacilityContinuations(MeshBuild &mesh, const RoadNetwork &network, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeNodes)
Fill compatible continuous-facility gaps across active degree-two junction sockets.
V3 authoredOutwardDirection(const RoadNode &node, const RoadEdge &edge)
Module-private road bake implementation helper.
Definition RoadBake.cpp:93
int circularArcSegments(float radius, float angle, float maximumError)
Module-private road bake implementation helper.
Definition RoadBake.cpp:47
float roadSurfaceHeight(const RoadStyle &style, float lateral)
Return custom asphalt height at one unscaled driving lateral position.
void appendStripQuad(MeshBuild &mesh, V3 a, V3 b, V3 c, V3 d, V3 normal, float u0, float u1, float v0, float v1, RoadMaterial material, QuadDiagonal diagonal)
Emit a bounded strip quad with the selected physical diagonal and unchanged vertex attributes.
Definition RoadBake.cpp:425
Result< SplinePath > edgeToSpline(const RoadEdge &edge)
Module-private road bake implementation helper.
Definition RoadBake.cpp:62
void appendOrientedTri(MeshBuild &mesh, V3 a, V3 b, V3 c, V3 normal, float uvMeters, RoadMaterial material)
Module-private road bake implementation helper.
Definition RoadBake.cpp:455
void blendFrameToJunctionPlane(SplineFrameSample &frame, const JunctionPlane &plane, float weight)
Module-private road bake implementation helper.
Definition RoadBake.cpp:298
TriangulationStatus
Module-private road bake implementation helper.
float junctionSocketDistance(const RoadNetwork &network, const RoadNode &node, const RoadEdge &edge, float pathLength)
Module-private road bake implementation helper.
Definition RoadBake.cpp:146
bool isMeshBudgetExceeded(const MeshBuild &mesh, const RoadBakeOptions &options)
Module-private road bake implementation helper.
Definition RoadBake.cpp:37
float crossXZ(V3 a, V3 b, V3 c)
Module-private road bake implementation helper.
Definition RoadBake.cpp:337
float junctionSocketDistanceForBake(const RoadNetwork &network, const RoadNode &node, const RoadEdge &edge, float pathLength)
Definition RoadBake.cpp:679
Result< void > addLaneMarkings(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadEdge &edge, float pathLength, const RoadBakeOptions &options)
Module-private road bake implementation helper.
Definition RoadBake.cpp:539
JunctionPlane fitJunctionPlane(const RoadNetwork &network, const RoadNode &node)
Module-private road bake implementation helper.
Definition RoadBake.cpp:197
float maximumAsphaltHalfWidth(const RoadEdge &edge)
Module-private conservative half-width bound for varying-width junction sockets.
Definition RoadBake.cpp:135
Result< void > bakeEdgeGeometry(MeshBuild &mesh, RoadOverlay &overlay, const RoadNetwork &network, const RoadEdge &edge, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeJunctionNodes, const std::unordered_set< std::uint32_t > &terminalNodes, RoadFrameCache &frameCache)
Bake one edge.
V3 operator*(V3 a, float s)
Module-private road bake implementation helper.
Definition RoadBake.cpp:27
TriangulationStatus triangulateBoundary(const std::vector< V3 > &polygon, std::vector< std::array< std::size_t, 3 > > &triangles, BoundaryPointPolicy boundaryPolicy)
Definition RoadBake.cpp:348
Result< SplinePath > edgeSplineForBake(const RoadEdge &edge)
Definition RoadBake.cpp:677
Result< void > bakePlacementPoints(PointSet &placements, const RoadNetwork &network, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeJunctionNodes, RoadFrameCache &frameCache)
Module-private road bake implementation helper.
Result< void > sealRoadGeometrySources(MeshBuild &mesh, std::span< const RoadGeometrySourceSpan > spans)
Seal producer-owned intervals into full-mesh metadata; synchronous borrowed inputs,...
float dot(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:28
void appendOrientedQuad(MeshBuild &mesh, V3 a, V3 b, V3 c, V3 d, V3 normal, float u0, float u1, float v0, float v1, RoadMaterial material, QuadDiagonal diagonal)
Emit a quad whose winding matches normal (mesh3D expects object-space CCW).
Definition RoadBake.cpp:441
Result< void > validateConnectionSeams(const RoadNetwork &network, const RoadNode &node)
Validate actual baked spline sockets against every full custom asset seam.
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
Result< void > addDeckAndPiers(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadStyle &style, float halfWidth, float asphaltHalf, bool includePiers, std::size_t budget)
Module-private road bake implementation helper.
Definition RoadBake.cpp:471
float planeHeight(const JunctionPlane &plane, float x, float z)
Module-private road bake implementation helper.
Definition RoadBake.cpp:192
Result< void > validateJunctionGeometry(const RoadNetwork &network, const RoadNode &node)
Module-private road bake implementation helper.
Definition RoadBake.cpp:237
float connectionSocketDistance(const RoadNode &node, const RoadEdge &edge, float pathLength)
Return deterministic arc-distance to the bound custom socket (zero when absent).
void appendArrow(MeshBuild &mesh, V3 pos, V3 forward, V3 up, float size)
Module-private road bake implementation helper.
Definition RoadBake.cpp:652
bool isPointInTriangleXZ(V3 p, V3 a, V3 b, V3 c, float orientation)
Module-private road bake implementation helper.
Definition RoadBake.cpp:339
Result< RoadProfile > makeRoadProfile(const RoadStyle &style, int lanesForward, int lanesBackward)
Build a mathematical road cross-section for the given lane counts.
const char * roadMaterialGroup(RoadMaterial material) noexcept
Map a material tag to the MeshBuild group name used by the baker. @ownership borrowed — returns a poi...
Result< RoadBakeResult > bakeRoadNetwork(const RoadNetwork &network, const RoadBakeOptions &options)
Bake every edge, junction platform, pier, marking and navigation overlay.
Definition RoadBake.cpp:729
RoadGeometrySourceKind
Source category, scoped to one immutable road bake rather than a live object handle.
RoadLaneDirection
Travel direction of a lane relative to the authored edge centerline.
Definition RoadTypes.h:304
@ Backward
Travels from RoadEdge::to to RoadEdge::from.
RoadMaterial
Profile material tags used when lofting a road cross-section.
Definition RoadTypes.h:37
SplineSpanControl
Native outgoing overrides; Inherit uses the path kind.
Definition SplinePath.h:22
Owning section frame; sample position includes roll-pivot displacement, unlike the author centerline ...
Definition SplinePath.h:63
One spline anchor with incoming and outgoing handle offsets.
Definition SplinePath.h:25
SplineSpanControl spanControl
Native-only outgoing geometry controller.
Definition SplinePath.h:40
bool worldUpFrame
Native-only outgoing frame policy; vertical tangents use a deterministic X reference.
Definition SplinePath.h:35
SplineScaleInterpolation scaleXInterpolation
Native-only; cubic scale is floored at 1e-4 to retain a positive frame.
Definition SplinePath.h:33
float spanStrength
Native-only outgoing Catmull tangent coefficient in [0.01,1].
Definition SplinePath.h:39
float rollPivotSide
Local transverse roll-axis offset before scaleX; native owning value, metres.
Definition SplinePath.h:45
bool alignStraightEnd
Straight-adjacent XZ alignment with zero vertical derivative; reversal swaps ends.
Definition SplinePath.h:42
bool circularFromEnd
Derive circular geometry from the following endpoint tangent after reversal.
Definition SplinePath.h:44
SplineRollInterpolation rollInterpolation
Native-only owning outgoing policy.
Definition SplinePath.h:37
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Owning bake result: mesh, overlays and generic decoration placement points.
Definition RoadBake.h:79
std::vector< RoadConnectionPort > ports
Empty selects the procedural junction, with no custom mesh.
Definition RoadTypes.h:272
Directed centerline edge with optional reverse lanes.
Definition RoadTypes.h:286
Junction node owned by a road network.
Definition RoadTypes.h:275
Owning open profile polyline for one edge style and lane layout.
Definition RoadTypes.h:347
std::vector< RoadProfilePoint > points
Definition RoadTypes.h:348
Owning cross-section, width policy and structural authoring for one road edge.
Definition RoadTypes.h:182