Files
ConformalLabpp/code/examples/example_euclidean.cpp
Tarik Moussa ccbfd852b2 docs(examples): use gauge-vertex overload everywhere (U6)
Finding-U6 from doc/reviewer/usability-audit-2026-05-31.md.

The Finding-D fix (external-audit-2026-05-30) added a clean one-call
gauge-vertex overload:
  assign_euclidean_vertex_dof_indices(mesh, maps, gauge_vertex)

But all user-facing code still showed the old verbose manual loop:
  auto vit = mesh.vertices().begin();
  maps.v_idx[*vit++] = -1;
  int idx = 0;
  for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;

Replaced in three places:
  README.md 'Minimal usage' code block
  example_euclidean.cpp Step 3
  example_layout.cpp  pin_first() helper

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 01:49:05 +02:00

123 lines
5.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// example_euclidean.cpp
//
// conformallab++ — Euclidean discrete conformal map (headless example)
//
// This program demonstrates the full library pipeline for the EUCLIDEAN
// discrete conformal functional:
//
// 1. Load a triangle mesh from an OFF file
// 2. Set up the Euclidean functional maps
// 3. Pin one vertex (gauge fix for open surfaces)
// 4. Set target angles via "natural equilibrium" (x* = x_input)
// 5. Solve with Newton + backtracking line search
// 6. Print per-vertex conformal factors u_i = x[v_idx[v]]
// 7. Save the result mesh (same geometry, solver state printed)
//
// Build (requires -DWITH_CGAL=ON):
// cmake -S code -B build -DWITH_CGAL=ON
// cmake --build build --target example_euclidean
// ./build/examples/example_euclidean [input.off] [output.off]
//
// If no input file is given the built-in make_quad_strip() mesh is used.
#include "conformal_mesh.hpp"
#include "mesh_builder.hpp"
#include "mesh_io.hpp"
#include "euclidean_functional.hpp"
#include "newton_solver.hpp"
#include <iostream>
#include <string>
#include <vector>
using namespace conformallab;
int main(int argc, char* argv[])
{
// ── Step 1: obtain mesh ───────────────────────────────────────────────
ConformalMesh mesh;
std::string input_path = (argc > 1) ? argv[1] : "";
std::string output_path = (argc > 2) ? argv[2] : "/tmp/conformallab_euclidean_out.off";
if (input_path.empty()) {
std::cout << "[example_euclidean] No input file given — using make_quad_strip().\n";
mesh = make_quad_strip();
} else {
std::cout << "[example_euclidean] Loading mesh from: " << input_path << "\n";
try { mesh = load_mesh(input_path); }
catch (const std::exception& e) {
std::cerr << "Error loading mesh: " << e.what() << "\n";
return 1;
}
}
std::cout << "[example_euclidean] Mesh: "
<< mesh.number_of_vertices() << " vertices, "
<< mesh.number_of_faces() << " faces.\n";
// ── Step 2: set up functional maps ────────────────────────────────────
auto maps = setup_euclidean_maps(mesh);
compute_euclidean_lambda0_from_mesh(mesh, maps);
// ── Step 3: pin the first vertex (gauge fix) ──────────────────────────
// The gauge-vertex overload pins the chosen vertex (v_idx = -1) and
// assigns sequential indices 0..n-1 to the rest in a single call.
Vertex_index v_pinned = *mesh.vertices().begin();
const int n = assign_euclidean_vertex_dof_indices(mesh, maps, v_pinned);
std::cout << "[example_euclidean] DOFs: " << n << " (1 vertex pinned).\n";
// ── Step 4: natural equilibrium — set theta_v = actual angle sum at x=0 ─
//
// ⚠ TESTING CONVENTION — NOT A REAL CONFORMAL MAP
//
// "Natural theta" sets Θ_v = actual angle sum at x = 0, so x* = 0 is the
// equilibrium by construction. The solver converges in 01 iterations and
// u_v ≈ 0 everywhere. This is useful for testing the solver pipeline but
// produces NO conformal deformation.
//
// For a REAL conformal flattening (the primary use case):
// → see example_flatten.cpp
// which sets Θ_v = 2π (flat interior target) and produces non-trivial u_v.
{
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
auto G0 = euclidean_gradient(mesh, x0, maps);
for (auto v : mesh.vertices()) {
int iv = maps.v_idx[v];
if (iv >= 0) maps.theta_v[v] -= G0[static_cast<std::size_t>(iv)];
}
}
// ── Step 5: solve from a small perturbation to demonstrate Newton ─────
std::vector<double> x0(static_cast<std::size_t>(n), -0.05);
std::cout << "[example_euclidean] Solving Newton system…\n";
auto result = newton_euclidean(mesh, x0, maps, /*tol=*/1e-9, /*max_iter=*/100);
// ── Step 6: report ────────────────────────────────────────────────────
if (result.converged) {
std::cout << "[example_euclidean] Converged in " << result.iterations
<< " iterations. ||G||_inf = " << result.grad_inf_norm << "\n";
} else {
std::cout << "[example_euclidean] Did NOT converge after " << result.iterations
<< " iterations. ||G||_inf = " << result.grad_inf_norm << "\n";
}
std::cout << "[example_euclidean] Per-vertex conformal factors u_i:\n";
for (auto v : mesh.vertices()) {
int iv = maps.v_idx[v];
double u = (iv >= 0) ? result.x[static_cast<std::size_t>(iv)] : 0.0;
std::cout << " v" << v << " u = " << u;
if (iv < 0) std::cout << " (pinned)";
std::cout << "\n";
}
// ── Step 7: write output mesh ─────────────────────────────────────────
try {
save_mesh(output_path, mesh);
std::cout << "[example_euclidean] Mesh saved to: " << output_path << "\n";
} catch (const std::exception& e) {
std::cerr << "Warning: could not write output: " << e.what() << "\n";
}
return result.converged ? 0 : 1;
}