55 const M& omega,
double radius,
const std::vector<V>& path,
56 const std::vector<PotentialEvaluation>& native,
double stiffness,
57 const std::vector<M>& hessians,
const std::vector<double>& meshWeights)
59 const auto n = path.size();
60 require(n >= 3 && native.size() == n &&
61 (hessians.empty() || hessians.size() == n),
62 "Matching geodesic nodes and native values required");
63 require(meshWeights.empty() || meshWeights.size() + 1 == n,
64 "One graded mesh weight per geodesic link required");
65 for (
double weight: meshWeights)
66 require(weight > 0 && std::isfinite(weight),
67 "Positive finite geodesic mesh weights required");
68 const auto d = path.front().size();
69 const M identity = M::Identity(d, d);
70 require(d > 0 && omega.rows() == d && omega.cols() == d &&
72 (omega - omega.transpose()).norm() <=
73 1e-12 * std::max(1., omega.norm()) &&
74 radius > 0 && std::isfinite(radius) && stiffness > 0 &&
75 std::isfinite(stiffness),
76 "Positive geodesic well, radius and mesh scale required");
77 Eigen::SelfAdjointEigenSolver<M> spectrum(omega);
78 require(spectrum.info() == Eigen::Success &&
79 spectrum.eigenvalues().minCoeff() > 0,
80 "Positive harmonic well root required");
81 for (std::size_t j = 0; j < n; ++j) {
82 require(path[j].size() == d && path[j].allFinite() &&
83 native[j].gradient.size() == d &&
84 native[j].gradient.allFinite() &&
85 std::isfinite(native[j].energy),
86 "Invalid native geodesic point");
87 if (j + 1 < n && !(native[j].energy > 0))
88 throw std::domain_error(
89 "Interior geodesic nodes must remain in the forbidden region");
90 if (!hessians.empty())
91 require(hessians[j].rows() == d && hessians[j].cols() == d &&
92 hessians[j].allFinite() &&
93 (hessians[j] - hessians[j].transpose()).norm() <
94 1e-12 * std::max(1., hessians[j].norm()),
95 "Invalid geodesic search curvature");
97 std::vector<double> lengths;
99 for (std::size_t j = 0; j + 1 < n; ++j) {
100 const V delta = path[j + 1] - path[j];
101 const double length = delta.norm();
102 require(length > 0 && std::isfinite(length),
103 "Distinct finite geodesic nodes required");
104 lengths.push_back(length);
105 unit.push_back(delta / length);
108 out.residual.assign(n, V::Zero(d));
109 if (!hessians.empty()) {
110 out.lower.assign(n, M::Zero(d, d));
114 const double r = path.front().norm();
115 require(r > 0 && std::isfinite(r),
116 "Nonzero harmonic matching cut required");
117 const V radial = path.front() / r, t0 = unit.front();
118 const M pr = identity - radial * radial.transpose();
119 const double p = std::sqrt(2 * native.front().energy),
120 frequency = omega.trace() / d;
121 const V momentum = omega * path.front() - p * t0;
122 const V angular = pr * momentum;
123 out.residual.front() = angular + frequency * (r - radius) * radial;
124 out.momentumResidual = angular.norm();
125 out.radiusResidual = std::abs(r - radius);
126 out.tailPointsOutward = (omega * path.front()).dot(t0) > 0;
127 if (!hessians.empty())
128 for (
int side = 0; side < 2; ++side) {
129 const M dq = M((side == 0 ? 1. : 0.) * identity);
130 const M dr = pr * dq / r, dt = (identity - t0 * t0.transpose()) /
132 ((side == 0 ? -1. : 1.) * identity);
133 const M dm = omega * dq -
134 t0 * native.front().gradient.transpose() * dq / p -
137 (momentum.dot(radial) * identity +
138 radial * momentum.transpose()) *
140 frequency * (radial * radial.transpose() * dq +
143 out.diagonal.front() = block;
145 out.upper.front() = block;
147 for (std::size_t j = 1; j + 1 < n; ++j) {
148 const double a = lengths[j - 1], b = lengths[j], sum = a + b;
149 const V u = unit[j - 1], v = unit[j], both = u + v;
150 const double norm = both.norm();
151 require(norm > 1e-12,
152 "Geodesic links cannot reverse at an interior node");
153 V t = both / norm, kappa = 2 * (v - u) / sum;
156 if (!meshWeights.empty()) {
157 graded = meshGeometry(path[j - 1], path[j], path[j + 1],
158 meshWeights[j - 1], meshWeights[j]);
160 kappa = graded.curvature;
161 mesh = 2 * (graded.right * a - graded.left * b) /
162 (graded.left + graded.right);
164 const V g = native[j].gradient;
165 const M projection = identity - t * t.transpose();
166 const V force = projection * g - 2 * native[j].energy * kappa;
167 out.forceResidual = std::max(
out.forceResidual, force.norm());
168 out.meshResidual = std::max(
out.meshResidual, std::abs(mesh));
169 out.residual[j] = force + stiffness * mesh * t;
170 if (!hessians.empty())
171 for (
int side = -1; side <= 1; ++side) {
172 const M prev = M((side == -1 ? 1. : 0.) * identity),
173 here = M((side == 0 ? 1. : 0.) * identity),
174 next = M((side == 1 ? 1. : 0.) * identity);
175 const M du = (identity - u * u.transpose()) / a * (here - prev),
176 dv = (identity - v * v.transpose()) / b * (next - here);
177 M dt = projection * (du + dv) / norm;
178 const Eigen::RowVectorXd dl = u.transpose() * (here - prev) +
179 v.transpose() * (next - here);
180 Eigen::RowVectorXd dc = u.transpose() * (here - prev) -
181 v.transpose() * (next - here);
182 M dk = 2 * (dv - du) / sum - kappa * dl / sum;
183 if (!meshWeights.empty()) {
184 const double hm = graded.left, hp = graded.right,
185 speed = graded.speed;
187 (hp / hm * (here - prev) + hm / hp * (next - here)) /
189 const M dz = 2 * ((next - here) / hp - (here - prev) / hm) /
192 ((path[j + 1] - path[j]) / hp -
193 (path[j] - path[j - 1]) / hm) /
195 dt = projection * dw / speed;
196 dk = (projection * dz -
197 (second.dot(t) * identity + t * second.transpose()) *
200 2 * kappa * (t.transpose() * dw) / speed;
202 (hp * u.transpose() * (here - prev) -
203 hm * v.transpose() * (next - here)) /
206 const M block = projection * hessians[j] * here -
207 (g.dot(t) * identity + t * g.transpose()) * dt -
208 2 * kappa * g.transpose() * here -
209 2 * native[j].energy * dk +
210 stiffness * (t * dc + mesh * dt);
212 out.lower[j] = block;
214 out.diagonal[j] = block;
216 out.upper[j] = block;
219 const auto last = n - 1;
220 const V t = unit.back(), g = native.back().gradient;
221 const M projection = identity - t * t.transpose();
222 const double length = lengths.back(), energy = native.back().energy;
223 const V transverse = projection * g;
224 out.residual.back() = transverse + (energy / length) * t;
225 out.forceResidual = std::max(
out.forceResidual, transverse.norm());
226 out.turningEnergyResidual = std::abs(energy);
227 out.turningPointsInward = g.dot(t) < 0;
228 if (!hessians.empty())
229 for (
int side = 0; side < 2; ++side) {
230 const M here = M((side == 1 ? 1. : 0.) * identity);
231 const M delta = M((side == 1 ? 1. : -1.) * identity),
232 dt = projection * delta / length;
233 const Eigen::RowVectorXd dl = t.transpose() * delta;
235 projection * hessians[last] * here +
236 ((-g.dot(t) + energy / length) * identity - t * g.transpose()) *
238 t * (g.transpose() * here / length -
239 energy * dl / (length * length));
241 out.lower.back() = block;
243 out.diagonal.back() = block;
245 for (
const auto& value:
out.residual)
246 require(value.allFinite(),
"Nonfinite geodesic residual");
299 const M& omega,
double radius,
const std::vector<V>& initial,
305 "Native geodesic value and search-curvature callbacks required");
310 require(value > 0 && std::isfinite(value),
311 "Positive finite geodesic search scales required");
313 "Geodesic line search needs at least one trial");
314 std::size_t evaluations = 0, batches = 0, hessians = 0, proposals = 0,
315 rejectedProposals = 0;
316 auto measure = [&](
const std::vector<V>& q,
bool* observed =
nullptr) {
320 evaluations += q.size();
322 if (observed) *observed =
true;
327 result.
converged = e.tailPointsOutward && e.turningPointsInward &&
340 require(initial.size() >= 3 && initial.front().size() > 0 &&
341 initial.front().allFinite() && radius > 0 &&
342 std::isfinite(radius) && initial.front().norm() > 0,
343 "A finite nonzero initial matching cut is required");
344 auto feasibleInitial = initial;
345 const double initialRadius = feasibleInitial.front().norm();
347 std::abs(initialRadius - radius) <= options.
maximumStep * (1 + 1e-12),
348 "Initial matching-radius restoration exceeds the native displacement "
350 feasibleInitial.front() *= radius / initialRadius;
351 auto state = measure(feasibleInitial);
352 if (retain) retain(state);
355 for (std::size_t iteration = 0;
362 bool includeTurningEnergy =
true) {
363 const auto& q = point.
path;
368 for (std::size_t j = 1; j + 1 < q.size(); ++j) {
369 const V left = (q[j] - q[j - 1]).normalized(),
370 right = (q[j + 1] - q[j]).normalized();
372 ? V((left + right).normalized())
373 : meshGeometry(q[j - 1], q[j], q[j + 1],
377 const double along = e.residual[j].dot(tangent);
379 std::hypot(norm, (e.residual[j] - along * tangent).norm() /
384 const V chord = q.back() - q[q.size() - 2],
385 tangent = chord.normalized();
386 const double along = e.residual.back().dot(tangent);
388 std::hypot(norm, (e.residual.back() - along * tangent).norm() /
390 return includeTurningEnergy
391 ? std::hypot(norm, along * chord.norm() /
395 const double initialFrameNorm = frameNorm(state);
396 const V initialMesh =
399 auto attempt = [&](
const std::vector<V>& step,
400 std::size_t maximumBacktracks,
const V& meshChange,
401 bool returnAfterObservation =
false) {
403 for (
const auto& value: step)
404 maximum = std::max(maximum, value.norm());
406 maximum > trialStepLimit ? trialStepLimit / maximum : 1.;
407 for (std::size_t trial = 0; trial < maximumBacktracks;
408 ++trial, scale *= .5) {
410 for (std::size_t j = 0; j < q.size(); ++j)
411 q[j] += scale * step[j];
412 const double rawRadius = q.front().norm();
413 if (!(rawRadius > 0) || !std::isfinite(rawRadius))
continue;
414 q.front() *= radius / rawRadius;
418 initialMesh + scale * meshChange);
419 }
catch (
const std::domain_error&) {
422 double displacement = 0;
423 for (std::size_t j = 0; j < q.size(); ++j)
425 std::max(displacement, (q[j] - state.path[j]).norm());
426 if (displacement > options.
maximumStep * (1 + 1e-12))
continue;
427 bool observed =
false;
429 auto next = measure(q, &observed);
431 next.evaluation.turningEnergyResidual >
433 frameNorm(next,
false) < initialFrameNorm) {
434 std::vector<ZeroEnergyGeodesicResult> contours;
436 next.path[next.path.size() - 2], next.path.back(),
438 [&](
const V& point) {
439 if ((point - state.path.back()).norm() >
440 options.maximumStep * (1 + 1e-12))
441 throw std::domain_error(
442 "Native contour restoration exceeds "
443 "the displacement bound");
444 auto restored = next.path;
445 restored.back() = point;
446 contours.push_back(measure(restored));
447 return contours.back().native.back();
449 for (
auto& contour: contours) {
450 const auto& e = contour.evaluation;
451 if ((state.evaluation.tailPointsOutward &&
452 !e.tailPointsOutward) ||
453 (state.evaluation.turningPointsInward &&
454 !e.turningPointsInward))
456 if (frameNorm(contour) < frameNorm(next))
457 next = std::move(contour);
460 if ((state.evaluation.tailPointsOutward &&
461 !next.evaluation.tailPointsOutward) ||
462 (state.evaluation.turningPointsInward &&
463 !next.evaluation.turningPointsInward)) {
464 if (returnAfterObservation)
return false;
467 if (next.converged || frameNorm(next) < initialFrameNorm) {
468 next.acceptedSteps = state.acceptedSteps + 1;
469 state = std::move(next);
470 if (retain) retain(state);
473 if (returnAfterObservation)
return false;
474 }
catch (
const std::domain_error&) {
475 if (returnAfterObservation && observed)
return false;
481 bool accepted =
false;
483 for (std::size_t modelTrial = 0; modelTrial < 2 && !accepted;
485 state.evaluations = evaluations;
486 state.batchEvaluations = batches;
487 state.searchHessianEvaluations = hessians;
488 state.modelProposals = proposals;
489 state.rejectedModelProposals = rejectedProposals;
490 const auto proposed = potential.
propose(state);
491 if (!proposed.empty()) {
492 require(proposed.size() == state.path.size(),
493 "Geodesic model proposal image count differs");
496 for (std::size_t j = 0; j < proposed.size(); ++j) {
497 require(proposed[j].size() == state.path[j].size() &&
498 proposed[j].allFinite(),
499 "Invalid geodesic model proposal metric or "
501 step.push_back(proposed[j] - state.path[j]);
502 maximum = std::max(maximum, step.back().norm());
507 auto endpoint = proposed;
508 const double endpointRadius =
509 endpoint.front().norm();
510 if (!(endpointRadius > 0) ||
511 !std::isfinite(endpointRadius))
512 throw std::domain_error(
513 "Finite model matching cut required");
514 endpoint.front() *= radius / endpointRadius;
521 }
catch (
const std::domain_error&) {
527 .5 * std::min(trialStepLimit, maximum);
537 hessians += state.path.size();
539 omega, radius, state.path, state.native, options.
meshStiffness,
541 const auto d = state.path.front().size();
542 const V radial = state.path.front().normalized();
543 const M identity = M::Identity(d, d),
544 projection = identity - radial * radial.transpose();
548 auto transport = [&](M& block,
const V& tangent,
const V& residual,
550 const double along = residual.dot(tangent);
551 const V transverse = residual - along * tangent;
552 block += tangent * (transverse.transpose() * dt) - along * dt;
554 transport(quadratic.diagonal.front(), radial,
555 quadratic.residual.front(),
556 projection / state.path.front().norm());
557 for (std::size_t j = 1; j + 1 < state.path.size(); ++j) {
558 const V previous = state.path[j] - state.path[j - 1],
559 next = state.path[j + 1] - state.path[j];
560 const double a = previous.norm(), b = next.norm();
561 const V u = previous / a, v = next / b, both = u + v;
562 V tangent = both.normalized();
565 graded = meshGeometry(
566 state.path[j - 1], state.path[j], state.path[j + 1],
568 tangent = graded.tangent;
570 const M normal = identity - tangent * tangent.transpose();
571 for (
int side = -1; side <= 1; ++side) {
572 const double before = side == -1 ? 1. : 0.,
573 here = side == 0 ? 1. : 0.,
574 after = side == 1 ? 1. : 0.;
578 (here - before) / a *
579 (identity - u * u.transpose()) +
580 (after - here) / b * (identity - v * v.transpose());
582 tangent * (tangent.transpose() * derivative)) /
585 const double hm = graded.left, hp = graded.right;
586 const double coefficient = (hp / hm * (here - before) +
587 hm / hp * (after - here)) /
589 dt = (coefficient / graded.speed) * normal;
591 M& block = side == -1 ? quadratic.lower[j]
592 : (side == 0 ? quadratic.diagonal[j]
593 : quadratic.upper[j]);
594 transport(block, tangent, quadratic.residual[j], dt);
598 state.path.back() - state.path[state.path.size() - 2],
599 tangent = chord.normalized();
600 const M turnNormal = identity - tangent * tangent.transpose();
601 transport(quadratic.lower.back(), tangent,
602 quadratic.residual.back(), M(-turnNormal / chord.norm()));
603 transport(quadratic.diagonal.back(), tangent,
604 quadratic.residual.back(), M(turnNormal / chord.norm()));
608 }
catch (
const std::exception& error) {
609 state.failure = error.what();
616 "Native geodesic residual does not decrease along the search "
621 if (!state.converged && state.failure.empty())
622 state.failure =
"Maximum native geodesic iterations reached";
623 state.evaluations = evaluations;
624 state.batchEvaluations = batches;
625 state.searchHessianEvaluations = hessians;
626 state.modelProposals = proposals;
627 state.rejectedModelProposals = rejectedProposals;