Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
center_of_mass_error_function.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
13
14#include <Eigen/Core>
15
16#include <string>
17#include <vector>
18
19namespace momentum {
20
40template <typename T>
43 std::vector<size_t> jointIndices;
44
46 std::vector<T> masses;
47
53 std::vector<Eigen::Vector3<T>> offsets;
54
56 Eigen::Vector3<T> target = Eigen::Vector3<T>::Zero();
57
62 bool projectToPlane = false;
63
65 Eigen::Vector3<T> projectionNormal = Eigen::Vector3<T>::UnitY();
66
68 T projectionD = T(0);
69
71 T weight = T(1);
72
74 std::string name;
75
77 [[nodiscard]] T totalMass() const;
78};
79
110template <typename T>
112 public:
114
120 const Skeleton& skeleton,
121 const ParameterTransform& parameterTransform);
122
123 ~CenterOfMassErrorFunctionT() override = default;
124
125 [[nodiscard]] double getError(
126 const ModelParametersT<T>& params,
127 const SkeletonStateT<T>& state,
128 const MeshStateT<T>& meshState) final;
129
131 const ModelParametersT<T>& params,
132 const SkeletonStateT<T>& state,
133 const MeshStateT<T>& meshState,
134 Eigen::Ref<Eigen::VectorX<T>> gradient) override;
135
137 const ModelParametersT<T>& params,
138 const SkeletonStateT<T>& state,
139 const MeshStateT<T>& meshState,
140 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
141 Eigen::Ref<Eigen::VectorX<T>> residual,
142 int& usedRows) override;
143
144 [[nodiscard]] size_t getJacobianSize() const final;
145
149 void addConstraint(const ConstraintType& constraint);
150
154 void setConstraints(std::span<const ConstraintType> constraints);
155
158
160 [[nodiscard]] const std::vector<ConstraintType>& getConstraints() const;
161
163 [[nodiscard]] size_t getNumConstraints() const;
164
165 private:
166 std::vector<ConstraintType> constraints_;
167 SkeletonDerivativeT<T> skeletonDerivative_;
168};
169
170} // namespace momentum
CenterOfMassErrorFunctionT(const Skeleton &skeleton, const ParameterTransform &parameterTransform)
Constructs the error function.
void addConstraint(const ConstraintType &constraint)
double getJacobian(const ModelParametersT< T > &params, 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 > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
const std::vector< ConstraintType > & getConstraints() const
void setConstraints(std::span< const ConstraintType > constraints)
~CenterOfMassErrorFunctionT() override=default
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
Shared utility for derivative computation in error functions.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
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...
Eigen::Vector3< T > target
Target position for the center of mass.
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.
std::string name
Optional name for debugging.
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
A vector of size numModelParams holding the values of all model parameters for a character.
Definition types.h:99
Represents the complete state of a skeleton.