载入中...
搜索中...
未找到
ActionGuidePack.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
4
5#include <cmath>
6#include <set>
7#include <string>
8#include <utility>
9
10namespace eve {
11namespace {
12
13template <typename T>
14Result<T> invalid(std::string message, std::string path) {
16}
17
18Result<void> invalidVoid(std::string message, std::string path) {
21}
22
23const Value* field(const Value& value, std::string_view name) {
24 if (!value.isObject()) return nullptr;
25 return value.find(std::string(name));
26}
27
28Result<void> rejectUnknown(const Value& value, const std::set<std::string, std::less<>>& known, std::string path) {
29 if (!value.isObject()) return invalidVoid("expected object", std::move(path));
30 for (const auto& [name, item] : *value.getIf<Value::Object>()) {
31 (void)item;
32 if (!known.contains(name)) return invalidVoid("unknown field", path + "." + name);
33 }
34 return Result<void>::success();
35}
36
37Result<std::string> text(const Value& value, std::string_view name, std::string path) {
38 const auto* item = field(value, name);
39 if (!item || !item->isString()) return invalid<std::string>("expected text field", std::move(path));
40 return Result<std::string>::success(item->asString());
41}
42
43Result<std::int64_t> integer(const Value& value, std::string_view name, std::string path) {
44 const auto* item = field(value, name);
45 if (!item || !item->isInt64()) return invalid<std::int64_t>("expected integer field", std::move(path));
46 return Result<std::int64_t>::success(item->asInt());
47}
48
49Result<bool> boolean(const Value& value, std::string_view name, std::string path) {
50 const auto* item = field(value, name);
51 if (!item || !item->isBool()) return invalid<bool>("expected boolean field", std::move(path));
52 return Result<bool>::success(item->asBool());
53}
54
55Result<double> number(const Value& value, std::string_view name, std::string path) {
56 const auto* item = field(value, name);
57 if (!item || (!item->isDouble() && !item->isInt64()))
58 return invalid<double>("expected numeric field", std::move(path));
59 const double result = item->isDouble() ? item->asDouble() : static_cast<double>(item->asInt());
60 if (!std::isfinite(result)) return invalid<double>("numeric field must be finite", std::move(path));
62}
63
64Result<std::array<double, 3>> vec3(const Value& value, std::string_view name, std::string path) {
65 const auto* item = field(value, name);
66 if (!item || !item->isArray() || item->getIf<Value::Array>()->size() != 3)
67 return invalid<std::array<double, 3>>("expected three-component array", std::move(path));
68 std::array<double, 3> result{};
69 for (std::size_t i = 0; i < 3; ++i) {
70 const auto& component = item->getIf<Value::Array>()->at(i);
71 if (!component.isDouble() && !component.isInt64())
72 return invalid<std::array<double, 3>>("vector component must be numeric", path);
73 result[i] = component.isDouble() ? component.asDouble() : static_cast<double>(component.asInt());
74 if (!std::isfinite(result[i])) return invalid<std::array<double, 3>>("vector component must be finite", path);
75 }
76 return Result<std::array<double, 3>>::success(result);
77}
78
79Result<std::array<double, 4>> vec4(const Value& value, std::string_view name, std::string path) {
80 const auto* item = field(value, name);
81 if (!item || !item->isArray() || item->getIf<Value::Array>()->size() != 4)
82 return invalid<std::array<double, 4>>("expected four-component array", std::move(path));
83 std::array<double, 4> result{};
84 for (std::size_t i = 0; i < 4; ++i) {
85 const auto& component = item->getIf<Value::Array>()->at(i);
86 if (!component.isDouble() && !component.isInt64())
87 return invalid<std::array<double, 4>>("vector component must be numeric", path);
88 result[i] = component.isDouble() ? component.asDouble() : static_cast<double>(component.asInt());
89 if (!std::isfinite(result[i])) return invalid<std::array<double, 4>>("vector component must be finite", path);
90 }
91 return Result<std::array<double, 4>>::success(result);
92}
93
94Result<std::vector<std::string>> stringArray(const Value& parent, std::string_view name, std::string path) {
95 const auto* source = field(parent, name);
96 if (!source || !source->isArray()) return invalid<std::vector<std::string>>("expected text array", path);
97 std::vector<std::string> result;
98 result.reserve(source->getIf<Value::Array>()->size());
99 for (std::size_t i = 0; i < source->getIf<Value::Array>()->size(); ++i) {
100 const auto& item = source->getIf<Value::Array>()->at(i);
101 if (!item.isString())
102 return invalid<std::vector<std::string>>("expected text item", path + "[" + std::to_string(i) + "]");
103 result.push_back(item.asString());
104 }
105 return Result<std::vector<std::string>>::success(std::move(result));
106}
107
108Value::Array strings(const std::vector<std::string>& values) {
110 result.reserve(values.size());
111 for (const auto& value : values) result.emplace_back(value);
112 return result;
113}
114
115Value::Array array3(const std::array<double, 3>& value) { return Value::Array{value[0], value[1], value[2]}; }
116
117Value::Array array4(const std::array<double, 4>& value) { return Value::Array{value[0], value[1], value[2], value[3]}; }
118
119std::string_view rootModeName(ActionGuideRootMode mode) {
120 switch (mode) {
121 case ActionGuideRootMode::InPlace: return "inPlace";
122 case ActionGuideRootMode::WorldRoot: return "worldRoot";
123 }
124 return "inPlace";
125}
126
127Result<ActionGuideRootMode> parseRootMode(std::string_view text) {
130 return invalid<ActionGuideRootMode>("unknown rootMode", "rootMode");
131}
132
133std::string_view projectionName(ActionGuideProjection projection) {
134 switch (projection) {
135 case ActionGuideProjection::Orthographic: return "orthographic";
136 case ActionGuideProjection::Perspective: return "perspective";
137 }
138 return "orthographic";
139}
140
141Result<ActionGuideProjection> parseProjection(std::string_view text) {
144 return invalid<ActionGuideProjection>("unknown projection", "camera.projection");
145}
146
147bool allowedAov(std::string_view name) {
148 return name == "silhouette" || name == "depth" || name == "normal" || name == "bone" || name == "partId" ||
149 name == "final";
150}
151
152Result<void> validatePack(const ActionGuidePack& pack) {
153 if (pack.revision == 0) return invalidVoid("revision must be positive", "revision");
154 if (pack.subject.domain.empty()) return invalidVoid("subject.domain is required", "subject.domain");
155 if (pack.skeletonRef.empty()) return invalidVoid("skeletonRef is required", "skeletonRef");
156 if (pack.clipRef.empty()) return invalidVoid("clipRef is required", "clipRef");
157 if (pack.canvas.width <= 0 || pack.canvas.height <= 0) return invalidVoid("canvas size must be positive", "canvas");
158 if (!(pack.canvas.pixelsPerUnit > 0.0) || !std::isfinite(pack.canvas.pixelsPerUnit))
159 return invalidVoid("canvas.pixelsPerUnit must be finite and > 0", "canvas.pixelsPerUnit");
160 for (double channel : pack.canvas.backgroundRgba) {
161 if (!std::isfinite(channel) || channel < 0.0 || channel > 1.0)
162 return invalidVoid("canvas.backgroundRgba channels must be in [0,1]", "canvas.backgroundRgba");
163 }
165 !std::isfinite(pack.camera.nearPlane) || !std::isfinite(pack.camera.farPlane))
166 return invalidVoid("camera near/far must be finite with far > near > 0", "camera");
168 if (!(pack.camera.orthoSize > 0.0) || !std::isfinite(pack.camera.orthoSize))
169 return invalidVoid("camera.orthoSize must be finite and > 0", "camera.orthoSize");
170 } else if (!(pack.camera.fovYDegrees > 0.0) || !(pack.camera.fovYDegrees < 180.0) ||
171 !std::isfinite(pack.camera.fovYDegrees)) {
172 return invalidVoid("camera.fovYDegrees must be in (0,180)", "camera.fovYDegrees");
173 }
174 const auto toEye =
175 std::array{pack.camera.target[0] - pack.camera.eye[0], pack.camera.target[1] - pack.camera.eye[1],
176 pack.camera.target[2] - pack.camera.eye[2]};
177 const double eyeLen2 = toEye[0] * toEye[0] + toEye[1] * toEye[1] + toEye[2] * toEye[2];
178 if (!(eyeLen2 > 1e-12)) return invalidVoid("camera eye and target must differ", "camera");
179 if (pack.shots.empty()) return invalidVoid("shots must be non-empty", "shots");
180 std::set<std::string, std::less<>> shotIds;
181 for (std::size_t i = 0; i < pack.shots.size(); ++i) {
182 const auto& shot = pack.shots[i];
183 const std::string path = "shots[" + std::to_string(i) + "]";
184 if (shot.shotId.empty()) return invalidVoid("shotId is required", path + ".shotId");
185 if (!shotIds.insert(shot.shotId).second) return invalidVoid("duplicate shotId", path + ".shotId");
186 if (!std::isfinite(shot.timeSeconds) || shot.timeSeconds < 0.0)
187 return invalidVoid("timeSeconds must be finite and >= 0", path + ".timeSeconds");
188 if (shot.sampleIndex && *shot.sampleIndex < 0)
189 return invalidVoid("sampleIndex must be >= 0", path + ".sampleIndex");
190 if (shot.view.empty()) return invalidVoid("view is required", path + ".view");
191 if (shot.aovs.empty()) return invalidVoid("aovs must be non-empty", path + ".aovs");
192 for (std::size_t a = 0; a < shot.aovs.size(); ++a) {
193 if (!allowedAov(shot.aovs[a]))
194 return invalidVoid("unsupported aov", path + ".aovs[" + std::to_string(a) + "]");
195 }
196 }
197 if (pack.actionBinding.abilityId.empty())
198 return invalidVoid("actionBinding.abilityId is required", "actionBinding.abilityId");
199 if (pack.actionBinding.clipName.empty())
200 return invalidVoid("actionBinding.clipName is required", "actionBinding.clipName");
201 if (!(pack.actionBinding.fps > 0.0) || !std::isfinite(pack.actionBinding.fps))
202 return invalidVoid("actionBinding.fps must be finite and > 0", "actionBinding.fps");
203 for (std::size_t i = 0; i < pack.actionBinding.phaseMarks.size(); ++i) {
204 const auto& mark = pack.actionBinding.phaseMarks[i];
205 const std::string path = "actionBinding.phaseMarks[" + std::to_string(i) + "]";
206 if (mark.name.empty()) return invalidVoid("phase mark name is required", path + ".name");
207 if (!std::isfinite(mark.timeSeconds) || mark.timeSeconds < 0.0)
208 return invalidVoid("phase mark timeSeconds must be finite and >= 0", path + ".timeSeconds");
209 }
210 if (!std::isfinite(pack.qc.minSilhouetteIoU) || pack.qc.minSilhouetteIoU < 0.0 || pack.qc.minSilhouetteIoU > 1.0)
211 return invalidVoid("qc.minSilhouetteIoU must be in [0,1]", "qc.minSilhouetteIoU");
212 if (!std::isfinite(pack.qc.maxJointErrorPx) || pack.qc.maxJointErrorPx < 0.0)
213 return invalidVoid("qc.maxJointErrorPx must be finite and >= 0", "qc.maxJointErrorPx");
214 if (pack.qc.maxRetries < 0) return invalidVoid("qc.maxRetries must be >= 0", "qc.maxRetries");
215 return Result<void>::success();
216}
217
218Result<ActionGuideSubject> decodeSubject(const Value& value) {
219 auto unknown = rejectUnknown(value, {"domain", "id", "generation"}, "subject");
220 if (!unknown) return Result<ActionGuideSubject>::failure(unknown.status());
221 auto domain = text(value, "domain", "subject.domain");
222 auto id = integer(value, "id", "subject.id");
223 auto generation = integer(value, "generation", "subject.generation");
225 if (!id) return Result<ActionGuideSubject>::failure(id.status());
227 if (id.value() < 0 || generation.value() < 0)
228 return invalid<ActionGuideSubject>("subject id/generation must be >= 0", "subject");
229 ActionGuideSubject subject;
230 subject.domain = std::move(domain).takeValue();
231 subject.id = static_cast<std::uint64_t>(id.value());
232 subject.generation = static_cast<std::uint64_t>(generation.value());
234}
235
236Result<ActionGuideCanvas> decodeCanvas(const Value& value) {
237 auto unknown =
238 rejectUnknown(value, {"width", "height", "pixelsPerUnit", "backgroundRgba", "transparent"}, "canvas");
239 if (!unknown) return Result<ActionGuideCanvas>::failure(unknown.status());
240 auto width = integer(value, "width", "canvas.width");
241 auto height = integer(value, "height", "canvas.height");
242 auto ppu = number(value, "pixelsPerUnit", "canvas.pixelsPerUnit");
243 auto rgba = vec4(value, "backgroundRgba", "canvas.backgroundRgba");
244 auto transparent = boolean(value, "transparent", "canvas.transparent");
245 if (!width) return Result<ActionGuideCanvas>::failure(width.status());
246 if (!height) return Result<ActionGuideCanvas>::failure(height.status());
247 if (!ppu) return Result<ActionGuideCanvas>::failure(ppu.status());
248 if (!rgba) return Result<ActionGuideCanvas>::failure(rgba.status());
249 if (!transparent) return Result<ActionGuideCanvas>::failure(transparent.status());
250 ActionGuideCanvas canvas;
251 canvas.width = width.value();
252 canvas.height = height.value();
253 canvas.pixelsPerUnit = ppu.value();
254 canvas.backgroundRgba = rgba.value();
255 canvas.transparent = transparent.value();
256 return Result<ActionGuideCanvas>::success(std::move(canvas));
257}
258
259Result<ActionGuideCamera> decodeCamera(const Value& value) {
260 auto unknown = rejectUnknown(
261 value, {"projection", "fovYDegrees", "orthoSize", "nearPlane", "farPlane", "eye", "target", "up"}, "camera");
262 if (!unknown) return Result<ActionGuideCamera>::failure(unknown.status());
263 auto projectionText = text(value, "projection", "camera.projection");
264 auto fov = number(value, "fovYDegrees", "camera.fovYDegrees");
265 auto ortho = number(value, "orthoSize", "camera.orthoSize");
266 auto nearPlane = number(value, "nearPlane", "camera.nearPlane");
267 auto farPlane = number(value, "farPlane", "camera.farPlane");
268 auto eye = vec3(value, "eye", "camera.eye");
269 auto target = vec3(value, "target", "camera.target");
270 auto up = vec3(value, "up", "camera.up");
271 if (!projectionText) return Result<ActionGuideCamera>::failure(projectionText.status());
272 if (!fov) return Result<ActionGuideCamera>::failure(fov.status());
273 if (!ortho) return Result<ActionGuideCamera>::failure(ortho.status());
274 if (!nearPlane) return Result<ActionGuideCamera>::failure(nearPlane.status());
275 if (!farPlane) return Result<ActionGuideCamera>::failure(farPlane.status());
276 if (!eye) return Result<ActionGuideCamera>::failure(eye.status());
277 if (!target) return Result<ActionGuideCamera>::failure(target.status());
278 if (!up) return Result<ActionGuideCamera>::failure(up.status());
279 auto projection = parseProjection(projectionText.value());
280 if (!projection) return Result<ActionGuideCamera>::failure(projection.status());
281 ActionGuideCamera camera;
282 camera.projection = projection.value();
283 camera.fovYDegrees = fov.value();
284 camera.orthoSize = ortho.value();
285 camera.nearPlane = nearPlane.value();
286 camera.farPlane = farPlane.value();
287 camera.eye = eye.value();
288 camera.target = target.value();
289 camera.up = up.value();
290 return Result<ActionGuideCamera>::success(std::move(camera));
291}
292
293Result<ActionGuideShot> decodeShot(const Value& value, std::string path) {
294 auto unknown = rejectUnknown(value, {"shotId", "timeSeconds", "sampleIndex", "view", "aovs", "tags"}, path);
295 if (!unknown) return Result<ActionGuideShot>::failure(unknown.status());
296 auto shotId = text(value, "shotId", path + ".shotId");
297 auto timeSeconds = number(value, "timeSeconds", path + ".timeSeconds");
298 auto view = text(value, "view", path + ".view");
299 auto aovs = stringArray(value, "aovs", path + ".aovs");
300 auto tags = stringArray(value, "tags", path + ".tags");
301 if (!shotId) return Result<ActionGuideShot>::failure(shotId.status());
302 if (!timeSeconds) return Result<ActionGuideShot>::failure(timeSeconds.status());
303 if (!view) return Result<ActionGuideShot>::failure(view.status());
304 if (!aovs) return Result<ActionGuideShot>::failure(aovs.status());
305 if (!tags) return Result<ActionGuideShot>::failure(tags.status());
306 ActionGuideShot shot;
307 shot.shotId = std::move(shotId).takeValue();
308 shot.timeSeconds = timeSeconds.value();
309 shot.view = std::move(view).takeValue();
310 shot.aovs = std::move(aovs).takeValue();
311 shot.tags = std::move(tags).takeValue();
312 if (const auto* sample = field(value, "sampleIndex")) {
313 if (!sample->isInt64()) return invalid<ActionGuideShot>("expected integer field", path + ".sampleIndex");
314 shot.sampleIndex = sample->asInt();
315 }
316 return Result<ActionGuideShot>::success(std::move(shot));
317}
318
319Result<ActionGuidePhaseMark> decodePhaseMark(const Value& value, std::string path) {
320 auto unknown = rejectUnknown(value, {"name", "timeSeconds"}, path);
321 if (!unknown) return Result<ActionGuidePhaseMark>::failure(unknown.status());
322 auto name = text(value, "name", path + ".name");
323 auto timeSeconds = number(value, "timeSeconds", path + ".timeSeconds");
324 if (!name) return Result<ActionGuidePhaseMark>::failure(name.status());
325 if (!timeSeconds) return Result<ActionGuidePhaseMark>::failure(timeSeconds.status());
326 ActionGuidePhaseMark mark;
327 mark.name = std::move(name).takeValue();
328 mark.timeSeconds = timeSeconds.value();
329 return Result<ActionGuidePhaseMark>::success(std::move(mark));
330}
331
332Result<ActionGuideBinding> decodeBinding(const Value& value) {
333 auto unknown = rejectUnknown(value, {"abilityId", "clipName", "fps", "loop", "phaseMarks"}, "actionBinding");
334 if (!unknown) return Result<ActionGuideBinding>::failure(unknown.status());
335 auto abilityId = text(value, "abilityId", "actionBinding.abilityId");
336 auto clipName = text(value, "clipName", "actionBinding.clipName");
337 auto fps = number(value, "fps", "actionBinding.fps");
338 auto loop = boolean(value, "loop", "actionBinding.loop");
339 if (!abilityId) return Result<ActionGuideBinding>::failure(abilityId.status());
341 if (!fps) return Result<ActionGuideBinding>::failure(fps.status());
342 if (!loop) return Result<ActionGuideBinding>::failure(loop.status());
343 const auto* marksValue = field(value, "phaseMarks");
344 if (!marksValue || !marksValue->isArray())
345 return invalid<ActionGuideBinding>("expected phaseMarks array", "actionBinding.phaseMarks");
346 ActionGuideBinding binding;
347 binding.abilityId = std::move(abilityId).takeValue();
348 binding.clipName = std::move(clipName).takeValue();
349 binding.fps = fps.value();
350 binding.loop = loop.value();
351 for (std::size_t i = 0; i < marksValue->getIf<Value::Array>()->size(); ++i) {
352 const auto& item = marksValue->getIf<Value::Array>()->at(i);
353 auto mark = decodePhaseMark(item, "actionBinding.phaseMarks[" + std::to_string(i) + "]");
354 if (!mark) return Result<ActionGuideBinding>::failure(mark.status());
355 binding.phaseMarks.push_back(std::move(mark).takeValue());
356 }
358}
359
360Result<ActionGuideQc> decodeQc(const Value& value) {
361 auto unknown = rejectUnknown(value, {"minSilhouetteIoU", "maxJointErrorPx", "maxRetries"}, "qc");
362 if (!unknown) return Result<ActionGuideQc>::failure(unknown.status());
363 auto iou = number(value, "minSilhouetteIoU", "qc.minSilhouetteIoU");
364 auto joint = number(value, "maxJointErrorPx", "qc.maxJointErrorPx");
365 auto retries = integer(value, "maxRetries", "qc.maxRetries");
366 if (!iou) return Result<ActionGuideQc>::failure(iou.status());
367 if (!joint) return Result<ActionGuideQc>::failure(joint.status());
368 if (!retries) return Result<ActionGuideQc>::failure(retries.status());
369 ActionGuideQc qc;
370 qc.minSilhouetteIoU = iou.value();
371 qc.maxJointErrorPx = joint.value();
372 qc.maxRetries = retries.value();
373 return Result<ActionGuideQc>::success(std::move(qc));
374}
375
376struct Vec3d {
377 double x = 0, y = 0, z = 0;
378};
379
380Vec3d sub(Vec3d a, Vec3d b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
381Vec3d cross(Vec3d a, Vec3d b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
382double dot(Vec3d a, Vec3d b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
383double length(Vec3d v) { return std::sqrt(dot(v, v)); }
384Result<Vec3d> normalize(Vec3d v, std::string path) {
385 const double len = length(v);
386 if (!(len > 1e-12)) return invalid<Vec3d>("degenerate camera basis", std::move(path));
387 return Result<Vec3d>::success(Vec3d{v.x / len, v.y / len, v.z / len});
388}
389
390struct Mat4d {
391 double m[16]{};
392};
393
394Mat4d mul(const Mat4d& a, const Mat4d& b) {
395 Mat4d out{};
396 for (int column = 0; column < 4; ++column) {
397 for (int row = 0; row < 4; ++row) {
398 out.m[column * 4 + row] = a.m[0 * 4 + row] * b.m[column * 4 + 0] + a.m[1 * 4 + row] * b.m[column * 4 + 1] +
399 a.m[2 * 4 + row] * b.m[column * 4 + 2] + a.m[3 * 4 + row] * b.m[column * 4 + 3];
400 }
401 }
402 return out;
403}
404
405std::array<double, 4> transform(const Mat4d& matrix, double x, double y, double z, double w) {
406 return {matrix.m[0] * x + matrix.m[4] * y + matrix.m[8] * z + matrix.m[12] * w,
407 matrix.m[1] * x + matrix.m[5] * y + matrix.m[9] * z + matrix.m[13] * w,
408 matrix.m[2] * x + matrix.m[6] * y + matrix.m[10] * z + matrix.m[14] * w,
409 matrix.m[3] * x + matrix.m[7] * y + matrix.m[11] * z + matrix.m[15] * w};
410}
411
412Result<Mat4d> lookAt(const ActionGuideCamera& camera) {
413 Vec3d eye{camera.eye[0], camera.eye[1], camera.eye[2]};
414 Vec3d target{camera.target[0], camera.target[1], camera.target[2]};
415 Vec3d up{camera.up[0], camera.up[1], camera.up[2]};
416 auto forward = normalize(sub(target, eye), "camera");
417 if (!forward) return Result<Mat4d>::failure(forward.status());
418 auto right = normalize(cross(forward.value(), up), "camera.up");
419 if (!right) return Result<Mat4d>::failure(right.status());
420 auto trueUp = cross(right.value(), forward.value());
421 Mat4d view{};
422 view.m[0] = right.value().x;
423 view.m[4] = right.value().y;
424 view.m[8] = right.value().z;
425 view.m[1] = trueUp.x;
426 view.m[5] = trueUp.y;
427 view.m[9] = trueUp.z;
428 view.m[2] = -forward.value().x;
429 view.m[6] = -forward.value().y;
430 view.m[10] = -forward.value().z;
431 view.m[15] = 1.0;
432 view.m[12] = -dot(right.value(), eye);
433 view.m[13] = -dot(trueUp, eye);
434 view.m[14] = dot(forward.value(), eye);
436}
437
438Result<Mat4d> projectionMatrix(const ActionGuidePack& pack) {
439 const double aspect = static_cast<double>(pack.canvas.width) / static_cast<double>(pack.canvas.height);
440 Mat4d proj{};
442 const double halfH = pack.camera.orthoSize;
443 const double halfW = halfH * aspect;
444 const double nearP = pack.camera.nearPlane;
445 const double farP = pack.camera.farPlane;
446 proj.m[0] = 1.0 / halfW;
447 proj.m[5] = 1.0 / halfH;
448 proj.m[10] = -2.0 / (farP - nearP);
449 proj.m[14] = -(farP + nearP) / (farP - nearP);
450 proj.m[15] = 1.0;
452 }
453 const double f = 1.0 / std::tan((pack.camera.fovYDegrees * 0.5) * 3.14159265358979323846 / 180.0);
454 const double nearP = pack.camera.nearPlane;
455 const double farP = pack.camera.farPlane;
456 proj.m[0] = f / aspect;
457 proj.m[5] = f;
458 proj.m[10] = (farP + nearP) / (nearP - farP);
459 proj.m[11] = -1.0;
460 proj.m[14] = (2.0 * farP * nearP) / (nearP - farP);
462}
463
464} // namespace
465
467 auto valid = validatePack(pack);
468 if (!valid) return Result<Value>::failure(valid.status());
469
470 Value::Array shots;
471 shots.reserve(pack.shots.size());
472 for (const auto& shot : pack.shots) {
473 Value::Object object{{"shotId", shot.shotId},
474 {"timeSeconds", shot.timeSeconds},
475 {"view", shot.view},
476 {"aovs", strings(shot.aovs)},
477 {"tags", strings(shot.tags)}};
478 if (shot.sampleIndex) object["sampleIndex"] = *shot.sampleIndex;
479 shots.emplace_back(std::move(object));
480 }
481
482 Value::Array phaseMarks;
483 phaseMarks.reserve(pack.actionBinding.phaseMarks.size());
484 for (const auto& mark : pack.actionBinding.phaseMarks) {
485 phaseMarks.emplace_back(Value::Object{{"name", mark.name}, {"timeSeconds", mark.timeSeconds}});
486 }
487
489 {"schema", std::string(kActionGuidePackSchemaId)},
490 {"schemaVersion", static_cast<std::int64_t>(kActionGuidePackSchemaVersion)},
491 {"revision", static_cast<std::int64_t>(pack.revision)},
492 {"subject", Value::Object{{"domain", pack.subject.domain},
493 {"id", static_cast<std::int64_t>(pack.subject.id)},
494 {"generation", static_cast<std::int64_t>(pack.subject.generation)}}},
495 {"skeletonRef", pack.skeletonRef},
496 {"clipRef", pack.clipRef},
497 {"rootMode", std::string(rootModeName(pack.rootMode))},
498 {"canvas", Value::Object{{"width", pack.canvas.width},
499 {"height", pack.canvas.height},
500 {"pixelsPerUnit", pack.canvas.pixelsPerUnit},
501 {"backgroundRgba", array4(pack.canvas.backgroundRgba)},
502 {"transparent", pack.canvas.transparent}}},
503 {"camera", Value::Object{{"projection", std::string(projectionName(pack.camera.projection))},
504 {"fovYDegrees", pack.camera.fovYDegrees},
505 {"orthoSize", pack.camera.orthoSize},
506 {"nearPlane", pack.camera.nearPlane},
507 {"farPlane", pack.camera.farPlane},
508 {"eye", array3(pack.camera.eye)},
509 {"target", array3(pack.camera.target)},
510 {"up", array3(pack.camera.up)}}},
511 {"shots", std::move(shots)},
512 {"actionBinding", Value::Object{{"abilityId", pack.actionBinding.abilityId},
513 {"clipName", pack.actionBinding.clipName},
514 {"fps", pack.actionBinding.fps},
515 {"loop", pack.actionBinding.loop},
516 {"phaseMarks", std::move(phaseMarks)}}},
517 {"qc", Value::Object{{"minSilhouetteIoU", pack.qc.minSilhouetteIoU},
518 {"maxJointErrorPx", pack.qc.maxJointErrorPx},
519 {"maxRetries", pack.qc.maxRetries}}},
520 };
521 return Result<Value>::success(Value(std::move(root)));
522}
523
525 auto unknown = rejectUnknown(value,
526 {"schema", "schemaVersion", "revision", "subject", "skeletonRef", "clipRef",
527 "rootMode", "canvas", "camera", "shots", "actionBinding", "qc"},
528 "$");
529 if (!unknown) return Result<ActionGuidePack>::failure(unknown.status());
530 auto schema = text(value, "schema", "schema");
531 auto version = integer(value, "schemaVersion", "schemaVersion");
532 auto revision = integer(value, "revision", "revision");
533 auto skeletonRef = text(value, "skeletonRef", "skeletonRef");
534 auto clipRef = text(value, "clipRef", "clipRef");
535 auto rootModeText = text(value, "rootMode", "rootMode");
536 if (!schema) return Result<ActionGuidePack>::failure(schema.status());
537 if (!version) return Result<ActionGuidePack>::failure(version.status());
539 if (!skeletonRef) return Result<ActionGuidePack>::failure(skeletonRef.status());
540 if (!clipRef) return Result<ActionGuidePack>::failure(clipRef.status());
541 if (!rootModeText) return Result<ActionGuidePack>::failure(rootModeText.status());
542 if (schema.value() != kActionGuidePackSchemaId)
543 return invalid<ActionGuidePack>("unexpected guide-pack schema", "schema");
544 if (version.value() != static_cast<std::int64_t>(kActionGuidePackSchemaVersion))
545 return invalid<ActionGuidePack>("unsupported guide-pack schema version", "schemaVersion");
546 if (revision.value() <= 0) return invalid<ActionGuidePack>("revision must be positive", "revision");
547
548 const auto* subjectValue = field(value, "subject");
549 const auto* canvasValue = field(value, "canvas");
550 const auto* cameraValue = field(value, "camera");
551 const auto* shotsValue = field(value, "shots");
552 const auto* bindingValue = field(value, "actionBinding");
553 const auto* qcValue = field(value, "qc");
554 if (!subjectValue) return invalid<ActionGuidePack>("subject is required", "subject");
555 if (!canvasValue) return invalid<ActionGuidePack>("canvas is required", "canvas");
556 if (!cameraValue) return invalid<ActionGuidePack>("camera is required", "camera");
557 if (!shotsValue || !shotsValue->isArray()) return invalid<ActionGuidePack>("shots must be an array", "shots");
558 if (!bindingValue) return invalid<ActionGuidePack>("actionBinding is required", "actionBinding");
559 if (!qcValue) return invalid<ActionGuidePack>("qc is required", "qc");
560
561 auto subject = decodeSubject(*subjectValue);
562 if (!subject) return Result<ActionGuidePack>::failure(subject.status());
563 auto canvas = decodeCanvas(*canvasValue);
564 if (!canvas) return Result<ActionGuidePack>::failure(canvas.status());
565 auto camera = decodeCamera(*cameraValue);
566 if (!camera) return Result<ActionGuidePack>::failure(camera.status());
567 auto binding = decodeBinding(*bindingValue);
568 if (!binding) return Result<ActionGuidePack>::failure(binding.status());
569 auto qc = decodeQc(*qcValue);
570 if (!qc) return Result<ActionGuidePack>::failure(qc.status());
571 auto rootMode = parseRootMode(rootModeText.value());
572 if (!rootMode) return Result<ActionGuidePack>::failure(rootMode.status());
573
575 pack.revision = static_cast<std::uint64_t>(revision.value());
576 pack.subject = std::move(subject).takeValue();
577 pack.skeletonRef = std::move(skeletonRef).takeValue();
578 pack.clipRef = std::move(clipRef).takeValue();
579 pack.rootMode = rootMode.value();
580 pack.canvas = std::move(canvas).takeValue();
581 pack.camera = std::move(camera).takeValue();
582 pack.actionBinding = std::move(binding).takeValue();
583 pack.qc = std::move(qc).takeValue();
584 for (std::size_t i = 0; i < shotsValue->getIf<Value::Array>()->size(); ++i) {
585 auto shot = decodeShot(shotsValue->getIf<Value::Array>()->at(i), "shots[" + std::to_string(i) + "]");
586 if (!shot) return Result<ActionGuidePack>::failure(shot.status());
587 pack.shots.push_back(std::move(shot).takeValue());
588 }
589
590 auto valid = validatePack(pack);
591 if (!valid) return Result<ActionGuidePack>::failure(valid.status());
592 return Result<ActionGuidePack>::success(std::move(pack));
593}
594
596 std::span<const ActionGuideBoneWorld> bones) {
597 auto valid = validatePack(pack);
598 if (!valid) return Result<std::vector<ActionGuideLandmark>>::failure(valid.status());
599 auto view = lookAt(pack.camera);
600 if (!view) return Result<std::vector<ActionGuideLandmark>>::failure(view.status());
601 auto proj = projectionMatrix(pack);
602 if (!proj) return Result<std::vector<ActionGuideLandmark>>::failure(proj.status());
603 const Mat4d viewProj = mul(proj.value(), view.value());
604
605 std::vector<ActionGuideLandmark> landmarks;
606 landmarks.reserve(bones.size());
607 for (std::size_t i = 0; i < bones.size(); ++i) {
608 const auto& bone = bones[i];
609 if (bone.boneId.empty())
610 return invalid<std::vector<ActionGuideLandmark>>("boneId is required",
611 "bones[" + std::to_string(i) + "].boneId");
612 if (!std::isfinite(bone.position[0]) || !std::isfinite(bone.position[1]) || !std::isfinite(bone.position[2]))
613 return invalid<std::vector<ActionGuideLandmark>>("bone position must be finite",
614 "bones[" + std::to_string(i) + "].position");
615 const auto clip = transform(viewProj, bone.position[0], bone.position[1], bone.position[2], 1.0);
616 if (!(std::abs(clip[3]) > 1e-12) || !std::isfinite(clip[3]))
617 return invalid<std::vector<ActionGuideLandmark>>("projected bone has non-finite w",
618 "bones[" + std::to_string(i) + "]");
619 const double invW = 1.0 / clip[3];
620 const double ndcX = clip[0] * invW;
621 const double ndcY = clip[1] * invW;
622 if (!std::isfinite(ndcX) || !std::isfinite(ndcY))
623 return invalid<std::vector<ActionGuideLandmark>>("projected bone has non-finite ndc",
624 "bones[" + std::to_string(i) + "]");
625 ActionGuideLandmark landmark;
626 landmark.boneId = bone.boneId;
627 landmark.name = bone.name.empty() ? bone.boneId : bone.name;
628 landmark.x = (ndcX * 0.5 + 0.5) * static_cast<double>(pack.canvas.width);
629 // Top-left origin: NDC +Y maps to smaller pixel Y.
630 landmark.y = (1.0 - (ndcY * 0.5 + 0.5)) * static_cast<double>(pack.canvas.height);
631 const auto viewPos = transform(view.value(), bone.position[0], bone.position[1], bone.position[2], 1.0);
632 landmark.depth = -viewPos[2];
633 landmark.parentId = bone.parentId;
634 landmarks.push_back(std::move(landmark));
635 }
636 return Result<std::vector<ActionGuideLandmark>>::success(std::move(landmarks));
637}
638
640 switch (phase) {
641 case ActionGuideCapturePhase::Queued: return "queued";
642 case ActionGuideCapturePhase::Rendering: return "rendering";
643 case ActionGuideCapturePhase::Ready: return "ready";
644 case ActionGuideCapturePhase::Failed: return "failed";
645 case ActionGuideCapturePhase::Cancelled: return "cancelled";
646 case ActionGuideCapturePhase::Released: return "released";
647 }
648 return "unknown";
649}
650
651} // namespace eve
double z
double y
double m[16]
double x
Versioned eve.action.guide-pack document codec and CPU landmark projection.
LogicalId target
float w
Definition AnimClip.cpp:738
int root
Definition AnimSmr.cpp:119
int subject
Definition AnimSmr.cpp:163
std::uint64_t revision
float halfW
float halfH
Stable, structured diagnostics shared by engine modules.
Generation generation
Definition ECS.cpp:40
std::string name
Definition ECS.cpp:154
std::string message
float camera[2]
wgpu::PopErrorScopeStatus status
glm::vec4 clip
float v
HexVec3 up
HexVec3 right
std::uint32_t height
std::uint32_t width
float length
const ColourScope * parent
std::vector< Bone > bones
std::string clipName
std::uint32_t bone
ActionGuidePack pack
Poco::JSON::Object::Ptr result
bool valid
std::unordered_set< std::string > tags
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanSprayView view
eve::action::ActionVfxBinding binding
float f
std::string path
Definition PlayHost.cpp:111
std::set< std::string > values
Object::Ptr value
std::string domain
glm::vec3 eye
glm::mat4 viewProj
glm::mat4 proj
double number
glm::dmat4 matrix
std::string text
ScriptStructure out
std::string_view source
unsigned column
bool boolean
float phase
float size
Definition TreeMesh.cpp:158
std::size_t at
V forward
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
constexpr std::uint64_t value() const noexcept
Returns the underlying value at an explicit protocol boundary.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
Value stringArray(const std::vector< std::string > &values)
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
Status invalid(const char *message)
double dot(ForceVector a, ForceVector b)
Definition ForceMath.h:14
Vec3 sub(Vec3 a, Vec3 b)
Vec3 mul(Vec3 a, float scale)
double sample(const Heightmap &map, double u, double v)
Sample.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
Result< Value > encodeActionGuidePack(const ActionGuidePack &pack)
Encode one validated guide pack as canonical schema version 1 data.
Result< std::vector< ActionGuideLandmark > > projectActionGuideLandmarks(const ActionGuidePack &pack, std::span< const ActionGuideBoneWorld > bones)
Project world-space bones into guide-canvas landmarks using the pack camera/canvas.
ActionGuideRootMode
Root-motion handling when sampling the subject for guide shots.
std::string_view actionGuideCapturePhaseName(ActionGuideCapturePhase phase)
Stable protocol spelling of a guide-capture phase.
ActionGuideCapturePhase
Guide-capture job progress; Ready is published only after every requested AOV exists.
Result< ActionGuidePack > decodeActionGuidePack(const Value &value)
Decode schema version 1; unknown fields/versions and incomplete shots are rejected.
constexpr std::uint64_t kActionGuidePackSchemaVersion
ActionGuideProjection
Camera projection used for guide AOVs and landmark projection.
constexpr std::string_view kActionGuidePackSchemaId
std::vector< ActionGuidePhaseMark > phaseMarks
std::array< double, 3 > eye
std::array< double, 3 > target
std::array< double, 3 > up
ActionGuideProjection projection
std::array< double, 4 > backgroundRgba
Projected joint in guide-canvas pixels (origin top-left) plus view depth.
std::optional< std::string > parentId
Authoritative guide-pack document (no pointers, GPU tokens, or ECS slots). @ownership Caller-owned va...
ActionGuideCamera camera
ActionGuideSubject subject
ActionGuideRootMode rootMode
ActionGuideCanvas canvas
std::vector< ActionGuideShot > shots
ActionGuideBinding actionBinding
std::int64_t maxRetries