28template <
typename JetType>
32 const int parentJointIndex_cons,
33 const Eigen::Vector3<JetType>& offset_cons,
34 const JetType& weight_cons,
35 const Eigen::Matrix<JetType, 3, 4>& projection_cons,
36 const Eigen::Vector2<JetType>& target_cons,
37 Eigen::Ref<
const Eigen::VectorX<T>> vec)
const {
38 auto result = JetType();
40 const auto& jsCons = skelState.
jointState[parentJointIndex_cons];
41 const Eigen::Vector3<JetType> p_world_cm =
42 (jsCons.transform.toAffine3().
template cast<T>() * offset_cons).eval();
43 const Eigen::Vector3<JetType> p_projected_cm =
44 (projection_cons * p_world_cm.homogeneous()).eval();
51 const Eigen::Vector2<JetType> p_res = p_projected_cm.hnormalized() - target_cons;
55 const JetType& z = p_projected_cm(2);
56 const JetType zSqr = z * z;
57 const JetType x_zz = p_projected_cm(0) / zSqr;
58 const JetType y_zz = p_projected_cm(1) / zSqr;
63 auto add_gradient_dot = [&](
const size_t jFullBodyDOF,
64 const Eigen::Vector3<JetType>& d_p_world_cm) {
65 const Eigen::Vector3<JetType> d_p_projected =
66 (projection_cons.template topLeftCorner<3, 3>() * d_p_world_cm).eval();
67 const JetType& dx = d_p_projected(0);
68 const JetType& dy = d_p_projected(1);
69 const JetType& dz = d_p_projected(2);
71 const Eigen::Vector2<JetType> d_p_res(dx / z - x_zz * dz, dy / z - y_zz * dz);
77 const JetType gradFull = d_p_res.dot(p_res);
85 size_t jointIndex = parentJointIndex_cons;
88 assert(jointIndex < this->
skeleton_.joints.size());
90 const auto& jointState = skelState.
jointState[jointIndex];
91 const size_t paramIndex = jointIndex * 7;
92 const Eigen::Vector3<JetType> posd_cm = p_world_cm - jointState.translation();
95 for (
size_t d = 0; d < 3; d++) {
98 paramIndex + d, jointState.getTranslationDerivative(d).template
cast<JetType>());
112 jointIndex = this->
skeleton_.joints[jointIndex].parent;
120 const std::string& inputName,
123 Eigen::Ref<
const Eigen::VectorX<T>> inputVec) {
127 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(nCons);
128 using JetType = ceres::Jet<T, 1>;
129 for (
int iCons = 0; iCons < nCons; ++iCons) {
139 result(iCons) = dotProd.v[0];
143 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(3 * nCons);
145 using JetType = ceres::Jet<T, 3>;
146 for (
int iCons = 0; iCons < nCons; ++iCons) {
156 result.template segment<3>(3 * iCons) = dotProd.v;
160 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(2 * nCons);
161 using JetType = ceres::Jet<T, 2>;
162 for (
int iCons = 0; iCons < nCons; ++iCons) {
172 result.template segment<2>(2 * iCons) = dotProd.v;
176 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(12 * nCons);
177 using JetType = ceres::Jet<T, 12>;
178 for (
int iCons = 0; iCons < nCons; ++iCons) {
188 result.template segment<12>(12 * iCons) = dotProd.v;
192 throw std::runtime_error(
193 "Unknown input name in FullyDifferentiablePositionErrorFunction::d_gradient_d_input_dot: " +
207 const std::string&
name)
const {
217 throw std::runtime_error(
"Unknown input for ProjectionErrorFunction: " +
name);
223 const std::string&
name,
224 Eigen::Ref<Eigen::VectorX<T>> result)
const {
226 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
230 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
231 result.template segment<3>(3 * i) = this->
constraints_[i].offset;
234 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
235 result.template segment<2>(2 * i) = this->
constraints_[i].target;
238 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
242 for (
int jRow = 0; jRow < 3; ++jRow) {
243 for (
int kCol = 0; kCol < 4; ++kCol) {
244 result(12 * i + 4 * jRow + kCol) = proj(jRow, kCol);
249 throw std::runtime_error(
"Unknown input for ProjectionErrorFunction: " +
name);
255 const std::string&
name,
256 Eigen::Ref<
const Eigen::VectorX<T>> value) {
258 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
262 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
263 this->
constraints_[i].offset = value.template segment<3>(3 * i);
266 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
267 const Eigen::Vector2<T> target = value.template segment<2>(2 * i);
271 for (
size_t i = 0; i < this->
constraints_.size(); ++i) {
275 for (
int jRow = 0; jRow < 3; ++jRow) {
276 for (
int kCol = 0; kCol < 4; ++kCol) {
277 proj(jRow, kCol) = value(12 * i + 4 * jRow + kCol);
282 throw std::runtime_error(
"Unknown input for ProjectionErrorFunction: " +
name);
FullyDifferentiableProjectionErrorFunctionT(const momentum::Skeleton &skel, const momentum::ParameterTransform &pt, T nearClip=T(1))
JetType constraintGradient_dot(const momentum::ModelParametersT< T > &modelParams, const momentum::SkeletonStateT< T > &skelState, int parentJointIndex_cons, const Eigen::Vector3< JetType > &offset_cons, const JetType &weight_cons, const Eigen::Matrix< JetType, 3, 4 > &projection_cons, const Eigen::Vector2< JetType > &target_cons, Eigen::Ref< const Eigen::VectorX< T > > inputVec) const
static constexpr const char * kWeights
void getInputImp(const std::string &name, Eigen::Ref< Eigen::VectorX< T > > result) const override
static constexpr const char * kProjections
Eigen::Index getInputSize(const std::string &name) const override
static constexpr const char * kOffsets
const char * name() const override
void setInputImp(const std::string &name, Eigen::Ref< const Eigen::VectorX< T > > value) override
static constexpr const char * kTargets
std::vector< std::string > inputs() const override
Eigen::VectorX< T > d_gradient_d_input_dot(const std::string &inputName, const momentum::ModelParametersT< T > &modelParams, const momentum::SkeletonStateT< T > &state, Eigen::Ref< const Eigen::VectorX< T > > inputVec) override
ProjectionErrorFunctionT(const momentum::Skeleton &skel, const momentum::ParameterTransform &pt, T nearClip=T(1))
std::vector< ProjectionConstraintDataT< T > > constraints_
static constexpr T kProjectionWeight
const Skeleton & skeleton_
VectorX< bool > activeJointParams_
const ParameterTransform & parameterTransform_
SkeletonT< float > Skeleton
T times_parameterTransform_times_v(const T &value, const size_t jointParamIdx, const ParameterTransform ¶meterTransform, Eigen::Ref< const Eigen::VectorX< T2 > > v)
Eigen::Vector3< T > getScaleDerivative(const JointStateT< T2 > &js, const Eigen::Vector3< T > &ref)
Eigen::Matrix< ceres::Jet< T, N >, N, 1 > buildJetVec(const Eigen::Matrix< T, N, 1 > &v)
ParameterTransformT< float > ParameterTransform
Eigen::Matrix< ceres::Jet< T, Cols *Rows >, Rows, Cols > buildJetMat(const Eigen::Matrix< T, Rows, Cols > &m)
Eigen::Vector3< T > getRotationDerivative(const JointStateT< T2 > &js, const size_t index, const Eigen::Vector3< T > &ref)
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
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.
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.