Files
hm e84f12b510
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat(proposal): 落地 VDE-364 提案 API(Gizmo 吸附变换 + 双面标注)
vde_ui/gizmo_api:
- SnapConfig{grid_spacing, snap_to_grid, snap_to_vertex, snap_tolerance} +
  gizmo_transform_snapped:顶点吸附优先(snap_tolerance 内最近体顶点),否则
  栅格交点吸附,再复用 gizmo_transform 生成变换矩阵。
- FacePairDimension{距离, 夹角, 最近点} + dimension_between_faces:两面 AABB
  最近间隙 + 法向夹角(度,复用 brep::angular_dimension)。

测试:新增 tests/ui/test_gizmo_api.cpp(4/4 全绿)
- 栅格吸附:吸附后 value 等价于在 (1,1,0) 求值,与未吸附相差 0.03。
- 顶点吸附优先于大间距栅格。
- 相对面 距离=2/夹角=180°,相邻面 距离=0/夹角=90°,非法输入 valid=false。

文档:VDE-364.md 状态更正为 Fixed;核验表计数 35/42/0/239。
2026-10-05 02:54:39 +08:00

87 lines
3.4 KiB
C++

#include <gtest/gtest.h>
#include "vde_ui/gizmo_api.h"
#include "vde/brep/modeling.h"
#include <cmath>
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<int>(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);
}