26Eigen::Vector3<T> worldMassPosition(
30 if (constraint.offsets.empty()) {
31 return jointState.translation();
33 return jointState.transform * constraint.offsets[index];
37Eigen::Vector3<T> centerOfMass(
40 const T invTotalMass = T(1) / constraint.totalMass();
42 Eigen::Vector3<T> com = Eigen::Vector3<T>::Zero();
43 for (
size_t i = 0; i < constraint.jointIndices.size(); ++i) {
44 const Eigen::Vector3<T> worldPos =
45 worldMassPosition(jointStates[constraint.jointIndices[i]], constraint, i);
46 com += constraint.masses[i] * worldPos;
48 return com * invTotalMass;
52Eigen::Vector3<T> projectToPlane(
53 const Eigen::Vector3<T>& position,
56 constraint.projectionNormal *
57 (position.dot(constraint.projectionNormal) - constraint.projectionD);
62 if (!constraint.projectToPlane) {
63 return Eigen::Matrix<T, 3, 3>::Identity();
66 return Eigen::Matrix<T, 3, 3>::Identity() -
67 constraint.projectionNormal * constraint.projectionNormal.transpose();
71Eigen::Vector3<T> centerOfMassResidual(
72 const Eigen::Vector3<T>& com,
74 if (!constraint.projectToPlane) {
75 return com - constraint.target;
78 return projectToPlane(com, constraint) - constraint.target;
86 return std::accumulate(
masses.begin(),
masses.end(), T(0));
108 "Joint indices and masses must have the same size");
111 "Offsets must be empty or have the same size as joint indices");
117 MT_CHECK(jointIdx < this->
skeleton_.joints.size(),
"Invalid joint index: {}", jointIdx);
124 "Projection plane normal must contain only finite values");
126 std::isfinite(normalizedConstraint.
projectionD),
"Projection plane offset must be finite");
128 MT_CHECK(normalNorm > T(1e-6),
"Projection plane normal must be non-zero");
133 constraints_.push_back(std::move(normalizedConstraint));
138 constraints_.clear();
139 constraints_.reserve(constraints.size());
140 for (
const auto& constraint : constraints) {
147 constraints_.clear();
158 return constraints_.size();
166 if (state.
jointState.empty() || constraints_.empty()) {
172 for (
const auto& constr : constraints_) {
173 const Eigen::Vector3<T> com = centerOfMass(constr, state.
jointState);
174 const Eigen::Vector3<T> res = centerOfMassResidual(com, constr);
175 error += constr.weight * res.squaredNorm() * this->
weight_;
186 Eigen::Ref<Eigen::VectorX<T>> gradient) {
189 if (state.
jointState.empty() || constraints_.empty()) {
196 Eigen::VectorX<T> jointGrad =
198 std::vector<Eigen::Vector3<T>> worldPositions;
200 for (
const auto& constr : constraints_) {
201 const T invTotalMass = T(1) / constr.totalMass();
202 const bool hasOffsets = !constr.offsets.empty();
204 worldPositions.resize(constr.jointIndices.size());
208 Eigen::Vector3<T> com = Eigen::Vector3<T>::Zero();
209 for (
size_t i = 0; i < constr.jointIndices.size(); ++i) {
210 const Eigen::Vector3<T> worldPos =
211 worldMassPosition(state.
jointState[constr.jointIndices[i]], constr, i);
213 worldPositions[i] = worldPos;
215 com += constr.masses[i] * worldPos;
220 const Eigen::Vector3<T> residual = centerOfMassResidual(com, constr);
221 error += constr.weight * residual.squaredNorm() * this->
weight_;
223 const T wgt = T(2) * constr.weight * this->
weight_;
228 const Eigen::Vector3<T> weightedRes = wgt * residual;
229 const Eigen::Matrix<T, 3, 3> dResidualDCom = residualDerivative(constr);
231 for (
size_t i = 0; i < constr.jointIndices.size(); ++i) {
232 const size_t jointIdx = constr.jointIndices[i];
233 const T normalizedMass = constr.masses[i] * invTotalMass;
234 const Eigen::Vector3<T> worldPos =
235 hasOffsets ? worldPositions[i] : state.
jointState[jointIdx].translation();
237 const Eigen::Matrix<T, 3, 3> dfdv = normalizedMass * dResidualDCom;
238 std::array<Eigen::Vector3<T>, 1> worldVecs = {worldPos};
239 std::array<Eigen::Matrix<T, 3, 3>, 1> dfdvArr = {dfdv};
240 std::array<uint8_t, 1> isPoints = {1};
242 skeletonDerivative_.template accumulateJointGradient<3>(
244 std::span<
const Eigen::Vector3<T>>(worldVecs.data(), 1),
245 std::span<
const Eigen::Matrix<T, 3, 3>>(dfdvArr.data(), 1),
246 std::span<const uint8_t>(isPoints.data(), 1),
253 gradient += jointGrad;
262 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
263 Eigen::Ref<Eigen::VectorX<T>> residual,
267 if (state.
jointState.empty() || constraints_.empty()) {
274 std::vector<Eigen::Vector3<T>> worldPositions;
276 for (
const auto& constr : constraints_) {
277 const T invTotalMass = T(1) / constr.totalMass();
278 const bool hasOffsets = !constr.offsets.empty();
280 worldPositions.resize(constr.jointIndices.size());
284 Eigen::Vector3<T> com = Eigen::Vector3<T>::Zero();
285 for (
size_t i = 0; i < constr.jointIndices.size(); ++i) {
286 const Eigen::Vector3<T> worldPos =
287 worldMassPosition(state.
jointState[constr.jointIndices[i]], constr, i);
289 worldPositions[i] = worldPos;
291 com += constr.masses[i] * worldPos;
296 const Eigen::Vector3<T> res = centerOfMassResidual(com, constr);
297 error += constr.weight * res.squaredNorm() * this->
weight_;
299 const T wgt = std::sqrt(constr.weight * this->weight_);
300 residual.template segment<3>(rowIndex) = wgt * res;
301 const Eigen::Matrix<T, 3, 3> dResidualDCom = residualDerivative(constr);
304 for (
size_t i = 0; i < constr.jointIndices.size(); ++i) {
305 const size_t jointIdx = constr.jointIndices[i];
306 const T normalizedMass = constr.masses[i] * invTotalMass;
307 const Eigen::Vector3<T> worldPos =
308 hasOffsets ? worldPositions[i] : state.
jointState[jointIdx].translation();
310 const Eigen::Matrix<T, 3, 3> dfdv = normalizedMass * dResidualDCom;
311 std::array<Eigen::Vector3<T>, 1> worldVecs = {worldPos};
312 std::array<Eigen::Matrix<T, 3, 3>, 1> dfdvArr = {dfdv};
313 std::array<uint8_t, 1> isPoints = {1};
315 skeletonDerivative_.template accumulateJointJacobian<3>(
317 std::span<
const Eigen::Vector3<T>>(worldVecs.data(), 1),
318 std::span<
const Eigen::Matrix<T, 3, 3>>(dfdvArr.data(), 1),
319 std::span<const uint8_t>(isPoints.data(), 1),
329 usedRows =
static_cast<int>(rowIndex);
335 return 3 * constraints_.size();
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Error function that constrains the center of mass to a target position.
size_t getJacobianSize() const final
CenterOfMassErrorFunctionT(const Skeleton &skeleton, const ParameterTransform ¶meterTransform)
Constructs the error function.
void clearConstraints()
Clears all constraints.
void addConstraint(const ConstraintType &constraint)
Adds a center of mass constraint.
double getJacobian(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) override
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
const std::vector< ConstraintType > & getConstraints() const
Returns the current constraints.
size_t getNumConstraints() const
Returns the number of constraints.
void setConstraints(std::span< const ConstraintType > constraints)
Sets all constraints, replacing any existing ones.
CenterOfMassConstraintT< T > ConstraintType
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
const Skeleton & skeleton_
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
VectorX< bool > activeJointParams_
const ParameterTransform & parameterTransform_
ParameterSet enabledParameters_
SkeletonT< float > Skeleton
ParameterTransformT< float > ParameterTransform
std::vector< JointStateT< T > > JointStateListT
Structure describing the state of all joints in a skeleton.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Constraint data for a center of mass target.
T totalMass() const
Returns the total mass (sum of all masses).
bool projectToPlane
If true, project the computed center of mass to projectionNormal dot x - projectionD = 0 before compa...
T projectionD
Offset for the optional projection plane in the equation normal dot x - d = 0.
std::vector< Eigen::Vector3< T > > offsets
Optional local center-of-mass offsets for each joint.
std::vector< T > masses
Mass values for each joint (must match jointIndices size).
std::vector< size_t > jointIndices
Indices of joints contributing to this CoM constraint.
Eigen::Vector3< T > projectionNormal
Normal for the optional projection plane.
Represents the state of a joint with local and global transformations.
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.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.