/** * @file test_surface_editing.cpp * @brief 曲面编辑三件套测试:match_surface / shape_fillet / control_point_edit */ #include #include "vde/curves/surface_editing.h" #include "vde/curves/control_point_edit.h" #include "vde/curves/nurbs_surface.h" #include "vde/core/point.h" #include #include #include #include namespace vde::curves { namespace test { using core::Point3D; using core::Vector3D; // ── Helper: create a simple biquadratic plane surface ── NurbsSurface make_test_plane() { // 3×3 control grid on z=0 plane std::vector> grid = { {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}}, {{0, 1, 0}, {1, 1, 0}, {2, 1, 0}}, {{0, 2, 0}, {1, 2, 0}, {2, 2, 0}} }; return NurbsSurface(grid, {0, 0, 0, 1, 1, 1}, // u-knots, degree=2 {0, 0, 0, 1, 1, 1}, // v-knots, degree=2 {}, 2, 2); // weights (empty = all 1.0) } // ── Helper: create a slightly curved surface (degree 2×2) ── NurbsSurface make_test_curved() { // 3×3 grid, slightly curved in z std::vector> grid = { {{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}} }; return NurbsSurface(grid, {0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}, {}, 2, 2); } // ═══════════════════════════════════════════════════════════ // match_surface 测试 // ═══════════════════════════════════════════════════════════ TEST(SurfaceEditingTest, MatchSurfaceG0Planar) { // Two identical planes on z=0 — matching should produce no error auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G0; opts.boundary_samples = 10; auto result = match_surface(plane_b, plane_a, BoundaryEdge::VMin, opts); EXPECT_TRUE(result.g0_ok); EXPECT_LT(result.max_position_error, 1e-4); EXPECT_EQ(result.rows_adjusted, 1); EXPECT_EQ(result.control_displacements.size(), 9u); // 3×3 grid } TEST(SurfaceEditingTest, MatchSurfaceG0DifferentSurfaces) { // Two slightly different curved surfaces — match VMin edge auto surf_a = make_test_curved(); // Modify a few control points to make it different auto surf_b = make_test_curved(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G0; opts.boundary_samples = 10; auto result = match_surface(surf_b, surf_a, BoundaryEdge::VMin, opts); // Should produce a valid surface auto [nu, nv] = result.matched_surface.num_control_points(); EXPECT_GE(nu, 1); EXPECT_GE(nv, 1); // Control points should be adjusted EXPECT_GT(result.control_displacements.size(), 0u); } TEST(SurfaceEditingTest, MatchSurfaceG1Planar) { auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G1; opts.boundary_samples = 10; auto result = match_surface(plane_b, plane_a, BoundaryEdge::VMin, opts); EXPECT_TRUE(result.g0_ok); EXPECT_TRUE(result.g1_ok); EXPECT_EQ(result.rows_adjusted, 1); } TEST(SurfaceEditingTest, MatchSurfaceG1DifferentSurfaces) { auto surf_a = make_test_curved(); auto surf_b = make_test_curved(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G1; opts.boundary_samples = 10; auto result = match_surface(surf_b, surf_a, BoundaryEdge::VMin, opts); // Check that errors are computed EXPECT_GE(result.max_position_error, 0.0); EXPECT_GE(result.max_tangent_error, 0.0); EXPECT_EQ(result.rows_adjusted, 1); } TEST(SurfaceEditingTest, MatchSurfaceG2Planar) { auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G2; opts.boundary_samples = 10; auto result = match_surface(plane_b, plane_a, BoundaryEdge::VMin, opts); EXPECT_TRUE(result.g0_ok); EXPECT_EQ(result.rows_adjusted, 2); } TEST(SurfaceEditingTest, MatchSurfaceG3Planar) { auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G3; opts.boundary_samples = 10; auto result = match_surface(plane_b, plane_a, BoundaryEdge::VMin, opts); EXPECT_TRUE(result.g0_ok); EXPECT_EQ(result.rows_adjusted, 3); // G3 adjusts 3 rows } TEST(SurfaceEditingTest, MatchSurfaceAllEdges) { auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G1; // Test all 4 boundary edges for (int e = 0; e < 4; ++e) { BoundaryEdge edge = static_cast(e); auto result = match_surface(plane_b, plane_a, edge, opts); EXPECT_TRUE(result.g0_ok) << "Failed on edge " << e; } } TEST(SurfaceEditingTest, MatchSurfaceExplicitRows) { auto plane_a = make_test_plane(); auto plane_b = make_test_plane(); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G2; opts.rows_to_adjust = 1; // override, only 1 row opts.boundary_samples = 10; auto result = match_surface(plane_b, plane_a, BoundaryEdge::VMin, opts); EXPECT_EQ(result.rows_adjusted, 1); } // ═══════════════════════════════════════════════════════════ // control_point_edit 测试 // ═══════════════════════════════════════════════════════════ TEST(SurfaceEditingTest, ControlPointEditSingle) { auto surf = make_test_plane(); std::vector> indices = {{1, 1}}; // center control point std::vector new_pos = {{1, 1, 5}}; // lift up in z auto result = control_point_edit(surf, indices, new_pos); EXPECT_EQ(result.points_modified, 1); EXPECT_GT(result.max_displacement, 4.0); // moved ~5 units in z EXPECT_TRUE(result.weights_preserved); // Verify the control point was moved auto modified_cp = result.modified_surface.control_points(); EXPECT_NEAR(modified_cp[1][1].z(), 5.0, 1e-6); // Other control points unchanged EXPECT_NEAR(modified_cp[0][0].z(), 0.0, 1e-6); } TEST(SurfaceEditingTest, ControlPointEditMultiple) { auto surf = make_test_plane(); std::vector> indices = {{0, 0}, {2, 2}}; std::vector new_pos = {{0, 0, 3}, {3, 3, 3}}; auto result = control_point_edit(surf, indices, new_pos); EXPECT_EQ(result.points_modified, 2); EXPECT_GT(result.max_displacement, 2.0); auto cp = result.modified_surface.control_points(); EXPECT_NEAR(cp[0][0].z(), 3.0, 1e-6); EXPECT_NEAR(cp[2][2].z(), 3.0, 1e-6); // Center unchanged EXPECT_NEAR(cp[1][1].z(), 0.0, 1e-6); } TEST(SurfaceEditingTest, ControlPointEditEmpty) { auto surf = make_test_plane(); std::vector> indices; std::vector new_pos; auto result = control_point_edit(surf, indices, new_pos); EXPECT_EQ(result.points_modified, 0); EXPECT_NEAR(result.max_displacement, 0.0, 1e-12); // Should return a valid copy of original auto cp = result.modified_surface.control_points(); EXPECT_EQ(cp.size(), 3u); EXPECT_NEAR(cp[0][0].z(), 0.0, 1e-6); } TEST(SurfaceEditingTest, ControlPointEditPreservesKnots) { auto surf = make_test_plane(); std::vector> indices = {{0, 0}}; std::vector new_pos = {{-1, -1, 0}}; auto result = control_point_edit(surf, indices, new_pos); // Knot vectors preserved const auto& ku = result.modified_surface.knots_u(); const auto& kv = result.modified_surface.knots_v(); EXPECT_EQ(ku.size(), 6u); EXPECT_EQ(kv.size(), 6u); EXPECT_EQ(result.modified_surface.degree_u(), 2); EXPECT_EQ(result.modified_surface.degree_v(), 2); } // ═══════════════════════════════════════════════════════════ // VDE-344: control_point_smooth // ═══════════════════════════════════════════════════════════ TEST(SurfaceEditingVDE344Test, ControlPointSmooth_RelaxesCurvedInterior) { auto curved = make_test_curved(); // 中心 z=1.0,4 邻域 z=0.5 // 全强度 → 中心 z 降到邻域平均 0.5 auto smoothed = control_point_smooth(curved, {}, 2, 1.0); EXPECT_NEAR(smoothed.control_points()[1][1].z(), 0.5, 1e-9); // 拓扑/节点/次数保持 EXPECT_EQ(smoothed.degree_u(), curved.degree_u()); EXPECT_EQ(smoothed.degree_v(), curved.degree_v()); EXPECT_EQ(smoothed.knots_u().size(), curved.knots_u().size()); EXPECT_EQ(smoothed.control_points().size(), curved.control_points().size()); // 半强度 → 中心 z = 0.75 auto half = control_point_smooth(curved, {}, 2, 0.5); EXPECT_NEAR(half.control_points()[1][1].z(), 0.75, 1e-9); // 固定中心点 → 中心不变 auto pinned = control_point_smooth(curved, {{1, 1}}, 2, 1.0); EXPECT_NEAR(pinned.control_points()[1][1].z(), 1.0, 1e-9); // strength = 0 → 原样返回 auto none = control_point_smooth(curved, {}, 2, 0.0); EXPECT_NEAR(none.control_points()[1][1].z(), 1.0, 1e-9); // 平面网格本身已是邻域平均 → 平滑后不变 auto flat = make_test_plane(); auto flat_s = control_point_smooth(flat, {}, 2, 1.0); EXPECT_NEAR(flat_s.control_points()[1][1].z(), 0.0, 1e-12); } // ═══════════════════════════════════════════════════════════ // VDE-389: edit_weights / edit_soft / edit_linked_surfaces // ═══════════════════════════════════════════════════════════ TEST(SurfaceEditingVDE389Test, EditWeights_Multiple) { auto surf = make_test_plane(); // 权重为空 → 全 1.0 auto res = edit_weights(surf, {{1, 1}, {0, 0}}, {2.5, 0.5}); EXPECT_EQ(res.points_modified, 2); EXPECT_TRUE(res.weights_preserved); const auto& w = res.modified_surface.weights(); ASSERT_FALSE(w.empty()); EXPECT_NEAR(w[1][1], 2.5, 1e-12); EXPECT_NEAR(w[0][0], 0.5, 1e-12); EXPECT_NEAR(w[2][2], 1.0, 1e-12); // 未指定者保持 // 控制点位置不变 EXPECT_NEAR(res.max_displacement, 0.0, 1e-12); EXPECT_NEAR(res.modified_surface.control_points()[1][1].z(), 0.0, 1e-12); } TEST(SurfaceEditingVDE389Test, EditWeights_ClampsNonPositive) { auto surf = make_test_plane(); auto res = edit_weights(surf, {{1, 1}}, {-3.0}); // 非正 → 钳到 1e-12 EXPECT_FALSE(res.weights_preserved); EXPECT_NEAR(res.modified_surface.weights()[1][1], 1e-12, 1e-18); } TEST(SurfaceEditingVDE389Test, EditSoft_PowerFalloff) { auto surf = make_test_plane(); // 中心 (1,1) SoftSelection sel; sel.center = {1, 1}; sel.radius = 1.5; sel.exponent = 2.0; sel.linear = false; auto res = edit_soft(surf, sel, Vector3D(0, 0, 1.0)); const auto& cp = res.modified_surface.control_points(); // 中心位移 = delta EXPECT_NEAR(cp[1][1].z(), 1.0, 1e-9); // d=1 处 w = 1 − (1/1.5)² EXPECT_NEAR(cp[1][0].z(), 1.0 - std::pow(1.0 / 1.5, 2.0), 1e-9); // d=√2 处 w = 1 − (√2/1.5)² EXPECT_NEAR(cp[0][0].z(), 1.0 - std::pow(std::sqrt(2.0) / 1.5, 2.0), 1e-9); // 衰减单调:中心 > d=1 > d=√2 EXPECT_GT(cp[1][1].z(), cp[1][0].z()); EXPECT_GT(cp[1][0].z(), cp[0][0].z()); } TEST(SurfaceEditingVDE389Test, EditSoft_LinearFalloff) { auto surf = make_test_plane(); SoftSelection sel; sel.center = {1, 1}; sel.radius = 2.0; sel.linear = true; auto res = edit_soft(surf, sel, Vector3D(0, 0, 2.0)); const auto& cp = res.modified_surface.control_points(); EXPECT_NEAR(cp[1][1].z(), 2.0, 1e-9); // d=0 → w=1 EXPECT_NEAR(cp[1][0].z(), 2.0 * 0.5, 1e-9); // d=1 → w=0.5 } TEST(SurfaceEditingVDE389Test, EditLinkedSurfaces_GroupsByIndex) { auto s1 = make_test_plane(); auto s2 = make_test_plane(); std::vector edits = { {0, 1, 1, Vector3D(0, 0, 2.0)}, {1, 0, 0, Vector3D(1, 0, 0)}, {5, 1, 1, Vector3D(0, 0, 9.0)}, // surface_index 越界 → 忽略 }; auto res = edit_linked_surfaces({s1, s2}, edits); ASSERT_EQ(res.size(), 2u); const auto& c1 = res[0].modified_surface.control_points(); const auto& c2 = res[1].modified_surface.control_points(); EXPECT_NEAR(c1[1][1].z(), 2.0, 1e-9); EXPECT_NEAR(c1[0][0].z(), 0.0, 1e-9); // s1 其他点不变 EXPECT_NEAR(c2[0][0].x(), 1.0, 1e-9); // s2 原点位移 +x EXPECT_NEAR(c2[1][1].z(), 0.0, 1e-9); // s2 其他点不变 } // ═══════════════════════════════════════════════════════════ // Phase4-RE task-90:G3 匹配鲁棒性(不同 degree / 非均匀节点) // ═══════════════════════════════════════════════════════════ namespace { /// Greville 横标(B-spline 曲线/曲面参数化) std::vector re_greville(const std::vector& k, int p) { const int ncp = static_cast(k.size()) - p - 1; std::vector g(static_cast(ncp)); for (int i = 0; i < ncp; ++i) { double s = 0.0; for (int j = 1; j <= p; ++j) s += k[static_cast(i + j)]; g[static_cast(i)] = p > 0 ? s / p : 0.0; } return g; } } // namespace // 10 组随机(degree 2/3、非均匀节点、共享线性边界)G3 成功率 ≥90%, // 成功组:位置/曲率/曲率变化率误差 ≤1e-6(归一化)、内部 deviation ≤1e-6。 TEST(SurfaceEditingRE90Test, MatchSurfaceG3_RandomNonUniformKnots_Success10of10) { std::mt19937 rng(9001); std::uniform_int_distribution deg_dist(2, 3); std::uniform_real_distribution mid_dist(0.15, 0.85); int ok = 0; std::string first_diag; for (int g = 0; g < 10; ++g) { const int du = deg_dist(rng); const int dv = deg_dist(rng); auto make_knots = [&](int deg) { std::vector k(static_cast(deg + 1), 0.0); k.push_back(mid_dist(rng)); // 单个内部节点(非均匀) for (int i = 0; i < deg + 1; ++i) k.push_back(1.0); return k; }; const auto ku = make_knots(du); const auto kv = make_knots(dv); const int nu = static_cast(ku.size()) - du - 1; const int nv = static_cast(kv.size()) - dv - 1; const auto gv = re_greville(kv, dv); // 共享边界:线性曲线 P(v)=P0+v*D,在两套表示中均精确 const Point3D P0(0, 0, 0.1 * g); const Vector3D D(0, 1, 0.2); std::vector> ra(static_cast(nu), std::vector(static_cast(nv))); std::vector> rb(static_cast(nu), std::vector(static_cast(nv))); for (int i = 0; i < nu; ++i) for (int j = 0; j < nv; ++j) { const double u = static_cast(i) / (nu - 1); const double v = static_cast(j) / (nv - 1); ra[static_cast(i)][static_cast(j)] = Point3D(u, v, 0.2 * std::sin(2 * u) * std::cos(v) + 0.05 * g * u * u); rb[static_cast(i)][static_cast(j)] = Point3D(u, v, -0.15 * u * u + 0.1 * v * v + 0.03 * g * u * v); } for (int j = 0; j < nv; ++j) { const Point3D b = P0 + D * gv[static_cast(j)]; ra[0][static_cast(j)] = b; rb[0][static_cast(j)] = b; } auto A = NurbsSurface(ra, ku, kv, {}, du, dv); auto B = NurbsSurface(rb, ku, kv, {}, du, dv); MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G3; opts.boundary_samples = 16; opts.tolerance = 1e-6; auto r = match_surface(B, A, BoundaryEdge::UMin, opts); double dev = 0.0; if (r.success) { for (int i = 0; i <= 8; ++i) { const double u = 0.15 + 0.85 * i / 8.0; for (int j = 0; j <= 8; ++j) { const double v = j / 8.0; dev = std::max(dev, (r.matched_surface.evaluate(u, v) - B.evaluate(u, v)).norm()); } } } const bool good = r.success && r.g3_ok && r.max_position_error <= 1e-6 && r.max_curvature_error <= 1e-6 && r.max_derivative_error <= 1e-6 && dev <= 1e-6; if (good) ++ok; else if (first_diag.empty()) first_diag = "case " + std::to_string(g); std::printf("[G3 rand %d] du=%d dv=%d good=%d errs=(%.2e,%.2e,%.2e) dev=%.2e\n", g, du, dv, (int)good, r.max_position_error, r.max_curvature_error, r.max_derivative_error, dev); } EXPECT_GE(ok, 9) << "随机 10 组 G3 成功率 <90%: " << first_diag; } // 边界几何不兼容时给出明确诊断(不伪造成功) TEST(SurfaceEditingRE90Test, MatchSurfaceG3_IncompatibleBoundary_Diagnosed) { std::vector k = {0, 0, 0, 0, 1, 1, 1, 1}; std::vector> ra(4, std::vector(4)); std::vector> rb(4, std::vector(4)); for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) { const double u = i / 3.0, v = j / 3.0; ra[static_cast(i)][static_cast(j)] = Point3D(u, v, 0.2 * u); rb[static_cast(i)][static_cast(j)] = Point3D(u, v + 0.5, -0.2 * u); // 边界在 v 向错位 0.5 } auto A = NurbsSurface(ra, k, k, {}, 3, 3); NurbsSurface degenerate; // 空/退化曲面:不可匹配 MatchSurfaceOptions opts; opts.continuity = ContinuityLevel::G3; std::string diag; auto r = match_surface_between(degenerate, A, BoundaryEdge::UMin, BoundaryEdge::UMin, false, opts, &diag); EXPECT_FALSE(r.success); EXPECT_FALSE(diag.empty()); } } // namespace test } // namespace vde::curves