载入中...
搜索中...
未找到
DeformTrack.cpp
浏览该文件的文档.
1// eve-hot-path
2#include <algorithm>
3#include <cmath>
4#include <utility>
7namespace eve::physics {
8
9namespace {
10Diagnostic invalidDeform(const char* message) {
12}
13bool sameTopology(const DeformTopology& a, const DeformTopology& b) {
14 // Exact comparison avoids accepting a noncryptographic hash collision as topology equality.
15 return a.primitive() == b.primitive() && a.vertexCount() == b.vertexCount() &&
16 std::equal(a.indices().begin(), a.indices().end(), b.indices().begin(), b.indices().end()) &&
17 std::equal(a.uv().begin(), a.uv().end(), b.uv().begin(), b.uv().end(),
18 [](DeformUV x, DeformUV y) { return x.u == y.u && x.v == y.v; });
19}
20} // namespace
22 const BakeRecipe& recipe, std::uint32_t index, float previous) {
23 auto valid = recipe.validateSampleTime(index, frame.seconds);
24 if (!valid) return valid;
25 if (frame.seconds <= previous || frame.positions.size() != topology.vertexCount() ||
26 !sameTopology(topology, frame.topology))
27 return Result<void>::failure(invalidDeform("changed topology, count or nonincreasing time"));
28 for (const auto point : frame.positions)
29 if (!std::isfinite(point.x) || !std::isfinite(point.y) || !std::isfinite(point.z) ||
31 return Result<void>::failure(invalidDeform("nonfinite or out-of-plane deformation point"));
32 return Result<void>::success();
33}
34DeformTrack::DeformTrack(std::shared_ptr<const Data> data) : data_(std::move(data)) {}
35Result<DeformTrack> DeformTrack::prepare(std::vector<DeformFrame> frames, const BakeRecipe& recipe) {
36 auto encoded = recipe.encode();
37 if (!encoded) return Result<DeformTrack>::failure(encoded.status());
38 if (frames.empty() || frames.size() > 2000000)
39 return Result<DeformTrack>::failure(invalidDeform("empty or oversized deformation recording"));
40 const auto topology = frames.front().topology;
41 if (frames.size() > 2000000 / topology.vertexCount())
42 return Result<DeformTrack>::failure(invalidDeform("deformation sample budget exceeded"));
43 float previous = -1;
44 for (std::size_t i = 0; i < frames.size(); ++i) {
45 auto& frame = frames[i];
46 auto valid = bake_detail::validateDeformFrame(frame, topology, recipe, static_cast<std::uint32_t>(i), previous);
47 if (!valid) return Result<DeformTrack>::failure(valid.status());
48 previous = frame.seconds;
49 frame.topology = topology;
50 }
52 DeformTrack(std::make_shared<const Data>(Data{std::move(frames), std::make_shared<const BakeRecipe>(recipe)})));
53}
54Result<void> DeformTrack::sample(float seconds, std::span<DeformPoint> output) const {
55 const auto& frames = data_->frames;
56 if (!std::isfinite(seconds) || output.size() != frames.front().positions.size())
57 return Result<void>::failure(invalidDeform("invalid deformation sample time or output extent"));
58 auto upper = std::upper_bound(frames.begin(), frames.end(), seconds,
59 [](float time, const DeformFrame& frame) { return time < frame.seconds; });
60 const auto& a = upper == frames.begin() ? frames.front() : *(upper - 1);
61 const auto& b = upper == frames.end() ? frames.back() : *upper;
62 const double fraction = a.seconds == b.seconds
63 ? 0
64 : std::clamp((double(seconds) - a.seconds) / (double(b.seconds) - a.seconds), 0., 1.);
65 auto blend = [fraction](float x, float y) {
66 return static_cast<float>((1 - fraction) * double(x) + fraction * double(y));
67 };
68 for (std::size_t i = 0; i < output.size(); ++i) {
69 const auto x = a.positions[i], y = b.positions[i];
70 output[i] = {blend(x.x, y.x), blend(x.y, y.y), blend(x.z, y.z)};
71 }
72 return Result<void>::success();
73}
74DeformTopology DeformTrack::topology() const { return data_->frames.front().topology; }
75std::shared_ptr<const BakeRecipe> DeformTrack::recipe() const noexcept { return data_->recipe; }
76float DeformTrack::duration() const noexcept { return data_->frames.back().seconds; }
77std::size_t DeformTrack::frameCount() const noexcept { return data_->frames.size(); }
78} // namespace eve::physics
double y
double x
std::string output
std::string message
float blend
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
ssq::Object frame
std::shared_ptr< const OceanSprayFrame > previous
BakeRecipe recipe
Topology topology
uint32_t index
float fraction
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
Bounded provenance builder for explicit-coordinate baked recordings. @ownership Owns names and conten...
Definition BakeRecipe.h:21
BakeCoordinates coordinates() const noexcept
Current coordinate contract, defaulting to the original metre convention.
Result< void > validateSampleTime(std::uint32_t frame, float seconds) const
Validate a sample against the declared fixed-step/stride grid.
Immutable connectivity and UV ownership for fixed-topology deformation playback. @ownership Copies sh...
std::size_t vertexCount() const noexcept
Number of stable vertex slots.
Immutable fixed-topology deformation clip, independent of simulation and rendering providers....
Definition DeformTrack.h:25
std::size_t frameCount() const noexcept
Number of immutable frames.
std::shared_ptr< const BakeRecipe > recipe() const noexcept
Return shared immutable provenance including explicit coordinates.
DeformTopology topology() const
Return a cheap owning copy of the fixed connectivity and UVs.
float duration() const noexcept
Last sample time in seconds.
static Result< DeformTrack > prepare(std::vector< DeformFrame > frames, const BakeRecipe &recipe)
Validate matching topology, positions, coordinates and fixed sample grid, then freeze all frames.
Result< void > sample(float seconds, std::span< DeformPoint > output) const
Interpolate all vertices into an exact-sized caller buffer; no allocation or solver work....
eve::Diagnostic Diagnostic
Result< void > validateDeformFrame(const DeformFrame &frame, const DeformTopology &topology, const BakeRecipe &recipe, std::uint32_t index, float previous)
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
Owning snapshot with actual connectivity, stable indexed positions and explicit sample time.
Definition DeformTrack.h:15