31 character_(character) {
32 MT_CHECK(
static_cast<bool>(character_.mesh));
33 MT_CHECK(
static_cast<bool>(character_.skinWeights));
45 const Eigen::Vector3<T>& targetVelocity) {
46 MT_CHECK(vertexIndex >= 0 && ((
size_t)vertexIndex) < character_.mesh->vertices.size());
56 MT_CHECK(modelParameters.size() == 2);
65 for (
const auto& constraint : constraints_) {
66 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.vertexIndex];
67 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.vertexIndex];
70 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
73 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.targetVelocity;
75 error += constraint.weight * velocityDiff.squaredNorm() *
kVelocityWeight;
82double VertexSequenceErrorFunctionT<T>::calculateVelocityGradient(
86 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
87 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.
vertexIndex];
88 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.
vertexIndex];
90 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
91 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.
targetVelocity;
93 const T wgt = constraint.
weight * 2.0f * kVelocityWeight * this->weight_;
95 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
97 Eigen::Ref<Eigen::VectorX<T>> grad0 = gradient.segment(0, nParam);
98 Eigen::Ref<Eigen::VectorX<T>> grad1 = gradient.segment(nParam, nParam);
103 character_, *meshStates[0].restMesh_, skelStates[0], constraint.
vertexIndex);
105 while (!skinningIter.finished()) {
106 size_t jointIndex = 0;
108 Eigen::Vector3<T> pos;
109 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
111 MT_CHECK(jointIndex < this->skeleton_.joints.size());
113 const auto& jointState = skelStates[0].jointState[jointIndex];
115 const Eigen::Vector3<T> posd = pos - jointState.translation();
117 for (
size_t d = 0; d < 3; d++) {
118 if (this->activeJointParams_[paramIndex + d]) {
121 -boneWeight * velocityDiff.dot(jointState.getTranslationDerivative(d)) * wgt,
123 this->parameterTransform_,
126 if (this->activeJointParams_[paramIndex + 3 + d]) {
128 -boneWeight * velocityDiff.dot(jointState.getRotationDerivative(d, posd)) * wgt,
130 this->parameterTransform_,
134 if (this->activeJointParams_[paramIndex + 6]) {
136 -boneWeight * velocityDiff.dot(jointState.getScaleDerivative(posd)) * wgt,
138 this->parameterTransform_,
147 character_, *meshStates[1].restMesh_, skelStates[1], constraint.
vertexIndex);
149 while (!skinningIter.finished()) {
150 size_t jointIndex = 0;
152 Eigen::Vector3<T> pos;
153 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
155 MT_CHECK(jointIndex < this->skeleton_.joints.size());
157 const auto& jointState = skelStates[1].jointState[jointIndex];
159 const Eigen::Vector3<T> posd = pos - jointState.translation();
161 for (
size_t d = 0; d < 3; d++) {
162 if (this->activeJointParams_[paramIndex + d]) {
165 boneWeight * velocityDiff.dot(jointState.getTranslationDerivative(d)) * wgt,
167 this->parameterTransform_,
170 if (this->activeJointParams_[paramIndex + 3 + d]) {
172 boneWeight * velocityDiff.dot(jointState.getRotationDerivative(d, posd)) * wgt,
174 this->parameterTransform_,
178 if (this->activeJointParams_[paramIndex + 6]) {
180 boneWeight * velocityDiff.dot(jointState.getScaleDerivative(posd)) * wgt,
182 this->parameterTransform_,
188 return constraint.
weight * velocityDiff.squaredNorm() * kVelocityWeight;
196 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
198 MT_CHECK(modelParameters.size() == 2);
207 for (
const auto& constraint : constraints_) {
208 error += calculateVelocityGradient(skelStates, meshStates, constraint, gradient);
211 return this->weight_ * error;
215double VertexSequenceErrorFunctionT<T>::calculateVelocityJacobian(
219 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
220 Eigen::Ref<Eigen::VectorX<T>> residual,
221 Eigen::Index startRow)
const {
222 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.
vertexIndex];
223 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.
vertexIndex];
225 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
226 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.
targetVelocity;
228 const T wgt = std::sqrt(constraint.
weight * kVelocityWeight * this->weight_);
230 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
235 character_, *meshStates[0].restMesh_, skelStates[0], constraint.
vertexIndex);
237 while (!skinningIter.finished()) {
238 size_t jointIndex = 0;
240 Eigen::Vector3<T> pos;
241 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
243 MT_CHECK(jointIndex < this->skeleton_.joints.size());
245 const auto& jointState = skelStates[0].jointState[jointIndex];
247 const Eigen::Vector3<T> posd = pos - jointState.translation();
249 for (
size_t d = 0; d < 3; d++) {
250 if (this->activeJointParams_[paramIndex + d]) {
252 -boneWeight * wgt * jointState.getTranslationDerivative(d),
254 this->parameterTransform_,
255 jacobian.block(startRow, 0, 3, nParam));
257 if (this->activeJointParams_[paramIndex + 3 + d]) {
259 -boneWeight * wgt * jointState.getRotationDerivative(d, posd),
261 this->parameterTransform_,
262 jacobian.block(startRow, 0, 3, nParam));
265 if (this->activeJointParams_[paramIndex + 6]) {
267 -boneWeight * wgt * jointState.getScaleDerivative(posd),
269 this->parameterTransform_,
270 jacobian.block(startRow, 0, 3, nParam));
278 character_, *meshStates[1].restMesh_, skelStates[1], constraint.
vertexIndex);
280 while (!skinningIter.finished()) {
281 size_t jointIndex = 0;
283 Eigen::Vector3<T> pos;
284 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
286 MT_CHECK(jointIndex < this->skeleton_.joints.size());
288 const auto& jointState = skelStates[1].jointState[jointIndex];
290 const Eigen::Vector3<T> posd = pos - jointState.translation();
292 for (
size_t d = 0; d < 3; d++) {
293 if (this->activeJointParams_[paramIndex + d]) {
295 boneWeight * wgt * jointState.getTranslationDerivative(d),
297 this->parameterTransform_,
298 jacobian.block(startRow, nParam, 3, nParam));
300 if (this->activeJointParams_[paramIndex + 3 + d]) {
302 boneWeight * wgt * jointState.getRotationDerivative(d, posd),
304 this->parameterTransform_,
305 jacobian.block(startRow, nParam, 3, nParam));
308 if (this->activeJointParams_[paramIndex + 6]) {
310 boneWeight * wgt * jointState.getScaleDerivative(posd),
312 this->parameterTransform_,
313 jacobian.block(startRow, nParam, 3, nParam));
318 residual.segment(startRow, 3) = velocityDiff * wgt;
320 return wgt * wgt * velocityDiff.squaredNorm();
328 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
329 Eigen::Ref<Eigen::VectorX<T>> residual,
330 int& usedRows)
const {
332 MT_CHECK(modelParameters.size() == 2);
339 MT_CHECK(jacobian.cols() == 2 * nParam);
340 MT_CHECK(jacobian.rows() >= (Eigen::Index)(3 * constraints_.size()));
341 MT_CHECK(residual.rows() >= (Eigen::Index)(3 * constraints_.size()));
346 for (
size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
347 error += calculateVelocityJacobian(
348 skelStates, meshStates, constraints_[iCons], jacobian, residual, 3 * iCons);
351 usedRows = gsl::narrow_cast<int>(3 * constraints_.size());
357 return 3 * 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:
const ParameterTransform & parameterTransform_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Error function that penalizes differences in vertex velocity between source and target motion.
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates) const final
void addConstraint(int vertexIndex, T weight, const Eigen::Vector3< T > &targetVelocity)
Add a vertex velocity constraint.
double getJacobian(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) const final
size_t getJacobianSize() const final
VertexSequenceErrorFunctionT(const Character &character)
static constexpr T kVelocityWeight
void clearConstraints()
Clear all vertex velocity constraints.
double getGradient(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::VectorX< T > > gradient) const final
Functions for linear blend skinning (Skeletal Subspace Deformation).
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)
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
constexpr size_t kParametersPerJoint
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.
Represents the complete state of a model's mesh.
A vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.
Constraint structure for vertex velocity.
Eigen::Vector3< T > targetVelocity