载入中...
搜索中...
未找到
RoadBakeFacilityLinks.cpp
浏览该文件的文档.
4
5#include <algorithm>
6#include <cmath>
7#include <optional>
8#include <set>
9
11namespace {
12struct Peer {
13 const RoadEdge* edge;
14 const RoadContinuousFacility* facility;
15};
16template <class Mesh>
17bool samePrefabMesh(const Mesh& a, const Mesh& b) {
18 if (a.assetId != b.assetId || std::fabs(a.length - b.length) > 1e-4f || a.vertices.size() != b.vertices.size() ||
19 a.triangles.size() != b.triangles.size())
20 return false;
21 for (std::size_t i = 0; i < a.vertices.size(); ++i) {
22 const auto& x = a.vertices[i];
23 const auto& y = b.vertices[i];
24 const float av[]{x.x, x.y, x.z, x.nx, x.ny, x.nz, x.u, x.v}, bv[]{y.x, y.y, y.z, y.nx, y.ny, y.nz, y.u, y.v};
25 for (int k = 0; k < 8; ++k)
26 if (std::fabs(av[k] - bv[k]) > 1e-4f) return false;
27 }
28 for (std::size_t i = 0; i < a.triangles.size(); ++i) {
29 const auto& x = a.triangles[i];
30 const auto& y = b.triangles[i];
31 if (x.a != y.a || x.b != y.b || x.c != y.c || x.material != y.material || x.smoothingGroup != y.smoothingGroup)
32 return false;
33 }
34 return true;
35}
36bool isCompatible(const RoadEdge& first, const RoadContinuousFacility& a, const RoadEdge& second,
37 const RoadContinuousFacility& b, std::uint32_t nodeId) {
38 if (a.prefab.vertices.empty() != b.prefab.vertices.empty() || a.prefab.assetId != b.prefab.assetId) return false;
39 const float firstSign = first.to == nodeId ? 1.f : -1.f, secondSign = second.from == nodeId ? 1.f : -1.f;
40 if (!a.prefab.vertices.empty()) {
41 auto x = a.prefab, y = b.prefab;
42 if (firstSign < 0.f) reverseFacilityPrefab(x);
43 if (secondSign < 0.f) reverseFacilityPrefab(y);
44 if (x.automaticSpacing != y.automaticSpacing || x.averageSpacing != y.averageSpacing ||
45 (!x.averageSpacing && x.reverseRepeatSequence != y.reverseRepeatSequence) || !samePrefabMesh(x, y) ||
46 !samePrefabMesh(x.startMesh, y.startMesh) || !samePrefabMesh(x.endMesh, y.endMesh))
47 return false;
48 }
49 if (std::fabs(a.lateralOffset * firstSign - b.lateralOffset * secondSign) > 1e-4f ||
50 std::fabs(a.verticalOffset - b.verticalOffset) > 1e-4f || std::fabs(a.uvMeters - b.uvMeters) > 1e-4f ||
51 a.profile.size() != b.profile.size())
52 return false;
53 auto profileA = a.profile, profileB = b.profile;
54 if (firstSign < 0.f) reverseSectionGeometry(profileA, true);
55 if (secondSign < 0.f) reverseSectionGeometry(profileB, true);
56 for (std::size_t i = 0; i < profileA.size(); ++i)
57 if (std::fabs(profileA[i].side - profileB[i].side) > 1e-4f ||
58 std::fabs(profileA[i].up - profileB[i].up) > 1e-4f || profileA[i].material != profileB[i].material)
59 return false;
60 return true;
61}
62std::optional<Peer> peer(const RoadNetwork& network, std::uint32_t nodeId, const RoadEdge& edge,
63 const RoadContinuousFacility& facility) {
64 if ((edge.from == nodeId && facility.start > 1e-5f) || (edge.to == nodeId && facility.end < 1.f - 1e-5f))
65 return std::nullopt;
66 const RoadEdge* other = nullptr;
67 const auto node = network.nodeResult(nodeId);
68 if (!node.ok()) return std::nullopt;
69 if (!node.value().connection.facilityRoutes.empty()) {
70 for (const auto& route : node.value().connection.facilityRoutes) {
71 std::uint32_t otherId = 0, facilityId = 0;
72 if (route.firstEdge == edge.id && route.firstFacility == facility.id) {
73 otherId = route.secondEdge;
74 facilityId = route.secondFacility;
75 } else if (route.secondEdge == edge.id && route.secondFacility == facility.id) {
76 otherId = route.firstEdge;
77 facilityId = route.firstFacility;
78 }
79 for (const auto& item : network.edges())
80 if (item.id == otherId)
81 for (const auto& f : item.style.facilities)
82 if (f.id == facilityId) return Peer{&item, &f};
83 }
84 return std::nullopt;
85 }
86 int count = 0;
87 for (const auto& item : network.edges())
88 if (item.from == nodeId || item.to == nodeId) {
89 ++count;
90 if (item.id != edge.id) other = &item;
91 }
92 if (count != 2 || !other) return std::nullopt;
93 for (const auto& candidate : other->style.facilities) {
94 if ((other->from == nodeId && candidate.start > 1e-5f) || (other->to == nodeId && candidate.end < 1.f - 1e-5f))
95 continue;
96 if (facility.id == candidate.id && isCompatible(edge, facility, *other, candidate, nodeId))
97 return Peer{other, &candidate};
98 }
99 return std::nullopt;
100}
101V3 shifted(const SplineFrameSample& frame, const RoadEdge& edge, const RoadContinuousFacility& facility) {
102 const float lateral = scaledProfileSide(facility.lateralOffset, asphaltHalfWidth(edge), frame.sample.scaleX);
103 return V3{frame.sample.x, frame.sample.y, frame.sample.z} + V3{frame.sideX, frame.sideY, frame.sideZ} * lateral +
104 V3{frame.upX, frame.upY, frame.upZ} * facility.verticalOffset;
105}
106} // namespace
108 if (node.connection.facilityRoutes.size() > 128u)
109 return bakeFail<void>(DiagnosticCode::InvalidArgument, "too many facility routes", "facilityRoutes");
110 std::set<std::pair<std::uint32_t, std::uint32_t>> endpoints;
111 auto lookup = [&](std::uint32_t edgeId, std::uint32_t facilityId) -> std::optional<Peer> {
112 for (const auto& edge : network.edges()) {
113 if (edge.id != edgeId || (edge.from != node.id && edge.to != node.id)) continue;
114 for (const auto& f : edge.style.facilities)
115 if (f.id == facilityId && (edge.from != node.id || f.start <= 1e-5f) &&
116 (edge.to != node.id || f.end >= 1.f - 1e-5f))
117 return Peer{&edge, &f};
118 }
119 return std::nullopt;
120 };
121 for (const auto& route : node.connection.facilityRoutes) {
122 const auto a = lookup(route.firstEdge, route.firstFacility), b = lookup(route.secondEdge, route.secondFacility);
123 if (route.firstEdge == route.secondEdge || !a || !b ||
124 !endpoints.insert({route.firstEdge, route.firstFacility}).second ||
125 !endpoints.insert({route.secondEdge, route.secondFacility}).second ||
126 !isCompatible(*a->edge, *a->facility, *b->edge, *b->facility, node.id))
127 return bakeFail<void>(DiagnosticCode::PreconditionViolation,
128 "facility route has stale, duplicate, clipped or incompatible endpoints",
129 "facilityRoutes");
130 }
131 return Result<void>::success();
132}
133bool isFacilityContinued(const RoadNetwork& network, std::uint32_t nodeId, const RoadEdge& edge,
135 return peer(network, nodeId, edge, facility).has_value();
136}
138 const std::unordered_set<std::uint32_t>& activeNodes) {
139 for (const auto& node : network.nodes()) {
140 if (!activeNodes.contains(node.id)) continue;
141 for (const auto& edge : network.edges()) {
142 if (edge.from != node.id && edge.to != node.id) continue;
143 for (const auto& facility : edge.style.facilities) {
144 const auto other = peer(network, node.id, edge, facility);
145 if (!other || edge.id > other->edge->id) continue;
146 auto firstPath = edgeToSpline(edge), secondPath = edgeToSpline(*other->edge);
147 if (!firstPath.ok()) return Result<void>::failure(firstPath.status());
148 if (!secondPath.ok()) return Result<void>::failure(secondPath.status());
149 auto firstLength = firstPath.value().lengthResult(24),
150 secondLength = secondPath.value().lengthResult(24);
151 if (!firstLength.ok()) return Result<void>::failure(firstLength.status());
152 if (!secondLength.ok()) return Result<void>::failure(secondLength.status());
153 const float firstDistance = junctionSocketDistance(network, node, edge, firstLength.value());
154 const float secondDistance = junctionSocketDistance(network, node, *other->edge, secondLength.value());
155 auto first = firstPath.value().travelFrameResult(
156 edge.from == node.id ? firstDistance : firstLength.value() - firstDistance, "clamp", 24);
157 auto second = secondPath.value().travelFrameResult(
158 other->edge->from == node.id ? secondDistance : secondLength.value() - secondDistance, "clamp", 24);
159 if (!first.ok()) return Result<void>::failure(first.status());
160 if (!second.ok()) return Result<void>::failure(second.status());
161 if (node.connection.ports.empty()) {
162 const auto plane = fitJunctionPlane(network, node);
163 blendFrameToJunctionPlane(first.value(), plane, 1.f);
164 blendFrameToJunctionPlane(second.value(), plane, 1.f);
165 }
166 const auto& a = first.value();
167 const auto& b = second.value();
168 const V3 p0 = shifted(a, edge, facility), p1 = shifted(b, *other->edge, *other->facility);
169 const float span = length(p1 - p0);
170 if (span < 1e-5f) continue;
171 const V3 f0 = V3{a.forwardX, a.forwardY, a.forwardZ} * (edge.to == node.id ? 1.f : -1.f);
172 const V3 f1 = V3{b.forwardX, b.forwardY, b.forwardZ} * (other->edge->from == node.id ? 1.f : -1.f);
173 const V3 control0 = p0 + f0 * (span / 3.f), control1 = p1 - f1 * (span / 3.f);
174 std::vector<SplineFrameSample> frames;
175 for (int i = 0; i <= options.turnSamples; ++i) {
176 const float t = static_cast<float>(i) / static_cast<float>(options.turnSamples), u = 1.f - t;
177 const V3 position = p0 * (u * u * u) + control0 * (3.f * u * u * t) + control1 * (3.f * u * t * t) +
178 p1 * (t * t * t);
179 const V3 forward = normalize((control0 - p0) * (u * u) + (control1 - control0) * (2.f * u * t) +
180 (p1 - control1) * (t * t));
181 const V3 upHint = normalize(
182 V3{std::lerp(a.upX, b.upX, t), std::lerp(a.upY, b.upY, t), std::lerp(a.upZ, b.upZ, t)});
183 const V3 side = normalize(cross(upHint, forward)), up = normalize(cross(forward, side));
186 frame.sample.y = position.y;
187 frame.sample.z = position.z;
188 frame.sample.normalizedDistance = t;
189 frame.sideX = side.x;
190 frame.sideY = side.y;
191 frame.sideZ = side.z;
192 frame.upX = up.x;
193 frame.upY = up.y;
194 frame.upZ = up.z;
195 frame.forwardX = forward.x;
196 frame.forwardY = forward.y;
197 frame.forwardZ = forward.z;
198 frames.push_back(frame);
199 }
200 RoadProfile profile;
201 profile.points = facility.profile;
202 profile.closed = true;
203 profile.explicitMaterials = true;
204 if (edge.from == node.id) reverseSectionGeometry(profile.points, true);
205 if (!facility.prefab.vertices.empty()) {
206 auto prefab = facility.prefab;
207 if (edge.from == node.id) reverseFacilityPrefab(prefab);
208 float sourceLow = 0.f, sourceHigh = 1.f;
209 const auto& next = *other->facility;
210 const float nextStart =
211 other->edge->from == node.id ? next.prefab.sourceStart : 1.f - next.prefab.sourceEnd;
212 if (prefab.middleCopies == next.prefab.middleCopies &&
213 std::fabs(prefab.sourceEnd - nextStart) < 1e-4f) {
214 const float rawFirst =
215 std::lerp(facility.prefab.sourceStart, facility.prefab.sourceEnd,
216 (a.sample.normalizedDistance - facility.start) / (facility.end - facility.start));
217 const float rawSecond =
218 std::lerp(next.prefab.sourceStart, next.prefab.sourceEnd,
219 (b.sample.normalizedDistance - next.start) / (next.end - next.start));
220 sourceLow = edge.to == node.id ? rawFirst : 1.f - rawFirst;
221 sourceHigh = other->edge->from == node.id ? rawSecond : 1.f - rawSecond;
222 }
223 float connectorLength = 0.f;
224 for (std::size_t i = 1; i < frames.size(); ++i)
225 connectorLength += length(V3{frames[i].sample.x - frames[i - 1].sample.x,
226 frames[i].sample.y - frames[i - 1].sample.y,
227 frames[i].sample.z - frames[i - 1].sample.z});
228 auto deformed =
229 bakeFacilityPrefab(mesh, prefab, frames, sourceLow, sourceHigh,
230 connectorLength / (sourceHigh - sourceLow), options.maximumMeshElements);
231 if (!deformed.ok()) return deformed;
232 continue;
233 }
234 auto loft = loftProfileClean(mesh, frames, profile, span, facility.uvMeters, RoadMaterial::Curb,
235 options.maximumMeshElements);
236 if (!loft.ok()) return loft;
237 }
238 }
239 }
240 return Result<void>::success();
241}
242} // namespace eve::procgen::road::detail
double y
double x
double lateral
std::string prefab
std::string nodeId
float u
Definition Grass.cpp:233
HexVec3 up
std::int32_t second
std::int32_t first
float length
std::array< float, 3 > position
std::string to
std::vector< Edge > edges
std::string from
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanMeshSettings mesh
ssq::Object frame
std::array< uint32_t, 16 > material
float f
Object::Ptr value
float t
const RoadNode * node
const SquirrelValueOptions & options
int count
V forward
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
const std::vector< RoadEdge > & edges() const noexcept
Edges.
const std::vector< RoadNode > & nodes() const noexcept
Nodes.
Result< void > bakeFacilityPrefab(MeshBuild &mesh, const RoadFacilityPrefab &prefab, const std::vector< SplineFrameSample > &frames, float sourceLow, float sourceHigh, float fullSourceLength, std::size_t budget)
Clip source triangles into frame slabs and deform their positions and inverse-transpose normals.
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
void reverseFacilityPrefab(RoadFacilityPrefab &prefab)
Reflect an owned prefab into reverse traversal coordinates, including its segment sequence/window.
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.
void reverseSectionGeometry(std::vector< RoadProfilePoint > &section, bool closed)
Reflect an owning section and preserve outgoing segment semantics.
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 > validateFacilityRoutes(const RoadNetwork &network, const RoadNode &node)
Validate explicit one-to-one routes, compatible sections and live incident endpoint ownership.
JunctionPlane fitJunctionPlane(const RoadNetwork &network, const RoadNode &node)
Module-private road bake implementation helper.
Definition RoadBake.cpp:197
Result< void > loftProfileClean(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadProfile &profile, float pathLength, float uvMeters, RoadMaterial asphaltMaterial, std::size_t budget, int subdivisions=1)
Module-private road bake implementation helper.
bool isFacilityContinued(const RoadNetwork &network, std::uint32_t nodeId, const RoadEdge &edge, const RoadContinuousFacility &facility)
Query compatible degree-two facility continuation; no retained peer pointer.
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
Owning section frame; sample position includes roll-pivot displacement, unlike the author centerline ...
Definition SplinePath.h:63
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Continuous facility over an authored edge interval; all profiles and prefab meshes are owned values.
Definition RoadTypes.h:112
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