21 MT_THROW_IF(errorFunctions_.empty(),
"No error functions in union?");
23 for (
size_t i = 0; i < errorFunctions_.size(); ++i) {
25 std::dynamic_pointer_cast<FullyDifferentiableSkeletonErrorFunctionT<T>>(errorFunctions_[i]);
26 diffErrorFunctions_.push_back(diff_e);
29 for (
const auto& input : diff_e->inputs()) {
30 inputs_[input].push_back(i);
36 std::vector<std::string> names;
37 for (
const auto& e : diffErrorFunctions_) {
39 names.emplace_back(e->name());
41 names.emplace_back(
"Unknown");
45 std::ostringstream
name;
47 if (std::adjacent_find(std::begin(names), std::end(names), std::not_equal_to<>()) ==
50 name << names.front() <<
" x " << names.size();
53 for (
const auto& n : names) {
72 for (
auto& fun : errorFunctions_) {
73 error += fun->getError(params, state, meshState);
83 Eigen::Ref<Eigen::VectorX<T>> gradient) {
85 for (
auto& fun : errorFunctions_) {
86 error += fun->getGradient(params, state, meshState, gradient);
97 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
98 Eigen::Ref<Eigen::VectorX<T>> residual,
101 size_t jacobianStart = 0;
102 for (
auto& fun : errorFunctions_) {
103 const size_t memberJacobianSize = fun->getJacobianSize();
105 error += fun->getJacobian(
109 jacobian.block(jacobianStart, 0, memberJacobianSize, jacobian.cols()),
110 residual.segment(jacobianStart, memberJacobianSize),
113 jacobianStart += memberJacobianSize;
116 jacobian *= std::sqrt(this->
weight_);
117 residual *= std::sqrt(this->
weight_);
119 usedRows =
static_cast<int>(jacobian.rows());
127 for (
const auto& e : errorFunctions_) {
128 result += e->getJacobianSize();
135 std::vector<std::string> result;
136 result.reserve(inputs_.size());
137 for (
const auto& i : inputs_) {
138 result.push_back(i.first);
145 auto itr = inputs_.find(
name);
147 itr == inputs_.end(),
"Invalid input '{}' for UnionErrorFunction::getInputSize",
name);
149 Eigen::Index result = 0;
150 for (
const auto& iErrFun : itr->second) {
151 const auto errorFun = diffErrorFunctions_[iErrFun];
152 result += errorFun->getInputSize(
name);
159 const std::string&
name,
160 Eigen::Ref<Eigen::VectorX<T>> result)
const {
161 auto itr = inputs_.find(
name);
162 MT_THROW_IF(itr == inputs_.end(),
"Invalid input '{}' for UnionErrorFunction::getInput",
name);
164 Eigen::Index currentOffset = 0;
165 for (
const auto& iErrFun : itr->second) {
166 const auto errorFun = diffErrorFunctions_[iErrFun];
167 const Eigen::Index sz = errorFun->getInputSize(
name);
168 errorFun->getInput(
name, result.segment(currentOffset, sz));
175 const std::string&
name,
176 Eigen::Ref<
const Eigen::VectorX<T>> value) {
177 auto itr = inputs_.find(
name);
178 MT_THROW_IF(itr == inputs_.end(),
"Invalid input '{}' for UnionErrorFunction::setInput",
name);
180 Eigen::Index currentOffset = 0;
181 for (
const auto& iErrFun : itr->second) {
182 const auto errorFun = diffErrorFunctions_[iErrFun];
183 const Eigen::Index sz = errorFun->getInputSize(
name);
184 errorFun->setInput(
name, value.segment(currentOffset, sz));
191 const std::string&
name,
194 Eigen::Ref<
const Eigen::VectorX<T>> inputVec) {
195 auto itr = inputs_.find(
name);
197 itr == inputs_.end(),
198 "Invalid input '{}' for UnionErrorFunction::d_gradient_d_input_dot",
201 Eigen::Index currentOffset = 0;
204 for (
const auto& iErrFun : itr->second) {
205 const auto errFun = diffErrorFunctions_[iErrFun];
206 const Eigen::Index sz = errFun->getInputSize(
name);
207 result.segment(currentOffset, sz) =
208 errFun->d_gradient_d_input_dot(
name, modelParams, state, inputVec);
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
void setInputImp(const std::string &name, Eigen::Ref< const Eigen::VectorX< T > > value) override
size_t getJacobianSize() const final
void getInputImp(const std::string &name, Eigen::Ref< Eigen::VectorX< T > > result) const override
UnionErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const std::vector< std::shared_ptr< SkeletonErrorFunctionT< T > > > &errorFunctions)
Eigen::VectorX< T > d_gradient_d_input_dot(const std::string &inputName, const ModelParametersT< T > &modelParams, const SkeletonStateT< T > &state, Eigen::Ref< const Eigen::VectorX< T > > inputVec) override
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
Eigen::Index getInputSize(const std::string &name) const override
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) final
const char * name() const override
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
std::vector< std::string > inputs() const override
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
SkeletonT< float > Skeleton
ParameterTransformT< float > ParameterTransform
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.