载入中...
搜索中...
未找到
AnimCurveLibrary.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <bit>
5#include <cmath>
6#include <cstdint>
7#include <map>
8#include <stdexcept>
9#include <string>
10#include <utility>
11#include <vector>
13
14namespace eve::animation {
15namespace {
16struct Key {
17 float time, value;
18};
19struct Clip {
20 float duration;
21 std::map<std::string, std::vector<Key>, std::less<>> channels;
22};
23class Reader {
24public:
25 explicit Reader(std::span<const std::byte> data) : bytes(data) {}
26 std::uint32_t integer() {
27 require(4);
28 std::uint32_t result = 0;
29 for (unsigned i = 0; i < 4; ++i) result |= std::to_integer<std::uint32_t>(bytes[offset++]) << (8 * i);
30 return result;
31 }
32 float number() {
33 float value = std::bit_cast<float>(integer());
34 if (!std::isfinite(value)) throw std::runtime_error("nonfinite animation curve value");
35 return value;
36 }
37 std::string string() {
38 auto count = integer();
39 if (count == 0 || count > 4096) throw std::runtime_error("invalid animation curve name length");
40 require(count);
41 std::string value(reinterpret_cast<const char*>(bytes.data() + offset), count);
42 offset += count;
44 throw std::runtime_error("invalid animation curve UTF-8");
45 return value;
46 }
47 void require(std::size_t count) const {
48 if (count > bytes.size() - offset) throw std::runtime_error("truncated animation curves");
49 }
50 bool finished() const { return offset == bytes.size(); }
51
52private:
53 std::span<const std::byte> bytes;
54 std::size_t offset = 0;
55};
56eve::Diagnostic invalid(std::string message) {
57 return eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, std::move(message), "animationCurves", {},
58 "animation");
59}
60} // namespace
62 std::map<std::string, Clip, std::less<>> clips;
63};
64AnimCurveLibrary::AnimCurveLibrary() : impl_(std::make_unique<Impl>()) {}
66eve::Result<void> AnimCurveLibrary::load(std::span<const std::byte> bytes) {
67 try {
68 if (bytes.size() > 64 * 1024 * 1024) throw std::runtime_error("animation curves exceed 64 MiB");
69 Reader reader(bytes);
70 if (reader.integer() != 0x43465645 || reader.integer() != 1)
71 throw std::runtime_error("unsupported animation curves format");
72 auto count = reader.integer();
73 if (count > 100000) throw std::runtime_error("too many animation curve sources");
74 Impl next;
75 for (std::uint32_t i = 0; i < count; ++i) {
76 auto source = reader.string();
77 Clip clip;
78 clip.duration = reader.number();
79 auto channelCount = reader.integer();
80 if (channelCount > 64 || clip.duration < 0 || (channelCount && clip.duration <= 0))
81 throw std::runtime_error("invalid animation curve duration or channel count");
82 for (std::uint32_t j = 0; j < channelCount; ++j) {
83 auto name = reader.string();
84 auto keyCount = reader.integer();
85 if (keyCount < 2 || keyCount > 1000000) throw std::runtime_error("invalid animation curve key count");
86 reader.require(std::size_t(keyCount) * 8);
87 std::vector<Key> keys;
88 keys.reserve(keyCount);
89 for (std::uint32_t k = 0; k < keyCount; ++k) {
90 Key key{reader.number(), reader.number()};
91 if (key.time < 0 || key.time > clip.duration || (!keys.empty() && key.time <= keys.back().time))
92 throw std::runtime_error("unordered animation curve time");
93 keys.push_back(key);
94 }
95 if (keys.front().time != 0 || keys.back().time != clip.duration)
96 throw std::runtime_error("animation curve endpoints do not span duration");
97 if (!clip.channels.emplace(name, std::move(keys)).second)
98 throw std::runtime_error("duplicate animation curve channel");
99 }
100 if (!next.clips.emplace(source, std::move(clip)).second)
101 throw std::runtime_error("duplicate animation curve source");
102 }
103 if (!reader.finished()) throw std::runtime_error("trailing animation curve bytes");
104 impl_->clips.swap(next.clips);
106 } catch (const std::exception& error) {
107 return eve::Result<void>::failure(invalid(error.what()));
108 }
109}
110bool AnimCurveLibrary::contains(std::string_view source) const { return impl_->clips.contains(source); }
111std::size_t AnimCurveLibrary::size() const noexcept { return impl_->clips.size(); }
112eve::Result<std::optional<float>> AnimCurveLibrary::sample(std::string_view source, std::string_view channel,
113 double seconds, bool loop) const {
115 if (!std::isfinite(seconds)) return Result::failure(invalid("nonfinite animation curve time"));
116 auto found = impl_->clips.find(source);
117 if (found == impl_->clips.end())
118 return Result::failure(eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "animation curve source not found",
119 "source", {}, "animation"));
120 const auto& clip = found->second;
121 auto curve = clip.channels.find(channel);
122 if (curve == clip.channels.end()) return Result::success(std::optional<float>{});
123 if (loop) {
124 seconds = std::fmod(seconds, double(clip.duration));
125 if (seconds < 0) seconds += clip.duration;
126 }
127 const auto& keys = curve->second;
128 if (seconds <= 0) return Result::success(keys.front().value);
129 if (seconds >= clip.duration) return Result::success(keys.back().value);
130 auto end = std::upper_bound(keys.begin(), keys.end(), seconds,
131 [](double time, const Key& key) { return time < key.time; });
132 const auto& a = *(end - 1);
133 const auto& b = *end;
134 double alpha = (seconds - a.time) / (double(b.time) - a.time);
135 return Result::success(float(double(a.value) * (1 - alpha) + double(b.value) * alpha));
136}
137} // namespace eve::animation
float duration
std::map< std::string, std::vector< Key >, std::less<> > channels
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::string message
glm::vec4 clip
size_t offset
Poco::JSON::Object::Ptr result
std::uint64_t bytes
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string error
Definition Package.cpp:174
Object::Ptr value
double number
bool found
std::string_view source
size_t end
std::string string
int count
Heightmap curve
Strict allocation-free UTF-8 validation.
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
AnimCurveLibrary()
Construct an empty library with independent lifetime.
eve::Result< std::optional< float > > sample(std::string_view source, std::string_view channel, double seconds, bool loop) const
Evaluate one scalar channel with linear interpolation.
std::size_t size() const noexcept
Return the number of source records currently owned.
bool contains(std::string_view source) const
Query whether a source record exists, including records without channels.
eve::Result< void > load(std::span< const std::byte > bytes)
Atomically replace data from little-endian eve.animation-curves/1 bytes.
~AnimCurveLibrary()
Destroy owned curve data without accessing external objects.
Status invalid(const char *message)
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
bool isValidUtf8(std::string_view text, Utf8NullPolicy nullPolicy) noexcept
Validate shortest-form UTF-8, Unicode scalar range and optional NUL exclusion.
std::map< std::string, Clip, std::less<> > clips