10constexpr const char*
key =
"road.geometrySources";
11constexpr const char* schema =
"eve.procgen.road.geometrySources";
12constexpr const char* algorithm =
"fnv1a64-le-noncrypto-v1";
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()) ||
23 (!
m.colors().empty() &&
m.colors().size() !=
vertices * 4) ||
24 m.triangleGroups().size() !=
m.indices().size() / 3)
26 for (
const auto*
values : {&
m.positions(), &
m.normals(), &
m.uvs(), &
m.colors()})
28 if (!
std::isfinite(
v)) return false;
31 for (
auto g :
m.triangleGroups())
32 if (
g < 0 || static_cast<
std::size_t>(
g) >=
m.groupNames().
size()) return false;
35std::string
digest(
const MeshBuild&
m) {
36 std::uint64_t
hash = 14695981039346656037ull;
37 auto byte = [&](std::uint8_t
b) {
39 hash *= 1099511628211ull;
41 auto integer = [&](std::uint64_t
v) {
42 for (
int n = 0; n < 8; ++n) byte(static_cast<std::uint8_t>(
v >> (8 *
n)));
44 for (
const auto*
values : {&
m.positions(), &
m.normals(), &
m.uvs(), &
m.colors()}) {
51 for (
auto g :
m.triangleGroups())
integer(static_cast<
std::uint32_t>(
g));
53 for (
const auto&
name :
m.groupNames()) {
55 for (
unsigned char b :
name) byte(
b);
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());
62bool partitionValid(std::span<const RoadGeometrySourceSpan> spans, std::size_t
triangles) {
64 for (
const auto&
s : spans) {
65 if (
static_cast<unsigned>(
s.kind) > 2 ||
69 next +=
s.triangleCount;
74 const auto*
p =
v.find(field);
75 return p ?
p->getIf<std::int64_t>() : nullptr;
78 const auto*
p =
v.find(field);
79 return p ?
p->getIf<std::string>() : nullptr;
83 : spans_(
std::move(spans)) {}
84PreparedRoadGeometrySources::~PreparedRoadGeometrySources() =
default;
85PreparedRoadGeometrySources::PreparedRoadGeometrySources(PreparedRoadGeometrySources&&) noexcept = default;
86PreparedRoadGeometrySources& PreparedRoadGeometrySources::operator=(PreparedRoadGeometrySources&&) noexcept = default;
89 auto encoded =
mesh.getMeta(
key,
"");
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");
97 const auto&
root = decoded.value();
100 const auto*
rows =
root.find(
"spans");
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");
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) {
114 if (!
kind || *kind < 0 || *kind > 2 || !
id || *id < 0 || *id > std::numeric_limits<std::uint32_t>::max() ||
116 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
117 "malformed road source interval");
119 static_cast<std::uint32_t
>(*first),
static_cast<std::uint32_t
>(*size)});
121 if (!partitionValid(spans,
mesh.indices().size() / 3))
122 return failure<PreparedRoadGeometrySources>(DiagnosticCode::InvalidArgument,
123 "road source intervals do not partition the mesh");
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)
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)}});
139 {
"algorithm", algorithm},
141 {
"triangles",
mesh.getIndexCount() / 3},
142 {
"spans", std::move(
rows)}};
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();
std::vector< std::uint32_t > indices
std::set< std::string > values
std::array< double, 2 > vertices[3]
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.
static Result failure(Status status)
Construct a failed result from a structured status.
The canonical owning dynamic value used by data-facing protocols.
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
std::map< std::string, Value > Object
std::vector< Value > Array
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
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).
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.
Contiguous triangle interval; nonzero ids name author edges/nodes, NetworkDerived uses id zero.