Files
ViewDesignEngine/tests/brep/test_direct_modeling.cpp
T
hm 6e12bd1a36 E5: 多厚度抽壳 shell_multi + 分型线拔模 parting_line_draft(修复 HalfedgeMesh 边表 O(n²) 挂死)
- shell_multi: 每面独立壁厚,壁厚变化共享边生成台阶侧壁,开口/封闭壳支持
- parting_line_draft: 上下模分类 + 铰链边旋转 + 逐面重建校验,5 项测试通过
- HalfedgeMesh::add_face 改用持久边查找表,to_mesh 从挂死恢复(ShellMulti 4 测试通过)
- 路线图 E5 标记完成
2026-08-09 23:34:47 +08:00

586 lines
22 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/brep/brep.h"
#include "vde/brep/modeling.h"
#include "vde/brep/direct_modeling.h"
#include "vde/brep/brep_validate.h"
#include "vde/core/transform.h"
#include <cmath>
#include <algorithm>
#include <vector>
using namespace vde::brep;
using namespace vde::core;
using namespace vde::curves;
// ═══════════════════════════════════════════════════════════
// tweak_face — 面偏移测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, TweakFace_BoxTopOffset) {
auto box = make_box(2, 2, 2);
auto result = tweak_face(box, 0, 0.5);
EXPECT_TRUE(result.success) << "tweak_face should succeed for box top face";
EXPECT_GT(result.body.num_faces(), 0u);
// After tweak, model should still be valid
auto val = validate(result.body);
EXPECT_TRUE(val.valid) << "Offset model should still be valid";
}
TEST(DirectModelingTest, TweakFace_BoxInwardOffset) {
auto box = make_box(2, 2, 2);
auto result = tweak_face(box, 0, -0.3);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, TweakFace_ZeroDelta) {
auto box = make_box(2, 2, 2);
auto result = tweak_face(box, 0, 0.0);
EXPECT_TRUE(result.success);
// Zero delta should produce valid model (essentially unchanged)
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
EXPECT_EQ(result.body.num_faces(), box.num_faces());
}
TEST(DirectModelingTest, TweakFace_InvalidFaceId) {
auto box = make_box(2, 2, 2);
auto result = tweak_face(box, 99, 0.5);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, TweakFace_CylinderCap) {
auto cyl = make_cylinder(2, 4, 32);
auto result = tweak_face(cyl, 0, 0.5);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
// ═══════════════════════════════════════════════════════════
// move_face — 面移动测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, MoveFace_Translate) {
auto box = make_box(2, 2, 2);
auto T = translate(Vector3D(0.5, 0, 0));
auto result = move_face(box, 0, T);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, MoveFace_Rotate) {
auto box = make_box(2, 2, 2);
// Rotate around Z axis by small angle
auto T = rotate_z(M_PI / 12);
auto result = move_face(box, 0, T);
// Small rotation should succeed
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, MoveFace_TranslateAndRotate) {
auto box = make_box(2, 2, 2);
auto T = translate(Vector3D(0.2, 0.1, 0)) * rotate_z(0.1);
auto result = move_face(box, 0, T);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, MoveFace_LargeRotationFails) {
auto box = make_box(2, 2, 2);
// Large rotation should cause normal flip and be rejected
auto T = rotate_z(2.0); // ~115 degrees
auto result = move_face(box, 0, T);
// May succeed or fail depending on detection, but should not crash
// At minimum the result body should exist
EXPECT_GE(result.body.num_faces(), 0u);
}
TEST(DirectModelingTest, MoveFace_InvalidFaceId) {
auto box = make_box(2, 2, 2);
auto T = translate(Vector3D(0.5, 0, 0));
auto result = move_face(box, 99, T);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
// ═══════════════════════════════════════════════════════════
// replace_face — 面替换测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, ReplaceFace_WithSameSurface) {
auto box = make_box(2, 2, 2);
// Replace face 0 with its own surface (should succeed)
const auto& orig_face = box.face(0);
const auto& orig_surf = box.surface(orig_face.surface_id);
auto result = replace_face(box, 0, orig_surf);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, ReplaceFace_WithCurvedSurface) {
auto box = make_box(2, 2, 2);
// Create a slightly curved surface matching the top face boundary
// Top face of box(2,2,2) has corners: (-1,-1,1), (1,-1,1), (1,1,1), (-1,1,1)
std::vector<std::vector<Point3D>> grid = {
{Point3D(-1, -1, 1.0), Point3D(-1, 1, 1.0)},
{Point3D( 1, -1, 1.2), Point3D( 1, 1, 1.2)} // curved up in middle
};
NurbsSurface curved(grid, {0,0,1,1}, {0,0,1,1}, {}, 1, 1);
auto result = replace_face(box, 0, curved);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, ReplaceFace_InvalidFaceId) {
auto box = make_box(2, 2, 2);
const auto& orig_face = box.face(0);
const auto& orig_surf = box.surface(orig_face.surface_id);
auto result = replace_face(box, 99, orig_surf);
EXPECT_FALSE(result.success);
}
// ═══════════════════════════════════════════════════════════
// push_pull — 推拉测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, PushPull_ExtrudePositive) {
auto box = make_box(2, 2, 2);
auto result = push_pull(box, 0, 1.0);
EXPECT_TRUE(result.success);
// Extrusion should add side faces
EXPECT_GT(result.new_faces, 0);
EXPECT_GT(result.body.num_faces(), box.num_faces());
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, PushPull_CutNegative) {
auto box = make_box(2, 2, 2);
auto result = push_pull(box, 0, -0.3);
EXPECT_TRUE(result.success);
// Cut should not add new faces, but reshape existing ones
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, PushPull_ZeroDistance) {
auto box = make_box(2, 2, 2);
auto result = push_pull(box, 0, 0.0);
// Zero distance push-pull: model unchanged (side faces degenerate)
EXPECT_GE(result.body.num_faces(), box.num_faces());
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, PushPull_InvalidFaceId) {
auto box = make_box(2, 2, 2);
auto result = push_pull(box, 99, 1.0);
EXPECT_FALSE(result.success);
}
// ═══════════════════════════════════════════════════════════
// offset_face — 面等距偏移测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, OffsetFace_Basic) {
auto box = make_box(2, 2, 2);
auto result = offset_face(box, 0, 0.3);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, OffsetFace_Inward) {
auto box = make_box(2, 2, 2);
auto result = offset_face(box, 0, -0.2);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
// ═══════════════════════════════════════════════════════════
// rotate_face — 面旋转测试 (E1)
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, RotateFace_Basic) {
auto box = make_box(2, 2, 2);
// 绕穿过体中心的 Z 轴旋转 15°
auto result = rotate_face(box, 0, Point3D(1, 1, 0), Vector3D::UnitZ(), 15.0);
EXPECT_TRUE(result.success) << "rotate_face should succeed for box top face";
EXPECT_GT(result.body.num_faces(), 0u);
auto val = validate(result.body);
EXPECT_TRUE(val.valid) << "Rotated model should still be valid";
}
TEST(DirectModelingTest, RotateFace_SmallAnglePreservesGeometry) {
auto box = make_box(2, 2, 2);
// 0° 旋转:几何不应变化
auto result = rotate_face(box, 0, Point3D(1, 1, 0), Vector3D::UnitZ(), 0.0);
EXPECT_TRUE(result.success);
EXPECT_EQ(result.body.num_faces(), box.num_faces());
// 顶点应保持不变
for (size_t i = 0; i < box.num_vertices(); ++i) {
double d = (result.body.vertex(static_cast<int>(i)).point -
box.vertex(static_cast<int>(i)).point).norm();
EXPECT_LT(d, 1e-9) << "vertex " << i << " moved for 0-degree rotation";
}
}
TEST(DirectModelingTest, RotateFace_InvalidFaceId) {
auto box = make_box(2, 2, 2);
auto result = rotate_face(box, 99, Point3D(1, 1, 0), Vector3D::UnitZ(), 15.0);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, RotateFace_ZeroAxis) {
auto box = make_box(2, 2, 2);
auto result = rotate_face(box, 0, Point3D(1, 1, 0), Vector3D(0, 0, 0), 15.0);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, RotateFace_LargeAngleDegenerateOrValid) {
auto box = make_box(2, 2, 2);
// 大角度旋转可能导致法线翻转被拒绝,也可能成功——但不应崩溃
auto result = rotate_face(box, 0, Point3D(1, 1, 0), Vector3D::UnitZ(), 60.0);
EXPECT_GE(result.body.num_faces(), 0u);
}
// ═══════════════════════════════════════════════════════════
// Validation after operations — 操作后模型验证
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, AllOps_PreserveValidation) {
auto box = make_box(2, 2, 2);
// tweak_face
auto r1 = tweak_face(box, 0, 0.3);
EXPECT_TRUE(validate(r1.body).valid);
// move_face
auto r2 = move_face(box, 0, translate(Vector3D(0.1, 0, 0)));
EXPECT_TRUE(validate(r2.body).valid);
// push_pull
auto r3 = push_pull(box, 0, 0.5);
EXPECT_TRUE(validate(r3.body).valid);
// offset_face
auto r4 = offset_face(box, 0, 0.2);
EXPECT_TRUE(validate(r4.body).valid);
}
// ═══════════════════════════════════════════════════════════
// 退化情况 — 非法位置测试
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, Degenerate_NegativeFaceId) {
auto box = make_box(2, 2, 2);
auto result = tweak_face(box, -1, 0.5);
EXPECT_FALSE(result.success);
}
TEST(DirectModelingTest, Degenerate_MoveIntoSelfIntersection) {
auto box = make_box(2, 2, 2);
// Move a face far enough to flip the normal → should be detected
auto T = rotate_x(M_PI); // 180° rotation
auto result = move_face(box, 0, T);
// Should at minimum produce a body (even if invalid)
EXPECT_GE(result.body.num_faces(), 0u);
}
TEST(DirectModelingTest, Degenerate_ExcessivePushPull) {
auto box = make_box(2, 2, 2);
// Push-pull with very large negative distance (cut through)
auto result = push_pull(box, 0, -5.0);
EXPECT_TRUE(result.success || !result.errors.empty());
// Should not crash even with excessive distance
}
// ═══════════════════════════════════════════════════════════
// fill_hole — 填孔测试 (v5.5)
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, FillHole_BoxFaceEdges) {
// 使用 box 已有面的边作为 hole 边界来测试 fill_hole
auto box = make_box(2, 2, 2);
auto face_edges = box.face_edges(0);
auto result = fill_hole(box, face_edges);
EXPECT_TRUE(result.success) << "fill_hole should succeed with valid edge loop";
EXPECT_GT(result.affected_faces, 0); // 应返回新增面的 face_id
EXPECT_GT(result.body.num_faces(), box.num_faces()); // 新增了一个面
}
TEST(DirectModelingTest, FillHole_TooFewEdges) {
auto box = make_box(2, 2, 2);
// 少于 3 条边不能构成面
std::vector<int> two_edges = {0, 1};
auto result = fill_hole(box, two_edges);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, FillHole_NonClosedLoop) {
auto box = make_box(2, 2, 2);
// 取不连续的边,无法形成封闭环
std::vector<int> non_closed = {0, 5, 10};
auto result = fill_hole(box, non_closed);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, FillHole_InvalidEdgeId) {
auto box = make_box(2, 2, 2);
// 包含不存在的边 ID
std::vector<int> bad_edges = {0, 999, 1, 2};
auto result = fill_hole(box, bad_edges);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, FillHole_TriangleHole) {
// 用三条边(三角形 hole)测试
auto box = make_box(2, 2, 2);
// 取某个面的前 3 条边组成三角形环
auto face_edges = box.face_edges(0);
ASSERT_GE(face_edges.size(), 3u);
std::vector<int> tri_loop = {face_edges[0], face_edges[1], face_edges[2]};
// 检查这 3 条边是否首尾相连
const auto& b = box;
int v00 = b.edge(tri_loop[0]).v_start;
int v01 = b.edge(tri_loop[0]).v_end;
int v10 = b.edge(tri_loop[1]).v_start;
int v11 = b.edge(tri_loop[1]).v_end;
int v20 = b.edge(tri_loop[2]).v_start;
int v21 = b.edge(tri_loop[2]).v_end;
if (v01 == v10 && v11 == v20 && v21 == v00) {
auto result = fill_hole(box, tri_loop);
// 即使可能不完全封闭整个面,也不应崩溃
EXPECT_GE(result.body.num_faces(), 0u);
}
// 如果不能构成环则跳过
}
// ═══════════════════════════════════════════════════════════
// pull_up_to_face — 拉伸面到目标面测试 (v5.5)
// ═══════════════════════════════════════════════════════════
TEST(DirectModelingTest, PullUpFace_Basic) {
// Box: 面 0 (顶面, z=1) → 面 2 (底面, z=-1),拉到底面
auto box = make_box(2, 2, 2);
// 面 0 和面 2 是相对面
auto result = pull_up_to_face(box, 0, 2);
EXPECT_TRUE(result.success) << "pull_up_to_face should succeed";
EXPECT_GT(result.body.num_faces(), 0u);
}
TEST(DirectModelingTest, PullUpFace_OppositeFace) {
// 将 box 的一个侧面拉到对面
auto box = make_box(2, 2, 2);
// 面 3 和面 4 是相对的侧面 (x 方向)
auto result = pull_up_to_face(box, 3, 4);
EXPECT_TRUE(result.success);
auto val = validate(result.body);
EXPECT_TRUE(val.valid);
}
TEST(DirectModelingTest, PullUpFace_InvalidFaceId) {
auto box = make_box(2, 2, 2);
auto result = pull_up_to_face(box, 99, 0);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, PullUpFace_SameFace) {
auto box = make_box(2, 2, 2);
auto result = pull_up_to_face(box, 0, 0);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
TEST(DirectModelingTest, PullUpFace_InvalidTarget) {
auto box = make_box(2, 2, 2);
auto result = pull_up_to_face(box, 0, 999);
EXPECT_FALSE(result.success);
EXPECT_FALSE(result.errors.empty());
}
// ═══════════════════════════════════════════════════════════
// parting_line_draft — 分型线拔模 (E5)
// ═══════════════════════════════════════════════════════════
namespace {
// 收集指定面的所有顶点坐标(用于几何断言)
std::vector<Point3D> face_vertex_points(const BrepModel& m, int fid) {
std::vector<Point3D> pts;
for (int ei : m.face_edges(fid)) {
const auto& e = m.edge(ei);
pts.push_back(m.vertex(e.v_start).point);
pts.push_back(m.vertex(e.v_end).point);
}
return pts;
}
// 返回一组点的 x/y 包围范围(xmin, xmax, ymin, ymax)
void xy_extent(const std::vector<Point3D>& pts,
double& xmin, double& xmax, double& ymin, double& ymax) {
xmin = 1e9; xmax = -1e9; ymin = 1e9; ymax = -1e9;
for (const auto& p : pts) {
xmin = std::min(xmin, p.x()); xmax = std::max(xmax, p.x());
ymin = std::min(ymin, p.y()); ymax = std::max(ymax, p.y());
}
}
} // namespace
TEST(PartingLineDraftTest, BoxEntirelyAbovePartingLine_DraftsFourSides) {
// 盒体整体位于分型面 (z=0) 上方:z ∈ [1,5]
auto box = make_box(10, 6, 4).transformed(translate(0, 0, 3));
ASSERT_TRUE(box.is_valid());
ASSERT_EQ(box.num_faces(), 6u);
auto r = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 5.0, 3.0);
ASSERT_TRUE(r.success());
EXPECT_EQ(r.drafted_faces, 4); // 4 个侧面全部拔模
EXPECT_EQ(r.drafted_face_ids.size(), 4u);
EXPECT_TRUE(r.body.is_valid());
// 水密校验
auto val = validate(r.body);
EXPECT_TRUE(val.valid);
// 上方半模:顶部收窄 → 顶面(face 1)的 x/y 范围小于原始盒体
auto top_pts = face_vertex_points(r.body, 1);
double xmin, xmax, ymin, ymax;
xy_extent(top_pts, xmin, xmax, ymin, ymax);
EXPECT_LT(xmax - xmin, 10.0 - 0.1);
EXPECT_LT(ymax - ymin, 6.0 - 0.1);
// 底面(face 0,铰链所在)保持原位:z 仍为 1
auto bot_pts = face_vertex_points(r.body, 0);
for (const auto& p : bot_pts) {
EXPECT_NEAR(p.z(), 1.0, 1e-9);
}
}
TEST(PartingLineDraftTest, BoxEntirelyBelowPartingLine_DraftsFourSides) {
// 盒体整体位于分型面 (z=0) 下方:z ∈ [-5,-1]
auto box = make_box(10, 6, 4).transformed(translate(0, 0, -3));
auto r = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 5.0, 3.0);
ASSERT_TRUE(r.success());
EXPECT_EQ(r.drafted_faces, 4);
EXPECT_EQ(r.drafted_face_ids.size(), 4u);
EXPECT_TRUE(r.body.is_valid());
auto val = validate(r.body);
EXPECT_TRUE(val.valid);
// 下方半模:底部收窄 → 底面(face 0)的 x/y 范围小于原始盒体
auto bot_pts = face_vertex_points(r.body, 0);
double xmin, xmax, ymin, ymax;
xy_extent(bot_pts, xmin, xmax, ymin, ymax);
EXPECT_LT(xmax - xmin, 10.0 - 0.1);
EXPECT_LT(ymax - ymin, 6.0 - 0.1);
// 顶面(face 1,铰链所在)保持原位:z 仍为 -1
auto top_pts = face_vertex_points(r.body, 1);
for (const auto& p : top_pts) {
EXPECT_NEAR(p.z(), -1.0, 1e-9);
}
}
TEST(PartingLineDraftTest, BoxCrossingPartingLine_SkippedWithWarning) {
// 盒体跨分型面(z ∈ [-2,2])→ 所有侧面均跨越 → 无拔模面 + 警告
auto box = make_box(10, 6, 4);
auto r = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 5.0, 3.0);
EXPECT_TRUE(r.success());
EXPECT_EQ(r.drafted_faces, 0);
EXPECT_TRUE(r.drafted_face_ids.empty());
EXPECT_FALSE(r.warnings.empty());
EXPECT_TRUE(r.body.is_valid());
}
TEST(PartingLineDraftTest, InvalidInputs_Rejected) {
auto box = make_box(10, 6, 4).transformed(translate(0, 0, 3));
// 零拔模方向
auto r0 = parting_line_draft(box, Vector3D::Zero(), 0.0, 5.0, 3.0);
EXPECT_FALSE(r0.success());
EXPECT_FALSE(r0.errors.empty());
// 角度越界(≥ 90° / < 0° / = 0°)
auto r1 = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 90.0, 3.0);
EXPECT_FALSE(r1.success());
auto r2 = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 5.0, -1.0);
EXPECT_FALSE(r2.success());
auto r3 = parting_line_draft(box, Vector3D::UnitZ(), 0.0, 0.0, 3.0);
EXPECT_FALSE(r3.success());
}
TEST(PartingLineDraftTest, NonUnitPullDir_Normalized) {
// 非单位拔模方向(长度 5)应被归一化后正常工作
auto box = make_box(10, 6, 4).transformed(translate(0, 0, 3));
auto r = parting_line_draft(box, Vector3D(0, 0, 5.0), 0.0, 5.0, 3.0);
EXPECT_TRUE(r.success());
EXPECT_EQ(r.drafted_faces, 4);
auto val = validate(r.body);
EXPECT_TRUE(val.valid);
}