Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5efc3d3cc | ||
|
|
c8e77e715c | ||
|
|
b02f08625c | ||
|
|
d25f3cafe6 | ||
|
|
d0dd1bad3b | ||
|
|
52604d8544 | ||
|
|
de3a35ad4f | ||
|
|
b235666725 | ||
|
|
e28aee7051 | ||
|
|
04b0ae22f2 | ||
|
|
c91230579f | ||
|
|
66d52fc028 | ||
|
|
95d48c434a | ||
|
|
5859d78a37 | ||
|
|
14134b99ce | ||
|
|
279f964b96 | ||
|
|
26405be149 | ||
|
|
9c35c2cb71 | ||
|
|
4e8213f9aa | ||
| 4a036b8214 | |||
| 28d05f5863 | |||
|
|
6886803a29 | ||
|
|
78d13bf561 | ||
|
|
c0e421e5af | ||
|
|
f8686a073c | ||
|
|
a937edcafe | ||
|
|
9c61c9fc40 | ||
|
|
a4438c9a1a | ||
|
|
62e2875c30 | ||
|
|
5333ed143a | ||
|
|
e7dfaed56c | ||
|
|
4fc48b39f0 | ||
|
|
24f7607b2d | ||
|
|
b7593e3f6d | ||
|
|
3f124eb071 | ||
|
|
e70689d29f | ||
|
|
5841646017 | ||
|
|
194effba97 | ||
|
|
8c353bb884 | ||
|
|
dc0d3ca005 | ||
|
|
a4c2a89e7e | ||
|
|
516ac89bd8 | ||
|
|
bf9c323d60 | ||
|
|
ae5db7216e | ||
|
|
c5a86cb30a |
@@ -2,6 +2,8 @@ FROM --platform=linux/arm64 ubuntu:22.04
|
||||
|
||||
# Node.js 20 from NodeSource (Ubuntu Jammy ships v12 which is too old
|
||||
# for actions/checkout@v4 — static class blocks require Node.js >= 16).
|
||||
#
|
||||
# libboost-dev — header-only Boost required by CGAL 6.x (-DWITH_CGAL=ON)
|
||||
RUN apt-get update -qq && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
curl ca-certificates && \
|
||||
@@ -11,6 +13,7 @@ RUN apt-get update -qq && \
|
||||
cmake \
|
||||
build-essential \
|
||||
git \
|
||||
libboost-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /workspace
|
||||
|
||||
@@ -6,10 +6,16 @@ on:
|
||||
- main
|
||||
- dev
|
||||
- "claude/**"
|
||||
- "feature/**"
|
||||
pull_request:
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Job 1 — test-fast
|
||||
# Pure-math tests (Clausen, ImLi₂, Hyper-ideal Geometrie).
|
||||
# Kein CGAL, kein Boost. Nur Eigen + GTest. Läuft auf ALLEN Branches.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
jobs:
|
||||
test:
|
||||
test-fast:
|
||||
runs-on: eulernest
|
||||
container:
|
||||
image: git.eulernest.eu/conformallab/ci-cpp:latest
|
||||
@@ -17,11 +23,11 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Configure (tests-only mode)
|
||||
- name: Configure (tests-only)
|
||||
run: cmake -S code -B build -DCMAKE_BUILD_TYPE=Release
|
||||
|
||||
- name: Build test binary
|
||||
run: cmake --build build --target conformallab_tests -j$(nproc)
|
||||
- name: Build
|
||||
run: nice -n 19 cmake --build build --target conformallab_tests -j$(nproc)
|
||||
|
||||
- name: Run tests
|
||||
run: >
|
||||
@@ -29,7 +35,7 @@ jobs:
|
||||
--output-on-failure
|
||||
--output-junit test-results.xml
|
||||
|
||||
- name: Show test summary
|
||||
- name: Zusammenfassung
|
||||
if: always()
|
||||
run: |
|
||||
if [ -f test-results.xml ]; then
|
||||
@@ -37,5 +43,51 @@ jobs:
|
||||
failed=$(grep -o 'failures="[0-9]*"' test-results.xml | grep -o '[0-9]*' | head -1)
|
||||
skipped=$(grep -o 'skipped="[0-9]*"' test-results.xml | grep -o '[0-9]*' | head -1)
|
||||
passed=$(( ${total:-0} - ${failed:-0} - ${skipped:-0} ))
|
||||
echo "TOTAL: ${total:-0} | PASSED: $passed | FAILED: ${failed:-0} | SKIPPED: ${skipped:-0}"
|
||||
echo "FAST ▸ TOTAL ${total:-0} | PASSED $passed | FAILED ${failed:-0} | SKIPPED ${skipped:-0}"
|
||||
fi
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Job 2 — test-cgal
|
||||
# Vollständige CGAL-Test-Suite (Phase 3–7, 158 Tests).
|
||||
# Läuft NUR bei Pull Requests (nicht bei direkten Pushes auf dev/main).
|
||||
# Startet erst nach erfolgreichem test-fast.
|
||||
#
|
||||
# Verwendet -DWITH_CGAL_TESTS=ON (nicht -DWITH_CGAL=ON), damit kein
|
||||
# Viewer/GLFW gebaut wird — der CI-Container hat kein wayland-scanner.
|
||||
#
|
||||
# Boost (libboost-dev) ist seit Image-Rebuild bereits im Container.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
test-cgal:
|
||||
needs: test-fast
|
||||
if: github.event_name == 'pull_request'
|
||||
runs-on: eulernest
|
||||
container:
|
||||
image: git.eulernest.eu/conformallab/ci-cpp:latest
|
||||
options: "--memory=1400m --memory-swap=1400m"
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Configure (WITH_CGAL_TESTS — kein Viewer, kein wayland-scanner)
|
||||
run: cmake -S code -B build -DWITH_CGAL_TESTS=ON -DCMAKE_BUILD_TYPE=Release
|
||||
|
||||
- name: Build CGAL-Tests
|
||||
run: nice -n 19 cmake --build build --target conformallab_cgal_tests -j2
|
||||
|
||||
- name: Run CGAL-Tests
|
||||
run: >
|
||||
ctest --test-dir build
|
||||
-R "^cgal\."
|
||||
--output-on-failure
|
||||
--output-junit cgal-results.xml
|
||||
|
||||
- name: Zusammenfassung
|
||||
if: always()
|
||||
run: |
|
||||
if [ -f cgal-results.xml ]; then
|
||||
total=$(grep -o 'tests="[0-9]*"' cgal-results.xml | grep -o '[0-9]*' | head -1)
|
||||
failed=$(grep -o 'failures="[0-9]*"' cgal-results.xml | grep -o '[0-9]*' | head -1)
|
||||
skipped=$(grep -o 'skipped="[0-9]*"' cgal-results.xml | grep -o '[0-9]*' | head -1)
|
||||
passed=$(( ${total:-0} - ${failed:-0} - ${skipped:-0} ))
|
||||
echo "CGAL ▸ TOTAL ${total:-0} | PASSED $passed | FAILED ${failed:-0} | SKIPPED ${skipped:-0}"
|
||||
fi
|
||||
|
||||
@@ -4,7 +4,6 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- dev
|
||||
|
||||
jobs:
|
||||
mirror:
|
||||
@@ -14,10 +13,11 @@ jobs:
|
||||
- name: Mirror all branches to Codeberg
|
||||
env:
|
||||
CODEBERG_TOKEN: ${{ secrets.CODEBERG_TOKEN }}
|
||||
GITEA_MIRROR_TOKEN: ${{ secrets.MIRROR_TOKEN }}
|
||||
run: |
|
||||
git clone --bare \
|
||||
https://oauth2:${GITHUB_TOKEN}@git.eulernest.eu/conformallab/ConformalLabpp.git \
|
||||
https://oauth2:${GITEA_MIRROR_TOKEN}@git.eulernest.eu/conformallab/ConformalLabpp.git \
|
||||
repo.git
|
||||
cd repo.git
|
||||
git push --mirror \
|
||||
https://TMoussa:${CODEBERG_TOKEN}@codeberg.org/TMoussa/ConformalLabpp.git
|
||||
https://TMoussa:${CODEBERG_TOKEN}@codeberg.org/TMoussa/ConformalLabpp.git
|
||||
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
# macOS
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Build directories
|
||||
build/
|
||||
build-*/
|
||||
build_*/
|
||||
code/build*/
|
||||
|
||||
# CMake
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
cmake_install.cmake
|
||||
CTestTestfile.cmake
|
||||
|
||||
# Test output
|
||||
*.xml
|
||||
Testing/
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.user
|
||||
*.suo
|
||||
|
||||
# Claude Code worktrees
|
||||
.claude/
|
||||
68
CITATION.cff
Normal file
68
CITATION.cff
Normal file
@@ -0,0 +1,68 @@
|
||||
cff-version: 1.2.0
|
||||
message: "If you use this software in your research, please cite it as below."
|
||||
|
||||
authors:
|
||||
- family-names: Moussa
|
||||
given-names: Tarik
|
||||
email: Tarik.moussa95@gmail.com
|
||||
|
||||
title: "conformallab++"
|
||||
version: 0.7.0
|
||||
date-released: 2026-05-18
|
||||
url: "https://codeberg.org/TMoussa/ConformalLabpp"
|
||||
repository-code: "https://codeberg.org/TMoussa/ConformalLabpp"
|
||||
license: MIT
|
||||
|
||||
abstract: >
|
||||
conformallab++ is a C++17 implementation of discrete conformal maps on
|
||||
triangulated surfaces, covering Euclidean, Spherical, and Hyper-ideal geometry
|
||||
modes. It provides a Newton solver for discrete conformal equivalence (DCE),
|
||||
tree-cotree cut graphs, Möbius holonomy, period matrix computation with
|
||||
SL(2,ℤ) reduction, and fundamental domain construction. The long-term goal is
|
||||
a CGAL package for discrete conformal geometry.
|
||||
|
||||
keywords:
|
||||
- discrete conformal geometry
|
||||
- conformal maps
|
||||
- surface parameterization
|
||||
- period matrix
|
||||
- Teichmüller theory
|
||||
- CGAL
|
||||
- C++
|
||||
|
||||
references:
|
||||
- type: thesis
|
||||
authors:
|
||||
- family-names: Sechelmann
|
||||
given-names: Stefan
|
||||
title: >
|
||||
Variational Methods for Discrete Surface Parameterization:
|
||||
Applications and Implementation
|
||||
institution:
|
||||
name: Technische Universität Berlin
|
||||
year: 2016
|
||||
doi: 10.14279/depositonce-5415
|
||||
notes: "Primary algorithmic source for this implementation"
|
||||
|
||||
- type: article
|
||||
authors:
|
||||
- family-names: Springborn
|
||||
given-names: Boris
|
||||
title: "Ideal Hyperbolic Polyhedra and Discrete Uniformization"
|
||||
journal: "Discrete & Computational Geometry"
|
||||
year: 2020
|
||||
doi: 10.1007/s00454-019-00132-8
|
||||
notes: "Mathematical basis for the HyperIdeal geometry mode"
|
||||
|
||||
- type: article
|
||||
authors:
|
||||
- family-names: Bobenko
|
||||
given-names: Alexander I.
|
||||
- family-names: Springborn
|
||||
given-names: Boris A.
|
||||
title: >
|
||||
Variational Principles for Circle Patterns and Koebe's Theorem
|
||||
journal: "Transactions of the American Mathematical Society"
|
||||
year: 2004
|
||||
doi: 10.1090/S0002-9947-03-03239-2
|
||||
notes: "Variational framework underlying all three geometry modes"
|
||||
269
CLAUDE.md
Normal file
269
CLAUDE.md
Normal file
@@ -0,0 +1,269 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project purpose and long-term goal
|
||||
|
||||
conformallab++ is a C++17 reimplementation of [ConformalLab](https://github.com/varylab/conformallab) — Stefan Sechelmann's Java research library for discrete conformal geometry (TU Berlin, ~850 commits, v1.0.0 2018). The algorithmic foundation is his dissertation:
|
||||
|
||||
> Stefan Sechelmann — *Variational Methods for Discrete Surface Parameterization: Applications and Implementation*, TU Berlin 2016.
|
||||
> DOI: [10.14279/depositonce-5415](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f) · CC BY-SA 4.0
|
||||
|
||||
**The long-term goal is a CGAL package** — a submission to the CGAL library that brings discrete conformal maps (hyper-ideal, spherical, Euclidean) to the CGAL ecosystem using `CGAL::Surface_mesh` as the underlying halfedge data structure, with a traits-class design compatible with arbitrary CGAL-conforming mesh types.
|
||||
|
||||
The project has three distinct phases:
|
||||
- **Phase 1–7 (done):** Direct port of the Java library algorithms to C++
|
||||
- **Phase 8–9 (planned):** CGAL package infrastructure + remaining Java features not yet ported (inversive-distance functional, analytic HyperIdeal Hessian, genus-g > 1 fundamental domain)
|
||||
- **Phase 10+ (research):** New mathematics beyond the Java original — holomorphic differentials, Siegel period matrix Ω ∈ H_g, full uniformization for genus g ≥ 2
|
||||
|
||||
## Language
|
||||
|
||||
**All code, comments, documentation, commit messages, and test descriptions must be in English.** The project is intended for international collaboration and CGAL submission. Existing German-language comments in older files should be replaced with English when editing those files.
|
||||
|
||||
## Build commands
|
||||
|
||||
All source lives under `code/`. Three build modes:
|
||||
|
||||
```bash
|
||||
# Mode 1 — fast tests, no CGAL, no Boost, no display (CI default)
|
||||
cmake -S code -B build
|
||||
cmake --build build --target conformallab_tests -j$(nproc)
|
||||
ctest --test-dir build --output-on-failure
|
||||
|
||||
# Mode 2 — CGAL tests, headless (CI full, requires Boost headers only)
|
||||
# macOS: brew install boost Linux: apt install libboost-dev
|
||||
cmake -S code -B build -DWITH_CGAL_TESTS=ON
|
||||
cmake --build build --target conformallab_cgal_tests -j$(nproc)
|
||||
ctest --test-dir build -R "^cgal\." --output-on-failure
|
||||
|
||||
# Mode 3 — full local build: CLI app + viewer + examples (requires Wayland/X11)
|
||||
cmake -S code -B build -DWITH_CGAL=ON
|
||||
cmake --build build -j$(nproc)
|
||||
```
|
||||
|
||||
`-DWITH_CGAL=ON` automatically enables `-DWITH_VIEWER=ON`, which pulls in GLFW and requires `wayland-scanner`. Never use this in headless CI.
|
||||
|
||||
### Running a single test
|
||||
|
||||
```bash
|
||||
# By GTest suite/test name
|
||||
./build/conformallab_cgal_tests --gtest_filter="NewtonSolver*"
|
||||
./build/conformallab_tests --gtest_filter="Clausen*"
|
||||
|
||||
# By CTest regex (prefix "cgal." for all CGAL tests)
|
||||
ctest --test-dir build -R "cgal.NewtonSolver" --output-on-failure
|
||||
```
|
||||
|
||||
### Rebuilding the CI Docker image
|
||||
|
||||
```bash
|
||||
docker buildx build \
|
||||
--platform linux/arm64 \
|
||||
-f .gitea/docker/Dockerfile.ci-cpp \
|
||||
-t git.eulernest.eu/conformallab/ci-cpp:latest \
|
||||
--push \
|
||||
.gitea/docker/
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
### Everything is header-only
|
||||
|
||||
All algorithms live in `code/include/*.hpp`. There is no compiled library. The three CMake targets (`conformallab_tests`, `conformallab_cgal_tests`, `conformallab_core`) compile headers directly from their `.cpp` entry points. To add a new algorithm: create a `.hpp` in `code/include/`, add a test in `code/tests/cgal/`, and register the test file in `code/tests/cgal/CMakeLists.txt`.
|
||||
|
||||
### Central type: `ConformalMesh`
|
||||
|
||||
`conformal_mesh.hpp` defines the core type:
|
||||
```cpp
|
||||
using ConformalMesh = CGAL::Surface_mesh<Point3>; // CGAL::Simple_cartesian<double>
|
||||
```
|
||||
|
||||
This replaces the Java `CoHDS` (half-edge data structure) and its intrusive `CoVertex`/`CoEdge`/`CoFace` types. Data is attached via named CGAL property maps instead of intrusive fields:
|
||||
|
||||
| Property map name | Type | Meaning |
|
||||
|---|---|---|
|
||||
| `"v:lambda"` | `double` per vertex | log scale factor (conformal variable uᵢ) |
|
||||
| `"v:theta"` | `double` per vertex | target cone angle Θᵥ |
|
||||
| `"v:idx"` | `int` per vertex | solver DOF index; `-1` = pinned/boundary |
|
||||
| `"e:alpha"` | `double` per edge | intersection angle αᵢⱼ (hyperbolic only) |
|
||||
| `"f:type"` | `int` per face | geometry type (0=Euclidean, 1=Hyperbolic, 2=Spherical) |
|
||||
|
||||
`CGAL_DISABLE_GMP` and `CGAL_DISABLE_MPFR` are defined for all CGAL targets — the library deliberately uses `Simple_cartesian<double>` (floating-point, no exact arithmetic) because conformal geometry does not require exact predicates.
|
||||
|
||||
### The three geometry modes
|
||||
|
||||
Each mode has its own Maps struct that bundles all property maps, plus functional, Hessian, and Newton function:
|
||||
|
||||
| Mode | Space | Maps struct | Key headers | Newton function |
|
||||
|---|---|---|---|---|
|
||||
| Euclidean | ℝ² | `EuclideanMaps` | `euclidean_functional.hpp`, `euclidean_hessian.hpp` | `newton_euclidean()` |
|
||||
| Spherical | S² | `SphericalMaps` | `spherical_functional.hpp`, `spherical_hessian.hpp` | `newton_spherical()` |
|
||||
| Hyper-ideal | H² (Poincaré disk) | `HyperIdealMaps` | `hyper_ideal_functional.hpp`, `hyper_ideal_hessian.hpp` | `newton_hyper_ideal()` |
|
||||
|
||||
HyperIdeal also has edge DOFs (`e_idx[e]`); Euclidean and Spherical are vertex-DOF only. For HyperIdeal: `assign_all_dof_indices(mesh, maps)` assigns all vertex and edge DOFs automatically. For Euclidean/Spherical: pin one vertex manually (`maps.v_idx[first_vertex] = -1`) then assign sequential indices.
|
||||
|
||||
### The full pipeline
|
||||
|
||||
```
|
||||
load_mesh() → ConformalMesh (OFF/OBJ/PLY)
|
||||
setup_*_maps(mesh) → *Maps (property maps created, all zero)
|
||||
compute_*_lambda0_from_mesh(mesh, m) → λ° initialised from 3-D edge lengths
|
||||
DOF assignment → v_idx[v] set; -1 = pinned
|
||||
check_gauss_bonnet(mesh, maps) → throws if Σ(2π−Θᵥ) ≠ 2π·χ(M)
|
||||
enforce_gauss_bonnet(mesh, maps) → redistributes angle defect uniformly
|
||||
newton_*(mesh, x0, maps) → NewtonResult{x*, iterations, converged}
|
||||
compute_cut_graph(mesh) → CutGraph (2g seam edges, tree-cotree)
|
||||
*_layout(mesh, x*, maps, &cg, &hol) → Layout2D/3D + HolonomyData
|
||||
normalise_*(layout) → canonical position (PCA / Möbius / Rodrigues)
|
||||
compute_period_matrix(hol) → PeriodData{τ∈ℍ} (genus 1 flat torus)
|
||||
compute_fundamental_domain(hol) → FundamentalDomain{vertices, generators}
|
||||
tiling_neighbourhood(layout, hol) → vector of translated layout copies
|
||||
save_result_json/xml() → serialised result
|
||||
```
|
||||
|
||||
After `compute_*_lambda0_from_mesh()` the original vertex positions are no longer used — all subsequent computation is in log-length/scale-factor space.
|
||||
|
||||
### Newton solver (`newton_solver.hpp`)
|
||||
|
||||
The gradient sign convention differs between modes:
|
||||
- **Euclidean/HyperIdeal:** `G_v = actual_angle_sum − Θ_v`, H is PSD → `SimplicialLDLT(H)`
|
||||
- **Spherical:** `G_v = Θ_v − actual_angle_sum`, H is NSD → `SimplicialLDLT(−H)`
|
||||
|
||||
When `SimplicialLDLT` fails (rank-deficient H — gauge mode on closed mesh without pinned vertex), the solver automatically retries with `Eigen::SparseQR` to find the minimum-norm step orthogonal to the null space. Public API: `solve_linear_system(H, rhs, &used_fallback)`.
|
||||
|
||||
The HyperIdeal Hessian is currently a **symmetric finite-difference approximation** (O(ε²), costs n extra gradient evaluations per Newton step). The analytic Hessian via the chain `(bᵢ, aₑ) → lᵢⱼ → ζ₁₃/ζ₁₄/ζ₁₅ → αᵢⱼ/βᵢ` is deferred to Phase 9b.
|
||||
|
||||
### Layout and holonomy (`layout.hpp`)
|
||||
|
||||
BFS-trilateration with a **priority min-heap on BFS depth** (`depth = max(depth[src], depth[tgt]) + 1`). Root face = largest 3-D area face. This minimises trilateration error accumulation compared to simple BFS.
|
||||
|
||||
Key output fields:
|
||||
- `layout.uv[v.idx()]` — primary UV (first/shallowest BFS visit per vertex)
|
||||
- `layout.halfedge_uv[h.idx()]` — UV of `source(h)` as seen from `face(h)`; at seam halfedges the two opposite halfedges carry *different* UV values, enabling proper GPU texture atlasing without vertex duplication
|
||||
- `hol.translations[i]` — lattice generator ωᵢ ∈ ℂ (Euclidean/spherical)
|
||||
- `hol.mobius_maps[i]` — Möbius isometry Tᵢ ∈ SU(1,1) (hyperbolic, Poincaré disk)
|
||||
|
||||
`MobiusMap` is defined in `layout.hpp`: T(z) = (az+b)/(cz+d). Key methods: `from_three()` (fit to 3 point correspondences via 3×3 complex linear system), `compose()`, `inverse()`, `apply(Vector2d)`.
|
||||
|
||||
### Key mathematical reference for each header
|
||||
|
||||
| Header | Java original | Key reference |
|
||||
|---|---|---|
|
||||
| `hyper_ideal_geometry.hpp` | `HyperIdealGeometry.java` | Springborn (2020) — ζ₁₃/ζ₁₄/ζ₁₅ functions |
|
||||
| `euclidean_hessian.hpp` | `EuclideanHessian.java` | Pinkall & Polthier (1993) — cotangent Laplacian |
|
||||
| `spherical_hessian.hpp` | `SphericalHessian.java` | ∂α/∂u from spherical law of cosines |
|
||||
| `cut_graph.hpp` | `CuttingUtility.java` | Erickson & Whittlesey (SODA 2005) — tree-cotree |
|
||||
| `period_matrix.hpp` | `PeriodMatrixUtility.java` | Sechelmann (2016) §4 — SL(2,ℤ) reduction |
|
||||
| `gauss_bonnet.hpp` | (distributed across Java) | Gauss–Bonnet: Σ(2π−Θᵥ) = 2π·χ(M) |
|
||||
|
||||
### Java features not yet ported (Phase 9)
|
||||
|
||||
The Java library under `de.varylab.discreteconformal` contains these items not yet in C++:
|
||||
|
||||
| Java class | Planned C++ header | Phase |
|
||||
|---|---|---|
|
||||
| `InversiveDistanceFunctional` | `inversive_distance_functional.hpp` | 9a |
|
||||
| Analytic HyperIdeal Hessian | `hyper_ideal_hessian.hpp` (replace FD) | 9b |
|
||||
| 4g-polygon boundary walk in `FundamentalDomainUtility` | `fundamental_domain.hpp` (extend) | 9c |
|
||||
| `DiscreteHarmonicFormUtility` | Phase 10a prerequisite | 10 |
|
||||
| `DiscreteHolomorphicFormUtility` | Phase 10a | 10 |
|
||||
| `HomologyUtility`, `CanonicalBasisUtility` | Phase 10 | 10 |
|
||||
|
||||
When porting a Java class, locate the original in `de.varylab.discreteconformal.*` at [github.com/varylab/conformallab](https://github.com/varylab/conformallab) and use it as the reference implementation.
|
||||
|
||||
## Test design patterns
|
||||
|
||||
### "Natural theta" — constructing a known equilibrium at x* = 0
|
||||
|
||||
```cpp
|
||||
// Evaluate gradient at x=0; set target angles = actual angle sums → x*=0 by definition
|
||||
std::vector<double> x0(n_dofs, 0.0);
|
||||
auto G0 = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices())
|
||||
if (maps.v_idx[v] >= 0)
|
||||
maps.theta_v[v] -= G0[maps.v_idx[v]]; // shift so G(x=0) = 0
|
||||
```
|
||||
|
||||
This is used in virtually every Newton convergence test — it avoids hardcoding specific angle values.
|
||||
|
||||
### Gradient check pattern
|
||||
|
||||
```cpp
|
||||
// Copy from any test_*_functional.cpp — GradientCheck_* test suite
|
||||
double eps = 1e-5;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
xp[i] += eps; auto Gp = euclidean_gradient(mesh, xp, maps);
|
||||
xm[i] -= eps; auto Gm = euclidean_gradient(mesh, xm, maps);
|
||||
double fd = (energy(xp) - energy(xm)) / (2*eps);
|
||||
EXPECT_NEAR(G[i], fd, 1e-7);
|
||||
xp[i] = xm[i] = x0[i];
|
||||
}
|
||||
```
|
||||
|
||||
All new functionals must have a gradient-check test before being considered complete.
|
||||
|
||||
### Halfedge traversal
|
||||
|
||||
```cpp
|
||||
for (auto f : mesh.faces()) {
|
||||
auto h0 = mesh.halfedge(f); // canonical halfedge of face
|
||||
auto h1 = mesh.next(h0);
|
||||
auto h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0); // = mesh.target(h2)
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
// Angle at v3 is opposite to h0 (edge v1–v2)
|
||||
// h_alpha[h0] = α₃, h_alpha[h1] = α₁, h_alpha[h2] = α₂
|
||||
|
||||
bool is_boundary = mesh.is_border(mesh.opposite(h0));
|
||||
}
|
||||
```
|
||||
|
||||
### Attaching custom data to the mesh
|
||||
|
||||
```cpp
|
||||
auto [my_map, created] = mesh.add_property_map<Vertex_index, double>("v:my_data", 0.0);
|
||||
my_map[v] = 3.14;
|
||||
```
|
||||
|
||||
## CI pipeline
|
||||
|
||||
Two jobs in `.gitea/workflows/cpp-tests.yml`:
|
||||
|
||||
| Job | CMake flags | Deps | Triggers on |
|
||||
|---|---|---|---|
|
||||
| `test-fast` | *(none)* | Eigen + GTest only | all branches |
|
||||
| `test-cgal` | `-DWITH_CGAL_TESTS=ON` | + Boost | `main`, `dev`, PRs only |
|
||||
|
||||
Runner: `eulernest` — self-hosted Raspberry Pi, ARM64, Ubuntu 22.04. Docker image: `git.eulernest.eu/conformallab/ci-cpp:latest`. `test-cgal` needs `test-fast` to pass first (`needs: test-fast`).
|
||||
|
||||
Expected results: **36 non-CGAL tests pass**, **173 CGAL tests pass, 1 skipped** (intentional `GTEST_SKIP` stub for analytic HyperIdeal Hessian — deferred to Phase 9b).
|
||||
|
||||
## Key documentation for mathematical context
|
||||
|
||||
When working on math-heavy tasks, read these before reasoning from scratch:
|
||||
|
||||
| Question | Document |
|
||||
|---|---|
|
||||
| What is the mathematical problem this library solves? | `doc/math/discrete-conformal-theory.md` |
|
||||
| What are the three geometry modes and how do they differ? | `doc/math/geometry-modes.md` |
|
||||
| How does conformallab++ relate to geometry-central (CMU)? | `doc/architecture/geometry-central-comparison.md` |
|
||||
| What analytic results can be used to validate correctness? | `doc/math/validation.md` |
|
||||
| Which Java classes are ported, which are planned? | `doc/roadmap/java-parity.md` |
|
||||
| What does each processing function require/provide? | `doc/api/contracts.md` |
|
||||
|
||||
**geometry-central** (Keenan Crane, CMU) implements the same discrete conformal
|
||||
equivalence problem (Gillespie, Springborn & Crane, SIGGRAPH 2021) but uses
|
||||
Ptolemaic flips on intrinsic triangulations instead of Newton on the original mesh.
|
||||
It has no period matrix, holonomy, or spherical geometry mode.
|
||||
The shared mathematical core (Springborn 2020) means cross-validation is meaningful.
|
||||
See `doc/architecture/geometry-central-comparison.md` for the full comparison.
|
||||
|
||||
## Known quirks
|
||||
|
||||
- **`test-fast` also runs stubs**: `conformallab_tests` (non-CGAL) contains `GTEST_SKIP`-based stubs for functionals that need CGAL. This is intentional — those tests document what was in the Java port scope but requires the CGAL mesh type.
|
||||
- **Boost is header-only**: CGAL 6.x uses only Boost headers (`Boost.Config`, `Boost.Graph`). No compiled Boost libraries are needed. `find_package(Boost REQUIRED)` only locates the include path.
|
||||
- **`main` branch is protected** on `origin` (Gitea). Push to `dev`, then merge via pull request. Codeberg `main` can be pushed to directly.
|
||||
- **Both remotes must stay in sync**: `origin` = `git.eulernest.eu` (CI runs here), `codeberg` = `codeberg.org/TMoussa/ConformalLabpp` (public mirror). Push to both after every significant change.
|
||||
17
CONTRIBUTING.md
Normal file
17
CONTRIBUTING.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Contributing to conformallab++
|
||||
|
||||
See **[doc/contributing.md](doc/contributing.md)** for the full guide:
|
||||
|
||||
- Git workflow (dev → PR → main)
|
||||
- CI pipeline (test-fast / test-cgal)
|
||||
- Test standards (gradient check, convergence test, registration)
|
||||
- Code style (C++17, header-only, namespace, property map naming)
|
||||
- Release process
|
||||
|
||||
For the mathematical background of what's being implemented, see:
|
||||
|
||||
- [doc/math/discrete-conformal-theory.md](doc/math/discrete-conformal-theory.md) — theory overview
|
||||
- [doc/math/validation.md](doc/math/validation.md) — how to validate the implementation
|
||||
- [doc/math/references.md](doc/math/references.md) — all referenced papers
|
||||
|
||||
To add your own research, see [doc/api/extending.md](doc/api/extending.md).
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 user2595
|
||||
Copyright (c) 2024–2026 Tarik Moussa <Tarik.moussa95@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
||||
185
README.md
185
README.md
@@ -1,104 +1,133 @@
|
||||
# conformallab++
|
||||
|
||||
conformallab++ is a modern C++ reimplementation of the [ConformalLab](https://github.com/sechel/conformallab) software by Stefan Sechelmann for experiments in discrete conformal geometry and related mesh transformations.
|
||||
[](https://git.eulernest.eu/conformallab/ConformalLabpp/actions)
|
||||
[](LICENSE)
|
||||
[](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f)
|
||||
|
||||
> **Status:** early prototype stage. API, file formats, and CLI are subject to change.
|
||||
C++17 reimplementation of [ConformalLab](https://github.com/varylab/conformallab) —
|
||||
Stefan Sechelmann's Java research library for discrete conformal geometry (TU Berlin).
|
||||
The long-term goal is a **CGAL package** for discrete conformal maps.
|
||||
|
||||
## Features
|
||||
Algorithmic foundation:
|
||||
> Stefan Sechelmann — *Variational Methods for Discrete Surface Parameterization: Applications and Implementation*, TU Berlin 2016.
|
||||
> DOI: [10.14279/depositonce-5415](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f) · CC BY-SA 4.0 ·
|
||||
> [Java original](https://github.com/varylab/conformallab) · [sechel.de](https://sechel.de/)
|
||||
|
||||
- Discrete conformal geometry utilities (Clausen function, hyper-ideal tetrahedra, surface curves)
|
||||
- Mesh I/O and conversion using CGAL — optional, only needed for the CLI app
|
||||
- Linear algebra routines with Eigen
|
||||
- Interactive mesh viewer using libigl / GLFW — optional
|
||||
- Lightweight CLI with CLI11 and JSON configuration
|
||||
**Status:** Phase 7 complete. Newton solver for all three geometries (Euclidean / Spherical / HyperIdeal), priority-BFS layout in ℝ²/S²/Poincaré disk, Gauss–Bonnet, tree-cotree cut graph, Möbius holonomy, period matrix (genus 1), fundamental domain, halfedge_uv texture atlas, JSON/XML serialisation, CLI app. **173 CGAL tests + 36 non-CGAL tests.**
|
||||
|
||||
## Build modes
|
||||
---
|
||||
|
||||
The project uses three clearly separated CMake modes so you only pull in what you need.
|
||||
|
||||
| Mode | CMake flag | What gets built |
|
||||
|------|-----------|-----------------|
|
||||
| **Tests only** (default, used in CI) | *(none)* | `conformallab_tests` · deps: Eigen + GTest |
|
||||
| **Viewer** | `-DWITH_VIEWER=ON` | `viewer` library · deps: libigl / GLFW / GLAD + Eigen |
|
||||
| **Full app** | `-DWITH_CGAL=ON` | `conformallab_core` CLI + viewer · deps: CGAL + libigl / GLFW / GLAD + Eigen |
|
||||
|
||||
`-DWITH_CGAL=ON` automatically enables `WITH_VIEWER` because the CLI app uses the viewer library for mesh visualisation.
|
||||
|
||||
External dependencies ship as tarballs in `code/deps/tarballs/` and are extracted lazily at CMake configure time — no internet access needed after cloning (GTest is the only exception: fetched from GitHub via FetchContent).
|
||||
|
||||
## Prerequisites
|
||||
|
||||
| Tool | Minimum version |
|
||||
|------|----------------|
|
||||
| C++ compiler (GCC or Clang) | C++17 |
|
||||
| CMake | 3.20 |
|
||||
|
||||
No system-level libraries are required for the default tests-only build. CGAL and libigl are header-only and bundled in the repo.
|
||||
|
||||
## Getting started
|
||||
## Quick start
|
||||
|
||||
```bash
|
||||
git clone https://codeberg.org/TMoussa/ConformalLabpp
|
||||
cd ConformalLabpp
|
||||
```
|
||||
git clone https://codeberg.org/TMoussa/ConformalLabpp && cd ConformalLabpp
|
||||
|
||||
### Tests only (CI default)
|
||||
|
||||
```bash
|
||||
cmake -S code -B build
|
||||
cmake --build build --target conformallab_tests -j$(nproc)
|
||||
# Fast tests — no system dependencies
|
||||
cmake -S code -B build && cmake --build build --target conformallab_tests -j$(nproc)
|
||||
ctest --test-dir build --output-on-failure
|
||||
|
||||
# CGAL tests headless (apt install libboost-dev / brew install boost)
|
||||
cmake -S code -B build -DWITH_CGAL_TESTS=ON
|
||||
cmake --build build --target conformallab_cgal_tests -j$(nproc)
|
||||
ctest --test-dir build -R "^cgal\." --output-on-failure
|
||||
|
||||
# Full build with CLI + viewer (requires Wayland/X11 dev headers)
|
||||
cmake -S code -B build -DWITH_CGAL=ON && cmake --build build -j$(nproc)
|
||||
./bin/conformallab_core -i input.off -g euclidean -o layout.off -j result.json
|
||||
```
|
||||
|
||||
### Full CLI app (CGAL + viewer)
|
||||
---
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_CGAL=ON
|
||||
cmake --build build -j$(nproc)
|
||||
./code/bin/conformallab_core --input data/off/example.off --show
|
||||
## Minimal usage
|
||||
|
||||
```cpp
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "gauss_bonnet.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
ConformalMesh mesh = load_mesh("input.off");
|
||||
EuclideanMaps maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Assign DOFs — pin first vertex (gauge fix)
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
|
||||
// Natural equilibrium target: x* = 0 by construction
|
||||
std::vector<double> x0(idx, 0.0);
|
||||
auto G0 = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices())
|
||||
if (maps.v_idx[v] >= 0) maps.theta_v[v] -= G0[maps.v_idx[v]];
|
||||
|
||||
check_gauss_bonnet(mesh, maps);
|
||||
NewtonResult res = newton_euclidean(mesh, x0, maps);
|
||||
Layout2D layout = euclidean_layout(mesh, res.x, maps);
|
||||
```
|
||||
|
||||
### Viewer only (no CGAL)
|
||||
---
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_VIEWER=ON
|
||||
cmake --build build --target viewer -j$(nproc)
|
||||
```
|
||||
## Documentation
|
||||
|
||||
## Project structure
|
||||
| | |
|
||||
|---|---|
|
||||
| **Getting started** — build modes, single-test invocation, CLI, Docker | [doc/getting-started.md](doc/getting-started.md) |
|
||||
| **Pipeline API** — all three geometries, holonomy, serialisation | [doc/api/pipeline.md](doc/api/pipeline.md) |
|
||||
| **Public headers** — all 24 headers with descriptions | [doc/api/headers.md](doc/api/headers.md) |
|
||||
| **Test suites** — 28 suites, 170 tests, individual counts | [doc/api/tests.md](doc/api/tests.md) |
|
||||
| **Extending** — new functionals, geometry modes, porting from Java | [doc/api/extending.md](doc/api/extending.md) |
|
||||
| **Processing unit contracts** — preconditions / provides table | [doc/api/contracts.md](doc/api/contracts.md) |
|
||||
| **CGAL package design** — Phase 8 target, YAML pipeline | [doc/api/cgal-package.md](doc/api/cgal-package.md) |
|
||||
| **Architecture & pipeline diagram** | [doc/architecture/overall_pipeline.md](doc/architecture/overall_pipeline.md) |
|
||||
| **geometry-central comparison** — shared core, demarcation, adoption candidates, scientific added value | [doc/architecture/geometry-central-comparison.md](doc/architecture/geometry-central-comparison.md) |
|
||||
| **Design decisions** — key architectural choices + rationale | [doc/architecture/design-decisions.md](doc/architecture/design-decisions.md) |
|
||||
| **Project structure** — directory tree + build targets | [doc/architecture/project-structure.md](doc/architecture/project-structure.md) |
|
||||
| **Discrete conformal theory** — mathematical background for collaborators | [doc/math/discrete-conformal-theory.md](doc/math/discrete-conformal-theory.md) |
|
||||
| **Validation** — known analytic results + how to verify them | [doc/math/validation.md](doc/math/validation.md) |
|
||||
| **Validation protocol** — concrete commands with expected outputs | [doc/math/validation-protocol.md](doc/math/validation-protocol.md) |
|
||||
| **Tutorial: add a new functional** — step-by-step Inversive-Distance port | [doc/tutorials/add-inversive-distance.md](doc/tutorials/add-inversive-distance.md) |
|
||||
| **Declarative YAML pipeline** — concept, token vocabulary, 5 examples | [doc/concepts/declarative-pipeline.md](doc/concepts/declarative-pipeline.md) |
|
||||
| **Geometry modes** — Euclidean / Spherical / HyperIdeal comparison | [doc/math/geometry-modes.md](doc/math/geometry-modes.md) |
|
||||
| **References** — all papers by module | [doc/math/references.md](doc/math/references.md) |
|
||||
| **Software landscape** — how conformallab++ relates to libigl, CGAL, geometry-central | [doc/math/software-landscape.md](doc/math/software-landscape.md) |
|
||||
| **Novelty statement** — unique features, target audience, what this is not | [doc/math/novelty-statement.md](doc/math/novelty-statement.md) |
|
||||
| **Roadmap** — Phases 1–10 | [doc/roadmap/phases.md](doc/roadmap/phases.md) |
|
||||
| **Java parity table** — what is ported, what is planned | [doc/roadmap/java-parity.md](doc/roadmap/java-parity.md) |
|
||||
| **Contributing** — language policy, test standards, release flow | [doc/contributing.md](doc/contributing.md) |
|
||||
| **Claude Code context** | [CLAUDE.md](CLAUDE.md) |
|
||||
|
||||
```
|
||||
code/
|
||||
├── include/ # Public headers (Clausen, hyper-ideal, mesh utils, …)
|
||||
├── src/
|
||||
│ ├── apps/v0/ # conformallab_core CLI app (requires WITH_CGAL)
|
||||
│ └── viewer/ # simple_viewer (requires WITH_VIEWER)
|
||||
├── tests/ # GTest unit tests (always built)
|
||||
└── deps/
|
||||
├── tarballs/ # Bundled dependency archives
|
||||
├── eigen-3.4.0/ # Header-only linear algebra (always extracted)
|
||||
├── CGAL-6.1.1/ # Header-only geometry (extracted with WITH_CGAL)
|
||||
├── libigl-2.6.0/ # Header-only viewer toolkit (extracted with WITH_VIEWER)
|
||||
├── glfw-3.4/ # Windowing (extracted with WITH_VIEWER)
|
||||
├── libigl-glad/ # OpenGL loader (extracted with WITH_VIEWER)
|
||||
└── single_includes/ # CLI11, json.hpp
|
||||
```
|
||||
---
|
||||
|
||||
## CI
|
||||
## Citing
|
||||
|
||||
Tests run automatically on push to `main`, `dev`, and `claude/**` branches via a self-hosted Gitea Actions runner (`eulernest`, ARM64 Raspberry Pi). The pipeline uses a minimal Docker image (`git.eulernest.eu/conformallab/ci-cpp:latest`) with cmake, g++, git, and Node.js 20 pre-installed.
|
||||
If you use conformallab++ in your research, please cite it using the metadata
|
||||
in [`CITATION.cff`](CITATION.cff). GitHub and Codeberg show a "Cite this repository"
|
||||
button that generates BibTeX and APA automatically.
|
||||
|
||||
The Dockerfile for the CI image lives in `.gitea/docker/Dockerfile.ci-cpp`. Build and push it once whenever the image needs updating:
|
||||
The primary algorithmic source is:
|
||||
|
||||
```bash
|
||||
docker buildx build \
|
||||
--platform linux/arm64 \
|
||||
-f .gitea/docker/Dockerfile.ci-cpp \
|
||||
-t git.eulernest.eu/conformallab/ci-cpp:latest \
|
||||
--push \
|
||||
.gitea/docker/
|
||||
```
|
||||
> Stefan Sechelmann — *Variational Methods for Discrete Surface Parameterization:
|
||||
> Applications and Implementation*, TU Berlin 2016.
|
||||
> DOI: [10.14279/depositonce-5415](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f)
|
||||
|
||||
---
|
||||
|
||||
## Bugs & questions
|
||||
|
||||
- **Bug reports / feature requests:** [Gitea Issues](https://git.eulernest.eu/conformallab/ConformalLabpp/issues)
|
||||
- **Code mirror (read-only):** [Codeberg](https://codeberg.org/TMoussa/ConformalLabpp)
|
||||
- **Contact:** Tarik Moussa · Tarik.moussa95@gmail.com
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
conformallab++ is released under the MIT License (see [LICENSE](LICENSE)).
|
||||
conformallab++ is released under the MIT License (see [LICENSE](LICENSE)).
|
||||
Copyright © 2024–2026 Tarik Moussa.
|
||||
The dissertation (Sechelmann 2016) is CC BY-SA 4.0.
|
||||
|
||||
2
code/.gitignore
vendored
2
code/.gitignore
vendored
@@ -40,6 +40,8 @@ Thumbs.db
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
# Exception: mesh data files in code/data/ are not compiled objects
|
||||
!data/**/*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
|
||||
@@ -7,24 +7,42 @@ message(STATUS "Configuring ${PROJECT_NAME}...")
|
||||
|
||||
# ── Build modes ────────────────────────────────────────────────────────────────
|
||||
#
|
||||
# Default (CI / tests-only): only Eigen + GTest are required.
|
||||
# Default (CI fast / pure-math tests):
|
||||
# cmake -S code -B build
|
||||
# Only Eigen + GTest required. 36 non-CGAL tests.
|
||||
#
|
||||
# -DWITH_CGAL=ON builds the conformallab_core CLI app (needs CGAL).
|
||||
# Automatically enables WITH_VIEWER because the app uses
|
||||
# the viewer library for mesh visualisation.
|
||||
# -DWITH_CGAL_TESTS=ON CGAL test suite only — no viewer, no CLI app.
|
||||
# cmake -S code -B build -DWITH_CGAL_TESTS=ON
|
||||
# Requires: system Boost headers (apt install libboost-dev).
|
||||
# Builds: conformallab_cgal_tests (158 tests).
|
||||
# Does NOT require wayland-scanner, GLFW, libigl or a display.
|
||||
# Use this in headless CI.
|
||||
#
|
||||
# -DWITH_VIEWER=ON builds the viewer library standalone (libigl/GLFW/GLAD).
|
||||
# -DWITH_CGAL=ON Full build: CLI app + viewer + examples + CGAL tests.
|
||||
# cmake -S code -B build -DWITH_CGAL=ON
|
||||
# Requires: Boost + Wayland/X11 dev headers (wayland-scanner, libx11-dev …).
|
||||
# Automatically enables WITH_VIEWER.
|
||||
# Use this for local development with the interactive viewer.
|
||||
#
|
||||
# -DWITH_VIEWER=ON Viewer library only (libigl / GLFW / GLAD).
|
||||
#
|
||||
# ──────────────────────────────────────────────────────────────────────────────
|
||||
option(WITH_CGAL "Build conformallab_core app (requires CGAL + Viewer)" OFF)
|
||||
option(WITH_VIEWER "Build viewer library (libigl / GLFW / GLAD)" OFF)
|
||||
option(WITH_CGAL_TESTS "Build CGAL test suite without viewer/CLI (headless CI)" OFF)
|
||||
option(WITH_CGAL "Build conformallab_core CLI app + viewer + CGAL tests" OFF)
|
||||
option(WITH_VIEWER "Build viewer library (libigl / GLFW / GLAD)" OFF)
|
||||
|
||||
# The CLI app always needs the viewer; enable it implicitly.
|
||||
# WITH_CGAL_TESTS is a strict subset of WITH_CGAL — no viewer, no CLI.
|
||||
# WITH_CGAL (full build) implies WITH_VIEWER.
|
||||
if(WITH_CGAL AND NOT WITH_VIEWER)
|
||||
message(STATUS "WITH_CGAL implies WITH_VIEWER – enabling automatically.")
|
||||
set(WITH_VIEWER ON CACHE BOOL "" FORCE)
|
||||
endif()
|
||||
|
||||
# Propagate Boost requirement for both CGAL modes.
|
||||
if(WITH_CGAL OR WITH_CGAL_TESTS)
|
||||
find_package(Boost REQUIRED)
|
||||
endif()
|
||||
|
||||
# ── Standard settings ──────────────────────────────────────────────────────────
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
@@ -85,11 +103,6 @@ endif()
|
||||
|
||||
# ── Core CLI app (optional, requires CGAL + Viewer) ───────────────────────────
|
||||
if(WITH_CGAL)
|
||||
# CGAL 6.x still needs Boost.Config headers unconditionally.
|
||||
# Install via: brew install boost (macOS)
|
||||
# apt install libboost-dev (Debian/Ubuntu)
|
||||
find_package(Boost REQUIRED)
|
||||
|
||||
add_executable(${PROJECT_NAME} src/apps/v0/conformallab_cli.cpp)
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/deps/single_includes
|
||||
@@ -106,5 +119,24 @@ if(WITH_CGAL)
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
|
||||
endif()
|
||||
|
||||
# ── Example programs (require WITH_CGAL; viewer example also needs WITH_VIEWER) ─
|
||||
if(WITH_CGAL)
|
||||
add_subdirectory(examples)
|
||||
endif()
|
||||
|
||||
# ── Tests (always) ────────────────────────────────────────────────────────────
|
||||
add_subdirectory(tests)
|
||||
|
||||
# ── Install target (header-only library) ──────────────────────────────────────
|
||||
# Installs all public headers to <prefix>/include/conformallab/
|
||||
# Usage from another CMake project:
|
||||
# cmake --install build --prefix /usr/local
|
||||
# target_include_directories(myapp PRIVATE /usr/local/include/conformallab)
|
||||
include(GNUInstallDirs)
|
||||
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/conformallab
|
||||
FILES_MATCHING PATTERN "*.hpp"
|
||||
PATTERN "* 2.*" EXCLUDE) # exclude macOS Finder duplicates
|
||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../LICENSE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../CITATION.cff
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/conformallab)
|
||||
|
||||
27662
code/data/obj/brezel.obj
Executable file
27662
code/data/obj/brezel.obj
Executable file
File diff suppressed because it is too large
Load Diff
7874
code/data/obj/brezel2.obj
Normal file
7874
code/data/obj/brezel2.obj
Normal file
File diff suppressed because it is too large
Load Diff
379
code/data/obj/cathead.obj
Normal file
379
code/data/obj/cathead.obj
Normal file
@@ -0,0 +1,379 @@
|
||||
v -0.206972 0.0740737 0.544664
|
||||
v -0.398695 -0.0740794 0.501089
|
||||
v -0.211327 -0.187367 0.588234
|
||||
v -0.511983 -0.235298 0.400872
|
||||
v -0.56863 0.0348535 0.405227
|
||||
v -0.35948 0.178646 0.50545
|
||||
v -0.525054 0.313721 0.387801
|
||||
v -0.189545 0.30065 0.544664
|
||||
v 0.124182 0.0740737 0.509805
|
||||
v -0.455336 -0.33987 0.544664
|
||||
v -0.411766 -0.427021 0.671024
|
||||
v -0.237476 -0.413949 0.745097
|
||||
v -0.250547 -0.535954 0.797389
|
||||
v -0.346403 -0.562097 0.758169
|
||||
v 0.0152491 -0.33116 0.531593
|
||||
v -0.106755 -0.496734 0.692812
|
||||
v -0.254903 -0.692812 0.749458
|
||||
v -0.0631798 -0.601311 0.55338
|
||||
v -0.14597 -0.666669 0.666669
|
||||
v -0.250547 -0.727671 0.583879
|
||||
v -0.198256 -0.714599 0.42266
|
||||
v 0.0283206 -0.514166 0.383445
|
||||
v -0.124182 -0.623099 0.196078
|
||||
v -0.285401 -0.692812 0.413944
|
||||
v -0.333331 -0.596956 0.226582
|
||||
v -0.333331 -0.418305 0.413944
|
||||
v 0.233115 -0.366019 0.313727
|
||||
v 0.215688 -0.156864 0.457519
|
||||
v -0.342048 -0.623099 -0.0915061
|
||||
v -0.708061 -0.431376 0.0697184
|
||||
v -0.647058 -0.501095 -0.0915061
|
||||
v -0.716777 -0.318088 0.169935
|
||||
v -0.699344 -0.143792 0.235292
|
||||
v -0.747275 0.00871052 0.235292
|
||||
v -0.747275 0.165574 0.191723
|
||||
v -0.764708 0.291939 0.12636
|
||||
v -0.747275 0.461874 0.0130715
|
||||
v -0.337692 0.535948 0.366013
|
||||
v -0.472768 0.644881 0.0653574
|
||||
v -0.272329 0.753814 0.0827899
|
||||
v -0.294117 0.797383 0.278868
|
||||
v -0.307188 0.801744 0.435731
|
||||
v -0.324621 0.779957 0.583879
|
||||
v -0.355119 0.714594 0.710238
|
||||
v -0.407405 0.59695 0.692812
|
||||
v -0.442264 0.505444 0.610022
|
||||
v -0.477123 0.413944 0.51416
|
||||
v -0.285401 0.509805 0.666669
|
||||
v -0.185184 0.605661 0.631809
|
||||
v -0.0326816 0.518516 0.479301
|
||||
v -0.198256 0.72331 0.575162
|
||||
v -0.124182 0.745097 0.278868
|
||||
v 0.189545 0.479301 0.357297
|
||||
v -0.0413921 0.675379 0.352942
|
||||
v -0.843136 -0.0392202 -0.0522859
|
||||
v -0.764708 -0.33116 0.0348592
|
||||
v -0.816994 -0.152508 -0.178651
|
||||
v -0.87364 -0.0522916 -0.187362
|
||||
v -0.816994 0.108933 -0.204794
|
||||
v -0.816994 0.25708 -0.00435526
|
||||
v -0.795206 0.387795 -0.0653574
|
||||
v -0.764708 0.440087 -0.152503
|
||||
v -0.642703 0.535948 -0.370368
|
||||
v -0.516338 0.636165 -0.21351
|
||||
v -0.856208 0.265791 -0.148147
|
||||
v -0.816994 0.261435 -0.291939
|
||||
v -0.847497 0.239648 -0.418299
|
||||
v -0.82571 0.265791 -0.479301
|
||||
v -0.912855 -0.0697184 -0.270152
|
||||
v -0.947714 0.0130715 -0.326799
|
||||
v -0.938998 0.0740737 -0.392156
|
||||
v -0.799567 -0.344231 -0.108933
|
||||
v -0.620916 -0.509805 -0.331154
|
||||
v -0.764708 -0.287584 -0.244009
|
||||
v -0.799567 -0.291939 -0.352942
|
||||
v -0.9085 -0.174296 -0.33987
|
||||
v -1 -0.0697184 -0.409588
|
||||
v -0.965141 -0.148153 -0.461874
|
||||
v -0.95643 -0.0610022 -0.549019
|
||||
v -0.978212 0.021782 -0.483662
|
||||
v -0.729848 -0.357302 -0.501089
|
||||
v -0.921571 -0.230937 -0.544664
|
||||
v -0.821354 -0.222227 -0.662308
|
||||
v -0.9085 -0.16558 -0.623093
|
||||
v -0.342048 -0.557736 -0.383445
|
||||
v -0.381262 -0.300656 -0.727671
|
||||
v -0.167757 -0.557736 -0.501089
|
||||
v -0.599128 -0.252725 -0.692812
|
||||
v -0.420482 -0.0697184 -0.797389
|
||||
v -0.14597 -0.610027 -0.631809
|
||||
v -0.947714 0.0915004 -0.522877
|
||||
v -0.891067 0.178646 -0.50545
|
||||
v -0.869279 -0.0697184 -0.705883
|
||||
v -0.934643 0.0348535 -0.627454
|
||||
v -0.847497 0.156858 -0.649236
|
||||
v -0.620916 0.235292 -0.701528
|
||||
v -0.559913 0.143786 -0.745097
|
||||
v -0.315905 0.278862 -0.753814
|
||||
v -0.0196101 0.440087 -0.784312
|
||||
v -0.211327 0.46623 -0.636165
|
||||
v -0.381262 0.505444 -0.579523
|
||||
v -0.647058 0.374724 -0.601305
|
||||
v -0.355119 0.653591 -0.344225
|
||||
v -0.0108939 0.644881 -0.313727
|
||||
v 0.163396 0.64052 0.104578
|
||||
v 0.433554 0.0697127 0.322443
|
||||
v 0.516344 -0.318088 0.0435754
|
||||
v 0.35948 -0.492378 -0.0653574
|
||||
v 0.185184 -0.535954 0.0305039
|
||||
v 0.0631798 -0.618738 -0.313727
|
||||
v 0.328976 -0.618738 -0.296295
|
||||
v 0.102394 -0.684101 -0.43137
|
||||
v 0.612199 -0.396517 -0.16558
|
||||
v 0.307188 0.522877 -0.779957
|
||||
v 0.163396 0.549019 -0.562091
|
||||
v 0.468413 0.400872 0.00871623
|
||||
v 0.599128 0.374724 -0.261435
|
||||
v 0.721132 0.357297 -0.50545
|
||||
v 0.843136 0.326793 -0.718955
|
||||
v 0.124182 -0.801744 -0.562091
|
||||
v 0.35948 -0.749458 -0.448803
|
||||
v 0.559913 0.0479307 0.161219
|
||||
v 0.673201 0.0304982 -0.0566469
|
||||
v 0.808283 0.0174267 -0.309366
|
||||
v 0.891067 -0.00871623 -0.483662
|
||||
v 1 -0.0697184 -0.753814
|
||||
v 0.355119 0.440087 0.191723
|
||||
v 0.694989 -0.58824 -0.440087
|
||||
v 0.886712 -0.230937 -0.457519
|
||||
v 0.912855 -0.366019 -0.575162
|
||||
v -0.921571 -0.126365 -0.300656
|
||||
f 1 2 3
|
||||
f 4 2 5
|
||||
f 2 1 5
|
||||
f 1 6 5
|
||||
f 6 7 5
|
||||
f 7 6 8
|
||||
f 6 1 8
|
||||
f 9 1 3
|
||||
f 3 4 10
|
||||
f 10 11 3
|
||||
f 11 12 3
|
||||
f 11 13 12
|
||||
f 13 11 14
|
||||
f 12 15 3
|
||||
f 15 12 16
|
||||
f 12 13 16
|
||||
f 13 17 16
|
||||
f 17 13 14
|
||||
f 16 18 15
|
||||
f 18 16 19
|
||||
f 16 17 19
|
||||
f 17 20 19
|
||||
f 20 21 19
|
||||
f 21 18 19
|
||||
f 18 21 22
|
||||
f 21 23 22
|
||||
f 21 24 23
|
||||
f 24 21 20
|
||||
f 24 25 23
|
||||
f 25 24 26
|
||||
f 24 20 26
|
||||
f 20 17 26
|
||||
f 17 14 26
|
||||
f 14 11 26
|
||||
f 11 10 26
|
||||
f 10 4 26
|
||||
f 3 15 9
|
||||
f 15 27 28
|
||||
f 23 25 29
|
||||
f 25 30 31
|
||||
f 30 25 32
|
||||
f 25 26 32
|
||||
f 4 33 32
|
||||
f 18 22 15
|
||||
f 22 27 15
|
||||
f 5 34 33
|
||||
f 34 5 35
|
||||
f 5 7 35
|
||||
f 7 36 35
|
||||
f 36 7 37
|
||||
f 7 38 37
|
||||
f 38 39 37
|
||||
f 39 38 40
|
||||
f 38 41 40
|
||||
f 41 38 42
|
||||
f 38 43 42
|
||||
f 43 38 44
|
||||
f 38 45 44
|
||||
f 45 38 46
|
||||
f 46 38 47
|
||||
f 38 7 47
|
||||
f 7 8 47
|
||||
f 8 46 47
|
||||
f 46 8 48
|
||||
f 8 49 48
|
||||
f 49 44 48
|
||||
f 44 45 48
|
||||
f 49 8 50
|
||||
f 49 51 44
|
||||
f 51 43 44
|
||||
f 43 51 42
|
||||
f 51 52 42
|
||||
f 52 41 42
|
||||
f 41 52 40
|
||||
f 53 54 50
|
||||
f 49 54 51
|
||||
f 54 49 50
|
||||
f 51 54 52
|
||||
f 50 9 53
|
||||
f 9 50 8
|
||||
f 55 33 34
|
||||
f 33 55 32
|
||||
f 55 56 32
|
||||
f 56 55 57
|
||||
f 55 58 57
|
||||
f 58 55 59
|
||||
f 55 60 59
|
||||
f 60 55 35
|
||||
f 55 34 35
|
||||
f 36 60 35
|
||||
f 60 36 37
|
||||
f 37 61 60
|
||||
f 61 37 62
|
||||
f 37 63 62
|
||||
f 63 37 64
|
||||
f 37 39 64
|
||||
f 39 40 64
|
||||
f 62 65 61
|
||||
f 65 62 66
|
||||
f 62 67 66
|
||||
f 67 62 68
|
||||
f 62 63 68
|
||||
f 65 59 60
|
||||
f 59 69 58
|
||||
f 69 59 70
|
||||
f 59 71 70
|
||||
f 71 59 67
|
||||
f 59 66 67
|
||||
f 59 65 66
|
||||
f 56 30 32
|
||||
f 30 56 31
|
||||
f 56 72 31
|
||||
f 72 56 57
|
||||
f 60 61 65
|
||||
f 46 48 45
|
||||
f 31 29 25
|
||||
f 29 31 73
|
||||
f 31 74 73
|
||||
f 74 31 72
|
||||
f 57 74 72
|
||||
f 74 57 75
|
||||
f 57 76 75
|
||||
f 57 58 69
|
||||
f 77 78 76
|
||||
f 78 77 79
|
||||
f 77 80 79
|
||||
f 80 77 71
|
||||
f 77 70 71
|
||||
f 70 77 69
|
||||
f 75 73 74
|
||||
f 73 75 81
|
||||
f 75 82 81
|
||||
f 82 75 76
|
||||
f 82 83 81
|
||||
f 83 82 84
|
||||
f 82 79 84
|
||||
f 79 82 78
|
||||
f 82 76 78
|
||||
f 73 85 29
|
||||
f 85 73 86
|
||||
f 73 81 86
|
||||
f 85 87 29
|
||||
f 87 85 86
|
||||
f 88 86 81
|
||||
f 86 88 89
|
||||
f 88 83 89
|
||||
f 83 88 81
|
||||
f 86 90 87
|
||||
f 71 91 80
|
||||
f 91 71 92
|
||||
f 71 67 92
|
||||
f 67 68 92
|
||||
f 79 93 84
|
||||
f 93 79 94
|
||||
f 79 91 94
|
||||
f 91 79 80
|
||||
f 92 95 91
|
||||
f 92 68 95
|
||||
f 91 95 94
|
||||
f 95 93 94
|
||||
f 93 95 96
|
||||
f 95 68 96
|
||||
f 93 83 84
|
||||
f 83 93 89
|
||||
f 93 97 89
|
||||
f 97 93 96
|
||||
f 89 97 98
|
||||
f 98 100 99
|
||||
f 100 98 101
|
||||
f 98 96 101
|
||||
f 98 97 96
|
||||
f 102 68 63
|
||||
f 68 102 96
|
||||
f 102 101 96
|
||||
f 101 102 63
|
||||
f 64 103 63
|
||||
f 103 64 40
|
||||
f 103 101 63
|
||||
f 101 104 100
|
||||
f 104 101 103
|
||||
f 40 104 103
|
||||
f 40 52 104
|
||||
f 52 105 104
|
||||
f 9 106 53
|
||||
f 106 9 28
|
||||
f 106 27 107
|
||||
f 27 106 28
|
||||
f 27 108 107
|
||||
f 108 27 22
|
||||
f 22 109 108
|
||||
f 109 22 23
|
||||
f 109 110 108
|
||||
f 110 109 23
|
||||
f 23 29 110
|
||||
f 110 111 108
|
||||
f 111 110 112
|
||||
f 110 90 112
|
||||
f 90 110 87
|
||||
f 110 29 87
|
||||
f 108 111 113
|
||||
f 108 113 107
|
||||
f 114 99 115
|
||||
f 99 104 115
|
||||
f 104 105 115
|
||||
f 116 117 115
|
||||
f 117 118 115
|
||||
f 115 118 114
|
||||
f 118 119 114
|
||||
f 120 112 90
|
||||
f 112 120 111
|
||||
f 120 121 111
|
||||
f 99 100 104
|
||||
f 2 4 3
|
||||
f 5 33 4
|
||||
f 116 122 123
|
||||
f 123 117 116
|
||||
f 117 123 124
|
||||
f 117 124 118
|
||||
f 124 125 118
|
||||
f 125 119 118
|
||||
f 119 125 126
|
||||
f 122 106 107
|
||||
f 105 116 115
|
||||
f 54 53 105
|
||||
f 52 54 105
|
||||
f 105 127 116
|
||||
f 122 116 127
|
||||
f 106 122 127
|
||||
f 53 127 105
|
||||
f 121 128 113
|
||||
f 121 113 111
|
||||
f 113 128 124
|
||||
f 128 129 124
|
||||
f 129 128 130
|
||||
f 107 113 122
|
||||
f 113 123 122
|
||||
f 123 113 124
|
||||
f 1 9 8
|
||||
f 9 15 28
|
||||
f 4 32 26
|
||||
f 106 127 53
|
||||
f 57 131 76
|
||||
f 131 77 76
|
||||
f 69 131 57
|
||||
f 131 69 77
|
||||
f 129 125 124
|
||||
f 125 130 126
|
||||
f 130 125 129
|
||||
16
code/data/obj/tetraflat.obj
Executable file
16
code/data/obj/tetraflat.obj
Executable file
@@ -0,0 +1,16 @@
|
||||
#
|
||||
# Wavefront OBJ file
|
||||
# Converted by the DEEP Exploration Deep Exploration 5 5.0.3.1555 Release
|
||||
# Right Hemisphere, LTD
|
||||
# http://www.righthemisphere.com/
|
||||
#
|
||||
# object sgc 1
|
||||
g sgc_1
|
||||
v 0.00000 0.00000 0.00000
|
||||
v -1.29492 0.95275 -0.28653
|
||||
v 1.14390 0.47528 1.06408
|
||||
v 0.15103 -1.42803 -0.77755
|
||||
# 4 verticies
|
||||
f 1 2 3
|
||||
f 2 1 4
|
||||
f 1 3 4
|
||||
50
code/data/off/torus_4x4.off
Normal file
50
code/data/off/torus_4x4.off
Normal file
@@ -0,0 +1,50 @@
|
||||
OFF
|
||||
16 32 0
|
||||
3.000000 0.000000 0.000000
|
||||
2.000000 0.000000 1.000000
|
||||
1.000000 0.000000 0.000000
|
||||
2.000000 0.000000 -1.000000
|
||||
0.000000 3.000000 0.000000
|
||||
0.000000 2.000000 1.000000
|
||||
0.000000 1.000000 0.000000
|
||||
0.000000 2.000000 -1.000000
|
||||
-3.000000 0.000000 0.000000
|
||||
-2.000000 0.000000 1.000000
|
||||
-1.000000 0.000000 0.000000
|
||||
-2.000000 0.000000 -1.000000
|
||||
-0.000000 -3.000000 0.000000
|
||||
-0.000000 -2.000000 1.000000
|
||||
-0.000000 -1.000000 0.000000
|
||||
-0.000000 -2.000000 -1.000000
|
||||
3 0 4 1
|
||||
3 1 4 5
|
||||
3 1 5 2
|
||||
3 2 5 6
|
||||
3 2 6 3
|
||||
3 3 6 7
|
||||
3 3 7 0
|
||||
3 0 7 4
|
||||
3 4 8 5
|
||||
3 5 8 9
|
||||
3 5 9 6
|
||||
3 6 9 10
|
||||
3 6 10 7
|
||||
3 7 10 11
|
||||
3 7 11 4
|
||||
3 4 11 8
|
||||
3 8 12 9
|
||||
3 9 12 13
|
||||
3 9 13 10
|
||||
3 10 13 14
|
||||
3 10 14 11
|
||||
3 11 14 15
|
||||
3 11 15 8
|
||||
3 8 15 12
|
||||
3 12 0 13
|
||||
3 13 0 1
|
||||
3 13 1 14
|
||||
3 14 1 2
|
||||
3 14 2 15
|
||||
3 15 2 3
|
||||
3 15 3 12
|
||||
3 12 3 0
|
||||
194
code/data/off/torus_8x8.off
Normal file
194
code/data/off/torus_8x8.off
Normal file
@@ -0,0 +1,194 @@
|
||||
OFF
|
||||
64 128 0
|
||||
4.000000 0.000000 0.000000
|
||||
3.707107 0.000000 0.707107
|
||||
3.000000 0.000000 1.000000
|
||||
2.292893 0.000000 0.707107
|
||||
2.000000 0.000000 0.000000
|
||||
2.292893 0.000000 -0.707107
|
||||
3.000000 0.000000 -1.000000
|
||||
3.707107 0.000000 -0.707107
|
||||
2.828427 2.828427 0.000000
|
||||
2.621320 2.621320 0.707107
|
||||
2.121320 2.121320 1.000000
|
||||
1.621320 1.621320 0.707107
|
||||
1.414214 1.414214 0.000000
|
||||
1.621320 1.621320 -0.707107
|
||||
2.121320 2.121320 -1.000000
|
||||
2.621320 2.621320 -0.707107
|
||||
0.000000 4.000000 0.000000
|
||||
0.000000 3.707107 0.707107
|
||||
0.000000 3.000000 1.000000
|
||||
0.000000 2.292893 0.707107
|
||||
0.000000 2.000000 0.000000
|
||||
0.000000 2.292893 -0.707107
|
||||
0.000000 3.000000 -1.000000
|
||||
0.000000 3.707107 -0.707107
|
||||
-2.828427 2.828427 0.000000
|
||||
-2.621320 2.621320 0.707107
|
||||
-2.121320 2.121320 1.000000
|
||||
-1.621320 1.621320 0.707107
|
||||
-1.414214 1.414214 0.000000
|
||||
-1.621320 1.621320 -0.707107
|
||||
-2.121320 2.121320 -1.000000
|
||||
-2.621320 2.621320 -0.707107
|
||||
-4.000000 0.000000 0.000000
|
||||
-3.707107 0.000000 0.707107
|
||||
-3.000000 0.000000 1.000000
|
||||
-2.292893 0.000000 0.707107
|
||||
-2.000000 0.000000 0.000000
|
||||
-2.292893 0.000000 -0.707107
|
||||
-3.000000 0.000000 -1.000000
|
||||
-3.707107 0.000000 -0.707107
|
||||
-2.828427 -2.828427 0.000000
|
||||
-2.621320 -2.621320 0.707107
|
||||
-2.121320 -2.121320 1.000000
|
||||
-1.621320 -1.621320 0.707107
|
||||
-1.414214 -1.414214 0.000000
|
||||
-1.621320 -1.621320 -0.707107
|
||||
-2.121320 -2.121320 -1.000000
|
||||
-2.621320 -2.621320 -0.707107
|
||||
-0.000000 -4.000000 0.000000
|
||||
-0.000000 -3.707107 0.707107
|
||||
-0.000000 -3.000000 1.000000
|
||||
-0.000000 -2.292893 0.707107
|
||||
-0.000000 -2.000000 0.000000
|
||||
-0.000000 -2.292893 -0.707107
|
||||
-0.000000 -3.000000 -1.000000
|
||||
-0.000000 -3.707107 -0.707107
|
||||
2.828427 -2.828427 0.000000
|
||||
2.621320 -2.621320 0.707107
|
||||
2.121320 -2.121320 1.000000
|
||||
1.621320 -1.621320 0.707107
|
||||
1.414214 -1.414214 0.000000
|
||||
1.621320 -1.621320 -0.707107
|
||||
2.121320 -2.121320 -1.000000
|
||||
2.621320 -2.621320 -0.707107
|
||||
3 0 8 1
|
||||
3 1 8 9
|
||||
3 1 9 2
|
||||
3 2 9 10
|
||||
3 2 10 3
|
||||
3 3 10 11
|
||||
3 3 11 4
|
||||
3 4 11 12
|
||||
3 4 12 5
|
||||
3 5 12 13
|
||||
3 5 13 6
|
||||
3 6 13 14
|
||||
3 6 14 7
|
||||
3 7 14 15
|
||||
3 7 15 0
|
||||
3 0 15 8
|
||||
3 8 16 9
|
||||
3 9 16 17
|
||||
3 9 17 10
|
||||
3 10 17 18
|
||||
3 10 18 11
|
||||
3 11 18 19
|
||||
3 11 19 12
|
||||
3 12 19 20
|
||||
3 12 20 13
|
||||
3 13 20 21
|
||||
3 13 21 14
|
||||
3 14 21 22
|
||||
3 14 22 15
|
||||
3 15 22 23
|
||||
3 15 23 8
|
||||
3 8 23 16
|
||||
3 16 24 17
|
||||
3 17 24 25
|
||||
3 17 25 18
|
||||
3 18 25 26
|
||||
3 18 26 19
|
||||
3 19 26 27
|
||||
3 19 27 20
|
||||
3 20 27 28
|
||||
3 20 28 21
|
||||
3 21 28 29
|
||||
3 21 29 22
|
||||
3 22 29 30
|
||||
3 22 30 23
|
||||
3 23 30 31
|
||||
3 23 31 16
|
||||
3 16 31 24
|
||||
3 24 32 25
|
||||
3 25 32 33
|
||||
3 25 33 26
|
||||
3 26 33 34
|
||||
3 26 34 27
|
||||
3 27 34 35
|
||||
3 27 35 28
|
||||
3 28 35 36
|
||||
3 28 36 29
|
||||
3 29 36 37
|
||||
3 29 37 30
|
||||
3 30 37 38
|
||||
3 30 38 31
|
||||
3 31 38 39
|
||||
3 31 39 24
|
||||
3 24 39 32
|
||||
3 32 40 33
|
||||
3 33 40 41
|
||||
3 33 41 34
|
||||
3 34 41 42
|
||||
3 34 42 35
|
||||
3 35 42 43
|
||||
3 35 43 36
|
||||
3 36 43 44
|
||||
3 36 44 37
|
||||
3 37 44 45
|
||||
3 37 45 38
|
||||
3 38 45 46
|
||||
3 38 46 39
|
||||
3 39 46 47
|
||||
3 39 47 32
|
||||
3 32 47 40
|
||||
3 40 48 41
|
||||
3 41 48 49
|
||||
3 41 49 42
|
||||
3 42 49 50
|
||||
3 42 50 43
|
||||
3 43 50 51
|
||||
3 43 51 44
|
||||
3 44 51 52
|
||||
3 44 52 45
|
||||
3 45 52 53
|
||||
3 45 53 46
|
||||
3 46 53 54
|
||||
3 46 54 47
|
||||
3 47 54 55
|
||||
3 47 55 40
|
||||
3 40 55 48
|
||||
3 48 56 49
|
||||
3 49 56 57
|
||||
3 49 57 50
|
||||
3 50 57 58
|
||||
3 50 58 51
|
||||
3 51 58 59
|
||||
3 51 59 52
|
||||
3 52 59 60
|
||||
3 52 60 53
|
||||
3 53 60 61
|
||||
3 53 61 54
|
||||
3 54 61 62
|
||||
3 54 62 55
|
||||
3 55 62 63
|
||||
3 55 63 48
|
||||
3 48 63 56
|
||||
3 56 0 57
|
||||
3 57 0 1
|
||||
3 57 1 58
|
||||
3 58 1 2
|
||||
3 58 2 59
|
||||
3 59 2 3
|
||||
3 59 3 60
|
||||
3 60 3 4
|
||||
3 60 4 61
|
||||
3 61 4 5
|
||||
3 61 5 62
|
||||
3 62 5 6
|
||||
3 62 6 63
|
||||
3 63 6 7
|
||||
3 63 7 56
|
||||
3 56 7 0
|
||||
110
code/data/off/torus_hex_6x6.off
Normal file
110
code/data/off/torus_hex_6x6.off
Normal file
@@ -0,0 +1,110 @@
|
||||
OFF
|
||||
36 72 0
|
||||
4.000000 0.000000 0.000000
|
||||
3.500000 0.000000 0.866025
|
||||
2.500000 0.000000 0.866025
|
||||
2.000000 0.000000 0.000000
|
||||
2.500000 0.000000 -0.866025
|
||||
3.500000 0.000000 -0.866025
|
||||
2.000000 3.464102 0.000000
|
||||
1.750000 3.031089 0.866025
|
||||
1.250000 2.165064 0.866025
|
||||
1.000000 1.732051 0.000000
|
||||
1.250000 2.165064 -0.866025
|
||||
1.750000 3.031089 -0.866025
|
||||
-2.000000 3.464102 0.000000
|
||||
-1.750000 3.031089 0.866025
|
||||
-1.250000 2.165064 0.866025
|
||||
-1.000000 1.732051 0.000000
|
||||
-1.250000 2.165064 -0.866025
|
||||
-1.750000 3.031089 -0.866025
|
||||
-4.000000 0.000000 0.000000
|
||||
-3.500000 0.000000 0.866025
|
||||
-2.500000 0.000000 0.866025
|
||||
-2.000000 0.000000 0.000000
|
||||
-2.500000 0.000000 -0.866025
|
||||
-3.500000 0.000000 -0.866025
|
||||
-2.000000 -3.464102 0.000000
|
||||
-1.750000 -3.031089 0.866025
|
||||
-1.250000 -2.165064 0.866025
|
||||
-1.000000 -1.732051 0.000000
|
||||
-1.250000 -2.165064 -0.866025
|
||||
-1.750000 -3.031089 -0.866025
|
||||
2.000000 -3.464102 0.000000
|
||||
1.750000 -3.031089 0.866025
|
||||
1.250000 -2.165064 0.866025
|
||||
1.000000 -1.732051 0.000000
|
||||
1.250000 -2.165064 -0.866025
|
||||
1.750000 -3.031089 -0.866025
|
||||
3 0 6 1
|
||||
3 1 6 7
|
||||
3 1 7 2
|
||||
3 2 7 8
|
||||
3 2 8 3
|
||||
3 3 8 9
|
||||
3 3 9 4
|
||||
3 4 9 10
|
||||
3 4 10 5
|
||||
3 5 10 11
|
||||
3 5 11 0
|
||||
3 0 11 6
|
||||
3 6 12 7
|
||||
3 7 12 13
|
||||
3 7 13 8
|
||||
3 8 13 14
|
||||
3 8 14 9
|
||||
3 9 14 15
|
||||
3 9 15 10
|
||||
3 10 15 16
|
||||
3 10 16 11
|
||||
3 11 16 17
|
||||
3 11 17 6
|
||||
3 6 17 12
|
||||
3 12 18 13
|
||||
3 13 18 19
|
||||
3 13 19 14
|
||||
3 14 19 20
|
||||
3 14 20 15
|
||||
3 15 20 21
|
||||
3 15 21 16
|
||||
3 16 21 22
|
||||
3 16 22 17
|
||||
3 17 22 23
|
||||
3 17 23 12
|
||||
3 12 23 18
|
||||
3 18 24 19
|
||||
3 19 24 25
|
||||
3 19 25 20
|
||||
3 20 25 26
|
||||
3 20 26 21
|
||||
3 21 26 27
|
||||
3 21 27 22
|
||||
3 22 27 28
|
||||
3 22 28 23
|
||||
3 23 28 29
|
||||
3 23 29 18
|
||||
3 18 29 24
|
||||
3 24 30 25
|
||||
3 25 30 31
|
||||
3 25 31 26
|
||||
3 26 31 32
|
||||
3 26 32 27
|
||||
3 27 32 33
|
||||
3 27 33 28
|
||||
3 28 33 34
|
||||
3 28 34 29
|
||||
3 29 34 35
|
||||
3 29 35 24
|
||||
3 24 35 30
|
||||
3 30 0 31
|
||||
3 31 0 1
|
||||
3 31 1 32
|
||||
3 32 1 2
|
||||
3 32 2 33
|
||||
3 33 2 3
|
||||
3 33 3 34
|
||||
3 34 3 4
|
||||
3 34 4 35
|
||||
3 35 4 5
|
||||
3 35 5 30
|
||||
3 30 5 0
|
||||
@@ -5,9 +5,10 @@
|
||||
#
|
||||
# Which deps are extracted depends on the active build mode:
|
||||
#
|
||||
# (default) Eigen only → tests-only build
|
||||
# WITH_VIEWER=ON + Eigen, libigl, libigl-glad, glfw
|
||||
# WITH_CGAL=ON + Eigen, CGAL (WITH_VIEWER is implied by WITH_CGAL)
|
||||
# (default) Eigen only
|
||||
# WITH_CGAL_TESTS=ON + Eigen, CGAL (headless CI — no viewer)
|
||||
# WITH_VIEWER=ON + Eigen, libigl, libigl-glad, glfw
|
||||
# WITH_CGAL=ON + Eigen, CGAL, libigl, libigl-glad, glfw
|
||||
#
|
||||
|
||||
function(setup_dependency NAME SUBDIR)
|
||||
@@ -51,6 +52,7 @@ if(WITH_VIEWER)
|
||||
endif()
|
||||
|
||||
# ── CGAL mode ─────────────────────────────────────────────────────────────────
|
||||
if(WITH_CGAL)
|
||||
# Both WITH_CGAL_TESTS (headless CI) and WITH_CGAL (full build) need CGAL headers.
|
||||
if(WITH_CGAL OR WITH_CGAL_TESTS)
|
||||
setup_dependency("CGAL-6.1.1" "include")
|
||||
endif()
|
||||
|
||||
56
code/examples/CMakeLists.txt
Normal file
56
code/examples/CMakeLists.txt
Normal file
@@ -0,0 +1,56 @@
|
||||
# examples/CMakeLists.txt
|
||||
#
|
||||
# Example programs for conformallab++.
|
||||
# All examples require -DWITH_CGAL=ON.
|
||||
# example_viewer additionally requires -DWITH_VIEWER=ON.
|
||||
#
|
||||
# Run after building:
|
||||
# ./build/examples/example_euclidean [input.off] [output.off]
|
||||
# ./build/examples/example_hyper_ideal [input.off] [output.off]
|
||||
# ./build/examples/example_viewer [input.off] (requires WITH_VIEWER)
|
||||
|
||||
# ── Shared include paths for all examples ─────────────────────────────────────
|
||||
set(EXAMPLE_INCLUDES
|
||||
${CMAKE_SOURCE_DIR}/deps/eigen-3.4.0
|
||||
${CMAKE_SOURCE_DIR}/deps/CGAL-6.1.1/include
|
||||
${CMAKE_SOURCE_DIR}/deps/single_includes
|
||||
${Boost_INCLUDE_DIRS}
|
||||
)
|
||||
set(EXAMPLE_PRIVATE_INCLUDES
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
set(EXAMPLE_DEFS
|
||||
CGAL_DISABLE_GMP
|
||||
CGAL_DISABLE_MPFR
|
||||
)
|
||||
|
||||
# ── example_euclidean ─────────────────────────────────────────────────────────
|
||||
add_executable(example_euclidean example_euclidean.cpp)
|
||||
target_include_directories(example_euclidean SYSTEM PRIVATE ${EXAMPLE_INCLUDES})
|
||||
target_include_directories(example_euclidean PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
|
||||
target_compile_definitions(example_euclidean PRIVATE ${EXAMPLE_DEFS})
|
||||
|
||||
# ── example_hyper_ideal ───────────────────────────────────────────────────────
|
||||
add_executable(example_hyper_ideal example_hyper_ideal.cpp)
|
||||
target_include_directories(example_hyper_ideal SYSTEM PRIVATE ${EXAMPLE_INCLUDES})
|
||||
target_include_directories(example_hyper_ideal PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
|
||||
target_compile_definitions(example_hyper_ideal PRIVATE ${EXAMPLE_DEFS})
|
||||
|
||||
# ── example_layout ───────────────────────────────────────────────────────────
|
||||
add_executable(example_layout example_layout.cpp)
|
||||
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_viewer (requires WITH_VIEWER) ─────────────────────────────────────
|
||||
if(WITH_VIEWER)
|
||||
add_executable(example_viewer example_viewer.cpp)
|
||||
target_include_directories(example_viewer SYSTEM PRIVATE
|
||||
${EXAMPLE_INCLUDES}
|
||||
${CMAKE_SOURCE_DIR}/deps/libigl-2.6.0/include
|
||||
${CMAKE_SOURCE_DIR}/deps/libigl-glad/include
|
||||
)
|
||||
target_include_directories(example_viewer PRIVATE ${EXAMPLE_PRIVATE_INCLUDES})
|
||||
target_compile_definitions(example_viewer PRIVATE ${EXAMPLE_DEFS})
|
||||
target_link_libraries(example_viewer PRIVATE viewer)
|
||||
endif()
|
||||
117
code/examples/example_euclidean.cpp
Normal file
117
code/examples/example_euclidean.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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) ──────────────────────────
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v_pinned = *vit++;
|
||||
maps.v_idx[v_pinned] = -1; // pinned: u[v_pinned] = 0 (fixed)
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
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).
|
||||
{
|
||||
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;
|
||||
}
|
||||
147
code/examples/example_hyper_ideal.cpp
Normal file
147
code/examples/example_hyper_ideal.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
// example_hyper_ideal.cpp
|
||||
//
|
||||
// conformallab++ — Hyper-ideal discrete conformal map (headless example)
|
||||
//
|
||||
// Demonstrates the full library pipeline for the HYPER-IDEAL discrete conformal
|
||||
// functional (Springborn 2020). The hyper-ideal functional operates in
|
||||
// hyperbolic geometry: vertices have "horoball radii" (DOF b_i) and edges have
|
||||
// "intersection lengths" (DOF a_e). The energy is strictly convex, so Newton
|
||||
// converges globally from any valid starting point.
|
||||
//
|
||||
// Pipeline:
|
||||
// 1. Load (or synthesise) a triangle mesh
|
||||
// 2. Set up HyperIdeal maps + assign all vertex and edge DOFs
|
||||
// 3. Choose equilibrium base point (b=1.0, a=0.5) and set natural targets
|
||||
// 4. Perturb and solve with Newton
|
||||
// 5. Print DOF values at equilibrium
|
||||
// 6. Save result mesh
|
||||
//
|
||||
// Build (requires -DWITH_CGAL=ON):
|
||||
// cmake -S code -B build -DWITH_CGAL=ON
|
||||
// cmake --build build --target example_hyper_ideal
|
||||
// ./build/examples/example_hyper_ideal [input.off] [output.off]
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
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_hyper_ideal_out.off";
|
||||
|
||||
if (input_path.empty()) {
|
||||
std::cout << "[example_hyper_ideal] No input file — using make_triangle().\n";
|
||||
mesh = make_triangle();
|
||||
} else {
|
||||
std::cout << "[example_hyper_ideal] 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_hyper_ideal] Mesh: "
|
||||
<< mesh.number_of_vertices() << " vertices, "
|
||||
<< mesh.number_of_faces() << " faces.\n";
|
||||
|
||||
// ── Step 2: set up functional maps ────────────────────────────────────
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
std::cout << "[example_hyper_ideal] DOFs: " << n
|
||||
<< " (" << mesh.number_of_vertices() << " vertex + "
|
||||
<< mesh.number_of_edges() << " edge).\n";
|
||||
|
||||
// ── Step 3: choose equilibrium base point and set natural targets ─────
|
||||
//
|
||||
// x = 0 is degenerate for the HyperIdeal functional (log-space).
|
||||
// We pick a valid base point (b_i = b_base, a_e = a_base), evaluate
|
||||
// the gradient there, and absorb it into the target angles so that
|
||||
// G(xbase) = 0. This makes xbase the equilibrium x*.
|
||||
//
|
||||
// In a real application you would set theta_v / theta_e to the desired
|
||||
// hyperbolic angle targets (e.g. from a reference mesh).
|
||||
const double b_base = 1.0; // horoball radii at equilibrium
|
||||
const double a_base = 0.5; // edge-length DOFs at equilibrium
|
||||
|
||||
const auto sz = static_cast<std::size_t>(n);
|
||||
std::vector<double> xbase(sz, 0.0);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) xbase[static_cast<std::size_t>(iv)] = b_base;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) xbase[static_cast<std::size_t>(ie)] = a_base;
|
||||
}
|
||||
|
||||
// G = Σβ − theta_target; absorb G(xbase) into targets so G(xbase) = 0
|
||||
auto G0 = evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
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)];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) maps.theta_e[e] += G0[static_cast<std::size_t>(ie)];
|
||||
}
|
||||
|
||||
// ── Step 4: perturb and solve ─────────────────────────────────────────
|
||||
const double perturb = 0.25;
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += perturb;
|
||||
|
||||
double g_start = 0.0;
|
||||
for (double v : G0) g_start = std::max(g_start, std::abs(v));
|
||||
std::cout << "[example_hyper_ideal] Starting Newton from perturbation +" << perturb
|
||||
<< " (G at xbase = " << g_start << ").\n";
|
||||
|
||||
auto result = newton_hyper_ideal(mesh, x0, maps, /*tol=*/1e-9, /*max_iter=*/200);
|
||||
|
||||
// ── Step 5: report ────────────────────────────────────────────────────
|
||||
if (result.converged) {
|
||||
std::cout << "[example_hyper_ideal] Converged in " << result.iterations
|
||||
<< " iterations. ||G||_inf = " << result.grad_inf_norm << "\n";
|
||||
} else {
|
||||
std::cout << "[example_hyper_ideal] Did NOT converge after " << result.iterations
|
||||
<< " iterations. ||G||_inf = " << result.grad_inf_norm << "\n";
|
||||
}
|
||||
|
||||
std::cout << "[example_hyper_ideal] DOF values at equilibrium:\n";
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
std::cout << " v" << v
|
||||
<< " b = " << result.x[static_cast<std::size_t>(iv)]
|
||||
<< " (expected " << b_base << ")\n";
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
std::cout << " e" << e
|
||||
<< " a = " << result.x[static_cast<std::size_t>(ie)]
|
||||
<< " (expected " << a_base << ")\n";
|
||||
}
|
||||
|
||||
// ── Step 6: write output mesh ─────────────────────────────────────────
|
||||
try {
|
||||
save_mesh(output_path, mesh);
|
||||
std::cout << "[example_hyper_ideal] 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;
|
||||
}
|
||||
168
code/examples/example_layout.cpp
Normal file
168
code/examples/example_layout.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
// example_layout.cpp
|
||||
//
|
||||
// Phase 5 example — Euclidean conformal layout with JSON/XML serialisation.
|
||||
//
|
||||
// Demonstrates the full pipeline that a library user would run:
|
||||
//
|
||||
// 1. Build (or load) a mesh.
|
||||
// 2. Set up Euclidean conformal maps + compute λ° from vertex positions.
|
||||
// 3. Choose natural target angles (→ x* = 0 is the equilibrium).
|
||||
// 4. Run Newton's method.
|
||||
// 5. Unfold the mesh in the plane (euclidean_layout).
|
||||
// 6. Save the layout as an OFF file for inspection in MeshLab/Blender.
|
||||
// 7. Serialise the solver result and UV coordinates to JSON and XML.
|
||||
// 8. Reload from JSON and verify the DOF vector is recovered.
|
||||
//
|
||||
// Build (from code/):
|
||||
// cmake -S . -B build -DWITH_CGAL=ON && cmake --build build -t example_layout
|
||||
//
|
||||
// Run:
|
||||
// ./build/examples/example_layout
|
||||
// ./build/examples/example_layout input.off layout.off result.json result.xml
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "serialization.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ── Helper: natural target angles so x* = 0 is the equilibrium ───────────────
|
||||
static void set_natural_theta(ConformalMesh& mesh, EuclideanMaps& maps, int n)
|
||||
{
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto G = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] -= G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helper: pin vertex 0, assign DOF indices 0..n-1 to the rest ──────────────
|
||||
static int pin_first(ConformalMesh& mesh, EuclideanMaps& maps)
|
||||
{
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1; // pinned
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// ── Main ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// ── Step 1: mesh ──────────────────────────────────────────────────────────
|
||||
ConformalMesh mesh;
|
||||
std::string out_off = "layout.off";
|
||||
std::string out_json = "result.json";
|
||||
std::string out_xml = "result.xml";
|
||||
|
||||
if (argc >= 2) {
|
||||
// User provided an input mesh
|
||||
if (!read_mesh(argv[1], mesh) || mesh.is_empty()) {
|
||||
std::cerr << "Cannot load " << argv[1] << "\n";
|
||||
return 1;
|
||||
}
|
||||
if (argc >= 3) out_off = argv[2];
|
||||
if (argc >= 4) out_json = argv[3];
|
||||
if (argc >= 5) out_xml = argv[4];
|
||||
} else {
|
||||
// Use built-in quad-strip (6 vertices, 4 triangles)
|
||||
mesh = make_quad_strip();
|
||||
std::cout << "Using built-in quad-strip mesh (no arguments given).\n";
|
||||
}
|
||||
|
||||
std::cout << "Mesh: " << mesh.number_of_vertices() << " vertices, "
|
||||
<< mesh.number_of_faces() << " faces\n";
|
||||
|
||||
// ── Step 2: maps + λ° ────────────────────────────────────────────────────
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// ── Step 3: DOF assignment + natural targets ──────────────────────────────
|
||||
int n = pin_first(mesh, maps);
|
||||
std::cout << "DOFs: " << n << " (vertex 0 pinned)\n";
|
||||
|
||||
set_natural_theta(mesh, maps, n);
|
||||
|
||||
// ── Step 4: Newton ────────────────────────────────────────────────────────
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto res = newton_euclidean(mesh, x0, maps);
|
||||
|
||||
std::cout << std::boolalpha
|
||||
<< "Newton converged: " << res.converged
|
||||
<< " iterations: " << res.iterations
|
||||
<< " |grad|_inf: "
|
||||
<< std::scientific << std::setprecision(3) << res.grad_inf_norm << "\n";
|
||||
|
||||
// Print scale factors u_v
|
||||
std::cout << "Conformal factors u_v:\n";
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
double u = (iv >= 0) ? res.x[static_cast<std::size_t>(iv)] : 0.0;
|
||||
std::cout << " v" << v.idx() << ": u = " << std::fixed << std::setprecision(8) << u << "\n";
|
||||
}
|
||||
|
||||
// ── Step 5: Layout ────────────────────────────────────────────────────────
|
||||
auto layout = euclidean_layout(mesh, res.x, maps);
|
||||
|
||||
std::cout << "Layout success: " << layout.success
|
||||
<< " has_seam: " << layout.has_seam << "\n";
|
||||
|
||||
if (layout.success) {
|
||||
std::cout << "UV coordinates:\n";
|
||||
for (auto v : mesh.vertices()) {
|
||||
auto& p = layout.uv[v.idx()];
|
||||
std::cout << " v" << v.idx()
|
||||
<< ": (" << std::fixed << std::setprecision(6)
|
||||
<< p.x() << ", " << p.y() << ")\n";
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 6: Save layout OFF ───────────────────────────────────────────────
|
||||
save_layout_off(out_off, mesh, layout);
|
||||
std::cout << "Layout saved → " << out_off << "\n";
|
||||
|
||||
// ── Step 7: Serialise JSON + XML ──────────────────────────────────────────
|
||||
save_result_json(out_json, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
std::cout << "JSON saved → " << out_json << "\n";
|
||||
|
||||
save_result_xml(out_xml, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
std::cout << "XML saved → " << out_xml << "\n";
|
||||
|
||||
// ── Step 8: JSON round-trip verification ──────────────────────────────────
|
||||
NewtonResult res2;
|
||||
std::string geom;
|
||||
Layout2D layout2;
|
||||
auto x2 = load_result_json(out_json, &res2, &geom, &layout2);
|
||||
|
||||
std::cout << "Round-trip: geometry=\"" << geom << "\" "
|
||||
<< "DOFs=" << x2.size() << " "
|
||||
<< "converged=" << res2.converged << "\n";
|
||||
|
||||
// Verify the DOF vector survived the round-trip
|
||||
bool ok = (x2.size() == res.x.size());
|
||||
for (std::size_t i = 0; i < x2.size() && ok; ++i)
|
||||
ok = (std::abs(x2[i] - res.x[i]) < 1e-10);
|
||||
std::cout << "DOF vector round-trip: " << (ok ? "OK ✓" : "MISMATCH ✗") << "\n";
|
||||
|
||||
return res.converged ? 0 : 1;
|
||||
}
|
||||
150
code/examples/example_viewer.cpp
Normal file
150
code/examples/example_viewer.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
// example_viewer.cpp
|
||||
//
|
||||
// conformallab++ — Interactive viewer example (requires -DWITH_VIEWER=ON)
|
||||
//
|
||||
// This example demonstrates the full end-to-end pipeline WITH visual output:
|
||||
//
|
||||
// 1. Load (or synthesise) a mesh
|
||||
// 2. Compute the Euclidean discrete conformal map (Newton solver)
|
||||
// 3. Display the result in an interactive libigl / GLFW window
|
||||
// • Left pane: input mesh, coloured by per-vertex conformal factor u_i
|
||||
// • Right pane: a flat parameterisation (future, placeholder)
|
||||
//
|
||||
// The viewer is split into two data sets using libigl's multi-mesh API so
|
||||
// users can inspect geometry and solution simultaneously.
|
||||
//
|
||||
// Build (requires -DWITH_CGAL=ON -DWITH_VIEWER=ON):
|
||||
// cmake -S code -B build -DWITH_CGAL=ON -DWITH_VIEWER=ON
|
||||
// cmake --build build --target example_viewer
|
||||
// ./build/examples/example_viewer [input.off]
|
||||
//
|
||||
// Navigation (libigl default):
|
||||
// Mouse drag — rotate
|
||||
// Scroll — zoom
|
||||
// C — toggle camera mode (trackball / 2D)
|
||||
// Z / X / Y — snap to axis-aligned view
|
||||
// Q / Esc — quit
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "mesh_utils.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include <igl/opengl/glfw/Viewer.h>
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ── Jet colour map: scalar → RGB ─────────────────────────────────────────────
|
||||
static Eigen::RowVector3d jet(double t)
|
||||
{
|
||||
t = std::max(0.0, std::min(1.0, t));
|
||||
double r = std::clamp(1.5 - std::abs(4.0 * t - 3.0), 0.0, 1.0);
|
||||
double g = std::clamp(1.5 - std::abs(4.0 * t - 2.0), 0.0, 1.0);
|
||||
double b = std::clamp(1.5 - std::abs(4.0 * t - 1.0), 0.0, 1.0);
|
||||
return {r, g, b};
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// ── Step 1: load or synthesise mesh ───────────────────────────────────
|
||||
ConformalMesh mesh;
|
||||
std::string input_path = (argc > 1) ? argv[1] : "";
|
||||
|
||||
if (input_path.empty()) {
|
||||
std::cout << "[example_viewer] No input file — using make_quad_strip().\n";
|
||||
mesh = make_quad_strip();
|
||||
} else {
|
||||
std::cout << "[example_viewer] Loading: " << input_path << "\n";
|
||||
try { mesh = load_mesh(input_path); }
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "[example_viewer] Mesh: "
|
||||
<< mesh.number_of_vertices() << " vertices, "
|
||||
<< mesh.number_of_faces() << " faces.\n";
|
||||
|
||||
// ── Step 2: solve the Euclidean discrete conformal map ────────────────
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin first vertex
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
// Natural equilibrium
|
||||
{
|
||||
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)];
|
||||
}
|
||||
}
|
||||
|
||||
// Perturb and solve
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.08);
|
||||
auto result = newton_euclidean(mesh, x0, maps, 1e-9, 200);
|
||||
|
||||
if (result.converged)
|
||||
std::cout << "[example_viewer] Converged in " << result.iterations << " iterations.\n";
|
||||
else
|
||||
std::cout << "[example_viewer] Warning: did not converge fully "
|
||||
"(||G||_inf = " << result.grad_inf_norm << ").\n";
|
||||
|
||||
// ── Step 3: build Eigen V / F for libigl ─────────────────────────────
|
||||
using Kernel = CGAL::Simple_cartesian<double>;
|
||||
Eigen::MatrixXd V;
|
||||
Eigen::MatrixXi F;
|
||||
mesh_utils::cgal_to_eigen<Kernel>(mesh, V, F);
|
||||
|
||||
// Per-vertex colour: conformal factor u_i, mapped via jet palette
|
||||
const int nv = static_cast<int>(V.rows());
|
||||
Eigen::MatrixXd C(nv, 3);
|
||||
|
||||
// Collect all u values to normalise
|
||||
std::vector<double> u_all(static_cast<std::size_t>(nv), 0.0);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0)
|
||||
u_all[static_cast<std::size_t>(v.idx())] = result.x[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
double u_min = *std::min_element(u_all.begin(), u_all.end());
|
||||
double u_max = *std::max_element(u_all.begin(), u_all.end());
|
||||
double u_range = (u_max > u_min) ? (u_max - u_min) : 1.0;
|
||||
|
||||
for (int vi = 0; vi < nv; ++vi) {
|
||||
double t = (u_all[static_cast<std::size_t>(vi)] - u_min) / u_range;
|
||||
C.row(vi) = jet(t);
|
||||
}
|
||||
|
||||
// ── Step 4: launch interactive viewer ─────────────────────────────────
|
||||
igl::opengl::glfw::Viewer viewer;
|
||||
viewer.data().set_mesh(V, F);
|
||||
viewer.data().set_colors(C);
|
||||
viewer.data().show_lines = true;
|
||||
viewer.data().show_overlay = true;
|
||||
|
||||
// Status text overlay
|
||||
viewer.data().add_label(
|
||||
Eigen::Vector3d(V.col(0).mean(), V.col(1).mean(), V.col(2).maxCoeff()),
|
||||
"Euclidean conformal factor u_i (jet: blue=min, red=max)");
|
||||
|
||||
std::cout << "[example_viewer] Launching viewer. Press Q or Esc to quit.\n";
|
||||
viewer.launch();
|
||||
|
||||
return 0;
|
||||
}
|
||||
94
code/include/conformal_mesh.hpp
Normal file
94
code/include/conformal_mesh.hpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
// conformal_mesh.hpp
|
||||
//
|
||||
// Central mesh type for the discrete conformal mapping algorithms.
|
||||
// Replaces the Java CoHDS (de.varylab.discreteconformal.heds.CoHDS)
|
||||
// and its associated vertex/edge/face types (CoVertex, CoEdge, CoFace).
|
||||
//
|
||||
// Design
|
||||
// ------
|
||||
// Java │ C++ (this file)
|
||||
// ─────────────────────────────┼──────────────────────────────────────────
|
||||
// CoHDS │ ConformalMesh (CGAL::Surface_mesh)
|
||||
// CoVertex / CoEdge / CoFace │ Vertex_index / Edge_index / Face_index
|
||||
// HyperIdealRadiusAdapter │ property_map<Vertex_index, double>
|
||||
// HalfedgeInterface adapters │ named property maps ("v:lambda", …)
|
||||
//
|
||||
// Property-map naming convention
|
||||
// ───────────────────────────────
|
||||
// "v:lambda" per-vertex log scale factor (conformal variable u_i)
|
||||
// "v:theta" per-vertex target cone angle
|
||||
// "v:idx" per-vertex solver DOF index (-1 = pinned / boundary)
|
||||
// "e:alpha" per-edge intersection angle (α_ij, hyperbolic geometry)
|
||||
// "f:type" per-face geometry type (0=Euclidean, 1=Hyperbolic, 2=Spherical)
|
||||
//
|
||||
// All property maps are optional; add only what a given algorithm needs.
|
||||
//
|
||||
// Note on descriptor types (CGAL 6.x)
|
||||
// ────────────────────────────────────
|
||||
// CGAL::Surface_mesh exposes its index types as nested types:
|
||||
// Surface_mesh::Vertex_index, ::Halfedge_index, ::Edge_index, ::Face_index
|
||||
// The BGL graph_traits aliases expose the same types as vertex_descriptor etc.,
|
||||
// but those live in boost::graph_traits<Surface_mesh>, not in Surface_mesh itself.
|
||||
// We use the Surface_mesh member names throughout for clarity.
|
||||
|
||||
#include <CGAL/Simple_cartesian.h>
|
||||
#include <CGAL/Surface_mesh.h>
|
||||
#include <string>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Kernel ──────────────────────────────────────────────────────────────────
|
||||
// Simple double-precision Cartesian. Conformal mapping algorithms never
|
||||
// need exact arithmetic — they operate on floating-point lengths and angles.
|
||||
using Kernel = CGAL::Simple_cartesian<double>;
|
||||
using Point3 = Kernel::Point_3;
|
||||
using Point2 = Kernel::Point_2;
|
||||
|
||||
// ── Mesh type ────────────────────────────────────────────────────────────────
|
||||
using ConformalMesh = CGAL::Surface_mesh<Point3>;
|
||||
|
||||
// ── Index/descriptor aliases (CGAL 6.x naming) ───────────────────────────────
|
||||
using Vertex_index = ConformalMesh::Vertex_index;
|
||||
using Halfedge_index = ConformalMesh::Halfedge_index;
|
||||
using Edge_index = ConformalMesh::Edge_index;
|
||||
using Face_index = ConformalMesh::Face_index;
|
||||
|
||||
// ── Geometry type constant (replaces Java CoFace.type enum) ─────────────────
|
||||
enum class GeometryType : int {
|
||||
Euclidean = 0,
|
||||
Hyperbolic = 1,
|
||||
Spherical = 2
|
||||
};
|
||||
|
||||
// ── Standard property-map bundles ────────────────────────────────────────────
|
||||
|
||||
// Add the vertex properties used by all conformal-map functionals.
|
||||
// Returns {lambda, theta, idx}.
|
||||
inline auto add_vertex_properties(ConformalMesh& mesh)
|
||||
{
|
||||
auto [lambda, ok1] = mesh.add_property_map<Vertex_index, double>("v:lambda", 0.0);
|
||||
auto [theta, ok2] = mesh.add_property_map<Vertex_index, double>("v:theta", 0.0);
|
||||
auto [idx, ok3] = mesh.add_property_map<Vertex_index, int> ("v:idx", -1);
|
||||
(void)ok1; (void)ok2; (void)ok3;
|
||||
return std::make_tuple(lambda, theta, idx);
|
||||
}
|
||||
|
||||
// Add the edge intersection-angle property used by the hyperbolic functional.
|
||||
inline auto add_edge_properties(ConformalMesh& mesh)
|
||||
{
|
||||
auto [alpha, ok] = mesh.add_property_map<Edge_index, double>("e:alpha", 0.0);
|
||||
(void)ok;
|
||||
return alpha;
|
||||
}
|
||||
|
||||
// Add the face geometry-type property.
|
||||
inline auto add_face_properties(ConformalMesh& mesh)
|
||||
{
|
||||
auto [ftype, ok] = mesh.add_property_map<Face_index, int>(
|
||||
"f:type", static_cast<int>(GeometryType::Euclidean));
|
||||
(void)ok;
|
||||
return ftype;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
17
code/include/constants.hpp
Normal file
17
code/include/constants.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
// constants.hpp
|
||||
//
|
||||
// Single source of truth for mathematical constants used throughout
|
||||
// conformallab++. Include this header instead of defining π locally.
|
||||
//
|
||||
// All constants are in the conformallab namespace and are constexpr double.
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
/// π to 30 significant digits (well beyond double precision of ~15-16 digits).
|
||||
constexpr double PI = 3.14159265358979323846264338328;
|
||||
|
||||
/// 2π (full turn).
|
||||
constexpr double TWO_PI = 2.0 * PI;
|
||||
|
||||
} // namespace conformallab
|
||||
152
code/include/cut_graph.hpp
Normal file
152
code/include/cut_graph.hpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
// cut_graph.hpp
|
||||
//
|
||||
// Phase 6 — Tree-cotree algorithm for computing a cut graph of a triangulated
|
||||
// surface.
|
||||
//
|
||||
// For a closed, orientable, genus-g surface:
|
||||
// #vertices (V), #edges (E), #faces (F)
|
||||
// Euler: V − E + F = 2 − 2g
|
||||
// Primal spanning tree: V − 1 edges
|
||||
// Dual spanning tree: F − 1 edges (avoiding duals of tree edges)
|
||||
// Remaining: E − (V−1) − (F−1) = 2g cut edges
|
||||
//
|
||||
// These 2g cut edges generate H₁(M, ℤ) ≅ ℤ^{2g}.
|
||||
// Cutting along them turns M into a topological disk.
|
||||
//
|
||||
// For open meshes (boundary present) the algorithm still works: the dual BFS
|
||||
// starts from a boundary-adjacent face, and boundary half-edges are skipped.
|
||||
// The number of cut edges will be E − (V−1) − (F−1) − B where B counts
|
||||
// boundary edges treated as dual tree edges.
|
||||
//
|
||||
// Usage:
|
||||
// CutGraph cg = compute_cut_graph(mesh);
|
||||
// // cg.cut_edge_flags[e.idx()] == true → treat edge as seam in BFS
|
||||
// euclidean_layout(mesh, x, maps, &cg); // layout with holonomy tracking
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "gauss_bonnet.hpp" // for euler_characteristic / genus
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <cstddef>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// CutGraph
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
struct CutGraph {
|
||||
/// cut_edge_flags[e.idx()] = true ↔ this edge is a cut edge.
|
||||
/// Size = mesh.number_of_edges().
|
||||
std::vector<bool> cut_edge_flags;
|
||||
|
||||
/// Indices of the 2g cut edges in order (size = 2g).
|
||||
std::vector<std::size_t> cut_edge_indices;
|
||||
|
||||
/// Genus of the surface (0 for topological spheres and open patches).
|
||||
int genus = 0;
|
||||
|
||||
bool is_cut(Edge_index e) const
|
||||
{
|
||||
return static_cast<std::size_t>(e.idx()) < cut_edge_flags.size()
|
||||
&& cut_edge_flags[static_cast<std::size_t>(e.idx())];
|
||||
}
|
||||
};
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// compute_cut_graph
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// Implements the standard tree-cotree algorithm (Erickson–Whittlesey 2005):
|
||||
//
|
||||
// Step 1: BFS primal spanning tree T (V−1 primal tree edges).
|
||||
// Step 2: BFS dual spanning tree T* (F−1 dual/primal edges, avoiding
|
||||
// edges whose primal crosses T).
|
||||
// Step 3: cut edges = primal edges neither in T nor "used" by T*.
|
||||
|
||||
inline CutGraph compute_cut_graph(const ConformalMesh& mesh)
|
||||
{
|
||||
const std::size_t nv = mesh.number_of_vertices();
|
||||
const std::size_t ne = mesh.number_of_edges();
|
||||
const std::size_t nf = mesh.number_of_faces();
|
||||
|
||||
CutGraph cg;
|
||||
cg.cut_edge_flags.assign(ne, false);
|
||||
cg.genus = conformallab::genus(mesh);
|
||||
|
||||
if (nv == 0 || nf == 0) return cg;
|
||||
|
||||
// ── Step 1: primal spanning tree via BFS from vertex 0 ───────────────────
|
||||
std::vector<bool> tree_edge(ne, false);
|
||||
std::vector<bool> v_visited(nv, false);
|
||||
|
||||
{
|
||||
std::queue<Vertex_index> q;
|
||||
auto v0 = *mesh.vertices().begin();
|
||||
v_visited[v0.idx()] = true;
|
||||
q.push(v0);
|
||||
while (!q.empty()) {
|
||||
Vertex_index v = q.front(); q.pop();
|
||||
for (Halfedge_index h : CGAL::halfedges_around_target(v, mesh)) {
|
||||
Vertex_index u = mesh.source(h);
|
||||
if (!v_visited[static_cast<std::size_t>(u.idx())]) {
|
||||
v_visited[static_cast<std::size_t>(u.idx())] = true;
|
||||
tree_edge[static_cast<std::size_t>(mesh.edge(h).idx())] = true;
|
||||
q.push(u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 2: dual spanning tree via BFS from face 0 ───────────────────────
|
||||
// Dual edge between face f and face f_adj crosses primal edge e.
|
||||
// Include dual edge only if:
|
||||
// (a) e is not a primal tree edge (tree_edge[e] == false)
|
||||
// (b) h_adj is not a border halfedge
|
||||
std::vector<bool> dual_tree_edge(ne, false);
|
||||
std::vector<bool> f_visited(nf, false);
|
||||
|
||||
{
|
||||
std::queue<Face_index> q;
|
||||
auto f0 = *mesh.faces().begin();
|
||||
f_visited[static_cast<std::size_t>(f0.idx())] = true;
|
||||
q.push(f0);
|
||||
while (!q.empty()) {
|
||||
Face_index f = q.front(); q.pop();
|
||||
for (Halfedge_index h :
|
||||
CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
{
|
||||
Halfedge_index h_opp = mesh.opposite(h);
|
||||
if (mesh.is_border(h_opp)) continue; // boundary edge
|
||||
Face_index f_adj = mesh.face(h_opp);
|
||||
if (f_visited[static_cast<std::size_t>(f_adj.idx())]) continue;
|
||||
|
||||
std::size_t eidx = static_cast<std::size_t>(mesh.edge(h).idx());
|
||||
if (!tree_edge[eidx]) {
|
||||
// Use this dual edge in T*
|
||||
f_visited[static_cast<std::size_t>(f_adj.idx())] = true;
|
||||
dual_tree_edge[eidx] = true;
|
||||
q.push(f_adj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 3: cut edges = neither in T nor in T* nor on boundary ───────────
|
||||
// Boundary edges are adjacent to the "outer face" and need no cutting —
|
||||
// they are implicitly handled by the boundary itself.
|
||||
for (Edge_index e : mesh.edges()) {
|
||||
std::size_t idx = static_cast<std::size_t>(e.idx());
|
||||
if (tree_edge[idx] || dual_tree_edge[idx]) continue;
|
||||
// Skip boundary edges — they are not interior homological cycles.
|
||||
Halfedge_index h = mesh.halfedge(e);
|
||||
if (mesh.is_border(h) || mesh.is_border(mesh.opposite(h))) continue;
|
||||
cg.cut_edge_flags[idx] = true;
|
||||
cg.cut_edge_indices.push_back(idx);
|
||||
}
|
||||
|
||||
return cg;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
43
code/include/discrete_elliptic_utility.hpp
Normal file
43
code/include/discrete_elliptic_utility.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
// Ported from de.varylab.discreteconformal.util.DiscreteEllipticUtility (Java).
|
||||
// Only the pure-math subset (no HDS required).
|
||||
|
||||
#include <complex>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// Move tau into the fundamental domain of the modular group SL(2,Z):
|
||||
// |Re(tau)| <= 0.5, Im(tau) >= 0, Re(tau) >= 0, |tau| >= 1
|
||||
//
|
||||
// Algorithm: iteratively apply
|
||||
// 1. T-shift: Re > 0.5 or Re < 0 → Re -= sign(Re)
|
||||
// 2. Im-flip: Im < 0 → Im = -Im
|
||||
// 3. Re-flip: Re < 0 → Re = -Re
|
||||
// 4. S-invert: |tau| < 1 → tau = 1/tau
|
||||
//
|
||||
// Corresponds to Java DiscreteEllipticUtility.normalizeModulus(Complex).
|
||||
inline std::complex<double> normalizeModulus(std::complex<double> tau) {
|
||||
int maxIter = 100;
|
||||
while (--maxIter > 0) {
|
||||
double re = tau.real();
|
||||
double im = tau.imag();
|
||||
// exit when all conditions satisfied
|
||||
if (std::abs(re) <= 0.5 && im >= 0.0 && re >= 0.0 && std::abs(tau) >= 1.0)
|
||||
break;
|
||||
|
||||
if (std::abs(re) > 0.5)
|
||||
re -= (re > 0.0 ? 1.0 : -1.0); // signum shift
|
||||
if (im < 0.0)
|
||||
im = -im;
|
||||
if (re < 0.0)
|
||||
re = -re;
|
||||
tau = std::complex<double>(re, im);
|
||||
if (std::abs(tau) < 1.0)
|
||||
tau = 1.0 / tau; // S-transformation: invert
|
||||
}
|
||||
return tau;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
323
code/include/euclidean_functional.hpp
Normal file
323
code/include/euclidean_functional.hpp
Normal file
@@ -0,0 +1,323 @@
|
||||
#pragma once
|
||||
// euclidean_functional.hpp
|
||||
//
|
||||
// Energy and gradient of the Euclidean discrete conformal functional
|
||||
// (EuclideanCyclicFunctional) evaluated on a ConformalMesh.
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.EuclideanCyclicFunctional.
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ DOFs │
|
||||
// │ x[v_idx[v]] = u_v – conformal factor at vertex v │
|
||||
// │ x[e_idx[e]] = λ_e – edge log-length variable (optional) │
|
||||
// │ -1 means "pinned" (u_v = 0 / λ_e = 0, only λ° contributes) │
|
||||
// │ │
|
||||
// │ Effective log-length (always additive, unlike SphericalFunctional): │
|
||||
// │ Λ̃_ij = λ°_ij + u_i + u_j + (x[e_idx[e]] if variable, else 0) │
|
||||
// │ │
|
||||
// │ Side length: l_ij = exp(Λ̃_ij / 2) │
|
||||
// │ │
|
||||
// │ Gradient: │
|
||||
// │ ∂E/∂u_v = Θ_v − Σ_{faces adj. v} α_v(face) │
|
||||
// │ ∂E/∂λ_e = α_opp(face⁺) + α_opp(face⁻) − φ_e │
|
||||
// │ │
|
||||
// │ Energy: │
|
||||
// │ Computed as the path integral E(x) = ∫₀¹ ⟨G(tx), x⟩ dt │
|
||||
// │ using 10-point Gauss-Legendre quadrature (same as SphericalFunctional)│
|
||||
// │ This is E(0)=0 by construction and exact for conservative G. │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// Halfedge convention (identical to SphericalFunctional):
|
||||
// h0 = mesh.halfedge(f), h1 = next(h0), h2 = next(h1)
|
||||
// v1 = source(h0), v2 = source(h1), v3 = source(h2)
|
||||
// h_alpha[h0] = α3 (angle at v3, opposite edge h0 = v1v2)
|
||||
// h_alpha[h1] = α1 (angle at v1, opposite edge h1 = v2v3)
|
||||
// h_alpha[h2] = α2 (angle at v2, opposite edge h2 = v3v1)
|
||||
//
|
||||
// Property-map name prefix: "ev:" (vertex) and "ee:" (edge).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "euclidean_geometry.hpp"
|
||||
#include <CGAL/boost/graph/iterator.h>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Property-map type aliases ─────────────────────────────────────────────────
|
||||
|
||||
using EuclVMapD = ConformalMesh::Property_map<Vertex_index, double>;
|
||||
using EuclVMapI = ConformalMesh::Property_map<Vertex_index, int>;
|
||||
using EuclEMapD = ConformalMesh::Property_map<Edge_index, double>;
|
||||
using EuclEMapI = ConformalMesh::Property_map<Edge_index, int>;
|
||||
|
||||
// ── Persistent map bundle ─────────────────────────────────────────────────────
|
||||
|
||||
struct EuclideanMaps {
|
||||
EuclVMapI v_idx; ///< DOF index per vertex (-1 = pinned / u_v = 0)
|
||||
EuclEMapI e_idx; ///< DOF index per edge (-1 = no edge DOF)
|
||||
EuclVMapD theta_v; ///< target cone angle Θ_v (default 2π)
|
||||
EuclEMapD phi_e; ///< target edge turn angle φ_e (default π)
|
||||
EuclEMapD lambda0; ///< base log-length λ°_e (default 0.0)
|
||||
};
|
||||
|
||||
// Create and attach property maps with sensible defaults.
|
||||
// theta_v = 2π (flat vertex), phi_e = π (interior edge, flat surface).
|
||||
inline EuclideanMaps setup_euclidean_maps(ConformalMesh& mesh)
|
||||
{
|
||||
EuclideanMaps m;
|
||||
m.v_idx = mesh.add_property_map<Vertex_index, int> ("ev:idx", -1 ).first;
|
||||
m.e_idx = mesh.add_property_map<Edge_index, int> ("ee:idx", -1 ).first;
|
||||
m.theta_v= mesh.add_property_map<Vertex_index, double>("ev:theta", TWO_PI ).first;
|
||||
m.phi_e = mesh.add_property_map<Edge_index, double>("ee:phi", PI ).first;
|
||||
m.lambda0= mesh.add_property_map<Edge_index, double>("ee:lam0", 0.0 ).first;
|
||||
return m;
|
||||
}
|
||||
|
||||
// Assign DOF indices 0..n-1 for all vertices only (no edge DOFs).
|
||||
inline int assign_euclidean_vertex_dof_indices(ConformalMesh& mesh, EuclideanMaps& m)
|
||||
{
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices()) m.v_idx[v] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// Assign DOF indices for all vertices AND all edges.
|
||||
inline int assign_euclidean_all_dof_indices(ConformalMesh& mesh, EuclideanMaps& m)
|
||||
{
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices()) m.v_idx[v] = idx++;
|
||||
for (auto e : mesh.edges()) m.e_idx[e] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// Count variable DOFs (vertices + edges).
|
||||
inline int euclidean_dimension(const ConformalMesh& mesh, const EuclideanMaps& m)
|
||||
{
|
||||
int dim = 0;
|
||||
for (auto v : mesh.vertices()) if (m.v_idx[v] >= 0) ++dim;
|
||||
for (auto e : mesh.edges()) if (m.e_idx[e] >= 0) ++dim;
|
||||
return dim;
|
||||
}
|
||||
|
||||
// Set lambda0 from mesh vertex positions (Euclidean):
|
||||
// λ°_e = 2·log(|p_i − p_j|) (natural log of Euclidean edge length squared)
|
||||
//
|
||||
// This gives exp(Λ̃_ij / 2) = l_ij at x=0.
|
||||
inline void compute_euclidean_lambda0_from_mesh(ConformalMesh& mesh, EuclideanMaps& m)
|
||||
{
|
||||
for (auto e : mesh.edges()) {
|
||||
auto h = mesh.halfedge(e);
|
||||
auto p1 = mesh.point(mesh.source(h));
|
||||
auto p2 = mesh.point(mesh.target(h));
|
||||
double dx = p1.x() - p2.x();
|
||||
double dy = p1.y() - p2.y();
|
||||
double dz = p1.z() - p2.z();
|
||||
double len = std::sqrt(dx*dx + dy*dy + dz*dz);
|
||||
if (len > 1e-15)
|
||||
m.lambda0[e] = 2.0 * std::log(len);
|
||||
else
|
||||
m.lambda0[e] = -30.0; // degenerate edge
|
||||
}
|
||||
}
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
static inline double eucl_dof_val(int idx, const std::vector<double>& x)
|
||||
{
|
||||
return idx >= 0 ? x[static_cast<std::size_t>(idx)] : 0.0;
|
||||
}
|
||||
|
||||
static inline std::size_t eucl_hidx(Halfedge_index h)
|
||||
{
|
||||
return static_cast<std::size_t>(static_cast<std::uint32_t>(h));
|
||||
}
|
||||
|
||||
// ── Gradient ──────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// G_v = Θ_v − Σ_{faces adj. v} α_v(face)
|
||||
// G_e = α_opp(face⁺) + α_opp(face⁻) − φ_e
|
||||
//
|
||||
// Corner-angle storage (h_alpha):
|
||||
// h_alpha[h] = corner angle OPPOSITE to the edge of halfedge h in its face.
|
||||
// h_alpha[h0] = α3, h_alpha[h1] = α1, h_alpha[h2] = α2
|
||||
// (same convention as SphericalFunctional)
|
||||
inline std::vector<double> euclidean_gradient(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const EuclideanMaps& m)
|
||||
{
|
||||
const int n = euclidean_dimension(mesh, m);
|
||||
std::vector<double> G(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
// Per-halfedge corner-angle storage.
|
||||
const std::size_t nh = mesh.number_of_halfedges();
|
||||
std::vector<double> h_alpha(nh, 0.0);
|
||||
|
||||
// ── Pass 1: compute corner angles per face ────────────────────────────────
|
||||
for (auto f : mesh.faces()) {
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0);
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
Edge_index e12 = mesh.edge(h0);
|
||||
Edge_index e23 = mesh.edge(h1);
|
||||
Edge_index e31 = mesh.edge(h2);
|
||||
|
||||
// Effective log-lengths (always additive: u_i + u_j regardless of DOF status)
|
||||
double u1 = eucl_dof_val(m.v_idx[v1], x);
|
||||
double u2 = eucl_dof_val(m.v_idx[v2], x);
|
||||
double u3 = eucl_dof_val(m.v_idx[v3], x);
|
||||
|
||||
double lam12 = m.lambda0[e12] + u1 + u2 + eucl_dof_val(m.e_idx[e12], x);
|
||||
double lam23 = m.lambda0[e23] + u2 + u3 + eucl_dof_val(m.e_idx[e23], x);
|
||||
double lam31 = m.lambda0[e31] + u3 + u1 + eucl_dof_val(m.e_idx[e31], x);
|
||||
|
||||
auto fa = euclidean_angles(lam12, lam23, lam31);
|
||||
if (!fa.valid) continue; // degenerate triangle: contributes 0
|
||||
|
||||
// h_alpha[h] = corner angle OPPOSITE to h's edge:
|
||||
// h0 (edge v1v2) → opposite corner at v3 → α3
|
||||
// h1 (edge v2v3) → opposite corner at v1 → α1
|
||||
// h2 (edge v3v1) → opposite corner at v2 → α2
|
||||
h_alpha[eucl_hidx(h0)] = fa.alpha3;
|
||||
h_alpha[eucl_hidx(h1)] = fa.alpha1;
|
||||
h_alpha[eucl_hidx(h2)] = fa.alpha2;
|
||||
}
|
||||
|
||||
// ── Pass 2: accumulate vertex gradient ───────────────────────────────────
|
||||
// G_v = Θ_v − Σ h_alpha[prev(h)] for each incoming non-border h to v.
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
double sum_alpha = 0.0;
|
||||
for (auto h : CGAL::halfedges_around_target(v, mesh)) {
|
||||
if (mesh.is_border(h)) continue;
|
||||
sum_alpha += h_alpha[eucl_hidx(mesh.prev(h))];
|
||||
}
|
||||
G[static_cast<std::size_t>(iv)] = m.theta_v[v] - sum_alpha;
|
||||
}
|
||||
|
||||
// ── Pass 3: accumulate edge gradient ─────────────────────────────────────
|
||||
// G_e = α_opp(f⁺) + α_opp(f⁻) − φ_e
|
||||
// α_opp of edge e in face f = h_alpha[halfedge h of e pointing INTO f].
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = m.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
auto h = mesh.halfedge(e);
|
||||
auto ho = mesh.opposite(h);
|
||||
double sum = -m.phi_e[e];
|
||||
if (!mesh.is_border(h)) sum += h_alpha[eucl_hidx(h)];
|
||||
if (!mesh.is_border(ho)) sum += h_alpha[eucl_hidx(ho)];
|
||||
G[static_cast<std::size_t>(ie)] = sum;
|
||||
}
|
||||
|
||||
return G;
|
||||
}
|
||||
|
||||
// ── Energy via Gauss-Legendre path integral ───────────────────────────────────
|
||||
//
|
||||
// E(x) = ∫₀¹ ⟨G(tx), x⟩ dt (10-point GL quadrature, same as SphericalFunctional)
|
||||
inline double euclidean_energy(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const EuclideanMaps& m)
|
||||
{
|
||||
static const double gl_s[10] = {
|
||||
-0.9739065285171717, -0.8650633666889845,
|
||||
-0.6794095682990244, -0.4333953941292472,
|
||||
-0.1488743389816312, 0.1488743389816312,
|
||||
0.4333953941292472, 0.6794095682990244,
|
||||
0.8650633666889845, 0.9739065285171717
|
||||
};
|
||||
static const double gl_w[10] = {
|
||||
0.0666713443086881, 0.1494513491505806,
|
||||
0.2190863625159820, 0.2692667193099963,
|
||||
0.2955242247147529, 0.2955242247147529,
|
||||
0.2692667193099963, 0.2190863625159820,
|
||||
0.1494513491505806, 0.0666713443086881
|
||||
};
|
||||
|
||||
const std::size_t n = x.size();
|
||||
double E = 0.0;
|
||||
|
||||
for (int k = 0; k < 10; ++k) {
|
||||
double t = (1.0 + gl_s[k]) * 0.5;
|
||||
double wt = gl_w[k] * 0.5;
|
||||
|
||||
std::vector<double> tx(n);
|
||||
for (std::size_t i = 0; i < n; ++i) tx[i] = t * x[i];
|
||||
|
||||
auto G = euclidean_gradient(mesh, tx, m);
|
||||
|
||||
double dot = 0.0;
|
||||
for (std::size_t i = 0; i < n; ++i) dot += G[i] * x[i];
|
||||
E += wt * dot;
|
||||
}
|
||||
return E;
|
||||
}
|
||||
|
||||
// ── Full evaluation (energy + gradient) ──────────────────────────────────────
|
||||
|
||||
struct EuclideanResult {
|
||||
double energy = 0.0;
|
||||
std::vector<double> gradient;
|
||||
};
|
||||
|
||||
inline EuclideanResult evaluate_euclidean(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const EuclideanMaps& m,
|
||||
bool need_energy = true,
|
||||
bool need_gradient = true)
|
||||
{
|
||||
EuclideanResult res;
|
||||
if (need_gradient)
|
||||
res.gradient = euclidean_gradient(mesh, x, m);
|
||||
if (need_energy)
|
||||
res.energy = euclidean_energy(mesh, x, m);
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Finite-difference gradient check ─────────────────────────────────────────
|
||||
//
|
||||
// Tests |G[i] − (E(x+εeᵢ) − E(x−εeᵢ))/(2ε)| / max(1,|G[i]|) < tol
|
||||
// for all variable DOFs.
|
||||
inline bool gradient_check_euclidean(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x0,
|
||||
const EuclideanMaps& m,
|
||||
double eps = 1e-5,
|
||||
double tol = 1e-4)
|
||||
{
|
||||
auto G = euclidean_gradient(mesh, x0, m);
|
||||
const int n = static_cast<int>(G.size());
|
||||
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
bool ok = true;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::size_t si = static_cast<std::size_t>(i);
|
||||
xp[si] = x0[si] + eps;
|
||||
xm[si] = x0[si] - eps;
|
||||
|
||||
double Ep = euclidean_energy(mesh, xp, m);
|
||||
double Em = euclidean_energy(mesh, xm, m);
|
||||
|
||||
xp[si] = xm[si] = x0[si]; // restore
|
||||
|
||||
double fd = (Ep - Em) / (2.0 * eps);
|
||||
double err = std::abs(G[si] - fd);
|
||||
double scale = std::max(1.0, std::abs(G[si]));
|
||||
if (err / scale > tol) ok = false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
91
code/include/euclidean_geometry.hpp
Normal file
91
code/include/euclidean_geometry.hpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
// euclidean_geometry.hpp
|
||||
//
|
||||
// Corner-angle formula for Euclidean triangles in the discrete conformal
|
||||
// (log-length) parametrisation.
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.EuclideanCyclicFunctional.
|
||||
//
|
||||
// In the discrete conformal parametrisation a Euclidean triangle is described by
|
||||
// its three effective log-lengths Λ̃_ij = λ°_ij + u_i + u_j (+ edge DOF).
|
||||
// The corresponding side lengths are l_ij = exp(Λ̃_ij / 2).
|
||||
//
|
||||
// Vertex ordering convention (matches EuclideanCyclicFunctional.java):
|
||||
// v1 is opposite edge l23, v2 is opposite l31, v3 is opposite l12.
|
||||
//
|
||||
// t-value trick (Springborn 2008 §3):
|
||||
// t12 = −l12 + l23 + l31 = 2(s − l12)
|
||||
// t23 = +l12 − l23 + l31 = 2(s − l23)
|
||||
// t31 = +l12 + l23 − l31 = 2(s − l31)
|
||||
// denom = sqrt(t12 · t23 · t31 · l123) = 4 · Area
|
||||
//
|
||||
// α_v = 2 · atan2( product of t-values adjacent to v, denom )
|
||||
//
|
||||
// The centering trick (l_ij ← exp((Λ̃_ij − 2·μ)/2), μ = (Λ̃12+Λ̃23+Λ̃31)/6)
|
||||
// rescales all three sides by the same factor, leaving angles unchanged but
|
||||
// keeping the arguments of exp in a safe numerical range.
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
struct EuclideanFaceAngles {
|
||||
double alpha1; ///< corner angle at v1 (opposite l23)
|
||||
double alpha2; ///< corner angle at v2 (opposite l31)
|
||||
double alpha3; ///< corner angle at v3 (opposite l12)
|
||||
bool valid;
|
||||
};
|
||||
|
||||
// ── From side lengths ─────────────────────────────────────────────────────────
|
||||
//
|
||||
// Given three Euclidean side lengths l12, l23, l31 > 0 satisfying the triangle
|
||||
// inequality, compute the corner angles.
|
||||
//
|
||||
// Returns valid=false if the triangle inequality is violated (any t-value ≤ 0).
|
||||
inline EuclideanFaceAngles euclidean_angles_from_lengths(
|
||||
double l12, double l23, double l31)
|
||||
{
|
||||
const double t12 = -l12 + l23 + l31; // 2*(s − l12)
|
||||
const double t23 = +l12 - l23 + l31; // 2*(s − l23)
|
||||
const double t31 = +l12 + l23 - l31; // 2*(s − l31)
|
||||
|
||||
if (t12 <= 0.0 || t23 <= 0.0 || t31 <= 0.0)
|
||||
return {0.0, 0.0, 0.0, false};
|
||||
|
||||
const double l123 = l12 + l23 + l31;
|
||||
const double denom2 = t12 * t23 * t31 * l123; // = (4·Area)²
|
||||
if (denom2 <= 0.0)
|
||||
return {0.0, 0.0, 0.0, false};
|
||||
|
||||
const double denom = std::sqrt(denom2);
|
||||
|
||||
// α at v1 (opposite l23): adjacent t-values are t12 and t31
|
||||
// α at v2 (opposite l31): adjacent t-values are t12 and t23
|
||||
// α at v3 (opposite l12): adjacent t-values are t23 and t31
|
||||
return {
|
||||
2.0 * std::atan2(t12 * t31, denom),
|
||||
2.0 * std::atan2(t12 * t23, denom),
|
||||
2.0 * std::atan2(t23 * t31, denom),
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
// ── From effective log-lengths Λ̃ ─────────────────────────────────────────────
|
||||
//
|
||||
// Converts to side lengths l_ij = exp(Λ̃_ij / 2), applying the centering
|
||||
// trick for numerical safety, then delegates to euclidean_angles_from_lengths.
|
||||
//
|
||||
// The centering constant μ = (Λ̃12 + Λ̃23 + Λ̃31) / 6 ensures
|
||||
// l12 · l23 · l31 = 1 (geometric mean = 1)
|
||||
// which keeps all l values near 1 and prevents float overflow for large |Λ̃|.
|
||||
inline EuclideanFaceAngles euclidean_angles(
|
||||
double lam12, double lam23, double lam31)
|
||||
{
|
||||
const double mu = (lam12 + lam23 + lam31) / 6.0;
|
||||
const double l12 = std::exp((lam12 - 2.0 * mu) * 0.5);
|
||||
const double l23 = std::exp((lam23 - 2.0 * mu) * 0.5);
|
||||
const double l31 = std::exp((lam31 - 2.0 * mu) * 0.5);
|
||||
return euclidean_angles_from_lengths(l12, l23, l31);
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
211
code/include/euclidean_hessian.hpp
Normal file
211
code/include/euclidean_hessian.hpp
Normal file
@@ -0,0 +1,211 @@
|
||||
#pragma once
|
||||
// euclidean_hessian.hpp
|
||||
//
|
||||
// Analytical Hessian of the Euclidean discrete conformal energy —
|
||||
// the cotangent-Laplace operator.
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.EuclideanCyclicFunctional
|
||||
// (the hessian() method).
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ Hessian formula (vertex DOFs only) │
|
||||
// │ │
|
||||
// │ For a face (v1, v2, v3) with effective log-lengths Λ̃ij and │
|
||||
// │ side lengths lij = exp(Λ̃ij/2): │
|
||||
// │ │
|
||||
// │ t12 = −l12+l23+l31, t23 = l12−l23+l31, t31 = l12+l23−l31 │
|
||||
// │ denom2 = 2·sqrt(t12·t23·t31·l123) = 8·Area │
|
||||
// │ │
|
||||
// │ cot_k = (t_adj1·l123 − t_adj2·t_opp) / denom2 │
|
||||
// │ = cotangent of the angle αk at vertex k │
|
||||
// │ │
|
||||
// │ Hessian contributions per face: │
|
||||
// │ H[vi, vi] += cot_vj + cot_vk (diagonal, both non-opp angles) │
|
||||
// │ H[vi, vj] -= cot_vk (off-diagonal, for variable vi,vj│
|
||||
// │ │
|
||||
// │ This is exactly the cotangent-Laplace operator from Pinkall–Polthier. │
|
||||
// │ │
|
||||
// │ Pinned vertices (v_idx = −1) contribute to diagonal of neighbours but │
|
||||
// │ do not create a column/row in H themselves. │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// Requires Eigen (header-only). The Hessian is returned as an
|
||||
// Eigen::SparseMatrix<double> for direct use in the Phase-4 Newton solver
|
||||
// (Eigen::SimplicialLDLT).
|
||||
//
|
||||
// The Hessian is symmetric positive semi-definite for any valid mesh with
|
||||
// no degenerate faces. The null space is spanned by the uniform-shift
|
||||
// vector 1 on closed surfaces (Euler characteristic = 0).
|
||||
|
||||
#include "euclidean_functional.hpp"
|
||||
#include <Eigen/Sparse>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Cotangent weight helper ───────────────────────────────────────────────────
|
||||
//
|
||||
// Given three Euclidean SIDE LENGTHS l12, l23, l31 (already exp(Λ̃/2)),
|
||||
// return the three cotangent weights (cot1, cot2, cot3).
|
||||
//
|
||||
// cot_k = (t_adj·l123 − t_opp·t_other) / (8·Area)
|
||||
//
|
||||
// Returns {0,0,0} for degenerate faces (triangle inequality violated or Area=0).
|
||||
struct EuclCotWeights { double cot1, cot2, cot3; bool valid; };
|
||||
|
||||
inline EuclCotWeights euclidean_cot_weights(double l12, double l23, double l31)
|
||||
{
|
||||
const double t12 = -l12 + l23 + l31;
|
||||
const double t23 = +l12 - l23 + l31;
|
||||
const double t31 = +l12 + l23 - l31;
|
||||
|
||||
if (t12 <= 0.0 || t23 <= 0.0 || t31 <= 0.0)
|
||||
return {0.0, 0.0, 0.0, false};
|
||||
|
||||
const double l123 = l12 + l23 + l31;
|
||||
const double denom2_sq = t12 * t23 * t31 * l123;
|
||||
if (denom2_sq <= 0.0) return {0.0, 0.0, 0.0, false};
|
||||
|
||||
// denom2 = 2·sqrt(t12·t23·t31·l123) = 8·Area
|
||||
const double denom2 = 2.0 * std::sqrt(denom2_sq);
|
||||
|
||||
// cot at v1 (opposite l23): adjacent t-values are t12 and t31.
|
||||
// cot at v2 (opposite l31): adjacent t-values are t12 and t23.
|
||||
// cot at v3 (opposite l12): adjacent t-values are t23 and t31.
|
||||
return {
|
||||
(t23 * l123 - t31 * t12) / denom2, // cot1
|
||||
(t31 * l123 - t12 * t23) / denom2, // cot2
|
||||
(t12 * l123 - t23 * t31) / denom2, // cot3
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
// ── Analytical Hessian (cotangent Laplacian) ──────────────────────────────────
|
||||
//
|
||||
// Returns the n×n sparse Hessian matrix H where n = euclidean_dimension(mesh, m).
|
||||
//
|
||||
// Only vertex DOFs are supported. Edge DOFs (m.e_idx[e] >= 0) produce
|
||||
// additional mixed-derivative entries that are not yet implemented; this
|
||||
// function asserts they are absent.
|
||||
//
|
||||
// x – current DOF vector (used to compute effective log-lengths Λ̃ij).
|
||||
inline Eigen::SparseMatrix<double> euclidean_hessian(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const EuclideanMaps& m)
|
||||
{
|
||||
const int n = euclidean_dimension(mesh, m);
|
||||
|
||||
// Collect triplets (row, col, value) — setFromTriplets sums duplicates.
|
||||
std::vector<Eigen::Triplet<double>> trips;
|
||||
trips.reserve(static_cast<std::size_t>(n) * 7); // rough estimate
|
||||
|
||||
for (auto f : mesh.faces()) {
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0);
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
Edge_index e12 = mesh.edge(h0);
|
||||
Edge_index e23 = mesh.edge(h1);
|
||||
Edge_index e31 = mesh.edge(h2);
|
||||
|
||||
// Effective log-lengths.
|
||||
double u1 = eucl_dof_val(m.v_idx[v1], x);
|
||||
double u2 = eucl_dof_val(m.v_idx[v2], x);
|
||||
double u3 = eucl_dof_val(m.v_idx[v3], x);
|
||||
|
||||
double lam12 = m.lambda0[e12] + u1 + u2 + eucl_dof_val(m.e_idx[e12], x);
|
||||
double lam23 = m.lambda0[e23] + u2 + u3 + eucl_dof_val(m.e_idx[e23], x);
|
||||
double lam31 = m.lambda0[e31] + u3 + u1 + eucl_dof_val(m.e_idx[e31], x);
|
||||
|
||||
// Side lengths (centered to avoid overflow, same as euclidean_angles).
|
||||
const double mu = (lam12 + lam23 + lam31) / 6.0;
|
||||
const double l12 = std::exp((lam12 - 2.0 * mu) * 0.5);
|
||||
const double l23 = std::exp((lam23 - 2.0 * mu) * 0.5);
|
||||
const double l31 = std::exp((lam31 - 2.0 * mu) * 0.5);
|
||||
|
||||
auto [cot1, cot2, cot3, valid] = euclidean_cot_weights(l12, l23, l31);
|
||||
if (!valid) continue;
|
||||
|
||||
const int i1 = m.v_idx[v1];
|
||||
const int i2 = m.v_idx[v2];
|
||||
const int i3 = m.v_idx[v3];
|
||||
|
||||
// ── Diagonal contributions ──────────────────────────────────────────
|
||||
// H[v1,v1] += (cot2 + cot3)/2 (Pinkall–Polthier factor of 1/2)
|
||||
// H[v2,v2] += (cot3 + cot1)/2
|
||||
// H[v3,v3] += (cot1 + cot2)/2
|
||||
if (i1 >= 0) trips.emplace_back(i1, i1, (cot2 + cot3) * 0.5);
|
||||
if (i2 >= 0) trips.emplace_back(i2, i2, (cot3 + cot1) * 0.5);
|
||||
if (i3 >= 0) trips.emplace_back(i3, i3, (cot1 + cot2) * 0.5);
|
||||
|
||||
// ── Off-diagonal contributions (only for variable pairs) ────────────
|
||||
// Edge v1-v2 opposite α3: H[v1,v2] -= cot3/2
|
||||
if (i1 >= 0 && i2 >= 0) {
|
||||
trips.emplace_back(i1, i2, -cot3 * 0.5);
|
||||
trips.emplace_back(i2, i1, -cot3 * 0.5);
|
||||
}
|
||||
// Edge v2-v3 opposite α1: H[v2,v3] -= cot1/2
|
||||
if (i2 >= 0 && i3 >= 0) {
|
||||
trips.emplace_back(i2, i3, -cot1 * 0.5);
|
||||
trips.emplace_back(i3, i2, -cot1 * 0.5);
|
||||
}
|
||||
// Edge v3-v1 opposite α2: H[v3,v1] -= cot2/2
|
||||
if (i3 >= 0 && i1 >= 0) {
|
||||
trips.emplace_back(i3, i1, -cot2 * 0.5);
|
||||
trips.emplace_back(i1, i3, -cot2 * 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
Eigen::SparseMatrix<double> H(n, n);
|
||||
H.setFromTriplets(trips.begin(), trips.end());
|
||||
return H;
|
||||
}
|
||||
|
||||
// ── Finite-difference Hessian check ──────────────────────────────────────────
|
||||
//
|
||||
// Compares the analytical Hessian column-by-column against
|
||||
// H_fd[:, j] = (G(x + ε·eⱼ) − G(x − ε·eⱼ)) / (2ε).
|
||||
//
|
||||
// Returns true if max relative error < tol for every entry.
|
||||
inline bool hessian_check_euclidean(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x0,
|
||||
const EuclideanMaps& m,
|
||||
double eps = 1e-5,
|
||||
double tol = 1e-4)
|
||||
{
|
||||
const int n = static_cast<int>(x0.size());
|
||||
auto H = euclidean_hessian(mesh, x0, m);
|
||||
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
bool ok = true;
|
||||
|
||||
for (int j = 0; j < n; ++j) {
|
||||
const std::size_t sj = static_cast<std::size_t>(j);
|
||||
xp[sj] = x0[sj] + eps;
|
||||
xm[sj] = x0[sj] - eps;
|
||||
|
||||
auto Gp = euclidean_gradient(mesh, xp, m);
|
||||
auto Gm = euclidean_gradient(mesh, xm, m);
|
||||
|
||||
xp[sj] = xm[sj] = x0[sj]; // restore
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
double fd_ij = (Gp[static_cast<std::size_t>(i)]
|
||||
- Gm[static_cast<std::size_t>(i)]) / (2.0 * eps);
|
||||
double H_ij = H.coeff(i, j);
|
||||
double err = std::abs(H_ij - fd_ij);
|
||||
double scale = std::max(1.0, std::abs(H_ij));
|
||||
if (err / scale > tol) ok = false;
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
204
code/include/fundamental_domain.hpp
Normal file
204
code/include/fundamental_domain.hpp
Normal file
@@ -0,0 +1,204 @@
|
||||
#pragma once
|
||||
// fundamental_domain.hpp
|
||||
//
|
||||
// Phase 7 — Fundamental domain polygon for closed surfaces.
|
||||
//
|
||||
// For a closed genus-g surface cut open via a CutGraph + Euclidean layout:
|
||||
//
|
||||
// The universal cover is tiled by copies of the cut-open disk.
|
||||
// The fundamental domain is the polygon whose sides are identified in pairs
|
||||
// by the holonomy generators.
|
||||
//
|
||||
// ─── Genus-1 (flat torus) ────────────────────────────────────────────────────
|
||||
//
|
||||
// Parallelogram with vertices 0, ω_1, ω_1 + ω_2, ω_2.
|
||||
// The four edges are identified in pairs:
|
||||
// bottom (0 → ω_1) ≡ top (ω_2 → ω_1 + ω_2) — translation ω_2
|
||||
// left (0 → ω_2) ≡ right (ω_1 → ω_1 + ω_2) — translation ω_1
|
||||
//
|
||||
// ─── Genus g > 1 (general) ──────────────────────────────────────────────────
|
||||
//
|
||||
// The standard 4g-polygon with sides labelled a_1 b_1 a_1^{-1} b_1^{-1} ...
|
||||
// can be recovered from the layout boundary, but requires walking the
|
||||
// boundary of the cut-open mesh — not yet implemented (see note below).
|
||||
//
|
||||
// For now, this file provides the genus-1 parallelogram only.
|
||||
// The polygon vertices for genus-1 are computed from the holonomy generators.
|
||||
//
|
||||
// ─── API ─────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// FundamentalDomain fd = compute_fundamental_domain_genus1(holonomy);
|
||||
// fd.vertices — 2D polygon corners (size = 4 for genus-1)
|
||||
// fd.edge_identifications — pairs (i, j) meaning edge i is identified with j
|
||||
// fd.is_valid() — true if genus == 1 and data makes sense
|
||||
|
||||
#include "layout.hpp"
|
||||
#include "period_matrix.hpp"
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// FundamentalDomain
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
struct FundamentalDomain {
|
||||
/// Polygon corners in order (CCW). Size = 4 for genus-1.
|
||||
std::vector<Eigen::Vector2d> vertices;
|
||||
|
||||
/// edge_identifications[k] = (i, j) means the edge from vertices[i] to
|
||||
/// vertices[(i+1) % n] is identified with the edge from vertices[j] to
|
||||
/// vertices[(j+1) % n] (with matching orientation).
|
||||
std::vector<std::pair<int, int>> edge_identifications;
|
||||
|
||||
/// Holonomy generators (one per identified edge pair).
|
||||
/// For genus-1: generators[0] = ω_1, generators[1] = ω_2.
|
||||
std::vector<Eigen::Vector2d> generators;
|
||||
|
||||
bool is_valid() const { return vertices.size() >= 3; }
|
||||
};
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// compute_fundamental_domain_genus1
|
||||
//
|
||||
// Builds the parallelogram fundamental domain from Euclidean holonomy data
|
||||
// with exactly 2 generators ω_1, ω_2.
|
||||
//
|
||||
// Vertices (CCW):
|
||||
// v0 = (0, 0)
|
||||
// v1 = ω_1
|
||||
// v2 = ω_1 + ω_2
|
||||
// v3 = ω_2
|
||||
//
|
||||
// Edge identifications:
|
||||
// bottom (v0→v1) ≡ top (v3→v2) by ω_2
|
||||
// left (v3→v0) ≡ right (v2→v1) by ω_1 (reversed convention)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline FundamentalDomain compute_fundamental_domain_genus1(
|
||||
const HolonomyData& hol)
|
||||
{
|
||||
FundamentalDomain fd;
|
||||
if (hol.translations.size() < 2) return fd;
|
||||
|
||||
Eigen::Vector2d w1 = hol.translations[0];
|
||||
Eigen::Vector2d w2 = hol.translations[1];
|
||||
|
||||
// Ensure CCW orientation: cross product z-component w1 × w2 > 0
|
||||
double cross = w1.x() * w2.y() - w1.y() * w2.x();
|
||||
if (cross < 0.0) std::swap(w1, w2);
|
||||
|
||||
Eigen::Vector2d origin = Eigen::Vector2d::Zero();
|
||||
fd.vertices = { origin, w1, w1 + w2, w2 };
|
||||
|
||||
// Edge 0: v0→v1 (= bottom), Edge 2: v3→v2 (= top, reversed)
|
||||
// Identification: bottom ≡ top translated by w2
|
||||
// Edge 1: v1→v2 (= right), Edge 3: v0→v3... wait let me use standard labeling:
|
||||
// Edges by index: 0: v0→v1, 1: v1→v2, 2: v2→v3, 3: v3→v0
|
||||
// Identifications: 0 ≡ 2 (reversed: bottom ≡ top by w2)
|
||||
// 1 ≡ 3 (reversed: right ≡ left by w1)
|
||||
fd.edge_identifications = { {0, 2}, {1, 3} };
|
||||
fd.generators = { w1, w2 };
|
||||
return fd;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// compute_fundamental_domain
|
||||
//
|
||||
// Dispatcher: for genus-1 uses compute_fundamental_domain_genus1.
|
||||
// For higher genus returns an empty FundamentalDomain (not yet implemented).
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// TODO(Phase 8): Implement the standard 4g-gon fundamental domain for genus g > 1.
|
||||
//
|
||||
// Algorithm outline (boundary-walk method):
|
||||
// ─────────────────────────────────────────
|
||||
// 1. Construct the CutGraph on the cut-open mesh (already done upstream).
|
||||
// This yields 2g cut edges; cutting them converts the closed surface into
|
||||
// a topological disk.
|
||||
//
|
||||
// 2. Walk the boundary of the cut-open disk in CCW order:
|
||||
// Start from any boundary halfedge and follow `next(h)` along the boundary
|
||||
// (i.e. skip to the next boundary halfedge at each vertex). Collect the
|
||||
// 2·(4g) = 8g boundary halfedges in order.
|
||||
// Each halfedge h_k corresponds to a UV vertex `halfedge_uv[h_k.idx()]`.
|
||||
//
|
||||
// 3. Identify paired sides:
|
||||
// The 4g sides of the polygon alternate as a_1 b_1 a_1^{-1} b_1^{-1} …
|
||||
// For each cut edge e_i (i = 1 … 2g) the two sides that are identified
|
||||
// are those whose source/target vertices match under the holonomy generator
|
||||
// ω_i (Euclidean) or T_i (hyperbolic).
|
||||
// Record the identifications as edge_identifications[k] = (i, j).
|
||||
//
|
||||
// 4. Fill FundamentalDomain:
|
||||
// vertices = UV corners from the boundary walk.
|
||||
// edge_identifications = paired-edge list from step 3.
|
||||
// generators = holonomy.translations (Euclidean) or the
|
||||
// fixed points of holonomy.mobius_maps (hyperbolic,
|
||||
// requires computing axis of T_i ∈ SU(1,1)).
|
||||
//
|
||||
// References:
|
||||
// Erickson & Whittlesey, "Greedy optimal homotopy and homology generators"
|
||||
// SODA 2005.
|
||||
// Desbrun, Kanso, Tong, "Discrete Differential Forms for Computational
|
||||
// Modeling", in Discrete Differential Geometry (2008).
|
||||
//
|
||||
// Note: The Siegel period matrix Ω ∈ H_g (g×g complex symmetric, Im Ω > 0)
|
||||
// for genus g > 1 also requires integration of holomorphic differentials —
|
||||
// this is intentionally deferred and NOT implemented here.
|
||||
// See period_matrix.hpp for the genus-1 case (τ = ω_2/ω_1 ∈ ℍ).
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline FundamentalDomain compute_fundamental_domain(
|
||||
const HolonomyData& hol)
|
||||
{
|
||||
int n = static_cast<int>(hol.translations.size());
|
||||
int g = n / 2;
|
||||
if (g == 1) return compute_fundamental_domain_genus1(hol);
|
||||
// Higher genus: boundary-walk 4g-polygon — not yet implemented (see TODO above).
|
||||
return FundamentalDomain{};
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// tiling_copy
|
||||
//
|
||||
// Given a Layout2D for the cut-open surface and two lattice generators ω_1, ω_2,
|
||||
// return a translated copy of the layout shifted by m·ω_1 + n·ω_2.
|
||||
// Useful for visualising the tiled universal cover.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline Layout2D tiling_copy(const Layout2D& layout,
|
||||
const Eigen::Vector2d& w1,
|
||||
const Eigen::Vector2d& w2,
|
||||
int m, int n)
|
||||
{
|
||||
Layout2D copy = layout;
|
||||
Eigen::Vector2d shift = static_cast<double>(m) * w1
|
||||
+ static_cast<double>(n) * w2;
|
||||
for (auto& p : copy.uv) p += shift;
|
||||
return copy;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// tiling_neighbourhood
|
||||
//
|
||||
// Returns a vector of tiling copies for (m, n) with |m| ≤ m_max, |n| ≤ n_max.
|
||||
// The result includes the original (m=0, n=0) at index (m_max)(2*n_max+1)+n_max.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline std::vector<Layout2D> tiling_neighbourhood(
|
||||
const Layout2D& layout,
|
||||
const HolonomyData& hol,
|
||||
int m_max = 2, int n_max = 2)
|
||||
{
|
||||
std::vector<Layout2D> tiles;
|
||||
if (hol.translations.size() < 2) {
|
||||
tiles.push_back(layout);
|
||||
return tiles;
|
||||
}
|
||||
const Eigen::Vector2d& w1 = hol.translations[0];
|
||||
const Eigen::Vector2d& w2 = hol.translations[1];
|
||||
for (int m = -m_max; m <= m_max; ++m)
|
||||
for (int n = -n_max; n <= n_max; ++n)
|
||||
tiles.push_back(tiling_copy(layout, w1, w2, m, n));
|
||||
return tiles;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
145
code/include/gauss_bonnet.hpp
Normal file
145
code/include/gauss_bonnet.hpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#pragma once
|
||||
// gauss_bonnet.hpp
|
||||
//
|
||||
// Phase 6 — Gauss–Bonnet 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 = Gauss–Bonnet 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 << "Gauss–Bonnet 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 Gauss–Bonnet 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
|
||||
344
code/include/hyper_ideal_functional.hpp
Normal file
344
code/include/hyper_ideal_functional.hpp
Normal file
@@ -0,0 +1,344 @@
|
||||
#pragma once
|
||||
// hyper_ideal_functional.hpp
|
||||
//
|
||||
// Energy and gradient of the hyper-ideal discrete conformal map functional
|
||||
// evaluated on a ConformalMesh (CGAL::Surface_mesh).
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.HyperIdealFunctional.
|
||||
//
|
||||
// ┌─────────────────────────────────────────────────────────────────────────┐
|
||||
// │ E(x) = Σ_faces U(f) − Σ_edges θ_e · a_e − Σ_vertices Θ_v · b_v │
|
||||
// │ │
|
||||
// │ ∂E/∂b_v = Σ_{faces adj. v} β_v(face) − Θ_v │
|
||||
// │ ∂E/∂a_e = α_e(face⁺) + α_e(face⁻) − θ_e │
|
||||
// └─────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// DOF vector layout (matches getDimension() ordering):
|
||||
// x[v_idx[v]] = b_v for each variable vertex (log scale factor)
|
||||
// x[e_idx[e]] = a_e for each variable edge (intersection angle)
|
||||
// -1 in v_idx / e_idx means "pinned" (ideal / fixed at 0).
|
||||
//
|
||||
// Usage
|
||||
// ─────
|
||||
// auto mesh = make_tetrahedron();
|
||||
// auto maps = setup_hyper_ideal_maps(mesh);
|
||||
// int n = assign_all_dof_indices(mesh, maps); // all variable
|
||||
// std::vector<double> x(n, 1.0);
|
||||
// auto res = evaluate_hyper_ideal(mesh, x, maps);
|
||||
// // res.energy, res.gradient
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "hyper_ideal_geometry.hpp"
|
||||
#include "hyper_ideal_utility.hpp"
|
||||
#include <CGAL/boost/graph/iterator.h>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Property-map type aliases ─────────────────────────────────────────────────
|
||||
|
||||
using VMapD = ConformalMesh::Property_map<Vertex_index, double>;
|
||||
using VMapI = ConformalMesh::Property_map<Vertex_index, int>;
|
||||
using EMapD = ConformalMesh::Property_map<Edge_index, double>;
|
||||
using EMapI = ConformalMesh::Property_map<Edge_index, int>;
|
||||
|
||||
// ── Persistent map bundle ─────────────────────────────────────────────────────
|
||||
|
||||
struct HyperIdealMaps {
|
||||
VMapI v_idx; // DOF index per vertex (-1 = pinned / ideal point)
|
||||
EMapI e_idx; // DOF index per edge (-1 = fixed)
|
||||
VMapD theta_v; // target cone angle Θ_v (parameter, not variable)
|
||||
EMapD theta_e; // target intersection angle θ_e
|
||||
};
|
||||
|
||||
// Add all needed persistent property maps and return handles.
|
||||
// Defaults: theta_v = 2π (regular cone), theta_e = π (orthogonal circles).
|
||||
inline HyperIdealMaps setup_hyper_ideal_maps(ConformalMesh& mesh)
|
||||
{
|
||||
HyperIdealMaps m;
|
||||
m.v_idx = mesh.add_property_map<Vertex_index, int> ("v:idx", -1 ).first;
|
||||
m.e_idx = mesh.add_property_map<Edge_index, int> ("e:idx", -1 ).first;
|
||||
m.theta_v = mesh.add_property_map<Vertex_index, double>("v:theta", 2.0*PI ).first;
|
||||
m.theta_e = mesh.add_property_map<Edge_index, double>("e:theta", PI ).first;
|
||||
return m;
|
||||
}
|
||||
|
||||
// Count variable DOFs: #variable_vertices + #variable_edges.
|
||||
inline int hyper_ideal_dimension(const ConformalMesh& mesh, const HyperIdealMaps& m)
|
||||
{
|
||||
int dim = 0;
|
||||
for (auto v : mesh.vertices()) if (m.v_idx[v] >= 0) ++dim;
|
||||
for (auto e : mesh.edges()) if (m.e_idx[e] >= 0) ++dim;
|
||||
return dim;
|
||||
}
|
||||
|
||||
// Assign DOF indices 0..n-1: vertices first, then edges.
|
||||
// All vertices and edges become variable. Returns total DOF count.
|
||||
inline int assign_all_dof_indices(ConformalMesh& mesh, HyperIdealMaps& m)
|
||||
{
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices()) m.v_idx[v] = idx++;
|
||||
for (auto e : mesh.edges()) m.e_idx[e] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// ── Evaluation result ─────────────────────────────────────────────────────────
|
||||
|
||||
struct HyperIdealResult {
|
||||
double energy = 0.0;
|
||||
std::vector<double> gradient; // empty when gradient was not requested
|
||||
};
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
// Get the DOF value from x, or 0.0 if pinned.
|
||||
static inline double dof_val(int idx, const std::vector<double>& x)
|
||||
{
|
||||
return idx >= 0 ? x[static_cast<std::size_t>(idx)] : 0.0;
|
||||
}
|
||||
|
||||
// Convert a CGAL halfedge index to a plain std::size_t (for vector indexing).
|
||||
static inline std::size_t hidx(Halfedge_index h)
|
||||
{
|
||||
return static_cast<std::size_t>(static_cast<std::uint32_t>(h));
|
||||
}
|
||||
|
||||
// ── Per-face angle kernel ─────────────────────────────────────────────────────
|
||||
|
||||
struct FaceAngles {
|
||||
double alpha12, alpha23, alpha31; // dihedral angles at each edge
|
||||
double beta1, beta2, beta3; // interior angles at each vertex
|
||||
double a12, a23, a31; // edge DOF values (used in energy)
|
||||
double b1, b2, b3; // vertex DOF values
|
||||
bool v1b, v2b, v3b; // whether each vertex is variable
|
||||
};
|
||||
|
||||
static FaceAngles compute_face_angles(
|
||||
const ConformalMesh& mesh,
|
||||
Face_index f,
|
||||
const std::vector<double>& x,
|
||||
const HyperIdealMaps& m)
|
||||
{
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0);
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
Edge_index e12 = mesh.edge(h0);
|
||||
Edge_index e23 = mesh.edge(h1);
|
||||
Edge_index e31 = mesh.edge(h2);
|
||||
|
||||
FaceAngles fa;
|
||||
fa.v1b = m.v_idx[v1] >= 0;
|
||||
fa.v2b = m.v_idx[v2] >= 0;
|
||||
fa.v3b = m.v_idx[v3] >= 0;
|
||||
|
||||
fa.a12 = dof_val(m.e_idx[e12], x);
|
||||
fa.a23 = dof_val(m.e_idx[e23], x);
|
||||
fa.a31 = dof_val(m.e_idx[e31], x);
|
||||
fa.b1 = dof_val(m.v_idx[v1], x);
|
||||
fa.b2 = dof_val(m.v_idx[v2], x);
|
||||
fa.b3 = dof_val(m.v_idx[v3], x);
|
||||
|
||||
// Clamp invalid inputs (mirrors Java log.warning + clamp)
|
||||
if (fa.v1b && fa.v2b && fa.a12 < 0.0) fa.a12 = 0.0;
|
||||
if (fa.v2b && fa.v3b && fa.a23 < 0.0) fa.a23 = 0.0;
|
||||
if (fa.v3b && fa.v1b && fa.a31 < 0.0) fa.a31 = 0.0;
|
||||
if (fa.v1b && fa.b1 < 0.0) fa.b1 = 0.01;
|
||||
if (fa.v2b && fa.b2 < 0.0) fa.b2 = 0.01;
|
||||
if (fa.v3b && fa.b3 < 0.0) fa.b3 = 0.01;
|
||||
|
||||
double l12 = lij(fa.b1, fa.b2, fa.a12, fa.v1b, fa.v2b);
|
||||
double l23 = lij(fa.b2, fa.b3, fa.a23, fa.v2b, fa.v3b);
|
||||
double l31 = lij(fa.b3, fa.b1, fa.a31, fa.v3b, fa.v1b);
|
||||
|
||||
// Guard degenerate lengths
|
||||
if (l12 < 1E-12 && l23 < 1E-12 && l31 < 1E-12)
|
||||
l12 = l23 = l31 = 1E-12;
|
||||
|
||||
// Check triangle inequalities; degenerate cases get extreme angles
|
||||
if (l12 > l23 + l31) {
|
||||
fa.beta1 = 0.0; fa.beta2 = 0.0; fa.beta3 = PI;
|
||||
fa.alpha12 = PI; fa.alpha23 = 0.0; fa.alpha31 = 0.0;
|
||||
} else if (l23 > l12 + l31) {
|
||||
fa.beta1 = PI; fa.beta2 = 0.0; fa.beta3 = 0.0;
|
||||
fa.alpha12 = 0.0; fa.alpha23 = PI; fa.alpha31 = 0.0;
|
||||
} else if (l31 > l12 + l23) {
|
||||
fa.beta1 = 0.0; fa.beta2 = PI; fa.beta3 = 0.0;
|
||||
fa.alpha12 = 0.0; fa.alpha23 = 0.0; fa.alpha31 = PI;
|
||||
} else {
|
||||
fa.beta1 = zeta(l12, l31, l23);
|
||||
fa.beta2 = zeta(l23, l12, l31);
|
||||
fa.beta3 = zeta(l31, l23, l12);
|
||||
|
||||
fa.alpha12 = alpha_ij(fa.a12, fa.a23, fa.a31,
|
||||
fa.b1, fa.b2, fa.b3,
|
||||
fa.beta1, fa.beta2, fa.beta3,
|
||||
fa.v1b, fa.v2b, fa.v3b);
|
||||
fa.alpha23 = alpha_ij(fa.a23, fa.a31, fa.a12,
|
||||
fa.b2, fa.b3, fa.b1,
|
||||
fa.beta2, fa.beta3, fa.beta1,
|
||||
fa.v2b, fa.v3b, fa.v1b);
|
||||
fa.alpha31 = alpha_ij(fa.a31, fa.a12, fa.a23,
|
||||
fa.b3, fa.b1, fa.b2,
|
||||
fa.beta3, fa.beta1, fa.beta2,
|
||||
fa.v3b, fa.v1b, fa.v2b);
|
||||
}
|
||||
return fa;
|
||||
}
|
||||
|
||||
// Per-face energy contribution U(f) (before subtracting θ·a and Θ·b terms).
|
||||
static double face_energy(const FaceAngles& fa)
|
||||
{
|
||||
double aa = fa.a12*fa.alpha12 + fa.a23*fa.alpha23 + fa.a31*fa.alpha31;
|
||||
double bb = fa.b1 *fa.beta1 + fa.b2 *fa.beta2 + fa.b3 *fa.beta3;
|
||||
|
||||
double V = 0.0;
|
||||
if (fa.v1b && fa.v2b && fa.v3b) {
|
||||
V = calculateTetrahedronVolume(
|
||||
fa.beta1, fa.beta2, fa.beta3,
|
||||
fa.alpha23, fa.alpha31, fa.alpha12);
|
||||
} else if (!fa.v1b) {
|
||||
V = calculateTetrahedronVolumeWithIdealVertexAtGamma(
|
||||
fa.beta1, fa.alpha31, fa.alpha12,
|
||||
fa.alpha23, fa.beta2, fa.beta3);
|
||||
} else if (!fa.v2b) {
|
||||
V = calculateTetrahedronVolumeWithIdealVertexAtGamma(
|
||||
fa.beta2, fa.alpha12, fa.alpha23,
|
||||
fa.alpha31, fa.beta3, fa.beta1);
|
||||
} else { // !v3b
|
||||
V = calculateTetrahedronVolumeWithIdealVertexAtGamma(
|
||||
fa.beta3, fa.alpha23, fa.alpha31,
|
||||
fa.alpha12, fa.beta1, fa.beta2);
|
||||
}
|
||||
return aa + bb + 2.0 * V;
|
||||
}
|
||||
|
||||
// ── Full evaluation ───────────────────────────────────────────────────────────
|
||||
|
||||
inline HyperIdealResult evaluate_hyper_ideal(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const HyperIdealMaps& m,
|
||||
bool need_energy = true,
|
||||
bool need_gradient = true)
|
||||
{
|
||||
HyperIdealResult res;
|
||||
|
||||
// Temporary per-halfedge storage for computed angles.
|
||||
// Indexed by the integer value of Halfedge_index.
|
||||
const std::size_t nh = mesh.number_of_halfedges();
|
||||
std::vector<double> h_alpha(nh, 0.0); // α_ij stored on halfedge
|
||||
std::vector<double> h_beta (nh, 0.0); // β_i stored on opposite halfedge
|
||||
|
||||
// ── Pass 1: angles + energy per face ─────────────────────────────────────
|
||||
for (auto f : mesh.faces()) {
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
FaceAngles fa = compute_face_angles(mesh, f, x, m);
|
||||
|
||||
// Store computed angles into temporary arrays.
|
||||
// h_alpha[h] = α for the edge of h in this face.
|
||||
h_alpha[hidx(h0)] = fa.alpha12;
|
||||
h_alpha[hidx(h1)] = fa.alpha23;
|
||||
h_alpha[hidx(h2)] = fa.alpha31;
|
||||
|
||||
// h_beta[h] = β at the vertex OPPOSITE to h.
|
||||
// β1 (at v1 = source(h0)) is opposite to h1 = e23.
|
||||
h_beta[hidx(h1)] = fa.beta1; // h1 is across from v1
|
||||
h_beta[hidx(h2)] = fa.beta2; // h2 is across from v2
|
||||
h_beta[hidx(h0)] = fa.beta3; // h0 is across from v3
|
||||
|
||||
if (need_energy)
|
||||
res.energy += face_energy(fa);
|
||||
}
|
||||
|
||||
// ── Pass 2: linear energy terms ──────────────────────────────────────────
|
||||
if (need_energy) {
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = m.e_idx[e];
|
||||
if (ie >= 0) res.energy -= m.theta_e[e] * x[static_cast<std::size_t>(ie)];
|
||||
}
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv >= 0) res.energy -= m.theta_v[v] * x[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
}
|
||||
|
||||
// ── Pass 3: gradient ─────────────────────────────────────────────────────
|
||||
if (need_gradient) {
|
||||
const int n = hyper_ideal_dimension(mesh, m);
|
||||
res.gradient.assign(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
// ∂E/∂b_v = Σ_{faces adj. v} β_v(face) − Θ_v
|
||||
// β_v(face) = h_beta[prev(h)] for the incoming halfedge h to v in that face.
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
for (auto h : CGAL::halfedges_around_target(v, mesh)) {
|
||||
if (mesh.is_border(h)) continue;
|
||||
res.gradient[static_cast<std::size_t>(iv)] += h_beta[hidx(mesh.prev(h))];
|
||||
}
|
||||
res.gradient[static_cast<std::size_t>(iv)] -= m.theta_v[v];
|
||||
}
|
||||
|
||||
// ∂E/∂a_e = α_e(face⁺) + α_e(face⁻) − θ_e
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = m.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
auto h = mesh.halfedge(e);
|
||||
auto ho = mesh.opposite(h);
|
||||
if (!mesh.is_border(h)) res.gradient[static_cast<std::size_t>(ie)] += h_alpha[hidx(h)];
|
||||
if (!mesh.is_border(ho)) res.gradient[static_cast<std::size_t>(ie)] += h_alpha[hidx(ho)];
|
||||
res.gradient[static_cast<std::size_t>(ie)] -= m.theta_e[e];
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Finite-difference gradient check ─────────────────────────────────────────
|
||||
//
|
||||
// Returns true if |G[i] − fd[i]| / max(1, |G[i]|) < tol for all DOFs.
|
||||
// eps = step size, tol = tolerance (same defaults as Java FunctionalTest).
|
||||
inline bool gradient_check(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x0,
|
||||
const HyperIdealMaps& m,
|
||||
double eps = 1E-5,
|
||||
double tol = 1E-4)
|
||||
{
|
||||
// Analytic gradient
|
||||
auto res = evaluate_hyper_ideal(mesh, x0, m, false, true);
|
||||
const auto& G = res.gradient;
|
||||
const int n = static_cast<int>(G.size());
|
||||
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
bool ok = true;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::size_t si = static_cast<std::size_t>(i);
|
||||
xp[si] = x0[si] + eps;
|
||||
xm[si] = x0[si] - eps;
|
||||
|
||||
double Ep = evaluate_hyper_ideal(mesh, xp, m, true, false).energy;
|
||||
double Em = evaluate_hyper_ideal(mesh, xm, m, true, false).energy;
|
||||
|
||||
xp[si] = xm[si] = x0[si];
|
||||
|
||||
double fd = (Ep - Em) / (2.0 * eps);
|
||||
double err = std::abs(G[si] - fd);
|
||||
double scale = std::max(1.0, std::abs(G[si]));
|
||||
if (err / scale > tol) ok = false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
137
code/include/hyper_ideal_geometry.hpp
Normal file
137
code/include/hyper_ideal_geometry.hpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#pragma once
|
||||
// hyper_ideal_geometry.hpp
|
||||
//
|
||||
// Pure-math building blocks for the hyper-ideal discrete conformal map.
|
||||
// Ported from de.varylab.discreteconformal.functional.HyperIdealUtility
|
||||
// and the private helpers of HyperIdealFunctional (lij, αij, σi, σij).
|
||||
//
|
||||
// All functions are independent of the mesh type.
|
||||
//
|
||||
// Notation follows the original Java / paper:
|
||||
// b_i, b_j – vertex variables (log scale factors, hyper-ideal vertices)
|
||||
// a_ij – edge variable (intersection angle between horocycles)
|
||||
// l_ij – effective hyperbolic edge length in the auxiliary triangle
|
||||
// β_i – interior angle of the hyperbolic triangle at vertex i
|
||||
// α_ij – dihedral angle of the tetrahedron at edge ij
|
||||
|
||||
#include "constants.hpp"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Length functions ─────────────────────────────────────────────────────────
|
||||
|
||||
// ζ(x,y,z) — interior angle in a hyperbolic triangle with edge lengths
|
||||
// x, y, z, opposite to the side of length z.
|
||||
// Ports HyperIdealUtility.ζ(x, y, z).
|
||||
inline double zeta(double x, double y, double z)
|
||||
{
|
||||
double cx = std::cosh(x), cy = std::cosh(y), cz = std::cosh(z);
|
||||
double sx = std::sinh(x), sy = std::sinh(y);
|
||||
double nbd = (cx*cy - cz) / (sx*sy);
|
||||
nbd = std::clamp(nbd, -1.0, 1.0); // guard floating-point rounding
|
||||
return std::acos(nbd);
|
||||
}
|
||||
|
||||
// ζ₁₃(x,y,z) — third edge length in a right-angled hyperbolic hexagon.
|
||||
// Ports HyperIdealUtility.ζ_13(x, y, z).
|
||||
inline double zeta13(double x, double y, double z)
|
||||
{
|
||||
double cx = std::cosh(x), cy = std::cosh(y), cz = std::cosh(z);
|
||||
double sx = std::sinh(x), sy = std::sinh(y);
|
||||
return std::acosh((cx*cy + cz) / (sx*sy));
|
||||
}
|
||||
|
||||
// ζ₁₄(x,y) — edge length in a hyperbolic pentagon with one ideal vertex.
|
||||
// Ports HyperIdealUtility.ζ_14(x, y).
|
||||
inline double zeta14(double x, double y)
|
||||
{
|
||||
double cy = std::cosh(y), sy = std::sinh(y);
|
||||
return std::acosh((std::exp(x) + cy) / sy);
|
||||
}
|
||||
|
||||
// ζ₁₅(x) — length in a hyperbolic quadrilateral with two ideal vertices.
|
||||
// Ports HyperIdealUtility.ζ_15(x).
|
||||
inline double zeta15(double x)
|
||||
{
|
||||
return 2.0 * std::asinh(std::exp(x / 2.0));
|
||||
}
|
||||
|
||||
// ── Effective edge length ─────────────────────────────────────────────────────
|
||||
|
||||
// l_ij: effective hyperbolic length of edge ij.
|
||||
// b_i, b_j – vertex log scale factors (used only if vertex is hyper-ideal)
|
||||
// a_ij – edge intersection-angle variable
|
||||
// vi_var – true if vertex i is hyper-ideal (has a DOF b_i)
|
||||
// vj_var – true if vertex j is hyper-ideal
|
||||
// Ports HyperIdealFunctional.lij().
|
||||
inline double lij(double bi, double bj, double aij, bool vi_var, bool vj_var)
|
||||
{
|
||||
if (vi_var && vj_var) return zeta13(bi, bj, aij);
|
||||
if (vi_var) return zeta14(aij, bi);
|
||||
if (vj_var) return zeta14(aij, bj);
|
||||
return zeta15(aij);
|
||||
}
|
||||
|
||||
// ── Auxiliary angle functions ─────────────────────────────────────────────────
|
||||
|
||||
// σᵢ(aᵢⱼ, aₖᵢ, aⱼₖ, vj_var, vk_var) — intermediate half-length at vertex i.
|
||||
// Ports HyperIdealFunctional.σi().
|
||||
inline double sigma_i(double aij, double aki, double ajk, bool vj_var, bool vk_var)
|
||||
{
|
||||
if (vj_var && vk_var) return zeta13(aij, aki, ajk);
|
||||
if (vj_var) return zeta14(ajk - aki, aij);
|
||||
if (vk_var) return zeta14(ajk - aij, aki);
|
||||
return zeta15(ajk - aij - aki);
|
||||
}
|
||||
|
||||
// σᵢⱼ(aᵢⱼ, bᵢ, bⱼ, vj_var) — intermediate half-length for edge ij from vertex i.
|
||||
// Ports HyperIdealFunctional.σij().
|
||||
inline double sigma_ij(double aij, double bi, double bj, bool vj_var)
|
||||
{
|
||||
if (vj_var) return zeta13(aij, bi, bj);
|
||||
return zeta14(-aij, bi);
|
||||
}
|
||||
|
||||
// α_ij: computed dihedral angle at edge ij in the face with vertices i, j, k.
|
||||
//
|
||||
// Arguments (cyclic role assignment):
|
||||
// aij, ajk, aki – edge variables
|
||||
// bi, bj, bk – vertex variables
|
||||
// βi, βj, βk – interior angles of the auxiliary hyperbolic triangle
|
||||
// vi_var, vj_var, vk_var – which vertices are hyper-ideal
|
||||
//
|
||||
// Ports HyperIdealFunctional.αij() (the private helper).
|
||||
// Note: the vk_var case recurses once (never more than one level deep).
|
||||
inline double alpha_ij(
|
||||
double aij, double ajk, double aki,
|
||||
double bi, double bj, double bk,
|
||||
double beta_i, double beta_j, double beta_k,
|
||||
bool vi_var, bool vj_var, bool vk_var)
|
||||
{
|
||||
if (vi_var) {
|
||||
double si = sigma_i (aij, aki, ajk, vj_var, vk_var);
|
||||
double sij = sigma_ij(aij, bi, bj, vj_var);
|
||||
double sik = sigma_ij(aki, bi, bk, vk_var);
|
||||
return zeta(si, sij, sik);
|
||||
}
|
||||
if (vj_var) {
|
||||
double sj = sigma_i (ajk, aij, aki, vk_var, vi_var);
|
||||
double sjk = sigma_ij(ajk, bj, bk, vk_var);
|
||||
double sji = sigma_ij(aij, bj, bi, vi_var);
|
||||
return zeta(sj, sji, sjk);
|
||||
}
|
||||
if (vk_var) {
|
||||
// Derive α_ij from α_jk (one level of recursion).
|
||||
double a_jk = alpha_ij(ajk, aki, aij,
|
||||
bj, bk, bi,
|
||||
beta_j, beta_k, beta_i,
|
||||
vj_var, vk_var, vi_var);
|
||||
return PI - a_jk - beta_j;
|
||||
}
|
||||
// All ideal: closed-form formula.
|
||||
return 0.5 * (PI + beta_k - beta_i - beta_j);
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
87
code/include/hyper_ideal_hessian.hpp
Normal file
87
code/include/hyper_ideal_hessian.hpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
// hyper_ideal_hessian.hpp
|
||||
//
|
||||
// Phase 4a — Hessian of the hyper-ideal discrete conformal functional.
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ Implementation strategy │
|
||||
// │ │
|
||||
// │ The hyper-ideal functional involves angle functions (ζ, σ, α, β) │
|
||||
// │ composed through several nested layers (lij → ζ13/14/15 → β/α). │
|
||||
// │ Deriving closed-form Hessian entries analytically through all these │
|
||||
// │ layers is feasible but lengthy; an analytical Hessian is left for a │
|
||||
// │ future phase. │
|
||||
// │ │
|
||||
// │ Here we compute the Hessian by symmetric finite differences of the │
|
||||
// │ gradient, which is exact to O(ε²) and sufficient for Newton's method │
|
||||
// │ at the meshes typical in Phase 4 (< 500 DOFs): │
|
||||
// │ │
|
||||
// │ H[i,j] = (G(x + ε·eⱼ)[i] − G(x − ε·eⱼ)[i]) / (2ε) │
|
||||
// │ │
|
||||
// │ The hyper-ideal energy is strictly convex (Springborn 2020), so H is │
|
||||
// │ positive semi-definite everywhere and Eigen::SimplicialLDLT applies │
|
||||
// │ directly. │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include <Eigen/Sparse>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Numerical Hessian via symmetric finite differences ────────────────────────
|
||||
//
|
||||
// Returns the n×n sparse Hessian, where n = hyper_ideal_dimension(mesh, m).
|
||||
// eps: finite-difference step size (default 1e-5 gives ~1e-10 relative error).
|
||||
inline Eigen::SparseMatrix<double> hyper_ideal_hessian(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const HyperIdealMaps& m,
|
||||
double eps = 1e-5)
|
||||
{
|
||||
const int n = hyper_ideal_dimension(mesh, m);
|
||||
std::vector<Eigen::Triplet<double>> trips;
|
||||
trips.reserve(static_cast<std::size_t>(n * n)); // dense upper bound
|
||||
|
||||
std::vector<double> xp = x, xm = x;
|
||||
|
||||
for (int j = 0; j < n; ++j) {
|
||||
const std::size_t sj = static_cast<std::size_t>(j);
|
||||
xp[sj] = x[sj] + eps;
|
||||
xm[sj] = x[sj] - eps;
|
||||
|
||||
auto Gp = evaluate_hyper_ideal(mesh, xp, m, /*energy=*/false).gradient;
|
||||
auto Gm = evaluate_hyper_ideal(mesh, xm, m, /*energy=*/false).gradient;
|
||||
|
||||
xp[sj] = xm[sj] = x[sj]; // restore
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
double val = (Gp[static_cast<std::size_t>(i)]
|
||||
- Gm[static_cast<std::size_t>(i)]) / (2.0 * eps);
|
||||
if (std::abs(val) > 1e-15)
|
||||
trips.emplace_back(i, j, val);
|
||||
}
|
||||
}
|
||||
|
||||
Eigen::SparseMatrix<double> H(n, n);
|
||||
H.setFromTriplets(trips.begin(), trips.end());
|
||||
return H;
|
||||
}
|
||||
|
||||
// ── Symmetrised Hessian ───────────────────────────────────────────────────────
|
||||
//
|
||||
// The FD Hessian is symmetric in exact arithmetic; floating-point rounding
|
||||
// can introduce tiny asymmetries. This helper returns (H + Hᵀ)/2.
|
||||
inline Eigen::SparseMatrix<double> hyper_ideal_hessian_sym(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const HyperIdealMaps& m,
|
||||
double eps = 1e-5)
|
||||
{
|
||||
auto H = hyper_ideal_hessian(mesh, x, m, eps);
|
||||
Eigen::SparseMatrix<double> Ht = H.transpose();
|
||||
return (H + Ht) * 0.5;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
@@ -4,6 +4,7 @@
|
||||
// Ported from de.varylab.discreteconformal.functional.HyperIdealUtility (Java).
|
||||
|
||||
#include "clausen.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
@@ -16,10 +17,10 @@ namespace conformallab {
|
||||
// Corresponds to Java HyperIdealUtility.calculateTetrahedronVolume().
|
||||
inline double calculateTetrahedronVolume(double A, double B, double C,
|
||||
double D, double E, double F) {
|
||||
constexpr double pi = 3.14159265358979323846264338328;
|
||||
// PI from constants.hpp (conformallab::PI)
|
||||
|
||||
// Degenerate if any angle equals pi.
|
||||
if (A == pi || B == pi || C == pi || D == pi || E == pi || F == pi)
|
||||
if (A == PI || B == PI || C == PI || D == PI || E == PI || F == PI)
|
||||
return 0.0;
|
||||
|
||||
const double sA = std::sin(A), sB = std::sin(B), sC = std::sin(C);
|
||||
@@ -81,26 +82,26 @@ inline double calculateTetrahedronVolumeWithIdealVertexAtGamma(
|
||||
double gamma1, double gamma2, double gamma3,
|
||||
double alpha23, double alpha31, double alpha12)
|
||||
{
|
||||
constexpr double pi = 3.14159265358979323846264338328;
|
||||
// PI from constants.hpp (conformallab::PI)
|
||||
auto L = [](double x) { return Lobachevsky(x); };
|
||||
|
||||
double result = L(gamma1) + L(gamma2) + L(gamma3);
|
||||
|
||||
result += L((pi + alpha31 - alpha12 - gamma1) / 2.0);
|
||||
result += L((pi + alpha12 - alpha23 - gamma2) / 2.0);
|
||||
result += L((pi + alpha23 - alpha31 - gamma3) / 2.0);
|
||||
result += L((PI + alpha31 - alpha12 - gamma1) / 2.0);
|
||||
result += L((PI + alpha12 - alpha23 - gamma2) / 2.0);
|
||||
result += L((PI + alpha23 - alpha31 - gamma3) / 2.0);
|
||||
|
||||
result += L((pi - alpha31 + alpha12 - gamma1) / 2.0);
|
||||
result += L((pi - alpha12 + alpha23 - gamma2) / 2.0);
|
||||
result += L((pi - alpha23 + alpha31 - gamma3) / 2.0);
|
||||
result += L((PI - alpha31 + alpha12 - gamma1) / 2.0);
|
||||
result += L((PI - alpha12 + alpha23 - gamma2) / 2.0);
|
||||
result += L((PI - alpha23 + alpha31 - gamma3) / 2.0);
|
||||
|
||||
result += L((pi + alpha31 + alpha12 - gamma1) / 2.0);
|
||||
result += L((pi + alpha12 + alpha23 - gamma2) / 2.0);
|
||||
result += L((pi + alpha23 + alpha31 - gamma3) / 2.0);
|
||||
result += L((PI + alpha31 + alpha12 - gamma1) / 2.0);
|
||||
result += L((PI + alpha12 + alpha23 - gamma2) / 2.0);
|
||||
result += L((PI + alpha23 + alpha31 - gamma3) / 2.0);
|
||||
|
||||
result += L((pi - alpha31 - alpha12 - gamma1) / 2.0);
|
||||
result += L((pi - alpha12 - alpha23 - gamma2) / 2.0);
|
||||
result += L((pi - alpha23 - alpha31 - gamma3) / 2.0);
|
||||
result += L((PI - alpha31 - alpha12 - gamma1) / 2.0);
|
||||
result += L((PI - alpha12 - alpha23 - gamma2) / 2.0);
|
||||
result += L((PI - alpha23 - alpha31 - gamma3) / 2.0);
|
||||
|
||||
return result / 2.0;
|
||||
}
|
||||
|
||||
129
code/include/hyper_ideal_visualization_utility.hpp
Normal file
129
code/include/hyper_ideal_visualization_utility.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#pragma once
|
||||
// Port of the static helper
|
||||
// HyperIdealVisualizationPlugin.getEuclideanCircleFromHyperbolic()
|
||||
// from de.varylab.discreteconformal.plugin.
|
||||
//
|
||||
// Converts a hyperbolic circle (center + radius in the hyperboloid model)
|
||||
// to its Euclidean representation (cx, cy, r) in the Poincaré disk model.
|
||||
//
|
||||
// Mathematical background
|
||||
// -----------------------
|
||||
// Hyperboloid model: points (x,y,z,w) with w²-x²-y²-z²=1, w>0.
|
||||
// Metric signature: g = diag(+1,+1,+1,−1) (spatial-first, time-last).
|
||||
//
|
||||
// Hyperbolic translation from the origin e₄=(0,0,0,1) to p=(a,b,c,d):
|
||||
// T = [ I₃ + p'·p'ᵀ/(d+1) p' ] p' = (a,b,c)
|
||||
// [ p'ᵀ d ]
|
||||
// This is the standard Lorentz boost; it is in O(3,1) and maps e₄ → p.
|
||||
//
|
||||
// Poincaré disk projection (jReality convention):
|
||||
// (x,y,z,w) → (x,y) / (w+1)
|
||||
//
|
||||
// The three reference points on the unit hyperbolic circle (at origin) are
|
||||
// p1 = (sinh r, 0, 0, cosh r)
|
||||
// p2 = (0, sinh r, 0, cosh r)
|
||||
// p3 = (-sinh r, 0, 0, cosh r)
|
||||
// After translation and projection to the Poincaré disk their circumcircle
|
||||
// equals the image of the original hyperbolic circle.
|
||||
|
||||
#include <Eigen/Dense>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Circumcenter of three 2-D points
|
||||
// ---------------------------------------------------------------------------
|
||||
inline Eigen::Vector2d circumcenter2d(
|
||||
const Eigen::Vector2d& a,
|
||||
const Eigen::Vector2d& b,
|
||||
const Eigen::Vector2d& c)
|
||||
{
|
||||
double ax = a.x(), ay = a.y();
|
||||
double bx = b.x(), by = b.y();
|
||||
double cx = c.x(), cy = c.y();
|
||||
|
||||
double D = 2.0 * (ax*(by - cy) + bx*(cy - ay) + cx*(ay - by));
|
||||
double a2 = ax*ax + ay*ay;
|
||||
double b2 = bx*bx + by*by;
|
||||
double c2 = cx*cx + cy*cy;
|
||||
|
||||
double ux = (a2*(by - cy) + b2*(cy - ay) + c2*(ay - by)) / D;
|
||||
double uy = (a2*(cx - bx) + b2*(ax - cx) + c2*(bx - ax)) / D;
|
||||
return {ux, uy};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 4×4 Lorentz boost: maps the hyperboloid origin e₄=(0,0,0,1) to `center`.
|
||||
// `center` must lie on the hyperboloid: center[3]² - ‖center.head<3>()‖² = 1.
|
||||
// ---------------------------------------------------------------------------
|
||||
inline Eigen::Matrix4d hyperboloidTranslation(const Eigen::Vector4d& center)
|
||||
{
|
||||
Eigen::Vector3d p = center.head<3>();
|
||||
double d = center(3);
|
||||
|
||||
Eigen::Matrix4d T = Eigen::Matrix4d::Identity();
|
||||
// Upper-left 3×3 block: I + p'·p'ᵀ / (d+1)
|
||||
T.block<3,3>(0,0) += p * p.transpose() / (d + 1.0);
|
||||
// Right column and bottom row
|
||||
T.block<3,1>(0,3) = p;
|
||||
T.block<1,3>(3,0) = p.transpose();
|
||||
T(3,3) = d;
|
||||
return T;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Project a hyperboloid point to the Poincaré disk (jReality convention:
|
||||
// add 1 to the w-coordinate, then dehomogenize the spatial part).
|
||||
// ---------------------------------------------------------------------------
|
||||
inline Eigen::Vector2d toPoincareDisk(const Eigen::Vector4d& x)
|
||||
{
|
||||
double w = x(3) + 1.0;
|
||||
return {x(0) / w, x(1) / w};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// getEuclideanCircleFromHyperbolic
|
||||
//
|
||||
// Inputs
|
||||
// center – point on the hyperboloid, e.g. (0,0,0,1) for the origin
|
||||
// radius – hyperbolic radius (real number > 0)
|
||||
//
|
||||
// Output
|
||||
// { euclidean_cx, euclidean_cy, euclidean_radius }
|
||||
// describing the circle in the Poincaré disk that corresponds to the
|
||||
// given hyperbolic circle.
|
||||
//
|
||||
// Port of HyperIdealVisualizationPlugin.getEuclideanCircleFromHyperbolic()
|
||||
// ---------------------------------------------------------------------------
|
||||
inline std::array<double,3> getEuclideanCircleFromHyperbolic(
|
||||
const Eigen::Vector4d& center, double radius)
|
||||
{
|
||||
const double s = std::sinh(radius);
|
||||
const double ch = std::cosh(radius);
|
||||
|
||||
// Three points on the hyperbolic circle centered at the origin
|
||||
Eigen::Vector4d p1( s, 0.0, 0.0, ch);
|
||||
Eigen::Vector4d p2(0.0, s, 0.0, ch);
|
||||
Eigen::Vector4d p3(-s, 0.0, 0.0, ch);
|
||||
|
||||
// Apply the hyperbolic translation to the target center
|
||||
const Eigen::Matrix4d T = hyperboloidTranslation(center);
|
||||
p1 = T * p1;
|
||||
p2 = T * p2;
|
||||
p3 = T * p3;
|
||||
|
||||
// Project to the Poincaré disk
|
||||
const Eigen::Vector2d q1 = toPoincareDisk(p1);
|
||||
const Eigen::Vector2d q2 = toPoincareDisk(p2);
|
||||
const Eigen::Vector2d q3 = toPoincareDisk(p3);
|
||||
|
||||
// Euclidean circumcircle of the three projected points
|
||||
const Eigen::Vector2d ec = circumcenter2d(q1, q2, q3);
|
||||
const double r = (ec - q1).norm();
|
||||
|
||||
return {ec.x(), ec.y(), r};
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
868
code/include/layout.hpp
Normal file
868
code/include/layout.hpp
Normal file
@@ -0,0 +1,868 @@
|
||||
#pragma once
|
||||
// layout.hpp
|
||||
//
|
||||
// Phase 5/6/7 — Layout / embedding: DOF vector → vertex coordinates in the
|
||||
// target geometry via BFS-trilateration.
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ Algorithm (all three geometries) │
|
||||
// │ │
|
||||
// │ 1. For every edge e compute the updated length l_e from the DOF x. │
|
||||
// │ 2. Choose root face = largest 3-D area face (quality heuristic). │
|
||||
// │ 3. Priority BFS over the dual graph: faces are processed in order of │
|
||||
// │ shortest distance (BFS depth) from the root face, minimising error │
|
||||
// │ accumulation. Equal depth: arbitrary tie-break. │
|
||||
// │ 4. For each face: trilaterate the one unplaced vertex from the two │
|
||||
// │ already-placed vertices on the shared edge. │
|
||||
// │ 5. If a CutGraph is supplied, cut edges are treated as boundary; │
|
||||
// │ holonomy is recorded for each cut edge. │
|
||||
// │ 6. After the first connected component, unvisited faces start new │
|
||||
// │ BFS sweeps (multi-component support). │
|
||||
// │ │
|
||||
// │ For open meshes: globally consistent placement. │
|
||||
// │ For closed meshes without CutGraph: first (shallowest) visit wins, │
|
||||
// │ has_seam = true. │
|
||||
// │ For closed meshes with CutGraph: each vertex placed exactly once; │
|
||||
// │ holonomy = translation (Euclidean) or Möbius map (hyperbolic). │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// Trilateration
|
||||
// Euclidean — exact (analytic formula in ℝ²)
|
||||
// Spherical — exact (spherical law of cosines on S²)
|
||||
// Hyperbolic — exact (hyperbolic law of cosines + Möbius maps,
|
||||
// Poincaré disk model)
|
||||
//
|
||||
// Normalisation
|
||||
// normalise_euclidean(layout) — centroid → origin, major axis → x-axis (PCA)
|
||||
// normalise_hyperbolic(layout) — face-area-weighted iterative Möbius centering
|
||||
// normalise_spherical(layout) — rotate centroid to north pole (Rodrigues)
|
||||
//
|
||||
// Holonomy (genus-g surfaces with CutGraph)
|
||||
// HolonomyData.translations[i] — translation ω_i (Euclidean / spherical)
|
||||
// HolonomyData.mobius_maps[i] — Möbius map T_i (hyperbolic)
|
||||
// For a flat torus: ω_1, ω_2 are the lattice generators; τ = ω_2/ω_1 ∈ ℍ.
|
||||
//
|
||||
// Texture atlas
|
||||
// Layout2D.halfedge_uv[h.idx()] — UV of source(h) as seen from face(h).
|
||||
// At seam edges the two opposite halfedges carry different UV values,
|
||||
// enabling proper per-halfedge UV for GPU texture atlasing.
|
||||
//
|
||||
// Möbius map
|
||||
// MobiusMap::from_three(z1→w1, z2→w2, z3→w3) — fit a Möbius transformation
|
||||
// to three point correspondences (via 3×3 complex linear system).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "cut_graph.hpp"
|
||||
#include <Eigen/Dense>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <complex>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Möbius map ────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// T(z) = (a·z + b) / (c·z + d)
|
||||
//
|
||||
// For hyperbolic holonomy the map is an orientation-preserving isometry of
|
||||
// the Poincaré disk (SU(1,1) element).
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
struct MobiusMap {
|
||||
using C = std::complex<double>;
|
||||
C a{1.0, 0.0};
|
||||
C b{0.0, 0.0};
|
||||
C c{0.0, 0.0};
|
||||
C d{1.0, 0.0};
|
||||
|
||||
C apply(C z) const { return (a * z + b) / (c * z + d); }
|
||||
|
||||
Eigen::Vector2d apply(const Eigen::Vector2d& p) const {
|
||||
C w = apply(C(p.x(), p.y()));
|
||||
return Eigen::Vector2d(w.real(), w.imag());
|
||||
}
|
||||
|
||||
static MobiusMap identity() { return {C(1), C(0), C(0), C(1)}; }
|
||||
|
||||
MobiusMap inverse() const { return {d, -b, -c, a}; }
|
||||
MobiusMap compose(const MobiusMap& T) const {
|
||||
return { a*T.a + b*T.c, a*T.b + b*T.d,
|
||||
c*T.a + d*T.c, c*T.b + d*T.d };
|
||||
}
|
||||
|
||||
bool is_identity(double tol = 1e-9) const {
|
||||
if (std::abs(d) < 1e-14) return false;
|
||||
C a_ = a/d, b_ = b/d, c_ = c/d;
|
||||
return std::abs(a_ - C(1)) < tol && std::abs(b_) < tol && std::abs(c_) < tol;
|
||||
}
|
||||
|
||||
/// Fit T to three point correspondences T(z_i) = w_i.
|
||||
/// Sets d=1 and solves the 3×3 complex linear system.
|
||||
/// Returns identity when the system is (near-)singular.
|
||||
static MobiusMap from_three(C z1, C w1, C z2, C w2, C z3, C w3) {
|
||||
Eigen::Matrix3cd M;
|
||||
M << z1, C(1), -w1*z1,
|
||||
z2, C(1), -w2*z2,
|
||||
z3, C(1), -w3*z3;
|
||||
Eigen::Vector3cd rhs; rhs << w1, w2, w3;
|
||||
Eigen::ColPivHouseholderQR<Eigen::Matrix3cd> qr(M);
|
||||
if (qr.rank() < 3) return identity();
|
||||
Eigen::Vector3cd sol = qr.solve(rhs);
|
||||
return { sol[0], sol[1], sol[2], C(1.0) };
|
||||
}
|
||||
};
|
||||
|
||||
// ── Result types ──────────────────────────────────────────────────────────────
|
||||
|
||||
struct Layout2D {
|
||||
/// uv[v.idx()] — primary 2-D position (first / shallowest-BFS-depth visit).
|
||||
std::vector<Eigen::Vector2d> uv;
|
||||
|
||||
/// halfedge_uv[h.idx()] — UV of source(h) as seen from face(h).
|
||||
///
|
||||
/// For interior (non-seam) halfedges: equals uv[source(h).idx()].
|
||||
/// For seam halfedges: carries the UV from the virtual unfolding across
|
||||
/// the cut (i.e. the trilaterated position that was NOT used as the
|
||||
/// primary uv). This gives each face its own copy of a seam vertex,
|
||||
/// enabling a proper GPU texture atlas without vertex duplication.
|
||||
///
|
||||
/// Size = mesh.number_of_halfedges(). Border halfedges = (0,0).
|
||||
std::vector<Eigen::Vector2d> halfedge_uv;
|
||||
|
||||
bool success = false;
|
||||
bool has_seam = false; ///< true when a vertex was reached via two paths
|
||||
};
|
||||
|
||||
struct Layout3D {
|
||||
std::vector<Eigen::Vector3d> pos;
|
||||
bool success = false;
|
||||
bool has_seam = false;
|
||||
};
|
||||
|
||||
/// Per-cut-edge holonomy.
|
||||
///
|
||||
/// Euclidean: translations[i] = ω_i (translation vector for cut_edge_indices[i]).
|
||||
/// For a flat torus ω_1, ω_2 are the lattice generators; τ = ω_2/ω_1 ∈ ℍ.
|
||||
///
|
||||
/// Hyperbolic: mobius_maps[i] = T_i (Möbius isometry of the Poincaré disk).
|
||||
/// T_i(p_actual) = p_virtual — maps the placed vertex position to the
|
||||
/// trilaterated virtual position obtained by continuing the unfolding across
|
||||
/// the cut.
|
||||
struct HolonomyData {
|
||||
std::vector<Eigen::Vector2d> translations; ///< Euclidean / spherical
|
||||
std::vector<MobiusMap> mobius_maps; ///< hyperbolic (Phase 7)
|
||||
std::vector<std::size_t> cut_edge_indices;
|
||||
};
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
namespace detail {
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Priority-BFS queue entry
|
||||
// Faces closer to the root (lower depth) are processed first, reducing the
|
||||
// accumulation of trilateration errors for later-placed vertices.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
struct BFSEntry {
|
||||
int depth; ///< BFS depth = max(depth[v_src], depth[v_tgt]) + 1
|
||||
Halfedge_index h; ///< halfedge pointing INTO the face to be placed
|
||||
/// min-heap: smaller depth = higher priority
|
||||
bool operator>(const BFSEntry& o) const { return depth > o.depth; }
|
||||
};
|
||||
|
||||
using BFSQueue = std::priority_queue<BFSEntry,
|
||||
std::vector<BFSEntry>,
|
||||
std::greater<BFSEntry>>;
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Euclidean trilateration — LEFT (CCW) side of p_a → p_b
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline Eigen::Vector2d trilaterate_2d(
|
||||
const Eigen::Vector2d& pa, const Eigen::Vector2d& pb,
|
||||
double da, double db)
|
||||
{
|
||||
Eigen::Vector2d ab = pb - pa;
|
||||
double d = ab.norm();
|
||||
if (d < 1e-14) return pa;
|
||||
Eigen::Vector2d e1 = ab / d;
|
||||
Eigen::Vector2d e2(-e1.y(), e1.x());
|
||||
double t = (da*da - db*db + d*d) / (2.0 * d);
|
||||
double s2 = da*da - t*t;
|
||||
double s = (s2 > 0.0) ? std::sqrt(s2) : 0.0;
|
||||
return pa + t*e1 + s*e2;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Spherical trilateration (S²) — LEFT side of geodesic arc pa → pb
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline Eigen::Vector3d trilaterate_sph(
|
||||
const Eigen::Vector3d& pa, const Eigen::Vector3d& pb,
|
||||
double da, double db)
|
||||
{
|
||||
double c = pa.dot(pb);
|
||||
double denom = 1.0 - c*c;
|
||||
if (denom < 1e-14) return pa;
|
||||
double cda = std::cos(da), cdb = std::cos(db);
|
||||
double alpha = (cda - c*cdb) / denom;
|
||||
double beta = (cdb - c*cda) / denom;
|
||||
Eigen::Vector3d cross = pa.cross(pb);
|
||||
double cn2 = cross.squaredNorm();
|
||||
double gamma2 = 1.0 - alpha*alpha - beta*beta - 2.0*alpha*beta*c;
|
||||
double gamma = (gamma2 > 0.0 && cn2 > 1e-28) ? std::sqrt(gamma2 / cn2) : 0.0;
|
||||
Eigen::Vector3d p = alpha*pa + beta*pb + gamma*cross;
|
||||
double n = p.norm();
|
||||
return (n > 1e-14) ? (p / n) : pa;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Hyperbolic trilateration — exact via Möbius + hyperbolic law of cosines.
|
||||
// LEFT (CCW) side of pa → pb in the Poincaré disk.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline Eigen::Vector2d trilaterate_hyp(
|
||||
const Eigen::Vector2d& pa, const Eigen::Vector2d& pb,
|
||||
double D, double da, double db)
|
||||
{
|
||||
using C = std::complex<double>;
|
||||
if (D < 1e-12 || da < 1e-12) return pa;
|
||||
if (std::sinh(da) * std::sinh(D) < 1e-14) return pa;
|
||||
C a(pa.x(), pa.y()), b(pb.x(), pb.y());
|
||||
auto fwd = [](C z, C c_) { return (z-c_)/(C(1)-std::conj(c_)*z); };
|
||||
auto inv = [](C w, C c_) { return (w+c_)/(C(1)+std::conj(c_)*w); };
|
||||
double theta_b = std::arg(fwd(b, a));
|
||||
double cos_alpha = (std::cosh(da)*std::cosh(D) - std::cosh(db))
|
||||
/ (std::sinh(da)*std::sinh(D));
|
||||
cos_alpha = std::max(-1.0, std::min(1.0, cos_alpha));
|
||||
C pc_origin = std::tanh(da*0.5) * std::exp(C(0.0, theta_b + std::acos(cos_alpha)));
|
||||
C pc_c = inv(pc_origin, a);
|
||||
return Eigen::Vector2d(pc_c.real(), pc_c.imag());
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// 3-D face area (cross product / 2)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline double face_3d_area(const ConformalMesh& mesh, Face_index f)
|
||||
{
|
||||
Halfedge_index h = mesh.halfedge(f);
|
||||
auto pA = mesh.point(mesh.source(h));
|
||||
auto pB = mesh.point(mesh.target(h));
|
||||
auto pC = mesh.point(mesh.target(mesh.next(h)));
|
||||
Eigen::Vector3d ab(pB.x()-pA.x(), pB.y()-pA.y(), pB.z()-pA.z());
|
||||
Eigen::Vector3d ac(pC.x()-pA.x(), pC.y()-pA.y(), pC.z()-pA.z());
|
||||
return 0.5 * ab.cross(ac).norm();
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Best root face: largest 3-D area, with 1.5× bonus for interior faces.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline Face_index best_root_face(const ConformalMesh& mesh)
|
||||
{
|
||||
Face_index best;
|
||||
double best_score = -1.0;
|
||||
for (auto f : mesh.faces()) {
|
||||
double area = face_3d_area(mesh, f);
|
||||
bool interior = true;
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
if (mesh.is_border(mesh.opposite(h))) { interior = false; break; }
|
||||
double score = area * (interior ? 1.5 : 1.0);
|
||||
if (score > best_score) { best_score = score; best = f; }
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Euclidean bounding box of placed vertices (for multi-component offset)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline void bbox_2d(const std::vector<Eigen::Vector2d>& uv,
|
||||
const std::vector<bool>& placed,
|
||||
double& xmax)
|
||||
{
|
||||
xmax = 0.0;
|
||||
for (std::size_t i = 0; i < uv.size(); ++i)
|
||||
if (placed[i]) xmax = std::max(xmax, uv[i].x());
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Möbius centering — unweighted (fallback / Phase 6 compat.)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline void center_poincare_disk(std::vector<Eigen::Vector2d>& uv)
|
||||
{
|
||||
if (uv.empty()) return;
|
||||
using C = std::complex<double>;
|
||||
C c(0);
|
||||
for (auto& p : uv) c += C(p.x(), p.y());
|
||||
c /= static_cast<double>(uv.size());
|
||||
double r = std::abs(c);
|
||||
if (r > 0.999) c *= 0.999/r;
|
||||
if (r < 1e-12) return;
|
||||
for (auto& p : uv) {
|
||||
C z(p.x(), p.y());
|
||||
C w = (z-c)/(C(1)-std::conj(c)*z);
|
||||
p = {w.real(), w.imag()};
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Face-area-weighted iterative Möbius centering (Fréchet mean, Phase 7).
|
||||
// weights[i] = Voronoi area of vertex i.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline void center_poincare_disk_weighted(
|
||||
std::vector<Eigen::Vector2d>& uv, const std::vector<double>& weights)
|
||||
{
|
||||
if (uv.empty()) return;
|
||||
using C = std::complex<double>;
|
||||
double total_w = 0.0; for (double w : weights) total_w += w;
|
||||
if (total_w < 1e-14) { center_poincare_disk(uv); return; }
|
||||
C c(0);
|
||||
for (int iter = 0; iter < 30; ++iter) {
|
||||
C mt(0);
|
||||
for (std::size_t i = 0; i < uv.size(); ++i) {
|
||||
C z(uv[i].x(), uv[i].y());
|
||||
mt += weights[i] * (z-c)/(C(1)-std::conj(c)*z);
|
||||
}
|
||||
mt /= total_w;
|
||||
C c_new = (mt+c)/(C(1)+std::conj(c)*mt);
|
||||
if (std::abs(c_new-c) < 1e-12) { c = c_new; break; }
|
||||
c = c_new;
|
||||
}
|
||||
double r = std::abs(c);
|
||||
if (r > 0.999) c *= 0.999/r;
|
||||
if (r < 1e-12) return;
|
||||
for (auto& p : uv) {
|
||||
C z(p.x(), p.y());
|
||||
C w = (z-c)/(C(1)-std::conj(c)*z);
|
||||
p = {w.real(), w.imag()};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// ── Vertex Voronoi area weights ───────────────────────────────────────────────
|
||||
inline std::vector<double> compute_vertex_area_weights(const ConformalMesh& mesh)
|
||||
{
|
||||
std::vector<double> w(mesh.number_of_vertices(), 0.0);
|
||||
for (auto f : mesh.faces()) {
|
||||
double area = detail::face_3d_area(mesh, f);
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
w[static_cast<std::size_t>(mesh.target(h).idx())] += area / 3.0;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
// ── Layout normalisation ──────────────────────────────────────────────────────
|
||||
|
||||
inline void normalise_euclidean(Layout2D& layout)
|
||||
{
|
||||
if (!layout.success || layout.uv.empty()) return;
|
||||
const std::size_t n = layout.uv.size();
|
||||
Eigen::Vector2d mean = Eigen::Vector2d::Zero();
|
||||
for (auto& p : layout.uv) mean += p;
|
||||
mean /= static_cast<double>(n);
|
||||
for (auto& p : layout.uv) p -= mean;
|
||||
for (auto& p : layout.halfedge_uv) p -= mean;
|
||||
Eigen::Matrix2d cov = Eigen::Matrix2d::Zero();
|
||||
for (auto& p : layout.uv) cov += p * p.transpose();
|
||||
cov /= static_cast<double>(n);
|
||||
Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> eig(cov);
|
||||
Eigen::Vector2d major = eig.eigenvectors().col(1);
|
||||
double angle = -std::atan2(major.y(), major.x());
|
||||
Eigen::Matrix2d R;
|
||||
R << std::cos(angle), -std::sin(angle),
|
||||
std::sin(angle), std::cos(angle);
|
||||
for (auto& p : layout.uv) p = R * p;
|
||||
for (auto& p : layout.halfedge_uv) p = R * p;
|
||||
}
|
||||
|
||||
/// Hyperbolic — face-area-weighted iterative Möbius centering.
|
||||
inline void normalise_hyperbolic(Layout2D& layout, const ConformalMesh& mesh)
|
||||
{
|
||||
if (!layout.success || layout.uv.empty()) return;
|
||||
auto weights = compute_vertex_area_weights(mesh);
|
||||
detail::center_poincare_disk_weighted(layout.uv, weights);
|
||||
// halfedge_uv follows the same Möbius map
|
||||
detail::center_poincare_disk(layout.halfedge_uv);
|
||||
}
|
||||
inline void normalise_hyperbolic(Layout2D& layout) // fallback without mesh
|
||||
{
|
||||
if (!layout.success || layout.uv.empty()) return;
|
||||
detail::center_poincare_disk(layout.uv);
|
||||
detail::center_poincare_disk(layout.halfedge_uv);
|
||||
}
|
||||
|
||||
inline void normalise_spherical(Layout3D& layout)
|
||||
{
|
||||
if (!layout.success || layout.pos.empty()) return;
|
||||
Eigen::Vector3d mean = Eigen::Vector3d::Zero();
|
||||
for (auto& p : layout.pos) mean += p;
|
||||
double len = mean.norm(); if (len < 1e-12) return;
|
||||
mean /= len;
|
||||
Eigen::Vector3d north(0, 0, 1);
|
||||
Eigen::Vector3d axis = mean.cross(north);
|
||||
double sin_a = axis.norm(), cos_a = mean.dot(north);
|
||||
if (sin_a < 1e-12) return;
|
||||
axis /= sin_a;
|
||||
Eigen::Matrix3d K;
|
||||
K << 0, -axis.z(), axis.y(),
|
||||
axis.z(), 0, -axis.x(),
|
||||
-axis.y(), axis.x(), 0;
|
||||
Eigen::Matrix3d Rot = Eigen::Matrix3d::Identity() + sin_a*K + (1-cos_a)*K*K;
|
||||
for (auto& p : layout.pos) p = Rot * p;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// BFS placement helpers shared across all three layout functions.
|
||||
//
|
||||
// set_face_huv: populate halfedge_uv for the three halfedges of face f,
|
||||
// given that the face was entered via halfedge h_enter.
|
||||
// h_enter: source=v_src, target=v_tgt → huv = uv[v_src]
|
||||
// next(h_enter): source=v_tgt, target=v_new → huv = uv[v_tgt]
|
||||
// prev(h_enter): source=v_new, target=v_src → huv = p_new (trilaterated)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
namespace detail {
|
||||
|
||||
inline void set_face_huv_2d(
|
||||
std::vector<Eigen::Vector2d>& huv,
|
||||
const ConformalMesh& mesh,
|
||||
Halfedge_index h,
|
||||
const std::vector<Eigen::Vector2d>& uv,
|
||||
const Eigen::Vector2d& p_new)
|
||||
{
|
||||
auto s = [](std::size_t x){ return x; };
|
||||
huv[s(static_cast<std::size_t>(h.idx()))] = uv[static_cast<std::size_t>(mesh.source(h).idx())];
|
||||
huv[s(static_cast<std::size_t>(mesh.next(h).idx()))] = uv[static_cast<std::size_t>(mesh.target(h).idx())];
|
||||
huv[s(static_cast<std::size_t>(mesh.prev(h).idx()))] = p_new;
|
||||
}
|
||||
|
||||
inline void set_root_huv_2d(
|
||||
std::vector<Eigen::Vector2d>& huv,
|
||||
const ConformalMesh& mesh,
|
||||
Face_index f,
|
||||
const std::vector<Eigen::Vector2d>& uv)
|
||||
{
|
||||
for (auto hf : CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
huv[static_cast<std::size_t>(hf.idx())] = uv[static_cast<std::size_t>(mesh.source(hf).idx())];
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// ── Euclidean layout ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Embed the mesh in ℝ² using the Euclidean metric encoded in x.
|
||||
///
|
||||
/// Runs priority-BFS trilateration: places vertices in order of BFS depth
|
||||
/// from the root face (largest 3D area), so errors accumulate last.
|
||||
/// For closed genus-g surfaces a CutGraph must be supplied — otherwise
|
||||
/// the layout will have a seam discontinuity (Layout2D::has_seam = true).
|
||||
///
|
||||
/// \param mesh Input surface mesh. Must have lambda0 and v_idx set in maps.
|
||||
/// \param x DOF vector returned by newton_euclidean().
|
||||
/// \param maps EuclideanMaps (lambda0, v_idx, e_idx).
|
||||
/// \param cut Optional cut graph (compute_cut_graph()). Pass nullptr for
|
||||
/// open meshes or if seams are acceptable.
|
||||
/// \param holonomy If non-null and cut != nullptr, receives the lattice
|
||||
/// translations ω_i ∈ ℂ per cut edge.
|
||||
/// Pass to compute_period_matrix() for the conformal modulus τ.
|
||||
/// \param normalise If true, calls normalise_euclidean() on the result:
|
||||
/// centroid → origin, major axis → x-axis (PCA).
|
||||
/// \return Layout2D with .uv[v] (per-vertex UV) and
|
||||
/// .halfedge_uv[h] (per-halfedge UV for texture atlasing).
|
||||
///
|
||||
/// \note halfedge_uv differs from uv at seam edges: the two sides of a cut
|
||||
/// carry different UV coordinates for proper GPU texture atlasing.
|
||||
inline Layout2D euclidean_layout(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const EuclideanMaps& maps,
|
||||
const CutGraph* cut = nullptr,
|
||||
HolonomyData* holonomy = nullptr,
|
||||
bool normalise = false)
|
||||
{
|
||||
const std::size_t nv = mesh.number_of_vertices();
|
||||
const std::size_t nf = mesh.number_of_faces();
|
||||
const std::size_t nh = mesh.number_of_halfedges();
|
||||
Layout2D result;
|
||||
result.uv.assign(nv, Eigen::Vector2d::Zero());
|
||||
result.halfedge_uv.assign(nh, Eigen::Vector2d::Zero());
|
||||
if (nf == 0) return result;
|
||||
|
||||
auto get_u = [&](Vertex_index v) {
|
||||
int iv = maps.v_idx[v]; return (iv>=0) ? x[static_cast<std::size_t>(iv)] : 0.0; };
|
||||
auto get_ue = [&](Edge_index e) {
|
||||
int ie = maps.e_idx[e]; return (ie>=0) ? x[static_cast<std::size_t>(ie)] : 0.0; };
|
||||
auto edge_len = [&](Halfedge_index h) {
|
||||
Edge_index e = mesh.edge(h);
|
||||
double lam = maps.lambda0[e] + get_u(mesh.source(h)) + get_u(mesh.target(h)) + get_ue(e);
|
||||
return std::exp(lam * 0.5); };
|
||||
|
||||
std::vector<bool> vertex_placed(nv, false);
|
||||
std::vector<bool> face_placed(nf, false);
|
||||
std::vector<int> vertex_depth(nv, std::numeric_limits<int>::max());
|
||||
|
||||
auto place_component = [&](Face_index f_root, Eigen::Vector2d offset) {
|
||||
Halfedge_index h0 = mesh.halfedge(f_root);
|
||||
Halfedge_index h1 = mesh.next(h0), h2 = mesh.next(h1);
|
||||
Vertex_index vA = mesh.source(h0), vB = mesh.source(h1), vC = mesh.source(h2);
|
||||
double lAB = edge_len(h0), lBC = edge_len(h1), lCA = edge_len(h2);
|
||||
result.uv[vA.idx()] = offset;
|
||||
result.uv[vB.idx()] = offset + Eigen::Vector2d(lAB, 0.0);
|
||||
result.uv[vC.idx()] = detail::trilaterate_2d(result.uv[vA.idx()], result.uv[vB.idx()], lCA, lBC);
|
||||
vertex_placed[vA.idx()] = vertex_placed[vB.idx()] = vertex_placed[vC.idx()] = true;
|
||||
vertex_depth[vA.idx()] = vertex_depth[vB.idx()] = vertex_depth[vC.idx()] = 0;
|
||||
face_placed[f_root.idx()] = true;
|
||||
detail::set_root_huv_2d(result.halfedge_uv, mesh, f_root, result.uv);
|
||||
|
||||
detail::BFSQueue pq;
|
||||
auto enqueue = [&](Face_index f) {
|
||||
for (auto hf : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) {
|
||||
Halfedge_index ho = mesh.opposite(hf);
|
||||
if (mesh.is_border(ho)) continue;
|
||||
if (cut && cut->is_cut(mesh.edge(hf))) continue;
|
||||
Face_index fadj = mesh.face(ho);
|
||||
if (!face_placed[fadj.idx()]) {
|
||||
int d = std::max(vertex_depth[mesh.source(ho).idx()],
|
||||
vertex_depth[mesh.target(ho).idx()]) + 1;
|
||||
pq.push({d, ho});
|
||||
}
|
||||
}
|
||||
};
|
||||
enqueue(f_root);
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [depth, h] = pq.top(); pq.pop();
|
||||
Face_index f = mesh.face(h);
|
||||
if (face_placed[f.idx()]) continue;
|
||||
|
||||
Vertex_index v_src = mesh.source(h), v_tgt = mesh.target(h);
|
||||
Vertex_index v_new = mesh.target(mesh.next(h));
|
||||
Eigen::Vector2d p = detail::trilaterate_2d(
|
||||
result.uv[v_src.idx()], result.uv[v_tgt.idx()],
|
||||
edge_len(mesh.prev(h)), edge_len(mesh.next(h)));
|
||||
|
||||
if (!vertex_placed[v_new.idx()]) {
|
||||
result.uv[v_new.idx()] = p;
|
||||
vertex_placed[v_new.idx()] = true;
|
||||
vertex_depth[v_new.idx()] = depth;
|
||||
} else {
|
||||
result.has_seam = true;
|
||||
}
|
||||
// halfedge_uv: p is the UV of v_new in THIS face (may differ from uv[v_new] at seam)
|
||||
detail::set_face_huv_2d(result.halfedge_uv, mesh, h, result.uv, p);
|
||||
face_placed[f.idx()] = true;
|
||||
enqueue(f);
|
||||
}
|
||||
};
|
||||
|
||||
place_component(detail::best_root_face(mesh), Eigen::Vector2d::Zero());
|
||||
for (auto f : mesh.faces()) {
|
||||
if (face_placed[f.idx()]) continue;
|
||||
double xmax; detail::bbox_2d(result.uv, vertex_placed, xmax);
|
||||
place_component(f, Eigen::Vector2d(xmax * 1.1 + 1.0, 0.0));
|
||||
}
|
||||
result.success = true;
|
||||
|
||||
// ── Holonomy ──────────────────────────────────────────────────────────────
|
||||
if (cut && holonomy) {
|
||||
holonomy->cut_edge_indices = cut->cut_edge_indices;
|
||||
holonomy->translations.clear();
|
||||
holonomy->translations.reserve(cut->cut_edge_indices.size());
|
||||
holonomy->mobius_maps.clear();
|
||||
for (std::size_t ce_idx : cut->cut_edge_indices) {
|
||||
Edge_index e = *std::next(mesh.edges().begin(), static_cast<std::ptrdiff_t>(ce_idx));
|
||||
Halfedge_index h = mesh.halfedge(e);
|
||||
Halfedge_index ho = mesh.opposite(h);
|
||||
Halfedge_index hx = mesh.is_border(ho) ? h : ho;
|
||||
if (mesh.is_border(hx)) { holonomy->translations.push_back(Eigen::Vector2d::Zero()); continue; }
|
||||
Vertex_index vs = mesh.source(hx), vt = mesh.target(hx), vn = mesh.target(mesh.next(hx));
|
||||
if (!vertex_placed[vn.idx()]) { holonomy->translations.push_back(Eigen::Vector2d::Zero()); continue; }
|
||||
Eigen::Vector2d p_tri = detail::trilaterate_2d(
|
||||
result.uv[vs.idx()], result.uv[vt.idx()],
|
||||
edge_len(mesh.prev(hx)), edge_len(mesh.next(hx)));
|
||||
// Store seam UV for the cut-crossing halfedges
|
||||
detail::set_face_huv_2d(result.halfedge_uv, mesh, hx, result.uv, p_tri);
|
||||
holonomy->translations.push_back(p_tri - result.uv[vn.idx()]);
|
||||
}
|
||||
}
|
||||
|
||||
if (normalise) normalise_euclidean(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── Spherical layout ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Embed the mesh on the unit sphere S² using the spherical metric encoded in x.
|
||||
///
|
||||
/// Runs priority-BFS trilateration using the spherical law of cosines.
|
||||
/// Typical use: genus-0 (sphere-like) surfaces after newton_spherical().
|
||||
///
|
||||
/// \param mesh Input genus-0 surface mesh.
|
||||
/// \param x DOF vector returned by newton_spherical().
|
||||
/// \param maps SphericalMaps.
|
||||
/// \param cut Optional cut graph (rarely needed for genus-0).
|
||||
/// \param holonomy If non-null, receives rotational holonomies (spherical).
|
||||
/// \param normalise If true, calls normalise_spherical(): rotates the centroid
|
||||
/// to the north pole (Rodrigues rotation formula).
|
||||
/// \return Layout3D with .xyz[v] ∈ S² ⊂ ℝ³ for each vertex.
|
||||
inline Layout3D spherical_layout(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& maps,
|
||||
const CutGraph* cut = nullptr,
|
||||
HolonomyData* holonomy = nullptr,
|
||||
bool normalise = false)
|
||||
{
|
||||
const std::size_t nv = mesh.number_of_vertices();
|
||||
const std::size_t nf = mesh.number_of_faces();
|
||||
Layout3D result;
|
||||
result.pos.assign(nv, Eigen::Vector3d::Zero());
|
||||
if (nf == 0) return result;
|
||||
|
||||
auto get_u = [&](Vertex_index v) {
|
||||
int iv = maps.v_idx[v]; return (iv>=0) ? x[static_cast<std::size_t>(iv)] : 0.0; };
|
||||
auto get_ue = [&](Edge_index e) {
|
||||
int ie = maps.e_idx[e]; return (ie>=0) ? x[static_cast<std::size_t>(ie)] : 0.0; };
|
||||
auto arc_len = [&](Halfedge_index h) {
|
||||
Edge_index e = mesh.edge(h);
|
||||
double lam = maps.lambda0[e] + get_u(mesh.source(h)) + get_u(mesh.target(h)) + get_ue(e);
|
||||
return 2.0 * std::asin(std::min(std::exp(lam*0.5), 1.0)); };
|
||||
|
||||
std::vector<bool> vertex_placed(nv, false);
|
||||
std::vector<bool> face_placed(nf, false);
|
||||
std::vector<int> vertex_depth(nv, std::numeric_limits<int>::max());
|
||||
|
||||
auto place_component = [&](Face_index f_root) {
|
||||
Halfedge_index h0 = mesh.halfedge(f_root);
|
||||
Halfedge_index h1 = mesh.next(h0), h2 = mesh.next(h1);
|
||||
Vertex_index vA = mesh.source(h0), vB = mesh.source(h1), vC = mesh.source(h2);
|
||||
double lAB = arc_len(h0), lBC = arc_len(h1), lCA = arc_len(h2);
|
||||
result.pos[vA.idx()] = Eigen::Vector3d(0, 0, 1);
|
||||
result.pos[vB.idx()] = Eigen::Vector3d(std::sin(lAB), 0, std::cos(lAB));
|
||||
result.pos[vC.idx()] = detail::trilaterate_sph(result.pos[vA.idx()], result.pos[vB.idx()], lCA, lBC);
|
||||
vertex_placed[vA.idx()] = vertex_placed[vB.idx()] = vertex_placed[vC.idx()] = true;
|
||||
vertex_depth[vA.idx()] = vertex_depth[vB.idx()] = vertex_depth[vC.idx()] = 0;
|
||||
face_placed[f_root.idx()] = true;
|
||||
|
||||
detail::BFSQueue pq;
|
||||
auto enqueue = [&](Face_index f) {
|
||||
for (auto hf : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) {
|
||||
Halfedge_index ho = mesh.opposite(hf);
|
||||
if (mesh.is_border(ho)) continue;
|
||||
if (cut && cut->is_cut(mesh.edge(hf))) continue;
|
||||
Face_index fadj = mesh.face(ho);
|
||||
if (!face_placed[fadj.idx()]) {
|
||||
int d = std::max(vertex_depth[mesh.source(ho).idx()],
|
||||
vertex_depth[mesh.target(ho).idx()]) + 1;
|
||||
pq.push({d, ho});
|
||||
}
|
||||
}
|
||||
};
|
||||
enqueue(f_root);
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [depth, h] = pq.top(); pq.pop();
|
||||
Face_index f = mesh.face(h);
|
||||
if (face_placed[f.idx()]) continue;
|
||||
Vertex_index vs = mesh.source(h), vt = mesh.target(h), vn = mesh.target(mesh.next(h));
|
||||
Eigen::Vector3d p = detail::trilaterate_sph(
|
||||
result.pos[vs.idx()], result.pos[vt.idx()],
|
||||
arc_len(mesh.prev(h)), arc_len(mesh.next(h)));
|
||||
if (!vertex_placed[vn.idx()]) {
|
||||
result.pos[vn.idx()] = p; vertex_placed[vn.idx()] = true; vertex_depth[vn.idx()] = depth;
|
||||
} else result.has_seam = true;
|
||||
face_placed[f.idx()] = true; enqueue(f);
|
||||
}
|
||||
};
|
||||
|
||||
place_component(detail::best_root_face(mesh));
|
||||
for (auto f : mesh.faces()) if (!face_placed[f.idx()]) place_component(f);
|
||||
result.success = true;
|
||||
|
||||
if (cut && holonomy) {
|
||||
holonomy->cut_edge_indices = cut->cut_edge_indices;
|
||||
holonomy->translations.clear();
|
||||
holonomy->translations.reserve(cut->cut_edge_indices.size());
|
||||
holonomy->mobius_maps.clear();
|
||||
for (std::size_t ce_idx : cut->cut_edge_indices) {
|
||||
Edge_index e = *std::next(mesh.edges().begin(), static_cast<std::ptrdiff_t>(ce_idx));
|
||||
Halfedge_index hx = mesh.is_border(mesh.opposite(mesh.halfedge(e)))
|
||||
? mesh.halfedge(e) : mesh.opposite(mesh.halfedge(e));
|
||||
if (mesh.is_border(hx)) { holonomy->translations.push_back(Eigen::Vector2d::Zero()); continue; }
|
||||
Vertex_index vs = mesh.source(hx), vt = mesh.target(hx), vn = mesh.target(mesh.next(hx));
|
||||
if (!vertex_placed[vn.idx()]) { holonomy->translations.push_back(Eigen::Vector2d::Zero()); continue; }
|
||||
Eigen::Vector3d p_tri = detail::trilaterate_sph(
|
||||
result.pos[vs.idx()], result.pos[vt.idx()],
|
||||
arc_len(mesh.prev(hx)), arc_len(mesh.next(hx)));
|
||||
Eigen::Vector3d diff = p_tri - result.pos[vn.idx()];
|
||||
holonomy->translations.push_back(Eigen::Vector2d(diff.x(), diff.y()));
|
||||
}
|
||||
}
|
||||
|
||||
if (normalise) normalise_spherical(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── HyperIdeal layout (Poincaré disk) — exact trilateration ──────────────────
|
||||
|
||||
/// Embed the mesh in the Poincaré disk (H²) using the hyperbolic metric encoded in x.
|
||||
///
|
||||
/// Runs priority-BFS trilateration using exact Möbius-isometric placement:
|
||||
/// each new vertex is located by solving the hyperbolic law of cosines and
|
||||
/// applying a Möbius map to position it in the disk.
|
||||
/// For closed genus-g surfaces (g ≥ 1) a CutGraph is required.
|
||||
///
|
||||
/// \param mesh Input genus-g surface mesh (g ≥ 1).
|
||||
/// \param x DOF vector returned by newton_hyper_ideal()
|
||||
/// (vertex b_v and edge a_e variables).
|
||||
/// \param maps HyperIdealMaps (lambda0, v_idx, e_idx).
|
||||
/// \param cut CutGraph from compute_cut_graph(). Required for closed surfaces.
|
||||
/// \param holonomy If non-null, receives the Möbius maps T_i ∈ SU(1,1) per cut edge.
|
||||
/// Pass to compute_period_matrix() for holonomy analysis.
|
||||
/// \param normalise If true, calls normalise_hyperbolic(): iterative face-area-weighted
|
||||
/// Möbius centring (Fréchet mean, 30 iterations) → disk origin.
|
||||
/// \return Layout2D with .uv[v] ∈ Poincaré disk (|uv| < 1) for each vertex,
|
||||
/// and .halfedge_uv[h] for seam-aware texture atlasing.
|
||||
///
|
||||
/// \note All vertex positions satisfy |uv[v]| < 1 (inside the Poincaré disk)
|
||||
/// if the metric is hyperbolic. Points on or outside the boundary indicate
|
||||
/// a non-hyperbolic metric (Gauss–Bonnet violation).
|
||||
inline Layout2D hyper_ideal_layout(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const HyperIdealMaps& maps,
|
||||
const CutGraph* cut = nullptr,
|
||||
HolonomyData* holonomy = nullptr,
|
||||
bool normalise = false)
|
||||
{
|
||||
const std::size_t nv = mesh.number_of_vertices();
|
||||
const std::size_t nf = mesh.number_of_faces();
|
||||
const std::size_t nh = mesh.number_of_halfedges();
|
||||
Layout2D result;
|
||||
result.uv.assign(nv, Eigen::Vector2d::Zero());
|
||||
result.halfedge_uv.assign(nh, Eigen::Vector2d::Zero());
|
||||
if (nf == 0) return result;
|
||||
|
||||
auto get_b = [&](Vertex_index v) {
|
||||
int iv = maps.v_idx[v]; return (iv>=0) ? x[static_cast<std::size_t>(iv)] : 0.0; };
|
||||
auto get_a = [&](Edge_index e) {
|
||||
int ie = maps.e_idx[e]; return (ie>=0) ? x[static_cast<std::size_t>(ie)] : 0.0; };
|
||||
auto hyp_dist = [&](Halfedge_index h) {
|
||||
double bi = get_b(mesh.source(h)), bj = get_b(mesh.target(h)), a = get_a(mesh.edge(h));
|
||||
return std::acosh(std::max(1.0, std::cosh(bi+a*0.5)*std::cosh(bj+a*0.5)-std::sinh(bi)*std::sinh(bj))); };
|
||||
|
||||
std::vector<bool> vertex_placed(nv, false);
|
||||
std::vector<bool> face_placed(nf, false);
|
||||
std::vector<int> vertex_depth(nv, std::numeric_limits<int>::max());
|
||||
|
||||
auto place_component = [&](Face_index f_root) {
|
||||
Halfedge_index h0 = mesh.halfedge(f_root);
|
||||
Halfedge_index h1 = mesh.next(h0), h2 = mesh.next(h1);
|
||||
Vertex_index vA = mesh.source(h0), vB = mesh.source(h1), vC = mesh.source(h2);
|
||||
double dAB = hyp_dist(h0), dBC = hyp_dist(h1), dCA = hyp_dist(h2);
|
||||
result.uv[vA.idx()] = Eigen::Vector2d::Zero();
|
||||
result.uv[vB.idx()] = Eigen::Vector2d(std::tanh(dAB*0.5), 0.0);
|
||||
result.uv[vC.idx()] = detail::trilaterate_hyp(result.uv[vA.idx()], result.uv[vB.idx()], dAB, dCA, dBC);
|
||||
vertex_placed[vA.idx()] = vertex_placed[vB.idx()] = vertex_placed[vC.idx()] = true;
|
||||
vertex_depth[vA.idx()] = vertex_depth[vB.idx()] = vertex_depth[vC.idx()] = 0;
|
||||
face_placed[f_root.idx()] = true;
|
||||
detail::set_root_huv_2d(result.halfedge_uv, mesh, f_root, result.uv);
|
||||
|
||||
detail::BFSQueue pq;
|
||||
auto enqueue = [&](Face_index f) {
|
||||
for (auto hf : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) {
|
||||
Halfedge_index ho = mesh.opposite(hf);
|
||||
if (mesh.is_border(ho)) continue;
|
||||
if (cut && cut->is_cut(mesh.edge(hf))) continue;
|
||||
Face_index fadj = mesh.face(ho);
|
||||
if (!face_placed[fadj.idx()]) {
|
||||
int d = std::max(vertex_depth[mesh.source(ho).idx()],
|
||||
vertex_depth[mesh.target(ho).idx()]) + 1;
|
||||
pq.push({d, ho});
|
||||
}
|
||||
}
|
||||
};
|
||||
enqueue(f_root);
|
||||
|
||||
while (!pq.empty()) {
|
||||
auto [depth, h] = pq.top(); pq.pop();
|
||||
Face_index f = mesh.face(h);
|
||||
if (face_placed[f.idx()]) continue;
|
||||
Vertex_index vs = mesh.source(h), vt = mesh.target(h), vn = mesh.target(mesh.next(h));
|
||||
double D = hyp_dist(h), da = hyp_dist(mesh.prev(h)), db = hyp_dist(mesh.next(h));
|
||||
Eigen::Vector2d p = detail::trilaterate_hyp(result.uv[vs.idx()], result.uv[vt.idx()], D, da, db);
|
||||
if (!vertex_placed[vn.idx()]) {
|
||||
result.uv[vn.idx()] = p; vertex_placed[vn.idx()] = true; vertex_depth[vn.idx()] = depth;
|
||||
} else result.has_seam = true;
|
||||
detail::set_face_huv_2d(result.halfedge_uv, mesh, h, result.uv, p);
|
||||
face_placed[f.idx()] = true; enqueue(f);
|
||||
}
|
||||
};
|
||||
|
||||
place_component(detail::best_root_face(mesh));
|
||||
for (auto f : mesh.faces()) if (!face_placed[f.idx()]) place_component(f);
|
||||
result.success = true;
|
||||
|
||||
// ── Möbius-map holonomy ───────────────────────────────────────────────────
|
||||
if (cut && holonomy) {
|
||||
holonomy->cut_edge_indices = cut->cut_edge_indices;
|
||||
holonomy->translations.clear();
|
||||
holonomy->mobius_maps.clear();
|
||||
holonomy->mobius_maps.reserve(cut->cut_edge_indices.size());
|
||||
for (std::size_t ce_idx : cut->cut_edge_indices) {
|
||||
Edge_index e = *std::next(mesh.edges().begin(), static_cast<std::ptrdiff_t>(ce_idx));
|
||||
Halfedge_index hcut = mesh.halfedge(e), ho = mesh.opposite(hcut);
|
||||
Halfedge_index hx = mesh.is_border(ho) ? hcut : ho;
|
||||
if (mesh.is_border(hx)) { holonomy->mobius_maps.push_back(MobiusMap::identity()); continue; }
|
||||
Vertex_index vs = mesh.source(hx), vt = mesh.target(hx), vn = mesh.target(mesh.next(hx));
|
||||
if (!vertex_placed[vn.idx()]) { holonomy->mobius_maps.push_back(MobiusMap::identity()); continue; }
|
||||
double D = hyp_dist(hx), da = hyp_dist(mesh.prev(hx)), db = hyp_dist(mesh.next(hx));
|
||||
Eigen::Vector2d p_tri = detail::trilaterate_hyp(result.uv[vs.idx()], result.uv[vt.idx()], D, da, db);
|
||||
detail::set_face_huv_2d(result.halfedge_uv, mesh, hx, result.uv, p_tri);
|
||||
using C = std::complex<double>;
|
||||
holonomy->mobius_maps.push_back(MobiusMap::from_three(
|
||||
C(result.uv[vs.idx()].x(), result.uv[vs.idx()].y()),
|
||||
C(result.uv[vs.idx()].x(), result.uv[vs.idx()].y()),
|
||||
C(result.uv[vt.idx()].x(), result.uv[vt.idx()].y()),
|
||||
C(result.uv[vt.idx()].x(), result.uv[vt.idx()].y()),
|
||||
C(result.uv[vn.idx()].x(), result.uv[vn.idx()].y()),
|
||||
C(p_tri.x(), p_tri.y())));
|
||||
}
|
||||
}
|
||||
|
||||
if (normalise) normalise_hyperbolic(result, mesh);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ── Convenience: save layout as OFF ──────────────────────────────────────────
|
||||
|
||||
inline void save_layout_off(
|
||||
const std::string& path, ConformalMesh& mesh, const Layout2D& layout)
|
||||
{
|
||||
std::ofstream ofs(path);
|
||||
ofs << "OFF\n" << mesh.number_of_vertices() << " " << mesh.number_of_faces() << " 0\n";
|
||||
for (auto v : mesh.vertices()) { auto& p = layout.uv[v.idx()]; ofs << p.x() << " " << p.y() << " 0\n"; }
|
||||
for (auto f : mesh.faces()) {
|
||||
ofs << "3";
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) ofs << " " << mesh.target(h).idx();
|
||||
ofs << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
inline void save_layout_off(
|
||||
const std::string& path, ConformalMesh& mesh, const Layout3D& layout)
|
||||
{
|
||||
std::ofstream ofs(path);
|
||||
ofs << "OFF\n" << mesh.number_of_vertices() << " " << mesh.number_of_faces() << " 0\n";
|
||||
for (auto v : mesh.vertices()) { auto& p = layout.pos[v.idx()]; ofs << p.x() << " " << p.y() << " " << p.z() << "\n"; }
|
||||
for (auto f : mesh.faces()) {
|
||||
ofs << "3";
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh)) ofs << " " << mesh.target(h).idx();
|
||||
ofs << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
150
code/include/mesh_builder.hpp
Normal file
150
code/include/mesh_builder.hpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#pragma once
|
||||
// mesh_builder.hpp
|
||||
//
|
||||
// Factory functions that build simple reference meshes for testing and examples.
|
||||
// All functions return a ConformalMesh (CGAL::Surface_mesh<Point3>).
|
||||
//
|
||||
// Replaces Java mesh generators:
|
||||
// CoHDS generators (convex hull, hyper-ideal generator) come later (Phase 3c/4).
|
||||
// These builders cover the minimal meshes needed for functional unit tests.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Single triangle ──────────────────────────────────────────────────────────
|
||||
//
|
||||
// v2
|
||||
// | \
|
||||
// | \
|
||||
// v0 ─ v1
|
||||
//
|
||||
// Returns a mesh with 1 face, 3 vertices, 3 edges.
|
||||
// The triangle lies in the xy-plane with a right angle at v0.
|
||||
inline ConformalMesh make_triangle(
|
||||
double x0=0, double y0=0,
|
||||
double x1=1, double y1=0,
|
||||
double x2=0, double y2=1)
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
auto v0 = mesh.add_vertex(Point3(x0, y0, 0));
|
||||
auto v1 = mesh.add_vertex(Point3(x1, y1, 0));
|
||||
auto v2 = mesh.add_vertex(Point3(x2, y2, 0));
|
||||
mesh.add_face(v0, v1, v2);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
// ── Regular tetrahedron ──────────────────────────────────────────────────────
|
||||
//
|
||||
// 4 vertices, 4 faces, 6 edges.
|
||||
// Euler characteristic: V - E + F = 4 - 6 + 4 = 2 (sphere topology).
|
||||
// Used to test closed-surface traversal.
|
||||
inline ConformalMesh make_tetrahedron()
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
|
||||
// Vertices of a regular tetrahedron centred at origin, edge length √2·2
|
||||
auto v0 = mesh.add_vertex(Point3( 1, 1, 1));
|
||||
auto v1 = mesh.add_vertex(Point3( 1, -1, -1));
|
||||
auto v2 = mesh.add_vertex(Point3(-1, 1, -1));
|
||||
auto v3 = mesh.add_vertex(Point3(-1, -1, 1));
|
||||
|
||||
// 4 outward-facing triangles (consistent winding)
|
||||
mesh.add_face(v0, v2, v1); // bottom (z=-1 side)
|
||||
mesh.add_face(v0, v1, v3); // front (y=-1 side)
|
||||
mesh.add_face(v0, v3, v2); // left (x=-1 side)
|
||||
mesh.add_face(v1, v2, v3); // back
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
// ── Two-triangle strip ───────────────────────────────────────────────────────
|
||||
//
|
||||
// v2 ─ v3
|
||||
// | \ |
|
||||
// v0 ─ v1
|
||||
//
|
||||
// 4 vertices, 2 faces, 5 edges (1 interior edge v1–v2 shared by both faces).
|
||||
// Useful for testing edge-interior vs edge-boundary distinction.
|
||||
inline ConformalMesh make_quad_strip()
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
auto v0 = mesh.add_vertex(Point3(0, 0, 0));
|
||||
auto v1 = mesh.add_vertex(Point3(1, 0, 0));
|
||||
auto v2 = mesh.add_vertex(Point3(0, 1, 0));
|
||||
auto v3 = mesh.add_vertex(Point3(1, 1, 0));
|
||||
|
||||
mesh.add_face(v0, v1, v2); // lower-left triangle
|
||||
mesh.add_face(v1, v3, v2); // upper-right triangle (shares edge v1–v2)
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
// ── Regular flat polygon fan ─────────────────────────────────────────────────
|
||||
//
|
||||
// n triangles sharing a central vertex; forms a disk topology (boundary).
|
||||
// Used to verify valence-n vertex traversal.
|
||||
inline ConformalMesh make_fan(int n)
|
||||
{
|
||||
CGAL_precondition(n >= 3);
|
||||
ConformalMesh mesh;
|
||||
|
||||
auto center = mesh.add_vertex(Point3(0, 0, 0));
|
||||
|
||||
const double dtheta = TWO_PI / n;
|
||||
std::vector<Vertex_index> rim(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
double a = i * dtheta;
|
||||
rim[i] = mesh.add_vertex(Point3(std::cos(a), std::sin(a), 0));
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
mesh.add_face(center, rim[i], rim[(i+1) % n]);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
// ── Spherical tetrahedron (vertices on the unit sphere) ───────────────────────
|
||||
//
|
||||
// The four vertices of a regular tetrahedron projected onto the unit sphere.
|
||||
// Starting from (±1,±1,±1), dividing by √3 gives unit-length positions.
|
||||
// All edge lengths equal arccos(−1/3) ≈ 1.9106 radians.
|
||||
// Used for SphericalFunctional tests (all four faces are valid spherical triangles).
|
||||
inline ConformalMesh make_spherical_tetrahedron()
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
const double s = 1.0 / std::sqrt(3.0);
|
||||
|
||||
auto v0 = mesh.add_vertex(Point3( s, s, s));
|
||||
auto v1 = mesh.add_vertex(Point3( s, -s, -s));
|
||||
auto v2 = mesh.add_vertex(Point3(-s, s, -s));
|
||||
auto v3 = mesh.add_vertex(Point3(-s, -s, s));
|
||||
|
||||
mesh.add_face(v0, v2, v1);
|
||||
mesh.add_face(v0, v1, v3);
|
||||
mesh.add_face(v0, v3, v2);
|
||||
mesh.add_face(v1, v2, v3);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
// ── Octahedron face triangle (vertices on the unit sphere) ────────────────────
|
||||
//
|
||||
// One face of a regular octahedron: the triangle (1,0,0)→(0,1,0)→(0,0,1).
|
||||
// All edge lengths equal arccos(0) = π/2.
|
||||
// The corner angles are all π/2 (right-angled spherical triangle).
|
||||
// base log-length: λ° = 2·log(sin(π/4)) = 2·log(1/√2) = −log(2) ≈ −0.6931.
|
||||
inline ConformalMesh make_octahedron_face()
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
auto v0 = mesh.add_vertex(Point3(1, 0, 0));
|
||||
auto v1 = mesh.add_vertex(Point3(0, 1, 0));
|
||||
auto v2 = mesh.add_vertex(Point3(0, 0, 1));
|
||||
mesh.add_face(v0, v1, v2);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
65
code/include/mesh_io.hpp
Normal file
65
code/include/mesh_io.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
// mesh_io.hpp
|
||||
//
|
||||
// Phase 4b — CGAL::IO wrappers for ConformalMesh.
|
||||
//
|
||||
// Reads and writes ConformalMesh (CGAL::Surface_mesh) in standard polygon-mesh
|
||||
// formats using the CGAL Polygon Mesh I/O utilities (CGAL 5.4+).
|
||||
//
|
||||
// Supported formats (detected by file extension):
|
||||
// .off — Object File Format (read + write)
|
||||
// .obj — Wavefront OBJ (read + write)
|
||||
// .ply — Polygon File Format (read + write)
|
||||
//
|
||||
// Usage:
|
||||
// ConformalMesh mesh;
|
||||
// if (!read_mesh("input.off", mesh)) throw std::runtime_error("read failed");
|
||||
// // ... process mesh ...
|
||||
// write_mesh("output.off", mesh);
|
||||
//
|
||||
// Note: property maps (lambda0, v_idx, etc.) are NOT serialised — they must
|
||||
// be re-initialised with setup_*_maps() + compute_lambda0_from_mesh() after
|
||||
// reading a file.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include <CGAL/IO/polygon_mesh_io.h>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Read ──────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// Reads a polygon mesh from file into `mesh` (clears any existing content).
|
||||
// Returns true on success, false on failure.
|
||||
inline bool read_mesh(const std::string& filename, ConformalMesh& mesh)
|
||||
{
|
||||
mesh.clear();
|
||||
return CGAL::IO::read_polygon_mesh(filename, mesh);
|
||||
}
|
||||
|
||||
// ── Write ─────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// Writes `mesh` to `filename`. Returns true on success.
|
||||
inline bool write_mesh(const std::string& filename, const ConformalMesh& mesh)
|
||||
{
|
||||
return CGAL::IO::write_polygon_mesh(filename, mesh);
|
||||
}
|
||||
|
||||
// ── Convenience: throwing wrappers ────────────────────────────────────────────
|
||||
|
||||
inline ConformalMesh load_mesh(const std::string& filename)
|
||||
{
|
||||
ConformalMesh mesh;
|
||||
if (!read_mesh(filename, mesh))
|
||||
throw std::runtime_error("conformallab: failed to read mesh from " + filename);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
inline void save_mesh(const std::string& filename, const ConformalMesh& mesh)
|
||||
{
|
||||
if (!write_mesh(filename, mesh))
|
||||
throw std::runtime_error("conformallab: failed to write mesh to " + filename);
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
378
code/include/newton_solver.hpp
Normal file
378
code/include/newton_solver.hpp
Normal file
@@ -0,0 +1,378 @@
|
||||
#pragma once
|
||||
// newton_solver.hpp
|
||||
//
|
||||
// Phase 4a — Newton solver for all three discrete conformal functionals.
|
||||
//
|
||||
// Solves G(x) = 0 where G is the gradient of the discrete conformal energy.
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ Gradient sign conventions │
|
||||
// │ Euclidean / Spherical: G_v = Θ_v − Σ α_v (target − actual) │
|
||||
// │ HyperIdeal: G_v = Σ β_v − Θ_v (actual − target) │
|
||||
// │ │
|
||||
// │ All solvers use the same Newton step Δx = −H⁻¹·G │
|
||||
// │ │
|
||||
// │ Hessian sign at equilibrium │
|
||||
// │ Euclidean: H PSD → SimplicialLDLT on H │
|
||||
// │ Spherical: H NSD → SimplicialLDLT on −H (solve (−H)Δx = G) │
|
||||
// │ HyperIdeal: H PSD → SimplicialLDLT on H (analytical H: future) │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// SparseQR fallback:
|
||||
// When SimplicialLDLT reports a failure (e.g. singular H on a closed mesh
|
||||
// without a pinned vertex), the solver automatically retries with
|
||||
// Eigen::SparseQR, which finds the minimum-norm Newton step orthogonal to
|
||||
// the null space. This handles the gauge mode on closed surfaces without
|
||||
// requiring the caller to pin a vertex explicitly.
|
||||
//
|
||||
// Requires:
|
||||
// Eigen::SimplicialLDLT, Eigen::SparseQR (Eigen sparse module)
|
||||
|
||||
#include "euclidean_hessian.hpp"
|
||||
#include "spherical_hessian.hpp"
|
||||
#include "hyper_ideal_hessian.hpp"
|
||||
#include <Eigen/SparseCholesky>
|
||||
#include <Eigen/SparseQR>
|
||||
#include <Eigen/OrderingMethods>
|
||||
#include <Eigen/Dense>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Result ────────────────────────────────────────────────────────────────────
|
||||
|
||||
struct NewtonResult {
|
||||
std::vector<double> x; ///< DOF vector at termination
|
||||
int iterations; ///< Newton steps taken
|
||||
double grad_inf_norm;///< max |G_i| at termination
|
||||
bool converged; ///< true iff grad_inf_norm < tol
|
||||
};
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Solve A·Δx = rhs with SimplicialLDLT; on failure fall back to SparseQR.
|
||||
// Returns Δx. ok is set to false only if both solvers fail.
|
||||
// If fallback_used is non-null, it is set to true iff SparseQR was needed.
|
||||
inline Eigen::VectorXd solve_with_fallback(
|
||||
const Eigen::SparseMatrix<double>& A,
|
||||
const Eigen::VectorXd& rhs,
|
||||
bool& ok,
|
||||
bool* fallback_used = nullptr)
|
||||
{
|
||||
if (fallback_used) *fallback_used = false;
|
||||
|
||||
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> ldlt(A);
|
||||
if (ldlt.info() == Eigen::Success) {
|
||||
Eigen::VectorXd dx = ldlt.solve(rhs);
|
||||
if (ldlt.info() == Eigen::Success) { ok = true; return dx; }
|
||||
}
|
||||
// Fallback: SparseQR — handles singular/rank-deficient H (gauge modes).
|
||||
if (fallback_used) *fallback_used = true;
|
||||
Eigen::SparseQR<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int>> qr(A);
|
||||
if (qr.info() == Eigen::Success) {
|
||||
Eigen::VectorXd dx = qr.solve(rhs);
|
||||
if (qr.info() == Eigen::Success) { ok = true; return dx; }
|
||||
}
|
||||
ok = false;
|
||||
return Eigen::VectorXd::Zero(rhs.size());
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// ── Public linear-system solver (SparseQR fallback) ──────────────────────────
|
||||
//
|
||||
// Solve A·x = rhs with Eigen::SimplicialLDLT; if that fails (singular or
|
||||
// rank-deficient A), retry with Eigen::SparseQR which finds the minimum-norm
|
||||
// solution orthogonal to the null space.
|
||||
//
|
||||
// This is the same primitive used internally by all three Newton solvers.
|
||||
// Exposing it publicly lets callers (tests, downstream code) reuse the logic
|
||||
// and — via the optional fallback_used pointer — verify which code path ran.
|
||||
//
|
||||
// fallback_used – if non-null, set to true iff SparseQR was invoked
|
||||
// Returns Eigen::VectorXd::Zero(rhs.size()) if both solvers fail.
|
||||
inline Eigen::VectorXd solve_linear_system(
|
||||
const Eigen::SparseMatrix<double>& A,
|
||||
const Eigen::VectorXd& rhs,
|
||||
bool* fallback_used = nullptr)
|
||||
{
|
||||
bool ok = false;
|
||||
return detail::solve_with_fallback(A, rhs, ok, fallback_used);
|
||||
}
|
||||
|
||||
namespace detail { // re-open for the remaining helpers
|
||||
|
||||
// Backtracking line search: find the largest α in {1, 0.5, 0.25, …} such that
|
||||
// ||G(x + α·Δx)||₂ < ||G(x)||₂. Returns the accepted step (α may stay 1).
|
||||
template <typename GradFn>
|
||||
inline std::vector<double> line_search(
|
||||
const std::vector<double>& x,
|
||||
const Eigen::VectorXd& dx,
|
||||
double norm0,
|
||||
GradFn&& grad_fn,
|
||||
int max_halvings = 20)
|
||||
{
|
||||
const int n = static_cast<int>(x.size());
|
||||
double alpha = 1.0;
|
||||
std::vector<double> xnew(static_cast<std::size_t>(n));
|
||||
|
||||
for (int ls = 0; ls < max_halvings; ++ls) {
|
||||
for (int i = 0; i < n; ++i)
|
||||
xnew[static_cast<std::size_t>(i)] = x[static_cast<std::size_t>(i)]
|
||||
+ alpha * dx[i];
|
||||
auto Gnew = grad_fn(xnew);
|
||||
double norm_new = 0.0;
|
||||
for (double v : Gnew) norm_new += v * v;
|
||||
norm_new = std::sqrt(norm_new);
|
||||
if (norm_new < norm0) return xnew;
|
||||
alpha *= 0.5;
|
||||
}
|
||||
// No improvement found — return best attempt (full step)
|
||||
for (int i = 0; i < n; ++i)
|
||||
xnew[static_cast<std::size_t>(i)] = x[static_cast<std::size_t>(i)] + dx[i];
|
||||
return xnew;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// ── Euclidean Newton solver ────────────────────────────────────────────────────
|
||||
|
||||
/// Solve the Euclidean discrete conformal problem: find u ∈ ℝ^V such that
|
||||
/// Σ_{faces adj v} α_v(u) = Θ_v for all vertices v.
|
||||
///
|
||||
/// Starting from x0, Newton's method minimises E(u) (the Euclidean DCE energy,
|
||||
/// which is convex) by iterating u ← u − H⁻¹·G with backtracking line search.
|
||||
/// The Hessian H is the cotangent Laplacian — PSD with one zero eigenvalue on
|
||||
/// closed surfaces (gauge mode). A SparseQR fallback handles this automatically.
|
||||
///
|
||||
/// \param mesh Input triangulated surface (edges must carry lambda0 + theta_v).
|
||||
/// \param x0 Initial DOF vector (length = number of free vertices).
|
||||
/// Pass all-zeros for a flat start (typical).
|
||||
/// \param m EuclideanMaps: lambda0[e], theta_v[v], v_idx[v] must be set.
|
||||
/// Call setup_euclidean_maps() + compute_euclidean_lambda0_from_mesh()
|
||||
/// + enforce_gauss_bonnet() before passing here.
|
||||
/// \param tol Convergence threshold on max |G_i|. Default: 1e-8.
|
||||
/// \param max_iter Maximum Newton iterations. Default: 200.
|
||||
/// \return NewtonResult{x*, iterations, grad_inf_norm, converged}.
|
||||
///
|
||||
/// \note On closed meshes without a pinned vertex, SimplicialLDLT detects the
|
||||
/// gauge singularity and falls back to SparseQR automatically.
|
||||
///
|
||||
/// \see doc/math/discrete-conformal-theory.md §3 for the mathematical background.
|
||||
inline NewtonResult newton_euclidean(
|
||||
ConformalMesh& mesh,
|
||||
std::vector<double> x0,
|
||||
const EuclideanMaps& m,
|
||||
double tol = 1e-8,
|
||||
int max_iter = 200)
|
||||
{
|
||||
std::vector<double> x = x0;
|
||||
const int n = static_cast<int>(x.size());
|
||||
|
||||
NewtonResult res;
|
||||
res.converged = false;
|
||||
res.iterations = 0;
|
||||
res.grad_inf_norm = 0.0;
|
||||
|
||||
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
|
||||
|
||||
for (int iter = 0; iter < max_iter; ++iter) {
|
||||
// ── Gradient ──────────────────────────────────────────────────────────
|
||||
auto G_std = euclidean_gradient(mesh, x, m);
|
||||
Eigen::Map<const Eigen::VectorXd> G(G_std.data(), n);
|
||||
|
||||
double inf_norm = G.cwiseAbs().maxCoeff();
|
||||
if (inf_norm < tol) {
|
||||
res.converged = true;
|
||||
res.grad_inf_norm = inf_norm;
|
||||
res.iterations = iter;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Hessian + solve H·Δx = −G (SparseQR fallback for singular H) ──
|
||||
auto H = euclidean_hessian(mesh, x, m);
|
||||
bool ok = false;
|
||||
Eigen::VectorXd dx = detail::solve_with_fallback(H, -G, ok);
|
||||
if (!ok) break;
|
||||
|
||||
// ── Backtracking line search ──────────────────────────────────────────
|
||||
double norm0 = G.norm();
|
||||
x = detail::line_search(x, dx, norm0,
|
||||
[&](const std::vector<double>& xnew) {
|
||||
return euclidean_gradient(mesh, xnew, m);
|
||||
});
|
||||
|
||||
res.iterations = iter + 1;
|
||||
}
|
||||
|
||||
// Report final gradient norm
|
||||
auto G_final = euclidean_gradient(mesh, x, m);
|
||||
double inf_final = 0.0;
|
||||
for (double v : G_final) inf_final = std::max(inf_final, std::abs(v));
|
||||
res.grad_inf_norm = inf_final;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Spherical Newton solver ───────────────────────────────────────────────────
|
||||
|
||||
/// Solve the spherical discrete conformal problem: find u ∈ ℝ^V such that
|
||||
/// Σ_{faces adj v} α_v(u) = Θ_v for all vertices v (genus-0 / sphere-like surfaces).
|
||||
///
|
||||
/// The spherical DCE energy is *concave*, so the Hessian H is NSD at the solution.
|
||||
/// The solver factorises −H (which is PSD) and solves (−H)·Δx = G.
|
||||
/// A gauge vertex must be pinned (set v_idx = -1) to remove the rotational mode.
|
||||
///
|
||||
/// \param mesh Input triangulated surface, genus 0.
|
||||
/// \param x0 Initial DOF vector (length = free vertices, excluding gauge_vertex).
|
||||
/// All-zeros is a good start.
|
||||
/// \param m SphericalMaps: lambda0[e], theta_v[v], v_idx[v], gauge_vertex set.
|
||||
/// Call setup_spherical_maps() + compute_spherical_lambda0_from_mesh()
|
||||
/// + enforce_gauss_bonnet() (checks Σ(2π-Θ) > 0) before passing here.
|
||||
/// \param tol Convergence threshold on max |G_i|. Default: 1e-8.
|
||||
/// \param max_iter Maximum Newton iterations. Default: 200.
|
||||
/// \return NewtonResult{x*, iterations, grad_inf_norm, converged}.
|
||||
///
|
||||
/// \note Unlike the Euclidean solver, the spherical solver does NOT need a SparseQR
|
||||
/// fallback — the gauge vertex pins the null mode directly.
|
||||
///
|
||||
/// \see doc/math/geometry-modes.md §Spherical for sign-convention details.
|
||||
inline NewtonResult newton_spherical(
|
||||
ConformalMesh& mesh,
|
||||
std::vector<double> x0,
|
||||
const SphericalMaps& m,
|
||||
double tol = 1e-8,
|
||||
int max_iter = 200)
|
||||
{
|
||||
std::vector<double> x = x0;
|
||||
const int n = static_cast<int>(x.size());
|
||||
|
||||
NewtonResult res;
|
||||
res.converged = false;
|
||||
res.iterations = 0;
|
||||
res.grad_inf_norm = 0.0;
|
||||
|
||||
for (int iter = 0; iter < max_iter; ++iter) {
|
||||
// ── Gradient ──────────────────────────────────────────────────────────
|
||||
auto G_std = spherical_gradient(mesh, x, m);
|
||||
Eigen::Map<const Eigen::VectorXd> G(G_std.data(), n);
|
||||
|
||||
double inf_norm = G.cwiseAbs().maxCoeff();
|
||||
if (inf_norm < tol) {
|
||||
res.converged = true;
|
||||
res.grad_inf_norm = inf_norm;
|
||||
res.iterations = iter;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Hessian: negate to get PSD; solve (−H)·Δx = G ──────────────────
|
||||
auto H = spherical_hessian(mesh, x, m);
|
||||
auto negH = Eigen::SparseMatrix<double>(-H);
|
||||
bool ok = false;
|
||||
Eigen::VectorXd dx = detail::solve_with_fallback(negH, G, ok);
|
||||
if (!ok) break;
|
||||
|
||||
// ── Backtracking line search ──────────────────────────────────────────
|
||||
double norm0 = G.norm();
|
||||
x = detail::line_search(x, dx, norm0,
|
||||
[&](const std::vector<double>& xnew) {
|
||||
return spherical_gradient(mesh, xnew, m);
|
||||
});
|
||||
|
||||
res.iterations = iter + 1;
|
||||
}
|
||||
|
||||
auto G_final = spherical_gradient(mesh, x, m);
|
||||
double inf_final = 0.0;
|
||||
for (double v : G_final) inf_final = std::max(inf_final, std::abs(v));
|
||||
res.grad_inf_norm = inf_final;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── HyperIdeal Newton solver ──────────────────────────────────────────────────
|
||||
|
||||
/// Solve the hyper-ideal discrete conformal problem: find (b, a) ∈ ℝ^{V+E} such that
|
||||
/// Σ β_v(b,a) = Θ_v and Σ α_e(b,a) = θ_e for all vertices v and edges e.
|
||||
///
|
||||
/// Used for genus-g surfaces (g ≥ 1) under hyperbolic cone metrics.
|
||||
/// The energy is *strictly convex* (Springborn 2020, Theorem 1.3), so Newton
|
||||
/// converges globally from any starting point.
|
||||
///
|
||||
/// DOF layout: first V_free entries are vertex variables b_v (hyper-ideal radii),
|
||||
/// followed by E entries for edge variables a_e (intersection angles).
|
||||
/// Use assign_all_dof_indices(mesh, maps) to set v_idx and e_idx automatically —
|
||||
/// no vertex needs to be pinned.
|
||||
///
|
||||
/// \param mesh Input triangulated surface, genus g ≥ 1.
|
||||
/// \param x0 Initial DOF vector (length = V + E). All-zeros typical.
|
||||
/// \param m HyperIdealMaps: lambda0[e], theta_v[v], v_idx[v], e_idx[e] set.
|
||||
/// Call setup_hyper_ideal_maps() + compute_hyper_ideal_lambda0_from_mesh().
|
||||
/// \param tol Convergence threshold on max |G_i|. Default: 1e-8.
|
||||
/// \param max_iter Maximum Newton iterations. Default: 200.
|
||||
/// \param hess_eps Finite-difference step for Hessian approximation. Default: 1e-5.
|
||||
/// (Phase 9b will replace this with an analytic Hessian.)
|
||||
/// \return NewtonResult{x*, iterations, grad_inf_norm, converged}.
|
||||
///
|
||||
/// \see Springborn (2020), Theorem 1.3 for the strict convexity proof.
|
||||
/// \see doc/math/geometry-modes.md §Hyper-ideal for DOF layout details.
|
||||
inline NewtonResult newton_hyper_ideal(
|
||||
ConformalMesh& mesh,
|
||||
std::vector<double> x0,
|
||||
const HyperIdealMaps& m,
|
||||
double tol = 1e-8,
|
||||
int max_iter = 200,
|
||||
double hess_eps = 1e-5)
|
||||
{
|
||||
std::vector<double> x = x0;
|
||||
const int n = static_cast<int>(x.size());
|
||||
|
||||
NewtonResult res;
|
||||
res.converged = false;
|
||||
res.iterations = 0;
|
||||
res.grad_inf_norm = 0.0;
|
||||
|
||||
for (int iter = 0; iter < max_iter; ++iter) {
|
||||
// ── Gradient ──────────────────────────────────────────────────────────
|
||||
auto G_std = evaluate_hyper_ideal(mesh, x, m, /*energy=*/false).gradient;
|
||||
Eigen::Map<const Eigen::VectorXd> G(G_std.data(), n);
|
||||
|
||||
double inf_norm = G.cwiseAbs().maxCoeff();
|
||||
if (inf_norm < tol) {
|
||||
res.converged = true;
|
||||
res.grad_inf_norm = inf_norm;
|
||||
res.iterations = iter;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Hessian (numerical FD) + solve H·Δx = −G ─────────────────────────
|
||||
auto H = hyper_ideal_hessian_sym(mesh, x, m, hess_eps);
|
||||
bool ok = false;
|
||||
Eigen::VectorXd dx = detail::solve_with_fallback(H, -G, ok);
|
||||
if (!ok) break;
|
||||
|
||||
// ── Backtracking line search ──────────────────────────────────────────
|
||||
double norm0 = G.norm();
|
||||
x = detail::line_search(x, dx, norm0,
|
||||
[&](const std::vector<double>& xnew) {
|
||||
return evaluate_hyper_ideal(mesh, xnew, m, false).gradient;
|
||||
});
|
||||
|
||||
res.iterations = iter + 1;
|
||||
}
|
||||
|
||||
auto G_final = evaluate_hyper_ideal(mesh, x, m, false).gradient;
|
||||
double inf_final = 0.0;
|
||||
for (double v : G_final) inf_final = std::max(inf_final, std::abs(v));
|
||||
res.grad_inf_norm = inf_final;
|
||||
res.x = x;
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
102
code/include/p2_utility.hpp
Normal file
102
code/include/p2_utility.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
// 2-D projective geometry utilities for the Euclidean signature.
|
||||
// Ported from de.jreality.math.P2 and de.varylab.discreteconformal.math.P2Big.
|
||||
//
|
||||
// Points and lines are represented as homogeneous 3-vectors (x, y, w).
|
||||
// In the Euclidean case a finite point (px, py) is stored as (px, py, 1).
|
||||
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Point / line duality ──────────────────────────────────────────────────────
|
||||
|
||||
// Intersection of two lines l1, l2 (or line through two points p1, p2)
|
||||
// via the cross product. Works for any P2 element.
|
||||
// Corresponds to Java P2.pointFromLines / P2.lineFromPoints.
|
||||
inline Eigen::Vector3d pointFromLines(const Eigen::Vector3d& l1,
|
||||
const Eigen::Vector3d& l2) {
|
||||
return l1.cross(l2);
|
||||
}
|
||||
|
||||
// ── Euclidean perpendicular bisector ─────────────────────────────────────────
|
||||
|
||||
// Returns the homogeneous line coordinates (a, b, c) of the perpendicular
|
||||
// bisector of the segment [p, q] in the Euclidean plane.
|
||||
// Coordinates: ax + by + c = 0 (after dehomogenizing p and q).
|
||||
//
|
||||
// Corresponds to Java P2.perpendicularBisector(p, q, Pn.EUCLIDEAN).
|
||||
inline Eigen::Vector3d perpendicularBisectorEuclidean(const Eigen::Vector3d& p_h,
|
||||
const Eigen::Vector3d& q_h) {
|
||||
// Dehomogenize
|
||||
Eigen::Vector2d p = p_h.head<2>() / p_h(2);
|
||||
Eigen::Vector2d q = q_h.head<2>() / q_h(2);
|
||||
|
||||
// Direction vector (p → direction, matching jReality sign convention)
|
||||
Eigen::Vector2d d = p - q;
|
||||
|
||||
// Midpoint
|
||||
Eigen::Vector2d m = (p + q) * 0.5;
|
||||
|
||||
// Line: d[0]*(x - m[0]) + d[1]*(y - m[1]) = 0
|
||||
// = d[0]*x + d[1]*y - (d[0]*m[0] + d[1]*m[1])
|
||||
double c = -(d(0) * m(0) + d(1) * m(1));
|
||||
return {d(0), d(1), c};
|
||||
}
|
||||
|
||||
// ── Euclidean distance between two P2 homogeneous points ─────────────────────
|
||||
|
||||
inline double euclideanDistanceP2(const Eigen::Vector3d& p_h,
|
||||
const Eigen::Vector3d& q_h) {
|
||||
Eigen::Vector2d p = p_h.head<2>() / p_h(2);
|
||||
Eigen::Vector2d q = q_h.head<2>() / q_h(2);
|
||||
return (p - q).norm();
|
||||
}
|
||||
|
||||
// ── Direct Euclidean isometry from two point-frames ──────────────────────────
|
||||
|
||||
// Build the 3×3 projective matrix that represents the coordinate frame
|
||||
// anchored at p0 with p1 defining the positive x-direction.
|
||||
// Euclidean case: columns are [dehom(p0), unit_dir(p0→p1), perp_dir].
|
||||
//
|
||||
// Template parameter S allows float / double / long double.
|
||||
template <typename S>
|
||||
Eigen::Matrix<S, 3, 3> makeFrameMatrix(Eigen::Matrix<S, 3, 1> p0_h,
|
||||
Eigen::Matrix<S, 3, 1> p1_h) {
|
||||
// Dehomogenize
|
||||
Eigen::Matrix<S, 3, 1> p0 = p0_h / p0_h(2); // (px, py, 1)
|
||||
Eigen::Matrix<S, 3, 1> p1_d = p1_h / p1_h(2);
|
||||
|
||||
// Unit direction p0 → p1
|
||||
Eigen::Matrix<S, 2, 1> dir2 = (p1_d - p0).template head<2>();
|
||||
dir2.normalize();
|
||||
Eigen::Matrix<S, 3, 1> p1n(dir2(0), dir2(1), S(0));
|
||||
|
||||
// Perpendicular direction
|
||||
Eigen::Matrix<S, 3, 1> p2(-dir2(1), dir2(0), S(0));
|
||||
|
||||
Eigen::Matrix<S, 3, 3> M;
|
||||
M.col(0) = p0;
|
||||
M.col(1) = p1n;
|
||||
M.col(2) = p2;
|
||||
return M;
|
||||
}
|
||||
|
||||
// Find the 3×3 Euclidean isometry (as a projective matrix) that maps
|
||||
// the frame (s1, s2) to the frame (t1, t2).
|
||||
//
|
||||
// Corresponds to Java P2.makeDirectIsometryFromFrames(s1, s2, t1, t2, Pn.EUCLIDEAN)
|
||||
// and P2Big.makeDirectIsometryFromFrames(...) (the BigDecimal / high-precision variant).
|
||||
template <typename S>
|
||||
Eigen::Matrix<S, 3, 3> makeDirectIsometryFromFramesEuclidean(
|
||||
Eigen::Matrix<S, 3, 1> s1, Eigen::Matrix<S, 3, 1> s2,
|
||||
Eigen::Matrix<S, 3, 1> t1, Eigen::Matrix<S, 3, 1> t2)
|
||||
{
|
||||
auto toS = makeFrameMatrix<S>(s1, s2);
|
||||
auto toT = makeFrameMatrix<S>(t1, t2);
|
||||
return toT * toS.inverse();
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
152
code/include/period_matrix.hpp
Normal file
152
code/include/period_matrix.hpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
// period_matrix.hpp
|
||||
//
|
||||
// Phase 7 — Period matrix for closed surfaces with Euclidean (flat) metric.
|
||||
//
|
||||
// For a closed genus-g surface with Euclidean conformal structure the holonomy
|
||||
// group is generated by 2g translations ω_1, ..., ω_{2g} ∈ ℂ ≅ ℝ².
|
||||
//
|
||||
// ─── Genus-1 (flat torus) ────────────────────────────────────────────────────
|
||||
//
|
||||
// The lattice Λ = ℤ·ω_1 ⊕ ℤ·ω_2 determines the conformal type.
|
||||
//
|
||||
// Period ratio: τ = ω_2 / ω_1 (as complex numbers)
|
||||
//
|
||||
// By convention choose ω_1 such that Im(τ) > 0.
|
||||
// The conformal modulus / Teichmüller parameter is the SL(2,ℤ)-orbit of τ.
|
||||
//
|
||||
// Reduction to fundamental domain {|τ| ≥ 1, −½ ≤ Re(τ) < ½, Im(τ) > 0}:
|
||||
// S: τ ↦ −1/τ (inversion)
|
||||
// T: τ ↦ τ + 1 (translation)
|
||||
// Apply S and T repeatedly until τ is in the fundamental domain.
|
||||
//
|
||||
// ─── Genus g > 1 ─────────────────────────────────────────────────────────────
|
||||
//
|
||||
// The full period matrix is a g×g complex symmetric matrix Ω with positive
|
||||
// definite imaginary part (Siegel upper half-space H_g).
|
||||
// Computing Ω from holonomy data requires integration of holomorphic
|
||||
// differentials — not implemented here. For g > 1, this function returns
|
||||
// only the 2×2 block for the first pair of generators.
|
||||
//
|
||||
// ─── API ─────────────────────────────────────────────────────────────────────
|
||||
//
|
||||
// PeriodData pd = compute_period_matrix(holonomy);
|
||||
// pd.tau — complex period ratio τ (genus 1)
|
||||
// pd.omega — holonomy generators as complex numbers (size = 2g)
|
||||
// pd.in_fundamental_domain — whether τ has been reduced
|
||||
//
|
||||
// std::complex<double> reduce_to_fundamental_domain(τ) — apply SL(2,ℤ)
|
||||
|
||||
#include "layout.hpp"
|
||||
#include <complex>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// PeriodData
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
struct PeriodData {
|
||||
/// Lattice generators as complex numbers (one per cut edge).
|
||||
/// omega[i] = translations[i].x() + i·translations[i].y()
|
||||
std::vector<std::complex<double>> omega;
|
||||
|
||||
/// Period ratio τ = omega[1] / omega[0] (genus-1 only).
|
||||
/// Undefined (NaN) for genus != 1 or if holonomy has fewer than 2 generators.
|
||||
std::complex<double> tau = std::complex<double>(
|
||||
std::numeric_limits<double>::quiet_NaN(), 0.0);
|
||||
|
||||
/// True if τ has been reduced to the standard fundamental domain.
|
||||
bool in_fundamental_domain = false;
|
||||
|
||||
int genus() const { return static_cast<int>(omega.size()) / 2; }
|
||||
};
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// reduce_to_fundamental_domain
|
||||
//
|
||||
// Applies SL(2,ℤ) generators S: τ↦−1/τ and T: τ↦τ+1 to bring τ into
|
||||
// F = { τ ∈ ℍ : |τ| ≥ 1, −½ ≤ Re(τ) < ½ }
|
||||
//
|
||||
// Returns the reduced τ. Throws if Im(τ) ≤ 0 (not in upper half-plane).
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline std::complex<double> reduce_to_fundamental_domain(std::complex<double> tau)
|
||||
{
|
||||
if (tau.imag() <= 0.0) {
|
||||
std::ostringstream msg;
|
||||
msg << "period_matrix: τ = " << tau.real() << " + " << tau.imag()
|
||||
<< "i is not in the upper half-plane (Im(τ) must be > 0).";
|
||||
throw std::domain_error(msg.str());
|
||||
}
|
||||
|
||||
// Iterate at most 200 times (convergence is rapid for well-conditioned τ)
|
||||
for (int k = 0; k < 200; ++k) {
|
||||
// T step: shift Re(τ) into [−½, ½)
|
||||
double re = tau.real();
|
||||
long n = static_cast<long>(std::floor(re + 0.5));
|
||||
tau -= std::complex<double>(static_cast<double>(n), 0.0);
|
||||
|
||||
// S step: if |τ| < 1, apply τ ← −1/τ
|
||||
if (std::abs(tau) < 1.0 - 1e-12) {
|
||||
tau = -1.0 / tau;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tau;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// is_in_fundamental_domain — check membership in F with tolerance tol.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline bool is_in_fundamental_domain(std::complex<double> tau, double tol = 1e-9)
|
||||
{
|
||||
if (tau.imag() <= 0.0) return false;
|
||||
if (std::abs(tau.real()) > 0.5 + tol) return false;
|
||||
if (std::abs(tau) < 1.0 - tol) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// compute_period_matrix
|
||||
//
|
||||
// Computes the period data from the Euclidean holonomy translations.
|
||||
// For genus-1 surfaces, also reduces τ to the fundamental domain.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
inline PeriodData compute_period_matrix(const HolonomyData& hol, bool reduce = true)
|
||||
{
|
||||
PeriodData pd;
|
||||
pd.omega.reserve(hol.translations.size());
|
||||
for (auto& t : hol.translations)
|
||||
pd.omega.push_back(std::complex<double>(t.x(), t.y()));
|
||||
|
||||
if (pd.omega.size() < 2) return pd; // need at least 2 generators
|
||||
|
||||
// τ = ω_2 / ω_1 — choose ω_1 such that Im(τ) > 0
|
||||
std::complex<double> w1 = pd.omega[0];
|
||||
std::complex<double> w2 = pd.omega[1];
|
||||
if (std::abs(w1) < 1e-14) return pd;
|
||||
|
||||
std::complex<double> tau = w2 / w1;
|
||||
if (tau.imag() < 0.0) {
|
||||
tau = std::conj(tau); // swap orientation
|
||||
w1 = std::conj(w1);
|
||||
w2 = std::conj(w2);
|
||||
pd.omega[0] = w1;
|
||||
pd.omega[1] = w2;
|
||||
}
|
||||
if (tau.imag() < 0.0) return pd; // degenerate
|
||||
|
||||
if (reduce) {
|
||||
tau = reduce_to_fundamental_domain(tau);
|
||||
pd.in_fundamental_domain = true;
|
||||
}
|
||||
pd.tau = tau;
|
||||
return pd;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
289
code/include/serialization.hpp
Normal file
289
code/include/serialization.hpp
Normal file
@@ -0,0 +1,289 @@
|
||||
#pragma once
|
||||
// serialization.hpp
|
||||
//
|
||||
// Phase 5 — Save and load conformal map results in JSON and XML formats.
|
||||
//
|
||||
// JSON (nlohmann/json, bundled in deps/single_includes/json.hpp):
|
||||
// save_result_json / load_result_json
|
||||
//
|
||||
// XML (hand-written, no external parser dependency):
|
||||
// save_result_xml / load_result_xml
|
||||
//
|
||||
// Both formats store:
|
||||
// - geometry type string ("euclidean" / "spherical" / "hyper_ideal")
|
||||
// - mesh statistics (vertex/face count)
|
||||
// - solver metadata (converged, iterations, grad_inf_norm)
|
||||
// - DOF vector x
|
||||
// - optional 2D or 3D layout positions
|
||||
//
|
||||
// XML schema (ConformalResult):
|
||||
//
|
||||
// <?xml version="1.0" encoding="UTF-8"?>
|
||||
// <ConformalResult geometry="euclidean" vertices="4" faces="2">
|
||||
// <Solver converged="true" iterations="3" grad_inf_norm="1.43e-13"/>
|
||||
// <DOFVector n="3">0.0 1.23e-13 2.87e-13</DOFVector>
|
||||
// <Layout dim="2" n="4">
|
||||
// 0.0 0.0 1.0 0.0 0.5 0.866 1.5 0.866
|
||||
// </Layout>
|
||||
// </ConformalResult>
|
||||
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include <json.hpp>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <iomanip>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// JSON
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
// Save solver result (+ optional 2D layout) to a JSON file.
|
||||
inline void save_result_json(
|
||||
const std::string& path,
|
||||
const NewtonResult& res,
|
||||
const std::string& geometry,
|
||||
int n_vertices,
|
||||
int n_faces,
|
||||
const Layout2D* layout2d = nullptr,
|
||||
const Layout3D* layout3d = nullptr)
|
||||
{
|
||||
using json = nlohmann::json;
|
||||
json j;
|
||||
j["geometry"] = geometry;
|
||||
j["mesh"] = { {"vertices", n_vertices}, {"faces", n_faces} };
|
||||
j["solver"] = {
|
||||
{"converged", res.converged},
|
||||
{"iterations", res.iterations},
|
||||
{"grad_inf_norm", res.grad_inf_norm}
|
||||
};
|
||||
j["dof_vector"] = res.x;
|
||||
|
||||
if (layout2d && layout2d->success) {
|
||||
json uv = json::array();
|
||||
for (auto& p : layout2d->uv) uv.push_back({p.x(), p.y()});
|
||||
j["layout"] = { {"dim", 2}, {"uv", uv}, {"has_seam", layout2d->has_seam} };
|
||||
}
|
||||
if (layout3d && layout3d->success) {
|
||||
json pos = json::array();
|
||||
for (auto& p : layout3d->pos) pos.push_back({p.x(), p.y(), p.z()});
|
||||
j["layout"] = { {"dim", 3}, {"pos", pos}, {"has_seam", layout3d->has_seam} };
|
||||
}
|
||||
|
||||
std::ofstream ofs(path);
|
||||
if (!ofs) throw std::runtime_error("Cannot write: " + path);
|
||||
ofs << std::setw(2) << j << "\n";
|
||||
}
|
||||
|
||||
// Load DOF vector from a JSON result file.
|
||||
// Returns the DOF vector; fills out res fields if non-null.
|
||||
inline std::vector<double> load_result_json(
|
||||
const std::string& path,
|
||||
NewtonResult* res = nullptr,
|
||||
std::string* geom = nullptr,
|
||||
Layout2D* layout2d = nullptr)
|
||||
{
|
||||
using json = nlohmann::json;
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs) throw std::runtime_error("Cannot open: " + path);
|
||||
json j; ifs >> j;
|
||||
|
||||
if (geom && j.contains("geometry"))
|
||||
*geom = j["geometry"].get<std::string>();
|
||||
|
||||
std::vector<double> x = j.at("dof_vector").get<std::vector<double>>();
|
||||
|
||||
if (res) {
|
||||
res->x = x;
|
||||
res->converged = j["solver"]["converged"].get<bool>();
|
||||
res->iterations = j["solver"]["iterations"].get<int>();
|
||||
res->grad_inf_norm = j["solver"]["grad_inf_norm"].get<double>();
|
||||
}
|
||||
|
||||
if (layout2d && j.contains("layout") && j["layout"]["dim"] == 2) {
|
||||
auto uv = j["layout"]["uv"];
|
||||
layout2d->uv.resize(uv.size());
|
||||
for (std::size_t i = 0; i < uv.size(); ++i)
|
||||
layout2d->uv[i] = { uv[i][0].get<double>(), uv[i][1].get<double>() };
|
||||
layout2d->has_seam = j["layout"].value("has_seam", false);
|
||||
layout2d->success = true;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// XML
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
namespace detail_xml {
|
||||
|
||||
// Minimal XML attribute escaping
|
||||
inline std::string xml_attr(double v)
|
||||
{
|
||||
std::ostringstream s;
|
||||
s << std::setprecision(15) << v;
|
||||
return s.str();
|
||||
}
|
||||
|
||||
// Write a flat vector of doubles as space-separated values
|
||||
inline std::string flat_doubles(const std::vector<double>& v)
|
||||
{
|
||||
std::ostringstream s;
|
||||
s << std::setprecision(15);
|
||||
for (std::size_t i = 0; i < v.size(); ++i) {
|
||||
if (i) s << ' ';
|
||||
s << v[i];
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
// Simple attribute parser: find value of key= in a tag string
|
||||
inline std::string xml_get_attr(const std::string& tag, const std::string& key)
|
||||
{
|
||||
auto pos = tag.find(key + "=\"");
|
||||
if (pos == std::string::npos) return {};
|
||||
pos += key.size() + 2;
|
||||
auto end = tag.find('"', pos);
|
||||
return tag.substr(pos, end - pos);
|
||||
}
|
||||
|
||||
// Read all text content between the current position and </tag>
|
||||
inline std::string xml_read_text(std::istream& is, const std::string& close_tag)
|
||||
{
|
||||
std::string buf, line;
|
||||
std::string ctag = "</" + close_tag + ">";
|
||||
while (std::getline(is, line)) {
|
||||
auto pos = line.find(ctag);
|
||||
if (pos != std::string::npos) {
|
||||
buf += line.substr(0, pos);
|
||||
return buf;
|
||||
}
|
||||
buf += line + " ";
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Parse space-separated doubles
|
||||
inline std::vector<double> parse_doubles(const std::string& s)
|
||||
{
|
||||
std::vector<double> v;
|
||||
std::istringstream ss(s);
|
||||
double d;
|
||||
while (ss >> d) v.push_back(d);
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace detail_xml
|
||||
|
||||
// Save solver result (+ optional layout) to an XML file.
|
||||
inline void save_result_xml(
|
||||
const std::string& path,
|
||||
const NewtonResult& res,
|
||||
const std::string& geometry,
|
||||
int n_vertices,
|
||||
int n_faces,
|
||||
const Layout2D* layout2d = nullptr,
|
||||
const Layout3D* layout3d = nullptr)
|
||||
{
|
||||
std::ofstream ofs(path);
|
||||
if (!ofs) throw std::runtime_error("Cannot write: " + path);
|
||||
ofs << std::setprecision(15);
|
||||
|
||||
ofs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||
ofs << "<ConformalResult"
|
||||
<< " geometry=\"" << geometry << "\""
|
||||
<< " vertices=\"" << n_vertices << "\""
|
||||
<< " faces=\"" << n_faces << "\">\n";
|
||||
|
||||
ofs << " <Solver"
|
||||
<< " converged=\"" << (res.converged ? "true" : "false") << "\""
|
||||
<< " iterations=\"" << res.iterations << "\""
|
||||
<< " grad_inf_norm=\"" << detail_xml::xml_attr(res.grad_inf_norm) << "\""
|
||||
<< "/>\n";
|
||||
|
||||
ofs << " <DOFVector n=\"" << res.x.size() << "\">"
|
||||
<< detail_xml::flat_doubles(res.x)
|
||||
<< "</DOFVector>\n";
|
||||
|
||||
if (layout2d && layout2d->success) {
|
||||
ofs << " <Layout dim=\"2\" n=\"" << layout2d->uv.size()
|
||||
<< "\" has_seam=\"" << (layout2d->has_seam ? "true" : "false") << "\">\n";
|
||||
for (auto& p : layout2d->uv)
|
||||
ofs << " " << p.x() << " " << p.y() << "\n";
|
||||
ofs << " </Layout>\n";
|
||||
}
|
||||
if (layout3d && layout3d->success) {
|
||||
ofs << " <Layout dim=\"3\" n=\"" << layout3d->pos.size()
|
||||
<< "\" has_seam=\"" << (layout3d->has_seam ? "true" : "false") << "\">\n";
|
||||
for (auto& p : layout3d->pos)
|
||||
ofs << " " << p.x() << " " << p.y() << " " << p.z() << "\n";
|
||||
ofs << " </Layout>\n";
|
||||
}
|
||||
|
||||
ofs << "</ConformalResult>\n";
|
||||
}
|
||||
|
||||
// Load solver result from an XML file written by save_result_xml.
|
||||
// Returns the DOF vector; fills res/geom/layout2d if non-null.
|
||||
inline std::vector<double> load_result_xml(
|
||||
const std::string& path,
|
||||
NewtonResult* res = nullptr,
|
||||
std::string* geom = nullptr,
|
||||
Layout2D* layout2d = nullptr)
|
||||
{
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs) throw std::runtime_error("Cannot open: " + path);
|
||||
|
||||
std::vector<double> x;
|
||||
std::string line;
|
||||
|
||||
while (std::getline(ifs, line)) {
|
||||
// Root element
|
||||
if (line.find("<ConformalResult") != std::string::npos) {
|
||||
if (geom) *geom = detail_xml::xml_get_attr(line, "geometry");
|
||||
}
|
||||
// Solver metadata
|
||||
else if (line.find("<Solver") != std::string::npos) {
|
||||
if (res) {
|
||||
res->converged = (detail_xml::xml_get_attr(line, "converged") == "true");
|
||||
res->iterations = std::stoi(detail_xml::xml_get_attr(line, "iterations"));
|
||||
res->grad_inf_norm = std::stod(detail_xml::xml_get_attr(line, "grad_inf_norm"));
|
||||
}
|
||||
}
|
||||
// DOF vector
|
||||
else if (line.find("<DOFVector") != std::string::npos) {
|
||||
// Text may be on same line: <DOFVector n="...">0 1 2...</DOFVector>
|
||||
auto open_end = line.find('>');
|
||||
auto close = line.find("</DOFVector>");
|
||||
std::string text;
|
||||
if (close != std::string::npos) {
|
||||
text = line.substr(open_end + 1, close - open_end - 1);
|
||||
} else {
|
||||
text = line.substr(open_end + 1);
|
||||
text += detail_xml::xml_read_text(ifs, "DOFVector");
|
||||
}
|
||||
x = detail_xml::parse_doubles(text);
|
||||
if (res) res->x = x;
|
||||
}
|
||||
// Layout
|
||||
else if (layout2d && line.find("<Layout") != std::string::npos
|
||||
&& detail_xml::xml_get_attr(line, "dim") == "2") {
|
||||
layout2d->has_seam = (detail_xml::xml_get_attr(line, "has_seam") == "true");
|
||||
std::string text = detail_xml::xml_read_text(ifs, "Layout");
|
||||
auto vals = detail_xml::parse_doubles(text);
|
||||
layout2d->uv.clear();
|
||||
for (std::size_t i = 0; i + 1 < vals.size(); i += 2)
|
||||
layout2d->uv.push_back({vals[i], vals[i+1]});
|
||||
layout2d->success = true;
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
475
code/include/spherical_functional.hpp
Normal file
475
code/include/spherical_functional.hpp
Normal file
@@ -0,0 +1,475 @@
|
||||
#pragma once
|
||||
// spherical_functional.hpp
|
||||
//
|
||||
// Energy and gradient of the spherical discrete conformal functional
|
||||
// evaluated on a ConformalMesh (CGAL::Surface_mesh).
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.SphericalFunctional.
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ DOFs │
|
||||
// │ x[v_idx[v]] = u_v – conformal factor at vertex v │
|
||||
// │ x[e_idx[e]] = λ_e – edge log-length variable (optional) │
|
||||
// │ -1 means "pinned" (u_v = 0 / λ_e = λ°_e fixed) │
|
||||
// │ │
|
||||
// │ Effective log-length: Λ_ij = λ°_ij + u_i + u_j │
|
||||
// │ Spherical arc length: l_ij = 2·asin(min(exp(Λ_ij/2), 1)) │
|
||||
// │ │
|
||||
// │ Gradient: │
|
||||
// │ ∂E/∂u_v = Θ_v – Σ_{faces adj. v} α_v(face) │
|
||||
// │ ∂E/∂λ_e = α_opp(face⁺) + α_opp(face⁻) – π │
|
||||
// │ │
|
||||
// │ Energy: │
|
||||
// │ Computed as the Schläfli path integral │
|
||||
// │ E(x) = ∫₀¹ ⟨G(tx), x⟩ dt │
|
||||
// │ using 10-point Gauss-Legendre quadrature. This is mathematically │
|
||||
// │ exact for any conservative (curl-free) gradient G and is numerically │
|
||||
// │ accurate to ~10⁻¹⁰ for smooth angle functions. The gradient check │
|
||||
// │ therefore tests curl-freeness of G, which is the key integrability │
|
||||
// │ condition for the spherical discrete conformal functional. │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "spherical_geometry.hpp"
|
||||
#include <CGAL/boost/graph/iterator.h>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Property-map type aliases ─────────────────────────────────────────────────
|
||||
|
||||
using SpherVMapD = ConformalMesh::Property_map<Vertex_index, double>;
|
||||
using SpherVMapI = ConformalMesh::Property_map<Vertex_index, int>;
|
||||
using SpherEMapD = ConformalMesh::Property_map<Edge_index, double>;
|
||||
using SpherEMapI = ConformalMesh::Property_map<Edge_index, int>;
|
||||
|
||||
// ── Persistent map bundle ─────────────────────────────────────────────────────
|
||||
|
||||
struct SphericalMaps {
|
||||
SpherVMapI v_idx; // DOF index per vertex (-1 = pinned / u_v = 0)
|
||||
SpherEMapI e_idx; // DOF index per edge (-1 = no edge DOF)
|
||||
SpherVMapD theta_v; // target cone angle Θ_v (default 2π)
|
||||
SpherEMapD theta_e; // target edge angle θ_e (default π)
|
||||
SpherEMapD lambda0; // base log-length λ°_e (default 0.0)
|
||||
};
|
||||
|
||||
// Defaults: theta_v = 2π, theta_e = π, lambda0 = 0.
|
||||
// lambda0 = 0 means exp(λ°/2)=1, i.e., l=π — degenerate unless u_i<0.
|
||||
// For real meshes, set lambda0 from mesh geometry via
|
||||
// compute_lambda0_from_mesh() below.
|
||||
inline SphericalMaps setup_spherical_maps(ConformalMesh& mesh)
|
||||
{
|
||||
SphericalMaps m;
|
||||
m.v_idx = mesh.add_property_map<Vertex_index, int> ("sv:idx", -1 ).first;
|
||||
m.e_idx = mesh.add_property_map<Edge_index, int> ("se:idx", -1 ).first;
|
||||
m.theta_v= mesh.add_property_map<Vertex_index, double>("sv:theta", 2.0*PI_SPHER).first;
|
||||
m.theta_e= mesh.add_property_map<Edge_index, double>("se:theta", PI_SPHER ).first;
|
||||
m.lambda0= mesh.add_property_map<Edge_index, double>("se:lam0", 0.0 ).first;
|
||||
return m;
|
||||
}
|
||||
|
||||
// Assign DOF indices 0..n-1 for all vertices (only vertex DOFs).
|
||||
inline int assign_vertex_dof_indices(ConformalMesh& mesh, SphericalMaps& m)
|
||||
{
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices()) m.v_idx[v] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// Assign DOF indices for all vertices AND edges.
|
||||
inline int assign_all_spherical_dof_indices(ConformalMesh& mesh, SphericalMaps& m)
|
||||
{
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices()) m.v_idx[v] = idx++;
|
||||
for (auto e : mesh.edges()) m.e_idx[e] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// Count variable DOFs.
|
||||
inline int spherical_dimension(const ConformalMesh& mesh, const SphericalMaps& m)
|
||||
{
|
||||
int dim = 0;
|
||||
for (auto v : mesh.vertices()) if (m.v_idx[v] >= 0) ++dim;
|
||||
for (auto e : mesh.edges()) if (m.e_idx[e] >= 0) ++dim;
|
||||
return dim;
|
||||
}
|
||||
|
||||
// Set lambda0 from mesh vertex positions (unit-sphere assumed):
|
||||
// λ°_e = 2·log(sin(l_e / 2)) where l_e = arccos(p_i · p_j).
|
||||
// Requires vertices to lie on the unit sphere.
|
||||
inline void compute_lambda0_from_mesh(ConformalMesh& mesh, SphericalMaps& m)
|
||||
{
|
||||
for (auto e : mesh.edges()) {
|
||||
auto h = mesh.halfedge(e);
|
||||
auto p1 = mesh.point(mesh.source(h));
|
||||
auto p2 = mesh.point(mesh.target(h));
|
||||
// Dot product (works for unit-sphere vertices).
|
||||
double dot = p1.x()*p2.x() + p1.y()*p2.y() + p1.z()*p2.z();
|
||||
dot = std::max(-1.0, std::min(1.0, dot));
|
||||
double l_e = std::acos(dot); // spherical arc length
|
||||
double half_sin = std::sin(l_e * 0.5); // = exp(λ°/2)
|
||||
if (half_sin > 1e-15)
|
||||
m.lambda0[e] = 2.0 * std::log(half_sin);
|
||||
else
|
||||
m.lambda0[e] = -30.0; // very short edge: essentially 0
|
||||
}
|
||||
}
|
||||
|
||||
// ── Evaluation result ─────────────────────────────────────────────────────────
|
||||
|
||||
struct SphericalResult {
|
||||
double energy = 0.0;
|
||||
std::vector<double> gradient;
|
||||
};
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
static inline double spher_dof_val(int idx, const std::vector<double>& x)
|
||||
{
|
||||
return idx >= 0 ? x[static_cast<std::size_t>(idx)] : 0.0;
|
||||
}
|
||||
|
||||
static inline std::size_t spher_hidx(Halfedge_index h)
|
||||
{
|
||||
return static_cast<std::size_t>(static_cast<std::uint32_t>(h));
|
||||
}
|
||||
|
||||
// ── Gradient only (no energy) ─────────────────────────────────────────────────
|
||||
|
||||
// Compute gradient G(x).
|
||||
// G_v = Θ_v − Σ_faces α_v(face)
|
||||
// G_e = α_opp(face+) + α_opp(face−) − θ_e
|
||||
//
|
||||
// The corner angle α_v is stored on halfedges using the convention:
|
||||
// h_alpha[h] = corner angle at source(prev(h)) = corner angle at the vertex
|
||||
// ACROSS FROM the edge of halfedge h in its face.
|
||||
// This convention makes both the vertex and edge gradient accumulators natural.
|
||||
inline std::vector<double> spherical_gradient(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& m)
|
||||
{
|
||||
const int n = spherical_dimension(mesh, m);
|
||||
std::vector<double> G(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
// Temporary per-halfedge corner-angle storage.
|
||||
// h_alpha[h] = corner angle at the vertex opposite to the edge of h.
|
||||
const std::size_t nh = mesh.number_of_halfedges();
|
||||
std::vector<double> h_alpha(nh, 0.0);
|
||||
|
||||
// ── Pass 1: compute corner angles per face ────────────────────────────────
|
||||
for (auto f : mesh.faces()) {
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0);
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
Edge_index e12 = mesh.edge(h0);
|
||||
Edge_index e23 = mesh.edge(h1);
|
||||
Edge_index e31 = mesh.edge(h2);
|
||||
|
||||
// Effective log-length Λ_ij = λ°_ij + u_i + u_j
|
||||
double u1 = spher_dof_val(m.v_idx[v1], x);
|
||||
double u2 = spher_dof_val(m.v_idx[v2], x);
|
||||
double u3 = spher_dof_val(m.v_idx[v3], x);
|
||||
|
||||
double lam12 = m.lambda0[e12] + u1 + u2 + spher_dof_val(m.e_idx[e12], x);
|
||||
double lam23 = m.lambda0[e23] + u2 + u3 + spher_dof_val(m.e_idx[e23], x);
|
||||
double lam31 = m.lambda0[e31] + u3 + u1 + spher_dof_val(m.e_idx[e31], x);
|
||||
|
||||
double l12 = spherical_l(lam12);
|
||||
double l23 = spherical_l(lam23);
|
||||
double l31 = spherical_l(lam31);
|
||||
|
||||
SphericalFaceAngles fa = spherical_angles(l12, l23, l31);
|
||||
|
||||
if (!fa.valid) continue; // degenerate face: contributes 0
|
||||
|
||||
// Store convention: h_alpha[h] = corner angle at source(prev(h))
|
||||
// h0 (e12): opposite vertex is v3 → source(prev(h0)) = source(h2) = v3 → α3
|
||||
// h1 (e23): opposite vertex is v1 → source(prev(h1)) = source(h0) = v1 → α1
|
||||
// h2 (e31): opposite vertex is v2 → source(prev(h2)) = source(h1) = v2 → α2
|
||||
h_alpha[spher_hidx(h0)] = fa.alpha3;
|
||||
h_alpha[spher_hidx(h1)] = fa.alpha1;
|
||||
h_alpha[spher_hidx(h2)] = fa.alpha2;
|
||||
}
|
||||
|
||||
// ── Pass 2: accumulate gradient ───────────────────────────────────────────
|
||||
|
||||
// Vertex: G_v = Θ_v − Σ h_alpha[prev(h)] for each incoming non-border h to v.
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
double sum_alpha = 0.0;
|
||||
for (auto h : CGAL::halfedges_around_target(v, mesh)) {
|
||||
if (mesh.is_border(h)) continue;
|
||||
sum_alpha += h_alpha[spher_hidx(mesh.prev(h))];
|
||||
}
|
||||
G[static_cast<std::size_t>(iv)] = m.theta_v[v] - sum_alpha;
|
||||
}
|
||||
|
||||
// Edge: G_e for λ_e additive (Λ_ij = λ°_ij + u_i + u_j + λ_e).
|
||||
//
|
||||
// From the Schläfli identity applied to the spherical face,
|
||||
// the contribution of edge DOF λ_e from face f is:
|
||||
// a_f = (2·α_opp − S_f) / 2 where S_f = Σ angles in face f.
|
||||
//
|
||||
// Summing over both adjacent faces:
|
||||
// G_e = a_f+ + a_f−
|
||||
// = α_opp⁺ + α_opp⁻ − (S_f⁺ + S_f⁻) / 2 − θ_e
|
||||
//
|
||||
// For flat (Euclidean) triangles S_f = π, recovering the familiar
|
||||
// α_opp⁺ + α_opp⁻ − π formula. For spherical triangles S_f > π.
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = m.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
auto h = mesh.halfedge(e);
|
||||
auto ho = mesh.opposite(h);
|
||||
double sum = 0.0;
|
||||
if (!mesh.is_border(h)) {
|
||||
double alpha_opp = h_alpha[spher_hidx(h)];
|
||||
double S_f = alpha_opp
|
||||
+ h_alpha[spher_hidx(mesh.next(h))]
|
||||
+ h_alpha[spher_hidx(mesh.prev(h))];
|
||||
sum += (2.0 * alpha_opp - S_f) * 0.5;
|
||||
}
|
||||
if (!mesh.is_border(ho)) {
|
||||
double alpha_opp = h_alpha[spher_hidx(ho)];
|
||||
double S_f = alpha_opp
|
||||
+ h_alpha[spher_hidx(mesh.next(ho))]
|
||||
+ h_alpha[spher_hidx(mesh.prev(ho))];
|
||||
sum += (2.0 * alpha_opp - S_f) * 0.5;
|
||||
}
|
||||
G[static_cast<std::size_t>(ie)] = sum - m.theta_e[e];
|
||||
}
|
||||
|
||||
return G;
|
||||
}
|
||||
|
||||
// ── Energy via Gauss-Legendre path integral ───────────────────────────────────
|
||||
//
|
||||
// E(x) = ∫₀¹ ⟨G(tx), x⟩ dt
|
||||
//
|
||||
// This is the correct potential for any conservative G = ∇E.
|
||||
// Uses 10-point Gauss-Legendre quadrature; error ≈ O(h²⁰) for smooth G.
|
||||
//
|
||||
// 10-point GL nodes and weights on [0, 1] (transformed from [-1, 1]):
|
||||
// t_k = (1 + s_k) / 2, w_k = w_GL_k / 2
|
||||
inline double spherical_energy(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& m)
|
||||
{
|
||||
// 10-point Gauss-Legendre nodes and weights on [-1, 1].
|
||||
static const double gl_s[10] = {
|
||||
-0.9739065285171717, -0.8650633666889845,
|
||||
-0.6794095682990244, -0.4333953941292472,
|
||||
-0.1488743389816312, 0.1488743389816312,
|
||||
0.4333953941292472, 0.6794095682990244,
|
||||
0.8650633666889845, 0.9739065285171717
|
||||
};
|
||||
static const double gl_w[10] = {
|
||||
0.0666713443086881, 0.1494513491505806,
|
||||
0.2190863625159820, 0.2692667193099963,
|
||||
0.2955242247147529, 0.2955242247147529,
|
||||
0.2692667193099963, 0.2190863625159820,
|
||||
0.1494513491505806, 0.0666713443086881
|
||||
};
|
||||
|
||||
const std::size_t n = x.size();
|
||||
double E = 0.0;
|
||||
|
||||
for (int k = 0; k < 10; ++k) {
|
||||
double t = (1.0 + gl_s[k]) * 0.5; // node on [0, 1]
|
||||
double wt = gl_w[k] * 0.5; // weight on [0, 1]
|
||||
|
||||
// Evaluate G(t·x)
|
||||
std::vector<double> tx(n);
|
||||
for (std::size_t i = 0; i < n; ++i) tx[i] = t * x[i];
|
||||
|
||||
auto G = spherical_gradient(mesh, tx, m);
|
||||
|
||||
// Accumulate ⟨G(tx), x⟩ · wt
|
||||
double dot = 0.0;
|
||||
for (std::size_t i = 0; i < n; ++i) dot += G[i] * x[i];
|
||||
E += wt * dot;
|
||||
}
|
||||
return E;
|
||||
}
|
||||
|
||||
// ── Full evaluation (energy + gradient) ──────────────────────────────────────
|
||||
|
||||
inline SphericalResult evaluate_spherical(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& m,
|
||||
bool need_energy = true,
|
||||
bool need_gradient = true)
|
||||
{
|
||||
SphericalResult res;
|
||||
if (need_gradient)
|
||||
res.gradient = spherical_gradient(mesh, x, m);
|
||||
if (need_energy)
|
||||
res.energy = spherical_energy(mesh, x, m);
|
||||
return res;
|
||||
}
|
||||
|
||||
// ── Finite-difference gradient check ─────────────────────────────────────────
|
||||
//
|
||||
// Tests |G[i] − fd[i]| / max(1, |G[i]|) < tol for all DOFs.
|
||||
// Same defaults as the hyper-ideal gradient check (Java FunctionalTest).
|
||||
inline bool gradient_check_spherical(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x0,
|
||||
const SphericalMaps& m,
|
||||
double eps = 1E-5,
|
||||
double tol = 1E-4)
|
||||
{
|
||||
auto G = spherical_gradient(mesh, x0, m);
|
||||
const int n = static_cast<int>(G.size());
|
||||
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
bool ok = true;
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::size_t si = static_cast<std::size_t>(i);
|
||||
xp[si] = x0[si] + eps;
|
||||
xm[si] = x0[si] - eps;
|
||||
|
||||
double Ep = spherical_energy(mesh, xp, m);
|
||||
double Em = spherical_energy(mesh, xm, m);
|
||||
|
||||
xp[si] = xm[si] = x0[si]; // restore
|
||||
|
||||
double fd = (Ep - Em) / (2.0 * eps);
|
||||
double err = std::abs(G[si] - fd);
|
||||
double scale = std::max(1.0, std::abs(G[si]));
|
||||
if (err / scale > tol) ok = false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
// ── Gauge-fix for closed spherical surfaces ───────────────────────────────────
|
||||
//
|
||||
// On a closed (boundaryless) spherical surface the energy E(u + t·1) has a
|
||||
// unique maximum w.r.t. t ∈ ℝ (the "global scale" gauge mode). Without
|
||||
// fixing this gauge the functional is unbounded below and no solver converges.
|
||||
//
|
||||
// This function returns the scalar shift t* such that
|
||||
// Σ_v G_v(x + t*·1_v) = 0
|
||||
// i.e., the derivative of E(u+t·1) w.r.t. t is zero at t = t*.
|
||||
//
|
||||
// Apply the shift by adding t* to every vertex DOF in x.
|
||||
//
|
||||
// Implementation: bisection on f(t) = Σ_v G_v(x + t·1_v).
|
||||
// f is strictly monotone decreasing (second derivative < 0) for a convex
|
||||
// functional, so bisection converges in O(log₂(2·bracket/tol)) iterations.
|
||||
//
|
||||
// Parameters:
|
||||
// bracket – initial search interval [−bracket, +bracket] (default 50)
|
||||
// tol – absolute tolerance on t* (default 1e-8)
|
||||
//
|
||||
// Returns 0.0 if the zero cannot be bracketed (already at gauge maximum,
|
||||
// or open surface — no shift needed).
|
||||
inline double spherical_gauge_shift(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& m,
|
||||
double bracket = 50.0,
|
||||
double tol = 1e-8)
|
||||
{
|
||||
// Helper: sum of all vertex gradient components at x + t·1_v.
|
||||
auto sum_Gv = [&](double t) -> double {
|
||||
// Build a shifted copy of x (only vertex DOFs shifted).
|
||||
std::vector<double> xt = x;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv >= 0)
|
||||
xt[static_cast<std::size_t>(iv)] += t;
|
||||
}
|
||||
auto G = spherical_gradient(mesh, xt, m);
|
||||
double sum = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv >= 0)
|
||||
sum += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
|
||||
// ── Try bisection first (works when there is a sign change in [−bracket, +bracket]) ──
|
||||
double a = -bracket, b = bracket;
|
||||
double fa = sum_Gv(a), fb = sum_Gv(b);
|
||||
|
||||
if (fa * fb <= 0.0) {
|
||||
for (int iter = 0; iter < 120; ++iter) {
|
||||
double c = 0.5 * (a + b);
|
||||
double fc = sum_Gv(c);
|
||||
if (std::abs(fc) < tol || (b - a) < tol) return c;
|
||||
if (fa * fc < 0.0) { b = c; fb = fc; }
|
||||
else { a = c; fa = fc; }
|
||||
}
|
||||
return 0.5 * (a + b);
|
||||
}
|
||||
|
||||
// ── No sign change (zero may lie at a domain boundary). ───────────────────
|
||||
// Use damped Newton's method with backtracking line search.
|
||||
// f'(t) estimated by forward finite difference.
|
||||
// When the Newton step overshoots the valid domain (ΣG_v jumps back up
|
||||
// because faces become degenerate), backtracking halves the step until
|
||||
// |f| strictly decreases.
|
||||
const double fd_eps = 1e-5;
|
||||
double t = 0.0;
|
||||
double ft = sum_Gv(t);
|
||||
|
||||
for (int iter = 0; iter < 120; ++iter) {
|
||||
if (std::abs(ft) < tol) return t;
|
||||
|
||||
double ftp = sum_Gv(t + fd_eps);
|
||||
double dft = (ftp - ft) / fd_eps;
|
||||
if (std::abs(dft) < 1e-14) return t; // gradient flat — give up
|
||||
|
||||
double dt_raw = -ft / dft;
|
||||
|
||||
// Backtracking line search: halve dt until |f| decreases.
|
||||
double alpha = 1.0;
|
||||
bool improved = false;
|
||||
for (int back = 0; back < 40; ++back) {
|
||||
double t_try = t + alpha * dt_raw;
|
||||
t_try = std::max(-bracket, std::min(bracket, t_try));
|
||||
double ft_try = sum_Gv(t_try);
|
||||
if (std::abs(ft_try) < std::abs(ft)) {
|
||||
t = t_try;
|
||||
ft = ft_try;
|
||||
improved = true;
|
||||
break;
|
||||
}
|
||||
alpha *= 0.5;
|
||||
}
|
||||
if (!improved) return t; // cannot reduce further
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
// Apply the gauge shift in-place: x_v ← x_v + t* for all variable vertices.
|
||||
inline void apply_spherical_gauge(
|
||||
ConformalMesh& mesh,
|
||||
std::vector<double>& x,
|
||||
const SphericalMaps& m,
|
||||
double bracket = 50.0,
|
||||
double tol = 1e-8)
|
||||
{
|
||||
double t = spherical_gauge_shift(mesh, x, m, bracket, tol);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = m.v_idx[v];
|
||||
if (iv >= 0)
|
||||
x[static_cast<std::size_t>(iv)] += t;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
84
code/include/spherical_geometry.hpp
Normal file
84
code/include/spherical_geometry.hpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
// spherical_geometry.hpp
|
||||
//
|
||||
// Pure-math building blocks for the spherical discrete conformal map.
|
||||
// Ported from de.varylab.discreteconformal.functional.SphericalFunctional
|
||||
// (the geometry helpers embedded there).
|
||||
//
|
||||
// Notation:
|
||||
// u_i – vertex conformal factor (DOF)
|
||||
// λ°_e – base log-length of edge e (fixed initial value)
|
||||
// λ_ij – effective log-length = λ°_ij + u_i + u_j
|
||||
// l_ij – spherical arc length = 2·asin(min(exp(λ_ij/2), 1))
|
||||
// α_k – interior angle of the spherical triangle at vertex k
|
||||
|
||||
#include "constants.hpp"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
/// Backward-compatible alias — prefer conformallab::PI in new code.
|
||||
constexpr double PI_SPHER = PI;
|
||||
|
||||
// ── Effective spherical arc length ────────────────────────────────────────────
|
||||
|
||||
// l(λ) = 2·asin(min(exp(λ/2), 1)).
|
||||
// Clamps exp(λ/2) to [0, 1] so the arcsin stays in domain.
|
||||
inline double spherical_l(double lambda)
|
||||
{
|
||||
double half = std::exp(lambda * 0.5);
|
||||
if (half >= 1.0) half = 1.0 - 1e-15;
|
||||
if (half <= 0.0) return 0.0;
|
||||
return 2.0 * std::asin(half);
|
||||
}
|
||||
|
||||
// ── Interior angles of a spherical triangle ──────────────────────────────────
|
||||
|
||||
struct SphericalFaceAngles {
|
||||
double alpha1, alpha2, alpha3; // corner angles at v1, v2, v3
|
||||
bool valid; // false when the three lengths fail the
|
||||
// spherical triangle inequality
|
||||
};
|
||||
|
||||
// Compute corner angles from spherical arc lengths using the half-angle formula.
|
||||
//
|
||||
// Convention (matching the halfedge cycle h0→v1→v2, h1→v2→v3, h2→v3→v1):
|
||||
// l12 – arc length of edge opposite v3 (edge e12)
|
||||
// l23 – arc length of edge opposite v1 (edge e23)
|
||||
// l31 – arc length of edge opposite v2 (edge e31)
|
||||
//
|
||||
// Half-angle formula (spherical law of cosines):
|
||||
// α_k = 2·atan2(sqrt(sin(s-a)·sin(s-b)), sqrt(sin(s)·sin(s-c)))
|
||||
// where a,b are the two edges ADJACENT to vertex k, c is the opposite edge.
|
||||
//
|
||||
// Equivalently (in terms of s-deficiencies):
|
||||
// α1 = 2·atan2( sqrt(sin(s12)·sin(s31)), sqrt(sin(s)·sin(s23)) )
|
||||
// α2 = 2·atan2( sqrt(sin(s12)·sin(s23)), sqrt(sin(s)·sin(s31)) )
|
||||
// α3 = 2·atan2( sqrt(sin(s23)·sin(s31)), sqrt(sin(s)·sin(s12)) )
|
||||
//
|
||||
// where s = (l12+l23+l31)/2 and s_ij = s - l_ij.
|
||||
inline SphericalFaceAngles spherical_angles(double l12, double l23, double l31)
|
||||
{
|
||||
double s = (l12 + l23 + l31) * 0.5;
|
||||
double s12 = s - l12;
|
||||
double s23 = s - l23;
|
||||
double s31 = s - l31;
|
||||
|
||||
// Spherical triangle inequalities: all s-deficiencies > 0 and s < π.
|
||||
if (s12 <= 0.0 || s23 <= 0.0 || s31 <= 0.0 || s >= PI_SPHER)
|
||||
return {0.0, 0.0, 0.0, false};
|
||||
|
||||
const double ss = std::sin(s);
|
||||
const double ss12 = std::sin(s12);
|
||||
const double ss23 = std::sin(s23);
|
||||
const double ss31 = std::sin(s31);
|
||||
|
||||
double a1 = 2.0 * std::atan2(std::sqrt(ss12 * ss31), std::sqrt(ss * ss23));
|
||||
double a2 = 2.0 * std::atan2(std::sqrt(ss12 * ss23), std::sqrt(ss * ss31));
|
||||
double a3 = 2.0 * std::atan2(std::sqrt(ss23 * ss31), std::sqrt(ss * ss12));
|
||||
|
||||
return {a1, a2, a3, true};
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
256
code/include/spherical_hessian.hpp
Normal file
256
code/include/spherical_hessian.hpp
Normal file
@@ -0,0 +1,256 @@
|
||||
#pragma once
|
||||
// spherical_hessian.hpp
|
||||
//
|
||||
// Analytical Hessian of the spherical discrete conformal energy —
|
||||
// the spherical cotangent-Laplace operator.
|
||||
//
|
||||
// Ported from de.varylab.discreteconformal.functional.SphericalFunctional
|
||||
// (the hessian() method, vertex DOFs).
|
||||
//
|
||||
// ┌──────────────────────────────────────────────────────────────────────────┐
|
||||
// │ Hessian formula (vertex DOFs only) │
|
||||
// │ │
|
||||
// │ For a spherical face (v1, v2, v3) with vertex angles α1, α2, α3: │
|
||||
// │ │
|
||||
// │ Spherical cotangent weight for edge (vi, vj) with opposite vk: │
|
||||
// │ β_k = (π − αi − αj + αk) / 2 │
|
||||
// │ w_k = cot(β_k) = 1/tan(β_k) │
|
||||
// │ │
|
||||
// │ Euclidean limit: α1+α2+α3 → π, β_k → αk, w_k → cot(αk). ✓ │
|
||||
// │ │
|
||||
// │ Hessian contributions per face: │
|
||||
// │ H[vi, vi] += w_ij + w_ik (diagonal: weights of incident edges) │
|
||||
// │ H[vi, vj] -= w_ij (off-diagonal: weight of edge ij) │
|
||||
// │ │
|
||||
// │ where w_ij is the weight of the edge between vi and vj (opposite vk): │
|
||||
// │ w_ij = cot(β_k) with β_k = (π − αi − αj + αk) / 2. │
|
||||
// └──────────────────────────────────────────────────────────────────────────┘
|
||||
//
|
||||
// Requires Eigen (header-only). Returns Eigen::SparseMatrix<double>.
|
||||
|
||||
#include "spherical_functional.hpp"
|
||||
#include <Eigen/Sparse>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
// ── Spherical cotangent weight helper ────────────────────────────────────────
|
||||
//
|
||||
// Given the three face angles α1, α2, α3 of a spherical triangle, return the
|
||||
// three edge cotangent weights w1 (edge opp v1), w2 (edge opp v2), w3 (edge opp v3).
|
||||
//
|
||||
// w_k = cot(β_k) where β_k = (π − α_adj1 − α_adj2 + α_opp) / 2
|
||||
// = (π − αi − αj + αk) / 2 for edge (vi,vj), opposite vk
|
||||
//
|
||||
// Mapping in our CGAL halfedge convention:
|
||||
// h0 = halfedge(f): edge v1-v2 → opposite v3 → w = cot(β3), β3=(π-α1-α2+α3)/2
|
||||
// h1: edge v2-v3 → opposite v1 → w = cot(β1), β1=(π-α2-α3+α1)/2
|
||||
// h2: edge v3-v1 → opposite v2 → w = cot(β2), β2=(π-α3-α1+α2)/2
|
||||
//
|
||||
// Returns valid=false if any β_k is out of range (degenerate face).
|
||||
struct SpherCotWeights { double w12, w23, w31; bool valid; };
|
||||
|
||||
inline SpherCotWeights spherical_cot_weights(double alpha1, double alpha2, double alpha3)
|
||||
{
|
||||
// β for each edge:
|
||||
// β3 = (π - α1 - α2 + α3)/2 — weight for edge v1-v2 (opposite v3)
|
||||
// β1 = (π - α2 - α3 + α1)/2 — weight for edge v2-v3 (opposite v1)
|
||||
// β2 = (π - α3 - α1 + α2)/2 — weight for edge v3-v1 (opposite v2)
|
||||
const double beta3 = (PI - alpha1 - alpha2 + alpha3) * 0.5;
|
||||
const double beta1 = (PI - alpha2 - alpha3 + alpha1) * 0.5;
|
||||
const double beta2 = (PI - alpha3 - alpha1 + alpha2) * 0.5;
|
||||
|
||||
// Each β_k must be in (0, π/2] for the weight to be positive and well-defined.
|
||||
// For degenerate or very flat triangles some β may be ≤ 0 or ≥ π/2.
|
||||
if (beta1 <= 0.0 || beta2 <= 0.0 || beta3 <= 0.0) return {0.0, 0.0, 0.0, false};
|
||||
|
||||
const double tb1 = std::tan(beta1);
|
||||
const double tb2 = std::tan(beta2);
|
||||
const double tb3 = std::tan(beta3);
|
||||
|
||||
if (std::abs(tb1) < 1e-15 || std::abs(tb2) < 1e-15 || std::abs(tb3) < 1e-15)
|
||||
return {0.0, 0.0, 0.0, false};
|
||||
|
||||
// w_ij = cot(β_k) where β_k is for the edge opposite vk.
|
||||
// w12 is for edge v1-v2 (opposite v3): cot(β3)
|
||||
// w23 is for edge v2-v3 (opposite v1): cot(β1)
|
||||
// w31 is for edge v3-v1 (opposite v2): cot(β2)
|
||||
return {1.0 / tb3, 1.0 / tb1, 1.0 / tb2, true};
|
||||
}
|
||||
|
||||
// ── Analytical Hessian ────────────────────────────────────────────────────────
|
||||
//
|
||||
// Returns the n×n sparse Hessian matrix H where n = spherical_dimension(mesh, m).
|
||||
// x – current DOF vector.
|
||||
//
|
||||
// Derivation: G_v = θ_v − Σ_f α_v^f → H[i,j] = −Σ_f ∂α_i^f/∂u_j
|
||||
//
|
||||
// For a face (v1,v2,v3) with arc-lengths l12,l23,l31 and angles α1,α2,α3,
|
||||
// differentiating the spherical law of cosines
|
||||
// cos(l_opp) = cos(l_a)cos(l_b) + sin(l_a)sin(l_b)cos(α)
|
||||
// gives:
|
||||
// ∂α1/∂l12 = [cot(l12)cos(α1) − cot(l31)] / sin(α1) (adjacent side)
|
||||
// ∂α1/∂l31 = [cot(l31)cos(α1) − cot(l12)] / sin(α1) (adjacent side)
|
||||
// ∂α1/∂l23 = sin(l23) / [sin(l12)sin(l31)sin(α1)] (opposite side)
|
||||
//
|
||||
// Chain rule with ∂l_ij/∂u_k = tan(l_ij/2) (from l = 2·asin(exp(λ/2))):
|
||||
// ∂α1/∂u1 = ∂α1/∂l12·t12 + ∂α1/∂l31·t31
|
||||
// ∂α1/∂u2 = ∂α1/∂l12·t12 + ∂α1/∂l23·t23
|
||||
// ∂α1/∂u3 = ∂α1/∂l23·t23 + ∂α1/∂l31·t31
|
||||
inline Eigen::SparseMatrix<double> spherical_hessian(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const SphericalMaps& m)
|
||||
{
|
||||
const int n = spherical_dimension(mesh, m);
|
||||
|
||||
std::vector<Eigen::Triplet<double>> trips;
|
||||
trips.reserve(static_cast<std::size_t>(n) * 9);
|
||||
|
||||
for (auto f : mesh.faces()) {
|
||||
Halfedge_index h0 = mesh.halfedge(f);
|
||||
Halfedge_index h1 = mesh.next(h0);
|
||||
Halfedge_index h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0);
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
Edge_index e12 = mesh.edge(h0);
|
||||
Edge_index e23 = mesh.edge(h1);
|
||||
Edge_index e31 = mesh.edge(h2);
|
||||
|
||||
// Effective log-lengths.
|
||||
double u1 = spher_dof_val(m.v_idx[v1], x);
|
||||
double u2 = spher_dof_val(m.v_idx[v2], x);
|
||||
double u3 = spher_dof_val(m.v_idx[v3], x);
|
||||
|
||||
double lam12 = m.lambda0[e12] + u1 + u2 + spher_dof_val(m.e_idx[e12], x);
|
||||
double lam23 = m.lambda0[e23] + u2 + u3 + spher_dof_val(m.e_idx[e23], x);
|
||||
double lam31 = m.lambda0[e31] + u3 + u1 + spher_dof_val(m.e_idx[e31], x);
|
||||
|
||||
const double l12 = spherical_l(lam12);
|
||||
const double l23 = spherical_l(lam23);
|
||||
const double l31 = spherical_l(lam31);
|
||||
|
||||
SphericalFaceAngles fa = spherical_angles(l12, l23, l31);
|
||||
if (!fa.valid) continue;
|
||||
|
||||
const double sinl12 = std::sin(l12), cosl12 = std::cos(l12);
|
||||
const double sinl23 = std::sin(l23), cosl23 = std::cos(l23);
|
||||
const double sinl31 = std::sin(l31), cosl31 = std::cos(l31);
|
||||
|
||||
if (sinl12 < 1e-15 || sinl23 < 1e-15 || sinl31 < 1e-15) continue;
|
||||
|
||||
const double cot12 = cosl12 / sinl12;
|
||||
const double cot23 = cosl23 / sinl23;
|
||||
const double cot31 = cosl31 / sinl31;
|
||||
|
||||
// ∂l_ij/∂λ_ij = tan(l_ij/2)
|
||||
const double t12 = std::tan(l12 * 0.5);
|
||||
const double t23 = std::tan(l23 * 0.5);
|
||||
const double t31 = std::tan(l31 * 0.5);
|
||||
|
||||
const double sinA1 = std::sin(fa.alpha1), cosA1 = std::cos(fa.alpha1);
|
||||
const double sinA2 = std::sin(fa.alpha2), cosA2 = std::cos(fa.alpha2);
|
||||
const double sinA3 = std::sin(fa.alpha3), cosA3 = std::cos(fa.alpha3);
|
||||
|
||||
if (sinA1 < 1e-15 || sinA2 < 1e-15 || sinA3 < 1e-15) continue;
|
||||
|
||||
// ∂α1/∂l_jk (α1 at v1; opposite l23, adjacent l12,l31)
|
||||
const double dA1_dl12 = (cot12 * cosA1 - cot31) / sinA1;
|
||||
const double dA1_dl31 = (cot31 * cosA1 - cot12) / sinA1;
|
||||
const double dA1_dl23 = sinl23 / (sinl12 * sinl31 * sinA1);
|
||||
|
||||
// ∂α2/∂l_jk (α2 at v2; opposite l31, adjacent l12,l23)
|
||||
const double dA2_dl12 = (cot12 * cosA2 - cot23) / sinA2;
|
||||
const double dA2_dl23 = (cot23 * cosA2 - cot12) / sinA2;
|
||||
const double dA2_dl31 = sinl31 / (sinl12 * sinl23 * sinA2);
|
||||
|
||||
// ∂α3/∂l_jk (α3 at v3; opposite l12, adjacent l23,l31)
|
||||
const double dA3_dl23 = (cot23 * cosA3 - cot31) / sinA3;
|
||||
const double dA3_dl31 = (cot31 * cosA3 - cot23) / sinA3;
|
||||
const double dA3_dl12 = sinl12 / (sinl23 * sinl31 * sinA3);
|
||||
|
||||
// Chain rule: ∂α_i/∂u_j (u1 affects l12,l31; u2 affects l12,l23; u3 affects l23,l31)
|
||||
const double dA1_du1 = dA1_dl12 * t12 + dA1_dl31 * t31;
|
||||
const double dA1_du2 = dA1_dl12 * t12 + dA1_dl23 * t23;
|
||||
const double dA1_du3 = dA1_dl23 * t23 + dA1_dl31 * t31;
|
||||
|
||||
const double dA2_du1 = dA2_dl12 * t12 + dA2_dl31 * t31;
|
||||
const double dA2_du2 = dA2_dl12 * t12 + dA2_dl23 * t23;
|
||||
const double dA2_du3 = dA2_dl23 * t23 + dA2_dl31 * t31;
|
||||
|
||||
const double dA3_du1 = dA3_dl12 * t12 + dA3_dl31 * t31;
|
||||
const double dA3_du2 = dA3_dl12 * t12 + dA3_dl23 * t23;
|
||||
const double dA3_du3 = dA3_dl23 * t23 + dA3_dl31 * t31;
|
||||
|
||||
const int i1 = m.v_idx[v1];
|
||||
const int i2 = m.v_idx[v2];
|
||||
const int i3 = m.v_idx[v3];
|
||||
|
||||
// H[vi, vj] -= ∂α_i/∂u_j (G_v = θ_v − Σ α_v, so ∂G_i/∂u_j = −∂α_i/∂u_j)
|
||||
if (i1 >= 0) trips.emplace_back(i1, i1, -dA1_du1);
|
||||
if (i2 >= 0) trips.emplace_back(i2, i2, -dA2_du2);
|
||||
if (i3 >= 0) trips.emplace_back(i3, i3, -dA3_du3);
|
||||
|
||||
if (i1 >= 0 && i2 >= 0) {
|
||||
trips.emplace_back(i1, i2, -dA1_du2);
|
||||
trips.emplace_back(i2, i1, -dA2_du1);
|
||||
}
|
||||
if (i2 >= 0 && i3 >= 0) {
|
||||
trips.emplace_back(i2, i3, -dA2_du3);
|
||||
trips.emplace_back(i3, i2, -dA3_du2);
|
||||
}
|
||||
if (i3 >= 0 && i1 >= 0) {
|
||||
trips.emplace_back(i3, i1, -dA3_du1);
|
||||
trips.emplace_back(i1, i3, -dA1_du3);
|
||||
}
|
||||
}
|
||||
|
||||
Eigen::SparseMatrix<double> H(n, n);
|
||||
H.setFromTriplets(trips.begin(), trips.end());
|
||||
return H;
|
||||
}
|
||||
|
||||
// ── Finite-difference Hessian check ──────────────────────────────────────────
|
||||
//
|
||||
// Compares the analytical Hessian column-by-column against
|
||||
// H_fd[:, j] = (G(x + ε·eⱼ) − G(x − ε·eⱼ)) / (2ε).
|
||||
inline bool hessian_check_spherical(
|
||||
ConformalMesh& mesh,
|
||||
const std::vector<double>& x0,
|
||||
const SphericalMaps& m,
|
||||
double eps = 1e-5,
|
||||
double tol = 1e-4)
|
||||
{
|
||||
const int n = static_cast<int>(x0.size());
|
||||
auto H = spherical_hessian(mesh, x0, m);
|
||||
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
bool ok = true;
|
||||
|
||||
for (int j = 0; j < n; ++j) {
|
||||
const std::size_t sj = static_cast<std::size_t>(j);
|
||||
xp[sj] = x0[sj] + eps;
|
||||
xm[sj] = x0[sj] - eps;
|
||||
|
||||
auto Gp = spherical_gradient(mesh, xp, m);
|
||||
auto Gm = spherical_gradient(mesh, xm, m);
|
||||
|
||||
xp[sj] = xm[sj] = x0[sj];
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
double fd_ij = (Gp[static_cast<std::size_t>(i)]
|
||||
- Gm[static_cast<std::size_t>(i)]) / (2.0 * eps);
|
||||
double H_ij = H.coeff(i, j);
|
||||
double err = std::abs(H_ij - fd_ij);
|
||||
double scale = std::max(1.0, std::abs(H_ij));
|
||||
if (err / scale > tol) ok = false;
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace conformallab
|
||||
@@ -1,107 +1,314 @@
|
||||
#include <CGAL/Simple_cartesian.h>
|
||||
#include <CGAL/Surface_mesh.h>
|
||||
#include <CGAL/IO/polygon_mesh_io.h>
|
||||
|
||||
|
||||
#include "viewer_utils.h"
|
||||
#include "mesh_utils.hpp"
|
||||
// conformallab_cli.cpp
|
||||
//
|
||||
// ConformalLab++ command-line interface.
|
||||
//
|
||||
// Usage:
|
||||
// conformallab_core -i input.off [-o layout.off] [-g euclidean|spherical|hyper_ideal]
|
||||
// [-j result.json] [-x result.xml] [-s] [-v]
|
||||
//
|
||||
// The tool:
|
||||
// 1. Loads an OFF/OBJ/PLY mesh.
|
||||
// 2. Sets up DOF maps + computes λ° from the input geometry.
|
||||
// 3. Pins one vertex (Euclidean/Spherical) or uses all-free DOFs (HyperIdeal).
|
||||
// 4. Runs Newton until convergence.
|
||||
// 5. Computes a 2-D (Euclidean / HyperIdeal) or 3-D (Spherical) layout.
|
||||
// 6. Saves the layout as an OFF file and optionally serialises the result
|
||||
// to JSON and/or XML.
|
||||
// 7. Optionally shows the input mesh in a viewer (-s flag, requires WITH_VIEWER).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "serialization.hpp"
|
||||
|
||||
#include <CLI11.hpp>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
// Viewer is always available when WITH_CGAL=ON (implies WITH_VIEWER=ON)
|
||||
#include "viewer_utils.h"
|
||||
#include <Eigen/Core>
|
||||
|
||||
namespace cl = conformallab;
|
||||
using cl::ConformalMesh;
|
||||
using cl::Vertex_index;
|
||||
using cl::Edge_index;
|
||||
|
||||
using Kernel = CGAL::Simple_cartesian<double>;
|
||||
using Point = Kernel::Point_3;
|
||||
using Mesh = CGAL::Surface_mesh<Point>;
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Shared helpers (mirroring test_pipeline.cpp patterns)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
|
||||
bool parse_arguments(int argc, char* argv[], std::string& input_file, std::string& output_file, bool& show, bool& verbose) {
|
||||
CLI::App app{"demo of conformallab"};
|
||||
app.add_option("-i,--input", input_file, "Input OFF file")->required();
|
||||
app.add_option("-o,--output", output_file, "Output OFF file (optional)");
|
||||
app.add_flag("-s,--show", show, "Show the input mesh in a viewer ");
|
||||
app.add_flag("-v,--verbose",verbose, "Enable verbose output");
|
||||
app.set_help_flag("-h,--help", "Show this help message and exit");
|
||||
|
||||
try {
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
} catch (const CLI::ParseError &e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (input_file.empty()) {
|
||||
std::cerr << "Input file is required.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (input_file.substr(input_file.find_last_of('.') + 1) != "off") {
|
||||
std::cerr << "Unsupported file format. Please provide an OFF file.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::cout << "Input file: " << input_file << "\n";
|
||||
std::cout << "Output file: " << output_file << "\n";
|
||||
std::cout << "Show mesh: " << (show ? "Yes" : "No") << "\n";
|
||||
std::cout << "Verbose: " << (verbose ? "Yes" : "No") << "\n";
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
// Pin vertex 0, assign 0..n-1 to the rest
|
||||
static int pin_first_vertex(ConformalMesh& mesh, cl::EuclideanMaps& maps)
|
||||
{
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
maps.v_idx[v0] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
return idx;
|
||||
}
|
||||
|
||||
// Natural theta for Euclidean: make x=0 the equilibrium
|
||||
static void set_natural_euclidean_theta(ConformalMesh& mesh, cl::EuclideanMaps& maps, int n)
|
||||
{
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto G = cl::euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] -= G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
}
|
||||
|
||||
// Natural theta for HyperIdeal at base point (b=1, a=0.5) to avoid x=0 singularity
|
||||
static std::vector<double> set_natural_hyper_ideal_targets(
|
||||
ConformalMesh& mesh, cl::HyperIdealMaps& maps, int n)
|
||||
{
|
||||
const auto sz = static_cast<std::size_t>(n);
|
||||
std::vector<double> xbase(sz, 0.0);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) xbase[static_cast<std::size_t>(iv)] = 1.0;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) xbase[static_cast<std::size_t>(ie)] = 0.5;
|
||||
}
|
||||
auto G = cl::evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
maps.theta_e[e] += G[static_cast<std::size_t>(ie)];
|
||||
}
|
||||
return xbase;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Euclidean pipeline
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
static int run_euclidean(ConformalMesh& mesh,
|
||||
const std::string& out_layout,
|
||||
const std::string& out_json,
|
||||
const std::string& out_xml,
|
||||
bool verbose)
|
||||
{
|
||||
// Setup
|
||||
auto maps = cl::setup_euclidean_maps(mesh);
|
||||
cl::compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// DOF assignment: pin vertex 0
|
||||
int n = pin_first_vertex(mesh, maps);
|
||||
if (n <= 0) { std::cerr << "Error: mesh has only one vertex.\n"; return 1; }
|
||||
|
||||
// Natural target angles
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
// Newton
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto res = cl::newton_euclidean(mesh, x0, maps);
|
||||
|
||||
if (!res.converged && verbose)
|
||||
std::cerr << "[warn] Newton did not converge (|grad|=" << res.grad_inf_norm << ")\n";
|
||||
|
||||
// Layout
|
||||
cl::Layout2D layout = cl::euclidean_layout(mesh, res.x, maps);
|
||||
|
||||
// Output
|
||||
if (!out_layout.empty()) cl::save_layout_off(out_layout, mesh, layout);
|
||||
if (!out_json.empty())
|
||||
cl::save_result_json(out_json, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
if (!out_xml.empty())
|
||||
cl::save_result_xml(out_xml, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
|
||||
std::cout << std::fixed << std::setprecision(6);
|
||||
std::cout << "Euclidean: converged=" << (res.converged ? "yes" : "no")
|
||||
<< " iter=" << res.iterations
|
||||
<< " |grad|_inf=" << std::scientific << std::setprecision(3)
|
||||
<< res.grad_inf_norm << "\n";
|
||||
if (!out_layout.empty()) std::cout << " layout → " << out_layout << "\n";
|
||||
if (!out_json.empty()) std::cout << " json → " << out_json << "\n";
|
||||
if (!out_xml.empty()) std::cout << " xml → " << out_xml << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Spherical pipeline
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
static int run_spherical(ConformalMesh& mesh,
|
||||
const std::string& out_layout,
|
||||
const std::string& out_json,
|
||||
const std::string& out_xml,
|
||||
bool verbose)
|
||||
{
|
||||
auto maps = cl::setup_spherical_maps(mesh);
|
||||
cl::compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = cl::assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto res = cl::newton_spherical(mesh, x0, maps);
|
||||
|
||||
if (!res.converged && verbose)
|
||||
std::cerr << "[warn] Newton did not converge (|grad|=" << res.grad_inf_norm << ")\n";
|
||||
|
||||
cl::Layout3D layout = cl::spherical_layout(mesh, res.x, maps);
|
||||
|
||||
if (!out_layout.empty()) cl::save_layout_off(out_layout, mesh, layout);
|
||||
if (!out_json.empty())
|
||||
cl::save_result_json(out_json, res, "spherical",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
nullptr, &layout);
|
||||
if (!out_xml.empty())
|
||||
cl::save_result_xml(out_xml, res, "spherical",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
nullptr, &layout);
|
||||
|
||||
std::cout << "Spherical: converged=" << (res.converged ? "yes" : "no")
|
||||
<< " iter=" << res.iterations
|
||||
<< " |grad|_inf=" << std::scientific << std::setprecision(3)
|
||||
<< res.grad_inf_norm << "\n";
|
||||
if (!out_layout.empty()) std::cout << " layout → " << out_layout << "\n";
|
||||
if (!out_json.empty()) std::cout << " json → " << out_json << "\n";
|
||||
if (!out_xml.empty()) std::cout << " xml → " << out_xml << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// HyperIdeal pipeline
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
static int run_hyper_ideal(ConformalMesh& mesh,
|
||||
const std::string& out_layout,
|
||||
const std::string& out_json,
|
||||
const std::string& out_xml,
|
||||
bool verbose)
|
||||
{
|
||||
auto maps = cl::setup_hyper_ideal_maps(mesh);
|
||||
int n = cl::assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// Natural targets at base point (b=1, a=0.5)
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
|
||||
// Perturb slightly from the base and solve back
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.3;
|
||||
|
||||
auto res = cl::newton_hyper_ideal(mesh, x0, maps);
|
||||
|
||||
if (!res.converged && verbose)
|
||||
std::cerr << "[warn] Newton did not converge (|grad|=" << res.grad_inf_norm << ")\n";
|
||||
|
||||
cl::Layout2D layout = cl::hyper_ideal_layout(mesh, res.x, maps);
|
||||
|
||||
if (!out_layout.empty()) cl::save_layout_off(out_layout, mesh, layout);
|
||||
if (!out_json.empty())
|
||||
cl::save_result_json(out_json, res, "hyper_ideal",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
if (!out_xml.empty())
|
||||
cl::save_result_xml(out_xml, res, "hyper_ideal",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout);
|
||||
|
||||
std::cout << "HyperIdeal: converged=" << (res.converged ? "yes" : "no")
|
||||
<< " iter=" << res.iterations
|
||||
<< " |grad|_inf=" << std::scientific << std::setprecision(3)
|
||||
<< res.grad_inf_norm << "\n";
|
||||
if (!out_layout.empty()) std::cout << " layout → " << out_layout << "\n";
|
||||
if (!out_json.empty()) std::cout << " json → " << out_json << "\n";
|
||||
if (!out_xml.empty()) std::cout << " xml → " << out_xml << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// main
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
CLI::App app{"ConformalLab++ — discrete conformal mapping"};
|
||||
|
||||
std::string input_file;
|
||||
std::string output_file;
|
||||
bool show = false;
|
||||
std::string out_layout;
|
||||
std::string out_json;
|
||||
std::string out_xml;
|
||||
std::string geometry = "euclidean";
|
||||
bool show = false;
|
||||
bool verbose = false;
|
||||
|
||||
if(!parse_arguments(argc, argv, input_file, output_file, show, verbose)) {
|
||||
std::cerr << "Failed to parse arguments.\n";
|
||||
app.add_option("-i,--input", input_file, "Input mesh (OFF/OBJ/PLY)")->required();
|
||||
app.add_option("-o,--output", out_layout, "Output layout OFF file");
|
||||
app.add_option("-j,--json", out_json, "Save result as JSON");
|
||||
app.add_option("-x,--xml", out_xml, "Save result as XML");
|
||||
app.add_option("-g,--geometry", geometry, "Target geometry: euclidean|spherical|hyper_ideal")
|
||||
->check(CLI::IsMember({"euclidean", "spherical", "hyper_ideal"}));
|
||||
app.add_flag("-s,--show", show, "Visualise input mesh (requires WITH_VIEWER)");
|
||||
app.add_flag("-v,--verbose", verbose, "Verbose output");
|
||||
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
// ── Load mesh ─────────────────────────────────────────────────────────────
|
||||
ConformalMesh mesh;
|
||||
if (!cl::read_mesh(input_file, mesh) || mesh.is_empty()) {
|
||||
std::cerr << "Error: cannot load mesh from '" << input_file << "'\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
std::cout << "Loaded: " << input_file
|
||||
<< " (" << mesh.number_of_vertices() << "v, "
|
||||
<< mesh.number_of_faces() << "f)\n";
|
||||
|
||||
Mesh surface_mesh;
|
||||
if (!CGAL::IO::read_polygon_mesh(input_file, surface_mesh) || surface_mesh.is_empty()) {
|
||||
std::cerr << "Invalid input file: " << input_file << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// ── Optional viewer ───────────────────────────────────────────────────────
|
||||
if (show) {
|
||||
// Convert ConformalMesh to Eigen matrices for the libigl viewer
|
||||
const std::size_t nv = mesh.number_of_vertices();
|
||||
const std::size_t nf = mesh.number_of_faces();
|
||||
Eigen::MatrixXd V(static_cast<Eigen::Index>(nv), 3);
|
||||
Eigen::MatrixXi F(static_cast<Eigen::Index>(nf), 3);
|
||||
|
||||
|
||||
|
||||
// #TODO: later: here we would call the unwrapping code, e.g.:
|
||||
// UnwrapSettings settings;
|
||||
// settings.target_geometry = TargetGeometry::Euclidean;
|
||||
// UnwrapJob job(surface_mesh, settings);
|
||||
// auto result = job.run();
|
||||
// Mesh unwrapped = result.surface_unwrapped;
|
||||
|
||||
|
||||
// CGAL -> libigl
|
||||
if(show){
|
||||
std::cout << "Visualizing input mesh...\n";
|
||||
Eigen::MatrixXd V;
|
||||
Eigen::MatrixXi F;
|
||||
|
||||
mesh_utils::cgal_to_eigen<Kernel>(surface_mesh, V, F);
|
||||
viewer_utils::simple_visualize(V, F);
|
||||
|
||||
}
|
||||
|
||||
// for now, we just write the input mesh to the output file as a placeholder
|
||||
if (!output_file.empty() && !CGAL::IO::write_polygon_mesh(output_file, surface_mesh)) {
|
||||
std::cerr << "Failed to write output file: " << output_file << "\n";
|
||||
return EXIT_FAILURE;
|
||||
for (auto v : mesh.vertices()) {
|
||||
auto p = mesh.point(v);
|
||||
V.row(v.idx()) << p.x(), p.y(), p.z();
|
||||
}
|
||||
Eigen::Index fi = 0;
|
||||
for (auto f : mesh.faces()) {
|
||||
int ci = 0;
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
F(fi, ci++) = static_cast<int>(mesh.target(h).idx());
|
||||
++fi;
|
||||
}
|
||||
viewer_utils::simple_visualize(V, F);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
// ── Dispatch ──────────────────────────────────────────────────────────────
|
||||
if (geometry == "euclidean")
|
||||
return run_euclidean(mesh, out_layout, out_json, out_xml, verbose);
|
||||
if (geometry == "spherical")
|
||||
return run_spherical(mesh, out_layout, out_json, out_xml, verbose);
|
||||
if (geometry == "hyper_ideal")
|
||||
return run_hyper_ideal(mesh, out_layout, out_json, out_xml, verbose);
|
||||
|
||||
std::cerr << "Unknown geometry: " << geometry << "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
add_executable(conformallab_tests
|
||||
# ── Fully ported (pure math, no HDS) ────────────────────────────────────
|
||||
test_clausen.cpp
|
||||
test_hyper_ideal_utility.cpp
|
||||
test_matrix_utility.cpp
|
||||
test_surface_curve_utility.cpp
|
||||
test_discrete_elliptic_utility.cpp
|
||||
test_p2_utility.cpp
|
||||
test_hyper_ideal_visualization_utility.cpp
|
||||
|
||||
# ── Stubs: blocked until HDS port (Phase 4) ──────────────────────────────
|
||||
# All tests call GTEST_SKIP() with a clear explanation.
|
||||
test_hyper_ideal_functional.cpp
|
||||
test_hyper_ideal_hyperelliptic_utility.cpp
|
||||
test_spherical_functional.cpp
|
||||
)
|
||||
|
||||
target_include_directories(conformallab_tests SYSTEM PRIVATE
|
||||
@@ -17,3 +27,10 @@ target_link_libraries(conformallab_tests PRIVATE GTest::gtest_main)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(conformallab_tests DISCOVERY_TIMEOUT 60)
|
||||
|
||||
# ── CGAL test suite ──────────────────────────────────────────────────────────
|
||||
# Built with -DWITH_CGAL_TESTS=ON (headless CI, no viewer) or
|
||||
# -DWITH_CGAL=ON (full build with viewer + CLI).
|
||||
if(WITH_CGAL OR WITH_CGAL_TESTS)
|
||||
add_subdirectory(cgal)
|
||||
endif()
|
||||
|
||||
84
code/tests/cgal/CMakeLists.txt
Normal file
84
code/tests/cgal/CMakeLists.txt
Normal file
@@ -0,0 +1,84 @@
|
||||
# tests/cgal/CMakeLists.txt
|
||||
#
|
||||
# CGAL-dependent test target. Only built when -DWITH_CGAL=ON.
|
||||
# Requires Boost (find_package(Boost REQUIRED) is called in the root CMakeLists).
|
||||
#
|
||||
# Run with:
|
||||
# cmake -S code -B build -DWITH_CGAL=ON
|
||||
# cmake --build build --target conformallab_cgal_tests
|
||||
# ctest --test-dir build -R cgal
|
||||
|
||||
add_executable(conformallab_cgal_tests
|
||||
# ── Phase 3a: mesh infrastructure ──────────────────────────────────────
|
||||
test_conformal_mesh.cpp
|
||||
|
||||
# ── Phase 3b: HyperIdealFunctional ─────────────────────────────────────
|
||||
test_hyper_ideal_functional.cpp
|
||||
|
||||
# ── Phase 3c + 3e: SphericalFunctional + gauge-fix ────────────────────
|
||||
test_spherical_functional.cpp
|
||||
|
||||
# ── Phase 3d: EuclideanCyclicFunctional ───────────────────────────────
|
||||
test_euclidean_functional.cpp
|
||||
|
||||
# ── Phase 3f: Hessians (cotangent Laplacian, spherical + Euclidean) ───
|
||||
test_euclidean_hessian.cpp
|
||||
test_spherical_hessian.cpp
|
||||
|
||||
# ── Phase 4a: Newton solver ────────────────────────────────────────────
|
||||
test_newton_solver.cpp
|
||||
|
||||
# ── Phase 4b: Mesh I/O (CGAL::IO) ─────────────────────────────────────
|
||||
test_mesh_io.cpp
|
||||
|
||||
# ── Phase 4c: End-to-end pipeline + user examples ─────────────────────
|
||||
test_pipeline.cpp
|
||||
|
||||
# ── Phase 5: Layout / embedding + serialization ────────────────────────
|
||||
test_layout.cpp
|
||||
|
||||
# ── Phase 6: Gauss–Bonnet, cut graph, exact trilateration, normalisation
|
||||
test_phase6.cpp
|
||||
|
||||
# ── Phase 7: Java-parity layout — MobiusMap, priority BFS, halfedge_uv,
|
||||
# period matrix, fundamental domain, tiling
|
||||
test_phase7.cpp
|
||||
|
||||
# ── Java-Parität: Geometrie-Utility-Tests ─────────────────────────────────
|
||||
# Portiert aus CuttinUtilityTest, UnwrapUtilityTest,
|
||||
# ConvergenceUtilityTests, HomologyTest (Tests 1–6).
|
||||
# Test 7 (Genus-2-Homologie) als GTEST_SKIP-Stub bis Phase 8.
|
||||
test_geometry_utils.cpp
|
||||
)
|
||||
|
||||
target_include_directories(conformallab_cgal_tests SYSTEM PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/deps/eigen-3.4.0
|
||||
${CMAKE_SOURCE_DIR}/deps/CGAL-6.1.1/include
|
||||
${CMAKE_SOURCE_DIR}/deps/single_includes
|
||||
${Boost_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
target_include_directories(conformallab_cgal_tests PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
target_compile_definitions(conformallab_cgal_tests PRIVATE
|
||||
CGAL_DISABLE_GMP
|
||||
CGAL_DISABLE_MPFR
|
||||
# Data directory — absolute path to code/data/ at build time.
|
||||
# Used by tests that load real mesh files (cathead.obj, brezel2.obj, …).
|
||||
CONFORMALLAB_DATA_DIR="${CMAKE_SOURCE_DIR}/data"
|
||||
)
|
||||
|
||||
# Suppress warnings from CGAL/Boost headers
|
||||
target_compile_options(conformallab_cgal_tests PRIVATE
|
||||
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-unused-parameter>
|
||||
)
|
||||
|
||||
target_link_libraries(conformallab_cgal_tests PRIVATE GTest::gtest_main)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(conformallab_cgal_tests
|
||||
TEST_PREFIX "cgal."
|
||||
DISCOVERY_TIMEOUT 60
|
||||
)
|
||||
240
code/tests/cgal/test_conformal_mesh.cpp
Normal file
240
code/tests/cgal/test_conformal_mesh.cpp
Normal file
@@ -0,0 +1,240 @@
|
||||
// test_conformal_mesh.cpp
|
||||
//
|
||||
// Phase 3a — CGAL Surface_mesh infrastructure tests.
|
||||
//
|
||||
// Verifies that ConformalMesh (CGAL::Surface_mesh<Point3>) and the
|
||||
// mesh_builder factories behave correctly before we build the functionals
|
||||
// on top of them (Phase 3b).
|
||||
//
|
||||
// Test groups
|
||||
// ───────────
|
||||
// Topology – vertex/edge/face counts, Euler characteristic
|
||||
// Traversal – halfedge iteration around vertex / face / edge
|
||||
// PropertyMaps – read/write of lambda, theta, idx, alpha, f:type
|
||||
// Validity – all make_* factories produce valid, consistent meshes
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include <CGAL/boost/graph/iterator.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// Topology
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
// Single triangle: 3 vertices, 1 face, 3 edges.
|
||||
TEST(ConformalMeshTopology, SingleTriangle)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
EXPECT_EQ(3u, mesh.number_of_vertices());
|
||||
EXPECT_EQ(1u, mesh.number_of_faces());
|
||||
EXPECT_EQ(3u, mesh.number_of_edges());
|
||||
}
|
||||
|
||||
// Tetrahedron: V=4, E=6, F=4 → Euler = 2 (sphere topology).
|
||||
TEST(ConformalMeshTopology, TetrahedronEuler)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
EXPECT_EQ(4u, mesh.number_of_vertices());
|
||||
EXPECT_EQ(6u, mesh.number_of_edges());
|
||||
EXPECT_EQ(4u, mesh.number_of_faces());
|
||||
|
||||
int euler = (int)mesh.number_of_vertices()
|
||||
- (int)mesh.number_of_edges()
|
||||
+ (int)mesh.number_of_faces();
|
||||
EXPECT_EQ(2, euler) << "Euler characteristic of closed sphere must be 2";
|
||||
}
|
||||
|
||||
// Two-triangle strip: V=4, E=5, F=2.
|
||||
// The interior edge (shared diagonal) has no border halfedge.
|
||||
TEST(ConformalMeshTopology, QuadStrip)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
EXPECT_EQ(4u, mesh.number_of_vertices());
|
||||
EXPECT_EQ(5u, mesh.number_of_edges());
|
||||
EXPECT_EQ(2u, mesh.number_of_faces());
|
||||
|
||||
// Count interior (non-boundary) edges
|
||||
int interior = 0;
|
||||
for (auto e : mesh.edges())
|
||||
if (!mesh.is_border(e)) ++interior;
|
||||
EXPECT_EQ(1, interior) << "Only the shared diagonal should be interior";
|
||||
}
|
||||
|
||||
// Fan with n triangles: V=n+1, E=2n, F=n.
|
||||
TEST(ConformalMeshTopology, FanCounts)
|
||||
{
|
||||
for (int n : {3, 4, 6, 8}) {
|
||||
auto mesh = make_fan(n);
|
||||
EXPECT_EQ((std::size_t)(n + 1), mesh.number_of_vertices());
|
||||
EXPECT_EQ((std::size_t)(2 * n), mesh.number_of_edges());
|
||||
EXPECT_EQ((std::size_t)(n), mesh.number_of_faces());
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// Halfedge Traversal
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
// For a regular tetrahedron every vertex has valence 3.
|
||||
TEST(ConformalMeshTraversal, TetrahedronVertexValence)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
for (auto v : mesh.vertices()) {
|
||||
int degree = 0;
|
||||
for (auto h : CGAL::halfedges_around_target(v, mesh))
|
||||
{ (void)h; ++degree; }
|
||||
EXPECT_EQ(3, degree) << "Each tetrahedron vertex has degree 3";
|
||||
}
|
||||
}
|
||||
|
||||
// For a fan with n triangles the center vertex has valence n.
|
||||
TEST(ConformalMeshTraversal, FanCenterValence)
|
||||
{
|
||||
for (int n : {3, 5, 7}) {
|
||||
auto mesh = make_fan(n);
|
||||
|
||||
// Center vertex is always the first one added (index 0).
|
||||
auto center = *mesh.vertices().begin();
|
||||
int degree = 0;
|
||||
for (auto h : CGAL::halfedges_around_target(center, mesh))
|
||||
{ (void)h; ++degree; }
|
||||
EXPECT_EQ(n, degree)
|
||||
<< "Fan center vertex must have valence == n=" << n;
|
||||
}
|
||||
}
|
||||
|
||||
// Every face of the tetrahedron has exactly 3 halfedges.
|
||||
TEST(ConformalMeshTraversal, FaceHalfedgeCount)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
for (auto f : mesh.faces()) {
|
||||
int count = 0;
|
||||
for (auto h : CGAL::halfedges_around_face(mesh.halfedge(f), mesh))
|
||||
{ (void)h; ++count; }
|
||||
EXPECT_EQ(3, count) << "Each triangular face must have exactly 3 halfedges";
|
||||
}
|
||||
}
|
||||
|
||||
// opposite(h) and h share the same edge; opposite(opposite(h)) == h.
|
||||
TEST(ConformalMeshTraversal, OppositeHalfedgeConsistency)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
for (auto h : mesh.halfedges()) {
|
||||
auto opp = mesh.opposite(h);
|
||||
EXPECT_EQ(mesh.edge(h), mesh.edge(opp))
|
||||
<< "h and opposite(h) must share the same edge";
|
||||
EXPECT_EQ(h, mesh.opposite(opp))
|
||||
<< "opposite(opposite(h)) must equal h";
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// Property Maps
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
// The conformal variable lambda can be written and read back per vertex.
|
||||
TEST(ConformalMeshProperties, VertexLambdaReadWrite)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto [lambda, theta, idx] = add_vertex_properties(mesh);
|
||||
|
||||
double value = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
lambda[v] = value;
|
||||
value += 1.0;
|
||||
}
|
||||
|
||||
value = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
EXPECT_DOUBLE_EQ(value, lambda[v]);
|
||||
value += 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Default solver index is -1 (pinned); can be overwritten.
|
||||
TEST(ConformalMeshProperties, VertexSolverIndex)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto [lambda, theta, idx] = add_vertex_properties(mesh);
|
||||
|
||||
// All vertices start at -1 (pinned / boundary)
|
||||
for (auto v : mesh.vertices())
|
||||
EXPECT_EQ(-1, idx[v]) << "Default solver index must be -1";
|
||||
|
||||
// Assign sequential indices
|
||||
int i = 0;
|
||||
for (auto v : mesh.vertices())
|
||||
idx[v] = i++;
|
||||
|
||||
i = 0;
|
||||
for (auto v : mesh.vertices())
|
||||
EXPECT_EQ(i++, idx[v]);
|
||||
}
|
||||
|
||||
// Edge alpha (intersection angle): set and retrieve per edge.
|
||||
TEST(ConformalMeshProperties, EdgeAlpha)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto alpha = add_edge_properties(mesh);
|
||||
|
||||
const double kAlpha = M_PI / 3.0; // 60°
|
||||
for (auto e : mesh.edges())
|
||||
alpha[e] = kAlpha;
|
||||
|
||||
for (auto e : mesh.edges())
|
||||
EXPECT_DOUBLE_EQ(kAlpha, alpha[e]);
|
||||
|
||||
EXPECT_EQ(5u, mesh.number_of_edges());
|
||||
}
|
||||
|
||||
// Face geometry type: Euclidean by default, switchable to Hyperbolic.
|
||||
TEST(ConformalMeshProperties, FaceGeometryType)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto ftype = add_face_properties(mesh);
|
||||
|
||||
// Default: Euclidean
|
||||
for (auto f : mesh.faces())
|
||||
EXPECT_EQ(static_cast<int>(GeometryType::Euclidean), ftype[f]);
|
||||
|
||||
// Switch all to Hyperbolic
|
||||
for (auto f : mesh.faces())
|
||||
ftype[f] = static_cast<int>(GeometryType::Hyperbolic);
|
||||
|
||||
for (auto f : mesh.faces())
|
||||
EXPECT_EQ(static_cast<int>(GeometryType::Hyperbolic), ftype[f]);
|
||||
}
|
||||
|
||||
// Adding the same named property map twice: second call returns ok=false
|
||||
// and both handles alias the same storage.
|
||||
TEST(ConformalMeshProperties, PropertyMapIdempotent)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto [pm1, ok1] = mesh.add_property_map<Vertex_index, double>("v:lambda", 0.0);
|
||||
auto [pm2, ok2] = mesh.add_property_map<Vertex_index, double>("v:lambda", 0.0);
|
||||
|
||||
EXPECT_TRUE(ok1) << "First add_property_map must succeed";
|
||||
EXPECT_FALSE(ok2) << "Second add_property_map on existing name must return ok=false";
|
||||
|
||||
// Both handles must alias the same storage
|
||||
auto v = *mesh.vertices().begin();
|
||||
pm1[v] = 42.0;
|
||||
EXPECT_DOUBLE_EQ(42.0, pm2[v]) << "Both handles must alias the same storage";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════
|
||||
// Mesh validity
|
||||
// ════════════════════════════════════════════════════════════
|
||||
|
||||
// CGAL's built-in validity check must pass for all factory meshes.
|
||||
TEST(ConformalMeshValidity, AllBuilders)
|
||||
{
|
||||
EXPECT_TRUE(make_triangle().is_valid()) << "triangle mesh invalid";
|
||||
EXPECT_TRUE(make_tetrahedron().is_valid()) << "tetrahedron mesh invalid";
|
||||
EXPECT_TRUE(make_quad_strip().is_valid()) << "quad strip mesh invalid";
|
||||
EXPECT_TRUE(make_fan(6).is_valid()) << "fan-6 mesh invalid";
|
||||
}
|
||||
269
code/tests/cgal/test_euclidean_functional.cpp
Normal file
269
code/tests/cgal/test_euclidean_functional.cpp
Normal file
@@ -0,0 +1,269 @@
|
||||
// test_euclidean_functional.cpp
|
||||
//
|
||||
// Phase 3d — EuclideanCyclicFunctional ported to ConformalMesh.
|
||||
//
|
||||
// Corresponds to de.varylab.discreteconformal.functional.EuclideanCyclicFunctionalTest.
|
||||
//
|
||||
// Test map (Java → C++)
|
||||
// ──────────────────────
|
||||
// testHessian (Ignored) → GradientCheck_Hessian (SKIPPED)
|
||||
// testGradient…Triangle → GradientCheck_TriangleVertex (ported)
|
||||
// testGradient…QuadStrip → GradientCheck_QuadStripVertex (ported)
|
||||
// testGradient…Tetrahedron → GradientCheck_TetrahedronVertex (ported)
|
||||
// testGradient…AllDofs → GradientCheck_TetrahedronAllDofs (ported)
|
||||
// testFunctionalAtNaNValue → AnglesFiniteAtKnownPoint (ported)
|
||||
//
|
||||
// Energy model
|
||||
// ────────────
|
||||
// Uses the Schläfli path integral E(x) = ∫₀¹⟨G(tx),x⟩dt (10-point GL).
|
||||
// The gradient check verifies G is curl-free.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_geometry.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// @Ignore in Java: no Hessian implemented yet
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_Hessian)
|
||||
{
|
||||
GTEST_SKIP() << "@Ignore in Java – Hessian not yet implemented";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angle formula: equilateral triangle → all angles = π/3
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, EquilateralTriangleAnglesArePiOver3)
|
||||
{
|
||||
// All sides equal: l = 1.0, log-length = 0.
|
||||
auto fa = euclidean_angles(0.0, 0.0, 0.0);
|
||||
|
||||
ASSERT_TRUE(fa.valid) << "Equilateral triangle must be valid";
|
||||
constexpr double PI_3 = 3.14159265358979323846 / 3.0;
|
||||
EXPECT_NEAR(fa.alpha1, PI_3, 1e-12);
|
||||
EXPECT_NEAR(fa.alpha2, PI_3, 1e-12);
|
||||
EXPECT_NEAR(fa.alpha3, PI_3, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angle formula: right isosceles triangle (legs 1, hypotenuse √2)
|
||||
//
|
||||
// For the 45-45-90 triangle: angles are π/4, π/4, π/2.
|
||||
// From make_triangle default (v0=(0,0), v1=(1,0), v2=(0,1)):
|
||||
// e01: l=1, λ°=0
|
||||
// e12: l=√2, λ°=log(2)
|
||||
// e02: l=1, λ°=0
|
||||
// Angle at v0 (opposite e12) = π/2.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, RightIsoscelesTriangleAnglesCorrect)
|
||||
{
|
||||
const double log2 = std::log(2.0);
|
||||
// lam12 = 0 (v0-v1, length 1), lam23 = log(2) (v1-v2, length √2), lam31 = 0 (v2-v0, length 1)
|
||||
// v1 = v0 in our ordering → remap: l01=1, l12=√2, l20=1
|
||||
// Using euclidean_angles(lam_v1v2, lam_v2v3, lam_v3v1):
|
||||
// v1=(0,0), v2=(1,0), v3=(0,1)
|
||||
// lam12 = log(1²) = 0, lam23 = log(√2 ²) = log2, lam31 = log(1²) = 0
|
||||
auto fa = euclidean_angles(0.0, log2, 0.0);
|
||||
|
||||
ASSERT_TRUE(fa.valid);
|
||||
constexpr double PI = 3.14159265358979323846;
|
||||
// v1=(0,0) is at the right-angle corner (opposite the hypotenuse l23=√2) → α1 = 90°.
|
||||
// v2=(1,0) and v3=(0,1) are the 45° corners (each opposite a leg of length 1).
|
||||
EXPECT_NEAR(fa.alpha1, PI / 2.0, 1e-12); // angle at v1 (opposite l23=√2): 90°
|
||||
EXPECT_NEAR(fa.alpha2, PI / 4.0, 1e-12); // angle at v2 (opposite l31=1): 45°
|
||||
EXPECT_NEAR(fa.alpha3, PI / 4.0, 1e-12); // angle at v3 (opposite l12=1): 45°
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angle sum = π for any valid Euclidean triangle
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, AngleSumEqualsPi)
|
||||
{
|
||||
// Scalene triangle with log-lengths (0, 0.5, -0.3).
|
||||
auto fa = euclidean_angles(0.0, 0.5, -0.3);
|
||||
|
||||
ASSERT_TRUE(fa.valid);
|
||||
constexpr double PI = 3.14159265358979323846;
|
||||
EXPECT_NEAR(fa.alpha1 + fa.alpha2 + fa.alpha3, PI, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Degenerate triangle → valid = false
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, DegenerateTriangleReturnsFalse)
|
||||
{
|
||||
// l12 = l23 = 1, l31 = 3 → violates triangle inequality.
|
||||
auto fa = euclidean_angles_from_lengths(1.0, 1.0, 3.0);
|
||||
EXPECT_FALSE(fa.valid);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: default right-isosceles triangle, vertex DOFs only
|
||||
//
|
||||
// Mirrors Java testGradient…SingleTriangle.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_TriangleVertex)
|
||||
{
|
||||
auto mesh = make_triangle(); // (0,0)–(1,0)–(0,1)
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// Small uniform conformal perturbation.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.1);
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed on right-isosceles triangle (vertex DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: quad strip (2 triangles, 1 interior edge), vertex DOFs only
|
||||
//
|
||||
// Mirrors Java testGradient…QuadStrip / testGradientInExtendedDomain.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_QuadStripVertex)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.2);
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed on quad strip (vertex DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: regular tetrahedron, vertex DOFs only
|
||||
//
|
||||
// Closed surface (4 faces, 4 vertices, 6 interior edges).
|
||||
// Exercises per-vertex angle-sum accumulation on multiple faces.
|
||||
// Mirrors Java testGradient…Tetrahedron / testGradientWithHyperIdeal…
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_TetrahedronVertex)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.15);
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed on regular tetrahedron (vertex DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: tetrahedron, all DOFs (vertex + edge)
|
||||
//
|
||||
// Exercises the edge-gradient branch G_e = α_opp⁺ + α_opp⁻ − π.
|
||||
// Mirrors Java testGradientWithHyperellipticCurve.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_TetrahedronAllDofs)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_all_dof_indices(mesh, maps);
|
||||
|
||||
// 4 vertex DOFs + 6 edge DOFs = 10 total.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
// Set vertex DOFs slightly negative to keep triangles non-degenerate.
|
||||
for (int i = 0; i < 4; ++i)
|
||||
x[static_cast<std::size_t>(i)] = -0.15;
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed on regular tetrahedron (all DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angles are finite at a known interior point
|
||||
//
|
||||
// Mirrors Java testFunctionalAtNaNValue: stress-test the angle formula with
|
||||
// large negative conformal factors (compressed triangle) to ensure no NaN/Inf.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, AnglesFiniteAtKnownPoint)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// Very compressed: u_i = -3 (all sides shrunk by exp(-3) ≈ 0.05).
|
||||
// Triangle stays well-formed (equilateral shrinks uniformly).
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -3.0);
|
||||
auto G = euclidean_gradient(mesh, x, maps);
|
||||
|
||||
for (std::size_t i = 0; i < G.size(); ++i) {
|
||||
EXPECT_FALSE(std::isnan(G[i])) << "Gradient component " << i << " is NaN";
|
||||
EXPECT_FALSE(std::isinf(G[i])) << "Gradient component " << i << " is Inf";
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: fan of 5 flat triangles, vertex DOFs only
|
||||
//
|
||||
// High-valence central vertex: exercises per-vertex angle accumulation
|
||||
// across 5 incident faces.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_Fan5Vertex)
|
||||
{
|
||||
auto mesh = make_fan(5);
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.05);
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed on flat fan-5 mesh";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: mixed pinned/variable vertices
|
||||
//
|
||||
// Pins the first vertex (u_v0 = 0 fixed), lets the rest be variable.
|
||||
// Verifies that the gradient accumulator skips pinned vertices correctly.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanFunctional, GradientCheck_MixedPinnedVertices)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Manually pin v0; assign v1, v2, v3 as DOFs 0, 1, 2.
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit++;
|
||||
Vertex_index v3 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // pinned
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
maps.v_idx[v3] = 2;
|
||||
|
||||
std::vector<double> x = {-0.1, -0.3, -0.2};
|
||||
|
||||
EXPECT_TRUE(gradient_check_euclidean(mesh, x, maps))
|
||||
<< "Gradient check failed for mixed pinned/variable vertices";
|
||||
}
|
||||
213
code/tests/cgal/test_euclidean_hessian.cpp
Normal file
213
code/tests/cgal/test_euclidean_hessian.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
// test_euclidean_hessian.cpp
|
||||
//
|
||||
// Phase 3f — Euclidean cotangent-Laplace Hessian.
|
||||
//
|
||||
// The Hessian of the Euclidean discrete conformal energy is the well-known
|
||||
// cotangent-Laplace operator (Pinkall–Polthier 1993, Springborn 2008).
|
||||
//
|
||||
// Tests:
|
||||
// 1. Cotangent weights are analytically correct for simple triangles.
|
||||
// 2. Hessian is symmetric.
|
||||
// 3. Hessian has the null-space property H·1 = 0 (uniform-shift mode).
|
||||
// 4. Hessian is positive semi-definite (all eigenvalues ≥ 0).
|
||||
// 5. Finite-difference check H[i,j] ≈ (G_i(x+ε·eⱼ)−G_i(x−ε·eⱼ))/(2ε).
|
||||
//
|
||||
// All tests use meshes and maps built with Phase-3d infrastructure.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_hessian.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <Eigen/Dense> // for dense conversion and eigenvalue solver
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Cotangent weight: equilateral triangle → all cots = 1/√3 = cot(60°)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, CotWeights_EquilateralTriangle)
|
||||
{
|
||||
// Equilateral triangle with l = 1 (all log-lengths = 0).
|
||||
auto cw = euclidean_cot_weights(1.0, 1.0, 1.0);
|
||||
|
||||
ASSERT_TRUE(cw.valid);
|
||||
const double expected = 1.0 / std::sqrt(3.0); // cot(60°)
|
||||
EXPECT_NEAR(cw.cot1, expected, 1e-12);
|
||||
EXPECT_NEAR(cw.cot2, expected, 1e-12);
|
||||
EXPECT_NEAR(cw.cot3, expected, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Cotangent weight: right-isosceles triangle (legs 1, hypotenuse √2)
|
||||
//
|
||||
// v1=(0,0): right angle → cot(90°) = 0
|
||||
// v2=(1,0), v3=(0,1): 45° angles → cot(45°) = 1
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, CotWeights_RightIsoscelesTriangle)
|
||||
{
|
||||
// l12=1, l23=√2, l31=1
|
||||
auto cw = euclidean_cot_weights(1.0, std::sqrt(2.0), 1.0);
|
||||
|
||||
ASSERT_TRUE(cw.valid);
|
||||
EXPECT_NEAR(cw.cot1, 0.0, 1e-12); // right angle at v1
|
||||
EXPECT_NEAR(cw.cot2, 1.0, 1e-12); // 45° at v2
|
||||
EXPECT_NEAR(cw.cot3, 1.0, 1e-12); // 45° at v3
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Hessian is symmetric: H[i,j] == H[j,i]
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, HessianIsSymmetric)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.1);
|
||||
auto H = euclidean_hessian(mesh, x, maps);
|
||||
|
||||
Eigen::MatrixXd Hd = Eigen::MatrixXd(H);
|
||||
EXPECT_NEAR((Hd - Hd.transpose()).norm(), 0.0, 1e-12)
|
||||
<< "Hessian must be symmetric";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Null-space property: H·1 = 0 for a closed surface (regular tetrahedron)
|
||||
//
|
||||
// The cotangent Laplacian on a closed mesh has the constant vector in its
|
||||
// null space (each row sums to zero).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, NullSpaceIsConstantVector_ClosedMesh)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
auto H = euclidean_hessian(mesh, x, maps);
|
||||
|
||||
// 1-vector
|
||||
Eigen::VectorXd ones = Eigen::VectorXd::Ones(n);
|
||||
Eigen::VectorXd Hones = H * ones;
|
||||
|
||||
EXPECT_NEAR(Hones.norm(), 0.0, 1e-10)
|
||||
<< "H·1 must be zero on a closed mesh (cotangent Laplacian null-space)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Hessian is positive semi-definite: all eigenvalues ≥ 0
|
||||
//
|
||||
// Checked on a small mesh (regular tetrahedron, 4 vertices) using dense
|
||||
// self-adjoint eigenvalue decomposition (only feasible for small n).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, HessianIsPositiveSemiDefinite)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
auto H = euclidean_hessian(mesh, x, maps);
|
||||
|
||||
Eigen::MatrixXd Hd = Eigen::MatrixXd(H);
|
||||
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(Hd);
|
||||
double min_ev = es.eigenvalues().minCoeff();
|
||||
|
||||
EXPECT_GE(min_ev, -1e-10)
|
||||
<< "All eigenvalues of the cotangent Laplacian must be ≥ 0; "
|
||||
"smallest = " << min_ev;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: single right-isosceles triangle
|
||||
//
|
||||
// H[i,j] ≈ (G_i(x+ε·eⱼ) − G_i(x−ε·eⱼ)) / (2ε)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, FDCheck_Triangle)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.1);
|
||||
|
||||
EXPECT_TRUE(hessian_check_euclidean(mesh, x, maps))
|
||||
<< "FD Hessian check failed on right-isosceles triangle";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: quad strip (2 triangles, 1 interior edge)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, FDCheck_QuadStrip)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.1);
|
||||
|
||||
EXPECT_TRUE(hessian_check_euclidean(mesh, x, maps))
|
||||
<< "FD Hessian check failed on quad strip";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: regular tetrahedron (closed, 4 faces)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, FDCheck_Tetrahedron)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_euclidean_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.15);
|
||||
|
||||
EXPECT_TRUE(hessian_check_euclidean(mesh, x, maps))
|
||||
<< "FD Hessian check failed on regular tetrahedron";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: with mixed pinned/variable vertices
|
||||
//
|
||||
// One vertex pinned: the corresponding row/column must be absent from H
|
||||
// while the diagonal of neighbouring variable vertices still gets the full
|
||||
// cotangent contribution.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanHessian, FDCheck_MixedPinnedVertices)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit++;
|
||||
Vertex_index v3 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // pinned
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
maps.v_idx[v3] = 2;
|
||||
|
||||
std::vector<double> x = {-0.1, -0.2, -0.15};
|
||||
|
||||
EXPECT_TRUE(hessian_check_euclidean(mesh, x, maps))
|
||||
<< "FD Hessian check failed for mixed pinned/variable vertices";
|
||||
}
|
||||
510
code/tests/cgal/test_geometry_utils.cpp
Normal file
510
code/tests/cgal/test_geometry_utils.cpp
Normal file
@@ -0,0 +1,510 @@
|
||||
// test_geometry_utils.cpp
|
||||
//
|
||||
// Portierung der Java ConformalLab Geometrie-Utility-Tests.
|
||||
//
|
||||
// Java-Quelle Java-Testmethode Status
|
||||
// ─────────────────────────────────────────────────────────────────────────────────────
|
||||
// CuttinUtilityTest.java testIsInConvexTextureFace_False PORTIERT
|
||||
// CuttinUtilityTest.java testIsInConvexTextureFace_True PORTIERT
|
||||
// UnwrapUtilityTest.java testGetAngleReturnsPI PORTIERT
|
||||
// ConvergenceUtilityTests.java testGetTextureCircumRadius PORTIERT
|
||||
// ConvergenceUtilityTests.java testGetTextureTriangleArea PORTIERT
|
||||
// ConvergenceUtilityTests.java testScaleInvariantCircumCircleRadius PORTIERT
|
||||
// HomologyTest.java testHomology PORTIERT
|
||||
// EuclideanLayoutTest.java testDoLayout PORTIERT
|
||||
// EuclideanCyclicConvergenceTest.java testEuclideanConvergence PORTIERT
|
||||
// SphericalConvergenceTest.java testSphericalConvergence PORTIERT
|
||||
//
|
||||
// ─── Geometrische Grundlage ──────────────────────────────────────────────────────────
|
||||
//
|
||||
// Tests 1–2 Punkt-in-konvexem-Dreieck (2D UV-Raum, baryzentrische Vorzeichen-Methode)
|
||||
// Java: CuttingUtility.isInConvexTextureFace(pp, face, adapters)
|
||||
// Hinweis: Java-Test 2 hat ein 5-elementiges T-Array mit w=0 (Punkt im
|
||||
// Unendlichen), was ein Tippfehler im Original ist. Hier werden
|
||||
// äquivalente, wohlgeformte Koordinaten verwendet.
|
||||
//
|
||||
// Test 3 Eckenwinkel für kollineare Vertices über den Kosinussatz.
|
||||
// Java: UnwrapUtility.getAngle(edge, adapters) — gibt den Winkel am
|
||||
// Zielknoten zurück. Für v0=(-1,0,0), v1=(0,0,0), v2=(1,0,0) ist
|
||||
// der Winkel bei v1 genau π (Dreiecksungleichung entartet).
|
||||
//
|
||||
// Tests 4–5 2D Umkreisradius und Dreiecksfläche.
|
||||
// Java: ConvergenceUtility.getTextureCircumCircleRadius(face)
|
||||
// ConvergenceUtility.getTextureTriangleArea(face)
|
||||
// Formeln: Area = |det([B-A, C-A])| / 2
|
||||
// R = (a·b·c) / (4·Area)
|
||||
//
|
||||
// Test 6 Skaleninvarianter Umkreisradius über ein Mesh.
|
||||
// Java: ConvergenceUtility.getMaxMeanSumScaleInvariantCircumRadius(hds)
|
||||
// Gibt [max, mean, sum] von R_f / sqrt(total_texture_area) zurück.
|
||||
// Invariant unter uniformer Skalierung der Texturkoordinaten (Test mit
|
||||
// homogenem Gewicht w: Position = (T[0]/w, T[1]/w)).
|
||||
//
|
||||
// Test 7 Genus-2 Homologie-Generatoren.
|
||||
// Java: HomologyTest.testHomology (brezel2.obj)
|
||||
// Erwartet: getGeneratorPaths(root).size() == 4 (2g = 4 für g = 2)
|
||||
// C++: compute_cut_graph(mesh).cut_edge_indices.size() == 4
|
||||
// Mesh: code/data/obj/brezel2.obj (V=2622, F=5248, χ=−2, g=2)
|
||||
// Pfad zur Compile-Zeit via CONFORMALLAB_DATA_DIR (CMakeLists.txt).
|
||||
//
|
||||
// Tests 8–9 Layout-Kanten-Längenerhalt (tetraflat.obj).
|
||||
// Java: EuclideanLayoutTest.testDoLayout
|
||||
// Nach Layout mit u=0 müssen UV-Kantenlängen == 3D-Kantenlängen (±1e-10).
|
||||
//
|
||||
// Test 10 Euklidischer Newton auf cathead.obj — Konvergenz + Winkeldefekt.
|
||||
// Java: EuclideanLayoutTest.testLayout02 (130-Werte-Array für cathead.heml)
|
||||
// C++: Newton ab u=0, prüft Konvergenz + Σα_v ≈ 2π für alle inneren Knoten.
|
||||
//
|
||||
// Test 11 Sphärischer Newton auf Oktaeder — Konvergenz + Winkeldefekt.
|
||||
// Java: SphericalConvergenceTest.testSphericalConvergence (Oktaeder, zufällig
|
||||
// störe Radien, seed=1). C++: konstruierter regulärer Oktaeder, prüft
|
||||
// Konvergenz und dass Σα_v ≈ 2π (Target für Sphäre nach prepareInvariantData).
|
||||
//
|
||||
// ─────────────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
#include "cut_graph.hpp"
|
||||
#include "gauss_bonnet.hpp"
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <Eigen/Dense>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Lokale Geometrie-Hilfsfunktionen
|
||||
// (portiert aus Java CuttingUtility / ConvergenceUtility)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Punkt-in-Dreieck Test (2D, baryzentrische Vorzeichenmethode).
|
||||
/// Gibt true zurück wenn p strikt innerhalb oder auf dem Rand von v0-v1-v2 liegt.
|
||||
/// Java: CuttingUtility.isInConvexTextureFace
|
||||
static bool point_in_triangle_2d(
|
||||
Eigen::Vector2d p,
|
||||
Eigen::Vector2d v0, Eigen::Vector2d v1, Eigen::Vector2d v2)
|
||||
{
|
||||
auto cross2d = [](Eigen::Vector2d a, Eigen::Vector2d b) -> double {
|
||||
return a.x() * b.y() - a.y() * b.x();
|
||||
};
|
||||
double d0 = cross2d(v1 - v0, p - v0);
|
||||
double d1 = cross2d(v2 - v1, p - v1);
|
||||
double d2 = cross2d(v0 - v2, p - v2);
|
||||
bool has_neg = (d0 < 0.0) || (d1 < 0.0) || (d2 < 0.0);
|
||||
bool has_pos = (d0 > 0.0) || (d1 > 0.0) || (d2 > 0.0);
|
||||
return !(has_neg && has_pos);
|
||||
}
|
||||
|
||||
/// 2D Dreiecksfläche (halbes Kreuzprodukt).
|
||||
/// Java: ConvergenceUtility.getTextureTriangleArea
|
||||
static double triangle_area_2d(
|
||||
Eigen::Vector2d A, Eigen::Vector2d B, Eigen::Vector2d C)
|
||||
{
|
||||
return std::abs((B - A).x() * (C - A).y()
|
||||
- (B - A).y() * (C - A).x()) * 0.5;
|
||||
}
|
||||
|
||||
/// 2D Umkreisradius: R = (a·b·c) / (4·Area).
|
||||
/// Java: ConvergenceUtility.getTextureCircumCircleRadius
|
||||
static double circumradius_2d(
|
||||
Eigen::Vector2d A, Eigen::Vector2d B, Eigen::Vector2d C)
|
||||
{
|
||||
double a = (B - C).norm();
|
||||
double b = (A - C).norm();
|
||||
double c = (A - B).norm();
|
||||
double area = triangle_area_2d(A, B, C);
|
||||
if (area < 1e-14) return 0.0;
|
||||
return (a * b * c) / (4.0 * area);
|
||||
}
|
||||
|
||||
/// Skaleninvarianter Umkreisradius für ein Mesh:
|
||||
/// scale_R_f = R_f / sqrt(total_area)
|
||||
/// Gibt {max, mean, sum} über alle Flächen zurück.
|
||||
/// Java: ConvergenceUtility.getMaxMeanSumScaleInvariantCircumRadius
|
||||
///
|
||||
/// Homogene Koordinaten: Position = (x/w, y/w).
|
||||
static std::array<double, 3> scale_invariant_circumradius_stats(
|
||||
const std::vector<Eigen::Vector2d>& verts,
|
||||
const std::vector<std::array<int, 3>>& faces)
|
||||
{
|
||||
// Gesamtfläche
|
||||
double total_area = 0.0;
|
||||
for (auto& f : faces)
|
||||
total_area += triangle_area_2d(verts[f[0]], verts[f[1]], verts[f[2]]);
|
||||
if (total_area < 1e-14) return {0, 0, 0};
|
||||
|
||||
double sqrt_total = std::sqrt(total_area);
|
||||
double max_r = 0.0, sum_r = 0.0;
|
||||
for (auto& f : faces) {
|
||||
double R = circumradius_2d(verts[f[0]], verts[f[1]], verts[f[2]]);
|
||||
double sr = R / sqrt_total;
|
||||
max_r = std::max(max_r, sr);
|
||||
sum_r += sr;
|
||||
}
|
||||
double mean_r = sum_r / static_cast<double>(faces.size());
|
||||
return {max_r, mean_r, sum_r};
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Tests 1–2 — CuttingUtility: Punkt-in-konvexem-Dreieck (2D UV-Raum)
|
||||
// Java: CuttinUtilityTest.testIsInConvexTextureFace_False / _True
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
// Test 1: Punkt liegt weit außerhalb — exakte Java-Koordinaten
|
||||
TEST(CuttingUtility, IsInConvexTextureFace_False)
|
||||
{
|
||||
// Winziges Dreieck um (0.7488, 0.0629) — Java-Testkoordinaten (T[3]=1, w=1)
|
||||
Eigen::Vector2d v0(0.7488102998904661, 0.06293998610761144);
|
||||
Eigen::Vector2d v1(0.7487811940754379, 0.06289451051246124);
|
||||
Eigen::Vector2d v2(0.7487254625255592, 0.06291429499873116);
|
||||
// Testpunkt weit entfernt bei (0.447, 0.000228)
|
||||
Eigen::Vector2d pp(0.44661534423161037, 2.2808373704822393e-4);
|
||||
|
||||
EXPECT_FALSE(point_in_triangle_2d(pp, v0, v1, v2));
|
||||
}
|
||||
|
||||
// Test 2: Punkt liegt innerhalb
|
||||
// Hinweis: Das originale Java-Array p2 hat 5 Elemente mit w=0 (Tippfehler im
|
||||
// Java-Original). Hier werden äquivalente, wohlgeformte Koordinaten verwendet,
|
||||
// die dasselbe geometrische Szenario abbilden.
|
||||
TEST(CuttingUtility, IsInConvexTextureFace_True)
|
||||
{
|
||||
// Dreieck: (0,0) — (1e-8, 0) — (0, 1e-8)
|
||||
Eigen::Vector2d v0(0.0, 0.0);
|
||||
Eigen::Vector2d v1(1e-8, 0.0);
|
||||
Eigen::Vector2d v2(0.0, 1e-8);
|
||||
// Schwerpunkt des Dreiecks — liegt immer innen
|
||||
Eigen::Vector2d pp(1e-8 / 3.0, 1e-8 / 3.0);
|
||||
|
||||
EXPECT_TRUE(point_in_triangle_2d(pp, v0, v1, v2));
|
||||
}
|
||||
|
||||
// Zusätzlich: einfaches Einheitsdreieck für Klarheit
|
||||
TEST(CuttingUtility, IsInConvexTextureFace_UnitTriangle_InAndOut)
|
||||
{
|
||||
Eigen::Vector2d v0(0.0, 0.0), v1(1.0, 0.0), v2(0.0, 1.0);
|
||||
EXPECT_TRUE( point_in_triangle_2d(Eigen::Vector2d(0.25, 0.25), v0, v1, v2));
|
||||
EXPECT_FALSE(point_in_triangle_2d(Eigen::Vector2d(2.0, 2.0), v0, v1, v2));
|
||||
EXPECT_FALSE(point_in_triangle_2d(Eigen::Vector2d(0.6, 0.6), v0, v1, v2)); // jenseits Hypotenuse
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 3 — UnwrapUtility: Eckenwinkel = π für kollineare Vertices
|
||||
// Java: UnwrapUtilityTest.testGetAngleReturnsPI
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
// Java: v0=(-1,0,0), v1=(0,0,0), v2=(1,0,0) kollinear.
|
||||
// Kante e von v2 nach v1. getAngle(e) = Winkel bei v1 = π.
|
||||
//
|
||||
// C++: Kosinussatz mit Kantenlängen a=|v0-v1|=1, b=|v1-v2|=1, c=|v0-v2|=2.
|
||||
// cos(γ_v1) = (a² + b² − c²) / (2ab) = (1 + 1 − 4) / 2 = −1 → γ = π
|
||||
TEST(UnwrapUtility, GetAngle_CollinearVertices_ReturnsPI)
|
||||
{
|
||||
const double a = 1.0; // |v0 − v1|
|
||||
const double b = 1.0; // |v1 − v2|
|
||||
const double c = 2.0; // |v0 − v2| (= a + b, entartet)
|
||||
double cos_angle = (a*a + b*b - c*c) / (2.0 * a * b);
|
||||
cos_angle = std::max(-1.0, std::min(1.0, cos_angle)); // numerisches Clamp
|
||||
double angle = std::acos(cos_angle);
|
||||
EXPECT_NEAR(M_PI, angle, 1e-15);
|
||||
}
|
||||
|
||||
// Gegenkontrolle: gleichseitiges Dreieck → Winkel = π/3
|
||||
TEST(UnwrapUtility, GetAngle_EquilateralTriangle_ReturnsPiOver3)
|
||||
{
|
||||
const double s = 1.0;
|
||||
double cos_angle = (s*s + s*s - s*s) / (2.0 * s * s); // = 0.5
|
||||
double angle = std::acos(cos_angle);
|
||||
EXPECT_NEAR(M_PI / 3.0, angle, 1e-15);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 4 — ConvergenceUtility: 2D Umkreisradius
|
||||
// Java: ConvergenceUtilityTests.testGetTextureCircumRadius
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(ConvergenceUtility, TextureCircumRadius_RightTriangle)
|
||||
{
|
||||
// A=(0,0), B=(1,0), C=(0,1): rechtwinkliges gleichschenkliges Dreieck
|
||||
// Seiten: 1, 1, √2. R = √2 / (4 · 0.5) = √2/2
|
||||
Eigen::Vector2d A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0);
|
||||
EXPECT_NEAR(std::sqrt(2.0) / 2.0, circumradius_2d(A, B, C), 1e-10);
|
||||
}
|
||||
|
||||
TEST(ConvergenceUtility, TextureCircumRadius_SmallerTriangle)
|
||||
{
|
||||
// A=(0,0), B=(0.5,0.5), C=(0,1): Java-Variante mit B.T={0.5,0.5,0,1}
|
||||
// Seiten: √0.5, √0.5, 1. Area = 0.25. R = (√0.5·√0.5·1)/(4·0.25) = 0.5
|
||||
Eigen::Vector2d A(0.0, 0.0), B(0.5, 0.5), C(0.0, 1.0);
|
||||
EXPECT_NEAR(0.5, circumradius_2d(A, B, C), 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 5 — ConvergenceUtility: 2D Dreiecksfläche
|
||||
// Java: ConvergenceUtilityTests.testGetTextureTriangleArea
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(ConvergenceUtility, TextureTriangleArea_RightTriangle)
|
||||
{
|
||||
// A=(0,0), B=(1,0), C=(0,1) → Fläche = 0.5
|
||||
Eigen::Vector2d A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0);
|
||||
EXPECT_NEAR(0.5, triangle_area_2d(A, B, C), 1e-10);
|
||||
}
|
||||
|
||||
TEST(ConvergenceUtility, TextureTriangleArea_SmallerTriangle)
|
||||
{
|
||||
// A=(0,0), B=(0.5,0.5), C=(0,1) → Fläche = 0.25
|
||||
Eigen::Vector2d A(0.0, 0.0), B(0.5, 0.5), C(0.0, 1.0);
|
||||
EXPECT_NEAR(0.25, triangle_area_2d(A, B, C), 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 6 — ConvergenceUtility: Skaleninvarianter Umkreisradius
|
||||
// Java: ConvergenceUtilityTests.testScaleInvariantCircumCircleRadius
|
||||
//
|
||||
// Mesh: 4 Vertices (v1..v4), 2 Flächen (f1: v1-v2-v3, f2: v1-v3-v4).
|
||||
// Skaleninvariante Größe: R_f / sqrt(total_area) — invariant unter
|
||||
// uniformer Skalierung (homogeneous weight w: pos = (x/w, y/w)).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(ConvergenceUtility, ScaleInvariantCircumRadius_BaseScale)
|
||||
{
|
||||
// Positionen bei w=1 (T[3]=1): v1=(0,0), v2=(1,0), v3=(0,1), v4=(-1,0)
|
||||
std::vector<Eigen::Vector2d> verts = {
|
||||
{0.0, 0.0}, // v1
|
||||
{1.0, 0.0}, // v2
|
||||
{0.0, 1.0}, // v3
|
||||
{-1.0, 0.0}, // v4
|
||||
};
|
||||
// f1: v1-v2-v3, f2: v1-v3-v4
|
||||
std::vector<std::array<int, 3>> faces = { {0, 1, 2}, {0, 2, 3} };
|
||||
|
||||
// Einzelflächen-Prüfung (Java testGetTextureTriangleArea-Anforderung)
|
||||
EXPECT_NEAR(0.5, triangle_area_2d(verts[0], verts[1], verts[2]), 1e-10);
|
||||
EXPECT_NEAR(0.5, triangle_area_2d(verts[0], verts[2], verts[3]), 1e-10);
|
||||
|
||||
auto [max_r, mean_r, sum_r] = scale_invariant_circumradius_stats(verts, faces);
|
||||
|
||||
// Erwartet: sin(π/4) = √2/2 für max und mean (beide Dreiecke identisch)
|
||||
EXPECT_NEAR(std::sin(M_PI / 4.0), max_r, 1e-10);
|
||||
EXPECT_NEAR(std::sin(M_PI / 4.0), mean_r, 1e-10);
|
||||
EXPECT_NEAR(2.0 * std::sin(M_PI / 4.0), sum_r, 1e-10);
|
||||
}
|
||||
|
||||
TEST(ConvergenceUtility, ScaleInvariantCircumRadius_HalvedByW2_SameResult)
|
||||
{
|
||||
// Skalierung durch w=2: alle Positionen halbiert (homogene Koordinaten)
|
||||
// pos_scaled = (T[0]/2, T[1]/2)
|
||||
std::vector<Eigen::Vector2d> verts = {
|
||||
{0.0, 0.0}, // v1/2
|
||||
{0.5, 0.0}, // v2/2
|
||||
{0.0, 0.5}, // v3/2
|
||||
{-0.5, 0.0}, // v4/2
|
||||
};
|
||||
std::vector<std::array<int, 3>> faces = { {0, 1, 2}, {0, 2, 3} };
|
||||
|
||||
// Flächen sind ein Viertel der ursprünglichen (Längen halbiert → Area / 4)
|
||||
EXPECT_NEAR(0.125, triangle_area_2d(verts[0], verts[1], verts[2]), 1e-10);
|
||||
EXPECT_NEAR(0.125, triangle_area_2d(verts[0], verts[2], verts[3]), 1e-10);
|
||||
|
||||
auto [max_r, mean_r, sum_r] = scale_invariant_circumradius_stats(verts, faces);
|
||||
|
||||
// Skaleninvariante Größe muss identisch zu w=1 sein
|
||||
EXPECT_NEAR(std::sin(M_PI / 4.0), max_r, 1e-10);
|
||||
EXPECT_NEAR(std::sin(M_PI / 4.0), mean_r, 1e-10);
|
||||
EXPECT_NEAR(2.0 * std::sin(M_PI / 4.0), sum_r, 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 7 — HomologyTest: Genus-2 Homologie-Generatoren
|
||||
// Java: HomologyTest.testHomology
|
||||
//
|
||||
// Java-Test:
|
||||
// CoHDS hds = TestUtility.readOBJ("brezel2.obj"); // Genus-2-Brezel-Fläche
|
||||
// List<Set<CoEdge>> paths = getGeneratorPaths(hds.getVertex(0), weightAdapter);
|
||||
// Assert.assertEquals(4, paths.size()); // 2g = 4 für g = 2
|
||||
//
|
||||
// C++-Äquivalent:
|
||||
// ConformalMesh mesh = load_mesh("code/data/obj/brezel2.obj");
|
||||
// CutGraph cg = compute_cut_graph(mesh);
|
||||
// EXPECT_EQ(4u, cg.cut_edge_indices.size()); // 2g = 4
|
||||
// EXPECT_EQ(2, cg.genus);
|
||||
//
|
||||
// Mesh: V=2622, F=5248, E=7872, χ=−2, genus=2.
|
||||
// Pfad via CONFORMALLAB_DATA_DIR (CMakeLists.txt: ${CMAKE_SOURCE_DIR}/data).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HomologyGenerators, Genus2_FourCutEdges)
|
||||
{
|
||||
const std::string path = std::string(CONFORMALLAB_DATA_DIR) + "/obj/brezel2.obj";
|
||||
ConformalMesh mesh;
|
||||
ASSERT_NO_THROW(mesh = load_mesh(path)) << "brezel2.obj not found at: " << path;
|
||||
|
||||
// Topology check: genus-2 surface has χ = -2.
|
||||
EXPECT_EQ(-2, euler_characteristic(mesh));
|
||||
|
||||
// Tree-cotree algorithm must produce exactly 2g = 4 cut edges.
|
||||
CutGraph cg = compute_cut_graph(mesh);
|
||||
EXPECT_EQ(4u, cg.cut_edge_indices.size())
|
||||
<< "Genus-2 surface must have 2g = 4 cut edges (homology generators).";
|
||||
EXPECT_EQ(2, cg.genus);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Tests 8–9 — EuclideanLayoutTest: Kantenlängenerhalt auf tetraflat.obj
|
||||
// Java: EuclideanLayoutTest.testDoLayout
|
||||
//
|
||||
// Java-Test:
|
||||
// Vector u = new SparseVector(n); // u = 0 (kein konformer Faktor)
|
||||
// EuclideanLayout.doLayout(hds, fun, u);
|
||||
// for (CoEdge e : hds.getEdges())
|
||||
// assertEquals(Pn.distanceBetween(s.P, t.P), Pn.distanceBetween(s.T, t.T), 1E-11);
|
||||
//
|
||||
// Bedeutung: Mit u=0 ist der konforme Faktor 0, also ℓ̃ = ℓ (keine Verformung).
|
||||
// Das Layout muss die ursprünglichen 3D-Kantenlängen exakt reproduzieren.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanLayout, DoLayout_TetraFlat_EdgeLengthsPreserved)
|
||||
{
|
||||
const std::string path = std::string(CONFORMALLAB_DATA_DIR) + "/obj/tetraflat.obj";
|
||||
ConformalMesh mesh;
|
||||
ASSERT_NO_THROW(mesh = load_mesh(path)) << "tetraflat.obj not found at: " << path;
|
||||
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// u = 0: no conformal deformation — layout must preserve 3D edge lengths exactly.
|
||||
// tetraflat.obj is an open mesh; pin boundary vertices, sequential DOFs interior.
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices())
|
||||
maps.v_idx[v] = mesh.is_border(v) ? -1 : idx++;
|
||||
const int n = idx;
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
Layout2D layout = euclidean_layout(mesh, x, maps);
|
||||
|
||||
// For every edge: UV length must equal 3D length within 1e-10.
|
||||
for (auto e : mesh.edges()) {
|
||||
auto h = mesh.halfedge(e);
|
||||
auto vs = mesh.source(h);
|
||||
auto vt = mesh.target(h);
|
||||
|
||||
auto ps = mesh.point(vs);
|
||||
auto pt = mesh.point(vt);
|
||||
double l3d = std::sqrt(
|
||||
(pt.x()-ps.x())*(pt.x()-ps.x()) +
|
||||
(pt.y()-ps.y())*(pt.y()-ps.y()) +
|
||||
(pt.z()-ps.z())*(pt.z()-ps.z()));
|
||||
|
||||
auto us = layout.uv[vs.idx()];
|
||||
auto ut = layout.uv[vt.idx()];
|
||||
double luv = (ut - us).norm();
|
||||
|
||||
EXPECT_NEAR(l3d, luv, 1e-10)
|
||||
<< "Edge " << e.idx() << ": 3D=" << l3d << " UV=" << luv;
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 10 — EuclideanCyclicConvergenceTest: Newton auf cathead.obj
|
||||
// Java: EuclideanLayoutTest.testLayout02 (130-Werte-Regression auf cathead.heml)
|
||||
// EuclideanCyclicConvergenceTest.testEuclideanConvergence
|
||||
//
|
||||
// Java-Test:
|
||||
// EuclideanLayout.doLayout(hdsCat, fun, uCat);
|
||||
// for (CoVertex v : interior vertices)
|
||||
// assertEquals(2*PI, calculateAngleSum(v), 1E-6);
|
||||
// for (CoEdge e : positiveEdges)
|
||||
// assertEquals(fun.getNewLength(e, u), tLength, 1E-6);
|
||||
//
|
||||
// C++-Äquivalent: Newton converges on cathead.obj; interior angle sums ≈ 2π.
|
||||
// The 130-value u-vector from the Java test is cathead-topology-specific and
|
||||
// depends on vertex ordering in the Java CoHDS — not portable directly.
|
||||
// Instead we verify the same mathematical invariant: convergence + angle sums.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(EuclideanLayout, CatHead_NewtonConverges_AngleSumsTwoPi)
|
||||
{
|
||||
const std::string path = std::string(CONFORMALLAB_DATA_DIR) + "/obj/cathead.obj";
|
||||
ConformalMesh mesh;
|
||||
ASSERT_NO_THROW(mesh = load_mesh(path)) << "cathead.obj not found at: " << path;
|
||||
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// cathead.obj is an open mesh (boundary present).
|
||||
// Pin boundary vertices (v_idx = -1), assign sequential DOFs to interior.
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices())
|
||||
maps.v_idx[v] = mesh.is_border(v) ? -1 : idx++;
|
||||
const int n = idx;
|
||||
ASSERT_GT(n, 0) << "No interior vertices found in cathead.obj";
|
||||
|
||||
enforce_gauss_bonnet(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
auto res = newton_euclidean(mesh, x0, maps, 1e-8, 200);
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton did not converge on cathead.obj (iterations=" << res.iterations
|
||||
<< ", |G|inf=" << res.grad_inf_norm << ")";
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
EXPECT_LT(res.iterations, 200);
|
||||
|
||||
// After convergence: all interior vertex angle sums must equal θ_v (2π for flat).
|
||||
// Matches Java: assertEquals(2*PI, calculateAngleSum(v), 1E-6) for interior v.
|
||||
auto G_final = euclidean_gradient(mesh, res.x, maps);
|
||||
for (std::size_t i = 0; i < G_final.size(); ++i)
|
||||
EXPECT_NEAR(0.0, G_final[i], 1e-6)
|
||||
<< "Angle sum residual at DOF " << i << " = " << G_final[i];
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 11 — SphericalConvergenceTest: Newton auf Oktaeder
|
||||
// Java: SphericalConvergenceTest.testSphericalConvergence
|
||||
//
|
||||
// Java-Test:
|
||||
// FunctionalTest.createOctahedron(hds, aSet);
|
||||
// // randomly perturb vertex radii (seed=1)
|
||||
// prepareInvariantDataHyperbolicAndSpherical(functional, hds, aSet, u);
|
||||
// optimizer.minimize(u, opt);
|
||||
// for (CoVertex v) assertEquals(2*PI, sum of angles at v, 1E-8);
|
||||
//
|
||||
// C++: regulärer Oktaeder (alle Knoten auf S², keine Störung), sphärischer Newton,
|
||||
// prüft Konvergenz + Restgradienten (≡ Winkeldefekt = 0 nach Konvergenz).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalLayout, SphericalTetrahedron_NewtonConverges_AngleSumsTwoPi)
|
||||
{
|
||||
// Build a spherical tetrahedron (genus 0, 4 vertices, 4 faces).
|
||||
// Java uses a randomly-perturbed octahedron; we use the canonical
|
||||
// spherical tetrahedron from mesh_builder.hpp for reproducibility.
|
||||
ConformalMesh mesh = make_spherical_tetrahedron();
|
||||
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps); // SphericalMaps version
|
||||
int n = assign_vertex_dof_indices(mesh, maps); // pins gauge_vertex, assigns DOFs
|
||||
// Note: enforce_gauss_bonnet not needed — natural theta from mesh satisfies Σ(2π-Θ)>0.
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
|
||||
auto res = newton_spherical(mesh, x0, maps, 1e-8, 200);
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Spherical Newton did not converge (iterations=" << res.iterations
|
||||
<< ", |G|inf=" << res.grad_inf_norm << ")";
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
|
||||
// Angle sum residual = 0 after convergence (≡ each interior vertex has Σα = θ_v).
|
||||
auto G_final = spherical_gradient(mesh, res.x, maps);
|
||||
for (std::size_t i = 0; i < G_final.size(); ++i)
|
||||
EXPECT_NEAR(0.0, G_final[i], 1e-6)
|
||||
<< "Spherical angle sum residual at DOF " << i << " = " << G_final[i];
|
||||
}
|
||||
196
code/tests/cgal/test_hyper_ideal_functional.cpp
Normal file
196
code/tests/cgal/test_hyper_ideal_functional.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
// test_hyper_ideal_functional.cpp
|
||||
//
|
||||
// Phase 3b — HyperIdealFunctional ported to ConformalMesh.
|
||||
//
|
||||
// Corresponds to de.varylab.discreteconformal.functional.HyperIdealFunctionalTest.
|
||||
//
|
||||
// The Java tests use CoHDS + HyperIdealGenerator to build complex meshes and
|
||||
// then verify correctness via a finite-difference gradient check (FunctionalTest).
|
||||
// Here we apply the same gradient-check strategy on simple hand-crafted meshes
|
||||
// (triangle, tetrahedron) to validate the port independently of the generator.
|
||||
//
|
||||
// Test map (Java → C++)
|
||||
// ──────────────────────
|
||||
// testHessian (Ignored) → GradientCheck_Hessian (SKIPPED, not implemented)
|
||||
// testGradientWithHyperIdeal… → GradientCheck_AllHyperIdealTriangle (ported)
|
||||
// testGradientInExtendedDomain → GradientCheck_ExtendedDomain (ported)
|
||||
// testGradientWithHyperelliptic → GradientCheck_TetrahedronAllVariable (ported)
|
||||
// testFunctionalAtNaNValue → EnergyFiniteAtTestPoint (ported)
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "hyper_ideal_hessian.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Helpers
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
// Build a mesh with all vertices and edges variable, and set reasonable
|
||||
// DOF values: b_i = b_val, a_e = a_val.
|
||||
static std::vector<double> make_x_all_variable(
|
||||
ConformalMesh& mesh, HyperIdealMaps& maps,
|
||||
double b_val, double a_val)
|
||||
{
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
std::vector<double> x(static_cast<std::size_t>(n));
|
||||
|
||||
// Vertices first, then edges (matching assign_all_dof_indices order)
|
||||
for (auto v : mesh.vertices())
|
||||
x[static_cast<std::size_t>(maps.v_idx[v])] = b_val;
|
||||
for (auto e : mesh.edges())
|
||||
x[static_cast<std::size_t>(maps.e_idx[e])] = a_val;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// @Ignore in Java: no Hessian implemented
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, HessianSymmetryCheck)
|
||||
{
|
||||
// Hessian is now implemented (numerical FD). Verify it is symmetric.
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.5);
|
||||
auto H = hyper_ideal_hessian_sym(mesh, x, maps);
|
||||
|
||||
Eigen::MatrixXd Hd(H);
|
||||
EXPECT_NEAR((Hd - Hd.transpose()).norm(), 0.0, 1e-8)
|
||||
<< "HyperIdeal Hessian must be symmetric";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: single triangle, all vertices hyper-ideal
|
||||
//
|
||||
// Mirrors testGradientWithHyperIdealAndIdealPoints (simplified to single face).
|
||||
// DOFs: 3 vertex b-values + 3 edge a-values = 6 total.
|
||||
// Point: b_i = 1.0, a_e = 0.5.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, GradientCheck_AllHyperIdealTriangle)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
auto x = make_x_all_variable(mesh, maps, /*b=*/1.0, /*a=*/0.5);
|
||||
|
||||
EXPECT_TRUE(gradient_check(mesh, x, maps))
|
||||
<< "Finite-difference gradient check failed on all-hyper-ideal triangle";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check in the "extended domain"
|
||||
//
|
||||
// Mirrors testGradientInTheExtendedDomain: larger DOF values
|
||||
// (Java: x_i = 1.2 + |rnd|, so typically > 1.2).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, GradientCheck_ExtendedDomain)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
auto x = make_x_all_variable(mesh, maps, /*b=*/2.0, /*a=*/1.5);
|
||||
|
||||
EXPECT_TRUE(gradient_check(mesh, x, maps))
|
||||
<< "Finite-difference gradient check failed in extended domain";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: tetrahedron (4 faces), all vertices and edges variable
|
||||
//
|
||||
// Mirrors testGradientWithHyperellipticCurve — larger mesh, closed surface.
|
||||
// DOFs: 4 vertex b-values + 6 edge a-values = 10 total.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, GradientCheck_TetrahedronAllVariable)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
auto x = make_x_all_variable(mesh, maps, /*b=*/1.0, /*a=*/0.5);
|
||||
|
||||
EXPECT_TRUE(gradient_check(mesh, x, maps))
|
||||
<< "Finite-difference gradient check failed on all-variable tetrahedron";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Energy is finite (not NaN / Inf)
|
||||
//
|
||||
// Mirrors testFunctionalAtNaNValue: evaluates at a test point and checks
|
||||
// the energy is a real number. Uses a quad-strip to exercise the interior
|
||||
// edge path (adjacent faces sharing one non-border edge).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, EnergyFiniteAtTestPoint)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// Values taken from the Java testFunctionalAtNaNValue spirit:
|
||||
// large-ish but positive DOF values that could expose degenerate paths.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 1.5);
|
||||
|
||||
auto res = evaluate_hyper_ideal(mesh, x, maps, true, false);
|
||||
|
||||
EXPECT_FALSE(std::isnan(res.energy)) << "Energy must not be NaN";
|
||||
EXPECT_FALSE(std::isinf(res.energy)) << "Energy must not be Inf";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: mixed vertices (some ideal = pinned, some hyper-ideal)
|
||||
//
|
||||
// One vertex is ideal (v_idx = -1, b = 0), the other two are hyper-ideal.
|
||||
// This exercises the lij / αij branches for the ideal-vertex case.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, GradientCheck_MixedIdealHyperIdeal)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
|
||||
// Make v0 ideal (pinned), v1 and v2 hyper-ideal.
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // ideal: b_0 = 0 (fixed)
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
|
||||
// All edges variable: indices 2, 3, 4
|
||||
int eidx = 2;
|
||||
for (auto e : mesh.edges()) maps.e_idx[e] = eidx++;
|
||||
|
||||
// DOF vector: [b1, b2, a_e0, a_e1, a_e2]
|
||||
std::vector<double> x = {1.0, 1.0, 0.5, 0.5, 0.5};
|
||||
|
||||
EXPECT_TRUE(gradient_check(mesh, x, maps))
|
||||
<< "Finite-difference gradient check failed for mixed ideal / hyper-ideal";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: fan mesh (n=6), all variable
|
||||
//
|
||||
// Exercises the full halfedge-around-vertex traversal for a high-valence
|
||||
// interior vertex.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(HyperIdealFunctional, GradientCheck_Fan6AllVariable)
|
||||
{
|
||||
auto mesh = make_fan(6);
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
auto x = make_x_all_variable(mesh, maps, /*b=*/1.0, /*a=*/0.5);
|
||||
|
||||
EXPECT_TRUE(gradient_check(mesh, x, maps))
|
||||
<< "Finite-difference gradient check failed on fan-6 mesh";
|
||||
}
|
||||
369
code/tests/cgal/test_layout.cpp
Normal file
369
code/tests/cgal/test_layout.cpp
Normal file
@@ -0,0 +1,369 @@
|
||||
// test_layout.cpp
|
||||
//
|
||||
// Phase 5 — Layout / embedding tests.
|
||||
//
|
||||
// Strategy: a conformal map that is the identity (natural equilibrium x*=0)
|
||||
// should reproduce the mesh's own edge lengths. We verify:
|
||||
// 1. Euclidean layout preserves edge lengths (to solver tolerance).
|
||||
// 2. Spherical layout preserves arc-lengths on S².
|
||||
// 3. Layout2D has correct size (one entry per vertex).
|
||||
// 4. Serialisation round-trip: save JSON → load → DOF vector unchanged.
|
||||
// 5. Serialisation round-trip: save XML → load → DOF vector unchanged.
|
||||
// 6. HyperIdeal layout returns success and finite positions.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "serialization.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// Helpers
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
// Euclidean edge length from layout (2D)
|
||||
static double layout_edge_len(const Layout2D& lay,
|
||||
ConformalMesh& mesh, Halfedge_index h)
|
||||
{
|
||||
auto vi = mesh.source(h).idx();
|
||||
auto vj = mesh.target(h).idx();
|
||||
return (lay.uv[vi] - lay.uv[vj]).norm();
|
||||
}
|
||||
|
||||
// Spherical arc-length from layout (3D)
|
||||
static double layout_arc_len(const Layout3D& lay,
|
||||
ConformalMesh& mesh, Halfedge_index h)
|
||||
{
|
||||
auto& a = lay.pos[mesh.source(h).idx()];
|
||||
auto& b = lay.pos[mesh.target(h).idx()];
|
||||
double c = std::max(-1.0, std::min(1.0, a.dot(b)));
|
||||
return std::acos(c);
|
||||
}
|
||||
|
||||
// Euclidean edge length from maps + x (the "true" target)
|
||||
static double maps_edge_len(const EuclideanMaps& m,
|
||||
ConformalMesh& mesh,
|
||||
Halfedge_index h,
|
||||
const std::vector<double>& x)
|
||||
{
|
||||
auto get_u = [&](Vertex_index v) -> double {
|
||||
int iv = m.v_idx[v]; return (iv >= 0) ? x[static_cast<std::size_t>(iv)] : 0.0;
|
||||
};
|
||||
Edge_index e = mesh.edge(h);
|
||||
double lam = m.lambda0[e] + get_u(mesh.source(h)) + get_u(mesh.target(h));
|
||||
return std::exp(lam * 0.5);
|
||||
}
|
||||
|
||||
// Spherical arc-length from maps + x
|
||||
static double maps_arc_len(const SphericalMaps& m,
|
||||
ConformalMesh& mesh,
|
||||
Halfedge_index h,
|
||||
const std::vector<double>& x)
|
||||
{
|
||||
auto get_u = [&](Vertex_index v) -> double {
|
||||
int iv = m.v_idx[v]; return (iv >= 0) ? x[static_cast<std::size_t>(iv)] : 0.0;
|
||||
};
|
||||
Edge_index e = mesh.edge(h);
|
||||
double lam = m.lambda0[e] + get_u(mesh.source(h)) + get_u(mesh.target(h));
|
||||
return 2.0 * std::asin(std::min(std::exp(lam * 0.5), 1.0));
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 1 — Euclidean layout preserves edge lengths (identity map)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, Euclidean_PreservesEdgeLengths)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin first vertex; natural equilibrium so x* = 0
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
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)];
|
||||
}
|
||||
|
||||
// Solve (identity map)
|
||||
auto res = newton_euclidean(mesh, x0, maps, 1e-10, 100);
|
||||
ASSERT_TRUE(res.converged);
|
||||
|
||||
auto layout = euclidean_layout(mesh, res.x, maps);
|
||||
ASSERT_TRUE(layout.success);
|
||||
EXPECT_EQ(layout.uv.size(), mesh.number_of_vertices());
|
||||
|
||||
// Every edge: |layout_len - expected_len| < 1e-8
|
||||
for (auto e : mesh.edges()) {
|
||||
Halfedge_index h = mesh.halfedge(e, 0);
|
||||
double expected = maps_edge_len(maps, mesh, h, res.x);
|
||||
double actual = layout_edge_len(layout, mesh, h);
|
||||
EXPECT_NEAR(actual, expected, 1e-7)
|
||||
<< "Edge " << e << ": expected " << expected << " got " << actual;
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 2 — Euclidean layout: correct vertex count
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, Euclidean_CorrectVertexCount)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
auto layout = euclidean_layout(mesh, x, maps);
|
||||
|
||||
EXPECT_TRUE(layout.success);
|
||||
EXPECT_EQ(static_cast<int>(layout.uv.size()),
|
||||
static_cast<int>(mesh.number_of_vertices()));
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 3 — Euclidean triangle layout: root face is a valid triangle
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, Euclidean_TriangleIsNonDegenerate)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
for (auto v : mesh.vertices()) maps.v_idx[v] = static_cast<int>(v.idx());
|
||||
|
||||
std::vector<double> x(mesh.number_of_vertices(), 0.0);
|
||||
auto layout = euclidean_layout(mesh, x, maps);
|
||||
ASSERT_TRUE(layout.success);
|
||||
|
||||
// Area of layout triangle > 0
|
||||
const auto& A = layout.uv[0];
|
||||
const auto& B = layout.uv[1];
|
||||
const auto& C = layout.uv[2];
|
||||
double area = std::abs((B - A).x() * (C - A).y()
|
||||
- (C - A).x() * (B - A).y()) * 0.5;
|
||||
EXPECT_GT(area, 1e-10) << "Layout triangle must be non-degenerate";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 4 — Spherical layout: arc-lengths preserved (identity map)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, Spherical_PreservesArcLengths)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// Solve to identity
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto G0 = spherical_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)];
|
||||
}
|
||||
auto res = newton_spherical(mesh, x0, maps, 1e-10, 100);
|
||||
ASSERT_TRUE(res.converged);
|
||||
|
||||
auto layout = spherical_layout(mesh, res.x, maps);
|
||||
ASSERT_TRUE(layout.success);
|
||||
EXPECT_EQ(layout.pos.size(), mesh.number_of_vertices());
|
||||
|
||||
for (auto e : mesh.edges()) {
|
||||
Halfedge_index h = mesh.halfedge(e, 0);
|
||||
double expected = maps_arc_len(maps, mesh, h, res.x);
|
||||
double actual = layout_arc_len(layout, mesh, h);
|
||||
EXPECT_NEAR(actual, expected, 1e-6)
|
||||
<< "Spherical edge " << e << ": expected " << expected << " got " << actual;
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 5 — Spherical layout: all positions on unit sphere
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, Spherical_PositionsOnUnitSphere)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
auto layout = spherical_layout(mesh, x, maps);
|
||||
ASSERT_TRUE(layout.success);
|
||||
|
||||
for (auto v : mesh.vertices()) {
|
||||
double r = layout.pos[v.idx()].norm();
|
||||
EXPECT_NEAR(r, 1.0, 1e-12) << "Vertex " << v << " not on unit sphere, |p| = " << r;
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 6 — HyperIdeal layout returns success and finite positions
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Layout, HyperIdeal_SuccessAndFinitePositions)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// Natural equilibrium at (b=1, a=0.5)
|
||||
std::vector<double> xbase(static_cast<std::size_t>(n));
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v]; if (iv >= 0) xbase[iv] = 1.0;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e]; if (ie >= 0) xbase[ie] = 0.5;
|
||||
}
|
||||
auto G0 = evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v]; if (iv >= 0) maps.theta_v[v] += G0[iv];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e]; if (ie >= 0) maps.theta_e[e] += G0[ie];
|
||||
}
|
||||
|
||||
auto res = newton_hyper_ideal(mesh, xbase, maps, 1e-9, 100);
|
||||
ASSERT_TRUE(res.converged);
|
||||
|
||||
auto layout = hyper_ideal_layout(mesh, res.x, maps);
|
||||
EXPECT_TRUE(layout.success);
|
||||
ASSERT_EQ(layout.uv.size(), mesh.number_of_vertices());
|
||||
|
||||
for (auto v : mesh.vertices()) {
|
||||
auto& p = layout.uv[v.idx()];
|
||||
EXPECT_FALSE(std::isnan(p.x())) << "NaN in layout x for v" << v;
|
||||
EXPECT_FALSE(std::isnan(p.y())) << "NaN in layout y for v" << v;
|
||||
// Poincaré disk: all points within the unit disk
|
||||
EXPECT_LE(p.norm(), 1.0 + 1e-9) << "Point outside Poincaré disk for v" << v;
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 7 — JSON serialisation round-trip
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Serialization, JSON_RoundTrip)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.05);
|
||||
auto G0 = euclidean_gradient(mesh, std::vector<double>(n, 0.0), 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)];
|
||||
}
|
||||
auto res = newton_euclidean(mesh, x0, maps, 1e-10, 100);
|
||||
ASSERT_TRUE(res.converged);
|
||||
|
||||
auto layout = euclidean_layout(mesh, res.x, maps);
|
||||
|
||||
const std::string path = "/tmp/conformallab_test_round_trip.json";
|
||||
ASSERT_NO_THROW(save_result_json(path, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout));
|
||||
ASSERT_TRUE(std::filesystem::exists(path));
|
||||
|
||||
NewtonResult res2;
|
||||
std::string geom;
|
||||
Layout2D layout2;
|
||||
auto x2 = load_result_json(path, &res2, &geom, &layout2);
|
||||
|
||||
EXPECT_EQ(geom, "euclidean");
|
||||
EXPECT_EQ(res2.converged, res.converged);
|
||||
EXPECT_EQ(res2.iterations, res.iterations);
|
||||
ASSERT_EQ(x2.size(), res.x.size());
|
||||
for (std::size_t i = 0; i < x2.size(); ++i)
|
||||
EXPECT_NEAR(x2[i], res.x[i], 1e-14);
|
||||
|
||||
EXPECT_TRUE(layout2.success);
|
||||
ASSERT_EQ(layout2.uv.size(), layout.uv.size());
|
||||
for (std::size_t i = 0; i < layout2.uv.size(); ++i) {
|
||||
EXPECT_NEAR(layout2.uv[i].x(), layout.uv[i].x(), 1e-12);
|
||||
EXPECT_NEAR(layout2.uv[i].y(), layout.uv[i].y(), 1e-12);
|
||||
}
|
||||
std::filesystem::remove(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 8 — XML serialisation round-trip
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Serialization, XML_RoundTrip)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
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)];
|
||||
}
|
||||
auto res = newton_euclidean(mesh, x0, maps, 1e-10, 100);
|
||||
ASSERT_TRUE(res.converged);
|
||||
|
||||
auto layout = euclidean_layout(mesh, res.x, maps);
|
||||
|
||||
const std::string path = "/tmp/conformallab_test_round_trip.xml";
|
||||
ASSERT_NO_THROW(save_result_xml(path, res, "euclidean",
|
||||
static_cast<int>(mesh.number_of_vertices()),
|
||||
static_cast<int>(mesh.number_of_faces()),
|
||||
&layout));
|
||||
ASSERT_TRUE(std::filesystem::exists(path));
|
||||
|
||||
NewtonResult res2;
|
||||
std::string geom;
|
||||
Layout2D layout2;
|
||||
auto x2 = load_result_xml(path, &res2, &geom, &layout2);
|
||||
|
||||
EXPECT_EQ(geom, "euclidean");
|
||||
EXPECT_EQ(res2.converged, res.converged);
|
||||
ASSERT_EQ(x2.size(), res.x.size());
|
||||
for (std::size_t i = 0; i < x2.size(); ++i)
|
||||
EXPECT_NEAR(x2[i], res.x[i], 1e-12);
|
||||
|
||||
EXPECT_TRUE(layout2.success);
|
||||
ASSERT_EQ(layout2.uv.size(), layout.uv.size());
|
||||
|
||||
std::filesystem::remove(path);
|
||||
}
|
||||
170
code/tests/cgal/test_mesh_io.cpp
Normal file
170
code/tests/cgal/test_mesh_io.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
// test_mesh_io.cpp
|
||||
//
|
||||
// Phase 4b — CGAL::IO mesh round-trip tests.
|
||||
//
|
||||
// Tests:
|
||||
// 1. OFF write + read round-trip: vertex count, face count, edge count preserved.
|
||||
// 2. OBJ write + read round-trip: same topology checks.
|
||||
// 3. load_mesh() throws on non-existent file.
|
||||
// 4. Round-trip preserves vertex positions (within floating-point precision).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// Helper: temp file path with given extension
|
||||
static std::string tmp_path(const char* ext)
|
||||
{
|
||||
return std::string("/tmp/conformallab_test.") + ext;
|
||||
}
|
||||
|
||||
// Helper: delete file if it exists
|
||||
static void rm(const std::string& path)
|
||||
{
|
||||
std::filesystem::remove(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// OFF round-trip: tetrahedron
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, OFF_RoundTrip_Tetrahedron)
|
||||
{
|
||||
auto path = tmp_path("off");
|
||||
rm(path);
|
||||
|
||||
auto mesh_out = make_tetrahedron();
|
||||
ASSERT_TRUE(write_mesh(path, mesh_out))
|
||||
<< "write_mesh should succeed for tetrahedron";
|
||||
|
||||
ConformalMesh mesh_in;
|
||||
ASSERT_TRUE(read_mesh(path, mesh_in))
|
||||
<< "read_mesh should succeed for the written OFF file";
|
||||
|
||||
EXPECT_EQ(mesh_in.number_of_vertices(), mesh_out.number_of_vertices());
|
||||
EXPECT_EQ(mesh_in.number_of_faces(), mesh_out.number_of_faces());
|
||||
EXPECT_EQ(mesh_in.number_of_edges(), mesh_out.number_of_edges());
|
||||
|
||||
rm(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// OFF round-trip: quad strip
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, OFF_RoundTrip_QuadStrip)
|
||||
{
|
||||
auto path = tmp_path("off");
|
||||
rm(path);
|
||||
|
||||
auto mesh_out = make_quad_strip();
|
||||
ASSERT_TRUE(write_mesh(path, mesh_out));
|
||||
|
||||
ConformalMesh mesh_in;
|
||||
ASSERT_TRUE(read_mesh(path, mesh_in));
|
||||
|
||||
EXPECT_EQ(mesh_in.number_of_vertices(), mesh_out.number_of_vertices());
|
||||
EXPECT_EQ(mesh_in.number_of_faces(), mesh_out.number_of_faces());
|
||||
|
||||
rm(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// OBJ round-trip: tetrahedron
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, OBJ_RoundTrip_Tetrahedron)
|
||||
{
|
||||
auto path = tmp_path("obj");
|
||||
rm(path);
|
||||
|
||||
auto mesh_out = make_tetrahedron();
|
||||
ASSERT_TRUE(write_mesh(path, mesh_out))
|
||||
<< "write_mesh (OBJ) should succeed";
|
||||
|
||||
ConformalMesh mesh_in;
|
||||
ASSERT_TRUE(read_mesh(path, mesh_in))
|
||||
<< "read_mesh (OBJ) should succeed";
|
||||
|
||||
EXPECT_EQ(mesh_in.number_of_vertices(), mesh_out.number_of_vertices());
|
||||
EXPECT_EQ(mesh_in.number_of_faces(), mesh_out.number_of_faces());
|
||||
|
||||
rm(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// load_mesh throws on missing file
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, LoadMeshThrowsOnMissingFile)
|
||||
{
|
||||
EXPECT_THROW(load_mesh("/tmp/does_not_exist_conflab.off"), std::runtime_error);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Vertex positions survive a round-trip (OFF)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, OFF_VertexPositionsPreserved)
|
||||
{
|
||||
auto path = tmp_path("off");
|
||||
rm(path);
|
||||
|
||||
auto mesh_out = make_tetrahedron();
|
||||
ASSERT_TRUE(write_mesh(path, mesh_out));
|
||||
|
||||
ConformalMesh mesh_in;
|
||||
ASSERT_TRUE(read_mesh(path, mesh_in));
|
||||
|
||||
// Collect and sort vertex positions from both meshes to compare
|
||||
auto collect_sorted = [](const ConformalMesh& m) {
|
||||
std::vector<std::array<double,3>> pts;
|
||||
for (auto v : m.vertices()) {
|
||||
auto p = m.point(v);
|
||||
pts.push_back({CGAL::to_double(p.x()),
|
||||
CGAL::to_double(p.y()),
|
||||
CGAL::to_double(p.z())});
|
||||
}
|
||||
std::sort(pts.begin(), pts.end());
|
||||
return pts;
|
||||
};
|
||||
|
||||
auto pts_out = collect_sorted(mesh_out);
|
||||
auto pts_in = collect_sorted(mesh_in);
|
||||
|
||||
ASSERT_EQ(pts_out.size(), pts_in.size());
|
||||
for (std::size_t i = 0; i < pts_out.size(); ++i) {
|
||||
EXPECT_NEAR(pts_out[i][0], pts_in[i][0], 1e-10);
|
||||
EXPECT_NEAR(pts_out[i][1], pts_in[i][1], 1e-10);
|
||||
EXPECT_NEAR(pts_out[i][2], pts_in[i][2], 1e-10);
|
||||
}
|
||||
|
||||
rm(path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// save_mesh / load_mesh convenience wrappers
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MeshIO, SaveLoadConvenienceWrappers)
|
||||
{
|
||||
auto path = tmp_path("off");
|
||||
rm(path);
|
||||
|
||||
auto mesh_out = make_quad_strip();
|
||||
EXPECT_NO_THROW(save_mesh(path, mesh_out));
|
||||
|
||||
ConformalMesh mesh_in;
|
||||
EXPECT_NO_THROW(mesh_in = load_mesh(path));
|
||||
|
||||
EXPECT_EQ(mesh_in.number_of_vertices(), mesh_out.number_of_vertices());
|
||||
|
||||
rm(path);
|
||||
}
|
||||
500
code/tests/cgal/test_newton_solver.cpp
Normal file
500
code/tests/cgal/test_newton_solver.cpp
Normal file
@@ -0,0 +1,500 @@
|
||||
// test_newton_solver.cpp
|
||||
//
|
||||
// Phase 4 — Newton solver tests.
|
||||
//
|
||||
// Design principle:
|
||||
// We test convergence to a KNOWN equilibrium. For the spherical tetrahedron
|
||||
// x* = 0 is built-in (G(0) ≈ 0 by construction). For Euclidean meshes we
|
||||
// use "natural theta": set theta_v[v] = actual angle sum at x=0, which makes
|
||||
// x* = 0 the exact equilibrium by definition.
|
||||
// For HyperIdeal we use the same "natural target" trick at a valid base point
|
||||
// (b=1.0, a=0.5), since x=0 is degenerate for the HyperIdeal functional.
|
||||
//
|
||||
// Tests:
|
||||
// Spherical:
|
||||
// 1. Converges from x=[-0.2,...] to x*=0 (spherical tetrahedron).
|
||||
// 2. Converges in few iterations (quadratic convergence near equilibrium).
|
||||
// 3. Converges from a large perturbation x=[-0.5,...].
|
||||
// 4. Result fields (x.size, grad_inf_norm) are self-consistent.
|
||||
//
|
||||
// Euclidean:
|
||||
// 5. Converges (triangle, 1 pinned vertex, natural theta).
|
||||
// 6. Converges (quad strip, 1 pinned vertex, natural theta).
|
||||
// 7. Converges with explicitly chosen mixed pinned/variable layout.
|
||||
//
|
||||
// HyperIdeal:
|
||||
// 8. Converges on triangle (all variable, natural targets).
|
||||
// 9. Result fields self-consistent.
|
||||
// 10. Converges on tetrahedron (10 DOFs, larger mesh).
|
||||
// 11. SparseQR: result consistent (valid starting region).
|
||||
//
|
||||
// SparseQR fallback (direct unit tests):
|
||||
// 12. solve_linear_system recovers correct solution on rank-deficient matrix.
|
||||
// 13. solve_linear_system sets fallback_used=true on a singular matrix.
|
||||
// 14. Euclidean Newton on closed tetrahedron (no pinned vertex) converges
|
||||
// via SparseQR gauge-mode handling.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include <Eigen/Dense>
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// Helper: set theta_v[v] = actual angle sum at x=0 for each variable vertex.
|
||||
//
|
||||
// Requires that DOF indices have already been assigned (v_idx populated).
|
||||
// Uses euclidean_gradient directly so the formula is exact and consistent.
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
static void set_natural_euclidean_theta(ConformalMesh& mesh, EuclideanMaps& maps, int n)
|
||||
{
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
// G[iv] = theta_v[v] - sum_alpha(x=0)
|
||||
// so sum_alpha(x=0) = theta_v[v] - G[iv]
|
||||
auto G = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] -= G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical 1 — Converges from moderate perturbation
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Spherical_ConvergesFromPerturbation)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.2);
|
||||
auto res = newton_spherical(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/50);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (spherical) should converge; grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical 2 — Quadratic convergence: few iterations suffice
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Spherical_FewIterations)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.2);
|
||||
auto res = newton_spherical(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/50);
|
||||
|
||||
EXPECT_LE(res.iterations, 20)
|
||||
<< "Newton should converge in ≤ 20 iterations; took " << res.iterations;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical 3 — Large perturbation: global convergence via line search
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Spherical_ConvergesFromLargePerturbation)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.5);
|
||||
auto res = newton_spherical(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/100);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (spherical, large perturbation) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical 4 — Result fields are self-consistent
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Spherical_ResultFieldsConsistent)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto res = newton_spherical(mesh, x0, maps, /*tol=*/1e-8);
|
||||
|
||||
EXPECT_EQ(static_cast<int>(res.x.size()), n);
|
||||
|
||||
// Reported grad_inf_norm must match re-computed gradient at res.x
|
||||
auto G = spherical_gradient(mesh, res.x, maps);
|
||||
double actual_inf = 0.0;
|
||||
for (double v : G) actual_inf = std::max(actual_inf, std::abs(v));
|
||||
EXPECT_NEAR(actual_inf, res.grad_inf_norm, 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Euclidean 1 — Triangle with 1 pinned vertex + natural theta
|
||||
//
|
||||
// make_triangle(): 3 vertices. Pin v0 → 2 free DOFs.
|
||||
// With natural theta: x* = 0 (G(0) = 0 by construction).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Euclidean_ConvergesTrianglePinned)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin first vertex, assign sequential DOFs to the other two
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
maps.v_idx[v0] = -1; // pinned
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
int n = idx; // = 2
|
||||
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto res = newton_euclidean(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/50);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (Euclidean, triangle, pinned) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Euclidean 2 — Quad strip with 1 pinned vertex + natural theta
|
||||
//
|
||||
// make_quad_strip(): 4 vertices, 2 faces. Pin v0 → 3 free DOFs.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Euclidean_ConvergesQuadStripPinned)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin first vertex
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
maps.v_idx[v0] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
int n = idx; // = 3
|
||||
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.15);
|
||||
auto res = newton_euclidean(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/50);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (Euclidean, quad strip, pinned) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Euclidean 3 — Mixed pinned layout: explicit vertex assignment
|
||||
//
|
||||
// Quad strip: v0 pinned, v1/v2/v3 free.
|
||||
// Natural theta set AFTER DOF assignment so that x* = 0 is the equilibrium
|
||||
// for the free vertices (with v0 fixed at u0=0).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, Euclidean_ConvergesMixedPinned)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Explicitly assign DOF indices
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit++;
|
||||
Vertex_index v3 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // pinned at u0 = 0
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
maps.v_idx[v3] = 2;
|
||||
const int n = 3;
|
||||
|
||||
// Set natural theta AFTER pinning so that x* = [0,0,0] is the equilibrium
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0 = {-0.1, -0.15, -0.05};
|
||||
auto res = newton_euclidean(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/50);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (Euclidean, mixed pinned) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Helper: set HyperIdeal target angles to actual sums at a non-degenerate
|
||||
// base point (b_base, a_base), making that point the equilibrium x*.
|
||||
//
|
||||
// Note: x = 0 is degenerate for the HyperIdeal functional (log-space; the
|
||||
// functional requires b_i > 0 / a_e > 0). We therefore choose a valid base
|
||||
// point, evaluate G there, and absorb G into the targets so that G(xbase) = 0.
|
||||
// Newton tests then start from a perturbation of xbase.
|
||||
//
|
||||
// Returns xbase so callers can construct a perturbed starting point.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
static std::vector<double> set_natural_hyper_ideal_targets(
|
||||
ConformalMesh& mesh, HyperIdealMaps& maps, int n,
|
||||
double b_base = 1.0, double a_base = 0.5)
|
||||
{
|
||||
const auto sz = static_cast<std::size_t>(n);
|
||||
std::vector<double> xbase(sz, 0.0);
|
||||
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) xbase[static_cast<std::size_t>(iv)] = b_base;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) xbase[static_cast<std::size_t>(ie)] = a_base;
|
||||
}
|
||||
|
||||
// G = Σβ - theta_target (initial target = 0 → G = Σβ = "actual" angles)
|
||||
auto G = evaluate_hyper_ideal(mesh, xbase, maps, /*energy=*/false).gradient;
|
||||
|
||||
// Set target := actual so that G(xbase) = actual - target = 0
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
maps.theta_e[e] += G[static_cast<std::size_t>(ie)];
|
||||
}
|
||||
|
||||
return xbase;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// HyperIdeal 1 — Triangle, all DOFs variable, converges from perturbation
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, HyperIdeal_ConvergesTriangleAllVariable)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// xbase = (b=1.0, a=0.5) is the equilibrium after natural-target setup.
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
|
||||
// Perturb by +0.2 uniformly
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.2;
|
||||
|
||||
auto res = newton_hyper_ideal(mesh, x0, maps, /*tol=*/1e-7, /*max_iter=*/100);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (HyperIdeal, triangle) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-7);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// HyperIdeal 2 — Result fields self-consistent
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, HyperIdeal_ResultFieldsConsistent)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.1;
|
||||
|
||||
auto res = newton_hyper_ideal(mesh, x0, maps, /*tol=*/1e-7, /*max_iter=*/100);
|
||||
|
||||
EXPECT_EQ(static_cast<int>(res.x.size()), n);
|
||||
|
||||
// Reported grad_inf_norm must match re-computed gradient at res.x
|
||||
auto G = evaluate_hyper_ideal(mesh, res.x, maps, false).gradient;
|
||||
double actual_inf = 0.0;
|
||||
for (double v : G) actual_inf = std::max(actual_inf, std::abs(v));
|
||||
EXPECT_NEAR(actual_inf, res.grad_inf_norm, 1e-9);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// HyperIdeal 3 — Tetrahedron (10 DOFs): 4 vertex b-vals + 6 edge a-vals
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, HyperIdeal_ConvergesTetrahedron)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
|
||||
// Perturb by +0.15
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.15;
|
||||
|
||||
auto res = newton_hyper_ideal(mesh, x0, maps, /*tol=*/1e-7, /*max_iter=*/200);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Newton (HyperIdeal, tetrahedron) should converge; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-7);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// HyperIdeal 4 — SparseQR fallback: solver returns a result (no crash)
|
||||
//
|
||||
// With all targets = 0 the equilibrium is not at x=0 but the solver should
|
||||
// at minimum not crash and return a consistent result struct.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NewtonSolver, HyperIdeal_SparseQRFallbackNoCrash)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// Leave targets at their default (0): solver tries to solve but the
|
||||
// "equilibrium" is at some unknown x*. With valid starting point the
|
||||
// Hessian is positive-definite and the solver should not crash.
|
||||
// We don't assert convergence — just that the result struct is consistent.
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 1.0);
|
||||
// Mix vertex / edge DOFs: b=1.0, a=0.5 (valid region of the functional)
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) x0[static_cast<std::size_t>(ie)] = 0.5;
|
||||
}
|
||||
auto res = newton_hyper_ideal(mesh, x0, maps, /*tol=*/1e-7, /*max_iter=*/50);
|
||||
|
||||
// Struct fields must always be populated
|
||||
EXPECT_EQ(static_cast<int>(res.x.size()), n);
|
||||
EXPECT_GE(res.iterations, 0);
|
||||
EXPECT_FALSE(std::isnan(res.grad_inf_norm));
|
||||
EXPECT_FALSE(std::isinf(res.grad_inf_norm));
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// SparseQR fallback — Test 12: solve_linear_system recovers correct solution
|
||||
//
|
||||
// The public API solve_linear_system(A, rhs) must return the correct answer
|
||||
// for a well-conditioned full-rank system (LDLT path taken).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SparseQRFallback, FullRankSystem_CorrectSolution)
|
||||
{
|
||||
// Build a simple 3×3 diagonal PD matrix: A = diag(1, 2, 3)
|
||||
Eigen::SparseMatrix<double> A(3, 3);
|
||||
A.insert(0, 0) = 1.0;
|
||||
A.insert(1, 1) = 2.0;
|
||||
A.insert(2, 2) = 3.0;
|
||||
A.makeCompressed();
|
||||
|
||||
Eigen::VectorXd rhs(3);
|
||||
rhs << 1.0, 4.0, 9.0; // solution = [1, 2, 3]
|
||||
|
||||
bool fallback = true; // expect it to be set to false (LDLT succeeds)
|
||||
Eigen::VectorXd x = conformallab::solve_linear_system(A, rhs, &fallback);
|
||||
|
||||
EXPECT_FALSE(fallback) << "Full-rank system: LDLT should succeed (no SparseQR needed)";
|
||||
EXPECT_NEAR(x[0], 1.0, 1e-12);
|
||||
EXPECT_NEAR(x[1], 2.0, 1e-12);
|
||||
EXPECT_NEAR(x[2], 3.0, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// SparseQR fallback — Test 13: fallback_used=true on a singular matrix
|
||||
//
|
||||
// Construct a symmetric 3×3 matrix of rank 1 where LDLT fails (the (2,2)
|
||||
// pivot is zero). SparseQR finds the minimum-norm least-squares solution.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SparseQRFallback, SingularMatrix_FallbackActivated)
|
||||
{
|
||||
// A = [[2, 0, 0],
|
||||
// [0, 0, 0], ← zero pivot → LDLT failure
|
||||
// [0, 0, 3]]
|
||||
// rhs compatible with the row space: [2, 0, 3] → solution [1, 0, 1]
|
||||
Eigen::SparseMatrix<double> A(3, 3);
|
||||
A.insert(0, 0) = 2.0;
|
||||
// row/col 1 deliberately all-zero
|
||||
A.insert(2, 2) = 3.0;
|
||||
A.makeCompressed();
|
||||
|
||||
Eigen::VectorXd rhs(3);
|
||||
rhs << 2.0, 0.0, 3.0;
|
||||
|
||||
bool fallback = false;
|
||||
Eigen::VectorXd x = conformallab::solve_linear_system(A, rhs, &fallback);
|
||||
|
||||
EXPECT_TRUE(fallback) << "Singular matrix: SparseQR fallback must be triggered";
|
||||
// SparseQR min-norm solution: x[0]=1, x[1]=0, x[2]=1
|
||||
EXPECT_NEAR(x[0], 1.0, 1e-10);
|
||||
EXPECT_NEAR(x[1], 0.0, 1e-10);
|
||||
EXPECT_NEAR(x[2], 1.0, 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// SparseQR fallback — Test 14: Euclidean Newton on a closed mesh, no pinning
|
||||
//
|
||||
// make_tetrahedron() is a closed surface (4 vertices, 4 faces). Without a
|
||||
// pinned vertex the Euclidean Hessian has a 1-D null space (uniform scale
|
||||
// gauge mode): H·1 = 0. SimplicialLDLT fails on this rank-deficient H;
|
||||
// SparseQR finds the min-norm Newton step orthogonal to the null space.
|
||||
//
|
||||
// The gradient always lives in the row space of H (Σ G_v = 0 by angle-sum
|
||||
// invariance), so the SparseQR step is also the Newton step and the solver
|
||||
// converges to the natural equilibrium.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SparseQRFallback, Euclidean_ClosedMeshNoPinConverges)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Assign all 4 vertices as free DOFs (no pinning).
|
||||
int idx = 0;
|
||||
for (auto v : mesh.vertices())
|
||||
maps.v_idx[v] = idx++;
|
||||
const int n = idx; // = 4
|
||||
|
||||
// Natural theta: equilibrium at x* = 0.
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto res = newton_euclidean(mesh, x0, maps, /*tol=*/1e-8, /*max_iter=*/100);
|
||||
|
||||
EXPECT_TRUE(res.converged)
|
||||
<< "Euclidean Newton on closed tetrahedron (no pin) must converge via SparseQR; "
|
||||
"grad_inf_norm = " << res.grad_inf_norm;
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
}
|
||||
406
code/tests/cgal/test_phase6.cpp
Normal file
406
code/tests/cgal/test_phase6.cpp
Normal file
@@ -0,0 +1,406 @@
|
||||
// test_phase6.cpp
|
||||
//
|
||||
// Phase 6 — Tests for:
|
||||
// - gauss_bonnet.hpp : euler_characteristic, genus, GB sum/rhs, check, enforce
|
||||
// - cut_graph.hpp : compute_cut_graph (tree-cotree algorithm)
|
||||
// - layout.hpp Phase6 : exact hyperbolic trilateration math
|
||||
// normalise_euclidean (centroid + PCA)
|
||||
// normalise_hyperbolic (Möbius centering)
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "gauss_bonnet.hpp"
|
||||
#include "cut_graph.hpp"
|
||||
#include "layout.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// mesh_builder.hpp provides make_triangle(), make_tetrahedron(), make_quad_strip().
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// GaussBonnet — topology helpers
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(GaussBonnet, EulerCharacteristic_Triangle)
|
||||
{
|
||||
// V=3 E=3 F=1 → χ = 1
|
||||
auto m = make_triangle();
|
||||
EXPECT_EQ(euler_characteristic(m), 1);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, EulerCharacteristic_QuadStrip)
|
||||
{
|
||||
// V=4 E=5 F=2 → χ = 1
|
||||
auto m = make_quad_strip();
|
||||
EXPECT_EQ(euler_characteristic(m), 1);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, EulerCharacteristic_Tetrahedron)
|
||||
{
|
||||
// V=4 E=6 F=4 → χ = 2
|
||||
auto m = make_tetrahedron();
|
||||
EXPECT_EQ(euler_characteristic(m), 2);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, Genus_Tetrahedron_IsZero)
|
||||
{
|
||||
auto m = make_tetrahedron();
|
||||
EXPECT_EQ(genus(m), 0);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, RHS_Triangle)
|
||||
{
|
||||
// χ=1 → 2π·χ = 2π
|
||||
auto m = make_triangle();
|
||||
EXPECT_NEAR(gauss_bonnet_rhs(m), 2.0 * M_PI, 1e-12);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, RHS_Tetrahedron)
|
||||
{
|
||||
// χ=2 → 2π·χ = 4π
|
||||
auto m = make_tetrahedron();
|
||||
EXPECT_NEAR(gauss_bonnet_rhs(m), 4.0 * M_PI, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// GaussBonnet — sum / deficit / check / enforce
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(GaussBonnet, DeficitIsNonZeroWithDefaultTheta)
|
||||
{
|
||||
// Default theta_v = 2π at all V=4 vertices of quad-strip →
|
||||
// Σ(2π−2π) = 0, rhs = 2π·1 = 2π → deficit = −2π ≠ 0.
|
||||
auto m = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
double def = gauss_bonnet_deficit(m, maps);
|
||||
EXPECT_GT(std::abs(def), 1e-6);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, EnforceAdjustsTheta_Deficit_BecomesZero)
|
||||
{
|
||||
auto m = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
// Set clearly wrong theta
|
||||
for (auto v : m.vertices()) maps.theta_v[v] = M_PI;
|
||||
EXPECT_GT(std::abs(gauss_bonnet_deficit(m, maps)), 1e-6);
|
||||
|
||||
enforce_gauss_bonnet(m, maps);
|
||||
EXPECT_NEAR(gauss_bonnet_deficit(m, maps), 0.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, CheckThrowsWhenViolated)
|
||||
{
|
||||
auto m = make_triangle();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
// theta_v = 2π everywhere → sum = 0, rhs = 2π → |deficit| = 2π
|
||||
EXPECT_THROW(check_gauss_bonnet(m, maps), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, CheckPassesAfterEnforce)
|
||||
{
|
||||
auto m = make_triangle();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
enforce_gauss_bonnet(m, maps);
|
||||
EXPECT_NO_THROW(check_gauss_bonnet(m, maps));
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, SumAfterEnforce_EqualsRHS)
|
||||
{
|
||||
auto m = make_tetrahedron();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
// theta_v starts at 2π everywhere; sum = 0, rhs = 4π
|
||||
enforce_gauss_bonnet(m, maps);
|
||||
double lhs = gauss_bonnet_sum(m, maps);
|
||||
double rhs = gauss_bonnet_rhs(m);
|
||||
EXPECT_NEAR(lhs, rhs, 1e-10);
|
||||
}
|
||||
|
||||
TEST(GaussBonnet, ManuallySetAnalyticalTheta_PassesCheck)
|
||||
{
|
||||
// Single triangle (χ=1): need Σ(2π−Θ_v) = 2π.
|
||||
// Set each of the 3 boundary vertices to Θ_v = 4π/3.
|
||||
// Then Σ(2π − 4π/3) = 3·(2π/3) = 2π. ✓
|
||||
auto m = make_triangle();
|
||||
auto maps = setup_euclidean_maps(m);
|
||||
for (auto v : m.vertices()) maps.theta_v[v] = 4.0 * M_PI / 3.0;
|
||||
EXPECT_NO_THROW(check_gauss_bonnet(m, maps));
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// CutGraph — tree-cotree algorithm
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(CutGraph, Triangle_ZeroCutEdges)
|
||||
{
|
||||
// Open disk → genus 0 → 0 cut edges.
|
||||
auto m = make_triangle();
|
||||
auto cg = compute_cut_graph(m);
|
||||
EXPECT_EQ(cg.cut_edge_indices.size(), 0u);
|
||||
}
|
||||
|
||||
TEST(CutGraph, QuadStrip_ZeroCutEdges)
|
||||
{
|
||||
// Open disk → genus 0 → 0 cut edges.
|
||||
auto m = make_quad_strip();
|
||||
auto cg = compute_cut_graph(m);
|
||||
EXPECT_EQ(cg.cut_edge_indices.size(), 0u);
|
||||
}
|
||||
|
||||
TEST(CutGraph, Tetrahedron_ZeroCutEdges_ClosedSphere)
|
||||
{
|
||||
// Closed genus-0 surface → 2g = 0 cut edges.
|
||||
auto m = make_tetrahedron();
|
||||
auto cg = compute_cut_graph(m);
|
||||
EXPECT_EQ(cg.cut_edge_indices.size(), 0u);
|
||||
EXPECT_EQ(cg.genus, 0);
|
||||
}
|
||||
|
||||
TEST(CutGraph, FlagsVsIndicesConsistent)
|
||||
{
|
||||
// Every index in cut_edge_indices has flag=true;
|
||||
// count of true flags == number of indices.
|
||||
auto m = make_tetrahedron();
|
||||
auto cg = compute_cut_graph(m);
|
||||
for (std::size_t idx : cg.cut_edge_indices)
|
||||
EXPECT_TRUE(cg.cut_edge_flags[idx]);
|
||||
|
||||
std::size_t count = 0;
|
||||
for (bool b : cg.cut_edge_flags) count += b ? 1u : 0u;
|
||||
EXPECT_EQ(count, cg.cut_edge_indices.size());
|
||||
}
|
||||
|
||||
TEST(CutGraph, IsCutMethodMatchesFlags)
|
||||
{
|
||||
auto m = make_tetrahedron();
|
||||
auto cg = compute_cut_graph(m);
|
||||
for (auto e : m.edges()) {
|
||||
bool flag = cg.cut_edge_flags[static_cast<std::size_t>(e.idx())];
|
||||
EXPECT_EQ(cg.is_cut(e), flag);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CutGraph, FlagsVectorHasCorrectSize)
|
||||
{
|
||||
auto m = make_quad_strip();
|
||||
auto cg = compute_cut_graph(m);
|
||||
EXPECT_EQ(cg.cut_edge_flags.size(), m.number_of_edges());
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Exact hyperbolic trilateration — Möbius + law of cosines
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
namespace {
|
||||
|
||||
/// Poincaré-disk hyperbolic distance.
|
||||
double hyp_dist_disk(Eigen::Vector2d a, Eigen::Vector2d b)
|
||||
{
|
||||
double num = (a.x()-b.x())*(a.x()-b.x()) + (a.y()-b.y())*(a.y()-b.y());
|
||||
double da = 1.0 - a.x()*a.x() - a.y()*a.y();
|
||||
double db_ = 1.0 - b.x()*b.x() - b.y()*b.y();
|
||||
double arg = 1.0 + 2.0*num/(da*db_);
|
||||
return std::acosh(std::max(1.0, arg));
|
||||
}
|
||||
|
||||
/// Reproduce the exact trilateration formula from layout.hpp detail namespace.
|
||||
Eigen::Vector2d trilaterate_hyp(Eigen::Vector2d pa, Eigen::Vector2d pb,
|
||||
double D, double da, double db)
|
||||
{
|
||||
if (D < 1e-12 || da < 1e-12) return pa;
|
||||
if (std::sinh(da) * std::sinh(D) < 1e-14) return pa;
|
||||
using C = std::complex<double>;
|
||||
auto mobius_fwd = [](C z, C center) -> C {
|
||||
return (z - center) / (C(1.0) - std::conj(center) * z);
|
||||
};
|
||||
auto mobius_inv = [](C w, C center) -> C {
|
||||
return (w + center) / (C(1.0) + std::conj(center) * w);
|
||||
};
|
||||
C a(pa.x(), pa.y()), b(pb.x(), pb.y());
|
||||
C b_mapped = mobius_fwd(b, a);
|
||||
double theta_b = std::arg(b_mapped);
|
||||
double cos_alpha = (std::cosh(da) * std::cosh(D) - std::cosh(db))
|
||||
/ (std::sinh(da) * std::sinh(D));
|
||||
cos_alpha = std::max(-1.0, std::min(1.0, cos_alpha));
|
||||
double alpha = std::acos(cos_alpha);
|
||||
C pc_origin = std::tanh(da * 0.5) * std::exp(C(0.0, theta_b + alpha));
|
||||
C pc_c = mobius_inv(pc_origin, a);
|
||||
return Eigen::Vector2d(pc_c.real(), pc_c.imag());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(HyperbolicTrilateration, RecoveredDistances_Small)
|
||||
{
|
||||
// pa at origin, pb at tanh(D/2) on x-axis.
|
||||
double D = 0.8, da = 0.5, db = 0.6;
|
||||
Eigen::Vector2d pa(0.0, 0.0);
|
||||
Eigen::Vector2d pb(std::tanh(D * 0.5), 0.0);
|
||||
|
||||
Eigen::Vector2d pc = trilaterate_hyp(pa, pb, D, da, db);
|
||||
|
||||
EXPECT_NEAR(hyp_dist_disk(pa, pb), D, 1e-10);
|
||||
EXPECT_NEAR(hyp_dist_disk(pa, pc), da, 1e-9);
|
||||
EXPECT_NEAR(hyp_dist_disk(pb, pc), db, 1e-9);
|
||||
}
|
||||
|
||||
TEST(HyperbolicTrilateration, RecoveredDistances_Large)
|
||||
{
|
||||
double D = 2.0, da = 1.5, db = 1.0;
|
||||
Eigen::Vector2d pa(0.0, 0.0);
|
||||
Eigen::Vector2d pb(std::tanh(D * 0.5), 0.0);
|
||||
|
||||
Eigen::Vector2d pc = trilaterate_hyp(pa, pb, D, da, db);
|
||||
|
||||
EXPECT_NEAR(hyp_dist_disk(pa, pc), da, 1e-8);
|
||||
EXPECT_NEAR(hyp_dist_disk(pb, pc), db, 1e-8);
|
||||
}
|
||||
|
||||
TEST(HyperbolicTrilateration, ResultInsidePoincareDisk)
|
||||
{
|
||||
double D = 1.2, da = 0.9, db = 0.7;
|
||||
Eigen::Vector2d pa(0.0, 0.0);
|
||||
Eigen::Vector2d pb(std::tanh(D * 0.5), 0.0);
|
||||
|
||||
Eigen::Vector2d pc = trilaterate_hyp(pa, pb, D, da, db);
|
||||
EXPECT_LT(pc.norm(), 1.0);
|
||||
}
|
||||
|
||||
TEST(HyperbolicTrilateration, OffOrigin_StillCorrect)
|
||||
{
|
||||
// Place pa somewhere in the disk (not at origin) to test Möbius map.
|
||||
double D = 0.6, da = 0.4, db = 0.5;
|
||||
// pa at (0.3, 0.0) in Poincaré coords.
|
||||
Eigen::Vector2d pa(0.3, 0.0);
|
||||
// pb at distance D from pa — compute pb in Poincaré disk:
|
||||
// Möbius inverse: tanh(D/2) maps back from origin frame.
|
||||
using C = std::complex<double>;
|
||||
C a_c(pa.x(), pa.y());
|
||||
C pb_c = (std::tanh(D*0.5) + a_c) / (C(1.0) + std::conj(a_c) * std::tanh(D*0.5));
|
||||
Eigen::Vector2d pb(pb_c.real(), pb_c.imag());
|
||||
|
||||
Eigen::Vector2d pc = trilaterate_hyp(pa, pb, D, da, db);
|
||||
EXPECT_LT(pc.norm(), 1.0);
|
||||
EXPECT_NEAR(hyp_dist_disk(pa, pc), da, 1e-8);
|
||||
EXPECT_NEAR(hyp_dist_disk(pb, pc), db, 1e-8);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Normalisation — euclidean_layout (centroid) + hyper_ideal_layout (Möbius)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/// Build a solved euclidean layout for the quad-strip at the natural equilibrium.
|
||||
static Layout2D make_euclidean_layout_normalised(bool normalise)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin first vertex; assign free DOF indices to all others.
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
const int n = idx;
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
// Adjust theta so G(x=0) = 0 (natural equilibrium).
|
||||
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)];
|
||||
}
|
||||
|
||||
auto res = newton_euclidean(mesh, x0, maps, 1e-10, 100);
|
||||
return euclidean_layout(mesh, res.x, maps, nullptr, nullptr, normalise);
|
||||
}
|
||||
|
||||
TEST(Normalisation, Euclidean_CentroidAtOrigin)
|
||||
{
|
||||
auto L = make_euclidean_layout_normalised(true);
|
||||
ASSERT_GT(L.uv.size(), 0u);
|
||||
|
||||
double cx = 0.0, cy = 0.0;
|
||||
for (auto& p : L.uv) { cx += p.x(); cy += p.y(); }
|
||||
int n = static_cast<int>(L.uv.size());
|
||||
EXPECT_NEAR(cx / n, 0.0, 1e-9);
|
||||
EXPECT_NEAR(cy / n, 0.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST(Normalisation, Euclidean_NormalisePreservesEdgeLengthRatios)
|
||||
{
|
||||
auto L_no = make_euclidean_layout_normalised(false);
|
||||
auto L_yes = make_euclidean_layout_normalised(true);
|
||||
|
||||
// Ratio of the lengths of the first two edges must be preserved.
|
||||
ASSERT_GE(L_no.uv.size(), 4u);
|
||||
// edges 0→1 and 0→2 (the two edges from vertex 0 in the quad-strip)
|
||||
double len0_no = (L_no.uv[1] - L_no.uv[0]).norm();
|
||||
double len1_no = (L_no.uv[2] - L_no.uv[0]).norm();
|
||||
double len0_yes = (L_yes.uv[1] - L_yes.uv[0]).norm();
|
||||
double len1_yes = (L_yes.uv[2] - L_yes.uv[0]).norm();
|
||||
|
||||
if (len1_no > 1e-12 && len1_yes > 1e-12) {
|
||||
double ratio_no = len0_no / len1_no;
|
||||
double ratio_yes = len0_yes / len1_yes;
|
||||
EXPECT_NEAR(ratio_no, ratio_yes, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a solved hyper-ideal layout for a triangle at natural equilibrium.
|
||||
static Layout2D make_hyper_ideal_layout_normalised(bool normalise)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> xbase(static_cast<std::size_t>(n));
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v]; if (iv >= 0) xbase[iv] = 1.0;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e]; if (ie >= 0) xbase[ie] = 0.5;
|
||||
}
|
||||
auto G0 = evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v]; if (iv >= 0) maps.theta_v[v] += G0[iv];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e]; if (ie >= 0) maps.theta_e[e] += G0[ie];
|
||||
}
|
||||
auto res = newton_hyper_ideal(mesh, xbase, maps, 1e-9, 100);
|
||||
return hyper_ideal_layout(mesh, res.x, maps, nullptr, nullptr, normalise);
|
||||
}
|
||||
|
||||
TEST(Normalisation, HyperIdeal_PositionsInsidePoincareDisk)
|
||||
{
|
||||
auto L = make_hyper_ideal_layout_normalised(true);
|
||||
ASSERT_GT(L.uv.size(), 0u);
|
||||
for (auto& p : L.uv) {
|
||||
EXPECT_LT(p.norm(), 1.0 + 1e-9)
|
||||
<< "Vertex outside Poincaré disk: r=" << p.norm();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Normalisation, HyperIdeal_CenteringReducesAverageRadius)
|
||||
{
|
||||
// After Möbius centering the average Euclidean radius inside the disk
|
||||
// must be ≤ the unconstrained average.
|
||||
auto L_no = make_hyper_ideal_layout_normalised(false);
|
||||
auto L_yes = make_hyper_ideal_layout_normalised(true);
|
||||
|
||||
int n = static_cast<int>(L_no.uv.size());
|
||||
double r_no = 0.0, r_yes = 0.0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
r_no += L_no.uv[i].norm();
|
||||
r_yes += L_yes.uv[i].norm();
|
||||
}
|
||||
EXPECT_LE(r_yes / n, r_no / n + 1e-9);
|
||||
}
|
||||
485
code/tests/cgal/test_phase7.cpp
Normal file
485
code/tests/cgal/test_phase7.cpp
Normal file
@@ -0,0 +1,485 @@
|
||||
// test_phase7.cpp
|
||||
//
|
||||
// Phase 7 — Tests for Java-parity layout features:
|
||||
// - MobiusMap : identity, inverse, compose, from_three, is_identity
|
||||
// - best_root_face : selects a valid face; interior bonus
|
||||
// - halfedge_uv : size, non-seam consistency, seam divergence
|
||||
// - Priority BFS : vertex ordering / depth correctness
|
||||
// - normalise_euclidean : halfedge_uv centroid at origin
|
||||
// - period_matrix.hpp : τ in upper half-plane, SL(2,ℤ) reduction
|
||||
// - fundamental_domain.hpp: parallelogram CCW, generators, tiling_copy
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "period_matrix.hpp"
|
||||
#include "fundamental_domain.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
|
||||
using namespace conformallab;
|
||||
using C = std::complex<double>;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// MobiusMap
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(MobiusMap, Identity_AppliesAsIdentity)
|
||||
{
|
||||
MobiusMap id = MobiusMap::identity();
|
||||
C z(0.3, 0.7);
|
||||
C w = id.apply(z);
|
||||
EXPECT_NEAR(w.real(), z.real(), 1e-12);
|
||||
EXPECT_NEAR(w.imag(), z.imag(), 1e-12);
|
||||
}
|
||||
|
||||
TEST(MobiusMap, Identity_IsIdentity)
|
||||
{
|
||||
EXPECT_TRUE(MobiusMap::identity().is_identity());
|
||||
}
|
||||
|
||||
TEST(MobiusMap, NonIdentity_IsNotIdentity)
|
||||
{
|
||||
// T(z) = z + 1 — translation, clearly not identity
|
||||
MobiusMap T{ C(1), C(1), C(0), C(1) };
|
||||
EXPECT_FALSE(T.is_identity());
|
||||
}
|
||||
|
||||
TEST(MobiusMap, Inverse_ComposeIsIdentity)
|
||||
{
|
||||
// T(z) = (2z + 1) / (z + 3)
|
||||
MobiusMap T{ C(2), C(1), C(1), C(3) };
|
||||
MobiusMap TinvT = T.inverse().compose(T);
|
||||
EXPECT_TRUE(TinvT.is_identity(1e-9));
|
||||
}
|
||||
|
||||
TEST(MobiusMap, Compose_OrderCorrect)
|
||||
{
|
||||
// S: z ↦ z + 1, T: z ↦ 2z
|
||||
// S.compose(T) means S applied after T: z ↦ 2z + 1
|
||||
MobiusMap S{ C(1), C(1), C(0), C(1) }; // z + 1
|
||||
MobiusMap T{ C(2), C(0), C(0), C(1) }; // 2z
|
||||
MobiusMap ST = S.compose(T);
|
||||
C z(1.0, 0.0);
|
||||
// S(T(z)) = S(2) = 3
|
||||
EXPECT_NEAR(ST.apply(z).real(), 3.0, 1e-12);
|
||||
EXPECT_NEAR(ST.apply(z).imag(), 0.0, 1e-12);
|
||||
}
|
||||
|
||||
TEST(MobiusMap, FromThree_RecoversMap)
|
||||
{
|
||||
// Known map T(z) = (z + i) / (1 + 0·z) — translation by i
|
||||
C w1 = C(0, 1) + C(0, 1); // T(i) = 2i
|
||||
C w2 = C(1, 0) + C(0, 1); // T(1) = 1 + i
|
||||
C w3 = C(-1, 0) + C(0, 1); // T(-1) = -1 + i
|
||||
MobiusMap T = MobiusMap::from_three(C(0, 1), w1, C(1, 0), w2, C(-1, 0), w3);
|
||||
// Verify T maps a fourth point correctly: T(0) = i
|
||||
C result = T.apply(C(0, 0));
|
||||
EXPECT_NEAR(result.real(), 0.0, 1e-9);
|
||||
EXPECT_NEAR(result.imag(), 1.0, 1e-9);
|
||||
}
|
||||
|
||||
TEST(MobiusMap, FromThree_DegenerateReturnsIdentity)
|
||||
{
|
||||
// Three coincident points → singular system → identity fallback
|
||||
C z(0.5, 0.5);
|
||||
MobiusMap T = MobiusMap::from_three(z, z, z, z, z, z);
|
||||
// Should not crash; returns identity (or at least a valid map)
|
||||
// We just check the result is finite
|
||||
C w = T.apply(C(0.1, 0.2));
|
||||
EXPECT_FALSE(std::isnan(w.real()));
|
||||
EXPECT_FALSE(std::isnan(w.imag()));
|
||||
}
|
||||
|
||||
TEST(MobiusMap, Apply_Vector2d)
|
||||
{
|
||||
MobiusMap id = MobiusMap::identity();
|
||||
Eigen::Vector2d p(0.4, 0.6);
|
||||
Eigen::Vector2d q = id.apply(p);
|
||||
EXPECT_NEAR(q.x(), p.x(), 1e-12);
|
||||
EXPECT_NEAR(q.y(), p.y(), 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// best_root_face
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(BestRootFace, ReturnsValidFace_Triangle)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
Face_index f = detail::best_root_face(mesh);
|
||||
EXPECT_NE(f, Face_index());
|
||||
EXPECT_GE(f.idx(), 0);
|
||||
}
|
||||
|
||||
TEST(BestRootFace, ReturnsValidFace_Tetrahedron)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
Face_index f = detail::best_root_face(mesh);
|
||||
EXPECT_NE(f, Face_index());
|
||||
// Tetrahedron has 4 faces — best is one of them
|
||||
EXPECT_LT(static_cast<std::size_t>(f.idx()), mesh.number_of_faces());
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// halfedge_uv — size and non-seam consistency
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
// Helper: build equilibrium Euclidean layout for a given mesh.
|
||||
// Uses x = 0 (identity scale factor) which is the equilibrium for natural edge lengths.
|
||||
static Layout2D make_euclidean_layout(ConformalMesh& mesh)
|
||||
{
|
||||
EuclideanMaps maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
// Pin first vertex (DOF = -1); assign sequential indices to the rest.
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
std::vector<double> x(static_cast<std::size_t>(idx), 0.0);
|
||||
return euclidean_layout(mesh, x, maps);
|
||||
}
|
||||
|
||||
TEST(HalfedgeUV, Size_EqualsNumberOfHalfedges_Triangle)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
EXPECT_EQ(lay.halfedge_uv.size(), mesh.number_of_halfedges());
|
||||
}
|
||||
|
||||
TEST(HalfedgeUV, Size_EqualsNumberOfHalfedges_QuadStrip)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
EXPECT_EQ(lay.halfedge_uv.size(), mesh.number_of_halfedges());
|
||||
}
|
||||
|
||||
TEST(HalfedgeUV, NonBorderHalfedges_MatchUV)
|
||||
{
|
||||
// For an open mesh with no cut graph the layout has no seams.
|
||||
// Every non-border halfedge h must satisfy:
|
||||
// halfedge_uv[h] == uv[source(h)]
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
for (auto h : mesh.halfedges()) {
|
||||
if (mesh.is_border(h)) continue;
|
||||
std::size_t hi = static_cast<std::size_t>(h.idx());
|
||||
std::size_t vi = static_cast<std::size_t>(mesh.source(h).idx());
|
||||
EXPECT_NEAR(lay.halfedge_uv[hi].x(), lay.uv[vi].x(), 1e-10)
|
||||
<< "halfedge " << hi << " source vertex " << vi;
|
||||
EXPECT_NEAR(lay.halfedge_uv[hi].y(), lay.uv[vi].y(), 1e-10)
|
||||
<< "halfedge " << hi << " source vertex " << vi;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HalfedgeUV, BorderHalfedges_AreZero)
|
||||
{
|
||||
auto mesh = make_triangle();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
bool found_border = false;
|
||||
for (auto h : mesh.halfedges()) {
|
||||
if (!mesh.is_border(h)) continue;
|
||||
std::size_t hi = static_cast<std::size_t>(h.idx());
|
||||
EXPECT_NEAR(lay.halfedge_uv[hi].x(), 0.0, 1e-12);
|
||||
EXPECT_NEAR(lay.halfedge_uv[hi].y(), 0.0, 1e-12);
|
||||
found_border = true;
|
||||
}
|
||||
EXPECT_TRUE(found_border);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Priority BFS — depth ordering
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(PriorityBFS, Layout_SucceedsOnOpenMesh)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
EXPECT_TRUE(lay.success);
|
||||
}
|
||||
|
||||
TEST(PriorityBFS, Layout_NoSeamOnOpenMesh)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
EXPECT_FALSE(lay.has_seam);
|
||||
}
|
||||
|
||||
TEST(PriorityBFS, AllVerticesPlaced)
|
||||
{
|
||||
auto mesh = make_tetrahedron();
|
||||
// Tetrahedron is closed; layout without cut graph will have a seam
|
||||
EuclideanMaps maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit) maps.v_idx[*vit] = idx++;
|
||||
std::vector<double> x(static_cast<std::size_t>(idx), 0.0);
|
||||
auto lay = euclidean_layout(mesh, x, maps);
|
||||
EXPECT_TRUE(lay.success);
|
||||
// All UVs must be finite
|
||||
for (auto& p : lay.uv) {
|
||||
EXPECT_FALSE(std::isnan(p.x()));
|
||||
EXPECT_FALSE(std::isnan(p.y()));
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// normalise_euclidean — centroid + PCA applied to both uv and halfedge_uv
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(NormaliseEuclidean, UVCentroidAtOrigin)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
normalise_euclidean(lay);
|
||||
|
||||
Eigen::Vector2d mean = Eigen::Vector2d::Zero();
|
||||
for (auto& p : lay.uv) mean += p;
|
||||
mean /= static_cast<double>(lay.uv.size());
|
||||
EXPECT_NEAR(mean.x(), 0.0, 1e-10);
|
||||
EXPECT_NEAR(mean.y(), 0.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(NormaliseEuclidean, HalfedgeUVCentroidAlsoShifted)
|
||||
{
|
||||
// After normalisation: the non-border halfedge_uv entries should also be
|
||||
// centred (since they are shifted by the same mean as uv).
|
||||
// We verify that the mean of non-border halfedge_uv is near (0,0).
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
normalise_euclidean(lay);
|
||||
|
||||
Eigen::Vector2d mean = Eigen::Vector2d::Zero();
|
||||
int count = 0;
|
||||
for (auto h : mesh.halfedges()) {
|
||||
if (mesh.is_border(h)) continue;
|
||||
mean += lay.halfedge_uv[static_cast<std::size_t>(h.idx())];
|
||||
++count;
|
||||
}
|
||||
if (count > 0) mean /= static_cast<double>(count);
|
||||
EXPECT_NEAR(mean.x(), 0.0, 1e-9);
|
||||
EXPECT_NEAR(mean.y(), 0.0, 1e-9);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// PeriodMatrix — reduce_to_fundamental_domain
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(PeriodMatrix, ReduceToFD_AlreadyInFD)
|
||||
{
|
||||
// τ = i is in F (|i|=1, Re(i)=0, Im(i)=1>0)
|
||||
C tau(0.0, 1.0);
|
||||
C reduced = reduce_to_fundamental_domain(tau);
|
||||
EXPECT_TRUE(is_in_fundamental_domain(reduced));
|
||||
EXPECT_NEAR(reduced.real(), 0.0, 1e-10);
|
||||
EXPECT_NEAR(reduced.imag(), 1.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, ReduceToFD_ShiftsRealPart)
|
||||
{
|
||||
// τ = 2 + 3i → T step: τ -= 2 → 3i (|3i|=3≥1, Re=0)
|
||||
C tau(2.0, 3.0);
|
||||
C reduced = reduce_to_fundamental_domain(tau);
|
||||
EXPECT_TRUE(is_in_fundamental_domain(reduced, 1e-9));
|
||||
EXPECT_NEAR(reduced.real(), 0.0, 1e-10);
|
||||
EXPECT_NEAR(reduced.imag(), 3.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, ReduceToFD_InvertsSmallTau)
|
||||
{
|
||||
// τ = 0.5i → |0.5i|=0.5<1 → S: τ↦-1/(0.5i) = 2i
|
||||
C tau(0.0, 0.5);
|
||||
C reduced = reduce_to_fundamental_domain(tau);
|
||||
EXPECT_TRUE(is_in_fundamental_domain(reduced, 1e-9));
|
||||
EXPECT_NEAR(reduced.real(), 0.0, 1e-10);
|
||||
EXPECT_NEAR(reduced.imag(), 2.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, ReduceToFD_ThrowsForNonUpperHalfPlane)
|
||||
{
|
||||
C tau(0.5, -1.0); // Im < 0 → not in upper half-plane
|
||||
EXPECT_THROW(reduce_to_fundamental_domain(tau), std::domain_error);
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, IsInFundamentalDomain_Square)
|
||||
{
|
||||
EXPECT_TRUE(is_in_fundamental_domain(C(0.0, 1.0))); // i
|
||||
EXPECT_TRUE(is_in_fundamental_domain(C(0.3, 1.5))); // inside
|
||||
EXPECT_FALSE(is_in_fundamental_domain(C(0.6, 1.5))); // Re > 1/2
|
||||
EXPECT_FALSE(is_in_fundamental_domain(C(0.0, 0.5))); // |τ| < 1
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, ComputePeriodMatrix_UnitSquare)
|
||||
{
|
||||
// ω_1 = (1, 0), ω_2 = (0, 1) → τ = i
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1.0, 0.0), Eigen::Vector2d(0.0, 1.0) };
|
||||
PeriodData pd = compute_period_matrix(hol, /*reduce=*/false);
|
||||
EXPECT_EQ(pd.genus(), 1);
|
||||
EXPECT_GT(pd.tau.imag(), 0.0);
|
||||
EXPECT_NEAR(pd.tau.real(), 0.0, 1e-10);
|
||||
EXPECT_NEAR(pd.tau.imag(), 1.0, 1e-10);
|
||||
}
|
||||
|
||||
TEST(PeriodMatrix, ComputePeriodMatrix_ReducedTau_InFD)
|
||||
{
|
||||
// ω_1 = (1, 0), ω_2 = (0.5, 0.25) → τ = 0.5 + 0.25i
|
||||
// |τ| = sqrt(0.25 + 0.0625) ≈ 0.559 < 1 → needs S step
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1.0, 0.0), Eigen::Vector2d(0.5, 0.25) };
|
||||
PeriodData pd = compute_period_matrix(hol, /*reduce=*/true);
|
||||
EXPECT_TRUE(pd.in_fundamental_domain);
|
||||
EXPECT_TRUE(is_in_fundamental_domain(pd.tau, 1e-9));
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// FundamentalDomain — genus-1 parallelogram
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(FundamentalDomain, Genus1_HasFourVertices)
|
||||
{
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1.0, 0.0), Eigen::Vector2d(0.0, 1.0) };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
EXPECT_EQ(fd.vertices.size(), 4u);
|
||||
EXPECT_TRUE(fd.is_valid());
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, Genus1_VerticesMatchGenerators_UnitSquare)
|
||||
{
|
||||
Eigen::Vector2d w1(1.0, 0.0), w2(0.0, 1.0);
|
||||
HolonomyData hol;
|
||||
hol.translations = { w1, w2 };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
// Expected (CCW): origin, w1, w1+w2, w2
|
||||
EXPECT_NEAR(fd.vertices[0].x(), 0.0, 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[0].y(), 0.0, 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[1].x(), w1.x(), 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[1].y(), w1.y(), 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[2].x(), (w1 + w2).x(), 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[2].y(), (w1 + w2).y(), 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[3].x(), w2.x(), 1e-12);
|
||||
EXPECT_NEAR(fd.vertices[3].y(), w2.y(), 1e-12);
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, Genus1_CCWOrientation)
|
||||
{
|
||||
// After possible swap, the signed area = cross(v1-v0, v3-v0) > 0 (CCW)
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1.0, 0.0), Eigen::Vector2d(0.0, 1.0) };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
Eigen::Vector2d v0 = fd.vertices[0], v1 = fd.vertices[1], v3 = fd.vertices[3];
|
||||
double cross = (v1 - v0).x() * (v3 - v0).y() - (v1 - v0).y() * (v3 - v0).x();
|
||||
EXPECT_GT(cross, 0.0);
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, Genus1_CCWEnforced_WhenInputIsCW)
|
||||
{
|
||||
// If we give CW generators (w2 × w1 < 0), the polygon must still be CCW.
|
||||
// w1 = (0,1), w2 = (1,0): cross w1×w2 = 0*0 - 1*1 = -1 < 0 → should swap
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(0.0, 1.0), Eigen::Vector2d(1.0, 0.0) };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
Eigen::Vector2d v0 = fd.vertices[0], v1 = fd.vertices[1], v3 = fd.vertices[3];
|
||||
double cross = (v1 - v0).x() * (v3 - v0).y() - (v1 - v0).y() * (v3 - v0).x();
|
||||
EXPECT_GT(cross, 0.0);
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, Genus1_EdgeIdentifications)
|
||||
{
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1.0, 0.0), Eigen::Vector2d(0.0, 1.0) };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
EXPECT_EQ(fd.edge_identifications.size(), 2u);
|
||||
// bottom ≡ top: (0,2)
|
||||
EXPECT_EQ(fd.edge_identifications[0].first, 0);
|
||||
EXPECT_EQ(fd.edge_identifications[0].second, 2);
|
||||
// right ≡ left: (1,3)
|
||||
EXPECT_EQ(fd.edge_identifications[1].first, 1);
|
||||
EXPECT_EQ(fd.edge_identifications[1].second, 3);
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, Genus1_GeneratorsStored)
|
||||
{
|
||||
Eigen::Vector2d w1(2.0, 1.0), w2(-1.0, 3.0);
|
||||
HolonomyData hol;
|
||||
hol.translations = { w1, w2 };
|
||||
FundamentalDomain fd = compute_fundamental_domain_genus1(hol);
|
||||
EXPECT_EQ(fd.generators.size(), 2u);
|
||||
// Generators are w1 and w2 (possibly swapped to ensure CCW)
|
||||
// Their sum of norms matches the originals
|
||||
double norm_gen = fd.generators[0].norm() + fd.generators[1].norm();
|
||||
double norm_in = w1.norm() + w2.norm();
|
||||
EXPECT_NEAR(norm_gen, norm_in, 1e-10);
|
||||
}
|
||||
|
||||
TEST(FundamentalDomain, HigherGenus_ReturnsEmpty)
|
||||
{
|
||||
HolonomyData hol;
|
||||
hol.translations = {
|
||||
Eigen::Vector2d(1, 0), Eigen::Vector2d(0, 1),
|
||||
Eigen::Vector2d(2, 0), Eigen::Vector2d(0, 2) // g=2, 4 generators
|
||||
};
|
||||
FundamentalDomain fd = compute_fundamental_domain(hol);
|
||||
// g > 1 returns empty (TODO Phase 8)
|
||||
EXPECT_FALSE(fd.is_valid());
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// tiling_copy / tiling_neighbourhood
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(TilingCopy, ShiftAppliedToAllUV)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
|
||||
Eigen::Vector2d w1(3.0, 0.0), w2(0.0, 2.0);
|
||||
// m=1, n=2 → expected shift = w1 + 2*w2 = (3, 4)
|
||||
Layout2D copy = tiling_copy(lay, w1, w2, 1, 2);
|
||||
Eigen::Vector2d expected_shift(3.0, 4.0);
|
||||
for (std::size_t i = 0; i < lay.uv.size(); ++i) {
|
||||
EXPECT_NEAR(copy.uv[i].x(), lay.uv[i].x() + expected_shift.x(), 1e-12);
|
||||
EXPECT_NEAR(copy.uv[i].y(), lay.uv[i].y() + expected_shift.y(), 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TilingCopy, ZeroShift_IsSameAsCopy)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
Eigen::Vector2d w1(1, 0), w2(0, 1);
|
||||
Layout2D copy = tiling_copy(lay, w1, w2, 0, 0);
|
||||
for (std::size_t i = 0; i < lay.uv.size(); ++i) {
|
||||
EXPECT_NEAR(copy.uv[i].x(), lay.uv[i].x(), 1e-12);
|
||||
EXPECT_NEAR(copy.uv[i].y(), lay.uv[i].y(), 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TilingNeighbourhood, CorrectCount)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
HolonomyData hol;
|
||||
hol.translations = { Eigen::Vector2d(1, 0), Eigen::Vector2d(0, 1) };
|
||||
// m_max=1, n_max=1 → (2*1+1) * (2*1+1) = 9 tiles
|
||||
auto tiles = tiling_neighbourhood(lay, hol, 1, 1);
|
||||
EXPECT_EQ(tiles.size(), 9u);
|
||||
}
|
||||
|
||||
TEST(TilingNeighbourhood, EmptyHolonomy_ReturnsSingleTile)
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto lay = make_euclidean_layout(mesh);
|
||||
HolonomyData hol; // no translations
|
||||
auto tiles = tiling_neighbourhood(lay, hol);
|
||||
EXPECT_EQ(tiles.size(), 1u);
|
||||
}
|
||||
292
code/tests/cgal/test_pipeline.cpp
Normal file
292
code/tests/cgal/test_pipeline.cpp
Normal file
@@ -0,0 +1,292 @@
|
||||
// test_pipeline.cpp
|
||||
//
|
||||
// Phase 4c — End-to-end pipeline tests and library-user examples.
|
||||
//
|
||||
// These tests exercise the full conformallab++ pipeline as a user would:
|
||||
//
|
||||
// 1. Build (or load) a mesh
|
||||
// 2. Set up maps and assign DOFs
|
||||
// 3. Configure target angles / targets
|
||||
// 4. Solve with Newton
|
||||
// 5. Inspect / export the result
|
||||
//
|
||||
// Each test mirrors a realistic usage scenario documented in the README.
|
||||
//
|
||||
// Tests:
|
||||
// 1. Pipeline_Euclidean_TriangleToEquilibrium
|
||||
// Read mesh → setup Euclidean maps → solve → verify convergence
|
||||
// 2. Pipeline_Spherical_TetrahedronToEquilibrium
|
||||
// Setup spherical tetrahedron → solve → verify angles sum to 4π
|
||||
// 3. Pipeline_HyperIdeal_TriangleRoundTrip
|
||||
// Build triangle → setup HyperIdeal → solve → verify G ≈ 0
|
||||
// 4. Pipeline_MeshIO_SolveAndExport
|
||||
// Build mesh → solve → write OFF → reload → verify vertex count intact
|
||||
// 5. Pipeline_AllThreeGeometries_SameTopology
|
||||
// Same quad-strip mesh solved under all three geometries: all converge
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// Shared helpers
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
static void pin_first_vertex_euclidean(ConformalMesh& mesh, EuclideanMaps& maps, int& n)
|
||||
{
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
maps.v_idx[v0] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
n = idx;
|
||||
}
|
||||
|
||||
static void set_natural_euclidean_theta(ConformalMesh& mesh, EuclideanMaps& maps, int n)
|
||||
{
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), 0.0);
|
||||
auto G = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] -= G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<double> set_natural_hyper_ideal_targets(
|
||||
ConformalMesh& mesh, HyperIdealMaps& maps, int n,
|
||||
double b_base = 1.0, double a_base = 0.5)
|
||||
{
|
||||
const auto sz = static_cast<std::size_t>(n);
|
||||
std::vector<double> xbase(sz, 0.0);
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) xbase[static_cast<std::size_t>(iv)] = b_base;
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie >= 0) xbase[static_cast<std::size_t>(ie)] = a_base;
|
||||
}
|
||||
auto G = evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv < 0) continue;
|
||||
maps.theta_v[v] += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
for (auto e : mesh.edges()) {
|
||||
int ie = maps.e_idx[e];
|
||||
if (ie < 0) continue;
|
||||
maps.theta_e[e] += G[static_cast<std::size_t>(ie)];
|
||||
}
|
||||
return xbase;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 1 — Euclidean full pipeline: triangle → equilibrium
|
||||
//
|
||||
// Simulates a user doing:
|
||||
// auto mesh = make_triangle();
|
||||
// auto maps = setup_euclidean_maps(mesh);
|
||||
// compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
// // … set target angles and DOF indices …
|
||||
// auto result = newton_euclidean(mesh, x0, maps);
|
||||
// assert(result.converged);
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Pipeline, Euclidean_TriangleToEquilibrium)
|
||||
{
|
||||
// ── Step 1: build mesh ────────────────────────────────────────────────
|
||||
auto mesh = make_triangle();
|
||||
|
||||
// ── Step 2: set up maps ───────────────────────────────────────────────
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// ── Step 3: assign DOFs (pin v0) ──────────────────────────────────────
|
||||
int n = 0;
|
||||
pin_first_vertex_euclidean(mesh, maps, n);
|
||||
ASSERT_EQ(n, 2);
|
||||
|
||||
// ── Step 4: choose natural target angles → x* = 0 ────────────────────
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
// ── Step 5: solve ─────────────────────────────────────────────────────
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto result = newton_euclidean(mesh, x0, maps);
|
||||
|
||||
// ── Step 6: verify ────────────────────────────────────────────────────
|
||||
EXPECT_TRUE(result.converged)
|
||||
<< "Euclidean pipeline: triangle should converge; "
|
||||
"grad_inf_norm = " << result.grad_inf_norm;
|
||||
EXPECT_LT(result.grad_inf_norm, 1e-8);
|
||||
EXPECT_EQ(static_cast<int>(result.x.size()), n);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 2 — Spherical full pipeline: tetrahedron → equilibrium
|
||||
//
|
||||
// The spherical tetrahedron equilibrium x* = 0 is built into the maps.
|
||||
// After solving, the total angle defect Σ(Θ_v − Σα_v) should be ≈ 0.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Pipeline, Spherical_TetrahedronToEquilibrium)
|
||||
{
|
||||
// ── Steps 1–3 ─────────────────────────────────────────────────────────
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// ── Step 4: solve ─────────────────────────────────────────────────────
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.2);
|
||||
auto result = newton_spherical(mesh, x0, maps);
|
||||
|
||||
// ── Step 5: verify convergence ────────────────────────────────────────
|
||||
EXPECT_TRUE(result.converged)
|
||||
<< "Spherical pipeline: tetrahedron should converge; "
|
||||
"grad_inf_norm = " << result.grad_inf_norm;
|
||||
EXPECT_LT(result.grad_inf_norm, 1e-8);
|
||||
|
||||
// ── Step 6: verify geometric invariant — total angle defect ≈ 0 ──────
|
||||
auto G_final = spherical_gradient(mesh, result.x, maps);
|
||||
double total_defect = 0.0;
|
||||
for (double gv : G_final) total_defect += gv;
|
||||
EXPECT_NEAR(total_defect, 0.0, 1e-7)
|
||||
<< "Spherical: total angle defect should vanish at equilibrium";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 3 — HyperIdeal full pipeline: triangle → equilibrium
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Pipeline, HyperIdeal_TriangleRoundTrip)
|
||||
{
|
||||
// ── Steps 1–3 ─────────────────────────────────────────────────────────
|
||||
auto mesh = make_triangle();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
|
||||
// ── Step 4: natural targets (equilibrium at b=1.0, a=0.5) ────────────
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
|
||||
// Verify: gradient at xbase must be ≈ 0 before solving
|
||||
auto G_at_base = evaluate_hyper_ideal(mesh, xbase, maps, false).gradient;
|
||||
double max_g = 0.0;
|
||||
for (double v : G_at_base) max_g = std::max(max_g, std::abs(v));
|
||||
ASSERT_LT(max_g, 1e-10) << "Natural target setup: gradient at base should be ~0";
|
||||
|
||||
// ── Step 5: perturb and solve ─────────────────────────────────────────
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.3;
|
||||
|
||||
auto result = newton_hyper_ideal(mesh, x0, maps);
|
||||
|
||||
// ── Step 6: verify ────────────────────────────────────────────────────
|
||||
EXPECT_TRUE(result.converged)
|
||||
<< "HyperIdeal pipeline: triangle should converge; "
|
||||
"grad_inf_norm = " << result.grad_inf_norm;
|
||||
EXPECT_LT(result.grad_inf_norm, 1e-8);
|
||||
|
||||
// Solution should be close to xbase (same equilibrium)
|
||||
for (int i = 0; i < n; ++i) {
|
||||
EXPECT_NEAR(result.x[static_cast<std::size_t>(i)],
|
||||
xbase[static_cast<std::size_t>(i)], 1e-6)
|
||||
<< "DOF " << i << " should recover the equilibrium value";
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 4 — Mesh I/O in the pipeline: solve → write → reload → check
|
||||
//
|
||||
// Demonstrates: compute a conformal factor on a mesh, write it to OFF, reload
|
||||
// and check that the mesh topology is preserved.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Pipeline, MeshIO_SolveAndExport)
|
||||
{
|
||||
// ── Build and solve ───────────────────────────────────────────────────
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
int n = 0;
|
||||
pin_first_vertex_euclidean(mesh, maps, n);
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto result = newton_euclidean(mesh, x0, maps);
|
||||
ASSERT_TRUE(result.converged) << "Solver must converge before export test";
|
||||
|
||||
// ── Write mesh ────────────────────────────────────────────────────────
|
||||
const std::string tmp_path = "/tmp/conformallab_pipeline_test.off";
|
||||
ASSERT_NO_THROW(save_mesh(tmp_path, mesh));
|
||||
ASSERT_TRUE(std::filesystem::exists(tmp_path));
|
||||
|
||||
// ── Reload and verify topology ────────────────────────────────────────
|
||||
ConformalMesh mesh2;
|
||||
ASSERT_NO_THROW(mesh2 = load_mesh(tmp_path));
|
||||
|
||||
EXPECT_EQ(mesh2.number_of_vertices(), mesh.number_of_vertices())
|
||||
<< "Vertex count must survive OFF round-trip";
|
||||
EXPECT_EQ(mesh2.number_of_faces(), mesh.number_of_faces())
|
||||
<< "Face count must survive OFF round-trip";
|
||||
|
||||
std::filesystem::remove(tmp_path);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Test 5 — All three geometries, same quad-strip topology
|
||||
//
|
||||
// Validates that the solver infrastructure works uniformly: the same mesh
|
||||
// topology is solvable under Euclidean, Spherical, and HyperIdeal geometries.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(Pipeline, AllThreeGeometries_QuadStrip)
|
||||
{
|
||||
// ── Euclidean ──────────────────────────────────────────────────────────
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
int n = 0;
|
||||
pin_first_vertex_euclidean(mesh, maps, n);
|
||||
set_natural_euclidean_theta(mesh, maps, n);
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto res = newton_euclidean(mesh, x0, maps);
|
||||
EXPECT_TRUE(res.converged) << "Euclidean: quad strip should converge";
|
||||
}
|
||||
|
||||
// ── HyperIdeal ────────────────────────────────────────────────────────
|
||||
{
|
||||
auto mesh = make_quad_strip();
|
||||
auto maps = setup_hyper_ideal_maps(mesh);
|
||||
int n = assign_all_dof_indices(mesh, maps);
|
||||
auto xbase = set_natural_hyper_ideal_targets(mesh, maps, n);
|
||||
std::vector<double> x0 = xbase;
|
||||
for (auto& v : x0) v += 0.1;
|
||||
auto res = newton_hyper_ideal(mesh, x0, maps);
|
||||
EXPECT_TRUE(res.converged) << "HyperIdeal: quad strip should converge";
|
||||
}
|
||||
|
||||
// ── Spherical (tetrahedron: smallest closed mesh with all vertices free) ─
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
std::vector<double> x0(static_cast<std::size_t>(n), -0.1);
|
||||
auto res = newton_spherical(mesh, x0, maps);
|
||||
EXPECT_TRUE(res.converged) << "Spherical: tetrahedron should converge";
|
||||
}
|
||||
}
|
||||
340
code/tests/cgal/test_spherical_functional.cpp
Normal file
340
code/tests/cgal/test_spherical_functional.cpp
Normal file
@@ -0,0 +1,340 @@
|
||||
// test_spherical_functional.cpp (Phase 3c + 3e)
|
||||
//
|
||||
// Phase 3c — SphericalFunctional ported to ConformalMesh.
|
||||
//
|
||||
// Corresponds to de.varylab.discreteconformal.functional.SphericalFunctionalTest.
|
||||
//
|
||||
// Test map (Java → C++)
|
||||
// ──────────────────────
|
||||
// testHessian (Ignored) → GradientCheck_Hessian (SKIPPED)
|
||||
// testGradientWithHyperIdeal… → GradientCheck_OctaFaceVertex (ported)
|
||||
// testGradientInExtendedDomain → GradientCheck_SpherTetVertex (ported)
|
||||
// testGradientWithHyperelliptic → GradientCheck_SpherTetAllDofs (ported)
|
||||
// testFunctionalAtNaNValue → AnglesFiniteAtKnownPoint (ported)
|
||||
//
|
||||
// Energy model
|
||||
// ────────────
|
||||
// The energy is computed as the Schläfli path integral E(x) = ∫₀¹⟨G(tx),x⟩dt
|
||||
// using 10-point Gauss-Legendre quadrature. The gradient check therefore
|
||||
// verifies that G is curl-free (the integrability / exactness condition of
|
||||
// the spherical discrete conformal functional). This is equivalent to the
|
||||
// Java FunctionalTest gradient check.
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "spherical_functional.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// @Ignore in Java: no Hessian implemented
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_Hessian)
|
||||
{
|
||||
GTEST_SKIP() << "@Ignore in Java – Hessian not implemented";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angle formula: octahedron-face triangle has all angles = π/2
|
||||
//
|
||||
// The triangle (1,0,0)–(0,1,0)–(0,0,1) has l_ij = π/2 for all edges.
|
||||
// Half-angle formula: s = 3π/4, s_ij = π/4 for all three.
|
||||
// All angles = π/2 (right-angled spherical triangle).
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, OctaFaceAnglesAreRightAngles)
|
||||
{
|
||||
// l_ij = π/2 for all edges (octahedron face on unit sphere)
|
||||
const double l = PI_SPHER / 2.0;
|
||||
auto fa = spherical_angles(l, l, l);
|
||||
|
||||
ASSERT_TRUE(fa.valid) << "Equilateral spherical triangle must be valid";
|
||||
EXPECT_NEAR(PI_SPHER / 2.0, fa.alpha1, 1e-12);
|
||||
EXPECT_NEAR(PI_SPHER / 2.0, fa.alpha2, 1e-12);
|
||||
EXPECT_NEAR(PI_SPHER / 2.0, fa.alpha3, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angle sum of a spherical triangle exceeds π (positive curvature)
|
||||
//
|
||||
// For the spherical tetrahedron face (arccos(−1/3) ≈ 1.9106 per edge):
|
||||
// The dihedral angle = arccos(1/3) ≈ 70.53°; by symmetry the face angles
|
||||
// (vertex angles of the spherical triangle) are all equal.
|
||||
// Angle sum must be > π and equal 3·arccos(1/3) ≈ 3·1.2310 ≈ 3.693 rad.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, SpherTetAngleSumExceedsPi)
|
||||
{
|
||||
// Edge length of spherical tetrahedron face: arccos(−1/3)
|
||||
const double l = std::acos(-1.0 / 3.0);
|
||||
auto fa = spherical_angles(l, l, l);
|
||||
|
||||
ASSERT_TRUE(fa.valid);
|
||||
EXPECT_GT(fa.alpha1 + fa.alpha2 + fa.alpha3, PI_SPHER)
|
||||
<< "Angle sum of spherical triangle must exceed π";
|
||||
|
||||
// By symmetry all three angles must be equal
|
||||
EXPECT_NEAR(fa.alpha1, fa.alpha2, 1e-12);
|
||||
EXPECT_NEAR(fa.alpha2, fa.alpha3, 1e-12);
|
||||
|
||||
// For a regular spherical tetrahedron with edge arccos(−1/3):
|
||||
// half-angle: tan(α/2) = √(sin(l/2)/sin(3l/2)) = √3 → α/2 = π/3 → α = 2π/3.
|
||||
// (arccos(1/3) ≈ 1.231 is the 3D dihedral angle of a Euclidean tetrahedron, not this.)
|
||||
double expected = 2.0 * PI_SPHER / 3.0; // 120°
|
||||
EXPECT_NEAR(fa.alpha1, expected, 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: octahedron-face triangle, vertex DOFs only
|
||||
//
|
||||
// Sets λ° from mesh geometry (unit sphere), all u_i = −0.3 (slightly smaller).
|
||||
// Mirrors Java testGradientWithHyperIdeal… on a single-triangle mesh.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_OctaFaceVertex)
|
||||
{
|
||||
auto mesh = make_octahedron_face();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// Small uniform conformal factor: shrink the triangle slightly.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.3);
|
||||
|
||||
EXPECT_TRUE(gradient_check_spherical(mesh, x, maps))
|
||||
<< "Gradient check failed on octahedron-face triangle (vertex DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: spherical tetrahedron (4 faces), vertex DOFs only
|
||||
//
|
||||
// Closed surface; exercises accumulation over multiple faces per vertex.
|
||||
// Mirrors Java testGradientInTheExtendedDomain.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_SpherTetVertex)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.2);
|
||||
|
||||
EXPECT_TRUE(gradient_check_spherical(mesh, x, maps))
|
||||
<< "Gradient check failed on spherical tetrahedron (vertex DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: spherical tetrahedron, all DOFs (vertex + edge)
|
||||
//
|
||||
// Exercises the edge-gradient branch: G_e = α_opp⁺ + α_opp⁻ − π.
|
||||
// Mirrors Java testGradientWithHyperellipticCurve.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_SpherTetAllDofs)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_all_spherical_dof_indices(mesh, maps);
|
||||
|
||||
// Small but non-zero values; vertex DOFs negative, edge DOFs zero.
|
||||
// Edge DOF adjusts the effective log-length Λ_ij = λ°_ij + u_i + u_j + λ_e.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
// Set vertex DOFs (indices 0..3) to -0.2 to keep triangle well-formed.
|
||||
for (int i = 0; i < 4; ++i) x[static_cast<std::size_t>(i)] = -0.2;
|
||||
|
||||
EXPECT_TRUE(gradient_check_spherical(mesh, x, maps))
|
||||
<< "Gradient check failed on spherical tetrahedron (all DOFs)";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Angles are finite at a known interior point
|
||||
//
|
||||
// Mirrors Java testFunctionalAtNaNValue: choose DOFs that could hit
|
||||
// a degenerate branch (l_ij → 0 or triangle inequality fails) and check
|
||||
// the gradient vector is free of NaN/Inf.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, AnglesFiniteAtKnownPoint)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// u_i = -1.5: contracts the triangle heavily but stays non-degenerate.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -1.5);
|
||||
auto G = spherical_gradient(mesh, x, maps);
|
||||
|
||||
for (std::size_t i = 0; i < G.size(); ++i) {
|
||||
EXPECT_FALSE(std::isnan(G[i])) << "Gradient component " << i << " is NaN";
|
||||
EXPECT_FALSE(std::isinf(G[i])) << "Gradient component " << i << " is Inf";
|
||||
}
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: fan-4 mesh on unit sphere, vertex DOFs only
|
||||
//
|
||||
// Make a fan of 4 triangles around the north pole (0,0,1);
|
||||
// rim vertices projected onto the equator.
|
||||
// Exercises high-valence vertex gradient accumulation.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_SpherFan4Vertex)
|
||||
{
|
||||
// Build a fan with 4 spherical triangles manually (can't use make_fan
|
||||
// directly because those vertices are not on the unit sphere).
|
||||
ConformalMesh mesh;
|
||||
auto center = mesh.add_vertex(Point3(0, 0, 1)); // north pole
|
||||
const int n_rim = 4;
|
||||
std::vector<Vertex_index> rim(n_rim);
|
||||
const double dtheta = 2.0 * PI_SPHER / n_rim;
|
||||
const double phi = PI_SPHER / 4.0; // 45° colatitude
|
||||
for (int i = 0; i < n_rim; ++i) {
|
||||
double theta = i * dtheta;
|
||||
rim[i] = mesh.add_vertex(Point3(
|
||||
std::sin(phi) * std::cos(theta),
|
||||
std::sin(phi) * std::sin(theta),
|
||||
std::cos(phi)));
|
||||
}
|
||||
for (int i = 0; i < n_rim; ++i)
|
||||
mesh.add_face(center, rim[i], rim[(i + 1) % n_rim]);
|
||||
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int ndof = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(ndof), -0.3);
|
||||
|
||||
EXPECT_TRUE(gradient_check_spherical(mesh, x, maps))
|
||||
<< "Gradient check failed on spherical fan-4 mesh";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Gradient check: mixed pinned/variable vertices
|
||||
//
|
||||
// One vertex pinned (u_v = 0 fixed), others variable.
|
||||
// Verifies that the gradient accumulation skips pinned vertices correctly.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GradientCheck_MixedPinnedVertices)
|
||||
{
|
||||
auto mesh = make_octahedron_face();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin v0, make v1 and v2 variable.
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // pinned
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
|
||||
std::vector<double> x = {-0.2, -0.4};
|
||||
|
||||
EXPECT_TRUE(gradient_check_spherical(mesh, x, maps))
|
||||
<< "Gradient check failed for mixed pinned/variable vertices";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Phase 3e — Gauge-fix for closed spherical surfaces
|
||||
//
|
||||
// On a closed spherical surface, the functional has a gauge mode:
|
||||
// E(u + t·1) is maximised at some t*.
|
||||
// At t*, the sum of all vertex gradients equals zero: Σ G_v = 0.
|
||||
//
|
||||
// Test: start from a point with non-zero ΣG_v, apply the gauge shift,
|
||||
// and verify ΣG_v(x + t*·1) ≈ 0.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalFunctional, GaugeFix_SpherTetVertexZerosSumGv)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// Off-gauge starting point: all u_i = -0.5
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.5);
|
||||
|
||||
// Compute ΣG_v before gauge shift.
|
||||
{
|
||||
auto G = spherical_gradient(mesh, x, maps);
|
||||
double sum = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) sum += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
// At -0.5 the surface is compressed; ΣG_v should be non-zero.
|
||||
EXPECT_NE(sum, 0.0) << "Pre-gauge ΣG_v should be non-zero";
|
||||
}
|
||||
|
||||
// Compute gauge shift and apply.
|
||||
double t = spherical_gauge_shift(mesh, x, maps);
|
||||
std::vector<double> x_fixed = x;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0)
|
||||
x_fixed[static_cast<std::size_t>(iv)] += t;
|
||||
}
|
||||
|
||||
// Verify ΣG_v ≈ 0 at the gauge-fixed point.
|
||||
{
|
||||
auto G = spherical_gradient(mesh, x_fixed, maps);
|
||||
double sum = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) sum += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
EXPECT_NEAR(sum, 0.0, 1e-6)
|
||||
<< "After gauge fix, Σ G_v should vanish; t* = " << t;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SphericalFunctional, GaugeFix_ApplyInPlace)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// x = -0.3: compressed but inside the valid spherical domain.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.3);
|
||||
|
||||
apply_spherical_gauge(mesh, x, maps);
|
||||
|
||||
// After in-place gauge fix, ΣG_v must be near 0.
|
||||
auto G = spherical_gradient(mesh, x, maps);
|
||||
double sum = 0.0;
|
||||
for (auto v : mesh.vertices()) {
|
||||
int iv = maps.v_idx[v];
|
||||
if (iv >= 0) sum += G[static_cast<std::size_t>(iv)];
|
||||
}
|
||||
EXPECT_NEAR(sum, 0.0, 1e-6)
|
||||
<< "apply_spherical_gauge must drive Σ G_v to zero";
|
||||
}
|
||||
|
||||
TEST(SphericalFunctional, GaugeFix_AlreadyAtGaugeReturnsTNearZero)
|
||||
{
|
||||
// A symmetric, equilateral spherical tetrahedron at x=0 is already
|
||||
// at the gauge maximum (by symmetry, ΣG_v = 0).
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
double t = spherical_gauge_shift(mesh, x, maps);
|
||||
// Symmetric starting point → t* should be very close to 0.
|
||||
EXPECT_NEAR(t, 0.0, 1e-5)
|
||||
<< "Gauge shift from the symmetric point should be ~0; got " << t;
|
||||
}
|
||||
205
code/tests/cgal/test_spherical_hessian.cpp
Normal file
205
code/tests/cgal/test_spherical_hessian.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
// test_spherical_hessian.cpp
|
||||
//
|
||||
// Phase 3f — Spherical cotangent-Laplace Hessian.
|
||||
//
|
||||
// The Hessian of the spherical discrete conformal energy is the "spherical
|
||||
// cotangent Laplacian" with edge weight
|
||||
// w_k = cot(β_k), β_k = (π − αi − αj + αk) / 2
|
||||
// for the edge (vi, vj) with opposite vertex vk and angles αi, αj, αk.
|
||||
//
|
||||
// In the flat limit (αi+αj+αk → π) this reduces to the Euclidean cotangent
|
||||
// Laplacian (β_k → αk, w_k → cot(αk)).
|
||||
//
|
||||
// Tests:
|
||||
// 1. Spherical cot weights match Euclidean weights in the near-flat limit.
|
||||
// 2. Hessian is symmetric.
|
||||
// 3. Hessian has H·1 ≈ 0 on the spherical tetrahedron (null-space property).
|
||||
// 4. Finite-difference check (the primary correctness criterion).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_builder.hpp"
|
||||
#include "spherical_hessian.hpp"
|
||||
#include "euclidean_hessian.hpp" // for Euclidean comparison
|
||||
#include <gtest/gtest.h>
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical cot weights reduce to Euclidean cot weights in the flat limit
|
||||
//
|
||||
// For a nearly-flat equilateral spherical triangle (l → 0, α → 60°):
|
||||
// β_k = (π − 60° − 60° + 60°)/2 = 60° → cot(60°) = 1/√3 ✓
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, CotWeights_NearFlatEquilateral)
|
||||
{
|
||||
// Use a very small equilateral spherical triangle: α1=α2=α3=60°
|
||||
const double alpha = PI / 3.0;
|
||||
auto sw = spherical_cot_weights(alpha, alpha, alpha);
|
||||
|
||||
ASSERT_TRUE(sw.valid);
|
||||
|
||||
const double expected = 1.0 / std::sqrt(3.0); // = cot(60°)
|
||||
EXPECT_NEAR(sw.w12, expected, 1e-12);
|
||||
EXPECT_NEAR(sw.w23, expected, 1e-12);
|
||||
EXPECT_NEAR(sw.w31, expected, 1e-12);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Spherical cot weights are positive for the regular spherical tetrahedron
|
||||
//
|
||||
// Each face has α_k = 2π/3 (120°), angle sum = 2π.
|
||||
// β_k = (π − 2π/3 − 2π/3 + 2π/3)/2 = (π − 2π/3)/2 = π/6
|
||||
// w_k = cot(π/6) = √3
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, CotWeights_RegularSphericalTetrahedronFace)
|
||||
{
|
||||
const double alpha = 2.0 * PI / 3.0; // 120°
|
||||
auto sw = spherical_cot_weights(alpha, alpha, alpha);
|
||||
|
||||
ASSERT_TRUE(sw.valid);
|
||||
|
||||
const double expected = std::sqrt(3.0); // cot(π/6)
|
||||
EXPECT_NEAR(sw.w12, expected, 1e-10);
|
||||
EXPECT_NEAR(sw.w23, expected, 1e-10);
|
||||
EXPECT_NEAR(sw.w31, expected, 1e-10);
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Hessian is symmetric: H[i,j] == H[j,i]
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, HessianIsSymmetric)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.2);
|
||||
auto H = spherical_hessian(mesh, x, maps);
|
||||
|
||||
Eigen::MatrixXd Hd = Eigen::MatrixXd(H);
|
||||
EXPECT_NEAR((Hd - Hd.transpose()).norm(), 0.0, 1e-12)
|
||||
<< "Spherical Hessian must be symmetric";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// H·1 is NOT zero for the spherical Hessian (unlike the Euclidean case).
|
||||
//
|
||||
// In the Euclidean case, a uniform shift u_i → u_i + c scales all edge
|
||||
// lengths by e^c, leaving angles unchanged → H·1 = 0 exactly.
|
||||
//
|
||||
// In the spherical case, l_ij = 2·asin(exp(λ_ij/2)) is NOT a linear
|
||||
// function of the DOFs, so a uniform shift DOES change the angles
|
||||
// → H·1 ≠ 0 in general. This test verifies that property.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, ConstantVectorNotInNullSpace)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.2);
|
||||
auto H = spherical_hessian(mesh, x, maps);
|
||||
|
||||
Eigen::VectorXd ones = Eigen::VectorXd::Ones(n);
|
||||
Eigen::VectorXd Hones = H * ones;
|
||||
|
||||
// H·1 should be non-trivial (norm well above zero)
|
||||
EXPECT_GT(Hones.norm(), 1e-3)
|
||||
<< "Spherical H·1 should be non-zero; norm = " << Hones.norm();
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Hessian is negative semi-definite at the equilibrium point (x = 0)
|
||||
//
|
||||
// The spherical discrete conformal energy is concave in the vertex DOFs
|
||||
// (unlike the Euclidean case which is convex). At the equilibrium x = 0,
|
||||
// the Hessian is NSD: all eigenvalues ≤ 0, with a multi-dimensional null
|
||||
// space corresponding to degenerate directions.
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, HessianIsNegativeSemiDefiniteAtEquilibrium)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
// x = 0 is the equilibrium for the regular spherical tetrahedron.
|
||||
std::vector<double> x(static_cast<std::size_t>(n), 0.0);
|
||||
auto H = spherical_hessian(mesh, x, maps);
|
||||
|
||||
Eigen::MatrixXd Hd = Eigen::MatrixXd(H);
|
||||
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(Hd);
|
||||
double max_ev = es.eigenvalues().maxCoeff();
|
||||
|
||||
EXPECT_LE(max_ev, 1e-9)
|
||||
<< "Largest eigenvalue of spherical Hessian at equilibrium must be ≤ 0; got " << max_ev;
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: octahedron-face triangle (vertex DOFs)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, FDCheck_OctaFaceVertex)
|
||||
{
|
||||
auto mesh = make_octahedron_face();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.3);
|
||||
|
||||
EXPECT_TRUE(hessian_check_spherical(mesh, x, maps))
|
||||
<< "FD Hessian check failed on octahedron-face triangle";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: spherical tetrahedron (vertex DOFs)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, FDCheck_SpherTetVertex)
|
||||
{
|
||||
auto mesh = make_spherical_tetrahedron();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
int n = assign_vertex_dof_indices(mesh, maps);
|
||||
|
||||
std::vector<double> x(static_cast<std::size_t>(n), -0.2);
|
||||
|
||||
EXPECT_TRUE(hessian_check_spherical(mesh, x, maps))
|
||||
<< "FD Hessian check failed on spherical tetrahedron";
|
||||
}
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// Finite-difference Hessian check: mixed pinned/variable vertices
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
TEST(SphericalHessian, FDCheck_MixedPinnedVertices)
|
||||
{
|
||||
auto mesh = make_octahedron_face();
|
||||
auto maps = setup_spherical_maps(mesh);
|
||||
compute_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
auto vit = mesh.vertices().begin();
|
||||
Vertex_index v0 = *vit++;
|
||||
Vertex_index v1 = *vit++;
|
||||
Vertex_index v2 = *vit;
|
||||
|
||||
maps.v_idx[v0] = -1; // pinned
|
||||
maps.v_idx[v1] = 0;
|
||||
maps.v_idx[v2] = 1;
|
||||
|
||||
std::vector<double> x = {-0.2, -0.3};
|
||||
|
||||
EXPECT_TRUE(hessian_check_spherical(mesh, x, maps))
|
||||
<< "FD Hessian check failed for mixed pinned/variable vertices";
|
||||
}
|
||||
39
code/tests/test_discrete_elliptic_utility.cpp
Normal file
39
code/tests/test_discrete_elliptic_utility.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Port of de.varylab.discreteconformal.util.DiscreteEllipticUtilityTest (Java/JUnit).
|
||||
// Tests the normalizeModulus function that moves a complex number tau into the
|
||||
// fundamental domain of the modular group SL(2,Z).
|
||||
|
||||
#include "discrete_elliptic_utility.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <complex>
|
||||
#include <cmath>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// Corresponds to Java testNormalizeModulus()
|
||||
TEST(DiscreteEllipticUtilityTest, NormalizeModulus) {
|
||||
// tau already in fundamental domain → should be returned unchanged
|
||||
std::complex<double> tau(0.45, 1.1);
|
||||
auto tauNorm = normalizeModulus(tau);
|
||||
EXPECT_NEAR(0.45, tauNorm.real(), 1E-12);
|
||||
EXPECT_NEAR(1.1, tauNorm.imag(), 1E-12);
|
||||
|
||||
// tau = i/3 (|tau| < 1) → inversion gives 3i
|
||||
tau = std::complex<double>(0.0, 1.0 / 3.0);
|
||||
tauNorm = normalizeModulus(tau);
|
||||
EXPECT_NEAR(3.0, tauNorm.imag(), 1E-12);
|
||||
EXPECT_NEAR(0.0, tauNorm.real(), 1E-12);
|
||||
}
|
||||
|
||||
// Corresponds to Java testNormalizeModulusPeriodShift()
|
||||
// Two tau values that differ by a T-shift (integer shift of Re) must normalize
|
||||
// to the same point in the fundamental domain.
|
||||
TEST(DiscreteEllipticUtilityTest, NormalizeModulusPeriodShift) {
|
||||
std::complex<double> tau1(0.3, 1.0);
|
||||
std::complex<double> tau2(-0.7, 1.0); // tau2 = tau1 - 1
|
||||
|
||||
auto n1 = normalizeModulus(tau1);
|
||||
auto n2 = normalizeModulus(tau2);
|
||||
|
||||
EXPECT_NEAR(n1.real(), n2.real(), 1E-12) << "real parts should be equal";
|
||||
EXPECT_NEAR(n1.imag(), n2.imag(), 1E-12) << "imag parts should be equal";
|
||||
}
|
||||
37
code/tests/test_hyper_ideal_functional.cpp
Normal file
37
code/tests/test_hyper_ideal_functional.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// Stub for de.varylab.discreteconformal.functional.HyperIdealFunctionalTest (Java/JUnit).
|
||||
//
|
||||
// STATUS: BLOCKED – requires HDS port (Phase 4).
|
||||
//
|
||||
// These tests evaluate gradient and Hessian of the HyperIdealFunctional on
|
||||
// actual mesh data (CoHDS + HyperIdealGenerator). They cannot be ported
|
||||
// until the HalfEdge data structure (CoHDS), the functional evaluation
|
||||
// framework, and the mesh generators are available in C++.
|
||||
//
|
||||
// Java tests and their status:
|
||||
// testHessian() – @Ignore in Java (skipped here too)
|
||||
// testGradientWithHyperIdealAndIdealPoints – blocked: needs HDS
|
||||
// testGradientInTheExtendedDomain – blocked: needs HDS
|
||||
// testGradientWithHyperellipticCurve – blocked: needs HDS
|
||||
// testFunctionalAtNaNValue – blocked: needs HDS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(HyperIdealFunctionalTest, TestHessian_IgnoredInJava) {
|
||||
GTEST_SKIP() << "@Ignore in Java – skipped here too";
|
||||
}
|
||||
|
||||
TEST(HyperIdealFunctionalTest, GradientWithHyperIdealAndIdealPoints) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HyperIdealFunctional)";
|
||||
}
|
||||
|
||||
TEST(HyperIdealFunctionalTest, GradientInTheExtendedDomain) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HyperIdealFunctional)";
|
||||
}
|
||||
|
||||
TEST(HyperIdealFunctionalTest, GradientWithHyperellipticCurve) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HyperIdealFunctional)";
|
||||
}
|
||||
|
||||
TEST(HyperIdealFunctionalTest, FunctionalAtNaNValue) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HyperIdealFunctional)";
|
||||
}
|
||||
26
code/tests/test_hyper_ideal_hyperelliptic_utility.cpp
Normal file
26
code/tests/test_hyper_ideal_hyperelliptic_utility.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// Stub for de.varylab.discreteconformal.functional.HyperIdealHyperellipticUtilityTest.
|
||||
//
|
||||
// STATUS: BLOCKED – requires HDS port (Phase 4).
|
||||
//
|
||||
// Tests compute intersection angles of circles associated with hyper-ideal
|
||||
// vertices using CoHDS + HalfEdgeUtils. All three tests operate on mesh
|
||||
// data structures that are not yet available in C++.
|
||||
//
|
||||
// Java tests and their status:
|
||||
// testCalculateCircleIntersections – blocked: needs CoHDS + HalfEdgeUtils
|
||||
// testCalculateCircleIntersectionsInfinite – blocked: needs CoHDS + HalfEdgeUtils
|
||||
// testLawsonHyperellipticAngles – blocked: needs CoHDS + HyperIdealGenerator
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(HyperIdealHyperellipticUtilityTest, CalculateCircleIntersections) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HalfEdgeUtils)";
|
||||
}
|
||||
|
||||
TEST(HyperIdealHyperellipticUtilityTest, CalculateCircleIntersectionsInfinite) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HalfEdgeUtils)";
|
||||
}
|
||||
|
||||
TEST(HyperIdealHyperellipticUtilityTest, LawsonHyperellipticAngles) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + HyperIdealGenerator)";
|
||||
}
|
||||
50
code/tests/test_hyper_ideal_visualization_utility.cpp
Normal file
50
code/tests/test_hyper_ideal_visualization_utility.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
// Port of de.varylab.discreteconformal.plugin.HyperIdealVisualizationPluginTest (Java/JUnit).
|
||||
//
|
||||
// Tests the conversion from a hyperbolic circle (hyperboloid model)
|
||||
// to its Euclidean representation (Poincaré disk model).
|
||||
//
|
||||
// Java test: static method HyperIdealVisualizationPlugin
|
||||
// .getEuclideanCircleFromHyperbolic(double[] center, double radius)
|
||||
// C++ port: conformallab::getEuclideanCircleFromHyperbolic(Vector4d, double)
|
||||
// in hyper_ideal_visualization_utility.hpp
|
||||
|
||||
#include "hyper_ideal_visualization_utility.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cmath>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// Corresponds to Java testGetEuclideanCircleFromHyperbolic_Centered()
|
||||
//
|
||||
// A hyperbolic circle centered at the origin (0,0,0,1) with radius 1.
|
||||
// In the Poincaré disk this maps to a Euclidean circle centered at (0,0)
|
||||
// with radius sinh(1) / (cosh(1) + 1).
|
||||
TEST(HyperIdealVisualizationUtilityTest, EuclideanCircleFromHyperbolic_Centered)
|
||||
{
|
||||
Eigen::Vector4d center(0.0, 0.0, 0.0, 1.0);
|
||||
const double radius = 1.0;
|
||||
|
||||
auto result = getEuclideanCircleFromHyperbolic(center, radius);
|
||||
|
||||
const double expected_r = std::sinh(1.0) / (std::cosh(1.0) + 1.0);
|
||||
EXPECT_NEAR(0.0, result[0], 1E-12) << "Euclidean cx should be 0";
|
||||
EXPECT_NEAR(0.0, result[1], 1E-12) << "Euclidean cy should be 0";
|
||||
EXPECT_NEAR(expected_r, result[2], 1E-12) << "Euclidean radius mismatch";
|
||||
}
|
||||
|
||||
// Corresponds to Java testGetEuclideanCircleFromHyperbolic_OffCenter()
|
||||
//
|
||||
// A hyperbolic circle centered at (sinh(1),0,0,cosh(1)) with radius 1.
|
||||
// By symmetry (center is on the x-axis, circle is symmetric about it):
|
||||
// • the Euclidean center lies on the x-axis → cy = 0
|
||||
// • the Euclidean center equals the radius → cx = r (the circle passes through the Poincaré origin)
|
||||
TEST(HyperIdealVisualizationUtilityTest, EuclideanCircleFromHyperbolic_OffCenter)
|
||||
{
|
||||
Eigen::Vector4d center(std::sinh(1.0), 0.0, 0.0, std::cosh(1.0));
|
||||
const double radius = 1.0;
|
||||
|
||||
auto result = getEuclideanCircleFromHyperbolic(center, radius);
|
||||
|
||||
EXPECT_NEAR(result[0], result[2], 1E-12) << "cx should equal Euclidean radius";
|
||||
EXPECT_NEAR(0.0, result[1], 1E-12) << "cy should be 0 (x-axis symmetry)";
|
||||
}
|
||||
87
code/tests/test_p2_utility.cpp
Normal file
87
code/tests/test_p2_utility.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
// Port of de.varylab.discreteconformal.math.P2BigTest (Java/JUnit).
|
||||
// Tests 2-D projective geometry utilities: perpendicular bisectors,
|
||||
// point-from-lines, and direct isometries in the Euclidean plane.
|
||||
//
|
||||
// The Java test compared double precision (P2) against BigDecimal precision
|
||||
// (P2Big) to 1E-10. Here we compare double against long double to the
|
||||
// same tolerance.
|
||||
|
||||
#include "p2_utility.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
// Corresponds to Java P2BigTest.testMakeDirectIsometryFromFramesEuclidean()
|
||||
//
|
||||
// Computes the Euclidean isometry mapping frame (s1,s2) to frame (t1,t2)
|
||||
// with both double and long-double precision, and checks:
|
||||
// 1. The two precisions agree to 1E-10 (precision stability).
|
||||
// 2. The matrix actually maps s1→t1 and s2→t2.
|
||||
TEST(P2UtilityTest, MakeDirectIsometryFromFramesEuclidean) {
|
||||
using V3d = Eigen::Vector3d;
|
||||
using V3ld = Eigen::Matrix<long double, 3, 1>;
|
||||
|
||||
V3d s1(-1.4142135623730963, 0.0, 1.0);
|
||||
V3d s2( 1.4142135623730951, 0.0, 1.0);
|
||||
V3d t1(-2.828427124746189, 2.4494897427831805, 1.0);
|
||||
V3d t2( 0.0, 2.4494897427831783, 1.0);
|
||||
|
||||
// double precision
|
||||
auto T = makeDirectIsometryFromFramesEuclidean<double>(s1, s2, t1, t2);
|
||||
|
||||
// long double precision (analogous to Java's BigDecimal P2Big)
|
||||
V3ld s1l = s1.cast<long double>();
|
||||
V3ld s2l = s2.cast<long double>();
|
||||
V3ld t1l = t1.cast<long double>();
|
||||
V3ld t2l = t2.cast<long double>();
|
||||
auto Tl = makeDirectIsometryFromFramesEuclidean<long double>(s1l, s2l, t1l, t2l);
|
||||
|
||||
// 1. double vs long double must agree to 1E-10
|
||||
for (int i = 0; i < 3; ++i)
|
||||
for (int j = 0; j < 3; ++j)
|
||||
EXPECT_NEAR((double)Tl(i,j), T(i,j), 1E-10)
|
||||
<< "element (" << i << "," << j << ") differs between precisions";
|
||||
|
||||
// 2. T must map s1 → t1 and s2 → t2 (verify isometry correctness)
|
||||
auto map_s1 = T * s1;
|
||||
auto map_s2 = T * s2;
|
||||
EXPECT_NEAR(euclideanDistanceP2(map_s1, t1), 0.0, 1E-9) << "T*s1 should equal t1";
|
||||
EXPECT_NEAR(euclideanDistanceP2(map_s2, t2), 0.0, 1E-9) << "T*s2 should equal t2";
|
||||
}
|
||||
|
||||
// Corresponds to Java P2BigTest.testPerpendicularBisector()
|
||||
TEST(P2UtilityTest, PerpendicularBisector) {
|
||||
Eigen::Vector3d p1(0.5, 0.0, 1.0);
|
||||
Eigen::Vector3d q1(0.0, 0.5, 1.0);
|
||||
|
||||
auto bisector = perpendicularBisectorEuclidean(p1, q1);
|
||||
|
||||
EXPECT_NEAR( 0.5, bisector(0), 1E-10);
|
||||
EXPECT_NEAR(-0.5, bisector(1), 1E-10);
|
||||
EXPECT_NEAR( 0.0, bisector(2), 1E-10);
|
||||
}
|
||||
|
||||
// Corresponds to Java P2BigTest.testPerpendicularBisectorIntersection()
|
||||
//
|
||||
// The intersection of the perpendicular bisectors of two edges must be
|
||||
// equidistant from the endpoints of each edge (circumcenter property).
|
||||
TEST(P2UtilityTest, PerpendicularBisectorIntersection) {
|
||||
Eigen::Vector3d p1(0.5, 0.0, 1.0);
|
||||
Eigen::Vector3d q1(0.0, 1.0, 1.0);
|
||||
Eigen::Vector3d p2(1.0, 0.0, 1.0);
|
||||
Eigen::Vector3d q2(0.0, 1.5, 1.0);
|
||||
|
||||
auto l1 = perpendicularBisectorEuclidean(p1, q1);
|
||||
auto l2 = perpendicularBisectorEuclidean(p2, q2);
|
||||
auto o = pointFromLines(l1, l2); // circumcenter
|
||||
|
||||
// o must be equidistant from p1 and q1
|
||||
EXPECT_NEAR(euclideanDistanceP2(p1, o),
|
||||
euclideanDistanceP2(q1, o), 1E-10);
|
||||
|
||||
// o must be equidistant from p2 and q2
|
||||
EXPECT_NEAR(euclideanDistanceP2(p2, o),
|
||||
euclideanDistanceP2(q2, o), 1E-10);
|
||||
}
|
||||
37
code/tests/test_spherical_functional.cpp
Normal file
37
code/tests/test_spherical_functional.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// Stub for de.varylab.discreteconformal.functional.SphericalFunctionalTest (Java/JUnit).
|
||||
//
|
||||
// STATUS: BLOCKED – requires HDS port (Phase 4).
|
||||
//
|
||||
// Tests evaluate gradient and Hessian of the SphericalFunctional on meshes
|
||||
// built via CoHDS + ConvexHull, and check that a regular spherical metric
|
||||
// is a critical point of the functional. All tests require the HalfEdge
|
||||
// data structure and the functional evaluation framework in C++.
|
||||
//
|
||||
// Java tests and their status:
|
||||
// testReducedGradient – blocked: needs CoHDS + SphericalFunctional
|
||||
// testReducedHessian – blocked: needs CoHDS + SphericalFunctional
|
||||
// testGradient – blocked: needs CoHDS + SphericalFunctional
|
||||
// testHessian – blocked: needs CoHDS + SphericalFunctional
|
||||
// testCriticalPoint – blocked: needs CoHDS + ConvexHull + SphericalFunctional
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(SphericalFunctionalTest, ReducedGradient) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + SphericalFunctional)";
|
||||
}
|
||||
|
||||
TEST(SphericalFunctionalTest, ReducedHessian) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + SphericalFunctional)";
|
||||
}
|
||||
|
||||
TEST(SphericalFunctionalTest, Gradient) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + SphericalFunctional)";
|
||||
}
|
||||
|
||||
TEST(SphericalFunctionalTest, Hessian) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + SphericalFunctional)";
|
||||
}
|
||||
|
||||
TEST(SphericalFunctionalTest, CriticalPoint) {
|
||||
GTEST_SKIP() << "Blocked: requires HDS port (CoHDS + ConvexHull + SphericalFunctional)";
|
||||
}
|
||||
166
doc/api/cgal-package.md
Normal file
166
doc/api/cgal-package.md
Normal file
@@ -0,0 +1,166 @@
|
||||
# Phase 8 — CGAL Package Design
|
||||
|
||||
> **Status: planned.** This document describes the target architecture for Phase 8.
|
||||
> No code has been written yet. The design is informed by the CGAL package submission
|
||||
> guidelines at https://www.cgal.org/developers.html
|
||||
|
||||
---
|
||||
|
||||
## Goal
|
||||
|
||||
Integrate conformallab++ into the CGAL library as a proper CGAL package:
|
||||
`Discrete_conformal_map`. The package must satisfy all CGAL submission requirements:
|
||||
traits-class design, Doxygen documentation, CGAL-format test suite, and coverage of
|
||||
the CGAL coding conventions.
|
||||
|
||||
---
|
||||
|
||||
## 8a — Traits class & concepts
|
||||
|
||||
The current code is tightly coupled to `CGAL::Surface_mesh<Point3>`. Phase 8a introduces
|
||||
a traits class that separates the mesh type from the algorithm:
|
||||
|
||||
```cpp
|
||||
// TODO(Phase 8a): implement this header
|
||||
// include/CGAL/Conformal_map_traits.h
|
||||
|
||||
template<
|
||||
typename MeshType, // any CGAL halfedge mesh
|
||||
typename KernelType, // CGAL kernel
|
||||
typename ScalarType = double
|
||||
>
|
||||
struct Conformal_map_traits {
|
||||
using Mesh = MeshType;
|
||||
using Kernel = KernelType;
|
||||
using FT = ScalarType;
|
||||
// ... vertex/edge/face descriptor types
|
||||
// ... property map access
|
||||
};
|
||||
```
|
||||
|
||||
Concept checks will ensure any user-provided mesh satisfies the halfedge mesh concept.
|
||||
|
||||
---
|
||||
|
||||
## 8b — Public header hierarchy
|
||||
|
||||
A clean public API separate from the internal implementation:
|
||||
|
||||
```
|
||||
include/CGAL/
|
||||
Discrete_conformal_map.h ← single user-facing include
|
||||
Conformal_map_traits.h
|
||||
Conformal_newton_solver.h
|
||||
Conformal_layout.h
|
||||
Conformal_cut_graph.h
|
||||
conformal_map_package.h ← PackageDescription
|
||||
```
|
||||
|
||||
All existing `include/*.hpp` headers remain as internal implementation details,
|
||||
not part of the public CGAL API.
|
||||
|
||||
---
|
||||
|
||||
## 8c — CGAL-style documentation
|
||||
|
||||
```
|
||||
doc/Conformal_map/
|
||||
PackageDescription.txt
|
||||
User_manual.md
|
||||
Reference_manual.md
|
||||
fig/ ← pipeline diagrams, mathematical figures
|
||||
```
|
||||
|
||||
All public functions and concepts require Doxygen comments following the CGAL style.
|
||||
|
||||
---
|
||||
|
||||
## 8d — CGAL test format
|
||||
|
||||
```
|
||||
test/Conformal_map/
|
||||
CMakeLists.txt ← CGAL-format, uses find_package(CGAL)
|
||||
test_euclidean_functional.cpp
|
||||
test_newton_solver.cpp
|
||||
...
|
||||
```
|
||||
|
||||
The existing GTest suite remains. CGAL-format tests are added alongside as a separate
|
||||
target, following the CGAL test infrastructure conventions.
|
||||
|
||||
---
|
||||
|
||||
## 8e — Declarative YAML pipeline
|
||||
|
||||
A lightweight YAML format for reproducible experiments. The CLI accepts
|
||||
`--pipeline experiment.yml`; the validator checks `require`/`provide` tokens
|
||||
before execution.
|
||||
|
||||
**Full concept & design specification:** [doc/concepts/declarative-pipeline.md](../concepts/declarative-pipeline.md)
|
||||
— token vocabulary, validation algorithm, 5 complete examples, implementation plan.
|
||||
|
||||
Abbreviated example:
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: flat_torus_period
|
||||
geometry: euclidean
|
||||
|
||||
input:
|
||||
source: data/torus.off
|
||||
|
||||
steps:
|
||||
- id: setup
|
||||
unit: setup_euclidean_maps
|
||||
provide: [maps_initialised]
|
||||
|
||||
- id: gauss_bonnet
|
||||
unit: enforce_gauss_bonnet
|
||||
require: [maps_initialised]
|
||||
provide: [gauss_bonnet_satisfied]
|
||||
|
||||
- id: solve
|
||||
unit: newton_euclidean
|
||||
require: [gauss_bonnet_satisfied]
|
||||
params:
|
||||
tol: 1.0e-10
|
||||
max_iter: 200
|
||||
provide: [x_converged]
|
||||
|
||||
- id: cut
|
||||
unit: compute_cut_graph
|
||||
require: [mesh_closed]
|
||||
provide: [cut_graph]
|
||||
|
||||
- id: layout
|
||||
unit: euclidean_layout
|
||||
require: [x_converged, cut_graph]
|
||||
params:
|
||||
normalise: true
|
||||
provide: [layout_uv, holonomy]
|
||||
|
||||
- id: period
|
||||
unit: compute_period_matrix
|
||||
require: [holonomy]
|
||||
provide: [tau]
|
||||
|
||||
output:
|
||||
layout: out/torus_layout.off
|
||||
json: out/torus_result.json
|
||||
tau: out/torus_tau.txt
|
||||
```
|
||||
|
||||
The contract table in [contracts.md](contracts.md) defines the valid `require`/`provide`
|
||||
token vocabulary.
|
||||
|
||||
---
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] Design `Conformal_map_traits.h` interface (8a)
|
||||
- [ ] Define concept requirements for `MeshType` (8a)
|
||||
- [ ] Create `include/CGAL/` header skeleton (8b)
|
||||
- [ ] Write `PackageDescription.txt` (8c)
|
||||
- [ ] Port GTest tests to CGAL format (8d)
|
||||
- [ ] Implement YAML validator (8e)
|
||||
- [ ] CLI: `--pipeline` flag (8e)
|
||||
69
doc/api/contracts.md
Normal file
69
doc/api/contracts.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# Processing Unit Contracts
|
||||
|
||||
Each pipeline unit has explicit preconditions and guarantees.
|
||||
A pipeline is valid if every unit's preconditions are satisfied by the outputs
|
||||
of all preceding units.
|
||||
|
||||
---
|
||||
|
||||
## Contract table
|
||||
|
||||
| Unit | Preconditions | Provides |
|
||||
|---|---|---|
|
||||
| `load_mesh()` | Valid file path, supported format (OFF/OBJ/PLY) | Manifold, oriented, triangulated `ConformalMesh` |
|
||||
| `setup_*_maps()` | Triangulated mesh | Initialised property maps; `lambda0` zeroed |
|
||||
| `compute_*_lambda0_from_mesh()` | `setup_*_maps()` called | `lambda0[e]` set from 3-D edge lengths |
|
||||
| `check_gauss_bonnet()` | `theta_v[v]` set for all vertices | Throws `std::runtime_error` if Σ(2π−Θᵥ) ≠ 2π·χ(M) |
|
||||
| `enforce_gauss_bonnet()` | `theta_v[v]` set | Σ(2π−Θᵥ) = 2π·χ(M) guaranteed; `theta_v` modified |
|
||||
| `newton_euclidean()` | GB satisfied · DOFs assigned · `lambda0` initialised | `NewtonResult.x` — converged scale factors; `.converged`, `.iterations`, `.grad_inf_norm` |
|
||||
| `newton_spherical()` | GB satisfied · DOFs assigned · gauge vertex pinned | Same as above |
|
||||
| `newton_hyper_ideal()` | `assign_all_dof_indices()` called · `lambda0` initialised | Same as above |
|
||||
| `compute_cut_graph()` | Closed, orientable, triangulated mesh | `CutGraph.cut_edge_flags` — 2g seam edges; `.genus` |
|
||||
| `euclidean_layout()` | `newton_euclidean()` converged | `Layout2D.uv[v]` · `.halfedge_uv[h]` · `HolonomyData.translations` |
|
||||
| `spherical_layout()` | `newton_spherical()` converged | `Layout3D.xyz[v]` |
|
||||
| `hyper_ideal_layout()` | `newton_hyper_ideal()` converged | `Layout2D.uv[v]` · `.halfedge_uv[h]` · `HolonomyData.mobius_maps` |
|
||||
| `normalise_euclidean()` | `layout.success == true` | Centroid at origin, major axis = x-axis |
|
||||
| `normalise_hyperbolic()` | `layout.success == true` | Weighted centroid at Poincaré disk origin |
|
||||
| `normalise_spherical()` | `layout.success == true` | Area centroid at north pole |
|
||||
| `compute_period_matrix()` | `hol.translations.size() >= 2` | `PeriodData.tau ∈ ℍ`; optionally SL(2,ℤ)-reduced |
|
||||
| `compute_fundamental_domain()` | `HolonomyData` (genus 1) | CCW parallelogram `{0, ω₁, ω₁+ω₂, ω₂}` + edge identifications |
|
||||
| `tiling_neighbourhood()` | `Layout2D` + `HolonomyData` (genus 1) | `(2m+1)·(2n+1)` translated layout copies |
|
||||
| `save_layout_off()` | `Layout2D` + mesh | OFF file with UV coordinates |
|
||||
| `save_result_json/xml()` | `NewtonResult` + optional `Layout2D` | JSON/XML serialised result |
|
||||
|
||||
---
|
||||
|
||||
## DOF assignment conventions
|
||||
|
||||
```cpp
|
||||
// Euclidean / Spherical: pin one vertex, assign sequential indices
|
||||
maps.v_idx[first_vertex] = -1; // pinned: u_v = 0
|
||||
int idx = 0;
|
||||
for (auto v : remaining_vertices)
|
||||
maps.v_idx[v] = idx++;
|
||||
|
||||
// HyperIdeal: all vertices and edges are free DOFs
|
||||
int n_dofs = assign_all_dof_indices(mesh, maps);
|
||||
// maps.v_idx[v] ∈ [0, n_v)
|
||||
// maps.e_idx[e] ∈ [n_v, n_v + n_e)
|
||||
```
|
||||
|
||||
`-1` in `v_idx` or `e_idx` means the DOF is pinned (fixed at its `lambda0` value).
|
||||
The solver never writes to pinned DOFs. All indexing is 0-based and contiguous.
|
||||
|
||||
---
|
||||
|
||||
## Gauss–Bonnet — the most common source of failure
|
||||
|
||||
Prescribing angles that violate Gauss–Bonnet means no conformal factor can realise the
|
||||
target metric — the Newton solver will iterate indefinitely without converging.
|
||||
|
||||
```cpp
|
||||
// Option A: verify before solving (throws on violation)
|
||||
check_gauss_bonnet(mesh, maps);
|
||||
|
||||
// Option B: auto-correct (redistributes defect uniformly across all vertices)
|
||||
enforce_gauss_bonnet(mesh, maps);
|
||||
```
|
||||
|
||||
The target violation is `|Σ(2π−Θᵥ) − 2π·χ(M)| > tol`. Default tolerance: `1e-10`.
|
||||
141
doc/api/extending.md
Normal file
141
doc/api/extending.md
Normal file
@@ -0,0 +1,141 @@
|
||||
# Extending conformallab++
|
||||
|
||||
## Adding a new functional
|
||||
|
||||
All three existing functionals (`euclidean_functional.hpp`, `spherical_functional.hpp`,
|
||||
`hyper_ideal_functional.hpp`) follow the same pattern. Copy the structure of the
|
||||
simplest one (`euclidean_functional.hpp`) as a template.
|
||||
|
||||
### Step 1 — Maps struct
|
||||
|
||||
```cpp
|
||||
// my_functional.hpp
|
||||
struct MyMaps {
|
||||
ConformalMesh::Property_map<Vertex_index, double> lambda0; // initial log-lengths
|
||||
ConformalMesh::Property_map<Vertex_index, double> theta_v; // target angles
|
||||
ConformalMesh::Property_map<Vertex_index, int> v_idx; // DOF index, -1 = pinned
|
||||
// add edge DOFs if needed:
|
||||
ConformalMesh::Property_map<Edge_index, int> e_idx;
|
||||
};
|
||||
|
||||
MyMaps setup_my_maps(ConformalMesh& mesh);
|
||||
void compute_my_lambda0_from_mesh(ConformalMesh& mesh, MyMaps& maps);
|
||||
```
|
||||
|
||||
### Step 2 — Gradient
|
||||
|
||||
The gradient must satisfy: `G_v = Σ(angle contributions at v) − theta_v[v]`.
|
||||
|
||||
```cpp
|
||||
std::vector<double> my_gradient(
|
||||
const ConformalMesh& mesh,
|
||||
const std::vector<double>& x,
|
||||
const MyMaps& maps)
|
||||
{
|
||||
std::vector<double> G(n_dofs, 0.0);
|
||||
for (auto f : mesh.faces()) {
|
||||
// compute angles in face f given current x
|
||||
// accumulate into G[maps.v_idx[v]] for each vertex v of f
|
||||
}
|
||||
// subtract target angles
|
||||
for (auto v : mesh.vertices())
|
||||
if (maps.v_idx[v] >= 0)
|
||||
G[maps.v_idx[v]] -= maps.theta_v[v];
|
||||
return G;
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3 — Gradient check test
|
||||
|
||||
Before claiming correctness, verify with finite differences. Copy any `GradientCheck_*`
|
||||
test suite from `tests/cgal/test_*_functional.cpp`:
|
||||
|
||||
```cpp
|
||||
TEST(MyFunctional, GradientCheck_Triangle) {
|
||||
ConformalMesh mesh = make_single_triangle();
|
||||
MyMaps maps = setup_my_maps(mesh);
|
||||
// ... assign DOFs, set natural theta ...
|
||||
|
||||
const double eps = 1e-5;
|
||||
auto G = my_gradient(mesh, x0, maps);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
std::vector<double> xp = x0, xm = x0;
|
||||
xp[i] += eps; xm[i] -= eps;
|
||||
double fd = (my_energy(mesh, xp, maps) - my_energy(mesh, xm, maps)) / (2*eps);
|
||||
EXPECT_NEAR(G[i], fd, 1e-7) << "DOF " << i;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Step 4 — Hessian and Newton wrapper
|
||||
|
||||
```cpp
|
||||
// Reuse solve_linear_system from newton_solver.hpp
|
||||
Eigen::SparseMatrix<double> H = my_hessian(mesh, x, maps);
|
||||
bool used_fallback;
|
||||
auto dx = conformallab::solve_linear_system(H, -G, &used_fallback);
|
||||
```
|
||||
|
||||
Or write a thin `newton_my()` wrapper following the structure of `newton_euclidean()`.
|
||||
|
||||
---
|
||||
|
||||
## Adding a new geometry mode
|
||||
|
||||
To add a new space (e.g. de Sitter, flat 3-torus):
|
||||
|
||||
1. **Trilateration function** — implement `trilaterate_my(p1, p2, l12, l13, l23)` that
|
||||
places a third point given two placed points and three edge lengths.
|
||||
This is the only geometry-specific part of the layout.
|
||||
|
||||
2. **BFS structure** — reuse the priority-BFS loop from `euclidean_layout()` in `layout.hpp`.
|
||||
The loop itself is geometry-agnostic; swap in your trilateration function.
|
||||
|
||||
3. **Holonomy** — if the space has a holonomy group, record the transition maps at seam edges
|
||||
the same way `HolonomyData` does for translations (Euclidean) or Möbius maps (hyperbolic).
|
||||
|
||||
---
|
||||
|
||||
## Adding a new processing unit
|
||||
|
||||
1. Declare preconditions and outputs explicitly (see [contracts.md](contracts.md)).
|
||||
|
||||
2. Write a header-only implementation in `code/include/my_unit.hpp`.
|
||||
|
||||
3. Add tests in `code/tests/cgal/test_my_unit.cpp` and register in
|
||||
`code/tests/cgal/CMakeLists.txt`.
|
||||
|
||||
4. Pipeline validation is currently manual (documented contracts). A compile-time or
|
||||
runtime check via the declarative YAML pipeline (Phase 8e) is the planned upgrade —
|
||||
see [cgal-package.md](cgal-package.md).
|
||||
|
||||
---
|
||||
|
||||
## Porting from Java
|
||||
|
||||
When porting a class from the Java library:
|
||||
|
||||
1. Locate the original at [github.com/varylab/conformallab](https://github.com/varylab/conformallab)
|
||||
under `src/main/java/de/varylab/discreteconformal/`.
|
||||
|
||||
2. The Java library uses `CoHDS` (half-edge data structure) with intrusive vertex/edge data.
|
||||
Map these to CGAL property maps:
|
||||
```
|
||||
Java: vertex.getLambda() → C++: maps.lambda0[v]
|
||||
Java: edge.getAlpha() → C++: maps.e_alpha[e]
|
||||
Java: vertex.getTheta() → C++: maps.theta_v[v]
|
||||
Java: vertex.getSolverIndex() → C++: maps.v_idx[v]
|
||||
```
|
||||
|
||||
3. Java uses `HalfedgeInterface` adapters with named accessors like `getOppositeVertex()`.
|
||||
C++ equivalent:
|
||||
```cpp
|
||||
// Java: h.getOppositeVertex()
|
||||
Vertex_index v_opp = mesh.target(mesh.next(h));
|
||||
|
||||
// Java: h.getNextHalfedge().getOppositeVertex()
|
||||
Vertex_index v_opp2 = mesh.target(mesh.next(mesh.next(h)));
|
||||
```
|
||||
|
||||
4. Port the test cases from the Java `@Test` methods. The "natural theta" trick
|
||||
(`theta_v[v] = actual_angle_sum`) works the same way in both languages.
|
||||
74
doc/api/headers.md
Normal file
74
doc/api/headers.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# Public Headers (`code/include/`)
|
||||
|
||||
All algorithms are header-only. Include the headers you need directly —
|
||||
there is no compiled library to link against (only GTest for tests and
|
||||
CGAL/Eigen for the CGAL-dependent headers).
|
||||
|
||||
## Core mesh type
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `conformal_mesh.hpp` | `ConformalMesh` = `CGAL::Surface_mesh<Point3>`. Property-map naming convention. Index type aliases. `GeometryType` enum. |
|
||||
| `constants.hpp` | `conformallab::PI`, `TWO_PI` |
|
||||
| `mesh_builder.hpp` | `make_triangle()` / `make_tetrahedron()` / `make_quad_strip()` / `make_fan()` / `make_open_cylinder()` — test mesh factories |
|
||||
| `mesh_io.hpp` | `load_mesh()` / `save_mesh()` via `CGAL::IO` (OFF / OBJ / PLY) |
|
||||
| `mesh_utils.hpp` | `cgal_to_eigen()` — convert `ConformalMesh` vertex positions to `Eigen::MatrixXd` |
|
||||
|
||||
## Special functions
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `clausen.hpp` | `clausen_cl2(θ)` (Clausen Cl₂), `lobachevsky(θ)` (Л), `im_li2(θ)` (ImLi₂ = imaginary part of dilogarithm) |
|
||||
|
||||
## HyperIdeal geometry (H²)
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `hyper_ideal_geometry.hpp` | `ζ₁₃`, `ζ₁₄`, `ζ₁₅` (Springborn 2020), `l_from_zeta()`, `alpha_ij()`, `beta_i()`, `sigma_i()`, `sigma_ij()` |
|
||||
| `hyper_ideal_utility.hpp` | Tetrahedron volumes (Meyerhoff formula, Kolpakov–Mednykh) |
|
||||
| `hyper_ideal_visualization_utility.hpp` | Poincaré disk projection, circumcircle helpers, Lorentz boost |
|
||||
| `hyper_ideal_functional.hpp` | `HyperIdealMaps`, `setup_hyper_ideal_maps()`, `compute_hyper_ideal_lambda0_from_mesh()`, `assign_all_dof_indices()`, `hyper_ideal_gradient()`, `hyper_ideal_energy()` |
|
||||
| `hyper_ideal_hessian.hpp` | `hyper_ideal_hessian()` — symmetric FD Hessian (Phase 9b: analytic) |
|
||||
|
||||
## Spherical geometry (S²)
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `spherical_geometry.hpp` | Spherical arc-length, half-angle formula, spherical law of cosines |
|
||||
| `spherical_functional.hpp` | `SphericalMaps`, `setup_spherical_maps()`, `compute_spherical_lambda0_from_mesh()`, `spherical_gradient()`, `spherical_energy()` |
|
||||
| `spherical_hessian.hpp` | `spherical_hessian()` — analytic Hessian via ∂α/∂u from law of cosines |
|
||||
|
||||
## Euclidean geometry (ℝ²)
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `euclidean_geometry.hpp` | Euclidean corner angle (t-value / atan2), edge lengths from DOF vector |
|
||||
| `euclidean_functional.hpp` | `EuclideanMaps`, `setup_euclidean_maps()`, `compute_euclidean_lambda0_from_mesh()`, `euclidean_gradient()`, `euclidean_energy()` |
|
||||
| `euclidean_hessian.hpp` | `euclidean_hessian()` — cotangent Laplacian (Pinkall–Polthier 1993) |
|
||||
|
||||
## Solver
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `newton_solver.hpp` | `newton_euclidean()`, `newton_spherical()`, `newton_hyper_ideal()`, `solve_linear_system()` (SimplicialLDLT + SparseQR fallback), `NewtonResult` struct |
|
||||
|
||||
## Preprocessing
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `gauss_bonnet.hpp` | `euler_characteristic()`, `genus()`, `gauss_bonnet_sum()`, `gauss_bonnet_rhs()`, `gauss_bonnet_deficit()`, `check_gauss_bonnet()`, `enforce_gauss_bonnet()` |
|
||||
|
||||
## Layout and holonomy
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `cut_graph.hpp` | `CutGraph` struct, `compute_cut_graph()` — tree-cotree algorithm (Erickson–Whittlesey 2005), produces 2g seam edges |
|
||||
| `layout.hpp` | `euclidean_layout()`, `spherical_layout()`, `hyper_ideal_layout()`, `normalise_{euclidean,hyperbolic,spherical}()`. Structs: `Layout2D`, `Layout3D`, `HolonomyData`. `MobiusMap` (T(z)=(az+b)/(cz+d), `from_three`, `compose`, `inverse`, `apply`). Priority-BFS, `halfedge_uv`. |
|
||||
|
||||
## Post-processing
|
||||
|
||||
| Header | Description |
|
||||
|---|---|
|
||||
| `period_matrix.hpp` | `PeriodData`, `compute_period_matrix()`, `reduce_to_fundamental_domain()`, `is_in_fundamental_domain()` — period ratio τ = ω₂/ω₁ ∈ ℍ, SL(2,ℤ) reduction |
|
||||
| `fundamental_domain.hpp` | `FundamentalDomain`, `compute_fundamental_domain()` (genus 1: CCW parallelogram; genus > 1: empty, TODO Phase 9c), `tiling_copy()`, `tiling_neighbourhood()` |
|
||||
| `serialization.hpp` | `save_result_json()`, `load_result_json()`, `save_result_xml()`, `load_result_xml()`, `save_layout_off()` |
|
||||
214
doc/api/pipeline.md
Normal file
214
doc/api/pipeline.md
Normal file
@@ -0,0 +1,214 @@
|
||||
# Pipeline API Reference
|
||||
|
||||
The full pipeline from mesh loading to layout and serialisation.
|
||||
See [doc/architecture/overall_pipeline.md](../architecture/overall_pipeline.md) for the
|
||||
Mermaid diagram and stage descriptions.
|
||||
|
||||
---
|
||||
|
||||
## Minimal Euclidean pipeline
|
||||
|
||||
```cpp
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "gauss_bonnet.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
|
||||
using namespace conformallab;
|
||||
|
||||
ConformalMesh mesh = load_mesh("input.off");
|
||||
|
||||
// 1. Set up property maps and initialise log-edge-lengths from 3-D positions
|
||||
EuclideanMaps maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// 2. Assign DOF indices — pin first vertex (gauge fix for open meshes)
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1; // pinned: u_v = 0
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
|
||||
// 3. Set target angles — "natural equilibrium" makes x* = 0
|
||||
std::vector<double> x0(idx, 0.0);
|
||||
auto G0 = euclidean_gradient(mesh, x0, maps);
|
||||
for (auto v : mesh.vertices())
|
||||
if (maps.v_idx[v] >= 0)
|
||||
maps.theta_v[v] -= G0[maps.v_idx[v]];
|
||||
|
||||
// 4. Check Gauss–Bonnet (mandatory before solving)
|
||||
check_gauss_bonnet(mesh, maps); // throws if Σ(2π−Θᵥ) ≠ 2π·χ(M)
|
||||
|
||||
// 5. Solve
|
||||
NewtonResult res = newton_euclidean(mesh, x0, maps);
|
||||
// res.converged, res.x, res.iterations, res.grad_inf_norm
|
||||
|
||||
// 6. Layout
|
||||
Layout2D layout = euclidean_layout(mesh, res.x, maps);
|
||||
// layout.uv[v.idx()], layout.halfedge_uv[h.idx()]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Layout with holonomy (closed surfaces)
|
||||
|
||||
```cpp
|
||||
#include "cut_graph.hpp"
|
||||
#include "period_matrix.hpp"
|
||||
#include "fundamental_domain.hpp"
|
||||
|
||||
// Tree-cotree cut graph — 2g seam edges
|
||||
CutGraph cg = compute_cut_graph(mesh);
|
||||
// cg.cut_edge_flags[e.idx()] — true if seam edge
|
||||
// cg.genus — topological genus g
|
||||
|
||||
// Layout with holonomy tracking
|
||||
HolonomyData hol;
|
||||
Layout2D layout = euclidean_layout(mesh, res.x, maps, &cg, &hol, /*normalise=*/true);
|
||||
// hol.translations[i] — lattice generator ωᵢ ∈ ℂ (Euclidean)
|
||||
|
||||
// Period matrix (genus 1 flat torus)
|
||||
PeriodData pd = compute_period_matrix(hol);
|
||||
// pd.tau — complex period ratio τ = ω₂/ω₁ ∈ ℍ
|
||||
// pd.omega — {ω₁, ω₂} as complex<double>
|
||||
// pd.in_fundamental_domain — true if SL(2,ℤ)-reduced to |τ|≥1, |Re(τ)|<½
|
||||
|
||||
// Fundamental domain parallelogram
|
||||
FundamentalDomain fd = compute_fundamental_domain(hol);
|
||||
// fd.vertices[0..3] — CCW corners: {0, ω₁, ω₁+ω₂, ω₂}
|
||||
// fd.generators[0..1] — ω₁, ω₂
|
||||
|
||||
// Tiling copies for universal cover visualisation
|
||||
auto tiles = tiling_neighbourhood(layout, hol, /*m_max=*/2, /*n_max=*/2);
|
||||
// returns (2·m_max+1)·(2·n_max+1) translated layout copies
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## HyperIdeal pipeline
|
||||
|
||||
```cpp
|
||||
#include "hyper_ideal_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "cut_graph.hpp"
|
||||
|
||||
HyperIdealMaps maps = setup_hyper_ideal_maps(mesh);
|
||||
compute_hyper_ideal_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// HyperIdeal has both vertex and edge DOFs — assign all automatically
|
||||
int n_dofs = assign_all_dof_indices(mesh, maps);
|
||||
// No vertex needs to be pinned — strictly convex functional
|
||||
|
||||
std::vector<double> x0(n_dofs, 0.0);
|
||||
NewtonResult res = newton_hyper_ideal(mesh, x0, maps);
|
||||
|
||||
// Layout with Möbius holonomy
|
||||
CutGraph cg = compute_cut_graph(mesh);
|
||||
HolonomyData hol;
|
||||
Layout2D layout = hyper_ideal_layout(mesh, res.x, maps, &cg, &hol);
|
||||
// hol.mobius_maps[i] — Möbius isometry Tᵢ ∈ SU(1,1) per cut edge
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Spherical pipeline
|
||||
|
||||
```cpp
|
||||
#include "spherical_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "layout.hpp"
|
||||
|
||||
SphericalMaps maps = setup_spherical_maps(mesh);
|
||||
compute_spherical_lambda0_from_mesh(mesh, maps);
|
||||
|
||||
// Pin one vertex (gauge fix) + assign DOFs
|
||||
auto vit = mesh.vertices().begin();
|
||||
maps.v_idx[*vit++] = -1;
|
||||
int idx = 0;
|
||||
for (; vit != mesh.vertices().end(); ++vit)
|
||||
maps.v_idx[*vit] = idx++;
|
||||
|
||||
std::vector<double> x0(idx, 0.0);
|
||||
NewtonResult res = newton_spherical(mesh, x0, maps);
|
||||
|
||||
// 3-D layout on the unit sphere
|
||||
Layout3D slayout = spherical_layout(mesh, res.x, maps);
|
||||
// slayout.xyz[v.idx()] — 3-D position on S²
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## SparseQR fallback — direct use
|
||||
|
||||
```cpp
|
||||
#include "newton_solver.hpp"
|
||||
|
||||
bool used_fallback = false;
|
||||
Eigen::VectorXd dx = conformallab::solve_linear_system(H, rhs, &used_fallback);
|
||||
if (used_fallback)
|
||||
std::cerr << "Warning: H is rank-deficient, used SparseQR\n";
|
||||
```
|
||||
|
||||
The fallback is automatically invoked by all three `newton_*` functions when
|
||||
`SimplicialLDLT` fails. It finds the minimum-norm Newton step orthogonal to the null space.
|
||||
|
||||
---
|
||||
|
||||
## Serialisation
|
||||
|
||||
```cpp
|
||||
#include "serialization.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
|
||||
// Save layout as OFF mesh file
|
||||
save_layout_off("layout.off", mesh, layout);
|
||||
|
||||
// Full result: DOF vector + metadata + layout UVs
|
||||
save_result_json("result.json", res, "euclidean", mesh, &layout);
|
||||
save_result_xml ("result.xml", res, "euclidean", mesh, &layout);
|
||||
|
||||
// Round-trip load
|
||||
NewtonResult res2;
|
||||
std::string geom;
|
||||
Layout2D uv2;
|
||||
load_result_json("result.json", &res2, &geom, &uv2);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Attaching custom property maps
|
||||
|
||||
```cpp
|
||||
// Add a custom per-vertex map
|
||||
auto [curv, created] = mesh.add_property_map<Vertex_index, double>("v:my_curv", 0.0);
|
||||
curv[v] = 3.14;
|
||||
|
||||
// Retrieve an existing map
|
||||
auto [curv2, found] = mesh.property_map<Vertex_index, double>("v:my_curv");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Halfedge traversal conventions
|
||||
|
||||
```cpp
|
||||
for (auto f : mesh.faces()) {
|
||||
auto h0 = mesh.halfedge(f); // canonical halfedge of face f
|
||||
auto h1 = mesh.next(h0);
|
||||
auto h2 = mesh.next(h1);
|
||||
|
||||
Vertex_index v1 = mesh.source(h0); // = mesh.target(h2)
|
||||
Vertex_index v2 = mesh.source(h1);
|
||||
Vertex_index v3 = mesh.source(h2);
|
||||
|
||||
// Angle at v3 is opposite to halfedge h0 (edge v1–v2)
|
||||
// h_alpha[h0] = α₃, h_alpha[h1] = α₁, h_alpha[h2] = α₂
|
||||
|
||||
Edge_index e0 = mesh.edge(h0);
|
||||
bool is_seam = cg.cut_edge_flags[e0.idx()];
|
||||
bool is_boundary = mesh.is_border(mesh.opposite(h0));
|
||||
}
|
||||
```
|
||||
81
doc/api/tests.md
Normal file
81
doc/api/tests.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# Test Suites
|
||||
|
||||
## `conformallab_tests` — always built (no CGAL)
|
||||
|
||||
Pure-math tests, only Eigen required. Covers Java utilities ported in Phase 1–2.
|
||||
|
||||
| File | What it tests |
|
||||
|---|---|
|
||||
| `test_clausen.cpp` | Clausen Cl₂, Lobachevsky Л, ImLi₂ — values at known points |
|
||||
| `test_hyper_ideal_utility.cpp` | Tetrahedron volumes (Meyerhoff / Kolpakov–Mednykh) |
|
||||
| `test_matrix_utility.cpp` | Matrix helpers |
|
||||
| `test_surface_curve_utility.cpp` | Surface curve utilities |
|
||||
| `test_discrete_elliptic_utility.cpp` | Discrete elliptic functions |
|
||||
| `test_p2_utility.cpp` | P2 projective utilities |
|
||||
| `test_hyper_ideal_visualization_utility.cpp` | Poincaré disk projection, circumcircle |
|
||||
|
||||
**Total: 36 tests, 0 skipped.**
|
||||
|
||||
---
|
||||
|
||||
## `conformallab_cgal_tests` — built with `-DWITH_CGAL_TESTS=ON` or `-DWITH_CGAL=ON`
|
||||
|
||||
All tests have CTest prefix `cgal.` (set via `TEST_PREFIX "cgal."` in CMakeLists).
|
||||
|
||||
| Suite | File | Tests | What is verified |
|
||||
|---|---|---:|---|
|
||||
| `ConformalMeshTopology` | `test_conformal_mesh.cpp` | 4 | Euler characteristic, vertex/edge/face counts |
|
||||
| `ConformalMeshTraversal` | `test_conformal_mesh.cpp` | 4 | Halfedge iteration, valence, opposite |
|
||||
| `ConformalMeshProperties` | `test_conformal_mesh.cpp` | 5 | Property maps (λ, θ, idx, α, geometry type) |
|
||||
| `ConformalMeshValidity` | `test_conformal_mesh.cpp` | 1 | CGAL validity for all factory meshes |
|
||||
| `HyperIdealFunctional` | `test_hyper_ideal_functional.cpp` | 7 | FD gradient checks + Hessian symmetry |
|
||||
| `SphericalFunctional` | `test_spherical_functional.cpp` | 12 | Angle formula + gradient + gauge-fix |
|
||||
| `EuclideanFunctional` | `test_euclidean_functional.cpp` | 11 | Angle formula + gradient |
|
||||
| `EuclideanHessian` | `test_euclidean_hessian.cpp` | 9 | Cotangent Laplacian structure, FD agreement, PSD, null space |
|
||||
| `SphericalHessian` | `test_spherical_hessian.cpp` | 8 | Derivative correctness, NSD at equilibrium |
|
||||
| `NewtonSolver` | `test_newton_solver.cpp` | 11 | Convergence: Euclidean ×3, Spherical ×4, HyperIdeal ×4 |
|
||||
| `SparseQRFallback` | `test_newton_solver.cpp` | 3 | Full-rank LDLT · singular matrix → QR · closed mesh gauge mode |
|
||||
| `MeshIO` | `test_mesh_io.cpp` | 9 | OFF/OBJ round-trips, error handling |
|
||||
| `Pipeline` | `test_pipeline.cpp` | 5 | End-to-end: build → setup → solve → export → reload |
|
||||
| `Layout` | `test_layout.cpp` | 8 | Edge-length preservation (Eucl./Spher.), Poincaré disk layout |
|
||||
| `Serialization` | `test_layout.cpp` | 2 | JSON and XML round-trips (DOF vector + layout UVs) |
|
||||
| `GaussBonnet` | `test_phase6.cpp` | 8 | χ, genus, sum/RHS, deficit, check, enforce |
|
||||
| `CutGraph` | `test_phase6.cpp` | 6 | Tree-cotree, open/closed meshes, flag–index consistency |
|
||||
| `HyperbolicTrilateration` | `test_phase6.cpp` | 4 | Möbius + law of cosines: exact distances, disk interior, off-origin |
|
||||
| `Normalisation` | `test_phase6.cpp` | 4 | Euclidean centroid, length ratios, Möbius centring |
|
||||
| `MobiusMap` | `test_phase7.cpp` | 8 | Identity, inverse, compose, `from_three`, `apply(Vector2d)` |
|
||||
| `BestRootFace` | `test_phase7.cpp` | 2 | Valid root face selection, interior bonus |
|
||||
| `HalfedgeUV` | `test_phase7.cpp` | 4 | Size = #halfedges, seam consistency, boundary halfedges = 0 |
|
||||
| `PriorityBFS` | `test_phase7.cpp` | 3 | Success, no seam on open meshes, all vertices placed |
|
||||
| `NormaliseEuclidean` | `test_phase7.cpp` | 2 | UV centroid = 0, halfedge_uv centroid = 0 |
|
||||
| `PeriodMatrix` | `test_phase7.cpp` | 7 | τ ∈ ℍ, SL(2,ℤ) reduction, exception outside ℍ |
|
||||
| `FundamentalDomain` | `test_phase7.cpp` | 7 | Genus-1 parallelogram CCW, generators, g > 1 empty |
|
||||
| `TilingCopy/Neighbourhood` | `test_phase7.cpp` | 4 | Translation correct, tile count |
|
||||
| `CuttingUtility` | `test_geometry_utils.cpp` | 3 | point_in_triangle_2d: false, true, unit triangle (Java CuttinUtilityTest) |
|
||||
| `UnwrapUtility` | `test_geometry_utils.cpp` | 2 | corner angle: collinear → π, equilateral → π/3 (Java UnwrapUtilityTest) |
|
||||
| `ConvergenceUtility` | `test_geometry_utils.cpp` | 6 | circumradius + scale-invariant R_f/√A (Java ConvergenceUtilityTests) |
|
||||
| `HomologyGenerators` | `test_geometry_utils.cpp` | 1 | GTEST_SKIP stub — genus-2 mesh missing (Java HomologyTest Test 7, Phase 9c) |
|
||||
|
||||
**Total: 170 tests, 1 intentional skip.**
|
||||
|
||||
The skip is `HomologyGenerators.Genus2_FourGeneratorPaths_BLOCKED` — blocked until
|
||||
a genus-2 mesh is available (Phase 9c). It corresponds to a Java `@Ignore`-annotated
|
||||
test in the original library.
|
||||
|
||||
---
|
||||
|
||||
## Running tests
|
||||
|
||||
```bash
|
||||
# All CGAL tests
|
||||
ctest --test-dir build -R "^cgal\." --output-on-failure
|
||||
|
||||
# One suite
|
||||
./build/conformallab_cgal_tests --gtest_filter="PeriodMatrix*"
|
||||
|
||||
# One specific test
|
||||
./build/conformallab_cgal_tests --gtest_filter="PeriodMatrix.TauInUpperHalfPlane"
|
||||
|
||||
# Verbose output with timing
|
||||
./build/conformallab_cgal_tests --gtest_filter="NewtonSolver*" --gtest_print_time=1
|
||||
```
|
||||
124
doc/architecture/design-decisions.md
Normal file
124
doc/architecture/design-decisions.md
Normal file
@@ -0,0 +1,124 @@
|
||||
# Key Design Decisions
|
||||
|
||||
Rationale for the architectural choices that distinguish conformallab++ from the
|
||||
Java original and from generic geometry-processing frameworks.
|
||||
|
||||
---
|
||||
|
||||
## CGAL `Surface_mesh` as the halfedge data structure
|
||||
|
||||
The Java library uses `CoHDS` — a custom intrusive halfedge data structure with
|
||||
`CoVertex`, `CoEdge`, `CoFace` types that carry domain-specific data directly as fields.
|
||||
|
||||
conformallab++ replaces this with `CGAL::Surface_mesh<Point3>` and attaches data via
|
||||
**named property maps**:
|
||||
|
||||
```cpp
|
||||
// Java: vertex.getLambda() → C++: maps.lambda0[v]
|
||||
// Java: edge.getAlpha() → C++: maps.e_alpha[e]
|
||||
// Java: vertex.getSolverIndex() → C++: maps.v_idx[v]
|
||||
|
||||
auto [lambda0, ok] = mesh.add_property_map<Edge_index, double>("e:lambda0", 0.0);
|
||||
lambda0[e] = 1.234;
|
||||
```
|
||||
|
||||
This decouples the mesh topology from the algorithm data, makes it straightforward
|
||||
to attach multiple independent data sets to the same mesh, and will enable the Phase 8
|
||||
traits-class design to work with any CGAL-conforming mesh type.
|
||||
|
||||
---
|
||||
|
||||
## DOF vector convention
|
||||
|
||||
All three functionals use the same indexing scheme: a flat `std::vector<double> x`
|
||||
indexed by `v_idx[v]` (vertices) and `e_idx[e]` (edges, HyperIdeal only).
|
||||
Index value `-1` means "pinned" — the DOF is fixed at zero and excluded from the
|
||||
Newton system.
|
||||
|
||||
```
|
||||
x[maps.v_idx[v]] = uᵥ (conformal scale factor, Euclidean/Spherical)
|
||||
x[maps.e_idx[e]] = λₑ (edge log-length variable, HyperIdeal only)
|
||||
-1 pinned — u_v = 0 / λ_e = 0
|
||||
```
|
||||
|
||||
This is consistent across all three geometry modes, enabling the same Newton solver
|
||||
and linear system infrastructure to serve all three without branching.
|
||||
|
||||
---
|
||||
|
||||
## Priority-BFS layout
|
||||
|
||||
A naive BFS layout places faces in arbitrary order; trilateration errors accumulate
|
||||
along the BFS frontier. conformallab++ uses a **priority min-heap on BFS depth**:
|
||||
|
||||
```
|
||||
depth(face) = max(depth[v_src], depth[v_tgt]) + 1 for each new face
|
||||
```
|
||||
|
||||
Faces with smaller depth (closer to the root) are placed first. This means each
|
||||
face's trilateration uses the two most accurately-placed adjacent vertices, minimising
|
||||
error propagation across the mesh.
|
||||
|
||||
Root face selection: largest 3-D area face, with an additional 1.5× bonus for
|
||||
interior faces over boundary faces. This heuristic places the root where metric
|
||||
distortion is lowest.
|
||||
|
||||
---
|
||||
|
||||
## `halfedge_uv` semantics
|
||||
|
||||
`layout.uv[v.idx()]` gives the *primary* UV coordinate of vertex `v` — the position
|
||||
from the shallowest BFS visit. At seam edges this is insufficient for GPU rendering:
|
||||
two faces sharing a seam vertex need *different* UV values for that vertex.
|
||||
|
||||
`layout.halfedge_uv[h.idx()]` stores the UV of `source(h)` **as seen from `face(h)`**:
|
||||
|
||||
```
|
||||
halfedge h → face(h) → source(h) has UV = halfedge_uv[h.idx()]
|
||||
opposite(h) → face(h') → source(h) has UV = halfedge_uv[opposite(h).idx()]
|
||||
(different value at a seam)
|
||||
```
|
||||
|
||||
At seam halfedges the two opposite halfedges carry different UV values — each face
|
||||
gets its own copy of the seam vertex. This enables a proper GPU texture atlas
|
||||
without vertex duplication in the index buffer.
|
||||
|
||||
---
|
||||
|
||||
## Spherical Hessian sign convention
|
||||
|
||||
The spherical energy functional is **concave** (negative semidefinite Hessian).
|
||||
Standard Newton would require solving `H·Δx = −G` with NSD `H`, which Cholesky
|
||||
cannot handle.
|
||||
|
||||
`newton_spherical()` solves `(−H)·Δx = G` instead — algebraically identical,
|
||||
but `−H` is PSD and `SimplicialLDLT` works correctly. This sign flip is handled
|
||||
transparently inside `newton_spherical()`; callers need not be aware of it.
|
||||
|
||||
The gradient sign in spherical mode is also flipped vs. Euclidean:
|
||||
- Euclidean: `G_v = actual_sum − Θᵥ`
|
||||
- Spherical: `G_v = Θᵥ − actual_sum`
|
||||
|
||||
Both conventions drive the same equilibrium condition `G = 0`.
|
||||
|
||||
---
|
||||
|
||||
## HyperIdeal Hessian via finite differences
|
||||
|
||||
The analytic HyperIdeal Hessian requires differentiating through the chain
|
||||
`(bᵢ, aₑ) → lᵢⱼ → ζ₁₃/ζ₁₄/ζ₁₅ → αᵢⱼ/βᵢ` with four vertex-type combinations
|
||||
per edge — substantial implementation complexity.
|
||||
|
||||
conformallab++ uses a **symmetric finite-difference Hessian** instead:
|
||||
|
||||
```
|
||||
H[i,j] = (G(x + ε·eⱼ)[i] − G(x − ε·eⱼ)[i]) / (2ε), ε = 1e-5
|
||||
```
|
||||
|
||||
Properties:
|
||||
- O(ε²) accuracy — relative error ≈ 10⁻¹⁰ at ε = 10⁻⁵
|
||||
- PSD guaranteed by strict convexity of the HyperIdeal energy (Springborn 2020)
|
||||
- Symmetrised automatically: `H = (H + Hᵀ) / 2`
|
||||
- Cost: n extra gradient evaluations per Newton step (acceptable for < 500 DOFs)
|
||||
|
||||
The analytic Hessian is deferred to Phase 9b. See [roadmap/java-parity.md](../roadmap/java-parity.md).
|
||||
281
doc/architecture/geometry-central-comparison.md
Normal file
281
doc/architecture/geometry-central-comparison.md
Normal file
@@ -0,0 +1,281 @@
|
||||
# conformallab++ vs. geometry-central — Detailed Comparison
|
||||
|
||||
> **Purpose of this document.**
|
||||
> conformallab++ and geometry-central (CMU, Keenan Crane's group) both implement
|
||||
> discrete conformal equivalence of triangulated surfaces. They share the same
|
||||
> mathematical core but diverge in algorithmic strategy, scope, and target audience.
|
||||
> This document maps the overlap precisely, identifies what cannot and should not be
|
||||
> adopted, and explains where a side-by-side study creates scientific added value.
|
||||
|
||||
---
|
||||
|
||||
## 1 — Shared mathematical foundation
|
||||
|
||||
Both libraries implement the following chain:
|
||||
|
||||
```
|
||||
Input mesh M → edge lengths ℓᵢⱼ → solve for u ∈ ℝᵛ
|
||||
such that ℓ̃ᵢⱼ = e^{(uᵢ+uⱼ)/2} · ℓᵢⱼ satisfies Σα_v(u) = Θᵥ ∀v
|
||||
```
|
||||
|
||||
The variational framework that makes this a well-posed optimisation problem goes
|
||||
back to **Bobenko & Springborn (2004)**. The hyperbolic (HyperIdeal) geometry is
|
||||
from **Springborn (2020)**. The geometry-central implementation (Gillespie,
|
||||
Springborn & Crane, SIGGRAPH 2021) is an explicit extension of Springborn 2020
|
||||
to intrinsic triangulations.
|
||||
|
||||
**Key consequence:** the *mathematical problem* is identical. Any difference in
|
||||
output is either a normalization convention or a bug in one of the two libraries —
|
||||
making cross-validation directly meaningful.
|
||||
|
||||
---
|
||||
|
||||
## 2 — Algorithmic comparison
|
||||
|
||||
| Dimension | conformallab++ | geometry-central (Gillespie 2021) |
|
||||
|---|---|---|
|
||||
| **Solver** | Newton–Raphson, analytical Hessian | Newton or Yamabe gradient flow (user choice) |
|
||||
| **Convergence** | Quadratic (8–20 iterations on typical meshes) | Newton: quadratic; Yamabe: linear (~hundreds of steps) |
|
||||
| **Triangulation** | Fixed throughout — operates on original `Surface_mesh` | Ptolemaic flips applied before/during solve to reach intrinsic Delaunay |
|
||||
| **Hessian** | SimplicialLDLT + SparseQR fallback; analytical for Euclidean/Spherical, FD for HyperIdeal | Assembled on the current (possibly flipped) triangulation |
|
||||
| **Mesh backend** | CGAL `Surface_mesh<Point_3>` | geometry-central `ManifoldSurfaceMesh` |
|
||||
| **Geometry modes** | Euclidean ✓ · Spherical ✓ · HyperIdeal ✓ | Euclidean ✓ · Hyperbolic ✓ · Spherical ✗ |
|
||||
| **Open meshes** | ✓ (boundary DOFs pinned) | ✓ |
|
||||
|
||||
### Why Newton on a fixed triangulation works well
|
||||
|
||||
The Euclidean and HyperIdeal energies are strictly convex after gauge-fixing.
|
||||
Newton therefore converges from u=0 in 8–20 iterations for any reasonable mesh.
|
||||
The Hessian is the cotangent Laplacian (Euclidean) or its hyperbolic analog —
|
||||
well-conditioned on Delaunay meshes, but can degrade on strongly non-Delaunay inputs.
|
||||
|
||||
### What Ptolemaic flips add
|
||||
|
||||
A Ptolemaic flip replaces diagonal AC with BD in a quadrilateral under the constraint
|
||||
that the Ptolemy relation
|
||||
|
||||
```
|
||||
AC · BD = AB · CD + AD · BC
|
||||
```
|
||||
|
||||
holds. This is *conformal-class-preserving* — the new λ₀ values represent the same
|
||||
discrete conformal structure. The gain: the flipped triangulation is intrinsic
|
||||
Delaunay, which bounds the off-diagonal Hessian entries and prevents ill-conditioning
|
||||
on pathological inputs.
|
||||
|
||||
**This is the only genuine algorithmic advantage geometry-central has for the shared
|
||||
sub-problem.** It costs nothing mathematically and buys robustness on bad meshes.
|
||||
|
||||
---
|
||||
|
||||
## 3 — Feature matrix: what exists where
|
||||
|
||||
| Feature | conformallab++ | geometry-central | Notes |
|
||||
|---|---|---|---|
|
||||
| Discrete conformal equivalence (Euclidean) | ✓ | ✓ | Shared core |
|
||||
| Discrete conformal equivalence (Hyperbolic/HyperIdeal) | ✓ (Springborn 2020 formulation) | ✓ (Gillespie 2021 extension) | Mathematically equivalent |
|
||||
| Discrete conformal equivalence (Spherical) | ✓ | ✗ | Unique to conformallab++ |
|
||||
| Analytical Hessian (Euclidean & Spherical) | ✓ | ✓ | |
|
||||
| Analytical Hessian (HyperIdeal) | FD (Phase 9b: analytical planned) | ✓ | gc has analytical version |
|
||||
| Gauss–Bonnet check & enforce | ✓ | implicit in solver | |
|
||||
| Tree-cotree cut graph (2g seam edges) | ✓ | ✗ | Required for period matrix |
|
||||
| Priority-BFS layout in ℝ²/S²/Poincaré disk | ✓ | partial (conformal param only) | |
|
||||
| Möbius holonomy SU(1,1) | ✓ | ✗ | Unique to conformallab++ |
|
||||
| Period matrix τ ∈ ℍ + SL(2,ℤ) reduction | ✓ | ✗ | Unique to conformallab++ |
|
||||
| Fundamental domain + tiling | ✓ | ✗ | Unique to conformallab++ |
|
||||
| Intrinsic Delaunay triangulation | ✗ | ✓ | gc has via SignpostIntrinsicTriangulation |
|
||||
| Ptolemaic flips | ✗ | ✓ | gc's robustness mechanism |
|
||||
| Heat method (geodesic distances) | ✗ | ✓ | Auxiliary tool in gc |
|
||||
| YAML/declarative pipeline | ✓ (Phase 8e spec) | ✗ | |
|
||||
| CGAL-package submission | ✓ (Phase 8 target) | ✗ | |
|
||||
| JSON/XML serialisation | ✓ | ✗ | |
|
||||
| CLI app | ✓ | ✗ | |
|
||||
| Inversive distance functional (Luo 2004) | ✗ (Phase 9a) | ✗ | Neither has it yet |
|
||||
| Siegel period matrix Ω (genus g≥2) | ✗ (Phase 10b) | ✗ | |
|
||||
|
||||
---
|
||||
|
||||
## 4 — What should be adopted — and what should not
|
||||
|
||||
### Adopt: Ptolemaic pre-conditioning (GC-2, after Phase 8)
|
||||
|
||||
**What:** a single preprocessing pass that Delaunay-izes the input triangulation
|
||||
via Ptolemaic flips, updates λ₀ accordingly, then hands off to the existing
|
||||
Newton pipeline unchanged.
|
||||
|
||||
**Why it fits:**
|
||||
- Preserves the conformal class — mathematically sound
|
||||
- Drop-in before `compute_euclidean_lambda0_from_mesh()`, no interface change
|
||||
- Does not touch cut graph, holonomy, period matrix
|
||||
- ~200 lines of code, one new test suite
|
||||
|
||||
**Interface sketch:**
|
||||
```cpp
|
||||
// include/preprocessing_delaunay.hpp (Phase GC-2)
|
||||
void ptolemy_delaunay(ConformalMesh& mesh, EuclideanMaps& maps);
|
||||
// Flips edges until all faces satisfy the Delaunay condition.
|
||||
// Updates maps.lambda0[e] via the Ptolemy relation after each flip.
|
||||
// Prerequisite: compute_euclidean_lambda0_from_mesh() already called.
|
||||
// Postcondition: mesh is an intrinsic Delaunay triangulation of the same surface.
|
||||
```
|
||||
|
||||
### Adopt partially: analytical HyperIdeal Hessian
|
||||
|
||||
geometry-central has a closed-form Hessian for the hyperbolic energy.
|
||||
conformallab++ currently uses finite differences (Phase 9b plans analytical).
|
||||
The geometry-central implementation can serve as a reference for Phase 9b —
|
||||
not a code copy, but a mathematical cross-check.
|
||||
|
||||
### Do not adopt: full intrinsic triangulations as architecture
|
||||
|
||||
**SignpostIntrinsicTriangulation** is geometry-central's core data structure.
|
||||
It tracks vertex positions as (face, barycentric coordinates) rather than 3D points.
|
||||
Replacing `CGAL::Surface_mesh` with this would require:
|
||||
|
||||
1. Rewriting the cut graph algorithm (which works on halfedges of the *original* mesh
|
||||
and must survive across flips — non-trivial bookkeeping)
|
||||
2. Tracking seam edges through flip events for holonomy computation
|
||||
3. Abandoning the CGAL package target (Phase 8) — CGAL's mesh concepts are extrinsic
|
||||
4. Losing the 3D layout output (Poincaré disk, sphere) which clients depend on
|
||||
|
||||
**Verdict:** the architecture incompatibility is fundamental, not incidental.
|
||||
The period matrix pipeline requires a stable topological cut that does not survive
|
||||
arbitrary flip sequences. This is not a solvable engineering problem within the
|
||||
current project scope — it would be a different project.
|
||||
|
||||
### Do not adopt: Yamabe flow
|
||||
|
||||
Newton converges in 8–20 iterations; Yamabe flow needs hundreds.
|
||||
The only reason to use Yamabe flow is when the Hessian is indefinite (which happens
|
||||
in the Spherical case — and conformallab++ already handles this with the correct
|
||||
sign flip in the energy). There is no mesh type where Yamabe flow beats Newton
|
||||
on metrics that conformallab++ targets.
|
||||
|
||||
---
|
||||
|
||||
## 5 — Where cross-comparison creates scientific added value
|
||||
|
||||
### 5.1 — Independent cross-validation of the shared core
|
||||
|
||||
The discrete conformal equivalence problem for Euclidean and HyperIdeal geometry
|
||||
is implemented independently in two codebases, by different groups, with different
|
||||
algorithms. Agreement on:
|
||||
|
||||
- the u-vector (after normalization)
|
||||
- UV coordinates (up to Möbius transformation)
|
||||
- the residual ‖G(u*)‖ at convergence
|
||||
|
||||
would constitute **mutual validation without ground truth**. This is the same
|
||||
methodology used in numerical PDE literature to validate independent solvers.
|
||||
|
||||
**Concrete protocol:**
|
||||
```
|
||||
For each test mesh (cathead.obj, brezel.obj, torus_4x4.off, torus_hex_6x6.off):
|
||||
1. Load into both libraries with identical vertex ordering
|
||||
2. Run conformallab++ Newton solver → u_clab, UV_clab
|
||||
3. Run geometry-central solver → u_gc, UV_gc
|
||||
4. Normalize both (subtract mean, divide by scale)
|
||||
5. Report max|u_clab[v] - u_gc[v]| and mean conformal distortion difference
|
||||
```
|
||||
|
||||
Expected: agreement to ≤ 1e-8 on well-conditioned meshes. Discrepancy would
|
||||
indicate a bug or a normalization mismatch worth investigating.
|
||||
|
||||
### 5.2 — Convergence study: Newton with vs. without Ptolemaic pre-conditioning
|
||||
|
||||
Hypothesis: on non-Delaunay meshes (e.g. a torus refined by subdivision without
|
||||
re-meshing), Ptolemaic Delaunay pre-conditioning reduces Newton iteration count.
|
||||
|
||||
**Measurable quantities:**
|
||||
- Newton iterations to ‖G‖ < 1e-8
|
||||
- Hessian condition number κ(H) at u = 0
|
||||
- Wall-clock time
|
||||
|
||||
This comparison requires only GC-2 to be implemented in conformallab++ and would
|
||||
answer the question: *how bad does a mesh have to be before Ptolemaic pre-conditioning
|
||||
pays off?*
|
||||
|
||||
**Publication potential:** a short note or conference contribution comparing the
|
||||
two approaches on a systematic mesh quality benchmark would be self-contained and
|
||||
novel — neither library has published this comparison.
|
||||
|
||||
### 5.3 — Period matrix and holonomy as differentiating contribution
|
||||
|
||||
geometry-central deliberately stops at the conformal parameterization. The period
|
||||
matrix τ and Möbius holonomy computation in conformallab++ extend the pipeline into
|
||||
Teichmüller theory territory that geometry-central does not address.
|
||||
|
||||
This is the strongest scientific differentiator: conformallab++ can compute
|
||||
|
||||
```
|
||||
τ = ω_b / ω_a ∈ ℍ, SL(2,ℤ)-reduced
|
||||
```
|
||||
|
||||
for any closed genus-1 surface, and (in Phase 10) the Siegel matrix Ω for genus g≥2.
|
||||
No other open-source C++ library does this. Cross-comparison with geometry-central
|
||||
makes this gap explicit and positions conformallab++ as the more complete tool for
|
||||
Teichmüller-theoretic applications.
|
||||
|
||||
### 5.4 — Spherical geometry as unique contribution
|
||||
|
||||
The Spherical geometry mode (angle sums → 4π on a sphere, NSD Hessian with sign
|
||||
flip) has no counterpart in geometry-central. A mathematician interested in
|
||||
conformal maps on surfaces of positive curvature (constant curvature +1) has no
|
||||
alternative in open-source C++.
|
||||
|
||||
### 5.5 — Validation of Springborn 2020 in two independent implementations
|
||||
|
||||
Springborn 2020 ("Ideal Hyperbolic Polyhedra and Discrete Uniformization") is the
|
||||
shared theoretical reference for both the HyperIdeal geometry mode in conformallab++
|
||||
(Phase 2/3) and the hyperbolic component of Gillespie 2021 in geometry-central.
|
||||
Cross-checking the ζ-function values (ζ₁₃, ζ₁₄, ζ₁₅) and the resulting angle sums
|
||||
on the same meshes would validate both implementations of the paper — a service to
|
||||
the discrete geometry community.
|
||||
|
||||
---
|
||||
|
||||
## 6 — Demarcation: where the comparison ends
|
||||
|
||||
| Topic | conformallab++ | geometry-central | Comparable? |
|
||||
|---|---|---|---|
|
||||
| u-vector at convergence | ✓ | ✓ | ✓ after normalization |
|
||||
| UV parameterization | ✓ | ✓ | ✓ up to Möbius |
|
||||
| Convergence speed (iterations) | ✓ | ✓ (Newton mode) | ✓ direct |
|
||||
| HyperIdeal angle sums | ✓ | ✓ | ✓ |
|
||||
| Spherical angle sums | ✓ | ✗ | ✗ |
|
||||
| Period matrix τ | ✓ | ✗ | ✗ |
|
||||
| Holonomy T_a, T_b | ✓ | ✗ | ✗ |
|
||||
| Fundamental domain | ✓ | ✗ | ✗ |
|
||||
| Intrinsic Delaunay quality | not tracked | ✓ | — |
|
||||
| Mesh topology handling | CGAL halfedge | gc manifold mesh | not comparable |
|
||||
| Scalability (large meshes) | not benchmarked yet | benchmarked in paper | comparable if same mesh |
|
||||
|
||||
The comparison is meaningful and complete for the **shared conformal core**.
|
||||
It ends where conformallab++ continues into Teichmüller theory (holonomy, τ,
|
||||
fundamental domain) — that region has no counterpart in geometry-central and
|
||||
must be validated by analytic invariants alone (→ `doc/math/validation.md`).
|
||||
|
||||
---
|
||||
|
||||
## 7 — Practical roadmap for the comparison
|
||||
|
||||
| Step | When | What | Effort |
|
||||
|---|---|---|---|
|
||||
| **GC-1a** | Now | Manual UV comparison on cathead.obj — run both, diff u-vectors | 1 day |
|
||||
| **GC-1b** | Now | Add normalization utility to conformallab++ (`normalize_u_vector()`) | 2h |
|
||||
| **GC-1c** | After Phase 8 | Automated comparison script (Python or small C++ binary) | 2 days |
|
||||
| **GC-2** | After Phase 8 | `ptolemy_delaunay()` preprocessing pass | 1 week |
|
||||
| **GC-bench** | After GC-2 | Convergence study: Newton ± Ptolemaic pre-conditioning on 10 meshes | 1 week |
|
||||
| **GC-paper** | Phase 10 | Short note on the comparison — period matrix as differentiator | — |
|
||||
|
||||
---
|
||||
|
||||
## 8 — References
|
||||
|
||||
| Reference | Role in this comparison |
|
||||
|---|---|
|
||||
| **Bobenko, Springborn** — *Variational Principles for Circle Patterns*, Trans. AMS (2004) | Shared variational foundation for all three geometry modes |
|
||||
| **Springborn** — *Ideal Hyperbolic Polyhedra and Discrete Uniformization*, DCG (2020) | Mathematical basis for HyperIdeal in conformallab++ AND for Gillespie 2021 |
|
||||
| **Gillespie, Springborn, Crane** — *Discrete Conformal Equivalence of Polyhedral Surfaces*, SIGGRAPH (2021) | geometry-central implementation; introduces Ptolemaic flips |
|
||||
| **Sharp, Soliman, Crane** — *Navigating Intrinsic Triangulations*, SIGGRAPH (2019) | geometry-central `SignpostIntrinsicTriangulation` — basis for GC-2 |
|
||||
| **Sechelmann** — doctoral thesis, TU Berlin (2016) | conformallab++ primary source; covers period matrix, holonomy, all three modes |
|
||||
@@ -1,251 +1,405 @@
|
||||
# Draft High-level Architecture
|
||||
# conformallab++ — Architecture & Pipeline
|
||||
|
||||
## Origin
|
||||
|
||||
## Positioning and relation to existing libraries (Nice To Have but maybe to much)
|
||||
ConformalLab++ is not intended to replace established geometry processing libraries such as CGAL, libigl, or Geometry Central. Instead, it acts as an experiment-friendly pipeline layer on top of existing data structures and algorithms provided by these libraries.
|
||||
conformallab++ is a C++ reimplementation of
|
||||
[ConformalLab](https://github.com/varylab/conformallab),
|
||||
the Java research library for discrete conformal geometry by
|
||||
**Stefan Sechelmann** (TU Berlin, Institut für Mathematik,
|
||||
SFB/Transregio 109 *Discretization in Geometry and Dynamics*).
|
||||
|
||||
The project revives the ideas and algorithms of the original ConformalLab by Stefan Sechelmann in a modern C++ setting, making them easier to combine with contemporary mesh libraries (e.g. Geometry Central for intrinsic quantities and operators) and to reuse in new experimental setups.
|
||||
The algorithmic foundation is his doctoral dissertation:
|
||||
|
||||
In practice, ConformalLab++ focuses on:
|
||||
> Stefan Sechelmann —
|
||||
> **Variational Methods for Discrete Surface Parameterization: Applications and Implementation**
|
||||
> Doctoral thesis, Technische Universität Berlin, 2016.
|
||||
> DOI: [10.14279/depositonce-5415](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f) · CC BY-SA 4.0
|
||||
|
||||
+ a clear, declarative pipeline description for conformal and Möbius-based mesh experiments,
|
||||
The dissertation develops the variational framework for discrete conformal equivalence:
|
||||
discrete uniformization of Riemann surfaces, cone metrics, period matrices, and
|
||||
holonomy — all of which are directly implemented in this library.
|
||||
|
||||
+ a unified halfedge-based UniversalMesh representation plus optional alternative representations,
|
||||
Further links:
|
||||
**Java original:** [github.com/varylab/conformallab](https://github.com/varylab/conformallab) ·
|
||||
**Website:** [sechel.de](https://sechel.de/) ·
|
||||
**LinkedIn:** [linkedin.com/in/sechel](https://www.linkedin.com/in/sechel/)
|
||||
|
||||
adapters and plugin units that wrap robust external implementations where appropriate (e.g. CGAL-based repair or reconstruction, Geometry Central–style discrete geometry operators, or libigl functionality) instead of reimplementing them.
|
||||
---
|
||||
|
||||
## Positioning
|
||||
|
||||
conformallab++ is a **specialised research library for discrete conformal geometry** on
|
||||
triangulated surfaces. It is not a general geometry-processing framework.
|
||||
|
||||
## High-level geometry pipeline
|
||||
The following diagram outlines the planned high-level pipeline for ConformalLab++. It shows the rough division into preprocessing, processing, and postprocessing. In preprocessing, various input types are first converted into a universal representation (half-edge mesh) via adapter layers and any necessary preprocessing steps. From there, they are processed by the various core processors units and then either exported or visualized in postprocessing, with minor optimizations as necessary.
|
||||
The library revives the algorithms of the original ConformalLab by Stefan Sechelmann
|
||||
in a modern C++ setting. Its core concern is one precise mathematical question:
|
||||
|
||||
> Given a triangulated surface, find a conformally equivalent metric that satisfies
|
||||
> prescribed curvature (angle-sum) constraints at each vertex.
|
||||
|
||||
Everything in the library serves this goal:
|
||||
|
||||
| What it is | What it is not |
|
||||
|------------|----------------|
|
||||
| Discrete conformal maps (Euclidean, spherical, hyperbolic) | General mesh processing |
|
||||
| Newton solver for angle-sum energy functionals | Remeshing / boolean / smoothing |
|
||||
| Priority-BFS layout into ℝ², S², Poincaré disk | Point-cloud or implicit-field processing |
|
||||
| Holonomy, period matrices, fundamental domains | NURBS / parametric modelling |
|
||||
| Research platform — experiment-first, CLI-ready | Production rendering engine |
|
||||
|
||||
**Relation to existing libraries.**
|
||||
conformallab++ sits *on top of* CGAL, not beside it.
|
||||
`CGAL::Surface_mesh` is the internal mesh representation; there is no additional
|
||||
abstraction layer. Eigen handles all linear algebra. libigl provides the optional
|
||||
interactive viewer. The library adds the conformal-geometry layer that none of these
|
||||
provide.
|
||||
|
||||
---
|
||||
|
||||
## The conformal geometry pipeline
|
||||
|
||||
The full pipeline runs in three stages. All stages operate on the same
|
||||
`ConformalMesh` (= `CGAL::Surface_mesh<Point3>` with attached property maps).
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A1["Explicit<br/>(OBJ, PLY, STL)"]
|
||||
A2["Implicit<br/>(SDF, Formulas)"]
|
||||
A3["Parametric<br/>(NURBS, Curves)"]
|
||||
A4["Procedural<br/>(Noise, L-Sys)"]
|
||||
ADAPTER1["Input Adapter <br/>(Convert to Universal Represenation)"]
|
||||
PREPROCESSING["Preprocessor <br/>(refined triangulation etc.)"]
|
||||
EXT["EXTERNAL LIBRARIES<br/>(Plugin System)<br/>- CGAL"]
|
||||
UNIVERSAL["UNIVERSAL REPRESENTATION FORM<br/>(HalfEdge Mesh Interface)"]
|
||||
EXPORT_ADAPTER["Export Adapter Layer"]
|
||||
PROCESSING["PROCESSING CORE LAYER"]
|
||||
OPT["Optimization<br/>- Decimation<br/>- Denoising<br/>- Remeshing"]
|
||||
ALGO["Algorithmic<br/>- Boolean Ops<br/>- Smoothing<br/>- Hole Filling"]
|
||||
OTHER["Other<br/>- Custom Algos<br/>- Transforms<br/>- Filters"]
|
||||
POSTPROCESSING["POSTPROCESSING <br/>(refined triangulation etc.)"]
|
||||
EXPORT["Export Formats<br/>- OBJ<br/>- PLY<br/>- STL<br/>- Custom"]
|
||||
VIZ["Visualization<br/>- OpenGL<br/>- QT<br/>- Screenshot"]
|
||||
subgraph PRE[PREPROCESSING]
|
||||
A1 --> ADAPTER1
|
||||
A2 --> ADAPTER1
|
||||
A3 --> ADAPTER1
|
||||
A4 --> ADAPTER1
|
||||
PREPROCESSING <-->ADAPTER1
|
||||
end
|
||||
EXT <--> UNIVERSAL
|
||||
UNIVERSAL --> PROCESSING
|
||||
UNIVERSAL --> EXPORT_ADAPTER
|
||||
ADAPTER1 <--> EXT
|
||||
ADAPTER1 <--> UNIVERSAL
|
||||
OPT --> UNIVERSAL
|
||||
ALGO --> UNIVERSAL
|
||||
OTHER --> UNIVERSAL
|
||||
subgraph CORE[PROCESSING ]
|
||||
PROCESSING --> OPT
|
||||
PROCESSING --> ALGO
|
||||
PROCESSING --> OTHER
|
||||
end
|
||||
subgraph POST[POSTPROCESSING]
|
||||
EXPORT_ADAPTER <--> POSTPROCESSING
|
||||
EXPORT_ADAPTER --> EXPORT
|
||||
EXPORT_ADAPTER --> VIZ
|
||||
end
|
||||
style UNIVERSAL fill:#90EE90,stroke:#000,stroke-width:3px,color:#000
|
||||
style PREPROCESSING fill:#87CEEB,stroke:#000,stroke-width:2px,color:#000
|
||||
style POSTPROCESSING fill:#87CEEB,stroke:#000,stroke-width:2px,color:#000
|
||||
style PROCESSING fill:#87CEEB,stroke:#000,stroke-width:2px,color:#000
|
||||
style EXT fill:#DDA0DD,stroke:#000,stroke-width:2px,color:#000
|
||||
style ADAPTER1 fill:#FFB347,stroke:#000,stroke-width:2px,color:#000
|
||||
style EXPORT_ADAPTER fill:#FFB347,stroke:#000,stroke-width:2px,color:#000
|
||||
IN["Input\n(OFF / OBJ / PLY / builder)"]
|
||||
ADAPTER["Input Adapter\nload_mesh() · make_triangle() · make_quad_strip() …"]
|
||||
|
||||
subgraph PRE["① PREPROCESSING"]
|
||||
MAPS["Maps Setup\nsetup_*_maps() + compute_lambda0_from_mesh()"]
|
||||
DOF["DOF Assignment\nv_idx[v] · e_idx[e] · pin / free"]
|
||||
THETA["Target Angles\ntheta_v[v] — cone metric or natural equilibrium"]
|
||||
GB["Gauss–Bonnet Check\ncheck_gauss_bonnet() / enforce_gauss_bonnet()"]
|
||||
MAPS --> DOF --> THETA --> GB
|
||||
end
|
||||
|
||||
subgraph CORE["② PROCESSING CORE"]
|
||||
NEWTON["Newton Solver\nnewton_euclidean/spherical/hyper_ideal()\n→ NewtonResult x* ∈ ℝⁿ"]
|
||||
CG["Cut Graph [closed surfaces]\ncompute_cut_graph()\n→ CutGraph (2g seam edges)"]
|
||||
LAYOUT["Layout / Embedding\neuclidean/spherical/hyper_ideal_layout()\n→ Layout2D / Layout3D\n · uv[v] · halfedge_uv[h]\n · HolonomyData"]
|
||||
NORM["Normalisation\nnormalise_euclidean / _hyperbolic / _spherical()"]
|
||||
PERIOD["Period Matrix [genus 1]\ncompute_period_matrix()\n→ PeriodData τ ∈ ℍ"]
|
||||
FD["Fundamental Domain\ncompute_fundamental_domain()\n→ FundamentalDomain + tiling"]
|
||||
NEWTON --> CG --> LAYOUT --> NORM --> PERIOD --> FD
|
||||
end
|
||||
|
||||
subgraph POST["③ POSTPROCESSING"]
|
||||
SERIAL["Serialisation\nsave_result_json/xml()\nsave_layout_off()"]
|
||||
VIZ["Visualisation\nexample_viewer (libigl / GLFW)"]
|
||||
CLI["CLI App\nconformallab_core -i -g -o -j -x -s"]
|
||||
end
|
||||
|
||||
IN --> ADAPTER --> PRE
|
||||
PRE --> CORE
|
||||
CORE --> POST
|
||||
|
||||
style PRE fill:#dbeafe,stroke:#3b82f6,stroke-width:2px,color:#000
|
||||
style CORE fill:#dcfce7,stroke:#16a34a,stroke-width:2px,color:#000
|
||||
style POST fill:#fef9c3,stroke:#ca8a04,stroke-width:2px,color:#000
|
||||
```
|
||||
All external inputs (file-based, implicit, parametric, procedural) are first converted by an spezfic input adapter into a single universal interface, which is the canonical internal representation used by the processing stages. The processing core units operates on this universal representation and can use external libraries via a plugin-like extension, while results are passed through an export adapter to file formats or visualization frontends.
|
||||
|
||||
---
|
||||
|
||||
## Three-stage processing pipeline
|
||||
The geometry processing pipeline in ConformalLab++ is structured into three main stages, all operating on the same universal mesh representation.
|
||||
## Stage ① — Preprocessing
|
||||
|
||||
### Preprocessing
|
||||
Converts all external inputs (files, analytic models, procedural sources) into the canonical half-edge mesh form.
|
||||
### Input adapter
|
||||
|
||||
Performs optional cleaning and normalization (e.g. fixing degeneracies, rescaling, enforcing orientation).
|
||||
All mesh input flows through a single entry point:
|
||||
|
||||
Guarantees that downstream stages see a well-formed, consistent mesh (or fail early with clear errors).
|
||||
```cpp
|
||||
ConformalMesh mesh = load_mesh("input.off"); // OFF · OBJ · PLY
|
||||
ConformalMesh mesh = make_quad_strip(); // built-in test meshes
|
||||
```
|
||||
|
||||
### Processing (core processing units)
|
||||
**Precondition guarantee:** the adapter ensures the mesh is a valid, orientable,
|
||||
triangulated surface with consistent halfedge structure (CGAL validity).
|
||||
Downstream stages never check for degeneracies — they trust the adapter.
|
||||
|
||||
Runs one or more core processing units in sequence on the canonical mesh.
|
||||
### Maps setup
|
||||
|
||||
Each unit takes a mesh of the same type as input and produces a mesh of the same type as output (possibly with enriched attributes).
|
||||
Each geometry mode has its own maps struct that attaches property maps to the mesh:
|
||||
|
||||
Examples: remeshing, conformal parameterization, smoothing, boolean operations, experiment-specific transforms.
|
||||
| Geometry | Setup function | Key property maps |
|
||||
|----------|---------------|-------------------|
|
||||
| Euclidean (ℝ²) | `setup_euclidean_maps()` | `lambda0[e]`, `theta_v[v]`, `v_idx[v]` |
|
||||
| Spherical (S²) | `setup_spherical_maps()` | `lambda0[e]`, `theta_v[v]`, `v_idx[v]` |
|
||||
| Hyper-ideal (H²) | `setup_hyper_ideal_maps()` | `beta_v[v]`, `alpha_e[e]`, `v_idx[v]`, `e_idx[e]` |
|
||||
|
||||
Each core processing unit conceptually has the shape UniversalMesh -> UniversalMesh, but units also declare:
|
||||
`compute_*_lambda0_from_mesh()` initialises log-edge-lengths from the 3-D vertex
|
||||
positions. After this step the solver works entirely in scale-factor space; the
|
||||
original vertex positions are no longer needed.
|
||||
|
||||
+ Preconditions: requirements on the input mesh (e.g. triangulated, manifold, oriented, specific attributes present).
|
||||
### DOF assignment & target angles
|
||||
|
||||
+ Capabilities: guarantees on the output mesh (e.g. remains manifold, produces curvature attributes, preserves boundary).
|
||||
```cpp
|
||||
// Pin first vertex (gauge fix for open meshes)
|
||||
maps.v_idx[*mesh.vertices().begin()] = -1;
|
||||
int idx = 0;
|
||||
for (auto v : rest_of_vertices) maps.v_idx[v] = idx++;
|
||||
|
||||
A simple example:
|
||||
// Set target curvature (cone metric or natural equilibrium)
|
||||
maps.theta_v[v] = 2 * M_PI; // flat interior vertex
|
||||
maps.theta_v[v] = M_PI / 3; // 60° cone singularity
|
||||
```
|
||||
|
||||
+ RemeshingUnit
|
||||
**Natural equilibrium shortcut:** evaluate the gradient at `x = 0`, subtract it from
|
||||
`theta_v` — the solver then converges to `x* = 0` identically (useful for tests).
|
||||
|
||||
+ Preconditions: triangulated, manifold, oriented
|
||||
### Gauss–Bonnet check
|
||||
|
||||
+ Capabilities: triangulated, manifold, vertex positions modified, edge count changed
|
||||
|
||||
Pipeline composition is valid if, for every adjacent pair of units, the capabilities of the previous unit satisfy the preconditions of the next unit.
|
||||
Before solving, the prescribed angles must satisfy:
|
||||
|
||||
$$\sum_{v} (2\pi - \Theta_v) = 2\pi \cdot \chi(M)$$
|
||||
|
||||
### Postprocessing
|
||||
```cpp
|
||||
check_gauss_bonnet(mesh, maps); // throws if violated
|
||||
enforce_gauss_bonnet(mesh, maps); // distributes residual uniformly
|
||||
```
|
||||
|
||||
Adapts the processed mesh for external consumption: export to file formats, visualization, analysis tools.
|
||||
**This is the most common source of silent Newton non-convergence.**
|
||||
Prescribing angles that violate Gauss–Bonnet means no conformal factor exists —
|
||||
the solver will iterate without converging.
|
||||
|
||||
May attach or convert attribute data (e.g. colors, scalar fields, experiment results) into a format suitable for rendering or further tools.
|
||||
---
|
||||
|
||||
Does not change the core topology or semantics of the processed mesh, only its representation for the outside world.
|
||||
## Stage ② — Processing core
|
||||
|
||||
This structure makes it easy to reason about where data enters, is modified, and leaves the system, and keeps experimental algorithms clearly separated from IO concerns.
|
||||
### The three geometry modes
|
||||
|
||||
## Role of the universal mesh representation
|
||||
The universal mesh representation (a half-edge mesh interface) is the shared contract between all pipeline stages.
|
||||
conformallab++ implements discrete conformal geometry in three model spaces:
|
||||
|
||||
### Single input/output type
|
||||
Every core processing unit exposes the same function shape conceptually:
|
||||
UniversalMesh → UniversalMesh.
|
||||
This allows units to be chained in arbitrary order as long as their preconditions on the mesh are satisfied.
|
||||
| Mode | Space | Curvature | Typical surfaces |
|
||||
|------|-------|-----------|-----------------|
|
||||
| **Euclidean** | ℝ² | K = 0 | Flat tori, developable surfaces, open patches |
|
||||
| **Spherical** | S² | K = +1 | Genus-0 (sphere-like) surfaces |
|
||||
| **Hyper-ideal** | H² (Poincaré disk) | K = −1 | Genus-g surfaces (g ≥ 1), hyperbolic structures |
|
||||
|
||||
### Local, topology-aware access
|
||||
The half-edge structure encodes vertices, halfedges, faces, and their adjacency relations, which is essential for typical geometry operations (e.g. local neighborhood queries, traversal, edge flips).
|
||||
All three share the same algorithmic structure — only the angle formula, the
|
||||
trilateration geometry, and the Hessian sign differ.
|
||||
|
||||
Algorithms do not need to know where the mesh came from (file vs. analytic vs. procedural); they only rely on this uniform interface.
|
||||
### Newton solver
|
||||
|
||||
### Extensibility via attributes
|
||||
The universal mesh may carry extensible per-vertex, per-edge, and per-face attributes (e.g. UVs, curvature, experimental scalar fields).
|
||||
Core units can read and write these attributes while still conforming to the same mesh type, enabling complex pipelines without changing the central data structure.
|
||||
```
|
||||
NewtonResult newton_euclidean (mesh, x0, maps [, tol, max_iter])
|
||||
NewtonResult newton_spherical (mesh, x0, maps [, tol, max_iter])
|
||||
NewtonResult newton_hyper_ideal(mesh, x0, maps [, tol, max_iter, hess_eps])
|
||||
```
|
||||
|
||||
Each iteration:
|
||||
1. Evaluate gradient **G** (angle-sum defect per vertex)
|
||||
2. Evaluate Hessian **H** (analytical for Euclidean/Spherical; symmetric FD for HyperIdeal)
|
||||
3. Solve **H·Δx = −G** — try `SimplicialLDLT`, fall back to `SparseQR` on rank deficiency
|
||||
4. Backtracking line search (up to 20 halvings)
|
||||
|
||||
**Preconditions:** mesh triangulated + manifold · Gauss–Bonnet satisfied · DOFs assigned
|
||||
**Provides:** `NewtonResult.x` — converged scale factors; `converged`, `iterations`, `grad_inf_norm`
|
||||
|
||||
Because all processing units speak the same “mesh language”, you can build pipelines like:
|
||||
**SparseQR fallback** handles gauge modes on closed meshes without a pinned vertex.
|
||||
The fallback is public API: `solve_linear_system(H, rhs, &used_fallback)`.
|
||||
|
||||
### Cut graph (closed surfaces)
|
||||
|
||||
For a closed genus-g surface, cutting along 2g independent homology cycles
|
||||
turns it into a topological disk — a prerequisite for globally consistent BFS layout.
|
||||
|
||||
```cpp
|
||||
CutGraph cg = compute_cut_graph(mesh);
|
||||
// cg.cut_edge_flags[e.idx()] — true for seam edges
|
||||
// cg.cut_edge_indices — ordered list of 2g seam edges
|
||||
// cg.genus — g
|
||||
```
|
||||
|
||||
**Algorithm:** tree-cotree decomposition (Erickson–Whittlesey 2005).
|
||||
**Preconditions:** closed, orientable, triangulated mesh
|
||||
**Provides:** exactly `2g` seam edges whose removal makes the surface simply connected
|
||||
|
||||
### Layout / Embedding
|
||||
|
||||
BFS-trilateration unfolds the mesh into the target geometry.
|
||||
The root face (largest 3-D area, 1.5× interior bonus) is placed first;
|
||||
all other faces are placed in **priority order by BFS depth** (min-heap),
|
||||
minimising trilateration error accumulation.
|
||||
|
||||
```cpp
|
||||
HolonomyData hol;
|
||||
Layout2D layout = euclidean_layout(mesh, result.x, maps, &cg, &hol, /*normalise=*/true);
|
||||
Layout3D slayout = spherical_layout(mesh, result.x, smaps);
|
||||
Layout2D hlayout = hyper_ideal_layout(mesh, result.x, hmaps, &cg, &hol);
|
||||
```
|
||||
|
||||
**Key outputs:**
|
||||
|
||||
| Field | Content |
|
||||
|-------|---------|
|
||||
| `layout.uv[v.idx()]` | Primary UV — first / shallowest-BFS visit |
|
||||
| `layout.halfedge_uv[h.idx()]` | UV of `source(h)` as seen from `face(h)` — seam-aware |
|
||||
| `layout.has_seam` | True when a vertex was reached via two different paths |
|
||||
| `hol.translations[i]` | Translation ω_i across cut edge i (Euclidean / spherical) |
|
||||
| `hol.mobius_maps[i]` | Möbius isometry T_i ∈ SU(1,1) across cut edge i (hyperbolic) |
|
||||
|
||||
**`halfedge_uv` — texture atlas semantics:**
|
||||
At a seam edge the two opposite halfedges carry *different* UV values.
|
||||
This gives each face its own UV copy of a seam vertex, enabling
|
||||
a proper GPU texture atlas without vertex duplication.
|
||||
|
||||
**Trilateration — geometry by mode:**
|
||||
|
||||
| Mode | Method | Accuracy |
|
||||
|------|--------|---------|
|
||||
| Euclidean | Analytic formula in ℝ² | Exact |
|
||||
| Spherical | Spherical law of cosines on S² | Exact |
|
||||
| Hyper-ideal | Möbius + hyperbolic law of cosines in Poincaré disk | Exact |
|
||||
|
||||
**Preconditions:** `NewtonResult.converged` · mesh · maps · optional `CutGraph`
|
||||
**Provides:** `Layout2D/3D` with `uv`, `halfedge_uv` · `HolonomyData` with translations / Möbius maps
|
||||
|
||||
### Möbius maps
|
||||
|
||||
`MobiusMap` (T(z) = (az+b)/(cz+d)) is the central algebraic object for hyperbolic geometry:
|
||||
|
||||
```cpp
|
||||
MobiusMap T = MobiusMap::from_three(z1,w1, z2,w2, z3,w3); // fit to 3 correspondences
|
||||
MobiusMap S = T.inverse().compose(U); // group operations
|
||||
Eigen::Vector2d p2 = T.apply(p); // apply to 2-D point
|
||||
```
|
||||
|
||||
Used for: hyperbolic trilateration · holonomy tracking · normalisation centering.
|
||||
|
||||
### Normalisation
|
||||
|
||||
After layout, a canonical post-processing step brings the result into a standard position:
|
||||
|
||||
| Mode | Method | Effect |
|
||||
|------|--------|--------|
|
||||
| Euclidean | PCA — centroid → origin, major axis → x-axis | Translation + rotation |
|
||||
| Hyperbolic | Weighted Möbius centering (Fréchet mean, 30 iterations) | Maps centroid to disk origin |
|
||||
| Spherical | Rodrigues rotation | Maps centroid to north pole |
|
||||
|
||||
Both `uv` and `halfedge_uv` are transformed identically.
|
||||
|
||||
### Period matrix (genus 1)
|
||||
|
||||
From the two holonomy translations ω₁, ω₂ ∈ ℂ read off from the cut graph,
|
||||
the conformal type of a flat torus is the SL(2,ℤ)-orbit of:
|
||||
|
||||
$$\tau = \omega_2 / \omega_1 \in \mathbb{H}$$
|
||||
|
||||
```cpp
|
||||
PeriodData pd = compute_period_matrix(hol);
|
||||
// pd.tau — complex period ratio
|
||||
// pd.omega[i] — lattice generators as complex numbers
|
||||
// pd.in_fundamental_domain — after SL(2,ℤ) reduction
|
||||
// pd.genus() — g = omega.size() / 2
|
||||
```
|
||||
|
||||
SL(2,ℤ) reduction alternates T: τ↦τ+1 and S: τ↦−1/τ steps until
|
||||
τ ∈ F = {|τ| ≥ 1, −½ ≤ Re(τ) < ½}.
|
||||
|
||||
**Note:** The Siegel period matrix Ω ∈ H_g for genus g ≥ 2 requires integrating
|
||||
holomorphic differentials — deferred to Phase 8.
|
||||
|
||||
### Fundamental domain
|
||||
|
||||
```cpp
|
||||
FundamentalDomain fd = compute_fundamental_domain(hol);
|
||||
// genus 1: CCW parallelogram {0, ω₁, ω₁+ω₂, ω₂}
|
||||
// genus g > 1: empty — 4g-polygon boundary walk deferred to Phase 8
|
||||
|
||||
auto tiles = tiling_neighbourhood(layout, hol, /*m_max=*/2, /*n_max=*/2);
|
||||
// returns (2·m_max+1)·(2·n_max+1) translated copies of the layout
|
||||
// for visualising the universal cover
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Stage ③ — Postprocessing
|
||||
|
||||
### Serialisation
|
||||
|
||||
```cpp
|
||||
// Layout as mesh file
|
||||
save_layout_off("layout.off", mesh, layout);
|
||||
|
||||
// Full result: DOF vector + metadata + layout UVs
|
||||
save_result_json("result.json", result, "euclidean", V, F, &layout);
|
||||
save_result_xml ("result.xml", result, "euclidean", V, F, &layout);
|
||||
|
||||
// Round-trip load
|
||||
NewtonResult res2; std::string geom; Layout2D uv2;
|
||||
load_result_json("result.json", &res2, &geom, &uv2);
|
||||
```
|
||||
|
||||
### CLI app
|
||||
|
||||
```bash
|
||||
Preprocessing
|
||||
-> RemeshingUnit
|
||||
-> ConformalMapUnit
|
||||
-> ExperimentSpecificFilter
|
||||
-> Export/Postprocessing
|
||||
conformallab_core \
|
||||
-i input.off # input mesh
|
||||
-g euclidean # geometry: euclidean | spherical | hyper_ideal
|
||||
-o layout.off # layout output
|
||||
-j result.json # JSON serialisation
|
||||
-x result.xml # XML serialisation
|
||||
-s # show input in viewer
|
||||
-v # verbose solver output
|
||||
```
|
||||
without changing the basic function signature or the surrounding infrastructure, only by reordering or swapping units.
|
||||
|
||||
### Interactive viewer
|
||||
|
||||
## Declarative pipeline descriptions (Nice To Have but maybe to much)
|
||||
ConformalLab++ optionally supports a lightweight YAML-based description format for experiments.
|
||||
A pipeline consists of an input adapter, a sequence of steps (core processing units), and an output adapter. Each step can specify params as well as structural constraints via require and provide keys. A pipeline is considered valid if, for every step, all require conditions are satisfied by the accumulated provide and expect constraints of previous steps
|
||||
`example_viewer` (libigl / GLFW) shows the 3-D mesh and the 2-D layout
|
||||
side-by-side. Built automatically with `-DWITH_CGAL=ON`.
|
||||
|
||||
```yaml
|
||||
---
|
||||
|
||||
pipeline:
|
||||
name: basic_conformal
|
||||
input:
|
||||
adapter: obj_reader
|
||||
source: data/bunny.obj
|
||||
## Processing unit contracts
|
||||
|
||||
steps:
|
||||
- id: clean
|
||||
unit: repair_soft_clean
|
||||
params:
|
||||
mode: soft
|
||||
require:
|
||||
representation: UniversalMesh
|
||||
triangulated: true
|
||||
provide:
|
||||
triangulated: true
|
||||
manifold: null
|
||||
Each stage has explicit preconditions and guarantees.
|
||||
A pipeline is valid if every unit's preconditions are satisfied
|
||||
by the outputs of all preceding units.
|
||||
|
||||
- id: param
|
||||
unit: conformal_parameterization
|
||||
params:
|
||||
method: cauchy_riemann
|
||||
require:
|
||||
triangulated: true
|
||||
manifold: true
|
||||
provide:
|
||||
attributes_add: [uv]
|
||||
| Unit | Preconditions | Provides |
|
||||
|------|--------------|---------|
|
||||
| `load_mesh` | Valid file path, supported format | Manifold, oriented, triangulated `ConformalMesh` |
|
||||
| `setup_*_maps` | Triangulated mesh | Initialised property maps; `lambda0` from 3-D positions |
|
||||
| `check_gauss_bonnet` | `theta_v` set | Throws if Σ(2π−Θ_v) ≠ 2π·χ |
|
||||
| `enforce_gauss_bonnet` | `theta_v` set | Σ(2π−Θ_v) = 2π·χ guaranteed |
|
||||
| `newton_*` | GB satisfied · DOFs assigned | `NewtonResult.converged` · `x*` · gradient norm |
|
||||
| `compute_cut_graph` | Closed, orientable mesh | `2g` seam edges · `CutGraph.genus` |
|
||||
| `euclidean_layout` | `newton_euclidean` converged | `uv[v]` · `halfedge_uv[h]` · `HolonomyData` |
|
||||
| `normalise_euclidean` | `layout.success == true` | Centroid at origin · major axis = x-axis |
|
||||
| `compute_period_matrix` | `hol.translations.size() >= 2` | `τ ∈ ℍ` · optionally reduced to F |
|
||||
| `compute_fundamental_domain` | `HolonomyData` (genus 1) | CCW parallelogram · edge identifications |
|
||||
|
||||
output:
|
||||
adapter: gltf_writer
|
||||
target: out/bunny.glb
|
||||
---
|
||||
|
||||
```
|
||||
Each unit may declare a require section describing what it needs from its input (e.g. representation, triangulated, manifold, required attributes) and a provide section describing what it guarantees on its output (e.g. still triangulated, now manifold, adds a uv attribute). Pipelines are considered valid if for every step, the accumulated provide information of all previous steps satisfies the require constraints of the next step.
|
||||
## The three geometry modes in detail
|
||||
|
||||
### Repair Units (Nice To Have but maybe to much)
|
||||
```
|
||||
Euclidean Spherical Hyper-ideal
|
||||
─────────────────────────────────────────────────────
|
||||
Space ℝ² S² H² (Poincaré disk)
|
||||
Curvature K = 0 K = +1 K = −1
|
||||
Genus any (cone metrics) 0 ≥ 1
|
||||
Angle sum Σα_v = Θ_v Σα_v = Θ_v Σβ_v = Θ_v
|
||||
Hessian PSD (cotangent-Lap.) NSD (sign-flip) PSD (FD, strict conv.)
|
||||
Holonomy translations ω_i rotations (2-D) Möbius maps T_i ∈ SU(1,1)
|
||||
Period τ = ω₂/ω₁ ∈ ℍ — axis of T_i
|
||||
Normalise PCA centring Rodrigues to N pole weighted Möbius centring
|
||||
```
|
||||
|
||||
Before entering the core conformal pipeline, input meshes are passed through an explicit preprocessing stage. This stage is responsible for turning “real-world” polygon data (often noisy, inconsistent, or non‑manifold) into a mesh that satisfies the structural requirements of the subsequent units
|
||||
|
||||
ConformalLab++ provides a small set of repair units that can be combined as needed:
|
||||
|
||||
+ removal of (almost) degenerate triangles (needles, caps),
|
||||
|
||||
+ stitching of compatible boundary cycles to close small gaps,
|
||||
|
||||
+ enforcing a consistent face orientation where possible.
|
||||
|
||||
These units are conceptually similar to geometric repair routines in modern polygon mesh processing toolkits
|
||||
|
||||
We distinguish between two typical modes of operation:
|
||||
|
||||
+ Soft cleaning: minimally invasive repairs intended for visualization and quick experiments. Soft cleaning tries to improve mesh quality without significantly altering topology or removing components.
|
||||
|
||||
+ Hard cleaning: more aggressive repairs intended for numerically robust experiments. Hard cleaning may merge vertices, remove tiny components, and modify local topology to enforce manifoldness and consistent orientation when possible.
|
||||
|
||||
## More internal representations (Nice To Have but maybe to much)
|
||||
ConformalLab++ distinguishes between internal representation spaces that experiments may move between:
|
||||
|
||||
+ UniversalMesh: a halfedge‑based surface mesh representation used for most combinatorial and conformal algorithms.
|
||||
|
||||
+ PointCloud: an unstructured set of sample points, optionally with normals and additional attributes, used for sampling, resampling, and certain reconstruction tasks.
|
||||
|
||||
+ ImplicitField: a scalar field $f: \mathbf{R}^n \rightarrow \mathbf{R}$
|
||||
represented on a grid or as a procedural function, used for iso‑surface extraction and robust shape transformations.
|
||||
|
||||
Each space has its own natural algorithms, and dedicated conversion units allow pipelines to move between these representations when needed.
|
||||
|
||||
### Conversion units
|
||||
|
||||
Typical conversion units include:
|
||||
|
||||
+ mesh_to_pointcloud (sampling vertices and surfaces of a UniversalMesh into a PointCloud),
|
||||
|
||||
+ pointcloud_to_mesh (surface reconstruction from point samples),
|
||||
|
||||
+ mesh_to_implicit (rasterizing a UniversalMesh into a signed distance or occupancy field),
|
||||
|
||||
+ implicit_to_mesh (iso‑surface extraction from an ImplicitField).
|
||||
|
||||
These units are modeled as regular processing units in the pipeline but change the underlying representation space instead of just transforming a mesh
|
||||
|
||||
## Attribute behaviour under topology changes (Nice To Have but maybe to much)
|
||||
|
||||
ConformalLab++ treats attributes as first-class data attached to mesh entities (vertices, edges, faces, halfedges). When topology changes, attributes are updated according to simple, explicit rules:
|
||||
|
||||
+ Vertex splits / edge splits: attributes on newly created vertices or edges are initialized by interpolation of the incident elements (e.g. linear interpolation of scalar fields along an edge).
|
||||
|
||||
+ Edge collapse: attributes on the surviving vertex are computed from the incident vertices, typically by a convex combination or area‑weighted average for scalar quantities.
|
||||
|
||||
+ Face operations (flip, subdivision): face attributes are either propagated (for categorical data) or interpolated (for scalar data) to new faces.
|
||||
|
||||
By default, ConformalLab++ uses linear interpolation for scalar attributes and nearest‑neighbour propagation for discrete or categorical attributes; units may override these policies where necessary
|
||||
---
|
||||
|
||||
## Further documentation
|
||||
|
||||
| Topic | Document |
|
||||
|---|---|
|
||||
| Public headers (all 24, with descriptions) | [api/headers.md](../api/headers.md) |
|
||||
| Test suites (26 suites, 158 tests, individual counts) | [api/tests.md](../api/tests.md) |
|
||||
| Extending the library (new functionals, geometry modes, Java porting guide) | [api/extending.md](../api/extending.md) |
|
||||
| Processing unit contracts (preconditions / provides table) | [api/contracts.md](../api/contracts.md) |
|
||||
| Phase 8 CGAL package design + declarative pipeline YAML | [api/cgal-package.md](../api/cgal-package.md) |
|
||||
| Key design decisions + rationale | [architecture/design-decisions.md](design-decisions.md) |
|
||||
| Project structure (directory tree + build targets) | [architecture/project-structure.md](project-structure.md) |
|
||||
| Three geometry modes in detail | [math/geometry-modes.md](../math/geometry-modes.md) |
|
||||
| References and papers | [math/references.md](../math/references.md) |
|
||||
| Development roadmap (Phases 1–10) | [roadmap/phases.md](../roadmap/phases.md) |
|
||||
| Java vs. C++ feature parity table | [roadmap/java-parity.md](../roadmap/java-parity.md) |
|
||||
|
||||
115
doc/architecture/project-structure.md
Normal file
115
doc/architecture/project-structure.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# Project Structure
|
||||
|
||||
```
|
||||
ConformalLabpp/
|
||||
├── CLAUDE.md # Claude Code context file
|
||||
├── README.md # Entry point — what/why, quick start, doc index
|
||||
├── LICENSE # MIT
|
||||
│
|
||||
├── code/ # All C++ source
|
||||
│ ├── CMakeLists.txt # Root: three build modes (default / CGAL_TESTS / CGAL)
|
||||
│ │
|
||||
│ ├── include/ # Header-only library — all algorithms here
|
||||
│ │ ├── conformal_mesh.hpp # ConformalMesh = CGAL::Surface_mesh<Point3>
|
||||
│ │ ├── constants.hpp # conformallab::PI, TWO_PI
|
||||
│ │ ├── clausen.hpp # Cl₂, Lobachevsky Л, ImLi₂
|
||||
│ │ ├── hyper_ideal_geometry.hpp # ζ₁₃/₁₄/₁₅, lᵢⱼ, αᵢⱼ, σᵢ, σᵢⱼ
|
||||
│ │ ├── hyper_ideal_utility.hpp # Tetrahedron volumes (Meyerhoff / Kolpakov–Mednykh)
|
||||
│ │ ├── hyper_ideal_visualization_utility.hpp # Poincaré disk projection, circumcircle helpers
|
||||
│ │ ├── hyper_ideal_functional.hpp # HyperIdeal energy + gradient on ConformalMesh
|
||||
│ │ ├── hyper_ideal_hessian.hpp # HyperIdeal Hessian (symmetric FD, Phase 9b: analytic)
|
||||
│ │ ├── spherical_geometry.hpp # Spherical arc-length, half-angle formula
|
||||
│ │ ├── spherical_functional.hpp # Spherical energy + gradient + gauge-fix
|
||||
│ │ ├── spherical_hessian.hpp # Spherical Hessian (∂α/∂u via law of cosines)
|
||||
│ │ ├── euclidean_geometry.hpp # Euclidean corner angle (t-value / atan2)
|
||||
│ │ ├── euclidean_functional.hpp # Euclidean energy + gradient
|
||||
│ │ ├── euclidean_hessian.hpp # Cotangent Laplacian Hessian (Pinkall–Polthier)
|
||||
│ │ ├── newton_solver.hpp # newton_{euclidean,spherical,hyper_ideal} + solve_linear_system
|
||||
│ │ ├── gauss_bonnet.hpp # euler_characteristic, genus, check/enforce_gauss_bonnet
|
||||
│ │ ├── cut_graph.hpp # CutGraph + compute_cut_graph (tree-cotree)
|
||||
│ │ ├── layout.hpp # Priority-BFS layout, MobiusMap, halfedge_uv, HolonomyData
|
||||
│ │ ├── mesh_builder.hpp # make_triangle / make_tetrahedron / make_quad_strip / make_fan
|
||||
│ │ ├── mesh_io.hpp # load_mesh / save_mesh (OFF/OBJ/PLY via CGAL::IO)
|
||||
│ │ ├── mesh_utils.hpp # CGAL → Eigen conversion (cgal_to_eigen)
|
||||
│ │ ├── serialization.hpp # save/load_result_json + save/load_result_xml
|
||||
│ │ ├── period_matrix.hpp # PeriodData, compute_period_matrix, SL(2,ℤ) reduction
|
||||
│ │ └── fundamental_domain.hpp # FundamentalDomain, tiling_copy, tiling_neighbourhood
|
||||
│ │
|
||||
│ ├── src/
|
||||
│ │ ├── apps/v0/conformallab_cli.cpp # CLI app (Phase 5): load → solve → layout → export
|
||||
│ │ └── viewer/simple_viewer.cpp # libigl/GLFW viewer wrapper
|
||||
│ │
|
||||
│ ├── examples/ # Standalone example programs (require WITH_CGAL=ON)
|
||||
│ │ ├── example_euclidean.cpp # Euclidean pipeline end-to-end
|
||||
│ │ ├── example_hyper_ideal.cpp # HyperIdeal pipeline end-to-end
|
||||
│ │ ├── example_layout.cpp # Full pipeline: solve → layout → OFF/JSON/XML + round-trip
|
||||
│ │ └── example_viewer.cpp # Interactive libigl viewer
|
||||
│ │
|
||||
│ ├── tests/
|
||||
│ │ ├── CMakeLists.txt
|
||||
│ │ ├── test_clausen.cpp
|
||||
│ │ ├── test_hyper_ideal_utility.cpp
|
||||
│ │ ├── test_matrix_utility.cpp
|
||||
│ │ ├── test_surface_curve_utility.cpp
|
||||
│ │ ├── test_discrete_elliptic_utility.cpp
|
||||
│ │ ├── test_p2_utility.cpp
|
||||
│ │ ├── test_hyper_ideal_visualization_utility.cpp
|
||||
│ │ └── cgal/ # CGAL test suite (WITH_CGAL_TESTS or WITH_CGAL)
|
||||
│ │ ├── test_conformal_mesh.cpp
|
||||
│ │ ├── test_hyper_ideal_functional.cpp
|
||||
│ │ ├── test_spherical_functional.cpp
|
||||
│ │ ├── test_euclidean_functional.cpp
|
||||
│ │ ├── test_euclidean_hessian.cpp
|
||||
│ │ ├── test_spherical_hessian.cpp
|
||||
│ │ ├── test_newton_solver.cpp
|
||||
│ │ ├── test_mesh_io.cpp
|
||||
│ │ ├── test_pipeline.cpp
|
||||
│ │ ├── test_layout.cpp
|
||||
│ │ ├── test_phase6.cpp
|
||||
│ │ ├── test_phase7.cpp
|
||||
│ │ └── test_geometry_utils.cpp
|
||||
│ │
|
||||
│ └── deps/ # All dependencies as bundled tarballs
|
||||
│ ├── tarballs/
|
||||
│ │ ├── eigen-3.4.0.tar.gz
|
||||
│ │ ├── CGAL-6.1.1.tar.xz
|
||||
│ │ ├── libigl-2.6.0.tar.gz
|
||||
│ │ ├── libigl-glad.tar.gz
|
||||
│ │ └── glfw-3.4.tar.gz
|
||||
│ └── single_includes/ # CLI11.hpp, json.hpp (header-only)
|
||||
│
|
||||
├── doc/
|
||||
│ ├── getting-started.md
|
||||
│ ├── contributing.md
|
||||
│ ├── api/
|
||||
│ │ ├── pipeline.md # Full pipeline API with code examples
|
||||
│ │ ├── extending.md # New functionals, geometry modes, Java porting
|
||||
│ │ ├── contracts.md # Processing unit preconditions/provides table
|
||||
│ │ └── cgal-package.md # Phase 8 CGAL package design (TODO)
|
||||
│ ├── architecture/
|
||||
│ │ ├── overall_pipeline.md # Mermaid diagram + stage descriptions
|
||||
│ │ ├── design-decisions.md # Key architectural choices + rationale
|
||||
│ │ └── project-structure.md # This file
|
||||
│ ├── math/
|
||||
│ │ ├── geometry-modes.md # Euclidean / Spherical / HyperIdeal comparison
|
||||
│ │ └── references.md # All papers by module
|
||||
│ └── roadmap/
|
||||
│ ├── phases.md # Phases 1–10 with porting/research boundary
|
||||
│ └── java-parity.md # Java vs C++ feature parity table
|
||||
│
|
||||
└── .gitea/
|
||||
├── docker/Dockerfile.ci-cpp # Ubuntu 22.04 ARM64 CI image
|
||||
└── workflows/cpp-tests.yml # Two-job CI pipeline
|
||||
```
|
||||
|
||||
## Build targets
|
||||
|
||||
| Target | Mode | Description |
|
||||
|---|---|---|
|
||||
| `conformallab_tests` | default | 36 non-CGAL pure-math tests |
|
||||
| `conformallab_cgal_tests` | `WITH_CGAL_TESTS` or `WITH_CGAL` | 158 CGAL tests |
|
||||
| `conformallab_core` | `WITH_CGAL` | CLI application |
|
||||
| `example_euclidean` | `WITH_CGAL` | Euclidean example program |
|
||||
| `example_hyper_ideal` | `WITH_CGAL` | HyperIdeal example program |
|
||||
| `example_layout` | `WITH_CGAL` | Full pipeline example |
|
||||
| `example_viewer` | `WITH_CGAL` | Interactive viewer |
|
||||
571
doc/concepts/declarative-pipeline.md
Normal file
571
doc/concepts/declarative-pipeline.md
Normal file
@@ -0,0 +1,571 @@
|
||||
# Declarative YAML Pipeline — Concept & Design
|
||||
|
||||
> **Status: Phase 8e — designed, not yet implemented.**
|
||||
> This document is the authoritative design specification.
|
||||
> Implementation target: `code/include/pipeline.hpp` + CLI flag `--pipeline`.
|
||||
|
||||
---
|
||||
|
||||
## 1 — Core idea
|
||||
|
||||
Every algorithm in conformallab++ is a **Processing Unit**: a function with
|
||||
explicit preconditions (*require*) and guarantees (*provide*).
|
||||
The contract table in [doc/api/contracts.md](../api/contracts.md) lists them all.
|
||||
|
||||
A **declarative pipeline** is a YAML file that:
|
||||
1. Lists the units to execute and their parameters.
|
||||
2. Annotates each step with the tokens it `require`s and `provide`s.
|
||||
3. Is validated **before any code runs** — the validator walks the dependency
|
||||
graph and rejects the file if any `require` token is not yet provided by
|
||||
a preceding step.
|
||||
|
||||
The result is a **self-documenting, reproducible experiment** that can be
|
||||
version-controlled, shared, and re-run identically.
|
||||
|
||||
---
|
||||
|
||||
## 2 — Token vocabulary
|
||||
|
||||
Tokens are short strings. Each Processing Unit consumes and produces a fixed
|
||||
set of tokens. The validator treats them as a monotonically growing *provided set*.
|
||||
|
||||
### Input tokens (provided by the `input:` block)
|
||||
| Token | Meaning |
|
||||
|---|---|
|
||||
| `mesh` | A loaded, triangulated, oriented `ConformalMesh` |
|
||||
| `mesh_closed` | Mesh has no boundary (required for cut graph) |
|
||||
| `mesh_open` | Mesh has at least one boundary component |
|
||||
|
||||
### Setup tokens
|
||||
| Token | Produced by | Required by |
|
||||
|---|---|---|
|
||||
| `maps_euclidean` | `setup_euclidean_maps` | `lambda0_euclidean`, `gauss_bonnet`, `newton_euclidean` |
|
||||
| `maps_spherical` | `setup_spherical_maps` | `lambda0_spherical`, `gauss_bonnet`, `newton_spherical` |
|
||||
| `maps_hyper_ideal` | `setup_hyper_ideal_maps` | `lambda0_hyper_ideal`, `gauss_bonnet`, `newton_hyper_ideal` |
|
||||
| `lambda0_euclidean` | `compute_euclidean_lambda0_from_mesh` | `gauss_bonnet_euclidean`, `newton_euclidean` |
|
||||
| `lambda0_spherical` | `compute_spherical_lambda0_from_mesh` | `gauss_bonnet_spherical`, `newton_spherical` |
|
||||
| `lambda0_hyper_ideal` | `compute_hyper_ideal_lambda0_from_mesh` | `gauss_bonnet_hyper_ideal`, `newton_hyper_ideal` |
|
||||
| `dof_indices` | DOF assignment step | `newton_*` |
|
||||
|
||||
### Gauss–Bonnet tokens
|
||||
| Token | Produced by |
|
||||
|---|---|
|
||||
| `gauss_bonnet_euclidean` | `check_gauss_bonnet` or `enforce_gauss_bonnet` (Euclidean maps) |
|
||||
| `gauss_bonnet_spherical` | same, Spherical maps |
|
||||
| `gauss_bonnet_hyper_ideal` | same, HyperIdeal maps |
|
||||
|
||||
### Solver tokens
|
||||
| Token | Produced by |
|
||||
|---|---|
|
||||
| `x_euclidean` | `newton_euclidean` (converged) |
|
||||
| `x_spherical` | `newton_spherical` (converged) |
|
||||
| `x_hyper_ideal` | `newton_hyper_ideal` (converged) |
|
||||
|
||||
### Topology tokens
|
||||
| Token | Produced by | Required by |
|
||||
|---|---|---|
|
||||
| `cut_graph` | `compute_cut_graph` | `euclidean_layout` (closed mesh), `hyper_ideal_layout` |
|
||||
|
||||
### Layout tokens
|
||||
| Token | Produced by |
|
||||
|---|---|
|
||||
| `layout_euclidean` | `euclidean_layout` |
|
||||
| `layout_spherical` | `spherical_layout` |
|
||||
| `layout_hyper_ideal` | `hyper_ideal_layout` |
|
||||
| `holonomy_euclidean` | `euclidean_layout` (with cut graph) |
|
||||
| `holonomy_hyper_ideal` | `hyper_ideal_layout` (with cut graph) |
|
||||
|
||||
### Period / domain tokens
|
||||
| Token | Produced by | Required by |
|
||||
|---|---|---|
|
||||
| `period_matrix` | `compute_period_matrix` | `fundamental_domain` |
|
||||
| `fundamental_domain` | `compute_fundamental_domain` | `tiling` |
|
||||
| `tiling` | `tiling_neighbourhood` | output steps |
|
||||
|
||||
### Output tokens
|
||||
| Token | Produced by |
|
||||
|---|---|
|
||||
| `saved_layout` | `save_layout_off` |
|
||||
| `saved_result` | `save_result_json` / `save_result_xml` |
|
||||
|
||||
---
|
||||
|
||||
## 3 — YAML schema
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: <string> # human-readable experiment name
|
||||
geometry: euclidean # euclidean | spherical | hyper_ideal
|
||||
description: | # optional multi-line description
|
||||
...
|
||||
|
||||
input:
|
||||
source: <path> # mesh file (.off / .obj / .ply)
|
||||
# Optional overrides:
|
||||
theta_v: flat # flat (2π everywhere) | cone:<file> | custom:<file>
|
||||
|
||||
steps:
|
||||
- id: <string> # unique step identifier
|
||||
unit: <function> # C++ function name (see token table)
|
||||
require: [<token>, ...] # tokens that must be in the provided set
|
||||
provide: [<token>, ...] # tokens added to provided set after this step
|
||||
params: # optional parameter overrides
|
||||
<key>: <value>
|
||||
|
||||
output:
|
||||
layout: <path> # optional: save Layout2D as .off with UVs
|
||||
json: <path> # optional: save NewtonResult + Layout as JSON
|
||||
xml: <path> # optional: save NewtonResult + Layout as XML
|
||||
tau: <path> # optional: write τ (period matrix) as text
|
||||
report: <path> # optional: write human-readable summary
|
||||
```
|
||||
|
||||
### Parameter defaults by unit
|
||||
|
||||
| Unit | Parameter | Default |
|
||||
|---|---|---|
|
||||
| `newton_euclidean` | `tol` | `1e-8` |
|
||||
| `newton_euclidean` | `max_iter` | `200` |
|
||||
| `newton_spherical` | `tol` | `1e-8` |
|
||||
| `newton_spherical` | `max_iter` | `200` |
|
||||
| `newton_hyper_ideal` | `tol` | `1e-8` |
|
||||
| `newton_hyper_ideal` | `max_iter` | `200` |
|
||||
| `newton_hyper_ideal` | `hess_eps` | `1e-5` |
|
||||
| `euclidean_layout` | `normalise` | `false` |
|
||||
| `hyper_ideal_layout` | `normalise` | `false` |
|
||||
| `spherical_layout` | `normalise` | `false` |
|
||||
| `compute_period_matrix` | `reduce` | `true` (SL(2,ℤ)) |
|
||||
| `tiling_neighbourhood` | `m` | `1` |
|
||||
| `tiling_neighbourhood` | `n` | `1` |
|
||||
|
||||
---
|
||||
|
||||
## 4 — Validation algorithm
|
||||
|
||||
```
|
||||
provided = { "mesh" } ← always available after input is loaded
|
||||
|
||||
if input.source has no boundary:
|
||||
provided ← provided ∪ { "mesh_closed" }
|
||||
else:
|
||||
provided ← provided ∪ { "mesh_open" }
|
||||
|
||||
for each step in pipeline.steps:
|
||||
for token in step.require:
|
||||
if token ∉ provided:
|
||||
ERROR: "Step '<id>' requires '<token>' which is not yet provided.
|
||||
Add a step that provides it before step '<id>'."
|
||||
provided ← provided ∪ step.provide
|
||||
|
||||
for each output key in pipeline.output:
|
||||
check that its required token is in provided
|
||||
(e.g. 'tau' requires 'period_matrix')
|
||||
```
|
||||
|
||||
The validator runs **before any C++ code executes**. If validation passes,
|
||||
the steps are executed in order. There is no parallelism — steps are sequential.
|
||||
|
||||
---
|
||||
|
||||
## 5 — Complete examples
|
||||
|
||||
### Example A — Euclidean uniformization of a flat torus (genus 1)
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: flat_torus_euclidean
|
||||
geometry: euclidean
|
||||
description: |
|
||||
Euclidean uniformization of the 4×4 torus of revolution.
|
||||
Computes the period matrix τ and the fundamental domain parallelogram.
|
||||
|
||||
input:
|
||||
source: code/data/off/torus_4x4.off
|
||||
|
||||
steps:
|
||||
- id: setup
|
||||
unit: setup_euclidean_maps
|
||||
require: [mesh]
|
||||
provide: [maps_euclidean]
|
||||
|
||||
- id: lambda0
|
||||
unit: compute_euclidean_lambda0_from_mesh
|
||||
require: [mesh, maps_euclidean]
|
||||
provide: [lambda0_euclidean]
|
||||
|
||||
- id: dofs
|
||||
unit: assign_dof_indices_euclidean
|
||||
require: [maps_euclidean]
|
||||
provide: [dof_indices]
|
||||
params:
|
||||
pin_strategy: first_vertex # pin v₀, assign sequential to rest
|
||||
|
||||
- id: gauss_bonnet
|
||||
unit: enforce_gauss_bonnet
|
||||
require: [maps_euclidean, lambda0_euclidean]
|
||||
provide: [gauss_bonnet_euclidean]
|
||||
|
||||
- id: solve
|
||||
unit: newton_euclidean
|
||||
require: [gauss_bonnet_euclidean, dof_indices]
|
||||
provide: [x_euclidean]
|
||||
params:
|
||||
tol: 1.0e-10
|
||||
max_iter: 200
|
||||
|
||||
- id: cut
|
||||
unit: compute_cut_graph
|
||||
require: [mesh_closed]
|
||||
provide: [cut_graph]
|
||||
|
||||
- id: layout
|
||||
unit: euclidean_layout
|
||||
require: [x_euclidean, cut_graph]
|
||||
provide: [layout_euclidean, holonomy_euclidean]
|
||||
params:
|
||||
normalise: true
|
||||
|
||||
- id: period
|
||||
unit: compute_period_matrix
|
||||
require: [holonomy_euclidean]
|
||||
provide: [period_matrix]
|
||||
params:
|
||||
reduce: true
|
||||
|
||||
- id: domain
|
||||
unit: compute_fundamental_domain
|
||||
require: [holonomy_euclidean]
|
||||
provide: [fundamental_domain]
|
||||
|
||||
output:
|
||||
layout: out/torus_layout.off
|
||||
json: out/torus_result.json
|
||||
tau: out/torus_tau.txt
|
||||
```
|
||||
|
||||
**Expected output (`torus_tau.txt`):**
|
||||
```
|
||||
tau = 0.000... + 0.9...i # Re(τ) ≈ 0 (4-fold symmetry), Im(τ) ≈ 1
|
||||
|tau| = 0.9... # ≥ 1 after SL(2,ℤ) reduction
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Example B — Spherical uniformization (genus 0)
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: cathead_spherical
|
||||
geometry: spherical
|
||||
description: |
|
||||
Map the open cathead mesh to the sphere.
|
||||
|
||||
input:
|
||||
source: code/data/obj/cathead.obj
|
||||
|
||||
steps:
|
||||
- id: setup
|
||||
unit: setup_spherical_maps
|
||||
require: [mesh]
|
||||
provide: [maps_spherical]
|
||||
|
||||
- id: lambda0
|
||||
unit: compute_spherical_lambda0_from_mesh
|
||||
require: [mesh, maps_spherical]
|
||||
provide: [lambda0_spherical]
|
||||
|
||||
- id: dofs
|
||||
unit: assign_dof_indices_spherical
|
||||
require: [maps_spherical]
|
||||
provide: [dof_indices]
|
||||
|
||||
- id: gauss_bonnet
|
||||
unit: enforce_gauss_bonnet
|
||||
require: [maps_spherical, lambda0_spherical]
|
||||
provide: [gauss_bonnet_spherical]
|
||||
|
||||
- id: solve
|
||||
unit: newton_spherical
|
||||
require: [gauss_bonnet_spherical, dof_indices]
|
||||
provide: [x_spherical]
|
||||
|
||||
- id: layout
|
||||
unit: spherical_layout
|
||||
require: [x_spherical]
|
||||
provide: [layout_spherical]
|
||||
params:
|
||||
normalise: true # rotate centroid to north pole
|
||||
|
||||
output:
|
||||
json: out/cathead_spherical.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Example C — Hyperbolic uniformization (genus 1, Poincaré disk)
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: torus_hyperbolic
|
||||
geometry: hyper_ideal
|
||||
description: |
|
||||
Hyperbolic (hyper-ideal) uniformization of the 8×8 torus.
|
||||
Lays out the mesh in the Poincaré disk with correct Möbius holonomy.
|
||||
|
||||
input:
|
||||
source: code/data/off/torus_8x8.off
|
||||
|
||||
steps:
|
||||
- id: setup
|
||||
unit: setup_hyper_ideal_maps
|
||||
require: [mesh]
|
||||
provide: [maps_hyper_ideal]
|
||||
|
||||
- id: lambda0
|
||||
unit: compute_hyper_ideal_lambda0_from_mesh
|
||||
require: [mesh, maps_hyper_ideal]
|
||||
provide: [lambda0_hyper_ideal]
|
||||
|
||||
- id: dofs
|
||||
unit: assign_all_dof_indices
|
||||
require: [maps_hyper_ideal]
|
||||
provide: [dof_indices]
|
||||
# HyperIdeal: all vertices AND edges are free DOFs — no vertex pinned.
|
||||
|
||||
- id: gauss_bonnet
|
||||
unit: enforce_gauss_bonnet
|
||||
require: [maps_hyper_ideal, lambda0_hyper_ideal]
|
||||
provide: [gauss_bonnet_hyper_ideal]
|
||||
|
||||
- id: solve
|
||||
unit: newton_hyper_ideal
|
||||
require: [gauss_bonnet_hyper_ideal, dof_indices]
|
||||
provide: [x_hyper_ideal]
|
||||
params:
|
||||
tol: 1.0e-10
|
||||
|
||||
- id: cut
|
||||
unit: compute_cut_graph
|
||||
require: [mesh_closed]
|
||||
provide: [cut_graph]
|
||||
|
||||
- id: layout
|
||||
unit: hyper_ideal_layout
|
||||
require: [x_hyper_ideal, cut_graph]
|
||||
provide: [layout_hyper_ideal, holonomy_hyper_ideal]
|
||||
params:
|
||||
normalise: true # Möbius-centre to disk origin
|
||||
|
||||
output:
|
||||
layout: out/torus_disk.off
|
||||
json: out/torus_disk.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Example D — Full pipeline with period matrix and tiling
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: torus_full
|
||||
geometry: euclidean
|
||||
|
||||
input:
|
||||
source: code/data/off/torus_hex_6x6.off
|
||||
|
||||
steps:
|
||||
- id: setup
|
||||
unit: setup_euclidean_maps
|
||||
require: [mesh]
|
||||
provide: [maps_euclidean]
|
||||
|
||||
- id: lambda0
|
||||
unit: compute_euclidean_lambda0_from_mesh
|
||||
require: [mesh, maps_euclidean]
|
||||
provide: [lambda0_euclidean]
|
||||
|
||||
- id: dofs
|
||||
unit: assign_dof_indices_euclidean
|
||||
require: [maps_euclidean]
|
||||
provide: [dof_indices]
|
||||
|
||||
- id: gauss_bonnet
|
||||
unit: enforce_gauss_bonnet
|
||||
require: [maps_euclidean, lambda0_euclidean]
|
||||
provide: [gauss_bonnet_euclidean]
|
||||
|
||||
- id: solve
|
||||
unit: newton_euclidean
|
||||
require: [gauss_bonnet_euclidean, dof_indices]
|
||||
provide: [x_euclidean]
|
||||
|
||||
- id: cut
|
||||
unit: compute_cut_graph
|
||||
require: [mesh_closed]
|
||||
provide: [cut_graph]
|
||||
|
||||
- id: layout
|
||||
unit: euclidean_layout
|
||||
require: [x_euclidean, cut_graph]
|
||||
provide: [layout_euclidean, holonomy_euclidean]
|
||||
params:
|
||||
normalise: true
|
||||
|
||||
- id: period
|
||||
unit: compute_period_matrix
|
||||
require: [holonomy_euclidean]
|
||||
provide: [period_matrix]
|
||||
|
||||
- id: domain
|
||||
unit: compute_fundamental_domain
|
||||
require: [holonomy_euclidean]
|
||||
provide: [fundamental_domain]
|
||||
|
||||
- id: tiling
|
||||
unit: tiling_neighbourhood
|
||||
require: [layout_euclidean, holonomy_euclidean]
|
||||
provide: [tiling]
|
||||
params:
|
||||
m: 2 # 5×5 tile grid around origin
|
||||
n: 2
|
||||
|
||||
output:
|
||||
layout: out/hex_torus_layout.off
|
||||
tau: out/hex_torus_tau.txt
|
||||
json: out/hex_torus_full.json
|
||||
report: out/hex_torus_summary.txt
|
||||
|
||||
# Expected tau for 6-fold symmetric torus:
|
||||
# Re(τ) ≈ 0.5, Im(τ) ≈ 0.866 (approaches e^{iπ/3} as mesh is refined)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Example E — Validation error (intentional mistake)
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
name: broken_example
|
||||
geometry: euclidean
|
||||
|
||||
input:
|
||||
source: code/data/off/torus_4x4.off
|
||||
|
||||
steps:
|
||||
- id: solve # ← WRONG: skipped setup and lambda0
|
||||
unit: newton_euclidean
|
||||
require: [gauss_bonnet_euclidean, dof_indices]
|
||||
provide: [x_euclidean]
|
||||
|
||||
- id: layout
|
||||
unit: euclidean_layout
|
||||
require: [x_euclidean, cut_graph] # ← WRONG: cut_graph never provided
|
||||
provide: [layout_euclidean]
|
||||
```
|
||||
|
||||
**Validator output:**
|
||||
```
|
||||
ERROR [step 'solve']: requires 'gauss_bonnet_euclidean' which is not yet provided.
|
||||
Hint: add setup_euclidean_maps → compute_euclidean_lambda0_from_mesh
|
||||
→ enforce_gauss_bonnet before 'solve'.
|
||||
|
||||
ERROR [step 'layout']: requires 'cut_graph' which is not yet provided.
|
||||
Hint: add compute_cut_graph (requires: mesh_closed) before 'layout'.
|
||||
|
||||
Pipeline rejected. 2 contract violations.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6 — Mapping to C++ code
|
||||
|
||||
Each `unit:` name in the YAML maps directly to a C++ function:
|
||||
|
||||
| YAML unit | C++ function | Header |
|
||||
|---|---|---|
|
||||
| `setup_euclidean_maps` | `conformallab::setup_euclidean_maps()` | `euclidean_functional.hpp` |
|
||||
| `compute_euclidean_lambda0_from_mesh` | `conformallab::compute_euclidean_lambda0_from_mesh()` | `euclidean_functional.hpp` |
|
||||
| `enforce_gauss_bonnet` | `conformallab::enforce_gauss_bonnet()` | `gauss_bonnet.hpp` |
|
||||
| `assign_dof_indices_euclidean` | manual pin + sequential loop | `euclidean_functional.hpp` |
|
||||
| `assign_all_dof_indices` | `conformallab::assign_all_dof_indices()` | `hyper_ideal_functional.hpp` |
|
||||
| `newton_euclidean` | `conformallab::newton_euclidean()` | `newton_solver.hpp` |
|
||||
| `newton_spherical` | `conformallab::newton_spherical()` | `newton_solver.hpp` |
|
||||
| `newton_hyper_ideal` | `conformallab::newton_hyper_ideal()` | `newton_solver.hpp` |
|
||||
| `compute_cut_graph` | `conformallab::compute_cut_graph()` | `cut_graph.hpp` |
|
||||
| `euclidean_layout` | `conformallab::euclidean_layout()` | `layout.hpp` |
|
||||
| `spherical_layout` | `conformallab::spherical_layout()` | `layout.hpp` |
|
||||
| `hyper_ideal_layout` | `conformallab::hyper_ideal_layout()` | `layout.hpp` |
|
||||
| `compute_period_matrix` | `conformallab::compute_period_matrix()` | `period_matrix.hpp` |
|
||||
| `compute_fundamental_domain` | `conformallab::compute_fundamental_domain()` | `fundamental_domain.hpp` |
|
||||
| `tiling_neighbourhood` | `conformallab::tiling_neighbourhood()` | `fundamental_domain.hpp` |
|
||||
| `save_layout_off` | `conformallab::save_layout_off()` | `mesh_io.hpp` |
|
||||
| `save_result_json` | `conformallab::save_result_json()` | `serialization.hpp` |
|
||||
|
||||
---
|
||||
|
||||
## 7 — Implementation plan (Phase 8e)
|
||||
|
||||
```
|
||||
code/include/pipeline.hpp ← YAML parser + validator + executor
|
||||
code/apps/conformallab_pipeline.cpp ← CLI: conformallab_pipeline --pipeline foo.yml
|
||||
|
||||
External YAML dependency (header-only, already bundled):
|
||||
code/deps/single_includes/yaml-cpp/yaml.h ← or single-include yaml.hpp
|
||||
Alternative: use the bundled single_includes/nlohmann/json.hpp for a
|
||||
JSON-based pipeline format (simpler, no new dependency).
|
||||
```
|
||||
|
||||
### Validator pseudocode
|
||||
|
||||
```cpp
|
||||
struct PipelineValidator {
|
||||
std::set<std::string> provided;
|
||||
|
||||
void load_input(const YAML::Node& input) {
|
||||
provided.insert("mesh");
|
||||
auto mesh = load_mesh(input["source"].as<std::string>());
|
||||
if (is_closed(mesh)) provided.insert("mesh_closed");
|
||||
else provided.insert("mesh_open");
|
||||
}
|
||||
|
||||
void validate_step(const YAML::Node& step) {
|
||||
for (auto& tok : step["require"])
|
||||
if (!provided.count(tok.as<std::string>()))
|
||||
throw PipelineError("Step '" + step["id"].as<std::string>()
|
||||
+ "' requires '" + tok.as<std::string>() + "' not yet provided.");
|
||||
for (auto& tok : step["provide"])
|
||||
provided.insert(tok.as<std::string>());
|
||||
}
|
||||
|
||||
void validate(const YAML::Node& pipeline) {
|
||||
load_input(pipeline["input"]);
|
||||
for (auto& step : pipeline["steps"])
|
||||
validate_step(step);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8 — Design decisions
|
||||
|
||||
**Why YAML and not JSON or TOML?**
|
||||
YAML supports multi-line strings (for `description:`), comments (`#`), and
|
||||
anchors/aliases — useful for parametric experiments. JSON lacks comments.
|
||||
TOML lacks the list syntax needed for `require:` / `provide:`.
|
||||
|
||||
**Why explicit require/provide instead of auto-inference?**
|
||||
Auto-inference would require the validator to know all function signatures
|
||||
at parse time — this couples the validator tightly to the C++ code.
|
||||
Explicit tokens make the contract visible in the YAML, making it
|
||||
self-documenting and readable without the source code.
|
||||
|
||||
**Why sequential steps and not a DAG?**
|
||||
The pipeline is a linear sequence for now (Phase 8e target). A DAG-based
|
||||
executor (parallel steps where contracts allow) is a natural Phase 10+ extension
|
||||
but adds significant complexity. Linear execution is correct and debuggable.
|
||||
|
||||
**Why one YAML per geometry mode?**
|
||||
A single YAML could support multiple geometries with conditional blocks, but
|
||||
this adds syntactic complexity. The `geometry:` field at the top locks the mode
|
||||
and keeps the YAML readable. Cross-geometry experiments can chain two pipelines.
|
||||
89
doc/contributing.md
Normal file
89
doc/contributing.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# Contributing
|
||||
|
||||
## Language
|
||||
|
||||
**All code, comments, documentation, commit messages, and test descriptions must be in English.**
|
||||
|
||||
The project targets CGAL submission and international collaboration. When editing
|
||||
files that still contain German-language comments or documentation, replace them
|
||||
with English.
|
||||
|
||||
---
|
||||
|
||||
## Git workflow
|
||||
|
||||
- `main` is protected on `origin` (Gitea). Push to `dev`, then open a pull request.
|
||||
- `codeberg/main` can be pushed to directly (public mirror, no CI).
|
||||
- Both remotes must stay in sync after every significant change:
|
||||
```bash
|
||||
git push origin HEAD:dev # triggers CI
|
||||
git push codeberg main # updates public mirror
|
||||
```
|
||||
- Branch naming: `feature/<topic>`, `fix/<topic>`, `phase<N>-<topic>`
|
||||
|
||||
---
|
||||
|
||||
## CI
|
||||
|
||||
Two jobs run on push to `dev`/`main` or on pull requests:
|
||||
|
||||
| Job | What it tests | Trigger |
|
||||
|---|---|---|
|
||||
| `test-fast` | 36 non-CGAL tests, no Boost | all branches |
|
||||
| `test-cgal` | 170 CGAL tests + 1 skip | `main`, `dev`, PRs only |
|
||||
|
||||
A PR is ready to merge when both jobs pass.
|
||||
|
||||
The runner is a self-hosted Raspberry Pi (ARM64). See `.gitea/workflows/cpp-tests.yml`
|
||||
and `.gitea/docker/Dockerfile.ci-cpp`.
|
||||
|
||||
---
|
||||
|
||||
## Test standards
|
||||
|
||||
Every new algorithm needs:
|
||||
|
||||
1. **Gradient check** — finite-difference verification of `G(x) = ∂E/∂x`.
|
||||
Copy any `GradientCheck_*` test suite from `tests/cgal/test_*_functional.cpp`.
|
||||
|
||||
2. **Convergence test** — Newton converges on a small mesh using the "natural theta"
|
||||
trick (see [CLAUDE.md](../CLAUDE.md) and [api/extending.md](api/extending.md)).
|
||||
|
||||
3. **Registration** — add the `.cpp` file to `code/tests/cgal/CMakeLists.txt`.
|
||||
|
||||
Expected CI result: **36 + 170 tests pass, exactly 1 skipped**.
|
||||
The skip is an intentional `GTEST_SKIP()` stub for the genus-2 homology test
|
||||
(`cgal.HomologyGenerators.Genus2_FourGeneratorPaths_BLOCKED`) — blocked until a
|
||||
genus-2 mesh is available in Phase 9c. Do not remove it.
|
||||
|
||||
---
|
||||
|
||||
## Code style
|
||||
|
||||
- C++17. Header-only (`code/include/*.hpp`). No compiled library.
|
||||
- `#pragma once` at the top of every header.
|
||||
- Everything in the `conformallab` namespace, internal helpers in `conformallab::detail`.
|
||||
- Property map names follow the prefix convention: `"v:"` (vertex), `"e:"` (edge), `"f:"` (face).
|
||||
- DOF index `-1` always means "pinned/fixed".
|
||||
|
||||
---
|
||||
|
||||
## Releases
|
||||
|
||||
Release tags follow `vMAJOR.MINOR.PATCH`:
|
||||
|
||||
```bash
|
||||
# Merge dev → main, then tag
|
||||
git checkout main && git merge --no-ff dev
|
||||
git tag -a vX.Y.Z -m "vX.Y.Z — <one-line summary>"
|
||||
git push origin main && git push origin vX.Y.Z
|
||||
git push codeberg main && git push codeberg vX.Y.Z
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] Define code review checklist
|
||||
- [ ] Add `.clang-format` configuration
|
||||
- [ ] Document how to request CGAL package review
|
||||
189
doc/getting-started.md
Normal file
189
doc/getting-started.md
Normal file
@@ -0,0 +1,189 @@
|
||||
# Getting Started
|
||||
|
||||
## Prerequisites
|
||||
|
||||
| Tool | Minimum | Notes |
|
||||
|---|---|---|
|
||||
| C++ compiler (GCC or Clang) | C++17 | GCC 11+ or Clang 14+ recommended |
|
||||
| CMake | 3.20 | |
|
||||
| Boost headers | 1.70 | Only for `-DWITH_CGAL_TESTS=ON` or `-DWITH_CGAL=ON` |
|
||||
| Wayland/X11 dev headers | — | Only for `-DWITH_CGAL=ON` (viewer) |
|
||||
|
||||
All other dependencies (Eigen 3.4, CGAL 6.1.1, libigl 2.6, GLFW 3.4, GTest 1.14)
|
||||
are bundled as tarballs in `code/deps/tarballs/` and extracted automatically at CMake
|
||||
configure time. No internet access is required at build time (except GTest, fetched via
|
||||
`FetchContent` from GitHub).
|
||||
|
||||
Install Boost on your system:
|
||||
```bash
|
||||
# Ubuntu / Debian
|
||||
apt install libboost-dev
|
||||
|
||||
# macOS
|
||||
brew install boost
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Clone
|
||||
|
||||
```bash
|
||||
git clone https://codeberg.org/TMoussa/ConformalLabpp
|
||||
cd ConformalLabpp
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Build modes
|
||||
|
||||
Three modes with increasing dependencies:
|
||||
|
||||
### Mode 1 — Fast tests (default, no system dependencies)
|
||||
|
||||
Pure-math tests: Clausen functions, hyper-ideal geometry, matrix utilities.
|
||||
|
||||
```bash
|
||||
cmake -S code -B build
|
||||
cmake --build build --target conformallab_tests -j$(nproc)
|
||||
ctest --test-dir build --output-on-failure
|
||||
```
|
||||
|
||||
Expected: **36 tests pass**.
|
||||
|
||||
### Mode 2 — CGAL tests, headless (recommended for CI and development)
|
||||
|
||||
Full CGAL test suite. Requires Boost headers only — no display, no Wayland, no GLFW.
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_CGAL_TESTS=ON
|
||||
cmake --build build --target conformallab_cgal_tests -j$(nproc)
|
||||
ctest --test-dir build -R "^cgal\." --output-on-failure
|
||||
```
|
||||
|
||||
Expected: **173 tests pass, 1 skipped** (intentional stub for analytic HyperIdeal Hessian, Phase 9b).
|
||||
|
||||
### Mode 3 — Full local build (CLI app + interactive viewer)
|
||||
|
||||
Requires Wayland or X11 development packages (`wayland-scanner`, `libx11-dev`, etc.).
|
||||
Automatically enables the viewer library (GLFW + libigl).
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_CGAL=ON
|
||||
cmake --build build -j$(nproc)
|
||||
```
|
||||
|
||||
> **Note:** `-DWITH_CGAL=ON` implies `-DWITH_VIEWER=ON`. Do not use this in headless
|
||||
> environments — it will fail with `Failed to find wayland-scanner`.
|
||||
|
||||
---
|
||||
|
||||
## Running a single test
|
||||
|
||||
```bash
|
||||
# By GTest filter (fastest, full output)
|
||||
./build/conformallab_cgal_tests --gtest_filter="NewtonSolver*"
|
||||
./build/conformallab_tests --gtest_filter="Clausen*"
|
||||
|
||||
# By CTest regex
|
||||
ctest --test-dir build -R "cgal.NewtonSolver" --output-on-failure
|
||||
```
|
||||
|
||||
All CGAL tests have the prefix `cgal.` in CTest (set in `tests/cgal/CMakeLists.txt`
|
||||
via `TEST_PREFIX "cgal."`).
|
||||
|
||||
---
|
||||
|
||||
## First run — CLI app
|
||||
|
||||
After a full build (`-DWITH_CGAL=ON`):
|
||||
|
||||
```bash
|
||||
# Euclidean conformal layout
|
||||
./bin/conformallab_core -i input.off -g euclidean -o layout.off -j result.json
|
||||
|
||||
# Spherical layout
|
||||
./bin/conformallab_core -i input.off -g spherical -o sphere.off
|
||||
|
||||
# Hyperbolic layout (HyperIdeal)
|
||||
./bin/conformallab_core -i input.off -g hyper_ideal -o hyperbolic.off
|
||||
|
||||
# Show input mesh in interactive viewer
|
||||
./bin/conformallab_core -i input.off -s
|
||||
|
||||
# All options
|
||||
./bin/conformallab_core --help
|
||||
```
|
||||
|
||||
## Example programs
|
||||
|
||||
```bash
|
||||
./build/examples/example_layout [input.off] [layout.off] [result.json]
|
||||
./build/examples/example_euclidean [input.off] [output.off]
|
||||
./build/examples/example_hyper_ideal [input.off] [output.off]
|
||||
./build/examples/example_viewer [input.off] # interactive, requires WITH_VIEWER
|
||||
```
|
||||
|
||||
`example_layout.cpp` is the best starting point — it shows the complete pipeline in ~120 lines.
|
||||
|
||||
**Expected output of `example_euclidean` on the built-in quad-strip mesh:**
|
||||
```
|
||||
[example_euclidean] No input file given — using make_quad_strip().
|
||||
[example_euclidean] Mesh: 6 vertices, 4 faces.
|
||||
[example_euclidean] DOFs: 5 (1 vertex pinned).
|
||||
[example_euclidean] Solving Newton system…
|
||||
[example_euclidean] Converged in 1 iterations. ||G||_inf = 0
|
||||
[example_euclidean] Per-vertex conformal factors u_i:
|
||||
v0 u = 0 (pinned)
|
||||
v1 u = -1.38778e-17 ← ≈ 0 (machine epsilon)
|
||||
...
|
||||
[example_euclidean] Mesh saved to: /tmp/conformallab_euclidean_out.off
|
||||
```
|
||||
Convergence in 1 iteration is expected: the "natural equilibrium" construction
|
||||
sets x* = 0, so a small perturbation (-0.05) needs only one Newton step.
|
||||
|
||||
**Quick automated start (no interactive build needed):**
|
||||
```bash
|
||||
bash scripts/try_it.sh
|
||||
```
|
||||
This clones nothing (run from inside the repo), builds the CGAL test suite, runs
|
||||
all 173+36 tests, and prints a summary. See `scripts/try_it.sh` for details.
|
||||
|
||||
---
|
||||
|
||||
## Known issues
|
||||
|
||||
### macOS Finder duplicates
|
||||
|
||||
macOS Finder sometimes creates duplicate files named `foo 2.hpp` when copying
|
||||
the repository. These cause confusing compile errors ("redefinition of …").
|
||||
|
||||
**Fix (run once from the repo root):**
|
||||
```bash
|
||||
find code/include -name "* 2.*" -delete
|
||||
find code/include -name "*\ 2.*" -delete
|
||||
```
|
||||
|
||||
Files without a ` 2` suffix are always canonical — the duplicates are safe to delete.
|
||||
|
||||
### First build is slow
|
||||
|
||||
The first CMake configure extracts four tarballs (Eigen 3.4, CGAL 6.1.1,
|
||||
libigl 2.6, GLFW 3.4) and downloads GTest via `FetchContent`.
|
||||
Allow **30–90 seconds** for the first configure. Subsequent builds are fast
|
||||
(< 10 s incremental).
|
||||
|
||||
---
|
||||
|
||||
## Rebuilding the CI Docker image
|
||||
|
||||
The CI runner is a self-hosted Raspberry Pi (ARM64). After changes to
|
||||
`.gitea/docker/Dockerfile.ci-cpp`:
|
||||
|
||||
```bash
|
||||
docker buildx build \
|
||||
--platform linux/arm64 \
|
||||
-f .gitea/docker/Dockerfile.ci-cpp \
|
||||
-t git.eulernest.eu/conformallab/ci-cpp:latest \
|
||||
--push \
|
||||
.gitea/docker/
|
||||
```
|
||||
175
doc/math/discrete-conformal-theory.md
Normal file
175
doc/math/discrete-conformal-theory.md
Normal file
@@ -0,0 +1,175 @@
|
||||
# Discrete Conformal Geometry — Mathematical Background
|
||||
|
||||
This document is written for a mathematician who knows Riemannian surfaces and
|
||||
complex analysis but is new to the *discrete* setting. It covers exactly the
|
||||
theory implemented in conformallab++.
|
||||
|
||||
---
|
||||
|
||||
## 1 — The continuous picture (in one paragraph)
|
||||
|
||||
A **Riemann surface** (M, g) carries a conformal structure: the class of all
|
||||
metrics related to g by a smooth positive factor. On a compact surface of genus
|
||||
g, the Uniformization Theorem gives a unique constant-curvature representative
|
||||
(flat for g = 1, hyperbolic for g ≥ 2, spherical for g = 0). The conformal
|
||||
modulus of a genus-1 surface is a point τ ∈ ℍ (upper half-plane), well-defined
|
||||
up to SL(2, ℤ).
|
||||
|
||||
---
|
||||
|
||||
## 2 — Discrete conformal equivalence (DCE)
|
||||
|
||||
A **triangulated surface** is a pair (K, ℓ) where K is a simplicial complex
|
||||
homeomorphic to a surface and ℓ: E → ℝ₊ assigns an edge length. Two
|
||||
length assignments ℓ and ℓ̃ are **discretely conformally equivalent** if there
|
||||
exist vertex weights u: V → ℝ such that
|
||||
|
||||
```
|
||||
ℓ̃ᵢⱼ = e^{(uᵢ + uⱼ)/2} · ℓᵢⱼ for every edge {i, j}.
|
||||
```
|
||||
|
||||
This is the discrete analogue of a conformal rescaling g̃ = e^{2φ} g.
|
||||
The weights u ∈ ℝ^V are the *conformal factors* (log-scale factors on vertices).
|
||||
|
||||
**Key fact** (Springborn 2020): within each DCE class there exists a unique
|
||||
length assignment realising a prescribed angle structure, and it can be found
|
||||
by Newton's method on a convex energy.
|
||||
|
||||
---
|
||||
|
||||
## 3 — The variational energy
|
||||
|
||||
For each target corner angle **Θ_v** at vertex v, define the **angle-defect energy**:
|
||||
|
||||
```
|
||||
E(u) = Σ_{corners} φ(αᵥ(u)) − Σᵥ Θᵥ · uᵥ + boundary terms
|
||||
```
|
||||
|
||||
where αᵥ(u) is the corner angle at v in the triangulation with edge lengths
|
||||
ℓ̃(u) and φ is an appropriate primitive (Clausen / Lobachevsky / ImLi₂ depending
|
||||
on geometry).
|
||||
|
||||
The **gradient** is simply the angle-sum residual:
|
||||
|
||||
```
|
||||
∂E/∂uᵥ = Σ_{faces containing v} αᵥ(face) − Θᵥ
|
||||
```
|
||||
|
||||
Setting G = 0 finds the unique u realising the prescribed angle sums.
|
||||
|
||||
### Three geometry modes
|
||||
|
||||
| Mode | Space | φ | Hessian | Newton step |
|
||||
|---|---|---|---|---|
|
||||
| Euclidean | ℝ² | Clausen Cl₂ | cotangent Laplacian, PSD | SimplicialLDLT(H) |
|
||||
| Spherical | S² | ImLi₂ | NSD (concave E) | SimplicialLDLT(−H) |
|
||||
| Hyper-ideal | H² | Lobachevsky | PSD (strictly convex) | SimplicialLDLT(H) |
|
||||
|
||||
---
|
||||
|
||||
## 4 — Gauss–Bonnet constraint
|
||||
|
||||
The target angles **must** satisfy the discrete Gauss–Bonnet equation
|
||||
|
||||
```
|
||||
Σᵥ (2π − Θᵥ) = 2π · χ(M)
|
||||
```
|
||||
|
||||
before any solver is called. If this fails, no conformal factor can realise Θ
|
||||
and Newton will not converge. conformallab++ provides:
|
||||
|
||||
```cpp
|
||||
check_gauss_bonnet(mesh, maps); // throws if violated
|
||||
enforce_gauss_bonnet(mesh, maps); // redistributes residual uniformly
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5 — From angles to geometry: trilateration
|
||||
|
||||
After Newton converges, edge lengths ℓ̃ are known. The layout
|
||||
(embedding into ℝ², S², or H²) is built by a **priority BFS**:
|
||||
|
||||
1. Place an initial face arbitrarily.
|
||||
2. For each adjacent face, place its third vertex by *trilateration* —
|
||||
solving the system of three distance equations.
|
||||
3. Priority is depth in the spanning tree (shallowest first).
|
||||
|
||||
For Euclidean geometry this is the standard cosine rule.
|
||||
For hyperbolic geometry (Poincaré disk model) it uses the Möbius-isometric
|
||||
placement formula implemented in `layout.hpp`.
|
||||
|
||||
---
|
||||
|
||||
## 6 — Cut graph and holonomy
|
||||
|
||||
For genus g ≥ 1 the layout does not close up: a handle introduces a
|
||||
**holonomy** — a non-trivial monodromy around each generator of π₁.
|
||||
|
||||
The **tree-cotree algorithm** (Erickson–Whittlesey 2005) computes a minimal
|
||||
cut graph with exactly 2g seam edges. After cutting, the surface is disk-like
|
||||
and the BFS layout is well-defined. The holonomies along the 2g cut edges are:
|
||||
|
||||
- **Euclidean:** lattice translations ω₁, ω₂ ∈ ℂ
|
||||
- **Hyperbolic:** Möbius isometries T₁, T₂ ∈ SU(1,1)
|
||||
|
||||
---
|
||||
|
||||
## 7 — Period matrix (genus 1)
|
||||
|
||||
For a torus the **conformal modulus** is
|
||||
|
||||
```
|
||||
τ = ω₂ / ω₁ ∈ ℍ
|
||||
```
|
||||
|
||||
After Euclidean uniformization, ω₁ and ω₂ are the holonomies computed from
|
||||
the seam edge displacements. The SL(2, ℤ)-reduction to the fundamental
|
||||
domain {|τ| ≥ 1, |Re(τ)| ≤ 1/2, Im(τ) > 0} is performed automatically by
|
||||
`compute_period_matrix()`.
|
||||
|
||||
For genus g ≥ 2, the Siegel period matrix Ω ∈ H_g (g×g complex symmetric with
|
||||
positive definite imaginary part) requires integrating holomorphic 1-forms — this
|
||||
is Phase 10b.
|
||||
|
||||
---
|
||||
|
||||
## 8 — Fundamental domain
|
||||
|
||||
The fundamental domain of a torus is the parallelogram with vertices
|
||||
{0, ω₁, ω₁+ω₂, ω₂} in ℂ. conformallab++ computes this and provides tiling
|
||||
utilities (`tiling_copy`, `tiling_neighbourhood`).
|
||||
|
||||
For genus g ≥ 2, the fundamental domain is the standard 4g-gon (Phase 9c).
|
||||
|
||||
---
|
||||
|
||||
## 9 — Where the code lives
|
||||
|
||||
```
|
||||
Energy / gradient code/include/*_functional.hpp
|
||||
Hessian code/include/*_hessian.hpp
|
||||
Newton solver code/include/newton_solver.hpp
|
||||
Trilateration / BFS code/include/layout.hpp
|
||||
Cut graph code/include/cut_graph.hpp
|
||||
Holonomy code/include/layout.hpp (HolonomyData)
|
||||
Period matrix code/include/period_matrix.hpp
|
||||
Fundamental domain code/include/fundamental_domain.hpp
|
||||
Möbius maps code/include/layout.hpp (MobiusMap)
|
||||
```
|
||||
|
||||
All implementations are header-only (C++17), no compiled library.
|
||||
|
||||
---
|
||||
|
||||
## 10 — Primary references
|
||||
|
||||
| Reference | Covers |
|
||||
|---|---|
|
||||
| Springborn, *Discrete Uniformization of Polyhedral Surfaces*, 2020 | Complete mathematical foundation of all three modes |
|
||||
| Sechelmann, *Variational Methods for Discrete Surface Parameterization*, TU Berlin 2016 | Original Java implementation — the direct source for this library |
|
||||
| Pinkall & Polthier, 1993 | Cotangent Laplacian |
|
||||
| Erickson & Whittlesey, SODA 2005 | Tree-cotree cut graph |
|
||||
| Bobenko & Springborn, Trans. AMS 2004 | Variational circle-pattern framework |
|
||||
|
||||
Full reference list: [doc/math/references.md](references.md)
|
||||
112
doc/math/geometry-modes.md
Normal file
112
doc/math/geometry-modes.md
Normal file
@@ -0,0 +1,112 @@
|
||||
# The Three Geometry Modes
|
||||
|
||||
conformallab++ implements discrete conformal geometry in three model spaces.
|
||||
All three share the same algorithmic structure — only the angle formula,
|
||||
the trilateration geometry, and the Hessian sign differ.
|
||||
|
||||
---
|
||||
|
||||
## Comparison
|
||||
|
||||
```
|
||||
Euclidean Spherical Hyper-ideal
|
||||
─────────────────────────────────────────────────────
|
||||
Space ℝ² S² H² (Poincaré disk)
|
||||
Curvature K = 0 K = +1 K = −1
|
||||
Typical genus any (cone metrics) 0 ≥ 1
|
||||
Angle sum Σαᵥ = Θᵥ Σαᵥ = Θᵥ Σβᵥ = Θᵥ
|
||||
Gradient sign G_v = actual − Θᵥ G_v = Θᵥ − actual G_v = actual − Θᵥ
|
||||
Hessian PSD (cotangent-Lap.) NSD (sign-flip) PSD (FD, strict conv.)
|
||||
Newton solve SimplicialLDLT(H) SimplicialLDLT(−H) SimplicialLDLT(H)
|
||||
Holonomy translations ωᵢ rotations (2-D) Möbius maps Tᵢ ∈ SU(1,1)
|
||||
Period τ = ω₂/ω₁ ∈ ℍ — axis of Tᵢ
|
||||
Normalise PCA centring Rodrigues to N pole weighted Möbius centring
|
||||
Layout Layout2D (ℝ²) Layout3D (unit sphere) Layout2D (Poincaré disk)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Euclidean (ℝ²)
|
||||
|
||||
**Energy:** the cotangent-weighted angle defect energy (Pinkall & Polthier 1993).
|
||||
|
||||
**Effective log-length:**
|
||||
```
|
||||
Λ̃ᵢⱼ = λ°ᵢⱼ + uᵢ + uⱼ
|
||||
```
|
||||
|
||||
**Gradient:** `G_v = Σ_{faces adj. v} α_v(face) − Θᵥ` — the actual angle sum minus the target.
|
||||
|
||||
**Hessian:** cotangent Laplacian, assembled in `euclidean_hessian.hpp`. PSD with one zero
|
||||
eigenvalue (constant function) for closed meshes — handled by pinning one vertex or SparseQR.
|
||||
|
||||
**Normalisation:** centroid → origin, major axis → x-axis via PCA (`normalise_euclidean`).
|
||||
|
||||
**Holonomy:** lattice translations ω₁, ω₂ ∈ ℂ for closed surfaces.
|
||||
Period ratio τ = ω₂/ω₁ ∈ ℍ is the conformal modulus of the torus.
|
||||
|
||||
---
|
||||
|
||||
## Spherical (S²)
|
||||
|
||||
**Typical use:** genus-0 (sphere-like) surfaces.
|
||||
|
||||
**Gradient:** `G_v = Θᵥ − Σ α_v(face)` — note the **sign reversal** vs. Euclidean.
|
||||
|
||||
**Hessian:** NSD (the spherical energy is concave). Newton solves `(−H)·Δx = G`, i.e.
|
||||
`SimplicialLDLT` is called on `−H`. This is handled transparently in `newton_spherical()`.
|
||||
|
||||
**Gauge fix:** for a closed spherical surface, one additional DOF must be pinned to remove
|
||||
the rotational gauge mode. `SphericalMaps` has a dedicated `gauge_vertex` field.
|
||||
|
||||
**Normalisation:** `normalise_spherical()` rotates the layout so the area centroid
|
||||
maps to the north pole (Rodrigues rotation formula).
|
||||
|
||||
---
|
||||
|
||||
## Hyper-ideal (H²)
|
||||
|
||||
**Typical use:** genus-g surfaces (g ≥ 1), hyperbolic cone metrics.
|
||||
|
||||
**DOFs:** both vertex variables `bᵢ` (hyper-ideal radius) and edge variables `aₑ`
|
||||
(intersection angle). `assign_all_dof_indices(mesh, maps)` assigns both automatically.
|
||||
No vertex needs to be pinned — the functional is strictly convex.
|
||||
|
||||
**Geometry functions** (in `hyper_ideal_geometry.hpp`):
|
||||
```
|
||||
ζ₁₃(b, a) ζ₁₄(b, a) ζ₁₅(b, a) — the three fundamental Springborn functions
|
||||
lᵢⱼ(ζ) — edge length from ζ value
|
||||
αᵢⱼ(l₁, l₂, l₃) — interior angle
|
||||
βᵢ(l₁, l₂, l₃) — vertex angle sum contribution
|
||||
```
|
||||
|
||||
**Hessian:** currently a symmetric finite-difference approximation (see `hyper_ideal_hessian.hpp`).
|
||||
The analytic Hessian through the `(bᵢ, aₑ) → lᵢⱼ → ζ → αᵢⱼ/βᵢ` chain is Phase 9b.
|
||||
|
||||
**Holonomy:** Möbius isometries Tᵢ ∈ SU(1,1) (orientation-preserving isometries of the Poincaré disk).
|
||||
`MobiusMap` in `layout.hpp`: T(z) = (az+b)/(cz+d).
|
||||
|
||||
**Normalisation:** `normalise_hyperbolic()` performs iterative face-area-weighted Möbius centring
|
||||
(Fréchet mean, 30 iterations) to map the weighted centroid to the disk origin.
|
||||
|
||||
---
|
||||
|
||||
## Gauss–Bonnet constraint
|
||||
|
||||
Before calling any Newton solver, the target angles must satisfy the Gauss–Bonnet equation:
|
||||
|
||||
```
|
||||
Σᵥ (2π − Θᵥ) = 2π · χ(M) (Euclidean / flat)
|
||||
Σᵥ (2π − Θᵥ) > 0 (spherical, χ > 0)
|
||||
Σᵥ (2π − Θᵥ) < 0 (hyperbolic, χ < 0)
|
||||
```
|
||||
|
||||
If this is violated, no conformal factor can realise the target angles and Newton will
|
||||
fail to converge without a visible error.
|
||||
|
||||
```cpp
|
||||
check_gauss_bonnet(mesh, maps); // throws if violated
|
||||
enforce_gauss_bonnet(mesh, maps); // redistributes residual uniformly across all vertices
|
||||
```
|
||||
|
||||
**This is the most common source of silent non-convergence.** Always call one of these before solving.
|
||||
143
doc/math/novelty-statement.md
Normal file
143
doc/math/novelty-statement.md
Normal file
@@ -0,0 +1,143 @@
|
||||
# Scientific Novelty Statement
|
||||
|
||||
> **Purpose.** This document explicitly states what conformallab++ contributes
|
||||
> that no other open-source C++ library provides, and for which research problems
|
||||
> it is the right tool. It is intended as a reference for paper introductions,
|
||||
> grant applications, and collaborator onboarding.
|
||||
|
||||
---
|
||||
|
||||
## 1 — The one-sentence statement
|
||||
|
||||
conformallab++ is the **only open-source C++ library** that implements discrete
|
||||
conformal equivalence in all three geometric settings (Euclidean, Spherical,
|
||||
Hyper-ideal), with a complete downstream Teichmüller pipeline — period matrix
|
||||
τ ∈ ℍ, Möbius holonomy, and fundamental domain construction — in a single
|
||||
cohesive codebase targeting the CGAL ecosystem.
|
||||
|
||||
---
|
||||
|
||||
## 2 — Unique features (no equivalent elsewhere in C++)
|
||||
|
||||
### 2.1 — Three geometry modes in one library
|
||||
|
||||
| Mode | Space | Energy | Application |
|
||||
|---|---|---|---|
|
||||
| Euclidean | ℝ² | Σ log(ℓᵢⱼ/ℓ̃ᵢⱼ)² | Flat torus uniformization, texture atlasing |
|
||||
| Spherical | S² | NSD variant | Constant positive curvature, Koebe's theorem |
|
||||
| HyperIdeal | H² (Poincaré disk) | Springborn 2020 ζ-functions | Hyperbolic surfaces, genus g ≥ 2 |
|
||||
|
||||
No other open-source C++ library implements all three. geometry-central
|
||||
(CMU) has Euclidean and partial HyperIdeal but lacks the Spherical mode entirely.
|
||||
|
||||
### 2.2 — Period matrix τ with SL(2,ℤ) reduction
|
||||
|
||||
For a closed genus-1 surface, conformallab++ computes the complex modulus
|
||||
τ = ω_b/ω_a ∈ ℍ from the holonomy of the uniformizing flat metric, then reduces
|
||||
τ to the standard fundamental domain
|
||||
|
||||
```
|
||||
F = { τ ∈ ℍ : |τ| ≥ 1, |Re(τ)| ≤ 1/2 }
|
||||
```
|
||||
|
||||
via the SL(2,ℤ) action. This identifies the conformal class of the surface in
|
||||
Teichmüller space T₁ ≅ ℍ/SL(2,ℤ).
|
||||
|
||||
**No other open-source C++ library computes τ.** The Java ConformalLab does,
|
||||
but requires the JVM and is not integrated with any modern mesh processing framework.
|
||||
|
||||
### 2.3 — Möbius holonomy in SU(1,1)
|
||||
|
||||
The holonomy representation ρ: π₁(Σ) → SU(1,1) is computed for closed surfaces
|
||||
of any genus. For the torus this gives the lattice generators ω_a, ω_b ∈ ℂ.
|
||||
For hyperbolic surfaces this gives deck transformations as Möbius maps acting on
|
||||
the Poincaré disk.
|
||||
|
||||
### 2.4 — Tree-cotree cut graph (Erickson–Whittlesey)
|
||||
|
||||
For a closed surface of genus g, the cut graph produces exactly 2g seam edges
|
||||
that cut the surface to a disk. This is required for layout, holonomy computation,
|
||||
and fundamental domain construction. The cut graph is not present in any other
|
||||
C++ conformal geometry library.
|
||||
|
||||
### 2.5 — Fundamental domain and tiling
|
||||
|
||||
From the holonomy generators, conformallab++ constructs the fundamental domain
|
||||
parallelogram and its lattice tiling for genus-1 surfaces. This is the discrete
|
||||
analog of the classical construction of a torus as ℂ/Λ.
|
||||
|
||||
---
|
||||
|
||||
## 3 — What makes this a research tool, not just an implementation
|
||||
|
||||
### 3.1 — Variational framework, not heuristic
|
||||
|
||||
The energy functionals are derived from first principles (Bobenko–Springborn 2004).
|
||||
The Newton solver guarantees quadratic convergence to the *global* optimum for
|
||||
Euclidean and HyperIdeal modes (strict convexity). The solution is mathematically
|
||||
unique (up to Möbius normalisation) — not an approximation.
|
||||
|
||||
### 3.2 — Analytic Hessians
|
||||
|
||||
For Euclidean and Spherical modes, the Hessian is computed analytically from the
|
||||
cotangent Laplacian and its spherical analog. This gives exact derivatives, not
|
||||
finite-difference approximations, which is required for reproducible research.
|
||||
|
||||
### 3.3 — Discrete-to-smooth correspondence
|
||||
|
||||
The discrete period matrix τ_discrete is a computable invariant of the triangulated
|
||||
surface. Its convergence to the smooth Riemannian τ_smooth under mesh refinement
|
||||
is an open research question that this library is designed to investigate.
|
||||
|
||||
### 3.4 — Full test coverage of analytic invariants
|
||||
|
||||
173 CGAL tests verify mathematically provable properties:
|
||||
- Gauss–Bonnet: Σ(2π−Θᵥ) = 2π·χ(M) to machine precision
|
||||
- τ ∈ fundamental domain: three inequalities
|
||||
- Holonomy closure: [T_a, T_b] = Id (abelian for genus 1)
|
||||
- Gradient consistency: FD check at ε = 1e-5 for all three functionals
|
||||
|
||||
These are not regression tests — they verify mathematical correctness independently
|
||||
of the input mesh.
|
||||
|
||||
---
|
||||
|
||||
## 4 — Target audience
|
||||
|
||||
| Audience | Primary use |
|
||||
|---|---|
|
||||
| Discrete differential geometers | Computing τ, holonomy, uniformization for theoretical examples |
|
||||
| Computational mathematicians | Benchmarking discrete-to-smooth convergence of τ |
|
||||
| CGAL developers | Extending the CGAL parameterization package (Phase 8) |
|
||||
| Computer graphics researchers | Conformal texture atlasing with exact angle preservation |
|
||||
| Algebraic geometers | Numerical experiments on moduli spaces of tori |
|
||||
|
||||
---
|
||||
|
||||
## 5 — Relationship to the Java original
|
||||
|
||||
conformallab++ is a port of Stefan Sechelmann's Java ConformalLab (TU Berlin,
|
||||
~850 commits, v1.0.0 2018, LGPL). The port:
|
||||
|
||||
- Replaces the custom Java halfedge structure (`CoHDS`) with `CGAL::Surface_mesh`
|
||||
- Replaces JUnit tests with GTest + CGAL test format (173 tests)
|
||||
- Adds Doxygen API documentation, CMake build, and CLI
|
||||
- Is MIT licensed (the Java original is LGPL)
|
||||
- Targets submission to the CGAL library as package `Discrete_conformal_map`
|
||||
|
||||
The mathematics is identical to the Java original. The C++ implementation is
|
||||
independently validated by the test suite and by agreement with Java outputs on
|
||||
shared test meshes (cathead, brezel, torus family).
|
||||
|
||||
---
|
||||
|
||||
## 6 — What conformallab++ is not
|
||||
|
||||
- **Not a mesh processing library.** It operates on existing triangulated surfaces.
|
||||
Remeshing, smoothing, and simplification are outside its scope.
|
||||
- **Not a real-time renderer.** The Newton solver is accurate but not optimised
|
||||
for interactive frame rates (though it converges in < 1 second for typical meshes).
|
||||
- **Not a distortion-minimisation tool.** It computes the unique conformally
|
||||
equivalent metric, not a least-distortion UV map. Use libigl for the latter.
|
||||
- **Not complete for genus g ≥ 2.** The Siegel period matrix Ω and full
|
||||
uniformization for higher genus are Phase 10 research targets, not yet implemented.
|
||||
56
doc/math/references.md
Normal file
56
doc/math/references.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# References
|
||||
|
||||
## Primary source
|
||||
|
||||
This library implements the algorithms from:
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| **Sechelmann** — *Variational Methods for Discrete Surface Parameterization: Applications and Implementation*, Doctoral thesis, TU Berlin 2016 | The complete mathematical foundation: discrete conformal equivalence, variational angle-sum functionals, Newton solver, uniformization, period matrices, holonomy. DOI: [10.14279/depositonce-5415](https://depositonce.tu-berlin.de/items/8e2988b2-d991-45b5-aad5-9fb7988f3b2f) · CC BY-SA 4.0 |
|
||||
|
||||
Java reference implementation: [github.com/varylab/conformallab](https://github.com/varylab/conformallab)
|
||||
|
||||
---
|
||||
|
||||
## References by module
|
||||
|
||||
| Reference | Used in |
|
||||
|---|---|
|
||||
| **Springborn** — *Ideal Hyperbolic Polyhedra and Discrete Uniformization*, Discrete & Computational Geometry (2020) | `hyper_ideal_geometry.hpp` — ζ₁₃/ζ₁₄/ζ₁₅ functions; `hyper_ideal_functional.hpp` |
|
||||
| **Pinkall, Polthier** — *Computing Discrete Minimal Surfaces and Their Conjugates*, Experimental Mathematics (1993) | `euclidean_hessian.hpp` — cotangent Laplacian |
|
||||
| **Bobenko, Springborn** — *Variational Principles for Circle Patterns and Koebe's Theorem*, Transactions AMS (2004) | Variational angle-sum framework underlying all three functionals |
|
||||
| **Luo** — *Combinatorial Yamabe Flow on Surfaces*, Communications in Contemporary Mathematics (2004) | Inversive-distance functional — **not yet ported**, Phase 9a |
|
||||
| **Erickson, Whittlesey** — *Greedy Optimal Homotopy and Homology Generators*, SODA (2005) | `cut_graph.hpp` — tree-cotree algorithm |
|
||||
| **Bobenko, Springborn** — *A Discrete Laplace–Beltrami Operator for Simplicial Surfaces*, Discrete & Computational Geometry (2007) | Background for cotangent weights |
|
||||
| **Desbrun, Kanso, Tong** — *Discrete Differential Forms for Computational Modeling*, SIGGRAPH Course Notes (2006) | Discrete exterior calculus background for Phase 10a |
|
||||
|
||||
---
|
||||
|
||||
## geometry-central cross-reference *(optional comparison track)*
|
||||
|
||||
> Diese Referenzen beziehen sich auf eine alternative Implementierung desselben
|
||||
> mathematischen Problems. Sie sind keine Voraussetzung für conformallab++,
|
||||
> aber relevant für Kreuz-Validierung und mögliche algorithmische Adoptionen
|
||||
> (→ GC-1/2/3 im Phasen-Roadmap, → Abschnitt 9 in `validation.md`).
|
||||
|
||||
| Reference | Relevanz |
|
||||
|---|---|
|
||||
| **Gillespie, Springborn, Crane** — *Discrete Conformal Equivalence of Polyhedral Surfaces*, ACM SIGGRAPH 2021. DOI: [10.1145/3450626.3459763](https://doi.org/10.1145/3450626.3459763) | Implementiert in **geometry-central**. Erweitert Springborn 2020 um intrinsische Triangulierungen und Ptolemäische Flips. Löst dasselbe DCE-Problem wie conformallab++, aber mit anderem Algorithmus. |
|
||||
| **Sharp, Soliman, Crane** — *Navigating Intrinsic Triangulations*, ACM SIGGRAPH 2019 | Algorithmische Grundlage für `SignpostIntrinsicTriangulation` in geometry-central — relevant für GC-2 (optionales Pre-Conditioning). |
|
||||
|
||||
**Hinweis zu Springborn 2020:**
|
||||
Das Papier *"Ideal Hyperbolic Polyhedra and Discrete Uniformization"*
|
||||
(Springborn, Discrete & Computational Geometry 2020) ist **in conformallab++
|
||||
bereits implementiert** — es ist die direkte Referenz für den HyperIdeal-Geometriemodus
|
||||
(`hyper_ideal_geometry.hpp`). Die geometry-central Implementierung (Gillespie 2021)
|
||||
baut auf diesem Papier auf und ergänzt es um Ptolemäische Flips.
|
||||
|
||||
---
|
||||
|
||||
## Phase 10 references (future research)
|
||||
|
||||
| Reference | Relevant for |
|
||||
|---|---|
|
||||
| **Farkas, Kra** — *Riemann Surfaces*, Springer GTM 71 | Siegel period matrix, Teichmüller theory |
|
||||
| **Siegel** — *Topics in Complex Function Theory, Vol. 2*, Wiley | Siegel upper half-space H_g, Sp(2g,ℤ) reduction |
|
||||
| **Bobenko, Mercat, Schmies** — *Period Matrices of Polyhedral Surfaces*, in: Computational Approach to Riemann Surfaces (2011) | Discrete period matrices on polyhedral surfaces |
|
||||
128
doc/math/software-landscape.md
Normal file
128
doc/math/software-landscape.md
Normal file
@@ -0,0 +1,128 @@
|
||||
# Software Landscape — Discrete Conformal Geometry Tools
|
||||
|
||||
> **Purpose.** A mathematician evaluating conformallab++ needs to know how it
|
||||
> relates to existing tools. This document maps the full landscape and explains
|
||||
> why no existing library covers the same ground.
|
||||
|
||||
---
|
||||
|
||||
## 1 — The two problems that look the same but are not
|
||||
|
||||
The term "conformal parameterization" covers two fundamentally different problems:
|
||||
|
||||
### Problem A — Conformal distortion minimization (LSCM / ABF++ / ARAP)
|
||||
|
||||
Find a UV map u: V → ℝ² that *minimises* a measure of angle distortion.
|
||||
This is an unconstrained or lightly constrained optimisation over UV coordinates.
|
||||
The solution depends on the embedding in ℝ³ and is **not unique** — it minimises
|
||||
distortion but does not assign the surface to a canonical conformal class.
|
||||
|
||||
Tools: **libigl, CGAL Surface_parameterization, pmp-library, Blender, MeshLab**.
|
||||
|
||||
### Problem B — Discrete conformal equivalence (DCE)
|
||||
|
||||
Find scale factors u ∈ ℝᵛ such that the rescaled metric
|
||||
ℓ̃ᵢⱼ = e^{(uᵢ+uⱼ)/2} · ℓᵢⱼ has prescribed cone angles Θᵥ at every vertex.
|
||||
This is a variational problem on the *intrinsic metric* — independent of any
|
||||
embedding. The solution is **unique** (up to a global Möbius transformation)
|
||||
and places the surface in its canonical position in Teichmüller space.
|
||||
|
||||
Tools: **conformallab++, geometry-central (partial), original Java ConformalLab**.
|
||||
|
||||
> **This distinction matters.** A UV map from LSCM minimises distortion but
|
||||
> cannot be used to compute the period matrix τ ∈ ℍ. A DCE solution can.
|
||||
|
||||
---
|
||||
|
||||
## 2 — Full comparison table
|
||||
|
||||
| Library | Lang | DCE solver | Spherical | HyperIdeal | Cut graph | Holonomy | Period τ | Open source |
|
||||
|---|---|---|---|---|---|---|---|---|
|
||||
| **conformallab++** | C++17 | Newton (quad.) | ✓ | ✓ | ✓ (tree-cotree) | ✓ SU(1,1) | ✓ SL(2,ℤ) | ✓ MIT |
|
||||
| Java ConformalLab | Java 8 | Newton | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ LGPL |
|
||||
| geometry-central | C++17 | Newton / Yamabe | ✗ | ✓ (partial) | ✗ | ✗ | ✗ | ✓ MIT |
|
||||
| libigl | C++14 | LSCM / ARAP¹ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ MPL-2 |
|
||||
| CGAL Parameterization | C++ | LSCM / Orbifold¹ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ GPL/LGPL |
|
||||
| pmp-library | C++17 | harmonic / param.¹ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ MIT |
|
||||
| OpenFlipper | C++ | LSCM plugin¹ | ✗ | ✗ | ✗ | ✗ | ✗ | ✓ LGPL |
|
||||
| Matlab geom. toolbox | MATLAB | LSCM / ABF++¹ | ✗ | ✗ | ✗ | ✗ | ✗ | commercial |
|
||||
|
||||
¹ These are Problem-A methods (distortion minimisation), not DCE.
|
||||
|
||||
---
|
||||
|
||||
## 3 — Detailed comparison: conformallab++ vs. Java ConformalLab
|
||||
|
||||
conformallab++ is a C++17 reimplementation of the Java library:
|
||||
|
||||
| Aspect | Java ConformalLab | conformallab++ |
|
||||
|---|---|---|
|
||||
| Language | Java 8 | C++17 |
|
||||
| Mesh type | Custom `CoHDS` halfedge | `CGAL::Surface_mesh` |
|
||||
| Build system | Maven | CMake |
|
||||
| Test framework | JUnit 4 | GTest + CGAL test format |
|
||||
| Static linking | JVM required | standalone binary |
|
||||
| CGAL integration | none | native (target: CGAL package) |
|
||||
| Inversive distance | ✓ | planned (Phase 9a) |
|
||||
| Analytic HI Hessian | ✓ | planned (Phase 9b) |
|
||||
| Genus g>1 domain | ✓ partial | planned (Phase 9c) |
|
||||
| Siegel matrix Ω | partial | planned (Phase 10b) |
|
||||
| All three modes | ✓ | ✓ |
|
||||
| Period matrix τ | ✓ | ✓ |
|
||||
| Holonomy | ✓ | ✓ |
|
||||
|
||||
**Phase parity:** Phases 1–7 of conformallab++ cover all core Java features.
|
||||
Phases 9–10 will complete the remaining items. See `doc/roadmap/java-parity.md`
|
||||
for the full feature-by-feature table.
|
||||
|
||||
---
|
||||
|
||||
## 4 — Detailed comparison: conformallab++ vs. geometry-central
|
||||
|
||||
geometry-central (Keenan Crane, CMU) is the closest external peer.
|
||||
Both implement DCE but diverge significantly in scope and algorithm.
|
||||
|
||||
| Dimension | conformallab++ | geometry-central |
|
||||
|---|---|---|
|
||||
| **Solver** | Newton, quadratic convergence | Newton or Yamabe flow |
|
||||
| **Triangulation** | Fixed original mesh | Intrinsic + Ptolemaic flips |
|
||||
| **Spherical geometry** | ✓ (NSD Hessian, sign flip) | ✗ |
|
||||
| **Cut graph** | ✓ tree-cotree, 2g seams | ✗ |
|
||||
| **Holonomy** | ✓ SU(1,1) Möbius maps | ✗ |
|
||||
| **Period matrix** | ✓ τ ∈ ℍ, SL(2,ℤ)-reduced | ✗ |
|
||||
| **Fundamental domain** | ✓ genus 1 complete | ✗ |
|
||||
| **Mesh backend** | CGAL `Surface_mesh` | gc `ManifoldSurfaceMesh` |
|
||||
| **CGAL integration** | ✓ (target: package) | ✗ |
|
||||
|
||||
For a full analysis including adoption candidates and scientific added value,
|
||||
see `doc/architecture/geometry-central-comparison.md`.
|
||||
|
||||
---
|
||||
|
||||
## 5 — What Problem-A tools cannot do
|
||||
|
||||
The following tasks require DCE (Problem B) and cannot be done with LSCM/ARAP:
|
||||
|
||||
| Task | Requires |
|
||||
|---|---|
|
||||
| Compute the period matrix τ of a torus | DCE + holonomy + period matrix |
|
||||
| Classify a surface in Teichmüller space | DCE |
|
||||
| Construct a flat metric with prescribed cone angles | DCE |
|
||||
| Tile a surface by a lattice (fundamental domain) | DCE + cut graph + holonomy |
|
||||
| Compare two surfaces conformally | DCE (same conformal class ↔ same τ) |
|
||||
| Uniformize a hyperbolic surface (genus g ≥ 2) | HyperIdeal DCE |
|
||||
| Compute holomorphic differentials (Phase 10) | DCE + cut graph + integration |
|
||||
|
||||
---
|
||||
|
||||
## 6 — When to use which tool
|
||||
|
||||
| Goal | Recommended tool |
|
||||
|---|---|
|
||||
| Fast UV unwrapping for texture mapping | libigl LSCM or pmp-library |
|
||||
| Angle-preserving parameterization, distortion study | CGAL Surface_parameterization |
|
||||
| Discrete conformal equivalence, research | **conformallab++** |
|
||||
| DCE with maximum numerical robustness on bad meshes | geometry-central (+ Ptolemaic flips) |
|
||||
| Full Teichmüller pipeline (τ, holonomy, domain) | **conformallab++** only |
|
||||
| Interactive exploration (viewer) | conformallab++ (`-DWITH_CGAL=ON`) |
|
||||
| Java ecosystem / existing ConformalLab workflow | Java ConformalLab |
|
||||
213
doc/math/validation-protocol.md
Normal file
213
doc/math/validation-protocol.md
Normal file
@@ -0,0 +1,213 @@
|
||||
# Validation Protocol
|
||||
|
||||
Concrete, reproducible steps to verify the mathematical correctness of
|
||||
conformallab++. Every check below has a deterministic expected outcome.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_CGAL=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build --target conformallab_cgal_tests -j$(nproc)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Check 0 — All tests pass
|
||||
|
||||
```bash
|
||||
ctest --test-dir build -R "^cgal\." --output-on-failure
|
||||
```
|
||||
|
||||
**Expected output (last lines):**
|
||||
```
|
||||
100% tests passed, 0 tests failed out of 170
|
||||
|
||||
The following tests did not run:
|
||||
206 - cgal.HomologyGenerators.Genus2_FourGeneratorPaths_BLOCKED (Skipped)
|
||||
```
|
||||
|
||||
If any test fails, stop — the implementation is broken.
|
||||
|
||||
---
|
||||
|
||||
## Check 1 — Gauss–Bonnet (topological identity, error < 1e-10)
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="GaussBonnet.*" -v
|
||||
```
|
||||
|
||||
**Expected:** all 8 tests `[ PASSED ]`
|
||||
|
||||
What is verified: for each test mesh (tetrahedron χ=2, torus χ=0, open mesh χ=1):
|
||||
|
||||
```
|
||||
Σᵥ (2π − Θᵥ) = 2π · χ(M) ± 1e-10
|
||||
```
|
||||
|
||||
This is a **pure topology check** — it fails only if vertex/face counts or
|
||||
property-map assignments are wrong. It does not depend on the Newton solver.
|
||||
|
||||
---
|
||||
|
||||
## Check 2 — Euclidean gradient consistency (FD vs. analytic, error < 1e-6)
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="EuclideanFunctional.GradientCheck*" -v
|
||||
```
|
||||
|
||||
**Expected:** all `GradientCheck_*` tests `[ PASSED ]`
|
||||
|
||||
What is verified: for ε = 1e-5,
|
||||
|
||||
```
|
||||
|G(u)ᵢ − (E(u + εeᵢ) − E(u − εeᵢ)) / (2ε)| < 1e-6
|
||||
```
|
||||
|
||||
This check **proves that the energy and its gradient are mathematically consistent**.
|
||||
A failing FD check means Newton will converge to the wrong point — it is the most
|
||||
important correctness check for any new functional.
|
||||
|
||||
Also run for Spherical and HyperIdeal:
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="SphericalFunctional.GradientCheck*" -v
|
||||
./build/conformallab_cgal_tests --gtest_filter="HyperIdealFunctional.GradientCheck*" -v
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Check 3 — Newton convergence on canonical test meshes
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="NewtonSolver.*" -v
|
||||
```
|
||||
|
||||
**Expected:** all 11 tests `[ PASSED ]`
|
||||
|
||||
Each test verifies:
|
||||
- `res.converged == true`
|
||||
- `res.grad_inf_norm < 1e-8`
|
||||
- `res.iterations < 50` (typically 5–20 for the small test meshes)
|
||||
|
||||
---
|
||||
|
||||
## Check 4 — Period matrix: SL(2,ℤ)-reduction invariants
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="PeriodMatrix.*" -v
|
||||
```
|
||||
|
||||
**Expected:** all 7 tests `[ PASSED ]`
|
||||
|
||||
The three mathematical invariants checked for **any** genus-1 output τ:
|
||||
|
||||
| Property | Condition | Why |
|
||||
|---|---|---|
|
||||
| Upper half-plane | `Im(τ) > 0` | τ encodes a positive-area lattice |
|
||||
| Outside unit disk | `|τ| ≥ 1 − 1e-10` | SL(2,ℤ) reduction step |
|
||||
| Vertical strip | `|Re(τ)| ≤ 0.5 + 1e-10` | SL(2,ℤ) reduction step |
|
||||
|
||||
These hold for **any** well-formed genus-1 mesh — they are topology, not geometry.
|
||||
|
||||
---
|
||||
|
||||
## Check 5 — Möbius arithmetic (complex analysis correctness)
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="MobiusMap.*" -v
|
||||
```
|
||||
|
||||
**Expected:** all 8 tests `[ PASSED ]`
|
||||
|
||||
What is verified:
|
||||
- `T ∘ T⁻¹ = Id` (inverse is correct)
|
||||
- `(T₁ ∘ T₂)(z) = T₁(T₂(z))` (composition is associative)
|
||||
- `from_three(z₁, z₂, z₃)` maps z₁→0, z₂→1, z₃→∞ (unique Möbius transformation)
|
||||
|
||||
A bug here would corrupt **all** hyperbolic holonomy computation.
|
||||
|
||||
---
|
||||
|
||||
## Check 6 — End-to-end pipeline (build + solve + layout)
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="Pipeline.*" -v
|
||||
```
|
||||
|
||||
**Expected:** all 5 tests `[ PASSED ]`
|
||||
|
||||
What is verified: starting from a mesh file, the full pipeline
|
||||
(setup → Gauss-Bonnet → Newton → layout → serialise → reload)
|
||||
produces a consistent result.
|
||||
|
||||
---
|
||||
|
||||
## Check 7 — Manual torus τ verification
|
||||
|
||||
This check requires adding a small program (or modifying an existing test).
|
||||
It validates that `torus_4x4.off` produces τ in the fundamental domain:
|
||||
|
||||
```cpp
|
||||
#include "conformal_mesh.hpp"
|
||||
#include "euclidean_functional.hpp"
|
||||
#include "newton_solver.hpp"
|
||||
#include "cut_graph.hpp"
|
||||
#include "layout.hpp"
|
||||
#include "period_matrix.hpp"
|
||||
#include "mesh_io.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
conformallab::ConformalMesh mesh;
|
||||
conformallab::load_mesh(mesh, "code/data/off/torus_4x4.off");
|
||||
|
||||
auto maps = conformallab::setup_euclidean_maps(mesh);
|
||||
conformallab::compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
conformallab::enforce_gauss_bonnet(mesh, maps);
|
||||
|
||||
auto res = conformallab::newton_euclidean(mesh, std::vector<double>(maps.n_dof, 0.0), maps);
|
||||
std::cout << "Converged: " << res.converged
|
||||
<< " iterations: " << res.iterations
|
||||
<< " |G|∞: " << res.grad_inf_norm << "\n";
|
||||
|
||||
auto cg = conformallab::compute_cut_graph(mesh);
|
||||
conformallab::HolonomyData hol;
|
||||
conformallab::euclidean_layout(mesh, res.x, maps, &cg, &hol, true);
|
||||
auto pd = conformallab::compute_period_matrix(hol);
|
||||
|
||||
std::cout << "τ = " << pd.tau_reduced.real()
|
||||
<< " + " << pd.tau_reduced.imag() << "i\n";
|
||||
std::cout << "|τ| = " << std::abs(pd.tau_reduced) << "\n";
|
||||
std::cout << "|Re(τ)| = " << std::abs(pd.tau_reduced.real()) << "\n";
|
||||
}
|
||||
```
|
||||
|
||||
**Expected output (torus_4x4.off, R=2, r=1 torus of revolution):**
|
||||
```
|
||||
Converged: 1 iterations: <30 |G|∞: <1e-8
|
||||
τ = [small] + [positive]i (Re close to 0 by 4-fold symmetry)
|
||||
|τ| ≥ 1.0 (fundamental domain)
|
||||
|Re(τ)| ≤ 0.5 (fundamental domain)
|
||||
```
|
||||
|
||||
The exact value of Im(τ) depends on the 3D embedding (R=2, r=1 gives unequal
|
||||
inner/outer edge lengths). Use `torus_8x8.off` for a finer approximation.
|
||||
|
||||
---
|
||||
|
||||
## Summary checklist
|
||||
|
||||
```
|
||||
[ ] Check 0: 170 tests pass, 1 skip
|
||||
[ ] Check 1: Gauss–Bonnet exact (1e-10)
|
||||
[ ] Check 2: FD gradient < 1e-6 for all 3 geometries
|
||||
[ ] Check 3: Newton convergence < 50 iterations
|
||||
[ ] Check 4: τ in SL(2,ℤ) fundamental domain
|
||||
[ ] Check 5: Möbius arithmetic (inverse, compose, from_three)
|
||||
[ ] Check 6: End-to-end pipeline
|
||||
[ ] Check 7: Torus τ in upper half-plane with correct symmetry
|
||||
```
|
||||
|
||||
All checks are deterministic and do not depend on random initialization or
|
||||
floating-point non-determinism beyond standard IEEE-754.
|
||||
271
doc/math/validation.md
Normal file
271
doc/math/validation.md
Normal file
@@ -0,0 +1,271 @@
|
||||
# Mathematical Validation
|
||||
|
||||
This document lists analytically known results and explains how to verify
|
||||
them against conformallab++ output. It is the primary tool for an independent
|
||||
mathematician to check the correctness of the implementation.
|
||||
|
||||
---
|
||||
|
||||
## How to run the examples
|
||||
|
||||
```bash
|
||||
cmake -S code -B build -DWITH_CGAL=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build --target conformallab_cgal_tests
|
||||
ctest --test-dir build -R cgal --output-on-failure
|
||||
```
|
||||
|
||||
All 173 tests pass (1 skipped by design — see `doc/api/tests.md`).
|
||||
|
||||
---
|
||||
|
||||
## 1 — Gauss–Bonnet (topology)
|
||||
|
||||
**Theorem.** For any closed triangulated surface M,
|
||||
|
||||
```
|
||||
Σᵥ (2π − Θᵥ) = 2π · χ(M)
|
||||
```
|
||||
|
||||
where χ(M) = 2 − 2g is the Euler characteristic.
|
||||
|
||||
| Surface | g | χ | Σ(2π − Θᵥ) |
|
||||
|---|---|---|---|
|
||||
| Sphere (tetrahedron, cube, …) | 0 | 2 | 4π |
|
||||
| Torus | 1 | 0 | 0 |
|
||||
| Double torus | 2 | −2 | −4π |
|
||||
|
||||
**How to check:**
|
||||
|
||||
```cpp
|
||||
#include "gauss_bonnet.hpp"
|
||||
auto defect = gauss_bonnet_sum(mesh, maps); // Σ(2π − Θᵥ)
|
||||
auto chi = mesh.euler_characteristic();
|
||||
EXPECT_NEAR(defect, 2.0 * M_PI * chi, 1e-10);
|
||||
```
|
||||
|
||||
Covered by: `cgal.GaussBonnet.*` tests in `test_phase6.cpp`.
|
||||
|
||||
---
|
||||
|
||||
## 2 — Period matrix: fundamental domain invariants
|
||||
|
||||
**Theorem (SL(2,ℤ)-reduction).** Every lattice τ ∈ ℍ has a unique representative
|
||||
in the standard fundamental domain
|
||||
|
||||
```
|
||||
F = { τ ∈ ℍ : |τ| ≥ 1, |Re(τ)| ≤ 1/2, Im(τ) > 0 }
|
||||
```
|
||||
|
||||
**After calling `compute_period_matrix(hol)`, the returned τ must satisfy:**
|
||||
|
||||
| Condition | Invariant |
|
||||
|---|---|
|
||||
| `pd.tau_reduced.imag() > 0` | τ lies in the upper half-plane |
|
||||
| `std::abs(pd.tau_reduced) >= 1.0 - 1e-10` | τ outside unit disk |
|
||||
| `std::abs(pd.tau_reduced.real()) <= 0.5 + 1e-10` | τ in vertical strip |
|
||||
|
||||
These three conditions hold for **any** closed genus-1 triangulated surface
|
||||
processed through Euclidean uniformization — they are topology, not geometry.
|
||||
|
||||
Covered by: `cgal.PeriodMatrix.TauInFundamentalDomain_*` tests in `test_phase7.cpp`.
|
||||
|
||||
---
|
||||
|
||||
## 3 — Square-symmetric torus
|
||||
|
||||
**Setup.** Take a torus mesh with 4-fold rotational symmetry around the z-axis
|
||||
(e.g. `code/data/off/torus_4x4.off`, which has M=4 columns of vertices).
|
||||
|
||||
**Expected.** The symmetry group Z₄ acts conformally. Conformal automorphisms
|
||||
of the torus correspond to SL(2,ℤ) symmetries of τ. The unique fixed point of
|
||||
a rotation of order 4 in the modular group is τ = i. Therefore:
|
||||
|
||||
```
|
||||
For a mesh with exact 4-fold symmetry and uniform edge lengths:
|
||||
Re(τ) = 0 (to machine precision, by symmetry)
|
||||
Im(τ) ≈ 1 (approaches 1 as mesh is refined)
|
||||
```
|
||||
|
||||
The coarse 4×4 mesh (`torus_4x4.off`) gives Im(τ) in (0.7, 1.3) depending on
|
||||
the 3D embedding (R=2, r=1 torus of revolution has unequal inner/outer edge lengths).
|
||||
The uniformization algorithm finds the conformal class of the *abstract* metric
|
||||
encoded in the edge lengths.
|
||||
|
||||
**Manual verification** (run from the build directory after adding a small
|
||||
program or reading from the test output):
|
||||
|
||||
```cpp
|
||||
ConformalMesh mesh; load_mesh(mesh, "code/data/off/torus_4x4.off");
|
||||
EuclideanMaps maps = setup_euclidean_maps(mesh);
|
||||
compute_euclidean_lambda0_from_mesh(mesh, maps);
|
||||
enforce_gauss_bonnet(mesh, maps);
|
||||
auto res = newton_euclidean(mesh, maps);
|
||||
CutGraph cg = compute_cut_graph(mesh);
|
||||
HolonomyData hol;
|
||||
euclidean_layout(mesh, res.x, maps, &cg, &hol, true);
|
||||
PeriodData pd = compute_period_matrix(hol);
|
||||
// pd.tau_reduced satisfies the fundamental domain invariants above
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4 — Hexagonal-symmetric torus
|
||||
|
||||
**Setup.** Take a torus mesh with 6-fold rotational symmetry
|
||||
(`code/data/off/torus_hex_6x6.off`, M=6).
|
||||
|
||||
**Expected.** The unique τ fixed under a rotation of order 6 in SL(2,ℤ) is
|
||||
τ = e^{iπ/3} = ½ + i√3/2. So:
|
||||
|
||||
```
|
||||
Re(τ) = 0.5 (to machine precision, by symmetry)
|
||||
Im(τ) = √3/2 ≈ 0.8660
|
||||
```
|
||||
|
||||
The coarse 6×6 torus of revolution approximates this: Re(τ) ≈ 0.5 by symmetry,
|
||||
Im(τ) approaches √3/2 as the mesh is refined toward a flat hexagonal lattice.
|
||||
|
||||
---
|
||||
|
||||
## 5 — Newton convergence rate
|
||||
|
||||
**Theorem.** Because the Euclidean and hyper-ideal energies are strictly convex
|
||||
(after gauge-fixing), Newton's method converges quadratically near the optimum.
|
||||
|
||||
**Expected:** for any mesh with up to a few hundred faces, Newton converges in
|
||||
**fewer than 30 iterations** starting from u = 0.
|
||||
|
||||
```cpp
|
||||
auto res = newton_euclidean(mesh, maps);
|
||||
EXPECT_LT(res.iterations, 30);
|
||||
EXPECT_LT(res.gradient_norm, 1e-10);
|
||||
```
|
||||
|
||||
Covered by: `cgal.EuclideanPipeline.ConvRates_*` and similar tests.
|
||||
|
||||
---
|
||||
|
||||
## 6 — Gradient check (finite differences)
|
||||
|
||||
For each functional F(u), the gradient G = ∂F/∂u is verified by:
|
||||
|
||||
```
|
||||
|G(u)ᵢ − (F(u + εeᵢ) − F(u − εeᵢ)) / (2ε)| < 1e-6
|
||||
```
|
||||
|
||||
with ε = 1e-5. This check is run **inside the test suite** for all three
|
||||
geometries (Euclidean, Spherical, HyperIdeal) at u = 0 and at random u.
|
||||
|
||||
Relevant test suites:
|
||||
```
|
||||
cgal.EuclideanFunctional.GradientCheck_*
|
||||
cgal.SphericalFunctional.GradientCheck_*
|
||||
cgal.HyperIdealFunctional.GradientCheck_*
|
||||
```
|
||||
|
||||
A failing gradient check means the energy and its derivative are inconsistent —
|
||||
the Newton solver will converge to the wrong point.
|
||||
|
||||
---
|
||||
|
||||
## 7 — Holonomy composition (Möbius maps)
|
||||
|
||||
For a closed surface, the composition of holonomies around any contractible cycle
|
||||
must be the identity. In genus 1 with a single handle:
|
||||
|
||||
```
|
||||
T₁ · T₂ · T₁⁻¹ · T₂⁻¹ = Id (commutator = Id for a torus)
|
||||
```
|
||||
|
||||
because π₁(T²) = ℤ × ℤ is abelian.
|
||||
|
||||
For genus g ≥ 2, the fundamental group is non-abelian and this check does not hold,
|
||||
but the representation ρ: π₁(Σ_g) → SU(1,1) must still satisfy the relation
|
||||
|
||||
```
|
||||
[T₁, T₂] · [T₃, T₄] · … = Id (product of g commutators = Id)
|
||||
```
|
||||
|
||||
These are the **holonomy consistency** checks implemented in `test_phase7.cpp`
|
||||
(`cgal.HolonomyData.*`).
|
||||
|
||||
---
|
||||
|
||||
## 9 — Cross-validation with geometry-central *(optional / hypothetical)*
|
||||
|
||||
> **Hinweis:** Dieser Abschnitt beschreibt eine mögliche externe Kreuz-Validierung,
|
||||
> die keine Voraussetzung für die Korrektheit der Implementierung ist.
|
||||
> Sie ist interessant, weil geometry-central denselben mathematischen Kern
|
||||
> implementiert (Gillespie, Springborn, Crane — SIGGRAPH 2021, aufbauend auf
|
||||
> Springborn 2020), aber mit einer anderen algorithmischen Strategie
|
||||
> (Ptolemäische Flips + intrinsische Triangulierungen statt Newton auf der
|
||||
> Original-Triangulierung).
|
||||
|
||||
### Welche Outputs sind vergleichbar?
|
||||
|
||||
| Output | conformallab++ | geometry-central | Vergleichbar? |
|
||||
|---|---|---|---|
|
||||
| u-Vektor (Skalierungsparameter) | `res.x` | `u` nach Yamabe flow | ✓ nach Normalisierung |
|
||||
| UV-Koordinaten | `layout.uv[v]` | konforme Parametrisierung | ✓ bis auf Möbius-Transformation |
|
||||
| Gauss-Bonnet Defekt | `gauss_bonnet_sum()` | implizit via Krümmungsfluss | ✓ (analytisch identisch) |
|
||||
| Anzahl Newton-Iterationen | `res.iterations` | Yamabe-Schritte | ~ (anderer Algorithmus) |
|
||||
| Period-Matrix τ | `pd.tau_reduced` | **nicht vorhanden** | ✗ |
|
||||
| Möbius-Holonomie | `hol.T_a, T_b` | **nicht vorhanden** | ✗ |
|
||||
|
||||
### Normalisierungsabgleich
|
||||
|
||||
Der u-Vektor in conformallab++ hat einen Freiheitsgrad (globale additive Konstante —
|
||||
Eichfreiheit nach Pin-Fixierung). geometry-central kann eine andere Konvention nutzen.
|
||||
Vor dem Vergleich normalisieren:
|
||||
|
||||
```cpp
|
||||
// conformallab++: u zentrieren
|
||||
double mean_u = std::accumulate(x.begin(), x.end(), 0.0) / x.size();
|
||||
std::vector<double> x_norm(x.size());
|
||||
for (int i = 0; i < x.size(); ++i) x_norm[i] = x[i] - mean_u;
|
||||
|
||||
// Dann mit geometry-central u-Vektor (ebenfalls zentriert) vergleichen:
|
||||
// max|x_norm[i] - gc_u[i]| < 1e-8 → identischer Konvergenzpunkt
|
||||
```
|
||||
|
||||
### Wann ist der Vergleich sinnvoll?
|
||||
|
||||
| Zeitpunkt | Was ist möglich |
|
||||
|---|---|
|
||||
| **Jetzt (Phase 7)** | Manueller Vergleich mit denselben `.off`/`.obj` Testnetzen |
|
||||
| **Nach Phase 8** | Automatisiertes Vergleichsskript (Python oder separates C++-Binary) |
|
||||
| **Phase 10 (Forschung)** | Algorithmus-Vergleich: Newton vs. Ptolemäische Flips auf schwierigen Netzen |
|
||||
|
||||
### Voraussetzungen für einen fairen Vergleich
|
||||
|
||||
1. Identische Eingabenetze (OFF/OBJ, gleiche Vertex-Orientierung)
|
||||
2. Gleiche Gauss-Bonnet-Zielkrümmungen (Θᵥ = 2π für alle v, geschlossene Fläche)
|
||||
3. u-Normalisierung abgeglichen (zentriert, gleiche Eichfixierung)
|
||||
4. Konvergenztoleranz synchronisiert (max. Gradientnorm < 1e-8)
|
||||
|
||||
### Verbindung zur Literatur
|
||||
|
||||
Das Springborn 2020-Papier ("Ideal Hyperbolic Polyhedra and Discrete Uniformization")
|
||||
ist **in conformallab++ bereits implementiert** — es ist die mathematische Grundlage
|
||||
für den HyperIdeal-Geometriemodus (Phase 2/3). Die geometry-central Implementierung
|
||||
basiert auf der Weiterentwicklung von Gillespie, Springborn & Crane (2021), die
|
||||
denselben Variationsprinzip von Bobenko–Springborn 2004 verwendet, aber zusätzlich
|
||||
Ptolemäische Flips einsetzt, um die Triangulierung während der Optimierung zu
|
||||
verbessern — eine Idee, die in conformallab++ noch nicht implementiert ist (→ GC-2
|
||||
im Phasen-Roadmap).
|
||||
|
||||
---
|
||||
|
||||
## 8 — Checklist for an independent reviewer
|
||||
|
||||
Run these in order to validate the implementation:
|
||||
|
||||
- [ ] `ctest --test-dir build -R cgal --output-on-failure` → 173 tests pass, 1 skip
|
||||
- [ ] `cgal.GaussBonnet.*` all pass → topology is correctly read from mesh
|
||||
- [ ] `cgal.EuclideanFunctional.GradientCheck_*` pass → energy = integral of gradient
|
||||
- [ ] `cgal.PeriodMatrix.TauInFundamentalDomain_*` pass → SL(2,ℤ) reduction correct
|
||||
- [ ] `cgal.MobiusMap.Compose_*` and `Inverse_*` pass → Möbius arithmetic correct
|
||||
- [ ] `cgal.HolonomyData.*` pass → holonomy loops close up
|
||||
|
||||
All of the above are **deterministic, analytic tests** — no mesh loading, no
|
||||
file I/O, no floating-point non-determinism beyond standard IEEE-754.
|
||||
86
doc/roadmap/java-parity.md
Normal file
86
doc/roadmap/java-parity.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# Java ConformalLab vs. conformallab++ — Feature Parity
|
||||
|
||||
Reference: [github.com/varylab/conformallab](https://github.com/varylab/conformallab)
|
||||
Java package root: `de.varylab.discreteconformal`
|
||||
|
||||
When porting a Java class, locate the original in the Java repository and use it
|
||||
as the reference implementation for expected behaviour, edge cases, and test cases.
|
||||
|
||||
---
|
||||
|
||||
## Algorithm parity
|
||||
|
||||
| Mathematical layer | Java ConformalLab | conformallab++ | Notes |
|
||||
|---|---|---|---|
|
||||
| Euclidean functional — energy, gradient | ✅ | ✅ | |
|
||||
| Spherical functional — energy, gradient, gauge-fix | ✅ | ✅ | |
|
||||
| HyperIdeal functional — energy, gradient | ✅ | ✅ | |
|
||||
| Inversive-distance functional (Luo 2004) | ✅ | ❌ Phase 9a | `InversiveDistanceFunctional.java` |
|
||||
| Euclidean Hessian — cotangent Laplacian | ✅ analytic | ✅ analytic | Pinkall–Polthier (1993) |
|
||||
| Spherical Hessian — ∂α/∂u via law of cosines | ✅ analytic | ✅ analytic | |
|
||||
| HyperIdeal Hessian — ζ → lᵢⱼ → β/α chain | ✅ analytic | ⚠️ symmetric FD | Phase 9b |
|
||||
| Newton solver | ✅ | ✅ | |
|
||||
| SparseQR fallback for gauge modes | unknown | ✅ | New in C++ |
|
||||
| Cone metrics — prescribed Θᵥ ≠ 2π | ✅ fully | ⚠️ data structure only | |
|
||||
| Layout / embedding — ℝ² / H² / S² | ✅ | ✅ priority-BFS all three | |
|
||||
| Exact hyperbolic trilateration | ✅ Möbius | ✅ Möbius + law of cosines | |
|
||||
| halfedge_uv — seam-aware UV (texture atlas) | ✅ | ✅ | |
|
||||
| Gauss–Bonnet consistency check | ✅ | ✅ | |
|
||||
| Tree-cotree cut graph (2g edges) | ✅ | ✅ Erickson–Whittlesey (2005) | |
|
||||
| Holonomy — Euclidean (translations) | ✅ | ✅ | |
|
||||
| Holonomy — Hyperbolic (SU(1,1) Möbius maps) | ✅ | ✅ | |
|
||||
| Period matrix τ — genus 1, SL(2,ℤ)-reduced | ✅ | ✅ | |
|
||||
| Fundamental domain — genus 1 | ✅ | ✅ CCW parallelogram | |
|
||||
| 4g-polygon boundary walk — genus g > 1 | ✅ | ❌ Phase 9c | `FundamentalDomainUtility.java` |
|
||||
| Siegel period matrix Ω — genus g ≥ 2 | ✅ | ❌ Phase 10b | |
|
||||
| Global uniformization — genus g ≥ 2 | ✅ | ❌ Phase 10c | |
|
||||
| Clausen / Lobachevsky / ImLi₂ | ✅ | ✅ | |
|
||||
| Poincaré disk / Lorentz boost visualisation | ✅ | ✅ | |
|
||||
| Mesh I/O + serialisation | ✅ XML/CoHDS | ✅ OFF/OBJ/PLY + JSON/XML | |
|
||||
| Interactive viewer | ✅ jReality | ✅ libigl/GLFW | |
|
||||
|
||||
---
|
||||
|
||||
## Java utility classes not yet ported
|
||||
|
||||
These exist in `de.varylab.discreteconformal.util` in the Java library.
|
||||
They are candidates for Phase 9 or Phase 10.
|
||||
|
||||
| Java class | Description | Phase |
|
||||
|---|---|---|
|
||||
| `InversiveDistanceFunctional` | Inversive-distance conformal energy | 9a |
|
||||
| `DiscreteHarmonicFormUtility` | Discrete harmonic 1-forms | 10a prerequisite |
|
||||
| `DiscreteHolomorphicFormUtility` | Holomorphic differentials on discrete surfaces | 10a |
|
||||
| `DiscreteRiemannUtility` | Discrete Riemann surfaces | 10 |
|
||||
| `CanonicalBasisUtility` | Canonical homology basis for genus g | 9c / 10 |
|
||||
| `HomologyUtility` | Homology computation | 9c |
|
||||
| `HomotopyUtility` | Homotopy generators | 9c |
|
||||
| `SpanningTreeUtility` | Spanning tree algorithms | 8 / infrastructure |
|
||||
| `SurgeryUtility` | Mesh surgery (cut/glue) | — |
|
||||
| `StitchingUtility` | Seam stitching | — |
|
||||
| `CuttingUtility` | Advanced cutting (beyond tree-cotree) | 9c |
|
||||
| `HyperellipticUtility` | Hyperelliptic surfaces | 10 |
|
||||
| `LaplaceUtility` | Discrete Laplace operators | 9 / infrastructure |
|
||||
| `ConformalStructureUtility` | Conformal structure extraction | 10 |
|
||||
|
||||
---
|
||||
|
||||
## HyperIdeal Hessian: FD vs. analytic
|
||||
|
||||
The Java library computes the HyperIdeal Hessian analytically through the chain:
|
||||
|
||||
```
|
||||
(bᵢ, aₑ) → lᵢⱼ → ζ₁₃/ζ₁₄/ζ₁₅ → αᵢⱼ / βᵢ
|
||||
```
|
||||
|
||||
conformallab++ uses a **symmetric finite-difference approximation**:
|
||||
|
||||
```
|
||||
H[i,j] = ( G(x + ε·eⱼ)[i] − G(x − ε·eⱼ)[i] ) / (2ε), ε = 1e-5
|
||||
```
|
||||
|
||||
Accuracy: O(ε²) ≈ 10⁻¹⁰ relative error. PSD guaranteed by strict convexity (Springborn 2020).
|
||||
Cost: n extra gradient evaluations per Newton step.
|
||||
Impact: negligible for meshes < 500 DOFs; measurable for larger meshes.
|
||||
|
||||
The analytic Hessian is deferred to Phase 9b.
|
||||
173
doc/roadmap/phases.md
Normal file
173
doc/roadmap/phases.md
Normal file
@@ -0,0 +1,173 @@
|
||||
# Development Roadmap
|
||||
|
||||
> **Legend:** ✅ complete · 🔲 planned
|
||||
>
|
||||
> **Porting / research boundary:**
|
||||
> Phases 1–7 are direct ports of the Java original and its dissertation.
|
||||
> From Phase 8 onwards the work goes beyond the scope of the Java library.
|
||||
> Phase 8 (CGAL package) is infrastructure. Phase 9 is porting of remaining Java features.
|
||||
> Phase 10+ is independent research with no direct Java reference implementation.
|
||||
|
||||
---
|
||||
|
||||
## ◼ Porting complete — Phases 1–7
|
||||
|
||||
```
|
||||
Phase 1 Clausen / Lobachevsky / ImLi₂ special functions ✅
|
||||
Phase 2 Hyper-ideal geometry (ζ, lᵢⱼ, αᵢⱼ, σᵢ, σᵢⱼ) ✅
|
||||
Phase 3 CGAL Surface_mesh infrastructure + all three functionals
|
||||
(Euclidean, Spherical, HyperIdeal) + analytical Hessians ✅
|
||||
Phase 4 Newton solver (SimplicialLDLT + SparseQR fallback)
|
||||
+ Mesh I/O (OFF/OBJ/PLY) + example programs ✅ 68 tests
|
||||
Phase 5 Priority-BFS layout + CLI app + JSON/XML serialisation ✅ 95 tests
|
||||
Phase 6 Gauss–Bonnet check/enforce, tree-cotree cut graph (2g),
|
||||
exact hyperbolic trilateration, layout normalisation ✅ 121 tests
|
||||
Phase 7 MobiusMap, halfedge_uv, Möbius holonomy (SU(1,1)),
|
||||
period matrix τ∈ℍ + SL(2,ℤ) reduction,
|
||||
fundamental domain parallelogram + tiling ✅ 158 tests
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ◼ Infrastructure — Phase 8: CGAL Package
|
||||
|
||||
Goal: conformallab++ as a standalone CGAL package, submission-ready, fulfilling all
|
||||
CGAL package conventions with a traits-class design compatible with any CGAL-conforming
|
||||
mesh type.
|
||||
|
||||
```
|
||||
8a Traits class & concepts
|
||||
→ include/CGAL/Conformal_map_traits.h
|
||||
Separates MeshType, KernelType, ScalarType from the algorithm.
|
||||
Enables use with any CGAL-compatible mesh, not just Surface_mesh.
|
||||
→ Concept checks via static_assert / CGAL_concept_check
|
||||
|
||||
8b Public CGAL header hierarchy
|
||||
→ include/CGAL/Discrete_conformal_map.h (user-facing entry header)
|
||||
→ include/CGAL/Conformal_newton_solver.h
|
||||
→ include/CGAL/Conformal_layout.h
|
||||
→ include/CGAL/Conformal_cut_graph.h
|
||||
All existing include/conformallab/*.hpp remain as implementation details.
|
||||
|
||||
8c CGAL-style documentation
|
||||
→ doc/Conformal_map/PackageDescription.txt
|
||||
→ doc/Conformal_map/fig/ (pipeline diagrams)
|
||||
→ Doxygen comments on all public concepts and functions
|
||||
→ User_manual.md + Reference_manual.md
|
||||
|
||||
8d CGAL test format
|
||||
→ test/Conformal_map/CMakeLists.txt (CGAL-style CMake)
|
||||
Existing GTest tests remain; CGAL-format tests are added alongside.
|
||||
|
||||
8e Declarative YAML pipeline
|
||||
→ Lightweight YAML format for reproducible experiments
|
||||
(specification in doc/api/cgal-package.md)
|
||||
→ Validator: checks require/provide tokens before execution
|
||||
→ CLI integration: conformallab_core --pipeline experiment.yml
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ◼ Remaining porting — Phase 9
|
||||
|
||||
Java features from `de.varylab.discreteconformal` not yet in C++:
|
||||
|
||||
```
|
||||
9a Inversive-distance functional (Luo 2004 / Bowers–Stephenson)
|
||||
→ inversive_distance_functional.hpp
|
||||
Follows the exact same pattern as the three existing functionals.
|
||||
→ newton_inversive_distance()
|
||||
→ New test suite: test_inversive_distance.cpp
|
||||
|
||||
9b Analytic HyperIdeal Hessian
|
||||
→ Replace FD Hessian in hyper_ideal_hessian.hpp
|
||||
Direct differentiation through the chain:
|
||||
(bᵢ, aₑ) → lᵢⱼ → ζ₁₃/ζ₁₄/ζ₁₅ → αᵢⱼ / βᵢ
|
||||
Relevant for meshes > 500 DOFs (current FD Hessian is slow there).
|
||||
|
||||
9c 4g-polygon boundary walk (genus g > 1)
|
||||
→ Extend compute_fundamental_domain() beyond genus 1
|
||||
Algorithm outline already in fundamental_domain.hpp as TODO(Phase 9).
|
||||
Java reference: FundamentalDomainUtility.java
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ◼ Optional / Hypothetical — geometry-central Cross-Comparison
|
||||
|
||||
> **Status: keine geplante Phase — rein explorativ.**
|
||||
> Diese Punkte sind keine Voraussetzung für Phase 8–10. Sie sind
|
||||
> interessant, weil geometry-central (Keenan Crane, CMU) auf denselben
|
||||
> mathematischen Grundlagen wie conformallab++ aufbaut — insbesondere auf
|
||||
> **Springborn 2020** und der direkten Weiterentwicklung durch
|
||||
> **Gillespie, Springborn & Crane (SIGGRAPH 2021)**.
|
||||
> Der entscheidende Unterschied: geometry-central löst dasselbe Problem
|
||||
> (diskrete konforme Äquivalenz) mit **intrinsischen Triangulierungen +
|
||||
> Ptolemäischen Flips**, während conformallab++ **Newton auf der
|
||||
> Original-Triangulierung** anwendet.
|
||||
|
||||
```
|
||||
GC-1 [optional, jetzt möglich]
|
||||
Mathematischer Output-Vergleich
|
||||
→ gleiche Testnetze (cathead.obj, brezel.obj, torus_4x4.off) in
|
||||
beide Bibliotheken laden
|
||||
→ UV-Koordinaten, u-Vektor, Residualnorm vergleichen
|
||||
→ Normalisierungskonventionen abgleichen (u-Mittelwert, Skalierung)
|
||||
Ziel: unabhängige Kreuz-Validierung der Konvergenzpunkte.
|
||||
Aufwand: kleines Python/C++ Vergleichsskript, kein Bibliotheks-Umbau.
|
||||
|
||||
GC-2 [optional, sinnvoll nach Phase 8]
|
||||
Intrinsic Delaunay Pre-Conditioning
|
||||
→ Vor dem Newton-Solver: geometry-central SignpostIntrinsicTriangulation
|
||||
auf die Eingabe anwenden
|
||||
→ Ptolemäische Flips konditionieren die Hessian-Matrix vor
|
||||
→ Hypothese: weniger Newton-Iterationen auf nicht-Delaunay-Eingaben
|
||||
→ Implementierbar als optionaler cmake-Flag: -DWITH_GC_PRECOND=ON
|
||||
Abhängigkeit: geometry-central als optionale externe Abhängigkeit
|
||||
(header-only Teile genügen für den Flip-Algorithmus).
|
||||
|
||||
GC-3 [hypothetisch, Phase 10+ Forschung]
|
||||
Ptolemäische Flip-basierter Solver als alternativer Backend
|
||||
→ Statt Newton: Ptolemäische Flips + penultimate-step Normalisierung
|
||||
(Gillespie–Springborn–Crane 2021 Algorithmus)
|
||||
→ Vergleich: Konvergenzradius, Robustheit auf pathologischen Netzen,
|
||||
numerische Stabilität auf hohen Genus-Flächen
|
||||
→ Für conformallab++ interessant, weil der Newton-Ansatz auf
|
||||
stark nicht-Delaunay Netzen (z.B. nach Remeshing) instabil
|
||||
werden kann.
|
||||
Keine Implementierung geplant — Konzeptnotiz für Phase 10-Forschung.
|
||||
```
|
||||
|
||||
**Verbindung zur Literatur:**
|
||||
Das Springborn 2020-Papier ("Ideal Hyperbolic Polyhedra and Discrete
|
||||
Uniformization") ist in conformallab++ als HyperIdeal-Geometriemodus
|
||||
bereits implementiert (Phase 2/3). Die Gillespie–Springborn–Crane
|
||||
2021-Erweiterung — die geometry-central implementiert — ergänzt dies um
|
||||
intrinsische Triangulierungen und macht den Algorithmus robust gegen
|
||||
schlechte Eingangs-Triangulierungen. Beide teilen denselben
|
||||
mathematischen Kern (diskrete konforme Äquivalenz, Gauss–Bonnet,
|
||||
Variationsprinzip von Bobenko–Springborn 2004).
|
||||
|
||||
---
|
||||
|
||||
## ◼ New research — Phase 10+
|
||||
|
||||
No direct Java reference implementation exists for these items.
|
||||
|
||||
```
|
||||
Phase 10 Global uniformization for genus g ≥ 2
|
||||
|
||||
10a Discrete holomorphic differentials
|
||||
Integrate basis 1-forms ωᵢ along b-cycles of the cut graph.
|
||||
Mathematical basis: Bobenko–Springborn (2004), §6.
|
||||
Java partial reference: DiscreteHolomorphicFormUtility.java
|
||||
|
||||
10b Siegel period matrix Ω ∈ H_g (g×g complex symmetric, Im(Ω) > 0)
|
||||
Ωᵢⱼ = ∫_{bⱼ} ωᵢ
|
||||
Reduction to Siegel fundamental domain via Sp(2g,ℤ).
|
||||
Requires: 10a
|
||||
|
||||
10c Full uniformization for genus g ≥ 2
|
||||
Embedding as H²/Γ with Γ ⊂ PSL(2,ℝ) a Fuchsian group.
|
||||
Requires: 10a + 10b + stable cut graph for g ≥ 2 (Phase 9c)
|
||||
```
|
||||
202
doc/tutorials/add-inversive-distance.md
Normal file
202
doc/tutorials/add-inversive-distance.md
Normal file
@@ -0,0 +1,202 @@
|
||||
# Tutorial: Porting the Inversive-Distance Functional (Phase 9a)
|
||||
|
||||
This is a complete, step-by-step example of how to add a new functional
|
||||
to conformallab++. It ports `InversiveDistanceFunctional.java` from the
|
||||
Java reference implementation (Luo 2004).
|
||||
|
||||
**Prerequisite:** Read [doc/api/extending.md](../api/extending.md) first
|
||||
for the general pattern. This tutorial fills in every detail for one
|
||||
specific case.
|
||||
|
||||
---
|
||||
|
||||
## Mathematical background
|
||||
|
||||
Inversive distance (Luo 2004) uses a different edge-length update formula:
|
||||
|
||||
```
|
||||
Given inversive distances I_{ij} ∈ ℝ for each edge {i,j}:
|
||||
|
||||
cosh(l̃_{ij}) = I_{ij} · cosh((u_i + u_j) / 2)
|
||||
+ (cosh²((u_i - u_j) / 2) - 1) · ...
|
||||
```
|
||||
|
||||
For the Euclidean version the angle formula is the same law of cosines,
|
||||
but the log-lengths Λ̃ are computed differently from the u-vector.
|
||||
|
||||
**Reference:** Luo, F. (2004). *Combinatorial Yamabe Flow on Surfaces*.
|
||||
Communications in Contemporary Mathematics, 6(5), 765–780.
|
||||
|
||||
**Java source:** `de.varylab.discreteconformal.functional.InversiveDistanceFunctional`
|
||||
|
||||
---
|
||||
|
||||
## Step 1 — Create the header
|
||||
|
||||
```bash
|
||||
cp code/include/euclidean_functional.hpp \
|
||||
code/include/inversive_distance_functional.hpp
|
||||
```
|
||||
|
||||
Edit the new file:
|
||||
|
||||
```cpp
|
||||
#pragma once
|
||||
// inversive_distance_functional.hpp
|
||||
//
|
||||
// Phase 9a — Inversive-distance discrete conformal functional (Luo 2004).
|
||||
// Ported from de.varylab.discreteconformal.functional.InversiveDistanceFunctional.
|
||||
//
|
||||
// Usage: identical to euclidean_functional.hpp — replace lambda0 with
|
||||
// inversive_distance0 (the initial inversive distances per edge).
|
||||
|
||||
#include "conformal_mesh.hpp"
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
|
||||
namespace conformallab {
|
||||
|
||||
struct InversiveDistanceMaps {
|
||||
CGAL::Surface_mesh<CGAL::Simple_cartesian<double>::Point_3>
|
||||
::Property_map<Edge_index, double> inv_dist0; ///< I_{ij} per edge
|
||||
CGAL::Surface_mesh<CGAL::Simple_cartesian<double>::Point_3>
|
||||
::Property_map<Vertex_index, double> theta_v; ///< target corner angles Θ_v
|
||||
CGAL::Surface_mesh<CGAL::Simple_cartesian<double>::Point_3>
|
||||
::Property_map<Vertex_index, int> v_idx; ///< DOF index (-1 = pinned)
|
||||
};
|
||||
|
||||
// ... (follow euclidean_functional.hpp exactly, replacing lambda0 with inv_dist0
|
||||
// and the angle formula with the Luo (2004) version)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 2 — Implement the energy, gradient, and angle formula
|
||||
|
||||
The Euclidean angle formula uses the law of cosines on edge lengths
|
||||
derived from log-lengths. For inversive distance, the edge lengths
|
||||
are derived from inversive distances I_{ij} and the u-vector:
|
||||
|
||||
```cpp
|
||||
// Euclidean (reference):
|
||||
// lambda_ij = lambda0_ij + u_i + u_j
|
||||
// l_ij = exp(lambda_ij / 2)
|
||||
|
||||
// Inversive distance (Luo 2004):
|
||||
// cosh(l̃_ij) = I_ij * cosh((u_i + u_j) / 2) [simplified form]
|
||||
// l̃_ij = acosh(I_ij * cosh((u_i + u_j) / 2))
|
||||
```
|
||||
|
||||
Then the **corner angle** at vertex k opposite edge {i,j} in triangle {i,j,k}
|
||||
is computed by the standard law of cosines from l̃_{ij}, l̃_{jk}, l̃_{ki}.
|
||||
|
||||
The **gradient** is the same as Euclidean:
|
||||
|
||||
```cpp
|
||||
G_v = Σ_{faces containing v} alpha_v(face, u) − Theta_v
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 3 — Write the gradient-check test
|
||||
|
||||
Create `code/tests/cgal/test_inversive_distance.cpp`:
|
||||
|
||||
```cpp
|
||||
#include "inversive_distance_functional.hpp"
|
||||
#include "mesh_factory.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
// Finite-difference gradient check: copy the pattern from
|
||||
// test_euclidean_functional.cpp :: EuclideanFunctional.GradientCheck_Triangle
|
||||
TEST(InversiveDistance, GradientCheck_Triangle)
|
||||
{
|
||||
// Build a single equilateral triangle (open mesh, no boundary issues).
|
||||
ConformalMesh mesh = MeshFactory::make_open_mesh(MeshFactory::Kind::triangle);
|
||||
InversiveDistanceMaps maps = setup_inversive_distance_maps(mesh);
|
||||
compute_inversive_distance0_from_mesh(mesh, maps); // I_ij from 3D edge lengths
|
||||
|
||||
const int n = /* count free DOFs */;
|
||||
std::vector<double> x0(n, 0.0);
|
||||
constexpr double eps = 1e-5;
|
||||
|
||||
auto G = inversive_distance_gradient(mesh, x0, maps);
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
auto xp = x0; xp[i] += eps;
|
||||
auto xm = x0; xm[i] -= eps;
|
||||
double Ep = inversive_distance_energy(mesh, xp, maps);
|
||||
double Em = inversive_distance_energy(mesh, xm, maps);
|
||||
double fd = (Ep - Em) / (2.0 * eps);
|
||||
EXPECT_NEAR(G[i], fd, 1e-6) << "gradient mismatch at DOF " << i;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Run with:
|
||||
|
||||
```bash
|
||||
./build/conformallab_cgal_tests --gtest_filter="InversiveDistance.*"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 4 — Register in CMakeLists
|
||||
|
||||
Add to `code/tests/cgal/CMakeLists.txt` inside the `add_executable` block:
|
||||
|
||||
```cmake
|
||||
# ── Phase 9a: Inversive-distance functional ────────────────────────────────
|
||||
test_inversive_distance.cpp
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 5 — Add a Newton wrapper
|
||||
|
||||
In `code/include/newton_solver.hpp` add:
|
||||
|
||||
```cpp
|
||||
/// Solve the inversive-distance conformal problem.
|
||||
/// \see newton_euclidean() — identical structure.
|
||||
inline NewtonResult newton_inversive_distance(
|
||||
ConformalMesh& mesh,
|
||||
std::vector<double> x0,
|
||||
const InversiveDistanceMaps& m,
|
||||
double tol = 1e-8,
|
||||
int max_iter = 200)
|
||||
{
|
||||
// Copy newton_euclidean() exactly, replacing euclidean_* with
|
||||
// inversive_distance_*. The Hessian is PSD (Luo 2004, Thm. 1),
|
||||
// so SimplicialLDLT with SparseQR fallback applies directly.
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 6 — Verify against Java output
|
||||
|
||||
The Java library outputs text results for a triangulated torus. To compare:
|
||||
|
||||
1. Run Java ConformalLab on the same OFF mesh with Inversive-Distance mode.
|
||||
2. Record the final gradient norm and angle sums.
|
||||
3. Run the C++ equivalent and check:
|
||||
|
||||
```cpp
|
||||
EXPECT_LT(res.grad_inf_norm, 1e-8);
|
||||
EXPECT_LT(res.iterations, 50); // inversive-distance converges faster than hyper-ideal
|
||||
```
|
||||
|
||||
**Java reference:** `InversiveDistanceFunctionalTest.java` in `de.varylab.discreteconformal.test`
|
||||
|
||||
---
|
||||
|
||||
## Checklist
|
||||
|
||||
- [ ] `code/include/inversive_distance_functional.hpp` compiles
|
||||
- [ ] `GradientCheck_Triangle` passes
|
||||
- [ ] `GradientCheck_OpenMesh` passes (copy from Euclidean tests)
|
||||
- [ ] Newton converges on `torus_4x4.off`
|
||||
- [ ] Result matches Java output (gradient norm < 1e-8 on same mesh)
|
||||
- [ ] Registered in `CMakeLists.txt`
|
||||
- [ ] `doc/roadmap/java-parity.md` updated: Phase 9a → ✅
|
||||
76
scripts/try_it.sh
Executable file
76
scripts/try_it.sh
Executable file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
# try_it.sh — conformallab++ quick start
|
||||
#
|
||||
# Clone, build (CGAL tests mode), run the full test suite, and run the
|
||||
# euclidean example on a bundled mesh. No system dependencies beyond a
|
||||
# C++17 compiler, CMake ≥ 3.20, and Boost headers.
|
||||
#
|
||||
# Usage:
|
||||
# cd ConformalLabpp
|
||||
# bash scripts/try_it.sh
|
||||
#
|
||||
# Expected output (last lines):
|
||||
# [PASS] 173 CGAL tests pass, 1 skipped
|
||||
# [PASS] 36 non-CGAL tests pass
|
||||
# [EXAMPLE] Converged in N iterations. ||G||_inf < 1e-9
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
BUILD_DIR="${REPO_ROOT}/build-try"
|
||||
NPROC=$(nproc 2>/dev/null || sysctl -n hw.logicalcpu 2>/dev/null || echo 4)
|
||||
|
||||
echo "========================================"
|
||||
echo " conformallab++ — quick start"
|
||||
echo " repo: ${REPO_ROOT}"
|
||||
echo " build: ${BUILD_DIR}"
|
||||
echo "========================================"
|
||||
echo ""
|
||||
|
||||
# ── 1. Configure ──────────────────────────────────────────────────────────────
|
||||
echo ">>> [1/4] CMake configure (CGAL tests, headless) …"
|
||||
cmake -S "${REPO_ROOT}/code" -B "${BUILD_DIR}" \
|
||||
-DWITH_CGAL_TESTS=ON \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-Wno-dev \
|
||||
2>&1 | tail -5
|
||||
echo ""
|
||||
|
||||
# ── 2. Build ──────────────────────────────────────────────────────────────────
|
||||
echo ">>> [2/4] Build (${NPROC} jobs) …"
|
||||
cmake --build "${BUILD_DIR}" \
|
||||
--target conformallab_cgal_tests conformallab_tests \
|
||||
-j"${NPROC}" \
|
||||
2>&1 | tail -3
|
||||
echo ""
|
||||
|
||||
# ── 3. Run tests ──────────────────────────────────────────────────────────────
|
||||
echo ">>> [3/4] Run test suites …"
|
||||
echo ""
|
||||
echo "--- non-CGAL tests (Clausen, HyperIdeal geometry, matrix utils) ---"
|
||||
ctest --test-dir "${BUILD_DIR}" --output-on-failure \
|
||||
--exclude-regex "^cgal\." 2>&1 | grep -E "Passed|Failed|tests passed"
|
||||
|
||||
echo ""
|
||||
echo "--- CGAL tests (full pipeline: Newton, layout, holonomy, period matrix) ---"
|
||||
ctest --test-dir "${BUILD_DIR}" --output-on-failure \
|
||||
-R "^cgal\." 2>&1 | grep -E "Passed|Failed|tests passed|Skipped"
|
||||
echo ""
|
||||
|
||||
# ── 4. Run example on a bundled mesh ──────────────────────────────────────────
|
||||
echo ">>> [4/4] Euclidean example on bundled torus mesh …"
|
||||
echo ""
|
||||
|
||||
# Build the example binary (requires WITH_CGAL_TESTS; example_euclidean
|
||||
# is part of the examples target only under WITH_CGAL=ON, so we run the
|
||||
# CGAL test binary with a filter instead for a headless demo).
|
||||
"${BUILD_DIR}/conformallab_cgal_tests" \
|
||||
--gtest_filter="EuclideanPipeline*:NewtonSolver*" \
|
||||
--gtest_color=no 2>&1 | grep -E "OK|FAILED|RUN|iterations" | head -20
|
||||
|
||||
echo ""
|
||||
echo "========================================"
|
||||
echo " Done. See doc/getting-started.md for next steps."
|
||||
echo " Extend: doc/tutorials/add-inversive-distance.md"
|
||||
echo " Validate: doc/math/validation-protocol.md"
|
||||
echo "========================================"
|
||||
Reference in New Issue
Block a user