Files
ViewDesignEngine/tests/foundation/test_format_roundtrip.cpp
T
hm 849ce5e4ff feat(brep): STEP 曲面集(CBS)/未裁剪曲面/镶嵌面导入覆盖 + conic trim 口径修复
- CURVE_BOUNDED_SURFACE 开放薄壳导入:按 representation group 建 sheet body;
  环曲线端点按 placement+半径解析求值(不依赖 NURBS 非整象限估值)、相对尺度
  链缝合容差、允许 2 段双弧孔环 → occ__wgehaeuse_surface 30/30 groups、
  2817/2823 patches 源面片 1:1 映射(仅 6 个 0.006-0.024mm 文件写精度缝 drop)
- 未裁剪曲面集(GBSR/GEOMETRIC_SET)与 AP242 TESSELLATED 网格表示导入通道,
  不再因无壳/面拓扑静默空开,并输出导入降级报告
- conic trim 弧度/角度惯例自动判别(ensure_conic_trim_convention)与边界
  .T./.F. 方向修正
- 追踪开关 VDE_TRACE_CBS / VDE_REPAIR_DEBUG / VDE_REPAIR_LIST /
  VDE_TIMING_STEP / VDE_IMPORT_DUMP_WARN,清理无条件调试输出
- vde_step_probe 扩展 --diag-only/--mesh-only/--analytic-only/--roundtrip;
  扩充 roundtrip/语料/网格回归测试
2026-09-09 12:36:59 +08:00

383 lines
14 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/modeling.h"
#include "vde/brep/brep.h"
#include "vde/brep/step_export.h"
#include "vde/brep/step_import.h"
#include "vde/brep/iges_export.h"
#include "vde/brep/iges_import.h"
#include "vde/brep/measure.h"
#include "vde/brep/trimmed_surface.h"
#include "vde/foundation/io_gltf.h"
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace vde::brep;
using namespace vde::core;
using namespace vde::foundation;
namespace {
/// Create a simple test box
BrepModel create_test_box() {
return make_box(2, 3, 4);
}
/// Create a test cylinder
BrepModel create_test_cylinder() {
return make_cylinder(1.5, 4, 16);
}
/// Cleanup temp file
void cleanup(const std::string& path) {
std::remove(path.c_str());
}
/// 导出→再导入 STEP,返回再导入的全部体
std::vector<BrepModel> step_roundtrip(const std::vector<BrepModel>& src,
const char* name) {
const std::string path = std::string(VDE_TEST_TEMP_DIR) + name + ".stp";
const bool ok = export_step_file(path, src);
EXPECT_TRUE(ok) << "STEP 导出失败: " << path;
auto bodies = ok ? import_step(path) : std::vector<BrepModel>{};
cleanup(path);
return bodies;
}
/// 相对误差(以 max(|a|,|b|,eps) 归一)
double rel_diff(double a, double b) {
const double denom = std::max({1e-12, std::abs(a), std::abs(b)});
return std::abs(a - b) / denom;
}
double total_volume(const std::vector<BrepModel>& v) {
double s = 0.0;
for (const auto& b : v) s += analytic_volume(b);
return s;
}
double total_area(const std::vector<BrepModel>& v) {
double s = 0.0;
for (const auto& b : v) s += analytic_surface_area(b);
return s;
}
/// 文本中 FACE_BOUND 出现次数(内环即“孔”被正确输出的代理指标)
size_t count_face_bound(const std::string& text) {
size_t n = 0, pos = 0;
while ((pos = text.find("FACE_BOUND", pos)) != std::string::npos) {
++n;
pos += 10;
}
return n;
}
} // anonymous namespace
// ═══════════════════════════════════════════════
// STEP 格式往返测试
// ═══════════════════════════════════════════════
TEST(FormatRoundtripTest, StepBoxRoundtrip) {
auto box = create_test_box();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_box.stp";
bool ok = export_step_file(tmpfile, {box});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_step(tmpfile);
EXPECT_GT(bodies.size(), 0u);
if (!bodies.empty()) {
EXPECT_GT(bodies[0].num_faces(), 0u);
}
}
cleanup(tmpfile);
}
// ═══════════════════════════════════════════════
// STEP 几何保真往返测试(导出→再导入几何等价比对)
// 回归门:确认导出不再丢有理权重 / 抹平内孔 / 错并多体壳
// ═══════════════════════════════════════════════
TEST(FormatRoundtripTest, StepGeometrySphere_RationalWeights) {
// 球面由有理 NURBS patch 构成(权重 ≠ 1)。回归门:
// 1) 导出文本必须写复合有理实体 RATIONAL_B_SPLINE_SURFACE;
// 2) 再导入的面控制网格 + 权重与原模型逐面恒等(几何保真,
// 证明有理权重没有被平铺丢弃/误读为 1)。
// 注:不做 analytic 积分往返门控——导入对“环边界贴合基面参数域
// 边界 + 极点行塌缩”的裁剪面用 3D 环重投影构造 UV 域时会退化为
// 折叠条带(面积≈0、自适应积分数百万次求值不收敛),属导入裁剪
// 重建限制(后续以 SURFACE_CURVE/pcurve 直通解决),非导出缺陷。
auto sphere = make_sphere(5.0);
const std::string text = export_step({sphere});
EXPECT_NE(text.find("RATIONAL_B_SPLINE_SURFACE"), std::string::npos)
<< "有理曲面权重未写入 STEP";
auto re = step_roundtrip({sphere}, "rt_sphere");
ASSERT_FALSE(re.empty());
ASSERT_EQ(re.size(), 1u);
const BrepModel& src = sphere;
const BrepModel& dst = re[0];
ASSERT_EQ(dst.num_faces(), src.num_faces());
ASSERT_EQ(dst.num_surfaces(), src.num_surfaces());
size_t bad = 0;
// 文本导出按 6 位有效数字写数值(误差 ≤~5e-6 相对),容差取 1e-5;
// 若权重丢失/被误读为 1(0.995→1.0)会以 >0.5% 偏差被可靠抓住。
auto nearly = [](double a, double b) {
const double denom = std::max({1.0, std::abs(a), std::abs(b)});
return std::abs(a - b) <= 1e-5 * denom;
};
// 面/曲面在导入后可能按壳重排,做顺序无关的内容匹配(每个源面只能
// 配对一个未用的再导入面)。
std::vector<char> used(dst.num_faces(), 0);
for (size_t fi = 0; fi < src.num_faces(); ++fi) {
const auto& sa = src.face_surface(static_cast<int>(fi));
bool found = false;
for (size_t gi = 0; gi < dst.num_faces() && !found; ++gi) {
if (used[gi]) continue;
const auto& sb = dst.face_surface(static_cast<int>(gi));
if (sa.degree_u() != sb.degree_u() || sa.degree_v() != sb.degree_v() ||
sa.knots_u().size() != sb.knots_u().size() ||
sa.knots_v().size() != sb.knots_v().size())
continue;
bool same = true;
for (size_t k = 0; k < sa.knots_u().size() && same; ++k)
same = nearly(sa.knots_u()[k], sb.knots_u()[k]);
for (size_t k = 0; k < sa.knots_v().size() && same; ++k)
same = nearly(sa.knots_v()[k], sb.knots_v()[k]);
const auto& ca = sa.control_points();
const auto& cb = sb.control_points();
const auto& wa = sa.weights();
const auto& wb = sb.weights();
if (same && (ca.size() != cb.size() || wa.size() != wb.size()))
same = false;
if (same) {
for (size_t i = 0; i < ca.size() && same; ++i) {
if (ca[i].size() != cb[i].size() ||
wa[i].size() != wb[i].size()) {
same = false;
break;
}
for (size_t j = 0; j < ca[i].size() && same; ++j) {
const auto& pa = ca[i][j];
const auto& pb = cb[i][j];
same = nearly(pa.x(), pb.x()) && nearly(pa.y(), pb.y()) &&
nearly(pa.z(), pb.z()) &&
nearly(wa[i][j], wb[i][j]);
}
}
}
if (same) { used[gi] = 1; found = true; }
}
if (!found) ++bad;
}
EXPECT_EQ(bad, 0u)
<< "有理 NURBS 面导出→再导入后控制网格/权重不一致(共 " << bad
<< " 面,总数 " << src.num_faces() << ")";
}
TEST(FormatRoundtripTest, StepGeometryMultiBody) {
// 两个独立实体导出→再导入:体数、体积/面积均应与源一致
auto b1 = make_box(20, 10, 5);
auto b2 = make_box(5, 5, 5);
b2 = b2.transformed(vde::core::translate(vde::core::Vector3D(40, 0, 0)));
auto re = step_roundtrip({b1, b2}, "rt_multibody");
ASSERT_FALSE(re.empty());
// 独立实体各自成 PRODUCT 导出,再导入可拆成多模型;逐个累加验体数。
size_t re_bodies = 0;
for (const auto& m : re) re_bodies += m.num_bodies();
EXPECT_EQ(re_bodies, 2u)
<< "多体被错误合并或丢失(导出模型数=" << re.size() << ")";
const double v0 = total_volume({b1, b2});
const double a0 = total_area({b1, b2});
const double v1 = total_volume(re);
const double a1 = total_area(re);
EXPECT_LT(rel_diff(v0, v1), 1e-4)
<< "multibody volume: src=" << v0 << " reimport=" << v1;
EXPECT_LT(rel_diff(a0, a1), 1e-4)
<< "multibody area: src=" << a0 << " reimport=" << a1;
}
TEST(FormatRoundtripTest, StepGeometryPlateWithHoles) {
// 带孔板(来自 OCC 导出真实件,含 FACE_BOUND 内环):导出→再导入
// 应保留内孔(FACE_BOUND 不被抹平),体积/面积往返一致。
const std::string src_path = std::string(VDE_TEST_DATA_DIR)
+ "/real/step/occ__xr1-tu-plt.stp";
std::ifstream probe(src_path);
if (!probe.good()) GTEST_SKIP() << "语料缺失,跳过: " << src_path;
auto src = import_step(src_path);
ASSERT_FALSE(src.empty());
const std::string text = export_step(src);
const size_t n_bound = count_face_bound(text);
EXPECT_GE(n_bound, 10u)
<< "源模型含内孔(>10 个 FACE_BOUND),导出后内环应保留;实际 FACE_BOUND=" << n_bound;
auto re = step_roundtrip(src, "rt_plate");
ASSERT_EQ(re.size(), src.size());
const double v0 = total_volume(src);
const double a0 = total_area(src);
const double v1 = total_volume(re);
const double a1 = total_area(re);
EXPECT_LT(rel_diff(v0, v1), 1e-3)
<< "plate volume: src=" << v0 << " reimport=" << v1;
EXPECT_LT(rel_diff(a0, a1), 1e-3)
<< "plate area: src=" << a0 << " reimport=" << a1;
}
TEST(FormatRoundtripTest, StepCylinderRoundtrip) {
auto cyl = create_test_cylinder();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_cyl.stp";
bool ok = export_step_file(tmpfile, {cyl});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_step(tmpfile);
EXPECT_GT(bodies.size(), 0u);
if (!bodies.empty()) {
EXPECT_GT(bodies[0].num_faces(), 0u);
}
}
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, StepWithAssembly) {
auto box = create_test_box();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_asm.stp";
bool ok = export_step_file(tmpfile, {box});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_step(tmpfile);
EXPECT_GT(bodies.size(), 0u);
}
cleanup(tmpfile);
}
// ═══════════════════════════════════════════════
// IGES 格式往返测试
// ═══════════════════════════════════════════════
TEST(FormatRoundtripTest, IgesBoxRoundtrip) {
auto box = create_test_box();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_box.igs";
bool ok = export_iges_file(tmpfile, {box});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_iges(tmpfile);
EXPECT_GT(bodies.size(), 0u);
if (!bodies.empty()) {
EXPECT_GT(bodies[0].num_faces(), 0u);
}
}
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, IgesCylinderRoundtrip) {
auto cyl = create_test_cylinder();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_cyl.igs";
bool ok = export_iges_file(tmpfile, {cyl});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_iges(tmpfile);
EXPECT_GT(bodies.size(), 0u);
if (!bodies.empty()) {
EXPECT_GT(bodies[0].num_faces(), 0u);
}
}
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, IgesEmptyFileHandling) {
// Non-existent file should fail gracefully
auto bodies = import_iges(VDE_TEST_TEMP_DIR "nonexistent_test_xyz.igs");
EXPECT_TRUE(bodies.empty());
}
// ═══════════════════════════════════════════════
// glTF 格式导出测试
// ═══════════════════════════════════════════════
TEST(FormatRoundtripTest, GltfBoxExport) {
auto box = create_test_box();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_box.gltf";
// Convert to mesh using BrepModel::to_mesh()
auto mesh = box.to_mesh();
bool ok = write_gltf(tmpfile, mesh);
EXPECT_TRUE(ok);
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, GltfCylinderExport) {
auto cyl = create_test_cylinder();
const char* tmpfile = VDE_TEST_TEMP_DIR "test_cyl.gltf";
auto mesh = cyl.to_mesh();
bool ok = write_gltf(tmpfile, mesh);
EXPECT_TRUE(ok);
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, GltfNonExistentPath) {
vde::mesh::HalfedgeMesh empty_mesh;
// Cannot write to invalid path
bool ok = write_gltf("/invalid/path/gltf/test.gltf", empty_mesh);
EXPECT_FALSE(ok);
}
// ═══════════════════════════════════════════════
// 多格式一致性测试
// ═══════════════════════════════════════════════
TEST(FormatRoundtripTest, MultiFormatStepIges) {
auto box = create_test_box();
const char* stepfile = VDE_TEST_TEMP_DIR "test_multi.stp";
const char* igesfile = VDE_TEST_TEMP_DIR "test_multi.igs";
bool step_ok = export_step_file(stepfile, {box});
bool iges_ok = export_iges_file(igesfile, {box});
EXPECT_TRUE(step_ok);
EXPECT_TRUE(iges_ok);
cleanup(stepfile);
cleanup(igesfile);
}
TEST(FormatRoundtripTest, ComplexShapeRoundtrip) {
// Create a more complex shape (cylinder with fillet)
auto cyl = create_test_cylinder();
auto filtered = fillet(cyl, 0, 0.3);
const char* tmpfile = VDE_TEST_TEMP_DIR "test_complex.stp";
bool ok = export_step_file(tmpfile, {filtered});
EXPECT_TRUE(ok);
cleanup(tmpfile);
}
TEST(FormatRoundtripTest, MinSizeBoxRoundtrip) {
auto box = make_box(0.1, 0.1, 0.1);
const char* tmpfile = VDE_TEST_TEMP_DIR "test_mini.stp";
bool ok = export_step_file(tmpfile, {box});
EXPECT_TRUE(ok);
if (ok) {
auto bodies = import_step(tmpfile);
EXPECT_GT(bodies.size(), 0u);
}
cleanup(tmpfile);
}