117 if (state.
jointState.empty() || constraints_.empty()) {
123 for (
const auto& constr : constraints_) {
124 const auto& srcJointState = state.
jointState[constr.sourceJoint];
125 const auto& refJointState = state.
jointState[constr.referenceJoint];
128 const Vector3<T> srcWorldPos = srcJointState.transform * constr.sourceOffset;
129 const Vector3<T> refWorldPos = refJointState.transform * constr.referenceOffset;
132 const Vector3<T> diff = srcWorldPos - refWorldPos;
133 const Matrix3<T> refRotT = refJointState.transform.toRotationMatrix().transpose();
134 const Vector3<T> relativePos = refRotT * diff;
137 const Vector3<T> posError = relativePos - constr.target;
138 error += constr.weight * posError.squaredNorm() * this->
weight_;
147 const T wgt = T(2) * constr.weight * this->
weight_;
149 const Vector3<T> worldGrad = refJointState.transform.toRotationMatrix() * posError;
152 auto processSourceJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos) {
153 size_t currentJoint = jointIndex;
155 const auto& currentJointState = state.
jointState[currentJoint];
157 const Vector3<T> posd = worldPos - currentJointState.translation();
160 for (
size_t d = 0; d < 3; d++) {
162 const T val = worldGrad.dot(currentJointState.getTranslationDerivative(d)) * wgt;
172 const T val = worldGrad.dot(currentJointState.getRotationDerivative(d, posd)) * wgt;
176 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
185 const T val = worldGrad.dot(currentJointState.getScaleDerivative(posd)) * wgt;
194 currentJoint = this->
skeleton_.joints[currentJoint].parent;
199 auto processReferenceJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos) {
200 size_t currentJoint = jointIndex;
202 const auto& currentJointState = state.
jointState[currentJoint];
204 const Vector3<T> posd = worldPos - currentJointState.translation();
207 for (
size_t d = 0; d < 3; d++) {
209 const T val = -worldGrad.dot(currentJointState.getTranslationDerivative(d)) * wgt;
219 const T val = -worldGrad.dot(currentJointState.getRotationDerivative(d, posd)) * wgt;
223 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
232 const T val = -worldGrad.dot(currentJointState.getScaleDerivative(posd)) * wgt;
241 currentJoint = this->
skeleton_.joints[currentJoint].parent;
247 auto processReferenceRotation = [&](
size_t jointIndex) {
248 size_t currentJoint = jointIndex;
250 const auto& currentJointState = state.
jointState[currentJoint];
254 for (
size_t d = 0; d < 3; d++) {
260 const T val = -worldGrad.dot(currentJointState.getRotationDerivative(d, diff)) * wgt;
264 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
272 currentJoint = this->
skeleton_.joints[currentJoint].parent;
276 processSourceJoint(constr.sourceJoint, srcWorldPos);
277 processReferenceJoint(constr.referenceJoint, refWorldPos);
278 processReferenceRotation(constr.referenceJoint);
295 jacobian.rows() >= gsl::narrow<Eigen::Index>(3 * constraints_.size()),
296 "Jacobian rows mismatch: Actual {}, Expected at least {}",
298 3 * constraints_.size());
301 residual.rows() >= gsl::narrow<Eigen::Index>(3 * constraints_.size()),
302 "Residual rows mismatch: Actual {}, Expected at least {}",
304 3 * constraints_.size());
306 if (state.
jointState.empty() || constraints_.empty()) {
311 const T wgt = std::sqrt(this->
weight_);
315 for (
const auto& constr : constraints_) {
316 const auto& srcJointState = state.
jointState[constr.sourceJoint];
317 const auto& refJointState = state.
jointState[constr.referenceJoint];
320 const Vector3<T> srcWorldPos = srcJointState.transform * constr.sourceOffset;
321 const Vector3<T> refWorldPos = refJointState.transform * constr.referenceOffset;
324 const Vector3<T> diff = srcWorldPos - refWorldPos;
325 const Matrix3<T> refRotT = refJointState.transform.toRotationMatrix().transpose();
326 const Vector3<T> relativePos = refRotT * diff;
329 const Vector3<T> posError = relativePos - constr.target;
330 error += constr.weight * posError.squaredNorm() * this->
weight_;
332 const T fac = std::sqrt(constr.weight) * wgt;
333 const int baseRow = pos;
337 for (
int d = 0; d < 3; d++) {
338 residual(baseRow + d) = posError(d) * fac;
342 auto processSourceJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos) {
343 size_t currentJoint = jointIndex;
345 const auto& currentJointState = state.
jointState[currentJoint];
347 const Vector3<T> posd = worldPos - currentJointState.translation();
349 for (
size_t d = 0; d < 3; d++) {
352 const Vector3<T> dWorldPos = currentJointState.getTranslationDerivative(d);
353 const Vector3<T> dRelativePos = refRotT * dWorldPos;
359 for (
int row = 0; row < 3; row++) {
360 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
365 const Vector3<T> dWorldPos = currentJointState.getRotationDerivative(d, posd);
366 const Vector3<T> dRelativePos = refRotT * dWorldPos;
370 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
374 for (
int row = 0; row < 3; row++) {
375 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
381 const Vector3<T> dWorldPos = currentJointState.getScaleDerivative(posd);
382 const Vector3<T> dRelativePos = refRotT * dWorldPos;
388 for (
int row = 0; row < 3; row++) {
389 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
394 currentJoint = this->
skeleton_.joints[currentJoint].parent;
399 auto processReferenceJoint = [&](
size_t jointIndex,
const Vector3<T>& worldPos) {
400 size_t currentJoint = jointIndex;
402 const auto& currentJointState = state.
jointState[currentJoint];
404 const Vector3<T> posd = worldPos - currentJointState.translation();
406 for (
size_t d = 0; d < 3; d++) {
408 const Vector3<T> dWorldPos = currentJointState.getTranslationDerivative(d);
409 const Vector3<T> dRelativePos = -refRotT * dWorldPos;
415 for (
int row = 0; row < 3; row++) {
416 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
421 const Vector3<T> dWorldPos = currentJointState.getRotationDerivative(d, posd);
422 const Vector3<T> dRelativePos = -refRotT * dWorldPos;
426 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
430 for (
int row = 0; row < 3; row++) {
431 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
437 const Vector3<T> dWorldPos = currentJointState.getScaleDerivative(posd);
438 const Vector3<T> dRelativePos = -refRotT * dWorldPos;
444 for (
int row = 0; row < 3; row++) {
445 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
450 currentJoint = this->
skeleton_.joints[currentJoint].parent;
455 auto processReferenceRotation = [&](
size_t jointIndex) {
456 size_t currentJoint = jointIndex;
458 const auto& currentJointState = state.
jointState[currentJoint];
461 for (
size_t d = 0; d < 3; d++) {
467 const Vector3<T> dRotDiff = currentJointState.getRotationDerivative(d, diff);
468 const Vector3<T> dRelativePos = -refRotT * dRotDiff;
472 this->parameterTransform_.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
476 for (
int row = 0; row < 3; row++) {
477 jacobian(baseRow + row, paramCol) += dRelativePos(row) * coeff;
483 currentJoint = this->
skeleton_.joints[currentJoint].parent;
487 processSourceJoint(constr.sourceJoint, srcWorldPos);
488 processReferenceJoint(constr.referenceJoint, refWorldPos);
489 processReferenceRotation(constr.referenceJoint);