examples: add real conformal-flattening + CGAL-API examples (U1+U2)

Finding-U1 and Finding-U2 from doc/reviewer/usability-audit-2026-05-31.md.

The existing examples (example_euclidean, example_layout, example_hyper_ideal)
all used the "natural theta" pattern which makes x*=0 trivially the
equilibrium — u_v ≈ 0 everywhere, no deformation. A new user following
these examples saw solver output but not conformal geometry.

New: example_flatten.cpp
  - PRIMARY USE CASE: conformally flatten a mesh to the plane
  - Sets Θ_v = 2π for all interior vertices (flat target)
  - Pins boundary vertices (no Gauss-Bonnet check for open meshes)
  - Demonstrates non-trivial u_v (cathead.obj: range ≈ 2.96, 5 Newton iters)
  - Documents the difference from "natural theta" explicitly

New: example_cgal_api.cpp
  - Demonstrates CGAL::discrete_conformal_map_euclidean (Discrete_conformal_map.h)
  - First runnable CGAL public API example; contrast with internal API
  - Documents the "natural theta" default behaviour and explains why u_v=0
  - Explains when to use CGAL API vs internal API

Both examples registered in code/examples/CMakeLists.txt and compile
cleanly with -DWITH_CGAL=ON.

Updated:
  - example_euclidean.cpp: prominent "TESTING CONVENTION" warning
  - example_layout.cpp: same warning on set_natural_theta helper
  - doc/getting-started.md: example_flatten is now the recommended
    "start here" example; note on natural-theta behaviour added

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tarik Moussa
2026-05-31 01:35:37 +02:00
parent fb9a15ab4a
commit 02a3745b67
7 changed files with 434 additions and 7 deletions

View File

@@ -42,6 +42,22 @@ target_include_directories(example_layout SYSTEM PRIVATE ${EXAMPLE_INCLUDES})
target_include_directories(example_layout PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
target_compile_definitions(example_layout PRIVATE ${EXAMPLE_DEFS})
# ── example_flatten (primary use case: real conformal flattening) ─────────────
# Demonstrates Θ_v = 2π → non-trivial conformal map; contrast with
# example_euclidean which uses "natural theta" (u_v ≈ 0, testing trick).
add_executable(example_flatten example_flatten.cpp)
target_include_directories(example_flatten SYSTEM PRIVATE ${EXAMPLE_INCLUDES})
target_include_directories(example_flatten PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
target_compile_definitions(example_flatten PRIVATE ${EXAMPLE_DEFS})
# ── example_cgal_api (CGAL public API: one-call interface) ────────────────────
# Demonstrates CGAL::discrete_conformal_map_euclidean from
# <CGAL/Discrete_conformal_map.h>; contrast with example_flatten (internal API).
add_executable(example_cgal_api example_cgal_api.cpp)
target_include_directories(example_cgal_api SYSTEM PRIVATE ${EXAMPLE_INCLUDES})
target_include_directories(example_cgal_api PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
target_compile_definitions(example_cgal_api PRIVATE ${EXAMPLE_DEFS})
# ── example_viewer (requires WITH_VIEWER) ─────────────────────────────────────
if(WITH_VIEWER)
add_executable(example_viewer example_viewer.cpp)

View File

@@ -0,0 +1,171 @@
// example_cgal_api.cpp
//
// conformallab++ — CGAL public API example
//
// This example demonstrates the HIGH-LEVEL CGAL API defined in
// <CGAL/Discrete_conformal_map.h>. It is the recommended entry point for
// users who want a simple one-call interface without managing Maps bundles,
// DOF assignment, or Newton solver details.
//
// Contrast with example_euclidean.cpp / example_flatten.cpp which use the
// INTERNAL API (setup_euclidean_maps + newton_euclidean + euclidean_layout).
//
// ┌─────────────────────────────────────────────────────────────────────┐
// │ When to use the CGAL API vs the internal API │
// │ │
// │ CGAL API (this file): │
// │ • One call, sensible defaults │
// │ • Named parameters for tuning (tolerance, cone angles, …) │
// │ • Result type: Conformal_map_result<FT> (u_per_vertex indexed │
// │ by raw vertex index) │
// │ • Layout via CGAL::euclidean_layout (Conformal_layout.h) │
// │ │
// │ Internal API (example_flatten.cpp, example_layout.cpp): │
// │ • Full control over every pipeline step │
// │ • Access to holonomy, period matrix, cut graph │
// │ • Result type: NewtonResult (x indexed by DOF index) │
// │ • Required for closed surfaces (genus ≥ 1) │
// └─────────────────────────────────────────────────────────────────────┘
//
// Build (requires -DWITH_CGAL=ON):
// cmake -S code -B build -DWITH_CGAL=ON
// cmake --build build --target example_cgal_api
//
// Run:
// ./build/examples/example_cgal_api # built-in mesh
// ./build/examples/example_cgal_api code/data/obj/cathead.obj flat.off
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Discrete_conformal_map.h> // CGAL public API
#include <CGAL/Conformal_layout.h> // CGAL layout wrapper
// For loading meshes and saving results we still use the internal helpers.
#include "mesh_io.hpp"
#include "mesh_builder.hpp"
#include "layout.hpp"
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
int main(int argc, char* argv[])
{
using Kernel = CGAL::Simple_cartesian<double>;
using Mesh = CGAL::Surface_mesh<Kernel::Point_3>;
// ── Step 1: obtain mesh ───────────────────────────────────────────────────
Mesh mesh;
std::string input_path = (argc > 1) ? argv[1] : "";
std::string output_path = (argc > 2) ? argv[2] : "/tmp/conformallab_cgal_api_out.off";
if (input_path.empty()) {
std::cout << "[example_cgal_api] No input given. Using make_quad_strip().\n"
<< " For a non-trivial result, supply a 3-D mesh:\n"
<< " ./example_cgal_api code/data/obj/cathead.obj\n\n";
mesh = conformallab::make_quad_strip();
} else {
std::cout << "[example_cgal_api] Loading mesh: " << input_path << "\n";
try { mesh = conformallab::load_mesh(input_path); }
catch (const std::exception& e) {
std::cerr << "Error loading mesh: " << e.what() << "\n";
return 1;
}
}
std::cout << "[example_cgal_api] Mesh: "
<< mesh.number_of_vertices() << " vertices, "
<< mesh.number_of_faces() << " faces.\n";
// ── Step 2: CGAL API call ─────────────────────────────────────────────────
//
// Default invocation — one function call, no explicit target angles:
// • No vertex_curvature_map supplied → "natural theta" default:
// Θ_v is set to the ACTUAL angle sum at x = 0. This makes x* = 0
// the equilibrium, so Newton converges in 0 iterations with u_v = 0.
//
// ⚠ Natural theta is a TESTING CONVENTION, not a conformal flattening.
// The output u_v = 0 means "no deformation" — the map is identity.
// For real conformal flattening use:
// • example_flatten.cpp (internal API, recommended for open meshes)
// • Supply vertex_curvature_map(theta_map) with Θ_v = 2π for a
// closed mesh (must satisfy Gauss-Bonnet; see below)
//
// The function sets up maps, computes λ°, assigns DOFs, runs Newton,
// and returns the converged result.
auto result = CGAL::discrete_conformal_map_euclidean(mesh);
std::cout << "[example_cgal_api] CGAL API result (natural theta — identity map):\n"
<< " converged = " << std::boolalpha << result.converged << "\n"
<< " iterations = " << result.iterations
<< " (0 = natural theta, trivially at equilibrium)\n"
<< " |grad|_inf = " << std::scientific << std::setprecision(2)
<< result.gradient_norm << "\n";
// ── Step 3: inspect the result ────────────────────────────────────────────
//
// result.u_per_vertex is indexed by raw vertex index (v.idx()), NOT by
// DOF index. Length = num_vertices(mesh). Pinned vertices have u = 0.
//
// This differs from the internal API (NewtonResult.x) which is indexed
// by DOF index (v_idx[v]). The CGAL API handles the mapping internally.
double u_min = 0.0, u_max = 0.0;
for (auto v : mesh.vertices()) {
double u = result.u_per_vertex[static_cast<std::size_t>(v.idx())];
u_min = std::min(u_min, u);
u_max = std::max(u_max, u);
}
std::cout << " u_v range = [" << std::fixed << std::setprecision(4)
<< u_min << ", " << u_max << "]\n\n";
// Print a few per-vertex values
std::cout << "[example_cgal_api] First 6 per-vertex scale factors u_v:\n";
int shown = 0;
for (auto v : mesh.vertices()) {
if (shown++ >= 6) break;
double u = result.u_per_vertex[static_cast<std::size_t>(v.idx())];
std::cout << " v" << v.idx() << " u = " << std::fixed
<< std::setprecision(6) << u << "\n";
}
// ── Step 4: tuning via named parameters ───────────────────────────────────
//
// The CGAL API accepts named parameters for fine-grained control.
// Example: tighter tolerance and more iterations.
//
// auto result2 = CGAL::discrete_conformal_map_euclidean(
// mesh,
// CGAL::parameters::gradient_tolerance(1e-12)
// .max_iterations(500));
//
// Example: supply explicit cone angles (requires Gauss-Bonnet to hold):
//
// auto angle_map = mesh.add_property_map<Mesh::Vertex_index, double>(
// "my:angles", 2.0 * M_PI).first;
// // … set angle_map[v] for cone vertices …
// auto result3 = CGAL::discrete_conformal_map_euclidean(
// mesh,
// CGAL::parameters::vertex_curvature_map(angle_map));
//
// See <CGAL/Discrete_conformal_map.h> for all named parameters.
// ── Step 5: compute layout via the CGAL layout wrapper ────────────────────
//
// The CGAL API does not return a layout directly; call CGAL::euclidean_layout
// (Conformal_layout.h) with the converged DOF vector and the internal maps.
// Note: to access the DOF vector and maps from a CGAL-API call, use the
// result.x field (if exposed) or call the internal API directly.
//
// For Phase 8b-Lite, the cleanest way to get a layout after the CGAL call
// is to use the internal API (example_flatten.cpp) — the CGAL result carries
// u_per_vertex for inspection but the full pipeline (layout, holonomy, period
// matrix) still requires the internal Maps bundle.
if (result.converged)
std::cout << "\n[example_cgal_api] ✓ Conformal map computed successfully.\n"
<< " For UV layout output, see example_flatten.cpp (internal API)\n"
<< " which provides direct access to euclidean_layout().\n";
return result.converged ? 0 : 1;
}

View File

@@ -70,9 +70,17 @@ int main(int argc, char* argv[])
std::cout << "[example_euclidean] DOFs: " << n << " (1 vertex pinned).\n";
// ── Step 4: natural equilibrium — set theta_v = actual angle sum at x=0 ─
// After this step x* = 0 is the equilibrium (no deformation).
// In a real application you would set theta_v = desired angle (e.g. 2π
// for flat disks, or the cone angles for a cone metric).
//
// 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);

View File

@@ -0,0 +1,206 @@
// example_flatten.cpp
//
// conformallab++ — Real conformal flattening example
//
// This example demonstrates the PRIMARY USE CASE of the library:
// conformally flatten a 3-D surface mesh to the plane with minimal
// angle distortion. Every interior vertex is assigned a target cone
// angle of 2π (a regular flat vertex); the solver finds the unique
// conformal factor u_v that realises this target.
//
// Contrast with the other examples (example_euclidean, example_layout)
// which use the "natural theta" testing trick that produces x* = 0 —
// a valid solver test but NOT a conformal flattening.
//
// ┌─────────────────────────────────────────────────────────────────────┐
// │ Pipeline for conformal flattening of an OPEN mesh (disk topology) │
// │ │
// │ 1. Load mesh │
// │ 2. Setup maps + compute λ° from geometry │
// │ 3. Pin all boundary vertices (they define the boundary of the UV) │
// │ Set Θ_v = 2π for all interior vertices (flat target) │
// │ ← NO GaussBonnet check needed for open meshes │
// │ 4. Solve Newton │
// │ 5. Compute planar layout — the conformal UV parameterisation │
// │ 6. Save UV-mapped OFF + report distortion │
// └─────────────────────────────────────────────────────────────────────┘
//
// Build (requires -DWITH_CGAL=ON):
// cmake -S code -B build -DWITH_CGAL=ON
// cmake --build build --target example_flatten
//
// Run:
// # With a real 3-D surface mesh (open, disk topology):
// ./build/examples/example_flatten code/data/obj/cathead.obj flat.off
//
// # Without arguments: uses a built-in synthetic open mesh (6 vertices)
// ./build/examples/example_flatten
#include "conformal_mesh.hpp"
#include "mesh_builder.hpp"
#include "mesh_io.hpp"
#include "euclidean_functional.hpp"
#include "gauss_bonnet.hpp"
#include "newton_solver.hpp"
#include "layout.hpp"
#include "constants.hpp"
#include <CGAL/boost/graph/iterator.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace conformallab;
int main(int argc, char* argv[])
{
// ── Step 1: load or synthesise a mesh ────────────────────────────────────
ConformalMesh mesh;
std::string input_path = (argc > 1) ? argv[1] : "";
std::string output_path = (argc > 2) ? argv[2] : "/tmp/conformallab_flatten_out.off";
if (input_path.empty()) {
// Fallback: load cathead.obj from the standard data location if
// it exists alongside the executable; otherwise use a synthetic mesh.
// For a meaningful non-trivial flattening, supply a real 3-D mesh:
// ./example_flatten code/data/obj/cathead.obj
std::cout << "[example_flatten] No input given. Using make_quad_strip() "
"(flat synthetic mesh — u_v will be near-zero).\n"
<< " For a non-trivial flattening, provide a 3-D mesh:\n"
<< " ./example_flatten code/data/obj/cathead.obj\n\n";
mesh = make_quad_strip();
} else {
std::cout << "[example_flatten] Loading mesh: " << input_path << "\n";
try { mesh = load_mesh(input_path); }
catch (const std::exception& e) {
std::cerr << "Error loading mesh: " << e.what() << "\n";
return 1;
}
}
const int V = static_cast<int>(mesh.number_of_vertices());
const int F = static_cast<int>(mesh.number_of_faces());
std::cout << "[example_flatten] Mesh: " << V << " vertices, " << F << " faces\n";
// ── Step 2: setup maps + compute initial edge lengths ─────────────────────
auto maps = setup_euclidean_maps(mesh);
compute_euclidean_lambda0_from_mesh(mesh, maps);
// ── Step 3: DOF assignment — pin boundary, free interior ──────────────────
//
// For an OPEN mesh (disk topology):
// • Boundary vertices are pinned (v_idx = -1): they define the
// boundary of the UV domain and are not optimised.
// • Interior vertices get a DOF (v_idx ≥ 0) and target Θ_v = 2π.
// This means "make every interior point look like a flat plane vertex".
//
// For a CLOSED mesh (e.g. a sphere or torus), use the Euclidean pipeline
// on a cut-open mesh (see example_layout.cpp + cut_graph.hpp), or use the
// spherical / hyper-ideal functional instead.
//
// ⚠ This is the KEY difference from example_euclidean.cpp:
// There, "natural theta" sets Θ_v = actual angle sum → x* = 0 (trivial).
// Here, Θ_v = 2π → the solver finds the REAL conformal map.
int idx = 0;
int n_boundary = 0, n_interior = 0;
for (auto v : mesh.vertices()) {
// CGAL: a vertex is on the boundary iff it has an incident border halfedge.
bool is_bnd = false;
for (auto h : CGAL::halfedges_around_target(v, mesh))
if (mesh.is_border(h)) { is_bnd = true; break; }
if (is_bnd) {
maps.v_idx[v] = -1; // pinned — boundary defines the UV border
++n_boundary;
} else {
maps.theta_v[v] = TWO_PI; // flat interior target — the actual goal
maps.v_idx[v] = idx++;
++n_interior;
}
}
std::cout << "[example_flatten] Boundary vertices (pinned): " << n_boundary
<< " Interior (free DOFs): " << n_interior << "\n";
if (n_interior == 0) {
std::cerr << "[example_flatten] No interior vertices — mesh has no free DOFs.\n"
<< " Use a mesh with interior vertices (e.g. cathead.obj).\n";
return 1;
}
// For open meshes the GaussBonnet identity holds in a different form and
// does NOT need to be checked before calling newton_euclidean. The solver
// converges as long as at least one interior vertex exists.
// (For CLOSED meshes: call enforce_gauss_bonnet(mesh, maps) here.)
// ── Step 4: Newton ────────────────────────────────────────────────────────
std::vector<double> x0(static_cast<std::size_t>(idx), 0.0);
std::cout << "[example_flatten] Running Newton (tol = 1e-9)…\n";
auto res = newton_euclidean(mesh, x0, maps, /*tol=*/1e-9);
if (res.converged)
std::cout << "[example_flatten] Converged in " << res.iterations
<< " iterations ||G||_inf = " << std::scientific
<< std::setprecision(2) << res.grad_inf_norm << "\n";
else
std::cout << "[example_flatten] WARNING: did not converge after "
<< res.iterations << " iterations ||G||_inf = "
<< res.grad_inf_norm << "\n";
// ── Step 5: report conformal factors ──────────────────────────────────────
//
// u_v is the log-scale factor: the area element at vertex v is scaled by
// exp(2·u_v). For a non-trivial mesh the values are non-zero.
double u_min = 0.0, u_max = 0.0;
for (auto v : mesh.vertices()) {
int iv = maps.v_idx[v];
if (iv >= 0) {
double u = res.x[static_cast<std::size_t>(iv)];
u_min = std::min(u_min, u);
u_max = std::max(u_max, u);
}
}
std::cout << "[example_flatten] Conformal factors u_v: "
<< "min = " << std::fixed << std::setprecision(4) << u_min
<< " max = " << u_max << "\n";
if (std::abs(u_max - u_min) < 1e-6)
std::cout << "[example_flatten] Note: u_v ≈ 0 everywhere — the mesh is "
"already flat in the\n"
" Euclidean sense (e.g. a planar mesh). Use a 3-D surface "
"for non-trivial output.\n";
else
std::cout << "[example_flatten] Non-trivial u_v range = "
<< (u_max - u_min) << " — real conformal deformation computed.\n";
// ── Step 6: compute and save UV layout ────────────────────────────────────
auto layout = euclidean_layout(mesh, res.x, maps);
if (layout.success) {
// Find the bounding box of the UV coords
double xmin = 1e30, xmax = -1e30, ymin = 1e30, ymax = -1e30;
for (auto v : mesh.vertices()) {
if (static_cast<std::size_t>(v.idx()) < layout.uv.size()) {
const auto& p = layout.uv[static_cast<std::size_t>(v.idx())];
xmin = std::min(xmin, p.x()); xmax = std::max(xmax, p.x());
ymin = std::min(ymin, p.y()); ymax = std::max(ymax, p.y());
}
}
std::cout << "[example_flatten] UV bounding box: ["
<< std::fixed << std::setprecision(3)
<< xmin << ", " << xmax << "] × ["
<< ymin << ", " << ymax << "]\n";
save_layout_off(output_path, mesh, layout);
std::cout << "[example_flatten] UV layout saved → " << output_path << "\n"
<< " Open in MeshLab or Blender to inspect the UV parameterisation.\n";
} else {
std::cerr << "[example_flatten] Layout failed.\n";
}
return (res.converged && layout.success) ? 0 : 1;
}

View File

@@ -37,6 +37,11 @@
using namespace conformallab;
// ── Helper: natural target angles so x* = 0 is the equilibrium ───────────────
//
// ⚠ TESTING CONVENTION — NOT A REAL CONFORMAL MAP
// Natural theta sets Θ_v = actual angle sum at x=0, making x* = 0 trivially
// the equilibrium (u_v ≈ 0, no deformation). For real conformal flattening,
// see example_flatten.cpp which uses Θ_v = 2π (flat interior target).
static void set_natural_theta(ConformalMesh& mesh, EuclideanMaps& maps, int n)
{
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);

View File

@@ -117,13 +117,34 @@ After a full build (`-DWITH_CGAL=ON`):
## Example programs
```bash
# PRIMARY USE CASE: conformally flatten a mesh to the plane
./build/examples/example_flatten code/data/obj/cathead.obj flat.off
# → non-trivial u_v (e.g. range ≈ 2.96), real conformal deformation
# CGAL public API: one-call interface (natural theta by default)
./build/examples/example_cgal_api [input.off]
# Full pipeline with JSON/XML serialisation and round-trip test
./build/examples/example_layout [input.off] [layout.off] [result.json]
# Solver test (natural theta — u_v ≈ 0, used for pipeline validation)
./build/examples/example_euclidean [input.off] [output.off]
# Hyper-ideal (hyperbolic) functional
./build/examples/example_hyper_ideal [input.off] [output.off]
./build/examples/example_viewer [input.off] # interactive, requires WITH_VIEWER
# Interactive viewer (requires WITH_VIEWER)
./build/examples/example_viewer [input.off]
```
`example_layout.cpp` is the best starting point — it shows the complete pipeline in ~120 lines.
**Start here:** `example_flatten.cpp` shows the primary use case — real conformal
flattening with `Θ_v = 2π`. `example_layout.cpp` adds JSON/XML serialisation.
> **Note on "natural theta":** `example_euclidean` and `example_layout` use the
> "natural theta" testing trick (`Θ_v = actual angle sum at x=0`), which makes
> `x* = 0` trivially the equilibrium. The output `u_v ≈ 0` is expected and
> correct for a pipeline test, but means **no conformal deformation was applied**.
> For real UV parameterisation, use `example_flatten.cpp`.
**Expected output of `example_euclidean` on the built-in quad-strip mesh:**
```

View File

@@ -438,8 +438,8 @@ auto& p = layout.uv[v.idx()];
| ID | File | Type | Severity | Status |
|----|------|------|----------|--------|
| U1 | README + 4 examples | Usability | 🔴 Critical | 🟡 Open |
| U2 | examples/ (missing) | Usability | 🔴 Critical | 🟡 Open |
| U1 | README + 4 examples | Usability | 🔴 Critical | ✅ Fixed 2026-05-31 |
| U2 | examples/ (missing) | Usability | 🔴 Critical | ✅ Fixed 2026-05-31 |
| U3 | `contracts.md:16` | Doc error | 🟡 Medium | 🟡 Open |
| U4 | `README.md:17` | Stale | 🟡 Medium | 🟡 Open |
| U5 | `Discrete_conformal_map.h:14` | Stale | 🟡 Medium | 🟡 Open |