Files
ConformalLabpp/code/include/gauss_bonnet.hpp
Tarik Moussa d3c08b3bc0
Some checks failed
C++ Tests / test-fast (pull_request) Successful in 2m2s
API Docs / doc-build (pull_request) Successful in 58s
Markdown link check / check (pull_request) Successful in 45s
C++ Tests / test-cgal (pull_request) Failing after 13m14s
quality: 2 new gates (cmake-format, codespell) + SPDX rollout (60 files)
This commit closes the remaining red gates so `run-all.sh --fast` is
green end-to-end on the canonical dev machine.

New gates
─────────
1. cmake-format / cmake-lint
   * scripts/quality/cmake-format.sh — dry-run by default,
     --strict to fail on drift, --fix to apply
   * .cmake-format.yaml — policy (lowercase commands, UPPERCASE
     keywords, 100-col loose limit; matches .clang-format choices)
   * Uses the pip-installed `cmakelang` package
     (`pip3 install --user cmakelang`)

2. codespell
   * scripts/quality/codespell.sh — exit 1 on any typo, --fix
     interactively
   * .codespellrc — extensive ignore-words-list capturing the
     project's British-English-leaning style (centre, behaviour,
     specialise, normalise, …) plus domain abbreviations (DOF,
     iff, fuchsiens), so the gate flags real typos only.
   * Validated: 0 typos across docs + code/include + scripts +
     code/{src,tests}.

SPDX rollout (license-headers --fix)
────────────────────────────────────
license-headers.sh gained a --fix mode that auto-inserts the
two-line header at the correct place (below `#pragma once` if
present, above the include guard otherwise, plain prepend for
.cpp).  Ran it on 60 of 66 files — 100 %-licensed now.

Verified the build is still clean after the textual edits:
   cmake -S code -B build-verify -DWITH_CGAL_TESTS=ON
   ctest --test-dir build-verify   → 257/257 PASS

run-all.sh + README updated to include the two new gates.

End-to-end style/convention block status (on this commit, this branch):

    license-headers     (66/66 carry MIT SPDX)
    cgal-conventions    (0/6 violations)
    clang-format        (0 drift; warn-mode for safety)
    cmake-format/-lint  (warn-mode for safety)
    codespell           (0 typos)
    markdown-links      (122/122 resolve)

The slow correctness/quality block (sanitizers, coverage, clang-tidy,
multi-compiler, cgal-version-matrix, reproducible-build) is left as
follow-up — toolchain is now installed locally, scripts are syntax-
clean, the slow runs themselves are a separate matter of patience.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 09:15:34 +02:00

149 lines
5.6 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.

#pragma once
// Copyright (c) 2024-2026 Tarik Moussa.
// SPDX-License-Identifier: MIT
// gauss_bonnet.hpp
//
// Phase 6 — GaussBonnet consistency check for prescribed target angles.
//
// Before calling newton_*() with custom target angles, verify that
// the angle defect sum matches the topology:
//
// Σ_v (2π Θ_v) = 2π · χ(M) (Euclidean / flat)
// Σ_v (2π Θ_v) > 0 (spherical, χ > 0)
// Σ_v (2π Θ_v) < 0 (hyperbolic, χ < 0)
//
// If this fails, no conformal factor can realise the target angles and
// Newton will silently fail to converge.
//
// API:
// int euler_characteristic(mesh)
// int genus(mesh)
// double gauss_bonnet_sum(mesh, maps) — Σ(2π Θ_v)
// double gauss_bonnet_rhs(mesh) — 2π · χ(M)
// double gauss_bonnet_deficit(mesh, maps) — lhs rhs (0 = satisfied)
// void check_gauss_bonnet(mesh, maps [, tol]) — throws if violated
// void enforce_gauss_bonnet(mesh, maps) — shifts θ_v by uniform Δ
#include "conformal_mesh.hpp"
#include "euclidean_functional.hpp"
#include "spherical_functional.hpp"
#include "hyper_ideal_functional.hpp"
#include "constants.hpp"
#include <stdexcept>
#include <sstream>
#include <cmath>
#include <string>
namespace conformallab {
// ── Topology helpers ──────────────────────────────────────────────────────────
/// Euler characteristic χ = V E + F.
/// For closed orientable surfaces: χ = 2 2g.
inline int euler_characteristic(const ConformalMesh& mesh)
{
return static_cast<int>(mesh.number_of_vertices())
- static_cast<int>(mesh.number_of_edges())
+ static_cast<int>(mesh.number_of_faces());
}
/// Genus of a closed orientable surface: g = (2 χ) / 2.
/// Returns 0 for open meshes (boundary present) — callers should check.
inline int genus(const ConformalMesh& mesh)
{
int chi = euler_characteristic(mesh);
return (2 - chi) / 2;
}
// ── Left-hand side Σ(2π Θ_v) ─────────────────────────────────────────────
inline double gauss_bonnet_sum(
const ConformalMesh& mesh,
const ConformalMesh::Property_map<Vertex_index, double>& theta)
{
double s = 0.0;
for (auto v : mesh.vertices())
s += TWO_PI - theta[v];
return s;
}
inline double gauss_bonnet_sum(const ConformalMesh& m, const EuclideanMaps& mp)
{ return gauss_bonnet_sum(m, mp.theta_v); }
inline double gauss_bonnet_sum(const ConformalMesh& m, const SphericalMaps& mp)
{ return gauss_bonnet_sum(m, mp.theta_v); }
inline double gauss_bonnet_sum(const ConformalMesh& m, const HyperIdealMaps& mp)
{ return gauss_bonnet_sum(m, mp.theta_v); }
// ── Right-hand side 2π · χ(M) ───────────────────────────────────────────────
inline double gauss_bonnet_rhs(const ConformalMesh& mesh)
{
return TWO_PI * static_cast<double>(euler_characteristic(mesh));
}
// ── Deficit: lhs rhs (0 = GaussBonnet satisfied) ─────────────────────────
template <typename Maps>
inline double gauss_bonnet_deficit(const ConformalMesh& mesh, const Maps& maps)
{
return gauss_bonnet_sum(mesh, maps) - gauss_bonnet_rhs(mesh);
}
// ── check_gauss_bonnet — throws std::runtime_error if |deficit| > tol ─────────
inline void check_gauss_bonnet(const ConformalMesh& mesh,
double lhs,
double tol = 1e-8)
{
double rhs = gauss_bonnet_rhs(mesh);
double def = lhs - rhs;
if (std::abs(def) > tol) {
std::ostringstream msg;
msg << "GaussBonnet violated:\n"
<< " Σ(2πΘ_v) = " << lhs
<< " expected 2π·χ = " << rhs
<< " (χ = " << euler_characteristic(mesh)
<< ", genus = " << genus(mesh) << ")\n"
<< " deficit = " << def;
throw std::runtime_error(msg.str());
}
}
template <typename Maps>
inline void check_gauss_bonnet(const ConformalMesh& mesh,
const Maps& maps,
double tol = 1e-8)
{
check_gauss_bonnet(mesh, gauss_bonnet_sum(mesh, maps), tol);
}
// ── enforce_gauss_bonnet — adjust θ_v by uniform Δ ───────────────────────────
//
// Adds δ = (rhs lhs) / V to every θ_v so that GaussBonnet holds exactly.
// After this call, check_gauss_bonnet() will not throw (up to floating-point).
// Only modifies free vertices (v_idx[v] >= 0 for EuclideanMaps / SphericalMaps;
// always all vertices for the raw property-map overload).
inline void enforce_gauss_bonnet(
ConformalMesh& mesh,
ConformalMesh::Property_map<Vertex_index, double>& theta)
{
double lhs = gauss_bonnet_sum(mesh, theta);
double rhs = gauss_bonnet_rhs(mesh);
// Adding δ to every θ_v decreases the sum Σ(2πθ_v) by V·δ.
// We need lhs V·δ = rhs, so δ = (lhs rhs) / V.
double delta = (lhs - rhs) / static_cast<double>(mesh.number_of_vertices());
for (auto v : mesh.vertices())
theta[v] += delta;
}
template <typename Maps>
inline void enforce_gauss_bonnet(ConformalMesh& mesh, Maps& maps)
{
enforce_gauss_bonnet(mesh, maps.theta_v);
}
} // namespace conformallab