载入中...
搜索中...
未找到
RoadProfile.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include "common/Diagnostic.h"
6
7#include <cmath>
8
9namespace eve::procgen::road {
10namespace {
11
12Result<void> validateStyle(const RoadStyle& style) {
13 const float values[] = {style.laneWidth, style.curbWidth, style.curbHeight, style.sidewalkWidth,
14 style.sidewalkHeight, style.deckThickness, style.pierWidth, style.pierDepth,
15 style.pierSpacing, style.pierClearance, style.markingWidth, style.dashLength,
16 style.dashGap, style.uvMeters};
17 for (float v : values) {
18 if (!std::isfinite(v) || v < 0.f)
20 DiagnosticCode::InvalidArgument, "road style values must be finite and non-negative", "style"));
21 }
22 if (style.laneWidth <= 1e-4f)
24 Diagnostic::error(DiagnosticCode::InvalidArgument, "laneWidth must be positive", "laneWidth"));
25 if (style.uvMeters <= 1e-4f)
27 Diagnostic::error(DiagnosticCode::InvalidArgument, "uvMeters must be positive", "uvMeters"));
29}
30
31void pushPoint(RoadProfile& profile, float side, float up, RoadMaterial material) {
32 profile.points.push_back(RoadProfilePoint{side, up, material});
33}
34
35} // namespace
36
38 switch (material) {
39 case RoadMaterial::Asphalt: return "asphalt";
40 case RoadMaterial::Curb: return "curb";
41 case RoadMaterial::Sidewalk: return "sidewalk";
42 case RoadMaterial::Deck: return "deck";
43 case RoadMaterial::Pier: return "pier";
44 case RoadMaterial::Marking: return "marking";
45 case RoadMaterial::MarkingYellow: return "markingYellow";
46 case RoadMaterial::Nav: return "nav";
47 case RoadMaterial::BridgeAsphalt: return "asphalt.bridge";
48 case RoadMaterial::TunnelAsphalt: return "asphalt.tunnel";
49 case RoadMaterial::Tunnel: return "tunnel";
50 case RoadMaterial::TunnelPortalAsphalt: return "asphalt.tunnel.portal";
51 }
52 return "asphalt";
53}
54
55Result<RoadProfile> makeRoadProfile(const RoadStyle& style, int lanesForward, int lanesBackward) {
56 auto ok = validateStyle(style);
57 if (!ok.ok()) return Result<RoadProfile>::failure(ok.status());
58 if (lanesForward < 0 || lanesForward > 8)
60 Diagnostic::error(DiagnosticCode::InvalidArgument, "lanesForward must be in [0,8]", "lanesForward"));
61 if (lanesBackward < 0 || lanesBackward > 8)
63 Diagnostic::error(DiagnosticCode::InvalidArgument, "lanesBackward must be in [0,8]", "lanesBackward"));
64 if (lanesForward + lanesBackward < 1)
66 DiagnosticCode::InvalidArgument, "road must contain at least one directional lane", "lanes"));
67
68 const float asphaltHalf = 0.5f * style.laneWidth * static_cast<float>(lanesForward + lanesBackward);
69 if (!style.crossSection.empty()) {
70 std::vector<std::pair<float, float>> coverage;
71 const std::size_t segments =
72 style.crossSectionClosed ? style.crossSection.size() : style.crossSection.size() - 1;
73 for (std::size_t i = 0; i < segments; ++i) {
74 const auto& a = style.crossSection[i];
75 const auto& b = style.crossSection[(i + 1) % style.crossSection.size()];
76 if (a.material == RoadMaterial::Asphalt && std::fabs(a.side - b.side) > 1e-5f)
77 coverage.emplace_back(std::min(a.side, b.side), std::max(a.side, b.side));
78 }
79 std::sort(coverage.begin(), coverage.end());
80 for (std::size_t i = 1; i < coverage.size(); ++i)
81 if (coverage[i].first < coverage[i - 1].second - 1e-5f)
84 "custom driving surface must be single-valued across its lateral range", "style.crossSection"));
85 float covered = -asphaltHalf;
86 for (const auto& span : coverage) {
87 if (span.first > covered + 1e-4f) break;
88 covered = std::max(covered, span.second);
89 }
90 if (coverage.empty() || coverage.front().first > -asphaltHalf + 1e-4f || covered < asphaltHalf - 1e-4f)
93 "custom asphalt section does not cover every driving lane", "style.crossSection"));
94 RoadProfile profile;
95 profile.points = style.crossSection;
96 profile.closed = style.crossSectionClosed;
97 profile.explicitMaterials = true;
98 for (const auto& point : profile.points) profile.halfWidth = std::max(profile.halfWidth, std::fabs(point.side));
99 return Result<RoadProfile>::success(std::move(profile));
100 }
101 const float barrierInner = asphaltHalf;
102 const float barrierOuter = asphaltHalf + style.curbWidth;
103 const float sidewalkOuter = barrierOuter + style.sidewalkWidth;
104 const float barrierTop = style.curbHeight;
105 const float walkH = style.sidewalkHeight;
106
107 RoadProfile profile;
108 profile.halfWidth = sidewalkOuter;
109
110 // Jersey-barrier U-channel matching the reference interchange look:
111 // sidewalk → tall barrier outer/top/inner → asphalt deck → mirror.
112 pushPoint(profile, -sidewalkOuter, walkH, RoadMaterial::Sidewalk);
113 pushPoint(profile, -barrierOuter, walkH, RoadMaterial::Sidewalk);
114 pushPoint(profile, -barrierOuter, barrierTop, RoadMaterial::Curb);
115 pushPoint(profile, -barrierInner, barrierTop, RoadMaterial::Curb);
116 pushPoint(profile, -barrierInner, 0.f, RoadMaterial::Curb);
117 pushPoint(profile, barrierInner, 0.f, RoadMaterial::Asphalt);
118 pushPoint(profile, barrierInner, barrierTop, RoadMaterial::Curb);
119 pushPoint(profile, barrierOuter, barrierTop, RoadMaterial::Curb);
120 pushPoint(profile, barrierOuter, walkH, RoadMaterial::Sidewalk);
121 pushPoint(profile, sidewalkOuter, walkH, RoadMaterial::Sidewalk);
122
123 return Result<RoadProfile>::success(std::move(profile));
124}
125
126} // namespace eve::procgen::road
Stable, structured diagnostics shared by engine modules.
float v
HexVec3 up
std::int32_t second
std::int32_t first
std::string coverage
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::array< uint32_t, 16 > material
std::set< std::string > values
int segments
Definition TreeMesh.cpp:281
int covered
glm::vec3 point
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
Result< void > validateRoadAuthoring(const RoadStyle &style)
Validate structural intervals and continuous-facility authoring.
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...
RoadMaterial
Profile material tags used when lofting a road cross-section.
Definition RoadTypes.h:37
@ TunnelPortalAsphalt
Driving surface inside an authored tunnel portal footprint.
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
std::vector< RoadProfilePoint > crossSection
Empty selects the standard road; custom segment tags are explicit.
Definition RoadTypes.h:192
bool crossSectionClosed
Close the final-to-first section segment.
Definition RoadTypes.h:193
float curbWidth
Jersey-barrier thickness.
Definition RoadTypes.h:200
float curbHeight
Raised barrier height (readable asphalt channel).
Definition RoadTypes.h:201