46 for (
int i = 0; i < edge->getNumVertices(); ++i)
48 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
49 if (vert_dim_unfixed == 0)
continue;
51 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
52 edge->computeJacobian(i, block_jacobian,
nullptr);
53 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
54 block_jacobian.colwise().sum();
60 for (
int i = 0; i < edge->getNumVertices(); ++i)
62 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
63 if (vert_dim_unfixed == 0)
continue;
65 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
66 edge->computeJacobian(i, block_jacobian,
nullptr);
69 Eigen::VectorXd values(edge->getDimension());
70 edge->computeValues(values);
72 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) += 2.0 * values.transpose() * block_jacobian;
78 if (edge->getObjectiveDimension() == 0)
continue;
80 for (
int i = 0; i < edge->getNumVertices(); ++i)
82 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
83 if (vert_dim_unfixed == 0)
continue;
85 Eigen::MatrixXd block_jacobian(edge->getObjectiveDimension(), vert_dim_unfixed);
86 edge->computeObjectiveJacobian(i, block_jacobian,
nullptr);
88 if (edge->isObjectiveLeastSquaresForm())
91 Eigen::VectorXd values(edge->getObjectiveDimension());
92 edge->computeObjectiveValues(values);
93 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) += 2.0 * values.transpose() * block_jacobian;
97 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
98 block_jacobian.colwise().sum();
119 for (
int i = 0; i < edge->getNumVertices(); ++i)
121 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
122 if (vert_dim_unfixed == 0)
continue;
124 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
125 edge->computeJacobian(i, block_jacobian,
nullptr);
126 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
127 block_jacobian.colwise().sum();
134 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLeastSquaresForm())
continue;
136 for (
int i = 0; i < edge->getNumVertices(); ++i)
138 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
139 if (vert_dim_unfixed == 0)
continue;
141 Eigen::MatrixXd block_jacobian(edge->getObjectiveDimension(), vert_dim_unfixed);
142 edge->computeObjectiveJacobian(i, block_jacobian,
nullptr);
144 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
145 block_jacobian.colwise().sum();
165 for (
int i = 0; i < edge->getNumVertices(); ++i)
167 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
168 if (vert_dim_unfixed == 0)
continue;
170 edge->computeJacobian(i,
171 jacobian.block(edge->getEdgeIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
172 multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
178 if (edge->getObjectiveDimension() == 0 || !edge->isObjectiveLeastSquaresForm())
continue;
180 for (
int i = 0; i < edge->getNumVertices(); ++i)
182 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
183 if (vert_dim_unfixed == 0)
continue;
185 edge->computeObjectiveJacobian(
187 jacobian.block(edge->getEdgeObjectiveIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getObjectiveDimension(), vert_dim_unfixed),
188 multipliers ? multipliers + edge->getEdgeObjectiveIdx() :
nullptr);
205 for (
int i = 0; i < edge->getNumVertices(); ++i)
207 nnz += edge->getDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
213 if (edge->getObjectiveDimension() == 0 || !edge->isObjectiveLeastSquaresForm())
continue;
215 for (
int i = 0; i < edge->getNumVertices(); ++i)
217 nnz += edge->getObjectiveDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
227 assert(j_col.size() == i_row.size());
236 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
238 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
241 for (
int i = 0; i < vertex->getDimension(); ++i)
243 if (!vertex->isFixedComponent(i))
246 for (
int j = 0; j < edge->getDimension(); ++j)
248 i_row[nnz_idx] = edge->getEdgeIdx() + j;
249 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
260 if (edge->getObjectiveDimension() == 0 || !edge->isObjectiveLeastSquaresForm())
continue;
262 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
264 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
267 for (
int i = 0; i < vertex->getDimension(); ++i)
269 if (!vertex->isFixedComponent(i))
272 for (
int j = 0; j < edge->getObjectiveDimension(); ++j)
274 i_row[nnz_idx] = edge->getEdgeObjectiveIdx() + j;
275 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
296 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
298 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
299 if (vert_dim_unfixed == 0)
continue;
302 edge->computeJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
303 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
310 if (edge->getObjectiveDimension() == 0 || !edge->isObjectiveLeastSquaresForm())
continue;
312 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
314 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
315 if (vert_dim_unfixed == 0)
continue;
318 edge->computeObjectiveJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeObjectiveIdx() :
nullptr);
319 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
339 for (
int i = 0; i < edge->getNumVertices(); ++i)
341 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
342 if (vert_dim_unfixed == 0)
continue;
344 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
345 edge->computeJacobian(i,
346 jacobian.block(edge->getEdgeIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
347 multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
353 if (edge->getEqualityDimension() == 0)
continue;
355 for (
int i = 0; i < edge->getNumVertices(); ++i)
357 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
358 if (vert_dim_unfixed == 0)
continue;
360 Eigen::MatrixXd block_jacobian(edge->getEqualityDimension(), vert_dim_unfixed);
361 edge->computeEqualityJacobian(
362 i, jacobian.block(edge->getEdgeEqualityIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getEqualityDimension(), vert_dim_unfixed),
363 multipliers ? multipliers + edge->getEdgeEqualityIdx() :
nullptr);
380 for (
int i = 0; i < edge->getNumVertices(); ++i)
382 nnz += edge->getDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
388 if (edge->getEqualityDimension() == 0)
continue;
390 for (
int i = 0; i < edge->getNumVertices(); ++i)
392 nnz += edge->getEqualityDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
402 assert(j_col.size() == i_row.size());
411 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
413 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
416 for (
int i = 0; i < vertex->getDimension(); ++i)
418 if (!vertex->isFixedComponent(i))
421 for (
int j = 0; j < edge->getDimension(); ++j)
423 i_row[nnz_idx] = edge->getEdgeIdx() + j;
424 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
435 if (edge->getEqualityDimension() == 0)
continue;
437 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
439 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
442 for (
int i = 0; i < vertex->getDimension(); ++i)
444 if (!vertex->isFixedComponent(i))
447 for (
int j = 0; j < edge->getEqualityDimension(); ++j)
449 i_row[nnz_idx] = edge->getEdgeEqualityIdx() + j;
450 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
471 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
473 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
474 if (vert_dim_unfixed == 0)
continue;
477 edge->computeJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
478 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
485 if (edge->getEqualityDimension() == 0)
continue;
487 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
489 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
490 if (vert_dim_unfixed == 0)
continue;
493 edge->computeEqualityJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeEqualityIdx() :
nullptr);
494 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
514 for (
int i = 0; i < edge->getNumVertices(); ++i)
516 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
517 if (vert_dim_unfixed == 0)
continue;
519 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
520 edge->computeJacobian(i,
521 jacobian.block(edge->getEdgeIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
522 multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
528 if (edge->getInequalityDimension() == 0)
continue;
530 for (
int i = 0; i < edge->getNumVertices(); ++i)
532 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
533 if (vert_dim_unfixed == 0)
continue;
535 Eigen::MatrixXd block_jacobian(edge->getInequalityDimension(), vert_dim_unfixed);
536 edge->computeInequalityJacobian(
538 jacobian.block(edge->getEdgeInequalityIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getInequalityDimension(), vert_dim_unfixed),
539 multipliers ? multipliers + edge->getEdgeInequalityIdx() :
nullptr);
556 for (
int i = 0; i < edge->getNumVertices(); ++i)
558 nnz += edge->getDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
564 if (edge->getInequalityDimension() == 0)
continue;
566 for (
int i = 0; i < edge->getNumVertices(); ++i)
568 nnz += edge->getInequalityDimension() * edge->getVertexRaw(i)->getDimensionUnfixed();
578 assert(j_col.size() == i_row.size());
587 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
589 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
592 for (
int i = 0; i < vertex->getDimension(); ++i)
594 if (!vertex->isFixedComponent(i))
597 for (
int j = 0; j < edge->getDimension(); ++j)
599 i_row[nnz_idx] = edge->getEdgeIdx() + j;
600 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
611 if (edge->getInequalityDimension() == 0)
continue;
613 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
615 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
618 for (
int i = 0; i < vertex->getDimension(); ++i)
620 if (!vertex->isFixedComponent(i))
623 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
625 i_row[nnz_idx] = edge->getEdgeInequalityIdx() + j;
626 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
647 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
649 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
650 if (vert_dim_unfixed == 0)
continue;
653 edge->computeJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeIdx() :
nullptr);
654 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
661 if (edge->getInequalityDimension() == 0)
continue;
663 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
665 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
666 if (vert_dim_unfixed == 0)
continue;
669 edge->computeInequalityJacobian(vert_idx, block_jacobian, multipliers ? multipliers + edge->getEdgeInequalityIdx() :
nullptr);
670 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
690 for (
int i = 0; i < edge->getNumVertices(); ++i)
692 const VertexInterface* vertex = edge->getVertexRaw(i);
693 int vert_dim_unfixed = vertex->getDimensionUnfixed();
694 if (vert_dim_unfixed == 0)
continue;
698 Eigen::VectorXd values(edge->getDimension());
699 edge->computeValues(values);
700 Eigen::Array<bool, -1, 1> active = values.array() > 0.0;
702 if (!active.any())
continue;
705 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
706 edge->computeJacobian(i, jacobian.block(edge->getEdgeIdx(), vertex->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
nullptr);
709 for (
int j = 0; j < edge->getDimension(); ++j)
711 if (active[j] && weight != 1.0)
712 jacobian.block(edge->getEdgeIdx() + j, edge->getVertexRaw(i)->getVertexIdx(), 1, vert_dim_unfixed) *= weight;
714 jacobian.block(edge->getEdgeIdx() + j, edge->getVertexRaw(i)->getVertexIdx(), 1, vert_dim_unfixed).setZero();
722 if (edge->getInequalityDimension() == 0)
continue;
724 for (
int i = 0; i < edge->getNumVertices(); ++i)
726 const VertexInterface* vertex = edge->getVertexRaw(i);
727 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
728 if (vert_dim_unfixed == 0)
continue;
732 Eigen::VectorXd values(edge->getInequalityDimension());
734 edge->computeInequalityValues(values);
735 Eigen::Array<bool, -1, 1> active = values.array() > 0.0;
737 if (!active.any())
continue;
739 Eigen::MatrixXd block_jacobian(edge->getInequalityDimension(), vert_dim_unfixed);
740 edge->computeInequalityJacobian(
741 i, jacobian.block(edge->getEdgeInequalityIdx(), vertex->getVertexIdx(), edge->getInequalityDimension(), vert_dim_unfixed),
nullptr);
744 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
746 if (active[j] && weight != 1.0)
747 jacobian.block(edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed) *= weight;
749 jacobian.block(edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed).setZero();
768 Eigen::VectorXd edge_values(edge->getDimension());
769 edge->computeValues(edge_values);
770 Eigen::Array<bool, -1, 1> active = edge_values.array() > 0.0;
772 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
774 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
775 if (vert_dim_unfixed == 0)
continue;
778 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
782 block_jacobian.setZero();
786 edge->computeJacobian(vert_idx, block_jacobian,
nullptr);
789 for (
int j = 0; j < edge->getDimension(); ++j)
791 if (active[j] && weight != 1.0)
792 block_jacobian.block(j, 0, 1, vert_dim_unfixed) *= weight;
794 block_jacobian.block(j, 0, 1, vert_dim_unfixed).setZero();
797 jac_row_idx += edge->getDimension();
803 if (edge->getInequalityDimension() == 0)
continue;
807 Eigen::VectorXd edge_values(edge->getInequalityDimension());
809 edge->computeInequalityValues(edge_values);
810 Eigen::Array<bool, -1, 1> active = edge_values.array() > 0.0;
812 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
814 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
815 if (vert_dim_unfixed == 0)
continue;
818 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
822 block_jacobian.setZero();
826 edge->computeInequalityJacobian(vert_idx, block_jacobian,
nullptr);
829 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
831 if (active[j] && weight != 1.0)
832 block_jacobian.block(j, 0, 1, vert_dim_unfixed) *= weight;
834 block_jacobian.block(j, 0, 1, vert_dim_unfixed).setZero();
837 jac_row_idx += edge->getInequalityDimension();
844 const double* multipliers_lsq_obj,
const double* multipliers_eq,
845 const double* multipliers_ineq,
bool active_ineq,
double active_ineq_weight)
856 "HyperGraphOptimizationProblemEdgeBased::computeDenseJacobians(): multiplier_ineq is ignored if active_ineq is enabled");
865 gradient_non_lsq_obj.setZero();
866 jacobian_lsq_obj.setZero();
867 jacobian_eq.setZero();
868 jacobian_ineq.setZero();
877 for (
int i = 0; i < edge->getNumVertices(); ++i)
879 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
880 if (vert_dim_unfixed == 0)
continue;
882 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
883 edge->computeJacobian(i, block_jacobian,
nullptr);
884 gradient_non_lsq_obj.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
885 block_jacobian.colwise().sum();
892 for (
int i = 0; i < edge->getNumVertices(); ++i)
894 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
895 if (vert_dim_unfixed == 0)
continue;
897 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
898 edge->computeJacobian(
899 i, jacobian_lsq_obj.block(edge->getEdgeIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
900 multipliers_lsq_obj ? multipliers_lsq_obj + edge->getEdgeIdx() :
nullptr);
907 for (
int i = 0; i < edge->getNumVertices(); ++i)
909 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
910 if (vert_dim_unfixed == 0)
continue;
912 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
913 edge->computeJacobian(
914 i, jacobian_eq.block(edge->getEdgeIdx(), edge->getVertexRaw(i)->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
915 multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr);
922 for (
int i = 0; i < edge->getNumVertices(); ++i)
924 const VertexInterface* vertex = edge->getVertexRaw(i);
925 int vert_dim_unfixed = vertex->getDimensionUnfixed();
926 if (vert_dim_unfixed == 0)
continue;
932 Eigen::VectorXd edge_values(edge->getDimension());
933 edge->computeValues(edge_values);
934 Eigen::Array<bool, -1, 1> active = edge_values.array() > 0.0;
936 if (!active.any())
continue;
939 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
940 edge->computeJacobian(i, jacobian_ineq.block(edge->getEdgeIdx(), vertex->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
944 for (
int j = 0; j < edge->getDimension(); ++j)
946 if (active[j] && active_ineq_weight != 1.0)
947 jacobian_ineq.block(edge->getEdgeIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed) *= active_ineq_weight;
949 jacobian_ineq.block(edge->getEdgeIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed).setZero();
954 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
955 edge->computeJacobian(i, jacobian_ineq.block(edge->getEdgeIdx(), vertex->getVertexIdx(), edge->getDimension(), vert_dim_unfixed),
956 multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr);
964 const double* mult_ineq_part = (multipliers_ineq && !active_ineq) ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
965 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
966 const double* mult_obj_lsq_part = multipliers_lsq_obj ? multipliers_lsq_obj + edge->getEdgeObjectiveIdx() :
nullptr;
968 for (
int i = 0; i < edge->getNumVertices(); ++i)
970 const VertexInterface* vertex = edge->getVertexRaw(i);
971 int vert_dim_unfixed = vertex->getDimensionUnfixed();
972 if (vert_dim_unfixed == 0)
continue;
975 jacobian_eq.block(edge->getEdgeEqualityIdx(), vertex->getVertexIdx(), edge->getEqualityDimension(), vert_dim_unfixed);
978 jacobian_ineq.block(edge->getEdgeInequalityIdx(), vertex->getVertexIdx(), edge->getInequalityDimension(), vert_dim_unfixed);
980 if (edge->isObjectiveLeastSquaresForm())
983 jacobian_lsq_obj.block(edge->getEdgeObjectiveIdx(), vertex->getVertexIdx(), edge->getObjectiveDimension(), vert_dim_unfixed);
984 edge->computeJacobians(i, block_lsq_obj, block_eq, block_ineq, mult_obj_lsq_part, mult_eq_part, mult_ineq_part);
988 Eigen::MatrixXd block_lsq_obj(edge->getObjectiveDimension(), vert_dim_unfixed);
989 edge->computeJacobians(i, block_lsq_obj, block_eq, block_ineq, mult_obj_lsq_part, mult_eq_part, mult_ineq_part);
991 gradient_non_lsq_obj.segment(vertex->getVertexIdx(), vert_dim_unfixed) +=
992 block_lsq_obj.colwise().sum();
999 Eigen::VectorXd ineq_values(edge->getInequalityDimension());
1001 edge->computeInequalityValues(ineq_values);
1002 Eigen::Array<bool, -1, 1> active = ineq_values.array() > 0.0;
1005 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
1007 if (active[j] && active_ineq_weight != 1.0)
1008 jacobian_ineq.block(edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed) *= active_ineq_weight;
1009 else if (!active[j])
1010 jacobian_ineq.block(edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx(), 1, vert_dim_unfixed).setZero();
1020 const double* multipliers_eq,
const double* multipliers_ineq,
1021 bool active_ineq,
double active_ineq_weight)
1028 active_ineq && multipliers_ineq,
1029 "HyperGraphOptimizationProblemEdgeBased::computeSparseJacobiansValues(): multiplier_ineq is ignored if active_ineq is enabled");
1033 int obj_nnz_idx = 0;
1035 int ineq_nnz_idx = 0;
1038 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1040 const double* mult_obj_lsq_part = multipliers_obj ? multipliers_obj + edge->getEdgeIdx() :
nullptr;
1042 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1044 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1045 if (vert_dim_unfixed == 0)
continue;
1048 edge->computeJacobian(vert_idx, block_jacobian, mult_obj_lsq_part);
1049 obj_nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1056 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
1058 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1060 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1061 if (vert_dim_unfixed == 0)
continue;
1064 edge->computeJacobian(vert_idx, block_jacobian, mult_eq_part);
1065 eq_nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1072 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
1074 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1076 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1077 if (vert_dim_unfixed == 0)
continue;
1083 Eigen::VectorXd edge_values(edge->getDimension());
1084 edge->computeValues(edge_values);
1085 Eigen::Array<bool, -1, 1> active = edge_values.array() > 0.0;
1088 ineq_nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1092 block_jacobian.setZero();
1096 edge->computeJacobian(vert_idx, block_jacobian,
nullptr);
1099 for (
int j = 0; j < edge->getDimension(); ++j)
1101 if (active[j] && active_ineq_weight != 1.0)
1102 block_jacobian.block(j, 0, 1, vert_dim_unfixed) *= active_ineq_weight;
1103 else if (!active[j])
1104 block_jacobian.block(j, 0, 1, vert_dim_unfixed).setZero();
1110 edge->computeJacobian(vert_idx, block_jacobian, mult_ineq_part);
1111 ineq_nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1120 if (!edge->isObjectiveLeastSquaresForm() && edge->getEqualityDimension() == 0 && edge->getInequalityDimension() == 0)
continue;
1122 const double* mult_obj_lsq_part = multipliers_obj ? multipliers_obj + edge->getEdgeObjectiveIdx() :
nullptr;
1123 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
1124 const double* mult_ineq_part = (multipliers_ineq && !active_ineq) ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
1126 for (
int i = 0; i < edge->getNumVertices(); ++i)
1128 const VertexInterface* vertex = edge->getVertexRaw(i);
1129 int vert_dim_unfixed = vertex->getDimensionUnfixed();
1130 if (vert_dim_unfixed == 0)
continue;
1133 eq_nnz_idx += block_eq.rows() * block_eq.cols();
1136 ineq_nnz_idx += block_ineq.rows() * block_ineq.cols();
1138 if (edge->isObjectiveLeastSquaresForm())
1141 obj_nnz_idx += block_lsq_obj.rows() * block_lsq_obj.cols();
1143 edge->computeJacobians(i, block_lsq_obj, block_eq, block_ineq, mult_obj_lsq_part, mult_eq_part, mult_ineq_part);
1149 Eigen::MatrixXd block_lsq_obj(edge->getObjectiveDimension(), vert_dim_unfixed);
1150 edge->computeJacobians(i, block_lsq_obj, block_eq, block_ineq, mult_obj_lsq_part, mult_eq_part, mult_ineq_part);
1163 Eigen::VectorXd ineq_values(edge->getInequalityDimension());
1165 edge->computeInequalityValues(ineq_values);
1166 Eigen::Array<bool, -1, 1> active = ineq_values.array() > 0.0;
1169 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
1171 if (active[j] && active_ineq_weight != 1.0)
1172 block_ineq.row(j) *= active_ineq_weight;
1173 else if (!active[j])
1174 block_ineq.row(j).setZero();
1191 bool equality,
bool inequality)
1194 assert(j_col.size() == i_row.size());
1201 int inequality_row_start = equality ? equality_row_start +
getEqualityDimension() : equality_row_start;
1206 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1208 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1218 for (
int j = 0; j < edge->getDimension(); ++j)
1220 i_row[nnz_idx] = edge->getEdgeIdx() + j;
1235 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1237 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1240 for (
int i = 0; i < vertex->getDimension(); ++i)
1242 if (!vertex->isFixedComponent(i))
1245 for (
int j = 0; j < edge->getDimension(); ++j)
1247 i_row[nnz_idx] = equality_row_start + edge->getEdgeIdx() + j;
1248 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
1262 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1264 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1267 for (
int i = 0; i < vertex->getDimension(); ++i)
1269 if (!vertex->isFixedComponent(i))
1272 for (
int j = 0; j < edge->getDimension(); ++j)
1274 i_row[nnz_idx] = inequality_row_start + edge->getEdgeIdx() + j;
1275 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
1288 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1290 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1297 for (
int i = 0; i < vertex->getDimension(); ++i)
1299 if (!vertex->isFixedComponent(i))
1301 for (
int j = 0; j < edge->getObjectiveDimension(); ++j)
1303 i_row[nnz_idx] = edge->getEdgeObjectiveIdx() + j;
1304 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
1316 for (
int i = 0; i < vertex->getDimension(); ++i)
1318 if (!vertex->isFixedComponent(i))
1320 for (
int j = 0; j < edge->getEqualityDimension(); ++j)
1322 i_row[nnz_idx] = equality_row_start + edge->getEdgeEqualityIdx() + j;
1323 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
1335 for (
int i = 0; i < vertex->getDimension(); ++i)
1337 if (!vertex->isFixedComponent(i))
1339 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
1341 i_row[nnz_idx] = inequality_row_start + edge->getEdgeInequalityIdx() + j;
1342 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
1353 bool equality,
bool inequality,
const double* multipliers_obj,
1354 const double* multipliers_eq,
const double* multipliers_ineq)
1365 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1367 const double* mult_obj_lsq_part = multipliers_obj ? multipliers_obj + edge->getEdgeIdx() :
nullptr;
1369 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1371 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1372 if (vert_dim_unfixed == 0)
continue;
1375 edge->computeJacobian(vert_idx, block_jacobian, mult_obj_lsq_part);
1376 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1386 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
1388 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1390 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1391 if (vert_dim_unfixed == 0)
continue;
1394 edge->computeJacobian(vert_idx, block_jacobian, mult_eq_part);
1395 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1405 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
1407 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1409 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1410 if (vert_dim_unfixed == 0)
continue;
1413 edge->computeJacobian(vert_idx, block_jacobian, mult_ineq_part);
1414 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1422 if (!objective_lsq && edge->getEqualityDimension() == 0 && edge->getInequalityDimension() == 0)
continue;
1425 const double* mult_obj_lsq_part = multipliers_obj ? multipliers_obj + edge->getEdgeObjectiveIdx() :
nullptr;
1426 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
1427 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
1429 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1431 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1432 if (vert_dim_unfixed == 0)
continue;
1434 if (objective_lsq && equality && inequality)
1437 nnz_idx += block_obj.rows() * block_obj.cols();
1439 nnz_idx += block_eq.rows() * block_eq.cols();
1441 nnz_idx += block_ineq.rows() * block_ineq.cols();
1443 edge->computeJacobians(vert_idx, block_obj, block_eq, block_ineq, mult_obj_lsq_part, mult_eq_part, mult_ineq_part);
1445 else if (equality && inequality)
1448 nnz_idx += block_eq.rows() * block_eq.cols();
1450 nnz_idx += block_ineq.rows() * block_ineq.cols();
1452 edge->computeConstraintJacobians(vert_idx, block_eq, block_ineq, mult_eq_part, mult_ineq_part);
1460 nnz_idx += block_obj.rows() * block_obj.cols();
1461 edge->computeObjectiveJacobian(vert_idx, block_obj, mult_obj_lsq_part);
1466 nnz_idx += block_eq.rows() * block_eq.cols();
1467 edge->computeEqualityJacobian(vert_idx, block_eq, mult_eq_part);
1472 nnz_idx += block_ineq.rows() * block_ineq.cols();
1473 edge->computeInequalityJacobian(vert_idx, block_ineq, mult_ineq_part);
1481 bool inequality,
bool finite_combined_bounds,
bool active_ineq,
1482 double weight_eq,
double weight_ineq,
double weight_bounds,
1483 const Eigen::VectorXd* values,
const Eigen::VectorXi* col_nnz)
1487 if (col_nnz) jacobian.
reserve(*col_nnz);
1492 int inequality_row_start = equality ? equality_row_start +
getEqualityDimension() : equality_row_start;
1498 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1500 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1505 if (vert_dim_unfixed == 0)
continue;
1507 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
1508 edge->computeJacobian(vert_idx, block_jacobian);
1517 for (
int j = 0; j < edge->getDimension(); ++j)
1519 jacobian.
coeffRef(edge->getEdgeIdx() + j, vertex->
getVertexIdx() + idx_free) = block_jacobian(j, idx_free);
1533 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1535 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1538 if (vert_dim_unfixed == 0)
continue;
1540 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
1541 edge->computeJacobian(vert_idx, block_jacobian);
1545 for (
int i = 0; i < vertex->getDimension(); ++i)
1547 if (!vertex->isFixedComponent(i))
1550 for (
int j = 0; j < edge->getDimension(); ++j)
1552 jacobian.
coeffRef(equality_row_start + edge->getEdgeIdx() + j, vertex->getVertexIdx() + idx_free) =
1553 block_jacobian(j, idx_free) * weight_eq;
1568 Eigen::Array<bool, -1, 1> active(edge->getDimension());
1572 active = values->segment(inequality_row_start + edge->getEdgeIdx(), edge->getDimension()).array() > 0.0;
1575 Eigen::VectorXd values_tmp(edge->getDimension());
1576 edge->computeValues(values_tmp);
1577 active = values_tmp.array() > 0.0;
1583 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1585 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1587 int vert_dim_unfixed = vertex->getDimensionUnfixed();
1588 if (vert_dim_unfixed == 0)
continue;
1590 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
1591 edge->computeJacobian(vert_idx, block_jacobian);
1595 for (
int i = 0; i < vertex->getDimension(); ++i)
1597 if (!vertex->isFixedComponent(i))
1600 for (
int j = 0; j < edge->getDimension(); ++j)
1604 jacobian.
coeffRef(inequality_row_start + edge->getEdgeIdx() + j, vertex->getVertexIdx() + idx_free) =
1605 block_jacobian(j, idx_free) * weight_ineq;
1609 jacobian.
coeffRef(inequality_row_start + edge->getEdgeIdx() + j, vertex->getVertexIdx() + idx_free) = 0.0;
1622 if (!objective_lsq && edge->getEqualityDimension() == 0 && edge->getInequalityDimension() == 0)
continue;
1626 Eigen::Array<bool, -1, 1> active(edge->getInequalityDimension());
1627 if (active_ineq && inequality)
1630 active = values->segment(inequality_row_start + edge->getEdgeInequalityIdx(), edge->getInequalityDimension()).array() > 0.0;
1633 Eigen::VectorXd values_tmp(edge->getInequalityDimension());
1634 edge->computeInequalityValues(values_tmp);
1635 active = values_tmp.array() > 0.0;
1638 else if (inequality)
1641 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1643 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
1645 int vert_dim_unfixed = vertex->getDimensionUnfixed();
1646 if (vert_dim_unfixed == 0)
continue;
1648 Eigen::MatrixXd block_obj(edge->getObjectiveDimension(), vert_dim_unfixed);
1649 Eigen::MatrixXd block_eq(edge->getEqualityDimension(), vert_dim_unfixed);
1650 Eigen::MatrixXd block_ineq(edge->getInequalityDimension(), vert_dim_unfixed);
1652 if (objective_lsq && equality && inequality)
1654 edge->computeJacobians(vert_idx, block_obj, block_eq, block_ineq);
1656 else if (equality && inequality)
1658 edge->computeConstraintJacobians(vert_idx, block_eq, block_ineq);
1665 edge->computeObjectiveJacobian(vert_idx, block_obj);
1669 edge->computeEqualityJacobian(vert_idx, block_eq);
1673 edge->computeInequalityJacobian(vert_idx, block_ineq);
1680 for (
int i = 0; i < vertex->getDimension(); ++i)
1682 if (!vertex->isFixedComponent(i))
1687 for (
int j = 0; j < edge->getObjectiveDimension(); ++j)
1689 jacobian.
coeffRef(edge->getEdgeObjectiveIdx() + j, vertex->getVertexIdx() + idx_free) = block_obj(j, idx_free);
1694 for (
int j = 0; j < edge->getEqualityDimension(); ++j)
1696 jacobian.
coeffRef(equality_row_start + edge->getEdgeEqualityIdx() + j, vertex->getVertexIdx() + idx_free) =
1697 block_eq(j, idx_free) * weight_eq;
1702 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
1706 jacobian.
coeffRef(inequality_row_start + edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx() + idx_free) =
1707 block_ineq(j, idx_free) * weight_ineq;
1711 jacobian.
coeffRef(inequality_row_start + edge->getEdgeInequalityIdx() + j, vertex->getVertexIdx() + idx_free) = 0.0;
1724 int jac_row_idx = bounds_start;
1728 int vert_idx = vertex->getVertexIdx();
1730 for (
int i = 0; i < vertex->getDimension(); ++i)
1732 if (vertex->isFixedComponent(i))
continue;
1734 if (vertex->hasFiniteLowerBound(i) || vertex->hasFiniteUpperBound(i))
1736 if (vertex->getData()[i] < vertex->getLowerBounds()[i])
1738 jacobian.
coeffRef(jac_row_idx, vert_idx + free_idx) = -weight_bounds;
1740 else if (vertex->getData()[i] > vertex->getUpperBounds()[i])
1742 jacobian.
coeffRef(jac_row_idx, vert_idx + free_idx) = weight_bounds;
1745 jacobian.
coeffRef(jac_row_idx, vert_idx + free_idx) = 0.0;
1758 bool equality,
bool inequality,
1759 const double* multipliers_eq,
1760 const double* multipliers_ineq)
1764 assert(equality ==
true);
1765 assert(inequality ==
true);
1776 for (
int i = 0; i < edge->getNumVertices(); ++i)
1778 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
1779 if (vert_dim_unfixed == 0)
continue;
1781 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
1782 edge->computeJacobian(i, block_jacobian,
nullptr);
1783 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
1784 block_jacobian.colwise().sum();
1788 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1790 for (
int i = 0; i < edge->getNumVertices(); ++i)
1792 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
1793 if (vert_dim_unfixed == 0)
continue;
1795 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
1796 edge->computeJacobian(i, block_jacobian,
nullptr);
1799 Eigen::VectorXd values(edge->getDimension());
1800 edge->computeValues(values);
1802 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) += 2.0 * values.transpose() * block_jacobian;
1813 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
1815 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1817 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1818 if (vert_dim_unfixed == 0)
continue;
1821 edge->computeJacobian(vert_idx, block_jacobian, mult_eq_part);
1822 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1832 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
1834 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1836 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1837 if (vert_dim_unfixed == 0)
continue;
1840 edge->computeJacobian(vert_idx, block_jacobian, mult_ineq_part);
1841 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
1854 for (
int i = 0; i < edge->getNumVertices(); ++i)
1856 int vert_dim_unfixed = edge->getVertexRaw(i)->getDimensionUnfixed();
1857 if (vert_dim_unfixed == 0)
continue;
1859 Eigen::MatrixXd block_jacobian(edge->getObjectiveDimension(), vert_dim_unfixed);
1860 edge->computeObjectiveJacobian(i, block_jacobian,
nullptr);
1862 if (edge->isObjectiveLeastSquaresForm())
1865 Eigen::VectorXd values(edge->getObjectiveDimension());
1866 edge->computeObjectiveValues(values);
1867 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) += 2.0 * values.transpose() * block_jacobian;
1871 gradient.segment(edge->getVertexRaw(i)->getVertexIdx(), vert_dim_unfixed) +=
1872 block_jacobian.colwise().sum();
1877 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
1878 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
1880 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
1882 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
1883 if (vert_dim_unfixed == 0)
continue;
1885 Eigen::MatrixXd block_obj(edge->getObjectiveDimension(), vert_dim_unfixed);
1887 nnz_idx += block_eq.rows() * block_eq.cols();
1889 nnz_idx += block_ineq.rows() * block_ineq.cols();
1890 edge->computeJacobians(vert_idx, block_obj, block_eq, block_ineq,
nullptr, mult_eq_part, mult_ineq_part);
1892 if (edge->isObjectiveLeastSquaresForm())
1895 Eigen::VectorXd values(edge->getObjectiveDimension());
1896 edge->computeObjectiveValues(values);
1897 gradient.segment(edge->getVertexRaw(vert_idx)->getVertexIdx(), vert_dim_unfixed) += 2.0 * values.transpose() * block_obj;
1901 gradient.segment(edge->getVertexRaw(vert_idx)->getVertexIdx(), vert_dim_unfixed) +=
1902 block_obj.colwise().sum();
1943 assert(hessian.rows() == hessian.cols());
1956 if (edge->isLinear())
continue;
1958 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
1963 if (vert_i_dim_unfixed == 0)
continue;
1967 edge->computeJacobian(row_i, block_jacobian1);
1969 for (
int col_j = row_i; col_j < edge->getNumVertices(); ++col_j)
1974 if (vert_j_dim_unfixed == 0)
continue;
1978 edge->computeHessianInc(row_i, col_j, block_jacobian1,
1980 nullptr, multiplier);
1986 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
1990 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
1992 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
1995 if (vert_i_dim_unfixed == 0)
continue;
1998 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
1999 edge->computeJacobian(row_i, block_jacobian1);
2001 for (
int col_j = row_i; col_j < edge->getNumVertices(); ++col_j)
2003 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2006 if (vert_j_dim_unfixed == 0)
continue;
2009 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
2010 edge->computeJacobian(col_j, block_jacobian2);
2015 hessian.block(vertex1->getVertexIdx(), vertex2->getVertexIdx(), vert_i_dim_unfixed, vert_j_dim_unfixed) +=
2016 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2039 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2041 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2043 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2045 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2046 if (vert_i_dim_unfixed == 0)
continue;
2049 Eigen::MatrixXd block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
2050 edge->computeObjectiveJacobian(row_i, block_jacobian1);
2052 for (
int col_j = row_i; col_j < edge->getNumVertices(); ++col_j)
2054 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2056 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2057 if (vert_j_dim_unfixed == 0)
continue;
2059 if (edge->isObjectiveLeastSquaresForm())
2062 Eigen::MatrixXd block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
2063 edge->computeObjectiveJacobian(col_j, block_jacobian2);
2068 hessian.block(vertex1->getVertexIdx(), vertex2->getVertexIdx(), vert_i_dim_unfixed, vert_j_dim_unfixed) +=
2069 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2075 edge->computeObjectiveHessianInc(
2076 row_i, col_j, block_jacobian1,
2077 hessian.block(vertex1->getVertexIdx(), vertex2->getVertexIdx(), vert_i_dim_unfixed, vert_j_dim_unfixed),
nullptr, multiplier);
2099 if (edge->isLinear())
continue;
2101 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2103 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2105 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2106 if (vert_i_dim_unfixed == 0)
continue;
2108 int col_start = lower_part_only ? row_i : 0;
2109 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2114 if (vert_j_dim_unfixed == 0)
continue;
2116 if (lower_part_only && vertex1 == vertex2)
2118 int n = vertex1->getDimensionUnfixed();
2119 nnz +=
n * (
n + 1) / 2;
2128 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
2132 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2134 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2136 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2137 if (vert_i_dim_unfixed == 0)
continue;
2139 int col_start = lower_part_only ? row_i : 0;
2140 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2142 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2144 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2145 if (vert_j_dim_unfixed == 0)
continue;
2147 if (lower_part_only && vertex1 == vertex2)
2149 int n = vertex1->getDimensionUnfixed();
2150 nnz +=
n * (
n + 1) / 2;
2153 nnz += vertex1->getDimensionUnfixed() * vertex2->getDimensionUnfixed();
2161 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2163 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2165 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2167 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2168 if (vert_i_dim_unfixed == 0)
continue;
2170 int col_start = lower_part_only ? row_i : 0;
2171 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2173 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2175 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2176 if (vert_j_dim_unfixed == 0)
continue;
2178 if (lower_part_only && vertex1 == vertex2)
2180 int n = vertex1->getDimensionUnfixed();
2181 nnz +=
n * (
n + 1) / 2;
2184 nnz += vertex1->getDimensionUnfixed() * vertex2->getDimensionUnfixed();
2199 assert(i_row.size() == j_col.size());
2208 if (edge->isLinear())
continue;
2210 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2212 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2214 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2215 if (vert_i_dim_unfixed == 0)
continue;
2217 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2218 for (
int col_j = 0; col_j < col_end; ++col_j)
2223 if (vert_j_dim_unfixed == 0)
continue;
2225 if (lower_part_only && vertex1 == vertex2)
2227 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2229 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
2231 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2239 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2241 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
2243 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2254 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
2258 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2260 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2262 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2263 if (vert_i_dim_unfixed == 0)
continue;
2265 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2266 for (
int col_j = 0; col_j < col_end; ++col_j)
2268 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2270 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2271 if (vert_j_dim_unfixed == 0)
continue;
2273 if (lower_part_only && vertex1 == vertex2)
2275 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2277 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
2279 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2280 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
2287 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2289 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
2291 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2292 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
2304 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2306 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2308 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2310 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2311 if (vert_i_dim_unfixed == 0)
continue;
2313 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2314 for (
int col_j = 0; col_j < col_end; ++col_j)
2316 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2318 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2319 if (vert_j_dim_unfixed == 0)
continue;
2321 if (lower_part_only && vertex1 == vertex2)
2323 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2325 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
2327 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2328 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
2335 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2337 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
2339 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2340 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
2351 bool lower_part_only)
2365 if (edge->isLinear())
continue;
2367 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2369 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2371 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2372 if (vert_i_dim_unfixed == 0)
continue;
2375 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
2376 edge->computeJacobian(row_i, block_jacobian1);
2378 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2379 for (
int col_j = 0; col_j < col_end; ++col_j)
2384 if (vert_j_dim_unfixed == 0)
continue;
2388 if (lower_part_only && vertex1 == vertex2)
2391 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
2392 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii,
nullptr, multiplier);
2393 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
2395 for (
int j = 0; j < i + 1; ++j)
2397 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
2406 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian,
nullptr, multiplier);
2407 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
2414 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
2418 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2420 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2422 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2423 if (vert_i_dim_unfixed == 0)
continue;
2426 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
2427 edge->computeJacobian(row_i, block_jacobian1);
2429 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2430 for (
int col_j = 0; col_j < col_end; ++col_j)
2432 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2435 if (vert_j_dim_unfixed == 0)
continue;
2438 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
2439 edge->computeJacobian(col_j, block_jacobian2);
2442 if (lower_part_only && vertex1 == vertex2)
2445 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
2446 block_hessian_ii.noalias() = 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2447 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
2449 for (
int j = 0; j < i + 1; ++j)
2451 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
2460 block_hessian += 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2461 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
2470 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2472 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2474 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2476 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2477 if (vert_i_dim_unfixed == 0)
continue;
2480 Eigen::MatrixXd block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
2481 edge->computeObjectiveJacobian(row_i, block_jacobian1);
2483 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2484 for (
int col_j = 0; col_j < col_end; ++col_j)
2486 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2488 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2489 if (vert_j_dim_unfixed == 0)
continue;
2491 if (edge->isObjectiveLeastSquaresForm())
2494 Eigen::MatrixXd block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
2495 edge->computeObjectiveJacobian(col_j, block_jacobian2);
2498 if (lower_part_only && vertex1 == vertex2)
2501 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
2502 block_hessian_ii.noalias() = 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2503 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
2505 for (
int j = 0; j < i + 1; ++j)
2507 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
2516 block_hessian += 2.0 * multiplier * block_jacobian1.transpose() * block_jacobian2;
2517 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
2524 if (lower_part_only && vertex1 == vertex2)
2527 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
2528 edge->computeObjectiveHessian(row_i, col_j, block_jacobian1, block_hessian_ii,
nullptr, multiplier);
2529 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
2531 for (
int j = 0; j < i + 1; ++j)
2533 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
2542 edge->computeObjectiveHessianInc(row_i, col_j, block_jacobian1, block_hessian,
nullptr, multiplier);
2543 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
2552 const Eigen::VectorXi* col_nnz,
bool upper_part_only)
2556 if (col_nnz) H.
reserve(*col_nnz);
2563 if (edge->isLinear())
continue;
2565 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2567 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2569 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2570 if (vert_i_dim_unfixed == 0)
continue;
2573 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
2574 edge->computeJacobian(row_i, block_jacobian1);
2576 int col_start = upper_part_only ? row_i : 0;
2577 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2582 if (vert_j_dim_unfixed == 0)
continue;
2586 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
2587 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian);
2590 if (upper_part_only && vertex1 == vertex2)
2593 for (
int i = 0; i < block_hessian.rows(); ++i)
2595 for (
int j = i; j < block_hessian.cols(); ++j)
2597 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2603 for (
int i = 0; i < block_hessian.rows(); ++i)
2605 for (
int j = 0; j < block_hessian.cols(); ++j)
2607 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2616 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
2620 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2622 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2624 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2625 if (vert_i_dim_unfixed == 0)
continue;
2628 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
2629 edge->computeJacobian(row_i, block_jacobian1);
2631 int col_start = upper_part_only ? row_i : 0;
2632 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2634 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2637 if (vert_j_dim_unfixed == 0)
continue;
2640 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
2641 edge->computeJacobian(col_j, block_jacobian2);
2644 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
2645 block_hessian = 2.0 * block_jacobian1.transpose() * block_jacobian2;
2648 if (upper_part_only && vertex1 == vertex2)
2651 for (
int i = 0; i < block_hessian.rows(); ++i)
2653 for (
int j = i; j < block_hessian.cols(); ++j)
2655 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2661 for (
int i = 0; i < block_hessian.rows(); ++i)
2663 for (
int j = 0; j < block_hessian.cols(); ++j)
2665 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2676 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2678 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2680 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2682 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2683 if (vert_i_dim_unfixed == 0)
continue;
2686 Eigen::MatrixXd obj_block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
2687 Eigen::MatrixXd eq_block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
2688 Eigen::MatrixXd ineq_block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
2689 edge->computeJacobians(row_i, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1);
2691 int col_start = upper_part_only ? row_i : 0;
2692 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2694 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2696 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2697 if (vert_j_dim_unfixed == 0)
continue;
2699 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
2700 block_hessian.setZero();
2702 if (edge->isObjectiveLeastSquaresForm())
2704 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
2707 Eigen::MatrixXd obj_block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
2708 edge->computeObjectiveJacobian(col_j, obj_block_jacobian2);
2711 block_hessian += 2.0 * obj_block_jacobian1.transpose() * obj_block_jacobian2;
2716 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
2718 edge->computeObjectiveHessianInc(row_i, col_j, obj_block_jacobian1, block_hessian,
nullptr, 1.0);
2722 if (upper_part_only && vertex1 == vertex2)
2725 for (
int i = 0; i < block_hessian.rows(); ++i)
2727 for (
int j = i; j < block_hessian.cols(); ++j)
2729 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2735 for (
int i = 0; i < block_hessian.rows(); ++i)
2737 for (
int j = 0; j < block_hessian.cols(); ++j)
2739 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
2763 if (edge->isLinear())
continue;
2765 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2767 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2769 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2770 if (vert_i_dim_unfixed == 0)
continue;
2772 int col_start = upper_part_only ? row_i : 0;
2773 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2778 if (vert_j_dim_unfixed == 0)
continue;
2780 if (upper_part_only && vertex1 == vertex2)
2783 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
2790 col_nnz.segment(vertex2->
getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
2797 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
2801 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2803 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2805 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2806 if (vert_i_dim_unfixed == 0)
continue;
2808 int col_start = upper_part_only ? row_i : 0;
2809 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2811 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2813 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2814 if (vert_j_dim_unfixed == 0)
continue;
2816 if (upper_part_only && vertex1 == vertex2)
2819 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
2821 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
2826 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
2835 if (edge->getObjectiveDimension() == 0 || edge->isObjectiveLinear())
continue;
2837 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2839 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2841 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2842 if (vert_i_dim_unfixed == 0)
continue;
2844 int col_start = upper_part_only ? row_i : 0;
2845 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
2847 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2849 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2850 if (vert_j_dim_unfixed == 0)
continue;
2852 if (upper_part_only && vertex1 == vertex2)
2855 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
2857 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
2862 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
2883 if (edge->isLinear())
continue;
2885 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2887 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2889 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2890 if (vert_i_dim_unfixed == 0)
continue;
2892 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2893 for (
int col_j = 0; col_j < col_end; ++col_j)
2898 if (vert_j_dim_unfixed == 0)
continue;
2900 if (lower_part_only && vertex1 == vertex2)
2902 int n = vertex1->getDimensionUnfixed();
2903 nnz +=
n * (
n + 1) / 2;
2915 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2917 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2919 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2920 if (vert_i_dim_unfixed == 0)
continue;
2922 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2923 for (
int col_j = 0; col_j < col_end; ++col_j)
2925 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
2927 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
2928 if (vert_j_dim_unfixed == 0)
continue;
2930 if (lower_part_only && vertex1 == vertex2)
2932 int n = vertex1->getDimensionUnfixed();
2933 nnz +=
n * (
n + 1) / 2;
2936 nnz += vertex1->getDimensionUnfixed() * vertex2->getDimensionUnfixed();
2950 assert(i_row.size() == j_col.size());
2959 if (edge->isLinear())
continue;
2961 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
2963 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
2965 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
2966 if (vert_i_dim_unfixed == 0)
continue;
2968 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
2969 for (
int col_j = 0; col_j < col_end; ++col_j)
2974 if (vert_j_dim_unfixed == 0)
continue;
2976 if (lower_part_only && vertex1 == vertex2)
2978 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2980 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
2982 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
2990 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
2992 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
2994 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3009 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3011 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3013 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3014 if (vert_i_dim_unfixed == 0)
continue;
3016 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3017 for (
int col_j = 0; col_j < col_end; ++col_j)
3019 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3021 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
3022 if (vert_j_dim_unfixed == 0)
continue;
3024 if (lower_part_only && vertex1 == vertex2)
3026 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3028 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
3030 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3031 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
3038 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3040 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
3042 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3043 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
3053 bool lower_part_only)
3068 if (edge->isLinear())
continue;
3070 const double* mult_eq_part = multipliers ? multipliers + edge->getEdgeIdx() :
nullptr;
3072 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3077 if (vert_i_dim_unfixed == 0)
continue;
3081 edge->computeJacobian(row_i, block_jacobian1);
3083 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3084 for (
int col_j = 0; col_j < col_end; ++col_j)
3089 if (vert_j_dim_unfixed == 0)
continue;
3093 if (lower_part_only && vertex1 == vertex2)
3096 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3097 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_eq_part);
3098 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3100 for (
int j = 0; j < i + 1; ++j)
3102 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
3111 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_eq_part);
3112 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3121 if (edge->getEqualityDimension() == 0 || edge->isEqualityLinear())
continue;
3123 const double* mult_eq_part = multipliers ? multipliers + edge->getEdgeEqualityIdx() :
nullptr;
3125 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3127 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3130 if (vert_i_dim_unfixed == 0)
continue;
3133 Eigen::MatrixXd block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
3134 edge->computeEqualityJacobian(row_i, block_jacobian1);
3136 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3137 for (
int col_j = 0; col_j < col_end; ++col_j)
3139 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3142 if (vert_j_dim_unfixed == 0)
continue;
3146 if (lower_part_only && vertex1 == vertex2)
3149 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3150 edge->computeEqualityHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_eq_part);
3151 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3153 for (
int j = 0; j < i + 1; ++j)
3155 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
3164 edge->computeEqualityHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_eq_part);
3165 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3186 if (edge->isLinear())
continue;
3188 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3190 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3192 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3193 if (vert_i_dim_unfixed == 0)
continue;
3195 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3196 for (
int col_j = 0; col_j < col_end; ++col_j)
3201 if (vert_j_dim_unfixed == 0)
continue;
3203 if (lower_part_only && vertex1 == vertex2)
3205 int n = vertex1->getDimensionUnfixed();
3206 nnz +=
n * (
n + 1) / 2;
3218 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3220 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3222 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3223 if (vert_i_dim_unfixed == 0)
continue;
3225 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3226 for (
int col_j = 0; col_j < col_end; ++col_j)
3228 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3230 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
3231 if (vert_j_dim_unfixed == 0)
continue;
3233 if (lower_part_only && vertex1 == vertex2)
3235 int n = vertex1->getDimensionUnfixed();
3236 nnz +=
n * (
n + 1) / 2;
3239 nnz += vertex1->getDimensionUnfixed() * vertex2->getDimensionUnfixed();
3253 assert(i_row.size() == j_col.size());
3262 if (edge->isLinear())
continue;
3264 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3266 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3268 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3269 if (vert_i_dim_unfixed == 0)
continue;
3271 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3272 for (
int col_j = 0; col_j < col_end; ++col_j)
3277 if (vert_j_dim_unfixed == 0)
continue;
3279 if (lower_part_only && vertex1 == vertex2)
3281 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3283 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
3285 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3293 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3295 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
3297 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3312 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3314 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3316 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3317 if (vert_i_dim_unfixed == 0)
continue;
3319 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3320 for (
int col_j = 0; col_j < col_end; ++col_j)
3322 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3324 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
3325 if (vert_j_dim_unfixed == 0)
continue;
3327 if (lower_part_only && vertex1 == vertex2)
3329 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3331 for (
int v2_idx = 0; v2_idx < v1_idx + 1; ++v2_idx)
3333 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3334 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
3341 for (
int v1_idx = 0; v1_idx < vert_i_dim_unfixed; ++v1_idx)
3343 for (
int v2_idx = 0; v2_idx < vert_j_dim_unfixed; ++v2_idx)
3345 i_row[nnz_idx] = vertex1->getVertexIdx() + v1_idx;
3346 j_col[nnz_idx] = vertex2->getVertexIdx() + v2_idx;
3356 bool lower_part_only)
3371 if (edge->isLinear())
continue;
3373 const double* mult_ineq_part = multipliers ? multipliers + edge->getEdgeIdx() :
nullptr;
3375 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3380 if (vert_i_dim_unfixed == 0)
continue;
3384 edge->computeJacobian(row_i, block_jacobian1);
3386 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3387 for (
int col_j = 0; col_j < col_end; ++col_j)
3392 if (vert_j_dim_unfixed == 0)
continue;
3396 if (lower_part_only && vertex1 == vertex2)
3399 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3400 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_ineq_part);
3401 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3403 for (
int j = 0; j < i + 1; ++j)
3405 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
3414 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_ineq_part);
3415 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3424 if (edge->getInequalityDimension() == 0 || edge->isInequalityLinear())
continue;
3426 const double* mult_ineq_part = multipliers ? multipliers + edge->getEdgeInequalityIdx() :
nullptr;
3428 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3430 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3433 if (vert_i_dim_unfixed == 0)
continue;
3436 Eigen::MatrixXd block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
3437 edge->computeInequalityJacobian(row_i, block_jacobian1);
3439 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3440 for (
int col_j = 0; col_j < col_end; ++col_j)
3442 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3445 if (vert_j_dim_unfixed == 0)
continue;
3449 if (lower_part_only && vertex1 == vertex2)
3452 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3453 edge->computeInequalityHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_ineq_part);
3454 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3456 for (
int j = 0; j < i + 1; ++j)
3458 values[nnz_idx] += block_hessian_ii.coeffRef(i, j);
3467 edge->computeInequalityHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_ineq_part);
3468 nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3494 const double* multipliers_eq,
const double* multipliers_ineq,
3495 bool lower_part_only)
3501 values_obj.setZero();
3502 values_eq.setZero();
3503 values_ineq.setZero();
3505 int obj_nnz_idx = 0;
3507 int ineq_nnz_idx = 0;
3514 if (edge->isLinear())
continue;
3516 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3521 if (vert_i_dim_unfixed == 0)
continue;
3525 edge->computeJacobian(row_i, block_jacobian1);
3527 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3528 for (
int col_j = 0; col_j < col_end; ++col_j)
3533 if (vert_j_dim_unfixed == 0)
continue;
3537 if (lower_part_only && vertex1 == vertex2)
3540 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3541 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii,
nullptr, multiplier_obj);
3542 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3544 for (
int j = 0; j < i + 1; ++j)
3546 values_obj[obj_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3555 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian,
nullptr, multiplier_obj);
3556 obj_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3563 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
3567 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3572 if (vert_i_dim_unfixed == 0)
continue;
3576 edge->computeJacobian(row_i, block_jacobian1);
3578 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3579 for (
int col_j = 0; col_j < col_end; ++col_j)
3584 if (vert_j_dim_unfixed == 0)
continue;
3588 edge->computeJacobian(col_j, block_jacobian2);
3591 if (lower_part_only && vertex1 == vertex2)
3594 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3595 block_hessian_ii.noalias() = 2.0 * multiplier_obj * block_jacobian1.transpose() * block_jacobian2;
3596 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3598 for (
int j = 0; j < i + 1; ++j)
3600 values_obj[obj_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3609 block_hessian += 2.0 * multiplier_obj * block_jacobian1.transpose() * block_jacobian2;
3610 obj_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3619 if (edge->isLinear())
continue;
3621 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
3623 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3625 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3628 if (vert_i_dim_unfixed == 0)
continue;
3631 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
3632 edge->computeJacobian(row_i, block_jacobian1);
3634 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3635 for (
int col_j = 0; col_j < col_end; ++col_j)
3637 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3640 if (vert_j_dim_unfixed == 0)
continue;
3644 if (lower_part_only && vertex1 == vertex2)
3647 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3648 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_eq_part);
3649 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3651 for (
int j = 0; j < i + 1; ++j)
3653 values_eq[eq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3662 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_eq_part);
3663 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3672 if (edge->isLinear())
continue;
3674 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
3676 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3678 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3680 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3681 if (vert_i_dim_unfixed == 0)
continue;
3684 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
3685 edge->computeJacobian(row_i, block_jacobian1);
3687 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3688 for (
int col_j = 0; col_j < col_end; ++col_j)
3690 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3692 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
3693 if (vert_j_dim_unfixed == 0)
continue;
3697 if (lower_part_only && vertex1 == vertex2)
3700 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3701 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian_ii, mult_ineq_part);
3702 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3704 for (
int j = 0; j < i + 1; ++j)
3706 values_ineq[ineq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3715 edge->computeHessianInc(row_i, col_j, block_jacobian1, block_hessian, mult_ineq_part);
3716 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3727 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
3728 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
3730 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
3732 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
3734 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
3735 if (vert_i_dim_unfixed == 0)
continue;
3738 Eigen::MatrixXd obj_block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
3739 Eigen::MatrixXd eq_block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
3740 Eigen::MatrixXd ineq_block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
3741 edge->computeJacobians(row_i, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1);
3743 int col_end = lower_part_only ? row_i + 1 : edge->getNumVertices();
3744 for (
int col_j = 0; col_j < col_end; ++col_j)
3746 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
3748 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
3749 if (vert_j_dim_unfixed == 0)
continue;
3751 if (edge->isObjectiveLeastSquaresForm())
3753 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
3756 Eigen::MatrixXd obj_block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
3757 edge->computeObjectiveJacobian(col_j, obj_block_jacobian2);
3760 if (lower_part_only && vertex1 == vertex2)
3763 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3764 block_hessian_ii.noalias() = 2.0 * multiplier_obj * obj_block_jacobian1.transpose() * obj_block_jacobian2;
3765 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3767 for (
int j = 0; j < i + 1; ++j)
3769 values_obj[obj_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3778 obj_block_hessian += 2.0 * multiplier_obj * obj_block_jacobian1.transpose() * obj_block_jacobian2;
3779 obj_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3783 if (!edge->isEqualityLinear() && !edge->isInequalityLinear())
3785 if (lower_part_only && vertex1 == vertex2)
3788 Eigen::MatrixXd eq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3789 Eigen::MatrixXd ineq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3790 edge->computeConstraintHessians(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, eq_block_hessian_ii,
3791 ineq_block_hessian_ii, mult_eq_part, mult_ineq_part);
3792 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3794 for (
int j = 0; j < i + 1; ++j)
3796 values_eq[eq_nnz_idx] += eq_block_hessian_ii.coeffRef(i, j);
3798 values_ineq[ineq_nnz_idx] += ineq_block_hessian_ii.coeffRef(i, j);
3808 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, eq_block_hessian,
3809 ineq_block_hessian, mult_eq_part, mult_ineq_part);
3810 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3811 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3816 if (!edge->isEqualityLinear())
3818 if (lower_part_only && vertex1 == vertex2)
3821 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3822 edge->computeEqualityHessian(row_i, col_j, eq_block_jacobian1, block_hessian_ii, mult_eq_part);
3823 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3825 for (
int j = 0; j < i + 1; ++j)
3827 values_eq[eq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3836 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, eq_block_hessian, mult_eq_part);
3837 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3840 else if (!edge->isInequalityLinear())
3842 if (lower_part_only && vertex1 == vertex2)
3845 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3846 edge->computeInequalityHessian(row_i, col_j, ineq_block_jacobian1, block_hessian_ii, mult_ineq_part);
3847 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3849 for (
int j = 0; j < i + 1; ++j)
3851 values_ineq[ineq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3860 vert_j_dim_unfixed);
3861 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, ineq_block_hessian, mult_ineq_part);
3862 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3869 if (!edge->isObjectiveLinear() && !edge->isEqualityLinear() && !edge->isInequalityLinear() &&
3870 edge->getObjectiveDimension() != 0 && edge->getEqualityDimension() != 0 && edge->getInequalityDimension() != 0)
3872 if (lower_part_only && vertex1 == vertex2)
3875 Eigen::MatrixXd obj_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3876 Eigen::MatrixXd eq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3877 Eigen::MatrixXd ineq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3878 edge->computeHessians(row_i, col_j, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1, obj_block_hessian_ii,
3879 eq_block_hessian_ii, ineq_block_hessian_ii, mult_eq_part, mult_ineq_part, multiplier_obj);
3880 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3882 for (
int j = 0; j < i + 1; ++j)
3884 values_obj[obj_nnz_idx] += obj_block_hessian_ii.coeffRef(i, j);
3886 values_eq[eq_nnz_idx] += eq_block_hessian_ii.coeffRef(i, j);
3888 values_ineq[ineq_nnz_idx] += ineq_block_hessian_ii.coeffRef(i, j);
3899 edge->computeHessiansInc(row_i, col_j, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1, obj_block_hessian,
3900 eq_block_hessian, ineq_block_hessian, mult_eq_part, mult_ineq_part, multiplier_obj);
3901 obj_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3902 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3903 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3906 else if (!edge->isEqualityLinear() && !edge->isInequalityLinear() && edge->getEqualityDimension() != 0 &&
3907 edge->getInequalityDimension() != 0)
3909 if (lower_part_only && vertex1 == vertex2)
3912 Eigen::MatrixXd eq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3913 Eigen::MatrixXd ineq_block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3914 edge->computeConstraintHessians(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, eq_block_hessian_ii,
3915 ineq_block_hessian_ii, mult_eq_part, mult_ineq_part);
3916 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3918 for (
int j = 0; j < i + 1; ++j)
3920 values_eq[eq_nnz_idx] += eq_block_hessian_ii.coeffRef(i, j);
3922 values_ineq[ineq_nnz_idx] += ineq_block_hessian_ii.coeffRef(i, j);
3932 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, eq_block_hessian,
3933 ineq_block_hessian, mult_eq_part, mult_ineq_part);
3934 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3935 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3940 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
3942 if (lower_part_only && vertex1 == vertex2)
3945 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3946 edge->computeObjectiveHessian(row_i, col_j, obj_block_jacobian1, block_hessian_ii,
nullptr, multiplier_obj);
3947 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3949 for (
int j = 0; j < i + 1; ++j)
3951 values_obj[obj_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3960 vert_j_dim_unfixed);
3961 edge->computeObjectiveHessianInc(row_i, col_j, obj_block_jacobian1, obj_block_hessian,
nullptr, multiplier_obj);
3962 obj_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3966 if (!edge->isEqualityLinear() && edge->getEqualityDimension() != 0)
3968 if (lower_part_only && vertex1 == vertex2)
3971 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3972 edge->computeEqualityHessian(row_i, col_j, eq_block_jacobian1, block_hessian_ii, mult_eq_part);
3973 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3975 for (
int j = 0; j < i + 1; ++j)
3977 values_eq[eq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
3986 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, eq_block_hessian, mult_eq_part);
3987 eq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
3990 else if (!edge->isInequalityLinear() && edge->getInequalityDimension() != 0)
3992 if (lower_part_only && vertex1 == vertex2)
3995 Eigen::MatrixXd block_hessian_ii(vert_i_dim_unfixed, vert_j_dim_unfixed);
3996 edge->computeInequalityHessian(row_i, col_j, ineq_block_jacobian1, block_hessian_ii, mult_ineq_part);
3997 for (
int i = 0; i < vert_i_dim_unfixed; ++i)
3999 for (
int j = 0; j < i + 1; ++j)
4001 values_ineq[ineq_nnz_idx] += block_hessian_ii.coeffRef(i, j);
4010 vert_j_dim_unfixed);
4011 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, ineq_block_hessian, mult_ineq_part);
4012 ineq_nnz_idx += vert_i_dim_unfixed * vert_j_dim_unfixed;
4023 const double* multipliers_eq,
const double* multipliers_ineq,
4024 const Eigen::VectorXi* col_nnz,
bool upper_part_only)
4028 if (col_nnz) H.
reserve(*col_nnz);
4035 if (edge->isLinear())
continue;
4037 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4039 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4041 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4042 if (vert_i_dim_unfixed == 0)
continue;
4045 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
4046 edge->computeJacobian(row_i, block_jacobian1);
4048 int col_start = upper_part_only ? row_i : 0;
4049 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4054 if (vert_j_dim_unfixed == 0)
continue;
4058 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
4059 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian);
4062 if (upper_part_only && vertex1 == vertex2)
4065 for (
int i = 0; i < block_hessian.rows(); ++i)
4067 for (
int j = i; j < block_hessian.cols(); ++j)
4069 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4075 for (
int i = 0; i < block_hessian.rows(); ++i)
4077 for (
int j = 0; j < block_hessian.cols(); ++j)
4079 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4088 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
4092 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4094 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4096 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4097 if (vert_i_dim_unfixed == 0)
continue;
4100 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
4101 edge->computeJacobian(row_i, block_jacobian1);
4103 int col_start = upper_part_only ? row_i : 0;
4104 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4106 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4109 if (vert_j_dim_unfixed == 0)
continue;
4112 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
4113 edge->computeJacobian(col_j, block_jacobian2);
4116 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
4117 block_hessian = 2.0 * block_jacobian1.transpose() * block_jacobian2;
4120 if (upper_part_only && vertex1 == vertex2)
4123 for (
int i = 0; i < block_hessian.rows(); ++i)
4125 for (
int j = i; j < block_hessian.cols(); ++j)
4127 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4133 for (
int i = 0; i < block_hessian.rows(); ++i)
4135 for (
int j = 0; j < block_hessian.cols(); ++j)
4137 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4148 if (edge->isLinear())
continue;
4150 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
4152 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4154 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4156 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4157 if (vert_i_dim_unfixed == 0)
continue;
4160 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
4161 edge->computeJacobian(row_i, block_jacobian1);
4163 int col_start = upper_part_only ? row_i : 0;
4164 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4166 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4168 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4169 if (vert_j_dim_unfixed == 0)
continue;
4173 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
4174 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian, mult_eq_part);
4177 if (upper_part_only && vertex1 == vertex2)
4180 for (
int i = 0; i < block_hessian.rows(); ++i)
4182 for (
int j = i; j < block_hessian.cols(); ++j)
4184 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4190 for (
int i = 0; i < block_hessian.rows(); ++i)
4192 for (
int j = 0; j < block_hessian.cols(); ++j)
4194 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4205 if (edge->isLinear())
continue;
4207 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
4209 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4211 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4213 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4214 if (vert_i_dim_unfixed == 0)
continue;
4217 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
4218 edge->computeJacobian(row_i, block_jacobian1);
4220 int col_start = upper_part_only ? row_i : 0;
4221 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4223 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4225 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4226 if (vert_j_dim_unfixed == 0)
continue;
4230 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
4231 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian, mult_ineq_part);
4234 if (upper_part_only && vertex1 == vertex2)
4237 for (
int i = 0; i < block_hessian.rows(); ++i)
4239 for (
int j = i; j < block_hessian.cols(); ++j)
4241 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4247 for (
int i = 0; i < block_hessian.rows(); ++i)
4249 for (
int j = 0; j < block_hessian.cols(); ++j)
4251 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4264 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
4265 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
4267 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4269 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4271 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4272 if (vert_i_dim_unfixed == 0)
continue;
4275 Eigen::MatrixXd obj_block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
4276 Eigen::MatrixXd eq_block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
4277 Eigen::MatrixXd ineq_block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
4278 edge->computeJacobians(row_i, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1);
4280 int col_start = upper_part_only ? row_i : 0;
4281 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4283 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4285 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4286 if (vert_j_dim_unfixed == 0)
continue;
4288 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
4289 block_hessian.setZero();
4291 if (edge->isObjectiveLeastSquaresForm())
4293 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
4296 Eigen::MatrixXd obj_block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
4297 edge->computeObjectiveJacobian(col_j, obj_block_jacobian2);
4300 block_hessian += 2.0 * obj_block_jacobian1.transpose() * obj_block_jacobian2;
4303 if (!edge->isEqualityLinear() && !edge->isInequalityLinear())
4305 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, block_hessian, block_hessian,
4306 mult_eq_part, mult_ineq_part);
4310 if (!edge->isEqualityLinear())
4312 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, block_hessian, mult_eq_part);
4314 else if (!edge->isInequalityLinear())
4316 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, block_hessian, mult_ineq_part);
4322 if (!edge->isObjectiveLinear() && !edge->isEqualityLinear() && !edge->isInequalityLinear() &&
4323 edge->getObjectiveDimension() != 0 && edge->getEqualityDimension() != 0 && edge->getInequalityDimension() != 0)
4325 edge->computeHessiansInc(row_i, col_j, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1, block_hessian,
4326 block_hessian, block_hessian, mult_eq_part, mult_ineq_part, 1.0);
4328 else if (!edge->isEqualityLinear() && !edge->isInequalityLinear() && edge->getEqualityDimension() != 0 &&
4329 edge->getInequalityDimension() != 0)
4331 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, block_hessian, block_hessian,
4332 mult_eq_part, mult_ineq_part);
4336 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
4338 edge->computeObjectiveHessianInc(row_i, col_j, obj_block_jacobian1, block_hessian,
nullptr, 1.0);
4341 if (!edge->isEqualityLinear() && edge->getEqualityDimension() != 0)
4343 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, block_hessian, mult_eq_part);
4345 else if (!edge->isInequalityLinear() && edge->getInequalityDimension() != 0)
4347 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, block_hessian, mult_ineq_part);
4352 if (upper_part_only && vertex1 == vertex2)
4355 for (
int i = 0; i < block_hessian.rows(); ++i)
4357 for (
int j = i; j < block_hessian.cols(); ++j)
4359 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4365 for (
int i = 0; i < block_hessian.rows(); ++i)
4367 for (
int j = 0; j < block_hessian.cols(); ++j)
4369 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
4393 if (edge->isLinear())
continue;
4395 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4397 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4399 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4400 if (vert_i_dim_unfixed == 0)
continue;
4402 int col_start = upper_part_only ? row_i : 0;
4403 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4408 if (vert_j_dim_unfixed == 0)
continue;
4410 if (upper_part_only && vertex1 == vertex2)
4413 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
4420 col_nnz.segment(vertex2->
getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
4427 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
4431 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4433 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4435 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4436 if (vert_i_dim_unfixed == 0)
continue;
4438 int col_start = upper_part_only ? row_i : 0;
4439 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4441 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4443 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4444 if (vert_j_dim_unfixed == 0)
continue;
4446 if (upper_part_only && vertex1 == vertex2)
4449 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
4451 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
4456 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
4465 if (edge->isLinear())
continue;
4467 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4469 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4471 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4472 if (vert_i_dim_unfixed == 0)
continue;
4474 int col_start = upper_part_only ? row_i : 0;
4475 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4477 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4479 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4480 if (vert_j_dim_unfixed == 0)
continue;
4482 if (upper_part_only && vertex1 == vertex2)
4485 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
4487 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
4492 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
4501 if (edge->isLinear())
continue;
4503 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4505 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4507 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4508 if (vert_i_dim_unfixed == 0)
continue;
4510 int col_start = upper_part_only ? row_i : 0;
4511 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4513 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4515 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4516 if (vert_j_dim_unfixed == 0)
continue;
4518 if (upper_part_only && vertex1 == vertex2)
4521 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
4523 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
4528 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
4539 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4541 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
4543 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
4544 if (vert_i_dim_unfixed == 0)
continue;
4546 int col_start = upper_part_only ? row_i : 0;
4547 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
4549 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
4551 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
4552 if (vert_j_dim_unfixed == 0)
continue;
4554 if (upper_part_only && vertex1 == vertex2)
4557 for (
int l = 0; l < vert_j_dim_unfixed; ++l)
4559 col_nnz(vertex2->getVertexIdx() + l) += l + 1;
4564 col_nnz.segment(vertex2->getVertexIdx(), vert_j_dim_unfixed).array() += vert_i_dim_unfixed;
4572 bool include_finite_bounds,
const Eigen::VectorXi* col_nnz)
4577 if (col_nnz)
A.reserve(*col_nnz);
4585 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4587 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4589 int vert_dim_unfixed = vertex->getDimensionUnfixed();
4590 if (vert_dim_unfixed == 0)
continue;
4593 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
4594 edge->computeJacobian(vert_idx, block_jacobian);
4597 for (
int i = 0; i < block_jacobian.rows(); ++i)
4599 for (
int j = 0; j < block_jacobian.cols(); ++j)
4601 A.insert(edge->getEdgeIdx() + i, vertex->getVertexIdx() + j) = block_jacobian.coeffRef(i, j);
4610 int A_edge_idx = inequality_row_start + edge->getEdgeIdx();
4611 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4613 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4615 int vert_dim_unfixed = vertex->getDimensionUnfixed();
4616 if (vert_dim_unfixed == 0)
continue;
4619 Eigen::MatrixXd block_jacobian(edge->getDimension(), vert_dim_unfixed);
4620 edge->computeJacobian(vert_idx, block_jacobian);
4623 for (
int i = 0; i < block_jacobian.rows(); ++i)
4625 for (
int j = 0; j < block_jacobian.cols(); ++j)
4627 A.insert(A_edge_idx + i, vertex->getVertexIdx() + j) = block_jacobian.coeffRef(i, j);
4636 int A_edge_eq_idx = edge->getEdgeEqualityIdx();
4637 int A_edge_ineq_idx = inequality_row_start + edge->getEdgeInequalityIdx();
4639 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4641 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4643 int vert_dim_unfixed = vertex->getDimensionUnfixed();
4644 if (vert_dim_unfixed == 0)
continue;
4647 Eigen::MatrixXd block_jacobian_eq(edge->getEqualityDimension(), vert_dim_unfixed);
4648 Eigen::MatrixXd block_jacobian_ineq(edge->getInequalityDimension(), vert_dim_unfixed);
4649 edge->computeConstraintJacobians(vert_idx, block_jacobian_eq, block_jacobian_ineq);
4652 for (
int i = 0; i < block_jacobian_eq.rows(); ++i)
4654 for (
int j = 0; j < block_jacobian_eq.cols(); ++j)
4656 A.insert(A_edge_eq_idx + i, vertex->getVertexIdx() + j) = block_jacobian_eq.coeffRef(i, j);
4659 for (
int i = 0; i < block_jacobian_ineq.rows(); ++i)
4661 for (
int j = 0; j < block_jacobian_ineq.cols(); ++j)
4663 A.insert(A_edge_ineq_idx + i, vertex->getVertexIdx() + j) = block_jacobian_ineq.coeffRef(i, j);
4669 if (include_finite_bounds)
4671 int row_idx = bounds_row_start;
4674 int vert_idx = vertex->getVertexIdx();
4676 for (
int i = 0; i < vertex->getDimension(); ++i)
4678 if (vertex->isFixedComponent(i))
continue;
4680 if (vertex->hasFiniteLowerBound(i) || vertex->hasFiniteUpperBound(i))
4682 A.insert(row_idx, vert_idx + free_idx) = 1;
4692 bool include_finite_bounds)
4703 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4705 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4706 int vert_dim_unfixed = vertex->getDimensionUnfixed();
4707 if (vert_dim_unfixed == 0)
continue;
4709 col_nnz.segment(vertex->getVertexIdx(), vert_dim_unfixed).array() += edge->getDimension();
4716 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4720 if (vert_dim_unfixed == 0)
continue;
4722 col_nnz.segment(vertex->
getVertexIdx(), vert_dim_unfixed).array() += edge->getDimension();
4729 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4734 if (vert_dim_unfixed == 0)
continue;
4736 col_nnz.segment(vertex->
getVertexIdx(), vert_dim_unfixed).array() += edge->getEqualityDimension() + edge->getInequalityDimension();
4740 if (include_finite_bounds)
4754 col_nnz[vert_idx + free_idx] += 1;
4773 bool include_finite_bounds)
4776 assert(j_col.size() == i_row.size());
4788 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4798 for (
int j = 0; j < edge->getDimension(); ++j)
4800 i_row[nnz_idx] = edge->getEdgeIdx() + j;
4813 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4815 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4818 for (
int i = 0; i < vertex->getDimension(); ++i)
4820 if (!vertex->isFixedComponent(i))
4823 for (
int j = 0; j < edge->getDimension(); ++j)
4825 i_row[nnz_idx] = inequality_row_start + edge->getEdgeIdx() + j;
4826 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
4838 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4840 const VertexInterface* vertex = edge->getVertexRaw(vert_idx);
4845 for (
int i = 0; i < vertex->getDimension(); ++i)
4847 if (!vertex->isFixedComponent(i))
4849 for (
int j = 0; j < edge->getEqualityDimension(); ++j)
4851 i_row[nnz_idx] = edge->getEdgeEqualityIdx() + j;
4852 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
4861 for (
int i = 0; i < vertex->getDimension(); ++i)
4863 if (!vertex->isFixedComponent(i))
4865 for (
int j = 0; j < edge->getInequalityDimension(); ++j)
4867 i_row[nnz_idx] = inequality_row_start + edge->getEdgeInequalityIdx() + j;
4868 j_col[nnz_idx] = vertex->getVertexIdx() + idx_free;
4877 if (include_finite_bounds)
4879 int row_idx = bounds_row_start;
4882 int vert_idx = vertex->getVertexIdx();
4884 for (
int i = 0; i < vertex->getDimension(); ++i)
4886 if (vertex->isFixedComponent(i))
continue;
4888 if (vertex->hasFiniteLowerBound(i) || vertex->hasFiniteUpperBound(i))
4890 i_row[nnz_idx] = row_idx;
4891 j_col[nnz_idx] = vert_idx + free_idx;
4901 assert(nnz_idx == i_row.size());
4905 bool include_finite_bounds)
4916 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4918 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
4919 if (vert_dim_unfixed == 0)
continue;
4922 edge->computeJacobian(vert_idx, block_jacobian);
4923 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
4930 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4932 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
4933 if (vert_dim_unfixed == 0)
continue;
4936 edge->computeJacobian(vert_idx, block_jacobian);
4937 nnz_idx += block_jacobian.rows() * block_jacobian.cols();
4944 if (edge->getEqualityDimension() == 0 && edge->getInequalityDimension() == 0)
continue;
4947 for (
int vert_idx = 0; vert_idx < edge->getNumVertices(); ++vert_idx)
4949 int vert_dim_unfixed = edge->getVertexRaw(vert_idx)->getDimensionUnfixed();
4950 if (vert_dim_unfixed == 0)
continue;
4953 nnz_idx += block_eq.rows() * block_eq.cols();
4955 nnz_idx += block_ineq.rows() * block_ineq.cols();
4957 edge->computeConstraintJacobians(vert_idx, block_eq, block_ineq);
4961 if (include_finite_bounds)
4972 const Eigen::VectorXi* col_nnz_A,
bool upper_part_only_H)
4976 if (col_nnz_H) H.
reserve(*col_nnz_H);
4979 if (col_nnz_A)
A.reserve(*col_nnz_A);
4989 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
4994 if (vert_i_dim_unfixed == 0)
continue;
4998 edge->computeJacobian(row_i, block_jacobian1);
5000 if (edge->isLinear())
continue;
5002 int col_start = upper_part_only_H ? row_i : 0;
5003 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5008 if (vert_j_dim_unfixed == 0)
continue;
5012 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5013 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian);
5016 if (upper_part_only_H && vertex1 == vertex2)
5019 for (
int i = 0; i < block_hessian.rows(); ++i)
5021 for (
int j = i; j < block_hessian.cols(); ++j)
5029 for (
int i = 0; i < block_hessian.rows(); ++i)
5031 for (
int j = 0; j < block_hessian.cols(); ++j)
5042 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
5046 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5048 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5051 if (vert_i_dim_unfixed == 0)
continue;
5054 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5055 edge->computeJacobian(row_i, block_jacobian1);
5057 if (edge->isLinear())
continue;
5059 int col_start = upper_part_only_H ? row_i : 0;
5060 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5062 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5065 if (vert_j_dim_unfixed == 0)
continue;
5068 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
5069 edge->computeJacobian(col_j, block_jacobian2);
5072 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5073 block_hessian = 2.0 * block_jacobian1.transpose() * block_jacobian2;
5076 if (upper_part_only_H && vertex1 == vertex2)
5079 for (
int i = 0; i < block_hessian.rows(); ++i)
5081 for (
int j = i; j < block_hessian.cols(); ++j)
5083 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5089 for (
int i = 0; i < block_hessian.rows(); ++i)
5091 for (
int j = 0; j < block_hessian.cols(); ++j)
5093 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5104 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeIdx() :
nullptr;
5106 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5108 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5110 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5111 if (vert_i_dim_unfixed == 0)
continue;
5114 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5115 edge->computeJacobian(row_i, block_jacobian1);
5118 for (
int i = 0; i < block_jacobian1.rows(); ++i)
5120 for (
int j = 0; j < block_jacobian1.cols(); ++j)
5122 A.insert(edge->getEdgeIdx() + i, vertex1->getVertexIdx() + j) = block_jacobian1.coeffRef(i, j);
5126 if (edge->isLinear())
continue;
5128 int col_start = upper_part_only_H ? row_i : 0;
5129 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5131 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5133 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
5134 if (vert_j_dim_unfixed == 0)
continue;
5138 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5139 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian, mult_eq_part);
5142 if (upper_part_only_H && vertex1 == vertex2)
5145 for (
int i = 0; i < block_hessian.rows(); ++i)
5147 for (
int j = i; j < block_hessian.cols(); ++j)
5149 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5155 for (
int i = 0; i < block_hessian.rows(); ++i)
5157 for (
int j = 0; j < block_hessian.cols(); ++j)
5159 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5170 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeIdx() :
nullptr;
5171 int A_edge_idx = inequality_row_start + edge->getEdgeIdx();
5173 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5175 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5177 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5178 if (vert_i_dim_unfixed == 0)
continue;
5181 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5182 edge->computeJacobian(row_i, block_jacobian1);
5185 for (
int i = 0; i < block_jacobian1.rows(); ++i)
5187 for (
int j = 0; j < block_jacobian1.cols(); ++j)
5189 A.insert(A_edge_idx + i, vertex1->getVertexIdx() + j) = block_jacobian1.coeffRef(i, j);
5193 if (edge->isLinear())
continue;
5195 int col_start = upper_part_only_H ? row_i : 0;
5196 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5198 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5200 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
5201 if (vert_j_dim_unfixed == 0)
continue;
5205 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5206 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian, mult_ineq_part);
5209 if (upper_part_only_H && vertex1 == vertex2)
5212 for (
int i = 0; i < block_hessian.rows(); ++i)
5214 for (
int j = i; j < block_hessian.cols(); ++j)
5216 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5222 for (
int i = 0; i < block_hessian.rows(); ++i)
5224 for (
int j = 0; j < block_hessian.cols(); ++j)
5226 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5239 const double* mult_eq_part = multipliers_eq ? multipliers_eq + edge->getEdgeEqualityIdx() :
nullptr;
5240 const double* mult_ineq_part = multipliers_ineq ? multipliers_ineq + edge->getEdgeInequalityIdx() :
nullptr;
5242 int A_edge_eq_idx = edge->getEdgeEqualityIdx();
5243 int A_edge_ineq_idx = inequality_row_start + edge->getEdgeInequalityIdx();
5245 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5247 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5249 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5250 if (vert_i_dim_unfixed == 0)
continue;
5253 Eigen::MatrixXd obj_block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
5254 Eigen::MatrixXd eq_block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
5255 Eigen::MatrixXd ineq_block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
5256 edge->computeJacobians(row_i, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1);
5259 for (
int i = 0; i < eq_block_jacobian1.rows(); ++i)
5261 for (
int j = 0; j < eq_block_jacobian1.cols(); ++j)
5263 A.insert(A_edge_eq_idx + i, vertex1->getVertexIdx() + j) = eq_block_jacobian1.coeffRef(i, j);
5266 for (
int i = 0; i < ineq_block_jacobian1.rows(); ++i)
5268 for (
int j = 0; j < ineq_block_jacobian1.cols(); ++j)
5270 A.insert(A_edge_ineq_idx + i, vertex1->getVertexIdx() + j) = ineq_block_jacobian1.coeffRef(i, j);
5274 int col_start = upper_part_only_H ? row_i : 0;
5275 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5277 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5279 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
5280 if (vert_j_dim_unfixed == 0)
continue;
5282 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5283 block_hessian.setZero();
5285 if (edge->isObjectiveLeastSquaresForm())
5287 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
5290 Eigen::MatrixXd obj_block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
5291 edge->computeObjectiveJacobian(col_j, obj_block_jacobian2);
5294 block_hessian += 2.0 * obj_block_jacobian1.transpose() * obj_block_jacobian2;
5297 if (!edge->isEqualityLinear() && !edge->isInequalityLinear())
5299 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, block_hessian, block_hessian,
5300 mult_eq_part, mult_ineq_part);
5304 if (!edge->isEqualityLinear())
5306 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, block_hessian, mult_eq_part);
5308 else if (!edge->isInequalityLinear())
5310 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, block_hessian, mult_ineq_part);
5316 if (!edge->isObjectiveLinear() && !edge->isEqualityLinear() && !edge->isInequalityLinear() &&
5317 edge->getObjectiveDimension() != 0 && edge->getEqualityDimension() != 0 && edge->getInequalityDimension() != 0)
5319 edge->computeHessiansInc(row_i, col_j, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1, block_hessian,
5320 block_hessian, block_hessian, mult_eq_part, mult_ineq_part, 1.0);
5322 else if (!edge->isEqualityLinear() && !edge->isInequalityLinear() && edge->getEqualityDimension() != 0 &&
5323 edge->getInequalityDimension() != 0)
5325 edge->computeConstraintHessiansInc(row_i, col_j, eq_block_jacobian1, ineq_block_jacobian1, block_hessian, block_hessian,
5326 mult_eq_part, mult_ineq_part);
5330 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
5332 edge->computeObjectiveHessianInc(row_i, col_j, obj_block_jacobian1, block_hessian,
nullptr, 1.0);
5335 if (!edge->isEqualityLinear() && edge->getEqualityDimension() != 0)
5337 edge->computeEqualityHessianInc(row_i, col_j, eq_block_jacobian1, block_hessian, mult_eq_part);
5339 else if (!edge->isInequalityLinear() && edge->getInequalityDimension() != 0)
5341 edge->computeInequalityHessianInc(row_i, col_j, ineq_block_jacobian1, block_hessian, mult_ineq_part);
5346 if (upper_part_only_H && vertex1 == vertex2)
5349 for (
int i = 0; i < block_hessian.rows(); ++i)
5351 for (
int j = i; j < block_hessian.cols(); ++j)
5353 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5359 for (
int i = 0; i < block_hessian.rows(); ++i)
5361 for (
int j = 0; j < block_hessian.cols(); ++j)
5363 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5371 if (include_finite_bounds)
5373 int row_idx = bounds_row_start;
5376 int vert_idx = vertex->getVertexIdx();
5378 for (
int i = 0; i < vertex->getDimension(); ++i)
5380 if (vertex->isFixedComponent(i))
continue;
5382 if (vertex->hasFiniteLowerBound(i) || vertex->hasFiniteUpperBound(i))
5384 A.insert(row_idx, vert_idx + free_idx) = 1;
5395 bool include_finite_bounds,
const Eigen::VectorXi* col_nnz_H,
const Eigen::VectorXi* col_nnz_A,
bool upper_part_only_H)
5399 if (col_nnz_H) H.
reserve(*col_nnz_H);
5402 if (col_nnz_A)
A.reserve(*col_nnz_A);
5412 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5414 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5416 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5417 if (vert_i_dim_unfixed == 0)
continue;
5420 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5421 edge->computeJacobian(row_i, block_jacobian1);
5423 if (edge->isLinear())
continue;
5425 int col_start = upper_part_only_H ? row_i : 0;
5426 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5431 if (vert_j_dim_unfixed == 0)
continue;
5435 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5436 edge->computeHessian(row_i, col_j, block_jacobian1, block_hessian);
5439 if (upper_part_only_H && vertex1 == vertex2)
5442 for (
int i = 0; i < block_hessian.rows(); ++i)
5444 for (
int j = i; j < block_hessian.cols(); ++j)
5446 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5452 for (
int i = 0; i < block_hessian.rows(); ++i)
5454 for (
int j = 0; j < block_hessian.cols(); ++j)
5456 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->
getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5465 for (
BaseEdge::Ptr& edge : edges->getLsqObjectiveEdgesRef())
5469 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5471 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5473 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5474 if (vert_i_dim_unfixed == 0)
continue;
5477 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5478 edge->computeJacobian(row_i, block_jacobian1);
5480 if (edge->isLinear())
continue;
5482 int col_start = upper_part_only_H ? row_i : 0;
5483 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5485 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5488 if (vert_j_dim_unfixed == 0)
continue;
5491 Eigen::MatrixXd block_jacobian2(edge->getDimension(), vertex2->getDimensionUnfixed());
5492 edge->computeJacobian(col_j, block_jacobian2);
5495 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5496 block_hessian = 2.0 * block_jacobian1.transpose() * block_jacobian2;
5499 if (upper_part_only_H && vertex1 == vertex2)
5502 for (
int i = 0; i < block_hessian.rows(); ++i)
5504 for (
int j = i; j < block_hessian.cols(); ++j)
5506 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5512 for (
int i = 0; i < block_hessian.rows(); ++i)
5514 for (
int j = 0; j < block_hessian.cols(); ++j)
5516 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5527 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5529 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5531 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5532 if (vert_i_dim_unfixed == 0)
continue;
5535 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5536 edge->computeJacobian(row_i, block_jacobian1);
5539 for (
int i = 0; i < block_jacobian1.rows(); ++i)
5541 for (
int j = 0; j < block_jacobian1.cols(); ++j)
5543 A.insert(edge->getEdgeIdx() + i, vertex1->getVertexIdx() + j) = block_jacobian1.coeffRef(i, j);
5552 int A_edge_idx = inequality_row_start + edge->getEdgeIdx();
5554 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5556 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5558 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5559 if (vert_i_dim_unfixed == 0)
continue;
5562 Eigen::MatrixXd block_jacobian1(edge->getDimension(), vertex1->getDimensionUnfixed());
5563 edge->computeJacobian(row_i, block_jacobian1);
5566 for (
int i = 0; i < block_jacobian1.rows(); ++i)
5568 for (
int j = 0; j < block_jacobian1.cols(); ++j)
5570 A.insert(A_edge_idx + i, vertex1->getVertexIdx() + j) = block_jacobian1.coeffRef(i, j);
5580 int A_edge_eq_idx = edge->getEdgeEqualityIdx();
5581 int A_edge_ineq_idx = inequality_row_start + edge->getEdgeInequalityIdx();
5583 for (
int row_i = 0; row_i < edge->getNumVertices(); ++row_i)
5585 const VertexInterface* vertex1 = edge->getVertexRaw(row_i);
5587 int vert_i_dim_unfixed = vertex1->getDimensionUnfixed();
5588 if (vert_i_dim_unfixed == 0)
continue;
5591 Eigen::MatrixXd obj_block_jacobian1(edge->getObjectiveDimension(), vertex1->getDimensionUnfixed());
5592 Eigen::MatrixXd eq_block_jacobian1(edge->getEqualityDimension(), vertex1->getDimensionUnfixed());
5593 Eigen::MatrixXd ineq_block_jacobian1(edge->getInequalityDimension(), vertex1->getDimensionUnfixed());
5594 edge->computeJacobians(row_i, obj_block_jacobian1, eq_block_jacobian1, ineq_block_jacobian1);
5597 for (
int i = 0; i < eq_block_jacobian1.rows(); ++i)
5599 for (
int j = 0; j < eq_block_jacobian1.cols(); ++j)
5601 A.insert(A_edge_eq_idx + i, vertex1->getVertexIdx() + j) = eq_block_jacobian1.coeffRef(i, j);
5604 for (
int i = 0; i < ineq_block_jacobian1.rows(); ++i)
5606 for (
int j = 0; j < ineq_block_jacobian1.cols(); ++j)
5608 A.insert(A_edge_ineq_idx + i, vertex1->getVertexIdx() + j) = ineq_block_jacobian1.coeffRef(i, j);
5612 if (edge->isObjectiveLinear() || edge->getObjectiveDimension() == 0)
continue;
5614 int col_start = upper_part_only_H ? row_i : 0;
5615 for (
int col_j = col_start; col_j < edge->getNumVertices(); ++col_j)
5617 const VertexInterface* vertex2 = edge->getVertexRaw(col_j);
5619 int vert_j_dim_unfixed = vertex2->getDimensionUnfixed();
5620 if (vert_j_dim_unfixed == 0)
continue;
5622 Eigen::MatrixXd block_hessian(vert_i_dim_unfixed, vert_j_dim_unfixed);
5623 block_hessian.setZero();
5625 if (edge->isObjectiveLeastSquaresForm())
5627 if (!edge->isObjectiveLinear() && edge->getObjectiveDimension() != 0)
5630 Eigen::MatrixXd obj_block_jacobian2(edge->getObjectiveDimension(), vertex2->getDimensionUnfixed());
5631 edge->computeObjectiveJacobian(col_j, obj_block_jacobian2);
5634 block_hessian += 2.0 * obj_block_jacobian1.transpose() * obj_block_jacobian2;
5639 edge->computeObjectiveHessianInc(row_i, col_j, obj_block_jacobian1, block_hessian,
nullptr, 1.0);
5643 if (upper_part_only_H && vertex1 == vertex2)
5646 for (
int i = 0; i < block_hessian.rows(); ++i)
5648 for (
int j = i; j < block_hessian.cols(); ++j)
5650 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5656 for (
int i = 0; i < block_hessian.rows(); ++i)
5658 for (
int j = 0; j < block_hessian.cols(); ++j)
5660 H.
coeffRef(vertex1->getVertexIdx() + i, vertex2->getVertexIdx() + j) += block_hessian.coeffRef(i, j);
5668 if (include_finite_bounds)
5670 int row_idx = bounds_row_start;
5673 int vert_idx = vertex->getVertexIdx();
5675 for (
int i = 0; i < vertex->getDimension(); ++i)
5677 if (vertex->isFixedComponent(i))
continue;
5679 if (vertex->hasFiniteLowerBound(i) || vertex->hasFiniteUpperBound(i))
5681 A.insert(row_idx, vert_idx + free_idx) = 1;