载入中...
搜索中...
未找到
RoadGeometrySources.cpp
浏览该文件的文档.
1#include <array>
2#include <bit>
3#include <cmath>
4#include <limits>
5#include "common/Value.h"
6#include "procgen/MeshBuild.h"
8namespace eve::procgen::road {
9namespace {
10constexpr const char* key = "road.geometrySources";
11constexpr const char* schema = "eve.procgen.road.geometrySources";
12constexpr const char* algorithm = "fnv1a64-le-noncrypto-v1";
13template <class T>
14Result<T> failure(DiagnosticCode code, const char* message) {
16}
17bool geometryValid(const MeshBuild& m) {
18 const auto vertices = m.positions().size() / 3;
19 if (vertices == 0 || m.positions().size() % 3 || m.indices().empty() || m.indices().size() % 3 ||
20 m.positions().size() > static_cast<std::size_t>(std::numeric_limits<int>::max()) ||
21 m.indices().size() > static_cast<std::size_t>(std::numeric_limits<int>::max()) ||
22 m.normals().size() != vertices * 3 || m.uvs().size() != vertices * 2 ||
23 (!m.colors().empty() && m.colors().size() != vertices * 4) ||
24 m.triangleGroups().size() != m.indices().size() / 3)
25 return false;
26 for (const auto* values : {&m.positions(), &m.normals(), &m.uvs(), &m.colors()})
27 for (float v : *values)
28 if (!std::isfinite(v)) return false;
29 for (auto i : m.indices())
30 if (i >= vertices) return false;
31 for (auto g : m.triangleGroups())
32 if (g < 0 || static_cast<std::size_t>(g) >= m.groupNames().size()) return false;
33 return true;
34}
35std::string digest(const MeshBuild& m) {
36 std::uint64_t hash = 14695981039346656037ull;
37 auto byte = [&](std::uint8_t b) {
38 hash ^= b;
39 hash *= 1099511628211ull;
40 };
41 auto integer = [&](std::uint64_t v) {
42 for (int n = 0; n < 8; ++n) byte(static_cast<std::uint8_t>(v >> (8 * n)));
43 };
44 for (const auto* values : {&m.positions(), &m.normals(), &m.uvs(), &m.colors()}) {
45 integer(values->size());
46 for (float v : *values) integer(std::bit_cast<std::uint32_t>(v));
47 }
48 integer(m.indices().size());
49 for (auto i : m.indices()) integer(i);
50 integer(m.triangleGroups().size());
51 for (auto g : m.triangleGroups()) integer(static_cast<std::uint32_t>(g));
52 integer(m.groupNames().size());
53 for (const auto& name : m.groupNames()) {
54 integer(name.size());
55 for (unsigned char b : name) byte(b);
56 }
57 std::array<char, 16> out{};
58 constexpr char digits[] = "0123456789abcdef";
59 for (std::size_t i = 0; i < out.size(); ++i) out[15 - i] = digits[(hash >> (4 * i)) & 15u];
60 return std::string(out.data(), out.size());
61}
62bool partitionValid(std::span<const RoadGeometrySourceSpan> spans, std::size_t triangles) {
63 std::size_t next = 0;
64 for (const auto& s : spans) {
65 if (static_cast<unsigned>(s.kind) > 2 ||
66 (s.kind == RoadGeometrySourceKind::NetworkDerived ? s.id != 0 : s.id == 0) || s.firstTriangle != next ||
67 s.triangleCount == 0 || s.triangleCount > triangles - next)
68 return false;
69 next += s.triangleCount;
70 }
71 return next == triangles;
72}
73const std::int64_t* number(const eve::Value& v, const char* field) {
74 const auto* p = v.find(field);
75 return p ? p->getIf<std::int64_t>() : nullptr;
76}
77const std::string* text(const eve::Value& v, const char* field) {
78 const auto* p = v.find(field);
79 return p ? p->getIf<std::string>() : nullptr;
80}
81} // namespace
82PreparedRoadGeometrySources::PreparedRoadGeometrySources(std::vector<RoadGeometrySourceSpan> spans)
83 : spans_(std::move(spans)) {}
84PreparedRoadGeometrySources::~PreparedRoadGeometrySources() = default;
85PreparedRoadGeometrySources::PreparedRoadGeometrySources(PreparedRoadGeometrySources&&) noexcept = default;
86PreparedRoadGeometrySources& PreparedRoadGeometrySources::operator=(PreparedRoadGeometrySources&&) noexcept = default;
87std::span<const RoadGeometrySourceSpan> PreparedRoadGeometrySources::spans() const noexcept { return spans_; }
88Result<PreparedRoadGeometrySources> PreparedRoadGeometrySources::prepare(const MeshBuild& mesh) {
89 auto encoded = mesh.getMeta(key, "");
90 if (encoded.empty())
91 return failure<PreparedRoadGeometrySources>(DiagnosticCode::Unsupported, "mesh has no road source attribution");
92 if (encoded.size() > 64u * 1024u * 1024u || !geometryValid(mesh))
93 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
94 "invalid attributed mesh layout or metadata budget");
95 auto decoded = eve::Value::fromJson(encoded);
96 if (!decoded.ok()) return Result<PreparedRoadGeometrySources>::failure(decoded.status());
97 const auto& root = decoded.value();
98 auto s = text(root, "schema"), a = text(root, "algorithm"), d = text(root, "digest");
99 auto version = number(root, "version"), count = number(root, "triangles");
100 const auto* rows = root.find("spans");
101 const auto* array = rows ? rows->getIf<eve::Value::Array>() : nullptr;
102 if (!s || *s != schema || !a || *a != algorithm || !d || !version || *version != 1 || !count ||
103 *count != mesh.getIndexCount() / 3 || !array || array->size() > mesh.indices().size() / 3)
104 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
105 "unsupported or malformed road source schema");
106 if (*d != digest(mesh))
107 return failure<PreparedRoadGeometrySources>(DiagnosticCode::StaleHandle,
108 "road source metadata does not match mesh streams");
109 std::vector<RoadGeometrySourceSpan> spans;
110 spans.reserve(array->size());
111 for (const auto& row : *array) {
112 auto kind = number(row, "kind"), id = number(row, "id"), first = number(row, "first"),
113 size = number(row, "count");
114 if (!kind || *kind < 0 || *kind > 2 || !id || *id < 0 || *id > std::numeric_limits<std::uint32_t>::max() ||
115 !first || *first < 0 || *first > *count || !size || *size <= 0 || *size > *count)
116 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
117 "malformed road source interval");
118 spans.push_back({static_cast<RoadGeometrySourceKind>(*kind), static_cast<std::uint32_t>(*id),
119 static_cast<std::uint32_t>(*first), static_cast<std::uint32_t>(*size)});
120 }
121 if (!partitionValid(spans, mesh.indices().size() / 3))
122 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
123 "road source intervals do not partition the mesh");
125}
126namespace detail {
127Result<void> sealRoadGeometrySources(MeshBuild& mesh, std::span<const RoadGeometrySourceSpan> spans) {
128 if (!geometryValid(mesh) || !partitionValid(spans, mesh.indices().size() / 3))
129 return failure<void>(DiagnosticCode::InvariantViolation, "producer road source partition is invalid");
131 rows.reserve(spans.size());
132 for (const auto& s : spans)
133 rows.emplace_back(eve::Value::Object{{"kind", static_cast<int>(s.kind)},
134 {"id", static_cast<std::int64_t>(s.id)},
135 {"first", static_cast<std::int64_t>(s.firstTriangle)},
136 {"count", static_cast<std::int64_t>(s.triangleCount)}});
137 eve::Value root = eve::Value::Object{{"schema", schema},
138 {"version", 1},
139 {"algorithm", algorithm},
140 {"digest", digest(mesh)},
141 {"triangles", mesh.getIndexCount() / 3},
142 {"spans", std::move(rows)}};
143 auto encoded = root.toJson();
144 if (!encoded.ok()) return Result<void>::failure(encoded.status());
145 if (encoded.value().size() > 64u * 1024u * 1024u)
146 return failure<void>(DiagnosticCode::InvalidArgument, "road source metadata exceeds budget");
147 mesh.setMeta(key, encoded.value());
148 return Result<void>::success();
149}
150} // namespace detail
151} // namespace eve::procgen::road
double m[16]
int root
Definition AnimSmr.cpp:119
const std::string & s
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
int rows
std::string message
DiagnosticCode code
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
std::vector< std::uint32_t > indices
float v
std::int32_t first
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< TriangleRef > triangles
OceanMeshSettings mesh
std::set< std::string > values
std::string hash
float d
int detail
double number
std::string text
ScriptStructure out
std::string kind
std::string digest
int count
std::array< double, 2 > vertices[3]
float size
Definition TreeMesh.cpp:158
Owning, renderer-independent dynamic values.
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 failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:187
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning validated source snapshot. No mesh pointer survives preparation. Schema eve....
PreparedRoadGeometrySources(PreparedRoadGeometrySources &&) noexcept
Transfer the snapshot; invalidates old borrowed spans.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
Result< void > sealRoadGeometrySources(MeshBuild &mesh, std::span< const RoadGeometrySourceSpan > spans)
Seal producer-owned intervals into full-mesh metadata; synchronous borrowed inputs,...
RoadGeometrySourceKind
Source category, scoped to one immutable road bake rather than a live object handle.
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Contiguous triangle interval; nonzero ids name author edges/nodes, NetworkDerived uses id zero.