Files
ViewDesignEngine/tests/core/test_exact_predicates.cpp
T

458 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/core/exact_predicates.h"
#include <vector>
#include <cmath>
using namespace vde::core;
TEST(ExactPredicatesTest, Orient2D_CounterClockwise) {
double r = exact_orient2d(0, 0, 1, 0, 0, 1);
EXPECT_GT(r, 0.0); // CCW
}
TEST(ExactPredicatesTest, Orient2D_Clockwise) {
double r = exact_orient2d(0, 0, 0, 1, 1, 0);
EXPECT_LT(r, 0.0);
}
TEST(ExactPredicatesTest, Orient2D_Collinear) {
double r = exact_orient2d(0, 0, 1, 1, 2, 2);
EXPECT_NEAR(r, 0.0, 1e-9);
}
TEST(ExactPredicatesTest, Orient3D_Above) {
// Tetrahedron: base on z=0, apex at z=1 → positive volume
double r = exact_orient3d(0,0,0, 1,0,0, 0,1,0, 0,0,1);
EXPECT_GT(r, 0.0);
}
TEST(ExactPredicatesTest, Orient3D_Coplanar) {
double r = exact_orient3d(0,0,0, 1,0,0, 0,1,0, 0.5,0.5,0);
EXPECT_NEAR(r, 0.0, 1e-9);
}
TEST(ExactPredicatesTest, InSphere_Inside) {
// Regular tetrahedron, point at centroid → inside circumsphere
double r = exact_in_sphere(0,0,0, 2,0,0, 0,2,0, 0,0,2, 0.2,0.2,0.2);
EXPECT_GT(r, 0.0);
}
TEST(ExactPredicatesTest, InSphere_OnSurface) {
double r = exact_in_sphere(0,0,0, 3,0,0, 0,3,0, 0,0,3, 0,0,3);
EXPECT_NEAR(r, 0.0, 1e-8);
}
TEST(ExactPredicatesTest, PointInPolygon_Inside) {
std::vector<Point3D> poly = {{0,0,0},{2,0,0},{2,2,0},{0,2,0}};
EXPECT_TRUE(exact_point_in_polygon({1,1,0}, poly));
}
TEST(ExactPredicatesTest, PointInPolygon_Outside) {
std::vector<Point3D> poly = {{0,0,0},{2,0,0},{2,2,0},{0,2,0}};
EXPECT_FALSE(exact_point_in_polygon({3,3,0}, poly));
}
TEST(ExactPredicatesTest, PointInPolygon_OnEdge) {
std::vector<Point3D> poly = {{0,0,0},{2,0,0},{2,2,0},{0,2,0}};
EXPECT_TRUE(exact_point_in_polygon({1,0,0}, poly));
}
TEST(ExactPredicatesTest, PrecisionMode) {
set_precision_mode(PrecisionMode::Fast);
EXPECT_EQ(precision_mode(), PrecisionMode::Fast);
set_precision_mode(PrecisionMode::Adaptive);
EXPECT_EQ(precision_mode(), PrecisionMode::Adaptive);
set_precision_mode(PrecisionMode::Adaptive); // restore default
}
TEST(ExactPredicatesTest, IsUncertain) {
EXPECT_TRUE(is_uncertain(1e-12));
EXPECT_FALSE(is_uncertain(1.0));
}
// ═══════════════════════════════════════════════════════════
// VDE-893: exact_point_in_polyhedron 未测试公开函数补测
// ═══════════════════════════════════════════════════════════
namespace {
// 单位立方体 [0,1]^3
std::vector<Point3D> unit_cube_vertices() {
return {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}};
}
std::vector<std::vector<int>> unit_cube_faces() {
return {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 1, 5, 4},
{3, 2, 6, 7}, {1, 2, 6, 5}, {0, 3, 7, 4}};
}
} // namespace
TEST(ExactPredicatesTest, PointInPolyhedron_InsideCube) {
// 射线沿 +X;测试点取 y≠z 以避开 x 面扇形对角线(y=z)
auto v = unit_cube_vertices();
auto f = unit_cube_faces();
EXPECT_TRUE(exact_point_in_polyhedron({0.5, 0.3, 0.7}, v, f)); // 仅穿出 x=1 一次
EXPECT_TRUE(exact_point_in_polyhedron({0.2, 0.8, 0.4}, v, f));
}
TEST(ExactPredicatesTest, PointInPolyhedron_OutsideCube) {
auto v = unit_cube_vertices();
auto f = unit_cube_faces();
// −X 外侧:射线穿入 x=0、穿出 x=1 → 2 次 → 偶 → 外
EXPECT_FALSE(exact_point_in_polyhedron({-0.5, 0.3, 0.7}, v, f));
// +X 外侧:射线背离立方体 → 0 次
EXPECT_FALSE(exact_point_in_polyhedron({2.0, 0.3, 0.7}, v, f));
// +Y / +Z 外侧:射线整体在体外 → 0 次
EXPECT_FALSE(exact_point_in_polyhedron({0.5, 1.5, 0.3}, v, f));
EXPECT_FALSE(exact_point_in_polyhedron({0.5, 0.3, 1.5}, v, f));
}
TEST(ExactPredicatesTest, PointInPolyhedron_EmptyInputs) {
std::vector<Point3D> no_v;
std::vector<std::vector<int>> no_f;
EXPECT_FALSE(exact_point_in_polyhedron({0, 0, 0}, no_v, no_f));
EXPECT_FALSE(exact_point_in_polyhedron({0, 0, 0}, unit_cube_vertices(), no_f));
EXPECT_FALSE(exact_point_in_polyhedron({0, 0, 0}, no_v, unit_cube_faces()));
}
// ═══════════════════════════════════════════════════════════
// Phase4-Exact:Shewchuk 自适应谓词 vs 整数精确参考
// 参考实现:把 double 坐标写成 m·2^-k(m 为整数、k≥0),所有谓词为齐次
// 多项式,符号与整数 mantissa 的行列式符号一致;用 __int128 精确计算
// 即得独立高精度参考(不依赖任何浮点/第三方库)。
// ═══════════════════════════════════════════════════════════
#include <string>
#include <random>
#include <cstdint>
#if defined(__SIZEOF_INT128__)
namespace {
using i128 = __int128_t;
inline int s128(i128 v) { return (v > 0) ? 1 : (v < 0 ? -1 : 0); }
i128 det3_i(const i128 m[3][3]) {
return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) +
m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
}
i128 det4_i(const i128 m[4][4]) {
i128 result = 0;
for (int c0 = 0; c0 < 4; ++c0) {
i128 minor[3][3];
for (int r = 1; r < 4; ++r) {
int cc = 0;
for (int c = 0; c < 4; ++c) {
if (c == c0) continue;
minor[r - 1][cc++] = m[r][c];
}
}
const i128 sub = det3_i(minor);
result += (c0 % 2 == 0 ? 1 : -1) * m[0][c0] * sub;
}
return result;
}
int ref_orient2d(int64_t ax, int64_t ay, int64_t bx, int64_t by,
int64_t cx, int64_t cy) {
const i128 det = static_cast<i128>(bx - ax) * (cy - ay) -
static_cast<i128>(by - ay) * (cx - ax);
return s128(det);
}
int ref_orient3d(int64_t ax, int64_t ay, int64_t az, int64_t bx, int64_t by,
int64_t bz, int64_t cx, int64_t cy, int64_t cz, int64_t dx,
int64_t dy, int64_t dz) {
const i128 m[3][3] = {
{static_cast<i128>(ax - dx), static_cast<i128>(ay - dy), static_cast<i128>(az - dz)},
{static_cast<i128>(bx - dx), static_cast<i128>(by - dy), static_cast<i128>(bz - dz)},
{static_cast<i128>(cx - dx), static_cast<i128>(cy - dy), static_cast<i128>(cz - dz)}};
return -s128(det3_i(m)); // 与 API 约定一致:>0 = d 在平面 abc 上方
}
int ref_incircle(int64_t ax, int64_t ay, int64_t bx, int64_t by, int64_t cx,
int64_t cy, int64_t dx, int64_t dy) {
const i128 adx = static_cast<i128>(ax - dx), ady = static_cast<i128>(ay - dy);
const i128 bdx = static_cast<i128>(bx - dx), bdy = static_cast<i128>(by - dy);
const i128 cdx = static_cast<i128>(cx - dx), cdy = static_cast<i128>(cy - dy);
const i128 m[3][3] = {
{adx, ady, adx * adx + ady * ady},
{bdx, bdy, bdx * bdx + bdy * bdy},
{cdx, cdy, cdx * cdx + cdy * cdy}};
return s128(det3_i(m));
}
int ref_insphere(int64_t ax, int64_t ay, int64_t az, int64_t bx, int64_t by,
int64_t bz, int64_t cx, int64_t cy, int64_t cz, int64_t dx,
int64_t dy, int64_t dz, int64_t ex, int64_t ey, int64_t ez) {
const i128 adx = static_cast<i128>(ax - ex), ady = static_cast<i128>(ay - ey),
adz = static_cast<i128>(az - ez);
const i128 bdx = static_cast<i128>(bx - ex), bdy = static_cast<i128>(by - ey),
bdz = static_cast<i128>(bz - ez);
const i128 cdx = static_cast<i128>(cx - ex), cdy = static_cast<i128>(cy - ey),
cdz = static_cast<i128>(cz - ez);
const i128 ddx = static_cast<i128>(dx - ex), ddy = static_cast<i128>(dy - ey),
ddz = static_cast<i128>(dz - ez);
const i128 m[4][4] = {
{adx, ady, adz, adx * adx + ady * ady + adz * adz},
{bdx, bdy, bdz, bdx * bdx + bdy * bdy + bdz * bdz},
{cdx, cdy, cdz, cdx * cdx + cdy * cdy + cdz * cdz},
{ddx, ddy, ddz, ddx * ddx + ddy * ddy + ddz * ddz}};
return -s128(det4_i(m)); // 与 API 约定一致:>0 = e 在球内
}
struct Rng {
std::mt19937 gen{20261026u};
int64_t uniform(int64_t lo, int64_t hi) {
return std::uniform_int_distribution<int64_t>(lo, hi)(gen);
}
};
} // namespace
TEST(ExactPredicatesTest, BackendAndModes) {
EXPECT_TRUE(exact_predicates_available());
const char* backend = exact_predicates_backend();
ASSERT_NE(backend, nullptr);
EXPECT_GT(std::string(backend).size(), 0u);
set_precision_mode(PrecisionMode::Adaptive);
EXPECT_EQ(precision_mode(), PrecisionMode::Adaptive);
set_precision_mode(PrecisionMode::Exact);
EXPECT_EQ(precision_mode(), PrecisionMode::Exact);
set_precision_mode(PrecisionMode::Adaptive);
}
TEST(ExactPredicatesTest, Orient2D_SignVsIntegerReference) {
Rng rng;
int mismatches = 0;
for (int i = 0; i < 400; ++i) {
const int64_t ax = rng.uniform(-1000, 1000), ay = rng.uniform(-1000, 1000);
const int64_t bx = rng.uniform(-1000, 1000), by = rng.uniform(-1000, 1000);
const int64_t cx = rng.uniform(-1000, 1000), cy = rng.uniform(-1000, 1000);
const int k = static_cast<int>(rng.uniform(0, 30));
const double scale = std::ldexp(1.0, -k);
const int got = exact_orient2d_sign(
static_cast<double>(ax) * scale, static_cast<double>(ay) * scale,
static_cast<double>(bx) * scale, static_cast<double>(by) * scale,
static_cast<double>(cx) * scale, static_cast<double>(cy) * scale);
const int ref = ref_orient2d(ax, ay, bx, by, cx, cy);
if (got != ref) ++mismatches;
}
EXPECT_EQ(mismatches, 0);
// 构造共线(含重合点)
for (int i = 0; i < 50; ++i) {
const int64_t ax = rng.uniform(-100, 100), ay = rng.uniform(-100, 100);
const int64_t bx = rng.uniform(-100, 100), by = rng.uniform(-100, 100);
const int64_t t = rng.uniform(-3, 3);
const int64_t cx = ax + t * (bx - ax), cy = ay + t * (by - ay);
const int got = exact_orient2d_sign(ax, ay, bx, by, cx, cy);
EXPECT_EQ(got, ref_orient2d(ax, ay, bx, by, cx, cy));
EXPECT_EQ(got, 0);
}
// 极近共线:大整数构造 det=1(浮点快路径会舍入为 0)
{
const int64_t P = 1000000;
const int got = exact_orient2d_sign(
0, 0, static_cast<double>(P), static_cast<double>(P - 1),
static_cast<double>(P + 1), static_cast<double>(P));
EXPECT_EQ(got, 1); // P*P - (P-1)(P+1) = 1
}
}
TEST(ExactPredicatesTest, Orient3D_SignVsIntegerReference) {
Rng rng;
int mismatches = 0;
for (int i = 0; i < 400; ++i) {
int64_t v[12];
for (int j = 0; j < 12; ++j) v[j] = rng.uniform(-500, 500);
const int k = static_cast<int>(rng.uniform(0, 30));
const double scale = std::ldexp(1.0, -k);
double d[12];
for (int j = 0; j < 12; ++j) d[j] = static_cast<double>(v[j]) * scale;
const int got = exact_orient3d_sign(d[0], d[1], d[2], d[3], d[4], d[5],
d[6], d[7], d[8], d[9], d[10], d[11]);
const int ref = ref_orient3d(v[0], v[1], v[2], v[3], v[4], v[5], v[6],
v[7], v[8], v[9], v[10], v[11]);
if (got != ref) ++mismatches;
}
EXPECT_EQ(mismatches, 0);
// 构造精确共面:d = a + u(b-a) + v(c-a)
for (int i = 0; i < 50; ++i) {
int64_t a[3], b[3], c[3];
for (int j = 0; j < 3; ++j) {
a[j] = rng.uniform(-100, 100);
b[j] = rng.uniform(-100, 100);
c[j] = rng.uniform(-100, 100);
}
const int64_t u = rng.uniform(-2, 2), v = rng.uniform(-2, 2);
int64_t d[3];
for (int j = 0; j < 3; ++j)
d[j] = a[j] + u * (b[j] - a[j]) + v * (c[j] - a[j]);
const int got = exact_orient3d_sign(a[0], a[1], a[2], b[0], b[1], b[2],
c[0], c[1], c[2], d[0], d[1], d[2]);
EXPECT_EQ(got, 0);
EXPECT_EQ(got, ref_orient3d(a[0], a[1], a[2], b[0], b[1], b[2], c[0],
c[1], c[2], d[0], d[1], d[2]));
}
}
TEST(ExactPredicatesTest, InCircle_SignVsIntegerReference) {
Rng rng;
int mismatches = 0;
for (int i = 0; i < 400; ++i) {
int64_t v[8];
for (int j = 0; j < 8; ++j) v[j] = rng.uniform(-500, 500);
const int k = static_cast<int>(rng.uniform(0, 30));
const double scale = std::ldexp(1.0, -k);
double d[8];
for (int j = 0; j < 8; ++j) d[j] = static_cast<double>(v[j]) * scale;
const int got = exact_incircle_sign(d[0], d[1], d[2], d[3], d[4], d[5],
d[6], d[7]);
const int ref = ref_incircle(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
if (got != ref) ++mismatches;
}
EXPECT_EQ(mismatches, 0);
// 共圆:整数点圆 (3,4,5) 上取 4 点 → 精确 0
const int64_t circle[][2] = {{5, 0}, {4, 3}, {3, 4}, {0, 5},
{-3, 4}, {-4, 3}, {-5, 0}, {-4, -3},
{-3, -4}, {0, -5}, {3, -4}, {4, -3}};
int zero_cases = 0;
for (int i = 0; i < 12; ++i)
for (int j = i + 1; j < 12; ++j)
for (int l = j + 1; l < 12; ++l) {
if ((i * 7 + j * 3 + l) % 23 != 0) continue;
const int got = exact_incircle_sign(
circle[i][0], circle[i][1], circle[j][0], circle[j][1],
circle[l][0], circle[l][1], circle[(l + 1) % 12][0],
circle[(l + 1) % 12][1]);
// (i,j,l) 的圆与第 4 点共圆? 所有点同一圆 → 任一第 4 点都共圆
EXPECT_EQ(got, 0);
++zero_cases;
}
EXPECT_GT(zero_cases, 0);
// 极近共圆:圆上点扰动 1(整数)→ 判定非零且与参考一致
int near_cases = 0;
for (int i = 0; i < 40; ++i) {
const int idx = static_cast<int>(rng.uniform(0, 11));
const int64_t px = circle[idx][0] + (i % 2 == 0 ? 1 : -1);
const int64_t py = circle[(idx + 1) % 12][1];
const int got = exact_incircle_sign(
circle[idx][0], circle[idx][1], circle[(idx + 1) % 12][0],
circle[(idx + 1) % 12][1], circle[(idx + 2) % 12][0],
circle[(idx + 2) % 12][1], px, py);
const int ref = ref_incircle(
circle[idx][0], circle[idx][1], circle[(idx + 1) % 12][0],
circle[(idx + 1) % 12][1], circle[(idx + 2) % 12][0],
circle[(idx + 2) % 12][1], px, py);
EXPECT_EQ(got, ref);
if (got != 0) ++near_cases;
}
EXPECT_GT(near_cases, 0);
}
TEST(ExactPredicatesTest, InSphere_SignVsIntegerReference) {
Rng rng;
int mismatches = 0;
for (int i = 0; i < 300; ++i) {
int64_t v[15];
for (int j = 0; j < 15; ++j) v[j] = rng.uniform(-300, 300);
const int k = static_cast<int>(rng.uniform(0, 25));
const double scale = std::ldexp(1.0, -k);
double d[15];
for (int j = 0; j < 15; ++j) d[j] = static_cast<double>(v[j]) * scale;
const int got = exact_insphere_sign(
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10],
d[11], d[12], d[13], d[14]);
const int ref = ref_insphere(v[0], v[1], v[2], v[3], v[4], v[5], v[6],
v[7], v[8], v[9], v[10], v[11], v[12],
v[13], v[14]);
if (got != ref) ++mismatches;
}
EXPECT_EQ(mismatches, 0);
// 共球:半径 5 的整数球面点(±5 轴向 + (±3,±4,0) 等替换)
const int64_t sphere[][3] = {{5, 0, 0}, {-5, 0, 0}, {0, 5, 0}, {0, -5, 0},
{0, 0, 5}, {0, 0, -5}, {3, 4, 0}, {-3, 4, 0},
{3, -4, 0}, {-3, -4, 0}, {4, 3, 0}, {-4, 3, 0}};
int zero_cases = 0;
for (int i = 0; i < 12; ++i)
for (int j = i + 1; j < 12; ++j)
for (int l = j + 1; l < 12; ++l)
for (int m = l + 1; m < 12; ++m) {
if ((i * 5 + j * 3 + l + m) % 37 != 0) continue;
const int got = exact_insphere_sign(
sphere[i][0], sphere[i][1], sphere[i][2], sphere[j][0],
sphere[j][1], sphere[j][2], sphere[l][0], sphere[l][1],
sphere[l][2], sphere[m][0], sphere[m][1], sphere[m][2],
sphere[(m + 1) % 12][0], sphere[(m + 1) % 12][1],
sphere[(m + 1) % 12][2]);
EXPECT_EQ(got, 0);
++zero_cases;
}
EXPECT_GT(zero_cases, 0);
// 极近共球:微扰动点,与整数参考一致
int nonzero = 0;
for (int i = 0; i < 30; ++i) {
const int idx = static_cast<int>(rng.uniform(0, 11));
const int64_t ex = sphere[idx][0] + (i % 2 == 0 ? 1 : -1);
const int64_t ey = sphere[idx][1];
const int64_t ez = sphere[idx][2];
const int got = exact_insphere_sign(
sphere[0][0], sphere[0][1], sphere[0][2], sphere[2][0], sphere[2][1],
sphere[2][2], sphere[3][0], sphere[3][1], sphere[3][2], sphere[4][0],
sphere[4][1], sphere[4][2], ex, ey, ez);
const int ref = ref_insphere(
sphere[0][0], sphere[0][1], sphere[0][2], sphere[2][0], sphere[2][1],
sphere[2][2], sphere[3][0], sphere[3][1], sphere[3][2], sphere[4][0],
sphere[4][1], sphere[4][2], ex, ey, ez);
EXPECT_EQ(got, ref);
if (got != 0) ++nonzero;
}
EXPECT_GT(nonzero, 0);
// 解析:四面体 (0,0,0),(1,0,0),(0,1,0),(0,0,1) 的外接球
EXPECT_EQ(exact_insphere_sign(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0.25, 0.25, 0.25), 1);
EXPECT_EQ(exact_insphere_sign(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2),
-1);
EXPECT_EQ(exact_insphere_sign(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1),
0);
}
TEST(ExactPredicatesTest, InCircleAnalyticCases) {
// abc 逆时针:内部 +1 / 外部 -1 / 圆上 0
EXPECT_EQ(exact_incircle_sign(0, 0, 1, 0, 0, 1, 0.2, 0.2), 1);
EXPECT_EQ(exact_incircle_sign(0, 0, 1, 0, 0, 1, 2, 2), -1);
EXPECT_EQ(exact_incircle_sign(0, 0, 1, 0, 0, 1, 0, 1), 0);
}
TEST(ExactPredicatesTest, AdaptiveMatchesExactMode) {
Rng rng;
set_precision_mode(PrecisionMode::Adaptive);
for (int i = 0; i < 150; ++i) {
const int64_t ax = rng.uniform(-1000, 1000), ay = rng.uniform(-1000, 1000);
const int64_t bx = rng.uniform(-1000, 1000), by = rng.uniform(-1000, 1000);
const int64_t P = 100000 + i;
const int64_t cx = ax + P * (bx - ax), cy = ay + P * (by - ay);
const int adaptive = exact_orient2d_sign(ax, ay, bx, by, cx, cy);
set_precision_mode(PrecisionMode::Exact);
const int exact = exact_orient2d_sign(ax, ay, bx, by, cx, cy);
set_precision_mode(PrecisionMode::Adaptive);
EXPECT_EQ(adaptive, exact);
EXPECT_EQ(adaptive, 0);
}
}
#endif // defined(__SIZEOF_INT128__):MSVC 无 __int128,跳过整数精确参考测试