载入中...
搜索中...
未找到
RigidPoseTrack.cpp
浏览该文件的文档.
1// eve-hot-path
2#include <algorithm>
3#include <cmath>
4#include <utility>
7
8namespace eve::physics {
9namespace {
10Diagnostic invalidTrack(const char* message) {
12}
13} // namespace
15 std::span<const RigidTrackSample> roster, float previous) {
17 return Result<void>::failure(invalidTrack("unknown coordinate space"));
18 if (!std::isfinite(frame.seconds) || frame.seconds < 0 || frame.seconds <= previous ||
19 (roster.empty() && frame.seconds != 0) || frame.samples.empty() || frame.samples.size() > 16384 ||
20 (!roster.empty() && frame.samples.size() != roster.size()))
21 return Result<void>::failure(invalidTrack("invalid timestamp or changed roster size"));
22 std::sort(frame.samples.begin(), frame.samples.end(), [](const auto& a, const auto& b) { return a.id < b.id; });
23 for (std::size_t i = 0; i < frame.samples.size(); ++i) {
24 auto& sample = frame.samples[i];
25 if (sample.id.isNil() || (i && sample.id == frame.samples[i - 1].id) ||
26 (!roster.empty() && sample.id != roster[i].id))
27 return Result<void>::failure(invalidTrack("nil, duplicate or changed stable identity"));
28 auto& p = sample.pose;
29 for (float v : {p.x, p.y, p.z, p.qx, p.qy, p.qz, p.qw})
30 if (!std::isfinite(v)) return Result<void>::failure(invalidTrack("nonfinite pose"));
31 if (space == RigidTrackSpace::PlanarPixels && (p.z != 0 || p.qx != 0 || p.qy != 0))
32 return Result<void>::failure(invalidTrack("out-of-plane planar pose"));
33 const double x = p.qx, y = p.qy, z = p.qz, w = p.qw;
34 const double norm = std::sqrt(x * x + y * y + z * z + w * w);
35 if (norm == 0) return Result<void>::failure(invalidTrack("zero rotation quaternion"));
36 p.qx = static_cast<float>(x / norm);
37 p.qy = static_cast<float>(y / norm);
38 p.qz = static_cast<float>(z / norm);
39 p.qw = static_cast<float>(w / norm);
40 }
41 return Result<void>::success();
42}
43RigidPoseTrack::RigidPoseTrack(std::shared_ptr<const Data> data) : data_(std::move(data)) {}
44Result<RigidPoseTrack> RigidPoseTrack::prepare(RigidTrackSpace space, std::vector<RigidTrackFrame> frames) {
45 return prepareValidated(space, std::move(frames), {});
46}
47Result<RigidPoseTrack> RigidPoseTrack::prepare(RigidTrackSpace space, std::vector<RigidTrackFrame> frames,
48 const BakeRecipe& recipe) {
49 const auto coordinates =
51 if (recipe.coordinates() != coordinates)
52 return Result<RigidPoseTrack>::failure(invalidTrack("recipe coordinate mismatch"));
53 auto encoded = recipe.encode();
54 if (!encoded) return Result<RigidPoseTrack>::failure(encoded.status());
55 if (frames.size() > 2000000) return Result<RigidPoseTrack>::failure(invalidTrack("frame budget exceeded"));
56 for (std::size_t i = 0; i < frames.size(); ++i) {
57 auto valid = recipe.validateSampleTime(static_cast<std::uint32_t>(i), frames[i].seconds);
58 if (!valid) return Result<RigidPoseTrack>::failure(valid.status());
59 }
60 return prepareValidated(space, std::move(frames), std::make_shared<const BakeRecipe>(recipe));
61}
62std::shared_ptr<const BakeRecipe> RigidPoseTrack::recipe() const noexcept { return data_->recipe; }
63Result<RigidPoseTrack> RigidPoseTrack::prepareValidated(RigidTrackSpace space, std::vector<RigidTrackFrame> frames,
64 std::shared_ptr<const BakeRecipe> recipe) {
65 constexpr std::size_t maxSamples = 2000000, maxTracks = 16384;
67 return Result<RigidPoseTrack>::failure(invalidTrack("unknown coordinate space"));
68 if (frames.empty() || frames.size() > maxSamples || frames.front().samples.empty() ||
69 frames.front().samples.size() > maxTracks)
70 return Result<RigidPoseTrack>::failure(invalidTrack("empty or oversized recording"));
71 const auto tracks = frames.front().samples.size();
72 if (frames.size() > maxSamples / tracks)
73 return Result<RigidPoseTrack>::failure(invalidTrack("sample budget exceeded"));
74 float previous = -1;
75 for (auto& frame : frames) {
76 const std::span<const RigidTrackSample> roster =
77 &frame == &frames.front() ? std::span<const RigidTrackSample>{} : frames.front().samples;
79 if (!valid) return Result<RigidPoseTrack>::failure(valid.status());
80 previous = frame.seconds;
81 }
83 RigidPoseTrack(std::make_shared<const Data>(Data{space, std::move(recipe), std::move(frames)})));
84}
86 if (!std::isfinite(seconds)) return Result<RigidTrackPose>::failure(invalidTrack("nonfinite sample time"));
87 const auto& roster = data_->frames.front().samples;
88 const auto found = std::lower_bound(roster.begin(), roster.end(), id,
89 [](const auto& sample, PersistentId wanted) { return sample.id < wanted; });
90 if (found == roster.end() || found->id != id)
92 Diagnostic::error(DiagnosticCode::NotFound, "unknown track identity", "rigidPoseTrack"));
93 const auto index = static_cast<std::size_t>(found - roster.begin());
94 auto upper = std::upper_bound(data_->frames.begin(), data_->frames.end(), seconds,
95 [](float t, const auto& frame) { return t < frame.seconds; });
96 if (upper == data_->frames.begin()) return Result<RigidTrackPose>::success(upper->samples[index].pose);
97 const auto& a = *(upper - 1);
98 if (upper == data_->frames.end()) return Result<RigidTrackPose>::success(a.samples[index].pose);
99 auto pose = a.samples[index].pose;
100 const auto& b = upper->samples[index].pose;
101 const float t = (seconds - a.seconds) / (upper->seconds - a.seconds);
102 pose.x = std::lerp(pose.x, b.x, t);
103 pose.y = std::lerp(pose.y, b.y, t);
104 pose.z = std::lerp(pose.z, b.z, t);
105 const float sign = pose.qx * b.qx + pose.qy * b.qy + pose.qz * b.qz + pose.qw * b.qw < 0 ? -1.f : 1.f;
106 pose.qx = std::lerp(pose.qx, sign * b.qx, t);
107 pose.qy = std::lerp(pose.qy, sign * b.qy, t);
108 pose.qz = std::lerp(pose.qz, sign * b.qz, t);
109 pose.qw = std::lerp(pose.qw, sign * b.qw, t);
110 const float norm = std::sqrt(pose.qx * pose.qx + pose.qy * pose.qy + pose.qz * pose.qz + pose.qw * pose.qw);
111 pose.qx /= norm;
112 pose.qy /= norm;
113 pose.qz /= norm;
114 pose.qw /= norm;
116}
117RigidTrackSpace RigidPoseTrack::space() const noexcept { return data_->space; }
118float RigidPoseTrack::duration() const noexcept { return data_->frames.back().seconds; }
119std::size_t RigidPoseTrack::trackCount() const noexcept { return data_->frames.front().samples.size(); }
120std::size_t RigidPoseTrack::frameCount() const noexcept { return data_->frames.size(); }
122 if (index >= trackCount()) return Result<PersistentId>::failure(invalidTrack("track index outside roster"));
123 return Result<PersistentId>::success(data_->frames.front().samples[index].id);
124}
125} // namespace eve::physics
double z
double y
double x
float w
Definition AnimClip.cpp:738
eve::EntitySpatialPose pose
glm::vec4 p[6]
std::string message
float v
std::vector< Track > tracks
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
float t
bool found
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
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
Id128 public API.
Definition Identity.h:111
Bounded provenance builder for explicit-coordinate baked recordings. @ownership Owns names and conten...
Definition BakeRecipe.h:21
std::size_t frameCount() const noexcept
Number of captured frames.
Result< RigidTrackPose > sample(float seconds, PersistentId id) const
Sample a known stable identity at finite seconds, clamping outside the recorded interval.
RigidTrackSpace space() const noexcept
Coordinate contract of every pose in this track.
static Result< RigidPoseTrack > prepare(RigidTrackSpace space, std::vector< RigidTrackFrame > frames)
Validate, normalize and freeze a bounded recording with stable sorted track identities....
Result< PersistentId > trackId(std::size_t index) const
Stable identity at canonical sorted index; returns failure for an out-of-range index.
std::shared_ptr< const BakeRecipe > recipe() const noexcept
Return shared immutable provenance; empty for explicit assets without verified origins.
std::size_t trackCount() const noexcept
Number of identities in the frozen roster.
float duration() const noexcept
Last recorded timestamp.
eve::Diagnostic Diagnostic
Result< void > validateRigidFrame(RigidTrackSpace space, RigidTrackFrame &frame, std::span< const RigidTrackSample > roster, float previous)
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
RigidTrackSpace
Explicit sample coordinates; planar pixels preserve the producer's XY axes without implicit Y flip.
Owning frame at an explicit finite simulation time, starting at zero.