提案 4 项 API 全部实现(vde/core/voronoi.h): - VoronoiCell3D + voronoi_3d:站点包围盒内三平面交点枚举 + 半空间平面直接生成面 - WeightedSite + power_diagram:Laguerre 图,Sutherland–Hodgman 半平面裁剪 - voronoi_2d_clipped:复用 Power Diagram 内核按 bounding_rect 裁剪 - centroidal_voronoi:已有 修复既有假实现/缺陷: - weighted_voronoi_2d 原按权重缩放标准 Voronoi 顶点(近似),改为真实 Power Diagram - convex_hull_3d 初始四面体极端点选取退化(立方体仅出 4 面、体积 0), 重写为增量 QuickHull(最远点分配 + 地平线扩张) - approximate_hull 体素累加器读到未初始化 Eigen 内存(结果不确定),显式置零 测试:test_voronoi 27/27、test_convex_hull 27/27 全绿; 回归 test_polygon 17/17、test_point 5/5。
356 lines
12 KiB
C++
356 lines
12 KiB
C++
#include <gtest/gtest.h>
|
|
#include "vde/core/convex_hull.h"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <vector>
|
|
|
|
using namespace vde::core;
|
|
|
|
namespace {
|
|
|
|
// 旋转到字典序最小顶点开头,便于逐点比对(两者同为 CCW 时唯一)
|
|
std::vector<Point2D> canon(std::vector<Point2D> h) {
|
|
if (h.empty()) return h;
|
|
size_t m = 0;
|
|
for (size_t i = 1; i < h.size(); ++i) {
|
|
if (h[i].x() < h[m].x() ||
|
|
(h[i].x() == h[m].x() && h[i].y() < h[m].y())) m = i;
|
|
}
|
|
std::rotate(h.begin(), h.begin() + static_cast<long>(m), h.end());
|
|
return h;
|
|
}
|
|
|
|
void expect_same_seq(const std::vector<Point2D>& a, const std::vector<Point2D>& b) {
|
|
auto ca = canon(a);
|
|
auto cb = canon(b);
|
|
ASSERT_EQ(ca.size(), cb.size());
|
|
for (size_t i = 0; i < ca.size(); ++i) {
|
|
EXPECT_NEAR(ca[i].x(), cb[i].x(), 1e-9) << "i=" << i;
|
|
EXPECT_NEAR(ca[i].y(), cb[i].y(), 1e-9) << "i=" << i;
|
|
}
|
|
}
|
|
|
|
bool is_convex_ccw(const std::vector<Point2D>& p) {
|
|
const size_t n = p.size();
|
|
if (n < 3) return false;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
const Point2D& a = p[i];
|
|
const Point2D& b = p[(i + 1) % n];
|
|
const Point2D& c = p[(i + 2) % n];
|
|
const double cr = (b.x() - a.x()) * (c.y() - a.y()) - (b.y() - a.y()) * (c.x() - a.x());
|
|
if (cr < -1e-9) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
double poly_area(const std::vector<Point2D>& p) {
|
|
double s = 0.0;
|
|
const size_t n = p.size();
|
|
for (size_t i = 0; i < n; ++i) {
|
|
s += p[i].x() * p[(i + 1) % n].y() - p[(i + 1) % n].x() * p[i].y();
|
|
}
|
|
return std::abs(s) * 0.5;
|
|
}
|
|
|
|
double dist_pt_seg(const Point2D& a, const Point2D& b, const Point2D& p) {
|
|
const double dx = b.x() - a.x(), dy = b.y() - a.y();
|
|
const double l2 = dx * dx + dy * dy;
|
|
if (l2 <= 1e-30) return std::hypot(p.x() - a.x(), p.y() - a.y());
|
|
double t = ((p.x() - a.x()) * dx + (p.y() - a.y()) * dy) / l2;
|
|
t = std::max(0.0, std::min(1.0, t));
|
|
return std::hypot(p.x() - (a.x() + t * dx), p.y() - (a.y() + t * dy));
|
|
}
|
|
|
|
double dist_to_boundary(const std::vector<Point2D>& poly, const Point2D& p) {
|
|
double m = 1e300;
|
|
const size_t n = poly.size();
|
|
for (size_t i = 0; i < n; ++i)
|
|
m = std::min(m, dist_pt_seg(poly[i], poly[(i + 1) % n], p));
|
|
return m;
|
|
}
|
|
|
|
// 一批含内部点 + 多个外部极值点的测试点集
|
|
std::vector<Point2D> sample_points() {
|
|
return {
|
|
{0, 0}, {10, 0}, {10, 10}, {0, 10}, // 方形
|
|
{5, 5}, {2, 8}, {9, 1}, {3, 3}, // 内部点
|
|
{12, 5}, {-2, 5}, {5, -3}, {5, 12}, {11, 11}, // 外部点
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ── 既有基础用例 ────────────────────────────────────
|
|
|
|
TEST(ConvexHullTest, ThreePoints_ReturnsTriangle) {
|
|
std::vector<Point2D> pts = {{0,0}, {1,0}, {0,1}};
|
|
auto hull = convex_hull_2d(pts);
|
|
EXPECT_EQ(hull.size(), 3u);
|
|
}
|
|
|
|
TEST(ConvexHullTest, CollinearPoints_ReturnsEndpoints) {
|
|
std::vector<Point2D> pts = {{0,0}, {1,1}, {2,2}, {3,3}};
|
|
auto hull = convex_hull_2d(pts);
|
|
EXPECT_GE(hull.size(), 2u);
|
|
}
|
|
|
|
TEST(ConvexHullTest, Empty_ReturnsEmpty) {
|
|
auto hull = convex_hull_2d({});
|
|
EXPECT_TRUE(hull.empty());
|
|
}
|
|
|
|
// ── VDE-383: 增量凸包 ───────────────────────────────
|
|
|
|
TEST(IncrementalHull2DTest, StepwiseMatchesBatchHull) {
|
|
const auto pts = sample_points();
|
|
IncrementalHull2D inc;
|
|
std::vector<Point2D> prefix;
|
|
for (const auto& p : pts) {
|
|
inc.insert(p);
|
|
prefix.push_back(p);
|
|
// 每一步都必须与对当前前缀做批量凸包完全一致
|
|
expect_same_seq(inc.hull(), convex_hull_2d(prefix));
|
|
}
|
|
EXPECT_EQ(inc.size(), pts.size());
|
|
}
|
|
|
|
TEST(IncrementalHull2DTest, ContainsInsideAndOutside) {
|
|
IncrementalHull2D inc({{0,0}, {10,0}, {10,10}, {0,10}});
|
|
EXPECT_TRUE(inc.contains({5, 5}));
|
|
EXPECT_TRUE(inc.contains({0, 5})); // 边界上
|
|
EXPECT_TRUE(inc.contains({0, 0})); // 顶点
|
|
EXPECT_FALSE(inc.contains({11, 5}));
|
|
EXPECT_FALSE(inc.contains({-1, 5}));
|
|
EXPECT_FALSE(inc.contains({5, 20}));
|
|
}
|
|
|
|
TEST(IncrementalHull2DTest, InsertInteriorPointKeepsHull) {
|
|
IncrementalHull2D inc({{0,0}, {10,0}, {10,10}, {0,10}});
|
|
auto before = inc.hull();
|
|
inc.insert({5, 5});
|
|
inc.insert({2, 2});
|
|
expect_same_seq(inc.hull(), before);
|
|
EXPECT_EQ(inc.hull().size(), 4u);
|
|
}
|
|
|
|
TEST(IncrementalHull2DTest, RemoveRebuildsHull) {
|
|
auto pts = sample_points();
|
|
IncrementalHull2D inc(pts);
|
|
// 移除两个外部极值点后应等于对剩余点做批量凸包
|
|
inc.remove({5, -3});
|
|
inc.remove({-2, 5});
|
|
std::vector<Point2D> remain;
|
|
for (const auto& p : pts) {
|
|
if (std::hypot(p.x() - 5, p.y() + 3) < 1e-9) continue;
|
|
if (std::hypot(p.x() + 2, p.y() - 5) < 1e-9) continue;
|
|
remain.push_back(p);
|
|
}
|
|
EXPECT_EQ(inc.size(), remain.size());
|
|
expect_same_seq(inc.hull(), convex_hull_2d(remain));
|
|
}
|
|
|
|
TEST(IncrementalHull2DTest, FreeFunctionConstructs) {
|
|
auto inc = convex_hull_incremental_2d({{0,0}, {1,0}, {0,1}});
|
|
EXPECT_EQ(inc.hull().size(), 3u);
|
|
EXPECT_TRUE(inc.contains({0.2, 0.2}));
|
|
EXPECT_FALSE(inc.contains({5, 5}));
|
|
}
|
|
|
|
// ── VDE-383: ε-近似凸包 ─────────────────────────────
|
|
|
|
TEST(ApproximateHullTest, ReducesCircleVertices) {
|
|
std::vector<Point2D> pts;
|
|
const int N = 64;
|
|
for (int i = 0; i < N; ++i) {
|
|
const double a = 2.0 * 3.14159265358979323846 * i / N;
|
|
pts.push_back({100.0 * std::cos(a), 100.0 * std::sin(a)});
|
|
}
|
|
auto exact = convex_hull_2d(pts);
|
|
auto approx = convex_hull_2d_approximate(pts, 8.0);
|
|
EXPECT_EQ(exact.size(), static_cast<size_t>(N));
|
|
EXPECT_LT(approx.size(), exact.size());
|
|
EXPECT_GE(approx.size(), 3u);
|
|
EXPECT_TRUE(is_convex_ccw(approx));
|
|
}
|
|
|
|
TEST(ApproximateHullTest, EveryExactVertexWithinEpsilon) {
|
|
std::vector<Point2D> pts;
|
|
const int N = 48;
|
|
for (int i = 0; i < N; ++i) {
|
|
const double a = 2.0 * 3.14159265358979323846 * i / N;
|
|
pts.push_back({50.0 * std::cos(a), 30.0 * std::sin(a)});
|
|
}
|
|
const double eps = 3.0;
|
|
auto exact = convex_hull_2d(pts);
|
|
auto approx = convex_hull_2d_approximate(pts, eps);
|
|
ASSERT_GE(approx.size(), 3u);
|
|
// 保证:真凸包边界上每个顶点到近似多边形边界的距离 ≤ ε
|
|
for (const auto& v : exact) {
|
|
EXPECT_LE(dist_to_boundary(approx, v), eps + 1e-6)
|
|
<< "vertex (" << v.x() << "," << v.y() << ")";
|
|
}
|
|
// 近似多边形为内接多边形,面积不超过真凸包
|
|
EXPECT_LE(poly_area(approx), poly_area(exact) + 1e-6);
|
|
}
|
|
|
|
TEST(ApproximateHullTest, KeepsOriginalVerticesOnly) {
|
|
auto pts = sample_points();
|
|
auto exact = convex_hull_2d(pts);
|
|
auto approx = convex_hull_2d_approximate(pts, 1.5);
|
|
for (const auto& p : approx) {
|
|
bool found = false;
|
|
for (const auto& q : exact)
|
|
if (std::hypot(p.x() - q.x(), p.y() - q.y()) < 1e-9) { found = true; break; }
|
|
EXPECT_TRUE(found);
|
|
}
|
|
}
|
|
|
|
TEST(ApproximateHullTest, NonPositiveEpsilonReturnsExact) {
|
|
auto pts = sample_points();
|
|
auto exact = convex_hull_2d(pts);
|
|
expect_same_seq(convex_hull_2d_approximate(pts, 0.0), exact);
|
|
expect_same_seq(convex_hull_2d_approximate(pts, -1.0), exact);
|
|
}
|
|
|
|
TEST(ApproximateHullTest, SmallEpsilonKeepsSquare) {
|
|
std::vector<Point2D> sq = {{0,0}, {10,0}, {10,10}, {0,10}};
|
|
auto approx = convex_hull_2d_approximate(sq, 0.5);
|
|
EXPECT_EQ(approx.size(), 4u);
|
|
EXPECT_NEAR(poly_area(approx), 100.0, 1e-9);
|
|
}
|
|
|
|
// ── VDE-383: 凹多边形凸分解 ──────────────────────────
|
|
|
|
TEST(ConvexDecompositionTest, ConvexPolygonReturnsSinglePiece) {
|
|
std::vector<Point2D> sq = {{0,0}, {4,0}, {4,4}, {0,4}};
|
|
auto pieces = convex_decomposition(sq);
|
|
ASSERT_EQ(pieces.size(), 1u);
|
|
EXPECT_TRUE(is_convex_ccw(pieces[0]));
|
|
EXPECT_NEAR(poly_area(pieces[0]), 16.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexDecompositionTest, LShapeSplitsIntoConvexPieces) {
|
|
// L 形(CCW),面积 = 12,凹点在 (2,2)
|
|
std::vector<Point2D> l = {{0,0}, {4,0}, {4,2}, {2,2}, {2,4}, {0,4}};
|
|
const double area = poly_area(l);
|
|
EXPECT_NEAR(area, 12.0, 1e-9);
|
|
auto pieces = convex_decomposition(l);
|
|
ASSERT_GE(pieces.size(), 2u);
|
|
double sum = 0.0;
|
|
for (const auto& piece : pieces) {
|
|
EXPECT_TRUE(is_convex_ccw(piece));
|
|
sum += poly_area(piece);
|
|
}
|
|
EXPECT_NEAR(sum, area, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexDecompositionTest, ClockwiseInputNormalized) {
|
|
// 与上面同一 L 形但为 CW 朝向
|
|
std::vector<Point2D> l = {{0,4}, {2,4}, {2,2}, {4,2}, {4,0}, {0,0}};
|
|
auto pieces = convex_decomposition(l);
|
|
double sum = 0.0;
|
|
for (const auto& piece : pieces) {
|
|
EXPECT_TRUE(is_convex_ccw(piece));
|
|
sum += poly_area(piece);
|
|
}
|
|
EXPECT_NEAR(sum, 12.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexDecompositionTest, CombShapeAreaConserved) {
|
|
// 梳齿多边形,含 3 个凹点
|
|
std::vector<Point2D> comb = {
|
|
{0,0}, {6,0}, {6,4},
|
|
{5,4}, {5,1}, {4,1}, {4,4},
|
|
{3,4}, {3,1}, {2,1}, {2,4},
|
|
{1,4}, {1,1}, {0,4}
|
|
};
|
|
const double area = poly_area(comb);
|
|
auto pieces = convex_decomposition(comb);
|
|
ASSERT_GE(pieces.size(), 2u);
|
|
double sum = 0.0;
|
|
for (const auto& piece : pieces) {
|
|
EXPECT_TRUE(is_convex_ccw(piece));
|
|
sum += poly_area(piece);
|
|
}
|
|
EXPECT_NEAR(sum, area, 1e-9);
|
|
}
|
|
|
|
// ── VDE-383: 凸包面积 ───────────────────────────────
|
|
|
|
TEST(HullAreaTest, UnitSquare) {
|
|
std::vector<Point2D> sq = {{0,0}, {1,0}, {1,1}, {0,1}};
|
|
EXPECT_NEAR(hull_area(sq), 1.0, 1e-12);
|
|
// 朝向不影响结果
|
|
std::reverse(sq.begin(), sq.end());
|
|
EXPECT_NEAR(hull_area(sq), 1.0, 1e-12);
|
|
}
|
|
|
|
TEST(HullAreaTest, Triangle) {
|
|
std::vector<Point2D> tri = {{0,0}, {2,0}, {0,3}};
|
|
EXPECT_NEAR(hull_area(tri), 3.0, 1e-12);
|
|
}
|
|
|
|
TEST(HullAreaTest, DegenerateReturnsZero) {
|
|
EXPECT_DOUBLE_EQ(hull_area({}), 0.0);
|
|
EXPECT_DOUBLE_EQ(hull_area({{0,0}}), 0.0);
|
|
EXPECT_DOUBLE_EQ(hull_area({{0,0}, {1,1}}), 0.0);
|
|
}
|
|
|
|
TEST(HullAreaTest, MatchesHullOfSamplePoints) {
|
|
auto hull = convex_hull_2d(sample_points());
|
|
EXPECT_NEAR(hull_area(hull), poly_area(hull), 1e-12);
|
|
EXPECT_GT(hull_area(hull), 100.0); // 大于内部方形
|
|
}
|
|
|
|
// ── 3D 凸包(旧实现极端点选取退化,立方体仅出 4 面) ──
|
|
|
|
TEST(ConvexHull3DTest, CubeReturnsTwelveFacesUnitVolume) {
|
|
std::vector<Point3D> cube = {{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
|
|
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}};
|
|
auto tris = convex_hull_3d(cube);
|
|
EXPECT_EQ(tris.size(), 12u);
|
|
EXPECT_NEAR(hull_volume(tris), 1.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexHull3DTest, TetrahedronFourFaces) {
|
|
std::vector<Point3D> tet = {{0,0,0}, {1,0,0}, {0,1,0}, {0,0,1}};
|
|
auto tris = convex_hull_3d(tet);
|
|
EXPECT_EQ(tris.size(), 4u);
|
|
EXPECT_NEAR(hull_volume(tris), 1.0 / 6.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexHull3DTest, InteriorPointDropped) {
|
|
std::vector<Point3D> pts = {{0,0,0}, {2,0,0}, {0,2,0}, {0,0,2}, {0.2,0.2,0.2}};
|
|
auto tris = convex_hull_3d(pts);
|
|
EXPECT_EQ(tris.size(), 4u);
|
|
EXPECT_NEAR(hull_volume(tris), 4.0 / 3.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexHull3DTest, CoplanarReturnsEmpty) {
|
|
std::vector<Point3D> pts = {{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0}};
|
|
EXPECT_TRUE(convex_hull_3d(pts).empty());
|
|
}
|
|
|
|
TEST(ConvexHull3DTest, IncrementalHullMatchesBatchVolume) {
|
|
std::vector<Point3D> pts = {{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
|
|
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1},
|
|
{0.5,0.5,0.5}};
|
|
auto batch = convex_hull_3d(pts);
|
|
auto inc = incremental_hull(pts);
|
|
EXPECT_NEAR(hull_volume(batch), 1.0, 1e-9);
|
|
EXPECT_NEAR(hull_volume(inc), 1.0, 1e-9);
|
|
}
|
|
|
|
TEST(ConvexHull3DTest, ApproximateHullKeepsVolume) {
|
|
std::vector<Point3D> pts;
|
|
for (int i = 0; i <= 8; ++i)
|
|
for (int j = 0; j <= 8; ++j)
|
|
for (int k = 0; k <= 8; ++k)
|
|
if (i == 0 || i == 8 || j == 0 || j == 8 || k == 0 || k == 8)
|
|
pts.push_back({i * 0.25, j * 0.25, k * 0.25});
|
|
auto approx = approximate_hull(pts, 0.25);
|
|
EXPECT_FALSE(approx.empty());
|
|
EXPECT_NEAR(hull_volume(approx), 8.0, 1e-6); // 边长 2 的立方体
|
|
}
|
|
|