Files
ConformalLabpp/code/include/projective_math.hpp
Tarik Moussa 62b02f88b9
Some checks failed
C++ Tests / test-fast (pull_request) Successful in 2m40s
API Docs / doc-build (pull_request) Successful in 1m2s
C++ Tests / test-cgal (pull_request) Failing after 11m52s
docs(doxygen): 100% public-API coverage (228 → 0 undocumented)
Completes the work begun in the previous commit on this branch.  Every
public symbol under code/include/ now carries a brief Doxygen comment
(0 undocumented per scripts/doxygen-coverage.sh, with the `detail::`
implementation namespaces excluded as before).

Trajectory on this branch:
  start (after Doxyfile fix):  24.0 %  (165 / 437 in the no-detail set
                                       was 105 / 437 when detail counted)
  after PR #17 base commit  :  42.4 %  (165 / 396)
  this commit               : 100.0 %  (396 / 396)

Files touched (all .hpp / .h headers under code/include/):
  * cgal/Conformal_map_traits.h
  * clausen.hpp, conformal_mesh.hpp, constants.hpp (already docd)
  * cp_euclidean_functional.hpp, cut_graph.hpp, discrete_elliptic_utility.hpp
  * euclidean_functional.hpp, euclidean_geometry.hpp, euclidean_hessian.hpp
  * fundamental_domain.hpp, gauss_bonnet.hpp
  * hyper_ideal_{functional,geometry,hessian,utility,visualization_utility}.hpp
  * inversive_distance_functional.hpp, layout.hpp
  * matrix_utility.hpp, mesh_builder.hpp, mesh_io.hpp
  * newton_solver.hpp, p2_utility.hpp, period_matrix.hpp, projective_math.hpp
  * serialization.hpp, spherical_functional.hpp, spherical_geometry.hpp
  * spherical_hessian.hpp, viewer_utils.h

CI:
.gitea/workflows/doxygen-pages.yml now enforces
`scripts/doxygen-coverage.sh --threshold 100`, so any future regression
(a new public function landed without a `///` brief) fails the build
before the Doxygen HTML is published to Codeberg Pages.

Doxygen warnings remain at 0.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 04:22:49 +02:00

86 lines
3.2 KiB
C++

#pragma once
// Projective and hyperbolic geometry utilities.
// Ported from de.jreality.math.Pn / Rn and
// de.varylab.discreteconformal.uniformization.SurfaceCurveUtility (Java).
#include <Eigen/Dense>
#include <cmath>
#include <algorithm>
#include <array>
#include <cassert>
namespace conformallab {
/// Dehomogenise: divide a homogeneous vector by its last component.
/// Same as Java `Pn.dehomogenize()`.
inline Eigen::VectorXd dehomogenize(const Eigen::VectorXd& p) {
return p / p(p.size() - 1);
}
/// Hyperbolic distance `arcosh(⟨p̂, q̂⟩)` between two homogeneous
/// vectors (last component = timelike coordinate; jReality convention).
/// Same as 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));
}
/// `true` iff the homogeneous point `p_h` lies on the segment
/// `[s0_h, s1_h]`. Same as 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 the target segment `(tgt0, tgt1)` corresponding
/// to `p` on the source segment `(src0, src1)`, parametrised by
/// hyperbolic distance ratios on the source. Same as 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