35 character_(character_in),
36 constraintType_(type) {
41 "Constraint type {} not implemented yet for face. Only Position type is supported.",
47 return this->character_.mesh->vertices.size();
61 const Eigen::Vector3i& triangleIndices,
62 const Eigen::Vector3<T>& triangleBaryCoords,
65 MT_CHECK(vertexIndex >= 0 && ((
size_t)vertexIndex) < character_.mesh->vertices.size());
66 constraints_.push_back(
68 vertexIndex, triangleIndices, triangleBaryCoords, depth, weight});
109 for (
size_t i = 0; i < constraints_.size(); ++i) {
114 const Eigen::Vector3<T> diff = srcPoint - tgtPoint;
118 const auto [sourceNormalWeight, targetNormalWeight] = computeNormalWeights();
119 for (
size_t i = 0; i < constraints_.size(); ++i) {
127 const Eigen::Vector3<T> tgtPoint =
130 if (sourceNormal.dot(targetNormal) < 0) {
133 const Eigen::Vector3<T>
normal =
134 sourceNormalWeight * sourceNormal + targetNormalWeight * targetNormal;
137 const Eigen::Vector3<T> diff = srcPoint - tgtPoint;
138 const T dist =
normal.dot(diff);
149 const T grad_jointParams,
150 const Eigen::Index iJointParam,
152 Eigen::Ref<Eigen::VectorX<T>> gradient) {
154 for (
auto index = parameterTransform.
transform.outerIndexPtr()[iJointParam];
155 index < parameterTransform.
transform.outerIndexPtr()[iJointParam + 1];
157 gradient[parameterTransform.
transform.innerIndexPtr()[index]] +=
158 grad_jointParams * parameterTransform.
transform.valuePtr()[index];
164 const T jacobian_jointParams,
165 const Eigen::Index iJointParam,
167 Eigen::Ref<Eigen::MatrixX<T>> jacobian) {
169 for (
auto index = parameterTransform.
transform.outerIndexPtr()[iJointParam];
170 index < parameterTransform.
transform.outerIndexPtr()[iJointParam + 1];
172 jacobian(0, parameterTransform.
transform.innerIndexPtr()[index]) +=
173 jacobian_jointParams * parameterTransform.
transform.valuePtr()[index];
181 const int vertexIndex,
182 const Eigen::Vector3<T>& d_restPos) {
185 Eigen::Vector3<T> d_worldPos = Eigen::Vector3<T>::Zero();
187 const auto w = skinWeights.weight(vertexIndex, i);
188 const auto parentBone = skinWeights.index(vertexIndex, i);
192 "Parent bone index {} exceeds jointState size {} or inverseBindPose size {}",
197 (state.
jointState[parentBone].transform.toLinear() *
209 std::array<Eigen::Matrix3<T>, 3> result;
210 for (
auto& r : result) {
214 const std::array<Eigen::Vector3<T>, 3> tgtTrianglePositions = {
219 Eigen::Vector3<T> n = (tgtTrianglePositions[1] - tgtTrianglePositions[0])
220 .
cross(tgtTrianglePositions[2] - tgtTrianglePositions[0]);
228 const T area_times_2 = n_norm;
230 for (
int k = 0; k < 3; ++k) {
232 const Eigen::Vector3<T> e =
233 tgtTrianglePositions[(k + 2) % 3] - tgtTrianglePositions[(k + 1) % 3];
234 result[k] += (e.cross(n) * n.transpose()) / area_times_2;
244 std::array<Eigen::Matrix3<T>, 3> result;
250 for (
int k = 0; k < 3; ++k) {
251 result[k] += cons.
depth * dNormal[k];
258double PointTriangleVertexErrorFunctionT<T>::calculatePositionGradient(
259 const ModelParametersT<T>& ,
260 const SkeletonStateT<T>& state,
261 const MeshStateT<T>& meshState,
262 const PointTriangleVertexConstraintT<T>& constr,
263 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
264 const Eigen::Vector3<T> srcPos = meshState.posedMesh_->vertices[constr.srcVertexIndex];
267 const Eigen::Vector3<T> diff = srcPos - tgtPos;
270 const T wgt = constr.weight * 2.0f * kPositionWeight * this->weight_;
272 const auto d_targetPos_d_tgtTriVertexPos =
278 const Eigen::Vector3<T> weightedResidual = wgt * diff;
280 SkeletonDerivativeT<T> skeletonDerivative(
282 this->parameterTransform_,
283 this->activeJointParams_,
284 this->enabledParameters_);
290 for (
int iTriVert = 0; iTriVert < 4; ++iTriVert) {
291 const auto vertexIndex =
292 iTriVert < 3 ? constr.tgtTriangleIndices[iTriVert] : constr.srcVertexIndex;
293 const Eigen::Vector3<T>& vertexPos = meshState.posedMesh_->vertices[vertexIndex];
296 const Eigen::Matrix3<T> dfdv = iTriVert < 3
297 ? Eigen::Matrix3<T>(-d_targetPos_d_tgtTriVertexPos[iTriVert])
300 skeletonDerivative.template accumulateVertexGradient<3>(
301 vertexIndex, vertexPos, dfdv, weightedResidual, state, meshState, character_, gradient);
304 return constr.weight * diff.squaredNorm() * kPositionWeight;
308double PointTriangleVertexErrorFunctionT<T>::calculatePositionJacobian(
313 Ref<Eigen::MatrixX<T>> jac,
314 Ref<Eigen::VectorX<T>> res)
const {
318 const Eigen::Vector3<T> diff = meshState.posedMesh_->vertices[constr.srcVertexIndex] - tgtPos;
320 const T wgt = std::sqrt(constr.weight * kPositionWeight * this->weight_);
322 const auto d_targetPos_d_tgtTriVertexPos =
327 this->parameterTransform_,
328 this->activeJointParams_,
329 this->enabledParameters_);
335 for (
int iTriVert = 0; iTriVert < 4; ++iTriVert) {
336 const auto vertexIndex =
337 iTriVert < 3 ? constr.tgtTriangleIndices[iTriVert] : constr.srcVertexIndex;
338 const Eigen::Vector3<T>& vertexPos = meshState.posedMesh_->vertices[vertexIndex];
341 const Eigen::Matrix3<T> dfdv = iTriVert < 3
342 ? Eigen::Matrix3<T>(-d_targetPos_d_tgtTriVertexPos[iTriVert])
343 : Eigen::
Matrix3<T>::Identity();
345 skeletonDerivative.template accumulateVertexJacobian<3>(
346 vertexIndex, vertexPos, dfdv, wgt, state, meshState, character_, jac, 0);
351 return wgt * wgt * diff.squaredNorm();
355double PointTriangleVertexErrorFunctionT<T>::calculateNormalGradient(
360 const T sourceNormalWeight,
361 const T targetNormalWeight,
362 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
363 const Eigen::Vector3<T> sourceNormal = meshState.posedMesh_->normals[constr.srcVertexIndex];
367 T targetNormalSign = 1;
368 if (sourceNormal.dot(targetNormal) < 0) {
370 targetNormalSign = -1;
372 const Eigen::Vector3<T>
normal =
373 sourceNormalWeight * sourceNormal + targetNormalWeight * targetNormal;
376 const Eigen::Vector3<T> diff =
377 meshState.posedMesh_->vertices[constr.srcVertexIndex] - tgtPosition;
378 const T dist = diff.dot(
normal);
380 const T wgt = constr.weight * 2.0f * kPlaneWeight * this->weight_;
382 const auto d_targetPos_d_tgtTriVertexPos =
384 const auto d_targetNormal_d_tgtTriVertexPos =
387 for (
int iTriVert = 0; iTriVert < 4; ++iTriVert) {
388 const auto vertexIndex =
389 iTriVert < 3 ? constr.tgtTriangleIndices[iTriVert] : constr.srcVertexIndex;
391 const Eigen::Matrix3<T> d_targetPos_d_vertexPos = iTriVert < 3
392 ? Eigen::Matrix3<T>(-d_targetPos_d_tgtTriVertexPos[iTriVert])
393 : Eigen::
Matrix3<T>::Identity();
396 this->character_, *meshState.restMesh_, state, vertexIndex);
399 while (!skinningIter.finished()) {
400 const auto [jointIndex, boneWeight, pos] = skinningIter.next();
403 MT_CHECK(jointIndex < this->skeleton_.joints.size());
405 const auto& jointState = state.jointState[jointIndex];
407 const Eigen::Vector3<T> posd = pos - jointState.translation();
410 for (
size_t d = 0; d < 3; d++) {
411 if (this->activeJointParams_[paramIndex + d]) {
415 normal.dot(d_targetPos_d_vertexPos * jointState.getTranslationDerivative(d)) *
418 this->parameterTransform_,
421 if (this->activeJointParams_[paramIndex + 3 + d]) {
430 normal.dot(d_targetPos_d_vertexPos * jointState.getRotationDerivative(d, posd));
433 const Eigen::Vector3<T> diff_tgt_normal = targetNormalSign *
434 d_targetNormal_d_tgtTriVertexPos[iTriVert] *
435 jointState.getRotationDerivative(d, posd);
436 diff_total += targetNormalWeight * diff.dot(diff_tgt_normal);
439 const Eigen::Vector3<T> diff_src_normal =
440 jointState.getRotationDerivative(d, sourceNormal);
441 diff_total += sourceNormalWeight * diff.dot(diff_src_normal);
445 boneWeight * wgt * diff_total * dist,
447 this->parameterTransform_,
451 if (this->activeJointParams_[paramIndex + 6]) {
455 normal.dot(d_targetPos_d_vertexPos * jointState.getScaleDerivative(posd)) * dist,
457 this->parameterTransform_,
464 for (Eigen::Index iBlendShape = 0;
465 iBlendShape < this->parameterTransform_.blendShapeParameters.size();
467 const auto paramIdx = this->parameterTransform_.blendShapeParameters[iBlendShape];
472 const Eigen::Vector3<T> d_restPos =
473 this->character_.blendShape->getShapeVectors()
474 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
476 Eigen::Vector3<T> d_worldPos =
calculateDWorldPos(character_, state, vertexIndex, d_restPos);
478 gradient(paramIdx) += wgt * dist *
normal.dot(d_targetPos_d_vertexPos * d_worldPos);
480 const Eigen::Vector3<T> diff_tgt_normal =
481 d_targetNormal_d_tgtTriVertexPos[iTriVert] * d_worldPos;
482 gradient(paramIdx) += targetNormalSign * wgt * dist * diff.dot(diff_tgt_normal);
488 return constr.weight * dist * dist * kPlaneWeight;
492double PointTriangleVertexErrorFunctionT<T>::calculateNormalJacobian(
497 const T sourceNormalWeight,
498 const T targetNormalWeight,
499 Ref<Eigen::MatrixX<T>> jac,
501 const Eigen::Vector3<T> sourceNormal = meshState.posedMesh_->normals[constr.srcVertexIndex];
505 T targetNormalSign = 1;
506 if (sourceNormal.dot(targetNormal) < 0) {
508 targetNormalSign = -1;
510 const Eigen::Vector3<T>
normal =
511 sourceNormalWeight * sourceNormal + targetNormalWeight * targetNormal;
514 const Eigen::Vector3<T> diff =
515 meshState.posedMesh_->vertices[constr.srcVertexIndex] - tgtPosition;
516 const T dist = diff.dot(
normal);
518 const T wgt = std::sqrt(constr.weight * kPositionWeight * this->weight_);
520 const auto d_targetPos_d_tgtTriVertexPos =
522 const auto d_targetNormal_d_tgtTriVertexPos =
525 for (
int iTriVert = 0; iTriVert < 4; ++iTriVert) {
526 const auto vertexIndex =
527 iTriVert < 3 ? constr.tgtTriangleIndices[iTriVert] : constr.srcVertexIndex;
529 const Eigen::Matrix3<T> d_targetPos_d_vertexPos = iTriVert < 3
530 ? Eigen::Matrix3<T>(-d_targetPos_d_tgtTriVertexPos[iTriVert])
531 : Eigen::
Matrix3<T>::Identity();
534 this->character_, *meshState.restMesh_, state, vertexIndex);
537 while (!skinningIter.finished()) {
538 const auto [jointIndex, boneWeight, pos] = skinningIter.next();
541 MT_CHECK(jointIndex < this->skeleton_.joints.size());
543 const auto& jointState = state.jointState[jointIndex];
545 const Eigen::Vector3<T> posd = pos - jointState.translation();
548 for (
size_t d = 0; d < 3; d++) {
549 if (this->activeJointParams_[paramIndex + d]) {
553 normal.dot(d_targetPos_d_vertexPos * jointState.getTranslationDerivative(d)),
555 this->parameterTransform_,
558 if (this->activeJointParams_[paramIndex + 3 + d]) {
567 normal.dot(d_targetPos_d_vertexPos * jointState.getRotationDerivative(d, posd));
570 const Eigen::Vector3<T> diff_tgt_normal = targetNormalSign *
571 d_targetNormal_d_tgtTriVertexPos[iTriVert] *
572 jointState.getRotationDerivative(d, posd);
573 diff_total += targetNormalWeight * diff.dot(diff_tgt_normal);
576 const Eigen::Vector3<T> diff_src_normal =
577 jointState.getRotationDerivative(d, sourceNormal);
578 diff_total += sourceNormalWeight * diff.dot(diff_src_normal);
582 boneWeight * wgt * diff_total, paramIndex + d + 3, this->parameterTransform_, jac);
585 if (this->activeJointParams_[paramIndex + 6]) {
589 normal.dot(d_targetPos_d_vertexPos * jointState.getScaleDerivative(posd)),
591 this->parameterTransform_,
598 for (Eigen::Index iBlendShape = 0;
599 iBlendShape < this->parameterTransform_.blendShapeParameters.size();
601 const auto paramIdx = this->parameterTransform_.blendShapeParameters[iBlendShape];
606 const Eigen::Vector3<T> d_restPos =
607 this->character_.blendShape->getShapeVectors()
608 .template block<3, 1>(3 * vertexIndex, iBlendShape, 3, 1)
610 Eigen::Vector3<T> d_worldPos =
calculateDWorldPos(character_, state, vertexIndex, d_restPos);
612 jac(0, paramIdx) += wgt *
normal.dot(d_targetPos_d_vertexPos * d_worldPos);
614 const Eigen::Vector3<T> diff_tgt_normal =
615 d_targetNormal_d_tgtTriVertexPos[iTriVert] * d_worldPos;
616 jac(0, paramIdx) += targetNormalSign * wgt * diff.dot(diff_tgt_normal);
624 return wgt * wgt * dist * dist;
628std::pair<T, T> PointTriangleVertexErrorFunctionT<T>::computeNormalWeights()
const {
629 switch (constraintType_) {
636 return {T(0.5), T(0.5)};
645 Eigen::Ref<Eigen::VectorX<T>> gradient) {
651 for (
size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
652 error += calculatePositionGradient(
653 modelParameters, state, meshState, constraints_[iCons], gradient);
656 T sourceNormalWeight;
657 T targetNormalWeight;
658 std::tie(sourceNormalWeight, targetNormalWeight) = computeNormalWeights();
660 for (
size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
661 error += calculateNormalGradient(
672 return this->weight_ * error;
680 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
681 Eigen::Ref<Eigen::VectorX<T>> residual,
684 jacobian.cols() ==
static_cast<Eigen::Index
>(this->parameterTransform_.transform.cols()));
685 MT_CHECK(jacobian.rows() >= (Eigen::Index)(1 * constraints_.size()));
686 MT_CHECK(residual.rows() >= (Eigen::Index)(1 * constraints_.size()));
695 for (
size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
696 error += calculatePositionJacobian(
701 jacobian.block(3 * iCons, 0, 3, modelParameters.
size()),
702 residual.middleRows(3 * iCons, 3));
704 usedRows =
static_cast<int>(3 * constraints_.size());
707 T sourceNormalWeight;
708 T targetNormalWeight;
709 std::tie(sourceNormalWeight, targetNormalWeight) = computeNormalWeights();
711 for (
size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
712 error += calculateNormalJacobian(
719 jacobian.block(iCons, 0, 1, modelParameters.
size()),
723 usedRows =
static_cast<int>(constraints_.size());
731 switch (constraintType_) {
733 return 3 * constraints_.size();
738 return constraints_.size();
This file provides functions for applying blend shape deformations and skeletal transformations to ch...
#define MT_CHECK_NOTNULL(ptr,...)
Asserts that ptr is not null; on failure, throws std::runtime_error with the optional fmt-style messa...
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Support constraining different parts of the mesh together by specifying that a source vertex should t...
PointTriangleVertexErrorFunctionT(const Character &character, VertexConstraintType type=VertexConstraintType::Position)
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
size_t getNumVertices() const
size_t getJacobianSize() const final
static constexpr T kPlaneWeight
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
double getJacobian(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
static constexpr T kPositionWeight
~PointTriangleVertexErrorFunctionT() override
void addConstraint(int vertexIndex, const Eigen::Vector3i &triangleIndices, const Eigen::Vector3< T > &triangleBaryCoords, float depth=0, T weight=1)
Shared utility for derivative computation in error functions.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Functions for linear blend skinning (Skeletal Subspace Deformation).
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Eigen::Vector3< T > computeTargetBaryPosition(const MeshT< T > &mesh, const PointTriangleVertexConstraintT< T > &c)
Eigen::Matrix< T, 3, 3 > Matrix3
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
Eigen::Vector3< T > computeTargetPosition(const MeshT< T > &mesh, const PointTriangleVertexConstraintT< T > &c)
ParameterTransformT< float > ParameterTransform
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Eigen::Vector3< T > calculateDWorldPos(const momentum::Character &character, const SkeletonStateT< T > &state, const int vertexIndex, const Eigen::Vector3< T > &d_restPos)
Eigen::Vector3< T > computeTargetTriangleNormal(const MeshT< T > &mesh, const PointTriangleVertexConstraintT< T > &c)
constexpr size_t kParametersPerJoint
std::array< Eigen::Matrix3< T >, 3 > compute_d_targetPos_d_vertexPos(const PointTriangleVertexConstraintT< T > &cons, const MeshT< T > &mesh)
std::array< Eigen::Matrix3< T >, 3 > compute_d_targetNormal_d_vertexPos(const PointTriangleVertexConstraintT< T > &cons, const MeshT< T > &mesh)
Vector3P< S > cross(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes cross product of Eigen::Vector3f and Vector3P.
std::string_view toString(const LimitType type)
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
VertexConstraintType
Type of vertex constraint mode.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_EVENT(NAME)
#define MT_PROFILE_FUNCTION()
A character model with skeletal structure, mesh, and optional components.
TransformationList inverseBindPose
Inverse of the bind pose transformations for each joint.
SkinWeights_u skinWeights
Defines how mesh vertices are influenced by joints.
BlendShapeBase_const_p faceExpressionBlendShape
Facial expression blend shapes.
Mesh_u mesh
3D mesh representing the character's surface
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto size() const
Represents the complete state of a model's mesh.
std::unique_ptr< MeshT< T > > posedMesh_
Posed mesh after the skeleton transforms have been applied.
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
A vector of size numModelParams holding the values of all model parameters for a character.
Eigen::Vector3< T > tgtTriangleBaryCoords
Eigen::Vector3i tgtTriangleIndices
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.