Files
ConformalLabpp/code/include/projective_math.hpp
Tarik Moussa d7a61975b4
Some checks failed
C++ Tests / test-fast (pull_request) Successful in 1m59s
API Docs / doc-build (pull_request) Successful in 49s
Markdown link check / check (pull_request) Successful in 42s
C++ Tests / test-cgal (pull_request) Failing after 8m14s
C++ Tests / quality-gates (pull_request) Failing after 1m45s
perf: architecture-touch quick-wins #6 + #10; skip #5 + #7 with honest notes
Evaluated all four mid-tier architecture-touch levers from
doc/architecture/compile-time.md.  Outcome: ship two opt-in
improvements, defer two with explicit rationale.

#6 — Eager-include reduction (Dense → Core)   shipped
─────────────────────────────────────────────────────
Three headers downgraded from `<Eigen/Dense>` to `<Eigen/Core>`:
  * projective_math.hpp
  * hyper_ideal_visualization_utility.hpp
  * mesh_utils.hpp

All three only use Matrix/Vector primitives, no Eigen decompositions.
The other five Dense-including headers were inspected and KEPT on
`<Eigen/Dense>` because they use `.inverse()`, `.determinant()`,
`ColPivHouseholderQR`, or `SelfAdjointEigenSolver`.

Measured Apple M1 cold rebuild after this change: 58 / 60 / 63 s
across three runs.  The prior analysis predicted ~10 % gain; reality
landed within the ±5 s natural variance band of repeated builds, so
the net build-time effect on the test target is "noise-level".

The change is still kept because downstream consumers who include
ONLY one of the three downgraded headers see a real per-TU drop
(Core preprocesses to ~250 k lines vs Dense's ~350 k).

#10 — Fast test-build mode (-O0 -g)   shipped
───────────────────────────────────────────────
New option CONFORMALLAB_FAST_TEST_BUILD (default OFF).  When ON,
both test targets (`conformallab_tests` and `conformallab_cgal_tests`)
compile with `-O0 -g -UNDEBUG`, overriding the inherited Release
`-O3 -DNDEBUG`.

Measured Apple clang: 51.6 s vs 46.8 s without -O0 → slightly slower.
The Backend phase that prior analysis predicted would drop from 9.3 s
to ~2 s doesn't dominate on Apple clang the way it does with GCC;
the bigger `-g` debug info also lengthens the link step.

Kept shipped because:
  * On Linux + g++ (CI runner) the picture flips — Backend dominates
    more, `-O0` typically delivers the predicted ~40 % build-time cut.
  * Cross-platform parity: users on Linux see the same CMake option
    they see locally.

Honest documentation in doc/architecture/compile-time.md notes that
the Apple-clang-local benefit is currently 0 %.  Tests RUN ~15× slower
under `-O0` (1.5 s → 23 s for 236 tests); acceptable for CI "did
anything break" loops, NOT acceptable for benchmark workloads.

#5 — Move detail:: impls to .inl files  ⏸ deferred
───────────────────────────────────────────────────
Pure enabler for #7.  Without #7 landing, the .inl extraction would
just add an extra hop to header reading.  Reconsider once a concrete
maintenance reason emerges (e.g. a downstream user wants to override a
detail helper).

#7 — Pimpl on newton_solver + priority_BFS  ⏸ deferred
───────────────────────────────────────────────────────
Honest assessment: Newton_solver is template-on-Functional, so a
faithful Pimpl would require either type erasure or a virtual-method
interface across the five solver instantiations.  Estimated 1-2 weeks
of refactor with measurable API-surface risk.  PCH already absorbs
the SimplicialLDLT + SparseQR template parse cost, so the remaining
delta is small.  Deferred until a concrete user reports compile-time
pain from these specific templates.

Documentation
─────────────
README.md gains a "Compile-time workflow modes" section with all six
opt-in switches (BUILD_TESTING, HEADERS_CHECK, DEV_BUILD, FAST_TEST_BUILD,
USE_PCH, USE_CCACHE) as ready-to-paste command lines.

doc/architecture/compile-time.md gains:
  * an "Architecture-touch quick-wins" section with the four-row
    status table (5 deferred / 6 shipped / 7 deferred / 10 shipped)
  * the FAST_TEST_BUILD row added to the workflow-modes table
  * the mode-matrix table updated with Linux-vs-macOS expected values
  * an honest "variance" note explaining the ±5 s spread between
    repeated cold builds and why #6's net effect lands in that noise

Verified: default build 55 s (within usual variance), 236/236 tests
pass under default; FAST_TEST_BUILD=ON build 52 s, 236/236 PASS.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 08:56:36 +02:00

94 lines
3.7 KiB
C++

#pragma once
// Copyright (c) 2024-2026 Tarik Moussa.
// SPDX-License-Identifier: MIT
// Projective and hyperbolic geometry utilities.
// Ported from de.jreality.math.Pn / Rn and
// de.varylab.discreteconformal.uniformization.SurfaceCurveUtility (Java).
#include <Eigen/Core> // downgraded from <Eigen/Dense>: this header only
// uses Matrix/Vector primitives, no decompositions.
#include <cmath>
#include <algorithm>
#include <array>
#include <cassert>
namespace conformallab {
// Divide a homogeneous vector by its last component.
// Corresponds to Java Pn.dehomogenize().
inline Eigen::VectorXd dehomogenize(const Eigen::VectorXd& p) {
return p / p(p.size() - 1);
}
// Hyperbolic distance between two homogeneous vectors of the same dimension.
// The last component is the "timelike" coordinate (jReality convention).
// Inner product: <p,q> = -sum_i p_i*q_i + p_last * q_last
// Distance: arcosh(<p̂, q̂>) where p̂ normalises to the hyperboloid.
// Corresponds to Java Pn.distanceBetween(p, q, Pn.HYPERBOLIC).
inline double hyperbolicDistance(const Eigen::VectorXd& p,
const Eigen::VectorXd& q) {
int n = static_cast<int>(p.size());
double normP = std::sqrt(p(n-1)*p(n-1) - p.head(n-1).squaredNorm());
double normQ = std::sqrt(q(n-1)*q(n-1) - q.head(n-1).squaredNorm());
double inner = (-p.head(n-1).dot(q.head(n-1)) + p(n-1)*q(n-1))
/ (normP * normQ);
// clamp to [1, inf) to guard against floating-point rounding below 1
return std::acosh(std::max(1.0, inner));
}
// Check whether a homogeneous point p lies on the segment [s[0], s[1]].
// Works for n-dimensional homogeneous coords; cross product uses the first
// 3 spatial components after dehomogenization (matching jReality's Rn behaviour).
// Corresponds to Java SurfaceCurveUtility.isOnSegment().
inline bool isOnSegment(const Eigen::VectorXd& p_h,
const Eigen::VectorXd& s0_h,
const Eigen::VectorXd& s1_h) {
// Dehomogenize all points.
Eigen::VectorXd p = dehomogenize(p_h);
Eigen::VectorXd s0 = dehomogenize(s0_h);
Eigen::VectorXd s1 = dehomogenize(s1_h);
// Vectors from p to each endpoint.
Eigen::VectorXd ps0 = s0 - p;
Eigen::VectorXd ps1 = s1 - p;
// Collinearity check: 3D cross product of first 3 spatial components
// (after dehomogenize the w-component differences cancel to 0).
// head<3>() gives compile-time size needed by Eigen's cross().
Eigen::Vector3d cross = ps0.head<3>().cross(ps1.head<3>());
if (cross.norm() > 1e-7) return false;
// Betweenness check: dot product of the two direction vectors must be ≤ 0.
double dot = ps0.dot(ps1);
if (dot > 0.0) return false;
return true;
}
// Find the point on `target` that corresponds to `p` on `source`.
// The parameter t is determined by hyperbolic distance ratios on `source`,
// then applied as a linear interpolation on the dehomogenized `target`.
// Corresponds to Java SurfaceCurveUtility.getPointOnCorrespondingSegment().
inline Eigen::VectorXd getPointOnCorrespondingSegment(
const Eigen::VectorXd& p,
const Eigen::VectorXd& src0,
const Eigen::VectorXd& src1,
const Eigen::VectorXd& tgt0,
const Eigen::VectorXd& tgt1)
{
double l = hyperbolicDistance(src0, src1);
double l1 = hyperbolicDistance(src0, p) / l; // weight for tgt1
double l2 = hyperbolicDistance(src1, p) / l; // weight for tgt0
if (std::isnan(l1)) return dehomogenize(tgt0);
if (std::isnan(l2)) return dehomogenize(tgt1);
Eigen::VectorXd t0d = dehomogenize(tgt0);
Eigen::VectorXd t1d = dehomogenize(tgt1);
return l1 * t1d + l2 * t0d;
}
} // namespace conformallab