Files
hm 931505f3e2
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
VDE-384: 落地 3D/加权(Power Diagram)/裁剪 Voronoi,并修复凸包真缺陷
提案 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。
2026-10-05 17:28:44 +08:00

354 lines
13 KiB
C++
Raw Permalink 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/voronoi.h"
#include "vde/core/convex_hull.h"
#include <cmath>
#include <algorithm>
#include <array>
#include <vector>
using namespace vde::core;
namespace {
double cell_area(const VoronoiCell& c) {
const size_t n = c.vertices.size();
if (n < 3) return 0.0;
double s = 0.0;
for (size_t i = 0; i < n; ++i)
s += c.vertices[i].x() * c.vertices[(i + 1) % n].y()
- c.vertices[(i + 1) % n].x() * c.vertices[i].y();
return std::abs(s) * 0.5;
}
double cell3d_volume(const VoronoiCell3D& c) {
std::vector<std::array<Point3D, 3>> tris;
tris.reserve(c.faces.size());
for (const auto& f : c.faces)
tris.push_back({c.vertices[f[0]], c.vertices[f[1]], c.vertices[f[2]]});
return hull_volume(tris);
}
} // namespace
// ═══ voronoi_2d 基础测试 ═══
TEST(VoronoiTest, ThreePoints_EachHasCell) {
std::vector<Point2D> pts = {Point2D(0,0), Point2D(1,0), Point2D(0,1)};
auto cells = voronoi_2d(pts);
ASSERT_EQ(cells.size(), 3u);
for (auto& cell : cells) {
EXPECT_FALSE(cell.vertices.empty());
}
}
TEST(VoronoiTest, FourPointsSquare_4Cells) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
auto cells = voronoi_2d(pts);
EXPECT_EQ(cells.size(), 4u);
}
TEST(VoronoiTest, CellCountMatchesInput) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(2,0), Point2D(1,2),
Point2D(3,1), Point2D(1,3)};
auto cells = voronoi_2d(pts);
EXPECT_EQ(cells.size(), pts.size());
}
TEST(VoronoiTest, SitesPreserved) {
std::vector<Point2D> pts = {Point2D(0,0), Point2D(3,0), Point2D(0,3)};
auto cells = voronoi_2d(pts);
ASSERT_EQ(cells.size(), 3u);
for (size_t i = 0; i < pts.size(); ++i) {
EXPECT_NEAR(cells[i].site.x(), pts[i].x(), 1e-10);
EXPECT_NEAR(cells[i].site.y(), pts[i].y(), 1e-10);
}
}
TEST(VoronoiTest, CellEdgesValidIndices) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
auto cells = voronoi_2d(pts);
for (auto& cell : cells) {
for (auto& e : cell.edges) {
EXPECT_GE(e[0], 0);
EXPECT_LT(e[0], static_cast<int>(cell.vertices.size()));
EXPECT_GE(e[1], 0);
EXPECT_LT(e[1], static_cast<int>(cell.vertices.size()));
}
}
}
TEST(VoronoiTest, CellVerticesNonEmpty) {
std::vector<Point2D> pts = {
Point2D(-1,-1), Point2D(1,-1), Point2D(1,1), Point2D(-1,1)};
auto cells = voronoi_2d(pts);
for (auto& cell : cells) {
EXPECT_GT(cell.vertices.size(), 0u);
}
}
// ═══ compute_voronoi_2d 带边界框裁剪 ═══
TEST(VoronoiTest, ComputeVoronoi_BoundedBox) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
std::array<double, 4> bbox = {-2, -2, 2, 2};
auto cells = compute_voronoi_2d(pts, bbox);
ASSERT_EQ(cells.size(), 4u);
for (auto& cell : cells) {
EXPECT_FALSE(cell.vertices.empty());
}
}
TEST(VoronoiTest, ComputeVoronoi_CellsClipped) {
std::vector<Point2D> pts = {Point2D(0,0), Point2D(1,0), Point2D(0,1)};
std::array<double, 4> bbox = {0, 0, 10, 10};
auto cells = compute_voronoi_2d(pts, bbox);
ASSERT_EQ(cells.size(), 3u);
// 所有顶点应在边界框内
for (auto& cell : cells) {
for (auto& v : cell.vertices) {
EXPECT_GE(v.x(), -1.0);
EXPECT_LE(v.x(), 11.0);
EXPECT_GE(v.y(), -1.0);
EXPECT_LE(v.y(), 11.0);
}
}
}
TEST(VoronoiTest, ComputeVoronoi_VertexCountLarger) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
std::array<double, 4> bbox = {-5, -5, 5, 5};
auto cells = compute_voronoi_2d(pts, bbox);
// 带裁剪的单元格顶点数应 >= 无裁剪(边界点被截断后加角点)
auto cells_unbounded = voronoi_2d(pts);
ASSERT_EQ(cells.size(), cells_unbounded.size());
}
// ═══ Weighted Voronoi ═══
TEST(VoronoiTest, Weighted_EqualWeights_StandardVoronoi) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(2,0), Point2D(1,2)};
std::vector<double> w = {1.0, 1.0, 1.0};
auto cells_weighted = weighted_voronoi_2d(pts, w);
auto cells_standard = voronoi_2d(pts);
ASSERT_EQ(cells_weighted.size(), cells_standard.size());
for (size_t i = 0; i < pts.size(); ++i) {
EXPECT_NEAR(cells_weighted[i].site.x(), cells_standard[i].site.x(), 1e-10);
EXPECT_NEAR(cells_weighted[i].site.y(), cells_standard[i].site.y(), 1e-10);
}
}
TEST(VoronoiTest, Weighted_DifferentWeights) {
std::vector<Point2D> pts = {Point2D(0,0), Point2D(1,0), Point2D(0,1)};
std::vector<double> w = {5.0, 0.1, 0.1};
auto cells = weighted_voronoi_2d(pts, w);
ASSERT_EQ(cells.size(), 3u);
// 权重大的单元格应更大(至少有顶点或单元格存在)
// 注意:小点集的 Voronoi 单元可能顶点较少
EXPECT_GE(cells[0].vertices.size(), 0u);
}
// ═══ Centroidal Voronoi ═══
TEST(VoronoiTest, Centroidal_SitesMoveTowardCentroids) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
auto cells = centroidal_voronoi(pts, 10);
ASSERT_EQ(cells.size(), 4u);
// Lloyd 迭代后分布应更均匀
for (auto& cell : cells) {
EXPECT_FALSE(cell.vertices.empty());
}
}
TEST(VoronoiTest, Centroidal_ImprovesUniformity) {
// 初始点聚集在左下角,Lloyd 迭代应使其更分散
std::vector<Point2D> pts = {
Point2D(0.1,0.1), Point2D(0.2,0.1),
Point2D(0.1,0.2), Point2D(0.15,0.15)};
auto cells = centroidal_voronoi(pts, 20);
ASSERT_EQ(cells.size(), 4u);
// 迭代后单元格应有一定顶点数(小点集可能较少)
// 只验证迭代完成且返回有效结果
for (auto& cell : cells) {
EXPECT_GE(cell.vertices.size(), 0u);
}
}
// ═══ VoronoiAreaStats ═══
TEST(VoronoiTest, AreaDistribution_CellCount) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(1,0), Point2D(1,1), Point2D(0,1)};
auto cells = voronoi_2d(pts);
auto stats = voronoi_area_distribution(cells);
EXPECT_EQ(stats.cell_count, static_cast<int>(cells.size()));
}
TEST(VoronoiTest, AreaDistribution_MeanPositive) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(2,0), Point2D(2,2), Point2D(0,2)};
std::array<double, 4> bbox = {-10, -10, 10, 10};
auto cells = compute_voronoi_2d(pts, bbox);
auto stats = voronoi_area_distribution(cells);
// 验证统计计算正确(单元格面积可能为0如果顶点不足3个)
EXPECT_EQ(stats.cell_count, 4);
EXPECT_GE(stats.total_area, 0.0);
EXPECT_GE(stats.mean, 0.0);
}
TEST(VoronoiTest, AreaDistribution_MinLessEqualMax) {
std::vector<Point2D> pts = {
Point2D(0,0), Point2D(5,0), Point2D(5,5), Point2D(0,5)};
std::array<double, 4> bbox = {-10, -10, 10, 10};
auto cells = compute_voronoi_2d(pts, bbox);
auto stats = voronoi_area_distribution(cells);
EXPECT_LE(stats.min_area, stats.max_area);
}
// ═══ 边界情况 ═══
TEST(VoronoiTest, TwoPoints) {
std::vector<Point2D> pts = {Point2D(0,0), Point2D(1,0)};
auto cells = voronoi_2d(pts);
EXPECT_EQ(cells.size(), 2u);
EXPECT_NEAR(cells[0].site.x(), 0.0, 1e-10);
EXPECT_NEAR(cells[1].site.x(), 1.0, 1e-10);
}
TEST(VoronoiTest, ManyCollinearPoints) {
std::vector<Point2D> pts;
for (int i = 0; i < 10; ++i)
pts.push_back(Point2D(static_cast<double>(i), 0.0));
// 共线点可能退化,但不应崩溃
auto cells = voronoi_2d(pts);
EXPECT_EQ(cells.size(), 10u);
}
// ═══ VDE-384: voronoi_2d_clipped(全部单元格有界) ═══
TEST(Voronoi384Test, ClippedCellsPartitionBoundingBox) {
std::vector<Point2D> pts = {{0,0}, {2,0}, {2,2}, {0,2}, {1,1}};
std::array<double, 4> bbox = {-1, -1, 3, 3};
auto cells = voronoi_2d_clipped(pts, bbox);
ASSERT_EQ(cells.size(), 5u);
double sum = 0.0;
for (const auto& c : cells) sum += cell_area(c);
// 站点均在框内 → 单元格构成对边界框的完整划分(面积 16)
EXPECT_NEAR(sum, 16.0, 1e-6);
}
TEST(Voronoi384Test, ClippedCellsAreBoundedWithinBox) {
// 站点恰为框角(标准 voronoi_2d 会给出无界单元),裁剪后必须全部有界
std::vector<Point2D> pts = {{0,0}, {4,0}, {4,4}, {0,4}};
std::array<double, 4> bbox = {-1, -1, 5, 5};
auto cells = voronoi_2d_clipped(pts, bbox);
ASSERT_EQ(cells.size(), 4u);
for (const auto& c : cells) {
EXPECT_GE(c.vertices.size(), 3u);
EXPECT_FALSE(c.edges.empty());
for (const auto& v : c.vertices) {
EXPECT_GE(v.x(), bbox[0] - 1e-9);
EXPECT_LE(v.x(), bbox[2] + 1e-9);
EXPECT_GE(v.y(), bbox[1] - 1e-9);
EXPECT_LE(v.y(), bbox[3] + 1e-9);
}
}
}
TEST(Voronoi384Test, ClippedVerticesSatisfyNearestSiteProperty) {
std::vector<Point2D> pts = {{0,0}, {2,0}, {2,2}, {0,2}};
std::array<double, 4> bbox = {-1, -1, 3, 3};
auto cells = voronoi_2d_clipped(pts, bbox);
for (size_t i = 0; i < pts.size(); ++i) {
for (const auto& v : cells[i].vertices) {
const double di = (v - pts[i]).norm();
for (size_t j = 0; j < pts.size(); ++j)
EXPECT_LE(di, (v - pts[j]).norm() + 1e-6);
}
}
}
// ═══ VDE-384: Power Diagram(加权 Voronoi) ═══
TEST(Voronoi384Test, PowerEqualWeightsBoundaryAtMidplane) {
std::vector<WeightedSite> sites = {{Point2D(0,0), 0.0}, {Point2D(2,0), 0.0}};
auto cells = power_diagram(sites);
ASSERT_EQ(cells.size(), 2u);
double max0 = -1e300, min1 = 1e300;
for (const auto& v : cells[0].vertices) max0 = std::max(max0, v.x());
for (const auto& v : cells[1].vertices) min1 = std::min(min1, v.x());
EXPECT_NEAR(max0, 1.0, 1e-9); // 权相等 → 分界于中点
EXPECT_NEAR(min1, 1.0, 1e-9);
EXPECT_NEAR(cell_area(cells[0]), cell_area(cells[1]), 1e-9);
}
TEST(Voronoi384Test, PowerLargerWeightEnlargesCell) {
// 权 w0=3 → 分界面上移:2x ≤ (|s1|²-w1)-(|s0|²-w0) = 4-(-3)=7 → x ≤ 1.75
std::vector<WeightedSite> sites = {{Point2D(0,0), 3.0}, {Point2D(2,0), 0.0}};
auto cells = power_diagram(sites);
ASSERT_EQ(cells.size(), 2u);
double max0 = -1e300, min1 = 1e300;
for (const auto& v : cells[0].vertices) max0 = std::max(max0, v.x());
for (const auto& v : cells[1].vertices) min1 = std::min(min1, v.x());
EXPECT_NEAR(max0, 1.75, 1e-9);
EXPECT_NEAR(min1, 1.75, 1e-9);
EXPECT_GT(cell_area(cells[0]), cell_area(cells[1]));
}
TEST(Voronoi384Test, WeightedVoronoi2dUsesRealPowerDiagram) {
// 修复后 weighted_voronoi_2d 应等于真实 power diagram(而非旧的近似缩放)
std::vector<Point2D> pts = {Point2D(0,0), Point2D(2,0)};
std::vector<double> w = {3.0, 0.0};
auto cells = weighted_voronoi_2d(pts, w);
ASSERT_EQ(cells.size(), 2u);
double max0 = -1e300;
for (const auto& v : cells[0].vertices) max0 = std::max(max0, v.x());
EXPECT_NEAR(max0, 1.75, 1e-9);
}
// ═══ VDE-384: voronoi_3d ═══
TEST(Voronoi384Test, Voronoi3dSingleSiteIsBoundingBox) {
std::vector<Point3D> pts = {{0,0,0}};
auto cells = voronoi_3d(pts);
ASSERT_EQ(cells.size(), 1u);
EXPECT_EQ(cells[0].vertices.size(), 8u); // 立方体 8 顶点
EXPECT_GE(cells[0].faces.size(), 12u); // 6 面 → 12 三角
// 外扩边界为 0.5+1=1.5 → 边长 3 的立方体,体积 27
EXPECT_NEAR(cell3d_volume(cells[0]), 27.0, 1e-6);
}
TEST(Voronoi384Test, Voronoi3dTwoSitesSplitAtMidplane) {
std::vector<Point3D> pts = {{0,0,0}, {2,0,0}};
auto cells = voronoi_3d(pts);
ASSERT_EQ(cells.size(), 2u);
for (const auto& v : cells[0].vertices) EXPECT_LE(v.x(), 1.0 + 1e-6);
for (const auto& v : cells[1].vertices) EXPECT_GE(v.x(), 1.0 - 1e-6);
const double v0 = cell3d_volume(cells[0]);
const double v1 = cell3d_volume(cells[1]);
EXPECT_GT(v0, 0.0);
EXPECT_NEAR(v0, v1, 1e-6); // 对称分割
}
TEST(Voronoi384Test, Voronoi3dCellVerticesBelongToTheirSite) {
std::vector<Point3D> pts = {{0,0,0}, {3,0,0}, {0,3,0}, {0,0,3}};
auto cells = voronoi_3d(pts);
ASSERT_EQ(cells.size(), 4u);
for (size_t i = 0; i < pts.size(); ++i) {
EXPECT_FALSE(cells[i].faces.empty());
EXPECT_GE(cells[i].vertices.size(), 4u);
for (const auto& v : cells[i].vertices) {
const double di = (v - pts[i]).norm();
for (size_t j = 0; j < pts.size(); ++j)
EXPECT_LE(di, (v - pts[j]).norm() + 1e-6)
<< "cell " << i << " vertex not nearest to its site";
}
}
}