24 character_(character) {
38 MT_CHECK(vertexIndex1 >= 0 && ((
size_t)vertexIndex1) < character_.mesh->vertices.size());
39 MT_CHECK(vertexIndex2 >= 0 && ((
size_t)vertexIndex2) < character_.mesh->vertices.size());
40 MT_CHECK(vertexIndex1 != vertexIndex2);
42 constraints_.push_back(
62 for (
const auto& constraint : constraints_) {
63 const auto& pos1 = meshState.
posedMesh_->vertices[constraint.vertexIndex1];
64 const auto& pos2 = meshState.
posedMesh_->vertices[constraint.vertexIndex2];
66 const T actualDistance = (pos1 - pos2).norm();
67 const T distanceDiff = actualDistance - constraint.targetDistance;
69 error += constraint.weight * distanceDiff * distanceDiff;
80 Eigen::Ref<Eigen::VectorX<T>> gradient) {
84 meshState.
posedMesh_,
"MeshState must have posed mesh for VertexVertexDistanceErrorFunction");
88 for (
const auto& constraint : constraints_) {
89 error += calculateGradient(modelParameters, state, meshState, constraint, gradient);
100 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
101 Eigen::Ref<Eigen::VectorX<T>> residual,
106 jacobian.cols() ==
static_cast<Eigen::Index
>(this->parameterTransform_.transform.cols()));
107 MT_CHECK(jacobian.rows() >=
static_cast<Eigen::Index
>(constraints_.size()));
108 MT_CHECK(residual.rows() >=
static_cast<Eigen::Index
>(constraints_.size()));
114 for (
size_t i = 0; i < constraints_.size(); i++) {
116 error += calculateJacobian(
121 jacobian.block(i, 0, 1, modelParameters.
size()),
123 residual(i) = residualValue;
126 usedRows =
static_cast<int>(constraints_.size());
132 return constraints_.size();
136double VertexVertexDistanceErrorFunctionT<T>::calculateJacobian(
141 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
148 const Eigen::Vector3<T> diff = pos1 - pos2;
149 const T actualDistance = diff.norm();
152 if (actualDistance == T(0)) {
157 const T distanceDiff = actualDistance - constraint.
targetDistance;
158 const Eigen::Vector3<T> distanceGradient = diff / actualDistance;
161 const T wgt = std::sqrt(constraint.
weight * this->weight_);
164 residual = wgt * distanceDiff;
168 const Eigen::Matrix<T, 1, 3> dfdv1 = distanceGradient.transpose();
169 const Eigen::Matrix<T, 1, 3> dfdv2 = -distanceGradient.transpose();
171 SkeletonDerivativeT<T> deriv(
173 this->parameterTransform_,
174 this->activeJointParams_,
175 this->enabledParameters_);
178 deriv.template accumulateVertexJacobian<1>(
179 constraint.
vertexIndex1, pos1, dfdv1, wgt, state, meshState, character_, jacobian, 0);
182 deriv.template accumulateVertexJacobian<1>(
183 constraint.
vertexIndex2, pos2, dfdv2, wgt, state, meshState, character_, jacobian, 0);
185 return wgt * wgt * distanceDiff * distanceDiff;
189double VertexVertexDistanceErrorFunctionT<T>::calculateGradient(
194 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
197 const auto& pos1 = meshState.posedMesh_->vertices[constraint.vertexIndex1];
198 const auto& pos2 = meshState.posedMesh_->vertices[constraint.vertexIndex2];
200 const Eigen::Vector3<T> diff = pos1 - pos2;
201 const T actualDistance = diff.norm();
204 if (actualDistance == T(0)) {
208 const T distanceDiff = actualDistance - constraint.targetDistance;
209 const Eigen::Vector3<T> distanceGradient = diff / actualDistance;
213 const Eigen::Matrix<T, 1, 3> dfdv1 = distanceGradient.transpose();
214 const Eigen::Matrix<T, 1, 3> dfdv2 = -distanceGradient.transpose();
219 const Eigen::Matrix<T, 1, 1> weightedResidual =
220 Eigen::Matrix<T, 1, 1>::Constant(T(2) * constraint.weight * this->weight_ * distanceDiff);
224 this->parameterTransform_,
225 this->activeJointParams_,
226 this->enabledParameters_);
229 deriv.template accumulateVertexGradient<1>(
230 constraint.vertexIndex1,
240 deriv.template accumulateVertexGradient<1>(
241 constraint.vertexIndex2,
250 return constraint.weight * distanceDiff * distanceDiff;
#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:
Shared utility for derivative computation in error functions.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Error function for vertex-to-vertex distance constraints.
VertexVertexDistanceErrorFunctionT(const Character &character)
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
void addConstraint(int vertexIndex1, int vertexIndex2, T weight, T targetDistance)
Add a vertex-to-vertex distance constraint.
void clearConstraints()
Clear all constraints.
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
~VertexVertexDistanceErrorFunctionT() override
size_t getJacobianSize() const final
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
A character model with skeletal structure, mesh, and optional components.
SkinWeights_u skinWeights
Defines how mesh vertices are influenced by joints.
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 vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.
Constraint for vertex-to-vertex distance errors.
int vertexIndex2
Second vertex index.
T weight
Constraint weight.
int vertexIndex1
First vertex index.
T targetDistance
Desired distance between the two vertices.