载入中...
搜索中...
未找到
FractureGeometry.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <array>
4#include <cmath>
5#include <map>
6#include <utility>
7namespace eve::physics {
9 auto fail = [] {
11 "invalid closed convex fracture mesh or budget", "geometry"));
12 };
13 if (fragments.empty() || fragments.size() > 4096) return fail();
14 std::size_t total = 0;
15 for (const auto& f : fragments) {
16 const auto count = f.positions.size() / 3;
17 if (f.positions.size() % 3 || count < 4 || count > 254 || f.indices.empty() || f.indices.size() % 3 ||
18 f.indices.size() > 8192 || f.materials.size() != f.indices.size() / 3)
19 return fail();
20 total += f.positions.size() + f.indices.size() + f.materials.size();
21 if (total > 4000000) return fail();
22 double scale = 0;
23 for (float v : f.positions) {
24 if (!std::isfinite(v)) return fail();
25 scale = std::max(scale, std::abs(static_cast<double>(v)));
26 }
27 const double epsilon = std::max(1e-9, scale * 1e-6);
28 auto point = [&](std::uint32_t i) {
29 return std::array<double, 3>{f.positions[3 * i], f.positions[3 * i + 1], f.positions[3 * i + 2]};
30 };
31 std::map<std::pair<std::uint32_t, std::uint32_t>, std::pair<int, int>> edges;
32 double volume = 0;
33 for (std::size_t t = 0; t < f.indices.size(); t += 3) {
34 const auto ia = f.indices[t], ib = f.indices[t + 1], ic = f.indices[t + 2];
35 if (ia >= count || ib >= count || ic >= count || ia == ib || ib == ic || ia == ic || f.materials[t / 3] > 1)
36 return fail();
37 auto a = point(ia), b = point(ib), c = point(ic);
38 const std::array<double, 3> ab{b[0] - a[0], b[1] - a[1], b[2] - a[2]},
39 ac{c[0] - a[0], c[1] - a[1], c[2] - a[2]};
40 const std::array<double, 3> n{ab[1] * ac[2] - ab[2] * ac[1], ab[2] * ac[0] - ab[0] * ac[2],
41 ab[0] * ac[1] - ab[1] * ac[0]};
42 const double length = std::sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);
43 if (length < epsilon * epsilon)
45 Diagnostic::error(DiagnosticCode::InvalidArgument, "degenerate fracture triangle", "geometry"));
46 for (std::uint32_t i = 0; i < count; ++i) {
47 auto q = point(i);
48 if (n[0] * (q[0] - a[0]) + n[1] * (q[1] - a[1]) + n[2] * (q[2] - a[2]) > epsilon * length * 4)
50 Diagnostic::error(DiagnosticCode::InvalidArgument, "fracture mesh is not convex", "geometry"));
51 }
52 volume += (a[0] * (b[1] * c[2] - b[2] * c[1]) + a[1] * (b[2] * c[0] - b[0] * c[2]) +
53 a[2] * (b[0] * c[1] - b[1] * c[0])) /
54 6;
55 const std::uint32_t ids[] = {ia, ib, ic, ia};
56 for (int k = 0; k < 3; ++k) {
57 const auto x = ids[k], y = ids[k + 1];
58 auto& edge = edges[std::minmax(x, y)];
59 ++edge.first;
60 edge.second += x < y ? 1 : -1;
61 }
62 }
63 if (!std::isfinite(volume) || volume <= epsilon * epsilon * epsilon) return fail();
64 for (const auto& [key, value] : edges) {
65 (void)key;
66 if (value.first != 2 || value.second != 0)
69 "fracture mesh has open or inconsistently oriented edges", "geometry"));
70 }
71 }
72 return Result<void>::success();
73}
74} // namespace eve::physics
double y
double x
double volume
SQObjectPtr key
Definition ECS.cpp:155
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
std::uint32_t ab
std::uint32_t ac
float v
std::int32_t c
float length
std::array< float, 3 > scale
std::vector< Edge > edges
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float f
Object::Ptr value
float t
int count
glm::vec3 point
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 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
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Result< void > validate() const
Validate budgets, closed outward convex meshes, indices, finite coordinates and tags....
std::vector< FractureFragment > fragments