sdf_smooth_subtraction: 参数顺序反转(等价 op_smooth_difference(d2,d1,k),与文档 d1-d2 及规范相反)+ k<=0 除零/NaN;改为 op_smooth_difference(d1,d2,k) 一致式并加 k<=0 锐利退化守卫。sdf_smooth_union 同补守卫。 smooth_hc_laplacian: 回推项误用顶点上一轮自身位移(契约应为 avg(neighbor_b)),单尖峰 5 轮由 1.0 放大到 1.99(发散);改为标准 Vollmer HC 两步,修复后 0.68 且不越界放大。 补 fit_nurbs_least_squares(平面点云拟合逐点复算)与 clip_mesh(裁剪保留面独立质心分类复算)判别性测试;复核 collision/sketch/mesh/boolean 清单假阳性并更新审计(未测试 160->140)。
714 lines
29 KiB
C++
714 lines
29 KiB
C++
#include <gtest/gtest.h>
|
||
#include "vde/mesh/reverse_engineering.h"
|
||
#include "vde/mesh/point_cloud.h"
|
||
#include "vde/mesh/halfedge_mesh.h"
|
||
#include "vde/curves/nurbs_surface.h"
|
||
#include "vde/brep/brep_validate.h"
|
||
#include "vde/brep/measure.h"
|
||
|
||
using namespace vde::mesh;
|
||
using namespace vde::curves;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Helper: generate a simple point cloud (e.g., a noisy plane)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
static PointCloud make_plane_cloud(int n = 100) {
|
||
std::vector<Point3D> pts;
|
||
for (int i = 0; i < n; ++i) {
|
||
double u = static_cast<double>(i % 10) / 10.0;
|
||
double v = static_cast<double>(i / 10) / 10.0;
|
||
pts.emplace_back(u, v, 0.01 * std::sin(u * 10) * std::cos(v * 10));
|
||
}
|
||
return PointCloud(pts);
|
||
}
|
||
|
||
static PointCloud make_sphere_cloud(int n = 200) {
|
||
std::vector<Point3D> pts;
|
||
for (int i = 0; i < n; ++i) {
|
||
double theta = 2.0 * M_PI * static_cast<double>(i) / n;
|
||
double phi = M_PI * static_cast<double>((i * 7) % n) / n;
|
||
double x = std::sin(phi) * std::cos(theta);
|
||
double y = std::sin(phi) * std::sin(theta);
|
||
double z = std::cos(phi);
|
||
pts.emplace_back(x, y, z);
|
||
}
|
||
return PointCloud(pts);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// PointCloud 基础测试
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(PointCloudTest, Construction) {
|
||
PointCloud empty;
|
||
EXPECT_EQ(empty.size(), 0u);
|
||
EXPECT_FALSE(empty.has_normals());
|
||
EXPECT_FALSE(empty.has_colors());
|
||
|
||
PointCloud cloud(std::vector<Point3D>{{0,0,0}, {1,0,0}, {0,1,0}});
|
||
EXPECT_EQ(cloud.size(), 3u);
|
||
}
|
||
|
||
TEST(PointCloudTest, Bounds) {
|
||
PointCloud cloud(std::vector<Point3D>{{0,0,0}, {2,0,0}, {0,3,0}, {2,3,0}});
|
||
auto bb = cloud.bounds();
|
||
EXPECT_NEAR(bb.min().x(), 0.0, 1e-9);
|
||
EXPECT_NEAR(bb.min().y(), 0.0, 1e-9);
|
||
EXPECT_NEAR(bb.max().x(), 2.0, 1e-9);
|
||
EXPECT_NEAR(bb.max().y(), 3.0, 1e-9);
|
||
}
|
||
|
||
TEST(PointCloudTest, VoxelDownsample) {
|
||
std::vector<Point3D> pts;
|
||
for (int i = 0; i < 50; ++i) pts.emplace_back(i * 0.01, 0, 0);
|
||
PointCloud cloud(pts);
|
||
auto down = cloud.voxel_downsample(0.1);
|
||
EXPECT_GT(down.size(), 0u);
|
||
EXPECT_LT(down.size(), pts.size());
|
||
}
|
||
|
||
TEST(PointCloudTest, StatisticalOutlierRemoval) {
|
||
std::vector<Point3D> pts;
|
||
// Dense cluster
|
||
for (int i = 0; i < 100; ++i)
|
||
pts.emplace_back(0.01 * i, 0, 0);
|
||
// Outlier far away
|
||
pts.emplace_back(100, 100, 100);
|
||
PointCloud cloud(pts);
|
||
auto clean = cloud.statistical_outlier_removal(6, 1.0);
|
||
EXPECT_LT(clean.size(), pts.size());
|
||
// The outlier should be removed
|
||
for (const auto& p : clean.vertices) {
|
||
EXPECT_GT((p - Point3D(100, 100, 100)).norm(), 50.0);
|
||
}
|
||
}
|
||
|
||
TEST(PointCloudTest, NormalEstimation) {
|
||
auto cloud = make_plane_cloud(100);
|
||
auto with_normals = cloud.normal_estimation(10);
|
||
EXPECT_EQ(with_normals.size(), cloud.size());
|
||
EXPECT_TRUE(with_normals.has_normals());
|
||
// Plane normals should be mostly Z-aligned
|
||
for (size_t i = 0; i < with_normals.size(); ++i) {
|
||
double dot_z = std::abs(with_normals.normals[i].z());
|
||
EXPECT_GT(dot_z, 0.7); // Should be mostly pointing in Z
|
||
}
|
||
}
|
||
|
||
TEST(PointCloudTest, SmoothingFilter) {
|
||
std::vector<Point3D> pts;
|
||
for (int i = 0; i < 50; ++i)
|
||
pts.emplace_back(i * 0.02, 0, 0.1 * std::sin(i * 0.5));
|
||
PointCloud cloud(pts);
|
||
auto smoothed = cloud.smoothing_filter(0.5, 0.3, 3);
|
||
EXPECT_EQ(smoothed.size(), cloud.size());
|
||
}
|
||
|
||
TEST(PointCloudTest, CurvatureAwareSampling) {
|
||
auto cloud = make_sphere_cloud(200);
|
||
auto indices = cloud.curvature_aware_sampling(50);
|
||
EXPECT_LE(indices.size(), 50u);
|
||
EXPECT_GT(indices.size(), 0u);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Plane fitting
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, FitPlane_XY_Plane) {
|
||
std::vector<Point3D> pts;
|
||
for (int i = 0; i < 100; ++i)
|
||
pts.emplace_back(static_cast<double>(i % 10), static_cast<double>(i / 10), 0.0);
|
||
|
||
auto result = fit_plane(pts);
|
||
EXPECT_TRUE(result.valid);
|
||
EXPECT_NEAR(result.rms_error, 0.0, 1e-6);
|
||
EXPECT_NEAR(std::abs(result.normal.z()), 1.0, 1e-6);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, FitPlane_Slanted) {
|
||
std::vector<Point3D> pts;
|
||
// z = x + y
|
||
for (int i = 0; i < 100; ++i) {
|
||
double x = static_cast<double>(i % 10);
|
||
double y = static_cast<double>(i / 10);
|
||
pts.emplace_back(x, y, x + y);
|
||
}
|
||
|
||
auto result = fit_plane(pts);
|
||
EXPECT_TRUE(result.valid);
|
||
EXPECT_NEAR(result.rms_error, 0.0, 1e-6);
|
||
// Normal should be perpendicular to (1,1,-1) → normalize
|
||
double dot = result.normal.x() + result.normal.y() - result.normal.z();
|
||
EXPECT_NEAR(std::abs(dot), std::sqrt(3.0), 1e-6);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, FitPlane_InsufficientPoints) {
|
||
std::vector<Point3D> pts = {{0,0,0}, {1,0,0}};
|
||
auto result = fit_plane(pts);
|
||
EXPECT_FALSE(result.valid);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Patch / NURBS fitting
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, FitPatch_PlaneCloud) {
|
||
auto cloud = make_plane_cloud(100);
|
||
auto result = fit_patch(cloud.vertices, 2, 2, 5, 5);
|
||
EXPECT_TRUE(result.valid);
|
||
EXPECT_GT(result.rms_error, 0.0);
|
||
// Surface should be evaluable
|
||
auto pt = result.surface.evaluate(0.5, 0.5);
|
||
EXPECT_TRUE(std::isfinite(pt.x()));
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, PointCloudToMesh) {
|
||
auto cloud = make_plane_cloud(200);
|
||
PointCloudToMeshParams p;
|
||
p.knn = 8;
|
||
p.poisson.max_depth = 5;
|
||
auto mesh = point_cloud_to_mesh(cloud, p);
|
||
EXPECT_GT(mesh.num_vertices(), 0u);
|
||
EXPECT_GT(mesh.num_faces(), 0u);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, MeshToNurbsSurface) {
|
||
// Build simple mesh from plane cloud
|
||
auto cloud = make_plane_cloud(100);
|
||
PointCloudToMeshParams pm;
|
||
pm.knn = 8;
|
||
pm.poisson.max_depth = 5;
|
||
auto mesh = point_cloud_to_mesh(cloud, pm);
|
||
|
||
ASSERT_GT(mesh.num_vertices(), 0u);
|
||
|
||
MeshToNURBSParams mp;
|
||
mp.nurbs.num_cp_u = 5;
|
||
mp.nurbs.num_cp_v = 5;
|
||
mp.nurbs.degree_u = 2;
|
||
mp.nurbs.degree_v = 2;
|
||
mp.nurbs.max_iterations = 10;
|
||
auto surf = mesh_to_nurbs_surface(mesh, mp);
|
||
// Surface should be valid
|
||
auto pt = surf.evaluate(0.5, 0.5);
|
||
EXPECT_TRUE(std::isfinite(pt.x()));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Curvature-aware sampling (free function)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, CurvatureAwareSampling_Basic) {
|
||
auto cloud = make_sphere_cloud(200);
|
||
auto indices = curvature_aware_sampling(cloud.vertices, 50);
|
||
EXPECT_LE(indices.size(), 50u);
|
||
EXPECT_GT(indices.size(), 0u);
|
||
// Indices should be within range
|
||
for (auto idx : indices) EXPECT_LT(idx, cloud.vertices.size());
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, CurvatureAwareSampling_AllPoints) {
|
||
auto cloud = make_plane_cloud(30);
|
||
auto indices = curvature_aware_sampling(cloud.vertices, 100);
|
||
EXPECT_EQ(indices.size(), cloud.vertices.size());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Hole filling
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, HoleFilling_NoHoles) {
|
||
// Create a closed mesh (tetrahedron-like)
|
||
HalfedgeMesh mesh;
|
||
mesh.build_from_triangles(
|
||
{{0,0,0}, {1,0,0}, {0,1,0}, {0,0,1}},
|
||
{{0,1,2}, {0,1,3}, {1,2,3}, {0,2,3}}
|
||
);
|
||
auto filled = hole_filling(mesh);
|
||
// Should be same size or larger
|
||
EXPECT_GE(filled.num_faces(), mesh.num_faces());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Smoothing filter (free function)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, SmoothingFilter_Basic) {
|
||
auto cloud = make_plane_cloud(100);
|
||
auto smoothed = smoothing_filter(cloud.vertices, 0.5, 3);
|
||
EXPECT_EQ(smoothed.size(), cloud.vertices.size());
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, SmoothingFilter_EmptyPoints) {
|
||
std::vector<Point3D> empty;
|
||
auto smoothed = smoothing_filter(empty, 0.5, 3);
|
||
EXPECT_TRUE(smoothed.empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// Estimate normals (free function)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, EstimateNormals_Plane) {
|
||
// 原数据是两条互相垂直的线段("L" 形),但线段上每一点的 k 近邻全部
|
||
// 共线,局部邻域退化为 1 维 —— 此时"平面法线"本身不可确定,断言
|
||
// "多数法线沿 Z" 对该数据不成立。改用二维平面网格(局部邻域张开二维
|
||
// 片),法线才有确定的垂直于平面解。
|
||
auto cloud = make_plane_cloud(100);
|
||
auto normals = estimate_normals(cloud.vertices, 10);
|
||
EXPECT_EQ(normals.size(), cloud.vertices.size());
|
||
// Most normals should be Z-aligned
|
||
int z_count = 0;
|
||
for (const auto& n : normals) {
|
||
if (std::abs(n.z()) > 0.7) ++z_count;
|
||
}
|
||
EXPECT_GT(z_count, static_cast<int>(cloud.vertices.size() * 0.8));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-1183: quad_remesh — 四边主导重网格化
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
/// 单位立方体(12 三角面,一致外法向)
|
||
HalfedgeMesh make_cube_tri_mesh() {
|
||
HalfedgeMesh m;
|
||
m.build_from_triangles(
|
||
{{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,2,1}, {0,3,2}, // z=0
|
||
{4,5,6}, {4,6,7}, // z=1
|
||
{0,1,5}, {0,5,4}, // y=0
|
||
{2,3,7}, {2,7,6}, // y=1
|
||
{0,4,7}, {0,7,3}, // x=0
|
||
{1,2,6}, {1,6,5}}); // x=1
|
||
return m;
|
||
}
|
||
|
||
/// 平面 4x4 网格(开放网格,三角化)
|
||
HalfedgeMesh make_grid_tri_mesh(int n = 4) {
|
||
std::vector<Point3D> verts;
|
||
for (int j = 0; j <= n; ++j)
|
||
for (int i = 0; i <= n; ++i)
|
||
verts.emplace_back(i * 0.1, j * 0.1, 0.0);
|
||
std::vector<std::array<int, 3>> tris;
|
||
auto vid = [n](int i, int j) { return j * (n + 1) + i; };
|
||
for (int j = 0; j < n; ++j)
|
||
for (int i = 0; i < n; ++i) {
|
||
tris.push_back({vid(i, j), vid(i + 1, j), vid(i + 1, j + 1)});
|
||
tris.push_back({vid(i, j), vid(i + 1, j + 1), vid(i, j + 1)});
|
||
}
|
||
HalfedgeMesh m;
|
||
m.build_from_triangles(verts, tris);
|
||
return m;
|
||
}
|
||
|
||
double mesh_area(const HalfedgeMesh& m) {
|
||
double a = 0.0;
|
||
for (size_t fi = 0; fi < m.num_faces(); ++fi) {
|
||
auto vs = m.face_vertices(static_cast<int>(fi));
|
||
if (vs.size() < 3) continue;
|
||
const Point3D& p0 = m.vertex(vs[0]);
|
||
for (size_t k = 1; k + 1 < vs.size(); ++k) {
|
||
Vector3D cr = (m.vertex(vs[k]) - p0).cross(m.vertex(vs[k + 1]) - p0);
|
||
a += 0.5 * cr.norm();
|
||
}
|
||
}
|
||
return a;
|
||
}
|
||
|
||
size_t count_boundary_edges(const HalfedgeMesh& m) {
|
||
size_t n = 0;
|
||
for (size_t he = 0; he < m.num_edges() * 2; ++he)
|
||
if (m.halfedge(he).face_index >= 0 && m.is_boundary_edge(static_cast<int>(he))) ++n;
|
||
return n;
|
||
}
|
||
|
||
size_t count_quads(const HalfedgeMesh& m) {
|
||
size_t n = 0;
|
||
for (size_t fi = 0; fi < m.num_faces(); ++fi)
|
||
if (m.face_vertices(static_cast<int>(fi)).size() == 4) ++n;
|
||
return n;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(ReverseEngineeringTest, QuadRemesh_CubeMergeCoplanarPairs) {
|
||
auto cube = make_cube_tri_mesh();
|
||
ASSERT_EQ(cube.num_faces(), 12u);
|
||
|
||
QuadRemeshParams p;
|
||
p.sharpness = 0.5; // 允许合并的最大二面角 45° → 立方体棱边不合并
|
||
auto quad = quad_remesh(cube, p);
|
||
|
||
// 每个正方形面由 2 个共面三角形合并成 1 个四边形 → 6 个四边形
|
||
EXPECT_EQ(quad.num_faces(), 6u);
|
||
EXPECT_EQ(count_quads(quad), 6u);
|
||
EXPECT_EQ(quad.num_vertices(), 8u);
|
||
// 闭合性保持:无边界边
|
||
EXPECT_EQ(count_boundary_edges(quad), 0u);
|
||
// 几何不变:总面积守恒
|
||
EXPECT_NEAR(mesh_area(quad), mesh_area(cube), 1e-12);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, QuadRemesh_GridPreservesBoundaryAndArea) {
|
||
auto grid = make_grid_tri_mesh(4);
|
||
const size_t tri_faces = grid.num_faces();
|
||
const size_t tri_boundary = count_boundary_edges(grid);
|
||
ASSERT_EQ(tri_boundary, 16u);
|
||
|
||
QuadRemeshParams p;
|
||
p.target_quads = 16;
|
||
p.sharpness = 0.5;
|
||
auto quad = quad_remesh(grid, p);
|
||
|
||
// 面数下降(三角面被配对成四边形)
|
||
EXPECT_LT(quad.num_faces(), tri_faces);
|
||
EXPECT_GT(count_quads(quad), 0u);
|
||
// 边界边数量与面积保持不变
|
||
EXPECT_EQ(count_boundary_edges(quad), tri_boundary);
|
||
EXPECT_NEAR(mesh_area(quad), mesh_area(grid), 1e-12);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, QuadRemesh_Deterministic) {
|
||
auto grid = make_grid_tri_mesh(3);
|
||
QuadRemeshParams p;
|
||
auto a = quad_remesh(grid, p);
|
||
auto b = quad_remesh(grid, p);
|
||
ASSERT_EQ(a.num_faces(), b.num_faces());
|
||
ASSERT_EQ(a.num_vertices(), b.num_vertices());
|
||
for (size_t fi = 0; fi < a.num_faces(); ++fi)
|
||
EXPECT_EQ(a.face_vertices(static_cast<int>(fi)),
|
||
b.face_vertices(static_cast<int>(fi)));
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, QuadRemesh_EmptyMesh) {
|
||
HalfedgeMesh empty;
|
||
auto out = quad_remesh(empty);
|
||
EXPECT_EQ(out.num_faces(), 0u);
|
||
EXPECT_EQ(out.num_vertices(), 0u);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, QuadRemesh_AdaptiveSizeStillValid) {
|
||
auto grid = make_grid_tri_mesh(4);
|
||
QuadRemeshParams p;
|
||
p.adaptive_size = true;
|
||
p.edge_length = 0.2;
|
||
p.regularity = 0.8;
|
||
auto quad = quad_remesh(grid, p);
|
||
EXPECT_GT(quad.num_faces(), 0u);
|
||
EXPECT_NEAR(mesh_area(quad), mesh_area(grid), 1e-12);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// mesh_to_brep_advanced — 共面区域合并(VDE-357)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringTest, MeshToBrepAdvanced_CubeMergesCoplanarFaces) {
|
||
auto cube = make_cube_tri_mesh();
|
||
ASSERT_EQ(cube.num_faces(), 12u);
|
||
|
||
auto body = mesh_to_brep_advanced(cube);
|
||
|
||
|
||
// 每个正方形面 = 2 个共面三角形合并为 1 张 B-Rep 面 → 6 张面
|
||
EXPECT_EQ(body.num_faces(), 6u);
|
||
EXPECT_EQ(body.num_vertices(), 8u);
|
||
// 共享边去重:立方体 12 条棱
|
||
EXPECT_EQ(body.num_edges(), 12u);
|
||
// 闭合网格 → 水密壳
|
||
auto val = vde::brep::validate(body);
|
||
EXPECT_TRUE(val.valid) << (val.errors.empty() ? std::string() : val.errors.front());
|
||
// 单位立方体体积守恒(面绕向正确,非 0 亦非负值)
|
||
EXPECT_NEAR(vde::brep::volume(body), 1.0, 1e-9);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, MeshToBrepAdvanced_PlanarGridSingleFace) {
|
||
auto grid = make_grid_tri_mesh(4);
|
||
auto body = mesh_to_brep_advanced(grid, BRepFeatureFlag::None);
|
||
// 全共面 → 合并为 1 张面;面内顶点不属于任何面边界,故只保留外边界顶点
|
||
// (4 边 × 4 段 = 16 个)。
|
||
EXPECT_EQ(body.num_faces(), 1u);
|
||
EXPECT_EQ(body.num_vertices(), 16u);
|
||
}
|
||
|
||
TEST(ReverseEngineeringTest, MeshToBrepAdvanced_EmptyMesh) {
|
||
HalfedgeMesh empty;
|
||
auto body = mesh_to_brep_advanced(empty);
|
||
EXPECT_EQ(body.num_faces(), 0u);
|
||
EXPECT_EQ(body.num_vertices(), 0u);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-357: classify_surface / stitch_patches
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
/// A:z=0 平面补片,x∈[0,1], y∈[0,1](双二次)
|
||
NurbsSurface make_flat_patch_a() {
|
||
std::vector<std::vector<Point3D>> g = {
|
||
{{0, 0, 0}, {0.5, 0, 0}, {1, 0, 0}},
|
||
{{0, 0.5, 0}, {0.5, 0.5, 0}, {1, 0.5, 0}},
|
||
{{0, 1, 0}, {0.5, 1, 0}, {1, 1, 0}}};
|
||
return NurbsSurface(g, {0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {}, 2, 2);
|
||
}
|
||
|
||
/// B:x∈[1,2],u=0 边应贴合 A 的 x=1 边;内坡升到 z=2。
|
||
/// boundary_gap 抬高 u=0 边界(模拟未缝合的缝隙)。
|
||
/// 控制网按 [u][v] 排列:u 沿 x 向,v 沿 y 向 ⇒ u=0 边界即 x=1 边。
|
||
NurbsSurface make_ramp_patch_b(double boundary_gap) {
|
||
const double z0 = boundary_gap, z1 = 1.0, z2 = 2.0;
|
||
std::vector<std::vector<Point3D>> g = {
|
||
{{1, 0, z0}, {1, 0.5, z0}, {1, 1, z0}},
|
||
{{1.5, 0, z1}, {1.5, 0.5, z1}, {1.5, 1, z1}},
|
||
{{2, 0, z2}, {2, 0.5, z2}, {2, 1, z2}}};
|
||
return NurbsSurface(g, {0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {}, 2, 2);
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(ReverseEngineeringVDE357Test, ClassifySurface_Plane) {
|
||
auto s = NurbsSurface::plane(Point3D(0, 0, 0), Vector3D(1, 0, 0),
|
||
Vector3D(0, 1, 0), 2.0, 3.0);
|
||
EXPECT_EQ(classify_surface(s), SurfaceType::Plane);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, ClassifySurface_Cylinder) {
|
||
auto s = NurbsSurface::cylinder(0, 0, 0, 1.0, 2.0);
|
||
EXPECT_EQ(classify_surface(s), SurfaceType::Cylinder);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, ClassifySurface_Sphere) {
|
||
auto s = NurbsSurface::sphere(0, 0, 0, 1.0);
|
||
EXPECT_EQ(classify_surface(s), SurfaceType::Sphere);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, ClassifySurface_Cone) {
|
||
auto s = NurbsSurface::cone(0, 0, 0, 1.0, 0.0, 2.0);
|
||
EXPECT_EQ(classify_surface(s), SurfaceType::Cone);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, ClassifySurface_Freeform) {
|
||
std::vector<std::vector<Point3D>> g = {
|
||
{{0, 0, 0}, {1, 0, 0.5}, {2, 0, 0}},
|
||
{{0, 1, 0.5}, {1, 1, 1.0}, {2, 1, 0.5}},
|
||
{{0, 2, 0}, {1, 2, 0.5}, {2, 2, 0}}};
|
||
NurbsSurface s(g, {0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {}, 2, 2);
|
||
EXPECT_EQ(classify_surface(s), SurfaceType::Freeform);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, StitchPatches_G0ClosesGap) {
|
||
auto A = make_flat_patch_a();
|
||
auto B = make_ramp_patch_b(0.1);
|
||
EXPECT_NEAR(B.evaluate(0.0, 0.5).z(), 0.1, 1e-9);
|
||
|
||
auto st = stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G0);
|
||
ASSERT_EQ(st.size(), 2u);
|
||
Point3D after = st[1].evaluate(0.0, 0.5);
|
||
EXPECT_NEAR(after.x(), 1.0, 1e-9);
|
||
EXPECT_NEAR(after.y(), 0.5, 1e-9);
|
||
EXPECT_NEAR(after.z(), 0.0, 1e-9); // 缝隙被缝合到 A 的边界
|
||
// 拓扑保持不变(控制点规模不变)
|
||
EXPECT_EQ(st[1].num_control_points()[0], 3);
|
||
EXPECT_EQ(st[1].num_control_points()[1], 3);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, StitchPatches_G1AlignsTangent) {
|
||
auto A = make_flat_patch_a();
|
||
auto B = make_ramp_patch_b(0.0);
|
||
// 未缝合时 u→0 附近法向明显偏离 A 的 +z(缝处切平面不连续)
|
||
Vector3D n_before = B.normal(0.01, 0.5);
|
||
EXPECT_LT(std::abs(n_before.dot(Vector3D(0, 0, 1))), 0.9);
|
||
|
||
auto st = stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G1);
|
||
// 缝合后 u→0(缝处)切平面应与 A(z=0 平面)一致 ⇒ 法向 ≈ +z
|
||
Vector3D n_after = st[1].normal(0.01, 0.5);
|
||
EXPECT_GT(std::abs(n_after.dot(Vector3D(0, 0, 1))), 0.99);
|
||
}
|
||
|
||
TEST(ReverseEngineeringVDE357Test, StitchPatches_NoPairsUnchanged) {
|
||
auto A = make_flat_patch_a();
|
||
auto B = make_ramp_patch_b(0.1);
|
||
auto st = stitch_patches({A, B}, {}, ContinuityLevel::G1);
|
||
ASSERT_EQ(st.size(), 2u);
|
||
EXPECT_NEAR(st[1].evaluate(0.0, 0.5).z(), 0.1, 1e-12);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-893: coons_interpolate — 双线性 Coons 插值
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
/// 直线段 NURBS(degree 1,域 [0,1])
|
||
NurbsCurve coons_seg(const Point3D& a, const Point3D& b) {
|
||
return NurbsCurve({a, b}, {0, 0, 1, 1}, {1, 1}, 1);
|
||
}
|
||
|
||
/// u=0 边界的抛物线段:(0,0,0) → (0,1,0),中点鼓起 z=0.25
|
||
NurbsCurve coons_parabola_u0() {
|
||
return NurbsCurve({Point3D(0, 0, 0), Point3D(0, 0.5, 0.5), Point3D(0, 1, 0)},
|
||
{0, 0, 0, 1, 1, 1}, {1, 1, 1}, 2);
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// 平面单位正方形直边界 → 真实 Coons 面 = 双线性面 S(u,v) = (u, v, 0)。
|
||
// 逐点独立解析复算,验证曲面精确重建 Coons 面。
|
||
TEST(CoonsInterpolateVDE893Test, BilinearPatchReproducedExactly) {
|
||
const Point3D P00(0, 0, 0), P10(1, 0, 0), P01(0, 1, 0), P11(1, 1, 0);
|
||
NurbsCurve cu0 = coons_seg(P00, P10); // v=0 边界
|
||
NurbsCurve cu1 = coons_seg(P01, P11); // v=1 边界
|
||
NurbsCurve cv0 = coons_seg(P00, P01); // u=0 边界
|
||
NurbsCurve cv1 = coons_seg(P10, P11); // u=1 边界
|
||
|
||
CoonsInterpParams pp; // samples 20, degree 3
|
||
auto s = coons_interpolate(cu0, cu1, cv0, cv1, pp);
|
||
|
||
auto [nu, nv] = s.num_control_points();
|
||
ASSERT_EQ(nu, pp.samples_u + 1);
|
||
ASSERT_EQ(nv, pp.samples_v + 1);
|
||
|
||
for (int i = 0; i <= 8; ++i) {
|
||
for (int j = 0; j <= 8; ++j) {
|
||
const double u = static_cast<double>(i) / 8.0;
|
||
const double v = static_cast<double>(j) / 8.0;
|
||
const Point3D p = s.evaluate(u, v);
|
||
EXPECT_NEAR(p.x(), u, 1e-9) << "u=" << u << " v=" << v;
|
||
EXPECT_NEAR(p.y(), v, 1e-9) << "u=" << u << " v=" << v;
|
||
EXPECT_NEAR(p.z(), 0.0, 1e-9) << "u=" << u << " v=" << v;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 非双线性边界(u=0 为抛物线)→ Coons 面必须在采样参数处精确通过
|
||
// 边界曲线(边界插值性质),这是 Coons 面的定义性契约。
|
||
TEST(CoonsInterpolateVDE893Test, BoundaryCurvesInterpolated) {
|
||
const Point3D P00(0, 0, 0), P10(1, 0, 0), P01(0, 1, 0), P11(1, 1, 0);
|
||
NurbsCurve cu0 = coons_seg(P00, P10); // v=0 直边界
|
||
NurbsCurve cu1 = coons_seg(P01, P11); // v=1 直边界
|
||
NurbsCurve cv0 = coons_parabola_u0(); // u=0 抛物线边界
|
||
NurbsCurve cv1 = coons_seg(P10, P11); // u=1 直边界
|
||
|
||
CoonsInterpParams pp;
|
||
auto s = coons_interpolate(cu0, cu1, cv0, cv1, pp);
|
||
|
||
// u=0 边界:S(0, v̄_j) 必须等于 cv0(v̄_j)
|
||
for (int j = 0; j <= pp.samples_v; ++j) {
|
||
const double v = static_cast<double>(j) / pp.samples_v;
|
||
const Point3D ps = s.evaluate(0.0, v);
|
||
const Point3D pc = cv0.evaluate(v);
|
||
EXPECT_NEAR(ps.x(), pc.x(), 1e-9) << "j=" << j;
|
||
EXPECT_NEAR(ps.y(), pc.y(), 1e-9) << "j=" << j;
|
||
EXPECT_NEAR(ps.z(), pc.z(), 1e-9) << "j=" << j;
|
||
}
|
||
// v=0 边界:S(ū_i, 0) 必须等于 cu0(ū_i)
|
||
for (int i = 0; i <= pp.samples_u; ++i) {
|
||
const double u = static_cast<double>(i) / pp.samples_u;
|
||
const Point3D ps = s.evaluate(u, 0.0);
|
||
const Point3D pc = cu0.evaluate(u);
|
||
EXPECT_NEAR(ps.x(), pc.x(), 1e-9) << "i=" << i;
|
||
EXPECT_NEAR(ps.y(), pc.y(), 1e-9) << "i=" << i;
|
||
EXPECT_NEAR(ps.z(), pc.z(), 1e-9) << "i=" << i;
|
||
}
|
||
|
||
// 抛物线边界的中点鼓起 z=0.25 必须(i)在边界上、(ii)传到内部(非零 z)
|
||
EXPECT_NEAR(cv0.evaluate(0.5).z(), 0.25, 1e-12);
|
||
EXPECT_NEAR(s.evaluate(0.0, 0.5).z(), 0.25, 1e-9);
|
||
EXPECT_GT(s.evaluate(0.5, 0.5).z(), 0.0);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-893: fit_nurbs_least_squares 判别性测试
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(ReverseEngineeringFitVDE893Test, PlanarPointCloudFitsToPlane) {
|
||
// z=0 平面上的规则点阵:最小二乘拟合应回归出同一平面。
|
||
// 3 次 B 样条可精确表示仿射函数 → 采样曲面必须落在 z=0 附近。
|
||
std::vector<Point3D> pts;
|
||
const int n = 10;
|
||
for (int i = 0; i < n; ++i)
|
||
for (int j = 0; j < n; ++j) {
|
||
double x = -1.0 + 2.0 * i / (n - 1);
|
||
double y = -1.0 + 2.0 * j / (n - 1);
|
||
pts.emplace_back(x, y, 0.0);
|
||
}
|
||
|
||
NURBSLSFitParams params;
|
||
params.degree_u = 3;
|
||
params.degree_v = 3;
|
||
params.num_cp_u = 6;
|
||
params.num_cp_v = 6;
|
||
params.smoothness = 1e-8; // 极弱正则,逼近插值
|
||
params.tolerance = 1e-10;
|
||
params.max_iterations = 200;
|
||
|
||
NurbsSurface surf = fit_nurbs_least_squares(pts, params);
|
||
|
||
// 控制点网格维度必须等于请求的 (6,6)(排除空壳/落回默认曲面)
|
||
auto ncp = surf.num_control_points();
|
||
EXPECT_EQ(ncp[0], 6);
|
||
EXPECT_EQ(ncp[1], 6);
|
||
|
||
// 逐点独立复算:所有采样点必须落在 z=0 平面内
|
||
double minx = 1e9, maxx = -1e9, miny = 1e9, maxy = -1e9, maxabsz = 0.0;
|
||
for (int a = 0; a <= 10; ++a)
|
||
for (int b = 0; b <= 10; ++b) {
|
||
double u = static_cast<double>(a) / 10.0;
|
||
double v = static_cast<double>(b) / 10.0;
|
||
Point3D p = surf.evaluate(u, v);
|
||
maxabsz = std::max(maxabsz, std::abs(p.z()));
|
||
minx = std::min(minx, p.x()); maxx = std::max(maxx, p.x());
|
||
miny = std::min(miny, p.y()); maxy = std::max(maxy, p.y());
|
||
}
|
||
EXPECT_LT(maxabsz, 1e-6) << "拟合平面应保持 z≈0";
|
||
|
||
// 曲面应覆盖数据范围(非退化收缩成一点)
|
||
EXPECT_GT(maxx - minx, 1.5);
|
||
EXPECT_GT(maxy - miny, 1.5);
|
||
}
|
||
|
||
TEST(ReverseEngineeringFitVDE893Test, TiltedPlaneFitsWithinPlaneEquation) {
|
||
// 倾斜平面 z = 0.5x + 0.25y:拟合曲面的采样点必须满足该平面方程
|
||
std::vector<Point3D> pts;
|
||
const int n = 9;
|
||
for (int i = 0; i < n; ++i)
|
||
for (int j = 0; j < n; ++j) {
|
||
double x = -1.0 + 2.0 * i / (n - 1);
|
||
double y = -1.0 + 2.0 * j / (n - 1);
|
||
pts.emplace_back(x, y, 0.5 * x + 0.25 * y);
|
||
}
|
||
|
||
NURBSLSFitParams params;
|
||
params.num_cp_u = params.num_cp_v = 6;
|
||
params.smoothness = 1e-8;
|
||
params.tolerance = 1e-10;
|
||
params.max_iterations = 200;
|
||
|
||
NurbsSurface surf = fit_nurbs_least_squares(pts, params);
|
||
|
||
double maxres = 0.0;
|
||
for (int a = 0; a <= 10; ++a)
|
||
for (int b = 0; b <= 10; ++b) {
|
||
Point3D p = surf.evaluate(a / 10.0, b / 10.0);
|
||
maxres = std::max(maxres, std::abs(p.z() - (0.5 * p.x() + 0.25 * p.y())));
|
||
}
|
||
EXPECT_LT(maxres, 1e-6) << "拟合曲面应满足平面方程 z = 0.5x + 0.25y";
|
||
}
|
||
|
||
TEST(ReverseEngineeringFitVDE893Test, TooFewPointsReturnsEmptySurface) {
|
||
std::vector<Point3D> pts = {
|
||
Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0)};
|
||
NurbsSurface surf = fit_nurbs_least_squares(pts);
|
||
auto ncp = surf.num_control_points();
|
||
EXPECT_EQ(ncp[0], 0);
|
||
EXPECT_EQ(ncp[1], 0);
|
||
EXPECT_EQ(surf.degree_u(), 0);
|
||
}
|
||
|
||
|