Files
ConformalLabpp/code/tests/test_p2_utility.cpp
Tarik Moussa c5a86cb30a
All checks were successful
C++ Tests / test (push) Successful in 2m39s
Mirror to Codeberg / mirror (push) Successful in 24s
tests: port DiscreteEllipticUtility + P2 tests; stub HDS-blocked tests
Fully ported (pure math, no HDS required):
  test_discrete_elliptic_utility.cpp  – 2 tests
    normalizeModulus: move tau into SL(2,Z) fundamental domain
  test_p2_utility.cpp                 – 3 tests
    P2 projective geometry (perpendicularBisector, pointFromLines,
    makeDirectIsometryFromFrames double vs long double precision)

New headers:
  include/discrete_elliptic_utility.hpp  – normalizeModulus
  include/p2_utility.hpp                 – P2 Euclidean geometry (templated
    on scalar type so double and long double share one implementation)

Stubs (GTEST_SKIP, blocked until HDS port – Phase 4):
  test_hyper_ideal_functional.cpp          – 5 tests (1 @Ignore in Java)
  test_hyper_ideal_hyperelliptic_utility.cpp – 3 tests
  test_spherical_functional.cpp            – 5 tests
  All use CoHDS + HalfEdgeUtils which are not yet ported to C++.

Result: 34 tests total | 21 passed | 13 skipped | 0 failed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-11 17:15:18 +02:00

88 lines
3.3 KiB
C++

// 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);
}