载入中...
搜索中...
未找到
ControlRigLimits.cpp
浏览该文件的文档.
2
3namespace eve::animation {
4namespace {
5constexpr float radians = 0.017453292519943295f;
6bool axisValid(const RigAxisLimit& a, float neutral, float lo, float hi) {
7 return (a.mode == RigAxisMode::Free || a.mode == RigAxisMode::Locked || a.mode == RigAxisMode::Limited) &&
8 std::isfinite(a.minimum) && std::isfinite(a.maximum) && a.minimum <= a.maximum &&
9 (a.mode != RigAxisMode::Limited ||
10 (a.minimum <= neutral && a.maximum >= neutral && a.minimum >= lo && a.maximum <= hi));
11}
12float project(float value, const RigAxisLimit& a, float neutral) {
13 if (a.mode == RigAxisMode::Locked) return neutral;
14 return a.mode == RigAxisMode::Limited ? std::clamp(value, a.minimum, a.maximum) : value;
15}
16} // namespace
18 using namespace rig_detail;
19 if (!validTRS(t) || !validTRS(l.reference)) return error<TransformTRS>("invalid constraint TRS");
20 for (size_t i = 0; i < 3; ++i) {
21 if (!axisValid(l.translation[i], 0, -1e20f, 1e20f) ||
22 !axisValid(l.rotation[i], 0, i == 1 ? -90.f : -180.f, i == 1 ? 90.f : 180.f))
23 return error<TransformTRS>("invalid axis range or mode");
24 if (l.extension && l.translation[i].mode != RigAxisMode::Free)
25 return error<TransformTRS>("extension and translation axis limits are mutually exclusive");
26 }
27 if (!axisValid(l.scale, 1, 1e-5f, 1e20f) || !std::isfinite(l.minimumLength) || !std::isfinite(l.maximumLength) ||
28 l.minimumLength <= 0 || l.minimumLength > 1 || l.maximumLength < 1)
29 return error<TransformTRS>("invalid scale or extension range");
30 auto result = t;
31 const auto& r = l.reference;
32 if (l.extension) {
33 const double length2 = double(r.px) * r.px + double(r.py) * r.py + double(r.pz) * r.pz;
34 if (length2 < 1e-12) return error<TransformTRS>("extension needs nonzero reference bone length");
35 const double ratio = std::clamp((double(t.px) * r.px + double(t.py) * r.py + double(t.pz) * r.pz) / length2,
36 double(l.minimumLength), double(l.maximumLength));
37 result.px = static_cast<float>(r.px * ratio);
38 result.py = static_cast<float>(r.py * ratio);
39 result.pz = static_cast<float>(r.pz * ratio);
40 } else {
41 if (l.translation[0].mode != RigAxisMode::Free) result.px = r.px + project(t.px - r.px, l.translation[0], 0);
42 if (l.translation[1].mode != RigAxisMode::Free) result.py = r.py + project(t.py - r.py, l.translation[1], 0);
43 if (l.translation[2].mode != RigAxisMode::Free) result.pz = r.pz + project(t.pz - r.pz, l.translation[2], 0);
44 }
45 if (std::any_of(l.rotation.begin(), l.rotation.end(), [](const auto& a) { return a.mode != RigAxisMode::Free; })) {
46 TransformTRS inverse;
47 inverse.qx = -r.qx;
48 inverse.qy = -r.qy;
49 inverse.qz = -r.qz;
50 inverse.qw = r.qw;
51 const auto q = detail::mulTRS(inverse, t);
52 const float sy = std::clamp(2 * (q.qw * q.qy - q.qz * q.qx), -1.f, 1.f);
53 float x, y = std::asin(sy), z;
54 if (std::abs(sy) > 0.999999f) {
55 // Canonical singular branch: Z=0, X contains the remaining rotation.
56 x = 2 * std::atan2(q.qx, q.qw);
57 z = 0;
58 } else {
59 x = std::atan2(2 * (q.qw * q.qx + q.qy * q.qz), 1 - 2 * (q.qx * q.qx + q.qy * q.qy));
60 z = std::atan2(2 * (q.qw * q.qz + q.qx * q.qy), 1 - 2 * (q.qy * q.qy + q.qz * q.qz));
61 }
62 x = std::remainder(x / radians, 360.f);
63 y /= radians;
64 z /= radians;
65 const float px = project(x, l.rotation[0], 0), py = project(y, l.rotation[1], 0),
66 pz = project(z, l.rotation[2], 0);
67 if (std::abs(px - x) > 1e-4f || std::abs(py - y) > 1e-4f || std::abs(pz - z) > 1e-4f) {
68 x = px * radians * 0.5f;
69 y = py * radians * 0.5f;
70 z = pz * radians * 0.5f;
71 const float cx = std::cos(x), sx = std::sin(x), cy = std::cos(y), sy2 = std::sin(y), cz = std::cos(z),
72 sz = std::sin(z);
73 TransformTRS delta;
74 delta.qx = sx * cy * cz - cx * sy2 * sz;
75 delta.qy = cx * sy2 * cz + sx * cy * sz;
76 delta.qz = cx * cy * sz - sx * sy2 * cz;
77 delta.qw = cx * cy * cz + sx * sy2 * sz;
78 const auto rotation = detail::mulTRS(r, delta);
79 result.qx = rotation.qx;
80 result.qy = rotation.qy;
81 result.qz = rotation.qz;
82 result.qw = rotation.qw;
83 result.normalizeRotation();
84 }
85 }
86 if (l.scale.mode != RigAxisMode::Free) result.sx = result.sy = result.sz = r.sx * project(t.sx / r.sx, l.scale, 1);
87 if (!validTRS(result)) return error<TransformTRS>("constraint projection overflow");
89}
90} // namespace eve::animation
double z
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
std::array< double, 10 > q
double r
std::vector< Colorf > px
float ratio
std::array< float, 4 > rotation
Poco::JSON::Object::Ptr result
MeleePoint3 a
Definition MeleeHit.cpp:40
Object::Ptr value
float t
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
TransformTRS mulTRS(const TransformTRS &parent, const TransformTRS &local)
Mul trs.
bool validTRS(const TransformTRS &t)
float length2(float x, float z)
Length 2.
Definition AnimMath.h:150
EVENGINE_API_WORLD Result< TransformTRS > constrainRigControl(const RigControlLimits &limits, const TransformTRS &transform)
Project a local TRS into limits, returning an owned value or invalid-config diagnostic.
Snapshot project(Files files, Object::Ptr stats, std::string defaultProjectId, std::string projectDir)
Build a candidate evidence graph without touching the published snapshot.
Owning parent-local limits; rotation is reference-relative XYZ in degrees.
Definition ControlRig.h:37
std::array< RigAxisLimit, 3 > rotation
Definition ControlRig.h:39
std::array< RigAxisLimit, 3 > translation
Definition ControlRig.h:39
Local TRS used by skeletal animation (quaternion xyzw).
Definition AnimMath.h:9