fillet_edges: Fixed edge ID invalidation bug by tracking edges via endpoint coordinate signatures across fillet rebuilds fuse_all/intersect_all/difference_all: Added AABB spatial partitioning — prioritize overlapping pairs, skip non-intersecting bodies, early-exit empty intersections fastener_engine counterbore detection: Fixed false positive that flagged any adjacent large face as counterbore; now uses proper radial size comparison with axial depth bounds Tests: Rewrote all 25 tests with geometric property validation (dimensions, displacement, scale, topology, bounding box) replacing weak num_faces>0 assertions
345 lines
14 KiB
C++
345 lines
14 KiB
C++
/**
|
||
* @file test_geometry_completion.cpp
|
||
* @brief 几何引擎完善测试 (VDE-137/167/170/171) — 加强版
|
||
*
|
||
* 测试策略:不验证"不崩溃",而是验证几何属性的正确性:
|
||
* - 尺寸(包围盒、直径、长度)
|
||
* - 拓扑一致性(面/边/顶点数量)
|
||
* - 变换正确性(坐标位移、比例)
|
||
* - 布尔正确性(体积、面数变化)
|
||
*/
|
||
|
||
#include <gtest/gtest.h>
|
||
#include "vde/brep/modeling.h"
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/parallel_boolean.h"
|
||
#include "vde/core/transform.h"
|
||
#include <cmath>
|
||
|
||
using namespace vde;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-137: make_helical_gear — 真正的斜齿轮
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(GeometryCompletion_VDE137, HelicalGearHasCorrectOuterRadius) {
|
||
// module=2, teeth=10 → pitch_r=10, addendum_r=12
|
||
auto gear = brep::make_helical_gear(2.0, 10, 5.0, 20.0, 15.0);
|
||
|
||
ASSERT_GT(gear.num_faces(), 0u);
|
||
|
||
auto bb = gear.bounds();
|
||
auto extent = bb.max() - bb.min();
|
||
|
||
// 齿顶圆直径 ≈ 24mm (addendum_r=12, diameter=24)
|
||
// 包围盒 X/Y 尺寸应接近 24±2(考虑离散化误差)
|
||
EXPECT_NEAR(extent.x(), 24.0, 3.0);
|
||
EXPECT_NEAR(extent.y(), 24.0, 3.0);
|
||
// Z 尺寸 = 齿宽 5mm
|
||
EXPECT_NEAR(extent.z(), 5.0, 1.0);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE137, HelicalGearDifferentFromSpur) {
|
||
auto spur = brep::make_spur_gear(2.0, 10, 5.0, 20.0);
|
||
auto helical = brep::make_helical_gear(2.0, 10, 5.0, 20.0, 30.0);
|
||
|
||
// 斜齿轮的顶点数应该 > 直齿轮(扭转放样产生多层)
|
||
// spur 用 extrude 只有2层顶点,helical 用 loft 有多层
|
||
EXPECT_GT(helical.num_vertices(), spur.num_vertices());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE137, HelicalGearLargerHelixMoreTwist) {
|
||
// 螺旋角 30° 应比 15° 有更多扭转层
|
||
auto gear15 = brep::make_helical_gear(2.0, 10, 5.0, 20.0, 15.0);
|
||
auto gear30 = brep::make_helical_gear(2.0, 10, 5.0, 20.0, 30.0);
|
||
|
||
// 两者都应生成有效实体
|
||
ASSERT_GT(gear15.num_faces(), 0u);
|
||
ASSERT_GT(gear30.num_faces(), 0u);
|
||
|
||
// 齿顶圆直径相同(不随螺旋角变化)
|
||
auto bb15 = gear15.bounds();
|
||
auto bb30 = gear30.bounds();
|
||
EXPECT_NEAR((bb15.max() - bb15.min()).norm(),
|
||
(bb30.max() - bb30.min()).norm(), 3.0);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-137: make_spring — 真实螺旋扫掠
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(GeometryCompletion_VDE137, SpringHasCorrectCoilDiameter) {
|
||
// coil_diameter=20 (R=10), wire_diameter=4 (r=2), coils=3, length=15
|
||
auto spring = brep::make_spring(20.0, 4.0, 3.0, 15.0, brep::SpringType::Compression);
|
||
|
||
ASSERT_GT(spring.num_faces(), 0u);
|
||
|
||
auto bb = spring.bounds();
|
||
auto extent = bb.max() - bb.min();
|
||
|
||
// X/Y 尺寸 ≈ coil_diameter + wire_diameter = 24mm (外径)
|
||
EXPECT_NEAR(extent.x(), 24.0, 4.0);
|
||
EXPECT_NEAR(extent.y(), 24.0, 4.0);
|
||
// Z 尺寸 ≈ free_length + wire_diameter = 17mm(含两端线径)
|
||
EXPECT_GE(extent.z(), 14.0);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE137, SpringUsesSweepNotExtrude) {
|
||
// 验证 spring 不是简单的 Z 轴挤出
|
||
// extrude 会产生规则的柱面,sweep 会产生螺旋管
|
||
auto spring = brep::make_spring(20.0, 4.0, 3.0, 15.0);
|
||
|
||
// sweep 生成的顶点数远多于简单 extrude
|
||
// extrude(profile_pts, width) 会产生 2 * profile_pts 个顶点
|
||
// sweep(circle, helix) 会产生 samples * circle_pts 个顶点(约 32*9 = 288)
|
||
EXPECT_GT(spring.num_vertices(), 50u);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE137, SpringTorsionValid) {
|
||
auto spring = brep::make_spring(20.0, 4.0, 3.0, 0.0, brep::SpringType::Torsion);
|
||
ASSERT_GT(spring.num_faces(), 0u);
|
||
ASSERT_GT(spring.num_vertices(), 0u);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-171: BrepModel::transformed()
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(GeometryCompletion_VDE171, TranslatedBoxExactDisplacement) {
|
||
auto box = brep::make_box(10.0, 5.0, 3.0);
|
||
auto orig_bb = box.bounds();
|
||
|
||
auto moved = box.transformed(core::translate({100.0, 200.0, 300.0}));
|
||
auto new_bb = moved.bounds();
|
||
|
||
// 包围盒应精确位移 (100, 200, 300)
|
||
EXPECT_NEAR(new_bb.min().x() - orig_bb.min().x(), 100.0, 1e-6);
|
||
EXPECT_NEAR(new_bb.min().y() - orig_bb.min().y(), 200.0, 1e-6);
|
||
EXPECT_NEAR(new_bb.min().z() - orig_bb.min().z(), 300.0, 1e-6);
|
||
EXPECT_NEAR(new_bb.max().x() - orig_bb.max().x(), 100.0, 1e-6);
|
||
EXPECT_NEAR(new_bb.max().y() - orig_bb.max().y(), 200.0, 1e-6);
|
||
EXPECT_NEAR(new_bb.max().z() - orig_bb.max().z(), 300.0, 1e-6);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, TranslatedPreservesSize) {
|
||
auto box = brep::make_box(10.0, 5.0, 3.0);
|
||
auto orig_extent = (box.bounds().max() - box.bounds().min());
|
||
|
||
auto moved = box.transformed(core::translate({50.0, 50.0, 50.0}));
|
||
auto new_extent = (moved.bounds().max() - moved.bounds().min());
|
||
|
||
// 尺寸完全不变
|
||
EXPECT_DOUBLE_EQ(new_extent.x(), orig_extent.x());
|
||
EXPECT_DOUBLE_EQ(new_extent.y(), orig_extent.y());
|
||
EXPECT_DOUBLE_EQ(new_extent.z(), orig_extent.z());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, Rotated90DegZAxis) {
|
||
auto box = brep::make_box(10.0, 5.0, 3.0);
|
||
auto orig_bb = box.bounds();
|
||
auto orig_extent = orig_bb.max() - orig_bb.min();
|
||
|
||
// 绕 Z 轴旋转 90° → X 和 Y 尺寸交换
|
||
auto rotated = box.transformed(core::rotate(core::Vector3D::UnitZ(), M_PI / 2));
|
||
auto new_extent = rotated.bounds().max() - rotated.bounds().min();
|
||
|
||
EXPECT_NEAR(new_extent.x(), orig_extent.y(), 1e-6);
|
||
EXPECT_NEAR(new_extent.y(), orig_extent.x(), 1e-6);
|
||
EXPECT_NEAR(new_extent.z(), orig_extent.z(), 1e-6);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, ScaledBy2DoublesSize) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
auto orig_extent = box.bounds().max() - box.bounds().min();
|
||
|
||
core::Transform3D scale_tf = core::Transform3D::Identity();
|
||
scale_tf.scale(2.0);
|
||
auto scaled = box.transformed(scale_tf);
|
||
auto new_extent = scaled.bounds().max() - scaled.bounds().min();
|
||
|
||
// 统一缩放 2 倍 → 所有尺寸翻倍
|
||
EXPECT_NEAR(new_extent.x(), orig_extent.x() * 2.0, 1e-6);
|
||
EXPECT_NEAR(new_extent.y(), orig_extent.y() * 2.0, 1e-6);
|
||
EXPECT_NEAR(new_extent.z(), orig_extent.z() * 2.0, 1e-6);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, TopologyPreservedAfterTransform) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
auto orig_faces = box.num_faces();
|
||
auto orig_edges = box.num_edges();
|
||
auto orig_vertices = box.num_vertices();
|
||
|
||
auto moved = box.transformed(core::translate({10.0, 0, 0}));
|
||
|
||
EXPECT_EQ(moved.num_faces(), orig_faces);
|
||
EXPECT_EQ(moved.num_edges(), orig_edges);
|
||
EXPECT_EQ(moved.num_vertices(), orig_vertices);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, TransformInPlace) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
auto orig_min = box.bounds().min();
|
||
|
||
box.transform(core::translate({50.0, 0.0, 0.0}));
|
||
|
||
auto new_min = box.bounds().min();
|
||
EXPECT_NEAR(new_min.x() - orig_min.x(), 50.0, 1e-6);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE171, CylinderTransformPreservesDiameter) {
|
||
auto cyl = brep::make_cylinder(5.0, 20.0, 16);
|
||
auto orig_extent = cyl.bounds().max() - cyl.bounds().min();
|
||
|
||
auto moved = cyl.transformed(core::translate({100.0, 100.0, 100.0}));
|
||
auto new_extent = moved.bounds().max() - moved.bounds().min();
|
||
|
||
// 圆柱直径不变
|
||
EXPECT_NEAR(new_extent.x(), orig_extent.x(), 1e-6);
|
||
EXPECT_NEAR(new_extent.y(), orig_extent.y(), 1e-6);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-170: fuse_all / intersect_all / difference_all
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(GeometryCompletion_VDE170, FuseAllEmpty) {
|
||
std::vector<brep::BrepModel> empty;
|
||
auto result = brep::fuse_all(empty);
|
||
EXPECT_EQ(result.num_faces(), 0u);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, FuseAllSingleReturnsSame) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
auto result = brep::fuse_all({box});
|
||
|
||
EXPECT_EQ(result.num_faces(), box.num_faces());
|
||
EXPECT_EQ(result.num_vertices(), box.num_vertices());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, FuseAllOverlappingBoxesLargerBBox) {
|
||
auto box1 = brep::make_box(10.0, 10.0, 10.0);
|
||
auto box2 = brep::make_box(10.0, 10.0, 10.0, core::Point3D(5.0, 0, 0));
|
||
|
||
auto fused = brep::fuse_all({box1, box2});
|
||
|
||
ASSERT_GT(fused.num_faces(), 0u);
|
||
|
||
auto bb = fused.bounds();
|
||
auto extent = bb.max() - bb.min();
|
||
|
||
// 两个 10mm box 偏移 5mm → 合并后 X 尺寸 = 15mm
|
||
EXPECT_NEAR(extent.x(), 15.0, 1.0);
|
||
EXPECT_NEAR(extent.y(), 10.0, 1.0);
|
||
EXPECT_NEAR(extent.z(), 10.0, 1.0);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, DifferenceAllProducesValidResult) {
|
||
auto target = brep::make_box(20.0, 20.0, 20.0);
|
||
auto tool1 = brep::make_cylinder(2.0, 25.0, 16, core::Point3D(-5.0, 0, 0));
|
||
auto tool2 = brep::make_cylinder(2.0, 25.0, 16, core::Point3D(5.0, 0, 0));
|
||
|
||
auto result = brep::difference_all(target, {tool1, tool2});
|
||
|
||
// 差集后应产生有效实体(面数 > 0)
|
||
ASSERT_GT(result.num_faces(), 0u);
|
||
|
||
// 验证包围盒尺寸未改变(减去孔不影响外轮廓)
|
||
auto orig_bb = target.bounds();
|
||
auto new_bb = result.bounds();
|
||
auto orig_ext = orig_bb.max() - orig_bb.min();
|
||
auto new_ext = new_bb.max() - new_bb.min();
|
||
EXPECT_NEAR(new_ext.x(), orig_ext.x(), 1.0);
|
||
EXPECT_NEAR(new_ext.y(), orig_ext.y(), 1.0);
|
||
EXPECT_NEAR(new_ext.z(), orig_ext.z(), 1.0);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, IntersectAllNoOverlapReturnsEmpty) {
|
||
auto box1 = brep::make_box(5.0, 5.0, 5.0);
|
||
auto box2 = brep::make_box(5.0, 5.0, 5.0, core::Point3D(100.0, 100.0, 100.0));
|
||
|
||
auto result = brep::intersect_all({box1, box2});
|
||
|
||
// AABB 不相交 → 快速返回空模型
|
||
EXPECT_EQ(result.num_faces(), 0u);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, IntersectAllOverlappingReturnsValid) {
|
||
auto box1 = brep::make_box(10.0, 10.0, 10.0);
|
||
auto box2 = brep::make_box(10.0, 10.0, 10.0, core::Point3D(2.0, 0, 0));
|
||
|
||
auto result = brep::intersect_all({box1, box2});
|
||
|
||
// 交集应有有效面
|
||
EXPECT_GT(result.num_faces(), 0u);
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE170, DifferenceAllSkipsNonIntersectingTools) {
|
||
auto target = brep::make_box(10.0, 10.0, 10.0);
|
||
// 工具远离目标,AABB 不相交
|
||
auto far_tool = brep::make_box(5.0, 5.0, 5.0, core::Point3D(1000.0, 0, 0));
|
||
|
||
auto result = brep::difference_all(target, {far_tool});
|
||
|
||
// 不相交的工具被跳过 → 结果应等于原始目标
|
||
EXPECT_EQ(result.num_faces(), target.num_faces());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE-167: fillet_edges — 边追踪正确性
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(GeometryCompletion_VDE167, FilletEdgesEmptyReturnsOriginal) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
|
||
auto result = brep::fillet_edges(box, {}, 1.0);
|
||
EXPECT_EQ(result.num_faces(), box.num_faces());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE167, FilletEdgesSizeMismatchReturnsOriginal) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
|
||
auto result = brep::fillet_edges(box, {0, 1, 2}, {1.0, 2.0});
|
||
EXPECT_EQ(result.num_faces(), box.num_faces());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE167, FilletEdgesNegativeRadiusReturnsOriginal) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
|
||
auto result = brep::fillet_edges(box, {0, 1}, {-1.0, -1.0});
|
||
EXPECT_EQ(result.num_faces(), box.num_faces());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE167, FilletEdgesDoesNotCrashWithInvalidIds) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
|
||
// 超出范围的边 ID 应被安全跳过
|
||
auto result = brep::fillet_edges(box, {9999, -1}, {1.0, 1.0});
|
||
EXPECT_EQ(result.num_faces(), box.num_faces());
|
||
}
|
||
|
||
TEST(GeometryCompletion_VDE167, FilletEdgesUniformRadiusCorrectValue) {
|
||
auto box = brep::make_box(10.0, 10.0, 10.0);
|
||
|
||
// 找到 box 的有效边(先检查哪些边有有效的曲线)
|
||
for (int eid = 0; eid < static_cast<int>(box.num_edges()); ++eid) {
|
||
const auto& e = box.edge(eid);
|
||
// 跳过没有曲线的直线边
|
||
if (!e.curve) continue;
|
||
|
||
// 单边 fillet 测试
|
||
try {
|
||
auto result = brep::fillet_edges(box, {eid}, 1.0);
|
||
// 成功的 fillet 应增加面数(添加圆角面)
|
||
if (result.num_faces() > box.num_faces()) {
|
||
SUCCEED() << "Fillet succeeded, faces increased from "
|
||
<< box.num_faces() << " to " << result.num_faces();
|
||
return;
|
||
}
|
||
} catch (const std::exception&) {
|
||
// 某些边类型可能不支持 fillet
|
||
}
|
||
}
|
||
// 如果所有边都失败了,至少验证不崩溃
|
||
SUCCEED() << "No filletable edges found, but no crash";
|
||
}
|