载入中...
搜索中...
未找到
UpperBodyOrientationCompensator.cpp
浏览该文件的文档.
2
4
5#include <cmath>
6#include <utility>
7
8namespace eve::animation {
9namespace {
10
11struct Quat {
12 float x = 0.0f;
13 float y = 0.0f;
14 float z = 0.0f;
15 float w = 1.0f;
16};
17
18Quat rotation(const TransformTRS& value) { return {value.qx, value.qy, value.qz, value.qw}; }
19Quat inverse(const Quat& value) { return {-value.x, -value.y, -value.z, value.w}; }
20Quat multiply(const Quat& a, const Quat& b) {
21 return {a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
22 a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
23 a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
24 a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z};
25}
26
27Result<void> profileError(std::string message, std::string path) {
30}
31
32bool isDescendant(const AnimSkeleton& skeleton, int bone, int ancestor) {
33 for (int current = bone; current >= 0; current = skeleton.getParent(current))
34 if (current == ancestor) return true;
35 return false;
36}
37
38} // namespace
39
41 : skeleton_(skeleton) {}
42
44 const int root = skeleton_.findBone(profile.rootBone);
45 const int hips = skeleton_.findBone(profile.hipsBone);
46 const int spine = skeleton_.findBone(profile.spineBone);
47 if (root < 0) return profileError("upper-body orientation root bone was not found", "profile.rootBone");
48 if (hips < 0) return profileError("upper-body orientation hips bone was not found", "profile.hipsBone");
49 if (spine < 0) return profileError("upper-body orientation spine bone was not found", "profile.spineBone");
50 if (!isDescendant(skeleton_, hips, root))
51 return profileError("upper-body orientation hips bone must descend from root", "profile.hipsBone");
52 if (skeleton_.getParent(spine) != hips)
53 return profileError("upper-body orientation spine bone must be a direct child of hips", "profile.spineBone");
54
55 rootBone_ = root;
56 hipsBone_ = hips;
57 spineBone_ = spine;
58 configured_ = true;
60}
61
63 rootBone_ = hipsBone_ = spineBone_ = -1;
64 configured_ = false;
65}
66
68 if (!configured_)
70 "upper-body orientation profile is not configured",
71 "profile"));
72 if (pose.getBoneCount() != skeleton_.getBoneCount() ||
73 upperBodyPose.getBoneCount() != skeleton_.getBoneCount())
74 return profileError("upper-body orientation poses must match the configured skeleton", "pose");
75 if (!std::isfinite(weight) || weight < 0.0f || weight > 1.0f)
76 return profileError("upper-body orientation weight must be finite and within [0,1]", "weight");
78
79 upperBodyPose.computeWorld(&skeleton_);
80 pose.computeWorld(&skeleton_);
81 const Quat targetInRoot = multiply(inverse(rotation(upperBodyPose.world(rootBone_))),
82 rotation(upperBodyPose.world(spineBone_)));
83 const Quat targetWorld = multiply(rotation(pose.world(rootBone_)), targetInRoot);
84 const Quat targetLocal = multiply(inverse(rotation(pose.world(hipsBone_))), targetWorld);
85
86 TransformTRS& spine = pose.local(spineBone_);
87 slerpQuat(spine.qx, spine.qy, spine.qz, spine.qw, targetLocal.x, targetLocal.y, targetLocal.z, targetLocal.w,
88 weight, spine.qx, spine.qy, spine.qz, spine.qw);
89 spine.normalizeRotation();
90 pose.computeWorld(&skeleton_);
92}
93
94} // namespace eve::animation
double z
double y
double x
float w
Definition AnimClip.cpp:738
int root
Definition AnimSmr.cpp:119
eve::EntitySpatialPose pose
std::string message
std::array< float, 4 > rotation
std::uint32_t bone
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
double current
double weight
Root-relative upper-body orientation correction.
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
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:17
int getBoneCount() const noexcept
Returns the bone count.
Definition AnimPose.h:36
const TransformTRS & world(int boneIndex) const
World.
Definition AnimPose.cpp:371
void computeWorld(const AnimSkeleton *skeleton)
Compute world transforms from local pose + skeleton hierarchy. World values readable via getWorld* af...
Definition AnimPose.cpp:207
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
int getBoneCount() const
Returns the bone count.
int getParent(int boneIndex) const
Returns the parent.
int findBone(const std::string &name) const
Finds bone.
Result< void > configure(const UpperBodyOrientationProfile &profile)
Transactionally replace the resolved profile.
Result< void > apply(AnimPose &pose, AnimPose &upperBodyPose, float weight) const
Blend the sampled upper-body spine orientation into an accumulated pose.
UpperBodyOrientationCompensator(const AnimSkeleton &skeleton) noexcept
Construct over a skeleton that must outlive this object.
void slerpQuat(float ax, float ay, float az, float aw, float bx, float by, float bz, float bw, float t, float &ox, float &oy, float &oz, float &ow)
Slerp quat.
Definition AnimMath.h:43
Local TRS used by skeletal animation (quaternion xyzw).
Definition AnimMath.h:9
void normalizeRotation()
Normalize rotation.
Definition AnimMath.h:18
Stable bone-name profile used to prepare upper-body orientation compensation.