19template <
typename T,
class Data,
size_t FuncDim,
size_t NumVec,
size_t NumPos>
26 loss_(lossAlpha, lossC),
28 static_assert(FuncDim > 0,
"The error function cannot be empty.");
29 static_assert(NumVec > 0,
"At least one vector is required in the constraint.");
31 NumVec >= NumPos,
"The number of points cannot be more than the total number of vectors.");
34template <
typename T,
class Data,
size_t FuncDim,
size_t NumVec,
size_t NumPos>
42 std::array<VType, NumVec> v;
43 std::array<DfdvType, NumVec> dfdv;
45 for (
size_t iConstr = 0; iConstr <
constraints_.size(); ++iConstr) {
47 if (constr.weight == 0) {
51 error += constr.weight *
loss_.value(f.squaredNorm());
56template <
typename T,
class Data,
size_t FuncDim,
size_t NumVec,
size_t NumPos>
61 Ref<Eigen::VectorX<T>> gradient) {
73 for (
size_t iConstr = 0; iConstr < this->
constraints_.size(); ++iConstr) {
75 if (constr.weight == 0) {
80 std::array<VType, NumVec> v;
81 std::array<DfdvType, NumVec> dfdv;
88 const T sqrError = f.squaredNorm();
89 const T w = constr.weight * this->
weight_;
90 error += w *
loss_.value(sqrError);
95 std::array<Eigen::Vector3<T>, NumVec> wR;
96 for (
size_t jVec = 0; jVec < NumVec; ++jVec) {
97 if (dfdv[jVec].isZero()) {
100 wR[jVec].noalias() = dfdv[jVec].transpose() * deriv;
105 size_t jntIndex = constr.parent;
109 const auto& jntState = state.
jointState[jntIndex];
112 for (
size_t jVec = 0; jVec < NumVec; ++jVec) {
113 if (wR[jVec].isZero()) {
117 Eigen::Vector3<T> offset;
119 offset.noalias() = v[jVec] - jntState.translation();
126 for (
size_t d = 0; d < 3; d++) {
127 if (!this->activeJointParams_[jntParamIndex + d]) {
130 const T val = wR[jVec].dot(jntState.getTranslationDerivative(d));
131 for (
auto index = outerIndexPtr[jntParamIndex + d];
132 index < outerIndexPtr[jntParamIndex + d + 1];
134 if (this->enabledParameters_.test(innerIndexPtr[index])) {
135 jointGrad_[innerIndexPtr[index]] += val * valuePtr[index];
142 for (
size_t d = 0; d < 3; d++) {
143 if (!this->activeJointParams_[jntParamIndex + 3 + d]) {
146 const T val = wR[jVec].dot(jntState.getRotationDerivative(d, offset));
147 for (
auto index = outerIndexPtr[jntParamIndex + 3 + d];
148 index < outerIndexPtr[jntParamIndex + 3 + d + 1];
151 jointGrad_[innerIndexPtr[index]] += val * valuePtr[index];
158 if (this->activeJointParams_[jntParamIndex + 6]) {
159 const T val = wR[jVec].dot(jntState.getScaleDerivative(offset));
160 for (
auto index = outerIndexPtr[jntParamIndex + 6];
161 index < outerIndexPtr[jntParamIndex + 6 + 1];
163 if (this->enabledParameters_.test(innerIndexPtr[index])) {
164 jointGrad_[innerIndexPtr[index]] += val * valuePtr[index];
170 jntIndex = this->skeleton_.joints[jntIndex].parent;
174 gradient += jointGrad_;
178template <
typename T,
class Data,
size_t FuncDim,
size_t NumVec,
size_t NumPos>
183 Ref<Eigen::MatrixX<T>> jacobian,
184 Ref<Eigen::VectorX<T>> residual,
195 for (
size_t iConstr = 0; iConstr < this->
constraints_.size(); ++iConstr) {
197 if (constr.weight == 0) {
202 std::array<VType, NumVec> v;
203 std::array<DfdvType, NumVec> dfdv;
206 const T sqrError = f.squaredNorm();
207 const T w = constr.weight * this->
weight_;
208 error += w * this->
loss_.value(sqrError);
209 const T derivScale = std::sqrt(w * this->
loss_.deriv(sqrError));
212 const size_t rowIndex = FuncDim * iConstr;
213 residual.template middleRows<FuncDim>(rowIndex).noalias() = derivScale * f;
219 const bool zeroDfdv =
220 !std::any_of(dfdv.begin(), dfdv.end(), [](
const auto& m) { return !m.isZero(); });
225 auto&& jac = jacobian.template middleRows<FuncDim>(rowIndex);
228 size_t jntIndex = constr.parent;
232 const auto& jntState = state.
jointState[jntIndex];
235 for (
size_t jVec = 0; jVec < NumVec; ++jVec) {
236 if (dfdv[jVec].isZero()) {
240 Eigen::Vector3<T> offset;
242 offset.noalias() = v[jVec] - jntState.translation();
249 for (
size_t d = 0; d < 3; ++d) {
253 const FuncType jc = derivScale * dfdv[jVec] * jntState.getTranslationDerivative(d);
254 for (
auto index = outerIndexPtr[jntParamIndex + d];
255 index < outerIndexPtr[jntParamIndex + d + 1];
258 jac.col(innerIndexPtr[index]).noalias() += jc * valuePtr[index];
265 for (
size_t d = 0; d < 3; ++d) {
269 const FuncType jc = derivScale * dfdv[jVec] * jntState.getRotationDerivative(d, offset);
270 for (
auto index = outerIndexPtr[jntParamIndex + 3 + d];
271 index < outerIndexPtr[jntParamIndex + 3 + d + 1];
274 jac.col(innerIndexPtr[index]).noalias() += jc * valuePtr[index];
282 const FuncType jc = derivScale * dfdv[jVec] * jntState.getScaleDerivative(offset);
283 for (
auto index = outerIndexPtr[jntParamIndex + 6];
284 index < outerIndexPtr[jntParamIndex + 6 + 1];
287 jac.col(innerIndexPtr[index]).noalias() += jc * valuePtr[index];
293 jntIndex = this->
skeleton_.joints[jntIndex].parent;
299template <
typename T,
class Data,
size_t FuncDim,
size_t NumVec,
size_t NumPos>
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Vector< T, FuncDim > FuncType
virtual void evalFunction(size_t constrIndex, const JointStateT< T > &state, FuncType &f, std::array< VType, NumVec > &v, std::array< DfdvType, NumVec > &dfdv) const =0
Evaluate the constraint residual and derivatives for a single constraint.
JointErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Constructor.
size_t getJacobianSize() const final
The number of rows in the jacobian is the dimension of f, FuncDim, times the number of constraints.
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< VectorX< T > > gradient) final
The gradient of the error function: dl/dq = dl/d[f^2] * 2f * df/dv * dv/dq.
std::vector< Data > constraints_
List of constraints.
const GeneralizedLossT< T > loss_
The generalized loss function that transforms f^2.
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
Computes the error function value l = w * loss(f^2).
VectorX< T > jointGrad_
Intermediate storage of the gradient from this error function.
double getJacobian(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< MatrixX< T > > jacobian, Ref< VectorX< T > > residual, int &usedRows) final
For least-square problems, we assume l is the square of a vector function F.
const Skeleton & skeleton_
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
VectorX< bool > activeJointParams_
const ParameterTransform & parameterTransform_
ParameterSet enabledParameters_
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
bool isApprox(T l, T r, T eps=Eps< T >(1e-4, 1e-6)) noexcept
Determines if two values are approximately equal within a given tolerance.
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()
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.