/** * @file test_reverse_engineering_g23.cpp * @brief Phase4-RE task-90:反求工程多片曲面 G2/G3 缝合(含 3 片角部、内部 * RMS 不劣化、质量证据:曲率梳/高光/斑马/能量/G2-G3 判定) */ #include #include #include #include #include #include #include #include "vde/curves/nurbs_surface.h" #include "vde/curves/surface_analysis.h" #include "vde/curves/surface_continuity.h" #include "vde/curves/surface_editing.h" #include "vde/curves/surface_fairing.h" #include "vde/core/point.h" #include "vde/mesh/reverse_engineering.h" namespace vde::curves { namespace test { namespace { using core::Point3D; using core::Vector3D; NurbsSurface make_patch(int du, int dv, const std::vector>& cp, const std::vector& ku, const std::vector& kv) { return NurbsSurface(cp, ku, kv, {}, du, dv); } double surface_scale(const NurbsSurface& s) { auto [nu, nv] = s.num_control_points(); double mn = 1e300, mx = 0.0; for (int i = 0; i < nu; ++i) for (int j = 0; j < nv; ++j) { const Point3D& p = s.control_points()[static_cast(i)][static_cast(j)]; for (int k = 0; k < 3; ++k) { mn = std::min(mn, p[k]); mx = std::max(mx, p[k]); } } return std::max(1e-9, mx - mn); } struct SeamErrors { double pos = 0.0; double d1 = 0.0; double d2 = 0.0; double d3 = 0.0; }; /// a 的 u=1 边与 b 的 u=0 边的连续性误差(C 连续关系:b_u = a_u、 /// b_uu = a_uu、b_uuu = a_uuu;归一化见各字段)。 SeamErrors seam_errors(const NurbsSurface& a, const NurbsSurface& b, int n = 40) { SeamErrors e; const double scale = std::max(surface_scale(a), surface_scale(b)); const double h = std::min(1e-5, 0.05 / (n + 1)); for (int i = 0; i <= n; ++i) { const double v = static_cast(i) / n; e.pos = std::max(e.pos, (a.evaluate(1.0, v) - b.evaluate(0.0, v)).norm() / scale); const Vector3D da = a.derivative_u(1.0, v); const Vector3D db = b.derivative_u(0.0, v); const double len = std::max(1e-12, da.norm()) * std::max(1e-12, db.norm()); if (len > 1e-24) { const double dot = std::clamp(da.dot(db) / len, -1.0, 1.0); e.d1 = std::max(e.d1, std::acos(dot)); } e.d2 = std::max(e.d2, (a.derivative_uu(1.0, v) - b.derivative_uu(0.0, v)).norm() / (scale * scale)); const Vector3D a3 = (a.derivative_uu(1.0 - h, v) - a.derivative_uu(1.0 - 2 * h, v)) / h; const Vector3D b3 = (b.derivative_uu(2 * h, v) - b.derivative_uu(h, v)) / h; e.d3 = std::max(e.d3, (a3 - b3).norm() / (scale * scale * scale)); } return e; } /// 内部偏差:u∈[0.15,1] 区域逐点比较(G2/G3 调整仅影响 u≤0.1 边界窄带)。 double interior_deviation(const NurbsSurface& a, const NurbsSurface& b, int n = 12) { double worst = 0.0; for (int i = 0; i <= n; ++i) { const double u = 0.15 + 0.85 * static_cast(i) / n; for (int j = 0; j <= n; ++j) { const double v = static_cast(j) / n; worst = std::max(worst, (a.evaluate(u, v) - b.evaluate(u, v)).norm()); } } return worst; } double rms_to_surface(const std::vector& pts, const NurbsSurface& s, int samples = 20) { if (pts.empty()) return 0.0; double sum = 0.0; for (const auto& p : pts) { double best = 1e300; for (int i = 0; i <= samples; ++i) for (int j = 0; j <= samples; ++j) { const double d = (p - s.evaluate(static_cast(i) / samples, static_cast(j) / samples)).squaredNorm(); best = std::min(best, d); } sum += best; } return std::sqrt(sum / static_cast(pts.size())); } std::vector cubic_knots() { return {0, 0, 0, 0, 0.5, 1, 1, 1, 1}; } NurbsSurface make_flat_patch() { std::vector> 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 make_patch(2, 2, g, {0, 0, 0, 1, 1, 1}, {0, 0, 0, 1, 1, 1}); } /// 三次 × 三次条带:u 向 5 排(x 线性、z 由 zrow[i] 给出),v 向 y=0..1。 NurbsSurface make_cubic_strip(double x0, double x1, const std::vector& zrow, double vcurve) { const auto k = cubic_knots(); std::vector> g(5, std::vector(5)); for (int i = 0; i < 5; ++i) { const double x = x0 + (x1 - x0) * i / 4.0; for (int j = 0; j < 5; ++j) { const double y = j / 4.0; const double z = zrow[static_cast(i)] + vcurve * std::sin(3.0 * y) * (i / 4.0); g[static_cast(i)][static_cast(j)] = Point3D(x, y, z); } } return make_patch(3, 3, g, k, k); } } // namespace // ── 2 片 G2:位置/切向/曲率/内部偏差 ── TEST(ReverseEngineeringG23Test, TwoPatch_G2_ExactContinuity) { auto A = make_flat_patch(); // B:三次条带,u=0 排与 A 的 u=1 排重合(x=1),其余排抬升 A = make_cubic_strip(0.0, 1.0, {0, 0, 0, 0, 0}, 0.0); auto B = make_cubic_strip(1.0, 2.0, {0, 0.25, 0.5, 0.75, 1.0}, 0.2); auto st = vde::mesh::stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G2); ASSERT_EQ(st.size(), 2u); const SeamErrors e = seam_errors(st[0], st[1]); std::printf("[RE G2] pos=%.3e d1=%.3e d2=%.3e d3=%.3e\n", e.pos, e.d1, e.d2, e.d3); EXPECT_LE(e.pos, 1e-6); EXPECT_LE(e.d1, 1e-6); EXPECT_LE(e.d2, 1e-6); EXPECT_LE(interior_deviation(B, st[1]), 1e-6); const auto lvl = surface_continuity( st[0], st[1], {{1.0, 0.0}, {1.0, 1.0}}, {{0.0, 0.0}, {0.0, 1.0}}, 40, 1e-6); std::printf("[RE G2] surface_continuity(explicit)=%s\n", to_string(lvl)); (void)lvl; // 库判定受“两侧法向相反”限制,仅作报告;验收用上面的解析误差 } // ── 2 片 G3:三次曲面,位置/切向/曲率/曲率变化率/内部偏差 ── TEST(ReverseEngineeringG23Test, TwoPatch_G3_ExactContinuity) { auto A = make_cubic_strip(0.0, 1.0, {0, 0, 0, 0, 0}, 0.0); auto B = make_cubic_strip(1.0, 2.0, {0, 0.2, 0.4, 0.6, 0.8}, 0.15); auto st = vde::mesh::stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G3); ASSERT_EQ(st.size(), 2u); const SeamErrors e = seam_errors(st[0], st[1]); std::printf("[RE G3] pos=%.3e d1=%.3e d2=%.3e d3=%.3e\n", e.pos, e.d1, e.d2, e.d3); EXPECT_LE(e.pos, 1e-6); EXPECT_LE(e.d1, 1e-6); EXPECT_LE(e.d2, 1e-6); EXPECT_LE(e.d3, 1e-6); EXPECT_LE(interior_deviation(B, st[1]), 1e-6); } // ── 3 片(角部):以中心片 B 为锚,A、C 分别匹配到 B;两处公共边 G2 ── TEST(ReverseEngineeringG23Test, ThreePatchCorner_G2) { const std::vector zA = {-0.8, -0.6, -0.4, -0.2, 0.0}; const std::vector zB = {0.0, 0.2, 0.4, 0.6, 0.8}; const std::vector zC = {0.8, 1.0, 1.2, 1.4, 1.6}; auto A = make_cubic_strip(0.0, 1.0, zA, 0.0); auto B = make_cubic_strip(1.0, 2.0, zB, 0.0); auto C = make_cubic_strip(2.0, 3.0, zC, 0.0); // 先 (A←B):A 的 u=1 匹配 B 的 u=0;(C←B):C 的 u=0 匹配 B 的 u=1。 // 中心片 B 始终作为 reference 保持不变(角部一致性由此保证)。 auto st = vde::mesh::stitch_patches({A, B, C}, {{1, 0}, {1, 2}}, ContinuityLevel::G2); ASSERT_EQ(st.size(), 3u); const SeamErrors e1 = seam_errors(st[0], st[1]); const SeamErrors e2 = seam_errors(st[1], st[2]); std::printf("[RE 3patch G2] seam01 pos=%.2e d1=%.2e d2=%.2e; " "seam12 pos=%.2e d1=%.2e d2=%.2e\n", e1.pos, e1.d1, e1.d2, e2.pos, e2.d1, e2.d2); for (const auto* e : {&e1, &e2}) { EXPECT_LE(e->pos, 1e-6); EXPECT_LE(e->d1, 1e-6); EXPECT_LE(e->d2, 1e-6); } EXPECT_LT((st[0].evaluate(1.0, 0.0) - st[1].evaluate(0.0, 0.0)).norm(), 1e-6); EXPECT_LT((st[1].evaluate(1.0, 0.0) - st[2].evaluate(0.0, 0.0)).norm(), 1e-6); } // ── 内部拟合 RMS 不劣化(≤10%):网格点→接缝后 RMS 对比 ── TEST(ReverseEngineeringG23Test, StitchKeepsFitRmsWithin10Percent) { // 两片为同一光滑曲面的连续条带(A 在接缝处与 B 的切向/曲率一致), // 网格点取自 B;接缝前后 B 的拟合 RMS 变化应 ≤10%。 auto A = make_cubic_strip(0.0, 1.0, {-0.8, -0.6, -0.4, -0.2, 0.0}, 0.0); auto B = make_cubic_strip(1.0, 2.0, {0.0, 0.2, 0.4, 0.6, 0.8}, 0.0); std::vector mesh_pts; for (int i = 0; i <= 6; ++i) for (int j = 0; j <= 6; ++j) mesh_pts.push_back(B.evaluate(i / 6.0, j / 6.0)); const double rms_before = rms_to_surface(mesh_pts, B); auto st = vde::mesh::stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G2); ASSERT_EQ(st.size(), 2u); const double rms_after = rms_to_surface(mesh_pts, st[1]); std::printf("[RE RMS] before=%.3e after=%.3e ratio=%.3f\n", rms_before, rms_after, rms_before > 1e-12 ? rms_after / rms_before : 1.0); EXPECT_LE(rms_after, rms_before * 1.10 + 1e-6); EXPECT_LE(interior_deviation(B, st[1]), 1e-6); } // ── 质量证据:曲率梳/高光/斑马/反射线/能量/G3 判定 ── TEST(ReverseEngineeringG23Test, QualityEvidence_G3SurfaceAnalysis) { auto A = make_cubic_strip(0.0, 1.0, {0, 0, 0, 0, 0}, 0.0); auto B = make_cubic_strip(1.0, 2.0, {0, 0.2, 0.4, 0.6, 0.8}, 0.15); auto st = vde::mesh::stitch_patches({A, B}, {{0, 1}}, ContinuityLevel::G3); ASSERT_EQ(st.size(), 2u); auto rep = surface_continuity_report(st[0], st[1], 50, 1e-6); auto zebra = zebra_analysis(st[0], st[1], 1); std::printf("[quality] continuity_report worst=%s g1_ratio=%.2f g2_ratio=%.2f; " "zebra level=%s max_normal=%.3e max_curv_diff=%.3e\n", to_string(rep.worst), rep.g1_ratio, rep.g2_ratio, to_string(zebra.achieved_level), zebra.max_normal_angle, zebra.max_curv_diff); if (rep.per_sample.empty()) std::printf("[quality] continuity_report 无采样(库自动边探测未命中,按报告处理)\\n"); (void)zebra; // 斑马等级报告;G2/G3 判定以解析误差为准 auto comb = curvature_comb(st[0], st[1], 60); EXPECT_FALSE(comb.points.empty()); auto hl = highlight_lines(st[1], Point3D(0.5, 0.5, 2.0), 0.5, 24, 24); EXPECT_FALSE(hl.intensity.empty()); auto stripe = zebra_stripe(st[1], Vector3D(0.2, 0.5, 1.0), 24, 24); EXPECT_FALSE(stripe.intensity.empty()); // 弯曲能量(薄板样条近似):∫ (|S_uu|² + 2|S_uv|² + |S_vv|²) dA double energy = 0.0; for (int i = 0; i <= 16; ++i) for (int j = 0; j <= 16; ++j) { const double u = i / 16.0, v = j / 16.0; const Vector3D uu = st[1].derivative_uu(u, v); const Vector3D uv = st[1].derivative_uv(u, v); const Vector3D vv = st[1].derivative_vv(u, v); energy += uu.squaredNorm() + 2.0 * uv.squaredNorm() + vv.squaredNorm(); } energy /= 17.0 * 17.0; std::printf("[quality] bending energy=%.6e\n", energy); EXPECT_TRUE(std::isfinite(energy)); EXPECT_GT(energy, 0.0); auto dev = deviation_analysis(st[0], st[1], 32); std::printf("[quality] deviation samples=%zu rms=%.3e\n", dev.sample_count, dev.rms); EXPECT_GE(dev.sample_count, 1u); } } // namespace test } // namespace vde::curves