Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skeleton_derivative.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
17
18#include <array>
19#include <limits>
20
21namespace momentum {
22
23template <typename T>
25 const Skeleton& skeleton,
26 const ParameterTransform& parameterTransform,
27 const VectorX<bool>& activeJointParams,
28 const ParameterSet& enabledParameters)
29 : skeleton_(skeleton),
30 parameterTransform_(parameterTransform),
31 activeJointParams_(activeJointParams),
32 enabledParameters_(enabledParameters) {}
33
34template <typename T>
35template <size_t FuncDim>
37 size_t jointIndex,
38 std::span<const Eigen::Vector3<T>> worldVecs,
39 std::span<const Eigen::Matrix<T, FuncDim, 3>> dfdvs,
40 std::span<const uint8_t> isPoints,
41 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
42 const JointStateListT<T>& jointStates,
43 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
44 MT_CHECK(worldVecs.size() == dfdvs.size());
45 MT_CHECK(worldVecs.size() == isPoints.size());
46
47 const size_t numVecs = worldVecs.size();
48 if (numVecs == 0) {
49 return;
50 }
51
52 // Check if all derivatives are zero
53 bool allZero = true;
54 for (size_t i = 0; i < numVecs; ++i) {
55 if (!dfdvs[i].isZero()) {
56 allZero = false;
57 break;
58 }
59 }
60 if (allZero) {
61 return;
62 }
63
64 // ---- Combined fast path for 1 JointPoint + N JointDirections ----
65 // When multiple contributions share the same joint (e.g. CameraProjection with
66 // 1 point + 3 directions), we combine them into a single hierarchy walk using
67 // the triple product identity: wR . (axis x offset) = axis . (offset x wR).
68 // Precompute C = pointWorldVec x pointWR + sum_dirs dirWorldVec[k] x dirWR[k],
69 // then at each joint j: rotVal = rotAxis[d] . (C + pointWR x t_j).
70 {
71 size_t pointIdx = SIZE_MAX;
72 bool canUseCombined = (numVecs > 1);
73 if (canUseCombined) {
74 for (size_t i = 0; i < numVecs; ++i) {
75 if (isPoints[i]) {
76 if (pointIdx != SIZE_MAX) {
77 canUseCombined = false;
78 break;
79 }
80 pointIdx = i;
81 }
82 }
83 if (pointIdx == SIZE_MAX) {
84 canUseCombined = false;
85 }
86 }
87
88 if (canUseCombined) {
89 const Eigen::Vector3<T> pointWR = dfdvs[pointIdx].transpose() * weightedResidual;
90
91 // C = pointWorldVec x pointWR + sum_dirs dirWorldVec[k] x dirWR[k]
92 Eigen::Vector3<T> C = worldVecs[pointIdx].cross(pointWR);
93 for (size_t i = 0; i < numVecs; ++i) {
94 if (i == pointIdx || dfdvs[i].isZero()) {
95 continue;
96 }
97 const Eigen::Vector3<T> dirWR = dfdvs[i].transpose() * weightedResidual;
98 C += worldVecs[i].cross(dirWR);
99 }
100
101 size_t jntIndex = jointIndex;
102 while (jntIndex != kInvalidIndex) {
103 MT_CHECK(jntIndex < skeleton_.joints.size());
104
105 const auto& jntState = jointStates[jntIndex];
106 const size_t jntParamIndex = jntIndex * kParametersPerJoint;
107
108 // Translation DOFs: only the JointPoint contributes
109 for (size_t d = 0; d < 3; ++d) {
110 if (!activeJointParams_[jntParamIndex + d]) {
111 continue;
112 }
113 const T val = pointWR.dot(jntState.getTranslationDerivative(d));
114 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d];
115 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d + 1];
116 ++index) {
117 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
118 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
119 val * parameterTransform_.transform.valuePtr()[index];
120 }
121 }
122 }
123
124 // Rotation DOFs: val = rotAxis[d] . (C + pointWR x t_j)
125 const Eigen::Vector3<T> Cj = C + pointWR.cross(jntState.translation());
126 for (size_t d = 0; d < 3; ++d) {
127 if (!activeJointParams_[jntParamIndex + 3 + d]) {
128 continue;
129 }
130 const T val = jntState.rotationAxis.col(d).dot(Cj);
131 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d];
132 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d + 1];
133 ++index) {
134 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
135 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
136 val * parameterTransform_.transform.valuePtr()[index];
137 }
138 }
139 }
140
141 // Scale DOF: only the JointPoint contributes
142 if (activeJointParams_[jntParamIndex + 6]) {
143 const Eigen::Vector3<T> offset = worldVecs[pointIdx] - jntState.translation();
144 const T val = pointWR.dot(jntState.getScaleDerivative(offset));
145 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6];
146 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6 + 1];
147 ++index) {
148 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
149 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
150 val * parameterTransform_.transform.valuePtr()[index];
151 }
152 }
153 }
154
155 jntIndex = skeleton_.joints[jntIndex].parent;
156 }
157 return;
158 }
159 }
160
161 // Walk the joint hierarchy from this joint to the root
162 size_t jntIndex = jointIndex;
163 while (jntIndex != kInvalidIndex) {
164 MT_CHECK(jntIndex < skeleton_.joints.size());
165
166 const auto& jntState = jointStates[jntIndex];
167 const size_t jntParamIndex = jntIndex * kParametersPerJoint;
168
169 for (size_t iVec = 0; iVec < numVecs; ++iVec) {
170 if (dfdvs[iVec].isZero()) {
171 continue;
172 }
173
174 // wR = dfdv^T * weightedResidual: converts matmul+dot to 3D dot
175 const Eigen::Vector3<T> wR = dfdvs[iVec].transpose() * weightedResidual;
176
177 // Compute offset for rotation/scale derivatives
178 // For points (w=1): offset = worldVec - translation
179 // For directions (w=0): offset = worldVec
180 Eigen::Vector3<T> offset;
181 if (isPoints[iVec]) {
182 offset.noalias() = worldVecs[iVec] - jntState.translation();
183 } else {
184 offset = worldVecs[iVec];
185 }
186
187 // Translational DOFs -- only affect points (w=1)
188 if (isPoints[iVec]) {
189 for (size_t d = 0; d < 3; ++d) {
190 if (!activeJointParams_[jntParamIndex + d]) {
191 continue;
192 }
193
194 const T val = wR.dot(jntState.getTranslationDerivative(d));
195
196 // Use the existing utility to map joint params to model params
197 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d];
198 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d + 1];
199 ++index) {
200 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
201 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
202 val * parameterTransform_.transform.valuePtr()[index];
203 }
204 }
205 }
206 }
207
208 // Rotational DOFs -- affect both points and directions
209 for (size_t d = 0; d < 3; ++d) {
210 if (!activeJointParams_[jntParamIndex + 3 + d]) {
211 continue;
212 }
213
214 const T val = wR.dot(jntState.getRotationDerivative(d, offset));
215
216 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d];
217 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d + 1];
218 ++index) {
219 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
220 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
221 val * parameterTransform_.transform.valuePtr()[index];
222 }
223 }
224 }
225
226 // Scale DOF -- only affects points (w=1)
227 if (isPoints[iVec]) {
228 if (activeJointParams_[jntParamIndex + 6]) {
229 const T val = wR.dot(jntState.getScaleDerivative(offset));
230
231 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6];
232 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6 + 1];
233 ++index) {
234 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
235 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
236 val * parameterTransform_.transform.valuePtr()[index];
237 }
238 }
239 }
240 }
241 }
242
243 // Move to parent joint
244 jntIndex = skeleton_.joints[jntIndex].parent;
245 }
246}
247
248template <typename T>
249template <size_t FuncDim>
251 size_t jointIndex,
252 std::span<const Eigen::Vector3<T>> worldVecs,
253 std::span<const Eigen::Matrix<T, FuncDim, 3>> dfdvs,
254 std::span<const uint8_t> isPoints,
255 T scale,
256 const JointStateListT<T>& jointStates,
257 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
258 size_t rowIndex) const {
259 MT_CHECK(worldVecs.size() == dfdvs.size());
260 MT_CHECK(worldVecs.size() == isPoints.size());
261
262 const size_t numVecs = worldVecs.size();
263 if (numVecs == 0) {
264 return;
265 }
266
267 // Check if all derivatives are zero
268 bool allZero = true;
269 for (size_t i = 0; i < numVecs; ++i) {
270 if (!dfdvs[i].isZero()) {
271 allZero = false;
272 break;
273 }
274 }
275 if (allZero) {
276 return;
277 }
278
279 // ---- Combined fast path for 1 JointPoint + N JointDirections ----
280 // Mirror of the gradient's combined path. For FuncDim > 1, the scalar triple-product
281 // identity doesn't apply directly. Instead, precompute a combined FuncDim×3 matrix:
282 // M_dirs = Σ_all scale * dfdv[i] * skew(worldVec[i])
283 // At each ancestor joint j, adjust for the point's per-joint offset:
284 // M_rot = M_dirs - scaledDfdvPoint * skew(t_j)
285 // jc[d] = M_rot * rotAxis[d]
286 // This reduces per-joint rotation cost from numVecs matmuls to 1 precompute + 3 matmuls.
287 {
288 size_t pointIdx = SIZE_MAX;
289 bool canUseCombined = (numVecs > 1);
290 if (canUseCombined) {
291 for (size_t i = 0; i < numVecs; ++i) {
292 if (isPoints[i]) {
293 if (pointIdx != SIZE_MAX) {
294 canUseCombined = false;
295 break;
296 }
297 pointIdx = i;
298 }
299 }
300 if (pointIdx == SIZE_MAX) {
301 canUseCombined = false;
302 }
303 }
304
305 if (canUseCombined) {
306 auto jac = jacobian.middleRows(rowIndex, FuncDim);
307
308 // Precompute scaled dfdv for the point (reused for translation and scale DOFs)
309 const Eigen::Matrix<T, FuncDim, 3> scaledDfdvPoint = scale * dfdvs[pointIdx];
310
311 // Helper: compute M * skew(v), i.e. the FuncDim×3 matrix such that
312 // (M * skew(v)) * a = M * (v × a) for any vector a.
313 auto matTimesSkew = [](const Eigen::Matrix<T, FuncDim, 3>& M,
314 const Eigen::Vector3<T>& v) -> Eigen::Matrix<T, FuncDim, 3> {
315 Eigen::Matrix<T, FuncDim, 3> result;
316 result.col(0) = M.col(1) * v[2] - M.col(2) * v[1];
317 result.col(1) = M.col(2) * v[0] - M.col(0) * v[2];
318 result.col(2) = M.col(0) * v[1] - M.col(1) * v[0];
319 return result;
320 };
321
322 // M_dirs_neg = -(Σ_all scale * dfdv[i] * skew(worldVec[i]))
323 // The negation comes from the identity: dfdv * (rotAxis × offset)
324 // = -(dfdv * skew(offset)) * rotAxis, since rotAxis × offset = -skew(offset) * rotAxis.
325 // By precomputing M_dirs_neg negated, the per-joint formula becomes:
326 // M_rot = M_dirs_neg + matTimesSkew(scaledDfdvPoint, t_j)
327 // = -(Σ scale*dfdv[i]*skew(offset_i))
328 // jc = M_rot * rotAxis[d]
329 Eigen::Matrix<T, FuncDim, 3> M_dirs_neg = -matTimesSkew(scaledDfdvPoint, worldVecs[pointIdx]);
330 for (size_t i = 0; i < numVecs; ++i) {
331 if (i == pointIdx || dfdvs[i].isZero()) {
332 continue;
333 }
334 M_dirs_neg.noalias() -= matTimesSkew(scale * dfdvs[i], worldVecs[i]);
335 }
336
337 size_t jntIndex = jointIndex;
338 while (jntIndex != kInvalidIndex) {
339 MT_CHECK(jntIndex < skeleton_.joints.size());
340
341 const auto& jntState = jointStates[jntIndex];
342 const size_t jntParamIndex = jntIndex * kParametersPerJoint;
343
344 // Translation DOFs: only the JointPoint contributes
345 for (size_t d = 0; d < 3; ++d) {
346 if (!activeJointParams_[jntParamIndex + d]) {
347 continue;
348 }
349 const Eigen::Matrix<T, FuncDim, 1> jc =
350 scaledDfdvPoint * jntState.getTranslationDerivative(d);
351 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d];
352 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d + 1];
353 ++index) {
354 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
355 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
356 jc * parameterTransform_.transform.valuePtr()[index];
357 }
358 }
359 }
360
361 // Rotation DOFs: use combined matrix
362 // M_rot = M_dirs_neg + matTimesSkew(scaledDfdvPoint, t_j)
363 // = -(Σ scale*dfdv[i]*skew(offset_i))
364 // where offset_point = worldVec_point - t_j, offset_dir_k = worldVec_k.
365 const Eigen::Matrix<T, FuncDim, 3> M_rot =
366 M_dirs_neg + matTimesSkew(scaledDfdvPoint, jntState.translation());
367 for (size_t d = 0; d < 3; ++d) {
368 if (!activeJointParams_[jntParamIndex + 3 + d]) {
369 continue;
370 }
371 const Eigen::Matrix<T, FuncDim, 1> jc = M_rot * jntState.rotationAxis.col(d);
372 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d];
373 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d + 1];
374 ++index) {
375 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
376 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
377 jc * parameterTransform_.transform.valuePtr()[index];
378 }
379 }
380 }
381
382 // Scale DOF: only the JointPoint contributes
383 if (activeJointParams_[jntParamIndex + 6]) {
384 const Eigen::Vector3<T> offset = worldVecs[pointIdx] - jntState.translation();
385 const Eigen::Matrix<T, FuncDim, 1> jc =
386 scaledDfdvPoint * jntState.getScaleDerivative(offset);
387 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6];
388 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6 + 1];
389 ++index) {
390 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
391 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
392 jc * parameterTransform_.transform.valuePtr()[index];
393 }
394 }
395 }
396
397 jntIndex = skeleton_.joints[jntIndex].parent;
398 }
399 return;
400 }
401 }
402
403 // Get the jacobian submatrix for this constraint
404 // Use runtime-sized middleRows to handle FuncDim=1 Eigen block type issues
405 auto jac = jacobian.middleRows(rowIndex, FuncDim);
406
407 // Walk the joint hierarchy from this joint to the root
408 size_t jntIndex = jointIndex;
409 while (jntIndex != kInvalidIndex) {
410 MT_CHECK(jntIndex < skeleton_.joints.size());
411
412 const auto& jntState = jointStates[jntIndex];
413 const size_t jntParamIndex = jntIndex * kParametersPerJoint;
414
415 for (size_t iVec = 0; iVec < numVecs; ++iVec) {
416 if (dfdvs[iVec].isZero()) {
417 continue;
418 }
419
420 // Compute offset for rotation/scale derivatives
421 Eigen::Vector3<T> offset;
422 if (isPoints[iVec]) {
423 offset.noalias() = worldVecs[iVec] - jntState.translation();
424 } else {
425 offset = worldVecs[iVec];
426 }
427
428 // Translational DOFs -- only affect points (w=1)
429 if (isPoints[iVec]) {
430 for (size_t d = 0; d < 3; ++d) {
431 if (!activeJointParams_[jntParamIndex + d]) {
432 continue;
433 }
434
435 // Compute jacobian contribution: scale * dfdv * derivative
436 const Eigen::Matrix<T, FuncDim, 1> jc =
437 scale * dfdvs[iVec] * jntState.getTranslationDerivative(d);
438
439 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d];
440 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + d + 1];
441 ++index) {
442 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
443 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
444 jc * parameterTransform_.transform.valuePtr()[index];
445 }
446 }
447 }
448 }
449
450 // Rotational DOFs -- affect both points and directions
451 for (size_t d = 0; d < 3; ++d) {
452 if (!activeJointParams_[jntParamIndex + 3 + d]) {
453 continue;
454 }
455
456 const Eigen::Matrix<T, FuncDim, 1> jc =
457 scale * dfdvs[iVec] * jntState.getRotationDerivative(d, offset);
458
459 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d];
460 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 3 + d + 1];
461 ++index) {
462 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
463 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
464 jc * parameterTransform_.transform.valuePtr()[index];
465 }
466 }
467 }
468
469 // Scale DOF -- only affects points (w=1)
470 if (isPoints[iVec]) {
471 if (activeJointParams_[jntParamIndex + 6]) {
472 const Eigen::Matrix<T, FuncDim, 1> jc =
473 scale * dfdvs[iVec] * jntState.getScaleDerivative(offset);
474
475 for (auto index = parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6];
476 index < parameterTransform_.transform.outerIndexPtr()[jntParamIndex + 6 + 1];
477 ++index) {
478 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
479 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
480 jc * parameterTransform_.transform.valuePtr()[index];
481 }
482 }
483 }
484 }
485 }
486
487 // Move to parent joint
488 jntIndex = skeleton_.joints[jntIndex].parent;
489 }
490}
491
492template <typename T>
493template <size_t FuncDim>
495 size_t vertexIndex,
496 const Eigen::Vector3<T>& /* worldPos */,
497 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
498 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
499 const SkeletonStateT<T>& skeletonState,
500 const MeshStateT<T>& meshState,
501 const Character& character,
502 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
503 // Skip if derivative is zero
504 if (dfdv.isZero()) {
505 return;
506 }
507
508 MT_CHECK(meshState.restMesh_ != nullptr);
509 MT_CHECK(character.skinWeights != nullptr);
510 MT_CHECK(!character.inverseBindPose.empty());
511
512 // Use SkinningWeightIterator to walk through influencing bones
513 SkinningWeightIteratorT<T> skinningIter(
514 character, *meshState.restMesh_, skeletonState, vertexIndex);
515
516 while (!skinningIter.finished()) {
517 size_t jointIndex = 0;
518 T boneWeight;
519 Eigen::Vector3<T> pos;
520 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
521
522 MT_CHECK(jointIndex < skeleton_.joints.size());
523
524 const auto& jointState = skeletonState.jointState[jointIndex];
525 const size_t paramIndex = jointIndex * kParametersPerJoint;
526 const Eigen::Vector3<T> offset = pos - jointState.translation();
527
528 // Translation DOFs
529 for (size_t d = 0; d < 3; ++d) {
530 if (!activeJointParams_[paramIndex + d]) {
531 continue;
532 }
533
534 const T val =
535 boneWeight * weightedResidual.dot(dfdv * jointState.getTranslationDerivative(d));
536
537 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
538 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
539 ++index) {
540 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
541 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
542 val * parameterTransform_.transform.valuePtr()[index];
543 }
544 }
545 }
546
547 // Rotation DOFs
548 for (size_t d = 0; d < 3; ++d) {
549 if (!activeJointParams_[paramIndex + 3 + d]) {
550 continue;
551 }
552
553 const T val =
554 boneWeight * weightedResidual.dot(dfdv * jointState.getRotationDerivative(d, offset));
555
556 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
557 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
558 ++index) {
559 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
560 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
561 val * parameterTransform_.transform.valuePtr()[index];
562 }
563 }
564 }
565
566 // Scale DOF
567 if (activeJointParams_[paramIndex + 6]) {
568 const T val = boneWeight * weightedResidual.dot(dfdv * jointState.getScaleDerivative(offset));
569
570 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
571 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
572 ++index) {
573 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
574 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
575 val * parameterTransform_.transform.valuePtr()[index];
576 }
577 }
578 }
579 }
580
581 // Handle blend shape parameters
582 if (character.blendShape) {
583 for (Eigen::Index iBlendShape = 0;
584 iBlendShape < parameterTransform_.blendShapeParameters.size();
585 ++iBlendShape) {
586 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
587 if (paramIdx < 0) {
588 continue;
589 }
590
591 const Eigen::Vector3<T> d_restPos =
592 character.blendShape->getShapeVectors()
593 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
594 .template cast<T>();
595
596 // Transform d_restPos through skinning
597 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
598 const auto& skinWeights = *character.skinWeights;
599 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
600 const auto w = skinWeights.weight(vertexIndex, i);
601 const auto parentBone = skinWeights.index(vertexIndex, i);
602 if (w > 0) {
603 d_worldPos += w *
604 (skeletonState.jointState[parentBone].transform.toLinear() *
605 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
606 }
607 }
608
609 gradient[paramIdx] += weightedResidual.dot(dfdv * d_worldPos);
610 }
611 }
612
613 // Handle face expression blend shape parameters
614 if (character.faceExpressionBlendShape) {
615 for (Eigen::Index iBlendShape = 0;
616 iBlendShape < parameterTransform_.faceExpressionParameters.size();
617 ++iBlendShape) {
618 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
619 if (paramIdx < 0) {
620 continue;
621 }
622
623 const Eigen::Vector3<T> d_restPos =
624 character.faceExpressionBlendShape->getShapeVectors()
625 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
626 .template cast<T>();
627
628 // Transform d_restPos through skinning
629 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
630 const auto& skinWeights = *character.skinWeights;
631 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
632 const auto w = skinWeights.weight(vertexIndex, i);
633 const auto parentBone = skinWeights.index(vertexIndex, i);
634 if (w > 0) {
635 d_worldPos += w *
636 (skeletonState.jointState[parentBone].transform.toLinear() *
637 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
638 }
639 }
640
641 gradient[paramIdx] += weightedResidual.dot(dfdv * d_worldPos);
642 }
643 }
644}
645
646template <typename T>
647template <size_t FuncDim>
649 size_t vertexIndex,
650 const Eigen::Vector3<T>& /* worldPos */,
651 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
652 T scale,
653 const SkeletonStateT<T>& skeletonState,
654 const MeshStateT<T>& meshState,
655 const Character& character,
656 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
657 size_t rowIndex) const {
658 // Skip if derivative is zero
659 if (dfdv.isZero()) {
660 return;
661 }
662
663 MT_CHECK(meshState.restMesh_ != nullptr);
664 MT_CHECK(character.skinWeights != nullptr);
665 MT_CHECK(!character.inverseBindPose.empty());
666
667 // Get the jacobian submatrix for this constraint
668 auto jac = jacobian.middleRows(rowIndex, FuncDim);
669
670 // Use SkinningWeightIterator to walk through influencing bones
671 SkinningWeightIteratorT<T> skinningIter(
672 character, *meshState.restMesh_, skeletonState, vertexIndex);
673
674 while (!skinningIter.finished()) {
675 size_t jointIndex = 0;
676 T boneWeight;
677 Eigen::Vector3<T> pos;
678 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
679
680 MT_CHECK(jointIndex < skeleton_.joints.size());
681
682 const auto& jointState = skeletonState.jointState[jointIndex];
683 const size_t paramIndex = jointIndex * kParametersPerJoint;
684 const Eigen::Vector3<T> offset = pos - jointState.translation();
685 const T scaledBoneWeight = scale * boneWeight;
686
687 // Translation DOFs
688 for (size_t d = 0; d < 3; ++d) {
689 if (!activeJointParams_[paramIndex + d]) {
690 continue;
691 }
692
693 const Eigen::Matrix<T, FuncDim, 1> jc =
694 scaledBoneWeight * dfdv * jointState.getTranslationDerivative(d);
695
696 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
697 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
698 ++index) {
699 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
700 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
701 jc * parameterTransform_.transform.valuePtr()[index];
702 }
703 }
704 }
705
706 // Rotation DOFs
707 for (size_t d = 0; d < 3; ++d) {
708 if (!activeJointParams_[paramIndex + 3 + d]) {
709 continue;
710 }
711
712 const Eigen::Matrix<T, FuncDim, 1> jc =
713 scaledBoneWeight * dfdv * jointState.getRotationDerivative(d, offset);
714
715 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
716 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
717 ++index) {
718 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
719 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
720 jc * parameterTransform_.transform.valuePtr()[index];
721 }
722 }
723 }
724
725 // Scale DOF
726 if (activeJointParams_[paramIndex + 6]) {
727 const Eigen::Matrix<T, FuncDim, 1> jc =
728 scaledBoneWeight * dfdv * jointState.getScaleDerivative(offset);
729
730 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
731 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
732 ++index) {
733 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
734 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
735 jc * parameterTransform_.transform.valuePtr()[index];
736 }
737 }
738 }
739 }
740
741 // Handle blend shape parameters
742 if (character.blendShape) {
743 for (Eigen::Index iBlendShape = 0;
744 iBlendShape < parameterTransform_.blendShapeParameters.size();
745 ++iBlendShape) {
746 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
747 if (paramIdx < 0) {
748 continue;
749 }
750
751 const Eigen::Vector3<T> d_restPos =
752 character.blendShape->getShapeVectors()
753 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
754 .template cast<T>();
755
756 // Transform d_restPos through skinning
757 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
758 const auto& skinWeights = *character.skinWeights;
759 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
760 const auto w = skinWeights.weight(vertexIndex, i);
761 const auto parentBone = skinWeights.index(vertexIndex, i);
762 if (w > 0) {
763 d_worldPos += w *
764 (skeletonState.jointState[parentBone].transform.toLinear() *
765 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
766 }
767 }
768
769 jac.col(paramIdx).noalias() += scale * dfdv * d_worldPos;
770 }
771 }
772
773 // Handle face expression blend shape parameters
774 if (character.faceExpressionBlendShape) {
775 for (Eigen::Index iBlendShape = 0;
776 iBlendShape < parameterTransform_.faceExpressionParameters.size();
777 ++iBlendShape) {
778 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
779 if (paramIdx < 0) {
780 continue;
781 }
782
783 const Eigen::Vector3<T> d_restPos =
784 character.faceExpressionBlendShape->getShapeVectors()
785 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
786 .template cast<T>();
787
788 // Transform d_restPos through skinning
789 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
790 const auto& skinWeights = *character.skinWeights;
791 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
792 const auto w = skinWeights.weight(vertexIndex, i);
793 const auto parentBone = skinWeights.index(vertexIndex, i);
794 if (w > 0) {
795 d_worldPos += w *
796 (skeletonState.jointState[parentBone].transform.toLinear() *
797 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
798 }
799 }
800
801 jac.col(paramIdx).noalias() += scale * dfdv * d_worldPos;
802 }
803 }
804}
805
806template <typename T>
808 size_t vertexIndex,
809 const Eigen::Vector3<T>& weightedResidual,
810 const SkeletonStateT<T>& skeletonState,
811 const MeshStateT<T>& meshState,
812 const Character& character,
813 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
814 MT_CHECK(meshState.restMesh_ != nullptr);
815 MT_CHECK(character.skinWeights != nullptr);
816 MT_CHECK(!character.inverseBindPose.empty());
817
818 SkinningWeightIteratorT<T> skinningIter(
819 character, *meshState.restMesh_, skeletonState, vertexIndex);
820
821 while (!skinningIter.finished()) {
822 size_t jointIndex = 0;
823 T boneWeight;
824 Eigen::Vector3<T> pos;
825 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
826
827 MT_CHECK(jointIndex < skeleton_.joints.size());
828
829 const auto& jointState = skeletonState.jointState[jointIndex];
830 const size_t paramIndex = jointIndex * kParametersPerJoint;
831 const Eigen::Vector3<T> offset = pos - jointState.translation();
832
833 // Translation DOFs (dfdv=I eliminates matrix multiply)
834 for (size_t d = 0; d < 3; ++d) {
835 if (!activeJointParams_[paramIndex + d]) {
836 continue;
837 }
838
839 const T val = boneWeight * weightedResidual.dot(jointState.getTranslationDerivative(d));
840
841 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
842 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
843 ++index) {
844 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
845 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
846 val * parameterTransform_.transform.valuePtr()[index];
847 }
848 }
849 }
850
851 // Rotation DOFs (dfdv=I eliminates matrix multiply)
852 for (size_t d = 0; d < 3; ++d) {
853 if (!activeJointParams_[paramIndex + 3 + d]) {
854 continue;
855 }
856
857 const T val = boneWeight * weightedResidual.dot(jointState.getRotationDerivative(d, offset));
858
859 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
860 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
861 ++index) {
862 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
863 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
864 val * parameterTransform_.transform.valuePtr()[index];
865 }
866 }
867 }
868
869 // Scale DOF (dfdv=I eliminates matrix multiply)
870 if (activeJointParams_[paramIndex + 6]) {
871 const T val = boneWeight * weightedResidual.dot(jointState.getScaleDerivative(offset));
872
873 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
874 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
875 ++index) {
876 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
877 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
878 val * parameterTransform_.transform.valuePtr()[index];
879 }
880 }
881 }
882 }
883
884 // Handle blend shape parameters (dfdv=I eliminates matrix multiply)
885 if (character.blendShape) {
886 for (Eigen::Index iBlendShape = 0;
887 iBlendShape < parameterTransform_.blendShapeParameters.size();
888 ++iBlendShape) {
889 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
890 if (paramIdx < 0) {
891 continue;
892 }
893
894 const Eigen::Vector3<T> d_restPos =
895 character.blendShape->getShapeVectors()
896 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
897 .template cast<T>();
898
899 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
900 const auto& skinWeights = *character.skinWeights;
901 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
902 const auto w = skinWeights.weight(vertexIndex, i);
903 const auto parentBone = skinWeights.index(vertexIndex, i);
904 if (w > 0) {
905 d_worldPos += w *
906 (skeletonState.jointState[parentBone].transform.toLinear() *
907 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
908 }
909 }
910
911 gradient[paramIdx] += weightedResidual.dot(d_worldPos);
912 }
913 }
914
915 // Handle face expression blend shape parameters (dfdv=I eliminates matrix multiply)
916 if (character.faceExpressionBlendShape) {
917 for (Eigen::Index iBlendShape = 0;
918 iBlendShape < parameterTransform_.faceExpressionParameters.size();
919 ++iBlendShape) {
920 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
921 if (paramIdx < 0) {
922 continue;
923 }
924
925 const Eigen::Vector3<T> d_restPos =
926 character.faceExpressionBlendShape->getShapeVectors()
927 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
928 .template cast<T>();
929
930 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
931 const auto& skinWeights = *character.skinWeights;
932 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
933 const auto w = skinWeights.weight(vertexIndex, i);
934 const auto parentBone = skinWeights.index(vertexIndex, i);
935 if (w > 0) {
936 d_worldPos += w *
937 (skeletonState.jointState[parentBone].transform.toLinear() *
938 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
939 }
940 }
941
942 gradient[paramIdx] += weightedResidual.dot(d_worldPos);
943 }
944 }
945}
946
947template <typename T>
949 size_t vertexIndex,
950 T scale,
951 const SkeletonStateT<T>& skeletonState,
952 const MeshStateT<T>& meshState,
953 const Character& character,
954 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
955 size_t rowIndex) const {
956 MT_CHECK(meshState.restMesh_ != nullptr);
957 MT_CHECK(character.skinWeights != nullptr);
958 MT_CHECK(!character.inverseBindPose.empty());
959
960 constexpr size_t FuncDim = 3;
961 auto jac = jacobian.middleRows(rowIndex, FuncDim);
962
963 SkinningWeightIteratorT<T> skinningIter(
964 character, *meshState.restMesh_, skeletonState, vertexIndex);
965
966 while (!skinningIter.finished()) {
967 size_t jointIndex = 0;
968 T boneWeight;
969 Eigen::Vector3<T> pos;
970 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
971
972 MT_CHECK(jointIndex < skeleton_.joints.size());
973
974 const auto& jointState = skeletonState.jointState[jointIndex];
975 const size_t paramIndex = jointIndex * kParametersPerJoint;
976 const Eigen::Vector3<T> offset = pos - jointState.translation();
977 const T scaledBoneWeight = scale * boneWeight;
978
979 // Translation DOFs (dfdv=I eliminates matrix multiply)
980 for (size_t d = 0; d < 3; ++d) {
981 if (!activeJointParams_[paramIndex + d]) {
982 continue;
983 }
984
985 const Eigen::Vector3<T> jc = scaledBoneWeight * jointState.getTranslationDerivative(d);
986
987 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
988 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
989 ++index) {
990 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
991 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
992 jc * parameterTransform_.transform.valuePtr()[index];
993 }
994 }
995 }
996
997 // Rotation DOFs (dfdv=I eliminates matrix multiply)
998 for (size_t d = 0; d < 3; ++d) {
999 if (!activeJointParams_[paramIndex + 3 + d]) {
1000 continue;
1001 }
1002
1003 const Eigen::Vector3<T> jc = scaledBoneWeight * jointState.getRotationDerivative(d, offset);
1004
1005 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
1006 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
1007 ++index) {
1008 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1009 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
1010 jc * parameterTransform_.transform.valuePtr()[index];
1011 }
1012 }
1013 }
1014
1015 // Scale DOF (dfdv=I eliminates matrix multiply)
1016 if (activeJointParams_[paramIndex + 6]) {
1017 const Eigen::Vector3<T> jc = scaledBoneWeight * jointState.getScaleDerivative(offset);
1018
1019 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
1020 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
1021 ++index) {
1022 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1023 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
1024 jc * parameterTransform_.transform.valuePtr()[index];
1025 }
1026 }
1027 }
1028 }
1029
1030 // Handle blend shape parameters (dfdv=I eliminates matrix multiply)
1031 if (character.blendShape) {
1032 for (Eigen::Index iBlendShape = 0;
1033 iBlendShape < parameterTransform_.blendShapeParameters.size();
1034 ++iBlendShape) {
1035 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
1036 if (paramIdx < 0) {
1037 continue;
1038 }
1039
1040 const Eigen::Vector3<T> d_restPos =
1041 character.blendShape->getShapeVectors()
1042 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
1043 .template cast<T>();
1044
1045 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
1046 const auto& skinWeights = *character.skinWeights;
1047 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
1048 const auto w = skinWeights.weight(vertexIndex, i);
1049 const auto parentBone = skinWeights.index(vertexIndex, i);
1050 if (w > 0) {
1051 d_worldPos += w *
1052 (skeletonState.jointState[parentBone].transform.toLinear() *
1053 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
1054 }
1055 }
1056
1057 jac.col(paramIdx).noalias() += scale * d_worldPos;
1058 }
1059 }
1060
1061 // Handle face expression blend shape parameters (dfdv=I eliminates matrix multiply)
1062 if (character.faceExpressionBlendShape) {
1063 for (Eigen::Index iBlendShape = 0;
1064 iBlendShape < parameterTransform_.faceExpressionParameters.size();
1065 ++iBlendShape) {
1066 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
1067 if (paramIdx < 0) {
1068 continue;
1069 }
1070
1071 const Eigen::Vector3<T> d_restPos =
1072 character.faceExpressionBlendShape->getShapeVectors()
1073 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
1074 .template cast<T>();
1075
1076 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
1077 const auto& skinWeights = *character.skinWeights;
1078 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
1079 const auto w = skinWeights.weight(vertexIndex, i);
1080 const auto parentBone = skinWeights.index(vertexIndex, i);
1081 if (w > 0) {
1082 d_worldPos += w *
1083 (skeletonState.jointState[parentBone].transform.toLinear() *
1084 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
1085 }
1086 }
1087
1088 jac.col(paramIdx).noalias() += scale * d_worldPos;
1089 }
1090 }
1091}
1092
1093// Explicit template instantiations for float
1094template class SkeletonDerivativeT<float>;
1096 size_t,
1097 std::span<const Eigen::Vector3<float>>,
1098 std::span<const Eigen::Matrix<float, 1, 3>>,
1099 std::span<const uint8_t>,
1100 const Eigen::Matrix<float, 1, 1>&,
1102 Eigen::Ref<Eigen::VectorX<float>>) const;
1104 size_t,
1105 std::span<const Eigen::Vector3<float>>,
1106 std::span<const Eigen::Matrix<float, 2, 3>>,
1107 std::span<const uint8_t>,
1108 const Eigen::Matrix<float, 2, 1>&,
1110 Eigen::Ref<Eigen::VectorX<float>>) const;
1112 size_t,
1113 std::span<const Eigen::Vector3<float>>,
1114 std::span<const Eigen::Matrix<float, 3, 3>>,
1115 std::span<const uint8_t>,
1116 const Eigen::Matrix<float, 3, 1>&,
1118 Eigen::Ref<Eigen::VectorX<float>>) const;
1120 size_t,
1121 std::span<const Eigen::Vector3<float>>,
1122 std::span<const Eigen::Matrix<float, 9, 3>>,
1123 std::span<const uint8_t>,
1124 const Eigen::Matrix<float, 9, 1>&,
1126 Eigen::Ref<Eigen::VectorX<float>>) const;
1127
1129 size_t,
1130 std::span<const Eigen::Vector3<float>>,
1131 std::span<const Eigen::Matrix<float, 1, 3>>,
1132 std::span<const uint8_t>,
1133 float,
1135 Eigen::Ref<Eigen::MatrixX<float>>,
1136 size_t) const;
1138 size_t,
1139 std::span<const Eigen::Vector3<float>>,
1140 std::span<const Eigen::Matrix<float, 2, 3>>,
1141 std::span<const uint8_t>,
1142 float,
1144 Eigen::Ref<Eigen::MatrixX<float>>,
1145 size_t) const;
1147 size_t,
1148 std::span<const Eigen::Vector3<float>>,
1149 std::span<const Eigen::Matrix<float, 3, 3>>,
1150 std::span<const uint8_t>,
1151 float,
1153 Eigen::Ref<Eigen::MatrixX<float>>,
1154 size_t) const;
1156 size_t,
1157 std::span<const Eigen::Vector3<float>>,
1158 std::span<const Eigen::Matrix<float, 9, 3>>,
1159 std::span<const uint8_t>,
1160 float,
1162 Eigen::Ref<Eigen::MatrixX<float>>,
1163 size_t) const;
1164
1165// Explicit template instantiations for double
1166template class SkeletonDerivativeT<double>;
1168 size_t,
1169 std::span<const Eigen::Vector3<double>>,
1170 std::span<const Eigen::Matrix<double, 1, 3>>,
1171 std::span<const uint8_t>,
1172 const Eigen::Matrix<double, 1, 1>&,
1174 Eigen::Ref<Eigen::VectorX<double>>) const;
1176 size_t,
1177 std::span<const Eigen::Vector3<double>>,
1178 std::span<const Eigen::Matrix<double, 2, 3>>,
1179 std::span<const uint8_t>,
1180 const Eigen::Matrix<double, 2, 1>&,
1182 Eigen::Ref<Eigen::VectorX<double>>) const;
1184 size_t,
1185 std::span<const Eigen::Vector3<double>>,
1186 std::span<const Eigen::Matrix<double, 3, 3>>,
1187 std::span<const uint8_t>,
1188 const Eigen::Matrix<double, 3, 1>&,
1190 Eigen::Ref<Eigen::VectorX<double>>) const;
1192 size_t,
1193 std::span<const Eigen::Vector3<double>>,
1194 std::span<const Eigen::Matrix<double, 9, 3>>,
1195 std::span<const uint8_t>,
1196 const Eigen::Matrix<double, 9, 1>&,
1198 Eigen::Ref<Eigen::VectorX<double>>) const;
1199
1201 size_t,
1202 std::span<const Eigen::Vector3<double>>,
1203 std::span<const Eigen::Matrix<double, 1, 3>>,
1204 std::span<const uint8_t>,
1205 double,
1207 Eigen::Ref<Eigen::MatrixX<double>>,
1208 size_t) const;
1210 size_t,
1211 std::span<const Eigen::Vector3<double>>,
1212 std::span<const Eigen::Matrix<double, 2, 3>>,
1213 std::span<const uint8_t>,
1214 double,
1216 Eigen::Ref<Eigen::MatrixX<double>>,
1217 size_t) const;
1219 size_t,
1220 std::span<const Eigen::Vector3<double>>,
1221 std::span<const Eigen::Matrix<double, 3, 3>>,
1222 std::span<const uint8_t>,
1223 double,
1225 Eigen::Ref<Eigen::MatrixX<double>>,
1226 size_t) const;
1228 size_t,
1229 std::span<const Eigen::Vector3<double>>,
1230 std::span<const Eigen::Matrix<double, 9, 3>>,
1231 std::span<const uint8_t>,
1232 double,
1234 Eigen::Ref<Eigen::MatrixX<double>>,
1235 size_t) const;
1236
1237// Explicit template instantiations for accumulateVertexGradient (float)
1238
1239// accumulateVertexNormalCorrectionGradient implementation
1240template <typename T>
1242 size_t vertexIndex,
1243 const Eigen::Vector3<T>& sourceNormal,
1244 const Eigen::Vector3<T>& targetPosition,
1245 T scale,
1246 const SkeletonStateT<T>& skeletonState,
1247 const MeshStateT<T>& meshState,
1248 const Character& character,
1249 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
1250 // This computes the CORRECTION to the standard vertex gradient for
1251 // the source mesh normal rotation. The standard accumulateVertexGradient
1252 // treats the mixed normal as constant, but the sourceNormal rotates with
1253 // each bone. This correction fixes the rotation derivative.
1254 //
1255 // Formula: For each bone (jointIndex, boneWeight, pos):
1256 // cross = (targetPosition - pos) × sourceNormal
1257 // correction += boneWeight * scale * rotationAxis[d] · cross
1258 //
1259 // WHERE pos is the skinned rest position from SkinningWeightIterator,
1260 // NOT jointState.translation(). See VERTEX_REFACTORING_PLAN.md for
1261 // the complete derivation using scalar triple product identity.
1262 MT_CHECK(meshState.restMesh_ != nullptr);
1263 MT_CHECK(character.skinWeights != nullptr);
1264 MT_CHECK(!character.inverseBindPose.empty());
1265
1266 SkinningWeightIteratorT<T> skinningIter(
1267 character, *meshState.restMesh_, skeletonState, vertexIndex);
1268
1269 while (!skinningIter.finished()) {
1270 auto [jointIndex, boneWeight, pos] = skinningIter.next();
1271 MT_CHECK(jointIndex < skeleton_.joints.size());
1272
1273 const auto& jointState = skeletonState.jointState[jointIndex];
1274 const size_t paramIndex = jointIndex * kParametersPerJoint;
1275
1276 // CRITICAL: use (targetPosition - pos), NOT (targetPosition - jointTranslation)
1277 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
1278
1279 for (size_t d = 0; d < 3; ++d) {
1280 if (!activeJointParams_[paramIndex + 3 + d]) {
1281 continue;
1282 }
1283 const T val = boneWeight * scale * jointState.rotationAxis.col(d).dot(cross);
1284 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
1285 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
1286 ++index) {
1287 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1288 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
1289 val * parameterTransform_.transform.valuePtr()[index];
1290 }
1291 }
1292 }
1293 }
1294}
1295
1296// accumulateVertexNormalCorrectionJacobian implementation
1297template <typename T>
1299 size_t vertexIndex,
1300 const Eigen::Vector3<T>& sourceNormal,
1301 const Eigen::Vector3<T>& targetPosition,
1302 T scale,
1303 const SkeletonStateT<T>& skeletonState,
1304 const MeshStateT<T>& meshState,
1305 const Character& character,
1306 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
1307 size_t rowIndex) const {
1308 MT_CHECK(meshState.restMesh_ != nullptr);
1309 MT_CHECK(character.skinWeights != nullptr);
1310 MT_CHECK(!character.inverseBindPose.empty());
1311
1312 SkinningWeightIteratorT<T> skinningIter(
1313 character, *meshState.restMesh_, skeletonState, vertexIndex);
1314
1315 while (!skinningIter.finished()) {
1316 auto [jointIndex, boneWeight, pos] = skinningIter.next();
1317 MT_CHECK(jointIndex < skeleton_.joints.size());
1318
1319 const auto& jointState = skeletonState.jointState[jointIndex];
1320 const size_t paramIndex = jointIndex * kParametersPerJoint;
1321
1322 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
1323
1324 for (size_t d = 0; d < 3; ++d) {
1325 if (!activeJointParams_[paramIndex + 3 + d]) {
1326 continue;
1327 }
1328 const T val = boneWeight * scale * jointState.rotationAxis.col(d).dot(cross);
1329 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
1330 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
1331 ++index) {
1332 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1333 jacobian(rowIndex, parameterTransform_.transform.innerIndexPtr()[index]) +=
1334 val * parameterTransform_.transform.valuePtr()[index];
1335 }
1336 }
1337 }
1338 }
1339}
1340
1341// accumulateVertexNormalBlendShapeGradient implementation
1342template <typename T>
1344 size_t vertexIndex,
1345 const Eigen::Vector3<T>& leverArm,
1346 T scale,
1347 const SkeletonStateT<T>& skeletonState,
1348 const MeshStateT<T>& meshState,
1349 const Character& character,
1350 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
1351 if (!character.blendShape && !character.faceExpressionBlendShape) {
1352 return;
1353 }
1354
1355 MT_CHECK(meshState.posedMesh_ != nullptr);
1356 MT_CHECK(character.skinWeights != nullptr);
1357
1358 const auto& faces = meshState.posedMesh_->faces;
1359
1360 // Find faces adjacent to this vertex via linear scan (fallback path)
1361 std::vector<size_t> adjacentFaces;
1362 for (size_t fIdx = 0; fIdx < faces.size(); ++fIdx) {
1363 const auto& face = faces[fIdx];
1364 if (face[0] == static_cast<int>(vertexIndex) || face[1] == static_cast<int>(vertexIndex) ||
1365 face[2] == static_cast<int>(vertexIndex)) {
1366 adjacentFaces.push_back(fIdx);
1367 }
1368 }
1369
1371 vertexIndex, leverArm, scale, adjacentFaces, skeletonState, meshState, character, gradient);
1372}
1373
1374// accumulateVertexNormalBlendShapeGradient with pre-built adjacency
1375template <typename T>
1377 size_t vertexIndex,
1378 const Eigen::Vector3<T>& leverArm,
1379 T scale,
1380 std::span<const size_t> adjacentFaces,
1381 const SkeletonStateT<T>& skeletonState,
1382 const MeshStateT<T>& meshState,
1383 const Character& character,
1384 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
1385 if (!character.blendShape && !character.faceExpressionBlendShape) {
1386 return;
1387 }
1388
1389 MT_CHECK(meshState.posedMesh_ != nullptr);
1390 MT_CHECK(meshState.restMesh_ != nullptr);
1391 MT_CHECK(character.skinWeights != nullptr);
1392 MT_CHECK(!character.inverseBindPose.empty());
1393
1394 const auto& faces = meshState.posedMesh_->faces;
1395 const auto& restVertices = meshState.restMesh_->vertices;
1396
1397 if (adjacentFaces.empty()) {
1398 return;
1399 }
1400
1401 // The forward path computes posed normals as:
1402 // restNormal = normalize(Σ_f restFaceNormal_f) [updateNormals on rest mesh]
1403 // posedNormal = normalize(M_v * restNormal) [applySSD skins + normalizes]
1404 //
1405 // Since normalize(M_v * normalize(s)) = normalize(M_v * s), the derivative simplifies to:
1406 // d(posedNormal)/d(s) = [(I - n̂*n̂^T) / |M_v * s|] * M_v
1407 // where s = restSum = unnormalized sum of rest face normals, n̂ = posedNormal.
1408
1409 // Step 1: Compute rest-space edge vectors and unnormalized sum of face normals
1410 Eigen::Vector3<T> restSum = Eigen::Vector3<T>::Zero();
1411 struct FaceEdges {
1412 Eigen::Vector3<T> e1;
1413 Eigen::Vector3<T> e2;
1414 Eigen::Vector3i indices;
1415 };
1416 std::vector<FaceEdges> faceEdges(adjacentFaces.size());
1417
1418 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1419 const auto& face = faces[adjacentFaces[i]];
1420 const Eigen::Vector3<T> v0 = restVertices[face[0]].template cast<T>();
1421 const Eigen::Vector3<T> v1 = restVertices[face[1]].template cast<T>();
1422 const Eigen::Vector3<T> v2 = restVertices[face[2]].template cast<T>();
1423 faceEdges[i].e1 = v1 - v0;
1424 faceEdges[i].e2 = v2 - v0;
1425 faceEdges[i].indices = face;
1426 restSum += faceEdges[i].e1.cross(faceEdges[i].e2);
1427 }
1428
1429 if (restSum.squaredNorm() < std::numeric_limits<T>::epsilon()) {
1430 return;
1431 }
1432
1433 // Step 2: Compute skinning rotation M_v for the constraint vertex
1434 const auto& skinWeights = *character.skinWeights;
1435 Eigen::Matrix3<T> Mv;
1436 Mv.setZero();
1437 for (uint32_t jWeight = 0; jWeight < kMaxSkinJoints; ++jWeight) {
1438 const auto w = skinWeights.weight(vertexIndex, jWeight);
1439 if (w > T(0)) {
1440 const auto parentBone = skinWeights.index(vertexIndex, jWeight);
1441 Mv.noalias() += w *
1442 (skeletonState.jointState[parentBone].transform.toLinear() *
1443 character.inverseBindPose[parentBone].linear().template cast<T>());
1444 }
1445 }
1446
1447 // Step 3: Compute posed unnormalized = M_v * restSum and normalization derivative
1448 const Eigen::Vector3<T> posedUnnorm = Mv * restSum;
1449 const T posedNorm = posedUnnorm.norm();
1450 if (posedNorm < T(1e-12)) {
1451 return;
1452 }
1453 const Eigen::Vector3<T> posedNhat = posedUnnorm / posedNorm;
1454
1455 // d(posedNormal)/d(restSum) = [(I - n̂*n̂^T) / |M_v * restSum|] * M_v
1456 const Eigen::Matrix3<T> dnormMv =
1457 ((Eigen::Matrix3<T>::Identity() - posedNhat * posedNhat.transpose()) / posedNorm) * Mv;
1458
1459 // Process standard blend shapes
1460 if (character.blendShape) {
1461 const auto& shapeVectors = character.blendShape->getShapeVectors();
1462 for (Eigen::Index iBS = 0; iBS < parameterTransform_.blendShapeParameters.size(); ++iBS) {
1463 const auto paramIdx = parameterTransform_.blendShapeParameters[iBS];
1464 if (paramIdx < 0) {
1465 continue;
1466 }
1467
1468 Eigen::Vector3<T> dRestSumDBs = Eigen::Vector3<T>::Zero();
1469
1470 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1471 const auto& fe = faceEdges[i];
1472
1473 // In rest space, d(restVertex)/d(bsWeight) = shapeVector (no skinning)
1474 const Eigen::Vector3<T> sv0 =
1475 shapeVectors.template block<3, 1>(3 * fe.indices[0], iBS, 3, 1).template cast<T>();
1476 const Eigen::Vector3<T> sv1 =
1477 shapeVectors.template block<3, 1>(3 * fe.indices[1], iBS, 3, 1).template cast<T>();
1478 const Eigen::Vector3<T> sv2 =
1479 shapeVectors.template block<3, 1>(3 * fe.indices[2], iBS, 3, 1).template cast<T>();
1480
1481 const Eigen::Vector3<T> de1 = sv1 - sv0;
1482 const Eigen::Vector3<T> de2 = sv2 - sv0;
1483
1484 // d(restFaceNormal)/d(bs) = de1 × e2 + e1 × de2
1485 dRestSumDBs += de1.cross(fe.e2) + fe.e1.cross(de2);
1486 }
1487
1488 const Eigen::Vector3<T> dNormalDBs = dnormMv * dRestSumDBs;
1489 gradient[paramIdx] += scale * leverArm.dot(dNormalDBs);
1490 }
1491 }
1492
1493 // Process face expression blend shapes
1494 if (character.faceExpressionBlendShape) {
1495 const auto& shapeVectors = character.faceExpressionBlendShape->getShapeVectors();
1496 for (Eigen::Index iBS = 0; iBS < parameterTransform_.faceExpressionParameters.size(); ++iBS) {
1497 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBS];
1498 if (paramIdx < 0) {
1499 continue;
1500 }
1501
1502 Eigen::Vector3<T> dRestSumDBs = Eigen::Vector3<T>::Zero();
1503
1504 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1505 const auto& fe = faceEdges[i];
1506
1507 // In rest space, d(restVertex)/d(bsWeight) = shapeVector (no skinning)
1508 const Eigen::Vector3<T> sv0 =
1509 shapeVectors.template block<3, 1>(3 * fe.indices[0], iBS, 3, 1).template cast<T>();
1510 const Eigen::Vector3<T> sv1 =
1511 shapeVectors.template block<3, 1>(3 * fe.indices[1], iBS, 3, 1).template cast<T>();
1512 const Eigen::Vector3<T> sv2 =
1513 shapeVectors.template block<3, 1>(3 * fe.indices[2], iBS, 3, 1).template cast<T>();
1514
1515 const Eigen::Vector3<T> de1 = sv1 - sv0;
1516 const Eigen::Vector3<T> de2 = sv2 - sv0;
1517
1518 dRestSumDBs += de1.cross(fe.e2) + fe.e1.cross(de2);
1519 }
1520
1521 const Eigen::Vector3<T> dNormalDBs = dnormMv * dRestSumDBs;
1522 gradient[paramIdx] += scale * leverArm.dot(dNormalDBs);
1523 }
1524 }
1525}
1526
1527// accumulateVertexNormalBlendShapeJacobian implementation
1528template <typename T>
1530 size_t vertexIndex,
1531 const Eigen::Vector3<T>& leverArm,
1532 T scale,
1533 const SkeletonStateT<T>& skeletonState,
1534 const MeshStateT<T>& meshState,
1535 const Character& character,
1536 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
1537 size_t rowIndex) const {
1538 if (!character.blendShape && !character.faceExpressionBlendShape) {
1539 return;
1540 }
1541
1542 MT_CHECK(meshState.posedMesh_ != nullptr);
1543 MT_CHECK(character.skinWeights != nullptr);
1544
1545 const auto& faces = meshState.posedMesh_->faces;
1546
1547 // Find faces adjacent to this vertex via linear scan (fallback path)
1548 std::vector<size_t> adjacentFaces;
1549 for (size_t fIdx = 0; fIdx < faces.size(); ++fIdx) {
1550 const auto& face = faces[fIdx];
1551 if (face[0] == static_cast<int>(vertexIndex) || face[1] == static_cast<int>(vertexIndex) ||
1552 face[2] == static_cast<int>(vertexIndex)) {
1553 adjacentFaces.push_back(fIdx);
1554 }
1555 }
1556
1558 vertexIndex,
1559 leverArm,
1560 scale,
1561 adjacentFaces,
1562 skeletonState,
1563 meshState,
1564 character,
1565 jacobian,
1566 rowIndex);
1567}
1568
1569// accumulateVertexNormalBlendShapeJacobian with pre-built adjacency
1570template <typename T>
1572 size_t vertexIndex,
1573 const Eigen::Vector3<T>& leverArm,
1574 T scale,
1575 std::span<const size_t> adjacentFaces,
1576 const SkeletonStateT<T>& skeletonState,
1577 const MeshStateT<T>& meshState,
1578 const Character& character,
1579 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
1580 size_t rowIndex) const {
1581 if (!character.blendShape && !character.faceExpressionBlendShape) {
1582 return;
1583 }
1584
1585 MT_CHECK(meshState.posedMesh_ != nullptr);
1586 MT_CHECK(meshState.restMesh_ != nullptr);
1587 MT_CHECK(character.skinWeights != nullptr);
1588 MT_CHECK(!character.inverseBindPose.empty());
1589
1590 const auto& faces = meshState.posedMesh_->faces;
1591 const auto& restVertices = meshState.restMesh_->vertices;
1592
1593 if (adjacentFaces.empty()) {
1594 return;
1595 }
1596
1597 // The forward path computes posed normals as:
1598 // restNormal = normalize(Σ_f restFaceNormal_f) [updateNormals on rest mesh]
1599 // posedNormal = normalize(M_v * restNormal) [applySSD skins + normalizes]
1600 //
1601 // Since normalize(M_v * normalize(s)) = normalize(M_v * s), the derivative simplifies to:
1602 // d(posedNormal)/d(s) = [(I - n̂*n̂^T) / |M_v * s|] * M_v
1603 // where s = restSum, n̂ = posedNormal.
1604
1605 // Step 1: Compute rest-space edge vectors and unnormalized sum
1606 Eigen::Vector3<T> restSum = Eigen::Vector3<T>::Zero();
1607 struct FaceEdges {
1608 Eigen::Vector3<T> e1;
1609 Eigen::Vector3<T> e2;
1610 Eigen::Vector3i indices;
1611 };
1612 std::vector<FaceEdges> faceEdges(adjacentFaces.size());
1613
1614 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1615 const auto& face = faces[adjacentFaces[i]];
1616 const Eigen::Vector3<T> v0 = restVertices[face[0]].template cast<T>();
1617 const Eigen::Vector3<T> v1 = restVertices[face[1]].template cast<T>();
1618 const Eigen::Vector3<T> v2 = restVertices[face[2]].template cast<T>();
1619 faceEdges[i].e1 = v1 - v0;
1620 faceEdges[i].e2 = v2 - v0;
1621 faceEdges[i].indices = face;
1622 restSum += faceEdges[i].e1.cross(faceEdges[i].e2);
1623 }
1624
1625 if (restSum.squaredNorm() < std::numeric_limits<T>::epsilon()) {
1626 return;
1627 }
1628
1629 // Step 2: Compute skinning rotation M_v for the constraint vertex
1630 const auto& skinWeights = *character.skinWeights;
1631 Eigen::Matrix3<T> Mv;
1632 Mv.setZero();
1633 for (uint32_t jWeight = 0; jWeight < kMaxSkinJoints; ++jWeight) {
1634 const auto w = skinWeights.weight(vertexIndex, jWeight);
1635 if (w > T(0)) {
1636 const auto parentBone = skinWeights.index(vertexIndex, jWeight);
1637 Mv.noalias() += w *
1638 (skeletonState.jointState[parentBone].transform.toLinear() *
1639 character.inverseBindPose[parentBone].linear().template cast<T>());
1640 }
1641 }
1642
1643 // Step 3: Compute posed unnormalized = M_v * restSum and normalization derivative
1644 const Eigen::Vector3<T> posedUnnorm = Mv * restSum;
1645 const T posedNorm = posedUnnorm.norm();
1646 if (posedNorm < T(1e-12)) {
1647 return;
1648 }
1649 const Eigen::Vector3<T> posedNhat = posedUnnorm / posedNorm;
1650
1651 // d(posedNormal)/d(restSum) = [(I - n̂*n̂^T) / |M_v * restSum|] * M_v
1652 const Eigen::Matrix3<T> dnormMv =
1653 ((Eigen::Matrix3<T>::Identity() - posedNhat * posedNhat.transpose()) / posedNorm) * Mv;
1654
1655 // Precompute leverArm^T * dnormMv for efficiency
1656 const Eigen::RowVector3<T> leverDnormMv = leverArm.transpose() * dnormMv;
1657
1658 // Process standard blend shapes
1659 if (character.blendShape) {
1660 const auto& shapeVectors = character.blendShape->getShapeVectors();
1661 for (Eigen::Index iBS = 0; iBS < parameterTransform_.blendShapeParameters.size(); ++iBS) {
1662 const auto paramIdx = parameterTransform_.blendShapeParameters[iBS];
1663 if (paramIdx < 0) {
1664 continue;
1665 }
1666
1667 Eigen::Vector3<T> dRestSumDBs = Eigen::Vector3<T>::Zero();
1668
1669 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1670 const auto& fe = faceEdges[i];
1671
1672 const Eigen::Vector3<T> sv0 =
1673 shapeVectors.template block<3, 1>(3 * fe.indices[0], iBS, 3, 1).template cast<T>();
1674 const Eigen::Vector3<T> sv1 =
1675 shapeVectors.template block<3, 1>(3 * fe.indices[1], iBS, 3, 1).template cast<T>();
1676 const Eigen::Vector3<T> sv2 =
1677 shapeVectors.template block<3, 1>(3 * fe.indices[2], iBS, 3, 1).template cast<T>();
1678
1679 const Eigen::Vector3<T> de1 = sv1 - sv0;
1680 const Eigen::Vector3<T> de2 = sv2 - sv0;
1681
1682 dRestSumDBs += de1.cross(fe.e2) + fe.e1.cross(de2);
1683 }
1684
1685 jacobian(rowIndex, paramIdx) += scale * leverDnormMv.dot(dRestSumDBs);
1686 }
1687 }
1688
1689 // Process face expression blend shapes
1690 if (character.faceExpressionBlendShape) {
1691 const auto& shapeVectors = character.faceExpressionBlendShape->getShapeVectors();
1692 for (Eigen::Index iBS = 0; iBS < parameterTransform_.faceExpressionParameters.size(); ++iBS) {
1693 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBS];
1694 if (paramIdx < 0) {
1695 continue;
1696 }
1697
1698 Eigen::Vector3<T> dRestSumDBs = Eigen::Vector3<T>::Zero();
1699
1700 for (size_t i = 0; i < adjacentFaces.size(); ++i) {
1701 const auto& fe = faceEdges[i];
1702
1703 const Eigen::Vector3<T> sv0 =
1704 shapeVectors.template block<3, 1>(3 * fe.indices[0], iBS, 3, 1).template cast<T>();
1705 const Eigen::Vector3<T> sv1 =
1706 shapeVectors.template block<3, 1>(3 * fe.indices[1], iBS, 3, 1).template cast<T>();
1707 const Eigen::Vector3<T> sv2 =
1708 shapeVectors.template block<3, 1>(3 * fe.indices[2], iBS, 3, 1).template cast<T>();
1709
1710 const Eigen::Vector3<T> de1 = sv1 - sv0;
1711 const Eigen::Vector3<T> de2 = sv2 - sv0;
1712
1713 dRestSumDBs += de1.cross(fe.e2) + fe.e1.cross(de2);
1714 }
1715
1716 jacobian(rowIndex, paramIdx) += scale * leverDnormMv.dot(dRestSumDBs);
1717 }
1718 }
1719}
1720
1721// accumulateVertexGradientWithNormalCorrection implementation
1722template <typename T>
1723template <size_t FuncDim>
1725 size_t vertexIndex,
1726 const Eigen::Vector3<T>& /* worldPos */,
1727 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
1728 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
1729 const Eigen::Vector3<T>& sourceNormal,
1730 const Eigen::Vector3<T>& targetPosition,
1731 T normalCorrectionScale,
1732 const SkeletonStateT<T>& skeletonState,
1733 const MeshStateT<T>& meshState,
1734 const Character& character,
1735 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
1736 static_assert(FuncDim == 1, "Normal correction assumes FuncDim == 1 (scalar residual)");
1737 // Skip if derivative is zero (normal correction still needs to run, so only
1738 // skip both if dfdv is zero AND normalCorrectionScale is zero)
1739 const bool hasDfdv = !dfdv.isZero();
1740 if (!hasDfdv && normalCorrectionScale == T(0)) {
1741 return;
1742 }
1743
1744 MT_CHECK(meshState.restMesh_ != nullptr);
1745 MT_CHECK(character.skinWeights != nullptr);
1746 MT_CHECK(!character.inverseBindPose.empty());
1747
1748 // Single skinning walk combining vertex gradient + normal correction
1749 SkinningWeightIteratorT<T> skinningIter(
1750 character, *meshState.restMesh_, skeletonState, vertexIndex);
1751
1752 while (!skinningIter.finished()) {
1753 size_t jointIndex = 0;
1754 T boneWeight;
1755 Eigen::Vector3<T> pos;
1756 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
1757
1758 MT_CHECK(jointIndex < skeleton_.joints.size());
1759
1760 const auto& jointState = skeletonState.jointState[jointIndex];
1761 const size_t paramIndex = jointIndex * kParametersPerJoint;
1762 const Eigen::Vector3<T> offset = pos - jointState.translation();
1763
1764 // Translation DOFs (from standard vertex gradient only)
1765 if (hasDfdv) {
1766 for (size_t d = 0; d < 3; ++d) {
1767 if (!activeJointParams_[paramIndex + d]) {
1768 continue;
1769 }
1770
1771 const T val =
1772 boneWeight * weightedResidual.dot(dfdv * jointState.getTranslationDerivative(d));
1773
1774 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
1775 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
1776 ++index) {
1777 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1778 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
1779 val * parameterTransform_.transform.valuePtr()[index];
1780 }
1781 }
1782 }
1783 }
1784
1785 // Rotation DOFs: COMBINED standard vertex + normal correction
1786 // Normal correction cross product (computed once per bone)
1787 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
1788
1789 for (size_t d = 0; d < 3; ++d) {
1790 if (!activeJointParams_[paramIndex + 3 + d]) {
1791 continue;
1792 }
1793
1794 T val = T(0);
1795 if (hasDfdv) {
1796 val +=
1797 boneWeight * weightedResidual.dot(dfdv * jointState.getRotationDerivative(d, offset));
1798 }
1799 val += boneWeight * normalCorrectionScale * jointState.rotationAxis.col(d).dot(cross);
1800
1801 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
1802 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
1803 ++index) {
1804 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1805 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
1806 val * parameterTransform_.transform.valuePtr()[index];
1807 }
1808 }
1809 }
1810
1811 // Scale DOF (from standard vertex gradient only)
1812 if (hasDfdv) {
1813 if (activeJointParams_[paramIndex + 6]) {
1814 const T val =
1815 boneWeight * weightedResidual.dot(dfdv * jointState.getScaleDerivative(offset));
1816
1817 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
1818 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
1819 ++index) {
1820 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1821 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
1822 val * parameterTransform_.transform.valuePtr()[index];
1823 }
1824 }
1825 }
1826 }
1827 }
1828
1829 if (!hasDfdv) {
1830 return;
1831 }
1832
1833 // Handle blend shape parameters (from standard vertex gradient)
1834 if (character.blendShape) {
1835 for (Eigen::Index iBlendShape = 0;
1836 iBlendShape < parameterTransform_.blendShapeParameters.size();
1837 ++iBlendShape) {
1838 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
1839 if (paramIdx < 0) {
1840 continue;
1841 }
1842
1843 const Eigen::Vector3<T> d_restPos =
1844 character.blendShape->getShapeVectors()
1845 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
1846 .template cast<T>();
1847
1848 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
1849 const auto& skinWeights = *character.skinWeights;
1850 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
1851 const auto w = skinWeights.weight(vertexIndex, i);
1852 const auto parentBone = skinWeights.index(vertexIndex, i);
1853 if (w > 0) {
1854 d_worldPos += w *
1855 (skeletonState.jointState[parentBone].transform.toLinear() *
1856 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
1857 }
1858 }
1859
1860 gradient[paramIdx] += weightedResidual.dot(dfdv * d_worldPos);
1861 }
1862 }
1863
1864 // Handle face expression blend shape parameters (from standard vertex gradient)
1865 if (character.faceExpressionBlendShape) {
1866 for (Eigen::Index iBlendShape = 0;
1867 iBlendShape < parameterTransform_.faceExpressionParameters.size();
1868 ++iBlendShape) {
1869 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
1870 if (paramIdx < 0) {
1871 continue;
1872 }
1873
1874 const Eigen::Vector3<T> d_restPos =
1875 character.faceExpressionBlendShape->getShapeVectors()
1876 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
1877 .template cast<T>();
1878
1879 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
1880 const auto& skinWeights = *character.skinWeights;
1881 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
1882 const auto w = skinWeights.weight(vertexIndex, i);
1883 const auto parentBone = skinWeights.index(vertexIndex, i);
1884 if (w > 0) {
1885 d_worldPos += w *
1886 (skeletonState.jointState[parentBone].transform.toLinear() *
1887 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
1888 }
1889 }
1890
1891 gradient[paramIdx] += weightedResidual.dot(dfdv * d_worldPos);
1892 }
1893 }
1894}
1895
1896// accumulateVertexJacobianWithNormalCorrection implementation
1897template <typename T>
1898template <size_t FuncDim>
1900 size_t vertexIndex,
1901 const Eigen::Vector3<T>& /* worldPos */,
1902 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
1903 T scale,
1904 const Eigen::Vector3<T>& sourceNormal,
1905 const Eigen::Vector3<T>& targetPosition,
1906 T normalCorrectionScale,
1907 const SkeletonStateT<T>& skeletonState,
1908 const MeshStateT<T>& meshState,
1909 const Character& character,
1910 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
1911 size_t rowIndex) const {
1912 static_assert(FuncDim == 1, "Normal correction assumes FuncDim == 1 (scalar residual)");
1913 const bool hasDfdv = !dfdv.isZero();
1914 if (!hasDfdv && normalCorrectionScale == T(0)) {
1915 return;
1916 }
1917
1918 MT_CHECK(meshState.restMesh_ != nullptr);
1919 MT_CHECK(character.skinWeights != nullptr);
1920 MT_CHECK(!character.inverseBindPose.empty());
1921
1922 auto jac = jacobian.middleRows(rowIndex, FuncDim);
1923
1924 // Single skinning walk combining vertex Jacobian + normal correction
1925 SkinningWeightIteratorT<T> skinningIter(
1926 character, *meshState.restMesh_, skeletonState, vertexIndex);
1927
1928 while (!skinningIter.finished()) {
1929 size_t jointIndex = 0;
1930 T boneWeight;
1931 Eigen::Vector3<T> pos;
1932 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
1933
1934 MT_CHECK(jointIndex < skeleton_.joints.size());
1935
1936 const auto& jointState = skeletonState.jointState[jointIndex];
1937 const size_t paramIndex = jointIndex * kParametersPerJoint;
1938 const Eigen::Vector3<T> offset = pos - jointState.translation();
1939 const T scaledBoneWeight = scale * boneWeight;
1940
1941 // Translation DOFs (from standard vertex Jacobian only)
1942 if (hasDfdv) {
1943 for (size_t d = 0; d < 3; ++d) {
1944 if (!activeJointParams_[paramIndex + d]) {
1945 continue;
1946 }
1947
1948 const Eigen::Matrix<T, FuncDim, 1> jc =
1949 scaledBoneWeight * dfdv * jointState.getTranslationDerivative(d);
1950
1951 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
1952 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
1953 ++index) {
1954 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1955 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
1956 jc * parameterTransform_.transform.valuePtr()[index];
1957 }
1958 }
1959 }
1960 }
1961
1962 // Rotation DOFs: COMBINED standard vertex + normal correction
1963 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
1964
1965 for (size_t d = 0; d < 3; ++d) {
1966 if (!activeJointParams_[paramIndex + 3 + d]) {
1967 continue;
1968 }
1969
1970 Eigen::Matrix<T, FuncDim, 1> jc;
1971 if (hasDfdv) {
1972 jc = scaledBoneWeight * dfdv * jointState.getRotationDerivative(d, offset);
1973 } else {
1974 jc.setZero();
1975 }
1976 const T normalVal =
1977 boneWeight * normalCorrectionScale * jointState.rotationAxis.col(d).dot(cross);
1978 jc[0] += normalVal;
1979
1980 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
1981 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
1982 ++index) {
1983 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
1984 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
1985 jc * parameterTransform_.transform.valuePtr()[index];
1986 }
1987 }
1988 }
1989
1990 // Scale DOF (from standard vertex Jacobian only)
1991 if (hasDfdv) {
1992 if (activeJointParams_[paramIndex + 6]) {
1993 const Eigen::Matrix<T, FuncDim, 1> jc =
1994 scaledBoneWeight * dfdv * jointState.getScaleDerivative(offset);
1995
1996 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
1997 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
1998 ++index) {
1999 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2000 jac.col(parameterTransform_.transform.innerIndexPtr()[index]).noalias() +=
2001 jc * parameterTransform_.transform.valuePtr()[index];
2002 }
2003 }
2004 }
2005 }
2006 }
2007
2008 if (!hasDfdv) {
2009 return;
2010 }
2011
2012 // Handle blend shape parameters (from standard vertex Jacobian)
2013 if (character.blendShape) {
2014 for (Eigen::Index iBlendShape = 0;
2015 iBlendShape < parameterTransform_.blendShapeParameters.size();
2016 ++iBlendShape) {
2017 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
2018 if (paramIdx < 0) {
2019 continue;
2020 }
2021
2022 const Eigen::Vector3<T> d_restPos =
2023 character.blendShape->getShapeVectors()
2024 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2025 .template cast<T>();
2026
2027 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2028 const auto& skinWeights = *character.skinWeights;
2029 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2030 const auto w = skinWeights.weight(vertexIndex, i);
2031 const auto parentBone = skinWeights.index(vertexIndex, i);
2032 if (w > 0) {
2033 d_worldPos += w *
2034 (skeletonState.jointState[parentBone].transform.toLinear() *
2035 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2036 }
2037 }
2038
2039 jac.col(paramIdx).noalias() += scale * dfdv * d_worldPos;
2040 }
2041 }
2042
2043 // Handle face expression blend shape parameters (from standard vertex Jacobian)
2044 if (character.faceExpressionBlendShape) {
2045 for (Eigen::Index iBlendShape = 0;
2046 iBlendShape < parameterTransform_.faceExpressionParameters.size();
2047 ++iBlendShape) {
2048 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
2049 if (paramIdx < 0) {
2050 continue;
2051 }
2052
2053 const Eigen::Vector3<T> d_restPos =
2054 character.faceExpressionBlendShape->getShapeVectors()
2055 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2056 .template cast<T>();
2057
2058 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2059 const auto& skinWeights = *character.skinWeights;
2060 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2061 const auto w = skinWeights.weight(vertexIndex, i);
2062 const auto parentBone = skinWeights.index(vertexIndex, i);
2063 if (w > 0) {
2064 d_worldPos += w *
2065 (skeletonState.jointState[parentBone].transform.toLinear() *
2066 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2067 }
2068 }
2069
2070 jac.col(paramIdx).noalias() += scale * dfdv * d_worldPos;
2071 }
2072 }
2073}
2074
2075// accumulateVertexGradientWithNormalCorrectionDirect implementation
2076// Scalar-specialized for FuncDim=1: uses Vector3.dot() instead of Matrix<1,3> * Vector3
2077template <typename T>
2079 size_t vertexIndex,
2080 const Eigen::Vector3<T>& /* worldPos */,
2081 const Eigen::Vector3<T>& normal,
2082 T weightedResidual,
2083 const Eigen::Vector3<T>& sourceNormal,
2084 const Eigen::Vector3<T>& targetPosition,
2085 T normalCorrectionScale,
2086 const SkeletonStateT<T>& skeletonState,
2087 const MeshStateT<T>& meshState,
2088 const Character& character,
2089 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
2090 const bool hasNormal = !normal.isZero();
2091 if (!hasNormal && normalCorrectionScale == T(0)) {
2092 return;
2093 }
2094
2095 MT_CHECK(meshState.restMesh_ != nullptr);
2096 MT_CHECK(character.skinWeights != nullptr);
2097 MT_CHECK(!character.inverseBindPose.empty());
2098
2099 SkinningWeightIteratorT<T> skinningIter(
2100 character, *meshState.restMesh_, skeletonState, vertexIndex);
2101
2102 while (!skinningIter.finished()) {
2103 size_t jointIndex = 0;
2104 T boneWeight;
2105 Eigen::Vector3<T> pos;
2106 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
2107
2108 MT_CHECK(jointIndex < skeleton_.joints.size());
2109
2110 const auto& jointState = skeletonState.jointState[jointIndex];
2111 const size_t paramIndex = jointIndex * kParametersPerJoint;
2112 const Eigen::Vector3<T> offset = pos - jointState.translation();
2113
2114 // Translation DOFs (scalar dot product instead of Matrix<1,3> * Vector3)
2115 if (hasNormal) {
2116 for (size_t d = 0; d < 3; ++d) {
2117 if (!activeJointParams_[paramIndex + d]) {
2118 continue;
2119 }
2120
2121 const T val =
2122 boneWeight * weightedResidual * normal.dot(jointState.getTranslationDerivative(d));
2123
2124 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
2125 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
2126 ++index) {
2127 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2128 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
2129 val * parameterTransform_.transform.valuePtr()[index];
2130 }
2131 }
2132 }
2133 }
2134
2135 // Rotation DOFs: COMBINED standard vertex + normal correction
2136 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
2137
2138 for (size_t d = 0; d < 3; ++d) {
2139 if (!activeJointParams_[paramIndex + 3 + d]) {
2140 continue;
2141 }
2142
2143 T val = T(0);
2144 if (hasNormal) {
2145 val +=
2146 boneWeight * weightedResidual * normal.dot(jointState.getRotationDerivative(d, offset));
2147 }
2148 val += boneWeight * normalCorrectionScale * jointState.rotationAxis.col(d).dot(cross);
2149
2150 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
2151 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
2152 ++index) {
2153 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2154 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
2155 val * parameterTransform_.transform.valuePtr()[index];
2156 }
2157 }
2158 }
2159
2160 // Scale DOF (scalar dot product instead of Matrix<1,3> * Vector3)
2161 if (hasNormal) {
2162 if (activeJointParams_[paramIndex + 6]) {
2163 const T val =
2164 boneWeight * weightedResidual * normal.dot(jointState.getScaleDerivative(offset));
2165
2166 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
2167 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
2168 ++index) {
2169 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2170 gradient[parameterTransform_.transform.innerIndexPtr()[index]] +=
2171 val * parameterTransform_.transform.valuePtr()[index];
2172 }
2173 }
2174 }
2175 }
2176 }
2177
2178 if (!hasNormal) {
2179 return;
2180 }
2181
2182 // Handle blend shape parameters (scalar dot product)
2183 if (character.blendShape) {
2184 for (Eigen::Index iBlendShape = 0;
2185 iBlendShape < parameterTransform_.blendShapeParameters.size();
2186 ++iBlendShape) {
2187 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
2188 if (paramIdx < 0) {
2189 continue;
2190 }
2191
2192 const Eigen::Vector3<T> d_restPos =
2193 character.blendShape->getShapeVectors()
2194 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2195 .template cast<T>();
2196
2197 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2198 const auto& skinWeights = *character.skinWeights;
2199 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2200 const auto w = skinWeights.weight(vertexIndex, i);
2201 const auto parentBone = skinWeights.index(vertexIndex, i);
2202 if (w > 0) {
2203 d_worldPos += w *
2204 (skeletonState.jointState[parentBone].transform.toLinear() *
2205 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2206 }
2207 }
2208
2209 gradient[paramIdx] += weightedResidual * normal.dot(d_worldPos);
2210 }
2211 }
2212
2213 // Handle face expression blend shape parameters (scalar dot product)
2214 if (character.faceExpressionBlendShape) {
2215 for (Eigen::Index iBlendShape = 0;
2216 iBlendShape < parameterTransform_.faceExpressionParameters.size();
2217 ++iBlendShape) {
2218 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
2219 if (paramIdx < 0) {
2220 continue;
2221 }
2222
2223 const Eigen::Vector3<T> d_restPos =
2224 character.faceExpressionBlendShape->getShapeVectors()
2225 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2226 .template cast<T>();
2227
2228 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2229 const auto& skinWeights = *character.skinWeights;
2230 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2231 const auto w = skinWeights.weight(vertexIndex, i);
2232 const auto parentBone = skinWeights.index(vertexIndex, i);
2233 if (w > 0) {
2234 d_worldPos += w *
2235 (skeletonState.jointState[parentBone].transform.toLinear() *
2236 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2237 }
2238 }
2239
2240 gradient[paramIdx] += weightedResidual * normal.dot(d_worldPos);
2241 }
2242 }
2243}
2244
2245// accumulateVertexJacobianWithNormalCorrectionDirect implementation
2246// Scalar-specialized for FuncDim=1: uses Vector3.dot() instead of Matrix<1,3> * Vector3
2247template <typename T>
2249 size_t vertexIndex,
2250 const Eigen::Vector3<T>& /* worldPos */,
2251 const Eigen::Vector3<T>& normal,
2252 T scale,
2253 const Eigen::Vector3<T>& sourceNormal,
2254 const Eigen::Vector3<T>& targetPosition,
2255 T normalCorrectionScale,
2256 const SkeletonStateT<T>& skeletonState,
2257 const MeshStateT<T>& meshState,
2258 const Character& character,
2259 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
2260 size_t rowIndex) const {
2261 const bool hasNormal = !normal.isZero();
2262 if (!hasNormal && normalCorrectionScale == T(0)) {
2263 return;
2264 }
2265
2266 MT_CHECK(meshState.restMesh_ != nullptr);
2267 MT_CHECK(character.skinWeights != nullptr);
2268 MT_CHECK(!character.inverseBindPose.empty());
2269
2270 SkinningWeightIteratorT<T> skinningIter(
2271 character, *meshState.restMesh_, skeletonState, vertexIndex);
2272
2273 while (!skinningIter.finished()) {
2274 size_t jointIndex = 0;
2275 T boneWeight;
2276 Eigen::Vector3<T> pos;
2277 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
2278
2279 MT_CHECK(jointIndex < skeleton_.joints.size());
2280
2281 const auto& jointState = skeletonState.jointState[jointIndex];
2282 const size_t paramIndex = jointIndex * kParametersPerJoint;
2283 const Eigen::Vector3<T> offset = pos - jointState.translation();
2284 const T scaledBoneWeight = scale * boneWeight;
2285
2286 // Translation DOFs (scalar dot product instead of Matrix<1,3> * Vector3)
2287 if (hasNormal) {
2288 for (size_t d = 0; d < 3; ++d) {
2289 if (!activeJointParams_[paramIndex + d]) {
2290 continue;
2291 }
2292
2293 const T jc = scaledBoneWeight * normal.dot(jointState.getTranslationDerivative(d));
2294
2295 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + d];
2296 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + d + 1];
2297 ++index) {
2298 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2299 jacobian(rowIndex, parameterTransform_.transform.innerIndexPtr()[index]) +=
2300 jc * parameterTransform_.transform.valuePtr()[index];
2301 }
2302 }
2303 }
2304 }
2305
2306 // Rotation DOFs: COMBINED standard vertex + normal correction
2307 const Eigen::Vector3<T> cross = (targetPosition - pos).cross(sourceNormal);
2308
2309 for (size_t d = 0; d < 3; ++d) {
2310 if (!activeJointParams_[paramIndex + 3 + d]) {
2311 continue;
2312 }
2313
2314 T jc = T(0);
2315 if (hasNormal) {
2316 jc += scaledBoneWeight * normal.dot(jointState.getRotationDerivative(d, offset));
2317 }
2318 jc += boneWeight * normalCorrectionScale * jointState.rotationAxis.col(d).dot(cross);
2319
2320 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d];
2321 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
2322 ++index) {
2323 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2324 jacobian(rowIndex, parameterTransform_.transform.innerIndexPtr()[index]) +=
2325 jc * parameterTransform_.transform.valuePtr()[index];
2326 }
2327 }
2328 }
2329
2330 // Scale DOF (scalar dot product instead of Matrix<1,3> * Vector3)
2331 if (hasNormal) {
2332 if (activeJointParams_[paramIndex + 6]) {
2333 const T jc = scaledBoneWeight * normal.dot(jointState.getScaleDerivative(offset));
2334
2335 for (auto index = parameterTransform_.transform.outerIndexPtr()[paramIndex + 6];
2336 index < parameterTransform_.transform.outerIndexPtr()[paramIndex + 6 + 1];
2337 ++index) {
2338 if (enabledParameters_.test(parameterTransform_.transform.innerIndexPtr()[index])) {
2339 jacobian(rowIndex, parameterTransform_.transform.innerIndexPtr()[index]) +=
2340 jc * parameterTransform_.transform.valuePtr()[index];
2341 }
2342 }
2343 }
2344 }
2345 }
2346
2347 if (!hasNormal) {
2348 return;
2349 }
2350
2351 // Handle blend shape parameters (scalar dot product)
2352 if (character.blendShape) {
2353 for (Eigen::Index iBlendShape = 0;
2354 iBlendShape < parameterTransform_.blendShapeParameters.size();
2355 ++iBlendShape) {
2356 const auto paramIdx = parameterTransform_.blendShapeParameters[iBlendShape];
2357 if (paramIdx < 0) {
2358 continue;
2359 }
2360
2361 const Eigen::Vector3<T> d_restPos =
2362 character.blendShape->getShapeVectors()
2363 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2364 .template cast<T>();
2365
2366 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2367 const auto& skinWeights = *character.skinWeights;
2368 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2369 const auto w = skinWeights.weight(vertexIndex, i);
2370 const auto parentBone = skinWeights.index(vertexIndex, i);
2371 if (w > 0) {
2372 d_worldPos += w *
2373 (skeletonState.jointState[parentBone].transform.toLinear() *
2374 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2375 }
2376 }
2377
2378 jacobian(rowIndex, paramIdx) += scale * normal.dot(d_worldPos);
2379 }
2380 }
2381
2382 // Handle face expression blend shape parameters (scalar dot product)
2383 if (character.faceExpressionBlendShape) {
2384 for (Eigen::Index iBlendShape = 0;
2385 iBlendShape < parameterTransform_.faceExpressionParameters.size();
2386 ++iBlendShape) {
2387 const auto paramIdx = parameterTransform_.faceExpressionParameters[iBlendShape];
2388 if (paramIdx < 0) {
2389 continue;
2390 }
2391
2392 const Eigen::Vector3<T> d_restPos =
2393 character.faceExpressionBlendShape->getShapeVectors()
2394 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
2395 .template cast<T>();
2396
2397 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
2398 const auto& skinWeights = *character.skinWeights;
2399 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
2400 const auto w = skinWeights.weight(vertexIndex, i);
2401 const auto parentBone = skinWeights.index(vertexIndex, i);
2402 if (w > 0) {
2403 d_worldPos += w *
2404 (skeletonState.jointState[parentBone].transform.toLinear() *
2405 (character.inverseBindPose[parentBone].linear().template cast<T>() * d_restPos));
2406 }
2407 }
2408
2409 jacobian(rowIndex, paramIdx) += scale * normal.dot(d_worldPos);
2410 }
2411 }
2412}
2413
2414// Explicit template instantiations for accumulateVertexGradientWithNormalCorrectionDirect
2416 size_t,
2417 const Eigen::Vector3<float>&,
2418 const Eigen::Vector3<float>&,
2419 float,
2420 const Eigen::Vector3<float>&,
2421 const Eigen::Vector3<float>&,
2422 float,
2423 const SkeletonStateT<float>&,
2424 const MeshStateT<float>&,
2425 const Character&,
2426 Eigen::Ref<Eigen::VectorX<float>>) const;
2428 size_t,
2429 const Eigen::Vector3<double>&,
2430 const Eigen::Vector3<double>&,
2431 double,
2432 const Eigen::Vector3<double>&,
2433 const Eigen::Vector3<double>&,
2434 double,
2436 const MeshStateT<double>&,
2437 const Character&,
2438 Eigen::Ref<Eigen::VectorX<double>>) const;
2439
2440// Explicit template instantiations for accumulateVertexJacobianWithNormalCorrectionDirect
2442 size_t,
2443 const Eigen::Vector3<float>&,
2444 const Eigen::Vector3<float>&,
2445 float,
2446 const Eigen::Vector3<float>&,
2447 const Eigen::Vector3<float>&,
2448 float,
2449 const SkeletonStateT<float>&,
2450 const MeshStateT<float>&,
2451 const Character&,
2452 Eigen::Ref<Eigen::MatrixX<float>>,
2453 size_t) const;
2455 size_t,
2456 const Eigen::Vector3<double>&,
2457 const Eigen::Vector3<double>&,
2458 double,
2459 const Eigen::Vector3<double>&,
2460 const Eigen::Vector3<double>&,
2461 double,
2463 const MeshStateT<double>&,
2464 const Character&,
2465 Eigen::Ref<Eigen::MatrixX<double>>,
2466 size_t) const;
2467
2468// Explicit template instantiations for accumulateVertexGradientWithNormalCorrection
2470 size_t,
2471 const Eigen::Vector3<float>&,
2472 const Eigen::Matrix<float, 1, 3>&,
2473 const Eigen::Matrix<float, 1, 1>&,
2474 const Eigen::Vector3<float>&,
2475 const Eigen::Vector3<float>&,
2476 float,
2477 const SkeletonStateT<float>&,
2478 const MeshStateT<float>&,
2479 const Character&,
2480 Eigen::Ref<Eigen::VectorX<float>>) const;
2482 size_t,
2483 const Eigen::Vector3<double>&,
2484 const Eigen::Matrix<double, 1, 3>&,
2485 const Eigen::Matrix<double, 1, 1>&,
2486 const Eigen::Vector3<double>&,
2487 const Eigen::Vector3<double>&,
2488 double,
2490 const MeshStateT<double>&,
2491 const Character&,
2492 Eigen::Ref<Eigen::VectorX<double>>) const;
2493
2494// Explicit template instantiations for accumulateVertexJacobianWithNormalCorrection
2496 size_t,
2497 const Eigen::Vector3<float>&,
2498 const Eigen::Matrix<float, 1, 3>&,
2499 float,
2500 const Eigen::Vector3<float>&,
2501 const Eigen::Vector3<float>&,
2502 float,
2503 const SkeletonStateT<float>&,
2504 const MeshStateT<float>&,
2505 const Character&,
2506 Eigen::Ref<Eigen::MatrixX<float>>,
2507 size_t) const;
2509 size_t,
2510 const Eigen::Vector3<double>&,
2511 const Eigen::Matrix<double, 1, 3>&,
2512 double,
2513 const Eigen::Vector3<double>&,
2514 const Eigen::Vector3<double>&,
2515 double,
2517 const MeshStateT<double>&,
2518 const Character&,
2519 Eigen::Ref<Eigen::MatrixX<double>>,
2520 size_t) const;
2521
2522// Explicit template instantiations for accumulateVertexNormalCorrection
2524 size_t,
2525 const Eigen::Vector3<float>&,
2526 const Eigen::Vector3<float>&,
2527 float,
2528 const SkeletonStateT<float>&,
2529 const MeshStateT<float>&,
2530 const Character&,
2531 Eigen::Ref<Eigen::VectorXf>) const;
2533 size_t,
2534 const Eigen::Vector3<double>&,
2535 const Eigen::Vector3<double>&,
2536 double,
2538 const MeshStateT<double>&,
2539 const Character&,
2540 Eigen::Ref<Eigen::VectorXd>) const;
2542 size_t,
2543 const Eigen::Vector3<float>&,
2544 const Eigen::Vector3<float>&,
2545 float,
2546 const SkeletonStateT<float>&,
2547 const MeshStateT<float>&,
2548 const Character&,
2549 Eigen::Ref<Eigen::MatrixXf>,
2550 size_t) const;
2552 size_t,
2553 const Eigen::Vector3<double>&,
2554 const Eigen::Vector3<double>&,
2555 double,
2557 const MeshStateT<double>&,
2558 const Character&,
2559 Eigen::Ref<Eigen::MatrixXd>,
2560 size_t) const;
2561
2562// Explicit template instantiations for accumulateVertexNormalBlendShape
2564 size_t,
2565 const Eigen::Vector3<float>&,
2566 float,
2567 const SkeletonStateT<float>&,
2568 const MeshStateT<float>&,
2569 const Character&,
2570 Eigen::Ref<Eigen::VectorXf>) const;
2572 size_t,
2573 const Eigen::Vector3<double>&,
2574 double,
2576 const MeshStateT<double>&,
2577 const Character&,
2578 Eigen::Ref<Eigen::VectorXd>) const;
2580 size_t,
2581 const Eigen::Vector3<float>&,
2582 float,
2583 std::span<const size_t>,
2584 const SkeletonStateT<float>&,
2585 const MeshStateT<float>&,
2586 const Character&,
2587 Eigen::Ref<Eigen::VectorXf>) const;
2589 size_t,
2590 const Eigen::Vector3<double>&,
2591 double,
2592 std::span<const size_t>,
2594 const MeshStateT<double>&,
2595 const Character&,
2596 Eigen::Ref<Eigen::VectorXd>) const;
2598 size_t,
2599 const Eigen::Vector3<float>&,
2600 float,
2601 const SkeletonStateT<float>&,
2602 const MeshStateT<float>&,
2603 const Character&,
2604 Eigen::Ref<Eigen::MatrixXf>,
2605 size_t) const;
2607 size_t,
2608 const Eigen::Vector3<double>&,
2609 double,
2611 const MeshStateT<double>&,
2612 const Character&,
2613 Eigen::Ref<Eigen::MatrixXd>,
2614 size_t) const;
2616 size_t,
2617 const Eigen::Vector3<float>&,
2618 float,
2619 std::span<const size_t>,
2620 const SkeletonStateT<float>&,
2621 const MeshStateT<float>&,
2622 const Character&,
2623 Eigen::Ref<Eigen::MatrixXf>,
2624 size_t) const;
2626 size_t,
2627 const Eigen::Vector3<double>&,
2628 double,
2629 std::span<const size_t>,
2631 const MeshStateT<double>&,
2632 const Character&,
2633 Eigen::Ref<Eigen::MatrixXd>,
2634 size_t) const;
2635
2637 size_t,
2638 const Eigen::Vector3<float>&,
2639 const Eigen::Matrix<float, 1, 3>&,
2640 const Eigen::Matrix<float, 1, 1>&,
2641 const SkeletonStateT<float>&,
2642 const MeshStateT<float>&,
2643 const Character&,
2644 Eigen::Ref<Eigen::VectorX<float>>) const;
2646 size_t,
2647 const Eigen::Vector3<float>&,
2648 const Eigen::Matrix<float, 2, 3>&,
2649 const Eigen::Matrix<float, 2, 1>&,
2650 const SkeletonStateT<float>&,
2651 const MeshStateT<float>&,
2652 const Character&,
2653 Eigen::Ref<Eigen::VectorX<float>>) const;
2655 size_t,
2656 const Eigen::Vector3<float>&,
2657 const Eigen::Matrix<float, 3, 3>&,
2658 const Eigen::Matrix<float, 3, 1>&,
2659 const SkeletonStateT<float>&,
2660 const MeshStateT<float>&,
2661 const Character&,
2662 Eigen::Ref<Eigen::VectorX<float>>) const;
2664 size_t,
2665 const Eigen::Vector3<float>&,
2666 const Eigen::Matrix<float, 9, 3>&,
2667 const Eigen::Matrix<float, 9, 1>&,
2668 const SkeletonStateT<float>&,
2669 const MeshStateT<float>&,
2670 const Character&,
2671 Eigen::Ref<Eigen::VectorX<float>>) const;
2672
2673// Explicit template instantiations for accumulateVertexJacobian (float)
2675 size_t,
2676 const Eigen::Vector3<float>&,
2677 const Eigen::Matrix<float, 1, 3>&,
2678 float,
2679 const SkeletonStateT<float>&,
2680 const MeshStateT<float>&,
2681 const Character&,
2682 Eigen::Ref<Eigen::MatrixX<float>>,
2683 size_t) const;
2685 size_t,
2686 const Eigen::Vector3<float>&,
2687 const Eigen::Matrix<float, 2, 3>&,
2688 float,
2689 const SkeletonStateT<float>&,
2690 const MeshStateT<float>&,
2691 const Character&,
2692 Eigen::Ref<Eigen::MatrixX<float>>,
2693 size_t) const;
2695 size_t,
2696 const Eigen::Vector3<float>&,
2697 const Eigen::Matrix<float, 3, 3>&,
2698 float,
2699 const SkeletonStateT<float>&,
2700 const MeshStateT<float>&,
2701 const Character&,
2702 Eigen::Ref<Eigen::MatrixX<float>>,
2703 size_t) const;
2705 size_t,
2706 const Eigen::Vector3<float>&,
2707 const Eigen::Matrix<float, 9, 3>&,
2708 float,
2709 const SkeletonStateT<float>&,
2710 const MeshStateT<float>&,
2711 const Character&,
2712 Eigen::Ref<Eigen::MatrixX<float>>,
2713 size_t) const;
2714
2715// Explicit template instantiations for accumulateVertexGradient (double)
2717 size_t,
2718 const Eigen::Vector3<double>&,
2719 const Eigen::Matrix<double, 1, 3>&,
2720 const Eigen::Matrix<double, 1, 1>&,
2722 const MeshStateT<double>&,
2723 const Character&,
2724 Eigen::Ref<Eigen::VectorX<double>>) const;
2726 size_t,
2727 const Eigen::Vector3<double>&,
2728 const Eigen::Matrix<double, 2, 3>&,
2729 const Eigen::Matrix<double, 2, 1>&,
2731 const MeshStateT<double>&,
2732 const Character&,
2733 Eigen::Ref<Eigen::VectorX<double>>) const;
2735 size_t,
2736 const Eigen::Vector3<double>&,
2737 const Eigen::Matrix<double, 3, 3>&,
2738 const Eigen::Matrix<double, 3, 1>&,
2740 const MeshStateT<double>&,
2741 const Character&,
2742 Eigen::Ref<Eigen::VectorX<double>>) const;
2744 size_t,
2745 const Eigen::Vector3<double>&,
2746 const Eigen::Matrix<double, 9, 3>&,
2747 const Eigen::Matrix<double, 9, 1>&,
2749 const MeshStateT<double>&,
2750 const Character&,
2751 Eigen::Ref<Eigen::VectorX<double>>) const;
2752
2753// Explicit template instantiations for accumulateVertexJacobian (double)
2755 size_t,
2756 const Eigen::Vector3<double>&,
2757 const Eigen::Matrix<double, 1, 3>&,
2758 double,
2760 const MeshStateT<double>&,
2761 const Character&,
2762 Eigen::Ref<Eigen::MatrixX<double>>,
2763 size_t) const;
2765 size_t,
2766 const Eigen::Vector3<double>&,
2767 const Eigen::Matrix<double, 2, 3>&,
2768 double,
2770 const MeshStateT<double>&,
2771 const Character&,
2772 Eigen::Ref<Eigen::MatrixX<double>>,
2773 size_t) const;
2775 size_t,
2776 const Eigen::Vector3<double>&,
2777 const Eigen::Matrix<double, 3, 3>&,
2778 double,
2780 const MeshStateT<double>&,
2781 const Character&,
2782 Eigen::Ref<Eigen::MatrixX<double>>,
2783 size_t) const;
2785 size_t,
2786 const Eigen::Vector3<double>&,
2787 const Eigen::Matrix<double, 9, 3>&,
2788 double,
2790 const MeshStateT<double>&,
2791 const Character&,
2792 Eigen::Ref<Eigen::MatrixX<double>>,
2793 size_t) const;
2794} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
Shared utility for derivative computation in error functions.
void accumulateVertexGradient(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates gradient for a skinned mesh vertex.
void accumulateJointGradient(size_t jointIndex, std::span< const Eigen::Vector3< T > > worldVecs, std::span< const Eigen::Matrix< T, FuncDim, 3 > > dfdvs, std::span< const uint8_t > isPoints, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const JointStateListT< T > &jointStates, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates gradient for vectors attached to a single joint.
void accumulateVertexNormalBlendShapeJacobian(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of accumulateVertexNormalBlendShapeGradient.
void accumulateVertexGradientWithNormalCorrectionDirect(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Vector3< T > &normal, T weightedResidual, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Scalar-specialized combined vertex gradient + normal rotation correction.
void accumulateVertexNormalCorrectionGradient(size_t vertexIndex, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates the gradient CORRECTION for how a mesh normal rotates with joints.
void accumulateVertexJacobianIdentityDfdv(size_t vertexIndex, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Specialized vertex Jacobian for identity dfdv (FuncDim=3, dfdv=I).
void accumulateVertexJacobianWithNormalCorrectionDirect(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Vector3< T > &normal, T scale, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Scalar-specialized combined vertex Jacobian + normal rotation correction.
void accumulateVertexNormalCorrectionJacobian(size_t vertexIndex, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of accumulateVertexNormalCorrectionGradient.
void accumulateVertexGradientWithNormalCorrection(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Combined vertex gradient + normal rotation correction in a single skinning walk.
void accumulateJointJacobian(size_t jointIndex, std::span< const Eigen::Vector3< T > > worldVecs, std::span< const Eigen::Matrix< T, FuncDim, 3 > > dfdvs, std::span< const uint8_t > isPoints, T scale, const JointStateListT< T > &jointStates, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Accumulates Jacobian for vectors attached to a single joint.
void accumulateVertexJacobianWithNormalCorrection(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, T scale, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of the combined vertex gradient + normal correction method.
void accumulateVertexNormalBlendShapeGradient(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates the gradient contribution from how mesh normals change with blend shape weights.
void accumulateVertexJacobian(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Accumulates Jacobian for a skinned mesh vertex.
SkeletonDerivativeT(const Skeleton &skeleton, const ParameterTransform &parameterTransform, const VectorX< bool > &activeJointParams, const ParameterSet &enabledParameters)
Constructs the derivative utility.
void accumulateVertexGradientIdentityDfdv(size_t vertexIndex, const Eigen::Vector3< T > &weightedResidual, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Specialized vertex gradient for identity dfdv (FuncDim=3, dfdv=I).
std::tuple< size_t, T, Eigen::Vector3< T > > next()
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
constexpr size_t kParametersPerJoint
Definition types.h:21
std::vector< JointStateT< T > > JointStateListT
Structure describing the state of all joints in a skeleton.
Vector3P< S > cross(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes cross product of Eigen::Vector3f and Vector3P.
Definition simd.h:309
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
TransformationList inverseBindPose
Inverse of the bind pose transformations for each joint.
Definition character.h:76
SkinWeights_u skinWeights
Defines how mesh vertices are influenced by joints.
Definition character.h:58
BlendShapeBase_const_p faceExpressionBlendShape
Facial expression blend shapes.
Definition character.h:73
BlendShape_const_p blendShape
Shape variations that can be blended.
Definition character.h:70
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
std::unique_ptr< MeshT< T > > posedMesh_
Posed mesh after the skeleton transforms have been applied.
Definition mesh_state.h:32
std::unique_ptr< MeshT< T > > restMesh_
Rest positions of the mesh after the shape basis (and potentially facial expression basis) has been a...
Definition mesh_state.h:30
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.