77 if (state.
jointState.empty() || constraints_.empty()) {
83 for (
const auto& constr : constraints_) {
84 const auto& srcJointState = state.
jointState[constr.sourceJoint];
85 const auto& refJointState = state.
jointState[constr.referenceJoint];
88 const Matrix3<T> srcRot = srcJointState.rotation().toRotationMatrix();
89 const Matrix3<T> refRot = refJointState.rotation().toRotationMatrix();
90 const Matrix3<T> relativeRot = refRot.transpose() * srcRot;
93 const Matrix3<T> targetRot = constr.target.toRotationMatrix();
94 const Matrix3<T> rotError = relativeRot - targetRot;
96 error += constr.weight * rotError.squaredNorm() * this->
weight_;
110 if (state.
jointState.empty() || constraints_.empty()) {
116 for (
const auto& constr : constraints_) {
117 const auto& srcJointState = state.
jointState[constr.sourceJoint];
118 const auto& refJointState = state.
jointState[constr.referenceJoint];
121 const Matrix3<T> srcRot = srcJointState.rotation().toRotationMatrix();
122 const Matrix3<T> refRot = refJointState.rotation().toRotationMatrix();
123 const Matrix3<T> relativeRot = refRot.transpose() * srcRot;
126 const Matrix3<T> targetRot = constr.target.toRotationMatrix();
127 const Matrix3<T> rotError = relativeRot - targetRot;
128 error += constr.weight * rotError.squaredNorm() * this->
weight_;
130 const T wgt = T(2) * constr.weight * this->
weight_;
145 for (
int col = 0; col < 3; col++) {
152 size_t currentJoint = constr.sourceJoint;
154 const auto& currentJointState = state.
jointState[currentJoint];
158 for (
size_t d = 0; d < 3; d++) {
160 const T val = worldGrad.dot(currentJointState.getRotationDerivative(d, srcCol)) * wgt;
164 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
172 currentJoint = this->
skeleton_.joints[currentJoint].parent;
178 currentJoint = constr.referenceJoint;
180 const auto& currentJointState = state.
jointState[currentJoint];
183 for (
size_t d = 0; d < 3; d++) {
188 const T val = -worldGrad.dot(currentJointState.getRotationDerivative(d, srcCol)) * wgt;
192 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
200 currentJoint = this->
skeleton_.joints[currentJoint].parent;
219 jacobian.rows() >= gsl::narrow<Eigen::Index>(9 * constraints_.size()),
220 "Jacobian rows mismatch: Actual {}, Expected at least {}",
222 9 * constraints_.size());
225 residual.rows() >= gsl::narrow<Eigen::Index>(9 * constraints_.size()),
226 "Residual rows mismatch: Actual {}, Expected at least {}",
228 9 * constraints_.size());
230 if (state.
jointState.empty() || constraints_.empty()) {
235 const T wgt = std::sqrt(this->
weight_);
239 for (
const auto& constr : constraints_) {
240 const auto& srcJointState = state.
jointState[constr.sourceJoint];
241 const auto& refJointState = state.
jointState[constr.referenceJoint];
244 const Matrix3<T> srcRot = srcJointState.rotation().toRotationMatrix();
245 const Matrix3<T> refRot = refJointState.rotation().toRotationMatrix();
246 const Matrix3<T> refRotT = refRot.transpose();
247 const Matrix3<T> relativeRot = refRotT * srcRot;
250 const Matrix3<T> targetRot = constr.target.toRotationMatrix();
251 const Matrix3<T> rotError = relativeRot - targetRot;
252 error += constr.weight * rotError.squaredNorm() * this->
weight_;
254 const T fac = std::sqrt(constr.weight) * wgt;
255 const int baseRow = pos;
259 for (
int col = 0; col < 3; col++) {
260 for (
int row = 0; row < 3; row++) {
261 residual(baseRow + col * 3 + row) = rotError(row, col) * fac;
266 for (
int col = 0; col < 3; col++) {
271 size_t currentJoint = constr.sourceJoint;
273 const auto& currentJointState = state.
jointState[currentJoint];
276 for (
size_t d = 0; d < 3; d++) {
279 const Vector3<T> dWorldCol = currentJointState.getRotationDerivative(d, srcCol);
280 const Vector3<T> dRelativeCol = refRotT * dWorldCol;
284 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
288 for (
int row = 0; row < 3; row++) {
289 jacobian(baseRow + col * 3 + row, paramCol) += dRelativeCol(row) * coeff;
295 currentJoint = this->
skeleton_.joints[currentJoint].parent;
300 currentJoint = constr.referenceJoint;
302 const auto& currentJointState = state.
jointState[currentJoint];
305 for (
size_t d = 0; d < 3; d++) {
308 const Vector3<T> dRotCol = currentJointState.getRotationDerivative(d, srcCol);
309 const Vector3<T> dRelativeCol = -refRotT * dRotCol;
313 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
317 for (
int row = 0; row < 3; row++) {
318 jacobian(baseRow + col * 3 + row, paramCol) += dRelativeCol(row) * coeff;
324 currentJoint = this->
skeleton_.joints[currentJoint].parent;