载入中...
搜索中...
未找到
GeneratedArtifactMeshCodec.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <limits>
5#include <type_traits>
7namespace {
8template <class T>
9eve::Value encodeNumbers(const std::vector<T>& values) {
11 out.reserve(values.size());
12 for (auto value : values) {
13 if constexpr (std::is_integral_v<T>)
14 out.emplace_back(static_cast<std::int64_t>(value));
15 else
16 out.emplace_back(value);
17 }
18 return eve::Value(std::move(out));
19}
20eve::Value encodeStringMap(const std::unordered_map<std::string, std::string>& values) {
22 for (const auto& [key, value] : values) out.emplace(key, eve::Value(value));
23 return eve::Value(std::move(out));
24}
25} // namespace
26bool validMesh(const MeshBuild& mesh) noexcept {
27 if (mesh.empty() || mesh.positions().size() % 3u != 0u || mesh.indices().size() % 3u != 0u) return false;
28 const std::size_t vertices = mesh.positions().size() / 3u;
29 if ((!mesh.normals().empty() && mesh.normals().size() != mesh.positions().size()) ||
30 (!mesh.uvs().empty() && mesh.uvs().size() != vertices * 2u) ||
31 (!mesh.colors().empty() && mesh.colors().size() != vertices * 4u))
32 return false;
33 return std::all_of(mesh.positions().begin(), mesh.positions().end(),
34 [](float value) { return std::isfinite(value); }) &&
35 std::all_of(mesh.normals().begin(), mesh.normals().end(),
36 [](float value) { return std::isfinite(value); }) &&
37 std::all_of(mesh.uvs().begin(), mesh.uvs().end(), [](float value) { return std::isfinite(value); }) &&
38 std::all_of(mesh.colors().begin(), mesh.colors().end(), [](float value) { return std::isfinite(value); }) &&
39 std::all_of(mesh.indices().begin(), mesh.indices().end(),
40 [vertices](std::uint32_t index) { return index < vertices; });
41}
44 result.emplace("positions", encodeNumbers(mesh.positions()));
45 result.emplace("normals", encodeNumbers(mesh.normals()));
46 result.emplace("uvs", encodeNumbers(mesh.uvs()));
47 result.emplace("colors", encodeNumbers(mesh.colors()));
48 result.emplace("indices", encodeNumbers(mesh.indices()));
49 result.emplace("groupNames", [&mesh] {
51 names.reserve(mesh.groupNames().size());
52 for (const auto& name : mesh.groupNames()) names.emplace_back(name);
53 return eve::Value(std::move(names));
54 }());
55 result.emplace("triangleGroups", encodeNumbers(mesh.triangleGroups()));
56 result.emplace("activeGroup", eve::Value(mesh.activeGroup()));
57 result.emplace("metadata", encodeStringMap(mesh.metadata()));
58 return eve::Value(std::move(result));
59}
61 std::vector<float> colors;
62 auto it = object.find("colors");
63 // Dense snapshot v2 predates colors. Missing means the original empty stream.
64 if (it == object.end()) return Result<std::vector<float>>::success(std::move(colors));
65 auto fail = [] {
66 return Result<std::vector<float>>::failure(
67 Diagnostic::error(DiagnosticCode::InvalidArgument, "invalid artifact mesh colors", "colors"));
68 };
69 const auto* array = it->second.getIf<eve::Value::Array>();
70 if (!array || (!array->empty() && array->size() != vertices * 4u)) return fail();
71 colors.reserve(array->size());
72 for (const auto& item : *array) {
73 double value;
74 if (const auto* number = item.getIf<double>())
75 value = *number;
76 else if (const auto* integer = item.getIf<std::int64_t>())
77 value = static_cast<double>(*integer);
78 else
79 return fail();
80 if (!std::isfinite(value) || std::abs(value) > std::numeric_limits<float>::max()) return fail();
81 colors.push_back(static_cast<float>(value));
82 }
83 return Result<std::vector<float>>::success(std::move(colors));
84}
85} // namespace eve::procgen::artifact_mesh
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
float u
Definition Grass.cpp:233
Poco::JSON::Object::Ptr result
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
OceanMeshSettings mesh
std::set< std::string > values
Object::Ptr value
double number
ScriptStructure out
size_t end
std::array< double, 2 > vertices[3]
uint32_t index
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
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
Result< std::vector< float > > decodeColors(const eve::Value::Object &object, std::size_t vertices)
eve::Value encodeMesh(const MeshBuild &mesh)
bool validMesh(const MeshBuild &mesh) noexcept