Files
hm 163d0c47e6 feat: P1.x 内核对齐 + P2-1 vde_cae + P2-2 插件扩展点 + VDE-1249 三阻塞修复(1.0.47)
### P1.x 内核对齐(P1 遗留收口)
- 原生 .vde 序列化(serializer/vde_format/vde_text)迁入内核 vde_brep;C API 序列化回迁 vde_kernel_capi;
  PMI/MBD 拆为内核数据模型(vde_brep)+ 3D-PDF 导出(vde_io/pmi_export.cpp),消除 kernel→io 反向依赖;
- make_geometry_host_services() 迁入内核 vde_brep,kernel-only 下 PluginManager+宿主服务可用;
- kernel-only 约 40 个测试 target + 4 个 examples 门控;六业务 OFF 的 2 个测试 target 门控;
- vde_version() 返回 0.5.0 既有缺陷修复;kernel SDK consumer 实测 1.0.47;

### P2-1 vde_cae 官方模块
- include/vde/cae/*、src/cae/*、tests/cae/*;旧路径转发头兼容;VDE_BUILD_CAE 开关;
- vde_io 移除 fea_mesh/fluid_domain;ctest 用例数保持(95 项 CAE 测试);CAE=OFF 构建 937/937;

### P2-2 插件 ABI 1.2:CAE solver / CAM strategy 扩展点
- VdeCaeSolverPlugin/VdeCamStrategyPlugin + Registration 尾部扩展(旧插件兼容);
- PluginManager cae_solvers()/cam_strategies()/solve_cae()/invoke_cam(),加载校验/失败隔离;
- plugins/sample_cae、sample_cam_post、abi11_compat、external/{runner,sample_stl_converter};
- docs/design/vde-service-protocol.md 进程外服务协议草案;test_plugin_extensions 9/9;

### VDE-1249 三阻塞修复
- fix1 共享边采样性能悬崖:sagitta 自适应段数 + 自由曲面证据触发 + fast planar loop 256 + IncrementalMesher 复用 mesh_face_diag;
  box+16 孔 legacy 3.3-3.6s→69.5ms、rebuild 4095→103ms;box+25 孔 5.1s→190.9ms;n42 链 ≤222ms;open=0/面积体积正确;
- fix2 fillet/blend 破损体护栏(fillet_edges_checked:watertight/dangling 护栏 + heal 重试 + 显式失败/诊断;
  静默 no-op success → 显式失败)+ delete_face_with_heal/fill_hole 边环重排(26 面盒 0/26→6/6,box 2/6→6/6);
- fix3 wireframe tight payload:WireframeOptions.include_segment_info + C API VdeWireframeOptions 尾扩展 +
  vde_body_to_wireframe_opt2(旧 ABI 安全);tight≈49-61% full(与 1.0.12 旧风格差 4B/段);

### 工程
- 修复 .gitignore `*.txt` 误伤:`!CMakeLists.txt`,把 plugins/**、tools/、tests/plugin、tests/cae 等漏入库的
  CMakeLists.txt 纳入版本库(保证干净 checkout 可构建);
- 版本 1.0.47(CMake/version.h/test_version/README/docs index/module-manual 同步);

验证:全量构建 0 error(334 targets);全量 ctest 7395 注册、7300/7300 PASS(0 failed,24 skipped,194.39s);
kernel-only(-DVDE_BUILD_IO=OFF + BUILD_TESTS=ON)全量构建 0 failed、plugin 30/12 + capi_split 7 全绿;
六业务 OFF 全量测试构建 0 failed;CAE 95/95;插件 ctest 61/61;VDE-1249 repro 前后数字见 CHANGELOG。
2026-10-28 16:32:01 +08:00

632 lines
24 KiB
C++
Raw Permalink 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/cae/fea_mesh.h"
#include "vde/brep/modeling.h"
#include <algorithm>
#include <cmath>
#include <map>
using namespace vde::mesh;
using namespace vde::brep;
using namespace vde::core;
namespace {
// 由 8 个节点计算六面体的轴对齐包围盒体积(对结构化/轴对齐单元即为单元体积)
double hex_aabb_volume(const FEAMesh& m, const std::vector<int>& e) {
double mnx = 1e300, mxx = -1e300;
double mny = 1e300, mxy = -1e300;
double mnz = 1e300, mxz = -1e300;
for (int vi : e) {
const auto& p = m.vertices[static_cast<size_t>(vi)];
mnx = std::min(mnx, p.x()); mxx = std::max(mxx, p.x());
mny = std::min(mny, p.y()); mxy = std::max(mxy, p.y());
mnz = std::min(mnz, p.z()); mxz = std::max(mxz, p.z());
}
return (mxx - mnx) * (mxy - mny) * (mxz - mnz);
}
} // namespace
// ═══════════════════════════════════════════════════════════
// 1. mapped_hex_mesh — 映射法六面体测试
// ═══════════════════════════════════════════════════════════
TEST(MappedHexTest, BoxMappedMesh_NonEmpty) {
auto box = make_box(2.0, 2.0, 2.0);
FaceRegion src;
src.face_indices = {0};
src.name = "bottom";
FaceRegion tgt;
tgt.face_indices = {1};
tgt.name = "top";
auto mesh = mapped_hex_mesh(box, src, tgt, 3);
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
}
TEST(MappedHexTest, BoxMappedMesh_ValidConnectivity) {
auto box = make_box(1.0, 1.0, 1.0);
FaceRegion src;
src.face_indices = {0};
FaceRegion tgt;
tgt.face_indices = {1};
auto mesh = mapped_hex_mesh(box, src, tgt, 2);
for (size_t ei = 0; ei < mesh.num_elements(); ++ei) {
auto& e = mesh.elements[ei];
EXPECT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
EXPECT_LT(static_cast<size_t>(vi), mesh.num_vertices());
}
}
}
TEST(MappedHexTest, BoxMappedMesh_LayerCount) {
auto box = make_box(1.0, 1.0, 1.0);
FaceRegion src;
src.face_indices = {0};
FaceRegion tgt;
tgt.face_indices = {1};
int layers = 4;
auto mesh = mapped_hex_mesh(box, src, tgt, layers);
// 顶点数 = 源面顶点 × (layers + 1)
EXPECT_GT(mesh.num_vertices(), 0u);
}
// ═══════════════════════════════════════════════════════════
// 2. submapped_hex_mesh — 子映射法六面体测试
// ═══════════════════════════════════════════════════════════
TEST(SubmappedHexTest, BoxSubmappedMesh_NonEmpty) {
auto box = make_box(1.0, 1.0, 1.0);
auto mesh = submapped_hex_mesh(box);
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
EXPECT_GT(mesh.num_vertices(), 0u);
EXPECT_GT(mesh.num_elements(), 0u);
}
TEST(SubmappedHexTest, BoxSubmappedMesh_ValidConnectivity) {
auto box = make_box(2.0, 1.0, 1.0);
auto mesh = submapped_hex_mesh(box);
for (size_t ei = 0; ei < mesh.num_elements(); ++ei) {
auto& e = mesh.elements[ei];
EXPECT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
EXPECT_LT(static_cast<size_t>(vi), mesh.num_vertices());
}
}
}
TEST(SubmappedHexTest, SphereSubmappedMesh_NonTrivial) {
auto sphere = make_sphere(1.0);
auto mesh = submapped_hex_mesh(sphere);
EXPECT_GT(mesh.num_vertices(), 0u);
EXPECT_GT(mesh.num_elements(), 0u);
}
// ═══════════════════════════════════════════════════════════
// 3. multi_block_hex_mesh — 多块结构化六面体测试
// ═══════════════════════════════════════════════════════════
TEST(MultiBlockHexTest, EmptyBlocks_FallbackGrid) {
auto box = make_box(2.0, 2.0, 2.0);
// blocks为空时回退到4×4×4均匀栅格
std::vector<BlockRegion> blocks;
auto mesh = multi_block_hex_mesh(box, blocks);
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
EXPECT_GT(mesh.num_vertices(), 0u);
EXPECT_GT(mesh.num_elements(), 0u);
// 4×4×4 grid → 4^3=64 elements
EXPECT_GE(mesh.num_elements(), 64u);
}
TEST(MultiBlockHexTest, SingleBlock_TFILinear) {
auto box = make_box(1.0, 1.0, 1.0);
BlockRegion block;
block.corner_vertices = {
Point3D(0,0,0), Point3D(1,0,0), Point3D(1,1,0), Point3D(0,1,0),
Point3D(0,0,1), Point3D(1,0,1), Point3D(1,1,1), Point3D(0,1,1)
};
block.n_i = 2;
block.n_j = 2;
block.n_k = 2;
std::vector<BlockRegion> blocks = {block};
auto mesh = multi_block_hex_mesh(box, blocks);
EXPECT_GT(mesh.num_vertices(), 0u);
EXPECT_GT(mesh.num_elements(), 0u);
}
TEST(MultiBlockHexTest, MultiBlocks_ValidConnectivity) {
auto box = make_box(2.0, 2.0, 2.0);
BlockRegion b1;
b1.corner_vertices = {
Point3D(0,0,0), Point3D(1,0,0), Point3D(1,1,0), Point3D(0,1,0),
Point3D(0,0,1), Point3D(1,0,1), Point3D(1,1,1), Point3D(0,1,1)
};
b1.n_i = 2; b1.n_j = 2; b1.n_k = 2;
BlockRegion b2;
b2.corner_vertices = {
Point3D(1,0,0), Point3D(2,0,0), Point3D(2,1,0), Point3D(1,1,0),
Point3D(1,0,1), Point3D(2,0,1), Point3D(2,1,1), Point3D(1,1,1)
};
b2.n_i = 2; b2.n_j = 2; b2.n_k = 2;
std::vector<BlockRegion> blocks = {b1, b2};
auto mesh = multi_block_hex_mesh(box, blocks);
for (size_t ei = 0; ei < mesh.num_elements(); ++ei) {
auto& e = mesh.elements[ei];
EXPECT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
EXPECT_LT(static_cast<size_t>(vi), mesh.num_vertices());
}
}
}
// ═══════════════════════════════════════════════════════════
// 4. hex_quality_optimization — 六面体质量优化 + untangling
// ═══════════════════════════════════════════════════════════
TEST(HexQualityOptTest, UnitCube_QualityOptimization) {
// 创建单位立方体六面体网格
FEAMesh mesh;
mesh.element_type = FEAElementType::Hex8;
mesh.vertices = {
{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}
};
mesh.elements.push_back({0,1,2,3,4,5,6,7});
mesh.material_ids.push_back(0);
HexOptimizationParams params;
params.untangle = true;
params.laplacian_smooth = true;
params.max_iterations = 5;
auto result = hex_quality_optimization(mesh, params);
EXPECT_GT(result.initial_quality.total_elements, 0u);
EXPECT_GE(result.final_quality.avg_jacobian, 0.0);
}
TEST(HexQualityOptTest, QualityReport_AfterOptimization) {
auto box = make_box(2.0, 2.0, 2.0);
// 生成结构化六面体网格
std::vector<BlockRegion> blocks;
auto mesh = multi_block_hex_mesh(box, blocks);
auto initial = fea_quality_report(mesh);
EXPECT_GT(initial.total_elements, 0u);
HexOptimizationParams params;
params.laplacian_smooth = true;
params.max_iterations = 10;
auto result = hex_quality_optimization(mesh, params);
EXPECT_EQ(result.final_quality.total_elements, initial.total_elements);
EXPECT_GE(result.final_quality.avg_jacobian, 0.0);
EXPECT_LE(result.final_quality.avg_jacobian, 1.0);
}
TEST(HexQualityOptTest, Untangle_Inverted) {
// 创建一个轻微扭曲的六面体
FEAMesh mesh;
mesh.element_type = FEAElementType::Hex8;
mesh.vertices = {
{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {0.3,0.3,1}, {0,1,1} // 顶点6向内扭曲
};
mesh.elements.push_back({0,1,2,3,4,5,6,7});
mesh.material_ids.push_back(0);
HexOptimizationParams params;
params.untangle = true;
params.laplacian_smooth = true;
params.optimization_based_smooth = false;
params.max_iterations = 10;
auto result = hex_quality_optimization(mesh, params);
EXPECT_GT(result.final_quality.avg_jacobian, 0.0);
}
// ═══════════════════════════════════════════════════════════
// 5. 综合集成测试
// ═══════════════════════════════════════════════════════════
TEST(HexIntegrationTest, MappedThenOptimize) {
auto box = make_box(2.0, 2.0, 2.0);
FaceRegion src;
src.face_indices = {0};
FaceRegion tgt;
tgt.face_indices = {1};
auto mesh = mapped_hex_mesh(box, src, tgt, 3);
ASSERT_FALSE(mesh.elements.empty())
<< "mapped_hex_mesh unexpectedly empty: " << mesh.diagnostic;
ASSERT_EQ(mesh.element_type, FEAElementType::Hex8);
EXPECT_EQ(mesh.num_elements(), 27u); // 3x3x3 结构化六面体
HexOptimizationParams params;
params.max_iterations = 5;
auto result = hex_quality_optimization(mesh, params);
EXPECT_EQ(result.final_quality.total_elements, mesh.num_elements());
}
TEST(HexIntegrationTest, SubmappedThenOptimize) {
auto box = make_box(1.0, 1.0, 1.0);
auto mesh = submapped_hex_mesh(box);
HexOptimizationParams params;
params.laplacian_smooth = true;
params.max_iterations = 5;
auto result = hex_quality_optimization(mesh, params);
EXPECT_EQ(result.final_quality.total_elements, mesh.num_elements());
}
// ═══════════════════════════════════════════════════════════
// 6. submapped_hex_mesh — 物理坐标 / 体积(回归 ±1e18 缺陷)
// ═══════════════════════════════════════════════════════════
TEST(SubmappedHexTest, BoxSubmappedMesh_PhysicalCoordsAndVolume) {
auto body = make_box(10.0, 4.0, 6.0);
auto mesh = submapped_hex_mesh(body);
ASSERT_FALSE(mesh.vertices.empty());
ASSERT_FALSE(mesh.elements.empty());
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
// 以实体 tessellation 的包围盒作为参考(box 默认以原点为中心)
auto surface = body.to_mesh(0.01);
double mnx = 1e300, mxx = -1e300;
double mny = 1e300, mxy = -1e300;
double mnz = 1e300, mxz = -1e300;
for (size_t i = 0; i < surface.num_vertices(); ++i) {
const auto& p = surface.vertex(i);
mnx = std::min(mnx, p.x()); mxx = std::max(mxx, p.x());
mny = std::min(mny, p.y()); mxy = std::max(mxy, p.y());
mnz = std::min(mnz, p.z()); mxz = std::max(mxz, p.z());
}
ASSERT_GT(mxx - mnx, 0.0);
ASSERT_GT(mxy - mny, 0.0);
ASSERT_GT(mxz - mnz, 0.0);
// 所有节点必须落在包围盒内(容差 1e-6)——捕获 ±1e18 垃圾坐标
const double eps = 1e-6;
for (const auto& p : mesh.vertices) {
EXPECT_GE(p.x(), mnx - eps); EXPECT_LE(p.x(), mxx + eps);
EXPECT_GE(p.y(), mny - eps); EXPECT_LE(p.y(), mxy + eps);
EXPECT_GE(p.z(), mnz - eps); EXPECT_LE(p.z(), mxz + eps);
}
// 连通性有效 + 单元体积之和 = 包围盒体积
const double box_vol = (mxx - mnx) * (mxy - mny) * (mxz - mnz);
double sum_vol = 0.0;
for (const auto& e : mesh.elements) {
ASSERT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
ASSERT_LT(static_cast<size_t>(vi), mesh.vertices.size());
}
sum_vol += hex_aabb_volume(mesh, e);
}
EXPECT_NEAR(sum_vol, box_vol, 1e-6 * box_vol);
}
// ═══════════════════════════════════════════════════════════
// 7. hex_subdivision — 2×2×2 细分
// ═══════════════════════════════════════════════════════════
TEST(HexSubdivisionTest, SingleHex_EightChildrenVolumePreserved) {
FEAMesh mesh;
mesh.element_type = FEAElementType::Hex8;
mesh.vertices = {
{0,0,0}, {2,0,0}, {2,1,0}, {0,1,0},
{0,0,3}, {2,0,3}, {2,1,3}, {0,1,3}
};
mesh.elements.push_back({0,1,2,3,4,5,6,7});
mesh.material_ids.push_back(7);
CatmullClarkParams params; // subdivisions = 1
auto out = hex_subdivision(mesh, params);
EXPECT_EQ(out.element_type, FEAElementType::Hex8);
ASSERT_EQ(out.elements.size(), 8u);
// 单个 Hex8 → 3×3×3 = 27 个节点(8 角 + 12 边中点 + 6 面心 + 1 体心)
EXPECT_EQ(out.vertices.size(), 27u);
// 所有新节点物理且落在原始 AABB(含 1e-6 容差)内
for (const auto& p : out.vertices) {
EXPECT_TRUE(std::isfinite(p.x()) && std::isfinite(p.y()) && std::isfinite(p.z()));
EXPECT_GE(p.x(), -1e-6); EXPECT_LE(p.x(), 2.0 + 1e-6);
EXPECT_GE(p.y(), -1e-6); EXPECT_LE(p.y(), 1.0 + 1e-6);
EXPECT_GE(p.z(), -1e-6); EXPECT_LE(p.z(), 3.0 + 1e-6);
}
// 子体积之和 = 父体积 (2×1×3 = 6),精确到 1e-9 相对
double sum_vol = 0.0;
for (const auto& e : out.elements) {
ASSERT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
ASSERT_LT(static_cast<size_t>(vi), out.vertices.size());
}
sum_vol += hex_aabb_volume(out, e);
}
EXPECT_NEAR(sum_vol, 6.0, 1e-9 * 6.0);
// 材料 ID 继承
ASSERT_EQ(out.material_ids.size(), 8u);
for (int m : out.material_ids) EXPECT_EQ(m, 7);
}
TEST(HexSubdivisionTest, TwoElementBlock_SharedInterfaceNodesDeduped) {
FEAMesh mesh;
mesh.element_type = FEAElementType::Hex8;
// 2×1×1 单元块:两个单位立方体沿 X 相邻,共享 x=1 界面
mesh.vertices = {
// hex A: [0,1]×[0,1]×[0,1]
{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0},
{0,0,1}, {1,0,1}, {1,1,1}, {0,1,1},
// hex B: [1,2]×[0,1]×[0,1]
{1,0,0}, {2,0,0}, {2,1,0}, {1,1,0},
{1,0,1}, {2,0,1}, {2,1,1}, {1,1,1}
};
mesh.elements.push_back({0,1,2,3,4,5,6,7});
mesh.elements.push_back({8,9,10,11,12,13,14,15});
mesh.material_ids = {1, 2};
auto out = hex_subdivision(mesh);
// 单元数 8×
EXPECT_EQ(out.elements.size(), 16u);
// 去重后节点数 = 5×3×3 = 45(若不去重则为 2×27 = 54)
EXPECT_EQ(out.vertices.size(), 45u);
EXPECT_LT(out.vertices.size(), 54u);
// 体积守恒(两个单位立方体 = 2),精确到 1e-9 相对
double sum_vol = 0.0;
for (const auto& e : out.elements) {
ASSERT_EQ(e.size(), 8u);
for (int vi : e) {
EXPECT_GE(vi, 0);
ASSERT_LT(static_cast<size_t>(vi), out.vertices.size());
}
sum_vol += hex_aabb_volume(out, e);
}
EXPECT_NEAR(sum_vol, 2.0, 1e-9 * 2.0);
// 材质继承:每个父单元生成 8 个子单元
ASSERT_EQ(out.material_ids.size(), 16u);
for (int k = 0; k < 8; ++k) EXPECT_EQ(out.material_ids[k], 1);
for (int k = 8; k < 16; ++k) EXPECT_EQ(out.material_ids[k], 2);
}
TEST(HexSubdivisionTest, NonHexInput_ReturnsEmptyHonestly) {
FEAMesh mesh;
mesh.element_type = FEAElementType::Tet4;
mesh.vertices = {{0,0,0}, {1,0,0}, {0,1,0}, {0,0,1}};
mesh.elements.push_back({0,1,2,3});
auto out = hex_subdivision(mesh);
EXPECT_EQ(out.element_type, FEAElementType::Hex8);
EXPECT_TRUE(out.elements.empty());
EXPECT_TRUE(out.vertices.empty());
}
// ═══════════════════════════════════════════════════════════
// Phase5-Hex:B-Rep 面 → 结构化四边形 → 映射六面体(unskip)
// ═══════════════════════════════════════════════════════════
namespace {
using namespace vde::mesh;
double hex_total_volume(const FEAMesh& mesh) {
// 散度定理:闭合六面体网格体积 = Σ 边界三角形 (a·(b×c))/6
double vol = 0.0;
for (const auto& e : mesh.elements) {
static const int faces[6][4] = {
{0, 3, 2, 1}, {4, 5, 6, 7}, {0, 1, 5, 4},
{1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7}};
for (const auto& f : faces) {
const Point3D& a = mesh.vertices[static_cast<size_t>(e[static_cast<size_t>(f[0])])];
const Point3D& b = mesh.vertices[static_cast<size_t>(e[static_cast<size_t>(f[1])])];
const Point3D& c = mesh.vertices[static_cast<size_t>(e[static_cast<size_t>(f[2])])];
const Point3D& d = mesh.vertices[static_cast<size_t>(e[static_cast<size_t>(f[3])])];
auto tri = [&](const Point3D& p, const Point3D& q, const Point3D& r) {
vol += p.dot(q.cross(r)) / 6.0;
};
tri(a, b, c);
tri(a, c, d);
}
}
return std::abs(vol);
}
/// 找一对近似反向平行的面(优先接近给定轴)
std::pair<int, int> find_opposite_face_pair(const BrepModel& body,
const Vector3D& axis = Vector3D(0, 0, 1)) {
const int nf = static_cast<int>(body.num_faces());
int plus = -1, minus = -1;
double dot_plus = -2.0, dot_minus = -2.0;
for (int i = 0; i < nf; ++i) {
Vector3D n = body.face_normal(i);
if (n.norm() < 1e-12) continue;
n = n.normalized();
const double d = n.dot(axis);
if (d > dot_plus) { dot_plus = d; plus = i; }
if (-d > dot_minus) { dot_minus = -d; minus = i; }
}
return {plus, minus};
}
double dist_to_box_surface(const Point3D& p, const AABB3D& bb) {
double best = 1e300;
const double xs[2] = {bb.min().x(), bb.max().x()};
const double ys[2] = {bb.min().y(), bb.max().y()};
const double zs[2] = {bb.min().z(), bb.max().z()};
for (double x : xs) best = std::min(best, std::abs(p.x() - x));
for (double y : ys) best = std::min(best, std::abs(p.y() - y));
for (double z : zs) best = std::min(best, std::abs(p.z() - z));
return best;
}
} // namespace
TEST(MappedHexTest, BRepBox_StructuredHexGrid) {
auto box = make_box(2.0, 2.0, 2.0);
auto pair = find_opposite_face_pair(box, Vector3D(0, 0, 1));
ASSERT_GE(pair.first, 0);
ASSERT_GE(pair.second, 0);
FaceRegion src; src.face_indices = {pair.first};
FaceRegion tgt; tgt.face_indices = {pair.second};
auto mesh = mapped_hex_mesh(box, src, tgt, 3);
ASSERT_FALSE(mesh.elements.empty()) << mesh.diagnostic;
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
EXPECT_EQ(mesh.num_elements(), 27u); // 3x3x3 结构网格
EXPECT_EQ(mesh.num_vertices(), 64u); // 4x4x4
EXPECT_TRUE(mesh.diagnostic.empty());
// 全部单元 Jacobian 为正
for (const auto& e : mesh.elements) {
std::vector<Point3D> ev;
ev.reserve(e.size());
for (int vi : e) ev.push_back(mesh.vertices[static_cast<size_t>(vi)]);
EXPECT_GT(element_scaled_jacobian(ev, FEAElementType::Hex8), 0.0);
}
// 节点在 box 内;表面节点(上下盖 + 4 个侧面边界)位于 box 表面
const auto bb = box.bounds();
const double eps = 1e-6;
const int cells = 3; // layers=3 → 每方向 3 单元
const size_t per_layer = mesh.num_vertices() / 4; // (cells+1)^2
for (const auto& p : mesh.vertices) {
EXPECT_GE(p.x(), bb.min().x() - eps); EXPECT_LE(p.x(), bb.max().x() + eps);
EXPECT_GE(p.y(), bb.min().y() - eps); EXPECT_LE(p.y(), bb.max().y() + eps);
EXPECT_GE(p.z(), bb.min().z() - eps); EXPECT_LE(p.z(), bb.max().z() + eps);
}
for (int l = 0; l <= cells; ++l) {
const size_t base = static_cast<size_t>(l) * per_layer;
for (int j = 0; j <= cells; ++j) {
for (int i = 0; i <= cells; ++i) {
const bool surface_node = (l == 0 || l == cells ||
i == 0 || i == cells || j == 0 || j == cells);
if (!surface_node) continue;
EXPECT_LT(dist_to_box_surface(mesh.vertices[base +
static_cast<size_t>(j) * (cells + 1) + i], bb), 1e-6);
}
}
}
// 体积守恒(规则 box 的映射网格应精确铺满 2x2x2)
EXPECT_NEAR(hex_total_volume(mesh), 8.0, 1e-9);
}
TEST(MappedHexTest, BRepCylinder_CapToCapHex) {
for (int segments : {0, 32}) {
auto cyl = make_cylinder(1.0, 2.0, segments);
auto pair = find_opposite_face_pair(cyl, Vector3D(0, 0, 1));
ASSERT_GE(pair.first, 0);
ASSERT_GE(pair.second, 0);
FaceRegion src; src.face_indices = {pair.first};
FaceRegion tgt; tgt.face_indices = {pair.second};
auto mesh = mapped_hex_mesh(cyl, src, tgt, 3);
ASSERT_FALSE(mesh.elements.empty())
<< "segments=" << segments << " diag=" << mesh.diagnostic;
EXPECT_EQ(mesh.element_type, FEAElementType::Hex8);
EXPECT_GT(mesh.num_elements(), 0u);
// Jacobian 全部为正
for (const auto& e : mesh.elements) {
std::vector<Point3D> ev;
ev.reserve(e.size());
for (int vi : e) ev.push_back(mesh.vertices[static_cast<size_t>(vi)]);
EXPECT_GT(element_scaled_jacobian(ev, FEAElementType::Hex8), 0.0);
}
// 节点在圆柱内(|z|<=1、半径<=1);首/末层整层位于两个盖面上
const double eps = 1e-6;
const size_t per_layer = mesh.num_vertices() / 4; // layers=3 => 4 层
const double z_first = mesh.vertices[0].z();
EXPECT_NEAR(std::abs(z_first), 1.0, 1e-9);
for (const auto& p : mesh.vertices) {
EXPECT_LE(std::hypot(p.x(), p.y()), 1.0 + eps);
EXPECT_LE(std::abs(p.z()), 1.0 + eps);
}
for (size_t i = 0; i < per_layer; ++i) {
EXPECT_NEAR(mesh.vertices[i].z(), z_first, 1e-9);
EXPECT_NEAR(mesh.vertices[mesh.vertices.size() - per_layer + i].z(), -z_first, 1e-9);
}
// 精确圆柱(segments=0):边界环节点在半径 1 的侧面上
if (segments == 0) {
const int c = static_cast<int>(std::lround(std::sqrt(static_cast<double>(per_layer)))) - 1;
EXPECT_GE(c, 2);
for (int l = 0; l <= 3; ++l) {
const size_t base = static_cast<size_t>(l) * per_layer;
for (int j = 0; j <= c; ++j) {
for (int i = 0; i <= c; ++i) {
if (i != 0 && i != c && j != 0 && j != c) continue;
const auto& p = mesh.vertices[base + static_cast<size_t>(j) * (c + 1) + i];
EXPECT_NEAR(std::hypot(p.x(), p.y()), 1.0, 1e-5);
}
}
}
}
// 体积合理:πr²h = 2π,粗网格应低于解析值但覆盖大部分
const double vol = hex_total_volume(mesh);
EXPECT_GT(vol, 0.70 * M_PI * 2.0);
EXPECT_LE(vol, 1.01 * M_PI * 2.0);
}
}
TEST(MappedHexTest, UnsupportedTopology_Diagnostic) {
// 球面为自由曲面(非平面面)→ 返回空且给出明确诊断
auto sphere = make_sphere(1.0, 16, 8);
FaceRegion src; src.face_indices = {0};
FaceRegion tgt; tgt.face_indices = {1};
auto mesh = mapped_hex_mesh(sphere, src, tgt, 2);
EXPECT_TRUE(mesh.elements.empty());
EXPECT_FALSE(mesh.diagnostic.empty());
EXPECT_NE(mesh.diagnostic.find("mapped_hex_mesh"), std::string::npos);
// 越界/非法面索引
auto box = make_box(1.0, 1.0, 1.0);
FaceRegion bad; bad.face_indices = {999};
auto mesh2 = mapped_hex_mesh(box, bad, bad, 2);
EXPECT_TRUE(mesh2.elements.empty());
EXPECT_FALSE(mesh2.diagnostic.empty());
}