载入中...
搜索中...
未找到
RoadRailway.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
4
5#include <array>
6#include <cmath>
7
8namespace eve::procgen::road {
9namespace {
10
11constexpr float kStandardGauge = 1.435f;
12constexpr float kTrackCenters = 4.0f;
13
14RoadFacilityPrefab sleeperPrefab(float halfWidth) {
15 RoadFacilityPrefab prefab;
16 prefab.assetId = "procgen.railway.sleeper";
17 prefab.length = 0.65f;
18 prefab.automaticSpacing = true;
19 prefab.averageSpacing = true;
20 prefab.middleCopies = 1;
21 const float x0 = -halfWidth, x1 = halfWidth, y0 = 0.f, y1 = 0.10f, z0 = 0.08f, z1 = 0.30f;
22 const std::array<std::array<float, 3>, 8> points{{{x0, y0, z0},
23 {x1, y0, z0},
24 {x1, y1, z0},
25 {x0, y1, z0},
26 {x0, y0, z1},
27 {x1, y0, z1},
28 {x1, y1, z1},
29 {x0, y1, z1}}};
30 const int faces[6][4]{{0, 3, 2, 1}, {4, 5, 6, 7}, {0, 4, 7, 3}, {1, 2, 6, 5}, {0, 1, 5, 4}, {3, 7, 6, 2}};
31 const float normals[6][3]{{0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}};
32 for (int f = 0; f < 6; ++f) {
33 const auto base = static_cast<std::uint32_t>(prefab.vertices.size());
34 for (int i = 0; i < 4; ++i) {
35 const auto& p = points[static_cast<std::size_t>(faces[f][i])];
36 prefab.vertices.push_back({p[0], p[1], p[2], normals[f][0], normals[f][1], normals[f][2],
37 i == 1 || i == 2 ? 1.f : 0.f, i >= 2 ? 1.f : 0.f});
38 }
39 prefab.triangles.push_back({base, base + 1u, base + 2u, RoadMaterial::Deck});
40 prefab.triangles.push_back({base, base + 2u, base + 3u, RoadMaterial::Deck});
41 }
42 return prefab;
43}
44
45RoadContinuousFacility railFacility(std::uint32_t id, float lateral) {
46 RoadContinuousFacility facility;
47 facility.id = id;
50 facility.uvMeters = 1.5f;
51 facility.profile = {{-.035f, 0.f, RoadMaterial::Curb},
52 {.035f, 0.f, RoadMaterial::Curb},
53 {.035f, .14f, RoadMaterial::Curb},
54 {-.035f, .14f, RoadMaterial::Curb}};
55 return facility;
56}
57
58RoadContinuousFacility sleeperFacility(std::uint32_t id, float lateral, float halfWidth) {
59 RoadContinuousFacility facility;
60 facility.id = id;
63 facility.uvMeters = 2.f;
64 facility.capStart = false;
65 facility.capEnd = false;
70 facility.prefab = sleeperPrefab(halfWidth);
71 return facility;
72}
73
74} // namespace
75
77 if (tracks < 1 || tracks > 2)
79 Diagnostic::error(DiagnosticCode::InvalidArgument, "railway tracks must be 1 or 2", "tracks"));
80
81 RoadStyle style;
82 style.laneWidth = kStandardGauge;
83 style.curbWidth = 0.f;
84 style.curbHeight = 0.f;
85 style.sidewalkWidth = 0.f;
86 style.sidewalkHeight = 0.f;
87 style.markingWidth = 0.f;
88 style.dashLength = 0.f;
89 style.dashGap = 0.f;
90 style.deckThickness = 0.35f;
91 style.pierClearance = 100.f;
92 style.uvMeters = 4.f;
93 style.sideObjectsLeft = false;
94 style.sideObjectsRight = false;
95 style.automaticStructures = true;
96 style.speedLimitMps = 33.3333f; // ~120 km/h rail cruise default for nav overlay.
97 style.samplingSpacingMeters = 2.f;
98
99 const float halfExtent = tracks == 1 ? 1.6f : (0.5f * kTrackCenters + 1.6f);
100 const float toe = halfExtent + 0.45f;
101 // Open loft ribbon: left toe → ballast crown (asphalt driving span) → right toe.
102 style.crossSection = {{-toe, -0.18f, RoadMaterial::Deck},
105 {toe, -0.18f, RoadMaterial::Deck}};
106
107 std::uint32_t nextId = 101;
108 for (int track = 0; track < tracks; ++track) {
109 const float center =
110 tracks == 1 ? 0.f : (static_cast<float>(track) - 0.5f * static_cast<float>(tracks - 1)) * kTrackCenters;
111 style.facilities.push_back(railFacility(nextId++, center - 0.5f * kStandardGauge));
112 style.facilities.push_back(railFacility(nextId++, center + 0.5f * kStandardGauge));
113 style.facilities.push_back(sleeperFacility(nextId++, center, 0.5f * kStandardGauge + 0.25f));
114 }
115
116 auto valid = makeRoadProfile(style, tracks, 0);
117 if (!valid.ok()) return Result<RoadStyle>::failure(valid.status());
118 return Result<RoadStyle>::success(std::move(style));
119}
120
122 auto style = makeRailwayStyle(tracks);
123 if (!style.ok()) return Result<void>::failure(style.status());
124
125 auto candidate = network;
126 for (const auto& edge : network.edges()) {
127 RoadStyle next = style.value();
128 next.structures = edge.style.structures;
129 next.automaticStructures = edge.style.automaticStructures;
130 next.deckThickness = edge.style.deckThickness;
131 next.pierWidth = edge.style.pierWidth;
132 next.pierDepth = edge.style.pierDepth;
133 next.pierSpacing = edge.style.pierSpacing;
134 next.pierClearance = edge.style.pierClearance;
135 next.terrainPolicy = edge.style.terrainPolicy;
136 auto styled = candidate.setEdgeStyle(edge.id, next);
137 if (!styled.ok()) return styled;
138 auto lanes = candidate.setEdgeLaneCounts(edge.id, tracks, 0);
139 if (!lanes.ok()) return Result<void>::failure(lanes.status());
140 }
141 for (const auto& node : candidate.nodes()) {
142 int incident = 0;
143 for (const auto& edge : candidate.edges())
144 if (edge.from == node.id || edge.to == node.id) ++incident;
145 if (incident < 2) continue;
146 auto linked = candidate.connectAllTurns(node.id);
147 if (!linked.ok()) return Result<void>::failure(linked.status());
148 }
149 network = std::move(candidate);
150 return Result<void>::success();
151}
152
153} // namespace eve::procgen::road
double lateral
std::string prefab
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
std::vector< std::vector< CompiledStatement > > lanes
Parallel lane bodies (parallel { lane {…} lane {…} }).
std::vector< float > normals
float halfWidth
std::vector< Track > tracks
std::string id
bool valid
glm::dvec2 center
float f
std::shared_ptr< const std::vector< glm::vec2 > > points
const RoadNode * node
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
const std::vector< RoadEdge > & edges() const noexcept
Edges.
Result< RoadProfile > makeRoadProfile(const RoadStyle &style, int lanesForward, int lanesBackward)
Build a mathematical road cross-section for the given lane counts.
Result< RoadStyle > makeRailwayStyle(int tracks)
Build a procedural railway edge style (ballast bed, rails, sleepers).
Result< void > applyRailwayRoadType(RoadNetwork &network, int tracks)
Rewrite every edge to a railway style while preserving bridge/tunnel intervals.
RoadFacilityPrefab prefab
Empty source mesh selects section sweep; profile remains the socket envelope.
Definition RoadTypes.h:113
float lateralOffset
Metres from the authored centerline, signed along its side axis.
Definition RoadTypes.h:116
std::uint32_t id
Unique within an edge style; preserved by split and reversal.
Definition RoadTypes.h:114
std::vector< RoadProfilePoint > profile
Closed simple section; outgoing segment material.
Definition RoadTypes.h:118
Owning cross-section, width policy and structural authoring for one road edge.
Definition RoadTypes.h:182
std::vector< RoadProfilePoint > crossSection
Empty selects the standard road; custom segment tags are explicit.
Definition RoadTypes.h:192
bool sideObjectsRight
Emit anchors on the authored centerline's right side.
Definition RoadTypes.h:220
float samplingSpacingMeters
New roads use the measured 2m trigger; explicit zero keeps legacy fixed counts.
Definition RoadTypes.h:221
float speedLimitMps
Navigation speed limit in metres per second.
Definition RoadTypes.h:213
float uvMeters
World-space metres per texture repeat.
Definition RoadTypes.h:212
float curbWidth
Jersey-barrier thickness.
Definition RoadTypes.h:200
std::vector< RoadStructureInterval > structures
Explicit normalized arc intervals; nonempty disables legacy automatic structures.
Definition RoadTypes.h:196
std::vector< RoadContinuousFacility > facilities
Continuous swept facilities, clipped by sockets.
Definition RoadTypes.h:197
bool sideObjectsLeft
Emit anchors on the authored centerline's left side.
Definition RoadTypes.h:219
float curbHeight
Raised barrier height (readable asphalt channel).
Definition RoadTypes.h:201
bool automaticStructures
Empty intervals retain legacy decks/piers unless explicitly disabled.
Definition RoadTypes.h:194
RoadTerrainPolicy terrainPolicy
Authoritative ground-channel policy; execution remains explicit.
Definition RoadTypes.h:228