#include #include "vde_ui/gizmo_api.h" #include "vde/brep/modeling.h" #include using namespace vde::brep; using namespace vde::ui; using namespace vde::core; /// 找出法向最接近 dir 的面 ID static int face_with_normal(const BrepModel& body, const Vector3D& dir) { int best = -1; double best_dot = -2.0; for (int f = 0; f < static_cast(body.num_faces()); ++f) { const Vector3D n = body.face_normal(f); const double d = n.normalized().dot(dir.normalized()); if (d > best_dot) { best_dot = d; best = f; } } return best; } // ── gizmo_transform_snapped:栅格吸附 ── TEST(GizmoApiTest, TransformSnapped_GridSnap) { auto body = make_box(2, 2, 2); // 中心在原点 SnapConfig cfg; cfg.snap_to_grid = true; cfg.snap_to_vertex = false; cfg.grid_spacing = 1.0; const Point3D raw(0.97, 1.04, -0.4); const auto sn = gizmo_transform_snapped(body, raw, GizmoAxisType::TranslateX, cfg); const auto raw_t = gizmo_transform(body, raw, GizmoAxisType::TranslateX); const auto expect = gizmo_transform(body, Point3D(1.0, 1.0, 0.0), GizmoAxisType::TranslateX); // 吸附后等价于在 (1,1,0) 处求变换;与未吸附值相差 0.03 EXPECT_NEAR(sn.value, expect.value, 1e-9); EXPECT_NEAR(sn.value - raw_t.value, 0.03, 1e-9); } // ── gizmo_transform_snapped:顶点吸附优先于栅格 ── TEST(GizmoApiTest, TransformSnapped_VertexPriority) { auto body = make_box(2, 2, 2); // 顶点 (±1,±1,±1) SnapConfig cfg; cfg.snap_to_vertex = true; cfg.snap_to_grid = true; cfg.grid_spacing = 100.0; // 栅格会给出 (0,0,0);顶点吸附须优先 cfg.snap_tolerance = 0.5; const Point3D raw(0.9, 0.9, 0.9); // 距顶点 (1,1,1) ≈ 0.173 < 0.5 const auto sn = gizmo_transform_snapped(body, raw, GizmoAxisType::TranslateX, cfg); const auto expect = gizmo_transform(body, Point3D(1.0, 1.0, 1.0), GizmoAxisType::TranslateX); EXPECT_NEAR(sn.value, expect.value, 1e-9); // = 1 - pivot.x = 1 } // ── dimension_between_faces:相对面(距离)+ 相邻面(夹角) ── TEST(GizmoApiTest, DimensionBetweenFaces_OppositeAndAdjacent) { auto body = make_box(2, 4, 6); // X∈[-1,1], Y∈[-2,2], Z∈[-3,3] const int fxp = face_with_normal(body, Vector3D(1, 0, 0)); const int fxn = face_with_normal(body, Vector3D(-1, 0, 0)); const int fyp = face_with_normal(body, Vector3D(0, 1, 0)); ASSERT_GE(fxp, 0); ASSERT_GE(fxn, 0); ASSERT_GE(fyp, 0); // 相对面:距离 = 宽 2,法向夹角 180° const auto opp = dimension_between_faces(body, fxp, fxn); EXPECT_TRUE(opp.valid); EXPECT_NEAR(opp.distance, 2.0, 1e-9); EXPECT_NEAR(opp.angle_deg, 180.0, 1e-6); // 相邻面:包围盒相接 → 距离 0,法向夹角 90° const auto adj = dimension_between_faces(body, fxp, fyp); EXPECT_TRUE(adj.valid); EXPECT_NEAR(adj.distance, 0.0, 1e-9); EXPECT_NEAR(adj.angle_deg, 90.0, 1e-6); } // ── dimension_between_faces:非法输入 ── TEST(GizmoApiTest, DimensionBetweenFaces_Invalid) { auto body = make_box(2, 2, 2); EXPECT_FALSE(dimension_between_faces(body, 0, 0).valid); EXPECT_FALSE(dimension_between_faces(body, -1, 1).valid); EXPECT_FALSE(dimension_between_faces(body, 0, 999).valid); }