载入中...
搜索中...
未找到
RoadBakeEdges.cpp
浏览该文件的文档.
3
4#include "common/Diagnostic.h"
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11#include <functional>
12#include <limits>
13#include <unordered_map>
14#include <unordered_set>
15#include <utility>
16#include <vector>
17
19
22 const std::unordered_set<std::uint32_t>& activeJunctionNodes,
23 const std::unordered_set<std::uint32_t>& terminalNodes, RoadFrameCache& frameCache) {
24 auto spline = edgeToSpline(edge);
25 if (!spline.ok()) return Result<void>::failure(spline.status());
26 auto pathLength = spline.value().lengthResult(24);
27 if (!pathLength.ok()) return Result<void>::failure(pathLength.status());
28
29 auto fromNode = network.nodeResult(edge.from);
30 auto toNode = network.nodeResult(edge.to);
31 if (!fromNode.ok() || !toNode.ok())
32 return bakeFail<void>(DiagnosticCode::NotFound, "edge endpoints missing during bake");
33
34 // A junction may not consume more than half of a short edge. Keep this
35 // clamp shared with junction mouths and navigation turn anchors.
36 const float trimStart = activeJunctionNodes.contains(fromNode.value().id)
37 ? junctionSocketDistance(network, fromNode.value(), edge, pathLength.value())
38 : 0.f;
39 const float trimEnd = activeJunctionNodes.contains(toNode.value().id)
40 ? junctionSocketDistance(network, toNode.value(), edge, pathLength.value())
41 : 0.f;
42 if (trimStart + trimEnd >= pathLength.value() - 0.5f)
43 return Result<void>::success(); // fully inside junction; skip strip
44
45 auto frames = prepareRoadFrames(frameCache, spline.value(), edge, options, pathLength.value());
46 if (!frames.ok()) return Result<void>::failure(frames.status());
47
48 // Keep mid frames, then force exact trim endpoints so the loft meets the
49 // junction apron (uniform samples alone leave ~segment-sized salmon gaps).
50 std::vector<SplineFrameSample> trimmed;
51 trimmed.reserve(frames.value().get().size() + 2);
52 if (trimStart > 1e-3f) {
53 auto tip = spline.value().travelFrameResult(trimStart, "clamp", 24);
54 if (!tip.ok()) return Result<void>::failure(tip.status());
55 trimmed.push_back(std::move(tip).takeValue());
56 } else {
57 trimmed.push_back(frames.value().get().front());
58 }
59 const float endDist = pathLength.value() - trimEnd;
60 for (const auto& frame : frames.value().get()) {
61 const float d = frame.sample.normalizedDistance * pathLength.value();
62 if (d > trimStart + 1e-3f && d < endDist - 1e-3f) trimmed.push_back(frame);
63 }
64 if (trimEnd > 1e-3f) {
65 auto tip = spline.value().travelFrameResult(endDist, "clamp", 24);
66 if (!tip.ok()) return Result<void>::failure(tip.status());
67 trimmed.push_back(std::move(tip).takeValue());
68 } else {
69 trimmed.push_back(frames.value().get().back());
70 }
71 if (trimmed.size() < 2) return Result<void>::success();
72
73 // The edge owns the transition into the shared junction plane. The last
74 // cross-section is therefore bit-for-bit compatible with the socket used
75 // by bakeJunction, while the preceding rings absorb grade and crossfall.
76 if (trimStart > 1e-3f && fromNode.value().connection.ports.empty()) {
77 const JunctionPlane plane = fitJunctionPlane(network, fromNode.value());
78 const float transition = std::max(3.f, fromNode.value().junctionRadius);
79 for (auto& frame : trimmed) {
80 const float distance = frame.sample.normalizedDistance * pathLength.value() - trimStart;
81 if (distance > transition) continue;
82 const float t = std::clamp(1.f - std::max(0.f, distance) / transition, 0.f, 1.f);
83 blendFrameToJunctionPlane(frame, plane, t * t * (3.f - 2.f * t));
84 }
85 }
86 if (trimEnd > 1e-3f && toNode.value().connection.ports.empty()) {
87 const JunctionPlane plane = fitJunctionPlane(network, toNode.value());
88 const float transition = std::max(3.f, toNode.value().junctionRadius);
89 for (auto& frame : trimmed) {
90 const float distance = endDist - frame.sample.normalizedDistance * pathLength.value();
91 if (distance > transition) continue;
92 const float t = std::clamp(1.f - std::max(0.f, distance) / transition, 0.f, 1.f);
93 blendFrameToJunctionPlane(frame, plane, t * t * (3.f - 2.f * t));
94 }
95 }
96
97 if (edge.style.sidePlacement.pathSampling == RoadSidePathSampling::SurfaceSegments) {
98 // Preserve one bake-owned metric path. Only the visible interval contains emitted strip rows;
99 // off-strip prefix/suffix retain author-relative exclusions and transition distances.
100 RoadSurfaceRows surfaceRows;
101 const float firstParameter = trimmed.front().sample.normalizedDistance;
102 const float lastParameter = trimmed.back().sample.normalizedDistance;
103 for (const auto& frame : frames.value().get())
104 if (frame.sample.normalizedDistance < firstParameter) surfaceRows.frames.push_back(frame);
105 surfaceRows.firstVisible = surfaceRows.frames.size();
106 surfaceRows.frames.insert(surfaceRows.frames.end(), trimmed.begin(), trimmed.end());
107 surfaceRows.lastVisible = surfaceRows.frames.size() - 1;
108 for (const auto& frame : frames.value().get())
109 if (frame.sample.normalizedDistance > lastParameter) surfaceRows.frames.push_back(frame);
110 frameCache.surfaces.emplace(edge.id, std::move(surfaceRows));
111 }
112
113 auto profile = makeRoadProfile(edge.style, edge.lanesForward, edge.lanesBackward);
114 if (!profile.ok()) return Result<void>::failure(profile.status());
115
116 auto lofted = bakeStructuralGeometry(mesh, trimmed, edge, profile.value(), pathLength.value(), options);
117 if (!lofted.ok()) return lofted;
118 // Cap stub ends fully. On large junction trims, only close curb/sidewalk
119 // shoulders — a full U-cap draws a dark asphalt tip bar that reads as a gap.
120 if (trimStart <= 0.05f && terminalNodes.contains(fromNode.value().id) &&
121 fromNode.value().connection.ports.empty()) {
122 {
123 auto capped = capProfileRing(mesh, trimmed.front(), profile.value(), false);
124 if (!capped.ok()) return capped;
125 }
126 } else if (trimStart > 0.05f && fromNode.value().connection.ports.empty()) {
127 if (trimStart < 2.5f) {
128 auto capped = capProfileRing(mesh, trimmed.front(), profile.value(), false);
129 if (!capped.ok()) return capped;
130 } else
131 capProfileShoulders(mesh, trimmed.front(), profile.value(), false);
132 }
133 if (trimEnd <= 0.05f && terminalNodes.contains(toNode.value().id) && toNode.value().connection.ports.empty()) {
134 {
135 auto capped = capProfileRing(mesh, trimmed.back(), profile.value(), true);
136 if (!capped.ok()) return capped;
137 }
138 } else if (trimEnd > 0.05f && toNode.value().connection.ports.empty()) {
139 if (trimEnd < 2.5f) {
140 auto capped = capProfileRing(mesh, trimmed.back(), profile.value(), true);
141 if (!capped.ok()) return capped;
142 } else
143 capProfileShoulders(mesh, trimmed.back(), profile.value(), true);
144 }
145 auto facilities =
146 bakeContinuousFacilities(mesh, trimmed, network, edge, pathLength.value(), options, activeJunctionNodes);
147 if (!facilities.ok()) return facilities;
148 if (options.includeMarkings) {
149 auto marks = addLaneMarkings(mesh, trimmed, edge, pathLength.value(), options);
150 if (!marks.ok()) return marks;
151 }
152
153 if (options.includeNavigation) {
154 for (int lane = 0; lane < edge.lanesForward; ++lane) {
155 const float lateral =
156 laneCenterOffset(edge.style, edge.lanesForward, edge.lanesBackward, lane, RoadLaneDirection::Forward);
157 RoadPolyline poly;
158 poly.r = 0.15f;
159 poly.g = 0.9f;
160 poly.b = 1.f;
161 poly.width = options.navRibbonHalfWidth * 2.f;
162 poly.inEdge = edge.id;
163 poly.inLane = lane;
166 poly.speedLimitMps = edge.style.speedLimitMps;
167 poly.trafficPriority = edge.style.trafficPriority;
168 float traveled = 0.f;
169 float nextArrow = options.arrowSpacing * 0.5f;
170 V3 prev{};
171 bool hasPrev = false;
172 for (const auto& frame : trimmed) {
173 const V3 origin{frame.sample.x, frame.sample.y, frame.sample.z};
174 const V3 side{frame.sideX, frame.sideY, frame.sideZ};
175 const V3 up{frame.upX, frame.upY, frame.upZ};
176 if (!std::isfinite(origin.x) || !std::isfinite(origin.y) || !std::isfinite(origin.z)) continue;
177 const V3 pos = origin + side * (lateral * frame.sample.scaleX) +
178 up * (roadSurfaceHeight(edge.style, lateral) + 0.08f);
179 poly.xyz.push_back(pos.x);
180 poly.xyz.push_back(pos.y);
181 poly.xyz.push_back(pos.z);
182 if (hasPrev) {
183 const float step = length(pos - prev);
184 if (std::isfinite(step) && step > 1e-5f && step < 1.0e4f) {
185 traveled += step;
186 const V3 fwd = normalize(pos - prev);
187 const V3 lat = normalize(cross(up, fwd));
188 const float hw = options.navRibbonHalfWidth;
189 appendStripQuad(mesh, prev + lat * -hw, pos + lat * -hw, pos + lat * hw, prev + lat * hw, up,
190 0.f, 1.f, 0.f, 1.f, RoadMaterial::Nav);
191 int arrowGuard = 0;
192 while (traveled >= nextArrow && arrowGuard++ < 64) {
193 appendArrow(mesh, pos, fwd, up, 0.85f);
194 nextArrow += std::max(options.arrowSpacing, 0.5f);
195 }
196 if (arrowGuard >= 64) nextArrow = traveled + std::max(options.arrowSpacing, 0.5f);
197 }
198 }
199 prev = pos;
200 hasPrev = true;
201 }
202 overlay.lanes.push_back(std::move(poly));
203 }
204 for (int lane = 0; lane < edge.lanesBackward; ++lane) {
205 const float lateral =
206 laneCenterOffset(edge.style, edge.lanesForward, edge.lanesBackward, lane, RoadLaneDirection::Backward);
207 RoadPolyline poly;
208 poly.r = 0.2f;
209 poly.g = 0.75f;
210 poly.b = 1.f;
211 poly.width = options.navRibbonHalfWidth * 2.f;
212 poly.inEdge = edge.id;
213 poly.inLane = lane;
216 poly.speedLimitMps = edge.style.speedLimitMps;
217 poly.trafficPriority = edge.style.trafficPriority;
218 for (auto it = trimmed.rbegin(); it != trimmed.rend(); ++it) {
219 const V3 origin{it->sample.x, it->sample.y, it->sample.z};
220 const V3 side{it->sideX, it->sideY, it->sideZ};
221 const V3 up{it->upX, it->upY, it->upZ};
222 const V3 pos = origin + side * (lateral * it->sample.scaleX) +
223 up * (roadSurfaceHeight(edge.style, lateral) + 0.08f);
224 poly.xyz.insert(poly.xyz.end(), {pos.x, pos.y, pos.z});
225 }
226 overlay.lanes.push_back(std::move(poly));
227 }
228 }
229 return Result<void>::success();
230}
231
232
233} // namespace eve::procgen::road::detail
int tip
Definition AnimSmr.cpp:121
double lateral
Stable, structured diagnostics shared by engine modules.
HexVec3 up
float length
float distance
OceanMeshSettings mesh
glm::dvec3 origin
ssq::Object frame
float d
float t
SplinePath spline
const SquirrelValueOptions & options
float step
Definition TreeMesh.cpp:451
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 directed road graph with lane connectivity.
Definition RoadNetwork.h:19
Result< RoadNode > nodeResult(std::uint32_t id) const
Return an owning node snapshot. @cost Copies the node-owned custom mesh and port sections; authoring ...
Result< void > bakeStructuralGeometry(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadEdge &edge, const RoadProfile &profile, float pathLength, const RoadBakeOptions &options)
Module-private road bake implementation helper.
float laneCenterOffset(const RoadStyle &style, int lanesForward, int lanesBackward, int laneIndex, RoadLaneDirection direction)
Module-private road bake implementation helper.
Definition RoadBake.cpp:393
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
Result< void > capProfileRing(MeshBuild &mesh, const SplineFrameSample &frame, const RoadProfile &profile, bool outward)
Module-private road bake implementation helper.
void capProfileShoulders(MeshBuild &mesh, const SplineFrameSample &frame, const RoadProfile &profile, bool outward)
Module-private road bake implementation helper.
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 blendFrameToJunctionPlane(SplineFrameSample &frame, const JunctionPlane &plane, float weight)
Module-private road bake implementation helper.
Definition RoadBake.cpp:298
float junctionSocketDistance(const RoadNetwork &network, const RoadNode &node, const RoadEdge &edge, float pathLength)
Module-private road bake implementation helper.
Definition RoadBake.cpp:146
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
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.
Result< void > bakeContinuousFacilities(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadNetwork &network, const RoadEdge &edge, float pathLength, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeNodes)
Sweep continuous facilities and clip them to exact interval/socket boundaries.
ResultRef< const std::vector< SplineFrameSample > > prepareRoadFrames(RoadFrameCache &cache, const SplinePath &path, const RoadEdge &edge, const RoadBakeOptions &options, float length)
Prepare once per immutable edge/options snapshot. Borrowed result expires with cache destruction....
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
void appendArrow(MeshBuild &mesh, V3 pos, V3 forward, V3 up, float size)
Module-private road bake implementation helper.
Definition RoadBake.cpp:652
Result< RoadProfile > makeRoadProfile(const RoadStyle &style, int lanesForward, int lanesBackward)
Build a mathematical road cross-section for the given lane counts.
@ Forward
Travels from RoadEdge::from to RoadEdge::to.
@ Backward
Travels from RoadEdge::to to RoadEdge::from.
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Directed centerline edge with optional reverse lanes.
Definition RoadTypes.h:286
Owning bake-time overlay snapshot (no pointers into the network).
Definition RoadTypes.h:341
std::vector< RoadPolyline > lanes
Definition RoadTypes.h:342
One renderer-neutral polyline chunk for navigation / marking overlays.
Definition RoadTypes.h:325
std::vector< float > xyz
Packed xyz samples.
Definition RoadTypes.h:326
RoadLaneDirection outDirection
Definition RoadTypes.h:335
RoadLaneDirection inDirection
Definition RoadTypes.h:334
std::unordered_map< std::uint32_t, RoadSurfaceRows > surfaces
One bake's sole owner of retained road frames and shared scan work; no callbacks or cross-bake reuse.
std::vector< SplineFrameSample > frames
Full metric path with actual clipped/blended rows in its visible interval.