20 Eigen::Ref<
const Eigen::VectorX<T>> in,
21 const std::vector<size_t>& mp) {
22 Eigen::VectorX<T> res = Eigen::VectorX<T>::Zero(mp.size());
23 for (
size_t i = 0; i < mp.size(); i++) {
37 std::vector<std::string> names)
39 names_(std::move(names)) {}
51 MT_THROW_IF(W.empty(),
"Empty W matrix in setPosePrior().");
53 for (
const auto& W_i : W) {
55 W_i.rows() != W[0].rows() || W_i.cols() != W[0].cols(),
56 "Expected matching dimensions for all matrices in W in setPosePrior.");
60 mmu_ = std::move(mmu);
64 auto mppca = std::make_shared<MppcaT<T>>();
68 "Mismatch in pose prior; expected number of names to match dimension.");
80 const std::string&
name)
const {
82 return this->
pi_.size();
84 return this->
mmu_.size();
86 Eigen::Index result = 0;
87 for (
const auto& W_i :
W_) {
95 "Unknown input to FullyDifferentiablePosePriorErrorFunctionT<T>::getInputSize: {}",
name);
101 const std::string&
name,
102 Eigen::Ref<Eigen::VectorX<T>> value)
const {
107 const Eigen::Index p = posePrior.p;
108 const Eigen::Index d = posePrior.d;
118 for (Eigen::Index iMix = 0; iMix < p; ++iMix) {
119 value.segment(iMix * d, d) = this->
mmu_.row(iMix).template
cast<T>();
123 Eigen::Index offset = 0;
124 for (Eigen::Index iMix = 0; iMix < p; ++iMix) {
125 const auto& W_i = this->
W_[iMix];
127 for (Eigen::Index j = 0; j < W_i.cols(); ++j) {
128 value.segment(offset, d) = W_i.col(j).template
cast<T>();
137 "Unknown input to FullyDifferentiablePosePriorErrorFunctionT<T>::getInputSize: {}",
name);
143 const std::string&
name,
144 Eigen::Ref<
const Eigen::VectorX<T>> value) {
149 const Eigen::Index p = posePrior.p;
150 const Eigen::Index d = posePrior.d;
160 for (Eigen::Index iMix = 0; iMix < p; ++iMix) {
161 this->
mmu_.row(iMix) = value.segment(iMix * d, d);
165 Eigen::Index offset = 0;
166 for (Eigen::Index iMix = 0; iMix < p; ++iMix) {
167 auto& W_i = this->
W_[iMix];
169 for (Eigen::Index j = 0; j < W_i.cols(); ++j) {
170 W_i.col(j) = value.segment(offset, d).transpose();
176 this->
sigma_ = value.cwiseMax(0);
179 "Unknown input to FullyDifferentiablePosePriorErrorFunctionT<T>::getInputSize: {}",
name);
183 auto mppca = std::make_shared<MppcaT<T>>();
191 const std::string& inputName,
194 Eigen::Ref<
const Eigen::VectorX<T>> inputVec) {
199 const Eigen::Index d = posePrior.d;
201 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(
getInputSize(inputName));
203 Eigen::VectorX<T> bestDiff;
214 if (inputName ==
kMu) {
217 result.segment(modeIdx * d, d) = -weight * posePrior.Cinv[modeIdx] * subInput;
218 }
else if (inputName ==
kSigma) {
241 const T sigma =
sigma_[modeIdx];
242 const Eigen::VectorX<T> diff = subParams - posePrior.mu.row(modeIdx).transpose();
243 result(modeIdx) = (-T(2) * sigma * weight) *
244 (posePrior.Cinv[modeIdx] * subInput).dot(posePrior.Cinv[modeIdx] * diff);
245 }
else if (inputName ==
kW) {
277 const Eigen::MatrixX<T>& W_i =
W_[modeIdx];
279 const Eigen::Index W_cols = W_i.cols();
281 const Eigen::Index result_offset = W_cols * d * modeIdx;
283 const Eigen::MatrixX<T>& Cinv = posePrior.Cinv[modeIdx];
285 const Eigen::VectorX<T> dLoss_times_Cinv = Cinv * subInput;
286 const Eigen::VectorX<T> diff = subParams - posePrior.mu.row(modeIdx).transpose();
287 const Eigen::VectorX<T> Cinv_times_diff = Cinv * diff;
289 for (Eigen::Index j = 0; j < d; ++j) {
290 for (Eigen::Index k = 0; k < W_cols; ++k) {
291 const T dGrad_dInput_dot_v = -weight *
292 (dLoss_times_Cinv.dot(W_i.col(k)) * Cinv_times_diff[j] +
293 W_i.col(k).dot(Cinv_times_diff) * dLoss_times_Cinv[j]);
294 result(result_offset + k * d + j) = dGrad_dInput_dot_v;
297 }
else if (inputName ==
kPi) {
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
static constexpr const char * kW
const char * name() const final
std::vector< std::string > inputs() const final
void setPosePrior(const VectorX< T > &pi, const MatrixX< T > &mmu, const std::vector< MatrixX< T > > &W, const VectorX< T > &sigma)
static constexpr const char * kSigma
std::vector< Eigen::MatrixX< T > > W_
Eigen::Index getInputSize(const std::string &name) const final
void getInputImp(const std::string &name, Eigen::Ref< Eigen::VectorX< T > > value) const final
const std::vector< std::string > names_
void setInputImp(const std::string &name, Eigen::Ref< const Eigen::VectorX< T > > value) final
~FullyDifferentiablePosePriorErrorFunctionT() override
Eigen::VectorX< T > d_gradient_d_input_dot(const std::string &inputName, const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, Eigen::Ref< const Eigen::VectorX< T > > inputVec) final
FullyDifferentiablePosePriorErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, std::vector< std::string > names)
static constexpr const char * kPi
Eigen::VectorX< T > sigma_
static constexpr const char * kMu
void setPosePrior(std::shared_ptr< const MppcaT< T > > pp)
std::shared_ptr< const MppcaT< T > > posePrior_
void getBestFitMode(const ModelParametersT< T > ¶ms, size_t &bestIdx, Eigen::VectorX< T > &bestDiff, T &bestR, T &minDist) const
static constexpr T kPosePriorWeight
PosePriorErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, std::shared_ptr< const MppcaT< T > > pp)
std::vector< size_t > ppMap_
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
#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
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Eigen::VectorX< T > mapParameters(const ModelParametersT< T > &in, const std::vector< size_t > &mp)
constexpr T pi() noexcept
Returns the mathematical constant pi.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
A vector of size numModelParams holding the values of all model parameters for a character.
Mixture of Probabilistic Principal Component Analyzers (MPPCA).
Represents the complete state of a skeleton.