Files
hm 4ccab8663e fix(brep): preserve trimmed surfaces and shared boundaries
Use robust constrained triangulation, retain narrow trim boundaries, and keep curve projection and shared-edge tessellation consistent across STEP faces. Add generic regression coverage and the required CDT headers.
2026-10-30 13:06:29 +08:00

165 lines
5.8 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/mesh/delaunay_2d.h"
#include <algorithm>
#include <map>
#include <utility>
using namespace vde::mesh;
namespace {
void expect_manifold_edges(const DelaunayResult& result) {
std::map<std::pair<int, int>, int> uses;
for (const auto& t : result.triangles)
for (int k = 0; k < 3; ++k)
++uses[std::minmax(t[k], t[(k + 1) % 3])];
for (const auto& [edge, count] : uses)
EXPECT_LE(count, 2) << "non-manifold edge " << edge.first << ", " << edge.second;
}
}
TEST(DelaunayTest, FourPointsSquare) {
std::vector<Point2D> pts = {{0,0}, {1,0}, {1,1}, {0,1}};
auto result = delaunay_2d(pts);
EXPECT_GE(result.triangles.size(), 2u);
EXPECT_EQ(result.vertices.size(), 4u);
}
TEST(DelaunayTest, ThreePoints) {
std::vector<Point2D> pts = {{0,0}, {1,0}, {0,1}};
auto result = delaunay_2d(pts);
EXPECT_EQ(result.triangles.size(), 1u);
}
TEST(DelaunayTest, FivePointsStar) {
// 中心点 + 四个方向的点,形成十字/星形
std::vector<Point2D> pts = {{0,0}, {1,0}, {0,1}, {-1,0}, {0,-1}};
auto result = delaunay_2d(pts);
EXPECT_GT(result.triangles.size(), 3u);
}
TEST(DelaunayTest, ColinearPoints) {
// 所有点共线(退化情况),不应崩溃
std::vector<Point2D> pts = {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}};
auto result = delaunay_2d(pts);
// 共线点无法形成有效三角形,但不应崩溃
(void)result;
}
TEST(DelaunayTest, ManyPointsRandom) {
// 生成 50 个伪随机点(确定性种子)
std::vector<Point2D> pts;
unsigned int seed = 12345;
for (int i = 0; i < 50; ++i) {
seed = seed * 1103515245 + 12345;
double x = static_cast<double>((seed >> 16) & 0x7FFF) / 32767.0 * 100.0;
seed = seed * 1103515245 + 12345;
double y = static_cast<double>((seed >> 16) & 0x7FFF) / 32767.0 * 100.0;
pts.push_back({x, y});
}
auto result = delaunay_2d(pts);
EXPECT_GT(result.triangles.size(), 0u);
EXPECT_GE(result.vertices.size(), pts.size());
expect_manifold_edges(result);
}
TEST(DelaunayTest, NearlyCocircularImportedRingStaysManifold) {
// 格网 STEP 第 22 面的实际输入前缀:第 17 点是近共圆圆弧端点。
// 旧实现会把不连通的空圆分量一并删除,产生被三个以上三角形使用的边。
const std::vector<Point2D> pts = {
{0, 0},
{1.9999999999999929, 0},
{1.9999999999999929, 3.0000000000000071},
{0, 3.0000000000000142},
{0, 11.600000000000005},
{0.0024695845542090922, 11.644379824194068},
{0.010249006113426162, 11.689967565068905},
{0.023773924736008922, 11.735845280711018},
{0.043269061937689202, 11.780950373940449},
{0.068696160781772875, 11.824137828398658},
{0.099725084521711693, 11.864263079401017},
{0.135736920598994, 11.900274915478299},
{0.17586217160134865, 11.931303839218231},
{0.21904962605955802, 11.956730938062318},
{0.26415471928898882, 11.976226075263998},
{0.31003243493110233, 11.989750993886584},
{0.3556201758059423, 11.997530415445798},
{0.40000000000000213, 12.000000000000007},
};
expect_manifold_edges(delaunay_2d(pts));
}
TEST(DelaunayTest, TriangleVerticesMatchInput) {
// 所有输入点都应出现在结果顶点中
std::vector<Point2D> pts = {{0,0}, {3,0}, {3,4}, {0,4}, {1.5, 2}};
auto result = delaunay_2d(pts);
for (const auto& p : pts) {
bool found = false;
for (const auto& v : result.vertices) {
if (v[0] == p[0] && v[1] == p[1]) {
found = true;
break;
}
}
EXPECT_TRUE(found) << "Input point (" << p[0] << "," << p[1] << ") not found in result vertices";
}
}
TEST(DelaunayTest, TriangleIndicesValid) {
// 所有三角形索引应在 [0, vertices.size()) 范围内
std::vector<Point2D> pts = {{0,0}, {5,0}, {5,5}, {0,5}, {2,2}};
auto result = delaunay_2d(pts);
ASSERT_GT(result.vertices.size(), 0u);
for (const auto& tri : result.triangles) {
for (int idx : tri) {
EXPECT_GE(idx, 0);
EXPECT_LT(static_cast<size_t>(idx), result.vertices.size());
}
}
}
TEST(DelaunayTest, TriangleOrientationCCW) {
// 验证所有三角形为逆时针方向(有向面积为正)
std::vector<Point2D> pts = {{0,0}, {4,0}, {4,3}, {0,3}, {2,1}};
auto result = delaunay_2d(pts);
ASSERT_GT(result.vertices.size(), 0u);
for (const auto& tri : result.triangles) {
const auto& a = result.vertices[tri[0]];
const auto& b = result.vertices[tri[1]];
const auto& c = result.vertices[tri[2]];
// 有向面积的二倍:(b-a) × (c-a)
double cross = (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
EXPECT_GT(cross, 0.0) << "Triangle not CCW: idx=(" << tri[0] << "," << tri[1] << "," << tri[2] << ")";
}
}
TEST(DelaunayTest, TwoPoints) {
// 仅两个点(退化),不应崩溃
std::vector<Point2D> pts = {{0,0}, {1,1}};
auto result = delaunay_2d(pts);
(void)result;
}
TEST(DelaunayTest, IdenticalPoints) {
// 多个点在同一位置,不应崩溃
std::vector<Point2D> pts = {{5,5}, {5,5}, {5,5}, {5,5}};
auto result = delaunay_2d(pts);
(void)result;
}
TEST(DelaunayTest, LargeCoordinates) {
// 大坐标值 (1e6),验证三角化有效
std::vector<Point2D> pts = {
{1e6, 1e6}, {1e6 + 100, 1e6}, {1e6 + 100, 1e6 + 100},
{1e6, 1e6 + 100}, {1e6 + 50, 1e6 + 50}
};
auto result = delaunay_2d(pts);
EXPECT_GT(result.triangles.size(), 0u);
// 验证索引有效
for (const auto& tri : result.triangles) {
for (int idx : tri) {
EXPECT_GE(idx, 0);
EXPECT_LT(static_cast<size_t>(idx), result.vertices.size());
}
}
}