22 const std::vector<T>& stencilCoefficients)
24 MT_CHECK(!stencilCoefficients_.empty(),
"Stencil coefficients cannot be empty");
25 targetWeights_.setOnes(skel.
joints.size());
26 targetValues_.resize(skel.
joints.size(), Eigen::Vector3<T>::Zero());
32 const std::vector<T>& stencilCoefficients)
35 character.parameterTransform,
36 stencilCoefficients) {}
40 targetWeights_.setOnes(this->
skeleton_.joints.size());
41 targetValues_.assign(this->
skeleton_.joints.size(), Eigen::Vector3<T>::Zero());
46 MT_CHECK(weights.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
47 targetWeights_ = weights;
52 const Eigen::Vector3<T>& targetValue) {
53 targetValues_.assign(this->
skeleton_.joints.size(), targetValue);
58 const std::vector<Eigen::Vector3<T>>& targetValues) {
59 MT_CHECK(targetValues.size() == this->skeleton_.joints.size());
60 targetValues_ = targetValues;
70 MT_CHECK(skelStates.size() == stencilCoefficients_.size());
74 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
75 if (targetWeights_[i] == 0) {
80 Eigen::Vector3<T> derivative = Eigen::Vector3<T>::Zero();
81 for (
size_t k = 0; k < stencilCoefficients_.size(); ++k) {
82 derivative += stencilCoefficients_[k] * skelStates[k].jointState[i].translation();
86 const Eigen::Vector3<T> residual = derivative - targetValues_[i];
88 error += residual.squaredNorm() * targetWeights_[i];
99 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
102 MT_CHECK(skelStates.size() == stencilCoefficients_.size());
105 MT_CHECK(gradient.size() ==
static_cast<Eigen::Index
>(stencilCoefficients_.size()) * nParam);
109 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
110 if (targetWeights_[i] == 0) {
115 Eigen::Vector3<T> derivative = Eigen::Vector3<T>::Zero();
116 for (
size_t k = 0; k < stencilCoefficients_.size(); ++k) {
117 derivative += stencilCoefficients_[k] * skelStates[k].jointState[i].translation();
119 const Eigen::Vector3<T> residual = derivative - targetValues_[i];
121 const T wgt = targetWeights_[i] * this->
weight_;
122 error += residual.squaredNorm() * wgt;
128 for (
size_t frameIdx = 0; frameIdx < stencilCoefficients_.size(); ++frameIdx) {
129 const auto& state = skelStates[frameIdx];
130 const T coeff = stencilCoefficients_[frameIdx];
133 size_t ancestorJointIndex = i;
138 const Eigen::Vector3<T> posd =
139 state.jointState[i].translation() - state.jointState[ancestorJointIndex].translation();
142 for (
size_t d = 0; d < 3; d++) {
144 const T val = T(2) * wgt * coeff *
145 residual.dot(state.jointState[ancestorJointIndex].getTranslationDerivative(d));
150 gradient.segment(frameIdx * nParam, nParam));
155 for (
size_t d = 0; d < 3; d++) {
157 const T val = T(2) * wgt * coeff *
158 residual.dot(state.jointState[ancestorJointIndex].getRotationDerivative(d, posd));
163 gradient.segment(frameIdx * nParam, nParam));
169 const T val = T(2) * wgt * coeff *
170 residual.dot(state.jointState[ancestorJointIndex].getScaleDerivative(posd));
175 gradient.segment(frameIdx * nParam, nParam));
178 ancestorJointIndex = this->
skeleton_.joints[ancestorJointIndex].parent;
189 const auto nActiveJoints = (targetWeights_.array() != 0).count();
190 return nActiveJoints * 3;
198 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
199 Eigen::Ref<Eigen::VectorX<T>> residual,
200 int& usedRows)
const {
203 MT_CHECK(skelStates.size() == stencilCoefficients_.size());
206 MT_CHECK(jacobian.cols() ==
static_cast<Eigen::Index
>(stencilCoefficients_.size()) * nParam);
209 Eigen::Index offset = 0;
211 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
212 if (targetWeights_[i] == 0) {
217 Eigen::Vector3<T> derivative = Eigen::Vector3<T>::Zero();
218 for (
size_t k = 0; k < stencilCoefficients_.size(); ++k) {
219 derivative += stencilCoefficients_[k] * skelStates[k].jointState[i].translation();
221 const Eigen::Vector3<T> res = derivative - targetValues_[i];
223 const T wgt = targetWeights_[i] * this->
weight_;
224 const T sqrtWgt = std::sqrt(wgt);
226 error += res.squaredNorm() * wgt;
227 residual.template segment<3>(offset).noalias() = res * sqrtWgt;
230 for (
size_t frameIdx = 0; frameIdx < stencilCoefficients_.size(); ++frameIdx) {
231 const auto& state = skelStates[frameIdx];
232 const T coeff = stencilCoefficients_[frameIdx] * sqrtWgt;
235 size_t ancestorJointIndex = i;
239 const auto& jointState = state.jointState[ancestorJointIndex];
241 const Eigen::Vector3<T> posd = state.jointState[i].translation() - jointState.translation();
244 for (
size_t d = 0; d < 3; d++) {
246 const Eigen::Vector3<T> jc = jointState.getTranslationDerivative(d) * coeff;
251 jacobian.block(offset, frameIdx * nParam, 3, nParam));
256 for (
size_t d = 0; d < 3; d++) {
258 const Eigen::Vector3<T> jc = jointState.getRotationDerivative(d, posd) * coeff;
263 jacobian.block(offset, frameIdx * nParam, 3, nParam));
269 const Eigen::Vector3<T> jc = jointState.getScaleDerivative(posd) * coeff;
274 jacobian.block(offset, frameIdx * nParam, 3, nParam));
277 ancestorJointIndex = this->
skeleton_.joints[ancestorJointIndex].parent;
284 usedRows = gsl::narrow_cast<int>(offset);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Base class for error functions that penalize finite difference derivatives of joint positions across ...
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates) const final
void setTargetValues(const std::vector< Eigen::Vector3< T > > &targetValues)
Set per-joint target values.
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
void setTargetValue(const Eigen::Vector3< T > &targetValue)
Set a single target value applied to all joints.
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
FiniteDifferenceSequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const std::vector< T > &stencilCoefficients)
void setTargetWeights(const Eigen::VectorX< T > &weights)
Set the per-joint weights for the derivative error.
size_t getJacobianSize() const final
void reset()
Reset weights to ones and target values to zero.
const ParameterTransform & parameterTransform_
VectorX< bool > activeJointParams_
const Skeleton & skeleton_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkeletonT< float > Skeleton
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)
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)
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
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.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...