31 return {
"targetPositionWeights",
"targetRotationWeights",
"targetState"};
36 const std::string&
name)
const {
37 if (
name ==
"targetPositionWeights" ||
name ==
"targetRotationWeights") {
39 }
else if (
name ==
"targetState") {
42 MT_THROW(
"Unknown input to FullyDifferentiableMotionErrorFunctionT: {}",
name);
48 const std::string&
name,
49 Eigen::Ref<Eigen::VectorX<T>> value)
const {
50 if (
name ==
"targetPositionWeights") {
52 }
else if (
name ==
"targetRotationWeights") {
54 }
else if (
name ==
"targetState") {
57 for (
size_t iJoint = 0; iJoint < this->
skeleton_.joints.size(); ++iJoint) {
58 value.template segment<3>(8 * iJoint + 0) = targetState[iJoint].translation;
59 value.template segment<4>(8 * iJoint + 3) =
60 targetState[iJoint].rotation.normalized().coeffs();
61 value(8 * iJoint + 7) = targetState[iJoint].scale;
64 MT_THROW(
"Unknown input to FullyDifferentiableMotionErrorFunctionT: {}",
name);
70 const std::string&
name,
71 Eigen::Ref<
const Eigen::VectorX<T>> value) {
72 if (
name ==
"targetPositionWeights") {
74 }
else if (
name ==
"targetRotationWeights") {
76 }
else if (
name ==
"targetState") {
78 for (
size_t iJoint = 0; iJoint < this->
skeleton_.joints.size(); ++iJoint) {
79 const Eigen::Vector3<T> pos = value.template segment<3>(8 * iJoint + 0);
80 const Eigen::Quaternion<T> rot =
81 Eigen::Quaternion<T>(value.template segment<4>(8 * iJoint + 3)).normalized();
82 const T scale = value(8 * iJoint + 7);
87 MT_THROW(
"Unknown input to FullyDifferentiableMotionErrorFunctionT: {}",
name);
92template <
typename JetType>
93JetType FullyDifferentiableStateErrorFunctionT<T>::calculateGradient_dot(
96 const Eigen::Vector3<JetType>& targetTranslation,
97 const Eigen::Quaternion<JetType>& targetRotation,
98 const JetType& targetPositionWeight,
99 const JetType& targetRotationWeight,
100 Eigen::Ref<
const Eigen::VectorX<T>> vec) {
102 Expects(state.
jointState.size() == gsl::narrow<Eigen::Index>(this->skeleton_.joints.size()));
107 const Eigen::Quaternion<T>& rot = state.
jointState[iJoint].rotation();
108 const Eigen::Matrix3<JetType> rotDiff =
109 rot.toRotationMatrix() - targetRotation.toRotationMatrix();
111 this->weight_ * targetRotationWeight;
114 const Eigen::Vector3<JetType> diff = state.
jointState[iJoint].translation() - targetTranslation;
116 this->weight_ * targetPositionWeight;
119 size_t jointIndex = iJoint;
122 Ensures(jointIndex < this->skeleton_.joints.size());
127 const Eigen::Vector3<T> posd =
130 for (
size_t d = 0; d < 3; d++) {
132 if (this->activeJointParams_[paramIndex + d]) {
134 T(2) * diff.dot(state.
jointState[jointIndex].getTranslationDerivative(d)) * pwgt,
136 this->parameterTransform_,
139 if (this->activeJointParams_[paramIndex + 3 + d]) {
141 const Eigen::Vector3<T> axis = state.
jointState[jointIndex].rotationAxis.col(d);
144 T(2) * diff.dot(state.
jointState[jointIndex].getRotationDerivative(d, posd)) * pwgt +
145 T(2) * rwgt * rotD.cwiseProduct(rotDiff).sum();
147 val, paramIndex + 3 + d, this->parameterTransform_, vec);
150 if (this->activeJointParams_[paramIndex + 6]) {
153 T(2) * diff.dot(state.
jointState[jointIndex].getScaleDerivative(posd)) * pwgt;
159 jointIndex = this->skeleton_.joints[jointIndex].parent;
168 const std::string& inputName,
171 Eigen::Ref<
const Eigen::VectorX<T>> inputVec) {
176 if (targetState.empty()) {
177 return Eigen::VectorX<T>::Zero(
getInputSize(inputName));
181 if (inputName ==
"targetPositionWeights") {
182 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(this->
skeleton_.joints.size());
183 using JetType = ceres::Jet<T, 1>;
184 for (
size_t iJoint = 0; iJoint < this->
skeleton_.joints.size(); ++iJoint) {
185 result(iJoint) = calculateGradient_dot<JetType>(
189 targetState[iJoint].rotation.normalized().template
cast<JetType>(),
190 JetType(targetPositionWeights[iJoint], 0),
191 JetType(targetRotationWeights[iJoint]),
196 }
else if (inputName ==
"targetRotationWeights") {
197 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(this->
skeleton_.joints.size());
198 using JetType = ceres::Jet<T, 1>;
199 for (
size_t iJoint = 0; iJoint < this->
skeleton_.joints.size(); ++iJoint) {
200 result(iJoint) = calculateGradient_dot<JetType>(
204 targetState[iJoint].rotation.normalized().template
cast<JetType>(),
205 JetType(targetPositionWeights[iJoint]),
206 JetType(targetRotationWeights[iJoint], 0),
211 }
else if (inputName ==
"targetState") {
212 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(8 * this->
skeleton_.joints.size());
213 for (
size_t iJoint = 0; iJoint < this->
skeleton_.joints.size(); ++iJoint) {
215 using JetType = ceres::Jet<T, 3>;
216 result.template segment<3>(8 * iJoint + 0) =
217 calculateGradient_dot<JetType>(
221 targetState[iJoint].rotation.normalized().template
cast<JetType>(),
222 JetType(targetPositionWeights[iJoint]),
223 JetType(targetRotationWeights[iJoint]),
228 using JetType = ceres::Jet<T, 4>;
229 result.template segment<4>(8 * iJoint + 3) =
230 calculateGradient_dot<JetType>(
234 Eigen::Quaternion<JetType>(
237 JetType(targetPositionWeights[iJoint]),
238 JetType(targetRotationWeights[iJoint]),
245 MT_THROW(
"Unknown input to FullyDifferentiableMotionErrorFunctionT: {}", inputName);
const char * name() const final
~FullyDifferentiableStateErrorFunctionT() override
std::vector< std::string > inputs() const final
void getInputImp(const std::string &name, Eigen::Ref< Eigen::VectorX< T > > value) const final
FullyDifferentiableStateErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
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
void setInputImp(const std::string &name, Eigen::Ref< const Eigen::VectorX< T > > value) final
Eigen::Index getInputSize(const std::string &name) const final
const Skeleton & skeleton_
void setTargetWeights(const Eigen::VectorX< T > &posWeight, const Eigen::VectorX< T > &rotWeight)
static constexpr T kPositionWeight
const TransformListT< T > & getTargetState() const
void setTargetState(const SkeletonStateT< T > *target)
StateErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, RotationErrorType rotationErrorType=RotationErrorType::RotationMatrixDifference)
static constexpr T kOrientationWeight
const Eigen::VectorX< T > & getRotationWeights() const
const Eigen::VectorX< T > & getPositionWeights() const
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Eigen::Matrix< T, 3, 3 > crossProductMatrix(const Eigen::Vector3< T > &v)
Creates a skew-symmetric matrix for computing cross products.
SkeletonT< float > Skeleton
std::vector< TransformT< T > > TransformListT
Per-joint transform list describing the state of all joints in a skeleton (one entry per joint,...
T times_parameterTransform_times_v(const T &value, const size_t jointParamIdx, const ParameterTransform ¶meterTransform, Eigen::Ref< const Eigen::VectorX< T2 > > v)
Eigen::Matrix< ceres::Jet< T, N >, N, 1 > buildJetVec(const Eigen::Matrix< T, N, 1 > &v)
ParameterTransformT< float > ParameterTransform
constexpr size_t kParametersPerJoint
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.