载入中...
搜索中...
未找到
Box3DShapeCook.cpp
浏览该文件的文档.
2#include <cmath>
3#include "common/Exception.h"
4namespace eve::physics::box3d {
5b3HullData* createCheckedHull(const std::vector<float>& vertices, int maxVertices, const char* operation) {
6 if (vertices.size() < 12 || vertices.size() % 3 != 0)
7 throw eve::Exception("%s: vertices must contain at least four packed XYZ points", operation);
8 if (vertices.size() / 3 > 100000) throw eve::Exception("%s: source point count must be <= 100000", operation);
9 if (maxVertices < 4 || maxVertices > 254) throw eve::Exception("%s: maxVertices must be in [4, 254]", operation);
10 std::vector<b3Vec3> points;
11 points.reserve(vertices.size() / 3);
12 for (size_t i = 0; i < vertices.size(); i += 3) {
13 if (!std::isfinite(vertices[i]) || !std::isfinite(vertices[i + 1]) || !std::isfinite(vertices[i + 2]))
14 throw eve::Exception("%s: all vertex components must be finite", operation);
15 points.push_back({vertices[i], vertices[i + 1], vertices[i + 2]});
16 }
17 b3HullData* hull = b3CreateHull(points.data(), static_cast<int>(points.size()), maxVertices);
18 if (!hull) throw eve::Exception("%s: points do not form a valid three-dimensional convex hull", operation);
19 return hull;
20}
21
22b3MeshData* createCheckedMesh(const std::vector<float>& vertices, const std::vector<int32_t>& indices,
23 bool weldVertices, float weldTolerance, bool identifyEdges, bool useMedianSplit,
24 b3Transform transform, const std::vector<uint8_t>* materialIndices,
25 const char* operation) {
26 if (vertices.size() < 9 || vertices.size() % 3 != 0)
27 throw eve::Exception("%s: vertices must contain at least three packed XYZ points", operation);
28 if (indices.size() < 3 || indices.size() % 3 != 0)
29 throw eve::Exception("%s: indices must contain complete triangles", operation);
30 if (vertices.size() / 3 > 1000000 || indices.size() / 3 > 2000000)
31 throw eve::Exception("%s: mesh exceeds 1000000 vertices or 2000000 triangles", operation);
32 if (!std::isfinite(weldTolerance) || weldTolerance < 0.f)
33 throw eve::Exception("%s: weldTolerance must be finite and >= 0", operation);
34 std::vector<b3Vec3> points;
35 points.reserve(vertices.size() / 3);
36 for (size_t i = 0; i < vertices.size(); i += 3) {
37 if (!std::isfinite(vertices[i]) || !std::isfinite(vertices[i + 1]) || !std::isfinite(vertices[i + 2]))
38 throw eve::Exception("%s: all vertex components must be finite", operation);
39 const b3Vec3 point{vertices[i], vertices[i + 1], vertices[i + 2]};
40 points.push_back(transform.p + b3RotateVector(transform.q, point));
41 }
42 for (int32_t index : indices) {
43 if (index < 0 || static_cast<size_t>(index) >= points.size())
44 throw eve::Exception("%s: triangle index is outside the vertex array", operation);
45 }
46 std::vector<int32_t> mutableIndices = indices;
47 b3MeshDef def{};
48 def.vertices = points.data();
49 def.indices = mutableIndices.data();
50 def.materialIndices =
51 materialIndices && !materialIndices->empty() ? const_cast<uint8_t*>(materialIndices->data()) : nullptr;
52 def.vertexCount = static_cast<int>(points.size());
53 def.triangleCount = static_cast<int>(indices.size() / 3);
54 def.weldVertices = weldVertices;
55 def.weldTolerance = weldTolerance;
56 def.identifyEdges = identifyEdges;
57 def.useMedianSplit = useMedianSplit;
58 b3MeshData* mesh = b3CreateMesh(&def, nullptr, 0);
59 if (!mesh || mesh->triangleCount != def.triangleCount) {
60 if (mesh) b3DestroyMesh(mesh);
61 throw eve::Exception("%s: mesh contains degenerate or zero-area triangles", operation);
62 }
63 return mesh;
64}
65
66b3HeightFieldData* createCheckedHeightField(int countX, int countZ, float cellSizeX, float cellSizeZ,
67 const std::vector<float>& heights, float globalMin, float globalMax,
68 bool clockwiseWinding, const char* operation) {
69 if (countX < 2 || countZ < 2) throw eve::Exception("%s: countX and countZ must be >= 2", operation);
70 const size_t sampleCount = static_cast<size_t>(countX) * static_cast<size_t>(countZ);
71 if (sampleCount > 16000000) throw eve::Exception("%s: sample count must be <= 16000000", operation);
72 if (heights.size() != sampleCount) throw eve::Exception("%s: heights size must equal countX* countZ", operation);
73 if (!(cellSizeX > 0.f) || !(cellSizeZ > 0.f) || !std::isfinite(cellSizeX) || !std::isfinite(cellSizeZ))
74 throw eve::Exception("%s: cell sizes must be finite and > 0", operation);
75 if (!std::isfinite(globalMin) || !std::isfinite(globalMax) || globalMin > globalMax)
76 throw eve::Exception("%s: global height range must be finite and ordered", operation);
77 for (float height : heights) {
78 if (!std::isfinite(height) || height < globalMin || height > globalMax)
79 throw eve::Exception("%s: every height must be finite and inside global range", operation);
80 }
81 std::vector<float> mutableHeights = heights;
82 b3HeightFieldDef def{};
83 def.heights = mutableHeights.data();
84 def.scale = {cellSizeX, 1.f, cellSizeZ};
85 def.countX = countX;
86 def.countZ = countZ;
87 def.globalMinimumHeight = globalMin;
88 def.globalMaximumHeight = globalMax;
89 def.clockwiseWinding = clockwiseWinding;
90 return b3CreateHeightField(&def);
91}
92
93
94} // namespace eve::physics::box3d
ActionParameterOperation operation
std::vector< std::uint32_t > indices
std::uint32_t height
OceanMeshSettings mesh
std::shared_ptr< const std::vector< glm::vec2 > > points
std::array< double, 2 > vertices[3]
uint32_t index
glm::vec3 point
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
b3MeshData * createCheckedMesh(const std::vector< float > &vertices, const std::vector< int32_t > &indices, bool weldVertices, float weldTolerance, bool identifyEdges, bool useMedianSplit, b3Transform transform, const std::vector< uint8_t > *materialIndices, const char *operation)
Validates and cooks native geometry; throws on invalid input. @ownership Transfers native allocation ...
b3HeightFieldData * createCheckedHeightField(int countX, int countZ, float cellSizeX, float cellSizeZ, const std::vector< float > &heights, float globalMin, float globalMax, bool clockwiseWinding, const char *operation)
Validates and cooks native geometry; throws on invalid input. @ownership Transfers native allocation ...
b3HullData * createCheckedHull(const std::vector< float > &vertices, int maxVertices, const char *operation)
Validates and cooks native geometry; throws on invalid input. @ownership Transfers native allocation ...