载入中...
搜索中...
未找到
RoadNetworkRecipes.cpp
浏览该文件的文档.
2
4
5#include "common/Diagnostic.h"
6
7#include <algorithm>
8#include <cmath>
9#include <limits>
10#include <string>
11#include <utility>
12
13namespace eve::procgen::road {
14namespace {
15RoadControlPoint P(float x, float y, float z) { return RoadControlPoint{x, y, z}; }
16RoadStyle groundStyle() {
17 RoadStyle style;
18 style.deckThickness = 0.35f;
19 style.pierClearance = 100.f;
20 style.curbHeight = 0.50f;
21 style.curbWidth = 0.42f;
22 return style;
23}
24
25RoadStyle bridgeStyle() {
26 RoadStyle style = groundStyle();
27 style.deckThickness = 0.75f;
28 style.pierClearance = 1.25f;
29 style.pierSpacing = 8.f;
30 style.pierWidth = 1.3f;
31 style.pierDepth = 1.3f;
32 style.curbHeight = 0.55f;
33 return style;
34}
35
36Result<RoadNetwork> makeTightTurnScene(int lanes) {
37 if (lanes < 1 || lanes > 4)
39 Diagnostic::error(DiagnosticCode::InvalidArgument, "lanes in [1,4] required", "tight-turn"));
40
41 RoadNetwork network;
42 RoadStyle style = groundStyle();
43 style.laneWidth = 1.2f;
44 style.sidewalkWidth = 0.25f;
45
46 auto incomingStart = network.addNode(-9.f, 0.f, -3.f, 1.f);
47 auto hub = network.addNode(0.f, 0.f, 0.f, 8.f);
48 auto outgoingEnd = network.addNode(-9.f, 0.f, 3.f, 1.f);
49 for (auto* node : {&incomingStart, &hub, &outgoingEnd}) {
50 if (!node->ok()) return Result<RoadNetwork>::failure(node->status());
51 }
52
53 auto incoming =
54 network.addEdge(incomingStart.value(), hub.value(), {P(-9.f, 0.f, -3.f), P(0.f, 0.f, 0.f)}, lanes, 0, style);
55 auto outgoing =
56 network.addEdge(hub.value(), outgoingEnd.value(), {P(0.f, 0.f, 0.f), P(-9.f, 0.f, 3.f)}, lanes, 0, style);
57 if (!incoming.ok()) return Result<RoadNetwork>::failure(incoming.status());
58 if (!outgoing.ok()) return Result<RoadNetwork>::failure(outgoing.status());
59 auto turn = network.addLaneLink(
60 {incoming.value(), 0, outgoing.value(), 0, RoadLaneDirection::Forward, RoadLaneDirection::Forward});
61 if (!turn.ok()) return Result<RoadNetwork>::failure(turn.status());
62 return Result<RoadNetwork>::success(std::move(network));
63}
64
65Result<RoadNetwork> makeThreeArmScene(const std::string& scene, int lanes) {
66 if (lanes < 1 || lanes > 4)
68 Diagnostic::error(DiagnosticCode::InvalidArgument, "lanes in [1,4] required", scene));
69
70 RoadNetwork network;
71 RoadStyle narrow = groundStyle();
72 RoadStyle wide = groundStyle();
73 narrow.laneWidth = 3.f;
74 wide.laneWidth = 4.2f;
75
76 const bool yJunction = scene == "y-junction";
77 auto hub = network.addNode(0.f, 0.f, 0.f, yJunction ? 5.f : 6.f);
78 auto stem = network.addNode(0.f, 0.f, 24.f, 2.f);
79 auto left = network.addNode(-18.f, 0.f, yJunction ? -18.f : 0.f, 2.f);
80 auto right = network.addNode(18.f, 0.f, yJunction ? -18.f : 0.f, 2.f);
81 for (auto* node : {&hub, &stem, &left, &right}) {
82 if (!node->ok()) return Result<RoadNetwork>::failure(node->status());
83 }
84 auto incoming = network.addEdge(stem.value(), hub.value(), {P(0.f, 0.f, 24.f), P(0.f, 0.f, 0.f)}, lanes,
85 yJunction && lanes > 1 ? 1 : 0, yJunction ? wide : narrow);
86 auto west = network.addEdge(hub.value(), left.value(), {P(0.f, 0.f, 0.f), P(-18.f, 0.f, yJunction ? -18.f : 0.f)},
87 lanes, 0, narrow);
88 auto east = network.addEdge(hub.value(), right.value(), {P(0.f, 0.f, 0.f), P(18.f, 0.f, yJunction ? -18.f : 0.f)},
89 yJunction ? std::min(4, lanes + 1) : lanes, 0, yJunction ? wide : narrow);
90 for (auto* edge : {&incoming, &west, &east}) {
91 if (!edge->ok()) return Result<RoadNetwork>::failure(edge->status());
92 }
93 auto turns = network.connectAllTurns(hub.value());
94 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
95 return Result<RoadNetwork>::success(std::move(network));
96}
97
98Result<RoadNetwork> makeSlopedJunctionScene(int lanes, bool curved) {
99 if (lanes < 1 || lanes > 4)
101 DiagnosticCode::InvalidArgument, "lanes in [1,4] required", curved ? "curve-uphill" : "sloped-t"));
102
103 RoadNetwork network;
104 RoadStyle style = groundStyle();
105 auto hub = network.addNode(0.f, 4.f, 0.f, 6.f);
106 auto low = network.addNode(-22.f, 0.f, 12.f, 2.f);
107 auto high = network.addNode(-20.f, 7.f, -14.f, 2.f);
108 auto exit = network.addNode(24.f, 4.f, 0.f, 2.f);
109 for (auto* node : {&hub, &low, &high, &exit}) {
110 if (!node->ok()) return Result<RoadNetwork>::failure(node->status());
111 }
112 const std::vector<RoadControlPoint> lowPoints =
113 curved ? std::vector<RoadControlPoint>{P(-22.f, 0.f, 12.f), P(-13.f, 1.4f, 14.f), P(-6.f, 3.f, 7.f),
114 P(0.f, 4.f, 0.f)}
115 : std::vector<RoadControlPoint>{P(-22.f, 0.f, 12.f), P(0.f, 4.f, 0.f)};
116 const std::vector<RoadControlPoint> highPoints =
117 curved ? std::vector<RoadControlPoint>{P(-20.f, 7.f, -14.f), P(-12.f, 6.4f, -16.f), P(-5.f, 5.f, -7.f),
118 P(0.f, 4.f, 0.f)}
119 : std::vector<RoadControlPoint>{P(-20.f, 7.f, -14.f), P(0.f, 4.f, 0.f)};
120 auto lowEdge = network.addEdge(low.value(), hub.value(), lowPoints, lanes, 0, style);
121 auto highEdge = network.addEdge(high.value(), hub.value(), highPoints, lanes, 0, style);
122 auto exitEdge = network.addEdge(hub.value(), exit.value(), {P(0.f, 4.f, 0.f), P(24.f, 4.f, 0.f)},
123 std::min(4, lanes + 1), 0, style);
124 for (auto* edge : {&lowEdge, &highEdge, &exitEdge}) {
125 if (!edge->ok()) return Result<RoadNetwork>::failure(edge->status());
126 }
127 auto turns = network.connectAllTurns(hub.value());
128 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
129 return Result<RoadNetwork>::success(std::move(network));
130}
131
132} // namespace
133
135 if (!std::isfinite(length) || length < 8.f || lanes < 1 || lanes > 4)
137 Diagnostic::error(DiagnosticCode::InvalidArgument, "length>=8, lanes in [1,4] required", "straight"));
138 RoadNetwork network;
139 const float half = length * 0.5f;
140 auto a = network.addNode(-half, 0.f, 0.f, 2.f);
141 auto b = network.addNode(half, 0.f, 0.f, 2.f);
142 if (!a.ok()) return Result<RoadNetwork>::failure(a.status());
143 if (!b.ok()) return Result<RoadNetwork>::failure(b.status());
144 auto edge = network.addEdge(a.value(), b.value(), {P(-half, 0.f, 0.f), P(0.f, 0.f, 0.f), P(half, 0.f, 0.f)}, lanes,
145 0, groundStyle());
146 if (!edge.ok()) return Result<RoadNetwork>::failure(edge.status());
147 return Result<RoadNetwork>::success(std::move(network));
148}
149
151 if (!std::isfinite(radius) || radius < 8.f || lanes < 1 || lanes > 4)
153 Diagnostic::error(DiagnosticCode::InvalidArgument, "radius>=8, lanes in [1,4] required", "curve"));
154 RoadNetwork network;
155 auto a = network.addNode(-radius, 0.f, 0.f, 2.f);
156 auto b = network.addNode(0.f, 0.f, radius, 2.f);
157 if (!a.ok()) return Result<RoadNetwork>::failure(a.status());
158 if (!b.ok()) return Result<RoadNetwork>::failure(b.status());
159 auto edge = network.addEdge(a.value(), b.value(),
160 {P(-radius, 0.f, 0.f), P(-radius * 0.55f, 0.f, radius * 0.15f),
161 P(-radius * 0.15f, 0.f, radius * 0.55f), P(0.f, 0.f, radius)},
162 lanes, 0, groundStyle());
163 if (!edge.ok()) return Result<RoadNetwork>::failure(edge.status());
164 return Result<RoadNetwork>::success(std::move(network));
165}
166
168 if (!std::isfinite(length) || length < 12.f || !std::isfinite(height) || height < 2.f || lanes < 1 || lanes > 4)
170 DiagnosticCode::InvalidArgument, "length>=12, height>=2, lanes in [1,4] required", "bridge"));
171 RoadNetwork network;
172 const float half = length * 0.5f;
173 // Flat elevated deck at constant height (no ramps yet) — isolates pier baking
174 // from Frenet-frame twist that shows up on steep climbs.
175 auto a = network.addNode(-half, height, 0.f, 2.f);
176 auto b = network.addNode(half, height, 0.f, 2.f);
177 if (!a.ok()) return Result<RoadNetwork>::failure(a.status());
178 if (!b.ok()) return Result<RoadNetwork>::failure(b.status());
179 auto edge =
180 network.addEdge(a.value(), b.value(), {P(-half, height, 0.f), P(0.f, height, 0.f), P(half, height, 0.f)}, lanes,
181 0, bridgeStyle());
182 if (!edge.ok()) return Result<RoadNetwork>::failure(edge.status());
183 return Result<RoadNetwork>::success(std::move(network));
184}
185
187 if (!std::isfinite(span) || span < 16.f || lanes < 1 || lanes > 4)
189 Diagnostic::error(DiagnosticCode::InvalidArgument, "span>=16, lanes in [1,4] required", "cross"));
190 RoadNetwork network;
191 const float half = span * 0.5f;
192 RoadStyle style = groundStyle();
193 style.deckThickness = 0.08f;
194 style.sidewalkWidth = 1.2f;
195 style.curbWidth = 0.35f;
196 style.curbHeight = 0.45f;
197 // Push arms outward past the asphalt half-width so sidewalks do not overlap;
198 // the leftover ring is filled by quarter-circle curb returns.
199 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(lanes);
200 const float cornerR = 2.8f;
201 const float jr = asphaltHalf + cornerR;
202 auto nC = network.addNode(0.f, 0.f, 0.f, jr);
203 auto nN = network.addNode(0.f, 0.f, -half, 2.f);
204 auto nS = network.addNode(0.f, 0.f, half, 2.f);
205 auto nW = network.addNode(-half, 0.f, 0.f, 2.f);
206 auto nE = network.addNode(half, 0.f, 0.f, 2.f);
207 for (auto* r : {&nC, &nN, &nS, &nW, &nE}) {
208 if (!r->ok()) return Result<RoadNetwork>::failure(r->status());
209 }
210 // Arms run into the hub; bake trims each end by junctionRadius so strips
211 // stop outside the corner arcs.
212 auto e1 = network.addEdge(nN.value(), nC.value(), {P(0.f, 0.f, -half), P(0.f, 0.f, 0.f)}, lanes, 0, style);
213 auto e2 = network.addEdge(nC.value(), nS.value(), {P(0.f, 0.f, 0.f), P(0.f, 0.f, half)}, lanes, 0, style);
214 auto e3 = network.addEdge(nW.value(), nC.value(), {P(-half, 0.f, 0.f), P(0.f, 0.f, 0.f)}, lanes, 0, style);
215 auto e4 = network.addEdge(nC.value(), nE.value(), {P(0.f, 0.f, 0.f), P(half, 0.f, 0.f)}, lanes, 0, style);
216 for (auto* e : {&e1, &e2, &e3, &e4}) {
217 if (!e->ok()) return Result<RoadNetwork>::failure(e->status());
218 }
219 auto turns = network.connectAllTurns(nC.value());
220 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
221 return Result<RoadNetwork>::success(std::move(network));
222}
223
225 if (!std::isfinite(span) || span < 16.f || lanes < 1 || lanes > 4)
227 Diagnostic::error(DiagnosticCode::InvalidArgument, "span>=16, lanes in [1,4] required", "tee"));
228 RoadNetwork network;
229 const float half = span * 0.5f;
230 RoadStyle style = groundStyle();
231 style.deckThickness = 0.08f;
232 style.sidewalkWidth = 1.2f;
233 style.curbWidth = 0.35f;
234 style.curbHeight = 0.45f;
235 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(lanes);
236 const float jr = asphaltHalf + 2.8f;
237 auto center = network.addNode(0.f, 0.f, 0.f, jr);
238 auto south = network.addNode(0.f, 0.f, half, 2.f);
239 auto west = network.addNode(-half, 0.f, 0.f, 2.f);
240 auto east = network.addNode(half, 0.f, 0.f, 2.f);
241 for (auto* node : {&center, &south, &west, &east})
242 if (!node->ok()) return Result<RoadNetwork>::failure(node->status());
243 auto westEdge = network.addEdge(west.value(), center.value(),
244 {P(-half, 0.f, 0.f), P(-half * 0.5f, 0.f, 0.f), P(0.f, 0.f, 0.f)}, lanes, 0, style);
245 auto eastEdge = network.addEdge(center.value(), east.value(),
246 {P(0.f, 0.f, 0.f), P(half * 0.5f, 0.f, 0.f), P(half, 0.f, 0.f)}, lanes, 0, style);
247 auto southEdge = network.addEdge(center.value(), south.value(),
248 {P(0.f, 0.f, 0.f), P(0.f, 0.f, half * 0.5f), P(0.f, 0.f, half)}, lanes, 0, style);
249 for (auto* edge : {&westEdge, &eastEdge, &southEdge})
250 if (!edge->ok()) return Result<RoadNetwork>::failure(edge->status());
251 auto turns = network.connectAllTurns(center.value());
252 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
253 return Result<RoadNetwork>::success(std::move(network));
254}
255
257 if (!std::isfinite(span) || span < 16.f || lanes < 1 || lanes > 4)
259 Diagnostic::error(DiagnosticCode::InvalidArgument, "span>=16, lanes in [1,4] required", "y"));
260 return makeFan(span, lanes, {270.f, 30.f, 150.f}, {true, false, false});
261}
262
263Result<RoadNetwork> RoadNetwork::makeFan(float span, int lanes, std::vector<float> armAnglesDeg,
264 std::vector<bool> intoHub) {
265 if (!std::isfinite(span) || span < 16.f || lanes < 1 || lanes > 4)
267 Diagnostic::error(DiagnosticCode::InvalidArgument, "span>=16, lanes in [1,4] required", "fan"));
268 if (armAnglesDeg.size() < 2u)
270 Diagnostic::error(DiagnosticCode::InvalidArgument, "fan needs at least two arm angles", "fan"));
271 if (!intoHub.empty() && intoHub.size() != armAnglesDeg.size())
273 Diagnostic::error(DiagnosticCode::InvalidArgument, "intoHub size must match armAnglesDeg", "fan"));
274
275 struct ArmSpec {
276 float degrees = 0.f;
277 bool intoHub = true;
278 };
279 std::vector<ArmSpec> specs;
280 specs.reserve(armAnglesDeg.size());
281 for (std::size_t index = 0; index < armAnglesDeg.size(); ++index) {
282 if (!std::isfinite(armAnglesDeg[index]))
284 Diagnostic::error(DiagnosticCode::InvalidArgument, "arm angles must be finite", "fan"));
285 float degrees = std::fmod(armAnglesDeg[index], 360.f);
286 if (degrees < 0.f) degrees += 360.f;
287 specs.push_back({degrees, intoHub.empty() || intoHub[index]});
288 }
289 std::sort(specs.begin(), specs.end(),
290 [](const ArmSpec& first, const ArmSpec& second) { return first.degrees < second.degrees; });
291 if (intoHub.empty())
292 for (std::size_t index = 0; index < specs.size(); ++index) specs[index].intoHub = index % 2u == 0u;
293 std::vector<ArmSpec> unique;
294 unique.reserve(specs.size());
295 for (const auto& spec : specs)
296 if (unique.empty() || std::fabs(spec.degrees - unique.back().degrees) > 1.f) unique.push_back(spec);
297 if (unique.size() >= 2u && std::fabs(unique.front().degrees + 360.f - unique.back().degrees) <= 1.f)
298 unique.pop_back();
299 if (unique.size() < 2u)
301 Diagnostic::error(DiagnosticCode::InvalidArgument, "fan needs at least two distinct arm angles", "fan"));
302
303 RoadNetwork network;
304 const float half = span * 0.5f;
305 RoadStyle style = groundStyle();
306 style.deckThickness = 0.08f;
307 style.sidewalkWidth = 1.2f;
308 style.curbWidth = 0.35f;
309 style.curbHeight = 0.45f;
310 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(lanes);
311 auto center = network.addNode(0.f, 0.f, 0.f, asphaltHalf + 2.8f);
312 if (!center.ok()) return Result<RoadNetwork>::failure(center.status());
313 for (const auto& spec : unique) {
314 const float radians = spec.degrees * 0.01745329252f;
315 const float x = half * std::cos(radians), z = half * std::sin(radians);
316 auto leaf = network.addNode(x, 0.f, z, 2.f);
317 if (!leaf.ok()) return Result<RoadNetwork>::failure(leaf.status());
318 auto edge =
319 spec.intoHub
320 ? network.addEdge(leaf.value(), center.value(),
321 {P(x, 0.f, z), P(x * 0.5f, 0.f, z * 0.5f), P(0.f, 0.f, 0.f)}, lanes, 0, style)
322 : network.addEdge(center.value(), leaf.value(),
323 {P(0.f, 0.f, 0.f), P(x * 0.5f, 0.f, z * 0.5f), P(x, 0.f, z)}, lanes, 0, style);
324 if (!edge.ok()) return Result<RoadNetwork>::failure(edge.status());
325 }
326 auto turns = network.connectAllTurns(center.value());
327 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
328 return Result<RoadNetwork>::success(std::move(network));
329}
330
331Result<RoadNetwork> RoadNetwork::makeFork(float span, int lanes) { return makeFan(span, lanes, {60.f, 120.f, 270.f}); }
332
333Result<RoadNetwork> RoadNetwork::makeSkew(float span, int lanes) { return makeFan(span, lanes, {0.f, 135.f, 270.f}); }
334
336 if (!std::isfinite(span) || span < 32.f || lanes < 1 || lanes > 3)
338 Diagnostic::error(DiagnosticCode::InvalidArgument, "span>=32, lanes in [1,3] required", "roundabout"));
339 RoadNetwork network;
340 const float outerRadius = span * 0.5f;
341 const float ringRadius = std::clamp(span * 0.25f, 8.f, 16.f);
342 constexpr float pi = 3.14159265358979323846f;
343 constexpr int ringCount = 8;
344 constexpr float ringStep = 2.f * pi / static_cast<float>(ringCount);
345 std::vector<std::uint32_t> ringNodes;
346 ringNodes.reserve(ringCount);
347 for (int i = 0; i < ringCount; ++i) {
348 const float angle = -0.5f * pi + static_cast<float>(i) * ringStep;
349 const float seamRadius = (i % 2 == 0) ? 1.5f : 0.25f;
350 auto node = network.addNode(std::cos(angle) * ringRadius, 0.f, std::sin(angle) * ringRadius, seamRadius);
351 if (!node.ok()) return Result<RoadNetwork>::failure(node.status());
352 ringNodes.push_back(node.value());
353 }
354
355 RoadStyle ringStyle = groundStyle();
356 ringStyle.laneWidth = 3.25f;
357 ringStyle.curbWidth = 0.3f;
358 ringStyle.sidewalkWidth = 0.75f;
359 ringStyle.trafficPriority = 10;
360 RoadStyle approachStyle = groundStyle();
361 approachStyle.laneWidth = 3.f;
362 approachStyle.trafficPriority = 1;
363 for (int i = 0; i < ringCount; ++i) {
364 const float a0 = -0.5f * pi + static_cast<float>(i) * ringStep;
365 const float a3 = a0 + ringStep;
366 const float handle = 2.f * ringRadius * std::sin(ringStep * 0.5f) / 3.f;
367 const RoadControlPoint p0 = P(std::cos(a0) * ringRadius, 0.f, std::sin(a0) * ringRadius);
368 const RoadControlPoint p3 = P(std::cos(a3) * ringRadius, 0.f, std::sin(a3) * ringRadius);
369 const RoadControlPoint p1 = P(p0.x - std::sin(a0) * handle, 0.f, p0.z + std::cos(a0) * handle);
370 const RoadControlPoint p2 = P(p3.x + std::sin(a3) * handle, 0.f, p3.z - std::cos(a3) * handle);
371 auto edge = network.addEdge(ringNodes[static_cast<std::size_t>(i)],
372 ringNodes[static_cast<std::size_t>((i + 1) % ringCount)], {p0, p1, p2, p3}, lanes,
373 0, ringStyle);
374 if (!edge.ok()) return Result<RoadNetwork>::failure(edge.status());
375 }
376 for (int i = 0; i < ringCount; i += 2) {
377 const float angle = -0.5f * pi + static_cast<float>(i) * ringStep;
378 auto outer = network.addNode(std::cos(angle) * outerRadius, 0.f, std::sin(angle) * outerRadius, 2.f);
379 if (!outer.ok()) return Result<RoadNetwork>::failure(outer.status());
380 auto approach = network.addEdge(outer.value(), ringNodes[static_cast<std::size_t>(i)], {{}, {}}, lanes, lanes,
381 approachStyle);
382 if (!approach.ok()) return Result<RoadNetwork>::failure(approach.status());
383 }
384 for (int i = 0; i < ringCount; i += 2) {
385 const auto node = ringNodes[static_cast<std::size_t>(i)];
387 if (!control.ok()) return Result<RoadNetwork>::failure(control.status());
388 auto connected = network.connectAllTurns(node);
389 if (!connected.ok()) return Result<RoadNetwork>::failure(connected.status());
390 }
391 return Result<RoadNetwork>::success(std::move(network));
392}
393
394Result<RoadNetwork> RoadNetwork::makeScene(const std::string& scene, float span, float bridgeHeight, int lanes,
395 std::uint32_t seed) {
396 if (scene == "straight") return makeStraight(span, lanes);
397 if (scene == "curve") return makeCurve(std::max(8.f, span * 0.5f), lanes);
398 if (scene == "bridge") return makeBridge(span, bridgeHeight, lanes);
399 if (scene == "cross") return makeCross(span, lanes);
400 if (scene == "tee" || scene == "t") return makeTee(span, lanes);
401 if (scene == "y") return makeY(span, lanes);
402 if (scene == "fork") return makeFork(span, lanes);
403 if (scene == "skew") return makeSkew(span, lanes);
404 if (scene == "t-junction" || scene == "y-junction") return makeThreeArmScene(scene, lanes);
405 if (scene == "sloped-t") return makeSlopedJunctionScene(lanes, false);
406 if (scene == "curve-uphill") return makeSlopedJunctionScene(lanes, true);
407 if (scene == "tight-turn") return makeTightTurnScene(lanes);
408 if (scene == "roundabout") return makeRoundabout(span, std::min(lanes, 3));
409 if (scene == "interchange" || scene.empty()) return makeInterchange(span, bridgeHeight, lanes, seed);
412 "scene must be "
413 "straight|curve|bridge|cross|tee|t|y|fork|skew|t-junction|y-junction|sloped-t|curve-uphill|"
414 "tight-turn|roundabout|interchange",
415 "scene"));
416}
417
418Result<RoadNetwork> RoadNetwork::makeInterchange(float span, float bridgeHeight, int lanes, std::uint32_t seed) {
419 if (!std::isfinite(span) || span < 16.f || !std::isfinite(bridgeHeight) || bridgeHeight < 1.f || lanes < 1 ||
420 lanes > 4)
422 DiagnosticCode::InvalidArgument, "span>=16, bridgeHeight>=1, lanes in [1,4] required", "interchange"));
423
424 RoadNetwork network;
425 const float half = span * 0.5f;
426 const float h = bridgeHeight;
427 // Tiny seed wobble on elevated mid-handles only — ground cross stays axis-aligned
428 // so bakeJunction keeps the same outward-center curb returns as makeCross.
429 const float wobble = 0.25f * static_cast<float>(static_cast<int>(seed % 7u) - 3);
430
431 RoadStyle ground = groundStyle();
432 ground.deckThickness = 0.08f;
433 ground.sidewalkWidth = 1.2f;
434 ground.curbWidth = 0.35f;
435 ground.curbHeight = 0.45f;
436 const float asphaltHalf = 0.5f * ground.laneWidth * static_cast<float>(lanes);
437 const float cornerR = 2.8f;
438 const float jr = asphaltHalf + cornerR;
439
440 RoadStyle bridge = bridgeStyle();
441 bridge.pierSpacing = 14.0f;
442 bridge.sidewalkWidth = 1.0f;
443 bridge.curbWidth = 0.38f;
444 bridge.curbHeight = 0.50f;
445 RoadStyle ramp = bridge;
446 ramp.sidewalkWidth = 0.5f;
447 ramp.pierSpacing = 10.f;
448
449 auto nGround = network.addNode(0.f, 0.f, 0.f, jr);
450 auto nN = network.addNode(0.f, 0.f, -half, 2.f);
451 auto nS = network.addNode(0.f, 0.f, half, 2.f);
452 auto nW = network.addNode(-half, 0.f, 0.f, 2.f);
453 auto nE = network.addNode(half, 0.f, 0.f, 2.f);
454
455 // The elevated diagonal crosses the ground graph in XZ but has its own node
456 // at bridge height. Equal planar coordinates never imply connectivity.
457 auto nBridgeNW = network.addNode(-half, h, -half, 2.0f);
458 auto nElev = network.addNode(0.f, h, 0.f, 2.0f);
459 auto nBridgeSE = network.addNode(half, h, half, 2.0f);
460 for (auto* res : {&nGround, &nN, &nS, &nW, &nE, &nBridgeNW, &nElev, &nBridgeSE}) {
461 if (!res->ok()) return Result<RoadNetwork>::failure(res->status());
462 }
463
464 auto add = [&](std::uint32_t a, std::uint32_t b, std::vector<RoadControlPoint> pts, const RoadStyle& style) {
465 return network.addEdge(a, b, std::move(pts), lanes, 0, style);
466 };
467
468 // Ground cross — same fillet construction as makeCross.
469 const auto g = nGround.value();
470 auto e1 = add(nN.value(), g, {P(0.f, 0.f, -half), P(0.f, 0.f, 0.f)}, ground);
471 auto e2 = add(g, nS.value(), {P(0.f, 0.f, 0.f), P(0.f, 0.f, half)}, ground);
472 auto e3 = add(nW.value(), g, {P(-half, 0.f, 0.f), P(0.f, 0.f, 0.f)}, ground);
473 auto e4 = add(g, nE.value(), {P(0.f, 0.f, 0.f), P(half, 0.f, 0.f)}, ground);
474
475 // The central split is a smooth continuation and therefore creates no
476 // junction apron. Seed wobble remains bounded and tangent-continuous.
477 auto e5 = add(nBridgeNW.value(), nElev.value(),
478 {P(-half, h, -half), P(-half * 0.45f + wobble, h, -half * 0.45f), P(0.f, h, 0.f)}, bridge);
479 auto e6 = add(nElev.value(), nBridgeSE.value(),
480 {P(0.f, h, 0.f), P(half * 0.45f, h, half * 0.45f + wobble), P(half, h, half)}, bridge);
481
482 // Four outer ramps connect both ground approaches to the elevated diagonal
483 // without inventing planar connectivity at the central grade separation.
484 auto r1 = network.addEdge(
485 nN.value(), nBridgeNW.value(),
486 {P(0.f, 0.f, -half), P(-half * 0.35f, h * 0.2f, -half), P(-half * 0.75f, h * 0.75f, -half), P(-half, h, -half)},
487 1, 1, ramp);
488 auto r2 = network.addEdge(
489 nW.value(), nBridgeNW.value(),
490 {P(-half, 0.f, 0.f), P(-half, h * 0.2f, -half * 0.35f), P(-half, h * 0.75f, -half * 0.75f), P(-half, h, -half)},
491 1, 1, ramp);
492 auto r3 = network.addEdge(
493 nBridgeSE.value(), nS.value(),
494 {P(half, h, half), P(half * 0.75f, h * 0.75f, half), P(half * 0.35f, h * 0.2f, half), P(0.f, 0.f, half)}, 1, 1,
495 ramp);
496 auto r4 = network.addEdge(
497 nBridgeSE.value(), nE.value(),
498 {P(half, h, half), P(half, h * 0.75f, half * 0.75f), P(half, h * 0.2f, half * 0.35f), P(half, 0.f, 0.f)}, 1, 1,
499 ramp);
500
501 for (auto* edge : {&e1, &e2, &e3, &e4, &e5, &e6, &r1, &r2, &r3, &r4}) {
502 if (!edge->ok()) return Result<RoadNetwork>::failure(edge->status());
503 }
504
505 // Route links exist only at the ground intersection. The elevated road is
506 // a separate graph despite sharing the same XZ crossing coordinate.
507 auto turns = network.connectAllTurns(g);
508 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
509 for (const auto node : {nN.value(), nW.value(), nBridgeNW.value(), nBridgeSE.value(), nS.value(), nE.value()}) {
510 turns = network.connectAllTurns(node);
511 if (!turns.ok()) return Result<RoadNetwork>::failure(turns.status());
512 }
513
514 return Result<RoadNetwork>::success(std::move(network));
515}
516
517} // namespace eve::procgen::road
double z
double y
double x
float degrees
Definition CardTypes.cpp:38
Stable, structured diagnostics shared by engine modules.
std::vector< std::vector< CompiledStatement > > lanes
Parallel lane bodies (parallel { lane {…} lane {…} }).
tensor::Graph g
Definition GpuGraph.cpp:7
float u
Definition Grass.cpp:233
double r
HexVec3 left
HexVec3 right
std::int32_t second
std::int32_t first
int h
std::uint32_t height
float length
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec2 center
std::weak_ptr< PrimitiveScene > scene
PrimitiveHandle handle
Heightmap ground
const RoadNode * node
TacticalUnit::TurnResources turn
uint32_t seed
Definition TreeMesh.cpp:276
uint32_t index
int turns
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.
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
Owning directed road graph with lane connectivity.
Definition RoadNetwork.h:19
static Result< RoadNetwork > makeStraight(float length=36.f, int lanes=2)
Scene 1: single flat straight segment (no junction).
static Result< RoadNetwork > makeCross(float span=32.f, int lanes=2)
Scene 4: simple ground-level 4-way cross (one junction disc).
static Result< RoadNetwork > makeInterchange(float span=48.f, float bridgeHeight=8.f, int lanes=2, std::uint32_t seed=1)
Build a multi-level interchange demo graph (ground cross + elevated loop + ramps).
static Result< RoadNetwork > makeBridge(float length=36.f, float height=6.f, int lanes=2)
Scene 3: elevated straight with piers.
static Result< RoadNetwork > makeY(float span=32.f, int lanes=2)
Ground-level Y-junction with three arms separated by 120 degrees.
static Result< RoadNetwork > makeFork(float span=32.f, int lanes=2)
Three-way scene with a 60-degree acute fork.
static Result< RoadNetwork > makeSkew(float span=32.f, int lanes=2)
Three-way scene with 135-degree skew corners.
Result< void > setNodeJunctionControl(std::uint32_t nodeId, RoadJunctionControl control)
Replace one node's authoritative traffic-control policy.
Result< std::uint32_t > addEdge(std::uint32_t from, std::uint32_t to, std::vector< RoadControlPoint > controlPoints, int lanesForward=2, int lanesBackward=0, RoadStyle style={})
Insert a non-degenerate directed edge between two distinct live nodes.
static Result< RoadNetwork > makeRoundabout(float span=48.f, int lanes=1)
Build a four-entry roundabout from ordinary nodes, curved edges and lane links.
static Result< RoadNetwork > makeScene(const std::string &scene, float span=36.f, float bridgeHeight=6.f, int lanes=2, std::uint32_t seed=1)
Dispatch a named debug/demo scene.
static Result< RoadNetwork > makeFan(float span, int lanes, std::vector< float > armAnglesDeg, std::vector< bool > intoHub={})
Build a ground-level N-way fan from absolute arm angles in degrees.
Result< int > connectAllTurns(std::uint32_t nodeId)
Auto-connect all forward and backward lane ports at one node.
Result< std::uint32_t > addNode(float x, float y, float z, float junctionRadius=6.f)
Insert a junction node after validating finite coordinates.
static Result< RoadNetwork > makeTee(float span=32.f, int lanes=2)
Ground-level T-junction with an east-west through road and south stem.
static Result< RoadNetwork > makeCurve(float radius=18.f, int lanes=2)
Scene 2: gentle horizontal curve (no junction).
@ Yield
Lower-priority approaches yield; equal-priority approaches all yield.
@ Forward
Travels from RoadEdge::from to RoadEdge::to.
One authored control point on a road centerline (world space, Y-up).
Definition RoadTypes.h:19
Owning cross-section, width policy and structural authoring for one road edge.
Definition RoadTypes.h:182
float curbWidth
Jersey-barrier thickness.
Definition RoadTypes.h:200
float curbHeight
Raised barrier height (readable asphalt channel).
Definition RoadTypes.h:201
int trafficPriority
Higher incoming-road values win uncontrolled junction priority.
Definition RoadTypes.h:214