39 MT_CHECK(joint1 < this->
skeleton_.joints.size(),
"Invalid joint1 index: {}", joint1);
40 MT_CHECK(joint2 < this->
skeleton_.joints.size(),
"Invalid joint2 index: {}", joint2);
41 MT_CHECK(joint1 != joint2,
"joint1 and joint2 must be different");
42 MT_CHECK(targetDistance >= 0,
"targetDistance must be non-negative");
44 constraints_.push_back(
58 if (state.
jointState.empty() || constraints_.empty()) {
64 for (
const auto& constr : constraints_) {
65 const auto& jointState1 = state.
jointState[constr.joint1];
66 const auto& jointState2 = state.
jointState[constr.joint2];
68 const Vector3<T> worldPos1 = jointState1.transform * constr.offset1;
69 const Vector3<T> worldPos2 = jointState2.transform * constr.offset2;
72 const T actualDistance = diff.norm();
73 const T distanceError = actualDistance - constr.targetDistance;
87 if (state.
jointState.empty() || constraints_.empty()) {
93 for (
const auto& constr : constraints_) {
94 const auto& jointState1 = state.
jointState[constr.joint1];
95 const auto& jointState2 = state.
jointState[constr.joint2];
97 const Vector3<T> worldPos1 = jointState1.transform * constr.offset1;
98 const Vector3<T> worldPos2 = jointState2.transform * constr.offset2;
100 const Vector3<T> diff = worldPos1 - worldPos2;
101 const T actualDistance = diff.norm();
103 if (actualDistance <
Eps<T>(1e-8f, 1e-14)) {
107 const T distanceError = actualDistance - constr.targetDistance;
110 const Vector3<T> direction = diff / actualDistance;
111 const T wgt = T(2) * constr.weight * distanceError *
kDistanceWeight * this->weight_;
113 auto processJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos, T sign) {
114 size_t currentJoint = jointIndex;
116 const auto& currentJointState = state.
jointState[currentJoint];
118 const Vector3<T> posd = worldPos - currentJointState.translation();
120 for (
size_t d = 0; d < 3; d++) {
122 const T val = sign * direction.dot(currentJointState.getTranslationDerivative(d)) * wgt;
132 sign * direction.dot(currentJointState.getRotationDerivative(d, posd)) * wgt;
136 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
144 const T val = sign * direction.dot(currentJointState.getScaleDerivative(posd)) * wgt;
153 currentJoint = this->
skeleton_.joints[currentJoint].parent;
157 processJoint(constr.joint1, worldPos1, T(1));
158 processJoint(constr.joint2, worldPos2, T(-1));
174 jacobian.rows() >= gsl::narrow<Eigen::Index>(constraints_.size()),
175 "Jacobian rows mismatch: Actual {}, Expected {}",
177 gsl::narrow<Eigen::Index>(constraints_.size()));
180 residual.rows() >= gsl::narrow<Eigen::Index>(constraints_.size()),
181 "Residual rows mismatch: Actual {}, Expected {}",
183 gsl::narrow<Eigen::Index>(constraints_.size()));
185 if (state.
jointState.empty() || constraints_.empty()) {
195 for (
const auto& constr : constraints_) {
196 const auto& jointState1 = state.
jointState[constr.joint1];
197 const auto& jointState2 = state.
jointState[constr.joint2];
199 const Vector3<T> worldPos1 = jointState1.transform * constr.offset1;
200 const Vector3<T> worldPos2 = jointState2.transform * constr.offset2;
202 const Vector3<T> diff = worldPos1 - worldPos2;
203 const T actualDistance = diff.norm();
205 if (actualDistance <
Eps<T>(1e-8f, 1e-14)) {
209 const T distanceError = actualDistance - constr.targetDistance;
212 const Vector3<T> direction = diff / actualDistance;
213 const T fac = std::sqrt(constr.weight) * wgt;
215 const int row = pos++;
217 auto processJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos, T sign) {
218 size_t currentJoint = jointIndex;
220 const auto& currentJointState = state.
jointState[currentJoint];
222 const Vector3<T> posd = worldPos - currentJointState.translation();
224 for (
size_t d = 0; d < 3; d++) {
226 const T val = sign * direction.dot(currentJointState.getTranslationDerivative(d)) * fac;
236 sign * direction.dot(currentJointState.getRotationDerivative(d, posd)) * fac;
240 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
248 const T val = sign * direction.dot(currentJointState.getScaleDerivative(posd)) * fac;
257 currentJoint = this->
skeleton_.joints[currentJoint].parent;
261 processJoint(constr.joint1, worldPos1, T(1));
262 processJoint(constr.joint2, worldPos2, T(-1));
264 residual(row) = distanceError * fac;
274 return constraints_.size();
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Error function that penalizes deviation from a target distance between two points attached to differe...
void clearConstraints()
Clear all constraints.
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
double getJacobian(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< MatrixX< T > > jacobian, Ref< VectorX< T > > residual, int &usedRows) override
void addConstraint(size_t joint1, const Vector3< T > &offset1, size_t joint2, const Vector3< T > &offset2, T targetDistance, T weight=T(1))
Add a constraint between two joints with local offsets.
size_t getJacobianSize() const final
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< VectorX< T > > gradient) override
static constexpr T kDistanceWeight
Default weight for distance constraints.
JointToJointDistanceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
const Skeleton & skeleton_
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
VectorX< bool > activeJointParams_
const ParameterTransform & parameterTransform_
constexpr T Eps(T FloatEps=T(1e-7), T DoubleEps=T(1e-16))
Returns a type-appropriate tolerance/epsilon value.
SkeletonT< float > Skeleton
ParameterTransformT< float > ParameterTransform
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
T sqr(const T &x)
Computes the square of a value.
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
A character model with skeletal structure, mesh, and optional components.
Constraint that enforces a distance relationship between two points attached to different joints.
Represents the complete state of a model's mesh.
A vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.