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