#include #include "vde/mesh/reverse_engineering.h" #include "vde/mesh/point_cloud.h" #include "vde/mesh/halfedge_mesh.h" #include "vde/curves/nurbs_surface.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 pts; for (int i = 0; i < n; ++i) { double u = static_cast(i % 10) / 10.0; double v = static_cast(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 pts; for (int i = 0; i < n; ++i) { double theta = 2.0 * M_PI * static_cast(i) / n; double phi = M_PI * static_cast((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{{0,0,0}, {1,0,0}, {0,1,0}}); EXPECT_EQ(cloud.size(), 3u); } TEST(PointCloudTest, Bounds) { PointCloud cloud(std::vector{{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 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 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 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 pts; for (int i = 0; i < 100; ++i) pts.emplace_back(static_cast(i % 10), static_cast(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 pts; // z = x + y for (int i = 0; i < 100; ++i) { double x = static_cast(i % 10); double y = static_cast(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 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 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(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 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> 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(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(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(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(fi)), b.face_vertices(static_cast(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); }