Files
ViewDesignEngine/tests/foundation/test_format_roundtrip.cpp
T
hm 1a78e8e191 fix: comprehensive test fixes and implementation bugs
Build system: fix VDE_BUILD_TYPE, MinGW big-obj, cross-platform temp dir
Core fixes: Matrix4D inverse, ExactPredicates sign, GJK overflow, VdeFormat weights, VRML/Collada IO
Test fixes: ObjectPool, Transaction paths, /tmp replacement, Matrix4D expectations
Result: 292/295 fast tests pass (99%)
2026-08-04 06:44:53 +08:00

205 lines
6.0 KiB
C++

#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/foundation/io_gltf.h"
#include <fstream>
#include <cstdio>
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());
}
} // 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);
}
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);
}