10 const bool scale_invariant,
12 const std::vector<int> &surface_selections) :
13 AdjointForm(variable_to_simulations), state_(state), scale_invariant_(scale_invariant), power_(power)
16 const int dim = mesh.dimension();
17 const int n_verts = mesh.n_vertices();
18 assert(mesh.is_simplicial());
20 surface_ids_ = std::set(surface_selections.begin(), surface_selections.end());
23 std::vector<bool> active_mask;
24 active_mask.assign(n_verts,
false);
25 std::vector<Eigen::Triplet<bool>> T_adj;
27 for (
int b = 0; b < mesh.n_boundary_elements(); b++)
29 const int boundary_id = mesh.get_boundary_id(b);
33 for (
int lv = 0; lv < dim; lv++)
35 active_mask[mesh.boundary_element_vertex(b, lv)] =
true;
38 for (
int lv1 = 0; lv1 < dim; lv1++)
39 for (
int lv2 = 0; lv2 < lv1; lv2++)
41 const int v1 = mesh.boundary_element_vertex(b, lv1);
42 const int v2 = mesh.boundary_element_vertex(b, lv2);
43 T_adj.emplace_back(v2, v1,
true);
44 T_adj.emplace_back(v1, v2,
true);
49 adj.resize(n_verts, n_verts);
50 adj.setFromTriplets(T_adj.begin(), T_adj.end());
52 std::vector<int> degrees(n_verts, 0);
53 for (
int k = 0; k <
adj.outerSize(); ++k)
54 for (Eigen::SparseMatrix<bool, Eigen::RowMajor>::InnerIterator it(
adj, k); it; ++it)
58 L.resize(n_verts, n_verts);
61 std::vector<Eigen::Triplet<double>> T_L;
62 for (
int k = 0; k <
adj.outerSize(); ++k)
66 T_L.emplace_back(k, k, 1);
67 for (Eigen::SparseMatrix<bool, Eigen::RowMajor>::InnerIterator it(
adj, k); it; ++it)
69 assert(it.row() == k);
70 T_L.emplace_back(it.row(), it.col(), -1. / degrees[k]);
73 L.setFromTriplets(T_L.begin(), T_L.end());
74 L.prune([](
int i,
int j,
double val) {
return abs(
val) > 1e-12; });
122 const int dim = mesh.dimension();
123 const int n_verts = mesh.n_vertices();
125 Eigen::VectorXd grad;
128 grad.setZero(n_verts * dim);
129 for (
int b = 0; b <
adj.rows(); b++)
134 auto sum_normalized = s;
136 for (Eigen::SparseMatrix<bool, Eigen::RowMajor>::InnerIterator it(
adj, b); it; ++it)
138 assert(it.col() != b);
139 auto x = mesh.point(b) - mesh.point(it.col());
141 sum_norm +=
x.norm();
142 sum_normalized +=
x.normalized();
148 const double coeff =
power_ * pow(s.norm(),
power_ - 2.) / sum_norm;
150 grad.segment(b * dim, dim) += (s * valence - s.squaredNorm() * sum_normalized) * coeff;
151 for (Eigen::SparseMatrix<bool, Eigen::RowMajor>::InnerIterator it(
adj, b); it; ++it)
152 grad.segment(it.col() * dim, dim) -= (s + s.squaredNorm() * (mesh.point(it.col()) - mesh.point(b)).normalized()) * coeff;