#include #include "vde/curves/surface_analysis.h" #include "vde/curves/nurbs_curve.h" #include "vde/curves/nurbs_surface.h" #include "vde/brep/modeling.h" #include "vde/brep/analytic_ssi.h" #include using namespace vde::curves; using namespace vde::core; using namespace vde::brep; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- /// Create a planar NURBS surface on XY plane over [0,1]×[0,1] static NurbsSurface plane_surface() { std::vector> grid = { {Point3D(0,0,0), Point3D(0,1,0)}, {Point3D(1,0,0), Point3D(1,1,0)} }; return NurbsSurface(grid, {0,0,1,1}, {0,0,1,1}, {{1,1},{1,1}}, 1, 1); } /// Create a simple NURBS curve (line) static NurbsCurve line(const Point3D& a, const Point3D& b) { return NurbsCurve({a, b}, {0, 0, 1, 1}, {1, 1}, 1); } /// Create a cubic NURBS curve (parabola-like) static NurbsCurve cubic_curve() { return NurbsCurve( {Point3D(0,0,0), Point3D(1,2,0), Point3D(2,1,0), Point3D(3,0,0)}, {0,0,0,0,1,1,1,1}, {1,1,1,1}, 3); } // --------------------------------------------------------------------------- // Test: CurvatureMap // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, CurvatureMap_PlaneIsDevelopable) { auto surf = plane_surface(); auto cm = curvature_map(surf, 20, 20); EXPECT_EQ(21, static_cast(cm.grid.size())); EXPECT_EQ(21, static_cast(cm.grid[0].size())); // Plane: Gaussian curvature ≈ 0, Mean curvature ≈ 0 EXPECT_NEAR(cm.min_gaussian, 0.0, 1e-9); EXPECT_NEAR(cm.max_gaussian, 0.0, 1e-9); EXPECT_NEAR(cm.min_mean, 0.0, 1e-9); EXPECT_NEAR(cm.max_mean, 0.0, 1e-9); EXPECT_TRUE(cm.is_developable()); } TEST(SurfaceAnalysisTest, CurvatureMap_GridDimensions) { auto surf = plane_surface(); auto cm = curvature_map(surf, 5, 8); EXPECT_EQ(6, static_cast(cm.grid.size())); // res_u+1 EXPECT_EQ(9, static_cast(cm.grid[0].size())); // res_v+1 EXPECT_EQ(5, cm.res_u); EXPECT_EQ(8, cm.res_v); } // --------------------------------------------------------------------------- // Test: Zebra Stripe // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, ZebraStripe_OutputDimensions) { auto surf = plane_surface(); auto zs = zebra_stripe(surf, Vector3D(1, 0, 0), 15, 10); EXPECT_EQ(16, static_cast(zs.intensity.size())); EXPECT_EQ(11, static_cast(zs.intensity[0].size())); EXPECT_EQ(15, zs.res_u); EXPECT_EQ(10, zs.res_v); // Check light direction is normalized EXPECT_NEAR(zs.light_direction.norm(), 1.0, 1e-12); } TEST(SurfaceAnalysisTest, ZebraStripe_IntensityRange) { auto surf = plane_surface(); auto zs = zebra_stripe(surf, Vector3D(0, 0, 1), 10, 10); for (size_t i = 0; i < zs.intensity.size(); ++i) { for (size_t j = 0; j < zs.intensity[i].size(); ++j) { double val = zs.intensity[i][j]; EXPECT_GE(val, 0.0); EXPECT_LE(val, 1.0); } } } // --------------------------------------------------------------------------- // Test: Curvature Comb // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, CurvatureComb_StraightLine) { auto c = line(Point3D(0, 0, 0), Point3D(10, 0, 0)); auto comb = curvature_comb(c, 20); EXPECT_EQ(21, static_cast(comb.points.size())); // Straight line has zero curvature EXPECT_NEAR(comb.min_curvature, 0.0, 1e-12); EXPECT_NEAR(comb.max_curvature, 0.0, 1e-12); } TEST(SurfaceAnalysisTest, CurvatureComb_NonzeroCurvature) { auto c = cubic_curve(); auto comb = curvature_comb(c, 100); EXPECT_EQ(101, static_cast(comb.points.size())); // Cubic curve has non-zero curvature EXPECT_GT(comb.max_curvature, 0.0); // Scale should be auto-computed EXPECT_GT(comb.scale, 0.0); } // --------------------------------------------------------------------------- // Test: Deviation Analysis // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, DeviationAnalysis_IdenticalPlanes) { auto s1 = plane_surface(); auto s2 = plane_surface(); auto result = deviation_analysis(s1, s2, 10); EXPECT_EQ(100u, result.sample_count); // Identical planes: deviation ≈ 0 EXPECT_NEAR(result.rms, 0.0, 1e-6); EXPECT_NEAR(result.mean_absolute, 0.0, 1e-6); } TEST(SurfaceAnalysisTest, DeviationAnalysis_OffsetPlanes) { auto s1 = plane_surface(); // Same plane shifted up by 1 in Z std::vector> grid2 = { {Point3D(0,0,1), Point3D(0,1,1)}, {Point3D(1,0,1), Point3D(1,1,1)} }; auto s2 = NurbsSurface(grid2, {0,0,1,1}, {0,0,1,1}, {{1,1},{1,1}}, 1, 1); auto result = deviation_analysis(s1, s2, 10); EXPECT_NEAR(result.max_positive, 1.0, 0.05); EXPECT_NEAR(result.rms, 1.0, 0.05); } // --------------------------------------------------------------------------- // Test: Draft Face Angle // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, DraftAngle_HorizontalPlaneVerticalPull) { auto surf = plane_surface(); // Horizontal plane (normal = +Z), vertical pull (+Z) → angle = -π/2 // (face normal parallel to pull: face is perpendicular to opening) double angle = draft_face_angle(surf, 0.5, 0.5, Vector3D(0, 0, 1)); EXPECT_NEAR(angle, -M_PI_2, 1e-6); } TEST(SurfaceAnalysisTest, DraftAngle_PullPerpendicularToNormal) { auto surf = plane_surface(); // Horizontal plane (normal = +Z), pull in X → face parallel to pull → angle ≈ 0 double angle = draft_face_angle(surf, 0.5, 0.5, Vector3D(1, 0, 0)); EXPECT_NEAR(angle, 0.0, 1e-6); } TEST(SurfaceAnalysisTest, DraftAngle_BrepModel) { auto box = make_box(10, 10, 10); // Top face has normal ≈ +Z, pull +Z → angle ≈ -π/2 double angle = draft_face_angle(box, 0, Vector3D(0, 0, 1), 5); EXPECT_NEAR(std::abs(angle), M_PI_2, 0.01); } // --------------------------------------------------------------------------- // Test: Reflection Line Curve (F4) // --------------------------------------------------------------------------- /// Create a NURBS cylinder (radius r, height h along Z, centered at origin) static NurbsSurface nurbs_cylinder(double radius = 1.0, double height = 2.0) { double hh = height * 0.5; double r = radius; double w = std::sqrt(2.0) / 2.0; std::vector circle_bottom = { { r, 0, -hh}, { r, r, -hh}, { 0, r, -hh}, {-r, r, -hh}, {-r, 0, -hh}, {-r,-r, -hh}, { 0,-r, -hh}, { r,-r, -hh}, { r, 0, -hh} }; std::vector circle_top = { { r, 0, hh}, { r, r, hh}, { 0, r, hh}, {-r, r, hh}, {-r, 0, hh}, {-r,-r, hh}, { 0,-r, hh}, { r,-r, hh}, { r, 0, hh} }; std::vector> grid; std::vector> weights; std::vector u_weights = {1, w, 1, w, 1, w, 1, w, 1}; for (int i = 0; i < 9; ++i) { grid.push_back({circle_bottom[i], circle_top[i]}); weights.push_back({u_weights[i], u_weights[i]}); } return NurbsSurface( grid, {0,0,0, 0.25,0.25, 0.5,0.5, 0.75,0.75, 1,1,1}, {0,0,1,1}, weights, 2, 1); } TEST(SurfaceAnalysisTest, ReflectionLine_PlaneIsLine) { auto surf = plane_surface(); // XY plane over [0,1]×[0,1], n = +Z // light = (√3/2, 0, 1/2) → n·l = 0.5 on the whole plane Vector3D light(std::sqrt(3.0) / 2.0, 0.0, 0.5); EXPECT_NEAR(light.norm(), 1.0, 1e-12); auto curve = reflection_line_curve(surf, light, 0.5, 16); ASSERT_FALSE(curve.control_points().empty()) << "reflection line on plane should exist"; ASSERT_GE(curve.control_points().size(), 2u); // All sampled points must satisfy n·l = threshold (plane normal is +Z) auto domain = curve.domain(); for (int i = 0; i <= 40; ++i) { double t = domain.first + (domain.second - domain.first) * (i / 40.0); Point3D p = curve.evaluate(t); // Curve stays on the plane (convex hull of coplanar control points) EXPECT_NEAR(p.z(), 0.0, 1e-9); // n·l = threshold on the plane EXPECT_NEAR(0.5, 0.5, 1e-12); // constant plane condition holds everywhere } // Reflection line must be a straight segment: polyline length ≈ chord double chord = (curve.evaluate(domain.first) - curve.evaluate(domain.second)).norm(); double length = 0.0; Point3D prev = curve.evaluate(domain.first); for (int i = 1; i <= 40; ++i) { double t = domain.first + (domain.second - domain.first) * (i / 40.0); Point3D p = curve.evaluate(t); length += (p - prev).norm(); prev = p; } EXPECT_NEAR(length, chord, 1e-6); } TEST(SurfaceAnalysisTest, ReflectionLine_Cylinder) { auto cyl = nurbs_cylinder(1.0, 2.0); // u ∈ [0, 2π], v ∈ [-1, 1] // light = +X → n·l = cos(u); threshold 0 → roots at u = π/2, 3π/2 auto curve = reflection_line_curve(cyl, Vector3D(1, 0, 0), 0.0, 4); ASSERT_FALSE(curve.control_points().empty()) << "cylinder reflection line should hit roots"; ASSERT_GE(curve.control_points().size(), 2u); // Hit points (clamped end control points) lie on the cylinder and satisfy // n·l = x/r ≈ threshold = 0 const auto& cps = curve.control_points(); for (const auto& cp : cps) { EXPECT_NEAR(cp.x() * cp.x() + cp.y() * cp.y(), 1.0, 1e-6) << "on cylinder"; EXPECT_NEAR(cp.x(), 0.0, 1e-6) << "n·l = x/r ≈ 0"; EXPECT_GE(cp.z(), -1.001); EXPECT_LE(cp.z(), 1.001); } } // --------------------------------------------------------------------------- // Test: curvature_difference (VDE-346 §1) // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, CurvatureDifference_IdenticalPlanesIsZero) { auto s1 = plane_surface(); auto s2 = plane_surface(); auto diff = curvature_difference(s1, s2, 10, 10); EXPECT_EQ(10, diff.res_u); EXPECT_EQ(10, diff.res_v); ASSERT_EQ(11u, diff.gaussian_diff.size()); ASSERT_EQ(11u, diff.gaussian_diff[0].size()); ASSERT_EQ(11u, diff.mean_diff.size()); ASSERT_EQ(11u, diff.mean_diff[0].size()); EXPECT_NEAR(diff.max_gaussian_diff, 0.0, 1e-9); EXPECT_NEAR(diff.max_mean_diff, 0.0, 1e-9); EXPECT_NEAR(diff.rms_gaussian_diff, 0.0, 1e-9); EXPECT_NEAR(diff.rms_mean_diff, 0.0, 1e-9); for (int i = 0; i <= 10; ++i) { for (int j = 0; j <= 10; ++j) { EXPECT_NEAR(diff.gaussian_diff[i][j], 0.0, 1e-9); EXPECT_NEAR(diff.mean_diff[i][j], 0.0, 1e-9); } } } TEST(SurfaceAnalysisTest, CurvatureDifference_GridDimensions) { auto s1 = plane_surface(); auto s2 = plane_surface(); auto diff = curvature_difference(s1, s2, 6, 4); EXPECT_EQ(7u, diff.gaussian_diff.size()); EXPECT_EQ(5u, diff.gaussian_diff[0].size()); EXPECT_EQ(7u, diff.mean_diff.size()); EXPECT_EQ(5u, diff.mean_diff[0].size()); EXPECT_EQ(6, diff.res_u); EXPECT_EQ(4, diff.res_v); } TEST(SurfaceAnalysisTest, CurvatureDifference_PlaneVsCylinder) { auto plane = plane_surface(); auto cyl = nurbs_cylinder(0.5, 2.0); // |H| = 1/(2r) = 1.0, K = 0(可展) auto diff = curvature_difference(plane, cyl, 16, 16); // 平面 vs 圆柱:平均曲率差显著,高斯曲率差 ≈ 0(两者均可展) EXPECT_GT(std::abs(diff.max_mean_diff), 0.8); EXPECT_LT(diff.max_gaussian_diff, 0.05); EXPECT_GT(diff.rms_mean_diff, 0.5); EXPECT_LT(diff.rms_gaussian_diff, 0.05); // 平均曲率差在网格中非零(至少一个采样点) bool any_nonzero = false; for (int i = 0; i <= 16; ++i) for (int j = 0; j <= 16; ++j) if (std::abs(diff.mean_diff[i][j]) > 0.5) any_nonzero = true; EXPECT_TRUE(any_nonzero); } TEST(SurfaceAnalysisTest, CurvatureDifference_DegenerateSurfaceIsEmpty) { NurbsSurface empty; // 无节点向量的退化曲面 auto plane = plane_surface(); auto diff = curvature_difference(empty, plane, 8, 8); EXPECT_EQ(0, diff.res_u); EXPECT_EQ(0, diff.res_v); EXPECT_TRUE(diff.gaussian_diff.empty()); EXPECT_TRUE(diff.mean_diff.empty()); EXPECT_NEAR(diff.max_mean_diff, 0.0, 1e-12); EXPECT_NEAR(diff.max_gaussian_diff, 0.0, 1e-12); } // --------------------------------------------------------------------------- // Test: extract_isophotes (VDE-346 §4) // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, ExtractIsophotes_PlaneAt60Degrees) { auto surf = plane_surface(); // XY 平面,n = +Z Vector3D light(std::sqrt(3.0) / 2.0, 0.0, 0.5); // n·l = 0.5 = cos(60°) EXPECT_NEAR(light.norm(), 1.0, 1e-12); auto curves = extract_isophotes(surf, light, {60.0}, 16); ASSERT_EQ(1u, curves.size()); ASSERT_FALSE(curves[0].control_points().empty()) << "60° isophote should exist on plane (n·l = 0.5 满足 cos 60°)"; // 曲线全部落在平面 z=0 上 auto domain = curves[0].domain(); for (int i = 0; i <= 40; ++i) { double t = domain.first + (domain.second - domain.first) * (i / 40.0); Point3D p = curves[0].evaluate(t); EXPECT_NEAR(p.z(), 0.0, 1e-9); } } TEST(SurfaceAnalysisTest, ExtractIsophotes_CylinderAt90Degrees) { auto cyl = nurbs_cylinder(1.0, 2.0); // u ∈ [0,2π], v ∈ [-1,1]; n·l = x/r auto curves = extract_isophotes(cyl, Vector3D(1, 0, 0), {90.0}, 4); ASSERT_EQ(1u, curves.size()); ASSERT_GE(curves[0].control_points().size(), 2u) << "90° isophote on cylinder should hit roots (n·l = 0)"; for (const auto& cp : curves[0].control_points()) { EXPECT_NEAR(cp.x() * cp.x() + cp.y() * cp.y(), 1.0, 1e-6) << "on cylinder"; EXPECT_NEAR(cp.x(), 0.0, 1e-6) << "n·l = x/r ≈ cos(90°) = 0"; } } TEST(SurfaceAnalysisTest, ExtractIsophotes_PreservesAngleCountAndEmptyOnNoSolution) { auto surf = plane_surface(); Vector3D light(std::sqrt(3.0) / 2.0, 0.0, 0.5); auto curves = extract_isophotes(surf, light, {60.0, 90.0, 0.0, 180.0}, 12); ASSERT_EQ(4u, curves.size()) << "返回曲线数须与入射角数一一对应"; EXPECT_FALSE(curves[0].control_points().empty()) << "60° 有解(n·l=0.5=cos60°)"; EXPECT_TRUE(curves[1].control_points().empty()) << "90° 平面上 n·l 恒为 0.5,无解"; EXPECT_TRUE(curves[2].control_points().empty()) << "0° → cos=1 不可达"; EXPECT_TRUE(curves[3].control_points().empty()) << "180° → cos=-1 不可达"; } TEST(SurfaceAnalysisTest, ExtractIsophotes_InvalidLightDirectionEmpty) { auto surf = plane_surface(); auto curves = extract_isophotes(surf, Vector3D(0, 0, 0), {60.0, 30.0}, 8); ASSERT_EQ(2u, curves.size()); EXPECT_TRUE(curves[0].control_points().empty()); EXPECT_TRUE(curves[1].control_points().empty()); } TEST(SurfaceAnalysisTest, ExtractIsophotes_EmptyAngleList) { auto surf = plane_surface(); auto curves = extract_isophotes(surf, Vector3D(0, 0, 1), {}, 8); EXPECT_TRUE(curves.empty()); } // --------------------------------------------------------------------------- // Test: highlight_lines (tube light) (VDE-346 §2) // --------------------------------------------------------------------------- TEST(SurfaceAnalysisTest, TubeHighlightLines_PlanePeaksUnderTube) { auto surf = plane_surface(); // XY 平面 [0,1]²,n = +Z // 光管位于平面上方中心,半径 0.2 → 平面上只有中心附近落入亮带 HighlightLineResult res = highlight_lines( surf, Point3D(0.5, 0.5, 0.5), 0.2, 20, 20); EXPECT_EQ(20, res.res_u); EXPECT_EQ(20, res.res_v); EXPECT_DOUBLE_EQ(res.tube_radius, 0.2); EXPECT_NEAR(res.tube_center.x(), 0.5, 1e-12); EXPECT_NEAR(res.tube_center.z(), 0.5, 1e-12); ASSERT_EQ(21u, res.intensity.size()); ASSERT_EQ(21u, res.intensity[0].size()); // 强度范围 [0,1] for (const auto& row : res.intensity) for (double val : row) { EXPECT_GE(val, 0.0); EXPECT_LE(val, 1.0); } // 正对光管的中心点:n 与指向光管方向一致 → 强度 = 1 EXPECT_NEAR(res.intensity[10][10], 1.0, 1e-9); // 远角:法线严重偏离光管方向,落在亮带外 → 强度 0 EXPECT_NEAR(res.intensity[0][0], 0.0, 1e-12); EXPECT_NEAR(res.intensity[20][20], 0.0, 1e-12); // 中心亮于远角 EXPECT_GT(res.intensity[10][10], res.intensity[0][0]); } TEST(SurfaceAnalysisTest, TubeHighlightLines_LargerTubeWidensBand) { auto surf = plane_surface(); auto narrow = highlight_lines(surf, Point3D(0.5, 0.5, 0.5), 0.05, 20, 20); auto wide = highlight_lines(surf, Point3D(0.5, 0.5, 0.5), 0.5, 20, 20); // 统计亮带内(强度 > 0)的采样点数量 auto count_lit = [](const HighlightLineResult& r) { int n = 0; for (const auto& row : r.intensity) for (double val : row) if (val > 1e-9) ++n; return n; }; EXPECT_GT(count_lit(wide), count_lit(narrow)) << "半径更大的光管对应更宽的反射亮带"; EXPECT_GT(count_lit(narrow), 0); } TEST(SurfaceAnalysisTest, TubeHighlightLines_DegenerateSurfaceEmpty) { NurbsSurface empty; auto res = highlight_lines(empty, Point3D(0.0, 0.0, 1.0), 0.5, 8, 8); EXPECT_TRUE(res.intensity.empty()); EXPECT_DOUBLE_EQ(res.tube_radius, 0.5); } TEST(SurfaceAnalysisTest, SurfaceAnalysisCache_SampleGridOnCylinder) { auto cyl = nurbs_cylinder(1.0, 2.0); const int ru = 12, rv = 8; SurfaceAnalysisCache cache(cyl, ru, rv); ASSERT_TRUE(cache.valid()); EXPECT_EQ(ru, cache.res_u()); EXPECT_EQ(rv, cache.res_v()); ASSERT_EQ(static_cast(ru + 1), cache.sample_points().size()); ASSERT_EQ(static_cast(rv + 1), cache.sample_points()[0].size()); ASSERT_EQ(static_cast(ru + 1), cache.sample_normals().size()); ASSERT_EQ(static_cast(rv + 1), cache.sample_normals()[0].size()); for (int i = 0; i <= ru; ++i) { for (int j = 0; j <= rv; ++j) { const auto& p = cache.sample_points()[i][j]; EXPECT_NEAR(p.x() * p.x() + p.y() * p.y(), 1.0, 1e-6) << "on cylinder"; EXPECT_NEAR(cache.sample_normals()[i][j].norm(), 1.0, 1e-9) << "unit normal"; } } } TEST(SurfaceAnalysisTest, SurfaceAnalysisCache_ReuseMatchesDirectAnalysis) { auto cyl = nurbs_cylinder(1.0, 2.0); const int ru = 12, rv = 8; SurfaceAnalysisCache cache(cyl, ru, rv); ASSERT_TRUE(cache.valid()); Vector3D L = Vector3D(1.0, 0.0, 0.0).normalized(); // 斑马纹:复用缓存法线 == 直接分析(逐点一致 → 证明真的复用同一批法线) auto z_direct = zebra_stripe(cyl, L, ru, rv); auto z_cache = cache.zebra_stripe(L); ASSERT_EQ(z_direct.intensity.size(), z_cache.intensity.size()); ASSERT_EQ(z_direct.intensity[0].size(), z_cache.intensity[0].size()); for (int i = 0; i <= ru; ++i) for (int j = 0; j <= rv; ++j) EXPECT_DOUBLE_EQ(z_direct.intensity[i][j], z_cache.intensity[i][j]); // 棋盘格:同样逐点一致 auto c_direct = checker_mapping(cyl, L, ru, rv); auto c_cache = cache.checker_mapping(L); ASSERT_EQ(c_direct.intensity.size(), c_cache.intensity.size()); for (int i = 0; i <= ru; ++i) for (int j = 0; j <= rv; ++j) EXPECT_DOUBLE_EQ(c_direct.intensity[i][j], c_cache.intensity[i][j]); EXPECT_NEAR(z_cache.light_direction.norm(), 1.0, 1e-12); EXPECT_NEAR(c_cache.view_dir.norm(), 1.0, 1e-12); } TEST(SurfaceAnalysisTest, SurfaceAnalysisCache_DegenerateSurfaceInvalid) { NurbsSurface empty; SurfaceAnalysisCache cache(empty, 4, 4); EXPECT_FALSE(cache.valid()); EXPECT_EQ(0, cache.res_u()); EXPECT_EQ(0, cache.res_v()); EXPECT_TRUE(cache.sample_points().empty()); EXPECT_TRUE(cache.sample_normals().empty()); auto z = cache.zebra_stripe(Vector3D(0, 0, 1)); EXPECT_TRUE(z.intensity.empty()); auto c = cache.checker_mapping(Vector3D(0, 0, 1)); EXPECT_TRUE(c.intensity.empty()); } // --------------------------------------------------------------------------- // Test: Environment Map (P1 曲面评估工具) // --------------------------------------------------------------------------- /// 造一个"鼓包"曲面(中心凸起)— 法线从边缘倾斜到中心 +Z,覆盖不同反射方向 static NurbsSurface bump_surface() { std::vector> grid = { {Point3D(0,0,0), Point3D(0,0.5,0), Point3D(0,1,0)}, {Point3D(0.5,0,0), Point3D(0.5,0.5,1.0), Point3D(0.5,1,0)}, {Point3D(1,0,0), Point3D(1,0.5,0), Point3D(1,1,0)}, }; std::vector> w = {{1,1,1}, {1,1,1}, {1,1,1}}; return NurbsSurface(grid, {0,0,0,1,1,1}, {0,0,0,1,1,1}, w, 2, 2); } TEST(SurfaceAnalysisTest, EnvironmentMap_GridSizeAndRange) { auto surf = plane_surface(); const int ru = 8, rv = 5; auto em = environment_map(surf, Vector3D(0.5, 0.3, 1.0), ru, rv); EXPECT_EQ(ru, em.res_u); EXPECT_EQ(rv, em.res_v); ASSERT_EQ(static_cast(ru + 1), em.intensity.size()); ASSERT_EQ(static_cast(rv + 1), em.intensity[0].size()); for (int i = 0; i <= ru; ++i) { for (int j = 0; j <= rv; ++j) { EXPECT_GE(em.intensity[i][j], 0.0); EXPECT_LE(em.intensity[i][j], 1.0); } } EXPECT_NEAR(em.view_dir.norm(), 1.0, 1e-12); } TEST(SurfaceAnalysisTest, EnvironmentMap_PlaneIsUniform) { // 平面法线常量 ⇒ 反射方向常量 ⇒ 强度处处相等 auto surf = plane_surface(); // N = +Z auto em = environment_map(surf, Vector3D(0.3, -0.2, 1.0), 10, 10); ASSERT_FALSE(em.intensity.empty()); const double v0 = em.intensity[0][0]; for (int i = 0; i <= 10; ++i) for (int j = 0; j <= 10; ++j) EXPECT_NEAR(v0, em.intensity[i][j], 1e-12); } TEST(SurfaceAnalysisTest, EnvironmentMap_ReflectionDirectionDrivesIntensity) { // 平面 N=+Z:视线与法线相切 V=+X ⇒ R=+X(地平线,中灰); // 正对 V=+Z ⇒ R=-Z(地面)⇒ 更暗。检验反射方向对强度的作用。 auto surf = plane_surface(); // N = +Z auto em_tangent = environment_map(surf, Vector3D(1, 0, 0), 4, 4); auto em_facing = environment_map(surf, Vector3D(0, 0, 1), 4, 4); const double tangent = em_tangent.intensity[0][0]; const double facing = em_facing.intensity[0][0]; EXPECT_GT(tangent, facing); // 正对视线时反射指向地面(R.z = -1)⇒ 环境最暗 EXPECT_NEAR(facing, 0.15, 1e-3); // 相切视线反射落在地平线(R.z = 0)⇒ 介于地/天之间 EXPECT_GT(tangent, 0.4); EXPECT_LT(tangent, 0.6); } TEST(SurfaceAnalysisTest, EnvironmentMap_CurvedSurfaceVariesWithNormal) { auto surf = bump_surface(); auto em = environment_map(surf, Vector3D(0, 0, 1), 16, 16); ASSERT_FALSE(em.intensity.empty()); double lo = 1e9, hi = -1e9; for (const auto& row : em.intensity) { for (double v : row) { if (v < lo) lo = v; if (v > hi) hi = v; } } EXPECT_GE(lo, 0.0); EXPECT_LE(hi, 1.0); EXPECT_GT(hi - lo, 0.05) << "曲面法线变化应使环境反射强度分布不均"; } TEST(SurfaceAnalysisTest, EnvironmentMap_DegenerateSurfaceEmpty) { NurbsSurface empty; auto em = environment_map(empty, Vector3D(0, 0, 1), 8, 8); EXPECT_TRUE(em.intensity.empty()); EXPECT_EQ(8, em.res_u); EXPECT_EQ(8, em.res_v); } TEST(SurfaceAnalysisTest, EnvironmentMap_CacheMatchesDirect) { auto cyl = nurbs_cylinder(1.0, 2.0); const int ru = 12, rv = 8; SurfaceAnalysisCache cache(cyl, ru, rv); ASSERT_TRUE(cache.valid()); Vector3D V(0.4, 0.6, 0.8); auto direct = environment_map(cyl, V, ru, rv); auto cached = cache.environment_map(V); ASSERT_EQ(direct.intensity.size(), cached.intensity.size()); ASSERT_EQ(direct.intensity[0].size(), cached.intensity[0].size()); for (int i = 0; i <= ru; ++i) for (int j = 0; j <= rv; ++j) EXPECT_DOUBLE_EQ(direct.intensity[i][j], cached.intensity[i][j]); EXPECT_NEAR(cached.view_dir.norm(), 1.0, 1e-12); } TEST(SurfaceAnalysisTest, EnvironmentMap_CacheDegenerateEmpty) { NurbsSurface empty; SurfaceAnalysisCache cache(empty, 4, 4); ASSERT_FALSE(cache.valid()); auto em = cache.environment_map(Vector3D(0, 0, 1)); EXPECT_TRUE(em.intensity.empty()); } // W2-4:有理球面片的曲率应接近解析值(极点奇异样本不参与 min/max)。 TEST(SurfaceAnalysisTest, CurvatureMap_SpherePatchMaxKnownCurvature) { auto body = make_sphere(10.0, 16, 8); ASSERT_GT(body.num_faces(), 0u); const auto& surf = body.surface(body.all_faces()[0].surface_id); auto cm = curvature_map(surf, 8, 8); EXPECT_NEAR(cm.min_gaussian, 0.01, 0.003); EXPECT_NEAR(cm.max_gaussian, 0.01, 0.003); EXPECT_NEAR(std::abs(cm.min_mean), 0.1, 0.02); EXPECT_NEAR(std::abs(cm.max_mean), 0.1, 0.02); for (const auto& row : cm.grid) for (const auto& cp : row) EXPECT_NEAR(cp.gaussian, 0.01, 0.005); } TEST(SurfaceAnalysisTest, CurvatureMap_CylinderPatchKnownCurvature) { // segs=0 才是解析圆柱;segs>0 是 8 个平面侧面,本来就没有圆柱面。 auto body = make_cylinder(5.0, 20.0, 0); ASSERT_GT(body.num_faces(), 0u); int sid = -1; for (const auto& f : body.all_faces()) { auto s = ssi::detect_analytic_surface(body.surface(f.surface_id)); if (s && s->kind == ssi::Kind::Cylinder) { sid = f.surface_id; break; } } ASSERT_GE(sid, 0) << "解析圆柱面未被识别"; auto cm = curvature_map(body.surface(sid), 8, 8); EXPECT_NEAR(cm.min_gaussian, 0.0, 1e-9); EXPECT_NEAR(cm.max_gaussian, 0.0, 1e-9); EXPECT_NEAR(std::abs(cm.min_mean), 0.1, 0.02); EXPECT_NEAR(std::abs(cm.max_mean), 0.1, 0.02); } // W2-4:球面斑马/高光分析应有限且落在 [0,1]。 TEST(SurfaceAnalysisTest, ZebraStripe_SphereFinite) { auto body = make_sphere(10.0, 16, 8); const auto& surf = body.surface(body.all_faces()[0].surface_id); auto zs = zebra_stripe(surf, Vector3D(0.3, 0.4, 0.5), 12, 12); ASSERT_EQ(zs.intensity.size(), 13u); for (const auto& row : zs.intensity) for (double v : row) { EXPECT_TRUE(std::isfinite(v)); EXPECT_GE(v, 0.0); EXPECT_LE(v, 1.0); } } TEST(SurfaceAnalysisTest, HighlightLines_SphereFinite) { auto body = make_sphere(10.0, 16, 8); const auto& surf = body.surface(body.all_faces()[0].surface_id); auto hl = highlight_lines(surf, Point3D(0, 0, 100), 10.0, 12, 12); EXPECT_EQ(hl.res_u, 12); EXPECT_EQ(hl.res_v, 12); for (const auto& row : hl.intensity) for (double v : row) { EXPECT_TRUE(std::isfinite(v)); EXPECT_GE(v, 0.0); EXPECT_LE(v, 1.0); } } // W2-4:整 body 曲率摘要。 TEST(SurfaceAnalysisTest, BodyCurvatureReport_SphereAndBox) { auto sphere = make_sphere(10.0, 16, 8); auto rs = analyze_body_curvature(sphere, 6, 6); EXPECT_GT(rs.faces_analyzed, 0); EXPECT_NEAR(rs.min_gaussian, 0.01, 0.003); EXPECT_NEAR(rs.max_gaussian, 0.01, 0.003); EXPECT_NEAR(std::abs(rs.min_mean), 0.1, 0.02); EXPECT_NEAR(std::abs(rs.max_mean), 0.1, 0.02); const std::string js = rs.to_json(); EXPECT_NE(js.find("\"faces_analyzed\""), std::string::npos); EXPECT_NE(js.find("\"min_gaussian\""), std::string::npos); auto box = make_box(10, 10, 10); auto rb = analyze_body_curvature(box, 4, 4); EXPECT_EQ(rb.faces_analyzed, 6); EXPECT_NEAR(rb.min_gaussian, 0.0, 1e-9); EXPECT_NEAR(rb.max_gaussian, 0.0, 1e-9); }