Files
ViewDesignEngine/tests/mesh/test_mesh_1177_fixes.cpp
hm d2f0a40d10 fix(brep,mesh): entity diagnostics/ref validation + robust polygon triangulation (VDE-1342..1345, 1.0.176)
- VDE-1345 mesh: to_mcm ear-clips n>3 faces (Newell normal + 2D ear clipping,
  fallback to fan on degeneracy) and maps local triangle indices back to face
  vertices; empty HalfedgeMesh/PointCloud bounds return a finite zero box;
  faces_range()/vertex_one_ring() implemented (FaceRange iterator, adjacency
  from vertex outgoing half-edges).
- VDE-1342 FeatureNode: last_error()/last_evaluate_ok() diagnostics, stronger
  boolean success predicate, invalid primitive/edit parameters throw
  std::invalid_argument instead of being silently clamped.
- VDE-1343 edit_feature: face_ids range validation before the no-op early
  return, analytic hole location first, warnings on geometric fallback,
  15% radial consistency check between reference faces and matched hole.
- VDE-1344 rebind_entity: id hit now verified against the anchor distance
  (vertex/edge/face), falls back to nearest-anchor match, returns -1 when
  still mismatched; Body rebinds through anchor's nearest face.
- Tests: test_mesh_1177_fixes 11 cases, test_entity_1177_fixes 12 cases;
  registry 8,339 -> 8,362; full CTest 8,362/8,362 passed, 0 failed, 35 SKIP,
  635.55 s; release checks 14/14 PASS, report-check latest=224.
- Docs: report 224 (self-verification), verifier report 223 (1.0.175 PASS)
  included, RegressionExtreme count corrected to 10/10 (P3-1).
  Mesh VDE id renumbered 1341 -> 1345 to avoid collision with upstream
  docs/feedback/VDE-1341 (shell_multi).
2026-11-07 03:53:07 +08:00

239 lines
8.5 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.
// VDE-1345 / 1.0.176: to_mcm 多边面耳切三角化 + 空网格/空点云 bounds 契约。
#include <gtest/gtest.h>
#include "vde/mesh/halfedge_mesh.h"
#include "vde/mesh/marching_cubes.h"
#include "vde/mesh/point_cloud.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <vector>
using namespace vde::mesh;
using vde::core::Point3D;
namespace {
double tri_area_2d(const Point3D& a, const Point3D& b, const Point3D& c) {
return 0.5 * std::abs((b.x() - a.x()) * (c.y() - a.y()) -
(b.y() - a.y()) * (c.x() - a.x()));
}
double polygon_area_2d(const std::vector<Point3D>& p) {
double s = 0.0;
for (std::size_t i = 0; i < p.size(); ++i) {
const auto& a = p[i];
const auto& b = p[(i + 1) % p.size()];
s += a.x() * b.y() - b.x() * a.y();
}
return 0.5 * std::abs(s);
}
bool point_in_polygon_2d(const std::vector<Point3D>& p, double x, double y) {
bool inside = false;
for (std::size_t i = 0, j = p.size() - 1; i < p.size(); j = i++) {
const double xi = p[i].x(), yi = p[i].y();
const double xj = p[j].x(), yj = p[j].y();
if (((yi > y) != (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi))
inside = !inside;
}
return inside;
}
HalfedgeMesh make_polygon_mesh(const std::vector<Point3D>& pts) {
HalfedgeMesh m;
std::vector<int> ids;
ids.reserve(pts.size());
for (const auto& p : pts) ids.push_back(m.add_vertex(p));
EXPECT_GE(m.add_face(ids), 0);
return m;
}
} // namespace
// U 形凹多边形(开口朝上,缺口 x∈[2,3], y>2):面积 9-2=7。
TEST(MeshToMcmFix, ConcavePolygonTriangulatesWithoutSpillingOut) {
const std::vector<Point3D> poly = {
{1, 1, 0}, {4, 1, 0}, {4, 4, 0}, {3, 4, 0},
{3, 2, 0}, {2, 2, 0}, {2, 4, 0}, {1, 4, 0}};
HalfedgeMesh m = make_polygon_mesh(poly);
ASSERT_EQ(m.num_faces(), 1u);
const MCMesh mc = m.to_mcm();
ASSERT_EQ(mc.triangles.size(), poly.size() - 2);
double total = 0.0;
for (const auto& t : mc.triangles) {
const Point3D& a = mc.vertices[static_cast<std::size_t>(t[0])];
const Point3D& b = mc.vertices[static_cast<std::size_t>(t[1])];
const Point3D& c = mc.vertices[static_cast<std::size_t>(t[2])];
const double area = tri_area_2d(a, b, c);
EXPECT_GT(area, 1e-12);
total += area;
const double cx = (a.x() + b.x() + c.x()) / 3.0;
const double cy = (a.y() + b.y() + c.y()) / 3.0;
EXPECT_TRUE(point_in_polygon_2d(poly, cx, cy))
<< "triangle centroid (" << cx << "," << cy << ") outside polygon";
}
EXPECT_NEAR(total, 7.0, 1e-9);
EXPECT_NEAR(polygon_area_2d(poly), 7.0, 1e-12);
}
// 凸五边形:耳切同样应给出 n-2 个三角形且面积精确。
TEST(MeshToMcmFix, ConvexPolygonKeepsAreaAndCount) {
const std::vector<Point3D> poly = {
{0, 0, 0}, {2, 0, 0}, {3, 2, 0}, {1, 3, 0}, {-1, 2, 0}};
HalfedgeMesh m = make_polygon_mesh(poly);
const MCMesh mc = m.to_mcm();
ASSERT_EQ(mc.triangles.size(), poly.size() - 2);
double total = 0.0;
for (const auto& t : mc.triangles) {
total += tri_area_2d(mc.vertices[static_cast<std::size_t>(t[0])],
mc.vertices[static_cast<std::size_t>(t[1])],
mc.vertices[static_cast<std::size_t>(t[2])]);
}
EXPECT_NEAR(total, polygon_area_2d(poly), 1e-9);
}
// 非平面四边形(微起伏):投影到最佳拟合平面后应给出 2 个三角形。
TEST(MeshToMcmFix, NonPlanarQuadTriangulates) {
const std::vector<Point3D> poly = {
{0, 0, 0}, {2, 0, 0.2}, {2, 2, 0}, {0, 2, -0.2}};
HalfedgeMesh m = make_polygon_mesh(poly);
const MCMesh mc = m.to_mcm();
EXPECT_EQ(mc.triangles.size(), 2u);
}
// 三角面网格:输出应与面数组一一对应(不受耳切路径影响)。
TEST(MeshToMcmFix, TriangleFacesPassThrough) {
HalfedgeMesh m;
m.add_vertex(Point3D(0, 0, 0));
m.add_vertex(Point3D(1, 0, 0));
m.add_vertex(Point3D(0, 1, 0));
m.add_vertex(Point3D(1, 1, 0));
ASSERT_GE(m.add_face({0, 1, 2}), 0);
ASSERT_GE(m.add_face({1, 3, 2}), 0);
const MCMesh mc = m.to_mcm();
ASSERT_EQ(mc.triangles.size(), 2u);
// face_vertices 可能从环的任意顶点起始(旋转等价),按顶点集合比较。
auto sorted_tri = [](std::array<int, 3> t) {
std::sort(t.begin(), t.end());
return t;
};
std::vector<std::array<int, 3>> got = {sorted_tri(mc.triangles[0]),
sorted_tri(mc.triangles[1])};
std::sort(got.begin(), got.end());
const std::vector<std::array<int, 3>> want = {
sorted_tri({0, 1, 2}), sorted_tri({1, 3, 2})};
EXPECT_EQ(got, want);
}
// 审计 P2-3 复现:凹五边形 (0,0),(3,0),(3,3),(1.5,1),(0,3),面积 6。
// 旧扇三角化 rot=0 得 7.5(+25%)、rot=1 得 12(2×)。
TEST(MeshToMcmFix, ConcavePentagonFromAuditIsExact) {
const std::vector<Point3D> poly = {
{0, 0, 0}, {3, 0, 0}, {3, 3, 0}, {1.5, 1, 0}, {0, 3, 0}};
HalfedgeMesh m = make_polygon_mesh(poly);
const MCMesh mc = m.to_mcm();
ASSERT_EQ(mc.triangles.size(), poly.size() - 2);
double total = 0.0;
for (const auto& t : mc.triangles) {
total += tri_area_2d(mc.vertices[static_cast<std::size_t>(t[0])],
mc.vertices[static_cast<std::size_t>(t[1])],
mc.vertices[static_cast<std::size_t>(t[2])]);
}
EXPECT_NEAR(total, 6.0, 1e-9);
}
// 同一凹多边形按旋转后的顶点顺序插入:结果不得随内部环起点变化。
TEST(MeshToMcmFix, ConcavePentagonRotatedInsertionIsExact) {
const std::vector<Point3D> poly = {
{3, 3, 0}, {1.5, 1, 0}, {0, 3, 0}, {0, 0, 0}, {3, 0, 0}};
HalfedgeMesh m = make_polygon_mesh(poly);
const MCMesh mc = m.to_mcm();
ASSERT_EQ(mc.triangles.size(), poly.size() - 2);
double total = 0.0;
for (const auto& t : mc.triangles) {
total += tri_area_2d(mc.vertices[static_cast<std::size_t>(t[0])],
mc.vertices[static_cast<std::size_t>(t[1])],
mc.vertices[static_cast<std::size_t>(t[2])]);
}
EXPECT_NEAR(total, 6.0, 1e-9);
}
// 空网格 bounds 必须是有限的原点退化盒(不是 ±inf)。
TEST(MeshBoundsFix, EmptyMeshBoundsIsFiniteZero) {
HalfedgeMesh m;
const auto b = m.bounds();
for (int i = 0; i < 3; ++i) {
EXPECT_TRUE(std::isfinite(b.min()[i]));
EXPECT_TRUE(std::isfinite(b.max()[i]));
EXPECT_DOUBLE_EQ(b.extent()[i], 0.0);
}
}
// 非空网格 bounds 仍应覆盖全部顶点。
TEST(MeshBoundsFix, NonEmptyMeshBoundsCoversVertices) {
HalfedgeMesh m;
m.add_vertex(Point3D(-1, 2, 3));
m.add_vertex(Point3D(4, -5, 6));
const auto b = m.bounds();
EXPECT_DOUBLE_EQ(b.min().x(), -1.0);
EXPECT_DOUBLE_EQ(b.max().x(), 4.0);
EXPECT_DOUBLE_EQ(b.min().y(), -5.0);
EXPECT_DOUBLE_EQ(b.max().z(), 6.0);
}
// ── 审计 P2-4:faces_range()/vertex_one_ring() 此前只有声明,按文档使用即链接失败。
TEST(MeshRangeApiFix, FacesRangeIteratesAllFaces) {
HalfedgeMesh m;
m.add_vertex(Point3D(0, 0, 0));
m.add_vertex(Point3D(1, 0, 0));
m.add_vertex(Point3D(0, 1, 0));
m.add_vertex(Point3D(1, 1, 0));
ASSERT_GE(m.add_face({0, 1, 2}), 0);
ASSERT_GE(m.add_face({1, 3, 2}), 0);
int count = 0;
for (const auto& f : m.faces_range()) {
EXPECT_EQ(f.valence, 3);
EXPECT_GE(f.halfedge_index, 0);
++count;
}
EXPECT_EQ(count, 2);
}
TEST(MeshRangeApiFix, VertexOneRingReturnsAdjacentVertices) {
HalfedgeMesh m;
m.add_vertex(Point3D(0, 0, 0));
m.add_vertex(Point3D(1, 0, 0));
m.add_vertex(Point3D(1, 1, 0));
m.add_vertex(Point3D(0, 1, 0));
ASSERT_GE(m.add_face({0, 1, 2}), 0);
ASSERT_GE(m.add_face({0, 2, 3}), 0);
std::vector<int> ring0;
for (int vj : m.vertex_one_ring(0)) ring0.push_back(vj);
EXPECT_EQ(ring0, (std::vector<int>{1, 2, 3}));
std::vector<int> ring1;
for (int vj : m.vertex_one_ring(1)) ring1.push_back(vj);
EXPECT_EQ(ring1, (std::vector<int>{0, 2}));
EXPECT_TRUE(m.vertex_one_ring(-1).empty());
EXPECT_TRUE(m.vertex_one_ring(999).empty());
}
// 空点云 bounds 与空网格保持一致语义。
TEST(MeshBoundsFix, EmptyPointCloudBoundsIsFiniteZero) {
PointCloud pc;
const auto b = pc.bounds();
for (int i = 0; i < 3; ++i) {
EXPECT_TRUE(std::isfinite(b.min()[i]));
EXPECT_TRUE(std::isfinite(b.max()[i]));
EXPECT_DOUBLE_EQ(b.extent()[i], 0.0);
}
}